GTK About Dialog Box GtkAboutDialog in Glade

Add an about dialog box to a GTK application using a GtkAboutDialog object in Glade. Example GTK application programmed in C on Linux Mint.

Using a GtkAboutDialog in an application is an easy way to display information about a program like its logo, author, copyright and license. The image below shows the dialog box created in Glade using a GtkAboutDialog window.

GtkAboutDialog Placed in Glade in this Tutorial
GtkAboutDialog Placed in Glade in this Tutorial

Part 23 of GTK 3 Programming with C and Glade Tutorial

See the full GTK3 tutorial index

Using GtkAboutDialog Box in this Tutorial

This tutorial extends the simple text reader program that we created in the previous part of this tutorial series. It adds an about dialog box to the About item on the Help menu.

It should be easy enough to add an about dialog box to any GTK Glade C program using the tutorial steps that follow.

GtkAboutDialog About Box Tutorial Steps

Follow the tutorial below to add a GtkAboutDialog about dialog box to the text reader project from the previous part of this tutorial series.

1. Open the Text Reader Project

Open the GTK text reader example project that we previously created, or make a copy of it if you would like to keep the original.

2. Add a Resource Folder and File

In the main project folder, create a folder called res that will be used to hold an image resource. The image will be displayed in the about dialog box as the application logo.

Save the image file below to the res folder. The name of the file is logo.png.

Text Reader Application Logo
Right-click the Above Image and Save it to the Res Folder

3. Edit the Glade File

Open the window_main.glade file from the glade folder in the Glade application for editing.

3.1 Place a GtkAboutDialog in Glade

Click the Toplevels button in Glade and then the GtkAboutDialog item to place a GtkAboutDialog.

3.2 Edit the GtkAboutDialog in Glade

Edit the following attributes of the GtkAboutDialog in Glade, under the General tab at the right of the Glade window.

Give the GtkAboutDialog and ID of dlg_about.

Program Attributes:

  • Change the Name item to Text Reader
  • Change the Version to 0.1
  • Click the File radio button for Logo
  • Use the folder button at the right of the File box under Logo to select the logo.png image in the res folder
  • Add a Website URL and Label
  • In the Comments box, describe the program

Select a License under License and Copyright. Add a Copyright message.

Add Authors, Artists, etc. under Credits.

Under Window Attributes, find Transient For. Click the pencil icon at the right of Transient For and then click the radio button next to window_main in the dialog box that opens. Click the OK button to close the dialog box.

Under Appearance, find Title and change it to About Text Reader.

Find Window Flags at the bottom of the General pane. Uncheck the Deletable checkbox.

3.3 Add a Handler for the GtkAboutDialog response Signal

With the GtkAboutDialog still selected in Glade, click the Signals tab at the top right of Glade. Expand the GtkDialog item in the right pane.

Add a handler for the response signal called on_dlg_about_response

This handler will be called when the user clicks the Close button in the about dialog box.

3.4 Add a Handler for the About Menu Item under Help

A handler must be added to show the GtkAboutDialog when the user clicks the Help menu and then the About item.

Click the Help menu item in the main window in Glade. In the left pane of Glade, expand the selected GtkMenuItem by clicking the arrow at the left of it. This makes a GtkMenu appear. Expand this item by clicking the arrow at the left of it. Now click the GtkImageMenuItem that it reveals. This should be the About menu item.

In the right pane of Glade, under the General tab, give the About menu item an ID of menuitm_about.

Click the Signals tab. Add a handler called on_menuitm_about_activate for the activate signal.

4. Edit the C Source File for Opening the GtkAboutDialog

Open the main.c file from the src folder and make the following changes.

4.1 Add a Pointer to the About Box in the Widget Structure

The new pointer is the last element in the following code.

typedef struct {
    GtkWidget *w_txtvw_main;            // Pointer to text view object
    GtkWidget *w_dlg_file_choose;       // Pointer to file chooser dialog box
    GtkTextBuffer *textbuffer_main;     // Pointer to text buffer
    GtkWidget *w_dlg_about;             // Pointer to about dialog box
} app_widgets;

4.2 Get a Pointer to the About Box Widget

