GTK Programming Lesson 1

The glade interface is intuitive, so minimal instructions are given here. The emphasis is on the underlying code for controlling the glade-designed screen GUI.

Glade instructions

In glade, in a new project:
Toplevel - Window and name it mainWindow
Containers - vertical box - number of items = 3 - create this in the mainWindow

Control and display - label - create a label in the first container

Container - horizontal box - create this in the second container
Control and display - label - create this in the left hand horizontal container
Control and display - text entry - create this in the right hand horizontal container and name this text entry field mainTextEntry

Container - horizontal button box - create this in the third container
Control and display - button - create this in the right hand horizontal buttonbox and name it mainQuitButton
Go to the signals tab, and for the handler for clicked, enter on_mainQuitButton_clicked

Your glade project should now look like this : You can download my glade xml file here Step 1

Code

This is main.c :

#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

/* These should be the only variables needed to define globally. All 
   other gtk variables should be defined within the relevant functions. */

GtkWidget 		*mainWindow;
GtkBuilder 		*builder;
GError 			*error = NULL;

void populate_mainWindow();

/* **********************************************************************
** Main signal to QUIT.
** This is the function called when the Quit Button is clicked.
* ***********************************************************************/
void on_mainQuitButton_clicked (GtkObject *object, gpointer user_data)
{
	g_object_unref(builder);
        gtk_main_quit();
}

/* **********************************************************************
** MAIN
** **********************************************************************/
int main (int argc, char **argv)
{
  gtk_init (&argc, &argv);

  builder = gtk_builder_new ();
  gtk_builder_add_from_file (builder, "tut1.glade", &error);

  if (error != NULL) 
	{
	g_warning ("%s", error->message);
	g_error_free (error);
	exit(1);
	}

  mainWindow = GTK_WIDGET (gtk_builder_get_object (builder, "mainWindow"));
  gtk_builder_connect_signals (builder, NULL);
  gtk_window_set_title (GTK_WINDOW(mainWindow), "Services Manager");

  populate_mainWindow();

  gtk_widget_show (mainWindow);
  gtk_main ();

return 0;
}

This is function.c :

#include <gtk/gtk.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>


/* Each additional source code file requires only this global variable. */
GtkBuilder          *builder;


/* **********************************************************************
** This example function shows how to populate a text entry field.
* ***********************************************************************/
void populate_mainWindow()
{
GtkEntry	*mainTextEntry;

char		tempText[200];

mainTextEntry = GTK_ENTRY (gtk_builder_get_object (builder, "mainTextEntry"));


sprintf(tempText, "Field start value");

/* And set the screen value. */
gtk_entry_set_text(mainTextEntry, tempText);


return;
}

This is compile.sh, which should be used to compile the above 2 code files :

gcc -Wall -g -o tutorial main.c function.c -export-dynamic `pkg-config --cflags --libs gtk+-2.0`

And you can download all the above here.