Attachment 'octave_gtk_libglade.html'

Download

This page is only for those who know/want-to know learn GTK and are a wee bit interested in exploring the terrain. What I can assure is that though your search may not end here, your results will be arriving faster. Happy Hacking: Last update on 26th February 2005. Contact the author at gnumuthu@users.sf.net.

Using Octave-GTK

(C) Feb 2005 Muthiah Annamalai, Octave-GTK & Octave-libGlade

Assumptions
I am writing this with the following situation in mind.

  1. A person has a glade file which s/he wants to use in a 'C/Octave' program
  2. Im frustrated with [manual/hand]-packing widgets and connections.
  3. You have a GNU+Linux system with gtk-2.0 or higher, glade2.0 or higher and the standard compiler 'gcc' and its associated libraries.
  4. I wanted a tutorial on GLADE+C/GLADE+Octave-GTK.
  5. I think you are using a GNU/Linux machine and love the GNU movement.

How to use GLADE & C

  • GLADE UI designer. In case you are using glade-2 or newer please skip this part, others using earlier versions of glade, this is a must.
    1. First create the user interface in glade UI builder and also save it in the ".glade" format.
    2. If you have not generated glade-2.0 UI file the convert to the libglade format using the libglade-convert on your old .glade file by calling the command like this:
      	$]libglade-convert 
              usage: libglade-convert [--no-upgrade] [--verbose] oldfile.glade
             
    3. Then you are ready to start off with the C part.
  • The Octave-GTK connection
  • Now that you have created a ".glade" file please proceed to generate your user interface as follows, from Octave.
    #! /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
    

  • The C connection
    1. Now that you have created a ".glade" file please proceed to generate your user interface as follows.
      #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 
      */
      
    2. The GladeXML is the structure that has the details of your parsed Gtk widget that you have generated.
    3. The Glade file that you have generated with glade is to be parsed using the function glade_xml_new. The first argument is the name of the .glade file. The next two I dont really know.
    4. Now one this has been done do an auto connect of all the widgets you have to their callbacks by using the glade_xml_signal_autoconnect(GladeXML *ptr) to which you have to pass the pointer of the GladeXML structure.
    5. Then Ideally you will get the main widget [topmost window] by calling the function glade_xml_get_widget(GladeXML* ptr,char *widgetname);
    6. Display it over the screen with the call gtk_widget_show_all(GtkWidget *w)

    Real World Example

    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.0
    
    Then run the program
    #]./gnu on the shell. You must see that we have the following window
    The same program written in Octave-GTK looks like this:
    #! /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:
    For more information, please visit us Octave-GTK page The contents of this tutorial are freely distributable under the terms of the GNU FDL.

    Attached Files

    To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
    • [get | view] (2021-02-25 10:00:35, 1.3 KB) [[attachment:gnu.c]]
    • [get | view] (2021-02-25 10:00:35, 2.3 KB) [[attachment:gnu.glade]]
    • [get | view] (2021-02-25 10:00:35, 0.8 KB) [[attachment:gnu.m]]
    • [get | view] (2021-02-25 10:00:35, 1.4 KB) [[attachment:gnu.png]]
    • [get | view] (2021-02-25 10:00:35, 32.0 KB) [[attachment:octave_gtk_hack.sxi]]
    • [get | view] (2021-02-25 10:00:35, 9.1 KB) [[attachment:octave_gtk_libglade.html]]
     All files | Selected Files: delete move to page copy to page

    You are not allowed to attach a file to this page.