home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / p / prbgi097.zip / C.ZIP / TV2.CPP < prev    next >
C/C++ Source or Header  |  1992-12-15  |  7KB  |  258 lines

  1.  
  2. #define Uses_TKeys
  3. #define Uses_TScrollBar
  4. #define Uses_TRect
  5. #define Uses_TDrawBuffer
  6. #define Uses_TEvent
  7. #define Uses_TDialog
  8. #define Uses_TCollection
  9. #define Uses_TPoint
  10. #define Uses_TButton
  11. #define Uses_TProgram
  12.  
  13. #include <tv.h>
  14.  
  15. #include "tv2.h"
  16. #include <stdlib.h>
  17. #include <string.h>
  18.  
  19.  
  20. // TInput_float:: ~TInput_float ()
  21. // { ~TInputLine() };
  22.  
  23.  
  24. /*====================================*/
  25. void   TInput_float::getData(void *rec)
  26. /*====================================*/
  27. {
  28.    *(float*)rec = atof(data);
  29. }
  30.  
  31. /*====================================*/
  32. void   TInput_float::setData(void *rec)
  33. /*====================================*/
  34. {  char s[16];
  35.    gcvt ( *(float*)rec, 4, s);
  36.    if ( strlen(s)<=maxLen ) strcpy(data,s);
  37.    else                     strcpy(data,"?");
  38. }
  39.  
  40. /*============================*/
  41. ushort TInput_float::dataSize()
  42. /*============================*/
  43. {
  44.    return sizeof(float);
  45. }
  46.  
  47.  
  48. void TDialogDbl::handleEvent(TEvent& event)
  49. {
  50.    if ( event.what==evBroadcast && event.message.command == cmListItemSelected )
  51.    {
  52.       event.what = evCommand;
  53.       event.message.command = cmOK;
  54.    }
  55.    TDialog::handleEvent(event);
  56. }
  57.  
  58.   //  if ( ( ctrlToArrow(event.keyDown.keyCode) ) == kbDown )
  59.  
  60. /*==========================================*/
  61. TEnumValue::TEnumValue( const TRect& bounds, int aMaxLen ):
  62. /*==========================================*/
  63.    TInputLine(bounds,aMaxLen)
  64. {
  65.    if ( data!=NULL )
  66.    {
  67.       memset(data,' ',maxLen);       // Don't use setData 'cause of overriden dataSize
  68.       data[maxLen]=0;
  69.    }
  70.    List = NULL; focusedItem=-1;
  71.    TView::hideCursor();
  72. }
  73.  
  74. /*================================*/
  75. void TEnumValue::getData(void *rec)
  76. /*================================*/
  77. {
  78.    *(ccIndex*)rec = focusedItem;
  79. }
  80. /*================================*/
  81. void TEnumValue::setData(void *rec)
  82. /*================================*/
  83. {
  84.   focusedItem = *(ccIndex*)rec;
  85.   newFocusedItem(focusedItem);
  86. }
  87.  
  88. /*-----------------------------------------*/
  89. void strMaxLen ( char *s, int *maxlen)
  90. /*-----------------------------------------*/
  91. {
  92.    if ( strlen(s) > *maxlen )
  93.       *maxlen = strlen(s);
  94. }
  95.  
  96. typedef void (*accAppFunc)(void*,void*);
  97. /*------------------------------*/
  98. void TEnumValue::MakeSelection( )
  99. /*------------------------------*/
  100. {
  101.    struct { TCollection *items;
  102.             ccIndex      focused; }
  103.           BoxAnswer;
  104.    if ( List == NULL ) return;
  105.    int h = List->size();
  106.    int w = 0;
  107.    List->forEach((accAppFunc)strMaxLen,&w);
  108.    w +=6;
  109.    h +=2;
  110.    if ( h>owner->size.y-4 )
  111.       h=owner->size.y-4;
  112.  
  113.    TRect R;
  114.     R.a.x=owner->origin.x+origin.x;
  115.     R.b.x=R.a.x+w;
  116.     R.a.y=max(0,owner->origin.y+origin.y-h/2);
  117.     R.b.y=R.a.y+h;
  118.    TScrollBar *scrollBar = NULL;
  119.  
  120.     TDialogDbl *D = new TDialogDbl(R, "..." );
  121.     R.a.x=2; R.a.y=1; R.b.x=D->size.x-R.a.x; R.b.y=D->size.y-R.a.y;
  122.    //if ( R.b.y-R.a.y<List->size() )
  123.    //{
  124.       scrollBar = new TScrollBar (TRect(R.b.x,R.a.y,R.b.x+1,R.b.y));
  125.       D->insert(scrollBar);
  126.    //}
  127.     TListBox *Lbox = new TListBox(R, 1, scrollBar );
  128.     if ( Lbox->size.y>=List->size() && scrollBar!=NULL )
  129.     {
  130.        scrollBar->setState(sfVisible,False);
  131.     }
  132.  
  133.     //Lbox->newList(List);
  134.     if ( focusedItem<0 || focusedItem>=List->size()) BoxAnswer.focused=0;
  135.     else BoxAnswer.focused=focusedItem;
  136.     //Lbox->focusItem(BoxAnswer.focused);
  137.     TButton *B=new TButton( TRect(R.b.x/2-3,R.b.y+1,R.b.x/2+3,R.b.y+3),"O~K~", cmOK, bfDefault);
  138.     B->setState(sfVisible,False);
  139.     B->options &= !(ofSelectable);
  140.     D->insert(B);
  141.  
  142.     BoxAnswer.items=List; /* ???????????????????????????????????? */
  143.      Lbox->setData(&BoxAnswer);
  144.     D->insert(Lbox);
  145.  
  146.     // D = TProgram::application->deskTop->validView(D);
  147.     if (D != NULL)
  148.     {
  149.        if (TProgram::application->execView(D) == cmOK)
  150.        {
  151.           Lbox->getData(&BoxAnswer);
  152.          newFocusedItem( BoxAnswer.focused );
  153.        }
  154.        destroy( D );
  155.     }
  156. }
  157.  
  158.  
  159. /*=============================================*/
  160. void  TEnumValue::newFocusedItem( ccIndex newItem )
  161. /*=============================================*/
  162. {
  163.    if ( List!=NULL )
  164.       if ( newItem>=0 && newItem<List->size() )
  165.       {
  166.          const char *t = (const char*)(List->at(newItem));
  167.          if ( t != NULL )
  168.          {  int l=strlen(t);
  169.             if (l>maxLen) l=maxLen;
  170.             memcpy(data,t,l);       // Don't use setData 'cause of override dataSize
  171.             if (maxLen>l) memset(data+l,' ',maxLen-l);
  172.             data[maxLen]=0;
  173.             focusedItem = newItem;
  174.             drawView();
  175.             return;
  176.          }
  177.       }
  178. }
  179.  
  180. /*===========================================*/
  181. void  TEnumValue::handleEvent( TEvent& event )
  182. /*===========================================*/
  183. {
  184.     TView::handleEvent(event);
  185.  
  186.     // if( (state & sfSelected) != 0 )
  187.  
  188.     switch ( event.what )
  189.     {
  190.        case evCommand:
  191.            if ( event.message.command==cmEnumChange )
  192.            {
  193.               clearEvent( event );
  194.               MakeSelection();
  195.            }
  196.            break;
  197.        case evKeyDown:
  198.            if ( event.keyDown.keyCode==kbEnter || event.keyDown.keyCode==kbDown )
  199.            {
  200.               clearEvent( event );
  201.               MakeSelection();
  202.            }
  203.            break;
  204.        case evMouseDown:
  205.            if( event.mouse.doubleClick )
  206.            {
  207.               clearEvent( event );
  208.               MakeSelection();
  209.            }
  210.            break;
  211.     }
  212. }
  213.  
  214.  
  215. /*=============================================*/
  216. void TEnumValue::newList ( TCollection2 *aList, ccIndex focusedNo )
  217. /*=============================================*/
  218. {
  219.    if (List!=NULL) destroy(List);
  220.    List = aList;
  221.    focusedItem = -1;
  222.    newFocusedItem(focusedNo);
  223.    drawView();
  224. }
  225.  
  226.  
  227. /*=====================================*/
  228. TPalette& TEnumArrow::getPalette() const
  229. /*=====================================*/
  230. {  static TPalette cp("\x0C",1);
  231.    // cp[0]=TPalette::cpButton[0];
  232.    return cp;
  233. }
  234.  
  235.  
  236. /*===================================================================*/
  237. TEnumArrow::TEnumArrow( const TRect& bounds, const char *aTitle,
  238.                         TView *aLink ):
  239. /*===================================================================*/
  240.    TStaticText(bounds,aTitle)
  241. {
  242.    Link=aLink;
  243.    options &= !ofSelectable;
  244.    setState(sfShadow,False);
  245. }
  246.  
  247. /*=====================*/
  248. void TEnumArrow::handleEvent(TEvent& event)
  249. /*=====================*/
  250. {
  251.     if ( event.what==evMouseDown )
  252.     {
  253.        clearEvent(event);
  254.        if ( Link!=NULL)
  255.           message( Link, evCommand, cmEnumChange, this );
  256.     }
  257. }
  258.