home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / df3os2.zip / FILEOPEN.CPP < prev    next >
C/C++ Source or Header  |  1993-09-23  |  4KB  |  151 lines

  1. // ---------- fileopen.cpp
  2.  
  3. #include <io.h>
  4. #include <dir.h>
  5. #include "fileopen.h"
  6. #include "notice.h"
  7.  
  8. // ----------------- File Open Dialog Box
  9. FileOpen::FileOpen(const char *spec, const char *ttl) :
  10.         Dialog(ttl, 19, 57),
  11.         // ----- file name editbox
  12.         filelabel ("~Filename:", 3, 2, 10, this),
  13.         filename  (13, 2, 1, 40, this),
  14.         // ----- drive:path display
  15.         dirlabel  (3, 4, 50, this),
  16.         // ----- files list box
  17.         fileslabel("F~iles:", 3, 6, 7, this),
  18.         files     (spec, 3, 7, this),
  19.         // ----- directories list box
  20.         dirslabel ("~Directories:", 19, 6, 13, this),
  21.         dirs      (19, 7, this),
  22.         // ----- drives list box
  23.         disklabel ("Dri~ves:", 34, 6, 8, this),
  24.         disks     (34, 7, this),
  25.         // ----- command buttons
  26.         ok        (46, 8, this),
  27.         cancel    (46,11, this),
  28.         help      (46,14, this),
  29.         // ------ file open data members
  30.         filespec(spec)
  31. {
  32.     filename.AddText(filespec);
  33. }
  34.  
  35. // --- Get selected filename: files listbox->filename editbox
  36. void FileOpen::SelectFileName()
  37. {
  38.     int sel = files.Selection();
  39.     if (sel != -1)    {
  40.         String fname = files.ExtractTextLine(sel);
  41.         filename.SetText(fname);
  42.         filename.Paint();
  43.     }
  44. }
  45.  
  46. // ---- called when user "selects" a control
  47. //      e.g. changes the selection on a listbox
  48. void FileOpen::ControlSelected(DFWindow *Wnd)
  49. {
  50.     if (Wnd == (DFWindow *) &files)
  51.         // --- user selected a filename from list
  52.         SelectFileName();
  53.     else if (Wnd == (DFWindow *) &dirs ||
  54.             Wnd == (DFWindow *) &disks)    {
  55.         // --- user is selecting a different drive or directory
  56.         filename.SetText(filespec);
  57.         filename.Paint();
  58.     }
  59. }
  60.  
  61. // ---- called when user "chooses" a control
  62. //      e.g. chooses the current selection on a listbox
  63. void FileOpen::ControlChosen(DFWindow *Wnd)
  64. {
  65.     if (Wnd == (DFWindow *) &files)
  66.         // --- user chose a filename from filename list
  67.         OKFunction();
  68.     else if (Wnd == (DFWindow *) &dirs)    {
  69.         // --- user chose a directory from directory list
  70.         int dr = dirs.Selection();
  71.         String dir = dirs.ExtractTextLine(dr);
  72.         int len = dir.Strlen();
  73.         String direc = dir.mid(len-2,1);
  74.         chdir(direc);
  75.         ShowLists();
  76.     }
  77.     else if (Wnd == (DFWindow *) &disks)    {
  78.         // --- user chose a drive from drive list
  79.         int dr = disks.Selection();
  80.         String drive = disks.ExtractTextLine(dr);
  81.         setdisk(drive[0] - 'A');
  82.         ShowLists();
  83.     }
  84. }
  85.  
  86. // ---- called when user chooses OK command
  87. void FileOpen::OKFunction()
  88. {
  89.     String fname = filename.ExtractTextLine(0);
  90.     if (access(fname, 0) == 0)    {
  91.         filespec = fname;
  92.         Dialog::OKFunction();
  93.     }
  94.     else if (fname.FindChar('*') != -1 ||
  95.             fname.FindChar('?') != -1)    {
  96.         filespec = fname;
  97.         ShowLists();
  98.     }
  99.     else
  100.         // ---- No file as specified
  101.         ErrorMessage("File does not exist");
  102. }
  103.  
  104. // ------ refresh the current directory display and 
  105. //        the directories and files list after
  106. //        user changes filespec, drive, or directory
  107. void FileOpen::ShowLists()
  108. {
  109.     dirlabel.FillLabel();
  110.     dirlabel.Show();
  111.     dirs.FillList();
  112.     dirs.Show();
  113.     files.FillList(filespec);
  114.     files.Show();
  115. }
  116.  
  117. // ------- called just before a control gets the focus
  118. void FileOpen::EnterFocus(DFWindow *Wnd)
  119. {
  120.     if (Wnd == (DFWindow *) &files)
  121.         // --- The file name list box is getting the focus
  122.         SelectFileName();
  123. }
  124.  
  125. // ---- called when user chooses OK command
  126. void SaveAs::OKFunction()
  127. {
  128.     String fname = filename.ExtractTextLine(0);
  129.     if (access(fname, 0) != 0)    {
  130.         // ---- chosen file does not exist
  131.         if (fname.FindChar('*') != -1 ||
  132.             fname.FindChar('?') != -1)    {
  133.             // --- wild cards
  134.             filespec = fname;
  135.             ShowLists();
  136.         }
  137.         else    {
  138.             filespec = fname;
  139.             Dialog::OKFunction();
  140.         }
  141.     }
  142.     else    {
  143.         // ---- file exists
  144.         String msg = fname + " already exists. Replace?";
  145.         if (YesNo(msg))    {
  146.             filespec = fname;
  147.             Dialog::OKFunction();
  148.         }
  149.     }
  150. }
  151.