Simple GTK+ programming using glade3

GTK+ GUI programming forum.

Simple GTK+ programming using glade3

Postby QuantumKnot on February 4th, 2008, 11:09 am

(Perhaps there needs to be a subforum for GTK+ programming)

Here is a quick guide to compiling a simple GTK program that uses the glade3 interface builder (paraphrased from http://www.gnome.org/~newren/tutorials/developing-with-gnome/html/index.html).

1. Firstly, install Glade3, which as seen below, bears similarities to other interface builders.

Screenshot-example.glade.png
Screenshot-example.glade.png (162.77 KB) Viewed 146 times


2. Note that the OK button has a callback function 'ok_button_clicked' connected to the 'clicked' signal.

3. Save the file as a .glade file. Here, I've called it 'example.glade'

4. Create the following C source file (I've called it 'example.c'). Note the callback function that's been written.

Code: Select all
#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  *xml;
  GtkWidget *widget;

  gtk_init(&argc, &argv);

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

  /* connect the signals in the interface */

  /* Have the ok button call the ok_button_clicked callback */
  widget = glade_xml_get_widget(xml, "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 (xml, "MainWindow");
  g_signal_connect(G_OBJECT (widget), "delete_event",
                    G_CALLBACK (gtk_main_quit), NULL);

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

  return 0;
}


5. Compile this file using the following command.

Code: Select all
gcc example.c -o example `pkg-config --cflags --libs libglade-2.0`


6. And voila

Screenshot-Useless Program.png
Screenshot-Useless Program.png (18.48 KB) Viewed 146 times
Image
User avatar
QuantumKnot
Member
 
Posts: 98
Joined: January 21st, 2008, 11:58 am

Re: Simple GTK+ programming using glade3

Postby kamil on February 4th, 2008, 1:44 pm

nice, i only have Glade 2 :o i'll yum glade 3 thonight,
(Perhaps there needs to be a subforum for GTK+ programming)

created and moved :)
User avatar
kamil
Linux Adept
 
Posts: 114
Joined: January 18th, 2008, 1:22 am
Location: Brisbane, Australia

Re: Simple GTK+ programming using glade3

Postby QuantumKnot on February 4th, 2008, 1:46 pm

To make the code simpler, you can do automatic signal connection. The only exception is that you need to compile with the -export-dynamic flag for gcc since libglade will look for the signal handler in the symbols table.

Code: Select all
#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  *xml;

  gtk_init(&argc, &argv);

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

  /* automatically connect signal handlers */
   glade_xml_signal_autoconnect(xml);

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

  return 0;
}


Compile this using the following command:

Code: Select all
gcc example.c -o example `pkg-config --cflags --libs libglade-2.0` -export-dynamic
Image
User avatar
QuantumKnot
Member
 
Posts: 98
Joined: January 21st, 2008, 11:58 am

Re: Simple GTK+ programming using glade3

Postby kamil on February 4th, 2008, 8:32 pm

hah, no rpms for FC6, had to build it from source...
Code: Select all
./configure
make
sudo make install

I did the simple example, it compiles and runs but the gui never comes up (sources attached)! :?:

also, since the glade UI file is now in the XML format it would be really cool if glade also generated a corresponding XSL file, that way you could maybe preview the UI files in the browser ;)
Attachments
g3_hello_world.glade
glade3 xml
(986 Bytes) Downloaded 8 times
compile.sh
gcc line
(119 Bytes) Downloaded 8 times
g3_hello_world.c
c code
(495 Bytes) Downloaded 8 times
User avatar
kamil
Linux Adept
 
Posts: 114
Joined: January 18th, 2008, 1:22 am
Location: Brisbane, Australia

Re: Simple GTK+ programming using glade3

Postby QuantumKnot on February 4th, 2008, 10:36 pm

Did you put the .glade file in the same directory as the executable?
Image
User avatar
QuantumKnot
Member
 
Posts: 98
Joined: January 21st, 2008, 11:58 am

Re: Simple GTK+ programming using glade3

Postby QuantumKnot on February 4th, 2008, 10:39 pm

I noticed it says on the site that glade3 files are backwards compatible with glade2 or something to that effect. So I guess you could use glade2 and benefit from the code generation or just use xml.

The xml method has the benefit of being language-independent. So you can mix glade xml with perl, for example. :)
Image
User avatar
QuantumKnot
Member
 
Posts: 98
Joined: January 21st, 2008, 11:58 am

Re: Simple GTK+ programming using glade3

Postby kamil on February 5th, 2008, 9:58 am

QuantumKnot wrote:Did you put the .glade file in the same directory as the executable?

sure did :/
User avatar
kamil
Linux Adept
 
Posts: 114
Joined: January 18th, 2008, 1:22 am
Location: Brisbane, Australia

Re: Simple GTK+ programming using glade3

Postby QuantumKnot on February 5th, 2008, 11:24 am

By default, the window is hidden (not visible).

Image

You need to go into glade3, click on the mainwindow, click on the common tab and set visible to 'yes'. No need to recompile. ;) Or I suppose you can get the window in the C code and set visibility as well.

Image
Image
User avatar
QuantumKnot
Member
 
Posts: 98
Joined: January 21st, 2008, 11:58 am

Re: Simple GTK+ programming using glade3

Postby kamil on February 5th, 2008, 12:00 pm

awesome :) now it works, cheers

Image
User avatar
kamil
Linux Adept
 
Posts: 114
Joined: January 18th, 2008, 1:22 am
Location: Brisbane, Australia


Return to GTK+

Who is online

Users browsing this forum: No registered users and 0 guests

cron

dsplabs homelinux bloglinux forums new! travel photographyawklores new! cryptographyjames' home
©2008 dsplabs.com.au