home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / bc45 / owlsrc.pak / OPENSAVE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-24  |  6.7 KB  |  261 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows
  3. // (C) Copyright 1992, 1994 by Borland International, All Rights Reserved
  4. //
  5. //   Implementation of OpenSave abstract, FileOpen, FileSave Common Dialog
  6. //   classes
  7. //----------------------------------------------------------------------------
  8. #pragma hdrignore SECTION
  9. #include <owl/owlpch.h>
  10. #include <owl/opensave.h>
  11. #include <dir.h>
  12.  
  13. #if !defined(SECTION) || SECTION == 1
  14.  
  15. TOpenSaveDialog::TData::TData(uint32      flags,
  16.                               const char* filter,
  17.                               char*       customFilter,
  18.                               char*       initialDir,
  19.                               char*       defExt,
  20.                               int         maxPath)
  21. :
  22.   Flags(flags), Error(0), FileName(0), Filter(0),
  23.   CustomFilter(customFilter), FilterIndex(0),
  24.   InitialDir(initialDir), DefExt(defExt),
  25.   MaxPath(maxPath ? maxPath : MAXPATH)
  26. {
  27.   FileName = new char[MaxPath];
  28.   *FileName = 0;
  29.   SetFilter(filter);
  30. }
  31.  
  32. TOpenSaveDialog::TData::TData(const TData& src)
  33. :
  34.   Flags(src.Flags), Error(0), FileName(0), Filter(0),
  35.   CustomFilter(src.CustomFilter), FilterIndex(src.FilterIndex),
  36.   InitialDir(src.InitialDir), DefExt(src.DefExt),
  37.   MaxPath(src.MaxPath)
  38. {
  39.   FileName = strnewdup(src.FileName, MaxPath);
  40.   SetFilter(src.Filter);
  41. }
  42.  
  43. TOpenSaveDialog::TData::~TData()
  44. {
  45.   delete [] FileName;
  46.   delete [] Filter;
  47. }
  48.  
  49. TOpenSaveDialog::TData&
  50. TOpenSaveDialog::TData::operator =(const TData& src)
  51. {
  52.   Flags = src.Flags;
  53.   Error = 0;
  54.   CustomFilter = src.CustomFilter;
  55.   FilterIndex = src.FilterIndex;
  56.   InitialDir = src.InitialDir;
  57.   DefExt = src.DefExt;
  58.   MaxPath = src.MaxPath;
  59.  
  60.   delete [] FileName;
  61.   FileName = strnewdup(src.FileName, MaxPath);
  62.  
  63.   SetFilter(src.Filter);
  64.  
  65.   return *this;
  66. }
  67.  
  68. //
  69. // Set the file list box filter strings. Translates '|'s into 0s so that the
  70. // string can be kept as a resource with imbeded '|'s like:
  71. // "Text Files(*.txt)|*.TXT|All Files(*.*)|*.*|"
  72. // Can also handle already processed filter strings.
  73. //
  74. void
  75. TOpenSaveDialog::TData::SetFilter(const char* filter)
  76. {
  77.   // Copy filter string
  78.   //
  79.   if (filter) {
  80.     delete [] Filter;
  81.     if (strchr(filter, '|')) {
  82.       uint len = strlen(filter) + 2; // one for each terminating 0
  83.       Filter = strcpy(new char[len], filter);
  84.       Filter[len-1] = 0;             // in case trailing '|' is missing
  85.     }
  86.     else {
  87.       const char* p = filter;
  88.       while (*p)
  89.         p += strlen(p) + 1;             // scan for 00 at end
  90.       uint len = uint(p - filter) + 1;  // one more for last 0
  91.       Filter = new char[len];
  92.       memcpy(Filter, filter, len);
  93.     }
  94.   }
  95.   // Stomp |s with 0s
  96.   //
  97.   if (Filter)
  98.     for (char* p = Filter; *p; p++)
  99.       if (*p == '|')
  100.         *p = 0;
  101. }
  102.  
  103.  
  104. //----------------------------------------------------------------------------
  105.  
  106. DEFINE_RESPONSE_TABLE1(TOpenSaveDialog, TCommonDialog)
  107. END_RESPONSE_TABLE;
  108.  
  109. uint TOpenSaveDialog::ShareViMsgId = 0;
  110.  
  111. void
  112. TOpenSaveDialog::Init(TResId templateId)
  113. {
  114.   if (!ShareViMsgId)
  115.     ShareViMsgId = ::RegisterWindowMessage(SHAREVISTRING);
  116.  
  117.   memset(&ofn, 0, sizeof(OPENFILENAME));
  118.   ofn.lStructSize = sizeof(OPENFILENAME);
  119.   ofn.hwndOwner = Parent ? Parent->HWindow : 0;
  120.   ofn.hInstance = *GetModule();
  121.   ofn.Flags = OFN_ENABLEHOOK | Data.Flags;
  122.   if (templateId) {
  123.     ofn.lpTemplateName = templateId;
  124.     ofn.Flags |= OFN_ENABLETEMPLATE;
  125.   }
  126.   else
  127.     ofn.Flags &= ~OFN_ENABLETEMPLATE;
  128.   ofn.lpfnHook = 0;
  129.  
  130.   ofn.lpstrFilter = Data.Filter;
  131.   ofn.nFilterIndex = Data.FilterIndex;
  132.   ofn.lpstrFile = Data.FileName;
  133.   ofn.nMaxFile = Data.MaxPath;
  134.   ofn.lpstrInitialDir = Data.InitialDir;
  135.   ofn.lpstrDefExt = Data.DefExt;
  136. }
  137.  
  138. TOpenSaveDialog::TOpenSaveDialog(TWindow* parent, TData& data, TModule*   module)
  139. :
  140.   TCommonDialog(parent, 0, module),
  141.   Data(data)
  142. {
  143. }
  144.  
  145. TOpenSaveDialog::TOpenSaveDialog(TWindow*        parent,
  146.                                  TData&          data,
  147.                                  TResId          templateId,
  148.                                  const char far* title,
  149.                                  TModule*        module)
  150. :
  151.   TCommonDialog(parent, 0, module),
  152.   Data(data)
  153. {
  154.   Init(templateId);
  155.   (LPCSTR)ofn.lpstrTitle = title;
  156. }
  157.  
  158. bool
  159. TOpenSaveDialog::DialogFunction(uint msg, WPARAM wParam, LPARAM lParam)
  160. {
  161.   if (TCommonDialog::DialogFunction(msg, wParam, lParam))
  162.     return true;
  163.  
  164.   if (msg == TOpenSaveDialog::ShareViMsgId)
  165.     return (bool)ShareViolation();
  166.  
  167.   return false;
  168. }
  169.  
  170. int
  171. TOpenSaveDialog::ShareViolation()
  172. {
  173.   return OFN_SHAREWARN;
  174. }
  175.  
  176. //----------------------------------------------------------------------------
  177.  
  178. TFileOpenDialog::TFileOpenDialog(TWindow*        parent,
  179.                                  TData&          data,
  180.                                  TResId          templateId,
  181.                                  const char far* title,
  182.                                  TModule*        module)
  183. :
  184.   TOpenSaveDialog(parent, data, templateId, title, module)
  185. {
  186. }
  187.  
  188. int
  189. TFileOpenDialog::DoExecute()
  190. {
  191.   (DLGPROC)ofn.lpfnHook = (DLGPROC)(FARPROC)StdDlgProcInstance;
  192.   int ret = ::GetOpenFileName(&ofn);
  193.   if (ret) {
  194.     Data.Flags = ofn.Flags;
  195.     Data.Error = 0;
  196.   }
  197.   else {
  198.     Data.Error = ::CommDlgExtendedError();
  199.   }
  200.   return ret ? IDOK : IDCANCEL;
  201. }
  202.  
  203.  
  204. //----------------------------------------------------------------------------
  205.  
  206. TFileSaveDialog::TFileSaveDialog(TWindow*        parent,
  207.                                  TData&          data,
  208.                                  TResId          templateId,
  209.                                  const char far* title,
  210.                                  TModule*        module)
  211. :
  212.   TOpenSaveDialog(parent, data, templateId, title, module)
  213. {
  214. }
  215.  
  216. int
  217. TFileSaveDialog::DoExecute()
  218. {
  219.   (DLGPROC)ofn.lpfnHook = (DLGPROC)(FARPROC)StdDlgProcInstance;
  220.   int ret = ::GetSaveFileName(&ofn);
  221.   if (ret) {
  222.     Data.Flags = ofn.Flags;
  223.     Data.Error = 0;
  224.   }
  225.   else {
  226.     Data.Error = ::CommDlgExtendedError();
  227.   }
  228.   return ret ? IDOK : IDCANCEL;
  229. }
  230.  
  231. #endif
  232. #if !defined(SECTION) || SECTION == 2
  233.  
  234. void
  235. TOpenSaveDialog::TData::Read(ipstream& is)
  236. {
  237.   is >> Flags;
  238.   delete FileName;
  239.   FileName = is.readString();
  240.   delete Filter;
  241.   Filter = is.readString();
  242.   CustomFilter = is.readString();
  243.   is >> FilterIndex;
  244.   InitialDir = is.readString();
  245.   DefExt = is.readString();
  246. }
  247.  
  248. void
  249. TOpenSaveDialog::TData::Write(opstream& os)
  250. {
  251.   os << Flags;
  252.   os.writeString(FileName);
  253.   os.writeString(Filter);
  254.   os.writeString(CustomFilter);
  255.   os << FilterIndex;
  256.   os.writeString(InitialDir);
  257.   os.writeString(DefExt);
  258. }
  259.  
  260. #endif
  261.