GtkCheckButton Glade GTK C Tutorial

How to use a GtkCheckButton, which creates a check button or check box, in a Glade GTK C program. Read the state of a GtkCheckButton in C code in this GTK Glade tutorial.

The following image shows the application built in this tutorial. It uses three GtkCheckButton widgets and prints their states to the command line.

GtkCheckButton Widgets used in a GTK C Program with Glade
GtkCheckButton Widgets used in a GTK C Program with Glade

Part 19 of GTK 3 Programming with C and Glade Tutorial

See the full GTK3 tutorial index

What this Tutorial Covers

This tutorial shows how to use a GtkCheckButton widget which creates a check button. A GTK check button is the same as a check box or checkbox in other graphical user interfaces.

The tutorial also shows how to use the Widget name field, found under the Common tab in Glade.

Two C programs are presented, which show two methods of using GtkCheckButton widgets. The second program is more optimized.

GtkCheckButton Tutorial Steps

Follow the tutorial steps below to build this simple GTK check button application.

1. Create a New Project

Create a new project using the template files from part 4 of this tutorial series.

1.1 Copy and Rename Template Folder

Make a copy of the template folder and rename the copy of the folder to checkbox.

1.2 Change Project Name in Make File

Open the make file and change the TARGET name to checkbox. See the complete make file below.

makefile

# change application name here (executable output name)
TARGET=checkbox

# compiler
CC=gcc
# debug
DEBUG=-g
# optimisation
OPT=-O0
# warnings
WARN=-Wall

PTHREAD=-pthread

CCFLAGS=$(DEBUG) $(OPT) $(WARN) $(PTHREAD) -pipe

GTKLIB=`pkg-config --cflags --libs gtk+-3.0`

# linker
LD=gcc
LDFLAGS=$(PTHREAD) $(GTKLIB) -export-dynamic

OBJS=    main.o

all: $(OBJS)
	$(LD) -o $(TARGET) $(OBJS) $(LDFLAGS)
    
main.o: src/main.c
	$(CC) -c $(CCFLAGS) src/main.c $(GTKLIB) -o main.o
    
clean:
	rm -f *.o $(TARGET)

2. Modify the Glade File

At this stage of the tutorial series, it is assumed that you can build Glade files. A detailed step-by-step approach with images of each step will not be used in this part of the tutorial. It should be possible to build the Glade file from the details below. If you have any doubts on how to build the Glade file, watch the video embedded near the top of this page.

Open window_main.glade found in the glade folder of the project.

2.1 Change Main Window Title

Click the main window and change its title to Checkbox.

2.2 Place a GtkBox

Leave the GtkBox settings at their default values, just add 10 pixels of spacing and a border width of 10 pixels.

2.3 Place 3 GtkCheckButton Widgets

Place a GtkCheckButton in each open slot in the GtkBox.

Change the following attributes of the GtkCheckButton Widgets:

IDLabelHandler
chkbtn_1Option 1on_chkbtn_1_toggled
chkbtn_2Option 2on_chkbtn_2_toggled
chkbtn_3Option 3on_chkbtn_3_toggled

At this stage your glade file should look as follows.

GtkCheckButton Widgets Placed in Glade
GtkCheckButton Widgets Placed in Glade

3. Write the C Code

Open main.c from the src folder for editing. Add the code shown in the following listing.

main.c

#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
    GtkBuilder      *builder; 
    GtkWidget       *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new_from_file("glade/window_main.glade");

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
    gtk_builder_connect_signals(builder, NULL);

    g_object_unref(builder);

    gtk_widget_show(window);                
    gtk_main();

    return 0;
}

void on_chkbtn_1_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
	//g_print("%s\n", gtk_widget_get_name(GTK_WIDGET(togglebutton)));
	
	if (gtk_toggle_button_get_active(togglebutton)) {
		g_print("Option 1 is Checked\n");
	}
	else {
		g_print("Option 1 is Unchecked\n");
	}
}

void on_chkbtn_2_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
	if (gtk_toggle_button_get_active(togglebutton)) {
		g_print("Option 2 is Checked\n");
	}
	else {
		g_print("Option 2 is Unchecked\n");
	}
}

void on_chkbtn_3_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
	if (gtk_toggle_button_get_active(togglebutton)) {
		g_print("Option 3 is Checked\n");
	}
	else {
		g_print("Option 3 is Unchecked\n");
	}
}

// called when window is closed
void on_window_main_destroy()
{
    gtk_main_quit();
}

When running the applications created in this tutorial, be sure to start them from a terminal window so that the output from the applications can be seen.

A handler function for each GtkCheckButton is called whenever a check button emits a toggled signal.

Each handler calls gtk_toggle_button_get_active() and passes it a pointer to its GtkToggleButton widget. This function determines whether the check button is checked or unchecked. It then prints the appropriate message to the command line using g_print().

Second GtkCheckButton Project

The previous project can be simplified to use only one handler for all three check buttons. This is done below and demonstrates how to to use the widget name attribute in Glade.

Create the Project

Make a copy of the previous project and rename it to checkbox2. Rename the project in the make file to the same name.

Edit the Glade File

Open the Glade file from the glade folder of the project. Change the handler name for each GtkCheckButton toggled signal to on_chkbtn_toggled.

For each GtkCheckButton, under the Common tab in Glade, find the Widget name field. For each check button widget, change its widget name to option_1, option_2 and option_3 from top to bottom.

Save the file after making the above changes.

Edit the C Source Code

Delete the old handler functions for the toggled signals and replace them with the single new handler. The new handler function can be seen in the source code listing below.

main.c

#include <gtk/gtk.h>

int main(int argc, char *argv[])
{
    GtkBuilder      *builder; 
    GtkWidget       *window;

    gtk_init(&argc, &argv);

    builder = gtk_builder_new_from_file("glade/window_main.glade");

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
    gtk_builder_connect_signals(builder, NULL);

    g_object_unref(builder);

    gtk_widget_show(window);                
    gtk_main();

    return 0;
}

void on_chkbtn_toggled(GtkToggleButton *togglebutton, gpointer user_data)
{
	// Print the check button name
	//g_print("%s\n", gtk_widget_get_name(GTK_WIDGET(togglebutton)));
	
	// Find out which check button was clicked
	if (g_strcmp0(gtk_widget_get_name(GTK_WIDGET(togglebutton)), "option_1") == 0) {
		g_print("Option 1 ");
	}
	else if (g_strcmp0(gtk_widget_get_name(GTK_WIDGET(togglebutton)), "option_2") == 0) {
		g_print("Option 2 ");
	}
	else if (g_strcmp0(gtk_widget_get_name(GTK_WIDGET(togglebutton)), "option_3") == 0) {
		g_print("Option 3 ");
	}
	else {
		g_print("Something went wrong!\n");
	}
	
	// Check whether check button was checked or unchecked
	if (gtk_toggle_button_get_active(togglebutton)) {
		g_print("is Checked\n");
	}
	else {
		g_print("is Unchecked\n");
	}
}

// called when window is closed
void on_window_main_destroy()
{
    gtk_main_quit();
}

The new handler function on_chkbtn_toggled(), is called when any check button is checked or unchecked. To find out which check button called the handler, gtk_widget_get_name() is used to find the name of the check button. This name is the name that was set in the Widget name field in Glade. It is returned by the function as a string.

A string comparison is done using the GLib g_strcmp0() function. It is used to compare the string returned by gtk_widget_get_name() and a string constant.

After the calling check button is determined, a string is printed to the terminal window. The state of the check button is then determined using the same method as the first C program in this tutorial.

Leave a Reply

Your email address will not be published. Required fields are marked *