GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Sat Jul 30, 2016 8:47 pm

All times are UTC




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: How to add menu shortcut in keyboard
PostPosted: Mon Dec 21, 2015 11:13 am 
Offline
GTK+ Geek

Joined: Thu Nov 12, 2015 6:46 am
Posts: 53
Dear Friends,
I am looking for a GTK menu keyboard short cut option . eg : clicking Alt+F select file option , Ctrl+Q for Quit ... I got a simple code for that it work for some option as below:
Code:
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>     
                                                                                                                                                                                   
int main(int argc, char *argv[])                                                               
{                                                                                                                                                                                   
                                                 
  GtkWidget *window;                                             
  GtkWidget *vbox;                                       
                                                                                                                                                                                   
  GtkWidget *menubar;                                     
  GtkWidget *filemenu;                                     
  GtkWidget *file;                       
  GtkWidget *new;                                           
  GtkWidget *open;                                           
  GtkWidget *quit;                                                           
  GtkWidget *sep;                                                     
                                                                                                                                                                                   
  GtkAccelGroup *accel_group = NULL;                                                           

  gtk_init(&argc, &argv);

  window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  gtk_window_set_default_size(GTK_WINDOW(window), 250, 200);
  gtk_window_set_title(GTK_WINDOW(window), "menu");

  vbox = gtk_vbox_new(FALSE, 0);
  gtk_container_add(GTK_CONTAINER(window), vbox);

  // In this code we create a menubar and a menu.
  menubar = gtk_menu_bar_new();
  filemenu = gtk_menu_new();

  accel_group = gtk_accel_group_new();
  gtk_window_add_accel_group(GTK_WINDOW(window), accel_group);

  file = gtk_menu_item_new_with_mnemonic("_File");
  new = gtk_image_menu_item_new_from_stock(GTK_STOCK_NEW, NULL);
  open = gtk_image_menu_item_new_from_stock(GTK_STOCK_OPEN, NULL);
  sep = gtk_separator_menu_item_new();
  quit = gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT, accel_group);

  gtk_widget_add_accelerator(quit, "activate", accel_group, GDK_KEY_q, GDK_CONTROL_MASK, GTK_ACCEL_VISIBLE);

  // The code line below implements a file menu
  gtk_menu_item_set_submenu(GTK_MENU_ITEM(file), filemenu);
  // Here menu items are implemented
  gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), new);
  gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), open);
  gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), sep);
  gtk_menu_shell_append(GTK_MENU_SHELL(filemenu), quit);
  gtk_menu_shell_append(GTK_MENU_SHELL(menubar), file);
  gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, FALSE, 3);

  g_signal_connect_swapped(G_OBJECT(window), "destroy",
        G_CALLBACK(gtk_main_quit), NULL);

  g_signal_connect(G_OBJECT(quit), "activate",
        G_CALLBACK(gtk_main_quit), NULL);

  gtk_widget_show_all(window);

  gtk_main();

  return 0;

}


but when I put that code in my application got error as
Quote:
Gtk-CRITICAL **: gtk_widget_add_accelerator: assertion 'GTK_IS_ACCEL_GROUP (accel_group)' failed


Please advise what I am wrong with ...

Thanks
Anes


Top
 Profile  
 
 Post subject: Re: How to add menu shortcut in keyboard
PostPosted: Tue Dec 22, 2015 5:30 pm 
Offline
GTK+ Geek

Joined: Thu Aug 20, 2009 1:54 pm
Posts: 62
Location: Belgium
Nowadays the accelerators can be specified in GMenu, with GTK+ 3.
A GMenu is usually described in a *.ui file (GtkBuilder file).
See: https://wiki.gnome.org/HowDoI/GMenu

_________________
I'm writing a GLib/GTK+ tutorial for the C language (still incomplete). Comments welcome.


Top
 Profile  
 
 Post subject: Re: How to add menu shortcut in keyboard
PostPosted: Tue Dec 22, 2015 5:42 pm 
Offline
GTK+ Geek

Joined: Thu Aug 20, 2009 1:54 pm
Posts: 62
Location: Belgium
If not already done, I recommend the reading of "Getting Started with GTK+" in the GTK+ API reference, that you can also read with Devhelp (but make sure you have the latest version of the GTK+ docs).

The Getting Started section explains how to add a menu, how to create a GtkApplication, adding GActions, etc.
But the HowDoI series is useful too (plus the document I've written which is linked below in my signature).

_________________
I'm writing a GLib/GTK+ tutorial for the C language (still incomplete). Comments welcome.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group