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
- Octave with dynamic loading enabled. Get it from www.octave.org
- GTK libraries and developement header files
- Octave-GTK and Octave-LibGlade bindings from
http://octave-gtk.sourceforge.net
- Glade UI Builder from http://glade.gnome.org
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.
- Click on the capital A in the Palette window.
- Click in the application's window.
- The entire application's window now says "label1".
- Go to the Properties window, and change the Label to it says
"Hello World" instead of "label1". The application window will
change as you type.
- o In case you're wondering, glade has no concept of "accept what I've
done". Everything you do happens as you do it, and there's no
going back. Save early, save often.
- o In the Glade: window, hit the Save button or File/Save.
You have now created the glade file. Mine is called octaveglade.glade.
Now to create the Octave program which activates the file.
- o Create the following Octave program:
#! /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.
- Go back to the Glade: window.
- Edit the options: File/Project Options...
- Change the project name to octaveglade1.
- Right-click on the Label widget to bring up the menu.
- Select "Cut" from the menu. The label will disappear.
- From the Palette window, select the Vertical Box. It's icon looks
like three boxes piled on top of each other.
- o Click on the application's window. This will pop up a dialog
asking you how many rows you want. Set it to 2.
- You now have two grayed-out boxes, and vbox1 is in the Properties
window.
- Right-click in the upper box and select Paste.
- label1 now occupies the top entry in vbox1.
- In the Palette, select the button saying "Ok".
- Click in the remaining gray box in the application window.
- Change the label of the button in the Properties window to "Quit"
instead of "button1".
- Change the name of the button in the Properties window to "quit"
instead of "button1".
- In the Properties window, click the Signals tab.
- To the right of the Signal: line is a button with three dots.
Click it to bring up the Select Signal dialog.
- Select the "clicked" signal from this dialog, and hit Ok.
- This will also fill in the Handler: box with
"on_quit_clicked". This is correct, so hit "Add".
- Save. You just created octaveglade1.glade
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
- The only changes are the name of the GladeXML file, and the
addition of the on_quit_clicked handler.
- Let's add another button that changes the text of label1.
- Change the project name to octaveglade2
- Decide where you want the button to be. Right-click on either
"Hello World" or Quit. Pick the vbox1 sub-menu. Select
insert-before or insert-after as you wish. You'll get a new grey
box.
- If you inserted it in the wrong place, right-click and select
"Delete".
- It'll be called button2. You can change either the name or label
if you want, using the Properties window's Widget tab, but I
won't.
- In the Properties Signals tab, add the "clicked" signal. It'll be
button2 again. Add it.
- Save.
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