Assumptions
I am writing this with the following situation in mind.
$]libglade-convert usage: libglade-convert [--no-upgrade] [--verbose] oldfile.glade
#! /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('Ohms law example with octave-libglade') function main() global xml gtk() glade() gtk_init() xml=glade_xml_new("ohms.glade","window1",""); %create the widget tree win1=glade_xml_get_widget(xml,"window1"); %get the widget pointer glade_xml_signal_autoconnect(xml); %autoconnect handlers & signals gtk_widget_show_all(win1); gtk_main() end main()Now we add a callback to the button clicked event from Glade, and set the handler to . On doing this, we just need to add this routine into the program.
function calculate_cb() global xml disp('Hello World') i=glade_xml_get_widget(xml,"current") r=glade_xml_get_widget(xml,"resistance") v=glade_xml_get_widget(xml,"voltage") amp=str2num(gtk_entry_get_text(i)) ohm=str2num(gtk_entry_get_text(r)) gtk_entry_set_text(v,num2str(amp*ohm)) end
#include<stdio.h> #include<gtk/gtk.h> #include<glade/glade.h> #include"idea85.h" int main(int argc,char *argv[]) { GladeXML *gx; GtkWidget *w; gtk_init(&argc,&argv); gx=glade_xml_new("idea85.glade",NULL,NULL); g_assert(gx != NULL); glade_xml_signal_autoconnect(gx); w=glade_xml_get_widget(gx,"window1"); g_signal_connect(G_OBJECT(w),"destroy",G_CALLBACK(gtk_main_quit),NULL); gtk_widget_show_all(w); gtk_main(); return 0; } /** gcc -o idea85 -Wall idea85.c `pkg-config gtk+-2.0 --cflags --libs` -I/usr/include/libglade-2.0 -I/usr/include/gnome-xml -I/usr/include/gtk-2.0 -I/usr/X11R6/include -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -L/usr/X11R6/lib -lglade-2.0 -lxml -lz -lXi -lXext -lX11 -lm -lglib-2.0 */
Now that you are aquainted with the idea of making a simple "hello world" program in glade interface designer and using the same with C coding, till the compilation & execution processm lets see a real world example.
First create the GLADE interface design, using glade-2.Otherwise people using the glade1.2 or earlier versions please use the libglade-convert program, to generate the ".glade" file. Save the same as gnu.glade. Then exit the .glade. Then run your glade file must be converted to a proper glade-2.0 file. So use the following commands.
#]libglade-convert gnu.glade > gnu2.glade.
#]mv gnu2.glade gnu.glade
Now note carefully that we have two callbacks in the code
of the gnu.glade file that has been attached to the callback
function on_spin_chaged() & on_spin_activated. Here in each of
these function we use the property of the glade_xml_get_widget
function to get the various widgets we needed during runtime.
Hence these callbacks simply use the functions to read the various
changes in the functions, and respond by changing the values of the
labels present in there.
/* Code for the program "gnu.c". Q: What does this program do ? A: When you click on a spin button, or when its value changes we update the value of the labels. It simply involves the connection to a special callback, for the signal called "value_changed" of the spin button widget. */ #include<stdio.h> #include<gtk/gtk.h> #include<glade/glade.h> void on_spin_activate(GtkSpinButton *b); void on_spin_changed(GtkSpinButton *b); GladeXML *gx; int main(int argc,char *argv[]) { GtkWidget *w; gtk_init(&argc,&argv); gx=glade_xml_new("gnu.glade",NULL,NULL); g_assert(gx != NULL); glade_xml_signal_autoconnect(gx); w=glade_xml_get_widget(gx,"window"); g_signal_connect(G_OBJECT(w),"destroy",G_CALLBACK(gtk_main_quit),NULL); gtk_widget_show_all(w); gtk_main(); return 0; } void on_spin_changed(GtkSpinButton *b) { gint count=0; char buffer[10]; GtkLabel *label=GTK_LABEL(glade_xml_get_widget(gx,"label")); count=gtk_spin_button_get_value_as_int(b); sprintf(buffer,"%3d",count); gtk_label_set_text(label,buffer); printf("changed: %s <=> %d\n",buffer,count); } void on_spin_activate(GtkSpinButton *b) { unsigned char count=0; char buffer[10]; GtkLabel *label=GTK_LABEL(glade_xml_get_widget(gx,"label")); count=gtk_spin_button_get_value_as_int(b); sprintf(buffer,"%d",count); gtk_label_set_text(label,buffer); printf("actiavted: %s <=> %d\n",buffer,count); } /*gcc -o gnu -Wall gnu.c `pkg-config gtk+-2.0 --cflags --libs` -I/usr/include/libglade-2.0 -lxml -lglade-2.0 */
For typical C programs Then use the compilation lines like follows.
gcc -o gnu -Wall gnu.c `pkg-config gtk+-2.0 --cflags --libs` -I/usr/include/libglade-2.0 -lxml -lglade-2.0Then run the program
#! /usr/bin/octave -q gx=0 function on_spin_changed() global gx label=glade_xml_get_widget(gx,"label"); b=glade_xml_get_widget(gx,"spin"); count=gtk_spin_button_get_value_as_int(b); buffer=sprintf("%3d",count); gtk_label_set_text(label,buffer); end function main() global gx gtk() glade() gtk_init() gx=glade_xml_new("gnu.glade","window",""); glade_xml_signal_autoconnect(gx); w=glade_xml_get_widget(gx,"window"); g_signal_connect(w,"destroy","gtk_main_quit"); gtk_widget_show_all(w); gtk_main(); return end main()
To run the program, just type
$octave -q ./gnu.m & and see the program work, running libGlade from Octave. Try the calculator example from the Octave-libGlade example.The file sources are here: