home *** CD-ROM | disk | FTP | other *** search
- /* Project emedit
- Early Morning Software
- Copyright ⌐ 1994. All Rights Reserved.
-
- SUBSYSTEM: emedit.exe Application
- FILE: mdtmdicl.cpp
- AUTHOR: Ted Stockwell
-
-
- OVERVIEW
- ========
- Source file for implementation of emeditMDIClient (TMDIClient).
- */
-
- #include <time.h>
- #include <dir.h>
- #include <owl\owlpch.h>
-
- #define CommonDialog_DATA
- #include <cmdialog.h>
- #pragma hdrstop
-
- #include "emedtapp.h"
- #include "mdtmdicl.h"
- #include "mdtmdich.h"
- #include "apxprint.h"
- #include "apxprev.h"
- #include "mdtchldc.h"
-
- #include "padedit.h"
-
-
-
-
- static string MostRecentFileNames[ 4 ];
- static int MRUFileItemCount= 0; // # of items on MRU file list.
-
- //{{emeditMDIClient Implementation}}
-
-
- //
- // Build a response table for all messages/commands handled
- // by emeditMDIClient derived from TMDIClient.
- //
- DEFINE_RESPONSE_TABLE1(emeditMDIClient, TMDIClient)
- //{{emeditMDIClientRSP_TBL_BEGIN}}
- EV_COMMAND(CM_MDIFILENEW, CmFileNew),
- EV_COMMAND(CM_MDIFILEOPEN, CmFileOpen),
- EV_COMMAND(CM_FILEPRINT, CmFilePrint),
- EV_COMMAND(CM_FILEPRINTERSETUP, CmFilePrintSetup),
- EV_COMMAND(CM_FILEPRINTPREVIEW, CmFilePrintPreview),
- EV_COMMAND_ENABLE(CM_FILEPRINT, CmPrintEnable),
- EV_COMMAND_ENABLE(CM_FILEPRINTERSETUP, CmPrintEnable),
- EV_COMMAND_ENABLE(CM_FILEPRINTPREVIEW, CmPrintEnable),
- EV_WM_DROPFILES,
- EV_COMMAND(CM_FONT, CmOptionsFont),
- EV_COMMAND_ENABLE(CM_FONT, CmOptionsFontEnable),
- EV_COMMAND_ENABLE(CM_EDITSELECT_ALL, CmEditSelectAllEnable),
- EV_COMMAND_ENABLE(CM_EDITTIMEDATE, CmEditTimeDateEnable),
- EV_COMMAND(CM_EDITTIMEDATE, CmEditTimeDate),
- EV_COMMAND_ENABLE(CM_FILECLOSE, CmFileCloseEnable),
- EV_COMMAND(CM_FILECLOSE, CmFileClose),
- EV_COMMAND_ENABLE(CM_STREAMBLOCK, CmStreamBlockEnable),
- EV_COMMAND_ENABLE(CM_FILELAST1, CmFileLast1Enable),
- EV_COMMAND_ENABLE(CM_FILELAST2, CmFileLast2Enable),
- EV_COMMAND_ENABLE(CM_FILELAST3, CmFileLast3Enable),
- EV_COMMAND_ENABLE(CM_FILELAST4, CmFileLast4Enable),
- EV_COMMAND(CM_FILELAST1, CmFileLast1),
- EV_COMMAND(CM_FILELAST2, CmFileLast2),
- EV_COMMAND(CM_FILELAST3, CmFileLast3),
- EV_COMMAND(CM_FILELAST4, CmFileLast4),
- EV_COMMAND_ENABLE(CM_LINEBLOCK, CmLineBlockEnable),
- EV_COMMAND_ENABLE(CM_COLUMNBLOCK, CmColumnBlockEnable),
- EV_COMMAND(CM_COLUMNBLOCK, CmColumnBlock),
- EV_COMMAND(CM_LINEBLOCK, CmLineBlock),
- EV_COMMAND(CM_STREAMBLOCK, CmStreamBlock),
- EV_COMMAND_ENABLE(CM_EDITCLEAR, CMEditClearEnable),
- //{{emeditMDIClientRSP_TBL_END}}
- END_RESPONSE_TABLE;
-
-
- //////////////////////////////////////////////////////////
- // emeditMDIClient
- // ===========
- // Construction/Destruction handling.
- emeditMDIClient::emeditMDIClient () :
- TMDIClient ()
- {
- ChildCount = 0;
- // INSERT>> Your constructor code here.
- }
-
-
- emeditMDIClient::~emeditMDIClient () {
- Destroy();
- // INSERT>> Your destructor code here.
- }
-
-
- //////////////////////////////////////////////////////////
- // emeditMDIClient
- // ===========
- // MDIClient site initialization.
- void emeditMDIClient::SetupWindow () {
- // Default SetUpWindow processing.
- TMDIClient::SetupWindow ();
-
- // Common file file flags and filters for Open/Save As dialogs. Filename and directory are
- // computed in the member functions CmFileOpen, and CmFileSaveAs.
- FileData.Flags = OFN_FILEMUSTEXIST | OFN_OVERWRITEPROMPT;
- FileData.SetFilter("All Files (*.*)|*.*|");
-
- // Accept files via drag/drop in the client window.
- DragAcceptFiles(TRUE);
-
- GetRecentFiles(); // Read EMEDIT.INI and set recent file menu items appropriately
- }
-
-
-
- void emeditMDIClient::SetDefaultBlockMarks( ENUM BlkType ) {
- ((emeditApp*)GetApplication())->SetDefaultBlockType( BlkType );
-
- // loop through all the children and set the default block type
- emeditMDIChild *child = TYPESAFE_DOWNCAST( GetFirstChild(), emeditMDIChild );
- for (unsigned I= NumChildren(); I--;) {
- emeditChildClient* const client = TYPESAFE_DOWNCAST(child->GetClientWindow(), emeditChildClient);
-
- if (client->editor->SetPropSelDefaultType( BlkType ) == 0 && BlkType == TVbxEditor::SelMark_3_Column) {
- MessageBox("Can't do column blocks with proportional fonts.", GetApplication()->GetName(), MB_OK);
- break;
- }
-
- child = TYPESAFE_DOWNCAST( child->Next(), emeditMDIChild );
- }
- }
-
-
-
- void emeditMDIClient::GetRecentFiles() {
- // Get recent file strings from EMEDIT.INI
- char* key[]= { "RecentFile1", "RecentFile2", "RecentFile3", "RecentFile4" };
- for (int I = 4; 1 <= I; I--) {
-
- // This variable must be large enough to hold the return string
- // from the GetPrivateProfileString API.
- char IniString[ 256 ];
-
- const int RetVal = GetPrivateProfileString("Recent Files", key[ I-1 ], "Not Used", IniString, sizeof(IniString) - 1, "EMEDIT.INI");
- if (RetVal && strstr(IniString, "Not Used") == 0)
- AppendFileMenuItem( IniString );
- }
- }
-
-
-
- void emeditMDIClient::WriteRecentFiles() {
- char* key[]= { "RecentFile1", "RecentFile2", "RecentFile3", "RecentFile4" };
-
- for (int I= MRUFileItemCount; I--;)
- WritePrivateProfileString("Recent Files", key[ I ], MostRecentFileNames[I].c_str(), "EMEDIT.INI");
- }
-
-
-
-
- void emeditMDIClient::AppendFileMenuItem(const char* fileitem) {
- int bad= 0;
- TMenu MainMenu( GetApplication()->GetMainWindow()->GetMenu() );
- TMenu FileMenu( MainMenu.GetSubMenu(0) );
-
- // 1st, get to see if the file is already on the list
- int onlist= FALSE;
- for (int I= MRUFileItemCount; I--;)
- if (MostRecentFileNames[ I ] == fileitem) {
- onlist= I + 1;
- break;
- }
-
- // If the given file name is on the list but not in the top position then we
- // must move it to the top position.
- if (1 < onlist) {
- string S= MostRecentFileNames[ onlist-1 ];
-
- // Move the given file name to the top of the list
- for (int I= onlist-1; I--; )
- MostRecentFileNames[ I + 1 ]= MostRecentFileNames[ I ];
- MostRecentFileNames[0]= S;
-
- // Now, set the text on all da menu items...
- for (int J= MRUFileItemCount; 1 <= J; J--) {
- char N[7];
- string X= "&";
- X+= itoa( J, N, 10 );
- X+= " ";
- X+= MostRecentFileNames[ J-1 ];
- FileMenu.ModifyMenu( CM_FILELAST1 + J - 1, MF_ENABLED | MF_STRING | MF_UNCHECKED, CM_FILELAST1 + J - 1, X.c_str() );
- }
- }
-
- // If the given file name is not on the list then we must add it (and
- // possibly drop the least recently used file name).
- if (!onlist) {
-
- // if there are no file names appended yet then
- // add a separator to the end of the file menu
- if (MRUFileItemCount == 0)
- if (FileMenu.AppendMenu( MF_SEPARATOR, 0, 0 )==0)
- bad= TRUE;
-
- // if there are less than four items on the list then we must add one
- if (!bad)
- if (MRUFileItemCount < 4)
- if (FileMenu.AppendMenu( MF_ENABLED | MF_STRING | MF_UNCHECKED, CM_FILELAST1 + MRUFileItemCount )==0)
- bad= TRUE;
-
- // Must now rearrange the text on the menu items
- if (!bad) {
- if (MRUFileItemCount < 4)
- MRUFileItemCount++;
-
- for (int I= MRUFileItemCount-1; I--; )
- MostRecentFileNames[ I + 1 ]= MostRecentFileNames[ I ];
- MostRecentFileNames[0]= fileitem;
-
- // Now, set the text on all da menu items...
- for (int J= MRUFileItemCount; 1 <= J; J--) {
- char N[7];
- string X= "&";
- X+= itoa( J, N, 10 );
- X+= " ";
- X+= MostRecentFileNames[ J-1 ];
- FileMenu.ModifyMenu( CM_FILELAST1 + J - 1, MF_ENABLED | MF_STRING | MF_UNCHECKED, CM_FILELAST1 + J - 1, X.c_str() );
- }
- }
- }
- }
-
-
-
- //
- // Open the modeless Replace commdlg
- //
-
- //////////////////////////////////////////////////////////
- // emeditMDIClient
- // ===========
- // Menu File New command
- void emeditMDIClient::CmFileNew () {
- char title[255];
-
- // Generate a title for the MDI child window.
- wsprintf(title, "%d", ChildCount++);
-
- emeditMDIChild* child = new emeditMDIChild( *this, title, 0 );
-
- // Associate ICON w/ this child window.
- child->SetIcon(GetApplication(), IDI_DOC);
-
- // If the current active MDI child is maximize then this one should be also.
- emeditMDIChild *curChild = (emeditMDIChild *)GetActiveMDIChild();
- if (curChild && (curChild->GetWindowLong(GWL_STYLE) & WS_MAXIMIZE))
- child->Attr.Style |= WS_MAXIMIZE;
-
- child->Create();
- }
-
-
- void emeditMDIClient::OpenFile (const char *fileName) {
- if (fileName)
- lstrcpy(FileData.FileName, fileName);
-
- // create the child window. set the file name and file attributes that the
- // editor contol should use when it sets itself up.
- emeditMDIChild* child = new emeditMDIChild( *this, "", 0 );
- emeditChildClient *cclient = TYPESAFE_DOWNCAST( child->GetClientWindow(), emeditChildClient );
- cclient->editor->SetFileName( FileData.FileName );
- cclient->editor->FileFlags= FileData.Flags;
-
- // Associate ICON w/ this child window.
- child->SetIcon(GetApplication(), IDI_DOC);
-
- // If the current active MDI child is maximized then this one should be also.
- emeditMDIChild *curChild = (emeditMDIChild *)GetActiveMDIChild();
- if (curChild && (curChild->GetWindowLong(GWL_STYLE) & WS_MAXIMIZE))
- child->Attr.Style |= WS_MAXIMIZE;
-
- child->Create();
-
- AppendFileMenuItem( FileData.FileName );
- }
-
-
- //////////////////////////////////////////////////////////
- // emeditMDIClient
- // ===========
- // Menu File Open command
- void emeditMDIClient::CmFileOpen ()
- {
- //
- // Display standard Open dialog box to select a file name.
- //
- *FileData.FileName = 0;
- if (TFileOpenDialog(this, FileData).Execute() == IDOK)
- OpenFile();
- }
-
-
- //////////////////////////////////////////////////////////
- // emeditMDIClient
- // ==========
- // Menu File Print command
- void emeditMDIClient::CmFilePrint ()
- {
- //
- // Create Printer object if not already created.
- //
- emeditApp *theApp = TYPESAFE_DOWNCAST(GetApplication(), emeditApp);
- if (theApp) {
- if (!theApp->Printer)
- theApp->Printer = new TPrinter;
-
- //
- // Create Printout window and set characteristics.
- //
- APXPrintOut printout(theApp->Printer, Title, GetActiveMDIChild(), TRUE);
-
- theApp->Printing = TRUE;
-
- //
- // Bring up the Print dialog and print the document.
- //
- theApp->Printer->Print(GetActiveMDIChild()->GetClientWindow(), printout, TRUE);
-
- theApp->Printing = FALSE;
- }
- }
-
-
- //////////////////////////////////////////////////////////
- // emeditMDIClient
- // ==========
- // Menu File Print Setup command
- void emeditMDIClient::CmFilePrintSetup ()
- {
- emeditApp *theApp = TYPESAFE_DOWNCAST(GetApplication(), emeditApp);
- if (theApp) {
- if (!theApp->Printer)
- theApp->Printer = new TPrinter;
-
- //
- // Bring up the Print Setup dialog.
- //
- theApp->Printer->Setup(this);
- }
- }
-
-
- //////////////////////////////////////////////////////////
- // emeditMDIClient
- // ==========
- // Menu File Print Preview command
- void emeditMDIClient::CmFilePrintPreview ()
- {
- emeditApp *theApp = TYPESAFE_DOWNCAST(GetApplication(), emeditApp);
- if (theApp) {
- if (!theApp->Printer)
- theApp->Printer = new TPrinter;
-
- theApp->Printing = TRUE;
-
- PreviewWindow *prevW = new PreviewWindow(Parent, theApp->Printer, GetActiveMDIChild(), "Print Preview", new TLayoutWindow(0));
- prevW->Create();
-
- GetApplication()->BeginModal(GetApplication()->MainWindow);
-
- // We must destroy the preview window explicitly. Otherwise, the window will not be destroyed until
- // it's parent the MainWindow is destroyed.
- prevW->Destroy();
- delete prevW;
-
- theApp->Printing = FALSE;
- }
- }
-
-
- //////////////////////////////////////////////////////////
- // emeditMDIClient
- // ==========
- // Menu enabler used by Print, Print Setup and Print Preview.
- void emeditMDIClient::CmPrintEnable (TCommandEnabler &tce) {
- if (GetActiveMDIChild()) {
- emeditApp *theApp = TYPESAFE_DOWNCAST(GetApplication(), emeditApp);
- if (theApp) {
- // If we have a Printer already created just test if all is okay.
- // Otherwise create a Printer object and make sure the printer
- // really exists and then delete the Printer object.
- if (!theApp->Printer) {
- theApp->Printer = new TPrinter;
-
- tce.Enable(theApp->Printer->GetSetup().Error == 0);
- } else
- tce.Enable(theApp->Printer->GetSetup().Error == 0);
- }
- } else
- tce.Enable(FALSE);
- }
-
-
-
- void emeditMDIClient::EvDropFiles (TDropInfo) {
- Parent->ForwardMessage();
- }
-
-
-
- void emeditMDIClient::CmOptionsFont ()
- {
- // Maybe I'm an idiot for not understanding how to use OWL to display
- // the common font dialog, however, CMDIALOG.VBX is incredibly easy
- // to use (assuming that you have the VB docs that explain how to use it)
- // and so I use that instead of OWL here. I think this is an example of
- // why VBX's are so popular.
-
- // instantiate a common dialog control
- string strFontName;
- float fFontSize;
- BOOL bFontBold, bFontItalic, bFontUnderLine, bFontStrikeThru;
- COLORREF clrFontColor;
- TVbxCommonDialog CMFontDialog(this, IDC_CMDIALOG1, 0, 0,0,0,0, sizeof( CommonDialogData ), CommonDialogData, ::Module );
- CMFontDialog.Create();
-
- // Get the font attributes of the active window :
- // WARNING, KLUDGE BELOW : So much for my flame above, while VBX's might in
- // theory be productive the environment that supports them can't be lame.
- // There seems to be a bug in BIVBX.DLL's support of the standard FontSize
- // property, instead of returning the font size in points it seems to be
- // returning the font size in twips, so I take the value returned from
- // GetPropFontSize and divide by 20. What's doubly bad about a bug like
- // this (besides robbing me of my productivity) is that if Borland fixes the
- // bug then the code below will not work for anyone with the new BIVBX DLL.
- emeditMDIChild* child = TYPESAFE_DOWNCAST( GetActiveMDIChild(), emeditMDIChild );
- emeditChildClient *cclient = TYPESAFE_DOWNCAST( child->GetClientWindow(), emeditChildClient );
- emeditEditor* Text1 = cclient->editor;
- Text1->GetPropFontName( strFontName );
- Text1->GetPropFontSize( fFontSize ); fFontSize/= 20;
- Text1->GetPropFontBold( bFontBold );
- Text1->GetPropFontItalic( bFontItalic );
- Text1->GetPropFontUnderline( bFontUnderLine );
- Text1->GetPropFontStrikethru( bFontStrikeThru );
- Text1->GetPropForeColor( clrFontColor );
-
- // set cancel to true
- CMFontDialog.SetPropCancelError( TRUE );
-
- // Set the font attributes in the common dialog box to the font attributes
- // in the current window:
- CMFontDialog.SetPropFlags( 0x3 | 0x100 ); // CF_BOTH and CF_EFFECTS
- CMFontDialog.SetPropFontName( strFontName );
- CMFontDialog.SetPropFontSize( fFontSize );
- CMFontDialog.SetPropFontBold( bFontBold );
- CMFontDialog.SetPropFontItalic( bFontItalic );
- CMFontDialog.SetPropFontUnderLine( bFontUnderLine );
- CMFontDialog.SetPropFontStrikeThru( bFontStrikeThru );
- CMFontDialog.SetPropColor( clrFontColor );
-
- // display the dialog box, if the user presses cancel then an error is
- // generated and FALSE is returned.
- BOOL OK= CMFontDialog.SetPropAction( 4 );
-
- // set the properties according to the user's selections...
- if( OK ) {
-
- // Get the font attrbutes the user chose:
- int CMFontDiallogFonstSize;
- CMFontDialog.GetPropFontName( strFontName );
- CMFontDialog.GetPropFontSize( CMFontDiallogFonstSize );
- CMFontDialog.GetPropFontBold( bFontBold );
- CMFontDialog.GetPropFontItalic( bFontItalic );
- CMFontDialog.GetPropFontUnderLine( bFontUnderLine );
- CMFontDialog.GetPropFontStrikeThru( bFontStrikeThru );
- CMFontDialog.GetPropColor( clrFontColor );
-
- // Set the active window's font attributes to the user's choices:
- Text1->SetPropVisible( FALSE );
- Text1->SetPropFontName( strFontName );
- Text1->SetPropFontSize( CMFontDiallogFonstSize * 20 );
- Text1->SetPropFontBold( bFontBold );
- Text1->SetPropFontItalic( bFontItalic );
- Text1->SetPropFontUnderline( bFontUnderLine );
- Text1->SetPropFontStrikethru( bFontStrikeThru );
- Text1->SetPropForeColor( clrFontColor );
- Text1->SetPropVisible( TRUE );
- }
- }
-
-
- void emeditMDIClient::CmOptionsFontEnable (TCommandEnabler &tce) {
- tce.Enable(GetActiveMDIChild() ? TRUE : FALSE);
- }
-
-
- void emeditMDIClient::CmEditSelectAllEnable (TCommandEnabler &tce) {
- tce.Enable(GetActiveMDIChild() ? TRUE : FALSE);
- }
-
-
- void emeditMDIClient::CmEditTimeDateEnable (TCommandEnabler &tce) {
- tce.Enable(GetActiveMDIChild() ? TRUE : FALSE);
- }
-
-
-
- void emeditMDIClient::CmEditTimeDate () {
- time_t t = time(NULL);
- string now( asctime( localtime( &t ) ) );
- emeditMDIChild* child = TYPESAFE_DOWNCAST( GetActiveMDIChild(), emeditMDIChild );
- emeditChildClient *cclient = TYPESAFE_DOWNCAST( child->GetClientWindow(), emeditChildClient );
- cclient->editor->SetPropSelText( now.remove( now.length() - 1 ) );
- }
-
-
-
- void emeditMDIClient::CmFileCloseEnable (TCommandEnabler &tce) {
- tce.Enable(GetActiveMDIChild() ? TRUE : FALSE);
- }
-
-
- void emeditMDIClient::CmFileClose () { GetActiveMDIChild()->CloseWindow(); }
-
-
- void emeditMDIClient::CmStreamBlockEnable (TCommandEnabler &tce)
- {
- tce.Enable(GetActiveMDIChild() ? TRUE : FALSE);
- }
-
- void emeditMDIClient::CmFileLast1Enable (TCommandEnabler &tce) { tce.Enable( TRUE ); }
-
- void emeditMDIClient::CmFileLast2Enable (TCommandEnabler &tce) { tce.Enable( TRUE ); }
-
- void emeditMDIClient::CmFileLast3Enable (TCommandEnabler &tce) { tce.Enable( TRUE ); }
-
- void emeditMDIClient::CmFileLast4Enable (TCommandEnabler &tce) { tce.Enable( TRUE ); }
-
-
- void emeditMDIClient::CmFileLast1 () { OpenFile( MostRecentFileNames[ 0 ].c_str() ); }
-
-
- void emeditMDIClient::CmFileLast2 () { OpenFile( MostRecentFileNames[ 1 ].c_str() ); }
-
- void emeditMDIClient::CmFileLast3 () { OpenFile( MostRecentFileNames[ 2 ].c_str() ); }
-
- void emeditMDIClient::CmFileLast4 () { OpenFile( MostRecentFileNames[ 3 ].c_str() ); }
-
-
- void emeditMDIClient::CmLineBlockEnable (TCommandEnabler &tce)
- {
- tce.Enable(GetActiveMDIChild() ? TRUE : FALSE);
- }
-
-
- void emeditMDIClient::CmColumnBlockEnable (TCommandEnabler &tce)
- {
- tce.Enable(GetActiveMDIChild() ? TRUE : FALSE);
- }
-
-
- void emeditMDIClient::CmColumnBlock () { SetDefaultBlockMarks( TVbxEditor::SelMark_3_Column ); }
-
-
- void emeditMDIClient::CmLineBlock () { SetDefaultBlockMarks( TVbxEditor::SelMark_2_Line ); }
-
-
- void emeditMDIClient::CmStreamBlock () { SetDefaultBlockMarks( TVbxEditor::SelMark_1_Stream ); }
-
-
- void emeditMDIClient::CMEditClearEnable (TCommandEnabler &tce)
- {
- tce.Enable(GetActiveMDIChild() ? TRUE : FALSE);
- }
-
-
- void emeditMDIClient::CleanupWindow ()
- {
- TMDIClient::CleanupWindow();
-
- // INSERT>> Your code here.
- WriteRecentFiles();
- }
-
-
-
- void emeditMDIClient::SetInsertMode( BOOL mode ) {
- // loop through all the children and set the insert mode
- emeditMDIChild *child = TYPESAFE_DOWNCAST( GetFirstChild(), emeditMDIChild );
- for (unsigned I= NumChildren(); I--;) {
- emeditChildClient* const client = TYPESAFE_DOWNCAST(child->GetClientWindow(), emeditChildClient);
-
- client->editor->SetPropInsertMode( mode ); // set insert mode
-
- // change caret width based on insert mode
- int caretwidth;
- client->editor->GetPropCaretWidth( caretwidth );
- client->editor->SetPropCaretWidth( mode ? (caretwidth >> 1) : (caretwidth << 1) );
-
- child = TYPESAFE_DOWNCAST( child->Next(), emeditMDIChild );
- }
- }
-
-