home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tv20cpp.zip / src / TDirListBox.cpp < prev    next >
C/C++ Source or Header  |  1998-05-03  |  4KB  |  198 lines

  1. /*
  2.  * TDirListBox.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_TDirListBox
  13. #define Uses_TEvent
  14. #define Uses_TDirCollection
  15. #define Uses_TChDirDialog
  16. #define Uses_TDirEntry
  17. #define Uses_TButton
  18. #include <tvision/tv.h>
  19.  
  20. #include <sys/stat.h>
  21. #include <sys/types.h>
  22. #include <dirent.h>
  23. #include <stdio.h>
  24. #include <string.h>
  25.  
  26. TDirListBox::TDirListBox( const TRect& bounds, TScrollBar *aScrollBar ) :
  27.     TListBox( bounds, 1, aScrollBar ),
  28.     cur( 0 )
  29. {
  30.     *dir = EOS;
  31. }
  32.  
  33. TDirListBox::~TDirListBox()
  34. {
  35.    if ( list() )
  36.       destroy( list() );
  37. }
  38.  
  39. void TDirListBox::getText( char *text, short item, short maxChars )
  40. {
  41.     strncpy( text, list()->at(item)->text(), maxChars );
  42.     text[maxChars] = '\0';
  43. }
  44.  
  45. void TDirListBox::selectItem( short item )
  46. {
  47.     message( owner, evCommand, cmChangeDir, list()->at(item) );
  48. }
  49.  
  50. /*
  51. void TDirListBox::handleEvent( TEvent& event )
  52. {
  53.     if( event.what == evMouseDown && (event.mouse.eventFlags & meDoubleClick) )
  54.         {
  55.         event.what = evCommand;
  56.         event.message.command = cmChangeDir;
  57.         putEvent( event );
  58.         clearEvent( event );
  59.         }
  60.     else
  61.        TListBox::handleEvent( event );
  62. }
  63. */
  64.  
  65. Boolean TDirListBox::isSelected( short item )
  66. {
  67.     return Boolean( item == cur );
  68. }
  69.  
  70. void TDirListBox::showDrives( TDirCollection* /* dirs */ )    /* XXX */
  71. {
  72.     /* SS: do nothing */
  73. }
  74.  
  75. void TDirListBox::showDirs( TDirCollection *dirs )
  76. {
  77.     /* SS: changed */
  78.  
  79.     char buf[PATH_MAX * 2];
  80.     char *curDir = dir;
  81.     char *end;
  82.     char *name = buf + sizeof(buf) / 2;
  83.     const indentSize = 2;
  84.     int indent = 0, len;
  85.  
  86.     /* extract directories from path string */
  87.  
  88.     memset(buf, ' ', sizeof(buf));
  89.     strcpy(name, pathDir);
  90.     len = strlen(pathDir);
  91.     while((end = strchr(curDir, '/' )) != NULL)
  92.     {
  93.         /* special case: root directory */
  94.  
  95.         if (end == dir) dirs->insert(new TDirEntry("/", ""));
  96.         else
  97.         {
  98.             memcpy(name + len, curDir, end - curDir);
  99.             name[len + end - curDir] = EOS;
  100.             *end = EOS;
  101.             dirs->insert(new TDirEntry(name - indent, dir));
  102.             *end = '/';
  103.             indent += indentSize;
  104.         }
  105.         curDir = end + 1;
  106.     }
  107.     cur = dirs->getCount() - 1;
  108.  
  109.     /* read subdirectories in the current directory */
  110.  
  111.     Boolean isFirst = True;
  112.     DIR *dp;
  113.     char path[PATH_MAX];
  114.     dirent *de;
  115.     struct stat s;
  116.  
  117.     sprintf(path, "%s.", dir);
  118.     if ((dp = opendir(path)) != NULL)
  119.     {
  120.         while ((de = readdir(dp)) != NULL)
  121.         {
  122.             /* we don't want these directories */
  123.  
  124.             if (strcmp(de->d_name, ".") == 0 ||
  125.                 strcmp(de->d_name, "..") == 0) continue;
  126.  
  127.             /* is it a directory ? */
  128.  
  129.             sprintf(path, "%s%s", dir, de->d_name);
  130.             if (stat(path, &s) == 0 && S_ISDIR(s.st_mode))
  131.             {
  132.                 if (isFirst)
  133.                 {
  134.                     isFirst = False;
  135.                     strcpy(name, firstDir);
  136.                     len = strlen(firstDir);
  137.                 }
  138.                 else
  139.                 {
  140.                     strcpy(name, middleDir);
  141.                     len = strlen(middleDir);
  142.                 }
  143.                 strcpy(name + len, de->d_name);
  144.                 dirs->insert(new TDirEntry(name - indent,
  145.                     path));
  146.             }
  147.         }
  148.         closedir(dp);
  149.     }
  150.  
  151.     /* old code */
  152.  
  153.     char *p = dirs->at(dirs->getCount()-1)->text();
  154.     char *i = strchr( p, graphics[0] );
  155.     if( i == 0 )
  156.         {
  157.         i = strchr( p, graphics[1] );
  158.         if( i != 0 )
  159.             *i = graphics[0];
  160.         }
  161.     else
  162.         {
  163.         *(i+1) = graphics[2];
  164.         *(i+2) = graphics[2];
  165.         }
  166. }
  167.  
  168. void TDirListBox::newDirectory( const char *str )
  169. {
  170.     /* SS: changed */
  171.  
  172.     strcpy( dir, str );
  173.     TDirCollection *dirs = new TDirCollection( 5, 5 );
  174.     showDirs( dirs );
  175.     newList( dirs );
  176.     focusItem( cur );
  177. }
  178.  
  179. void TDirListBox::setState( ushort nState, Boolean enable )
  180. {
  181.     TListBox::setState( nState, enable );
  182.     if( (nState & sfFocused) != 0 )
  183. #ifndef __UNPATCHED
  184.         message(owner, evCommand, cmDirSelection, (void *)enable);  //!!
  185. #else
  186.         ((TChDirDialog *)owner)->chDirButton->makeDefault( enable );
  187. #endif
  188. }
  189.  
  190. #if !defined(NO_STREAMABLE)
  191.  
  192. TStreamable *TDirListBox::build()
  193. {
  194.     return new TDirListBox( streamableInit );
  195. }
  196.  
  197. #endif
  198.