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

  1. /*------------------------------------------------------------*/
  2. /* filename -       tdirlist.cpp                              */
  3. /*                                                            */
  4. /* function(s)                                                */
  5. /*                  TDirListBox 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_TDirListBox
  19. #define Uses_TEvent
  20. #define Uses_TDirCollection
  21. #define Uses_TChDirDialog
  22. #define Uses_TDirEntry
  23. #define Uses_TButton
  24. #include <tv.h>
  25.  
  26. #if !defined( __STRING_H )
  27. #include <String.h>
  28. #endif  // __STRING_H
  29.  
  30. #if !defined( __DIR_H )
  31. #include <Dir.h>
  32. #endif  // __DIR_H
  33.  
  34. #if !defined( __DOS_H )
  35. #include <Dos.h>
  36. #endif  // __DOS_H
  37.  
  38. TDirListBox::TDirListBox( const TRect& bounds, TScrollBar *aScrollBar ) :
  39.     TListBox( bounds, 1, aScrollBar ),
  40.     cur( 0 )
  41. {
  42.     *dir = EOS;
  43. }
  44.  
  45. TDirListBox::~TDirListBox()
  46.    if ( list() )
  47.       destroy( list() );
  48. }
  49.  
  50. void TDirListBox::getText( char *text, short item, short maxChars )
  51. {
  52.     strncpy( text, list()->at(item)->text(), maxChars );
  53.     text[maxChars] = '\0';
  54. }
  55.  
  56. void TDirListBox::handleEvent( TEvent& event )
  57. {
  58.     if( event.what == evMouseDown && event.mouse.doubleClick )
  59.         {
  60.         event.what = evCommand;
  61.         event.message.command = cmChangeDir;
  62.         putEvent( event );
  63.         clearEvent( event );
  64.         }
  65.     else
  66.         TListBox::handleEvent( event );
  67. }
  68.  
  69. Boolean TDirListBox::isSelected( short item )
  70. {
  71.     return Boolean( item == cur );
  72. }
  73.  
  74. void TDirListBox::showDrives( TDirCollection *dirs )
  75. {
  76.     Boolean isFirst = True;
  77.     char oldc[5];
  78.     strcpy( oldc, "0:\\" );
  79.     for( char c = 'A'; c <= 'Z'; c++ )
  80.         {
  81.         if( c < 'C' || driveValid( c ) )
  82.             {
  83.             if( oldc[0] != '0' )
  84.                 {
  85.                 char s[ 16 ];
  86.                 if( isFirst )
  87.                     {
  88.                     strcpy( s, firstDir );
  89.                     s[ strlen(firstDir) ] = oldc[0];
  90.                     s[ strlen(firstDir)+1 ] = EOS;
  91.                     isFirst = False;
  92.                     }
  93.                 else
  94.                     {
  95.                     strcpy( s, middleDir );
  96.                     s[ strlen(middleDir) ] = oldc[0];
  97.                     s[ strlen(middleDir)+1 ] = EOS;
  98.                     }
  99.                 dirs->insert( new TDirEntry( s, oldc ) );
  100.                 }
  101.             if( c == getdisk() + 'A' )
  102.                 cur = dirs->getCount();
  103.             oldc[0] = c;
  104.             }
  105.         }
  106.     if( oldc[0] != '0' )
  107.         {
  108.         char s[ 16 ];
  109.         strcpy( s, lastDir );
  110.         s[ strlen(lastDir) ] = oldc[0];
  111.         s[ strlen(lastDir)+1 ] = EOS;
  112.         dirs->insert( new TDirEntry( s, oldc ) );
  113.         }
  114. }
  115.  
  116. void TDirListBox::showDirs( TDirCollection *dirs )
  117. {
  118.     const indentSize = 2;
  119.     int indent = indentSize;
  120.  
  121.     char buf[MAXPATH+16];
  122.     memset( buf, ' ', sizeof( buf ) );
  123.     char *name = buf + sizeof(buf) - (MAXFILE+MAXEXT);
  124.  
  125.     char *org = name - strlen(pathDir);
  126.     strcpy( org, pathDir );
  127.  
  128.     char *curDir = dir;
  129.     char *end = dir + 3;
  130.     char hold = *end;
  131.     *end = EOS;         // mark end of drive name
  132.     strcpy( name, curDir );
  133.     dirs->insert( new TDirEntry( org, name ) );
  134.  
  135.     *end = hold;        // restore full path
  136.     curDir = end;
  137.     while( (end = strchr( curDir, '\\' )) != 0 )
  138.         {
  139.         *end = EOS;
  140.         strncpy( name, curDir, size_t(end-curDir) );
  141.         name[size_t(end-curDir)] = EOS;
  142.         dirs->insert( new TDirEntry( org - indent, dir ) );
  143.         *end = '\\';
  144.         curDir = end+1;
  145.         indent += indentSize;
  146.         }
  147.  
  148.     cur = dirs->getCount() - 1;
  149.  
  150.     end = strrchr( dir, '\\' );
  151.     char path[MAXPATH];
  152.     strncpy( path, dir, size_t(end-dir+1) );
  153.     end = path + unsigned(end-dir)+1;
  154.     strcpy( end, "*.*" );
  155.  
  156.     Boolean isFirst = True;
  157.     ffblk ff;
  158.     int res = findfirst( path, &ff, FA_DIREC );
  159.     while( res == 0 )
  160.         {
  161.         if( (ff.ff_attrib & FA_DIREC) != 0 && ff.ff_name[0] != '.' )
  162.             {
  163.             if( isFirst )
  164.                 {
  165.                 memcpy( org, firstDir, strlen(firstDir)+1 );
  166.                 isFirst = False;
  167.                 }
  168.             else
  169.                 memcpy( org, middleDir, strlen(middleDir)+1 );
  170.             strcpy( name, ff.ff_name );
  171.             strcpy( end, ff.ff_name );
  172.             dirs->insert( new TDirEntry( org - indent, path ) );
  173.             }
  174.         res = findnext( &ff );
  175.         }
  176.  
  177.     char *p = dirs->at(dirs->getCount()-1)->text();
  178.     char *i = strchr( p, graphics[0] );
  179.     if( i == 0 )
  180.         {
  181.         i = strchr( p, graphics[1] );
  182.         if( i != 0 )
  183.             *i = graphics[0];
  184.         }
  185.     else
  186.         {
  187.         *(i+1) = graphics[2];
  188.         *(i+2) = graphics[2];
  189.         }
  190. }
  191.  
  192. void TDirListBox::newDirectory( const char *str )
  193. {
  194.     strcpy( dir, str );
  195.     TDirCollection *dirs = new TDirCollection( 5, 5 );
  196.     dirs->insert( new TDirEntry( drives, drives ) );
  197.     if( strcmp( dir, drives ) == 0 )
  198.         showDrives( dirs );
  199.     else
  200.         showDirs( dirs );
  201.     newList( dirs );
  202.     focusItem( cur );
  203. }
  204.  
  205. void TDirListBox::setState( ushort nState, Boolean enable )
  206. {
  207.     TListBox::setState( nState, enable );
  208.     if( (nState & sfFocused) != 0 )
  209.         ((TChDirDialog *)owner)->chDirButton->makeDefault( enable );
  210. }
  211.  
  212. TStreamable *TDirListBox::build()
  213. {
  214.     return new TDirListBox( streamableInit );
  215. }
  216.  
  217.  
  218.