Screenshots are here:
http://www.micahcarrick.com/v2/content/view/30/20/
Simple little example of how to use GtkFontSelectionDialog and GtkColorSelectionDialog with C/GTK+ and libglade.
main.c and gui.glade can be downloaded here:
http://www.micahcarrick.com/files/dialogs2/dialogs2.tar.gz
Code:
/*
* main.c
* Copyright (C) Micah Carrick 2006 <email@micahcarrick.com>
*
* main.c is free software.
*
* You may redistribute it and/or modify it under the terms of the
* GNU General Public License, as published by the Free Software
* Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* main.c is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with main.c. If not, write to:
* The Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor
* Boston, MA 02110-1301, USA.
*/
/*
gcc -o dialogs2 -Wall -g main.c `pkg-config gtk+-2.0 --cflags --libs `-I/usr/include/libglade-2.0 -lglade-2.0
*/
#include <gtk/gtk.h>
#include <glade/glade.h>
/* glade interface file is in same path as the executable */
#define GLADE_FILE "gui.glade"
/* signal callbacks */
void on_window_destroy (GtkWidget *widget, gpointer user_data);
void on_font_button_clicked (GtkWidget *widget, gpointer user_data);
void on_color_button_clicked (GtkWidget *widget, gpointer user_data);
int
main (int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *label;
GladeXML *gxml;
/* initialize the GTK+ library */
gtk_init (&argc, &argv);
/*
create an instance of the GladeXML object and build widgets within
the "window" root node. We don't want to build the other nodes (the
two dialogs) until/unless we need them.
*/
gxml = glade_xml_new (GLADE_FILE, "window", NULL);
/* get the window & label widgets from the glade XML file */
window = glade_xml_get_widget(gxml, "window");
label = glade_xml_get_widget(gxml, "website_label");
/* connect destroy signal */
glade_xml_signal_connect (gxml, "on_window_destroy",
G_CALLBACK(on_window_destroy));
/* connect button signals and pass reference to label */
glade_xml_signal_connect_data (gxml, "on_color_button_clicked",
G_CALLBACK(on_color_button_clicked), label);
glade_xml_signal_connect_data (gxml, "on_font_button_clicked",
G_CALLBACK(on_font_button_clicked), label);
/* show the main window */
gtk_widget_show (window);
/* begin main GTK loop */
gtk_main ();
return 0;
}
void
on_window_destroy (GtkWidget *widget, gpointer user_data)
{
/* break gtk_main() loop */
gtk_main_quit();
}
void
on_font_button_clicked (GtkWidget *widget, gpointer user_data)
{
/*
The font dialog is inside the glade file. We build the widgets in
the glade file starting with "font_dialog" as the root node so as not
to re-build the window1 and asscoiated widgets.
*/
GladeXML *gxml = glade_xml_new (GLADE_FILE, "font_dialog", NULL);
GtkWidget *dialog = glade_xml_get_widget(gxml, "font_dialog");
GtkResponseType result;
result = gtk_dialog_run(GTK_DIALOG(dialog));
if (result == GTK_RESPONSE_OK || result == GTK_RESPONSE_APPLY)
{
/* If user clicks "Ok" or "Apply" we will set the font */
PangoFontDescription *font_desc;
gchar *font_name = gtk_font_selection_dialog_get_font_name(
GTK_FONT_SELECTION_DIALOG(dialog));
font_desc = pango_font_description_from_string (font_name);
/* assign font to the label widget which is passed in user_data */
gtk_widget_modify_font (GTK_WIDGET(user_data), font_desc);
/* free memory used by font name string */
g_free(font_name);
}
/* destroy widget */
gtk_widget_destroy(dialog);
}
void
on_color_button_clicked (GtkWidget *widget, gpointer user_data)
{
/*
The color dialog is inside the glade file. We build the widgets in
the glade file starting with "color_dialog" as the root node so as not
to re-build the window1 and asscoiated widgets.
*/
GladeXML *gxml = glade_xml_new (GLADE_FILE, "color_dialog", NULL);
GtkWidget *dialog = glade_xml_get_widget(gxml, "color_dialog");
GtkResponseType result;
result = gtk_dialog_run(GTK_DIALOG(dialog));
if (result == GTK_RESPONSE_OK)
{
/*
Get the GtkColorSelection widget which is part of the
GtkColorSelectionDialog named "color_select" in the glade file.
*/
GdkColor color;
GtkWidget *color_select = glade_xml_get_widget(
gxml,
"color_select");
gtk_color_selection_get_current_color(
GTK_COLOR_SELECTION(color_select),
&color);
/* modify the foreground color of the lable widget */
gtk_widget_modify_fg (GTK_WIDGET(user_data),
GTK_STATE_NORMAL,
&color);
}
/* destroy widget */
gtk_widget_destroy(dialog);
}