home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / ocl150a.zip / OCL / Samples / GTakPM / Source / DrivesCnr.cpp < prev    next >
C/C++ Source or Header  |  1996-08-12  |  8KB  |  299 lines

  1. // GTakPM
  2. // (c) Benjamin Stein 1994
  3. // All Rights Reserved
  4. // DrivesCnr.cpp
  5.  
  6. // $Header: W:/Projects/OCL/Samples/GTakPM/Source/rcs/DrivesCnr.cpp 1.50 1996/08/11 23:48:06 B.STEIN Release $
  7.  
  8. #include "..\Source\GTakPM.hpp"
  9.  
  10.  
  11. DrivesCnr::DrivesCnr(pGTPMWin Parent)
  12.   : ParentWindow(Parent),
  13.     OContainer<DirInfo, RECORDCORE>(GTPM_DRIVECNR, Parent,
  14.                CCS_AUTOPOSITION | CCS_READONLY,
  15.                CA_CONTAINERTITLE | CA_TITLESEPARATOR | CV_TREE | CA_TREELINE),
  16.     populate(this, &DrivesCnr::collectDirectories, 65536),
  17.     select(this, &DrivesCnr::beginSelect, 65536),
  18.     hd_ico(HD_ICO, NULLHANDLE),
  19.     hdS_ico(HD_INC_ICO, NULLHANDLE),
  20.     cd_ico(CD_ICO, NULLHANDLE),
  21.     cdS_ico(CD_INC_ICO, NULLHANDLE),
  22.     folder_ico(FOLDER_ICO, NULLHANDLE),
  23.     folderS_ico(FOLDER_INC_ICO, NULLHANDLE),
  24.     openfolder_ico(FOPEN_ICO, NULLHANDLE),
  25.     openfolderS_ico(FOPEN_INC_ICO, NULLHANDLE),
  26.     dontKnow_ico(DONTKNOW_ICO, NULLHANDLE)
  27.   {}
  28.  
  29.  
  30. DrivesCnr::~DrivesCnr()
  31. {
  32.  DriveList.init();
  33. }
  34.  
  35.  
  36. BOOL DrivesCnr::setupDetails()
  37. {
  38.  return(TRUE);
  39. }
  40.  
  41.  
  42. BOOL DrivesCnr::setupItems()
  43. {
  44.  cnri.cxTreeIndent = -1;
  45.  cnri.cxTreeLine   = -1;
  46.  cnri.slBitmapOrIcon.cx = 32;
  47.  cnri.slBitmapOrIcon.cy = 32;
  48.  setCnrInfo(CMA_LINESPACING | CMA_CXTREEINDENT);
  49.  
  50.  populate.run();
  51.  
  52.  return(TRUE);
  53. }
  54.  
  55.  
  56. void DrivesCnr::collectDirectories()
  57. {
  58.  OSysInfo     info;
  59.  CHAR         drive[40];
  60.  pOString     tmp;
  61.  pDirInfo     pdi, pdiFirst;
  62.  RECORDINSERT ri;
  63.  
  64.  
  65.  if (!info.getAllDrives())
  66.    return;
  67.  
  68.  DosError(FERR_DISABLEHARDERR);
  69.  
  70.  ParentWindow->statline->setText("Scanning...");
  71.  ParentWindow->statline->setRDOnly(TRUE);
  72.  
  73.  freeItems();
  74.  DriveList.reset();
  75.  DirList.reset();
  76.  
  77.  tmp = info.SysInfoList.getFirst();
  78.  while (tmp) {
  79.    strcpy(drive, tmp->getText());
  80.    drive[2] = '\0';
  81.    DriveList << drive;
  82.    tmp = info.SysInfoList.getNext(); }
  83.  
  84.  pdi = allocRecords(DriveList.numberOfElements());
  85.  pdiFirst = pdi;
  86.  
  87.  tmp = DriveList.getFirst();
  88.  
  89.  while (tmp) {
  90.     ULONG       cbBuffer=sizeof(FSQBUFFER2) + (3 * CCHMAXPATH);
  91.     PBYTE       pszFSDName;
  92.     PVOID       fsqBuffer = (PVOID) new CHAR[cbBuffer];
  93.     PFSQBUFFER2 pfsqBuffer = (PFSQBUFFER2) fsqBuffer;
  94.     OString     FSystem;
  95.     pDirItem    item;
  96.  
  97.     if (!DosQueryFSAttach(tmp->getText(), 0, FSAIL_QUERYNAME, pfsqBuffer, &cbBuffer)) {
  98.       pszFSDName = ((PBYTE)pfsqBuffer->szName) + pfsqBuffer->cbName + 1;
  99.       FSystem << (PSZ) pszFSDName; }
  100.     delete pfsqBuffer;
  101.  
  102.     item = new DirItem;
  103.     item->fullName << tmp->getText();
  104.     item->fullName + "\\";
  105.     item->name << tmp->getText();
  106.     pdi->rc.pszIcon  = item->name;
  107.     pdi->rc.pszText  = item->name;
  108.     pdi->rc.pszName  = item->name;
  109.     pdi->rc.pszTree  = item->name;
  110.     pdi->dirItem     = item;
  111.     pdi->selected    = FALSE;
  112.     pdi->expanded    = FALSE;
  113.     pdi->wasExpanded = FALSE;
  114.  
  115.     if (!FSystem)
  116.       pdi->rc.hptrIcon = dontKnow_ico.hptr;
  117.     else if (!strcmpi(FSystem, "CDFS"))
  118.       pdi->rc.hptrIcon = cd_ico.hptr;
  119.     else
  120.       pdi->rc.hptrIcon = hd_ico.hptr;
  121.  
  122.     pdi = (pDirInfo) pdi->rc.preccNextRecord;
  123.     tmp = DriveList.getNext();
  124.  }
  125.  
  126.  memset(&ri, 0, sizeof(RECORDINSERT));
  127.  ri.cb                = sizeof(RECORDINSERT);
  128.  ri.pRecordOrder      = (PRECORDCORE) CMA_END;
  129.  ri.pRecordParent     = (PRECORDCORE) NULL;
  130.  ri.zOrder            = (USHORT) CMA_TOP;
  131.  ri.cRecordsInsert    = DriveList.numberOfElements();
  132.  ri.fInvalidateRecord = TRUE;
  133.  insertRecord(pdiFirst, &ri);
  134.  
  135.  recurse(NULL, FALSE);
  136.  
  137.  ParentWindow->statline->setRDOnly(FALSE);
  138.  ParentWindow->statline->setText("Ready.");
  139.  
  140.  DosError(FERR_ENABLEHARDERR);
  141. }
  142.  
  143.  
  144. void DrivesCnr::getDirectories(pDirInfo Parent, PSZ Directory, 
  145.                                BOOL recursive, BOOL selected)
  146. {
  147.  PFILEFINDBUF3 pffb = NULL;
  148.  OString       FileSpec(Directory);
  149.  ULONG         bufSize = sizeof(FILEFINDBUF3) * 100;
  150.  HDIR          hdir = HDIR_CREATE;
  151.  ULONG         ulMaxFiles = 100;
  152.  APIRET        rc;
  153.  
  154.  DosAllocMem((PPVOID)&pffb, bufSize, PAG_READ | PAG_WRITE | PAG_COMMIT);
  155.  
  156.  if (!pffb)
  157.    return;
  158.  
  159.  FileSpec + "\\*";
  160.  
  161.  rc = DosFindFirst(FileSpec, &hdir, 
  162.                    MUST_HAVE_DIRECTORY | FILE_HIDDEN | FILE_SYSTEM,
  163.                    pffb, bufSize, &ulMaxFiles, FIL_STANDARD);
  164.  
  165.  while((!rc) && (ulMaxFiles > 2))
  166.   {
  167.    insertDirs(Parent, FileSpec, pffb, ulMaxFiles, selected);
  168.    rc = DosFindNext(hdir, pffb, bufSize, &ulMaxFiles);
  169.    if (recursive)
  170.      recurse(Parent, FALSE, selected);
  171.   }
  172.  DosFindClose(hdir);
  173.  
  174.  DosFreeMem(pffb);
  175. }
  176.  
  177.  
  178. void DrivesCnr::insertDirs(pDirInfo Parent, PSZ Directory, 
  179.                            PFILEFINDBUF3 pffb, ULONG cFiles, BOOL selected)
  180. {
  181.  PBYTE          pbBuf = (PBYTE) pffb;
  182.  pDirInfo       pdiFirst, pdi;
  183.  ULONG          i;
  184.  PFILEFINDBUF3  pffbFile;
  185.  RECORDINSERT   ri;
  186.  ULONG          cFilesInserted = cFiles;
  187.  
  188.  if (!cFiles)
  189.    return;
  190.  
  191.  pdi = allocRecords(cFiles);
  192.  pdiFirst = pdi;
  193.  
  194.  for(i = 0; i < cFiles; i++)
  195.   {
  196.    pffbFile = (PFILEFINDBUF3) pbBuf;
  197.  
  198.    if (pffbFile->achName[0] != '.')
  199.     {
  200.      pDirItem item = new DirItem;
  201.  
  202.      item->fullName << Directory;
  203.      item->fullName.rightCut('\\');
  204.      item->fullName + "\\" + pffbFile->achName;
  205.      item->name     << pffbFile->achName;
  206.  
  207.      pdi->rc.pszIcon        = item->name;
  208.      pdi->rc.pszText        = item->name;
  209.      pdi->rc.pszName        = item->name;
  210.      pdi->rc.pszTree        = item->name;
  211.      pdi->dirItem           = item;
  212.      pdi->rc.hptrIcon       = selected ? folderS_ico.hptr : folder_ico.hptr;
  213.      pdi->selected          = selected;
  214.      pdi->expanded          = FALSE;
  215.      pdi->wasExpanded       = FALSE;
  216.  
  217.      DirList.add(item);
  218.      pdi = (pDirInfo) pdi->rc.preccNextRecord;   // Get the next container record
  219.     }
  220.    else
  221.      cFilesInserted--;
  222.  
  223.    pbBuf += pffbFile->oNextEntryOffset;          // Point to the next file in the buffer
  224.   }
  225.  
  226.  memset(&ri, 0, sizeof( RECORDINSERT));
  227.  ri.cb                 = sizeof(RECORDINSERT);
  228.  ri.pRecordOrder       = (PRECORDCORE) CMA_END;
  229.  ri.pRecordParent      = (PRECORDCORE) Parent;
  230.  ri.zOrder             = (USHORT) CMA_TOP;
  231.  ri.cRecordsInsert     = cFilesInserted;
  232.  ri.fInvalidateRecord  = TRUE;
  233.  
  234.  insertRecord(pdiFirst, &ri);
  235. }
  236.  
  237.  
  238.  
  239. // Recurse into Subdirs
  240.  
  241. void DrivesCnr::recurse(pDirInfo Parent, BOOL recursive, BOOL selected)
  242. {
  243.  ULONG     scanFlag = CMA_FIRSTCHILD;
  244.  pDirInfo  pdi = Parent;
  245.  
  246.  while(TRUE)
  247.   {
  248.    pdi = queryRecord(pdi, scanFlag, CMA_ITEMORDER);
  249.    if ((!pdi) || ((LONG) pdi == -1))
  250.      break;
  251.    if (!Parent)
  252.      getDirectories(pdi, pdi->dirItem->name, recursive, selected);
  253.    else
  254.      getDirectories(pdi, pdi->dirItem->fullName, recursive, selected);
  255.    scanFlag = CMA_NEXT;
  256.   }
  257. }
  258.  
  259.  
  260. void DrivesCnr::beginSelect()
  261. {
  262.  pDirInfo record = queryEmphasis();
  263.  
  264.  if (!record) 
  265.    return;
  266.  selectDirRecord(record);
  267.  invalidateRecord(record);
  268. }
  269.  
  270.  
  271. void DrivesCnr::selectDirRecord(pDirInfo Parent)
  272. {
  273.  ULONG     scanFlag = CMA_FIRSTCHILD;
  274.  pDirInfo  pdi = Parent;
  275.  
  276.  if (pdi->rc.pszTree[1] == ':') // its a drive, not a folder
  277.    pdi->rc.hptrIcon = pdi->selected ? hdS_ico.hptr : hd_ico.hptr;
  278.  else { // its a folder 
  279.    if (pdi->expanded) 
  280.      pdi->rc.hptrIcon = 
  281.      pdi->selected ? openfolderS_ico.hptr : openfolder_ico.hptr;
  282.    else
  283.      pdi->rc.hptrIcon = 
  284.      pdi->selected ? folderS_ico.hptr : folder_ico.hptr; }
  285.  
  286.  while(TRUE)
  287.   {
  288.    pdi = queryRecord(pdi, scanFlag, CMA_ITEMORDER);
  289.    if ((!pdi) || ((LONG) pdi == -1))
  290.      break;
  291.    pdi->selected = Parent->selected;
  292.    selectDirRecord(pdi);
  293.    scanFlag = CMA_NEXT;
  294.   }
  295. }
  296.  
  297.  
  298. // end of source
  299.