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;
}
|