Re: Accelerators for GtkNotebook to switch between pages



Oh no sorry. It not working. I had just defined the <Primary>t
accelerator in the activate function of the app.

If I change it to e.g. <Primary>b the accelerator doesn't work.


Am 07.02.2018 um 23:19 schrieb Alexander Koeppe:
Hi infirit,

crazy. I rebuilt the relevant parts in a demo app and there it's working
as expected. *argh*.
For completeness here's what I mean.

The code that creates my GtkNotebook:

      /* notebook */
      notebook = gtk_notebook_new();
      gtk_notebook_set_tab_pos(GTK_NOTEBOOK(notebook), GTK_POS_TOP);
      gtk_notebook_set_scrollable(GTK_NOTEBOOK(notebook), TRUE);
      gtk_container_add(GTK_CONTAINER(frame), notebook);

      /* notebook page 1 */
      label = gtk_label_new("Tab1");
      gtk_widget_show(label);
      page = gtk_frame_new("Content1");
      gtk_widget_show(page);
      gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page, label);

      /* notebook page 2 */
      label = gtk_label_new("Tab2");
      gtk_widget_show(label);
      page = gtk_frame_new("Content2");
      gtk_widget_show(page);
      gtk_notebook_append_page(GTK_NOTEBOOK(notebook), page, label);

      create_tab_menu(notebook);



And this is the content of the create_tab_menu() function to create the
context menu of the tab, that actually contains the action to be
triggered by the accelerator:

    void create_tab_menu(GtkWidget *notebook)
    {
       GtkWidget *menu;
       GtkBuilder *builder;
       GSimpleActionGroup *actiongroup;
       int i;
       struct accel_map {
          char *action;
          const char * const accel[2];
       };

       /* define actions */
       static GActionEntry tab_actions[] = {
          {"test_action", test_cb, NULL, NULL, NULL, {}}
       };

       /* define accelerators */
       static struct accel_map tab_accels[] = {
          {"tab.test_action", {"<Primary>t", NULL}}
       };

       /* define menu structure */
       builder = gtk_builder_new();
       gtk_builder_add_from_string(builder,
             "<interface>"
             "  <menu id='tab-menu'>"
             "    <section>"
             "      <item>"
             "        <attribute name='label'>Test Action</attribute>"
             "        <attribute name='action'>tab.test_action</attribute>"
             "        <attribute
    name='icon'>weather-clear-symbolic</attribute>"
             "      </item>"
             "    </section>"
             "  </menu>"
             "</interface>", -1, NULL);

       /* create actiongroup and map actions */
       actiongroup = g_simple_action_group_new();
       g_action_map_add_action_entries(G_ACTION_MAP(actiongroup),
    tab_actions,
             G_N_ELEMENTS(tab_actions), NULL);

       /* map accelerators to actions */
       for (i = 0; i < G_N_ELEMENTS(tab_accels); i++)
          gtk_application_set_accels_for_action(GTK_APPLICATION(app),
                tab_accels[i].action, tab_accels[i].accel);

       /* create new GtkMenu from GtkBuilder */
       menu =
    gtk_menu_new_from_model(G_MENU_MODEL(gtk_builder_get_object(builder,
    "tab-menu")));

       /* insert action group into menu */
       gtk_widget_insert_action_group(menu, "tab",
    G_ACTION_GROUP(actiongroup));

       /* connect tab menu to right-click handler of notebook */
       g_signal_connect(G_OBJECT(notebook), "button-press-event",
    G_CALLBACK(context_cb), menu);

       g_object_unref(builder);

    }




And for completeness the right-click handler:

    gboolean context_cb(GtkWidget *notebook, GdkEventButton *event,
    gpointer data)
    {
       (void) notebook;

       if (event->button == 3) {
          gtk_menu_popup_at_pointer(GTK_MENU(data), (GdkEvent*)event);
          return TRUE;
       }

       return FALSE;
    }



However, I'm afraid I'm back on my own and just have to find where it
goes wrong in my application.

Thanks
_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]