OiO.lk Blog C# MenuBar from gtk4 builder don't show up
C#

MenuBar from gtk4 builder don't show up


I made my menubar with builder using blueprint-compiler:

using Gtk 4.0;

menu menubar {
  section {
    label: _("File");
    item {
      label: _("_Close");
      action: "win.close";
    }
  }
}

The above produce the following glade file:

<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
  <requires lib="gtk" version="4.0"/>
  <menu id="menubar">
    <section>
      <attribute name="label" translatable="yes">File</attribute>
      <item>
        <attribute name="label" translatable="yes">_Close</attribute>
        <attribute name="action">win.close</attribute>
      </item>
    </section>
  </menu>
</interface>

Later in my code I use this galde for making the main window:

<?xml version="1.0" encoding="UTF-8"?>
<!--
DO NOT EDIT!
This file was @generated by blueprint-compiler. Instead, edit the
corresponding .blp file and regenerate this file with blueprint-compiler.
-->
<interface>
  <requires lib="gtk" version="4.0"/>
  <object class="GtkApplicationWindow" id="window1">
    <property name="show-menubar">true</property>
  </object>
</interface>

And the sources:

void on_startup(GtkApplication *app) {
  const GActionEntry app_entries[] = {
     {"win.close", on_close, NULL, NULL, NULL}
   };
  GtkBuilder *builder = gtk_builder_new_from_resource(PROGRAM_RC_PATH "/menubar.glade");
  GMenuModel *menubar = G_MENU_MODEL(gtk_builder_get_object(builder, "menubar"));
  gtk_application_set_menubar (app, menubar);
  g_object_unref (builder);
  g_action_map_add_action_entries (G_ACTION_MAP (app), app_entries, G_N_ELEMENTS (app_entries), app);
}

void on_activate(GtkApplication *app) {
  GtkBuilder* builder = gtk_builder_new_from_resource(WINDOW_GLADE_FILE);
  GtkWindow *window1 = GTK_WINDOW(gtk_builder_get_object(builder, "window1"));
  GtkIconTheme *icon_theme = gtk_icon_theme_get_for_display (gdk_display_get_default ());
  
  gtk_window_set_application (window1, app);
  gtk_window_set_title (window1, PROGRAM_TITLE);
  gtk_icon_theme_add_resource_path (icon_theme, PROGRAM_RC_PATH);
  gtk_window_set_default_icon_name (PROGRAM_NAME);
    g_object_unref(builder);
  
  const GActionEntry win_entries[] = {
   { "win.close", on_close, NULL, NULL, NULL }
 };
 g_action_map_add_action_entries (G_ACTION_MAP (window1), win_entries, G_N_ELEMENTS (win_entries), window1);
  
  gtk_window_present(window1);
}

int main(int argc, char** argv) {
    GtkApplication *app;
    int status;
    app = gtk_application_new (PROGRAM_ID, G_APPLICATION_DEFAULT_FLAGS);
    g_signal_connect(app, "startup", G_CALLBACK (on_startup), NULL);
  g_signal_connect(app, "activate", G_CALLBACK(on_activate), NULL);
    status = g_application_run(G_APPLICATION(app), argc, argv);
    g_object_unref (app);
    return status;
}

I can see the window, but not menubar, instead a see a 2 warnings: "Don’t know how to handle this item". I’m using this tutorial as example about loaing a xml for a menubar. Any ideas about how to show my menubar?



You need to sign in to view this answers

Exit mobile version