static void gtk_application_window_update_shell_shows_app_menu (GtkApplicationWindow *window, GtkSettings *settings) { gboolean shown_by_shell; gboolean shown_by_titlebar; g_object_get (settings, "gtk-shell-shows-app-menu", &shown_by_shell, NULL); shown_by_titlebar = _gtk_window_titlebar_shows_app_menu (GTK_WINDOW (window)); if (shown_by_shell || shown_by_titlebar) { /* the shell shows it, so don't show it locally */ if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) != 0) g_menu_remove (window->priv->app_menu_section, 0); } else { /* the shell does not show it, so make sure we show it */ if (g_menu_model_get_n_items (G_MENU_MODEL (window->priv->app_menu_section)) == 0) { GMenuModel *app_menu = NULL; if (gtk_window_get_application (GTK_WINDOW (window)) != NULL) app_menu = gtk_application_get_app_menu (gtk_window_get_application (GTK_WINDOW (window))); if (app_menu != NULL) { const gchar *app_name; gchar *name; app_name = g_get_application_name (); if (app_name != g_get_prgname ()) { /* the app has set its application name, use it */ name = g_strdup (app_name); } else { /* get the name from .desktop file */ name = gtk_application_window_get_app_desktop_name (); if (name == NULL) name = g_strdup (_("Application")); } g_menu_append_submenu (window->priv->app_menu_section, name, app_menu); g_free (name); } } } }
static void toggle_speed (GtkToggleButton *button, gpointer data) { GMenuModel *model; GActionGroup *group; GSimpleAction *action; gboolean adding; GMenuModel *m; GMenu *submenu; GtkTreeView *tv = data; GtkTreeModel *store; model = g_object_get_data (G_OBJECT (button), "model"); group = g_object_get_data (G_OBJECT (button), "group"); store = gtk_tree_view_get_model (tv); adding = gtk_toggle_button_get_active (button); m = g_menu_model_get_item_link (model, 1, G_MENU_LINK_SECTION); if (adding) { action = g_simple_action_new ("faster", NULL); g_action_map_add_action (G_ACTION_MAP (group), G_ACTION (action)); g_signal_connect (action, "activate", G_CALLBACK (activate_action), NULL); g_object_unref (action); action = g_simple_action_new ("slower", NULL); g_action_map_add_action (G_ACTION_MAP (group), G_ACTION (action)); g_signal_connect (action, "activate", G_CALLBACK (activate_action), NULL); g_object_unref (action); action_list_add (store, "faster"); action_list_add (store, "slower"); submenu = g_menu_new (); g_menu_append (submenu, "Faster", "faster"); g_menu_append (submenu, "Slower", "slower"); g_menu_append_submenu (G_MENU (m), "Speed", G_MENU_MODEL (submenu)); } else { g_action_map_remove_action (G_ACTION_MAP (group), "faster"); g_action_map_remove_action (G_ACTION_MAP (group), "slower"); action_list_remove (store, "faster"); action_list_remove (store, "slower"); g_menu_remove (G_MENU (m), g_menu_model_get_n_items (m) - 1); } }
static void gtk_application_menu_changed_quartz (GObject *object, GParamSpec *pspec, gpointer user_data) { GtkApplication *application = GTK_APPLICATION (object); GMenu *combined; combined = g_menu_new (); g_menu_append_submenu (combined, "Application", gtk_application_get_app_menu (application)); g_menu_append_section (combined, NULL, gtk_application_get_menubar (application)); gtk_quartz_set_main_menu (G_MENU_MODEL (combined), G_ACTION_OBSERVABLE (application->priv->muxer)); }
static void activate (GtkApplication *app, gpointer user_data) { GMenu *submenu; GtkWidget *grid; GMenu *menumodel; GtkWidget *window; GtkWidget *menubutton; GSimpleAction *about_action; window = gtk_application_window_new (app); grid = gtk_grid_new (); gtk_window_set_title (GTK_WINDOW (window), "MenuButton Example"); gtk_window_set_default_size (GTK_WINDOW (window), 600, 400); menubutton = gtk_menu_button_new (); gtk_widget_set_size_request (menubutton, 80, 35); gtk_grid_attach (GTK_GRID (grid), menubutton, 0, 0, 1, 1); gtk_container_add (GTK_CONTAINER (window), grid); menumodel = g_menu_new (); g_menu_append (menumodel, "New", "app.new"); g_menu_append (menumodel, "About", "win.about"); submenu = g_menu_new (); g_menu_append_submenu (menumodel, "Other", G_MENU_MODEL (submenu)); g_menu_append (submenu, "Quit", "app.quit"); gtk_menu_button_set_menu_model (GTK_MENU_BUTTON (menubutton), G_MENU_MODEL (menumodel)); about_action = g_simple_action_new ("about", NULL); g_signal_connect (about_action, "activate", G_CALLBACK (about_callback), GTK_WINDOW (window)); g_action_map_add_action (G_ACTION_MAP (window), G_ACTION (about_action)); gtk_widget_show_all (window); }
/*********************************************************************** Menu */ void bmd_construct_menu (GtkApplication *app, gpointer data) { GtkWidget *menubar; GMenu *menu, *filemenu, *editmenu, *helpmenu, *savemenu, *quitmenu; bmd_widgets *a = (bmd_widgets *) data; // define keyboard accelerators const gchar *open_accels[2] = { "<Ctrl>O", NULL }; const gchar *save_accels[2] = { "<Ctrl>S", NULL }; const gchar *quit_accels[2] = { "<Ctrl>Q", NULL }; const gchar *add_accels[2] = { "<Ctrl>A", NULL }; const gchar *del_accels[2] = { "<Ctrl>D", NULL }; const gchar *find_accels[2] = { "<Ctrl>F", NULL }; const gchar *help_accels[2] = { "F1", NULL }; /* map app entries to actions using the global structure */ g_action_map_add_action_entries (G_ACTION_MAP (app), app_entries, G_N_ELEMENTS (app_entries), (gpointer) a); /* create the menu */ menu = g_menu_new(); /* create the File menu */ filemenu = g_menu_new(); g_menu_append (filemenu, "_Open", "app.open"); savemenu = g_menu_new(); g_menu_append (savemenu, "_Save", "app.save"); g_menu_append (savemenu, "Save As ...", "app.saveAs"); g_menu_append_section (filemenu, NULL, G_MENU_MODEL (savemenu)); quitmenu = g_menu_new(); g_menu_append (quitmenu, "_Quit", "app.quit"); g_menu_append_section (filemenu, NULL, G_MENU_MODEL (quitmenu)); g_menu_insert_submenu (menu, 0, "_File", G_MENU_MODEL (filemenu)); g_object_unref (savemenu); g_object_unref (quitmenu); g_object_unref (filemenu); /* create the Edit menu */ editmenu = g_menu_new(); g_menu_append (editmenu, "_Find", "app.find"); g_menu_append (editmenu, "_Add", "app.add"); g_menu_append (editmenu, "_Delete", "app.delete"); g_menu_append_submenu (menu, "_Edit", G_MENU_MODEL (editmenu)); g_object_unref (editmenu); /* create the Help menu */ helpmenu = g_menu_new(); g_menu_append (helpmenu, "About", "app.about"); g_menu_append (helpmenu, "Help", "app.help"); g_menu_append_submenu (menu, "_Help", G_MENU_MODEL (helpmenu)); g_object_unref (helpmenu); /* create a menu bar and add the above menus */ menubar = gtk_menu_bar_new_from_model (G_MENU_MODEL (menu)); gtk_box_pack_start (GTK_BOX (a->box), menubar, FALSE, FALSE, 0); /* connect keyboard accelerators */ gtk_application_set_accels_for_action (GTK_APPLICATION (app), "app.open", open_accels); gtk_application_set_accels_for_action (GTK_APPLICATION (app), "app.save", save_accels); gtk_application_set_accels_for_action (GTK_APPLICATION (app), "app.quit", quit_accels); gtk_application_set_accels_for_action (GTK_APPLICATION (app), "app.add", add_accels); gtk_application_set_accels_for_action (GTK_APPLICATION (app), "app.delete", del_accels); gtk_application_set_accels_for_action (GTK_APPLICATION (app), "app.find", find_accels); gtk_application_set_accels_for_action (GTK_APPLICATION (app), "app.help", help_accels); }