A.26. Scrolled Windows

sw_parent = nil;
sw_float_parent = nil;
sw_destroyed_handler = 0;


function scrolled_windows_delete_cb (scrollwin)
{
  scrollwin.reparent(sw_parent);
  gtk_signal_disconnect(sw_parent, sw_destroyed_handler);
  sw_float_parent = nil;
  sw_parent = nil;
  sw_destroyed_handler = 0;

  FALSE;
}
  

function scrolled_windows_destroy_cb (scrollwin)
{
  sw_float_parent.destroy();
  sw_float_parent = nil;
  sw_parent = nil;
  sw_destroyed_handler = 0;  
}



function scrolled_windows_remove (scrollwin)
{
  if (sw_parent)
    {
      scrollwin.reparent(sw_parent);
      sw_float_parent.destroy();
      gtk_signal_disconnect (sw_parent, sw_destroyed_handler);

      sw_float_parent = nil;
      sw_parent = nil;
      sw_destroyed_handler = 0;
    }
  else
    {
      sw_parent = scrollwin.parent;
      sw_float_parent = new(GtkWindow);
      sw_float_parent.type = GTK_WINDOW_TOPLEVEL;
      sw_float_parent.set_default_size(200, 200);

      scrollwin.reparent(sw_float_parent);
      sw_float_parent.show();
      sw_destroyed_handler = sw_parent.signal("destroy",
						    `scrolled_windows_destroy_cb(@scrollwin));
      sw_float_parent.signal("delete_event", `scrolled_windows_delete_cb(@scrollwin));

    }
}



function create_scrolled_windows ()
{
  local scrolled_window, table, button, i, j;
  
  win_scrolled_windows = new (GtkDialog);
  win_scrolled_windows.signal ("destroy", #win_scrolled_windows = nil);
  win_scrolled_windows.title = "dialog";
  win_scrolled_windows.border_width = 0;
  
  vbox = car(win_scrolled_windows.children());
  separator = car(vbox.children());
  action_area = cadr(vbox.children());
  
  scrolled_window = new(GtkScrolledWindow);
  scrolled_window.border_width = 10;
  scrolled_window.set_policy(GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  vbox.pack_start(scrolled_window, TRUE, TRUE, 0);
  scrolled_window.show();
  
  table = new(GtkTable);
  table.n_rows = 20;
  table.n_columns = 20;
  table.set_row_spacings(10);
  table.set_col_spacings(10);
  scrolled_window.add_with_viewport(table);
  table.set_focus_hadjustment(scrolled_window.get_hadjustment());
  table.set_focus_vadjustment(scrolled_window.get_vadjustment());
  
  for (i = 0; i < 20; i++)
    for (j = 0; j < 20; j++)
      {
	labelstring = string("button \(", i, ",", j, "\)\n");
	button = new(GtkButton);
	button.label = labelstring;
	table.attach_defaults(button, i, i+1, j, j+1);
	button.show();
      }
  
  button = new (GtkButton);
  button.label = "Close";
  button.signal ("clicked", `(@win_scrolled_windows).destroy());
  action_area.pack_start (button, TRUE, TRUE, 0);
  button.can_default = TRUE;
  button.grab_default();
  button.show();
  
  button = new (GtkButton);
  button.label = "Reparent Out";
  button.signal ("clicked", `scrolled_windows_remove(@scrolled_window));
  action_area.pack_start (button, TRUE, TRUE, 0);
  button.can_default = TRUE;
  button.grab_default();
  button.show();
  
  win_scrolled_windows.set_default_size(300, 300);
  
  win_scrolled_windows.show_all();
  win_scrolled_windows;
}

function main ()
{
  local window, win_scrolled_windows;
  TRUE=1;
  FALSE=0;
  window = create_scrolled_windows ();
  window.signal ("destroy", #exit_program(0));
  init_ipc("scrolled_wins", "scrolled_winsq");
  gtk_main ();
}