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 12:24 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Menu Item doesn't show on top of window
PostPosted: Fri Jul 27, 2007 4:02 pm 
Offline
Familiar Face

Joined: Fri May 18, 2007 2:05 pm
Posts: 7
Hello,

I'm learning GTK+ and wrote this piece of code.
While it compiles and runs the menu item is shown in the middle of the window instead of in the left top corner.

Does anybody see what I'm missing?

Here is the code.

======================================

#include <gtk/gtk.h>

static void destroy(GtkWidget *win, gpointer data);

int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *menu_bar;
GtkWidget *file_menu;
GtkWidget *open_item, *save_item, *quit_item, *file_item;

gtk_init(&argc, &argv);

window = gtk_window_new( GTK_WINDOW_TOPLEVEL);
gtk_widget_set_size_request( window, 275, 300);
gtk_window_set_title( GTK_WINDOW(window), "Testing Menu Widget");

g_signal_connect( GTK_WINDOW(window), "destroy",
G_CALLBACK(destroy), NULL);


file_menu = gtk_menu_new();

open_item = gtk_menu_item_new_with_label( "Open");
save_item = gtk_menu_item_new_with_label( "Save");
quit_item = gtk_menu_item_new_with_label( "Quit");

gtk_menu_shell_append( GTK_MENU_SHELL( file_menu), open_item);
gtk_menu_shell_append( GTK_MENU_SHELL( file_menu), save_item);
gtk_menu_shell_append( GTK_MENU_SHELL( file_menu), quit_item);

gtk_widget_show(open_item);
gtk_widget_show(save_item);
gtk_widget_show(quit_item);

file_item = gtk_menu_item_new_with_label( "File");
gtk_widget_show( file_item);

menu_bar = gtk_menu_bar_new();
gtk_container_add( GTK_CONTAINER( window), menu_bar);
gtk_widget_show(menu_bar);


gtk_menu_item_set_submenu( GTK_MENU_ITEM( file_item), file_menu);
gtk_menu_bar_append( GTK_MENU_BAR( menu_bar), file_item);

gtk_widget_show(window);

gtk_main();

return 0;
}


static void destroy( GtkWidget *win, gpointer data)
{
gtk_main_quit();
}

==========================================


Top
 Profile  
 
 Post subject:
PostPosted: Sat Jul 28, 2007 9:17 pm 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Hey. One of the harder concepts when beginning in GTK+ is "packing". In time, you'll absolutely love it.

Try this instead of adding the menu bar directly to the window:

Code:
menu_bar = gtk_menu_bar_new();
GtkWidget *vbox = gtk_vbox_new(FALSE, 0);
gtk_box_pack_start (GTK_BOX (vbox), menu_bar, FALSE, FALSE, 0);
gtk_container_add( GTK_CONTAINER(window), vbox);

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


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jul 29, 2007 7:37 am 
Offline
Familiar Face

Joined: Fri May 18, 2007 2:05 pm
Posts: 7
Thank you very much for your answer.
Just read the section about packing in GTK+.

I just didn't know that you had to use packing for menu's.

Just thought that a menu would be added to the top of the window by default.

I apologize.


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 31, 2007 6:31 am 
Offline
Never Seen the Sunlight

Joined: Wed Sep 21, 2005 12:07 am
Posts: 563
Location: Portland, OR USA
Do not apologize. Asking these questions is why this forum was started.

And yes, I would make the same assumption when I started. I came from a Windows development background where we had to explicitly tell "controls" where to be positioned if they did not fit into one of the very fundamental layout schemes.

This is what made "packing" and GTK+ somewhat counter-intuitive to what I knew and almost frustrating at first. However, I soon learned that this "packing" thing shaved hours and hours off my development time as I could simply apply a couple of packing properties to my widgets and have my application resize appropriately.

It's somewhat awkward at first--in fact, I still have to refer to the API docs all the time for packing issues--but it really does speed up development time in the long run.

Have fun!

_________________
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:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group