Página Principal | Lista Alfabética | Lista de Componentes | Lista de Arquivos | Componentes Membros | Arquivos Membros

drv.c

Vá para a documentação deste arquivo.
00001 /*
00002 ** ---------------------------------------------------------------
00003 ** drv.c - Driver of second assignment of course CIV 2801 - 2004.2
00004 **
00005 ** ---------------------------------------------------------------
00006 **
00007 ** Project:
00008 **    CIV 2801 - Fundamentos de Computacao Grafica Aplicada - 2004.2
00009 **    Semester Projects.
00010 **    See documentation on class notes.
00011 **
00012 ** ---------------------------------------------------------------
00013 */
00014 
00039 /*
00040 ** ---------------------------------------------------------------
00041 ** Global variables and definitions:
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 ** Local variables and definitions:
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 ** Local auxiliary functions:
00082 */
00083 
00084 /* ======================  drvOpenFile  ======================= */
00085 
00094 static FILE *drvOpenFile( char *mode )
00095 {
00096  FILE    *fd;         /* file descriptor */
00097  Ihandle *dlg;        /* Ponteiro para o dialogo criado  */
00098  int     status;      /* return status of dialog to get file name */
00099 
00100  strcpy( file_name, "*.*" );
00101 
00102 /* Create a file open dialog.
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 /* Display file open dialog and get file name.
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 /* Open file.
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 ** Local callback functions:
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 /* ========================  drvResize  ======================= */
00157 
00170 static int drvResize( Ihandle *cv, int width, int height )
00171 {
00172  prjResize( width, height );
00173  return( IUP_DEFAULT );
00174 }
00175 
00176 /* ======================  drvRedisplay  ====================== */
00177 
00187 static int drvRedisplay( Ihandle *cv )
00188 {
00189  if( prjChkEmptyModel( ) )  /* In case model is empty, display help message */
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 /* Otherwise, resdiplay model and clear top message. */
00197  {
00198   prjRedisplay( );
00199   IupSetAttribute( Otopmsg, IUP_TITLE, " " );
00200  }
00201  return( IUP_DEFAULT );
00202 }
00203 
00204 /* ========================  drvMouseBt  ====================== */
00205 
00218 static int drvMouseBt( Ihandle *cv, int b, int mode,
00219                        int x, int y, char *mousecfg )
00220 {
00221  double xw, yw;   /* mouse position in world coordinates */
00222  int    status;
00223 
00224 /* Reject point if hit canvas is not the data screen.
00225  */
00226  if( cv != Odatascreen )
00227   return( IUP_DEFAULT );
00228 
00229 /* In case model is empty, do nothing.
00230  */
00231  if( prjChkEmptyModel( ) )
00232   return( IUP_DEFAULT );
00233 
00234 /* Disregard button release event.
00235  */
00236  if( mode == ButtonReleased )
00237   return( IUP_DEFAULT );
00238 
00239 /* Translate point from device (raster) coordinates to 
00240  * world coordinates.
00241  */
00242  cdCanvas2Raster( &x, &y );
00243  wdCanvas2World( x, y, &xw, &yw );
00244 
00245 /* Check to see whether mouse location is close to a node
00246  * of the current model and, if that is the case, process
00247  * the equilibrium of this node.
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 /* ========================  drvMouseMv  ====================== */
00275 
00286 static int drvMouseMv( Ihandle *cv, int x, int y, char *mousecfg )
00287 {
00288  return( IUP_DEFAULT );
00289 }
00290 
00291 /* =======================  drvKeyCrtl  ======================= */
00292 
00304 static int drvKeyCrtl( Ihandle *dg, int key )
00305 {
00306  FILE *fd;       /* pointer to file descriptor */
00307 
00308 /* Clear top message in the general case.
00309  */
00310  IupSetAttribute( Otopmsg, IUP_TITLE, " " );
00311 
00312 /* Take the appropriate action according to the pressed key.
00313  */
00314  switch( key )
00315  {
00316   case K_h:           /* h */
00317   case K_H:           /* 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:          /* Ctrl+q */
00325    prjQuit( );
00326    return( IUP_CLOSE );
00327 
00328   case K_cI:          /* Ctrl+i */
00329    prjInfo( );
00330    break;
00331 
00332   case K_cN:          /* Ctrl+n */
00333    prjNew( );
00334    break;
00335 
00336   case K_cO:          /* Ctrl+o */
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:          /* Ctrl+s */
00349    if( (fd = drvOpenFile( "w" )) != NULL )
00350    {
00351     prjSaveResults( fd );
00352     fclose( fd );
00353    }
00354    break;
00355 
00356   case K_i:           /* i */
00357   case K_I:           /* I */
00358    prjProcessInit( );
00359    break;
00360 
00361   case K_s:           /* s */
00362   case K_S:           /* 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:           /* g */
00369   case K_G:           /* 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:           /* l */
00376   case K_L:           /* 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:          /* Ctrl+p */
00385    prjPrint( );
00386    break;
00387 
00388   case K_cC:          /* Ctrl+c */
00389    prjClipboard( );
00390    break;
00391 
00392   case K_f:           /* f */
00393   case K_F:           /* 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:        /* arrow left */
00406    prjPanLeft( );
00407    break;
00408 
00409   case K_RIGHT:       /* arrow right */
00410    prjPanRight( );
00411    break;
00412 
00413   case K_DOWN:        /* arrow down */
00414    prjPanDown( );
00415    break;
00416 
00417   case K_UP:          /* arrow up */
00418    prjPanUp( );
00419    break;
00420 
00421   default:           /* any other key: redisplay */
00422    drvRedisplay( Odatascreen );
00423    break;
00424  }
00425 
00426  return( IUP_DEFAULT );
00427 }
00428 
00429 
00430 /*
00431 ** ---------------------------------------------------------------
00432 ** Main driver:
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 /* Get handle to interface objects.
00464  */
00465  Omain           = IupGetHandle( "main" );
00466  Otopmsg         = IupGetHandle( "topmsg" );
00467  Odatascreen     = IupGetHandle( "datascreen" );
00468 
00469 /* Realize user interface dialog.
00470  */
00471  IupMap( Omain );
00472 
00473 /* Register callback functions associated with each action.
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 /* Initialize project.
00482  */
00483  prjInit( );
00484 
00485 /* Exhibit user interface dialog.
00486  */
00487  IupShowXY( Omain, IUP_CENTER, IUP_CENTER );
00488 
00489 /* Pass control to user-interface system.
00490  */
00491  IupMainLoop( );
00492 
00493  IupClose( );
00494 
00495  return( 0 );
00496 }
00497 

Gerado em Tue Oct 5 04:55:00 2004 para Trab3 por doxygen 1.3.4