A.16. Menus

function create_menu(depth, tearoff)
{
  local menu, menuitem, group, tag, i, j;

  if (depth < 1)
    {
      menu = nil;
    }
  else
    {
      menu = new(GtkMenu);
      group = nil;

      if (tearoff == TRUE)
	{
	  menuitem = gtk_tearoff_menu_item_new ();
	  menu.append(menuitem);
	  menuitem.show();
	}

      for (i = 0, j = 1; i < 5; i++, j++)
	{
	  tag = string("item ", depth, " - ", j);
	  menuitem = gtk_radio_menu_item_new_with_label (group, tag);
	  group = menuitem.group ();
	  if (depth % 2 != 0)
	    menuitem.set_show_toggle(TRUE);
	  menu.append(menuitem);
	  menuitem.show();
	  
	  if (i == 3)
	    menuitem.set_sensitive(FALSE);
	  
	  menuitem.set_submenu (create_menu (depth - 1, TRUE));
	}
    }
  menu;
}


function create_menus ()
{
  local box1, box2, button, menu, menubar, menuitem,
  label, optionmenu, separator;
  
  win_menus = new (GtkWindow);
  win_menus.signal ("destroy", #win_menus = nil);
  win_menus.signal ("delete-event", #gtk_true());
  win_menus.title = "menus";
  win_menus.border_width = 0;
  win_menus.set_default_size(180, 100);
  
  accel_group = gtk_accel_group_new();
  accel_group.attach (win_menus);
  
  box1 = new(GtkVBox);
  win_menus.add(box1);
  box1.show();
  
  menubar = new(GtkMenuBar);
  box1.pack_start(menubar, FALSE, TRUE, 0);
  menubar.show();
  
  menu = create_menu(2, TRUE);
  menu.set_accel_group(accel_group);
  
  menuitem = gtk_menu_item_new_with_label ("test\nline2");
  menuitem.set_submenu(menu);
  menubar.append(menuitem);
  menuitem.show();
  
  menuitem = gtk_menu_item_new_with_label ("foo");
  menuitem.set_submenu(create_menu(3, TRUE));
  menubar.append(menuitem);
  menuitem.show();
  
  menuitem = gtk_menu_item_new_with_label ("bar");
  menuitem.set_submenu(create_menu(4, TRUE));
  menuitem.right_justify();
  menubar.append(menuitem);
  menuitem.show();
  
  box2 = gtk_vbox_new (FALSE, 10);
  box2.set_border_width(10);
  box1.pack_start(box2, TRUE, TRUE, 0);
  box2.show();
  
  menu = create_menu(1, FALSE);
  menu.set_accel_group(accel_group);
  
  menuitem = gtk_check_menu_item_new_with_label ("Accelerate Me");
  menuitem.right_justify();
  menu.append(menuitem);
  menuitem.show();
  menuitem.add_accelerator("activate", accel_group, GDK_F1, 0,
						   GTK_ACCEL_VISIBLE | GTK_ACCEL_SIGNAL_VISIBLE);

  menuitem = gtk_check_menu_item_new_with_label ("Accelerator Locked");
  menu.append (menuitem);
  menuitem.show ();
  menuitem.add_accelerator ("activate", accel_group, GDK_F2, 0,
							GTK_ACCEL_VISIBLE | GTK_ACCEL_LOCKED);
  menuitem = gtk_check_menu_item_new_with_label ("Accelerators Frozen");
  menu.append (menuitem);
  menuitem.show ();
  menuitem.add_accelerator ("activate", accel_group, GDK_F2, 0,
							GTK_ACCEL_VISIBLE);
  menuitem.add_accelerator ("activate", accel_group, GDK_F3, 0,
							GTK_ACCEL_VISIBLE);
  menuitem.lock_accelerators ();

  optionmenu = new(GtkOptionMenu);
  optionmenu.set_menu(menu);
  optionmenu.set_history(3);
  box2.pack_start(optionmenu, TRUE, TRUE, 0);
  optionmenu.show();

  separator = new(GtkHSeparator);
  box1.pack_start(separator, FALSE, TRUE, 0);
  separator.show();
  
  box2 = gtk_vbox_new (FALSE, 10);
  box2.set_border_width(10);
  box1.pack_start(box2, FALSE, TRUE, 0);
  box2.show();
  
  button = new (GtkButton);
  button.label = "close";
  button.signal ("clicked", `(@win_menus).destroy());
  box2.pack_start (button, TRUE, TRUE, 0);
  button.can_default = TRUE;
  button.grab_default();
  button.show();
  
  win_menus.show_all();
  win_menus;
}

function main ()
{
  local window, win_menus;
  TRUE=1;
  FALSE=0;
  window = create_menus ();
  window.signal ("destroy", #exit_program(0));
  init_ipc("menus", "menusq");
  gtk_main ();
}