int main(int argc, char* argv[]) { char caller_path[1024]; char message[2048]; int return_value=EXIT_ERROR; PinDialogInfo pindialog; // this struct contains all dialog objects gtk_init(&argc,&argv); // initialize gtk+ if(get_parent_path(caller_path, sizeof(caller_path)-2)>0) { if((argc==2) && (argv[1]!=NULL) && (strlen(argv[1])>0)) { snprintf(message,sizeof(message)-2,_MSG_(MSG_PLEASE_ENTER_PIN),caller_path,argv[1]); } else { fprintf(stderr,"Incorrect Parameter for <description of SPR>\n"); exit(EXIT_ERROR); } } else { fprintf(stderr,"Failed To Determine Parent Process. Aborting.\n"); exit(EXIT_ERROR); } // create new message dialog with CANCEL button in standard places, in center of user's screen /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// pindialog.dialog=gtk_message_dialog_new(NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_QUESTION,GTK_BUTTONS_NONE,message); gtk_dialog_set_default_response(GTK_DIALOG(pindialog.dialog),GTK_RESPONSE_OK); gtk_window_set_title(GTK_WINDOW(pindialog.dialog),_MSG_(MSG_PIN_CODE_REQUIRED)); gtk_window_set_position(GTK_WINDOW(pindialog.dialog), GTK_WIN_POS_CENTER); g_signal_connect (pindialog.dialog, "delete-event", G_CALLBACK (on_delete_event),&pindialog); // show all these widgets, and run the dialog as a modal dialog until it is closed by the user ////////////////////////////////////////////////////////////////////////////////////////////// gtk_widget_show_all(GTK_WIDGET(pindialog.dialog)); switch(gtk_dialog_run(GTK_DIALOG(pindialog.dialog))) { case GTK_RESPONSE_CANCEL: // if the use chose CANCEL printf("CANCEL\n"); // output CANCEL return_value=EXIT_OK; // and return OK (cancel is not an error) break; default: // otherwise printf("ERROR\n"); return_value=EXIT_ERROR; // output and return ERROR break; } // properly dispose of the dialog (which disposes of all it's children), and exit with specific return value ///////////////////////////////////////////////////////////////////////////////////////////////////////////// gtk_widget_destroy(pindialog.dialog); exit(return_value); }
// main program /////////////////////////////////////////////////////////////////////////////////////////////////// int main(int argc, char* argv[]) { int return_value=EXIT_ERROR; PinDialogInfo pindialog; char caller_path[1024]; gtk_init(&argc,&argv); // initialize gtk+ // create new message dialog with CANCEL and OK buttons in standard places, in center of user's screen /////////////////////////////////////////////////////////////////////////////////////////////////////////////////// if(get_parent_path(caller_path, sizeof(caller_path)-2)>0) { pindialog.dialog=gtk_message_dialog_new(NULL,GTK_DIALOG_MODAL,GTK_MESSAGE_QUESTION,GTK_BUTTONS_NONE,_MSG_(MSG_PLEASE_ENTER_OLD_AND_NEW_PINS), caller_path); } else { fprintf(stderr,"Failed To Determine Parent Process. Aborting.\n"); exit(EXIT_ERROR); } pindialog.cancelbutton =GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(pindialog.dialog),GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL)); pindialog.okbutton =GTK_BUTTON(gtk_dialog_add_button(GTK_DIALOG(pindialog.dialog),GTK_STOCK_OK, GTK_RESPONSE_OK)); gtk_dialog_set_default_response(GTK_DIALOG(pindialog.dialog),GTK_RESPONSE_OK); gtk_window_set_title(GTK_WINDOW(pindialog.dialog),_MSG_(MSG_CHANGE_PIN_CODE)); gtk_window_set_position(GTK_WINDOW(pindialog.dialog), GTK_WIN_POS_CENTER); g_signal_connect (pindialog.dialog, "delete-event", G_CALLBACK (on_delete_event),&pindialog); // create original, new, and verify new pin entry fields with labels, in a table /////////////////////////////////////////////////////////////////////////////////////////////////////////// pindialog.newPinsTable =gtk_table_new(3,2,TRUE); // table of 4 rows, 3 columns pindialog.originalPinLabel =gtk_label_new(_MSG_(MSG_CURRENT_PIN)); pindialog.newPin0Label =gtk_label_new(_MSG_(MSG_NEW_PIN)); pindialog.newPin1Label =gtk_label_new(_MSG_(MSG_NEW_PIN_AGAIN)); pindialog.originalPinEntry =gtk_entry_new(); pindialog.newPin0Entry =gtk_entry_new(); pindialog.newPin1Entry =gtk_entry_new(); // set max lengths gtk_entry_set_max_length(GTK_ENTRY(pindialog.originalPinEntry), MAX_PIN_LENGTH); gtk_entry_set_max_length(GTK_ENTRY(pindialog.newPin0Entry), MAX_PIN_LENGTH); gtk_entry_set_max_length(GTK_ENTRY(pindialog.newPin1Entry), MAX_PIN_LENGTH); // disable visibilities gtk_entry_set_visibility(GTK_ENTRY(pindialog.originalPinEntry), FALSE); gtk_entry_set_visibility(GTK_ENTRY(pindialog.newPin0Entry), FALSE); gtk_entry_set_visibility(GTK_ENTRY(pindialog.newPin1Entry), FALSE); // put labels and entries in a table gtk_table_attach(GTK_TABLE(pindialog.newPinsTable),pindialog.originalPinLabel, 0,1,0,1,(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),2,2); gtk_table_attach(GTK_TABLE(pindialog.newPinsTable),pindialog.newPin0Label, 0,1,1,2,(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),2,2); gtk_table_attach(GTK_TABLE(pindialog.newPinsTable),pindialog.newPin1Label, 0,1,2,3,(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),2,2); gtk_table_attach(GTK_TABLE(pindialog.newPinsTable),pindialog.originalPinEntry, 1,2,0,1,(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),2,2); gtk_table_attach(GTK_TABLE(pindialog.newPinsTable),pindialog.newPin0Entry, 1,2,1,2,(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),2,2); gtk_table_attach(GTK_TABLE(pindialog.newPinsTable),pindialog.newPin1Entry, 1,2,2,3,(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),(GtkAttachOptions)(GTK_SHRINK | GTK_FILL),2,2); // connect signals to filter and read inputs g_signal_connect(pindialog.originalPinEntry, "insert_text", G_CALLBACK(insert_only_digits), (gpointer)&pindialog); g_signal_connect(pindialog.newPin0Entry, "insert_text", G_CALLBACK(insert_only_digits), (gpointer)&pindialog); g_signal_connect(pindialog.newPin1Entry, "insert_text", G_CALLBACK(insert_only_digits), (gpointer)&pindialog); g_signal_connect(pindialog.originalPinEntry, "changed", G_CALLBACK(pins_changed), (gpointer)&pindialog); g_signal_connect(pindialog.newPin0Entry, "changed", G_CALLBACK(pins_changed), (gpointer)&pindialog); g_signal_connect(pindialog.newPin1Entry, "changed", G_CALLBACK(pins_changed), (gpointer)&pindialog); // add all these objects to the dialog /////////////////////////////////////////////////////////////////////////////////////////////////////////// gtk_container_set_border_width(GTK_CONTAINER(pindialog.dialog),10); gtk_box_pack_start(GTK_BOX(gtk_dialog_get_content_area(GTK_DIALOG(pindialog.dialog))),pindialog.newPinsTable, TRUE, TRUE,2); // initial state for OK button ///////////////////////////////////////////////////////////////////////////////////////////////////////// update_ok_button(&pindialog); // show all these widgets, and run the dialog as a modal dialog until it is closed by the user ////////////////////////////////////////////////////////////////////////////////////////////// gtk_widget_show_all(GTK_WIDGET(pindialog.dialog)); switch(gtk_dialog_run(GTK_DIALOG(pindialog.dialog))) { case GTK_RESPONSE_OK: // if the user chose OK { const char* oldpin=gtk_entry_get_text(GTK_ENTRY(pindialog.originalPinEntry)); const char* newpin=gtk_entry_get_text(GTK_ENTRY(pindialog.newPin0Entry)); printf("%s:%s\n",oldpin,newpin); // output the PINs to stdout return_value=EXIT_OK; // and return OK } break; default: // otherwise printf("CANCEL\n"); return_value=EXIT_OK; // output CANCEL and return ok (cancel is not an error) break; } // properly dispose of the dialog (which disposes of all it's children), and exit with specific return value ///////////////////////////////////////////////////////////////////////////////////////////////////////////// gtk_widget_destroy(pindialog.dialog); exit(return_value); }