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

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