home *** CD-ROM | disk | FTP | other *** search
/ The Pier Shareware 6 / The_Pier_Shareware_Number_6_(The_Pier_Exchange)_(1995).iso / 035 / kwclass.zip / KDIRLST.CPP < prev    next >
C/C++ Source or Header  |  1994-04-18  |  4KB  |  148 lines

  1. #define INCL_BASE
  2. #include <os2.h>
  3. #include <iwindow.hpp>
  4. #include <kdirlst.hpp>
  5. #include <itextctl.hpp>
  6. #include <ictlevt.hpp>
  7. #include <kwconst.hpp>
  8.  
  9. Boolean KDirListBox::fill()
  10. {
  11.    FILEFINDBUF3 findBuf;
  12.    unsigned long ulSearchCount;
  13.    HDIR hDir;
  14.    APIRET rc;
  15.  
  16.    // use the system-default search-directory handle
  17.    hDir = HDIR_SYSTEM;
  18.  
  19.    // initialize the search count to 1, to only find 1 file at a time
  20.    ulSearchCount = 1;
  21.  
  22.    // clear current items
  23.    removeAll();
  24.  
  25.    // initiate the search
  26.    rc = DosFindFirst(spec, 
  27.                      &hDir, FILE_DIRECTORY,
  28.                      &findBuf, sizeof(findBuf),
  29.                      &ulSearchCount, FIL_STANDARD);
  30.    if (rc)
  31.       return false;
  32.    else
  33.    {
  34.       while (rc != ERROR_NO_MORE_FILES)
  35.       {
  36.          // if this is a directory and it isn't the current directory,
  37.          // add it to the list box
  38.          IString name = findBuf.achName;
  39.          if ((findBuf.attrFile & FILE_DIRECTORY) &&  name != ".")
  40.             addAsLast(findBuf.achName);
  41.  
  42.          // get the next file
  43.          rc = DosFindNext(hDir, 
  44.                           &findBuf, sizeof(findBuf),
  45.                           &ulSearchCount);
  46.       }
  47.  
  48.       // terminate the search
  49.       DosFindClose(hDir);
  50.    }
  51.  
  52.    // add drive letters to the list box following all directories
  53.    unsigned long ulDriveNum,
  54.                  ulDriveMap;
  55.  
  56.    // first retrieve the drive map from the system
  57.    rc = DosQueryCurrentDisk(&ulDriveNum, &ulDriveMap);
  58.    if (rc)
  59.       return false;
  60.  
  61.    // loop through each bit of the drive map, each of the lower 26 bits
  62.    // represents a drive letter
  63.    for (char cDrive = 0; cDrive < 26; cDrive++)
  64.    {
  65.       if (ulDriveMap & (1 << cDrive))
  66.       {
  67.          // construct a string for the drive name
  68.          IString drive((char)(cDrive + 'A'));
  69.          IString label = "[" + drive + ":]";
  70.  
  71.          // and add it to the list box
  72.          addAsLast(label);
  73.       }
  74.    }
  75.  
  76.    // set the target entry field to point to the current drive/directory
  77.    if (target)
  78.    {
  79.       // query the current directory name
  80.       unsigned long ulPathLen = 256;
  81.       IString currentDir(0, ulPathLen);
  82.       DosQueryCurrentDir(ulDriveNum, currentDir, &ulPathLen);
  83.  
  84.       // construct a full path name string consisting of drive:\directory
  85.       IString currentStr((char)('@' + ulDriveNum));
  86.       currentStr += ":\\" + currentDir;
  87.  
  88.       // and add it to the list box
  89.       target->setText(currentStr);
  90.    }
  91.    
  92.    return true;
  93. }
  94.  
  95. static IBase::Boolean _Optlink DriveTest(int c)
  96. {
  97.    return (c >= 'A' && c <= 'Z');
  98. }
  99.  
  100. Boolean KDirListBox::enter(IControlEvent &event)
  101. {
  102.    IString selected = itemText(selection());
  103.  
  104.    // check to see if the new selection is a drive or a directory
  105.    if (selected.isLike("[?:]"))
  106.    {
  107.      // save current drive in case of error
  108.      unsigned long ulCurDisk, ulDriveMap;
  109.      DosQueryCurrentDisk(&ulCurDisk, &ulDriveMap);
  110.  
  111.      // change to the new drive. On error (e.g. the drive doesn't exist),
  112.      // restore to the previous drive
  113.  
  114.      IStringTest test(DriveTest);
  115.      char drive = selected[selected.indexOf(test)];
  116.      if (DosSetDefaultDisk(drive - '@') != 0)
  117.        DosSetDefaultDisk(ulCurDisk);
  118.  
  119.      // refill the list box, checking for an error again...
  120.      // if the drive isn't ready (e.g., floppy or CD), fill returns an error
  121.      if (!fill())
  122.      {
  123.        DosSetDefaultDisk(ulCurDisk);
  124.        fill();
  125.      }
  126.  
  127.    }
  128.    else
  129.    {
  130.       DosSetCurrentDir(selected);
  131.       // refill the list box
  132.       fill();
  133.  
  134.    }
  135.  
  136.    // inform all file list/combo boxes descended from my owner that 
  137.    // the current drive/directory has changed so they can refill themselves...
  138.  
  139.    // postEvents throws an exception, so simulate it here
  140.    //   owner()->handle().postEvents(KW_FILL);
  141.  
  142.    IWindow::ChildCursor cursor(*owner());
  143.    for(cursor.setToFirst(); cursor.isValid(); cursor.setToNext())
  144.       childAt(cursor).sendEvent(KW_FILL);
  145.    return false;
  146. }
  147.  
  148.