Can you style a GMenu?

,

In my program, I have a GMenu populated with child GMenus. The child GMenus have GMenuItems.
This is what it looks like:
pop-up_menu

Is it possible to style “pop-up” menus like the one there?

I understand it’s possible to style GTK widgets with CSS but that GMenu is not a GTK widget (unlike the older GtkMenu), and trying to flag the GMenus with CSS names or classes as suggested here predictably doesn’t work: invalid cast from 'GMenu' to 'GtkWidget'

(Specifically, what I’m trying to do is remove any rounded corners or transparency around the “pop-up” menu there.)

Hi! The answer is that yes, you can customize the menu shown in the picture.

A GMenu is just a description of a menu structure that can be either exported to the OS shell (e.g for the macOS app menu bar: What’s in the menu bar on Mac? - Apple Support), or simply make a GtkPopoverMenuBar widget out of it.

What you have in the picture is a GtkPopoverMenuBar widget, made out of a GMenu-based description.

As indicated in Gtk.PopoverMenu, you can style the menu popups with that css:

.menu {
  /* ... */
}
1 Like

Thanks!

I was indeed able to style it with CSS (for the most part – it seems resistant to certain types of styling, but that’s a different question):
styled_GMenu

For anyone curious, in my executable’s directory, I made a prototype.css file with:

/* Style menus */
.menu {
    margin: 50px;
}

/* Style menu entries */
.menu * {
    color: white;
    background-color: red;
}

Note: The margin: 50px; is why the menu in the screenshot’s surrounded by a huge black square. Ordinarily, the black area would be transparent, but I have my desktop environment’s compositor disabled. That disables transparency, rendering the transparent area solid black.

Then, in the callback function connected to my GtkApplication's activate signal:

// Apply CSS stylesheet.
GdkDisplay* display = gdk_display_get_default();
GtkCssProvider* css_provider = gtk_css_provider_new();
GFile* css_stylesheet = g_file_new_for_path("./prototype.css");

gtk_css_provider_load_from_file(css_provider,
                                css_stylesheet);
gtk_style_context_add_provider_for_display(display,
                                           GTK_STYLE_PROVIDER(css_provider),
                                           GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);

g_object_unref(css_stylesheet);
g_object_unref(css_provider);

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.