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

  1. /*
  2.  * TFileDialog.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_TFileDialog
  13. #define Uses_MsgBox
  14. #define Uses_TRect
  15. #define Uses_TFileInputLine
  16. #define Uses_TButton
  17. #define Uses_TLabel
  18. #define Uses_TFileList
  19. #define Uses_THistory
  20. #define Uses_TScrollBar
  21. #define Uses_TEvent
  22. #define Uses_TFileInfoPane
  23. #define Uses_opstream
  24. #define Uses_ipstream
  25. #include <tvision/tv.h>
  26.  
  27. #include <ctype.h>
  28. #include <errno.h>
  29. #include <stdio.h>
  30. #include <string.h>
  31.  
  32. // File dialog flags
  33. const
  34.     ffOpen        = 0x0001,
  35.     ffSaveAs      = 0x0002;
  36.  
  37. const
  38.     cmOpenDialogOpen    = 100,
  39.     cmOpenDialogReplace = 101;
  40.  
  41. TFileDialog::TFileDialog( const char *aWildCard,
  42.                           const char *aTitle,
  43.                           const char *inputName,
  44.                           ushort aOptions,
  45.                           uchar histId
  46.                         ) :
  47.     TDialog( TRect( 15, 1, 64, 20 ), aTitle ),
  48.     directory( newStr("") ),
  49.     TWindowInit( &TFileDialog::initFrame )
  50. {
  51.     options |= ofCentered;
  52.     strcpy( wildCard, aWildCard );
  53.  
  54.     fileName = new TFileInputLine( TRect( 3, 3, 31, 4 ), 79 );
  55.     strcpy( fileName->data, wildCard );
  56.     insert( fileName );
  57.  
  58.     insert( new TLabel( TRect( 2, 2, 3+cstrlen(inputName), 3 ),
  59.                         inputName,
  60.                         fileName
  61.                       ) );
  62.     insert( new THistory( TRect( 31, 3, 34, 4 ), fileName, histId ) );
  63.     TScrollBar *sb = new TScrollBar( TRect( 3, 14, 34, 15 ) );
  64.     insert( sb );
  65.     insert( fileList = new TFileList( TRect( 3, 6, 34, 14 ), sb ) );
  66.     insert( new TLabel( TRect( 2, 5, 8, 6 ), filesText, fileList ) );
  67.  
  68.     ushort opt = bfDefault;
  69.     TRect r( 35, 3, 46, 5 );
  70.  
  71.     if( (aOptions & fdOpenButton) != 0 )
  72.         {
  73.         insert( new TButton( r, openText, cmFileOpen, opt ) );
  74.         opt = bfNormal;
  75.         r.a.y += 3;
  76.         r.b.y += 3;
  77.         }
  78.  
  79.     if( (aOptions & fdOKButton) != 0 )
  80.         {
  81.         insert( new TButton( r, okText, cmFileOpen, opt ) );
  82.         opt = bfNormal;
  83.         r.a.y += 3;
  84.         r.b.y += 3;
  85.         }
  86.  
  87.     if( (aOptions & fdReplaceButton) != 0 )
  88.         {
  89.         insert( new TButton( r, replaceText, cmFileReplace, opt ) );
  90.         opt = bfNormal;
  91.         r.a.y += 3;
  92.         r.b.y += 3;
  93.         }
  94.  
  95.     if( (aOptions & fdClearButton) != 0 )
  96.         {
  97.         insert( new TButton( r, clearText, cmFileClear, opt ) );
  98. #ifndef __UNPATCHED
  99.     // opt = bfNormal;
  100. #else
  101.         opt = bfNormal;
  102. #endif
  103.         r.a.y += 3;
  104.         r.b.y += 3;
  105.         }
  106.  
  107.     insert( new TButton( r, cancelText, cmCancel, bfNormal ) );
  108.     r.a.y += 3;
  109.     r.b.y += 3;
  110.  
  111.     if( (aOptions & fdHelpButton) != 0 )
  112.         {
  113.         insert( new TButton( r, helpText, cmHelp, bfNormal ) );
  114. #ifndef __UNPATCHED
  115.     // opt = bfNormal;
  116. #else
  117.         opt = bfNormal;
  118. #endif
  119.         r.a.y += 3;
  120.         r.b.y += 3;
  121.         }
  122.  
  123.     insert( new TFileInfoPane( TRect( 1, 16, 48, 18 ) ) );
  124.  
  125.     selectNext( False );
  126.     if( (aOptions & fdNoLoadDir) == 0 )
  127.         readDirectory();
  128. }
  129.  
  130. TFileDialog::~TFileDialog()
  131. {
  132.     delete (char *)directory;
  133. }
  134.  
  135. void TFileDialog::shutDown()
  136. {
  137.     fileName = 0;
  138.     fileList = 0;
  139.     TDialog::shutDown();
  140. }
  141.  
  142. static Boolean relativePath( const char *path )
  143. {
  144.     /* SS: changed */
  145.  
  146.     if (path[0] == '/') return False;
  147.     else return True;
  148. }
  149.  
  150. /* Function defined but not used    // XXX
  151. static void noWildChars( char *dest, const char *src )
  152. {
  153.     while( *src != EOS )
  154.         {
  155.         if( *src != '?' && *src != '*' )
  156.             *dest++ = *src;
  157.         src++;
  158.         }
  159.     *dest = EOS;
  160. }
  161. */    /* XXX */
  162.  
  163. /* 'src' is cast to unsigned char * so that isspace sign extends it
  164.    correctly. */
  165. static void trim( char *dest, const char *src )
  166. {
  167.     while( *src != EOS && isspace( * (const unsigned char *) src ) )
  168.         src++;
  169.     while( *src != EOS && !isspace( * (const unsigned char *) src ) )
  170.         *dest++ = *src++;
  171.     *dest = EOS;
  172. }
  173.  
  174. void TFileDialog::getFileName( char *s )
  175. {
  176.     /* SS: changed */
  177.  
  178.     char buf[PATH_MAX];
  179.  
  180.     trim( buf, fileName->data );
  181.     if( relativePath( buf ) == True )
  182.         {
  183.         strcpy( buf, directory );
  184.         trim( buf + strlen(buf), fileName->data );
  185.         }
  186.     fexpand( buf );
  187.     strcpy( s, buf );
  188. }
  189.  
  190. void TFileDialog::handleEvent(TEvent& event)
  191. {
  192.     TDialog::handleEvent(event);
  193.     if( event.what == evCommand )
  194.         {
  195.         switch( event.message.command )
  196.             {
  197.             case cmFileOpen:
  198.             case cmFileReplace:
  199.             case cmFileClear:
  200.                 endModal(event.message.command);
  201.                 clearEvent(event);
  202.                 break;
  203.             default:
  204.                 break;
  205.             }
  206.         }
  207.     else if( event.what == evBroadcast && event.message.command == cmFileDoubleClicked )
  208.         {
  209.         event.what = evCommand;
  210.         event.message.command = cmOK;
  211.         putEvent( event );
  212.         clearEvent( event );
  213.         }
  214. }
  215.  
  216. void TFileDialog::readDirectory()
  217. {
  218.     char curDir[PATH_MAX];
  219.     getCurDir( curDir );
  220.     if( directory )
  221.         delete (char *)directory;
  222.     directory = newStr( curDir );
  223.     fileList->readDirectory( wildCard );
  224. }
  225.  
  226. void TFileDialog::setData( void *rec )
  227. {
  228.     TDialog::setData( rec );
  229.     if( *(char *)rec != EOS && isWild( (char *)rec ) )
  230.         {
  231.         valid( cmFileInit );
  232.         fileName->select();
  233.         }
  234. }
  235.  
  236. void TFileDialog::getData( void *rec )
  237. {
  238.     getFileName( (char *)rec );
  239. }
  240.  
  241. Boolean TFileDialog::checkDirectory( const char *str )
  242. {
  243.     if( pathValid( str ) )
  244.         return True;
  245.     else
  246.         {
  247.         messageBox( invalidDriveText, mfError | mfOKButton );
  248.         fileName->select();
  249.         return False;
  250.         }
  251. }
  252.  
  253. Boolean TFileDialog::valid(ushort command)
  254. {
  255.     /* SS: changed */
  256.  
  257.     char fName[PATH_MAX];
  258.     char name[PATH_MAX];
  259.  
  260.     if( command == 0 )
  261.         return True;
  262.  
  263.     if( TDialog::valid( command ) )
  264.         {
  265.         if( command != cmCancel && command != cmFileClear )
  266.             {
  267.             getFileName( fName );
  268.  
  269.             if( isWild( fName ) )
  270.                 {
  271.         /* SS: changed */
  272.  
  273.         char path[PATH_MAX];
  274.         expandPath(fName, path, name);
  275.                 if( checkDirectory( path ) )
  276.                     {
  277.                     delete (char *)directory;
  278.                     directory = newStr( path );
  279.                     strcpy( wildCard, name );
  280.                     if( command != cmFileInit )
  281.                         fileList->select();
  282.                     fileList->readDirectory( directory, wildCard );
  283.                     }
  284.                 }
  285.             else if( isDir( fName ) )
  286.                 {
  287.                 if( checkDirectory( fName ) )
  288.                     {
  289.                     delete (char *)directory;
  290.                     strcat( fName, "/" );
  291.                     directory = newStr( fName );
  292.                     if( command != cmFileInit )
  293.                         fileList->select();
  294.                     fileList->readDirectory( directory, wildCard );
  295.                     }
  296.                 }
  297.             else if( validFileName( fName ) )
  298.                 return True;
  299.             else
  300.                 {
  301.                 messageBox( invalidFileText, mfError | mfOKButton );
  302.                 return False;
  303.                 }
  304.             }
  305.         else
  306.             return True;
  307.         }
  308.     return False;
  309. }
  310.  
  311. #if !defined(NO_STREAMABLE)
  312.  
  313. void TFileDialog::write( opstream& os )
  314. {
  315.     TDialog::write( os );
  316.     os.writeString( wildCard );
  317.     os << fileName << fileList;
  318. }
  319.  
  320. void *TFileDialog::read( ipstream& is )
  321. {
  322.     TDialog::read( is );
  323.     is.readString( wildCard, sizeof(wildCard) );
  324.     is >> fileName >> fileList;
  325.     readDirectory();
  326.     return this;
  327. }
  328.  
  329. TStreamable *TFileDialog::build()
  330. {
  331.     return new TFileDialog( streamableInit );
  332. }
  333.  
  334.  
  335. #endif
  336.