HelloWorldOne More Time

only for RuBoard - do not distribute or recompile

HelloWorld!One More Time

This section provides a quick tutorial on how to use glade.h to build the user interface at runtime.

  1. First, open Glade and build the window (form) widget shown in Figure 14.1.

    Figure 14.1. The main form of HelloWorld.glade in design view.
    graphics/14fig01.gif
  2. In the Project Options dialog, deselect the Enable GNOME Support check box; and on the second tab, deselect the check boxes labeled Output Support Functions and Output Build Files, as shown in Figure 14.2.

    Figure 14.2. HelloWorld.glade with the appropriate options set.
    graphics/14fig02.gif
  3. Save the file, but in this case, you do not need to click the Build button. (There is nothing to build; the HelloWorld.glade file is what is required of this process, and it is created by clicking the Save button.)

  4. Return to the main form for HelloWorld, select the HelloWorld! command button, and then click the Signals tab in the Properties dialog box. Note that in Figure 14.3, the signal for this command button is button1_clicked . In this case, that signal handler was typed in by hand as opposed to being generated by Glade. button1_clicked is the function that should fire when button1 (Hello World!) is clicked. Click the Add button to bring the project to the point shown in Figure 14.3

    Figure 14.3. Manually setting the callback for button1.
    graphics/14fig03.gif
  5. Repeat these steps for button2 (Goodbye World!), but for the signal handler for button2s clicked event, specify button2_clicked .

Next, an examination of the HelloWorld.glade file is in order. Listing 14.1 is that file. For a review of XML as it relates to *.glade files, refer to Chapter 5, Glade for VB Developers.

Listing 14.1 HelloWorld.glade
 <?xml version="1.0"?>  <GTK-Interface>  <project>    <name>helloworld</name>    <program_name>helloworld</program_name>    <directory></directory>    <source_directory>src</source_directory>    <pixmaps_directory>pixmaps</pixmaps_directory>    <language>C</language>    <gnome_support>False</gnome_support>    <gettext_support>False</gettext_support>    <use_widget_names>False</use_widget_names>    <output_main_file>True</output_main_file>    <output_support_files>False</output_support_files>    <output_build_files>False</output_build_files>    <backup_source_files>True</backup_source_files>      <main_source_file>interface.c</main_source_file>    <main_header_file>interface.h</main_header_file>    <handler_source_file>callbacks.c</handler_source_file>    <handler_header_file>callbacks.h</handler_header_file>    <support_source_file>support.c</support_source_file>    <support_header_file>support.h</support_header_file>    <translatable_strings_file></translatable_strings_file>  </project>  <widget>    <class>GtkWindow</class>    <name>window1</name>    <title>window1</title>    <type>GTK_WINDOW_TOPLEVEL</type>    <position>GTK_WIN_POS_NONE</position>    <modal>False</modal>    <allow_shrink>False</allow_shrink>    <allow_grow>True</allow_grow>    <auto_shrink>False</auto_shrink>    <widget>      <class>GtkVBox</class>      <name>vbox1</name>      <homogeneous>False</homogeneous>      <spacing>0</spacing>      <widget>        <class>GtkButton</class>        <name>button1</name>        <can_focus>True</can_focus>        <signal>        <name>clicked</name>        <handler>button1_clicked</handler>        <last_modification_time>Wed, 21 Mar 2001 10:46:25  GMT</last_modification_time>        </signal>        <label>Hello World!</label>        <child>        <padding>0</padding>        <expand>False</expand>        <fill>False</fill>        </child>      </widget>      <widget>        <class>GtkButton</class>        <name>button2</name>        <can_focus>True</can_focus>        <signal>        <name>clicked</name>        <handler>button2_clicked</handler>        <last_modification_time>Wed, 21 Mar 2001 10:46: 13  GMT</last_modification_time>        </signal>        <label>Goodbye World!</label>        <child>        <padding>0</padding>        <expand>False</expand>        <fill>False</fill>        </child>      </widget>    </widget>  </widget>  </GTK-Interface> 

So far nothing too complicated has been done. An examination of Listing 14.1 provides some important points for future reference. There are two command buttons , button1 (Hello World!) and button2 (Goodbye World!); and they will call functions button1_clicked() and button2_clicked() , respectively.

Now move on to helloworld.c , which is Listing 14.2. Note that Glade output it as main.c, and I have changed the name for clarity. (This listing, like all the listings in this book, is available from the companion Web site.)

Listing 14.2 helloworld.c
  /* Initial main.c file generated by Glade. Edit as required.   * Glade will not overwrite this file.   */  #ifdef HAVE_CONFIG_H  #  include <config.h>  #endif  #include <gtk/gtk.h>  #include <glade/glade.h>  GtkWidget *window1;  void button1_clicked()  {    g_print("Hello World...\n");  }  void button2_clicked()  {    gtk_main_quit();  }    int  main (int argc, char *argv[])  {   GladeXML *xml;    gtk_init (&argc, &argv);    glade_init();    xml = glade_xml_new("../helloworld.glade", "window1");    if (!xml)       {        g_print("unable to load interface...\n");         return 1;       }    window1 = glade_xml_get_widget(xml, "window1");    glade_xml_signal_autoconnect(xml);  /* The following code was added by Glade to create one of each component   * (except popup menus), just so that you see something after building   * the project. Delete any components you don  '  t want shown initially.   */  gtk_widget_show (window1);    gtk_main ();    return 0;  } 

Following is a list of things to note about Listing 14.2:

  • #include <glade.h> is in the header. It is needed for the *_xml_* calls in the file.

  • GtkWidget * window1 is the main window of the application, and it has been made global in scope.

  • button1_clicked() and button2_clicked() are simple and self-explanatory. They are the functions executed by the Hello World and Goodbye World buttons, respectively.

  • There is a GladeXML variable type, and glade_init() appears at the start of function main() . These are needed by glade.h and for assigning a value to the *xml variable.

  • The glade_xml_new() call opens the *.glade file assigned and looks for the specification of a widget named window1.

  • The glade_xml_get_widget() call builds window1.

The rest is the same as with any Glade/GTK+ project. Figure 14.4 shows the application running.

Figure 14.4. HelloWorld running.
graphics/14fig04.gif

Finally, one small change is required when you compile the application. You must add the libglade.a library as shown at the following gcc command line:

 % gcc -Wall `gtk-configcflagslibs` helloworld.c -o helloworld -lglade 
only for RuBoard - do not distribute or recompile


MySQL Building User Interfaces
MySQL: Building User Interfaces (Landmark)
ISBN: 073571049X
EAN: 2147483647
Year: 2001
Pages: 119

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net