Previous: Simple Dialog Boxes with C/GTK+ and libglade
Next: AVR Controlled GP2D120 Distance Sensor

Color and Font Selection Dialogs in C/GTK+ with libglade

March 10th, 2006

In this example code, I show how to use the GtkFontSelectionDialog and the GtkColorSelectionDialog using C/GTK+ and libglade. Both dialogs as well as the main window are in the gui.glade file created by Glade.

The project can be downloaded here: dialogs2.tar.gz

GTK+ Dialogs Example

The code is fairly straight forward and there are comments throughout. The project compiles on my system using:

gcc -o dialogs2 -Wall -g main.c `pkg-config gtk+-2.0 --cflags --libs `-I/usr/include/libglade-2.0 -lglade-2.0

However, you may need to adjust the include path for libglade based on your distribution/installation. The file gui.glade must reside in the same path as the executable.

GTK+ Font Selection Dialog
GtkFontSelectionDialog

GTK+ Color Selection Dialog
GtkColorSelectionDialog

Some notes about the code

gxml = glade_xml_new (GLADE_FILE, "window", NULL);

Rather than passing NULL as the second parameter to glade_xml_new, I'm passing "window". This is the root node-- Only "window" and it's child widgets will be built by libglade. We don't want to build the dialogs until they are needed. Users may never even use the dialogs in a real world application, so why build them?

glade_xml_signal_connect_data (gxml, "on_color_button_clicked", 
                G_CALLBACK(on_color_button_clicked), label);

I'm using glade_xml_signal_connect_data() rather than glade_xml_signal_connect(). This allows me to pass the label widget to the callback function. The callback function can then manipulate that label based on the result of the dialog.



Categories
Linux GTK+ C Programming

Related Posts


Technorati Tags

Leave a Reply