home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c082_144 / 9.ddi / TVSRC.ZIP / TCHDRDLG.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-10  |  5.8 KB  |  203 lines

  1. /*------------------------------------------------------------*/
  2. /* filename -       tchdrdlg.cpp                              */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*          TChDirDialog member functions                     */
  6. /*------------------------------------------------------------*/
  7.  
  8. /*------------------------------------------------------------*/
  9. /*                                                            */
  10. /*    Turbo Vision -  Version 1.0                             */
  11. /*                                                            */
  12. /*                                                            */
  13. /*    Copyright (c) 1991 by Borland International             */
  14. /*    All Rights Reserved.                                    */
  15. /*                                                            */
  16. /*------------------------------------------------------------*/
  17.  
  18. #define Uses_MsgBox
  19. #define Uses_TChDirDialog
  20. #define Uses_TRect
  21. #define Uses_TInputLine
  22. #define Uses_TLabel
  23. #define Uses_THistory
  24. #define Uses_TScrollBar
  25. #define Uses_TDirListBox
  26. #define Uses_TButton
  27. #define Uses_TEvent
  28. #define Uses_TDirEntry
  29. #define Uses_TDirCollection
  30. #define Uses_TChDirDialog
  31. #define Uses_opstream
  32. #define Uses_ipstream
  33. #include <tv.h>
  34.  
  35. #if !defined( __CTYPE_H )
  36. #include <Ctype.h>
  37. #endif    // __CTYPE_H
  38.  
  39. #if !defined( __STRING_H )
  40. #include <String.h>
  41. #endif  // __STRING_H
  42.  
  43. TChDirDialog::TChDirDialog( ushort opts, ushort histId ) :
  44.     TDialog( TRect( 16, 2, 64, 20 ), changeDirTitle ),
  45.     TWindowInit( &TChDirDialog::initFrame )
  46. {
  47.     options |= ofCentered;
  48.  
  49.     dirInput = new TInputLine( TRect( 3, 3, 30, 4 ), 68 );
  50.     insert( dirInput );
  51.     insert( new TLabel( TRect( 2, 2, 17, 3 ), dirNameText, dirInput ));
  52.     insert( new THistory( TRect( 30, 3, 33, 4 ), dirInput, histId ) );
  53.  
  54.     TScrollBar *sb = new TScrollBar( TRect( 32, 6, 33, 16 ) );
  55.     insert( sb );
  56.     dirList = new TDirListBox( TRect( 3, 6, 32, 16 ), sb );
  57.     insert( dirList );
  58.     insert( new TLabel( TRect( 2, 5, 17, 6 ), dirTreeText, dirList ) );
  59.  
  60.     okButton = new TButton( TRect( 35, 6, 45, 8 ), okText, cmOK, bfDefault );
  61.     insert( okButton );
  62.     chDirButton = new TButton( TRect( 35, 9, 45, 11 ), chdirText, cmChangeDir, bfNormal );
  63.     insert( chDirButton );
  64.     insert( new TButton( TRect( 35, 12, 45, 14 ), revertText, cmRevert, bfNormal ) );
  65.     if( (opts & cdHelpButton) != 0 )
  66.         insert( new TButton( TRect( 35, 15, 45, 17 ), helpText, cmHelp, bfNormal ) );
  67.     if( (opts & cdNoLoadDir) == 0 )
  68.         setUpDialog();
  69.     selectNext( False );
  70. }
  71.  
  72. ushort TChDirDialog::dataSize()
  73. {
  74.     return 0;
  75. }
  76.  
  77. void TChDirDialog::shutDown()
  78. {
  79.     dirList = 0;
  80.     dirList = 0;
  81.     okButton = 0;
  82.     chDirButton = 0;
  83.     TDialog::shutDown();
  84. }
  85.  
  86. void TChDirDialog::getData( void * )
  87. {
  88. }
  89.  
  90. void TChDirDialog::handleEvent( TEvent& event )
  91. {
  92.     TDialog::handleEvent( event );
  93.     switch( event.what )
  94.         {
  95.         case evCommand:
  96.             {
  97.             char curDir[MAXPATH];
  98.             switch( event.message.command )
  99.                 {
  100.                 case cmRevert:
  101.                     getCurDir( curDir );
  102.                     break;
  103.                 case cmChangeDir:
  104.                     {
  105.                     TDirEntry *p = dirList->list()->at( dirList->focused );
  106.                     strcpy( curDir, p->dir() );
  107.                     if( strcmp( curDir, drivesText ) == 0 )
  108.                         break;
  109.                     else if( driveValid( curDir[0] ) )
  110.                         {
  111.                         if( curDir[strlen(curDir)-1] != '\\' )
  112.                             strcat( curDir, "\\" );
  113.                         }
  114.                     else
  115.                         return;
  116.                     break;
  117.                     }
  118.                 default:
  119.                     return;
  120.                 }
  121.             dirList->newDirectory( curDir );
  122.             int len = strlen( curDir );
  123.             if( len > 3 && curDir[len-1] == '\\' )
  124.                 curDir[len-1] = EOS;
  125.             strcpy( dirInput->data, curDir );
  126.             dirInput->drawView();
  127.             dirList->select();
  128.             clearEvent( event );
  129.             }
  130.         default:
  131.             break;
  132.         }
  133. }
  134.  
  135. void TChDirDialog::setData( void * )
  136. {
  137. }
  138.  
  139. void TChDirDialog::setUpDialog()
  140. {
  141.     if( dirList != 0 )
  142.         {
  143.         char curDir[MAXPATH];
  144.         getCurDir( curDir );
  145.         dirList->newDirectory( curDir );
  146.         if( dirInput != 0 )
  147.             {
  148.             int len = strlen( curDir );
  149.             if( len > 3 && curDir[len-1] == '\\' )
  150.                 curDir[len-1] = EOS;
  151.             strcpy( dirInput->data, curDir );
  152.             dirInput->drawView();
  153.             }
  154.         }
  155. }
  156.  
  157. static int changeDir( const char *path )
  158. {
  159.     if( path[1] == ':' )
  160.         setdisk( toupper(path[0]) - 'A' );
  161.     return chdir( path );
  162. }
  163.  
  164. Boolean TChDirDialog::valid( ushort command )
  165. {
  166.     if( command != cmOK )
  167.         return True;
  168.  
  169.     char path[MAXPATH];
  170.     strcpy( path, dirInput->data );
  171.     fexpand( path );
  172.  
  173.     int len = strlen( path );
  174.     if( len > 3 && path[len-1] == '\\' )
  175.         path[len-1] = EOS;
  176.  
  177.     if( changeDir( path ) != 0 )
  178.         {
  179.         messageBox( invalidText, mfError | mfOKButton );
  180.         return False;
  181.         }
  182.     return True;
  183. }
  184.  
  185. void TChDirDialog::write( opstream& os )
  186. {
  187.     TDialog::write( os );
  188.     os << dirList << dirInput << okButton << chDirButton;
  189. }
  190.  
  191. void *TChDirDialog::read( ipstream& is )
  192. {
  193.     TDialog::read( is );
  194.     is >> dirList >> dirInput >> okButton >> chDirButton;
  195.     setUpDialog();
  196.     return this;
  197. }
  198.  
  199. TStreamable *TChDirDialog::build()
  200. {
  201.     return new TChDirDialog( streamableInit );
  202. }
  203.