GTK Template Files with Widget Structure

GTK template files for GTK+ 3 projects that use Glade and the C programming language. C source code that includes a widget structure for getting pointers to widgets.

I got tired of manually adding a widget pointer structure, and supporting code, to every new GTK 3 Glade project. A new set of template files in this article fixes this problem. It adds the widget pointer code to the template.

Part 24 of GTK 3 Programming with C and Glade Tutorial

See the full GTK3 tutorial index

About the GTK Template Files

These template files are for easily starting new GTK+ 3 C code projects that use the Glade user interface designer and use a widget pointer structure.

Structure of the Template Files

The files consist of a combination of template files from part 4 of the GTK 3 Programming with C and Glade tutorial series and part 10 – GTK Glade Get Pointers to Widgets.

Reason for the GTK Template Files

The reason for making these template files is that the template files from part 4 are too basic. When creating a project that needs pointers to widgets, code from part 10 is needed.

Creating the New Template Files

Build the new set of widget pointer template files by following the steps below. This is done by simply copying the original template and then replacing the code in the C file as described in the steps that follow.

1. Create the Basic Set of Files

Follow the instructions from part 4 of the GTK 3 programming with C and Glade tutorial series to build the basic template.

2. Rename the Basic Template Folder

Rename the root folder of the basic template set to template_widget_ptrs or your own choice of name.

3. Replace the C Code in main.c

Replace the code in the main.c file, found in the src folder with the following code. You are now done with making the new template.

main.c

#include <gtk/gtk.h>

// Custom structure that holds pointers to widgets and user variables
typedef struct {
    // Add pointers to widgets below
    //GtkWidget *w_x;
} app_widgets;

int main(int argc, char *argv[])
{
    GtkBuilder      *builder; 
    GtkWidget       *window;
    // Instantiate structure, allocating memory for it
    app_widgets     *widgets = g_slice_new(app_widgets);
    
    gtk_init(&argc, &argv);

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

    window = GTK_WIDGET(gtk_builder_get_object(builder, "window_main"));
    // Get pointers to widgets here
    //widgets->w_x  = GTK_WIDGET(gtk_builder_get_object(builder, "x"));
    
     // Widgets pointer are passed to all widget handler functions as the user_data parameter
    gtk_builder_connect_signals(builder, widgets);

    g_object_unref(builder);

    gtk_widget_show_all(window);                
    gtk_main();
    // Free up widget structure memory
    g_slice_free(app_widgets, widgets);

    return 0;
}

// Dummy handler function
/*void x(GtkButton *button, app_widgets *app_wdgts)
{
}*/

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

How to Use the New Template

Using the new template files requires some knowledge of GTK programming from the GTK Glade C Programming Tutorial series. After following along to at least part 10, you should understand the new template.

Some dummy code is included in main.c and is commented out. This code can be used to add the necessary widget pointers and handler functions for new projects. It serves as an example and reminder of the format of various required code.

Leave a Reply

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