Appendix C. Libglade Examples

Table of Contents

Example 1
Example 2
Example 3
Example 4
Alternative form of examples

Example 1

A simple example that shows a single warning window is shown below.

GTK

The file using the C language bindings to GTK to make this interface layout into a functioning program is below.

/* This program displays a simple window and has a simple callback for
 * when the OK button is clicked.
 */

#include <gtk/gtk.h>
#include <glade/glade.h>

void
ok_button_clicked (GtkWidget *widget, gpointer user_data)
{
  printf ("Thanks for trying out my program.\n");
  gtk_main_quit ();
}

int
main (int argc, char *argv[])
{
  GladeXML  *main_window;
  GtkWidget *widget;

  gtk_init (&argc, &argv);

  /* load the interface */
  main_window = glade_xml_new ("example-1.glade", NULL, NULL);

  /* connect the signals in the interface */

  /* Have the ok button call the ok_button_clicked callback */
  widget = glade_xml_get_widget (main_window, "OKButton");
  g_signal_connect (G_OBJECT (widget), "clicked",
                    G_CALLBACK (ok_button_clicked),
                    NULL);

  /* Have the delete event (window close) end the program */
  widget = glade_xml_get_widget (main_window, "MainWindow");
  g_signal_connect (G_OBJECT (widget), "delete_event",
                    G_CALLBACK (gtk_main_quit), NULL);

  /* start the event loop */
  gtk_main ();

  return 0;
}

GTKMM

The file using the C++ language bindings to GTK to make this interface layout into a functioning program is below.

/* This program displays a simple window and has a simple callback for
 * when the OK button is clicked.
 */

#include <gtkmm.h>
#include <libglademm/xml.h>
#include <iostream>

using namespace std;

void
ok_button_clicked()
{
  cout << "Thanks for trying out my program." << endl;
  Gtk::Main::quit();
}

int
main (int argc, char *argv[])
{
  Glib::RefPtr<Gnome::Glade::Xml> main_window;
  Gtk::Main kit(argc, argv);

  // load the interface
  main_window = Gnome::Glade::Xml::create("example-1.glade");

  // connect the signals in the interface
  Gtk::Button& ok_button = 
    dynamic_cast<Gtk::Button&>( *main_window->get_widget("OKButton") );
  ok_button.signal_clicked().connect( sigc::ptr_fun(&ok_button_clicked) );

  // start the event loop; exit when the specified window is closed
  Gtk::Dialog& dialog =
    dynamic_cast<Gtk::Dialog&>( *main_window->get_widget("MainWindow") );
  //dialog.signal_delete_event().connect( sigc::ptr_fun(&Gtk::Main::quit) );

  Gtk::Main::run( dialog );
  //Gtk::Main::run( );

  return 0;
}

GTK2-PERL

The file using the perl language bindings to GTK to make this interface layout into a functioning program is below.

#!/usr/bin/perl -w

# This program displays a simple window and has a simple callback for
# when the OK button is clicked.

use Glib qw(TRUE FALSE);
use Gtk2 '-init';    # The '-init' means Gtk2->init gets called initially
use Gtk2::GladeXML;

# Just to be pedantic...
use strict;
use vars qw($main_window);

sub ok_button_clicked
{
  my (undef, undef) = @_;  # widget, data are unused
  print "Thanks for trying out my program.\n";

  Gtk2->main_quit;
}

# No predefined helper functions exist--all must be manually declared.
sub gtk_main_quit
{
  Gtk2->main_quit;
  return TRUE;
}

  # Init is automatically called
  #Gtk2->init;

  # load the interface
  $main_window = Gtk2::GladeXML->new('example-1.glade');

  # connect the signals in the interface; since we didn't declare what
  # package we were in, perl defaults to 'main'...
  $main_window->signal_autoconnect_from_package('main');

  # start the event loop
  Gtk2->main;

PYGTK

The file using the python language bindings to GTK to make this interface layout into a functioning program is below.

#!/usr/bin/env python

# This program displays a simple window and has a simple callback for
# when the OK button is clicked.

import gtk
import gtk.glade

# libglade needs some way to lookup the handler functions; defining
# them in a class provides an easy way to do that
class GladeHandlers:

  def ok_button_clicked(ok_button):
    print "Thanks for trying out my program."
    gtk.mainquit()

  # No predefined helper functions exist--all must be manually declared.
  def gtk_main_quit(window, event):
    gtk.mainquit()

# load the interface
main_window = gtk.glade.XML('example-1.glade')

# connect the signals in the interface
main_window.signal_autoconnect(GladeHandlers.__dict__)

# start the event loop
gtk.main()