fossfreedom
fossfreedom

Reputation: 3073

How to copy the GTK style of a widget and apply it to another?

My current GTK popups look like this - note it takes the dark ambiance colour theme.

pic

In GTK3.8 and later there are GTKMenuButtons - the popup looks like this - note it looks like it uses the button styling cues.

pic

I like this style and I want my application popups to look the same so there is a better look - integration and feel.

I know I can override the background colour of the popup using this snippet of python code:

style = button.get_style_context()
color = style.get_background_color(Gtk.StateFlags.NORMAL)
popup_menu.override_background_color(Gtk.StateFlags.NORMAL, color)

It looks like this if I apply the button background colour.

pic

I've no idea how to apply the button font colour to the popup.

More importantly there is that annoying black border - 1px wide?

Thus to my question - am I attempting this the correct way (overriding theme properties) or can I somehow apply the CSS styling of one widget (the button or the button popup) to the popup so I can mimic the menubutton popup styling?

More information - the GTKMenuButton source gtkmenubutton.c doesnt have any theming controls for the popup, thus I'm at a loss how the menubutton popup gets its theme.

Upvotes: 3

Views: 1129

Answers (2)

fossfreedom
fossfreedom

Reputation: 3073

After further investigation I discovered that the style class of the widget (or container) affects the overall style of embedded objects.

Let me explain further with an example:

Construct a grid and attach the MenuButton containing the popup menu.

Adding the Toolbar StyleClass to the Grid influences all objects in that grid including the popup.

style = grid.get_style_context()
style.add_class(Gtk.STYLE_CLASS_TOOLBAR)

The result is this:

Imgur


from gi.repository import Gtk

class MenuExampleWindow(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, title="Menu Example")

        self.set_default_size(200, 200)

        grid = Gtk.Grid()
        grid.insert_column(0)

        menu = Gtk.Menu()
        mitem1 = Gtk.MenuItem(label = "Item 1")
        mitem2 = Gtk.MenuItem(label = "Item 2")


        menub = Gtk.MenuButton(label='menu')
        menub.set_popup(menu)
        menub.set_align_widget(None)
        menub.show_all()

        menu.append(mitem1)
        menu.append(mitem2)
        menu.attach_to_widget(menub, None)
        menu.show_all()

        style = grid.get_style_context()
        style.add_class(Gtk.STYLE_CLASS_TOOLBAR)


        grid.attach(menub, 0,0,1,1)
        self.add(grid)

window = MenuExampleWindow()       
window.connect("delete-event", Gtk.main_quit)
window.show_all()
Gtk.main()

Upvotes: 1

drahnr
drahnr

Reputation: 6886

Assuming you use gtk3+

gtk_widget_get_style/gtk_widget_get_modifier_style and gtk_widget_set_style/gtk_widget_modify_style should do what you want. Be careful, there is a builtin precedence for which style gets applied, which you can not modify (see DocBook entries for the above functions)

Upvotes: 0

Related Questions