Back to Blog

GTK/Glade Program Development (Part 2)

#Programming#Button#GTK#Callback#Signal#User

Spin buttons

Common functions:

Creating a spin button widget:
/* Creating a spin button requires an "adjustment object," so you must also create an adjustment object.
Below is the spin button widget automatically generated by Glade, which uses an adjustment object */

GtkObject *spinbutton1_adj;
GtkWidget *spinbutton1;

spinbutton1_adj = gtk_adjustment_new (1, 0, 100, 1, 10, 10);
spinbutton1 = gtk_spin_button_new (GTK_ADJUSTMENT (spinbutton1_adj), 1, 0);

New functions introduced in this section:
Obtaining the current value of a spin button:
Function name: gtk_spin_button_get_value_as_int
Usage:

gfloat spinvalue1;
gint spinvalue2;
spinvalue1 = gtk_spin_button_get_value(GTK_SPIN_BUTTON(spinbutton));
spinvalue2 = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinbutton));

Function name: sprintf()
Usage: sprintf (buf, "%d", spinvalue1);

Function name: lookup_widget()
Usage: GtkWidget *label1 = lookup_widget (GTK_WIDGET(spinbutton), "label1");

I. Designing the Interface with Glade
Steps:

  1. Create a new Glade project.
  2. Add the following elements in order: window (400x300), fixed position container, label, and spin button.
  3. Add a value_changed signal handler for the spin button widget, and set the object field to: spinbutton1.
  4. Set certain properties of the spin button: value, minimum, maximum, step increment, page increment, and page size. (Try to figure out what these properties mean.)
  5. Save the project and generate code.

II. Modifying the Code

interface.c /* No modifications needed; check if your file matches this section */

g_signal_connect_swapped ((gpointer) spinbutton1, "value_changed",
G_CALLBACK (on_spinbutton1_value_changed),
GTK_OBJECT (spinbutton1));

callbacks.h /* No modifications needed; check if your file matches this */

void on_spinbutton1_value_changed (GtkSpinButton *spinbutton,
gpointer user_data);

callbacks.c

void
on_spinbutton1_value_changed (GtkSpinButton *spinbutton,
gpointer user_data)
{
gchar buf[10];
gint spinvalue1;

GtkWidget *label1 = lookup_widget (GTK_WIDGET(spinbutton), "label1");
spinvalue1 = gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(spinbutton));
sprintf (buf, "%d", spinvalue1);
gtk_label_set_text(GTK_LABEL(label1), buf);
}

Drawing Area Widget (drawingarea)

Common functions:

Creating a widget:
/* */
GtkWidget *drawingarea1;
drawingarea1 = gtk_drawing_area_new ();

New functions introduced in this section:

Function name: gdk_gc_new /* Create a new graphics context (pen) */
Usage:
GdkGC *gc1;
gc1 = gdk_gc_new (drawingarea1->window);

Function name: gdk_gc_set_rgb_fg_color /* Set the foreground color of the graphics context */
Usage:
GdkColor color;
color.red = 30000;
gdk_gc_set_rgb_fg_color (gc1, &color);

Function name: gdk_draw_line /* Draw a line /
Function name: gdk_draw_rectangle /
Draw a rectangle */

I. Designing the Interface with Glade
Steps:

  1. Create a new Glade project.
  2. Add the following elements in order: window (400x300), fixed position container, button, and drawing area.
  3. Add a clicked signal handler for the button widget.
  4. Save the project and generate code.

II. Modifying the Code

interface.c /* No modifications needed; check if your file matches this section */

g_signal_connect ((gpointer) button1, "clicked",
G_CALLBACK (on_button1_clicked),
NULL);

callbacks.h /* No modifications needed; check if your file matches this */

callbacks.c

void
on_button1_clicked (GtkButton *button,
gpointer user_data)
{
GtkWidget *drawingarea1 = lookup_widget (GTK_WIDGET(button), "drawingarea1");
GdkGC gc1; / Define graphics context /
GdkColor color; /
Define color variable /
gc1 = gdk_gc_new (drawingarea1->window); /
Create new graphics context */

color.red = 30000; /* Define color /
gdk_gc_set_rgb_fg_color (gc1, &color); /
Set foreground color of graphics context /
gdk_draw_line (drawingarea1->window, /
Draw line */
gc1,
0, 0, 0, 300);

color.red = 0;
color.blue = 30000; /* Redefine color /
gdk_gc_set_rgb_fg_color (gc1, &color); /
Set foreground color of graphics context */

gdk_draw_rectangle (drawingarea1->window, /* Draw rectangle */
gc1,
TRUE,
50,
50,
100,
100);
}

File Chooser Dialog Widget

Common functions:

Creating the widget:
/* */
GtkWidget *window2;
window2 = create_filechooserdialog1 ();
gtk_widget_show (window2);

New functions introduced in this section:

Function name: gtk_file_chooser_get_filename /* Retrieve filename */
Usage:

gchar *filename;
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (window2));

Note: This section involves calling a dialog from the main window, so a global pointer variable is defined in callbacks.h to reference the dialog.

I. Designing the Interface with Glade
Steps:

  1. Create a new Glade project.
  2. Add the following elements in order: window (400x300), fixed position container, button, and file chooser dialog.
  3. Add clicked signal handlers for the button widget and the "Cancel" and "Open" buttons inside the file chooser dialog.
  4. Save the project and generate code.

II. Modifying the Code
Before modifying the code, set the project property to "Win32 Console Application."

main.c

Remove the following section: /* This part is not present when designing with Anjuta IDE under Linux. */

GtkWidget *fontselectiondialog1;
fontselectiondialog1 = create_fontselectiondialog1 ();
gtk_widget_show (fontselectiondialog1);

interface.c /* No modifications needed; check if your file matches this section */

create_window1
g_signal_connect ((gpointer) button1, "clicked",
G_CALLBACK (on_button1_clicked),
NULL);

create_filechooserdialog1
g_signal_connect ((gpointer) button2, "clicked",
G_CALLBACK (on_button2_clicked),
NULL);
g_signal_connect ((gpointer) button3, "clicked",
G_CALLBACK (on_button3_clicked),
NULL);

callbacks.h /* Check if your file matches this */

gchar filename; / Global variable to store retrieved filename */
GtkWidget window2; / Global pointer to dialog widget, used to access the dialog */

void on_button1_clicked (GtkButton *button,
gpointer user_data);
void on_button2_clicked (GtkButton *button,
gpointer user_data);
void on_button3_clicked (GtkButton *button,
gpointer user_data);

callbacks.c

void on_button1_clicked (GtkButton button, / Button on main window, used to create dialog */
gpointer user_data)
{
window2 = create_filechooserdialog1 ();
gtk_widget_show (window2);
}

void on_button2_clicked (GtkButton button, / "Cancel" button on dialog /
gpointer user_data)
{
g_print("No file is selected\n");
gtk_widget_destroy (window2); /
Destroy dialog */
}

void on_button3_clicked (GtkButton button, / "Open" button on dialog /
gpointer user_data)
{
filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (window2)); /
Retrieve filename from dialog /
g_print("The file is: \n");
g_print(filename);
gtk_widget_destroy (window2); /
Destroy dialog */
}