home *** CD-ROM | disk | FTP | other *** search
-
- #include <owl\owlpch.h>
- #include <dir.h>
- #pragma hdrstop
- #include "padedit.h"
- #include "confirm.h"
-
-
- DEFINE_RESPONSE_TABLE1(emeditEditor, TVbxEditor)
- EV_COMMAND(CM_FILESAVE, CmFileSave),
- EV_COMMAND(CM_FILESAVEAS, CmFileSaveAs),
- EV_COMMAND_ENABLE(CM_FILESAVE, CmSaveEnable),
- EV_WM_GETDLGCODE,
- END_RESPONSE_TABLE;
-
-
-
- emeditEditor::emeditEditor(TWindow* parent, int resourceId,
- const char far* fileName,
- const DWORD fileFlags,
- TModule* module) :
- TVbxEditor(parent, resourceId, module) {
- FileName = fileName ? strnewdup(fileName) : 0;
- FileFlags = fileFlags;
- }
-
-
-
- emeditEditor::~emeditEditor() { delete (void*)FileName; }
-
-
-
- UINT emeditEditor::EvGetDlgCode (MSG far* msg) {
- return TVbxEditor::EvGetDlgCode(msg) | DLGC_WANTALLKEYS;
- }
-
-
-
- //
- // performs setup for a TEMEditFile
- //
- void emeditEditor::SetupWindow() {
- TVbxEditor::SetupWindow();
- FileData.Flags = OFN_FILEMUSTEXIST|OFN_OVERWRITEPROMPT|FileFlags;
- FileData.SetFilter(string(*GetModule(), IDS_FILEFILTER).c_str());
-
- SetFileName(FileName);
- if (FileName && !Read()) {
- string msgTemplate(*GetModule(), IDS_UNABLEREAD);
- char* msg = new char[MAXPATH + msgTemplate.length()];
- wsprintf(msg, msgTemplate.c_str(), FileName);
- MessageBox(msg, GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK);
- delete msg;
- SetFileName(0);
- }
-
- if ((FileData.Flags & OFN_READONLY) != 0)
- SetPropReadOnly( TRUE );
- }
-
-
-
-
- //
- // sets the file name of the window and updates the caption
- // replacing an empty name with 'Untitled' in its caption
- //
- void emeditEditor::SetFileName(const char far* fileName) {
- if (fileName != FileName) {
- delete (void*)FileName;
- FileName = fileName ? strnewdup(fileName) : 0;
- }
- }
-
-
- //
- // replaces the current file with the given file
- //
- void emeditEditor::ReplaceWith(const char far* fileName) {
- if (Read(fileName)) {
- Invalidate();
- SetFileName(fileName);
- } else {
- string msgTemplate(*GetModule(), IDS_UNABLEREAD);
- char* msg = new char[MAXPATH + msgTemplate.length()];
- wsprintf(msg, msgTemplate.c_str(), fileName);
- MessageBox(msg, GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK);
- delete msg;
- }
- }
-
- //
- // brings up a dialog allowing the user to open a file into this
- // window
- //
- // same as selecting File|Open from the menus
- //
- void emeditEditor::Open() {
- if (CanClear()) {
- *FileData.FileName = 0;
- if ((new TFileOpenDialog(this, FileData))->Execute() == IDOK)
- ReplaceWith(FileData.FileName);
- }
- }
-
- //
- // reads the contents of a specified file, or the previously-specified file
- // if no name passed, into the TEMEdit control
- // The caller is responsible for any error UI
- //
- BOOL emeditEditor::Read(const char far* fileName) {
- if (!fileName)
- if (FileName) {
- fileName = FileName;
- } else
- return FALSE;
-
- string S= fileName;
- return SetPropFileOpen( S );
- }
-
- //
- // saves the contents of the TEMEdit child control into the file currently
- // being editted
- //
- // returns true if the file was saved or IsModified returns FALSE
- //(contents already saved)
- //
- BOOL emeditEditor::Save() {
- if (IsDirty()) {
- if (!FileName)
- return SaveAs();
-
- if (!Write()) {
- string msgTemplate(*GetModule(), IDS_UNABLEWRITE);
- char* msg = new char[MAXPATH + msgTemplate.length()];
- wsprintf(msg, msgTemplate.c_str(), FileName);
- MessageBox(msg, GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK);
- delete msg;
- return FALSE;
- }
- }
- return TRUE; // editor's contents haven't been changed
- }
-
- //
- // saves the contents of the TEMEdit child control into a file whose name
- // is retrieved from the user, through execution of a "Save" file dialog
- //
- // returns true if the file was saved
- //
- BOOL emeditEditor::SaveAs() {
- if (FileName) {
- strcpy(FileData.FileName, FileName);
- } else
- *FileData.FileName = 0;
-
- if ((new TFileSaveDialog(this, FileData))->Execute() == IDOK) {
- if (Write(FileData.FileName)) {
- SetFileName(FileData.FileName);
- return TRUE;
- }
- string msgTemplate(*GetModule(), IDS_UNABLEWRITE);
- char* msg = new char[MAXPATH + msgTemplate.length()];
- wsprintf(msg, msgTemplate.c_str(), FileName);
- MessageBox(msg, GetApplication()->GetName(), MB_ICONEXCLAMATION | MB_OK);
- delete msg;
- }
- return FALSE;
- }
-
- //
- // Enables save command only if text is modified
- //
- void emeditEditor::CmSaveEnable(TCommandEnabler& commandHandler) {
- commandHandler.Enable(IsDirty());
- }
-
- //
- // writes the contents of the TEMEdit child control to a specified file, or
- // the previously-specified file if none passed.
- // The caller is responsible for any error UI
- //
- BOOL emeditEditor::Write(const char far* fileName) {
- if (!fileName)
- if (FileName) {
- fileName = FileName;
- } else
- return FALSE;
-
- return SetProp( "FileSave", fileName );
- }
-
- //
- // returns a BOOL value indicating whether or not it is Ok to clear
- // the TEMEdit's text
- //
- // returns TRUE if the text has not been changed, or if the user Oks the
- // clearing of the text
- //
- BOOL emeditEditor::CanClear() {
- if (IsDirty()) {
- string msgTemplate(*GetModule(), IDS_FILECHANGED);
- string untitled(*GetModule(), IDS_UNTITLED);
- char* msg = new char[MAXPATH+msgTemplate.length()];
-
- wsprintf(msg, msgTemplate.c_str(),
- FileName ? (const char far*)FileName : untitled.c_str());
-
- int result = MessageBox(msg, GetApplication()->GetName(), MB_YESNOCANCEL|MB_ICONQUESTION);
- delete msg;
- return result==IDYES ? Save() : result != IDCANCEL;
- }
- return TRUE;
- }
-
-
-
- BOOL emeditEditor::CanClose() { return CanClear(); }
-
-
- //
- // returns the text of line number "lineNumber" (0-terminated)
- // returns FALSE if an error occurs or if the text doesn't fit in "TextString"
- //
- BOOL emeditEditor::GetLine(string &strLineText, long lineNumber) {
- BOOL success= TRUE;
- const long L= (lineNumber < 1) ? CaretY() : lineNumber;
-
- if( !SetPropTextIndex( L ) )
- if (!GetPropText( strLineText ) )
- success= FALSE;
-
- return success;
- }
-
-
-
- //
- // returns the text of line number "lineNumber" (0-terminated)
- // returns FALSE if an error occurs or if the text doesn't fit in "TextString"
- //
- BOOL emeditEditor::GetLine(char far* textString, UINT strSize, long lineNumber) {
- BOOL success = FALSE;
-
- if (0 < strSize) {
- if( (success = SetPropTextIndex( lineNumber )) != 0 ) {
- string S;
- if( (success = GetPropText( S )) != 0 ) {
- const UINT L= S.length();
- if( strSize > L ) {
- S.copy( textString, L );
- textString[L]= '\0';
- }
- }
- }
- }
- return success;
- }
-
-
-
-
-
-
-