hello-world/src/dialog.c

Go to the documentation of this file.
00001 /*
00002  * File Name: dialog.c
00003  */
00004 
00005 /*
00006  * This file is part of hello-world.
00007  *
00008  * hello-world is free software: you can redistribute it and/or modify
00009  * it under the terms of the GNU General Public License as published by
00010  * the Free Software Foundation, either version 2 of the License, or
00011  * (at your option) any later version.
00012  *
00013  * hello-world is distributed in the hope that it will be useful,
00014  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00015  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00016  * GNU General Public License for more details.
00017  *
00018  * You should have received a copy of the GNU General Public License
00019  * along with this program. If not, see <http://www.gnu.org/licenses/>.
00020  */
00021 
00022 /**
00023  * Copyright (C) 2009 iRex Technologies B.V.
00024  * All rights reserved.
00025  */
00026 
00027 //----------------------------------------------------------------------------
00028 // Include Files
00029 //----------------------------------------------------------------------------
00030 
00031 // system include files, between < >
00032 #include <glib.h>
00033 #include <gtk/gtk.h>
00034 #include <signal.h>
00035 #include <string.h>
00036 #include <stdlib.h>
00037 #include <sys/types.h>
00038 #include <unistd.h>
00039 
00040 //----------------------------------------------------------------------------
00041 // Type Declarations
00042 //----------------------------------------------------------------------------
00043 
00044 
00045 //----------------------------------------------------------------------------
00046 // Constants
00047 //----------------------------------------------------------------------------
00048 
00049 GtkWidget       *g_main_window;
00050 
00051 
00052 //============================================================================
00053 // Functions Implementation
00054 //============================================================================
00055 
00056 // print usage text and quit
00057 static void usage (const char *argv_0)
00058 {
00059     static const char *usage_text = 
00060                         "\n"
00061                         "usage: %s <message>\n";
00062 
00063     printf(usage_text, argv_0);
00064 
00065     exit(1);
00066 }
00067 
00068 // terminate application
00069 void main_quit (void)
00070 {
00071     if (g_main_window)
00072     {
00073         gtk_widget_destroy(g_main_window);
00074         g_main_window = NULL;
00075     }    
00076     
00077     if (gtk_main_level() > 0)
00078     {
00079         gtk_main_quit();
00080     }
00081     else
00082     {
00083         _exit(0);
00084     }
00085 }
00086 
00087 // run error dialog
00088 static gboolean run_error_dialog (gpointer data)
00089 {
00090     GtkWidget   *dialog = NULL;
00091     gchar *msg = (gchar *) data;
00092 
00093     printf("Show dialog with message: [%s]\n", msg);
00094 
00095     dialog = gtk_message_dialog_new( GTK_WINDOW(g_main_window),
00096                                      GTK_DIALOG_DESTROY_WITH_PARENT,
00097                                      GTK_MESSAGE_INFO, //GTK_MESSAGE_ERROR,
00098                                      GTK_BUTTONS_OK,
00099                                      msg );
00100 
00101     gtk_dialog_run( GTK_DIALOG(dialog) );
00102     gtk_widget_destroy( dialog );
00103     main_quit();
00104     
00105     return FALSE;
00106 }
00107 
00108 // main function
00109 int main (int argc, char *argv[])
00110 {
00111     gchar *msg = NULL;
00112 
00113     // check command-line arguments
00114     if (argc < 2)
00115     {
00116         usage(argv[0]);
00117     }
00118 
00119     // init gtk, list the default rc files
00120     gtk_init(&argc, &argv);
00121 
00122     // create the top level window 
00123     g_main_window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
00124 
00125     if (argc > 0)
00126     {
00127         msg = g_strdup_printf("%s", argv[1]);
00128         g_idle_add(run_error_dialog, (gpointer) msg);
00129     }
00130     
00131     gtk_main();
00132     g_free(msg);
00133 
00134     return 0;
00135 }
00136 
Generated by  doxygen 1.6.2-20100208