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

  1. /*
  2.  * TChDirDialog.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_TChDirDialog
  14. #define Uses_TRect
  15. #define Uses_TInputLine
  16. #define Uses_TLabel
  17. #define Uses_THistory
  18. #define Uses_TScrollBar
  19. #define Uses_TDirListBox
  20. #define Uses_TButton
  21. #define Uses_TEvent
  22. #define Uses_TDirEntry
  23. #define Uses_TDirCollection
  24. #define Uses_TChDirDialog
  25. #define Uses_opstream
  26. #define Uses_ipstream
  27. #include <tvision/tv.h>
  28.  
  29. #include <ctype.h>
  30. #include <limits.h>
  31. #include <string.h>
  32. #ifdef __OS2__
  33. #include <dir.h>
  34. #else
  35. #include <unistd.h>
  36. #endif
  37.  
  38. TChDirDialog::TChDirDialog( ushort opts, ushort histId ) :
  39.     TDialog( TRect( 16, 2, 64, 20 ), changeDirTitle ),
  40.     TWindowInit( &TChDirDialog::initFrame )
  41. {
  42.     options |= ofCentered;
  43.  
  44.     dirInput = new TInputLine( TRect( 3, 3, 30, 4 ), 68 );
  45.     insert( dirInput );
  46.     insert( new TLabel( TRect( 2, 2, 17, 3 ), dirNameText, dirInput ));
  47.     insert( new THistory( TRect( 30, 3, 33, 4 ), dirInput, histId ) );
  48.  
  49.     TScrollBar *sb = new TScrollBar( TRect( 32, 6, 33, 16 ) );
  50.     insert( sb );
  51.     dirList = new TDirListBox( TRect( 3, 6, 32, 16 ), sb );
  52.     insert( dirList );
  53.     insert( new TLabel( TRect( 2, 5, 17, 6 ), dirTreeText, dirList ) );
  54.  
  55.     okButton = new TButton( TRect( 35, 6, 45, 8 ), okText, cmOK, bfDefault );
  56.     insert( okButton );
  57.     chDirButton = new TButton( TRect( 35, 9, 45, 11 ), chdirText, cmChangeDir, bfNormal );
  58.     insert( chDirButton );
  59.     insert( new TButton( TRect( 35, 12, 45, 14 ), revertText, cmRevert, bfNormal ) );
  60.     if( (opts & cdHelpButton) != 0 )
  61.         insert( new TButton( TRect( 35, 15, 45, 17 ), helpText, cmHelp, bfNormal ) );
  62.     if( (opts & cdNoLoadDir) == 0 )
  63.         setUpDialog();
  64.     selectNext( False );
  65. }
  66.  
  67. ushort TChDirDialog::dataSize()
  68. {
  69.     return 0;
  70. }
  71.  
  72. void TChDirDialog::shutDown()
  73. {
  74.     dirList = 0;
  75.     dirInput = 0;
  76.     okButton = 0;
  77.     chDirButton = 0;
  78.     TDialog::shutDown();
  79. }
  80.  
  81. void TChDirDialog::getData( void * )
  82. {
  83. }
  84.  
  85. void TChDirDialog::handleEvent( TEvent& event )
  86. {
  87.     TDialog::handleEvent( event );
  88.     switch( event.what )
  89.         {
  90.         case evCommand:
  91.             {
  92.             char curDir[PATH_MAX];
  93.             switch( event.message.command )
  94.                 {
  95.                 case cmRevert:
  96.                     getCurDir( curDir );
  97.                     break;
  98.                 case cmChangeDir:
  99.                     {
  100.                     TDirEntry *p = dirList->list()->at( dirList->focused );
  101.                     strcpy( curDir, p->dir() );
  102.             /* SS: changed */
  103.                         if( curDir[strlen(curDir)-1] != '/' )
  104.                             strcat( curDir, "/" );
  105.                     break;
  106.                     }
  107. #ifndef __UNPATCHED
  108.         //!! Handle directory selection.
  109.                 case cmDirSelection:
  110.                     chDirButton->makeDefault( (Boolean) event.message.infoPtr );
  111.                     return;     // NOTE: THIS IS RETURN NOT BREAK!!
  112. #endif
  113.                 default:
  114.                     return;
  115.                 }
  116.             dirList->newDirectory( curDir );
  117.             int len = strlen( curDir );
  118.         /* SS: changed */
  119.             if( len > 0 && curDir[len-1] == '/' )
  120.                 curDir[len-1] = EOS;
  121.             strcpy( dirInput->data, curDir );
  122.             dirInput->drawView();
  123.             dirList->select();
  124.             clearEvent( event );
  125.             }
  126.         default:
  127.             break;
  128.         }
  129. }
  130.  
  131. void TChDirDialog::setData( void * )
  132. {
  133. }
  134.  
  135. void TChDirDialog::setUpDialog()
  136. {
  137.     if( dirList != 0 )
  138.         {
  139.         char curDir[PATH_MAX];
  140.         getCurDir( curDir );
  141.         dirList->newDirectory( curDir );
  142.         if( dirInput != 0 )
  143.             {
  144.             int len = strlen( curDir );
  145.         /* SS: changed */
  146.             if( len > 0 && curDir[len-1] == '/' )
  147.                 curDir[len-1] = EOS;
  148.             strcpy( dirInput->data, curDir );
  149.             dirInput->drawView();
  150.             }
  151.         }
  152. }
  153.  
  154. static int changeDir( const char *path )
  155. {
  156.     /* SS: changed */
  157.     int rc; char *p;
  158.     if( strlen( path ) > 0 )
  159.         { 
  160.         p = (char *)&path[strlen(path)-1];
  161.         if( *p == ':' )
  162.             { 
  163.             ++p; *p = '/';
  164.             ++p; *p = 0;
  165.             }
  166.         }
  167.     Slash2backSlash( (char *)path );
  168.     rc = chdir( path );
  169.     backSlash2Slash( (char *)path );
  170.     return rc;
  171. }
  172.  
  173. Boolean TChDirDialog::valid( ushort command )
  174. {
  175.     if( command != cmOK )
  176.         return True;
  177.  
  178.     char path[PATH_MAX];
  179.     strcpy( path, dirInput->data );
  180.  
  181. #ifndef __UNPATCHED
  182.     // BUG FIX - EFW - Tue 05/16/95
  183.     // Ignore "Drives" line if switching drives.
  184.     if(!strcmp(path, drivesText))
  185.         path[0] = EOS;
  186.  
  187.     // If it was "Drives" or the input line was blank, issue a
  188.     // cmChangeDir event to select the current drive/directory.
  189.     if(!path[0])
  190.     {
  191.         TEvent event;
  192.         event.what = evCommand;
  193.         event.message.command = cmChangeDir;
  194.         putEvent(event);
  195.         return False;
  196.     }
  197.  
  198.     // Otherwise, expand and check the path.
  199. #endif
  200.     fexpand( path );
  201.  
  202.     int len = strlen( path );
  203.     /* SS: changed */
  204.     if( len > 0 && path[len-1] == '/' )
  205.         path[len-1] = EOS;
  206.  
  207.     if( changeDir( path ) != 0 )
  208.         {
  209.         messageBox( invalidText, mfError | mfOKButton );
  210.         return False;
  211.         }
  212.     return True;
  213. }
  214.  
  215. #if !defined(NO_STREAMABLE)
  216.  
  217. void TChDirDialog::write( opstream& os )
  218. {
  219.     TDialog::write( os );
  220.     os << dirList << dirInput << okButton << chDirButton;
  221. }
  222.  
  223. void *TChDirDialog::read( ipstream& is )
  224. {
  225.     TDialog::read( is );
  226.     is >> dirList >> dirInput >> okButton >> chDirButton;
  227.     setUpDialog();
  228.     return this;
  229. }
  230.  
  231. TStreamable *TChDirDialog::build()
  232. {
  233.     return new TChDirDialog( streamableInit );
  234. }
  235.  
  236. #endif
  237.