In main() where pointers are obtained for the other widgets, get a pointer to the about box as the following code shows.

widgets->w_dlg_about = GTK_WIDGET(gtk_builder_get_object(builder, "dlg_about"));

4.3 Add Handler Functions

Add a handler function for the About menu item from the Help menu.

// Help --> About
void on_menuitm_about_activate(GtkMenuItem *menuitem, app_widgets *app_wdgts)
{
    gtk_widget_show(app_wdgts->w_dlg_about);
}

Add a handler function for closing the about dialog box.

// About dialog box Close button
void on_dlg_about_response(GtkDialog *dialog, gint response_id, app_widgets *app_wdgts)
{
    gtk_widget_hide(app_wdgts->w_dlg_about);
}

The complete code listing for this project can be seen in the main.c file below.

main.c

#include <gtk/gtk.h>

typedef struct {
    GtkWidget *w_txtvw_main;            // Pointer to text view object
    GtkWidget *w_dlg_file_choose;       // Pointer to file chooser dialog box
    GtkTextBuffer *textbuffer_main;     // Pointer to text buffer
    GtkWidget *w_dlg_about;             // Pointer to about dialog box
} app_widgets;

int main(int argc, char *argv[])
{
    GtkBuilder      *builder; 
    GtkWidget       *window;
    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
    widgets->w_txtvw_main = GTK_WIDGET(gtk_builder_get_object(builder, "txtvw_main"));
    widgets->w_dlg_file_choose = GTK_WIDGET(gtk_builder_get_object(builder, "dlg_file_choose"));
    widgets->textbuffer_main = GTK_TEXT_BUFFER(gtk_builder_get_object(builder, "textbuffer_main"));
    widgets->w_dlg_about = GTK_WIDGET(gtk_builder_get_object(builder, "dlg_about"));
    
    gtk_builder_connect_signals(builder, widgets);

    g_object_unref(builder);

    gtk_widget_show(window);
    gtk_main();
    g_slice_free(app_widgets, widgets);

    return 0;
}

// File --> Open
void on_menuitm_open_activate(GtkMenuItem *menuitem, app_widgets *app_wdgts)
{
    gchar *file_name = NULL;        // Name of file to open from dialog box
    gchar *file_contents = NULL;    // For reading contents of file
    gboolean file_success = FALSE;  // File read status
    
    // Show the "Open Text File" dialog box
    gtk_widget_show(app_wdgts->w_dlg_file_choose);
    
    // Check return value from Open Text File dialog box to see if user clicked the Open button
    if (gtk_dialog_run(GTK_DIALOG (app_wdgts->w_dlg_file_choose)) == GTK_RESPONSE_OK) {
        // Get the file name from the dialog box
        file_name = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(app_wdgts->w_dlg_file_choose));
        if (file_name != NULL) {
            // Copy the contents of the file to dynamically allocated memory
            file_success = g_file_get_contents(file_name, &file_contents, NULL, NULL);
            if (file_success) {
                // Put the contents of the file into the GtkTextBuffer
                gtk_text_buffer_set_text(app_wdgts->textbuffer_main, file_contents, -1);
            }
            g_free(file_contents);
        }
        g_free(file_name);
    }

    // Finished with the "Open Text File" dialog box, so hide it
    gtk_widget_hide(app_wdgts->w_dlg_file_choose);
}

// File --> Close
void on_menuitm_close_activate(GtkMenuItem *menuitem, app_widgets *app_wdgts)
{
    // Clear the text from window "Close the file"
    gtk_text_buffer_set_text(app_wdgts->textbuffer_main, "", -1);
}

// File --> Quit
void on_menuitm_quit_activate(GtkMenuItem *menuitem, app_widgets *app_wdgts)
{
    gtk_main_quit();
}

// Help --> About
void on_menuitm_about_activate(GtkMenuItem *menuitem, app_widgets *app_wdgts)
{
    gtk_widget_show(app_wdgts->w_dlg_about);
}

// About dialog box Close button
void on_dlg_about_response(GtkDialog *dialog, gint response_id, app_widgets *app_wdgts)
{
    gtk_widget_hide(app_wdgts->w_dlg_about);
}

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

Leave a Reply

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