00001
00006 #include "guihandler.h"
00007
00010 static GdkPixmap *pixmap = NULL;
00011
00013 GladeXML *xml;
00014
00017 currentGameState gameState;
00018
00019
00020
00021 void gui(int argc, char **argv)
00022 {
00023 gtk_init(&argc, &argv);
00024
00025
00026 xml = glade_xml_new(GLADE_GUI_FILE_NAME, 0, 0);
00027
00028
00029 glade_xml_signal_autoconnect(xml);
00030
00031
00032 GtkWidget *window = glade_xml_get_widget( xml , MAIN_WINDOW_NAME );
00033
00034
00035 const char titsep[2] = " v";
00036
00037 char titl[ strlen(PRGNAME) + strlen(PRGVERS) + strlen(titsep) ];
00038 sprintf( titl, "%s%s%s", PRGNAME, titsep, PRGVERS );
00039
00040 gtk_window_set_title( GTK_WINDOW(window), titl );
00041 gtk_window_set_icon_from_file( GTK_WINDOW(window), PRGLOGO, NULL );
00042
00043
00044 configure_event( glade_xml_get_widget(xml ,DRAW_AREA_NAME) , NULL , NULL );
00045
00046
00047 gameState = initializeCurrentGameState();
00048
00049
00050 gtk_main();
00051 }
00052
00053
00054 gboolean delete_event( GtkWidget *window,
00055 GdkEvent *event,
00056 gpointer data
00057 )
00058 {
00059
00060 D1(printf("\nGUIHANDLER.delete_event() - START\n"));
00061
00062 window = glade_xml_get_widget( xml , MAIN_WINDOW_NAME );
00063
00064
00065
00066 static GtkWidget *dialog = NULL;
00067 const unsigned short int YES = 1;
00068 const unsigned short int NO = 2;
00069 gint response;
00070
00071 if( ! dialog )
00072 {
00073 D4(printf("\nGUIHANDLER.delete_event() - (! dialog) -> creation \n"));
00074
00075 GtkWidget *label;
00076 GtkWidget *box;
00077
00078
00079 dialog = gtk_dialog_new();
00080
00081
00082 gtk_window_set_modal( GTK_WINDOW( dialog ), TRUE );
00083 gtk_window_set_transient_for( GTK_WINDOW( dialog ),
00084 GTK_WINDOW( window ) );
00085
00086
00087 gtk_window_set_title( GTK_WINDOW( dialog ), "Confirm" );
00088
00089
00090 gtk_dialog_add_button( GTK_DIALOG( dialog ), GTK_STOCK_YES, YES );
00091 gtk_dialog_add_button( GTK_DIALOG( dialog ), GTK_STOCK_NO, NO );
00092
00093
00094 label = gtk_label_new( "Are you sure you want to quit?" );
00095
00096
00097 box = gtk_dialog_get_content_area( GTK_DIALOG( dialog ) );
00098
00099 gtk_box_pack_start( GTK_BOX( box ), label, TRUE, TRUE, 0 );
00100
00101
00102 gtk_widget_show_all( dialog );
00103 }
00104
00105
00106 response = gtk_dialog_run( GTK_DIALOG( dialog ) );
00107 gtk_widget_hide( dialog );
00108
00109 D2(printf("\nAt question 'Exit?' user say %i (%s)\n",
00110 response,((response==YES)?"YES":"NO")));
00111
00112 D1(printf("\nGUIHANDLER.delete_event() - END\n"));
00113
00114 if( response == YES ){
00115 gtk_main_quit();
00116 return false;
00117 }
00118
00119 assert( response != YES );
00120
00121 return true;
00122 }
00123
00124
00125 void new_tournament( GtkWidget *window,
00126 GtkWidget *widget,
00127 gpointer data
00128 )
00129 {
00130
00131 D1(printf("\nGUIHANDLER.new_tournament() - START\n"));
00132
00133 GtkWidget *downlabel = glade_xml_get_widget( xml , DOWN_LABEL_NAME );
00134
00135 gameState.numOfRound = NEW_GAME_ROUND;
00136
00137
00138 if( window != NULL ){
00139 gameState.currentlevel = START_LEVEL;
00140 gameState.totalpoints = START_TOTAL_POINTS;
00141 }
00142
00143 char *schemafilename = get_schema_name( (gameState.currentlevel-1) );
00144
00145 if( strcmp(schemafilename , "" ) == 0 )
00146 {
00147 char errmsg[MAX_ERR_BUFFER_LEN];
00148 sprintf( errmsg , "WARNING: No such file in ./%s/%s%u/",
00149 GAMES_DIR,GAMES_LEVEL_DIR,(gameState.currentlevel-1) );
00150
00151 gtk_label_set_text( GTK_LABEL(downlabel), errmsg);
00152 gameState.playing = false;
00153 free(schemafilename);
00154 }
00155 else
00156 {
00157 double loadsuccess = newgame( schemafilename , &gameState.bestrouteweight );
00158 free(schemafilename);
00159
00160 if(loadsuccess<0){
00161 gtk_label_set_text( GTK_LABEL(downlabel),
00162 "WARNING: Error in schema validating.");
00163 gameState.playing = false;
00164 }
00165 else
00166 {
00167 clock_t tEnd, tStart = clock();
00168
00169 drawschema();
00170
00171 tEnd = clock();
00172
00173 double drawtime = (double)(tEnd-tStart)/CLOCKS_PER_SEC;
00174
00175 char msg[MAX_OTH_BUFFER_LEN];
00176 unsigned int i=0;
00177 for(i=0;i<strlen(msg);i++)
00178 msg[i] = NULLCHAR;
00179
00180 sprintf( msg,
00181 "Schema times in seconds: "\
00182 "Loaded & validated in %.3lf. Printed in %.3lf.",
00183 loadsuccess,
00184 drawtime
00185 );
00186
00187 D2(printf("%s",msg));
00188
00189 gtk_label_set_text( GTK_LABEL(downlabel), msg );
00190
00191
00192 gameState.currentRow = schemaData.startRow;
00193 gameState.currentCol = schemaData.startCol;
00194
00195 gameState.iters = weight( schemaData.gameMap
00196 [gameState.currentRow]
00197 [gameState.currentCol]
00198 );
00199
00200 gameState.playing = true;
00201
00202 time_t *tmpstart=NULL;
00203 gameState.starttime = time(tmpstart);
00204 free(tmpstart);
00205 }
00206 }
00207
00208 D1(printf("\nGUIHANDLER.new_tournament() - END\n"));
00209 }
00210
00211
00212 void drawschema()
00213 {
00214 D3(printf("\nGUIHANDLER.drawschema() - START\n"));
00215 char *col;
00216
00217 GtkWidget *drawarea = glade_xml_get_widget( xml , DRAW_AREA_NAME );
00218
00219
00220 col = color(CLOSE);
00221 draw_brush( drawarea,
00222 0, 0,
00223 drawarea->allocation.width, drawarea->allocation.height,
00224 col
00225 );
00226
00227
00228 const unsigned int drawX =
00229 (unsigned int)( (drawarea->allocation.width) /
00230 ( (schemaData.numCol>0)?(schemaData.numCol):(1) ) );
00231
00232 const unsigned int drawY =
00233 (unsigned int)( (drawarea->allocation.height) /
00234 ( (schemaData.numRow>0)?(schemaData.numRow):(1) ) );
00235
00236 unsigned int i=0, j=0;
00237 for(i=0; i<schemaData.numRow; i++)
00238 for(j=0; j<schemaData.numCol; j++)
00239 {
00240 if( schemaData.gameMap[i][j] != FREE )
00241 col = color( schemaData.gameMap[i][j] );
00242 else
00243 col = colorVisited( schemaData.visitedCellInfo[i][j] );
00244
00245
00246 draw_brush( drawarea, (j*drawX), (i*drawY), drawX, drawY, col );
00247 }
00248
00249 free( col );
00250
00251 D3(printf("\nGUIHANDLER.drawschema() - END\n"));
00252 }
00253
00254
00255 gboolean configure_event( GtkWidget *widget,
00256 GdkEventConfigure *event,
00257 gpointer data
00258 )
00259 {
00260 D1(printf("\nGUIHANDLER.configure_event() - START\n"));
00261
00262 if (pixmap)
00263 g_object_unref(pixmap);
00264
00265 pixmap = gdk_pixmap_new(widget->window,
00266 widget->allocation.width,
00267 widget->allocation.height,
00268 -1);
00269
00270 gdk_draw_rectangle (pixmap,
00271 widget->style->white_gc,
00272 TRUE,
00273 0, 0,
00274 widget->allocation.width,
00275 widget->allocation.height);
00276
00277 drawschema();
00278
00279 D1(printf("\nGUIHANDLER.configure_event() - END\n"));
00280
00281 return true;
00282 }
00283
00284
00285 gboolean expose_event( GtkWidget *widget,
00286 GdkEventExpose *event,
00287 gpointer data
00288 )
00289 {
00290 D5(printf("\nGUIHANDLER.expose_event() - START\n"));
00291
00292 gdk_draw_drawable(widget->window,
00293 widget->style->fg_gc[GTK_WIDGET_STATE (widget)],
00294 pixmap,
00295 event->area.x, event->area.y,
00296 event->area.x, event->area.y,
00297 event->area.width, event->area.height);
00298
00299 D5(printf("\nGUIHANDLER.expose_event() - END\n"));
00300
00301 return false;
00302 }
00303
00304
00305 void draw_brush ( GtkWidget *widget,
00306 gdouble x,
00307 gdouble y,
00308 gdouble width,
00309 gdouble height,
00310 char *col
00311 )
00312 {
00313 D3(printf("\nGUIHANDLER.draw_brush() "\
00314 "rectangle[%.0lf,%.0lf] in (%.0lf,%.0lf) color %s",
00315 width,height, x , y , col ));
00316
00317 GdkRectangle update_rect;
00318
00319 update_rect.x = x;
00320 update_rect.y = y;
00321 update_rect.width = width;
00322 update_rect.height = height;
00323
00324 GdkGC *gc = gdk_gc_new(pixmap);
00325 GdkColor color;
00326
00327
00328 gdk_color_parse (col,&color);
00329 gdk_gc_set_rgb_fg_color(gc, &color);
00330
00331 gdk_draw_rectangle (pixmap,
00332 gc,
00333 TRUE,
00334 update_rect.x, update_rect.y,
00335 update_rect.width, update_rect.height);
00336
00337 gtk_widget_queue_draw_area (widget,
00338 update_rect.x, update_rect.y,
00339 update_rect.width, update_rect.height);
00340
00341 gtk_widget_draw (glade_xml_get_widget( xml , DRAW_AREA_NAME ), &update_rect);
00342 }
00343
00344
00345
00346 void keypressed( GtkWidget *window,
00347 GdkEventKey *event,
00348 gpointer data
00349 )
00350 {
00351
00352 D1(printf("\nGUIHANDLER.keypressed() - START\n"));
00353
00354 if( gameState.playing )
00355 {
00356 assert( gameState.playing );
00357
00358 GtkWidget *drawarea = glade_xml_get_widget( xml , DRAW_AREA_NAME );
00359
00360
00361 const unsigned int drawX =
00362 (unsigned int)( (drawarea->allocation.width) /
00363 ( (schemaData.numCol>0)?(schemaData.numCol):(1) ) );
00364
00365 const unsigned int drawY =
00366 (unsigned int)( (drawarea->allocation.height) /
00367 ( (schemaData.numRow>0)?(schemaData.numRow):(1) ) );
00368
00369 char key[MAX_OTH_BUFFER_LEN];
00370 unsigned int i = 0;
00371 for(i=0;i<strlen(key);i++)
00372 key[i] = NULLCHAR;
00373
00374 checkKeyPressed( event->keyval, key, drawarea, drawX, drawY );
00375
00376 D2(printf("\nPressed key: %s\n",key));
00377
00378 gtk_label_set_text( GTK_LABEL(glade_xml_get_widget(xml,DOWN_LABEL_NAME)),
00379 key);
00380
00381 if(schemaData.gameMap[gameState.currentRow][gameState.currentCol]==DOOR)
00382 win();
00383 }
00384 else{
00385 assert( !gameState.playing );
00386
00387 char key[] = "Select 'new game' or 'open game' from menu to play!";
00388 gtk_label_set_text( GTK_LABEL(glade_xml_get_widget( xml,DOWN_LABEL_NAME )),
00389 key);
00390 }
00391
00392 D1(printf("\nGUIHANDLER.keypressed() - END\n"));
00393
00394 }
00395
00396
00397 void checkKeyPressed( int keyval,
00398 char *key,
00399 GtkWidget *drawarea,
00400 unsigned int drawX,
00401 unsigned int drawY
00402 )
00403 {
00404 D1(printf("\nGUIHANDLER.checkKeyPressed() - START\n"));
00405
00406 enum point_type { WALL , SCHEMA_END , VALID } current_point;
00407
00408 current_point = VALID;
00409
00410 switch(keyval)
00411 {
00412 case GDK_Up:
00413 {
00414 strcpy(key,"UP");
00415 if(gameState.currentRow>0)
00416 {
00417 if( isaccessible( schemaData.gameMap[gameState.currentRow-1]
00418 [gameState.currentCol] ) )
00419 {
00420 gameState.currentRow--;
00421 updateSchemaCell(drawarea, drawX, drawY);
00422 }
00423 else
00424 { current_point = WALL; }
00425 }
00426 else
00427 { current_point = SCHEMA_END; }
00428 break;
00429 }
00430
00431 case GDK_Down:
00432 {
00433 strcpy(key,"DOWN");
00434 if((gameState.currentRow+1)<schemaData.numRow)
00435 {
00436 if( isaccessible( schemaData.gameMap[gameState.currentRow+1]
00437 [gameState.currentCol] ) )
00438 {
00439 gameState.currentRow++;
00440 updateSchemaCell(drawarea, drawX, drawY);
00441 }
00442 else
00443 { current_point = WALL; }
00444 }
00445 else
00446 { current_point = SCHEMA_END; }
00447 break;
00448 }
00449
00450 case GDK_Right:
00451 {
00452 strcpy(key,"RIGHT");
00453 if((gameState.currentCol+1)<schemaData.numCol)
00454 {
00455 if( isaccessible( schemaData.gameMap[gameState.currentRow]
00456 [gameState.currentCol+1] ) )
00457 {
00458 gameState.currentCol++;
00459 updateSchemaCell(drawarea, drawX, drawY);
00460 }
00461 else
00462 { current_point = WALL; }
00463 }
00464 else
00465 { current_point = SCHEMA_END; }
00466 break;
00467 }
00468
00469 case GDK_Left:
00470 {
00471 strcpy(key,"LEFT");
00472 if(gameState.currentCol>0)
00473 {
00474 if(isaccessible(schemaData.gameMap [gameState.currentRow]
00475 [gameState.currentCol-1] ) )
00476 {
00477 gameState.currentCol--;
00478 updateSchemaCell(drawarea, drawX, drawY);
00479 }
00480 else
00481 { current_point = WALL; }
00482 }
00483 else
00484 { current_point = SCHEMA_END; }
00485 break;
00486 }
00487
00488 default: {
00489 strcpy(key,"UNKNOWN KEY! Use upper menu for any help...");
00490 break;
00491 }
00492 }
00493
00494 assert( strlen(key) > 0 );
00495
00496 char tmp_key[ strlen(key) ];
00497 strcpy( tmp_key , key );
00498
00499 if( current_point == WALL )
00500 sprintf(key,"CAN'T GO %s! (This is a wall :)",tmp_key);
00501 else
00502 if( current_point == SCHEMA_END )
00503 sprintf(key,"CAN'T GO %s! (Schema's end)",tmp_key);
00504
00505 D1(printf("\nGUIHANDLER.checkKeyPressed() - END\n"));
00506 }
00507
00508
00509
00510 void updateSchemaCell(GtkWidget *drawarea,
00511 unsigned int drawX,
00512 unsigned int drawY
00513 )
00514 {
00515
00516 D1(printf("\nGUIHANDLER.updateSchemaCell() - START\n"));
00517
00518 gameState.iters += weight( schemaData.gameMap[gameState.currentRow]
00519 [gameState.currentCol]
00520 );
00521
00522 visitCell( gameState.currentRow, gameState.currentCol );
00523
00524 draw_brush( drawarea,
00525 ( gameState.currentCol * drawX ),
00526 ( gameState.currentRow * drawY ),
00527 drawX,
00528 drawY,
00529 colorVisited( schemaData.visitedCellInfo[gameState.currentRow]
00530 [gameState.currentCol]
00531 )
00532 );
00533
00534 D1(printf("\nGUIHANDLER.updateSchemaCell() - END\n"));
00535 }
00536
00537 void win(){
00538
00539 D1(printf("\nGUIHANDLER.win() - START\n"));
00540
00541 GtkWidget *window = glade_xml_get_widget( xml , MAIN_WINDOW_NAME );
00542 GtkWidget *downlabel = glade_xml_get_widget( xml , DOWN_LABEL_NAME );
00543 GtkWidget *windialog = NULL;
00544
00545 char labeltxt[MAX_OTH_BUFFER_LEN];
00546 unsigned int i = 0;
00547 for(i=0;i<strlen(labeltxt);i++)
00548 labeltxt[i] = NULLCHAR;
00549
00550 gameState.totalpoints = calcSchemaResults( labeltxt , gameState );
00551
00552 windialog = gtk_message_dialog_new( GTK_WINDOW(window),
00553 GTK_DIALOG_MODAL,
00554 GTK_MESSAGE_INFO,
00555 GTK_BUTTONS_OK,
00556 "%s",labeltxt
00557 );
00558 gtk_window_set_title(GTK_WINDOW(windialog), "Win current schema.");
00559 gtk_window_set_transient_for( GTK_WINDOW( windialog ),
00560 GTK_WINDOW( window ) );
00561
00562
00563 gtk_dialog_run( GTK_DIALOG( windialog ) );
00564 gtk_widget_destroy ( windialog );
00565
00566 D2(printf("\nCurrent level %u of %u\n",
00567 gameState.currentlevel,gameState.numOfRound));
00568
00569 if( gameState.currentlevel < gameState.numOfRound )
00570 {
00571 gameState.currentlevel++;
00572 new_tournament(NULL, NULL, NULL);
00573 }
00574 else
00575 {
00576
00577 sprintf(labeltxt,"Tournament finished! Total points: %u",
00578 gameState.totalpoints);
00579 gtk_label_set_text(GTK_LABEL(downlabel),labeltxt);
00580
00581
00582 if( gameState.numOfRound > START_ROUND_NUMBER ){
00583 GtkWidget *winalldialog = NULL;
00584
00585 sprintf(labeltxt,"Tournament finished, YOU WIN!\n\nTotal points: %u",
00586 gameState.totalpoints);
00587
00588 winalldialog = gtk_message_dialog_new( GTK_WINDOW(window),
00589 GTK_DIALOG_MODAL,
00590 GTK_MESSAGE_INFO,
00591 GTK_BUTTONS_OK,
00592 "%s",labeltxt
00593 );
00594 gtk_window_set_title(GTK_WINDOW(winalldialog), "Tournament win!");
00595 gtk_window_set_transient_for( GTK_WINDOW( winalldialog ),
00596 GTK_WINDOW( window ) );
00597
00598
00599 gtk_dialog_run( GTK_DIALOG( winalldialog ) );
00600 gtk_widget_destroy ( winalldialog );
00601 }
00602
00603 char playerName[ MAX_OTH_BUFFER_LEN ];
00604 for( i=0; i<strlen(playerName); i++ )
00605 playerName[i] = NULLCHAR;
00606
00607 if( get_player_name( GTK_WIDGET(window), playerName ) )
00608 save_record( playerName,
00609 gameState.totalpoints,
00610 gameState.currentlevel );
00611
00612
00613 gameState.totalpoints = START_TOTAL_POINTS;
00614 gameState.currentlevel = START_LEVEL;
00615 gameState.playing = false;
00616
00617
00618 show_records(NULL,GTK_WINDOW(window));
00619 }
00620
00621 D1(printf("\nGUIHANDLER.win() - END\n"));
00622 }
00623
00624
00625
00626 void show_records( GtkWidget *widget,
00627 GtkWindow *window
00628 )
00629 {
00630
00631 D1(printf("\nGUIHANDLER.show_records() - START\n"));
00632
00633 unsigned int numRecord = 0, i = 0;
00634
00635 bool check = false;
00636 check = readSetGameRecord( &numRecord );
00637
00638 char *labeltxt = (char *)malloc( sizeof(char) *
00639 MAX_OTH_BUFFER_LEN *
00640 (numRecord+1) );
00641
00642 for( i=0; i<strlen(labeltxt); i++ )
00643 labeltxt[i] = NULLCHAR;
00644
00645 if( ! check )
00646 {
00647 sprintf( labeltxt, "Record file corrupted or missing.");
00648 sprintf( labeltxt, "%s\n\nPlease save a record to resolve.", labeltxt);
00649 remove( RECORD_FILE_NAME );
00650 }
00651 else
00652 {
00653 if( numRecord == 0 )
00654 sprintf( labeltxt , "No record saved." );
00655 else
00656 for( i=0; i<numRecord ; i++ ){
00657 sprintf( labeltxt,"%s%u) %s - score: %u level: %u \n",
00658 labeltxt, (i+1) ,gameRecord[i].name, gameRecord[i].points,
00659 gameRecord[i].level );
00660 }
00661 }
00662
00663
00664 GtkWidget *windialog = NULL;
00665
00666 windialog = gtk_message_dialog_new( GTK_WINDOW(window),
00667 GTK_DIALOG_MODAL,
00668 GTK_MESSAGE_INFO,
00669 GTK_BUTTONS_OK,
00670 "%s",labeltxt
00671 );
00672 gtk_window_set_title(GTK_WINDOW(windialog), "Records");
00673 gtk_window_set_transient_for( GTK_WINDOW( windialog ),
00674 GTK_WINDOW( window ) );
00675
00676
00677 gtk_dialog_run( GTK_DIALOG( windialog ) );
00678 gtk_widget_destroy ( windialog );
00679
00680 free(labeltxt);
00681
00682 D1(printf("\nGUIHANDLER.show_records() - END\n"));
00683 }
00684
00685
00686
00687 bool get_player_name( GtkWidget *window,
00688 gchar *input_text
00689 )
00690 {
00691 D1(printf("\nGUIHANDLER.get_player_name() - START\n"));
00692
00693 GtkWidget *dialog;
00694 GtkWidget *label;
00695 GtkWidget *input;
00696 GtkWidget *box;
00697
00698 bool returnresponse = true;
00699 bool go = true;
00700
00701 do
00702 {
00703
00704 dialog = gtk_dialog_new();
00705
00706
00707 gtk_window_set_modal( GTK_WINDOW( dialog ), TRUE );
00708 gtk_window_set_transient_for( GTK_WINDOW( dialog ),
00709 GTK_WINDOW( window ) );
00710
00711
00712 gtk_window_set_title( GTK_WINDOW( dialog ), "Save record" );
00713
00714 const unsigned short int OK = 1, CANCEL = 2;
00715
00716
00717 gtk_dialog_add_button( GTK_DIALOG( dialog ), GTK_STOCK_OK, OK );
00718 gtk_dialog_add_button( GTK_DIALOG( dialog ), GTK_STOCK_CANCEL, CANCEL);
00719
00720
00721 label = gtk_label_new( "Insert your name:" );
00722
00723
00724 input = gtk_entry_new();
00725 gtk_entry_set_max_length( GTK_ENTRY(input) , MAX_RECORDNAME_LEN - 1 );
00726 if(input_text != NULL )
00727 if( strlen(input_text)>0 )
00728 gtk_entry_set_text( GTK_ENTRY(input) , input_text );
00729
00730
00731 box = gtk_dialog_get_content_area( GTK_DIALOG( dialog ) );
00732
00733 gtk_box_pack_start( GTK_BOX( box ), label, TRUE, TRUE, 0 );
00734 gtk_box_pack_start( GTK_BOX( box ), input, TRUE, TRUE, 0 );
00735
00736
00737 gtk_widget_show_all( dialog );
00738
00739
00740 gint response = gtk_dialog_run( GTK_DIALOG( dialog ) );
00741
00742 go = true;
00743
00744 if( response==OK )
00745 {
00746 strcpy( input_text, gtk_entry_get_text(GTK_ENTRY(input)) );
00747
00748 go = checkResponseNameError(window, input_text);
00749 }
00750 else
00751 returnresponse = false;
00752
00753 gtk_widget_destroy( dialog );
00754
00755 D2(printf("\nUser pressed %s and insert value %s that is %s.\n",
00756 (response==OK)?("OK"):("CANCEL"),
00757 input_text, go?"VALID":"NOT VALID" ));
00758 }
00759 while(!go);
00760
00761 D1(printf("\nGUIHANDLER.get_player_name() - END\n"));
00762
00763 return returnresponse;
00764 }
00765
00766
00767 bool checkResponseNameError(GtkWidget *window, char *input_text)
00768 {
00769 D1(printf("\nGUIHANDLER.checkResponseNameError() - START\n"));
00770
00771 bool go = true;
00772 unsigned int i = 0, j = 0;
00773
00774 char labeltxt[MAX_ERR_BUFFER_LEN];
00775 for( j=0; j<strlen(labeltxt); j++ )
00776 labeltxt[j] = NULLCHAR;
00777
00778 if( strlen( input_text ) <= 0)
00779 {
00780 sprintf( labeltxt , "\nName is empty!");
00781 go = false;
00782 }
00783
00784 for(i = 0; ( i<strlen(input_text) )&& go; i++ )
00785 if( ! isallowedchar( LOWERCASE + UPPERCASE + DIGITS , input_text[i] ) )
00786 {
00787 sprintf( labeltxt , "Name error:\n\n");
00788 sprintf( labeltxt , "%s %uth character not allowed!\n",
00789 labeltxt, (i+1) );
00790 sprintf( labeltxt , "%s Display character is %c\n",
00791 labeltxt, input_text[i] );
00792 sprintf( labeltxt , "%s ASCII value is %i\n",
00793 labeltxt, (int)input_text[i] );
00794 go = false;
00795 }
00796
00797 if( !go )
00798 {
00799 GtkWidget *errdialog = NULL;
00800
00801 errdialog = gtk_message_dialog_new( GTK_WINDOW(window),
00802 GTK_DIALOG_MODAL,
00803 GTK_MESSAGE_WARNING,
00804 GTK_BUTTONS_OK,
00805 "%s",labeltxt
00806 );
00807 gtk_window_set_title( GTK_WINDOW(errdialog),
00808 "Name error." );
00809 gtk_window_set_transient_for( GTK_WINDOW( errdialog ),
00810 GTK_WINDOW( window ) );
00811
00812
00813 gtk_dialog_run( GTK_DIALOG( errdialog ) );
00814 gtk_widget_destroy ( errdialog );
00815 }
00816
00817 D1(printf("\nGUIHANDLER.checkResponseNameError() - END\n"));
00818
00819 return go;
00820
00821 }
00822
00823 void drawingAndSetSchemaInfo( double loadsuccess )
00824 {
00825 D1(printf("\nGUIHANDLER.drawingAndSetSchemaInfo() - START\n"));
00826
00827 clock_t tEnd, tStart;
00828
00829 tStart = clock();
00830
00831 drawschema();
00832
00833 tEnd = clock();
00834
00835
00836 double drawtime = (double)(tEnd-tStart)/CLOCKS_PER_SEC;
00837
00838 unsigned int i=0;
00839
00840 char msg[MAX_OTH_BUFFER_LEN];
00841 for(i=0;i<strlen(msg);i++)
00842 msg[i] = NULLCHAR;
00843
00844 sprintf( msg,
00845 "Schema times in seconds: "\
00846 "Loaded & validated in %.3lf. Printed in %.3lf.",
00847 loadsuccess, drawtime
00848 );
00849
00850 D2(printf("%s",msg));
00851
00852 gtk_label_set_text( GTK_LABEL(glade_xml_get_widget(xml, DOWN_LABEL_NAME) ),
00853 msg
00854 );
00855
00856
00857 gameState.currentRow = schemaData.startRow;
00858 gameState.currentCol = schemaData.startCol;
00859
00860
00861 gameState.iters = weight( schemaData.gameMap[gameState.currentRow]
00862 [gameState.currentCol] );
00863
00864 gameState.playing = true;
00865
00866 time_t *tmpstart=NULL;
00867 gameState.starttime = time(tmpstart);
00868 free(tmpstart);
00869
00870 D1(printf("\nGUIHANDLER.drawingAndSetSchemaInfo() - END\n"));
00871 }
00872
00873
00874 void open_game( GtkWidget *window,
00875 GtkWidget *widget,
00876 gpointer data
00877 )
00878 {
00879 D1(printf("\nGUIHANDLER.open_game() - START\n"));
00880
00881 gameState.numOfRound = START_ROUND_NUMBER;
00882 gameState.currentlevel = START_LEVEL;
00883 gameState.totalpoints = START_TOTAL_POINTS;
00884
00885 GtkWidget *dialog;
00886
00887 dialog = gtk_file_chooser_dialog_new( "Open Game",
00888 NULL,
00889 GTK_FILE_CHOOSER_ACTION_OPEN,
00890 GTK_STOCK_CANCEL,
00891 GTK_RESPONSE_CANCEL,
00892 GTK_STOCK_OPEN,
00893 GTK_RESPONSE_ACCEPT,
00894 NULL
00895 );
00896
00897 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(dialog), GAMES_DIR );
00898
00899 if( gtk_dialog_run(GTK_DIALOG(dialog)) == GTK_RESPONSE_ACCEPT )
00900 {
00901 char *filename;
00902
00903 filename = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
00904
00905 double loadsuccess = newgame( filename, &gameState.bestrouteweight );
00906
00907 gtk_widget_destroy( dialog );
00908
00909 g_free( filename );
00910
00911 if( loadsuccess<0 ){
00912 gtk_label_set_text( GTK_LABEL( glade_xml_get_widget( xml ,
00913 DOWN_LABEL_NAME )
00914 ),
00915 "WARNING: Error in schema validating."
00916 );
00917 gameState.playing = false;
00918 }
00919 else
00920 drawingAndSetSchemaInfo( loadsuccess );
00921
00922 }
00923 else
00924 gtk_widget_destroy (dialog);
00925
00926 D1(printf("\nGUIHANDLER.open_game() - END\n"));
00927 }
00928
00929
00930 void generate_game( GtkWidget *widget,
00931 GtkWindow *window
00932 )
00933 {
00934 D1(printf("\nGUIHANDLER.generate_game() - START\n"));
00935
00936
00937 GtkWidget *dialog;
00938
00939 dialog = gtk_file_chooser_dialog_new( "Save File",
00940 window,
00941 GTK_FILE_CHOOSER_ACTION_SAVE,
00942 GTK_STOCK_CANCEL,
00943 GTK_RESPONSE_CANCEL,
00944 GTK_STOCK_SAVE,
00945 GTK_RESPONSE_ACCEPT,
00946 NULL
00947 );
00948
00949 gtk_file_chooser_set_do_overwrite_confirmation( GTK_FILE_CHOOSER (dialog),
00950 TRUE );
00951
00952 gtk_file_chooser_set_current_folder( GTK_FILE_CHOOSER(dialog), GAMES_DIR );
00953
00954 gtk_file_chooser_set_current_name( GTK_FILE_CHOOSER(dialog),
00955 "Select file name" );
00956
00957
00958 if( gtk_dialog_run(GTK_DIALOG(dialog)) != GTK_RESPONSE_ACCEPT ){
00959 gtk_widget_destroy( dialog );
00960 D1(printf("\nGUIHANDLER.generate_game() - CANCEL and END\n"));
00961 return;
00962 }
00963
00964 char *filename;
00965 filename = gtk_file_chooser_get_filename( GTK_FILE_CHOOSER( dialog ) );
00966
00967 gtk_widget_destroy(dialog);
00968
00969 unsigned int rows, cols, difficult;
00970 if( ! request_generate_parameters( window, &rows, &cols, &difficult ) ){
00971 D1(printf("\nRequest_generate_parameters() CANCEL or ERROR\n"));
00972 D1(printf("\nGUIHANDLER.generate_game() - END (1)\n"));
00973 return;
00974 }
00975
00976 if( ! generateSchema( filename, rows, cols, difficult) ){
00977 D1(printf("\nGenerateSchema() ERROR\n"));
00978 D1(printf("\nGUIHANDLER.generate_game() - END (2)\n"));
00979 return;
00980 }
00981
00982 gameState.numOfRound = START_ROUND_NUMBER;
00983 gameState.currentlevel = START_LEVEL;
00984 gameState.totalpoints = START_TOTAL_POINTS;
00985
00986 double loadsuccess = newgame( filename, &gameState.bestrouteweight );
00987
00988 g_free(filename);
00989
00990 if( loadsuccess<0 ){
00991 gtk_label_set_text(GTK_LABEL(glade_xml_get_widget(xml,DOWN_LABEL_NAME)),
00992 "WARNING: Error in schema validating.");
00993 gameState.playing = false;
00994 }
00995 else
00996 drawingAndSetSchemaInfo( loadsuccess );
00997
00998 D1(printf("\nGUIHANDLER.generate_game() - END\n"));
00999 }
01000
01001
01002
01003 bool request_generate_parameters( GtkWindow *window,
01004 unsigned int *rows,
01005 unsigned int *cols,
01006 unsigned int *difficult
01007 )
01008 {
01009 D1(printf("\nGUIHANDLER.request_generate_parameters() - START\n"));
01010
01011 unsigned int i = 0;
01012 bool returnresponse = true;
01013 bool go = true;
01014 const unsigned short int OK = 1, CANCEL = 2;
01015
01016 GtkWidget *dialog;
01017 GtkWidget *label_rows;
01018 GtkWidget *input_rows;
01019 GtkWidget *label_cols;
01020 GtkWidget *input_cols;
01021 GtkWidget *label_difficult;
01022 GtkWidget *input_difficult;
01023 GtkWidget *box;
01024
01025 do
01026 {
01027
01028 dialog = gtk_dialog_new();
01029
01030 gtk_window_set_modal( GTK_WINDOW( dialog ), TRUE );
01031 gtk_window_set_transient_for( GTK_WINDOW( dialog ),
01032 GTK_WINDOW( window ) );
01033
01034 gtk_window_set_title( GTK_WINDOW( dialog ), "Set generate parameters");
01035
01036 gtk_dialog_add_button( GTK_DIALOG( dialog ), GTK_STOCK_OK, OK );
01037 gtk_dialog_add_button( GTK_DIALOG( dialog ), GTK_STOCK_CANCEL, CANCEL);
01038
01039
01040 char labmsg[MAX_OTH_BUFFER_LEN];
01041 for(i = 0; i<strlen(labmsg); i++)
01042 labmsg[i] = NULLCHAR;
01043
01044 sprintf(labmsg,"Insert number of ROWS (%u-%u):",MINR,MAXR);
01045 label_rows = gtk_label_new( labmsg );
01046 sprintf(labmsg,"Insert number of COLS (%u-%u):",MINC,MAXC);
01047 label_cols = gtk_label_new( labmsg );
01048 sprintf(labmsg,"Set difficult (%u-%u):",MIN_DIFFICULT,MAX_DIFFICULT);
01049 label_difficult = gtk_label_new( labmsg );
01050
01051
01052 input_rows = gtk_entry_new();
01053 input_cols = gtk_entry_new();
01054 input_difficult = gtk_entry_new();
01055
01056 char *rows_text = set_gtk_entry_text_from_num( MAXR , input_rows );
01057 char *cols_text = set_gtk_entry_text_from_num( MAXC , input_cols );
01058 char *difficult_text = set_gtk_entry_text_from_num( MAX_DIFFICULT ,
01059 input_difficult );
01060
01061
01062 box = gtk_dialog_get_content_area( GTK_DIALOG( dialog ) );
01063
01064 gtk_box_pack_start( GTK_BOX( box ), label_rows, TRUE, TRUE, 0 );
01065 gtk_box_pack_start( GTK_BOX( box ), input_rows, TRUE, TRUE, 0 );
01066 gtk_box_pack_start( GTK_BOX( box ), label_cols, TRUE, TRUE, 0 );
01067 gtk_box_pack_start( GTK_BOX( box ), input_cols, TRUE, TRUE, 0 );
01068 gtk_box_pack_start( GTK_BOX( box ), label_difficult, TRUE, TRUE, 0 );
01069 gtk_box_pack_start( GTK_BOX( box ), input_difficult, TRUE, TRUE, 0 );
01070
01071 gtk_widget_show_all( dialog );
01072
01073 gint response = gtk_dialog_run( GTK_DIALOG(dialog) );
01074
01075 go = true;
01076
01077 if(response==OK)
01078 {
01079
01080 strcpy( rows_text, gtk_entry_get_text(GTK_ENTRY(input_rows) ) );
01081 strcpy( cols_text, gtk_entry_get_text(GTK_ENTRY(input_cols) ) );
01082 strcpy( difficult_text,
01083 gtk_entry_get_text(GTK_ENTRY(input_difficult) ) );
01084
01085 go = checkResponseErrorGenerateParameters( window,
01086 rows_text,
01087 cols_text,
01088 difficult_text,
01089 rows,
01090 cols,
01091 difficult
01092 );
01093 }
01094 else
01095 returnresponse = false;
01096
01097 gtk_widget_destroy( dialog );
01098
01099 D2(printf("\nUser pressed %s and insert values that are %s.\n",
01100 (response==OK)?("OK"):("CANCEL"),go?"VALID":"NOT VALID"));
01101
01102 free(rows_text);
01103 free(cols_text);
01104 free(difficult_text);
01105
01106 }
01107 while(!go);
01108
01109 D1(printf("\nGUIHANDLER.request_generate_parameters() - END\n"));
01110
01111 return returnresponse;
01112
01113 }
01114
01115
01116 char *set_gtk_entry_text_from_num( unsigned int number,
01117 GtkWidget *entry
01118 )
01119 {
01120 D1(printf("\nGUIHANDLER.set_gtk_entry_text_from_num() - START\n"));
01121
01122 unsigned int numDigits = countDigits( number );
01123 const unsigned short int ADDITIONAL_CHAR_NUMBER = 1;
01124
01125 gtk_entry_set_max_length( GTK_ENTRY( entry ) , numDigits );
01126
01127 char *text = (char *)malloc( sizeof(char) * ( numDigits +
01128 ADDITIONAL_CHAR_NUMBER
01129 )
01130 );
01131 sprintf(text,"%u",number);
01132
01133 gtk_entry_set_text( GTK_ENTRY(entry) , text );
01134
01135 D1(printf("\nGUIHANDLER.set_gtk_entry_text_from_num() - END\n"));
01136 return text;
01137 }
01138
01139
01140
01141 bool checkResponseErrorGenerateParameters(GtkWindow *window,
01142 char *rows_text,
01143 char *cols_text,
01144 char *difficult_text,
01145 unsigned int *rows,
01146 unsigned int *cols,
01147 unsigned int *difficult
01148 )
01149 {
01150 D1(printf("\nGUIHANDLER.checkResponseErrorGenerateParameters() - START\n"));
01151
01152 bool go = true;
01153
01154 char err[MAX_ERR_BUFFER_LEN];
01155 char err_label[MAX_OTH_BUFFER_LEN];
01156
01157 strcpy( err_label , "ROWS" );
01158 go = check_if_text_is_number( go , rows_text, err, err_label );
01159
01160 strcpy( err_label , "COLS" );
01161 go = check_if_text_is_number( go , cols_text, err, err_label );
01162
01163 strcpy( err_label , "DIFFICULT" );
01164 go = check_if_text_is_number( go , difficult_text, err, err_label );
01165
01166
01167 if( go ){
01168 *rows = atoi(rows_text);
01169 *cols = atoi(cols_text);
01170 *difficult = atoi(difficult_text);
01171
01172 if( (*rows<MINR) || (*rows>MAXR) ){
01173 go = false;
01174 sprintf(err, "Error in ROWS:\n\n" );
01175 sprintf(err, "%s Wrong size: %u",err,*rows);
01176 }
01177
01178 if( (*cols<MINC) || (*cols>MAXC) ){
01179 go = false;
01180 sprintf(err, "Error in COLS:\n\n" );
01181 sprintf(err, "%s Wrong size: %u",err,*cols);
01182 }
01183
01184 if( (*difficult<MIN_DIFFICULT) || (*difficult>MAX_DIFFICULT) ){
01185 go = false;
01186 sprintf(err, "Error in DIFFICULT:\n\n" );
01187 sprintf(err, "%s Wrong size: %u",err,*difficult);
01188 }
01189 }
01190
01191 if( !go ){
01192
01193 GtkWidget *errdialog = NULL;
01194
01195 errdialog = gtk_message_dialog_new( GTK_WINDOW(window),
01196 GTK_DIALOG_MODAL,
01197 GTK_MESSAGE_WARNING,
01198 GTK_BUTTONS_OK,
01199 "%s",err
01200 );
01201 gtk_window_set_title( GTK_WINDOW(errdialog), "Parameters error." );
01202 gtk_window_set_transient_for( GTK_WINDOW( errdialog ),
01203 GTK_WINDOW( window ) );
01204
01205
01206 gtk_dialog_run( GTK_DIALOG( errdialog ) );
01207 gtk_widget_destroy ( errdialog );
01208 }
01209
01210 D1(printf("\nGUIHANDLER.checkResponseErrorGenerateParameters() - END\n"));
01211
01212 return go;
01213 }
01214
01215
01216 void show_help( GtkWidget *widget,
01217 GtkWidget *window,
01218 gpointer data
01219 )
01220 {
01221 D1(printf("\nGUIHANDLER.show_help() - START\n"));
01222
01223 window = glade_xml_get_widget( xml , MAIN_WINDOW_NAME );
01224
01225
01226
01227 static GtkWidget *dialog = NULL;
01228
01229 if( ! dialog )
01230 {
01231 D4(printf("\nGUIHANDLER.show_help() - (! dialog) -> creation \n"));
01232
01233 dialog = gtk_message_dialog_new(
01234 GTK_WINDOW(window),
01235 GTK_DIALOG_MODAL,
01236 GTK_MESSAGE_INFO,
01237 GTK_BUTTONS_OK,
01238 "HELP:\nsee README file for more informations.\n\n"\
01239 "Game keys:\nUse the arrows to move UP,DOWN,LEFT,RIGHT."
01240 );
01241
01242 gtk_window_set_title(GTK_WINDOW(dialog), "Help");
01243 gtk_window_set_transient_for( GTK_WINDOW( dialog ),
01244 GTK_WINDOW( window ) );
01245 }
01246
01247
01248 gtk_dialog_run( GTK_DIALOG( dialog ) );
01249 gtk_widget_hide( dialog );
01250
01251 D1(printf("\nGUIHANDLER.show_help() - END\n"));
01252 }
01253
01254
01255 void show_about( GtkWidget *widget,
01256 GtkWidget *window
01257 )
01258 {
01259 D1(printf("\nGUIHANDLER.show_about() - START\n"));
01260
01261
01262
01263 static GtkWidget *dialog = NULL;
01264
01265 if( ! dialog )
01266 {
01267 D4(printf("\nGUIHANDLER.show_about() - (! dialog) -> creation \n"));
01268
01269
01270 GtkAboutDialog *about;
01271 const gchar *auth[] = { PRGAUTH , NULL };
01272 GdkPixbuf *logo = gdk_pixbuf_new_from_file(PRGLOGO, NULL);
01273
01274
01275 dialog = gtk_about_dialog_new();
01276 gtk_window_set_transient_for( GTK_WINDOW( dialog ),
01277 GTK_WINDOW( window ) );
01278 about = GTK_ABOUT_DIALOG( dialog );
01279
01280
01281 gtk_about_dialog_set_program_name( about, PRGNAME );
01282 gtk_about_dialog_set_version( about, PRGVERS );
01283 gtk_about_dialog_set_copyright( about, PRGCPYR );
01284 gtk_about_dialog_set_license( about, PRGLICS );
01285 gtk_about_dialog_set_comments( about, PRGAIMS );
01286 gtk_about_dialog_set_website( about, PRGWBLK );
01287 gtk_about_dialog_set_authors( about, auth );
01288 gtk_about_dialog_set_logo(about,logo);
01289 g_object_unref(logo), logo = NULL;
01290
01291
01292 gtk_widget_show_all( dialog );
01293 }
01294
01295
01296 gtk_dialog_run( GTK_DIALOG( dialog ) );
01297 gtk_widget_hide( dialog );
01298
01299 D1(printf("\nGUIHANDLER.show_about() - END\n"));
01300 }
01301
01302
01303