c - Accelerator not displayed in GtkCheckMenuItem -
i fiddliny around menubar in gtk+3. until had checkmenuitems label , accelerator shortcut displayed:
gtkwidget *create_menubar(void) { gtkwidget *menubar = gtk_menu_bar_new(); const static guint num_keys[] = {gdk_key_1, gdk_key_2, gdk_key_3, gdk_key_4, gdk_key_5, gdk_key_6, gdk_key_7, gdk_key_8, gdk_key_9, gdk_key_0}; // create gtkaccelgroup , add window. gtkaccelgroup *accel_group = gtk_accel_group_new(); gtk_window_add_accel_group(gtk_window(window_main), accel_group); // submenu show colors gtkwidget *colors_mi = gtk_menu_item_new_with_label("colors"); gtkwidget *colors_menu = gtk_menu_new(); gtk_menu_shell_append(gtk_menu_shell(menubar), colors_mi); gtk_menu_item_set_submenu(gtk_menu_item(colors_mi), colors_menu); (int = 0; < num_colors; i++) { char name[10]; sprintf(name, "col %d", i+1); // <<<<<<<<<<<<<<<<< cut mark... gtkwidget *show_color_mi = gtk_check_menu_item_new_with_label(name); // <<<<<<<<<<<<<<<<< cut mark... gtk_check_menu_item_set_active(gtk_check_menu_item(show_color_mi), show_color[i]); if (i < 10) { gtk_widget_add_accelerator(show_color_mi, "activate", accel_group, num_keys[i], gdk_mod1_mask, gtk_accel_visible); } gtk_menu_shell_append(gtk_menu_shell(colors_menu), show_color_mi); g_signal_connect(g_object(show_color_mi), "toggled", g_callback(on_menu_show_colors_toggled), &show_color[i]); } return menubar; }
this works fine far. program displays several sets of data different color each. entries in menu used enable or disable set of data. want add indication set of data drawn in color.
to changed creation of menuitem. create horizontal box, image , label enclosed.
the lines between cut marks (<<<<<<<<<<<<) replaced this:
// <<<<<<<<<<<<<<<<< gtkwidget *show_color_mi = gtk_check_menu_item_new(); gtkwidget *menubox = gtk_box_new(gtk_orientation_horizontal, 2); gtkwidget *image = gtk_image_new(); gtkwidget *menulabel = gtk_label_new(name); cairo_surface_t *surface = cairo_image_surface_create(cairo_format_rgb24, 20, 15); cairo_t *cr = cairo_create(surface); cairo_set_source_rgb(cr, colors[i].r, colors[i].g, colors[i].b); cairo_paint(cr); cairo_destroy(cr); gtk_image_set_from_surface(gtk_image(image), surface); gtk_container_add(gtk_container(show_color_mi), menubox); gtk_box_pack_start(gtk_box(menubox), image, false, false, 0); gtk_box_pack_start(gtk_box(menubox), menulabel, false, false, 0); // <<<<<<<<<<<<<<<<<
now have checkmenuitems contain colored rectangle , same text before change. thing...
the bad thing reason accelerator keys not shown longer.
any suggestions doing wrong there?
edit:
in updated according josé fonte's answer , in general works. in meantime found other strange effect. running program on ubuntu 14.04 machines xfce , works expected. run in ubuntu 16.04 in virtualbox , works fine, too. on ubuntu 14.04 machine in vmware behaves different. colored rectangles missing accelerators shown when use incorrect label type. getting bit weird...
now checked on machine works or not , version of libgtk-3-bin installed:
ubuntu 16.04 (virtualbox) 3.18.9-1ubuntu3.1 ok
raspbian jessie (raspberry pi) 3.14.5-1+deb8u1rpi1rpig ok
ubuntu 14.04, xfce (native) 3.10.8-0ubuntu1.6 ok
ubuntu 14.04, unity (vmware) 3.10.8-0ubuntu1.6 fail
your problem using normal gtklabel instead of gtkaccellabel.
you must change this:
[stuff deleted] ... gtkwidget *menulabel = gtk_accel_label_new(name); gtk_accel_label_set_accel_widget(gtk_accel_label(menulabel), show_color_mi); ... gtk_box_pack_start(gtk_box(menubox), menulabel, true, true, 0);
conclusion: use gtk_accel_label instead of gtk_label, set accel widget should menu item contains , change fill , expand flags on gtk_box_pack_start allow space. should solve problem.
Comments
Post a Comment