00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00039
00040
00041
00042
00043 #include <stdlib.h>
00044 #include <stdio.h>
00045 #include <string.h>
00046
00047 #include "iup.h"
00048 #include "iupkey.h"
00049 #include "cd.h"
00050 #include "wd.h"
00051
00052 #include "prj.h"
00053 #include "crs.h"
00054
00055
00056
00057
00058
00059
00061 static Ihandle *Omain;
00062
00064 static Ihandle *Otopmsg;
00065
00067 static char file_name[256];
00068
00070 static int dsp_load = 1;
00071
00073 #define ButtonPressed 1
00074
00076 #define ButtonReleased 0
00077
00078
00079
00080
00081
00082
00083
00084
00085
00094 static FILE *drvOpenFile( char *mode )
00095 {
00096 FILE *fd;
00097 Ihandle *dlg;
00098 int status;
00099
00100 strcpy( file_name, "*.*" );
00101
00102
00103
00104 dlg = IupFileDlg( );
00105 if( strcmp( mode, "r" ) == 0 )
00106 {
00107 IupSetAttribute( dlg, IUP_DIALOGTYPE, "OPEN" );
00108 }
00109 else
00110 {
00111 IupSetAttribute( dlg, IUP_DIALOGTYPE, "SAVE" );
00112 }
00113 IupSetAttribute( dlg, IUP_TITLE, "Trab3 get file name dialog" );
00114 IupSetAttribute( dlg, IUP_FILTER, "*.*" );
00115 IupSetAttribute( dlg, IUP_FILE, file_name );
00116
00117
00118
00119 IupPopup( dlg, IUP_ANYWHERE, IUP_ANYWHERE );
00120 status = IupGetInt( dlg, IUP_STATUS );
00121 if( status >= 0 )
00122 {
00123 strcpy( file_name, IupGetAttribute( dlg, IUP_VALUE ) );
00124 }
00125 else
00126 {
00127 return( NULL );
00128 }
00129
00130
00131
00132 fd = fopen( file_name, mode );
00133 if( fd == NULL )
00134 {
00135 IupMessage( " Mensagem de Erro ",
00136 "\n Nao foi possivel abrir arquivo! \n" );
00137 return( NULL );
00138 }
00139
00140 return( fd );
00141 }
00142
00143
00144
00145
00146
00147
00148 static int drvResize( Ihandle *cv, int width, int height );
00149 static int drvRedisplay( Ihandle *cv );
00150 static int drvMouseBt( Ihandle *cv, int b, int mode,
00151 int x, int y, char *mousecfg );
00152 static int drvMouseMv( Ihandle *cv, int x, int y, char *mousecfg );
00153 static int drvKeyCrtl( Ihandle *dg, int key );
00154
00155
00156
00157
00170 static int drvResize( Ihandle *cv, int width, int height )
00171 {
00172 prjResize( width, height );
00173 return( IUP_DEFAULT );
00174 }
00175
00176
00177
00187 static int drvRedisplay( Ihandle *cv )
00188 {
00189 if( prjChkEmptyModel( ) )
00190 {
00191 prjHelp( );
00192 IupSetAttribute( Otopmsg, IUP_TITLE,
00193 " Os comandos abaixo podem ser acionados"
00194 " de acordo com as teclas indicadas. " );
00195 }
00196 else
00197 {
00198 prjRedisplay( );
00199 IupSetAttribute( Otopmsg, IUP_TITLE, " " );
00200 }
00201 return( IUP_DEFAULT );
00202 }
00203
00204
00205
00218 static int drvMouseBt( Ihandle *cv, int b, int mode,
00219 int x, int y, char *mousecfg )
00220 {
00221 double xw, yw;
00222 int status;
00223
00224
00225
00226 if( cv != Odatascreen )
00227 return( IUP_DEFAULT );
00228
00229
00230
00231 if( prjChkEmptyModel( ) )
00232 return( IUP_DEFAULT );
00233
00234
00235
00236 if( mode == ButtonReleased )
00237 return( IUP_DEFAULT );
00238
00239
00240
00241
00242 cdCanvas2Raster( &x, &y );
00243 wdCanvas2World( x, y, &xw, &yw );
00244
00245
00246
00247
00248
00249 status = prjProcessNode( xw, yw );
00250 switch( status )
00251 {
00252 default:
00253 case CRS_SUCCESS:
00254 case CRS_NO_MODEL_DEFINED:
00255 IupSetAttribute( Otopmsg, IUP_TITLE, " " );
00256 break;
00257 case CRS_NO_STEP_TO_GO:
00258 IupSetAttribute( Otopmsg, IUP_TITLE,
00259 " Este no' ja' esta' equilibrado." );
00260 break;
00261 case CRS_FAILURE:
00262 IupSetAttribute( Otopmsg, IUP_TITLE,
00263 " Nenhum no' foi encontrado nesta posicao." );
00264 break;
00265 case CRS_FIXED_NODE:
00266 IupSetAttribute( Otopmsg, IUP_TITLE,
00267 " Este no' tem a rotacao fixa." );
00268 break;
00269 }
00270
00271 return( IUP_DEFAULT );
00272 }
00273
00274
00275
00286 static int drvMouseMv( Ihandle *cv, int x, int y, char *mousecfg )
00287 {
00288 return( IUP_DEFAULT );
00289 }
00290
00291
00292
00304 static int drvKeyCrtl( Ihandle *dg, int key )
00305 {
00306 FILE *fd;
00307
00308
00309
00310 IupSetAttribute( Otopmsg, IUP_TITLE, " " );
00311
00312
00313
00314 switch( key )
00315 {
00316 case K_h:
00317 case K_H:
00318 IupSetAttribute( Otopmsg, IUP_TITLE,
00319 " Os comandos abaixo podem ser acionados"
00320 " de acordo com as teclas indicadas. " );
00321 prjHelp( );
00322 break;
00323
00324 case K_cQ:
00325 prjQuit( );
00326 return( IUP_CLOSE );
00327
00328 case K_cI:
00329 prjInfo( );
00330 break;
00331
00332 case K_cN:
00333 prjNew( );
00334 break;
00335
00336 case K_cO:
00337 if( (fd = drvOpenFile( "r" )) != NULL )
00338 {
00339 if( ! prjOpenModel( fd ) )
00340 {
00341 IupMessage( " Mensagem de Erro ",
00342 "\n Erro nos dados do arquivo lido!\n" );
00343 }
00344 fclose( fd );
00345 }
00346 break;
00347
00348 case K_cS:
00349 if( (fd = drvOpenFile( "w" )) != NULL )
00350 {
00351 prjSaveResults( fd );
00352 fclose( fd );
00353 }
00354 break;
00355
00356 case K_i:
00357 case K_I:
00358 prjProcessInit( );
00359 break;
00360
00361 case K_s:
00362 case K_S:
00363 if( prjProcessStep( ) == CRS_NO_STEP_TO_GO )
00364 IupSetAttribute( Otopmsg, IUP_TITLE,
00365 " Todos os nos ja' estao equilibrados." );
00366 break;
00367
00368 case K_g:
00369 case K_G:
00370 if( prjProcessGoThru( ) == CRS_NO_STEP_TO_GO )
00371 IupSetAttribute( Otopmsg, IUP_TITLE,
00372 " Todos os nos ja' estao equilibrados." );
00373 break;
00374
00375 case K_l:
00376 case K_L:
00377 if( dsp_load )
00378 dsp_load = 0;
00379 else
00380 dsp_load = 1;
00381 prjLoadDisplay( dsp_load );
00382 break;
00383
00384 case K_cP:
00385 prjPrint( );
00386 break;
00387
00388 case K_cC:
00389 prjClipboard( );
00390 break;
00391
00392 case K_f:
00393 case K_F:
00394 prjFit( );
00395 break;
00396
00397 case K_plus:
00398 prjZoomIn( );
00399 break;
00400
00401 case K_minus:
00402 prjZoomOut( );
00403 break;
00404
00405 case K_LEFT:
00406 prjPanLeft( );
00407 break;
00408
00409 case K_RIGHT:
00410 prjPanRight( );
00411 break;
00412
00413 case K_DOWN:
00414 prjPanDown( );
00415 break;
00416
00417 case K_UP:
00418 prjPanUp( );
00419 break;
00420
00421 default:
00422 drvRedisplay( Odatascreen );
00423 break;
00424 }
00425
00426 return( IUP_DEFAULT );
00427 }
00428
00429
00430
00431
00432
00433
00434
00435
00447 int main( int argc, char* argv[] )
00448 {
00449 static char msg_alarm[512];
00450 char *msg_error;
00451
00452 IupOpen( );
00453
00454 msg_error = IupLoad( "trab3.led" );
00455 if( msg_error )
00456 {
00457 sprintf( msg_alarm, "\n%s\n", msg_error );
00458 IupAlarm( "Erro no arquivo trab3.led!", msg_alarm, " OK ", NULL, NULL );
00459 IupClose( );
00460 exit( 1 );
00461 }
00462
00463
00464
00465 Omain = IupGetHandle( "main" );
00466 Otopmsg = IupGetHandle( "topmsg" );
00467 Odatascreen = IupGetHandle( "datascreen" );
00468
00469
00470
00471 IupMap( Omain );
00472
00473
00474
00475 IupSetFunction( "drvResize", (Icallback)drvResize );
00476 IupSetFunction( "drvRedisplay", (Icallback)drvRedisplay );
00477 IupSetFunction( "drvMouseBt", (Icallback)drvMouseBt );
00478 IupSetFunction( "drvMouseMv", (Icallback)drvMouseMv );
00479 IupSetFunction( "drvKeyCrtl", (Icallback)drvKeyCrtl );
00480
00481
00482
00483 prjInit( );
00484
00485
00486
00487 IupShowXY( Omain, IUP_CENTER, IUP_CENTER );
00488
00489
00490
00491 IupMainLoop( );
00492
00493 IupClose( );
00494
00495 return( 0 );
00496 }
00497