12.2. GTK: the Controller window - create_control_win

A complete explanation of creating Gamma/GTK applications is beyond the scope of this tutorial, but it isn't difficult. We will mention a few of the basic concepts here, and you can refer to the Gamma/GTK manual for more details, examples, a tutorial, and reference.

   /*--------------------------------------------------------------------
    * Function:    create_control_win
    * Returns:     A GtkWindow
    * Description: Creates the Controller window.
    *------------------------------------------------------------------*/
   function create_control_win()
   {
     local control_win, title, frame, box1, box2;
     local pbut, mbut, dbut, xbut;
     
     control_win = new (GtkWindow);
     control_win.signal ("destroy", #control_win = nil);
     control_win.title = "Cogent Tools Demo: Controller";
     control_win.set_color(1, GTK_STATE_NORMAL, 0xdddddd);
     control_win.set_uposition(20, 20);
     control_win.border_width = 10;

Here we have created a new GtkWindow, and assigned some of its attributes. New widget instances are created with the new function. You can assign a widget's attributes with method calls, as in the control_win.signal call above, or directly, as is done with control_win.title.

Next we make a vertical box, a title, and a frame; then put the title in the frame, pack the frame in the box, and add the box to the main window.

     box1 = new(GtkVBox);
     title = new (GtkLabel);
     title.set_text("Cogent Tools Demo - Controller");
     title.set_color(1, GTK_STATE_NORMAL, 0xff0000);
     title.height = 25;
   
     frame = new(GtkFrame);
     frame.set_color(1, GTK_STATE_NORMAL, 0xff0000);
     frame.add(title);
     frame.border_width = 5;
     box1.pack_start(frame, TRUE, TRUE, 5);
     control_win.add (box1);

Now we need a GtkHBox to lay out a vertical column of buttons next to a GtkText widget. To organize the buttons, we make a GtkVButtonBox. We have to create the text widget before the buttons, because the control_button function will use it.

     box2 = new(GtkHBox);
     box1.pack_start(box2, TRUE, TRUE, 0);
     box2.show();
   
     box3 = new (GtkVButtonBox);
     box2.pack_start(box3, TRUE, TRUE, 0);
     box3.border_width = 5;
     box3.show();
   
     text = new(GtkText);
     text.set_editable(FALSE);
     text.width = 200;
     text.height = 250;
     text.set_word_wrap(TRUE);
     anygui_show_text(text, read_msg("1"), 1);
   
     text2 = new(GtkText);
     text2.set_editable(FALSE);
     text2.height = 130;
     text2.set_word_wrap(TRUE);
     show_names(text2);
   

Now we create the buttons.

     pbut = control_button("PID\nEmulator", 0xbbeecc, "gamma", "emul",
                           "emul.g", nil, nil, box3, text, "2");
     mbut = control_button("Monitor", 0xbbeecc, "gamma", "monitor",
                           "gtkmonitor.g", nil, nil, box3, text, "3");
     dbut = control_button("DataHub\nViewer", 0xeecc99, "xdhview",
                           "pidviewer", "-d", "toolsdemo", "-g 440x320+300+460",
                           box3, text, "4");
     lbut = control_button("Log", 0xbbeecc, "gamma", "log",
                           "gtklog.g", nil, nil, box3, text, "5");
     hbut = control_button("History", 0xbbeecc, "gamma", "history",
                           "gtkhistory.g", nil, nil, box3, text, "6");
   
     hbut = new(GtkButton);
     hbut.label = "Help";
     hbut.signal("clicked", `anyos_help());
     box3.pack_start (hbut, TRUE, TRUE, 0);
     hbut.show();
   
     xbut = new(GtkButton);
     xbut.label = "Exit";
     xbut.signal("clicked", `(@control_win).destroy());
     box3.pack_start (xbut, TRUE, TRUE, 0);
     xbut.show();
   
     box2.pack_start(text, TRUE, TRUE, 5);  
   
     box2 = new(GtkVBox);
     box2.border_width = 5;
     box1.pack_start(box2, TRUE, TRUE, 0);
   
     box3 = new(GtkHBox);
     box3.border_width = 5;
     box2.pack_start(box3, TRUE, TRUE, 0);
   
     label = new(GtkLabel);
     label.set_text("                Process Status                ");
     box3.pack_start(label, TRUE, TRUE, 0);
   
     rawbut = new(GtkToggleButton);
     rawbut.label = "Raw names";
     rawbut.signal("toggled", `toggle_raw(@rawbut, @text2));
     box3.pack_start (rawbut, TRUE, TRUE, 0);
     box2.pack_start(text2, TRUE, TRUE, 0);
   

The control buttons are created with the control_button function. That function, and the way quote operators are used when passing .signal method calls (such as for xbut, the Exit button) are explained in the GTK: Building Control Buttons section. The exit button (xbut) untoggles the other buttons, which closes down those programs, before destroying the window.

     control_win.show_all();
     control_win.reposition(5,5);
     control_win;
   }