home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-08-16 | 24.0 KB | 736 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: Commands.cpp
- // Release Version: $ ODF 1 $
- //
- // Copyright: (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #ifndef COMMANDS_H
- #include "Commands.h"
- #endif
-
- #ifndef FRAME_H
- #include "Frame.h"
- #endif
-
- #ifndef PART_H
- #include "Part.h"
- #endif
-
- #ifndef SELECTION_H
- #include "Selection.h"
- #endif
-
- #ifndef PROXY_H
- #include "Proxy.h"
- #endif
-
- #ifndef LINKING_H
- #include "Linking.h"
- #endif
-
- #ifndef CONTENT_H
- #include "Content.h"
- #endif
-
- // ----- Framework Includes -----
-
- #ifndef FWPART_H
- #include "FWPart.h"
- #endif
-
- #ifndef FWFRAME_H
- #include "FWFrame.h"
- #endif
-
- #ifndef FWSELECT_H
- #include "FWSelect.h"
- #endif
-
- #ifndef FWBARRAY_H
- #include "FWBArray.h"
- #endif
-
- #ifndef FWFRMING_H
- #include "FWFrming.h"
- #endif
-
- #ifndef FWLNKMGR_H
- #include "FWLnkMgr.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef SOM_Module_OpenDoc_Commands_defined
- #include <CmdDefs.xh>
- #endif
-
- #ifndef SOM_ODLinkSource_xh
- #include <LinkSrc.xh>
- #endif
-
- #ifndef SOM_ODLinkSpec_xh
- #include <LinkSpec.xh>
- #endif
-
- //========================================================================================
- // Runtime Info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfTable
- #endif
-
- FW_DEFINE_AUTO(CCellDragCommand)
- FW_DEFINE_AUTO(CCellDropCommand)
- FW_DEFINE_AUTO(CTableEditCommand)
- FW_DEFINE_AUTO(CTableInsertCommand)
- FW_DEFINE_AUTO(CBreakLinkSourceCommand)
- FW_DEFINE_AUTO(CBreakLinkCommand)
-
- //========================================================================================
- // class CCellDragCommand
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CCellDragCommand constructor
- //----------------------------------------------------------------------------------------
-
- CCellDragCommand::CCellDragCommand( Environment* ev,
- CTableContent* tableContent,
- FW_CFrame* frame,
- CTableSelection* selection) :
- FW_CDragCommand(ev, frame, FW_kCanUndo),
- fTableContent(tableContent),
- fTableSelection(selection),
- fSavedProxy(NULL)
- {
- fDragCell = selection->GetSelectedCell();
- }
-
- //----------------------------------------------------------------------------------------
- // CCellDragCommand destructor
- //----------------------------------------------------------------------------------------
-
- CCellDragCommand::~CCellDragCommand()
- {
- }
-
- //---------------------------------------------------------------------------------------
- // CCellDragCommand::UndoIt
- //---------------------------------------------------------------------------------------
- void CCellDragCommand::UndoIt(Environment* ev)
- {
- FW_ASSERT(fSavedProxy);
-
- // Restore saved proxy to the table
- fSavedProxy->AttachEmbeddedFrames(ev);
- fTableContent->AddProxy(fSavedProxy);
- fTableSelection->SelectProxy(ev, fSavedProxy);
- }
-
- //---------------------------------------------------------------------------------------
- // CCellDragCommand::RedoIt
- //---------------------------------------------------------------------------------------
- void CCellDragCommand::RedoIt(Environment* ev)
- {
- // Remove saved proxy, again
- fTableSelection->SelectProxy(ev, fSavedProxy);
- fTableSelection->ClearSelection(ev);
- }
-
- //---------------------------------------------------------------------------------------
- // CCellDragCommand::SaveUndoState
- //---------------------------------------------------------------------------------------
- void CCellDragCommand::SaveUndoState(Environment* ev)
- {
- // Save the proxy for the part that was dragged
- fSavedProxy = fTableContent->CellToProxy(fDragCell);
- }
-
- //---------------------------------------------------------------------------------------
- // CCellDragCommand::FreeUndoState
- //---------------------------------------------------------------------------------------
- void CCellDragCommand::FreeUndoState(Environment* ev)
- {
- FW_ASSERT(fSavedProxy);
-
- delete fSavedProxy;
- fSavedProxy = NULL;
- }
-
- //========================================================================================
- // class CCellDropCommand
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CCellDropCommand constructor
- //----------------------------------------------------------------------------------------
-
- CCellDropCommand::CCellDropCommand( Environment* ev,
- CTablePart* itsPart,
- CTableContent* tableContent,
- CTableFrame* frame,
- ODDragItemIterator* dropInfo,
- ODFacet* odFacet,
- const FW_CPoint& dropPoint,
- const CCell& dropCell,
- const CCell& draggedCell) :
- FW_CDropCommand(ev, frame, dropInfo, odFacet, dropPoint, FW_kCanUndo),
- fTablePart(itsPart),
- fTableContent(tableContent),
- fTableFrame(frame),
- fDropCell(dropCell),
- fDraggedCell(draggedCell),
- fSavedProxy(NULL)
- {
- // If this was a Cmd-drag, set the selection to the dropCell for DoDroppedPasteAs
- ODDragAndDrop* drag = itsPart->GetSession(ev)->GetDragAndDrop(ev);
- unsigned long attributes = drag->GetDragAttributes(ev);
- if (attributes & kODDropIsPasteAs)
- fTableFrame->SelectCell(ev, fDropCell);
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // CCellDropCommand destructor
- //----------------------------------------------------------------------------------------
-
- CCellDropCommand::~CCellDropCommand()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CCellDropCommand::DoDrop
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CCellDropCommand::DoDrop(Environment* ev,
- ODStorageUnit* dropSU,
- const FW_CPoint& mouseDownOffset,
- const FW_CPoint& dropPoint,
- FW_Boolean isDropMove,
- short itemNumber)
- {
- if (itemNumber > 1) // can only drop one item in a cell
- return false;
-
- fTableFrame->SelectCell(ev, fDropCell);
-
- return FW_CDropCommand::DoDrop(ev, dropSU, mouseDownOffset, dropPoint, isDropMove, itemNumber);
- }
-
- //----------------------------------------------------------------------------------------
- // CCellDropCommand::DoDroppedInSameFrame
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CCellDropCommand::DoDroppedInSameFrame(Environment* ev,
- ODStorageUnit* dropSU,
- const FW_CPoint& mouseDownOffset,
- const FW_CPoint& dropPoint)
- {
- FW_UNUSED(dropSU);
- FW_UNUSED(mouseDownOffset);
- FW_UNUSED(dropPoint);
-
- FW_ASSERT(fDropCell != fDraggedCell);
-
- // Move the frame's cell
-
- CTableProxy* proxy = fTableContent->CellToProxy(fDraggedCell);
- FW_ASSERT(proxy);
-
- fTableFrame->MoveProxy(ev, proxy, fDropCell);
- fTablePart->ProxyMoved(ev, fDraggedCell, fDropCell);
-
- return TRUE;
- }
-
- //---------------------------------------------------------------------------------------
- // CCellDropCommand::UndoIt
- //---------------------------------------------------------------------------------------
- void CCellDropCommand::UndoIt(Environment* ev)
- {
- FW_ASSERT(fSavedProxy);
-
- if (this->IsDragMoveInSameFrame(ev)) // simple move from one cell to another
- {
- // move the dropped part back to the cell it came from
- fTableFrame->MoveProxy(ev, fSavedProxy, fDraggedCell);
- }
- else // part was dropped here from outside
- {
- // remove the dropped cell from the table
- CTableSelection* selection = fTableFrame->GetSelection(ev);
- selection->SelectProxy(ev, fSavedProxy);
- selection->ClearSelection(ev);
- }
- }
-
- //---------------------------------------------------------------------------------------
- // CCellDropCommand::RedoIt
- //---------------------------------------------------------------------------------------
- void CCellDropCommand::RedoIt(Environment* ev)
- {
- if (this->IsDragMoveInSameFrame(ev)) // simple move from one cell to another
- {
- // move the dropped part to the cell it was dropped in
- fTableFrame->MoveProxy(ev, fSavedProxy, fDropCell);
- }
- else // part was dropped here from outside
- {
- // re-drop the cell
- fSavedProxy->AttachEmbeddedFrames(ev);
- fTableContent->AddProxy(fSavedProxy);
- fTableFrame->SelectCell(ev, fDropCell);
- }
- }
-
- //---------------------------------------------------------------------------------------
- // CCellDropCommand::SaveRedoState
- //---------------------------------------------------------------------------------------
- void CCellDropCommand::SaveRedoState(Environment* ev)
- {
- // Save the proxy for the part that was just dropped
- fSavedProxy = fTableContent->CellToProxy(fDropCell);
- }
-
- //---------------------------------------------------------------------------------------
- // CCellDropCommand::FreeRedoState
- //---------------------------------------------------------------------------------------
- void CCellDropCommand::FreeRedoState(Environment* ev)
- {
- // Delete the saved proxy
- delete fSavedProxy;
- }
-
- //========================================================================================
- // CTableEditCommand class
- //========================================================================================
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand constructor
- //---------------------------------------------------------------------------------------
- CTableEditCommand::CTableEditCommand(Environment* ev,
- ODCommandID id,
- CTablePart* itsPart,
- CTableContent* tableContent,
- FW_CFrame* frame,
- CTableSelection* selection) :
- FW_CClipboardCommand(ev, id, frame, FW_kCanUndo),
- fTablePart(itsPart),
- fTableContent(tableContent),
- fTableSelection(selection),
- fSavedProxy(NULL)
- {
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand destructor
- //---------------------------------------------------------------------------------------
- CTableEditCommand::~CTableEditCommand()
- {
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand::UndoIt
- //---------------------------------------------------------------------------------------
- void CTableEditCommand::UndoIt(Environment* ev)
- {
- FW_CClipboardCommand::UndoIt(ev); // call inherited
-
- switch (GetCommandID(ev))
- {
- case kODCommandCut:
- case kODCommandClear:
- this->RestoreSelection(ev);
- break;
-
- case kODCommandPaste:
- this->RemoveSelection(ev);
- break;
-
- case kODCommandPasteAs:
- this->UndoPasteAs(ev);
- }
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand::RedoIt
- //---------------------------------------------------------------------------------------
- void CTableEditCommand::RedoIt(Environment* ev) // Override
- {
- FW_CClipboardCommand::RedoIt(ev); // call inherited
-
- switch (GetCommandID(ev))
- {
- case kODCommandCut:
- case kODCommandClear:
- this->RemoveSelection(ev);
- break;
-
- case kODCommandPaste:
- this->RestoreSelection(ev);
- break;
-
- case kODCommandPasteAs:
- this->RedoPasteAs(ev);
- break;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CTableEditCommand::SaveUndoState
- //----------------------------------------------------------------------------------------
- void CTableEditCommand::SaveUndoState(Environment* ev) // Override
- {
- if (GetCommandID(ev) == kODCommandCut || GetCommandID(ev) == kODCommandClear)
- this->SaveSelection(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CTableEditCommand::FreeUndoState (doneState == kODDone)
- //----------------------------------------------------------------------------------------
- void CTableEditCommand::FreeUndoState(Environment* ev) // Override
- {
- if (GetCommandID(ev) == kODCommandCut || GetCommandID(ev) == kODCommandClear)
- this->DeleteSavedProxy(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CTableEditCommand::SaveRedoState
- //----------------------------------------------------------------------------------------
- void CTableEditCommand::SaveRedoState(Environment* ev) // Override
- {
- if (GetCommandID(ev) == kODCommandPaste)
- {
- this->SaveSelection(ev);
- }
- else if (GetCommandID(ev) == kODCommandPasteAs)
- {
- fPasteAsCell = fTableSelection->GetSelectedCell();
- // If a link was created don't save the proxy
- if (!WasLinkCreated(ev))
- this->SaveSelection(ev);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CTableEditCommand::WasLinkCreated
- //----------------------------------------------------------------------------------------
- FW_Boolean CTableEditCommand::WasLinkCreated(Environment* ev)
- {
- return (GetNewLink(ev) != NULL);
- }
-
- //----------------------------------------------------------------------------------------
- // CTableEditCommand::FreeRedoState (doneState == kODUndone)
- //----------------------------------------------------------------------------------------
- void CTableEditCommand::FreeRedoState(Environment* ev) // Override
- {
- if (GetCommandID(ev) == kODCommandPaste)
- {
- this->DeleteSavedProxy(ev);
- }
- else if (GetCommandID(ev) == kODCommandPasteAs)
- {
- if (!WasLinkCreated(ev))
- this->DeleteSavedProxy(ev);
- }
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand::SaveSelection
- //---------------------------------------------------------------------------------------
- void CTableEditCommand::SaveSelection(Environment* ev)
- {
- //--- The selection is a single embedded frame ---
- CCell cell = fTableSelection->GetSelectedCell(); // selected cell
- fSavedProxy = fTableContent->CellToProxy(cell);
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand::RestoreSelection
- //---------------------------------------------------------------------------------------
-
- void CTableEditCommand::RestoreSelection(Environment* ev)
- {
- // Re-attach the proxy's frames
- fSavedProxy->AttachEmbeddedFrames(ev);
-
- // Add the saved proxy back into the table part
- fTableContent->AddProxy(fSavedProxy);
-
- // Select it
- fTableSelection->CloseSelection(ev);
- fTableSelection->SelectProxy(ev, fSavedProxy);
- fTableSelection->InvalidateSelection(ev);
-
- // Notify part
- fTablePart->PartChanged(ev);
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand::RemoveSelection
- //---------------------------------------------------------------------------------------
- void CTableEditCommand::RemoveSelection(Environment* ev)
- {
- // Make sure the saved proxy is selected
- fTableSelection->CloseSelection(ev);
- fTableSelection->SelectProxy(ev, fSavedProxy);
-
- // Save the selected proxy
- this->SaveSelection(ev);
-
- // Clear the selection, which includes detaching the proxy frames.
- fTableSelection->ClearSelection(ev);
-
- // Notify part
- fTablePart->PartChanged(ev);
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand::DeleteSavedProxy
- //---------------------------------------------------------------------------------------
- void CTableEditCommand::DeleteSavedProxy(Environment* ev)
- {
- FW_ASSERT(fSavedProxy);
-
- delete fSavedProxy;
- fSavedProxy = NULL;
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand::UndoPasteAs
- //---------------------------------------------------------------------------------------
- void CTableEditCommand::UndoPasteAs(Environment* ev)
- {
- //--- Break the link, if there is one ---
- if (WasLinkCreated(ev))
- {
- //--- Remove the proxy frame ---
- CTableProxy* proxy = fTableContent->CellToProxy(fPasteAsCell);
- proxy->RemoveEmbeddedFrames(ev);
- fTableContent->RemoveProxy(proxy);
- delete proxy;
- }
- else // just like a normal Paste command
- this->RemoveSelection(ev);
-
- //--- Force redraw ---
- GetFrame(ev)->GetPresentation(ev)->Invalidate(ev);
-
- // Notify part
- fTablePart->PartChanged(ev);
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand::RedoPasteAs
- //---------------------------------------------------------------------------------------
- void CTableEditCommand::RedoPasteAs(Environment* ev)
- {
- if (!WasLinkCreated(ev))
- this->RestoreSelection(ev);
-
- // Notify part
- fTablePart->PartChanged(ev);
- }
-
- //========================================================================================
- // CTableInsertCommand class
- //========================================================================================
-
- //---------------------------------------------------------------------------------------
- // CTableInsertCommand constructor
- //---------------------------------------------------------------------------------------
- CTableInsertCommand::CTableInsertCommand(Environment* ev,
- FW_CEmbeddingFrame* frame,
- const FW_PFileSpecification& fileSpec,
- CTablePart* itsPart,
- const CCell& insertCell)
- : FW_CInsertCommand(ev, frame, fileSpec, FW_kCanUndo),
- fTablePart(itsPart),
- fTableContent(NULL),
- fInsertedProxy(NULL),
- fInsertCell(insertCell)
- {
- fTableContent = itsPart->GetTableContent(ev);
- }
-
- //---------------------------------------------------------------------------------------
- // CTableInsertCommand destructor
- //---------------------------------------------------------------------------------------
- CTableInsertCommand::~CTableInsertCommand()
- {
- }
-
- //---------------------------------------------------------------------------------------
- // CTableInsertCommand::UndoIt
- //---------------------------------------------------------------------------------------
- void CTableInsertCommand::UndoIt(Environment* ev)
- {
- // remove the inserted part
- CTableSelection* selection = fTablePart->GetTableSelection(ev);
- selection->Select(ev, fInsertCell);
- selection->ClearSelection(ev);
- }
-
- //---------------------------------------------------------------------------------------
- // CTableInsertCommand::RedoIt
- //---------------------------------------------------------------------------------------
- void CTableInsertCommand::RedoIt(Environment* ev)
- {
- // re-insert the part
- fInsertedProxy->AttachEmbeddedFrames(ev);
- fTableContent->AddProxy(fInsertedProxy);
- CTableSelection* selection = fTablePart->GetTableSelection(ev);
- selection->Select(ev, fInsertCell);
- }
-
- //---------------------------------------------------------------------------------------
- // CTableInsertCommand::CommitUndone
- //---------------------------------------------------------------------------------------
- void CTableInsertCommand::CommitUndone(Environment* ev)
- {
- delete fInsertedProxy;
- }
-
- //---------------------------------------------------------------------------------------
- // CTableInsertCommand::SaveRedoState
- //---------------------------------------------------------------------------------------
- void CTableInsertCommand::SaveRedoState(Environment* ev)
- {
- fInsertedProxy = fTableContent->CellToProxy(fInsertCell);
- }
-
- //========================================================================================
- // Constants for CBreakLinkSourceCommand and CBreakLinkCommand
- //========================================================================================
-
- const ODCommandID kBreakLinkSourceID = 12345;
- const ODCommandID kBreakLinkID = 56789;
-
- //========================================================================================
- // CBreakLinkSourceCommand class
- //========================================================================================
-
- //---------------------------------------------------------------------------------------
- // CBreakLinkSourceCommand constructor
- //---------------------------------------------------------------------------------------
- CBreakLinkSourceCommand::CBreakLinkSourceCommand(Environment* ev,
- FW_CFrame* frame,
- CTableLinkSource* linkSource) :
- FW_CCommand(ev, kBreakLinkSourceID, frame, FW_kCanUndo),
- fLinkSource(linkSource)
- {
- this->SetMenuStrings(ev, "Restore Link", "Break Link");
- }
-
- //---------------------------------------------------------------------------------------
- // CBreakLinkSourceCommand destructor
- //---------------------------------------------------------------------------------------
- CBreakLinkSourceCommand::~CBreakLinkSourceCommand()
- {
- }
-
- //---------------------------------------------------------------------------------------
- // CBreakLinkSourceCommand::DoIt
- //---------------------------------------------------------------------------------------
- void CBreakLinkSourceCommand::DoIt(Environment* ev)
- {
- //-- Break the link --
- GetPart(ev)->GetLinkManager(ev)->BreakSourceLink(ev, fLinkSource);
- GetPart(ev)->Changed(ev);
- }
-
- //---------------------------------------------------------------------------------------
- // CBreakLinkSourceCommand::UndoIt
- //---------------------------------------------------------------------------------------
- void CBreakLinkSourceCommand::UndoIt(Environment* ev)
- {
- //-- Restore the broken link --
- GetPart(ev)->GetLinkManager(ev)->RestoreSourceLink(ev, fLinkSource);
- GetPart(ev)->Changed(ev);
- }
-
- //---------------------------------------------------------------------------------------
- // CBreakLinkSourceCommand::RedoIt
- //---------------------------------------------------------------------------------------
- void CBreakLinkSourceCommand::RedoIt(Environment* ev)
- {
- this->DoIt(ev); // just DoIt
- }
-
- //---------------------------------------------------------------------------------------
- // CBreakLinkSourceCommand::CommitDone
- //---------------------------------------------------------------------------------------
- void CBreakLinkSourceCommand::CommitDone(Environment* ev)
- {
- // Delete the saved link source only if the command was not undone
- delete fLinkSource;
- }
-
- //========================================================================================
- // CBreakLinkCommand class
- //========================================================================================
-
- //---------------------------------------------------------------------------------------
- // CBreakLinkCommand constructor
- //---------------------------------------------------------------------------------------
- CBreakLinkCommand::CBreakLinkCommand(Environment* ev,
- FW_CFrame* frame,
- CTableLink* linkDestination) :
- FW_CCommand(ev, kBreakLinkID, frame, FW_kCanUndo),
- fLink(linkDestination)
- {
- this->SetMenuStrings(ev, "Restore Link", "Break Link");
- }
-
- //---------------------------------------------------------------------------------------
- // CBreakLinkCommand destructor
- //---------------------------------------------------------------------------------------
- CBreakLinkCommand::~CBreakLinkCommand()
- {
- }
-
- //---------------------------------------------------------------------------------------
- // CBreakLinkCommand::DoIt
- //---------------------------------------------------------------------------------------
- void CBreakLinkCommand::DoIt(Environment* ev)
- {
- //-- Break the link --
- GetPart(ev)->GetLinkManager(ev)->BreakDestinationLink(ev, fLink);
- GetPart(ev)->Changed(ev);
- }
-
- //---------------------------------------------------------------------------------------
- // CBreakLinkCommand::UndoIt
- //---------------------------------------------------------------------------------------
- void CBreakLinkCommand::UndoIt(Environment* ev)
- {
- //-- Restore the broken link --
- GetPart(ev)->GetLinkManager(ev)->RestoreDestinationLink(ev, fLink);
- GetPart(ev)->Changed(ev);
- }
-
- //---------------------------------------------------------------------------------------
- // CBreakLinkCommand::RedoIt
- //---------------------------------------------------------------------------------------
- void CBreakLinkCommand::RedoIt(Environment* ev)
- {
- this->DoIt(ev); // just DoIt
- }
-
- //---------------------------------------------------------------------------------------
- // CBreakLinkCommand::CommitDone
- //---------------------------------------------------------------------------------------
- void CBreakLinkCommand::CommitDone(Environment* ev)
- {
- // Delete the saved link destination only if the command was not undone
- delete fLink;
- }
-
-
-