GTK+ Forums

Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
It is currently Thu Aug 21, 2014 9:59 pm

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Submenu
PostPosted: Fri Sep 28, 2007 11:21 am 
Offline
Familiar Face

Joined: Fri Sep 28, 2007 10:59 am
Posts: 46
Submenu
I want to create a submenu for view containing file1(check item) and file2(check item) . what code should I write to create a submenu for a menu item
Like view?

Can you write this for me .
Code:
#include <gtk/gtk.h>

gint delete_event(GtkWidget *widget,GdkEvent *event,gpointer data)
{
gtk_main_quit();
return FALSE;
}

int main(int argc , char *argv[])
{
gtk_init(&argc,&argv);
GtkWidget *window;
window=gtk_window_new(GTK_WINDOW_TOPLEVEL);
gtk_widget_show(window);

GtkWidget *vbox;
vbox=gtk_vbox_new(FALSE,1);
gtk_widget_show(vbox);
gtk_container_add(GTK_CONTAINER(window),vbox);

GtkWidget *menubar;

menubar=gtk_menu_bar_new();
gtk_widget_set_size_request(GTK_WIDGET(menubar),200,40);
gtk_widget_show(menubar);

GtkWidget *hbox;
hbox=gtk_hbox_new(FALSE,1);

gtk_widget_show(hbox);
gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,FALSE,0);
gtk_box_pack_start_defaults(GTK_BOX(hbox),menubar);
GtkWidget *menu;
menu=gtk_menu_new();
GtkWidget *view;
GtkWidget *quit;
view= gtk_image_menu_item_new_from_stock(GTK_STOCK_MISSING_IMAGE,NULL);
GtkWidget *separator=gtk_separator_menu_item_new();
quit= gtk_image_menu_item_new_from_stock(GTK_STOCK_QUIT,NULL);
gtk_widget_show (view);
gtk_widget_show(separator);
gtk_widget_show (quit);
gtk_menu_shell_append(GTK_MENU_SHELL(menu),view);
gtk_menu_shell_append(GTK_MENU_SHELL(menu),separator);
gtk_menu_shell_append(GTK_MENU_SHELL(menu),quit);
GtkWidget *file;
file= gtk_menu_item_new_with_mnemonic("_File");
gtk_widget_show (file);
gtk_menu_item_set_submenu (GTK_MENU_ITEM (file), menu);
gtk_menu_bar_append (GTK_MENU_BAR (menubar), file);
g_signal_connect(G_OBJECT(window),"delete-event",G_CALLBACK(delete_event),NULL);
g_signal_connect(G_OBJECT(quit),"activate",G_CALLBACK(delete_event),NULL);
gtk_main();
return 0;
}
...


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 29, 2007 9:39 am 
Offline
Familiar Face

Joined: Fri Sep 28, 2007 10:59 am
Posts: 46
Noone can help me? :(


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 29, 2007 3:21 pm 
Offline
Familiar Face

Joined: Fri Sep 28, 2007 10:59 am
Posts: 46
I saw an example like this in Foundations of GTK+ page 328 figure 9-3 . But the code which created submenu for Sources item was not written . if Mr. openldev or other put the complete code of that program here for me I will be really appreciated :D


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 30, 2007 8:15 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
As is stated on page 328 in Foundations of GTK+ Development:

Quote:
Submenus in GTK+ are not created by a separate type of menu item widget but by calling gtk_menu_item_set_submenu().


You're already using them in your code.

Here is a modified version of Andrew's menu code from Chapter 9 (menubars.c) which creates a View menu in the File menu with "Hello!" as a submenu.

Code:
#include <gtk/gtk.h>



int main (int argc,

          char *argv[])

{

  GtkWidget *window, *menubar, *file, *edit, *help, *filemenu, *editmenu, *helpmenu;

  GtkWidget *new, *open, *cut, *copy, *paste, *contents, *about;
  GtkWidget *view, *viewmenu, *hello; // Added by Micah

  GtkAccelGroup *group;



  gtk_init (&argc, &argv);



  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);

  gtk_window_set_title (GTK_WINDOW (window), "Menu Bars");

  gtk_widget_set_size_request (window, 250, -1);

 

  g_signal_connect (G_OBJECT (window), "destroy",

                    G_CALLBACK (gtk_main_quit), NULL);



  group = gtk_accel_group_new ();

  menubar = gtk_menu_bar_new ();

  file = gtk_menu_item_new_with_label ("File");

  edit = gtk_menu_item_new_with_label ("Edit");

  help = gtk_menu_item_new_with_label ("Help");
  view = gtk_menu_item_new_with_label ("View"); // Added by Micah

  filemenu = gtk_menu_new ();

  editmenu = gtk_menu_new ();

  helpmenu = gtk_menu_new ();

  viewmenu = gtk_menu_new (); // Added by Micah
 

  gtk_menu_item_set_submenu (GTK_MENU_ITEM (file), filemenu);

  gtk_menu_item_set_submenu (GTK_MENU_ITEM (edit), editmenu);

  gtk_menu_item_set_submenu (GTK_MENU_ITEM (help), helpmenu);

  gtk_menu_item_set_submenu (GTK_MENU_ITEM (view), viewmenu); // Added by Micah
 

  gtk_menu_shell_append (GTK_MENU_SHELL (menubar), file);

  gtk_menu_shell_append (GTK_MENU_SHELL (menubar), edit);

  gtk_menu_shell_append (GTK_MENU_SHELL (menubar), help);

 

  /* Create the File menu content. */

  new = gtk_image_menu_item_new_from_stock (GTK_STOCK_NEW, group);

  open = gtk_image_menu_item_new_from_stock (GTK_STOCK_OPEN, group);
  gtk_menu_shell_append (GTK_MENU_SHELL (filemenu), new);

  gtk_menu_shell_append (GTK_MENU_SHELL (filemenu), open);

 
  hello = gtk_menu_item_new_with_mnemonic("_Hello!"); // Added by Micah

  gtk_menu_shell_append (GTK_MENU_SHELL (viewmenu), hello); // Added by Micah
  gtk_menu_shell_append (GTK_MENU_SHELL (filemenu), view); // Added by Micah
 

  /* Create the Edit menu content. */

  cut = gtk_image_menu_item_new_from_stock (GTK_STOCK_CUT, group);

  copy = gtk_image_menu_item_new_from_stock (GTK_STOCK_COPY, group);

  paste = gtk_image_menu_item_new_from_stock (GTK_STOCK_PASTE, group);

  gtk_menu_shell_append (GTK_MENU_SHELL (editmenu), cut);

  gtk_menu_shell_append (GTK_MENU_SHELL (editmenu), copy);

  gtk_menu_shell_append (GTK_MENU_SHELL (editmenu), paste);

 

  /* Create the Help menu content. */

  contents = gtk_image_menu_item_new_from_stock (GTK_STOCK_HELP, group);

  about = gtk_image_menu_item_new_from_stock (GTK_STOCK_ABOUT, group);

  gtk_menu_shell_append (GTK_MENU_SHELL (helpmenu), contents);

  gtk_menu_shell_append (GTK_MENU_SHELL (helpmenu), about);

 

  gtk_container_add (GTK_CONTAINER (window), menubar);

  gtk_window_add_accel_group (GTK_WINDOW (window), group);



  gtk_widget_show_all (window);

  gtk_main ();

  return 0;

}

_________________
Micah Carrick - Forum Administrator
http://www.micahcarrick.com


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 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:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group