EXAMPLES FOR OCTAVE-GLADE


-The Octave-GTK Team. [Muthiah Annamalai, Dhruvkaran Mehta]
octave-gtk-devel {@} sourceforge {dot} net

This document is an introduction to GUI application development with Octave-LibGlade, and helps you get started using Glade. To use this you will need

before you begin using these examples.
You may contact the authors regarding questions , or more information.

Step-by-step instructions

for a "Hello World" using Octave, gtk, octave-gtk (which Debian calls octave-gtk), and libglade.

Now, the very most important thing to remember as you use glade is that GTK uses a container model. Every grayed-out hole that you see can hold exactly one widget. So, all you can do with the window that you have created is select a widget from the Palette window and drop it into your application's window. If you didn't drop a container (Horizontal Box, Vertical Box, Table, or Fixed Positions) that can hold multiple widgets, you can't add any more widgets.

For our example, we're just displaying "Hello World" (boring yes, but a necessary first step), so we'll insert a label.


#! /usr/bin/octave -q

% * This program is free software.
% * This code is a part of Octave-GTK
% * Code may be used or distributed under GPL.
% * (C) Feb 2005 Muthiah Annamalai, Octave-GTK & Octave-libGlade


xml="";
disp('Hello World example with octave-libglade')


function main()
 
  global xml


  gtk()
  glade();
  gtk_init();

  %load the UI description to build some nice UI automagically.
  xml=glade_xml_new("octaveglade.glade","window","");  

  %get a handle to the main window.
  window=glade_xml_get_widget(xml,"window");

  %connect signal handlers if any.
  glade_xml_signal_autoconnect(xml);

  %show the window	
  gtk_widget_show_all(window);

  gtk_main();
end

main()

Run the program

.
 [dhruv@EarthSimulator]$ chmod +x ./octaveglade.m
 [dhruv@EarthSimulator]$ ./octaveglade.m
 
It will create a tiny window saying "Hello World". Unfortunately, the program swallows sigint, so we can't kill it with a ^C. You can kill it using ^Z and kill %jobnumber.

Let's modify the program so it has a quit button.

Here's octaveglade1.m, which handles the quit button:


#! /usr/bin/octave -q

% * This program is free software.
% * This code is a part of Octave-GTK
% * Code may be used or distributed under GPL.
% * (C) Feb 2005 Muthiah Annamalai, Octave-GTK & Octave-libGlade


xml="";
disp('Hello World with quit button example with octave-libglade')

%callback handler
function on_quit_clicked()
   	gtk_main_quit();
end


function main()
  global xml


  gtk();
  glade();

  gtk_init();

  %load the UI description to build some nice UI automagically.
  xml=glade_xml_new("octaveglade1.glade","window","");  

  %get a handle to the main window.
  window=glade_xml_get_widget(xml,"window");

  %connect signal handlers if any.
  glade_xml_signal_autoconnect(xml);

  %show the window	
  gtk_widget_show_all(window);

  gtk_main();
end
  main();

Run the program.

Output of octaveglade1.m

Here's the octave code for octaveglade2.m
#! /usr/bin/octave -q

% * This program is free software.
% * This code is a part of Octave-GTK
% * Code may be used or distributed under GPL.
% * (C) Feb 2005 Muthiah Annamalai, Octave-GTK & Octave-libGlade


xml="";
disp('Yet Another Hello World Example example with octave-libglade')

%change label callback
function on_button2_clicked()
 global xml;
 gtk_label_set_text(glade_xml_get_widget(xml,"label1"),
	 	             "spam, spam, spam");
end

%callback handler
function on_quit_clicked()
   	gtk_main_quit();
end


function main()
  global xml


  gtk()
  glade();
  gtk_init();

  %load the UI description to build some nice UI automagically.
  xml=glade_xml_new("octaveglade2.glade","window","");  

  %get a handle to the main window.
  window=glade_xml_get_widget(xml,"window");

  %connect signal handlers if any.
  glade_xml_signal_autoconnect(xml);

  %show the window	
  gtk_widget_show_all(window);

  gtk_main();
end
  main();


octaveglade2.m output image.

The only changes are the GladeXML file and the addition of the on_button2_clicked signal handler. Feel free to play around with glade and these test programs. libglade creates the widgets for you, so you can insert widgets without writing any octave code to handle their signals. For example, if you want to put vbox1 into a frame widget, you can do so without changing the Octave code. or you can change the order of widgets in vbox1. or create more vboxes and hboxes, and put the widgets wherever you want. They're identified by name and not position or hierarchy.

Muthu & Dhruv are Octave-GTK developers, and you can contact them, at the mailing lists. You may contact the authors regarding questions , or more information.


Various useful homepages:

This document, can be distributed under terms of GNU GFDL