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 

Simple Dialogs in C/GTK+ (w/libglade) using GtkMessageDialog

 
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: 407
Location: Portland, OR USA

PostPosted: Fri Mar 10, 2006 1:08 am    Post subject: Simple Dialogs in C/GTK+ (w/libglade) using GtkMessageDialog Reply with quote



glade file and source files here: dialogs1.tar.gz

Uses libglade for the main window but the dialogs are implemented with code. Just shows the 4 types of GtkMessageDialog.

Compiles on my machine (FC4) using:
Code: (Plaintext)
1
gcc -o dialogs -Wall -g main.c `pkg-config gtk+-2.0 --cflags --libs `-I/usr/include/libglade-2.0 -lglade-2.0


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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210

#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_window1_destroy (GtkWidget *widget, gpointer user_data);
void on_quit_button_clicked (GtkWidget *widget, gpointer user_data);
void on_info_button_clicked (GtkWidget *widget, gpointer user_data);
void on_warning_button_clicked (GtkWidget *widget, gpointer user_data);
void on_error_button_clicked (GtkWidget *widget, gpointer user_data);
void on_question_button_clicked (GtkWidget *widget, gpointer user_data);

/* functions */
GtkResponseType message_dialog( GtkMessageType type,
                                GtkWindow *parent,
                                const gchar *title,
                                const gchar *message);
               
int
main (int argc, char *argv[])
{
       
        GtkWidget *main_window; 
        GladeXML *gxml;
       
        /* initialize the GTK+ library */
        gtk_init (&argc, &argv);

        /*
        create an instance of the GladeXML object and build widgets within
        the window1 root node.
        */
        gxml = glade_xml_new (GLADE_FILE, NULL, NULL);
       
        /* get the window widget from the glade XML file */
        main_window = glade_xml_get_widget (gxml, "window1");
       
        /* connect signals */
        glade_xml_signal_connect (gxml, "on_window1_destroy",
                G_CALLBACK(on_window1_destroy));
       
        glade_xml_signal_connect (gxml, "on_quit_button_clicked",
                G_CALLBACK(on_quit_button_clicked));
               
        glade_xml_signal_connect_data (gxml, "on_info_button_clicked",
                G_CALLBACK(on_info_button_clicked), main_window);

        glade_xml_signal_connect_data (gxml, "on_warning_button_clicked",
                G_CALLBACK(on_warning_button_clicked), main_window);
               
        glade_xml_signal_connect_data (gxml, "on_error_button_clicked",
                G_CALLBACK(on_error_button_clicked), main_window);

        glade_xml_signal_connect_data (gxml, "on_question_button_clicked",
                G_CALLBACK(on_question_button_clicked), main_window);
               
               
        /* show the main window */
        gtk_widget_show (main_window);

        /* begin main GTK loop */
        gtk_main ();
       
        return 0;     
}

void
on_window1_destroy (GtkWidget *widget, gpointer user_data)
{
        /* break gtk_main() loop */
        gtk_main_quit();                       
}

void
on_quit_button_clicked (GtkWidget *widget, gpointer user_data)
{
        /* break gtk_main() loop */
        gtk_main_quit();                       
}

void
on_info_button_clicked (GtkWidget *widget, gpointer user_data)
{
        /*
        Show message dialog.  We're casting the user_data into a GtkWindow
        because we passed window1 into the user_data when we connected the
        signals in main().  So window1 is going to the the "parent" for the
        dialog.
        */
        message_dialog(GTK_MESSAGE_INFO,
                        GTK_WINDOW(user_data),
                        "Information",
                        "Did you know that it snowed today in Portland?");                     
}

void
on_warning_button_clicked (GtkWidget *widget, gpointer user_data)
{
        /*
        Show message dialog.  We're casting the user_data into a GtkWindow
        because we passed window1 into the user_data when we connected the
        signals in main().  So window1 is going to the the "parent" for the
        dialog.
        */
        message_dialog(GTK_MESSAGE_WARNING,
                        GTK_WINDOW(user_data),
                        "Warning",
                        "I'm warning you for the last time!");                 
}

