A.15. List

function list_add(x_list, counter)
{  
  local		item;
  item = gtk_list_item_new_with_label(string("added item ", counter));
  x_list.append_items(list(item));
  x_list.show_all();
}

function create_menuitem(label_string, mode, menu, widget, group, selected)
{
  local menuitem = gtk_radio_menu_item_new_with_label(group, label_string);
  menuitem.signal("activate", `(@widget).set_selection_mode(@mode));
  if (selected)
      menuitem.set_active (TRUE);
  menu.append(menuitem);
  menuitem.show();
  group = menuitem.group ();
  group;
}

function list_remove(x_list)
{  
  local clear_list;

  with child in (x_list.children()) clear_list=tcollect
    child.state == GTK_STATE_SELECTED ? child : nil;

  if (clear_list)
    x_list.remove_items (clear_list);
}

function create_list ()
{
  local	vbox, scrolled_win, my_list, infile, item, hbox,
  button, cbox, label, group;
  
  win_list = new (GtkWindow);
  win_list.signal ("destroy", #win_list = nil);
  win_list.title = "list";
  win_list.border_width = 0;
  win_list.set_usize(355, 425);
  
  vbox = new(GtkVBox);
  win_list.add(vbox);
  
  scrolled_win = new(GtkScrolledWindow);
  scrolled_win.border_width = 5;
  scrolled_win.set_usize(-1, 300);
  vbox.pack_start(scrolled_win, TRUE, TRUE, 0);
  scrolled_win.set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  
  my_list = new(GtkList);
  my_list.set_selection_mode(GTK_SELECTION_EXTENDED);
  scrolled_win.add_with_viewport(my_list);
  my_list.set_focus_vadjustment(scrolled_win.get_vadjustment());
  my_list.set_focus_hadjustment(scrolled_win.get_hadjustment());
  
  if ((infile = open("gtkenums.h", "r")) || (infile = open("../gtkenums.h", "r")))
    {
      local txt, item_list;
      item_list = nil;
      while ((txt = read_line(infile)) != _eof_)
	{
	  item = gtk_list_item_new_with_label(txt);
	  item_list = cons (item, item_list);
	}
      close(infile);
      my_list.append_items(reverse(item_list));
    }
  
  hbox = new(GtkHBox);
  hbox.spacing = 5;
  hbox.border_width = 5;
  vbox.pack_start(hbox, FALSE, TRUE, 0);
  
  button = new(GtkButton);
  button.label = "Insert Row";
  hbox.pack_start(button, TRUE, TRUE, 0);
  count = 0;
  button.signal("clicked", #count++);
  button.signal("clicked", `list_add(@my_list, count));
  
  button = new(GtkButton);
  button.label = "Clear List";
  hbox.pack_start(button, TRUE, TRUE, 0);
  button.signal("clicked", `(@my_list).clear_items(0,-1));
  
  to_remove = list(car(item_list));

  button = new(GtkButton);
  button.label = "Remove Selection";
  hbox.pack_start(button, TRUE, TRUE, 0);
  button.signal("clicked", `list_remove(@my_list));
  
  cbox = new(GtkHBox);
  vbox.pack_start(cbox, FALSE, TRUE, 0);
  
  hbox = new(GtkHBox);
  hbox.spacing = 5;
  hbox.border_width = 5;
  cbox.pack_start(hbox, TRUE, FALSE, 0);
  
  label = new(GtkLabel);
  label.set_text("Selection Mode :");
  hbox.pack_start(label, TRUE, TRUE, 0);
  
  menu = new(GtkMenu);
  group = nil;
  group = create_menuitem("Single", GTK_SELECTION_SINGLE, menu,
			  my_list, group, nil);
  group = create_menuitem("Browse", GTK_SELECTION_BROWSE, menu,
			  my_list, group, nil);
  group = create_menuitem("Multiple", GTK_SELECTION_MULTIPLE, menu,
			  my_list, group, nil);
  group = create_menuitem("Extended", GTK_SELECTION_EXTENDED, menu,
			  my_list, group, t);
  
  optionmenu = new(GtkOptionMenu);
  optionmenu.set_usize(100, 28);
  optionmenu.set_menu(menu);
  optionmenu.set_history(3);
  hbox.pack_start(optionmenu, TRUE, TRUE, 0);
  optionmenu.show();
  
  separator = new(GtkHSeparator);
  vbox.pack_start(separator, FALSE, TRUE, 0);
  
  cbox = new(GtkHBox);
  vbox.pack_start(cbox, FALSE, TRUE, 0);
  
  button = new (GtkButton);
  button.border_width = 10;
  button.label = "close";
  button.signal ("clicked", `(@win_list).destroy());
  cbox.pack_start (button, TRUE, TRUE, 0);
  button.can_default = TRUE;
  button.grab_default();
  
  win_list.show_all();
  win_list;
}

function main ()
{
  local window, win_list;
  TRUE=1;
  FALSE=0;
  window = create_list ();
  window.signal ("destroy", #exit_program(0));
  init_ipc("listcodeg", "listcodegq");
  gtk_main ();
}