Linux Headquarters

I've installed Linux… now what???

Introduction
The GtkMenuBar, GtkMenu, and GtkMenuItem widgets are used in conjunction with each other to provide drop-down menu lists. The GtkMenuBar is essentially a horizontal packing box for the menu items. Individual GtkMenu widgets such as File and Edit are then packed inside the menu bar. Then, the GtkMenuItem widgets are packed inside a GtkMenu widget. For example, the GtkMenu widget File contains three GtkMenuItem widgets, New, Open, and Exit.
The GtkWindow widget itself contains a GtkVBox. The GtkMenuBar is added to the uppermost box of the GtkVBox, and the GtkText widget is added below that.
Screenshot

Source Code

/*
*File name: menu.c
*/

#include
#include

/*– This function allows the program to exit properly when the window is closed –*/
gint destroyapp (GtkWidget *widget, gpointer gdata)
{
g_print (“Quitting…\n”);
gtk_main_quit();
return (FALSE);
}

/*– This function allows the program to exit properly when the window is closed –*/
gint ClosingAppWindow (GtkWidget *widget, gpointer gdata)
{
g_print (“Quitting…\n”);
gtk_main_quit();
return (FALSE);
}

int main (int argc, char *argv[])
{
/*– Declare the GTK Widgets used in the program –*/
GtkWidget *window;
GtkWidget *menuFile;
GtkWidget *menuEdit;
GtkWidget *menuHelp;
GtkWidget *menubar;
GtkWidget *menu;
GtkWidget *menuitem;
GtkWidget *vbox;
GtkWidget *text;

gchar *buffer = “Go to the file menu and select Exit to close the application.”;

/*– Initialize GTK –*/
gtk_init (&argc, &argv);

/*– Create the new window –*/
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);

/*– Create the vbox –*/
vbox = gtk_vbox_new(FALSE, 0);

/*– Create a text area –*/
text = gtk_text_new(NULL, NULL);

/*– Set text area to be editable –*/
gtk_text_set_editable(GTK_TEXT (text), TRUE);

/*– Connect the window to the destroyapp function –*/
gtk_signal_connect(GTK_OBJECT(window), “delete_event”, GTK_SIGNAL_FUNC(destroyapp), NULL);

/*– Create the menu bar –*/
menubar = gtk_menu_bar_new();

/*– Add the menubar to the vbox –*/
gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
gtk_widget_show(menubar);

/*– Add the text area to the window –*/
gtk_container_add(GTK_CONTAINER(vbox), text);

/*– Add the vbox to the main window –*/
gtk_container_add(GTK_CONTAINER(window), vbox);

/*—————- Create File menu items ——————*/

menuFile = gtk_menu_item_new_with_label (“File”);
gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuFile);
gtk_widget_show(menuFile);

/*– Create File submenu –*/
menu = gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuFile), menu);

/*– Create New menu item under File submenu –*/
menuitem = gtk_menu_item_new_with_label (“New”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_widget_show (menuitem);

/*– Create Open menu item under File submenu –*/
menuitem = gtk_menu_item_new_with_label (“Open”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_widget_show (menuitem);

/*– Create Exit menu item under File submenu –*/
menuitem = gtk_menu_item_new_with_label (“Exit”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_signal_connect(GTK_OBJECT (menuitem), “activate”, GTK_SIGNAL_FUNC (ClosingAppWindow), NULL);
gtk_widget_show (menuitem);
/*—————- End File menu declarations —————-*/

/*—————- Create Edit menu items ——————–*/

menuEdit = gtk_menu_item_new_with_label (“Edit”);
gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuEdit);
gtk_widget_show(menuEdit);

/*– Create File submenu –*/
menu = gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuEdit), menu);

/*– Create Undo menu item under Edit submenu –*/
menuitem = gtk_menu_item_new_with_label (“Undo”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_widget_show (menuitem);

/*– Create Copy menu item under File submenu –*/
menuitem = gtk_menu_item_new_with_label (“Copy”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_widget_show (menuitem);

/*– Create Cut menu item under File submenu –*/
menuitem = gtk_menu_item_new_with_label (“Cut”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_widget_show (menuitem);
/*—————- End Edit menu declarations —————-*/

/*—————- Start Help menu declarations —————-*/
menuHelp = gtk_menu_item_new_with_label (“Help”);
gtk_menu_bar_append (GTK_MENU_BAR(menubar), menuHelp);
gtk_widget_show(menuHelp);

/*– Create Help submenu –*/
menu = gtk_menu_new();
gtk_menu_item_set_submenu(GTK_MENU_ITEM(menuHelp), menu);

/*– Create About menu item under Help submenu –*/
menuitem = gtk_menu_item_new_with_label (“About”);
gtk_menu_append(GTK_MENU(menu), menuitem);
gtk_widget_show (menuitem);
/*—————- End Help menu declarations —————-*/

/*– Add some text to the window –*/
gtk_text_insert(GTK_TEXT(text), NULL, NULL, NULL, buffer, strlen(buffer));

/*– Set window border to zero so that text area takes up the whole window –*/
gtk_container_border_width (GTK_CONTAINER (window), 0);

/*– Set the window to be 640 x 480 pixels –*/
gtk_window_set_default_size (GTK_WINDOW(window), 640, 200);

/*– Set the window title –*/
gtk_window_set_title(GTK_WINDOW (window), “Text Area”);

/*– Display the widgets –*/
gtk_widget_show(text);
gtk_widget_show(menuitem);
gtk_widget_show(menuFile);
gtk_widget_show(menuEdit);
gtk_widget_show(menuHelp);
gtk_widget_show(vbox);
gtk_widget_show(window);

/*– Start the GTK event loop –*/
gtk_main();

/*– Return 0 if exit is successful –*/
return 0;
}

Compile the Source Code
gcc -Wall -g menu.c -o menu `gtk-config –cflags` `gtk-config –libs`

Execute the Program
./menu

What is Related

Comments are closed.

Subscribe to email feed

  • RSS
  • Delicious
  • Digg
  • Facebook
  • Twitter
  • Linkedin
  • Youtube

Dedicated Linux Serv

Linux is the popular system nowadays, offering all the benefits ...

StarOffice 5.1

Introduction StarOffice 5.1 is a complete office suite with a word ...

AxY FTP

Introduction AxY FTP, formerly known as wxFTP, is a graphical FTP ...

Adobe Acrobat PDF Re

Introduction Many of you are probably familar with Adobe Acrobat Reader ...

Macromedia Shockwave

Introduction Macromedia has developed a version of its popular Shockwave plugin ...

Twitter updates