1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| /*
* 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);
}
|