Virtual U.org
Get Personal Training on VU Today
    
Top shadow
 
 register/help
User Name:

Password:

OGET.CPP Source File
Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

OGET.CPP

Go to the documentation of this file.
00001 //Filename    : Get.CPP
00002 //Description : Object Get
00003 
00004 #include <string.h>
00005 #include <stdlib.h>
00006 #include <ctype.h>
00007 #include <time.h>
00008 
00009 #include <ALL.H>
00010 #include <KEY.H>
00011 #include <OSYS.H>
00012 #include <OVGA.H>
00013 #include <OFONT.H>
00014 #include <OMOUSE.H>
00015 #include <OGET.H>
00016 
00017 //-------- Define constant ----------//
00018 
00019 #define DEFAULT_DATE "DD/MM/YY"
00020 #define PICTURE_CHRS "!9#ALNYXCMD"
00021 
00022 //------- BEGIN OF FUNCTION Get::Get -----------//
00023 
00024 Get::Get() {                                      // constructor
00025     memset( this, 0, sizeof(Get) );
00026 
00027     font_ptr = &font_std;
00028 }
00029 
00030 //--------- END OF FUNCTION Get::Get-------------//
00031 
00032 //-------- Begin of function Get::field -----------//
00044 void Get::field(int desX, int y1, char* fldDes, int fldX1, char* dataPtr,
00045                 int dataWidth, int fldX2) {
00046     //-------------------------------------//
00047 
00048     fld_x1 = fldX1;                                 // for detect()
00049     fld_y1 = y1;
00050     fld_x2 = fldX2;
00051     fld_y2 = y1+font_ptr->height()+5;
00052 
00053   //-------------------------------------//
00054 
00055     init( 1, 0 );                                   // one field only for this get, 1-no updown exit
00056 
00057     // field description
00058     vga.d3_panel_up( desX, fld_y1, fld_x1, fld_y2, 1 );
00059 
00060     font_ptr->put( desX+3, fld_y1+3, fldDes );
00061 
00062                                                   // field content
00063     vga.d3_panel_down( fld_x1+4, fld_y1, fld_x2, fld_y2 );
00064 
00065     add  ( fld_x1+8, fld_y1+4, dataPtr, 'C', fld_x2-3 );
00066     width( dataWidth, dataWidth );
00067 
00068     display();
00069 }
00070 
00071 //----------- End of function Get::field -------------//
00072 
00073 //-------- Begin of function Get::field -----------//
00084 void Get::field(int x1, int y1, char* dataPtr, int dataWidth, int x2) {
00085     //-------------------------------------//
00086 
00087     fld_x1 = x1;                                    // for detect()
00088     fld_y1 = y1;
00089     fld_x2 = x2;
00090     fld_y2 = y1+font_ptr->height()+5;
00091 
00092     //-------------------------------------//
00093 
00094     init( 1, 0 );                                   // one field only for this get, 1-no updown exit
00095 
00096     // field content
00097     vga.d3_panel_down( fld_x1, fld_y1, fld_x2, fld_y2 );
00098 
00099     add  ( fld_x1+4, fld_y1+4, dataPtr, 'C', fld_x2-3 );
00100     width( dataWidth, dataWidth );
00101 
00102     display();
00103 }
00104 
00105 //----------- End of function Get::field -------------//
00106 
00107 //-------- Begin of function Get::detect -----------//
00109 int Get::detect() {
00110     if( get_num==0 )
00111         return 0;
00112 
00113     if( mouse.single_click(fld_x1, fld_y1, fld_x2, fld_y2 ) )
00114         return read();
00115 
00116     return 0;
00117 }
00118 
00119 //----------- End of function Get::detect -------------//
00120 
00121 //-------- BEGIN OF FUNCTION Get::init --------------//
00122 //
00123 // Description : Initiate all setting, allocate memory,
00124 //               this is called when a new GET object is defined.
00125 //
00126 // <int> = the no. of gets
00127 // <int> = exit when UP or DOWN is pressed at the first or last field
00128 //
00129 void Get::init(int maxGetNum,int updownExitFlag) {
00130     err_when( maxGetNum==0 );
00131 
00132     deinit();
00133 
00134     fld_array = (GetFld*) mem_add( sizeof(GetFld) * maxGetNum );
00135     memset(fld_array, NULL, sizeof(GetFld) * maxGetNum );
00136 
00137     max_get_num  = maxGetNum;
00138     up_down_exit = updownExitFlag;
00139 
00140     get_num=0;                                      // get_num is increased first, so that after running Get::add(), the get_num is still pointing to the last get
00141 }
00142 
00143 //------------- END OF FUNCTION Get::init --------------//
00144 
00145 //-------- BEGIN OF FUNCTION Get::deinit -----------//
00146 //
00147 void Get::deinit() {
00148     if (get_num>0 || max_get_num>0 ) {
00149         get_num=0;
00150         max_get_num=0;
00151     }
00152 
00153     if( fld_array ) {
00154         mem_del( fld_array );
00155         fld_array = NULL;
00156     }
00157 }
00158 
00159 //-------- END OF FUNCTION Get::deinit ------------//
00160 
00161 //----------- Begin of function Get::set_font ------------//
00168 void Get::set_font(Font* fontPtr) {
00169     font_ptr = fontPtr;
00170 }
00171 
00172 //------------- End of function Get::set_font ------------//
00173 
00174 //-------- BEGIN OF FUNCTION Get::add -----------//
00175 //
00176 // Description : add a GET element
00177 //
00178 // Syntax      : add( <int>,<int>,<void*>,<char>,[char] )
00179 //
00180 // <int>   = x
00181 // <int>   = y
00182 // <void*> = the data to be got
00183 // <char>  = type
00184 // <int>   = x2 of the field
00185 //
00186 // 'C'-Character
00187 // 'D'-Date
00188 // 'L'-Logical
00189 // 'N'-Numerical
00190 // 'l'-long
00191 // 'i'-integer
00192 // 'c'-char integer ( -128 - 127 )
00193 //
00194 // ** Note : Currently it only support "Character" type
00195 //
00196 void Get::add(int x, int y, void* data, char type, int x2) {
00197     err_when( get_num<0 || get_num >= max_get_num );
00198 
00199     err_if( type != 'C' )
00200         err_now( "Get class now only support Character type" );
00201 
00202     fld_ptr = fld_array + get_num;
00203 
00204     fld_ptr->x     = x;
00205     fld_ptr->y     = y;
00206     fld_ptr->data  = data;
00207     fld_ptr->type  = type;
00208     fld_ptr->x2    = x2;
00209 
00210     get_num++;
00211 }
00212 
00213 //----------- END OF FUNCTION Get::add -------------//
00214 
00215 //-------- BEGIN OF FUNCTION Get::picture ----------//
00216 //
00217 // Description : add a picture to the last get
00218 //
00219 // Syntax      : picture( <char*>,<int> )
00220 //
00221 // <char*> = picture
00222 // <int>   = convert all characters to upper case (default : 0)
00223 //
00224 void Get::picture( char* in_picture,int upper_convert ) {
00225     err_when( get_num<1 || get_num>max_get_num );
00226 
00227     fld_ptr->picture       = in_picture;
00228     fld_ptr->upper_convert = upper_convert;
00229 }
00230 
00231 //---------- END OF FUNCTION Get::picture -------------//
00232 
00233 //------- BEGIN OF FUNCTION Get::call -------------//
00234 //
00235 // Description : add the call routine pointer to the last get
00236 //
00237 // Syntax      : call( <*()>,<int> )
00238 //
00239 //
00240 void Get::call( GetCall *callPtr, int callData ) {
00241     err_when( get_num<1 || get_num>max_get_num );
00242 
00243     fld_ptr->call_ptr  = callPtr;
00244     fld_ptr->call_data = callData;
00245 }
00246 
00247 //-------- END OF FUNCTION Get::call --------------//
00248 
00249 //-------- BEGIN OF FUNCTION Get::valid  ----------//
00250 //
00251 // Description : add a valid function pointer to the last get
00252 //
00253 // Syntax      : valid( <*()> )
00254 //
00255 // <*()> = the valid function pointer
00256 //
00257 void Get::valid( char (*validPtr)() ) {
00258     err_when( get_num<1 || get_num>max_get_num );
00259 
00260     fld_ptr->valid_ptr = validPtr;
00261 }
00262 
00263 //-------- END OF FUNCTION Get::valid --------------//
00264 
00265 //------ BEGIN OF FUNCTION Get::width -------------//
00266 //
00267 // Description : Set the width of the last get field
00268 //
00269 // Syntax : width( <int>,<int> )
00270 //
00271 // <int> = the width of the data   ( width_data )
00272 // <int> = the width of the screen ( width_scr  )
00273 //
00274 void Get::width( int widthData, int widthScr ) {
00275     err_when( get_num<1 || get_num>max_get_num ||
00276               widthData > MAX_GET_WIDTH || widthScr > widthData );
00277 
00278     if ( widthData > 0)
00279         fld_ptr->width_data = widthData;
00280 
00281     if ( widthScr > 0)
00282         fld_ptr->width_scr  = widthScr;
00283 }
00284 
00285 //------- END OF FUNCTION Get::width -------------//
00286 
00287 //------ BEGIN OF FUNCTION Get::check_data -----------//
00288 //
00289 // Description : check_data the len, type of the data
00290 //
00291 // Syntax      : check_data(<char*>)
00292 //
00293 // <char*> = the get pointer
00294 //
00295 void Get::check_data( GetFld* getPtr ) {
00296     int   i, pictLen;
00297     char* ptr;
00298 
00299     if ( getPtr->picture == NULL )                  // get the length of the picture
00300         pictLen = 0;
00301     else
00302         pictLen = strlen( getPtr->picture );
00303 
00304     // if length of picture > 0, then the length of the get is equal to it
00305     // if not, than the length of the get is equal to the length of the data
00306 
00307     switch( getPtr->type ) {
00308     case 'C':
00309     case 'N':
00310     case 'D':
00311 
00312         //....... set data length ...........//
00313 
00314         if( getPtr->width_data <= 0 )
00315             getPtr->width_data = pictLen;             // if the length of the data is not defined picture length
00316 
00317         if( getPtr->width_data <= 0 ) {             // fill with the length of the data
00318             if( getPtr->type == 'D' )
00319                 getPtr->width_data = strlen(DEFAULT_DATE);
00320             else
00321                 getPtr->width_data = strlen( (char*) getPtr->data );
00322         }
00323 
00324         if ( getPtr->width_data <= 0 )
00325             getPtr->width_data = 1;
00326 
00327         //....... set data screen length ...........//
00328 
00329         if (getPtr->width_scr <= 0 || getPtr->width_scr > getPtr->width_data )
00330             getPtr->width_scr = getPtr->width_data;
00331 
00332         //....... Decimal place ..............//
00333 
00334         if ( getPtr->type == 'N' ) {
00335             if ( pictLen > 0 )
00336                 ptr = getPtr->picture;
00337             else
00338                 ptr = (char*) getPtr->data;
00339 
00340             for( i=1; i<= getPtr->width_data && *ptr ; i++,ptr++)
00341                 if (*ptr == '.') {
00342                     getPtr->dec_num = getPtr->width_data - i;
00343                     break;
00344                 }
00345 
00346             if ( getPtr->dec_num < 0 )
00347                 getPtr->dec_num = 0 ;
00348         }
00349         break;
00350 
00351     case 'L':
00352         getPtr->width_data = getPtr->width_scr = 1 ;
00353         break;
00354 
00355     case 'l':
00356     case 'i':
00357     case 'c':
00358         if ( getPtr->width_scr <= 0) {
00359             if ( pictLen > 0 )
00360                 getPtr->width_scr = pictLen;
00361             else {
00362                 switch( getPtr->type ) {
00363                 case 'l' :
00364                     getPtr->width_scr = 8;
00365                 case 'i' :
00366                     getPtr->width_scr = 4;
00367                 case 'c' :
00368                     getPtr->width_scr = 2;              // maximum 99
00369                 }
00370             }
00371         }
00372         getPtr->width_data = getPtr->width_scr ;
00373         break;
00374 
00375     case 'd':
00376         if ( pictLen > 0 ) {
00377             ptr = strchr( getPtr->picture, '.' );
00378             if ( ptr == NULL )
00379                 getPtr->dec_num = 0 ;
00380             else
00381                 getPtr->dec_num = pictLen - (int) getPtr->picture - 1;
00382 
00383             getPtr->width_scr = pictLen;
00384         }
00385         else {
00386             if ( getPtr->width_scr <= 0 )
00387                 getPtr->width_scr = 8;
00388 
00389             if ( getPtr->dec_num < 0 )
00390                 getPtr->dec_num = 2;
00391 
00392             if ( getPtr->dec_num >= getPtr->width_scr )
00393                 getPtr->dec_num = 0;
00394         }
00395 
00396         getPtr->width_data = getPtr->width_scr ;
00397         break;
00398     }
00399 }
00400 
00401 //--------- END OF FUNCTION Get::check_data ----------//
00402 
00403 //------- BEGIN OF FUNCTION Get::load_data ---------//
00404 //
00405 // Description : convert the raw data to pictured data
00406 //
00407 // Syntax : load_data( <GetFld*>, <char*> )
00408 //
00409 // <GetFld*> = the pointer to the get
00410 //
00411 // <char*>   = the destination buffer to store the pictured data
00412 //
00413 //
00414 int Get::load_data( GetFld *getPtr, char *chr_buf ) {
00415     //------- Currently it only support "Character" type -----//
00416     /*
00417       char* pict_char;
00418       int   len;
00419 
00420       if ( getPtr->type == 'd' )
00421       {
00422       ptr = c1dtoa( * ((double *) getPtr->data), getPtr->width_scr,
00423       getPtr->dec_num);
00424       memcpy( chr_buf, ptr, (size_t) getPtr->width_scr ) ;
00425       return( getPtr->width_scr ) ;
00426       }
00427 
00428       if ( getPtr->type == 'l' )
00429       {
00430       c1ltoa( * ((long *) getPtr->data), chr_buf, getPtr->width_scr ) ;
00431       return( getPtr->width_scr ) ;
00432       }
00433 
00434       if ( getPtr->type == 'i' )
00435       {
00436       c1ltoa((long) (* ((int *)getPtr->data)), chr_buf, getPtr->width_scr) ;
00437       return( getPtr->width_scr ) ;
00438       }
00439 
00440       if ( getPtr->type == 'c' )
00441       {
00442       c1ltoa((long) (* ((char *)getPtr->data)), chr_buf, getPtr->width_scr) ;
00443       return( getPtr->width_scr ) ;
00444       }
00445 
00446       if ( getPtr->type == 'D' )
00447       {
00448       if ( getPtr->picture == (char *) 0 )
00449       {
00450       ptr =  c1dt_format( (char *) getPtr->data, DEFAULT_DATE ) ;
00451       len =  (int) strlen(DEFAULT_DATE ) ;
00452       }
00453       else
00454       {
00455       ptr =  c1dt_format( (char *) getPtr->data, getPtr->picture ) ;
00456       len =  (int) strlen( getPtr->picture ) ;
00457       }
00458 
00459       memcpy( chr_buf, ptr, (size_t) len ) ;
00460       return( len ) ;
00461       }
00462 
00463       if ( getPtr->type == 'L' )
00464       {
00465       if ( getPtr->picture == (char *) 0 )
00466       pict_char =  'L' ;
00467       else
00468       pict_char =  getPtr->picture[0] ;
00469 
00470       if ( * ((int *) getPtr->data) )
00471       {
00472       if ( pict_char == 'L' )
00473       chr_buf[0] = 'T' ;
00474       else
00475       chr_buf[0] = 'Y' ;
00476       }
00477       else
00478       {
00479       if ( pict_char == 'L' )
00480       chr_buf[0] =  'F' ;
00481       else
00482       chr_buf[0] =  'N' ;
00483       }
00484       return 1 ;
00485       }
00486 
00487       if ( getPtr->type == 'N' && getPtr->dec_num > 0 )
00488       {
00489       ( (char *)getPtr->data )
00490       [ getPtr->width_data - getPtr->dec_num - 1] = '.' ;
00491       }
00492     */
00493 
00494     //------- Currently it only support "Character" type -----//
00495 
00496     err_when( (int) strlen((char*)getPtr->data) > (int) getPtr->width_data );
00497 
00498     //---- fill the string with tailing space in the whole data width ---//
00499 
00500     memset( chr_buf, ' ', getPtr->width_data );     // char after the NULL will be unknown in the original data
00501 
00502     if( mask_flag )
00503         // masked the field for displaying hidden data (like password)
00504         memset( chr_buf, '*', strlen((char*)getPtr->data) );
00505     else
00506         // don't copy the NULL character
00507         memcpy( chr_buf, (char*)getPtr->data, strlen((char*)getPtr->data) );
00508 
00509     chr_buf[getPtr->width_data] = NULL;
00510 
00511     //---- if has picture, process the character buf with the picture ----//
00512 
00513     char* ptr = getPtr->picture;
00514 
00515     if( ptr ) {
00516         for ( int i= 0; *ptr != '\0'; i++, ptr++ ) {
00517             if ( strchr( PICTURE_CHRS, *ptr) == (char *) 0 )
00518                 chr_buf[i] =  getPtr->picture[i] ;
00519         }
00520     }
00521 
00522     return( getPtr->width_data ) ;
00523 }
00524 
00525 //--------- END OF FUNCTION Get::load_data ----------//
00526 
00527 //------- BEGIN OF FUNCTION Get::display_get ---------//
00528 //
00529 // Description : display one get a time
00530 //
00531 // Syntax      : display_get( <GetFld*>,<int> )
00532 //
00533 // <GetFld*> = the pointer to get
00534 // <int>     = 1-Getting field, 0-Displaying field
00535 //
00536 //
00537 void Get::display_get( GetFld *getPtr,int getting) {
00538     char  buf[MAX_GET_WIDTH+1] ;
00539 
00540     load_data( getPtr, buf ) ;
00541 
00542     if( getting || getPtr->data )
00543         font_ptr->disp( getPtr->x, getPtr->y, buf, getPtr->x2 );
00544 
00545     sys.blt_virtual_buf();
00546 }
00547 
00548 //-------- END OF FUNCTION Get::display_get ------------//
00549 
00550 //------ BEGIN OF FUNCTION Get::display ---------------//
00551 //
00552 // Description : display all gets without read
00553 //
00554 // Syntax      : display()
00555 //
00556 void Get::display() {
00557     GetFld *getPtr ;
00558     int    i      ;
00559 
00560     for (i=0; i<get_num ; i++ ) {
00561         getPtr = fld_array + i;
00562 
00563         check_data( getPtr );
00564         display_get( getPtr, 0 );
00565     }
00566 }
00567 
00568 //-------- END OF FUNCTION Get::display ---------------//
00569 
00570 //-------- BEGIN OF FUNCTION Get::store_data --------------//
00571 //
00572 // Description : fill the get buffer with data
00573 //
00574 // Syntax      : store_data( <GetFld*>,<char*>,<char*>)
00575 //
00576 // <GetFld*> = get pointer
00577 // <char*>   = buffer pointer
00578 // <char*>   = picture pointer
00579 //
00580 
00581 void Get::store_data( GetFld *getPtr, char *buffer, char *pict ) {
00582     //------- Currently it only support "Character" type -----//
00583 
00584     /*
00585       // Transfer Back to  getPtr->data //
00586       switch ( getPtr->type )
00587       {
00588       case 'd':
00589       *((double *)getPtr->data) =
00590       c1atod( buffer, getPtr->width_scr ) ;
00591       break ;
00592 
00593       case 'N':
00594       memcpy( getPtr->data,
00595       c1dtoa( c1atod(buffer, getPtr->width_data),
00596       getPtr->width_data, getPtr->dec_num),
00597       (size_t) getPtr->width_data ) ;
00598       break ;
00599 
00600       case 'c':
00601       *((char*)getPtr->data) =
00602       (char) c1atoi( (char *) buffer, getPtr->width_scr ) ;
00603       break ;
00604 
00605       case 'i':
00606       *((int *)getPtr->data) =
00607       (int) c1atoi( (char *) buffer, getPtr->width_scr ) ;
00608       break ;
00609 
00610       case 'l':
00611       buffer[getPtr->width_scr] =  '\0' ;
00612       *((long *)getPtr->data) =  (long) atol( (char *) buffer ) ;
00613       break ;
00614 
00615       case 'D':
00616       memcpy( getPtr->data, c1dt_unformat((char *) buffer, pict), 8) ;
00617       break;
00618 
00619       case 'L':
00620       if ( buffer[0] == (int) 'Y'  ||  buffer[0] == (int) 'T' ||
00621       buffer[0] == (int) 't' || buffer[0] == (int) 'y' )
00622       *((int *)getPtr->data) =  1 ;
00623       else
00624       *((int *)getPtr->data) =  0 ;
00625       break ;
00626 
00627       default:
00628       memcpy( getPtr->data, buffer, getPtr->width_data ) ;
00629       getPtr->data[getPtr->width_data]=NULL;
00630       break ;
00631       }
00632     */
00633     //------- Currently it only support "Character" type -----//
00634 
00635     if( getPtr->type=='C' ) {
00636         memcpy( getPtr->data, buffer, getPtr->width_data ) ;
00637 
00638         ((char*)getPtr->data)[getPtr->width_data] = NULL;
00639 
00640         m.rtrim( (char*) getPtr->data );              // right rtrim the character
00641     }
00642 }
00643 
00644 //---------- END OF FUNCTION Get::store_data -------------//
00645 
00646 //-------- BEGIN OF FUNCTION Get::right_pos --------------//
00647 //
00648 // Description : calculate the correct cursor position by passing
00649 //              by the picture digit (e.g. "(999)", than pass '(' and ')' )
00650 //
00651 // Syntax      : right_pos( <int>,<int>,<char*> )
00652 //
00653 // <int>   = buffer position
00654 // <int>   = the length of the buffer
00655 // <char*> = picture pointer
00656 //
00657 int Get::right_pos( int buffer_pos, int len, char *pict ) {
00658     buffer_pos++ ;
00659 
00660     while ( buffer_pos < len ) {
00661         if ( strchr(PICTURE_CHRS,pict[buffer_pos]) == 0)
00662             buffer_pos++ ;
00663         else
00664             break ;
00665     }
00666 
00667     if ( buffer_pos == len) {
00668         buffer_pos-- ;
00669 
00670         while (buffer_pos > 0 &&
00671                strchr(PICTURE_CHRS,pict[buffer_pos]) == 0) {
00672             buffer_pos-- ;
00673         }
00674     }
00675 
00676     return  buffer_pos ;
00677 }
00678 
00679 //------ END OF FUNCTION Get::right_pos -------------//
00680 
00681 //-------- BEGIN OF FUNCTION Get::num_pos -------------//
00682 //
00683 // Description : Counts the Number of Positions to the Next Picture Data Character
00684 //
00685 // Syntax      : num_pos( <int>,<int>,<char*> )
00686 //
00687 // <int>   = buffer position
00688 // <int>   = the length of the buffer
00689 // <char*> = picture pointer
00690 //
00691 int Get::num_pos( int bufPos, int len, char *pict ) {
00692     int  on_pos ;
00693 
00694     on_pos =  bufPos ;
00695 
00696     while ( on_pos < len ) {
00697         if ( strchr(PICTURE_CHRS,pict[on_pos]) == 0)
00698             break ;
00699         else
00700             on_pos++ ;
00701     }
00702 
00703     return ( on_pos - bufPos ) ;
00704 }
00705 
00706 //-------- END OF FUNCTION Get::num_pos -------------//
00707 
00708 //--------- BEGIN OF FUNCTION Get::read ------------//
00709 //
00710 // Description : read the pre-defined gets
00711 //
00712 // Syntax      : read([int])
00713 //
00714 // [int] = the field to be read first
00715 //         (default : the first field in the get list, 1)
00716 //
00717 // Return : <int> the last key pressed for exiting read
00718 //                e.g. ESC, ENTER.
00719 //
00720 int Get::read(int firstRead) {
00721     int    firstFlag, firstKey;
00722     int    bufPos, curPos;
00723     int    bufOffset, bufWidth;
00724     int    fldId;                                   // id. of the GetFld in the fld_array
00725     int    num;                                     // temp value for location character
00726     int    keyCode;
00727 
00728     GetFld *getPtr ;
00729     char   dataBuf[MAX_GET_WIDTH+1], pictBuf[MAX_GET_WIDTH+1] ;
00730 
00731     //-------------- init vars -------------------//
00732 
00733     err_when( get_num == 0 );
00734 
00735     firstFlag = 1;                                  // whether it's the first time on the field or not
00736     firstKey  = 1;                                  // If the firstFlag key press is not cursor key, clear the field content, it can be any one of the types c,i,l,d,N
00737     fldId     = firstRead;
00738     getPtr    = fld_array + fldId - 1;
00739 
00740     display_get( getPtr, 1 );                       // display current getting field
00741 
00742     //--------------------------------------------//
00743 
00744     for (;;) {
00745         //------ it's the first time on this field --------//
00746 
00747         if (firstFlag) {
00748             if ( getPtr->type == 's' )
00749                 cursor_type(0);
00750             else {
00751                 if ( insert_flag )
00752                     cursor_type(2) ;
00753                 else
00754                     cursor_type(1) ;
00755             }
00756         }
00757         else
00758             firstKey = 0;                               // firstKey is for clearing numerical field if the first key stroke is not cursor key
00759 
00760         //--------------------------------------------//
00761 
00762         if ( firstFlag ) {                            // First time for this 'get'
00763             init_field(dataBuf, pictBuf, getPtr, bufPos, bufOffset, bufWidth);
00764             firstFlag = 0 ;
00765         }
00766 
00767         //--------- second time ( firstFlag == 0 ) ---------//
00768 
00769         keyCode = get_next_key( dataBuf, pictBuf, getPtr, bufPos, bufOffset,
00770                                 bufWidth );
00771 
00772         if( !keyCode )
00773             continue;
00774 
00775         //------------ cusror movement function keys ------------//
00776 
00777         if( process_field_key(keyCode, getPtr, dataBuf, pictBuf, curPos, bufPos) ) {
00778             // return 1 if field movement key
00779             continue;                                   // return 0 if alphabetical key / inter-field control key
00780         }
00781 
00782         //----------- Normal Alphanumerical keys --------------//
00783 
00784         if ( keyCode >= 0x20 && keyCode <= 0xFF ) {
00785             //------ Check the Picture Template -------//
00786 
00787             if( valid_pict(keyCode, getPtr, dataBuf, pictBuf, bufPos) ) {
00788                 //----- insert or Replace the character ---------//
00789 
00790                 // If the firstFlag key press is not cursor key, clear the field content, it can be any one of the types c,i,l,d,N
00791                 if ( m.str_chr("cildNC", getPtr->type) ) {
00792                     if ( firstKey )
00793                         memset(dataBuf,' ',getPtr->width_data);
00794                 }
00795 
00796                 if ( insert_flag ) {
00797                     // Insert the Character //
00798                     num = num_pos( bufPos, getPtr->width_data, pictBuf) ;
00799 
00800                     if ( num > 1 )
00801                         memmove( dataBuf+bufPos+1, dataBuf+bufPos, (size_t) num-1) ;
00802                 }
00803 
00804                 dataBuf[bufPos] = keyCode;
00805 
00806                 if ( bufPos < bufWidth-1 )
00807                     bufPos++;
00808             }
00809 
00810             continue;
00811         }
00812         else {                                        //--------- field movement function keys -----------//
00813             int rc = process_inter_field_key( keyCode, getPtr, firstFlag, firstKey, fldId, bufPos);
00814 
00815             if( rc != -1 ) {                            // -1 means exit read() immediately
00816                 int rc = store_and_valid( keyCode, dataBuf, pictBuf, getPtr, firstFlag );
00817 
00818                 if( !rc )                                 // not valid
00819                     continue;
00820             }
00821 
00822             //......... Exit Get and Return last pressed key .............//
00823 
00824             if( rc == -1 ) {                            // -1 means exit read() immediately
00825                 store_data( getPtr, dataBuf, pictBuf ) ;  // save the previous field dataBuf data
00826 
00827                 display_get(getPtr,0);
00828 
00829                 cursor_type( 1 ) ;
00830                 return keyCode;
00831             }
00832 
00833             continue ;
00834         }
00835     }
00836 }
00837 
00838 //--------- END OF FUNCTION Get::read ----------------//
00839 
00840 //-------- BEGIN OF FUNCTION Get::cursor_type -----------//
00841 //
00842 // Description : Set cursor type, represent insert mode or overwrite mode
00843 // Syntax      : cursor_type()
00844 //
00845 // Remark : hasn't be implemented yet
00846 //
00847 void Get::cursor_type(int newType) {
00848     newType++;
00849 }
00850 
00851 //-------- END OF FUNCTION Get::cursor_type ------------//
00852 
00853 //------- Begin of function Get::process_field_key -----------//
00860 int Get::process_field_key( int keyCode, GetFld* getPtr, char* dataBuf,
00861                             char* pictBuf, int& curPos, int& bufPos) {
00862     int num;
00863 
00864     switch ( keyCode ) {
00865     case KEY_INS:
00866         insert_flag =  ! insert_flag ;
00867         if ( insert_flag )
00868             cursor_type( 2 ) ;
00869         else
00870             cursor_type( 1 ) ;
00871         return 1;
00872 
00873     case KEY_BACK_SPACE:
00874     case KEY_LEFT:
00875         curPos =  bufPos - 1 ;
00876 
00877         while (curPos >= 0) {
00878             if ( strchr(PICTURE_CHRS,pictBuf[curPos]) == 0 )
00879                 curPos-- ;
00880             else
00881                 break;
00882         }
00883 
00884         if ( curPos < 0 )
00885             return 1 ;
00886 
00887         bufPos =  curPos ;
00888         // ####### begin Gilbert 24/7 ########//
00889         if ( keyCode == KEY_LEFT )                  // || keyCode == KEY_CTRL_S )
00890             return 1 ;
00891         // ####### end Gilbert 24/7 ########//
00892 
00893         // BACK_SPACE is a LEFT and a DEL //
00894 
00895     case KEY_DEL:
00896         num =  num_pos( bufPos, getPtr->width_data, pictBuf) ;
00897         if ( num == 0 )
00898             return 1 ;
00899         memcpy( dataBuf+bufPos, dataBuf+bufPos+1, num ) ;
00900         dataBuf[bufPos+num-1] = ' ' ;
00901 
00902         err_when( bufPos+num > getPtr->width_data );
00903 
00904         return 1;
00905 
00906         // ####### begin Gilbert 24/7 ########//
00907         /*
00908           case KEY_CTRL_Y:
00909           curPos =  bufPos ;
00910           memset( dataBuf + bufPos, (int) ' ',
00911           (size_t) num_pos( bufPos, getPtr->width_data, pictBuf));
00912           return 1;
00913         */
00914         // ####### end Gilbert 24/7 ########//
00915 
00916     case KEY_RIGHT:
00917         bufPos =
00918             right_pos( bufPos, getPtr->width_data, pictBuf);
00919         return 1 ;
00920 
00921     case KEY_HOME:
00922         bufPos = 0;
00923         return 1;
00924 
00925     case KEY_END:
00926         bufPos =  getPtr->width_data - 1;
00927 
00928         while ( bufPos >= 0 ) {
00929             if (dataBuf[bufPos] == ' ' || dataBuf[bufPos] == '\0')
00930                 bufPos-- ;
00931             else
00932                 break;
00933         }
00934 
00935         bufPos =
00936             right_pos( bufPos, getPtr->width_data, pictBuf);
00937         return 1 ;
00938     }
00939 
00940     return 0;
00941 }
00942 
00943 //------- End of function Get::process_field_key -----------//
00944 
00945 //------- Begin of function Get::valid_pict -----------//
00952 int Get::valid_pict( int keyCode, GetFld* getPtr, char* dataBuf,
00953                      char* pictBuf, int& bufPos ) {
00954     if( !pictBuf[bufPos] )
00955         return 1;
00956 
00957     int ok=0, i, num;
00958 
00959     switch( pictBuf[bufPos] ) {
00960     case '9':
00961     case '#':
00962         if ( (keyCode >= '0' && keyCode <= '9') || keyCode == '-' )
00963             ok = 1 ;
00964 
00965         if ( (keyCode == '+' || keyCode == '-' || keyCode == ' ')    &&
00966              pictBuf[bufPos] == '#') {
00967             ok =  1 ;
00968         }
00969 
00970         if ( keyCode == '.'  &&  getPtr->dec_num > 0 ) {
00971             int  num_shift ;
00972 
00973             num_shift =  getPtr->width_scr - getPtr->dec_num -
00974                 bufPos - 1 ;
00975 
00976             if ( --bufPos < 0 )  bufPos = 0 ;
00977 
00978             if ( num_shift > 0  &&  dataBuf[bufPos] != ' ' ) {
00979                 memmove( dataBuf+ num_shift, dataBuf,
00980                          (size_t) (getPtr->width_scr - num_shift) ) ;
00981                 memset( dataBuf, (int) ' ', (size_t) num_shift ) ;
00982             }
00983 
00984             bufPos = getPtr->width_scr - getPtr->dec_num-1;
00985             memset( dataBuf+ bufPos+1, (int) '0',
00986                     (size_t) getPtr->dec_num);
00987             ok = 1 ;
00988         }
00989         break ;
00990 
00991     case 'A':
00992         if ( keyCode >= 'A' && keyCode <= 'Z'  ||
00993              keyCode >= 'a' && keyCode <= 'z' ) {
00994             ok =  1 ;
00995         }
00996         break ;
00997 
00998     case 'L':
00999         keyCode =  (char) m.upper( keyCode ) ;
01000         if ( keyCode == 'Y' || keyCode == 'N' || keyCode == 'T' || keyCode == 'F' )
01001             ok = 1;
01002         break ;
01003 
01004     case 'N':
01005         if ( keyCode >= 'A' && keyCode <= 'Z'  ||
01006              keyCode >= 'a' && keyCode <= 'z'  ||
01007              keyCode >= '0' && keyCode <= '9'  ) {
01008             ok =  1 ;
01009         }
01010         break ;
01011 
01012     case 'Y':
01013         if ( getPtr->type == 'D' ) {                // Date Type: 'Y' for year //
01014             if ( keyCode >= '0'  &&  keyCode <= '9' || keyCode == ' ' )
01015                 ok =  1 ;
01016         }
01017         else {
01018             keyCode =  (char) m.upper( keyCode ) ;
01019 
01020             if ( keyCode == 'Y' ||  keyCode == 'N' )
01021                 ok =  1 ;
01022         }
01023         break ;
01024 
01025     case 'X':
01026     case '!':
01027         ok =  1 ;
01028         break ;
01029 
01030     case 'C':
01031     case 'D':
01032         if ( getPtr->type == 'D' &&
01033              (keyCode >= '0'  &&  keyCode <= '9' ||  keyCode == ' ')  ) {
01034             ok =  1 ;
01035         }
01036         break ;
01037 
01038     case 'M':
01039         if ( getPtr->type == 'D' ) {
01040             if ( keyCode == ' ' ) {
01041                 ok = 1 ;
01042                 break ;
01043             }
01044 
01045             for ( num=0, i=0; pictBuf[i] != '\0'; i++ )
01046                 if ( pictBuf[i] == 'M' ) num++ ;
01047 
01048             if ( num > 2 ) {
01049                 if ( keyCode >= 'A' && keyCode <= 'Z'  ||
01050                      keyCode >= 'a' && keyCode <= 'z') {
01051                     ok =  1;
01052                 }
01053             }
01054             else {
01055                 if ( keyCode >= '0' && keyCode <= '9')
01056                     ok =  1;
01057             }
01058         }
01059         break ;
01060     }
01061 
01062     return ok;
01063 }
01064 
01065 //------- End of function Get::valid_pict -----------//
01066 
01067 //------- Begin of function Get::process_inter_field_key -----------//
01075 int Get::process_inter_field_key( int keyCode, GetFld* getPtr, int& firstFlag,
01076                                   int& firstKey, int& fldId, int& bufPos ) {
01077     int newFldId=0;                                 // newFldId is set when user move to other field, compare fldId and newFldId, we can know the change
01078 
01079     switch( keyCode ) {
01080         // ####### begin Gilbert 24/7 ########//
01081         /*
01082           case KEY_CTRL_HOME:
01083           firstFlag = firstKey = 1;      // firstFlag time for this field
01084           newFldId  = 1;   // get number is the firstFlag one
01085           break;
01086 
01087           case KEY_CTRL_END:
01088           firstFlag = firstKey = 1 ;
01089           newFldId  = get_num;   // last one
01090           break;
01091         */
01092         // ####### begin Gilbert 24/7 ########//
01093 
01094     case KEY_UP:
01095         // ####### begin Gilbert 24/7 ########//
01096         // case KEY_CTRL_Z:
01097         // ####### end Gilbert 24/7 ########//
01098         firstFlag = firstKey = 1 ;
01099         newFldId = fldId-1;
01100         break;
01101 
01102     case KEY_DOWN:
01103     case KEY_RETURN:
01104     case KEY_TAB:
01105         // ####### begin Gilbert 24/7 ########//
01106         // case KEY_CTRL_B:
01107         // ####### end Gilbert 24/7 ########//
01108         firstFlag = firstKey = 1;
01109         bufPos =  0 ;
01110         newFldId = fldId+1;
01111         break;
01112 
01113     case KEY_PGUP:
01114     case KEY_PGDN:
01115         if (up_down_exit)
01116             return -1;                                // exit read()
01117         break;
01118     }
01119 
01120     //-------- Also Return if a Function Key was Pressed -------//
01121 
01122     if (
01123         // ####### begin Gilbert 24/7 ########//
01124         // keyCode == KEY_CTRL_W  ||   keyCode == KEY_CTRL_Q  ||
01125         // ####### begin Gilbert 24/7 ########//
01126         keyCode == KEY_ESC     ||   keyCode == KEY_F10     ||
01127         ( keyCode >= KEY_F1 && keyCode <= KEY_F9 && up_down_exit ) ||
01128         keyCode < 0  ) {
01129         return -1;                                    // exit read()
01130     }
01131 
01132     //--------- press up in the upper most field --------//
01133 
01134     if ( newFldId > get_num ) {
01135         if( keyCode == KEY_RETURN || keyCode == KEY_TAB || up_down_exit )
01136             return -1;                                  // exit read()
01137         else
01138             newFldId = get_num;                         // down or TAB is pressed and up_down_exit flag is not set
01139     }
01140 
01141     //------ or down key in the bottom most field -------//
01142 
01143     if ( newFldId < 1 ) {
01144         if ( up_down_exit )                           // exit when up is pressed at the firstFlag field
01145             return -1;                                  // exit read()
01146         else
01147             newFldId = 1;                               // else than reassign it to fist field
01148     }
01149 
01150     //------- assign the new field pointer ---------//
01151 
01152     if( newFldId ) {
01153         if (fldId != newFldId) {
01154             display_get( getPtr, 0 );
01155             fldId = newFldId;
01156             getPtr = fld_array + fldId - 1;
01157             display_get( getPtr, 1 );
01158         }
01159 
01160         return 1;
01161     }
01162 
01163     return 0;
01164 }
01165 
01166 //------- End of function Get::process_inter_field_key -----------//
01167 
01168 //------- Begin of function Get::init_field -----------//
01172 void Get::init_field(char* dataBuf, char* pictBuf  , GetFld* getPtr,
01173                      int& bufPos , int&  bufOffset, int &bufWidth ) {
01174     bufPos    = 0;
01175     bufOffset = 0;
01176 
01177     //-- Buffer is filled and the width is returned by 'load_data' -//
01178 
01179     bufWidth =  load_data( getPtr, dataBuf ) ;
01180     dataBuf[bufWidth] =  '\0' ;
01181 
01182   //----- picture buffer is filled --------//
01183 
01184     memset( pictBuf, 0, bufWidth+1 );
01185 
01186     if ( getPtr->picture != (char *) 0 ) {
01187         strncpy( pictBuf, getPtr->picture, bufWidth );
01188     }
01189     else {
01190         switch( getPtr->type ) {
01191         case 'D':
01192             strcpy( pictBuf, DEFAULT_DATE ) ;
01193             break ;
01194 
01195         case 'N':
01196         case 'd':
01197         case 'i':
01198         case 'c':
01199             memset( pictBuf, (int) '#', (size_t) getPtr->width_scr ) ;
01200 
01201             if ( getPtr->dec_num > 0) {
01202                 int t = getPtr->width_scr - getPtr->dec_num - 1 ;
01203 
01204                 dataBuf[t] = '.';
01205                 pictBuf[t] = '.';
01206             }
01207             break ;
01208 
01209         case 'L':
01210             pictBuf[0] = 'L' ;
01211             pictBuf[1] = '\0' ;
01212             break ;
01213         }
01214     }
01215 }
01216 
01217 //------- End of function Get::init_field -----------//
01218 
01219 //------- Begin of function Get::get_next_key ---------//
01226 int Get::get_next_key( char* dataBuf, char* pictBuf, GetFld* getPtr,
01227                        int&  bufPos , int&  bufOffset, int &bufWidth ) {
01228     int keyCode;
01229 
01230     if ( bufPos >= bufWidth ) {                     // the cursor exceeds the field width
01231         keyCode = KEY_RETURN ;                        // than simulate a RETURN key to next field
01232         font_ptr->disp( getPtr->x, getPtr->y, dataBuf, getPtr->x2 );
01233     }
01234     else {
01235         keyCode =  0 ;
01236 
01237         // if the picture has a mask like e.g. "(999) 9999-999" then the '(',')' and '-' position will not be got.
01238 
01239         if ( strchr( PICTURE_CHRS, pictBuf[bufPos] ) == (char *) 0  &&
01240              (int) pictBuf[bufPos] >= 0x20  &&
01241              (int) pictBuf[bufPos] <= 0xFF) {
01242             dataBuf[bufPos] = pictBuf[bufPos] ;
01243             if ( bufPos < bufWidth-1 )
01244                 bufPos++ ;
01245 
01246             return 0;
01247         }
01248 
01249         //------ otherwise, check bufOffset --------//
01250 
01251         if ( bufOffset <= bufPos - getPtr->width_scr )
01252             bufOffset =  1+ bufPos - getPtr->width_scr ;
01253 
01254         if ( bufPos < bufOffset )
01255             bufOffset =  bufPos ;
01256 
01257         //--------- Say field content ------------//
01258 
01259         font_ptr->disp( getPtr->x, getPtr->y, dataBuf+bufOffset, getPtr->x2 );
01260 
01261         int cursorX = getPtr->x + font_ptr->text_width( dataBuf, bufPos-bufOffset );
01262 
01263         //-------- Call defined function before entering the field ------//
01264 
01265         if ( getPtr->call_ptr ) {
01266             dataBuf[bufWidth] = '\0' ;
01267             keyCode = (*getPtr->call_ptr)( getPtr, dataBuf, getPtr->call_data);
01268 
01269             if ( keyCode == -1 )
01270                 return 0;
01271         }
01272 
01273         //--- Get key from keyboard and convert to upper case if necessary ----//
01274 
01275         if ( keyCode == 0 ) {
01276             sys.yield();
01277 
01278             DWORD nextTime=m.get_time();
01279             int   cursorDisplay=1;
01280 
01281             // ####### begin Gilbert 25/7 ##########//
01282             // while waitingfor input, flash the cursor
01283             while( mouse.is_key_event() ? ((keyCode = mouse.key_code),mouse.get_event(),0) : 1 ) {
01284                 // ####### end Gilbert 25/7 ##########//
01285                 if( m.get_time() >= nextTime ) {
01286                     if( cursorDisplay )                     // currently display -> vanish
01287                         vga.active_buf->bar( cursorX-1, getPtr->y, cursorX, getPtr->y+font_ptr->height()-1, V_WHITE );
01288                     else                                    // currently vanish -> display
01289                         vga.active_buf->bar_down( cursorX-1, getPtr->y, cursorX, getPtr->y+font_ptr->height()-1 );
01290 
01291                     cursorDisplay = !cursorDisplay;
01292                     nextTime = m.get_time() + 300;          // flash frequency : 0.3 seconds
01293                 }
01294 
01295                 sys.yield();
01296                 mouse.get_event();
01297 
01298                 sys.blt_virtual_buf();
01299 
01300                 //----- if the mouse is press outside the field, exit ----//
01301 
01302                 if( mouse.any_click() ) {
01303                     if( mouse.click_x() < fld_x1 || mouse.click_x() > fld_x2 ||
01304                         mouse.click_y() < fld_y1 || mouse.click_y() > fld_y2 ) {
01305                         keyCode = KEY_F10;                    // simulate a F10 to exit
01306                         break;
01307                     }
01308                 }
01309             }
01310 
01311             if( !cursorDisplay )                        // last time is display, then erase it
01312                 vga.active_buf->bar_down( cursorX-1, getPtr->y, cursorX, getPtr->y+font_ptr->height()-1 );
01313 
01314             sys.blt_virtual_buf();
01315         }
01316 
01317         if ( (getPtr->upper_convert ||  pictBuf[bufPos] == '!')  &&  keyCode >= 0x20  &&  keyCode <= 0xFF )
01318             keyCode = m.upper( keyCode ) ;
01319     }
01320 
01321     return keyCode;
01322 }
01323 
01324 //------- End of function Get::get_next_key ---------//
01325 
01326 //------- Begin of function Get::store_and_valid ---------//
01334 int Get::store_and_valid( int keyCode, char* dataBuf, char* pictBuf, GetFld* getPtr,
01335                           int& firstFlag) {
01336     if( firstFlag ) {
01337         store_data( getPtr, dataBuf, pictBuf ) ;      // save the previous field dataBuf data
01338 
01339         //-------- Call Check Validation function ----------//
01340 
01341         // ####### begin Gilbert 24/7 ########//
01342         // && keyCode != KEY_CTRL_Q )
01343         if ( getPtr->valid_ptr  && keyCode != KEY_ESC ) {
01344             // ####### end Gilbert 24/7 ########//
01345             if (!( (*getPtr->valid_ptr)() ) )
01346                 return 0;
01347         }
01348 
01349         //-------- Check validation of date content ---------//
01350 
01351         //------- Currently it only support "Character" type -----//
01352         /*
01353           if ( getPtr->type == 'D' && keyCode != KEY_ESC && keyCode != KEY_CTRL_Q )
01354           {
01355           double  temp_data ;
01356           int     date_rc ;
01357 
01358           // Date Check
01359           date_rc =  c1dt_julian( (char *) getPtr->data, &temp_data ) ;
01360 
01361           if ( date_rc == -1 )
01362           {
01363           dataBuf[bufWidth] = '\0' ;
01364           return 0;
01365           }
01366           }
01367         */
01368         //------- Currently it only support "Character" type -----//
01369     }
01370 
01371     return 1;
01372 }
01373 
01374 //------- End of function Get::store_and_valid ---------//

Generated on Fri Aug 23 01:37:45 2002 for VirtualU by doxygen1.2.17