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:
gcc -o dialogs -Wall -g main.c `pkg-config gtk+-2.0 --cflags --libs `-I/usr/include/libglade-2.0 -lglade-2.0
Code:
#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;
}