The Menu Bar (C)

Linux tutorials and code.
1 post Page 1 of 1
Contributors
User avatar
mikethedj4
VIP - Site Partner
VIP - Site Partner
Posts: 2592
Joined: Thu Mar 25, 2010 4:36 am

The Menu Bar (C)
mikethedj4
It's very simple you add a menubar and then you add menu items and thoughs are like file, edit, etc: and then after that is menus. Of course you need to declare them, and crap.

The code below is just a regular window with a menubar. It has New, and Close under file, they don't do anything, but this should help you have an understanding of how it works. Everything's pretty self explanatory though.

Full Code:
Code: Select all
#include <gtk/gtk.h>

static const char *titles[] = {"New", "Close"};

int main (int argc, char **argv) {
  GtkWidget *window;
  GtkWidget *vbox;
  GtkWidget *menu_bar;
  GtkWidget *menu_item_file;
  GtkWidget *menu;
  gtk_init (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  vbox = gtk_vbox_new(FALSE, 0);
  menu_bar = gtk_menu_bar_new();
  menu_item_file = gtk_menu_item_new_with_label("File");
  menu = gtk_menu_new();

  gtk_window_set_title (GTK_WINDOW (window), "Simple Menubar");
  gtk_window_set_default_size(GTK_WINDOW (window), 300, 200);
  gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
  g_signal_connect (G_OBJECT (window), "delete-event", gtk_main_quit, NULL);

  for(int i = 0; i < 2; i++) {
	GtkWidget *menu_item = gtk_menu_item_new_with_label(titles[i]);
	gtk_menu_shell_append(GTK_MENU_SHELL(menu), menu_item);}

  gtk_menu_item_set_submenu(GTK_MENU_ITEM(menu_item_file), menu);
  gtk_menu_shell_append(GTK_MENU_SHELL(menu_bar), menu_item_file);
  gtk_box_pack_start(GTK_BOX(vbox), menu_bar, FALSE, FALSE, 0);

  gtk_container_add (GTK_CONTAINER(window), vbox);
  gtk_widget_show_all(window);
  gtk_main();
  return 0;}
The zip below has the application, as well as the source code encase you wanna play around with that, and to compile it you would use this code.
Code: Select all
gcc -std=c99 `pkg-config --cflags --libs gtk+-2.0` source.c -o program
You do not have the required permissions to view the files attached to this post.
1 post Page 1 of 1
Return to “Programming”