home *** CD-ROM | disk | FTP | other *** search
/ Point Programming 1 / PPROG1.ISO / c / fli106c / examples / chgdir.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1992-01-25  |  9.3 KB  |  417 lines

  1. //
  2. // The Fusion Library Interface for DOS
  3. // Version 1.02
  4. // Copyright (C) 1990, 1991
  5. // Software Dimensions
  6. //
  7. // Dialog Development System
  8. //
  9.  
  10. #include "fliwin.h"
  11. #include "elements.h"
  12. #include "colors.h"
  13. #include "dds.h"
  14.  
  15. #include <dos.h>
  16. #include <time.h>
  17. #include <stddef.h>
  18. #include <string.h>
  19. #include <alloc.h>
  20.  
  21. #include "chgdir.h"
  22.  
  23. #pragma warn -inl
  24.  
  25. //-------------------------------------------------------------------------
  26. //
  27. // Powers up the PathStorage class
  28. //
  29. //-------------------------------------------------------------------------
  30.  
  31. PathStorage::PathStorage()
  32. {
  33.  
  34.   DirEntries=0;
  35.  
  36.   Drives=0;
  37.   Revert=0;
  38.   Path=0;
  39.  
  40.   CurrentDisk=0;
  41.   DriveCount=0;
  42.   SaveDisk=0;
  43.   Count=0;
  44.  
  45.   Path=new char[45];
  46.   Revert=new char[45];
  47.   DirEntries=new ffblk[MaxEntries];
  48.   getcwd(Revert,40);
  49.   Refresh();
  50.  
  51.   SaveDisk=getdisk();
  52.   for (int Disk=0;Disk<26;Disk++)
  53.   {
  54.     setdisk(Disk);
  55.     if (Disk==getdisk())
  56.     {
  57.       Drives=(char(*)[2])realloc(Drives,sizeof(Drives[0])*++DriveCount);
  58.       *(Drives[DriveCount-1]+0)='A'+Disk;
  59.       *(Drives[DriveCount-1]+1)=0;
  60.       if (Disk==SaveDisk)
  61.         CurrentDisk=DriveCount-1;
  62.     }
  63.   }
  64.  
  65.   setdisk(SaveDisk);
  66. }
  67.  
  68. //-------------------------------------------------------------------------
  69. //
  70. // Refresh the directories when our location switched
  71. //
  72. //-------------------------------------------------------------------------
  73.  
  74. void PathStorage::Refresh()
  75. {
  76.   getcwd(Path,40);
  77.   if (*(Path+strlen(Path)-1)!='\\')
  78.     strcat(Path,"\\");
  79.   strcat(Path,"*.*");
  80.  
  81.   ffblk DummyStorage;
  82.  
  83.   int Done=findfirst(Path,&DummyStorage,FA_DIREC);
  84.   Count=0;
  85.  
  86.   while (!Done)
  87.   {
  88.     if (DummyStorage.ff_attrib&FA_DIREC)
  89.       DirEntries[(++Count)-1]=DummyStorage;
  90.     if (Count==MaxEntries-1)
  91.     {
  92.       InfoBox &NotAvailable = *new InfoBox;
  93.       NotAvailable
  94.             + "Internal drive storage buffer is exhausted."
  95.             + ""
  96.             + "Directory listing may be missing some directories.";
  97.       NotAvailable.Title("Exhausted");
  98.       NotAvailable.UseInfoBox();
  99.       delete &NotAvailable;
  100.       break;
  101.     }
  102.     Done=findnext(&DummyStorage);
  103.   }
  104.  
  105.   *(strchr(Path,'*'))=0;
  106. }
  107.  
  108. //-------------------------------------------------------------------------
  109. //
  110. // Destructor that frees up the directory information
  111. //
  112. //-------------------------------------------------------------------------
  113.  
  114. PathStorage::~PathStorage()
  115. {
  116.   if (Path)
  117.   {
  118.     delete Path;
  119.     delete DirEntries;
  120.     delete Revert;
  121.     delete Drives;
  122.   }
  123.   Revert=0;
  124.   DirEntries=0;
  125.   Path=0;
  126.   DriveCount=0;
  127.   Drives=0;
  128. }
  129.  
  130. //-------------------------------------------------------------------------
  131. //
  132. // Construct the class that will contain the ChDir event handler
  133. // Destruct class upon exit
  134. //
  135. //-------------------------------------------------------------------------
  136.  
  137. ChDirDialog::ChDirDialog() : DialogClass(49,13,"Change Directory")
  138. {
  139.   Path=new PathStorage();
  140.  
  141.   Element(new ChDirPath(6,1,*Path));
  142.   Help("Enter the search path");
  143.   HotKey(1,1,"~Path",kbAltP);
  144.   FusionHelp(10103);
  145.  
  146.   Element(new ChDirList(1,4,*Path));
  147.   Help("Select the directory to traverse to");
  148.   HotKey(1,3,"~Directory",kbAltD);
  149.   Available(Path->Count);
  150.   FusionHelp(10104);
  151.  
  152.   Element(new DirList(17,4,*Path));
  153.   Help("Select the drive to jump to");
  154.   HotKey(17,3,"D~rive",kbAltR);
  155.   FusionHelp(10105);
  156.  
  157.   Element(new DiaPushButton(32,4,"   Okay   ",ChDirOkayButton,0,1));
  158.   Help("Accept the current drive and directory");
  159.  
  160.   Element(new DiaPushButton(32,6,"  ~Cancel  ",ChDirCancelButton,kbAltC));
  161.   Help("Cancel and do not change the drive and directory");
  162.  
  163.   Element(new DiaPushButton(32,8,"R~evert ...",ChDirRevertButton,kbAltE));
  164.   Help("Revert to the original drive and directory");
  165.  
  166.   Element(new Prompt(*Path));
  167. }
  168.  
  169. ChDirDialog::~ChDirDialog()
  170. {
  171.   delete Path;
  172. }
  173.  
  174. //-------------------------------------------------------------------------
  175. //
  176. // Event handler for the ChDir dialog
  177. //
  178. //-------------------------------------------------------------------------
  179.  
  180. int ChDirDialog::EventHandler(int Event)
  181. {
  182.   switch(Event)
  183.   {
  184.     case kbEsc:
  185.     case kbCr:
  186.     case CloseEvent:
  187.     case OutsideEvent:
  188.     case ChDirOkayButton:
  189.       return StopEvent;
  190.  
  191.     case ChDirRevertButton:
  192.       setdisk(Path->SaveDisk);
  193.       for (int i=0;i<Path->DriveCount;i++)
  194.         if (*Path->Drives[i]=='A'+Path->SaveDisk)
  195.           Path->CurrentDisk=i;
  196.       strcpy(Path->Path,Path->Revert);
  197.       chdir(Path->Path);
  198.       Path->Refresh();
  199.       return RefreshEvent;
  200.  
  201.     case ChDirCancelButton:
  202.       setdisk(Path->SaveDisk);
  203.       for (i=0;i<Path->DriveCount;i++)
  204.         if (*Path->Drives[i]=='A'+Path->SaveDisk)
  205.           Path->CurrentDisk=i;
  206.       strcpy(Path->Path,Path->Revert);
  207.       chdir(Path->Path);
  208.       return StopEvent;
  209.   }
  210.  
  211.   return CompleteEvent;
  212. }
  213.  
  214. //-------------------------------------------------------------------------
  215. //
  216. // ChDirPath element that permits typing of a path
  217. //
  218. //-------------------------------------------------------------------------
  219.  
  220. ChDirPath::ChDirPath(int X,int Y,PathStorage &_Path) :
  221.   Path(_Path),
  222.   DiaChar(X,Y,0,_Path.Path,0,'X',40,1)
  223. {
  224.   ComparePathStorage=new char[50];
  225.   strcpy(ComparePathStorage,Path.Path);
  226. }
  227.  
  228. ChDirPath::~ChDirPath()
  229. {
  230.   delete ComparePathStorage;
  231. }
  232.  
  233. int ChDirPath::EventHandler(int Event)
  234. {
  235.   if (Event==kbCr)
  236.   {
  237.     if (strcmp(Path.Path,ComparePathStorage))
  238.     {
  239.       char Drive[MAXDRIVE];
  240.       char Dir[MAXDIR];
  241.       char File[MAXFILE];
  242.       char Ext[MAXEXT];
  243.  
  244.       int Flags=fnsplit(Path.Path,Drive,Dir,File,Ext);
  245.  
  246.       if (Flags&FILENAME || Flags&DIRECTORY)
  247.       {
  248.         if (chdir(Path.Path))
  249.         {
  250.           if (strlen(Path.Path)!=2)
  251.           {
  252.  
  253. DirError:
  254.  
  255.             InfoBox &NotAvailable = *new InfoBox;
  256.             NotAvailable
  257.                   + "Sorry, either you entered in an incorrect"
  258.                   + "directory or the directory could not be"
  259.                   + "found.  Please try again.";
  260.             NotAvailable.Title("Not Found");
  261.             NotAvailable.UseInfoBox();
  262.             delete &NotAvailable;
  263.             HighLight();
  264.             return CompleteEvent;
  265.           }
  266.  
  267.           if (strlen(Path.Path)==2 && Path.Path[1]!=':')
  268.             goto DirError;
  269.         }
  270.       }
  271.  
  272.       if (Flags&DRIVE)
  273.       {
  274.         setdisk(Drive[0]-'A');
  275.  
  276.         for (int i=0;i<Path.DriveCount;i++)
  277.         {
  278.           if (*(Path.Drives[i]+0)==Drive[0])
  279.           {
  280.             Path.CurrentDisk=i;
  281.             break;
  282.           }
  283.         }
  284.       }
  285.  
  286.       Path.Refresh();
  287.       CurrentLocation=0;
  288.       strcpy(ComparePathStorage,Path.Path);
  289.       return RefreshEvent;
  290.     }
  291.     return CompleteEvent;
  292.   }
  293.   else if (Event==' ')
  294.     return CompleteEvent;
  295.   else
  296.     return DiaChar::EventHandler(Event);
  297. }
  298.  
  299. //-------------------------------------------------------------------------
  300. //
  301. // Element that lists the available directories
  302. //
  303. //-------------------------------------------------------------------------
  304.  
  305. int ChDirList::CurrentItem=0;
  306.  
  307. ChDirList::ChDirList(int X,int Y,PathStorage &_Path) :
  308.   Path(_Path),
  309.   DiaStructPickList(X,Y,12,5,CurrentItem,_Path.Count,sizeof(ffblk),
  310.   &_Path.DirEntries->ff_name)
  311. {
  312. }
  313.  
  314. int ChDirList::EventHandler(int Event)
  315. {
  316.   if (Event==kbCr || Event==' ')
  317.   {
  318.     chdir((Path.DirEntries+CurrentItem)->ff_name);
  319.     Item=0;
  320.     ItemAtTopOfList=0;
  321.     Path.Refresh();
  322.     ItemCount=Path.Count;
  323.     return RefreshEvent;
  324.   }
  325.   else
  326.   {
  327.     int EventStore=DiaStructPickList::EventHandler(Event);
  328.     if (EventStore==StopEvent)
  329.     {
  330.       chdir((Path.DirEntries+CurrentItem)->ff_name);
  331.       Item=0;
  332.       ItemAtTopOfList=0;
  333.       Path.Refresh();
  334.       ItemCount=Path.Count;
  335.       return RefreshEvent;
  336.     }
  337.     return EventStore;
  338.   }
  339. }
  340.  
  341. void ChDirList::Show()
  342. {
  343.   if (!Path.Count)
  344.   {
  345.     Item=0;
  346.     ItemAtTopOfList=0;
  347.     ItemCount=0;
  348.   }
  349.   DiaStructPickList::Show();
  350. }
  351.  
  352. //-------------------------------------------------------------------------
  353. //
  354. // DirList dialog that controls the display of the drives
  355. //
  356. //-------------------------------------------------------------------------
  357.  
  358. DirList::DirList(int X,int Y,PathStorage &_Path) :
  359.   Path(_Path),
  360.   DiaStructPickList(X,Y,2,5,_Path.CurrentDisk,_Path.DriveCount,2,_Path.Drives)
  361. {
  362. }
  363.  
  364. int DirList::EventHandler(int Event)
  365. {
  366.   if (Event==kbCr || Event==' ')
  367.   {
  368.     setdisk(*Path.Drives[Path.CurrentDisk]-'A');
  369.     Path.Refresh();
  370.     return RefreshEvent;
  371.   }
  372.   else
  373.   {
  374.     int EventStore=DiaStructPickList::EventHandler(Event);
  375.     if (EventStore==StopEvent)
  376.     {
  377.       setdisk(*Path.Drives[Path.CurrentDisk]-'A');
  378.       Path.Refresh();
  379.       return RefreshEvent;
  380.     }
  381.     return EventStore;
  382.   }
  383. }
  384.  
  385. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  386. //
  387. // Prompt class
  388. //
  389. // Indicates current directory and file.
  390. //
  391. //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
  392.  
  393. Prompt::Prompt(PathStorage &_Path) :
  394.   Path(_Path)
  395. {
  396. }
  397.  
  398. void Prompt::Show()
  399. {
  400.   Blaze->EraseArea(0,Blaze->WhatWinHeight()-1,Blaze->WhatWinWidth(),1,bCyan);
  401.   (*Blaze) (1,Blaze->WhatWinHeight()-1) << bCyan << Path.Path;
  402. }
  403.  
  404. void Prompt::HighLight()
  405. {
  406. }
  407.  
  408. int Prompt::Available()
  409. {
  410.   return FailedEvent;
  411. }
  412.  
  413. int Prompt::EventHandler(int Event)
  414. {
  415.   return Event;
  416. }
  417.