GTK+ Forums Forum Index GTK+ Forums
Discussion forum for GTK+ and Programming. Ask questions, troubleshoot problems, view and post example code, or express your opinions.
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

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

 
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code
Author Message
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 420
Location: Portland, OR USA

PostPosted: Fri Mar 10, 2006 10:09 pm    Post subject: Font and Color Selection dialogs in C/GTK+ with libglade Reply with quote

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: (Plaintext)
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);                         
}

Back to top
cnu_sree
GTK+ Geek


Joined: 06 Oct 2006
Posts: 57

PostPosted: Fri Oct 06, 2006 6:39 am    Post subject: very nice Reply with quote

iam new to gtk
iam getting thsese warning
while compling this coe
they are
(dialogs2:4413): libglade-WARNING **: unknown property `focus_on_map' for class `GtkWindow'

(dialogs2:4413): libglade-WARNING **: unknown property `ellipsize' for class `GtkLabel'

(dialogs2:4413): libglade-WARNING **: unknown property `width_chars' for class `GtkLabel'

(dialogs2:4413): libglade-WARNING **: unknown property `single_line_mode' for class `GtkLabel'

(dialogs2:4413): libglade-WARNING **: unknown property `angle' for class `GtkLabel'
Back to top
Micah Carrick
Never Seen the Sunlight


Joined: 21 Sep 2005
Posts: 420
Location: Portland, OR USA

PostPosted: Fri Oct 06, 2006 4:25 pm    Post subject: Reply with quote

What command did you use to compile and what Linux distro are you running?
Back to top
Display posts from previous:   
Post new topic   Reply to topic    GTK+ Forums Forum Index -> GTK+ Example Code All times are GMT
Page 1 of 1

 


Powered by phpBB © 2001, 2005 phpBB Group
CodeBB 1.0 Beta 2
Protected by Anti-Spam ACP