Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

How can I apply CSS to all children of an element, programmatically? #646

Closed
Boxylmer opened this issue Jun 30, 2022 · 1 comment
Closed

Comments

@Boxylmer
Copy link

I currently can style widgets through:

function default_button_style!(widget::Gtk.GtkWidget)
    sc = Gtk.GAccessor.style_context(widget)
    cssdata = "
    #btndefault {
            background-image:none;
            border-radius: 0px;
            border-width: 10px;
            border-style: solid;
            border-color: #000000;
            background-color: #FFFFFF;
            margin: 0.5em;

            font-family: Charcoal;
            font-size: 2em;
            font-weight: bold;
        }

        #btndefault:hover {
            border-radius: 0px;
            border-width: 10px;
            border-style: solid;
            border-color: #FFFFFF;
            background-color: #000000;
            color: #FFFFFF;
            margin: 0.5em;

            font-family: Charcoal;
            font-size: 2em;
            font-weight: bold;
        }
    "
    btndefault = Gtk.GtkCssProvider(data=cssdata)
    spb = GtkStyleProvider(btndefault)
    push!(sc, spb, 600)
    set_gtk_property!(widget, :name, "btndefault")
end

but this requires that I have set an ID and specifically reference that widget. If I have some default css data (font size, color, etc) that I want to set to a widget and all its children, what do I do?

Thanks!

@Boxylmer
Copy link
Author

Boxylmer commented Jul 1, 2022

It's a bit hacky, but I found a way to do it in a pinch

function apply_style_recursively!(widget::Gtk.GtkWidget, style_function::Function)
    style_function(widget)
    for child in widget
        if length(methods(iterate, (typeof(child),))) > 0
            apply_style_recursively!(child, style_function)
        end
    end
end

@Boxylmer Boxylmer closed this as completed Jul 1, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant