home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / windows / basic / emed15 / samples / owl / padedit.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-01  |  6.7 KB  |  266 lines

  1.  
  2. #include <owl\owlpch.h>
  3. #include <dir.h>
  4. #pragma hdrstop
  5. #include "padedit.h"
  6. #include "confirm.h"
  7.  
  8.  
  9. DEFINE_RESPONSE_TABLE1(emeditEditor, TVbxEditor)
  10.   EV_COMMAND(CM_FILESAVE, CmFileSave),
  11.   EV_COMMAND(CM_FILESAVEAS, CmFileSaveAs),
  12.   EV_COMMAND_ENABLE(CM_FILESAVE, CmSaveEnable),
  13.   EV_WM_GETDLGCODE,
  14. END_RESPONSE_TABLE;
  15.  
  16.  
  17.  
  18. emeditEditor::emeditEditor(TWindow* parent, int resourceId,
  19.           const char far* fileName,
  20.           const DWORD fileFlags,
  21.           TModule* module) :
  22. TVbxEditor(parent, resourceId, module) {
  23.   FileName = fileName ? strnewdup(fileName) : 0;
  24.   FileFlags = fileFlags;
  25. }
  26.  
  27.  
  28.  
  29. emeditEditor::~emeditEditor() { delete (void*)FileName; }
  30.  
  31.  
  32.  
  33. UINT emeditEditor::EvGetDlgCode (MSG far* msg) {
  34.   return TVbxEditor::EvGetDlgCode(msg) | DLGC_WANTALLKEYS;
  35. }
  36.  
  37.  
  38.  
  39. //
  40. // performs setup for a TEMEditFile
  41. //
  42. void emeditEditor::SetupWindow() {
  43.   TVbxEditor::SetupWindow();
  44.   FileData.Flags = OFN_FILEMUSTEXIST|OFN_OVERWRITEPROMPT|FileFlags;
  45.   FileData.SetFilter(string(*GetModule(), IDS_FILEFILTER).c_str());
  46.  
  47.   SetFileName(FileName);
  48.   if (FileName && !Read()) {
  49.     string msgTemplate(*GetModule(), IDS_UNABLEREAD);
  50.     char*  msg = new char[MAXPATH + msgTemplate.length()];
  51.     wsprintf(msg, msgTemplate.c_str(), FileName);
  52.     MessageBox(msg, GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK);
  53.     delete msg;
  54.     SetFileName(0);
  55.   }
  56.  
  57.   if ((FileData.Flags & OFN_READONLY) != 0)
  58.     SetPropReadOnly( TRUE );
  59. }
  60.  
  61.  
  62.  
  63.  
  64. //
  65. // sets the file name of the window and updates the caption
  66. // replacing an empty name with 'Untitled' in its caption
  67. //
  68. void emeditEditor::SetFileName(const char far* fileName) {
  69.   if (fileName != FileName) {
  70.     delete (void*)FileName;
  71.     FileName = fileName ? strnewdup(fileName) : 0;
  72.   }
  73. }
  74.  
  75.  
  76. //
  77. // replaces the current file with the given file
  78. //
  79. void emeditEditor::ReplaceWith(const char far* fileName) {
  80.   if (Read(fileName)) {
  81.     Invalidate();
  82.     SetFileName(fileName);
  83.   } else {
  84.     string msgTemplate(*GetModule(), IDS_UNABLEREAD);
  85.     char*  msg = new char[MAXPATH + msgTemplate.length()];
  86.     wsprintf(msg, msgTemplate.c_str(), fileName);
  87.     MessageBox(msg, GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK);
  88.     delete msg;
  89.   }
  90. }
  91.  
  92. //
  93. // brings up a dialog allowing the user to open a file into this
  94. // window
  95. //
  96. // same as selecting File|Open from the menus
  97. //
  98. void emeditEditor::Open() {
  99.   if (CanClear()) {
  100.     *FileData.FileName = 0;
  101.     if ((new TFileOpenDialog(this, FileData))->Execute() == IDOK)
  102.       ReplaceWith(FileData.FileName);
  103.   }
  104. }
  105.  
  106. //
  107. // reads the contents of a  specified file, or the previously-specified file
  108. // if no name passed, into the TEMEdit control
  109. // The caller is responsible for any error UI
  110. //
  111. BOOL emeditEditor::Read(const char far* fileName) {
  112.   if (!fileName)
  113.     if (FileName) {
  114.       fileName = FileName;
  115.     } else
  116.       return FALSE;
  117.  
  118.   string S= fileName;
  119.   return SetPropFileOpen( S );
  120. }
  121.  
  122. //
  123. // saves the contents of the TEMEdit child control into the file currently
  124. // being editted
  125. //
  126. // returns true if the file was saved or IsModified returns FALSE
  127. //(contents already saved)
  128. //
  129. BOOL emeditEditor::Save() {
  130.   if (IsDirty()) {
  131.     if (!FileName)
  132.       return SaveAs();
  133.  
  134.     if (!Write()) {
  135.       string msgTemplate(*GetModule(), IDS_UNABLEWRITE);
  136.       char*  msg = new char[MAXPATH + msgTemplate.length()];
  137.       wsprintf(msg, msgTemplate.c_str(), FileName);
  138.       MessageBox(msg, GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK);
  139.       delete msg;
  140.       return FALSE;
  141.     }
  142.   }
  143.   return TRUE;  // editor's contents haven't been changed
  144. }
  145.  
  146. //
  147. // saves the contents of the TEMEdit child control into a file whose name
  148. // is retrieved from the user, through execution of a "Save" file dialog
  149. //
  150. // returns true if the file was saved
  151. //
  152. BOOL emeditEditor::SaveAs() {
  153.   if (FileName) {
  154.     strcpy(FileData.FileName, FileName);
  155.   } else
  156.     *FileData.FileName = 0;
  157.  
  158.   if ((new TFileSaveDialog(this, FileData))->Execute() == IDOK) {
  159.     if (Write(FileData.FileName)) {
  160.       SetFileName(FileData.FileName);
  161.       return TRUE;
  162.     }
  163.     string msgTemplate(*GetModule(), IDS_UNABLEWRITE);
  164.     char*  msg = new char[MAXPATH + msgTemplate.length()];
  165.     wsprintf(msg, msgTemplate.c_str(), FileName);
  166.     MessageBox(msg, GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK);
  167.     delete msg;
  168.   }
  169.   return FALSE;
  170. }
  171.  
  172. //
  173. // Enables save command only if text is modified
  174. //
  175. void emeditEditor::CmSaveEnable(TCommandEnabler& commandHandler) {
  176.   commandHandler.Enable(IsDirty());
  177. }
  178.  
  179. //
  180. // writes the contents of the TEMEdit child control to a specified file, or
  181. // the previously-specified file if none passed.
  182. // The caller is responsible for any error UI
  183. //
  184. BOOL emeditEditor::Write(const char far* fileName) {
  185.   if (!fileName)
  186.     if (FileName) {
  187.       fileName = FileName;
  188.     } else
  189.       return FALSE;
  190.  
  191.   return SetProp( "FileSave", fileName );
  192. }
  193.  
  194. //
  195. // returns a BOOL value indicating whether or not it is Ok to clear
  196. // the TEMEdit's text
  197. //
  198. // returns TRUE if the text has not been changed, or if the user Oks the
  199. // clearing of the text
  200. //
  201. BOOL emeditEditor::CanClear() {
  202.   if (IsDirty()) {
  203.     string msgTemplate(*GetModule(), IDS_FILECHANGED);
  204.     string untitled(*GetModule(), IDS_UNTITLED);
  205.     char*  msg = new char[MAXPATH+msgTemplate.length()];
  206.  
  207.     wsprintf(msg, msgTemplate.c_str(),
  208.              FileName ? (const char far*)FileName : untitled.c_str());
  209.  
  210.     int result = MessageBox(msg, GetApplication()->GetName(), MB_YESNOCANCEL|MB_ICONQUESTION);
  211.     delete msg;
  212.     return result==IDYES ? Save() : result != IDCANCEL;
  213.   }
  214.   return TRUE;
  215. }
  216.  
  217.  
  218.  
  219. BOOL emeditEditor::CanClose() { return CanClear(); }
  220.  
  221.  
  222. //
  223. // returns the text of line number "lineNumber" (0-terminated)
  224. // returns FALSE if an error occurs or if the text doesn't fit in "TextString"
  225. //
  226. BOOL emeditEditor::GetLine(string &strLineText, long lineNumber) {
  227.   BOOL success= TRUE;
  228.   const long L= (lineNumber < 1) ? CaretY() : lineNumber;
  229.  
  230.   if( !SetPropTextIndex( L ) )
  231.      if (!GetPropText( strLineText ) )
  232.         success= FALSE;
  233.  
  234.   return success;
  235. }
  236.  
  237.  
  238.  
  239. //
  240. // returns the text of line number "lineNumber" (0-terminated)
  241. // returns FALSE if an error occurs or if the text doesn't fit in "TextString"
  242. //
  243. BOOL emeditEditor::GetLine(char far* textString, UINT strSize, long lineNumber) {
  244.   BOOL success = FALSE;
  245.  
  246.   if (0 < strSize) {
  247.      if( (success = SetPropTextIndex( lineNumber )) != 0 ) {
  248.         string S;
  249.         if( (success = GetPropText( S )) != 0 ) {
  250.           const UINT L= S.length();
  251.           if( strSize > L ) {
  252.              S.copy( textString, L );
  253.              textString[L]= '\0';
  254.           }
  255.         }
  256.      }
  257.   }
  258.   return success;
  259. }
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.