- Prison Code Breaker Diary -

=> aka: Nhật Kí Code Tù

Categories

Now, I will introduce you a very, very popular widget that is used most of the time, GtkMessageDialog.

As you use software, sometimes it will pops up a small dialog that contains message which can be in types of either about Information, or Warning, or Question, or Error.
Like this:

Under Windows, it's known as MessageBox, right? You might be familiar with this Message Box already.

Let's look over its documentation.
Reference GtkMessageDialog

First of all, in the inherited hierarchy, GtkMessageDialog is from GtkDialog, so it certainly contains all the properties and accessing methods of GtkDialog. Simply, it's a pre-decorated dialog.

Secondly, these are the properties that you need to consider when creating a MessageDialog:
[+] Title: inherited from GtkWindow
[+] Dialog Flags: either GTK_DIALOG_MODAL or GTK_DIALOG_DESTROY_WITH_PARENT
[+] Message Type: is one of the four: GTK_MESSAGE_INFO, GTK_MESSAGE_WARNING, GTK_MESSAGE_QUESTION, GTK_MESSAGE_ERROR
[+] Button Type: either GTK_BUTTONS_*{ NONE, OK, CLOSE, CANCEL, YES_NO, OK_CANCEL } or you can create and add your own button by gtk_dialog_add_buttons().
[+] Secondary Text: is the detail info what you want to describe.

[!] 'Til now, since I haven't introduced about GtkDialog, but just stick with what I mention below:
- use gtk_dialog_run() to run the message dialog.
- gtk_dialog_run() return a response signal, therefore, always try to handle the signal.
- always use gtk_widget_destroy() to close message dialog, to avoid memory leak.
Some reference you need to read about what I mentioned:
Reference gtk_dialog_run()
Reference GtkResponseType

So far, it's enough for you at the present.

Practice: write a Gtk+ application that contains 4 buttons that will emits different types of GtkMessageDialog.


Try to read documentation and what I've mentioned so far then make your code. Make it simple and clear.

Below is my source for reference:


/*
* @author [JaPh]
* @date 15 August, 2009
* @file GtkMessageDialog_01.c
* @site http://xjaphx.blogspot.com/
*/

#include <gtk/gtk.h>

/* declare a message box widget */
GtkWidget *msg_box;

/* construct a message box dialog */
void
MessageBox( GtkMessageType mType, GtkButtonsType bType, const gchar *title,
const gchar *msg, const gchar *details )
{
/* message box response code */
int res;
/* create a message box dialog */
msg_box = gtk_message_dialog_new( NULL,
GTK_DIALOG_DESTROY_WITH_PARENT,
mType, bType, msg );
/* set message box title */
gtk_window_set_title( GTK_WINDOW( msg_box ), title );
/* set detail text */
gtk_message_dialog_format_secondary_text( GTK_MESSAGE_DIALOG( msg_box ), details );
/* run the message box dialog */
res = gtk_dialog_run( GTK_DIALOG( msg_box ) );
/* check for dialog response and handle */
if( res == GTK_RESPONSE_OK )
gtk_widget_destroy( msg_box );
}

void
btn_info_clicked( GtkButton *btn, gpointer data )
{
MessageBox( GTK_MESSAGE_INFO, GTK_BUTTONS_OK, "[JaPh] Dialog: INFO" ,
"GtkMessageDialog: Info", "This is an example of INFO Dialog" );
}
void
btn_warn_clicked( GtkButton *btn, gpointer data )
{
MessageBox( GTK_MESSAGE_WARNING, GTK_BUTTONS_OK, "[JaPh] Dialog: WARNING",
"GtkMessageDialog: Warning", "This is an example of WARNING Dialog" );
}
void
btn_ques_clicked( GtkButton *btn, gpointer data )
{
MessageBox( GTK_MESSAGE_QUESTION, GTK_BUTTONS_OK, "[JaPh] Dialog: QUESTION",
"GtkMessageDialog: Question", "This is an example of QUESTION Dialog" );
}
void
btn_erro_clicked( GtkButton *btn, gpointer data )
{
MessageBox( GTK_MESSAGE_ERROR, GTK_BUTTONS_OK, "[JaPh] Dialog: ERROR",
"GtkMessageDialog: Error", "This is an example of QUESTION Dialog" );
}

/* MAIN */
int
main( int argc, char *argv[] )
{
GtkWidget *window;
GtkWidget *vbox;
GtkWidget *btn_info, *btn_warn, *btn_ques, *btn_erro;
GtkWidget *btn_exit;

gtk_init( &argc, &argv );
/* construct main window */
window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
gtk_window_set_title( GTK_WINDOW( window ), "[JaPh]Lesson 11: GtkMessageDialog" );
gtk_widget_set_size_request( window, 200, 180 );
/* create vbox layout */
vbox = gtk_vbox_new( TRUE, 5 );
/* create buttons */
btn_info = gtk_button_new_with_mnemonic( "Show _Info" );
btn_warn = gtk_button_new_with_mnemonic( "Show _Warning" );
btn_ques = gtk_button_new_with_mnemonic( "Show _Question" );
btn_erro = gtk_button_new_with_mnemonic( "Show _Error" );
btn_exit = gtk_button_new_with_mnemonic( "E_xit" );
/* pack buttons to vbox from TOP to BOTTOM */
gtk_box_pack_start( GTK_BOX(vbox), btn_info, TRUE, TRUE, 0 );
gtk_box_pack_start( GTK_BOX(vbox), btn_warn, TRUE, TRUE, 0 );
gtk_box_pack_start( GTK_BOX(vbox), btn_ques, TRUE, TRUE, 0 );
gtk_box_pack_start( GTK_BOX(vbox), btn_erro, TRUE, TRUE, 0 );
gtk_box_pack_start( GTK_BOX(vbox), btn_exit, TRUE, TRUE, 0 );
/* add layout widget to main window */
gtk_container_add( GTK_CONTAINER(window), vbox );
/* button signal */
g_signal_connect( GTK_BUTTON( btn_info ), "clicked",
G_CALLBACK( btn_info_clicked ), NULL );
g_signal_connect( GTK_BUTTON( btn_warn ), "clicked",
G_CALLBACK( btn_warn_clicked ), NULL );
g_signal_connect( GTK_BUTTON( btn_ques ), "clicked",
G_CALLBACK( btn_ques_clicked ), NULL );
g_signal_connect( GTK_BUTTON( btn_erro ), "clicked",
G_CALLBACK( btn_erro_clicked ), NULL );

g_signal_connect( GTK_BUTTON( btn_exit), "clicked",
G_CALLBACK( gtk_main_quit ), NULL );
/* window signal */
g_signal_connect( window, "destroy", G_CALLBACK( gtk_main_quit ), NULL );
/* show window & widgets */
gtk_widget_show_all( window );

/* run Gtk+ app */
gtk_main();
return 0;
}

Remember to keep your practice always, don't forget!

Have fun!@

0 comments

Post a Comment