home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c083 / 11.ddi / OWLSRC.PAK / OPENSAVE.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-02  |  5.4 KB  |  205 lines

  1. //----------------------------------------------------------------------------
  2. // ObjectWindows - (C) Copyright 1992, 1993 by Borland International
  3. //   source\owl\opensave.cpp
  4. //   Implementation of OpenSave abstract, FileOpen, FileSave Common Dialog
  5. //   classes
  6. //----------------------------------------------------------------------------
  7. #pragma hdrignore SECTION
  8. #include <owl\owlpch.h>
  9. #include <owl\opensave.h>
  10. #include <dir.h>
  11.  
  12. #if !defined(SECTION) || SECTION == 1
  13.  
  14. TOpenSaveDialog::TData::TData(DWORD flags,
  15.                               char* filter,
  16.                               char* customFilter,
  17.                               char* initialDir,
  18.                               char* defExt)
  19.   : Flags(flags), Error(0), FileName(0), Filter(0),
  20.     CustomFilter(customFilter), FilterIndex(0),
  21.     InitialDir(initialDir), DefExt(defExt)
  22. {
  23.   FileName = new char[MAXPATH];
  24.   *FileName = 0;
  25.   SetFilter(filter);
  26. }
  27.  
  28. TOpenSaveDialog::TData::~TData()
  29. {
  30.   delete FileName;
  31.   delete Filter;
  32. }
  33.  
  34. //
  35. // Set the file list box filter strings. Translates '|'s into 0s so that the
  36. // string can be kept as a resource with imbeded '|'s like:
  37. // "Text Files(*.txt)|*.TXT|All Files(*.*)|*.*|"
  38. //
  39. void
  40. TOpenSaveDialog::TData::SetFilter(const char* filter)
  41. {
  42.   if (filter) {
  43.     delete Filter;
  44.     Filter = strcpy(new char[strlen(filter)+2], filter);
  45.     Filter[strlen(filter)+1] = 0;  // in case trailing '|' is missing
  46.   }
  47.   if (Filter)
  48.     for (char* p = Filter; *p; p++)
  49.       if (*p == '|')
  50.         *p = 0;
  51. }
  52.  
  53.  
  54. //----------------------------------------------------------------------------
  55.  
  56. DEFINE_RESPONSE_TABLE1(TOpenSaveDialog, TCommonDialog)
  57. END_RESPONSE_TABLE;
  58.  
  59. UINT TOpenSaveDialog::ShareViMsgId = 0;
  60.  
  61. void
  62. TOpenSaveDialog::Init(TResId templateId)
  63. {
  64.   if (!ShareViMsgId)
  65.     ShareViMsgId = ::RegisterWindowMessage(SHAREVISTRING);
  66.  
  67.   memset(&ofn, 0, sizeof(OPENFILENAME));
  68.   ofn.lStructSize = sizeof(OPENFILENAME);
  69.   ofn.hwndOwner = Parent ? Parent->HWindow : 0;
  70.   ofn.hInstance = *GetModule();
  71.   ofn.Flags = OFN_ENABLEHOOK | Data.Flags;
  72.   if (templateId) {
  73.     ofn.lpTemplateName = templateId;
  74.     ofn.Flags |= OFN_ENABLETEMPLATE;
  75.   } else
  76.     ofn.Flags &= ~OFN_ENABLETEMPLATE;
  77.   ofn.lpfnHook = 0;
  78.  
  79.   ofn.lpstrFilter = Data.Filter;
  80.   ofn.nFilterIndex = Data.FilterIndex;
  81.   ofn.lpstrFile = Data.FileName;
  82.   ofn.nMaxFile = MAXPATH;
  83.   ofn.lpstrInitialDir = Data.InitialDir;
  84.   ofn.lpstrDefExt = Data.DefExt;
  85. }
  86.  
  87. TOpenSaveDialog::TOpenSaveDialog(TWindow* parent, TData& data, TModule*   module)
  88.   : TCommonDialog(parent, 0, module),
  89.     Data(data)
  90. {
  91. }
  92.  
  93. TOpenSaveDialog::TOpenSaveDialog(TWindow*        parent,
  94.                                  TData&          data,
  95.                                  TResId          templateId,
  96.                                  const char far* title,
  97.                                  TModule*        module)
  98.   : TCommonDialog(parent, 0, module),
  99.     Data(data)
  100. {
  101.   Init(templateId);
  102.   (LPCSTR)ofn.lpstrTitle = title;
  103. }
  104.  
  105. BOOL
  106. TOpenSaveDialog::DialogFunction(UINT msg, WPARAM wParam, LPARAM lParam)
  107. {
  108.   if (TCommonDialog::DialogFunction(msg, wParam, lParam))
  109.     return TRUE;
  110.  
  111.   if (msg == TOpenSaveDialog::ShareViMsgId)
  112.     return (BOOL)ShareViolation();
  113.  
  114.   return FALSE;
  115. }
  116.  
  117. int
  118. TOpenSaveDialog::ShareViolation()
  119. {
  120.   return OFN_SHAREWARN;
  121. }
  122.  
  123. //----------------------------------------------------------------------------
  124.  
  125. TFileOpenDialog::TFileOpenDialog(TWindow*        parent,
  126.                                  TData&          data,
  127.                                  TResId          templateId,
  128.                                  const char far* title,
  129.                                  TModule*        module)
  130.   : TOpenSaveDialog(parent, data, templateId, title, module)
  131. {
  132. }
  133.  
  134. int
  135. TFileOpenDialog::DoExecute()
  136. {
  137.   (DLGPROC)ofn.lpfnHook = (DLGPROC)(FARPROC)StdDlgProcInstance;
  138.   int ret = ::GetOpenFileName(&ofn);
  139.   if (ret) {
  140.     Data.Flags = ofn.Flags;
  141.     Data.Error = 0;
  142.   } else {
  143.     Data.Error = ::CommDlgExtendedError();
  144.   }
  145.   return ret ? IDOK : IDCANCEL;
  146. }
  147.  
  148.  
  149. //----------------------------------------------------------------------------
  150.  
  151. TFileSaveDialog::TFileSaveDialog(TWindow*        parent,
  152.                                  TData&          data,
  153.                                  TResId          templateId,
  154.                                  const char far* title,
  155.                                  TModule*        module)
  156.   : TOpenSaveDialog(parent, data, templateId, title, module)
  157. {
  158. }
  159.  
  160. int
  161. TFileSaveDialog::DoExecute()
  162. {
  163.   (DLGPROC)ofn.lpfnHook = (DLGPROC)(FARPROC)StdDlgProcInstance;
  164.   int ret = ::GetSaveFileName(&ofn);
  165.   if (ret) {
  166.     Data.Flags = ofn.Flags;
  167.     Data.Error = 0;
  168.   } else {
  169.     Data.Error = ::CommDlgExtendedError();
  170.   }
  171.   return ret ? IDOK : IDCANCEL;
  172. }
  173.  
  174. #endif
  175. #if !defined(SECTION) || SECTION == 2
  176.  
  177. void
  178. TOpenSaveDialog::TData::Read(ipstream& is)
  179. {
  180.   is >> Flags;
  181.   delete FileName;
  182.   FileName = is.readString();
  183.   delete Filter;
  184.   Filter = is.readString();
  185.   CustomFilter = is.readString();
  186.   is >> FilterIndex;
  187.   InitialDir = is.readString();
  188.   DefExt = is.readString();
  189. }
  190.  
  191. void
  192. TOpenSaveDialog::TData::Write(opstream& os)
  193. {
  194.   os << Flags;
  195.   os.writeString(FileName);
  196.   os.writeString(Filter);
  197.   os.writeString(CustomFilter);
  198.   os << FilterIndex;
  199.   os.writeString(InitialDir);
  200.   os.writeString(DefExt);
  201. }
  202.  
  203. #endif
  204.  
  205.