home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20os2.zip / src / stddlg.cpp < prev    next >
C/C++ Source or Header  |  1999-05-19  |  10KB  |  360 lines

  1. /*
  2.  * stddlg.cc
  3.  *
  4.  * Turbo Vision - Version 2.0
  5.  *
  6.  * Copyright (c) 1994 by Borland International
  7.  * All Rights Reserved.
  8.  *
  9.  * Modified by Sergio Sigala <ssigala@globalnet.it>
  10.  */
  11.  
  12. #define Uses_MsgBox
  13. #define Uses_TKeys
  14. #define Uses_TFileInputLine
  15. #define Uses_TEvent
  16. #define Uses_TSortedListBox
  17. #define Uses_TSearchRec
  18. #define Uses_TFileInfoPane
  19. #define Uses_TDrawBuffer
  20. #define Uses_TFileDialog
  21. #define Uses_TSortedCollection
  22. #include <tvision/tv.h>
  23.  
  24. #include <ctype.h>
  25. #include <errno.h>
  26. #include <limits.h>
  27. #include <stdio.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30.  
  31. void fexpand( char * );
  32.  
  33. #define cpInfoPane "\x1E"
  34.  
  35. TFileInputLine::TFileInputLine( const TRect& bounds, short aMaxLen ) :
  36.     TInputLine( bounds, aMaxLen )
  37. {
  38.     eventMask |= evBroadcast;
  39. }
  40.  
  41. void TFileInputLine::handleEvent( TEvent& event )
  42. {
  43.     TInputLine::handleEvent(event);
  44.     if( event.what == evBroadcast &&
  45.         event.message.command == cmFileFocused &&
  46.         !(state & sfSelected)
  47.       )
  48.         {
  49. #ifndef __UNPATCHED
  50.     // Prevents incorrect display in the input line if wildCard has
  51.     // already been expanded.
  52.     if( (((TSearchRec *)event.message.infoPtr)->attr & FA_DIREC) != 0 )
  53.     {
  54.         strcpy( data, ((TFileDialog *)owner)->wildCard );
  55.         if(!strchr(data, ':') && !strchr(data, '/'))
  56.         {
  57.             strcpy( data, ((TSearchRec *)event.message.infoPtr)->name );
  58.             strcat( data, "/" );
  59.             strcat( data, ((TFileDialog *)owner)->wildCard );
  60.         }
  61.         else
  62.         {
  63.             // Insert "<name>\\" between last name or wildcard and last '/'
  64.             fexpand(data);    // Insure complete expansion to begin with
  65.             char *tmp = strrchr(data, '/') + 1;
  66.             char *nm = ((TSearchRec *)event.message.infoPtr)->name;
  67.             memmove(tmp + strlen(nm) + 1, tmp, strlen(tmp) + 1);
  68.             memcpy(tmp, nm, strlen(nm));
  69.             *(tmp + strlen(nm)) = '/';
  70.             fexpand(data);    // Expand again incase it was '..'.
  71.         }
  72.     }
  73. #else
  74.         if( (((TSearchRec *)event.message.infoPtr)->attr & FA_DIREC) != 0 )
  75.             {
  76.             strcpy( data, ((TSearchRec *)event.message.infoPtr)->name );
  77.             strcat( data, "/" );
  78.             strcat( data, ((TFileDialog *)owner)->wildCard );
  79.             }
  80. #endif
  81.         else
  82.             strcpy( data, ((TSearchRec *)event.message.infoPtr)->name );
  83.         drawView();
  84.         }
  85. }
  86.  
  87. TSortedListBox::TSortedListBox( const TRect& bounds,
  88.                                 ushort aNumCols,
  89.                                 TScrollBar *aScrollBar) :
  90.     TListBox(bounds, aNumCols, aScrollBar),
  91.     searchPos( -1 ),
  92.     shiftState( 0 )
  93. {
  94.     showCursor();
  95.     setCursor(1, 0);
  96. }
  97.  
  98. static Boolean equal( const char *s1, const char *s2, ushort count)
  99. {
  100.     /* SS: moved to linux lib */
  101.  
  102. #ifdef __OS2__
  103.    return Boolean( strnicmp( s1, s2, count ) == 0 );
  104. #else
  105.     return Boolean( strncasecmp( s1, s2, count ) == 0 );
  106. #endif
  107. }
  108.  
  109. void TSortedListBox::handleEvent(TEvent& event)
  110. {
  111.     char curString[256], newString[256];
  112.     void* k;
  113.     int value;
  114.     short oldPos, oldValue;
  115.  
  116.     oldValue = focused;
  117.     TListBox::handleEvent( event );
  118.     if( oldValue != focused ||
  119.         ( event.what == evBroadcast &&
  120.           event.message.command == cmReleasedFocus )
  121.       )
  122.         searchPos = -1;
  123.     if( event.what == evKeyDown )
  124.         {
  125.         if( event.keyDown.charScan.charCode != 0 )
  126.             {
  127.             value = focused;
  128.             if( value < range )
  129.                 getText( curString, (short) value, 255 );
  130.             else
  131.                 *curString = EOS;
  132.             oldPos = searchPos;
  133.             if( event.keyDown.keyCode == kbBack )
  134.                 {
  135.                 if( searchPos == -1 )
  136.                     return;
  137.                 searchPos--;
  138.                 if( searchPos == -1 )
  139.                     shiftState = (ushort) event.keyDown.controlKeyState;
  140.                 curString[searchPos+1] = EOS;
  141.                 }
  142.             else if( (event.keyDown.charScan.charCode == '.') )
  143.                 {
  144.                 char *loc = strchr( curString, '.' );
  145.                 if( loc == 0 )
  146.                     searchPos = -1;
  147.                 else
  148.                     searchPos = short(loc - curString);
  149.                 }
  150.             else
  151.                 {
  152.                 searchPos++;
  153.                 if( searchPos == 0 )
  154.                     shiftState = (ushort) event.keyDown.controlKeyState;
  155.                 curString[searchPos] = event.keyDown.charScan.charCode;
  156.                 curString[searchPos+1] = EOS;
  157.                 }
  158.             k = getKey(curString);
  159.  
  160.         /* SS: this makes g++ happy */
  161.  
  162.         ccIndex c = (ccIndex) value;
  163. //            list()->search( k, (ccIndex) value );
  164.             list()->search( k, c );
  165.         value = (int) c;
  166.  
  167.             if( value < range )
  168.                 {
  169.                 getText( newString, (short) value, 255 );
  170.                 if( equal( curString, newString, (ushort) (searchPos+1) ) )
  171.                     {
  172.                     if( value != oldValue )
  173.                         {
  174.                         focusItem( (short) value );
  175.                         setCursor( cursor.x+searchPos+1, cursor.y );
  176.                         }
  177.                     else
  178.                         setCursor(cursor.x+(searchPos-oldPos), cursor.y );
  179.                     }
  180.                 else
  181.                     searchPos = oldPos;
  182.                 }
  183.             else
  184.                 searchPos = oldPos;
  185.             if( searchPos != oldPos ||
  186.                 isalpha( event.keyDown.charScan.charCode )
  187.               )
  188.                 clearEvent(event);
  189.             }
  190.         }
  191. }
  192.  
  193. void* TSortedListBox::getKey( const char *s )
  194. {
  195.     return (void *)s;
  196. }
  197.  
  198. void TSortedListBox::newList( TSortedCollection *aList )
  199. {
  200.     TListBox::newList( aList );
  201.     searchPos = -1;
  202. }
  203.  
  204. TFileInfoPane::TFileInfoPane( const TRect& bounds ) :
  205.     TView(bounds)
  206. {
  207.     eventMask |= evBroadcast;
  208. }
  209.  
  210. void TFileInfoPane::draw()
  211. {
  212.     Boolean PM;
  213.     TDrawBuffer b;
  214.     ushort  color;
  215.     ftime *time;
  216.     char path[PATH_MAX];
  217.  
  218. #ifndef __UNPATCHED
  219.     // Prevents incorrect directory name display in info pane if wildCard
  220.     // has already been expanded.
  221.     strcpy(path, ((TFileDialog *)owner)->wildCard );
  222.     if(!strchr(path, ':') && !strchr(path, '/'))
  223.     {
  224.         strcpy( path, ((TFileDialog *)owner)->directory );
  225.         strcat( path, ((TFileDialog *)owner)->wildCard );
  226.         fexpand( path );
  227.     }
  228. #else
  229.     strcpy( path, ((TFileDialog *)owner)->directory );
  230.     strcat( path, ((TFileDialog *)owner)->wildCard );
  231.     fexpand( path );
  232. #endif
  233.  
  234.     color = getColor(0x01);
  235.     b.moveChar( 0, ' ', color, (ushort) size.x );
  236.     b.moveStr( 1, path, color );
  237.     writeLine( 0, 0, (ushort) size.x, 1, b );
  238.  
  239.     b.moveChar( 0, ' ', color, (ushort) size.x );
  240.     b.moveStr( 1, file_block.name, color );
  241.  
  242.     if( *(file_block.name) != EOS )
  243.         {
  244.  
  245.         char buf[10];
  246.  
  247.     /* SS: another little fix */
  248.  
  249. //        ltoa( file_block.size, buf, 10 );
  250. //    sprintf(buf, "%d", file_block.size); /* XXX */
  251.     sprintf(buf, "%ld", file_block.size); /* XXX */
  252.         b.moveStr( 14, buf, color );
  253.  
  254.         time = (ftime *) &file_block.time;
  255.         b.moveStr( 25, months[time->ft_month], color );
  256.  
  257. /*        if( time->ft_day >= 10 )
  258.             itoa( time->ft_day, buf, 10 );
  259.         else
  260.             {
  261.             buf[0] = '0';
  262.             itoa( time->ft_day, buf+1, 10 );
  263.             }*/
  264.     sprintf(buf, "%02d", time->ft_day);
  265.         b.moveStr( 29, buf, color );
  266.  
  267.         b.putChar( 31, ',' );
  268.  
  269.     /* SS: another little fix */
  270.  
  271. //        itoa( time->ft_year+1980, buf, 10 );
  272.     sprintf(buf, "%d", time->ft_year+1980);
  273.         b.moveStr( 32, buf, color );
  274.  
  275.         PM = Boolean(time->ft_hour >= 12 );
  276.         time->ft_hour %= 12;
  277.  
  278.         if( time->ft_hour == 0 )
  279.             time->ft_hour = 12;
  280.  
  281.     /* SS: another little fix */
  282.  
  283. /*        if( time->ft_hour >= 10 )
  284.             itoa( time->ft_hour, buf, 10 );
  285.         else
  286.             {
  287.             buf[0] = '0';
  288.             itoa( time->ft_hour, buf+1, 10 );
  289.             }*/
  290.     sprintf(buf, "%02d", time->ft_hour);
  291.         b.moveStr( 38, buf, color );
  292.         b.putChar( 40, ':' );
  293.  
  294.     /* SS: another little fix */
  295.  
  296. /*        if( time->ft_min >= 10 )
  297.             itoa( time->ft_min, buf, 10 );
  298.         else
  299.             {
  300.             buf[0] = '0';
  301.             itoa( time->ft_min, buf+1, 10 );
  302.             }*/
  303.     sprintf(buf, "%02d", time->ft_min);
  304.         b.moveStr( 41, buf, color );
  305.  
  306.         if( PM )
  307.             b.moveStr( 43, pmText, color );
  308.         else
  309.             b.moveStr( 43, amText, color );
  310.         }
  311.  
  312.     writeLine(0, 1, (ushort) size.x, 1, b );
  313.     b.moveChar( 0, ' ', color, (ushort) size.x );
  314.     writeLine( 0, 2, (ushort) size.x, (ushort) (size.y-2), b);
  315. }
  316.  
  317. TPalette& TFileInfoPane::getPalette() const
  318. {
  319.     static TPalette palette( cpInfoPane, sizeof( cpInfoPane )-1 );
  320.     return palette;
  321. }
  322.  
  323. void TFileInfoPane::handleEvent( TEvent& event )
  324. {
  325.     TView::handleEvent(event);
  326.     if( event.what == evBroadcast && event.message.command == cmFileFocused )
  327.         {
  328.         file_block = *((TSearchRec *)(event.message.infoPtr));
  329.         drawView();
  330.         }
  331. }
  332.  
  333. #if !defined(NO_STREAMABLE)
  334.  
  335. TStreamable *TFileInfoPane::build()
  336. {
  337.     return new TFileInfoPane( streamableInit );
  338. }
  339.  
  340. #ifndef __UNPATCHED
  341. TStreamable *TSortedListBox::build()
  342. {
  343.     return new TSortedListBox( streamableInit );
  344. }
  345.  
  346. void *TSortedListBox::read( ipstream& is )
  347. {
  348.     TListBox::read(is);
  349.  
  350.     // Must initialize these or serious memory overwrite
  351.     // problems can occur!
  352.     searchPos = -1;
  353.     shiftState = 0;
  354.  
  355.     return this;
  356. }
  357. #endif
  358.  
  359. #endif
  360.