home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-17 | 23.7 KB | 748 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: Commands.cpp
- // Release Version: $ ODF 2 $
- //
- // 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
-
- #ifndef FWSESION_H
- #include "FWSesion.h"
- #endif
-
- #ifndef FWINTER_H
- #include "FWInter.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef SOM_Module_OpenDoc_Commands_defined
- #include <CmdDefs.xh>
- #endif
-
- #ifndef SOM_ODDragAndDrop_xh
- #include <DragDrp.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)
-
- //========================================================================================
- // class CCellDragCommand
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CCellDragCommand constructor
- //----------------------------------------------------------------------------------------
-
- CCellDragCommand::CCellDragCommand( Environment* ev,
- CTablePartContent* tableContent,
- FW_CFrame* frame,
- CTableSelection* selection) :
- FW_CDragCommand(ev, frame, FW_kCanUndo),
- fTableContent(tableContent),
- fTableSelection(selection),
- fSavedProxy(NULL)
- {
- fDragCell = selection->GetSelectedCell();
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // CCellDragCommand destructor
- //----------------------------------------------------------------------------------------
-
- CCellDragCommand::~CCellDragCommand()
- {
- FW_START_DESTRUCTOR
- }
-
- //---------------------------------------------------------------------------------------
- // 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)
- {
- FW_UNUSED(ev);
- // Save the proxy for the part that was dragged
- fSavedProxy = fTableContent->CellToProxy(fDragCell);
- }
-
- //---------------------------------------------------------------------------------------
- // CCellDragCommand::FreeUndoState
- //---------------------------------------------------------------------------------------
- void CCellDragCommand::FreeUndoState(Environment* ev)
- {
- FW_UNUSED(ev);
- FW_ASSERT(fSavedProxy);
-
- delete fSavedProxy;
- fSavedProxy = NULL;
- }
-
- //========================================================================================
- // class CCellDropCommand
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CCellDropCommand constructor
- //----------------------------------------------------------------------------------------
-
- CCellDropCommand::CCellDropCommand( Environment* ev,
- CTablePart* itsPart,
- CTablePartContent* 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),
- fLinkCreated(false)
- {
- // If this was a Cmd-drag, set the selection to the dropCell for DoDroppedPasteAs
- ODDragAndDrop* drag = FW_CSession::GetDragAndDrop(ev);
- unsigned long attributes = drag->GetDragAttributes(ev);
- if (attributes & kODDropIsPasteAs)
- fTableFrame->SelectCell(ev, fDropCell);
-
- FW_END_CONSTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // CCellDropCommand destructor
- //----------------------------------------------------------------------------------------
-
- CCellDropCommand::~CCellDropCommand()
- {
- FW_START_DESTRUCTOR
- }
-
- //----------------------------------------------------------------------------------------
- // 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::DoDroppedPasteAs
- //---------------------------------------------------------------------------------------
- void CCellDropCommand::DoDroppedPasteAs(Environment* ev,
- const FW_CPoint& mouseDownOffset,
- const FW_CPoint& dropPoint)
- {
- FW_UNUSED(mouseDownOffset);
- FW_UNUSED(dropPoint);
-
- // Remember whether a link was created
- fLinkCreated = (GetNewLink(ev) != NULL);
- }
-
- //---------------------------------------------------------------------------------------
- // CCellDropCommand::UndoIt
- //---------------------------------------------------------------------------------------
- void CCellDropCommand::UndoIt(Environment* ev)
- {
- // activate a table frame (the dropped part may be active)
- CTableFrame* tableFrame = fTablePart->FindValidTableFrame(ev, fTableFrame, (CTableFrame*) GetSourceFrame(ev));
- FW_ASSERT(tableFrame);
- tableFrame->ActivateFrame(ev, NULL);
-
- if (this->IsDragMoveInSameFrame(ev)) // simple move from one cell to another
- {
- // move the dropped part back to the cell it came from
- tableFrame->MoveProxy(ev, fSavedProxy, fDraggedCell);
- fTablePart->ProxyMoved(ev, fDropCell, fDraggedCell);
- }
- else // part was dropped here from outside
- {
- if (fLinkCreated) // a link was created
- {
- // with links, proxy did not exist yet when command was 'done', and may
- // change between redoing and undoing. Get it from the cell now.
-
- fSavedProxy = fTableContent->CellToProxy(fDropCell);
- }
-
- // remove the dropped cell from the table
- CTableSelection* selection = tableFrame->GetSelection(ev);
- selection->SelectProxy(ev, fSavedProxy);
- selection->ClearSelection(ev);
- }
- }
-
- //---------------------------------------------------------------------------------------
- // CCellDropCommand::RedoIt
- //---------------------------------------------------------------------------------------
- void CCellDropCommand::RedoIt(Environment* ev)
- {
- // use a valid frame
- CTableFrame* tableFrame = fTablePart->FindValidTableFrame(ev, fTableFrame, (CTableFrame*) GetSourceFrame(ev));
- FW_ASSERT(tableFrame);
-
- if (this->IsDragMoveInSameFrame(ev)) // simple move from one cell to another
- {
- // move the dropped part to the cell it was dropped in
- tableFrame->MoveProxy(ev, fSavedProxy, fDropCell);
- fTablePart->ProxyMoved(ev, fDraggedCell, fDropCell);
- }
- else // part was dropped here from outside
- {
- // re-drop the cell
- fSavedProxy->AttachEmbeddedFrames(ev);
- fTableContent->AddProxy(fSavedProxy);
- tableFrame->SelectCell(ev, fDropCell);
- fTablePart->GetTableSelection(ev)->InvalidateSelection(ev);
- }
- }
-
- //---------------------------------------------------------------------------------------
- // CCellDropCommand::SaveRedoState
- //---------------------------------------------------------------------------------------
- void CCellDropCommand::SaveRedoState(Environment* ev)
- {
- FW_UNUSED(ev);
- if (!fLinkCreated)
- {
- // Save the proxy for the part that was just dropped
- fSavedProxy = fTableContent->CellToProxy(fDropCell);
- }
- }
-
- //---------------------------------------------------------------------------------------
- // CCellDropCommand::FreeRedoState
- //---------------------------------------------------------------------------------------
- void CCellDropCommand::FreeRedoState(Environment* ev)
- {
- FW_UNUSED(ev);
- // Delete the saved proxy
- delete fSavedProxy;
- }
-
- //========================================================================================
- // CTableEditCommand class
- //========================================================================================
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand constructor
- //---------------------------------------------------------------------------------------
- CTableEditCommand::CTableEditCommand(Environment* ev,
- ODCommandID id,
- CTablePart* itsPart,
- CTablePartContent* tableContent,
- FW_CFrame* frame,
- CTableSelection* selection) :
- FW_CClipboardCommand(ev, id, frame, FW_kCanUndo),
- fTablePart(itsPart),
- fTableContent(tableContent),
- fTableSelection(selection),
- fSavedProxy(NULL),
- fSavedLinkSource(NULL),
- fSavedLink(NULL)
- {
- FW_END_CONSTRUCTOR
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand destructor
- //---------------------------------------------------------------------------------------
- CTableEditCommand::~CTableEditCommand()
- {
- FW_START_DESTRUCTOR
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand::UndoIt
- //---------------------------------------------------------------------------------------
- void CTableEditCommand::UndoIt(Environment* ev)
- {
- FW_CClipboardCommand::UndoIt(ev); // call inherited
-
- switch (GetCommandID(ev))
- {
- case kODCommandCut:
- case kODCommandClear:
- this->RestoreSelection(ev);
- this->RestoreSavedLinks(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->BreakSavedLinks(ev); // do first, because next line causes UpdateLinkSource to be called
- 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);
-
- // If the saved proxy is a link source or destination, save the links too
- CTableLinkManager* linkMgr = (CTableLinkManager*)fTablePart->GetLinkManager(ev);
- CCell cell = fSavedProxy->GetCell();
- fSavedLink = linkMgr->CellToLink(ev, cell);
- fSavedLinkSource = linkMgr->CellToSourceLink(ev, cell);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CTableEditCommand::FreeUndoState (called by FW_CCommand::CommitDone)
- //----------------------------------------------------------------------------------------
- void CTableEditCommand::FreeUndoState(Environment* ev) // Override
- {
- if (GetCommandID(ev) == kODCommandCut || GetCommandID(ev) == kODCommandClear)
- {
- this->DeleteSavedProxy(ev);
- delete fSavedLink;
- delete fSavedLinkSource;
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CTableEditCommand::SaveRedoState
- //----------------------------------------------------------------------------------------
- void CTableEditCommand::SaveRedoState(Environment* ev) // Override
- {
- if (GetCommandID(ev) == kODCommandPaste)
- {
- this->SaveSelection(ev);
- }
- else if (GetCommandID(ev) == kODCommandPasteAs)
- {
- // If a link was created don't save the proxy;
- // a new proxy is created when the link is updated
- if (!WasLinkCreated(ev))
- this->SaveSelection(ev);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CTableEditCommand::PreCommand
- //----------------------------------------------------------------------------------------
- void CTableEditCommand::PreCommand(Environment* ev)
- {
- if (GetCommandID(ev) == kODCommandCut || GetCommandID(ev) == kODCommandClear)
- {
- // See if there's a link spec on the clipboard for the selected item,
- // and if so remove it.
- FW_CLinkManager* linkMgr = fTablePart->GetLinkManager(ev);
- FW_CLinkSource* linkSource = linkMgr->GetPendingClipboardLink(ev);
- if (linkSource)
- { // clipboard does contain a link spec
- if (linkSource->GetPendingID(ev) == fTableSelection->GetSavedLinkSpecID(ev))
- linkMgr->DeletePendingClipboardLink(ev);
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CTableEditCommand::CommandDone
- //----------------------------------------------------------------------------------------
- void CTableEditCommand::CommandDone(Environment* ev)
- {
- if (GetCommandID(ev) == kODCommandPasteAs)
- {
- if (WasLinkCreated(ev))
- fPasteAsCell = fTableSelection->GetSelectedCell();
- }
- else if (GetCommandID(ev) == kODCommandCopy)
- {
- // remember which proxy's link spec is on the clipboard
- fTableSelection->SaveLinkSpecID(ev, fTablePart->GetDataInterchange(ev)->GetLastClipboardUpdateID(ev));
- }
- else if (GetCommandID(ev) == kODCommandCut || GetCommandID(ev) == kODCommandClear)
- {
- // if the proxy that was cleared was in a link source or destination, break them
- this->BreakSavedLinks(ev);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CTableEditCommand::BreakSavedLinks
- //----------------------------------------------------------------------------------------
- void CTableEditCommand::BreakSavedLinks(Environment* ev)
- {
- // If there are saved links (from SaveUndoState), break them
- if (fSavedLink)
- {
- fTablePart->GetLinkManager(ev)->BreakDestinationLink(ev, fSavedLink);
- }
- if (fSavedLinkSource)
- {
- fTablePart->GetLinkManager(ev)->BreakSourceLink(ev, fSavedLinkSource);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CTableEditCommand::RestoreSavedLinks
- //----------------------------------------------------------------------------------------
- void CTableEditCommand::RestoreSavedLinks(Environment* ev)
- {
- // If there are saved links, restore them
- if (fSavedLink)
- {
- fTablePart->GetLinkManager(ev)->RestoreDestinationLink(ev, fSavedLink);
- }
- if (fSavedLinkSource)
- {
- fTablePart->GetLinkManager(ev)->RestoreSourceLink(ev, fSavedLinkSource);
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CTableEditCommand::WasLinkCreated
- //----------------------------------------------------------------------------------------
- FW_Boolean CTableEditCommand::WasLinkCreated(Environment* ev)
- {
- return (GetNewLink(ev) != NULL);
- }
-
- //----------------------------------------------------------------------------------------
- // CTableEditCommand::FreeRedoState (called by FW_CCommand::CommitUndone)
- //----------------------------------------------------------------------------------------
- void CTableEditCommand::FreeRedoState(Environment* ev) // Override
- {
- if (GetCommandID(ev) == kODCommandPaste || GetCommandID(ev) == kODCommandPasteAs)
- {
- this->DeleteSavedProxy(ev);
- }
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand::SaveSelection
- //---------------------------------------------------------------------------------------
- void CTableEditCommand::SaveSelection(Environment* ev)
- {
- FW_UNUSED(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);
-
- }
-
- //---------------------------------------------------------------------------------------
- // 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);
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand::DeleteSavedProxy
- //---------------------------------------------------------------------------------------
-
- void CTableEditCommand::DeleteSavedProxy(Environment* ev)
- {
- FW_UNUSED(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))
- {
- // with links, proxy did not exist yet when command was 'done', and may
- // change between redoing and undoing get it from the cell now.
-
- fSavedProxy = fTableContent->CellToProxy(fPasteAsCell);
- }
-
- // just like a normal Paste command
- this->RemoveSelection(ev);
-
- //--- Force redraw ---
- GetFrame(ev)->GetPresentation(ev)->Invalidate(ev);
-
- }
-
- //---------------------------------------------------------------------------------------
- // CTableEditCommand::RedoPasteAs
- //---------------------------------------------------------------------------------------
- void CTableEditCommand::RedoPasteAs(Environment* ev)
- {
- this->RestoreSelection(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);
- FW_END_CONSTRUCTOR
- }
-
- //---------------------------------------------------------------------------------------
- // CTableInsertCommand destructor
- //---------------------------------------------------------------------------------------
-
- CTableInsertCommand::~CTableInsertCommand()
- {
- FW_START_DESTRUCTOR
- }
-
- //---------------------------------------------------------------------------------------
- // CTableInsertCommand::UndoIt
- //---------------------------------------------------------------------------------------
-
- void CTableInsertCommand::UndoIt(Environment* ev)
- {
- // activate a table frame (the inserted part may be active)
- CTableFrame* tableFrame = fTablePart->FindValidTableFrame(ev, (CTableFrame*) GetFrame(ev), (CTableFrame*) GetSourceFrame(ev));
- FW_ASSERT(tableFrame);
- tableFrame->ActivateFrame(ev, NULL);
-
- // 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);
- selection->InvalidateSelection(ev);
- }
-
- //---------------------------------------------------------------------------------------
- // CTableInsertCommand::FreeRedoState
- //---------------------------------------------------------------------------------------
-
- void CTableInsertCommand::FreeRedoState(Environment*)
- {
- delete fInsertedProxy;
- }
-
- //---------------------------------------------------------------------------------------
- // CTableInsertCommand::SaveRedoState
- //---------------------------------------------------------------------------------------
-
- void CTableInsertCommand::SaveRedoState(Environment*)
- {
- fInsertedProxy = fTableContent->CellToProxy(fInsertCell);
- }
-
-
-