GTK+ 3 Glade Radio Buttons C Code

GTK+ 3 Glade radio buttons C code that demonstrates how to use radio buttons in a Glade GTK project. The tutorial in the previous blog post showed how to group radio buttons in Glade, but did not show how to use the radio buttons in a program. This tutorial shows how to use the radio buttons in a GTK C program.

Radio Buttons in Glade

Before writing C code that uses GTK radio buttons, you will need to create a Glade file with radio buttons. Do this by following the tutorial on grouping radio buttons in Glade. The complete Glade file can be found at the bottom of the tutorial.

GTK Radio Button Project Structure

This project is based on the GTK+ 3 Glade C programming template files. Copy the Glade file, which should be named window_main.glade, to the project’s glade subdirectory. Copy the C file from the template to the src subdirectory. Copy the make file to the project’s root directory.

To summarize, you need the Glade file from the tutorial on grouping radio buttons in Glade. You also need the files from the GTK+ 3 Glade C programming template files. Place each file in directories as described above. The Glade file from the radio button tutorial replaces the Glade file from the template files. All the source code is included below in case there is any confusion.

Modify the Make File

Modify the make file to change the name of the project. The make file called makefile is shown below with the project name changed to radio_3btn.

Remember to replace the indenting in the above file with tab characters instead of spaces if you are copying the above file. Spaces will cause an error when running make.

The make file and both subdirectories (glade and src) are stored in the application’s directory called radio_3btn.

Modify the Glade File

Open the Glade file in the Glade user interface designer application. The C template file, main.c, in the src subdirectory contains a callback function called on_window_main_destroy() that must be run when the application emits a destroy signal. This function must be attached to the destroy signal in Glade.

Click the main GTK window in the top right pane of Glade. In the lower half of the right pane in Glade, click the Signals tab. Expand GtkWidget under the Signals tab. Find destroy and click it to select it. Click on the Type here text under Handler. Type “on” and Glade will display a suggested handler name of on_window_main_destroy. Push the down arrow key and then press the Enter key twice to select the suggested name. This connects the on_window_main_destroy() handler to the destroy signal. The image below shows the changes made in Glade.

Glade Destroy Signal Handler
Glade Destroy Signal Handler

At this stage you can build the project by typing make at the command prompt in the project’s directory. If everything is correct, the project will compile and produce an executable file called radio_3btn. Running this file will open the main window which will be able to be closed using the X in the application window or Alt + F4 keyboard shortcut.

Signal handlers must now be added for the three radio buttons. Click the top radio button in Glade to select it. In the lower half of the right Glade panel, click the Signals tab. Expand the GtkToggleButton item and click toggled. Click Type here under Handler as was done for the destroy handler. Start typing “on” and then select the suggested name of on_rb_1_toggled. Now click the Click here text under User data. Select lbl_choice in the dialog box that pops up. This will pass a pointer to the label to the radio button callback handler allowing us to modify the text of the label when the radio button is clicked. Changes in the main Glade window and the dialog box are shown below.

Glade Radio Button Toggled Handler
Glade Radio Button Toggled Handler
Glade Handler User Data
Glade Handler User Data

Do the same for the other two radio buttons. The only difference between the top radio button and the other radio buttons is the name of the handler functions. The second radio button handler will be named on_rb_2_toggled and the third radio button handler will be named on_rb_5_toggled. Don’t forget to select the same label for the user data of all radio buttons.

Save the changes to the Glade file. The C code can now be added to handle the button toggle events. The complete Glade file is shown below.

GTK+ 3 Glade Radio Buttons C Code

A handler function must be added for each of the radio buttons. Whenever a radio button changes state, or is toggled, the handler function will be called. The C code below shows the changes made to main.c in the src subdirectory.

Three radio button “toggled” handler functions can be found below main(), one function for each radio button. Inside each handler function, gtk_label_set_text() is used to directly change the text in the label at the bottom of the main application window. A pointer to the text label was set as the user data to be passed to the handler function in Glade.

A g_print() function is called in each handler to demonstrate something that might not be obvious at first. This function prints text to the command line, so start the application from a command prompt to see the text. When a radio button that is not currently selected is clicked, a toggled signal is emitted because the radio button is toggling from off to on. The previously selected radio button also toggles, from on to off. In other words two toggled signals are emitted by the radio buttons because whenever one radio button changes state, so does the previously selected radio button.

Look at the text that g_print() sends to the command line. When the application is first started, the first radio button is selected. If the second radio button is clicked, the text at the command line shows that radio button 1 and radio button 2 were both toggled. This is something to be aware of because the effects may be undesirable, depending on the application. A solution is to make all radio button signals call the same handler, then determine which radio button is selected in the handler. The next blog post will show how this is done.

Development Tools

This application was developed using:

  • Linux Mint 18.1, Mate, 64-bit
  • Glade 3.18.3
  • GTK library (libgtk-3-dev) 3.18.9-1ubuntu3.3

 

Leave a Reply

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