home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-12-16 | 10.8 KB | 446 lines | [TEXT/CWIE] |
- // =================================================================================
- // CActiveXDocument.cpp ©1996 Microsoft Corporation. All rights reserved.
- // portions ©1995 Metrowerks Inc. All rights reserved.
- // =================================================================================
-
- #include <LFile.h>
- #include <LPrintout.h>
- #include <LPlaceHolder.h>
- #include <LString.h>
- #include <LWindow.h>
- #include <PP_Messages.h>
- #include <UMemoryMgr.h>
- #include <UWindows.h>
-
- #include "ActiveXAppConstants.h"
- #include "CActiveXView.h"
- #include "CActiveXDocument.h"
- #include "CActiveXApp.h"
-
- CActiveXDocument *CActiveXDocument::sDefaultContainer = NULL;
-
-
- // ---------------------------------------------------------------------------------
- // • CActiveXDocument::CContainer::Release
- // ---------------------------------------------------------------------------------
-
- STDMETHODIMP_(ULONG)
- CActiveXDocument::Release(void)
- {
- if (--mRefCount < 1)
- DebugStr("\pToo many releases of CActiveXDocument::CContainer");
-
- return mRefCount;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • CActiveXDocument(LCommander*, FSSpec*)
- // ---------------------------------------------------------------------------------
-
- CActiveXDocument::CActiveXDocument(
- LCommander *inSuper,
- FSSpec *inFileSpec )
- : LSingleDoc( inSuper )
- {
- // set up the default container so active x sites constructed can connect to it
- sDefaultContainer = this;
-
- // Create window for our document.
- mWindow = LWindow::CreateWindow( rPPob_ActiveXWindow, this );
- Assert_( mWindow != nil );
- mHostPort = ((GrafPort *)(mWindow->GetMacPort()));
- mOwner = CActiveXApp::GetDefaultApplication();
-
- // Make activeX view target when window activated.
- mActiveXView[0] = (CActiveXView *) mWindow->FindPaneByID( kActiveXView );
- if (kActiveXViewCount > 1)
- mActiveXView[1] = (CActiveXView *) mWindow->FindPaneByID( kActiveXView2 );
- if (kActiveXViewCount > 2)
- mActiveXView[2] = (CActiveXView *) mWindow->FindPaneByID( kActiveXView3 );
- if (kActiveXViewCount > 3)
- mActiveXView[3] = (CActiveXView *) mWindow->FindPaneByID( kActiveXView4 );
- mWindow->SetLatentSub( mActiveXView[0] );
-
- mFocusView = NULL;
- mModalFocusView = NULL;
-
- // Set name of window or open file.
- if ( inFileSpec == nil ) {
-
- NameNewDoc();
-
- } else {
-
- OpenFile( *inFileSpec );
- }
-
- // Make the window visible.
- mWindow->Show();
- }
-
-
- // ---------------------------------------------------------------------------------
- // • CActiveXDocument(LCommander*, FSSpec*)
- // ---------------------------------------------------------------------------------
-
- CActiveXDocument::~CActiveXDocument(void)
- {
- Int16 i;
-
- if (mFocusView)
- {
- mFocusView->SetFocus(ReleaseCommand, KeyboardFocus);
- mFocusView->Release();
- mFocusView = NULL;
- }
-
- if (mModalFocusView)
- {
- mModalFocusView->SetFocus(ReleaseCommand, ModalFocus);
- mModalFocusView->Release();
- mModalFocusView = NULL;
- }
-
- i = kActiveXViewCount;
- while (i-- > 0)
- if (mActiveXView[i])
- delete mActiveXView[i];
-
- if (mOwner)
- mOwner->RemoveContainer( this );
-
- if (mRefCount != 1)
- DebugStr("\pToo few releases of CActiveXDocument::CContainer");
- }
-
-
- // ---------------------------------------------------------------------------------
- // • NameNewDoc
- // ---------------------------------------------------------------------------------
-
- void
- CActiveXDocument::NameNewDoc()
- {
- // Setup the window title. Start with the default title.
- LStr255 theTitle( "\pUntitled" );
-
- // Find the first available title. We could also check the window
- // pane id if we wanted to make sure we didn't collide with other
- // window types.
- Int32 theNumber = 0;
- while ( UWindows::FindNamedWindow( theTitle ) != nil ) {
-
- // An existing window has the current name
- // Increment counter and try again.
- ++theNumber;
- theTitle = "\pUntitled ";
- theTitle += (LStr255) theNumber;
-
- }
-
- // Finally, set window title.
- mWindow->SetDescriptor( theTitle );
- }
-
-
- // ---------------------------------------------------------------------------------
- // • OpenFile
- // ---------------------------------------------------------------------------------
-
- void
- CActiveXDocument::OpenFile(
- FSSpec &inFileSpec )
- {
- Try_ {
-
- // Create a new file object.
- mFile = new LFile( inFileSpec );
-
- // Open the data fork.
- mFile->OpenDataFork( fsRdWrPerm );
-
- // Read the entire file and close the file.
- // Handle theTextH = mFile->ReadDataFork();
- // mFile->CloseDataFork();
-
- // Put the contents in the text view
- // and clear the dirty flag.
- // mActiveXView->SetTextHandle( theTextH );
- // mActiveXView->SetDirty( false );
-
- // Dispose of the text.
- // ::DisposeHandle( theTextH );
-
- // Set window title to the name of the file.
- mWindow->SetDescriptor( inFileSpec.name );
-
- // Flag that document has an associated file.
- mIsSpecified = true;
-
- } Catch_( inErr ) {
-
- // Cleanup and rethrow the error.
- delete this;
- Throw_( inErr );
-
- } EndCatch_
- }
-
-
- // ---------------------------------------------------------------------------------
- // • IsModified
- // ---------------------------------------------------------------------------------
-
- Boolean
- CActiveXDocument::IsModified()
- {
- short i = kActiveXViewCount;
-
- while (i-- && !mIsModified)
- mIsModified = mActiveXView[i]->IsDirty();
-
- return mIsModified;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • DoAESave
- // ---------------------------------------------------------------------------------
-
- void
- CActiveXDocument::DoAESave(
- FSSpec &inFileSpec,
- OSType inFileType )
- {
- // Delete the existing file object.
- delete mFile;
-
- // Make a new file object.
- mFile = new LFile( inFileSpec );
-
- // Get the proper file type.
- OSType theFileType = 'TEXT';
- if ( inFileType != fileType_Default ) theFileType = inFileType;
-
- // Make new file on disk (we'll use
- // SimpleText's creator for this example).
- mFile->CreateNewDataFile( 'ttxt', theFileType );
-
- // Write out the data.
- DoSave();
-
- // Change window title to reflect the new name.
- mWindow->SetDescriptor( inFileSpec.name );
-
- // Document now has a specified file.
- mIsSpecified = true;
- }
-
-
- // ---------------------------------------------------------------------------------
- // • DoSave
- // ---------------------------------------------------------------------------------
-
- void
- CActiveXDocument::DoSave()
- {
- // Open the data fork.
- mFile->OpenDataFork( fsRdWrPerm );
-
- // Get the text from the text view.
- // Handle theTextH = mActiveXView->GetTextHandle();
-
- // Lock the text handle.
- // StHandleLocker theLock( theTextH );
-
- // Write the text to the file.
- // mFile->WriteDataFork( *theTextH, ::GetHandleSize( theTextH ) );
-
- // Close the data fork.
- mFile->CloseDataFork();
-
- // Saving makes doc un-dirty.
- short i = kActiveXViewCount;
-
- while (i-- && !mIsModified)
- mActiveXView[i]->SetDirty( false );
- }
-
-
- // ---------------------------------------------------------------------------------
- // • DoRevert
- // ---------------------------------------------------------------------------------
-
- void
- CActiveXDocument::DoRevert()
- {
- // Open the data fork.
- mFile->OpenDataFork( fsRdWrPerm );
-
- // Read the entire file contents and close the file.
- // Handle theTextH = mFile->ReadDataFork();
- // mFile->CloseDataFork();
-
- // Put the contents in the text view
- // and clear the dirty flag.
- // mActiveXView->SetTextHandle( theTextH );
- // mActiveXView->SetDirty( false );
-
- // Dispose of the text.
- // ::DisposeHandle( theTextH );
-
- // Refresh the text view.
- short i = kActiveXViewCount;
-
- while (i-- && !mIsModified)
- mActiveXView[i]->Refresh();
- }
-
-
- // ---------------------------------------------------------------------------------
- // • FindCommandStatus
- // ---------------------------------------------------------------------------------
-
- void
- CActiveXDocument::FindCommandStatus(
- CommandT inCommand,
- Boolean &outEnabled,
- Boolean &outUsesMark,
- Char16 &outMark,
- Str255 outName )
- {
- switch ( inCommand ) {
-
- case cmd_Print:
- case cmd_PrintOne:
- {
- // Short circuit the printing commands
- // since this example doesn't print.
- outEnabled = false;
- }
- break;
-
- default:
- {
- // Call inherited.
- LSingleDoc::FindCommandStatus( inCommand,
- outEnabled, outUsesMark, outMark, outName );
- }
- break;
-
- }
- }
-
-
- // ---------------------------------------------------------------------------------
- // • RequestFocus
- // ---------------------------------------------------------------------------------
-
- ErrorCode
- CActiveXDocument::RequestFocus ( CActiveXView* inActiveXView, Boolean inAcquire, FocusSet inFocus)
- {
- ErrorCode theResult = S_OK;
- CActiveXView* saveModal = mModalFocusView;
- Boolean8 modalReleased = false;
-
- if (inActiveXView == NULL)
- return E_FAIL;
-
- // Modal focus
- if (inFocus & ModalFocus)
- {
- if (inAcquire)
- {
- // If noone has the focus, give it to the requestor
- if (mModalFocusView == NULL)
- {
- mModalFocusView = inActiveXView;
- inActiveXView->AddRef();
- }
- // Does someone else have the focus?
- else if (mModalFocusView != inActiveXView)
- {
- // Ask them if they'll give it up
- if (mModalFocusView->SetFocus(RequestReleaseCommand, ModalFocus) == S_OK)
- {
- mModalFocusView->Release();
- mModalFocusView = inActiveXView;
- inActiveXView->AddRef();
- modalReleased = true;
- }
- else // They won't release it - fail the request
- theResult = E_FAIL;
- }
- }
- else // !inAcquire -- release
- {
- // If the requestor holds the focus, release it.
- if ((mModalFocusView != NULL) && (mModalFocusView == inActiveXView))
- {
- mModalFocusView->Release();
- mModalFocusView = NULL;
- }
- }
- }
-
- // Keyboard focus
- if ((theResult == S_OK) && (inFocus & KeyboardFocus))
- {
- if (inAcquire)
- {
- // If noone has the focus, give it to the requestor
- if (mFocusView == NULL)
- {
- mFocusView = inActiveXView;
- inActiveXView->AddRef();
- SwitchTarget(inActiveXView);
- }
- // Does someone else have the focus?
- else if (mFocusView != inActiveXView)
- {
- // Ask them if they'll give it up
- if (mFocusView->SetFocus(RequestReleaseCommand, KeyboardFocus) == S_OK)
- {
- mFocusView->Release();
- mFocusView = inActiveXView;
- inActiveXView->AddRef();
- SwitchTarget(inActiveXView);
- }
- else // They won't release it - fail the request
- theResult = E_FAIL;
- }
- }
- else // !inAcquire -- release
- {
- // If the requestor holds the focus, release it.
- if ((mFocusView != NULL) && (mFocusView == inActiveXView))
- {
- mFocusView->Release();
- mFocusView = NULL;
- SwitchTarget(this);
- }
- }
- }
-
- // If there was a problem and the modal focus was changed, attempt to restore it
- // to the state when we started.
- if ((theResult != S_OK) && inAcquire && modalReleased)
- {
- if (saveModal->SetFocus(TakeNextCommand, ModalFocus) == S_OK)
- {
- mModalFocusView->Release();
- mModalFocusView = saveModal;
- saveModal->AddRef();
- }
- else
- {
- mModalFocusView->Release();
- mModalFocusView = NULL;
- }
- }
-
- return theResult;
- }
-
-