gitkHelloUser.c

Go to the documentation of this file.
00001 
00022 #include "config.h"
00023 
00024 #include <glib.h>
00025 #include <stdio.h>
00026 
00027 #include <libgitk/gitk.h>
00028 
00029 /*
00030 intltool-extract --type=gettext/xml gitkHelloUser_main.xml.in
00031 */
00032 
00034 gchar *user_name=NULL;
00036 gchar *sex=NULL;
00038 //gchar *age=NULL;
00040 gboolean ret=TRUE;
00041 
00042 //== global event handler ======================================================
00043 
00047 gboolean global_on_close(gpointer dialog,gchar *widget_id,GitkEventPtr event,gpointer user_data) {
00048   gitk_log("global_on_close()");
00049   ret=FALSE;
00050   return(TRUE);
00051 }
00052 
00053 
00055 gboolean global_on_dialog_show(gpointer dialog,gchar *widget_id,GitkEventPtr event,gpointer user_data) {
00056   gitk_log("global_on_dialog_show()");
00057   return(TRUE);
00058 }
00059 
00060 
00062 gboolean global_on_dialog_hide(gpointer dialog,gchar *widget_id,GitkEventPtr event,gpointer user_data) {
00063   gitk_log("global_on_dialog_hide()");
00064   return(TRUE);
00065 }
00066 
00067 
00068 //== main dialog ===============================================================
00069 
00071 gboolean main_on_clicked(gpointer dialog,gchar *widget_id,GitkEventPtr event,gpointer user_data) {
00072   gitk_log1("main_on_clicked(\"%s\")",widget_id);
00073   if(!strcmp(widget_id,"Okay")) {
00074     gitk_event_loop_end();
00075     ret=TRUE;
00076     return(TRUE);
00077   }
00078   else if(!strcmp(widget_id,"Cancel")) {
00079     gitk_event_loop_end();
00080     ret=FALSE;
00081     return(TRUE);
00082   }
00083   return(FALSE);
00084 }
00085 
00086 
00088 gboolean main_on_changed(gpointer dialog,gchar *widget_id,GitkEventPtr event,gpointer user_data) {
00089   char *value=gitk_widget_get_value(dialog,widget_id,"value");
00090 
00091   gitk_log2("main_on_changed(\"%s\") -> \"%s\"",widget_id, gitk_save_get_string(value));
00092   if(!strcmp(widget_id,"UserName")) {
00093     if(*((gchar **)user_data)) g_free(*((gchar **)user_data));
00094     *((gchar **)user_data)=value;
00095     return(TRUE);
00096   }
00097   else if(!strcmp(widget_id,"Sex")) {
00098     gint num_value=(value?atoi(value):0);
00099     if(*((gchar **)user_data)) g_free(*((gchar **)user_data));
00100     switch(num_value) {
00101       case 0:  *((gchar **)user_data)=g_strdup(_("Mr"));break;
00102       case 1:  *((gchar **)user_data)=g_strdup(_("Ms"));break;
00103       default: *((gchar **)user_data)=g_strdup(_("Mr/Ms"));break;
00104     }
00105     g_free(value);
00106   }
00107   return(FALSE);
00108 }
00109 
00110 
00114 gboolean main_show(void) {
00115   gboolean result=FALSE;
00116 
00117   gpointer const dialog=gitk_dialog_new_from_file(GIML_PATH"gitkHelloUser_main.xml");
00118   if(dialog) {
00119     if(gitk_dialog_show(dialog,FALSE)) {
00120       gitk_log("dialog parsed and shown");
00121       //-- main callbacks
00122       gitk_dialog_attach_callback(dialog,"Okay",GITK_EVENT_TYPE_ON_CLICKED,NULL,main_on_clicked);
00123       gitk_dialog_attach_callback(dialog,"Cancel",GITK_EVENT_TYPE_ON_CLICKED,NULL,main_on_clicked);
00124       gitk_dialog_attach_callback(dialog,"UserName",GITK_EVENT_TYPE_ON_CHANGED,&user_name,main_on_changed);
00125       gitk_dialog_attach_callback(dialog,"Sex",GITK_EVENT_TYPE_ON_CHANGED,&sex,main_on_changed);
00126       //-- start event loop
00127       gitk_log("run");
00128       gitk_event_loop_start();
00129       gitk_log("hiding");
00130       gitk_dialog_hide(dialog);
00131       gitk_log("dialog done");
00132       result=TRUE;
00133     }
00134     else {
00135       //-- couldn't happen
00136     }
00137     gitk_dialog_free(dialog);
00138   }
00139   else {
00140     gitk_printf(_("error parsing dialog => %d : %s\n"),gitk_get_error_code(),gitk_get_error_str());
00141   }
00142   return(result);
00143 }
00144 
00145 //== info message ==============================================================
00146 
00148 void info_show(void) {
00149   gchar *message;
00150 
00151   message=g_new(gchar,strlen(_("Hello %s %s !\n"))+strlen(sex)+strlen(user_name)+1-4);
00152   sprintf(message,_("Hello %s %s !\n"),sex,user_name);
00153   gitk_log1("opening message dialog : \"%s\"",message);
00154   gitk_template_message_with_title(message,_("Greetings"));
00155   g_free(message);  
00156 }
00157 
00158 //== main ======================================================================
00159 
00162 int main(int argc, char **argv) {
00163   guint logger_id;
00164   guint i;
00165   gboolean info_only=FALSE;
00166   gboolean info_help=FALSE, info_version=FALSE;
00167   gchar *cs;
00168 
00169   logger_id=g_log_set_handler(G_LOG_DOMAIN,G_LOG_LEVEL_DEBUG|G_LOG_LEVEL_MESSAGE|G_LOG_LEVEL_WARNING|G_LOG_LEVEL_ERROR, gitk_log_handler, NULL);
00170   gitk_log("-- beg -----------------------");
00171   gitk_log1("prog : %s",argv[0]);
00172 
00173   //DEBUG
00174   /*
00175   {
00176     void *dbg_this=dlopen(NULL,RTLD_GLOBAL|RTLD_LAZY);
00177     printf("function pointer test : local=%p dlsym=%p (%p)\n",global_on_close,dlsym(dbg_this,"global_on_close"),dbg_this);
00178   }
00179   */
00180   //DEBUG 
00181 
00182 #ifdef ENABLE_NLS
00183   {
00184     gchar *_locale;
00185     //-- at programm startup locale will be set to "C", this initialises it from the env-var
00186     _locale=setlocale(LC_ALL, "");
00187     bindtextdomain(PACKAGE,LOCALEDIR);
00188     bind_textdomain_codeset(PACKAGE,"UTF-8");
00189     textdomain(PACKAGE);
00190     cs=nl_langinfo(CODESET);
00191     gitk_log3("\"%s\" NLS enabled for domain \"%s\" in dir \"%s\"",(_locale?_locale:"-"),PACKAGE,LOCALEDIR);
00192     gitk_log1("locale codeset is \"%s\"",cs);
00193   }
00194 #else
00195   cs="ISO-8859-1";
00196 #endif
00197 
00198   //-- process commandline args
00199   for(i=1;i<(guint)argc;i++) {
00200     if(!strcmp("--help", argv[i]) || !strcmp("-h", argv[i])) {
00201       info_only=info_help=TRUE;
00202     }
00203     else if(!strcmp("--version", argv[i])) {
00204       info_only=info_version=TRUE;
00205     }
00206   }
00207   if(info_help) {
00208     gitk_printf(_("Usage: %s [OPTION...]\n\n"),argv[0]);
00209     gitk_puts(_("A demo application for the GITK (Generalized Interface ToolKit - see http://gitk.sf.net)."));
00210     gitk_puts(_("It presents a simple form-dialog, followed by a confirmative message.\n"));
00211     gitk_puts(_("  -h, --help\t\t\tthis help screen"));
00212     gitk_puts(_("      --version\t\t\tshow program version"));
00213     putchar('\n');
00214   }
00215   if(info_version) printf(PACKAGE_NAME"::%s: version "PACKAGE_VERSION" by "PACKAGE_BUGREPORT"\n",argv[0]); 
00216 
00217   //-- init gitk
00218   gitk_init((guint *)&argc,&argv,PACKAGE,LOCALEDIR);
00219   gitk_log("init ok ---------------------------");
00220 
00221   if(!info_only) {
00222     //-- global callbacks
00223     gitk_dialog_attach_callback(NULL,NULL,GITK_EVENT_TYPE_ON_CLOSE,NULL,global_on_close);
00224     gitk_dialog_attach_callback(NULL,NULL,GITK_EVENT_TYPE_ON_DIALOG_SHOW,NULL,global_on_dialog_show);
00225     gitk_dialog_attach_callback(NULL,NULL,GITK_EVENT_TYPE_ON_DIALOG_HIDE,NULL,global_on_dialog_hide);
00226 
00227     if(main_show() && user_name && sex) {
00228       gitk_log("main finished ---------------------");
00229       if(ret) info_show(); 
00230     }
00231     else {
00232       gitk_log("main aborted ----------------------");
00233       if(!user_name) gitk_log("  no user_name");
00234       if(!sex) gitk_log("  no sex");
00235     }
00236   
00237     //-- done gitk
00238     gitk_done();
00239   
00240     //-- do output after gitk-done !!
00241     if(user_name && sex && *user_name && *sex) {
00242       char *s1,*s2;
00243       //-- show result
00244       s1=g_convert(sex,-1,cs,"UTF-8",NULL,NULL,NULL);
00245       s2=g_convert(user_name,-1,cs,"UTF-8",NULL,NULL,NULL);
00246       if(s1 && s2) gitk_printf(_("Hello %s %s !\n"),s1,s2);
00247       g_free(s1);g_free(s2);
00248     }
00249     //-- free data
00250     g_free(user_name);
00251     g_free(sex);
00252   }
00253   else {
00254     //-- done gitk
00255     gitk_done();
00256   }
00257   gitk_log("-- end -----------------------\n\n");
00258   g_log_remove_handler(G_LOG_DOMAIN,logger_id);
00259   return(EXIT_SUCCESS);
00260 }

Generated on Thu Oct 28 10:59:08 2004 for gitk by doxygen 1.3.6