Get Widget Pointer in Callback Function GTK Glade

How to get widget pointer in callback function of Glade GTK C program. Pass a pointer to a single widget to a callback function via the gpointer user_data parameter of the callback function.

Get Widget Pointer in Callback Function
Get Widget Pointer in Callback Function

How to Get Widget Pointer in Callback Function

In a previous post global variables were used to get pointers to text label widgets so that the text in the labels could be updated when a button in the application is clicked. To avoid the use of global variables a pointer to a label can be passed to the callback function of the button.

When the button in the application is clicked, it emits a signal which causes the button clicked handler to be called. The second parameter of the handler function, the user data parameter, can be used to pass a pointer to the text label widget. The text in the label widget can then be updated within the callback function without the need for a global pointer to the label widget. This is set up inside Glade as shown in the tutorial below.

Pointer to Widget in Callback Function of Glade C Program

The following tutorial steps show how to build a GTK+ 3 C program using Glade 3 which passes a pointer to a single widget via the callback function of the clicked signal of a button.

1. Create a New GTK Glade C Program

Start a new project by making a copy of the GTK+ 3 Glade 3 C program template files. Rename the project folder to glade_widget_pointer. Rename the application name in the makefile to glade_widget_pointer. The make file should now look as follows.

makefile

2. Lay out the Application Window in Glade

Open the window_main.glade Glade file from the glade folder of the template files in the Glade 3 editor program.

2.1 Change Window Title and Size

In Glade, click the main window to select it and change its title to Glade Widget Pointer. Uncheck the Default Width and Default Height parameters. The width and height would normally be unchecked in a new Glade window, but they were set in the Glade window of the template file.

Change Glade Window Title and Size
Change Glade Window Title and Size

2.2 Add a Box to the Main Window

Drag and drop a Box onto the main window from under Containers of the left panel in Glade. When prompted for the number of items in the Create a GtkBox dialog, select 2 and then click the Create button.

2.3 Add a Label and Button

Drag and drop a Label into the top section of the box on the main window. The label is found under Control and Display of the left Glade pane.

Drag and drop a Button onto the bottom section of the box of the main window. The button is also found under Control and Display of the left pane.

2.4 Change the Label and Button Properties

Change the ID of the label to lbl_text in the ID field and the label text to in the Label field.

Change Label Properties in Glade
Change Label Properties in Glade

Change the ID of the button to btn_print_txt and the text of the button to Print Text.

2.5 Change Widget Spacing and Margins

For a better appearance which stops the two main widgets from being tightly crammed in the window, change the spacing and margins of the GtkBox which should have a default name of box1.

Select the box in the top right pane of Glade. Change the Spacing to 10 under the General tab (bottom right pane in Glade). Change the left and right margins to 10 under the Common tab at the bottom right pane of Glade.

2.6 Add the Button Callback Function

In Glade select the button widget in Glade and then click the Signals tab in the bottom right pane of Glade. Next to the clicked signal under Handler make the name of the callback function on_btn_print_txt_clicked.

Widget Pointer in Callback Function: This is where the pointer to the label widget is passed to the button callback function. Now click the pencil icon under User data next to the callback function name. In the dialog box that pops up, select lbl_text and then click the OK button.

Set the User Data of the Callback Handler Function in Glade
Set the User Data of the Callback Handler Function in Glade

The finished Glade file found in the glade folder of the project is shown below.

window_main.glade

3. Modify the C Program

Open the main.c file from the src folder of the project for editing. The callback function for the button clicked signal must now be added to the C code.

3.1 Find the Correct Callback Function Format

If the Devhelp application was installed on the system and the help files for GTK were installed, a small Devhelp icon will appear next to the clicked event for the button in Glade. Click the icon to find the correct callback function format for the button clicked signal.

Glade Clicked Handler Devhelp Icon
Glade Clicked Handler Devhelp Icon

After clicking the icon, Devhelp opens with the following information on the button clicked signal.

Devhelp Information on the Button Clicked Signal
Devhelp Information on the Button Clicked Signal

As can be seen above, the callback function for the button clicked signal takes two parameters – the first parameter is a pointer to the button itself and the second parameter is a pointer to user data. The user data was set as a pointer to the label in the application in Glade.  Because gpointer is defined as a pointer to void, the GtkLabel can be passed as user data avoiding the need of a cast in the callback function. The function can therefore be defined as follows.

3.2 Add the Callback Function in the C Code

Add the callback function to the code that changes the text in the label. The full final listing for the program is as follows.

main.c

As can be seen in the code, a pointer to the text label is passed to the button clicked callback function. The pointer to the label is passed as user data and was set up in Glade. This is where we get the label widget pointer in callback function on_btn_print_txt_clicked(). gtk_label_set_text() can then be called and the pointer to the text label passed to it without the need for any casts.

4. Build the Project

Open a terminal window and change to the project directory. Build the project by entering make in the terminal window. Run the program by entering ./glade_widget_pointer in the terminal window.

After starting the application, click the button and the text in the label should change as shown below.

Glade Widget Pointer App
Glade Widget Pointer App

Resources and Software Tool Details

Refer to the GTK Glade C Programming index to get started with GTK Glade C projects.

Software development environment and tools: This project was developed on a Linux Mint 18 Mate 64-bit system using the following tools.

  • Glade version 3.18.3
  • GTK+3 version 3.18.9
  • GCC version 5.4.0

Leave a Reply

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