void
on_error_button_clicked (GtkWidget *widget, gpointer user_data)
{
        /*
        Show message dialog.  We're casting the user_data into a GtkWindow
        because we passed window1 into the user_data when we connected the
        signals in main().  So window1 is going to the the "parent" for the
        dialog.
        */
        message_dialog(GTK_MESSAGE_ERROR,
                        GTK_WINDOW(user_data),
                        "Error!",
                        "BOOM! You messed up bad this time buddy!");
                       
}

void
on_question_button_clicked (GtkWidget *widget, gpointer user_data)
{
        /*
        Show message dialog.  We're casting the user_data into a GtkWindow
        because we passed window1 into the user_data when we connected the
        signals in main().  So window1 is going to the the "parent" for the
        dialog.
        */
       
        GtkResponseType answer;
       
        answer = message_dialog(GTK_MESSAGE_QUESTION,
                        GTK_WINDOW(user_data),
                        "Question",
                        "Do you like GTK+ programming?");
                       
        if (answer == GTK_RESPONSE_YES)
        {
                message_dialog(GTK_MESSAGE_INFO,
                                GTK_WINDOW(user_data),
                                "Good!",
                                "Darn right you do!");
        }
        else if (answer == GTK_RESPONSE_NO)
        {
                message_dialog(GTK_MESSAGE_ERROR,
                                GTK_WINDOW(user_data),
                                "Uh Oh!",
                                "That's a horrible answer!");
        }
        else
        {
                message_dialog(GTK_MESSAGE_WARNING,
                                GTK_WINDOW(user_data),
                                "Hey!",
                                "Don't you ignore my question!");
        }
                       
}

GtkResponseType
message_dialog(GtkMessageType type,
                GtkWindow *parent,
                const gchar *title,
                const gchar *message)
{
        /*
        This function is used for all 4 types of GtkMessageDialog. See the API
        documentation for GtkMessageDialog for further information.
       
                type - 1 of 4 enum values for the type of message dialog.
                parent - The parent window for the dialog (window1)
                title - A string for the dialog box title
                message - A string to display in the dialog box
        */
       
        GtkWidget *dialog;
        GtkResponseType response;
        GtkButtonsType buttons;

        if (type == GTK_MESSAGE_QUESTION) buttons = GTK_BUTTONS_YES_NO;
        else buttons = GTK_BUTTONS_OK;
       
        /* create the dialog */
        dialog = gtk_message_dialog_new (parent,
                     GTK_DIALOG_DESTROY_WITH_PARENT,
                           type,
                        buttons,
                        message);
                       
        /* set title */
        gtk_window_set_title(GTK_WINDOW(dialog), title);
       
        /* show dialog */
     response = gtk_dialog_run (GTK_DIALOG (dialog));
    gtk_widget_destroy (dialog);   
       
        return response;
}
Back to top
flyjason
Familiar Face


Joined: 04 Mar 2007
Posts: 19

PostPosted: Sat Jun 09, 2007 11:32 am    Post subject: Reply with quote

Hello,

I'm getting a problem compiling this program dialogs1.tar.gz.

The error is:
Code: (Plaintext)
1
2
c:\program files\libglade\glade\glade-xml.h(26) : fatal error C1083: Cannot open include file: 'gtk/gtkdata.h': No such file or directory


I'm using Visual C++. I don't have gtkdata.h I think.

Can you please tell me everything (include files, etc) that I need in order to get this program running smoothly and also where can I get them?

Thanks
Back to top
caracal
GTK+ Geek


Joined: 21 Jun 2007
Posts: 75
Location: Wilkes Barre Pa

PostPosted: Fri Jun 29, 2007 6:47 am    Post subject: Reply with quote

You need
#include <gtk/gtk.h>
#include <glade/glade.h>

It looks like libglade can't find the gtk libs.

Im not sure how you would place the gtk+ libs in your path on ms windows.

Just go here http://gimp-win.sourceforge.net/stable.html and download the gtk libs
and install them.

And here is where you will find libglade
http://gladewin32.sourceforge.net/modules/wfdownloads/viewcat.php?cid=3

Install the gtk libs first then libglade

Are you planing on doing some cross platform development using visual C++ because
im not sure if an application built using visual c++ will work on linux or mac you will have to
make sure that your c++ application is compatable with glibc.
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