home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 October: Mac OS SDK / Dev.CD Oct 96 SDK / Dev.CD Oct 96 SDK2.toast / Development Kits (Disc 2) / OpenDoc Development Framework / ODFDev / Draw / Sources / GroupCmd.cpp < prev    next >
Encoding:
Text File  |  1996-08-16  |  11.7 KB  |  339 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                GroupCmd.cpp
  4. //    Release Version:    $ ODF 1 $
  5. //
  6. //    Author:                Mary Boetcher
  7. //
  8. //    Copyright:    (c) 1993 - 1996 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef GROUPCMD_H
  15. #include "GroupCmd.h"
  16. #endif
  17.  
  18. #ifndef DRAWCONT_H
  19. #include "DrawCont.h"
  20. #endif
  21.  
  22. #ifndef DRAWPART_H
  23. #include "DrawPart.h"
  24. #endif
  25.  
  26. #ifndef DRAWSEL_H
  27. #include "DrawSel.h"
  28. #endif
  29.  
  30. #ifndef BASESHP_H
  31. #include "BaseShp.h"
  32. #endif
  33.  
  34. #ifndef GROUPSHP_H
  35. #include "GroupShp.h"
  36. #endif
  37.  
  38. // for cGroup
  39. #ifndef DEFINES_K
  40. #include "Defines.k"
  41. #endif
  42.  
  43. // ----- FrameWork Includes -----
  44.  
  45. #ifndef FWPRESEN_H
  46. #include "FWPresen.h"
  47. #endif
  48.  
  49. #ifndef SOM_ODShape_xh
  50. #include <Shape.xh>
  51. #endif
  52.  
  53. //========================================================================================
  54. // RunTime Info
  55. //========================================================================================
  56.  
  57. #ifdef FW_BUILD_MAC
  58. #pragma segment odfdrawcommand
  59. #endif
  60.  
  61. FW_DEFINE_AUTO(CGroupContent)
  62. FW_DEFINE_AUTO(CGroupShapesCommand)
  63. FW_DEFINE_AUTO(CUngroupShapesCommand)
  64.     
  65. //========================================================================================
  66. // class CGroupContent
  67. //========================================================================================
  68.  
  69. //----------------------------------------------------------------------------------------
  70. //    CGroupContent constructor
  71. //----------------------------------------------------------------------------------------
  72. CGroupContent::CGroupContent(Environment* ev, CDrawContent* other)
  73. :    CDrawContent(ev, other)
  74. {
  75. }
  76.  
  77. //----------------------------------------------------------------------------------------
  78. //    CGroupContent destructor
  79. //----------------------------------------------------------------------------------------
  80. CGroupContent::~CGroupContent()
  81. {
  82. }
  83.  
  84. //----------------------------------------------------------------------------------------
  85. //    CGroupContent::GetShapeList
  86. //----------------------------------------------------------------------------------------
  87. CShapeCollection* CGroupContent::GetShapeList()
  88. {
  89.     return fShapeList;
  90. }
  91.  
  92. //========================================================================================
  93. // CGroupShapesCommand
  94. //========================================================================================
  95.  
  96. //----------------------------------------------------------------------------------------
  97. //    CGroupShapesCommand constructor
  98. //----------------------------------------------------------------------------------------
  99. CGroupShapesCommand::CGroupShapesCommand(Environment* ev, FW_CFrame* frame, CDrawSelection* selection)
  100. :    FW_CCommand(ev, cGroup, frame, FW_kCanUndo),
  101.     fDrawSelection(selection),
  102.     fGroupShape(NULL),
  103.     fGroupedContent(NULL)
  104. {
  105.     SetMenuStringsFromResource(ev, kDrawUndoStrings, kUndoGroupMsg, kRedoGroupMsg);
  106.     FW_END_CONSTRUCTOR    
  107. }
  108.  
  109. //----------------------------------------------------------------------------------------
  110. //    CGroupShapesCommand destructor
  111. //----------------------------------------------------------------------------------------
  112. CGroupShapesCommand::~CGroupShapesCommand()
  113. {
  114.     FW_START_DESTRUCTOR
  115.     delete fGroupedContent;
  116. }
  117.  
  118. //----------------------------------------------------------------------------------------
  119. //    CGroupShapesCommand::CommitUndone
  120. //----------------------------------------------------------------------------------------
  121. void CGroupShapesCommand::CommitUndone(Environment* ev)
  122. {
  123.     // Delete the group shape, but not the shapes it contains
  124.     fGroupShape->EmptyShapes(false);    // remove shapes, but don't delete them
  125.     delete fGroupShape;                    // deletes the shapeList
  126. }
  127.  
  128. //----------------------------------------------------------------------------------------
  129. //    CGroupShapesCommand::DoIt
  130. //----------------------------------------------------------------------------------------
  131. void CGroupShapesCommand::DoIt(Environment* ev)
  132. {
  133.     // Save Undo state - copy selected shapes
  134.     fGroupedContent = FW_NEW(CGroupContent, (ev, fDrawSelection->GetDrawContent(ev)));
  135.  
  136.     // Combine selected shapes into a new group shape
  137.     CShapeCollection* shapeList = fGroupedContent->GetShapeList();
  138.     fGroupShape = FW_NEW(CGroupShape, (shapeList));
  139.  
  140.     this->GroupShapes(ev);
  141. }
  142.  
  143. //----------------------------------------------------------------------------------------
  144. //    CGroupShapesCommand::UndoIt
  145. //----------------------------------------------------------------------------------------
  146. void CGroupShapesCommand::UndoIt(Environment* ev)
  147. {
  148.     // Ungroup the shapes
  149.     CShapeCollection* shapeList = fGroupedContent->GetShapeList();
  150.     CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
  151.     CDrawPartContent* partContent = drawPart->GetDrawContent();
  152.  
  153.     // Add each shape back into the part's list
  154.     CShapeCollectionIterator it(shapeList);
  155.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  156.     {
  157.         partContent->AddShape(ev, shape, fGroupShape);    // insert before the group shape
  158.     }
  159.  
  160.     // Remove group shape from the part
  161.     partContent->RemoveShape(ev, fGroupShape);
  162.  
  163.     // Select the shapes
  164.     fDrawSelection->SelectContent(ev, fGroupedContent);
  165. }
  166.  
  167. //----------------------------------------------------------------------------------------
  168. //    CGroupShapesCommand::RedoIt
  169. //----------------------------------------------------------------------------------------
  170. void CGroupShapesCommand::RedoIt(Environment* ev)
  171. {
  172.     this->GroupShapes(ev);
  173. }
  174.  
  175. //----------------------------------------------------------------------------------------
  176. //    CGroupShapesCommand::GroupShapes
  177. //----------------------------------------------------------------------------------------
  178. void CGroupShapesCommand::GroupShapes(Environment* ev)
  179. {
  180.     // Remove shapes from the selection
  181.     fDrawSelection->CloseSelection(ev);
  182.  
  183.     // Remove each shape from the part's list (but don't detach it)
  184.     CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
  185.     CDrawPartContent* partContent = drawPart->GetDrawContent();
  186.     CShapeCollection* shapeList = fGroupedContent->GetShapeList();
  187.  
  188.     CShapeCollectionIterator it(shapeList);
  189.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  190.     {
  191.         partContent->RemoveShape(ev, shape);
  192.     }
  193.  
  194.     // Add the new group shape to the part and select it
  195.     drawPart->AddShapeToPart(ev, fGroupShape);
  196.     fDrawSelection->AddToSelection(ev, fGroupShape, true);
  197. }
  198.  
  199. //========================================================================================
  200. // CUngroupShapesCommand
  201. //========================================================================================
  202.  
  203. //----------------------------------------------------------------------------------------
  204. //    CUngroupShapesCommand constructor
  205. //----------------------------------------------------------------------------------------
  206. CUngroupShapesCommand::CUngroupShapesCommand(Environment* ev, FW_CFrame* frame, CDrawSelection* selection)
  207. :    FW_CCommand(ev, cUngroup, frame, FW_kCanUndo),
  208.     fDrawSelection(selection),
  209.     fGroupList(NULL)
  210. {
  211.     // Copy selected shapes that are group shapes
  212.     fGroupList = FW_NEW(CShapeCollection, ());
  213.     CDrawContentShapeIterator it(fDrawSelection->GetDrawContent(ev));
  214.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  215.     {
  216.         if (shape->GetShapeType() == kGroupShape)
  217.             fGroupList->AddLast(shape);
  218.     }
  219.  
  220.     SetMenuStringsFromResource(ev, kDrawUndoStrings, kUndoUngroupMsg, kRedoUngroupMsg);
  221.     FW_END_CONSTRUCTOR    
  222. }
  223.  
  224. //----------------------------------------------------------------------------------------
  225. //    CUngroupShapesCommand destructor
  226. //----------------------------------------------------------------------------------------
  227. CUngroupShapesCommand::~CUngroupShapesCommand()
  228. {
  229.     FW_START_DESTRUCTOR
  230.     delete fGroupList;
  231. }
  232.  
  233. //----------------------------------------------------------------------------------------
  234. //    CUngroupShapesCommand::CommitDone
  235. //----------------------------------------------------------------------------------------
  236. void CUngroupShapesCommand::CommitDone(Environment* ev)
  237. {
  238.     // Delete the saved group shapes, since they're obsolete
  239.     CBaseShape* shape;
  240.     while ((shape = fGroupList->First()) != NULL)
  241.     {
  242.         fGroupList->Remove(shape);
  243.         ((CGroupShape*)shape)->EmptyShapes(false);    // don't delete shapes
  244.         delete shape;
  245.     }
  246. }
  247.  
  248. //----------------------------------------------------------------------------------------
  249. //    CUngroupShapesCommand::DoIt
  250. //----------------------------------------------------------------------------------------
  251. void CUngroupShapesCommand::DoIt(Environment* ev)    // Override
  252. {
  253.     if (fGroupList->Count() == 0) return;    // nothing to do
  254.  
  255.     // Remove shapes from the selection
  256.     fDrawSelection->CloseSelection(ev);
  257.  
  258.     // Iterate saved shapes and ungroup each one
  259.     CShapeCollectionIterator it(fGroupList);
  260.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  261.     {
  262.         this->UngroupShape(ev, (CGroupShape*)shape);
  263.     }
  264.  
  265.     fDrawSelection->GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  266. }
  267.  
  268. //----------------------------------------------------------------------------------------
  269. //    CUngroupShapesCommand::UndoIt
  270. //----------------------------------------------------------------------------------------
  271. void CUngroupShapesCommand::UndoIt(Environment* ev)    // Override
  272. {
  273.     if (fGroupList->Count() == 0) return;    // nothing to do
  274.  
  275.     // Remove shapes from the selection
  276.     fDrawSelection->CloseSelection(ev);
  277.  
  278.     // Iterate saved shapes and regroup each one
  279.     CShapeCollectionIterator it(fGroupList);
  280.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  281.     {
  282.         this->RegroupShape(ev, (CGroupShape*)shape);
  283.     }
  284.  
  285.     fDrawSelection->GetPresentation(ev)->Invalidate(ev, fDrawSelection->GetUpdateShape());
  286. }
  287.  
  288. //----------------------------------------------------------------------------------------
  289. //    CUngroupShapesCommand::RedoIt
  290. //----------------------------------------------------------------------------------------
  291. void CUngroupShapesCommand::RedoIt(Environment* ev)    // Override
  292. {
  293.     this->DoIt(ev);
  294. }
  295.  
  296. //----------------------------------------------------------------------------------------
  297. //    CUngroupShapesCommand::UngroupShape
  298. //----------------------------------------------------------------------------------------
  299. void CUngroupShapesCommand::UngroupShape(Environment* ev, CGroupShape* groupShape)
  300. {
  301.     CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
  302.     CDrawPartContent* partContent = drawPart->GetDrawContent();
  303.  
  304.     // Remove the group shape itself from the part
  305.     CBaseShape* followingShape = partContent->GetShapeAfter(groupShape);
  306.     partContent->RemoveShape(ev, groupShape);
  307.  
  308.     // Iterate the shapes in the group and add each to the part, in the right position
  309.     CShapeCollectionIterator it(groupShape->GetShapeList());
  310.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  311.     {
  312.         partContent->AddShape(ev, shape, followingShape);
  313.         fDrawSelection->AddToSelection(ev, shape, false);
  314.     }
  315. }
  316.  
  317. //----------------------------------------------------------------------------------------
  318. //    CUngroupShapesCommand::RegroupShape
  319. //----------------------------------------------------------------------------------------
  320. void CUngroupShapesCommand::RegroupShape(Environment* ev, CGroupShape* groupShape)
  321. {
  322.     CDrawPart* drawPart = (CDrawPart*) fDrawSelection->GetPart(ev);
  323.     CDrawPartContent* partContent = drawPart->GetDrawContent();
  324.  
  325.     // Remove the shapes from the part
  326.     CBaseShape* followingShape = NULL;
  327.     CShapeCollectionIterator it(groupShape->GetShapeList());
  328.     for (CBaseShape* shape = it.First(); it.IsNotComplete(); shape = it.Next())
  329.     {
  330.         followingShape = partContent->GetShapeAfter(shape);
  331.         partContent->RemoveShape(ev, shape);
  332.     }
  333.  
  334.     // Add the group shape back into the part, in the right position
  335.     partContent->AddShape(ev, groupShape, followingShape);
  336.     fDrawSelection->AddToSelection(ev, groupShape, false);
  337. }
  338.  
  339.