home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / tvision / fields / fields.cpp next >
Encoding:
C/C++ Source or Header  |  1994-01-02  |  2.9 KB  |  131 lines

  1. /*
  2.     FIELDS.CPP
  3.  
  4.     Written by: ing E.A.B. Schlösser, The Netherlands
  5.     CIS 100016,2263
  6.  
  7.     Again a new Turbo Vision class.
  8.  
  9.     When using a dialog screen, you create the dialog screen with it's
  10.     fields for data entry. When done you must define a data-struct
  11.     that matches your dialog screen. To me this does not seem 'right'.
  12.     I created an InputField class that updates your variables linked
  13.     to the input field when pressing enter.
  14.     Only create the dialog screen, put labels and input fields and
  15.     you are done.
  16.     Additionally the class inhibits using alpha keys in numeric fields !
  17.  
  18.     Only part that I do not like: A pointer to the TDialog window must be
  19.     provided as an argument. Currently you should use
  20.         TDialog *pd;
  21.  
  22.         TRect r( 1, 1, 77, 46 );
  23.         pd=new TDialog(r,"Enter geometry data");
  24.  
  25.         if (pd)
  26.         {
  27.           wing.Dubbel=stabilo.Dubbel=2;
  28.           kielvlak.Dubbel=0;
  29.           kielvlak.alpha=kielvlak.b0=0.0;
  30.  
  31.           pd->insert(new TButton(TRect(60,42,68,44),"~O~K",cmOK,bfNormal));
  32.  
  33.           pd->PutText(2,1,"Model name:");
  34.           new InputField(pd,25,1,24,naam);
  35.           .
  36.           .
  37.           .
  38.  
  39.     I would like to use:
  40.           pd->insert(InputField(25,1,24,naam));
  41.  
  42.     Who can help ?
  43.  
  44. */
  45. #pragma hdrfile "fields.sym"
  46.  
  47.   #define Uses_TEvent
  48.   #define Uses_TKeys
  49.  
  50.  
  51.   #include <tv.h>
  52.  
  53.   #include "fields.hpp"
  54.  
  55.   #include <ctype.h>
  56.   #include <stdio.h>
  57.  
  58. #pragma hdrstop
  59.  
  60. InputField::InputField(TDialog *pd, int x, int y, int w, float *data):
  61.                         TInputLine(TRect(x,y,x+w+3,y+1),w+1)
  62. {
  63.   char tmp[20];
  64.   sprintf(tmp,"%f",*data);
  65.   setData(tmp);
  66.   pd->insert(this);
  67.   ptrData=data;
  68.   dataType=0;
  69. }
  70.  
  71. InputField::InputField(TDialog *pd, int x, int y, int w, char *data):
  72.                         TInputLine(TRect(x,y,x+w+3,y+1),w+1)
  73. {
  74.   setData(data);
  75.   pd->insert(this);
  76.   ptrData=data;
  77.   dataType=4;
  78. }
  79.  
  80. InputField::~InputField(void)
  81. {
  82. }
  83.  
  84. int InputField::Validate(void)
  85. {
  86.   char tmpstr[50];
  87.   float tmpflt;
  88.  
  89.   TInputLine::getData(tmpstr);
  90.  
  91.   switch(dataType)
  92.   {
  93.     case 0: // float
  94.             if (1==sscanf(tmpstr,"%f",&tmpflt))
  95.             {
  96.               *(float *)ptrData=tmpflt;
  97.               return 1;
  98.             }
  99.             break;
  100.  
  101.     case 4: // string
  102.             strcpy((char *)ptrData,tmpstr);
  103.             return 1;
  104.  
  105.   }
  106.   return 0;
  107. }
  108.  
  109. void InputField::handleEvent(TEvent& event)
  110. {
  111.   ushort Kcode;
  112.   uchar  Ccode;
  113. //  uchar  Scode;
  114.  
  115.   if (event.what==evKeyDown)
  116.   {
  117.     Kcode=event.keyDown.keyCode;            // full two byte key code
  118.     Ccode=event.keyDown.charScan.charCode;  // lower byte
  119. //    Scode=event.keyDown.charScan.scanCode;  // upper byte
  120.     if (Kcode==kbEnter)
  121.     {
  122.       if (Validate()) event.keyDown.keyCode=kbTab;
  123.     }
  124.     else
  125.     {
  126.       if ( (dataType<4) && (isalpha(Ccode))) clearEvent(event);
  127.     }
  128.   }
  129.   TInputLine::handleEvent(event);
  130. }
  131.