pathndge.cpp

Go to the documentation of this file.
00001 // $Id: pathndge.cpp 1282 2006-06-09 09:46:49Z alex $
00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE
00003 ================================XARAHEADERSTART===========================
00004  
00005                Xara LX, a vector drawing and manipulation program.
00006                     Copyright (C) 1993-2006 Xara Group Ltd.
00007        Copyright on certain contributions may be held in joint with their
00008               respective authors. See AUTHORS file for details.
00009 
00010 LICENSE TO USE AND MODIFY SOFTWARE
00011 ----------------------------------
00012 
00013 This file is part of Xara LX.
00014 
00015 Xara LX is free software; you can redistribute it and/or modify it
00016 under the terms of the GNU General Public License version 2 as published
00017 by the Free Software Foundation.
00018 
00019 Xara LX and its component source files are distributed in the hope
00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the
00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00022 See the GNU General Public License for more details.
00023 
00024 You should have received a copy of the GNU General Public License along
00025 with Xara LX (see the file GPL in the root directory of the
00026 distribution); if not, write to the Free Software Foundation, Inc., 51
00027 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
00028 
00029 
00030 ADDITIONAL RIGHTS
00031 -----------------
00032 
00033 Conditional upon your continuing compliance with the GNU General Public
00034 License described above, Xara Group Ltd grants to you certain additional
00035 rights. 
00036 
00037 The additional rights are to use, modify, and distribute the software
00038 together with the wxWidgets library, the wxXtra library, and the "CDraw"
00039 library and any other such library that any version of Xara LX relased
00040 by Xara Group Ltd requires in order to compile and execute, including
00041 the static linking of that library to XaraLX. In the case of the
00042 "CDraw" library, you may satisfy obligation under the GNU General Public
00043 License to provide source code by providing a binary copy of the library
00044 concerned and a copy of the license accompanying it.
00045 
00046 Nothing in this section restricts any of the rights you have under
00047 the GNU General Public License.
00048 
00049 
00050 SCOPE OF LICENSE
00051 ----------------
00052 
00053 This license applies to this program (XaraLX) and its constituent source
00054 files only, and does not necessarily apply to other Xara products which may
00055 in part share the same code base, and are subject to their own licensing
00056 terms.
00057 
00058 This license does not apply to files in the wxXtra directory, which
00059 are built into a separate library, and are subject to the wxWindows
00060 license contained within that directory in the file "WXXTRA-LICENSE".
00061 
00062 This license does not apply to the binary libraries (if any) within
00063 the "libs" directory, which are subject to a separate license contained
00064 within that directory in the file "LIBS-LICENSE".
00065 
00066 
00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS
00068 ----------------------------------------------
00069 
00070 Subject to the terms of the GNU Public License (see above), you are
00071 free to do whatever you like with your modifications. However, you may
00072 (at your option) wish contribute them to Xara's source tree. You can
00073 find details of how to do this at:
00074   http://www.xaraxtreme.org/developers/
00075 
00076 Prior to contributing your modifications, you will need to complete our
00077 contributor agreement. This can be found at:
00078   http://www.xaraxtreme.org/developers/contribute/
00079 
00080 Please note that Xara will not accept modifications which modify any of
00081 the text between the start and end of this header (marked
00082 XARAHEADERSTART and XARAHEADEREND).
00083 
00084 
00085 MARKS
00086 -----
00087 
00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara
00089 designs are registered or unregistered trademarks, design-marks, and/or
00090 service marks of Xara Group Ltd. All rights in these marks are reserved.
00091 
00092 
00093       Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK.
00094                         http://www.xara.com/
00095 
00096 =================================XARAHEADEREND============================
00097  */
00098 // Implementation of the path nudge ops
00099 
00100 /*
00101 */
00102 
00103 #include "camtypes.h"
00104 #include "pathndge.h"
00105 
00106 //#include "app.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00107 //#include "markn.h"
00108 #include "nodepath.h"
00109 #include "opnudge.h"
00110 //#include "trans2d.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00111 #include "pathedit.h"
00112 //#include "docview.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00113 //#include "document.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00114 #include "ophist.h"
00115 
00116 DECLARE_SOURCE("$Revision: 1282 $");
00117 
00118 CC_IMPLEMENT_DYNCREATE(OpPathNudge,TransOperation)
00119 
00120 
00121 /********************************************************************************************
00122 
00123 >   void IMPLEMENT_OPPATHNUDGE_CLASS(DIR,X_NUDGE_FACTOR,Y_NUDGE_FACTOR,UNDO_IDS)
00124 
00125     Author:     Peter_Arnold (Xara Group Ltd) <camelotdev@xara.com> (via MarkN)
00126     Created:    14/10/94
00127     Inputs:     DIR             = String postfix specifying the nudge direction and magnitude, e.g. Up1
00128                 X_NUDGE_FACTOR  = signed INT32 giving the x nudge magnitude and direction
00129                 Y_NUDGE_FACTOR  = y version of above
00130                 UNDO_IDS        = IDS of undo string to append to the undo/redo men items
00131     Outputs:    -
00132     Returns:    
00133     Purpose:    The macro IMPLEMENT_OPPATHNUDGE_CLASS(DIR,X_NUDGE_FACTOR,Y_NUDGE_FACTOR,UNDO_IDS) implements
00134                 the OpNudgeX Do() function.
00135                 E.g. OpNudgeUp1
00136 
00137                 Firstly, CC_IMPLEMENT_DYNCREATE(OpNudgeUp1,OpNudge) is done
00138 
00139                 Then itsets the OpNudge protected member vars as follows-
00140                     X_NudgeFactor = 0;                  // I.e. don't nudge in the X dir
00141                     Y_NudgeFactor = 1;                  // I.e. Nudge up by 1 nudge step 
00142                     NudgeUndoIDS  = _R(IDS_UNDO_NUDGE_UP);  // This string appended to the undo/redo menu items
00143 
00144                 It then calls OpNudge::Do() which uses the nudge factors to determine nudging magnitude and
00145                 direction
00146 
00147     Errors:     -
00148     SeeAlso:    IMPLEMENT_OPNUDGE_CLASS
00149 
00150 ********************************************************************************************/
00151 
00152 #define IMPLEMENT_OPPATHNUDGE_CLASS(DIR,X_NUDGE_FACTOR,Y_NUDGE_FACTOR,UNDO_IDS) \
00153     CC_IMPLEMENT_DYNCREATE(OpPathNudge ## DIR,OpPathNudge) \
00154     void OpPathNudge ## DIR::Do(OpDescriptor* pOpDesc) \
00155     { \
00156         X_NudgeFactor = X_NUDGE_FACTOR; \
00157         Y_NudgeFactor = Y_NUDGE_FACTOR; \
00158         NudgeUndoIDS = UNDO_IDS; \
00159         OpPathNudge::Do(pOpDesc); \
00160     } \
00161 
00162 IMPLEMENT_OPPATHNUDGE_CLASS (Up1,0,1,_R(IDS_UNDO_NUDGE_UP))
00163 IMPLEMENT_OPPATHNUDGE_CLASS (Up5,0,5,_R(IDS_UNDO_NUDGE_UP))
00164 IMPLEMENT_OPPATHNUDGE_CLASS (Up10,0,10,_R(IDS_UNDO_NUDGE_UP))
00165 IMPLEMENT_OPPATHNUDGE_CLASS (UpFifth,0,0.2,_R(IDS_UNDO_NUDGE_UP))
00166 IMPLEMENT_OPPATHNUDGE_CLASS (UpPixel1,0,1,_R(IDS_UNDO_NUDGE_UP))
00167 IMPLEMENT_OPPATHNUDGE_CLASS (UpPixel10,0,10,_R(IDS_UNDO_NUDGE_UP))
00168 
00169 
00170 IMPLEMENT_OPPATHNUDGE_CLASS (Down1,0,-1,_R(IDS_UNDO_NUDGE_DOWN))
00171 IMPLEMENT_OPPATHNUDGE_CLASS (Down5,0,-5,_R(IDS_UNDO_NUDGE_DOWN))
00172 IMPLEMENT_OPPATHNUDGE_CLASS (Down10,0,-10,_R(IDS_UNDO_NUDGE_DOWN))
00173 IMPLEMENT_OPPATHNUDGE_CLASS (DownFifth,0,-0.2,_R(IDS_UNDO_NUDGE_DOWN))
00174 IMPLEMENT_OPPATHNUDGE_CLASS (DownPixel1 ,0,-1,_R(IDS_UNDO_NUDGE_DOWN))
00175 IMPLEMENT_OPPATHNUDGE_CLASS (DownPixel10,0,-10,_R(IDS_UNDO_NUDGE_DOWN))
00176 
00177 IMPLEMENT_OPPATHNUDGE_CLASS (Left1,-1,0,_R(IDS_UNDO_NUDGE_LEFT))
00178 IMPLEMENT_OPPATHNUDGE_CLASS (Left5,-5,0,_R(IDS_UNDO_NUDGE_LEFT))
00179 IMPLEMENT_OPPATHNUDGE_CLASS (Left10,-10,0,_R(IDS_UNDO_NUDGE_LEFT))
00180 IMPLEMENT_OPPATHNUDGE_CLASS (LeftFifth,-0.2,0,_R(IDS_UNDO_NUDGE_LEFT))
00181 IMPLEMENT_OPPATHNUDGE_CLASS (LeftPixel1 ,-1,0,_R(IDS_UNDO_NUDGE_LEFT))
00182 IMPLEMENT_OPPATHNUDGE_CLASS (LeftPixel10,-10,0,_R(IDS_UNDO_NUDGE_LEFT))
00183 
00184 
00185 IMPLEMENT_OPPATHNUDGE_CLASS (Right1,1,0,_R(IDS_UNDO_NUDGE_RIGHT))
00186 IMPLEMENT_OPPATHNUDGE_CLASS (Right5,5,0,_R(IDS_UNDO_NUDGE_RIGHT))
00187 IMPLEMENT_OPPATHNUDGE_CLASS (Right10,10,0,_R(IDS_UNDO_NUDGE_RIGHT))
00188 IMPLEMENT_OPPATHNUDGE_CLASS (RightFifth,0.2,0,_R(IDS_UNDO_NUDGE_RIGHT))
00189 IMPLEMENT_OPPATHNUDGE_CLASS (RightPixel1,1,0,_R(IDS_UNDO_NUDGE_RIGHT))
00190 IMPLEMENT_OPPATHNUDGE_CLASS (RightPixel10,10,0,_R(IDS_UNDO_NUDGE_RIGHT))
00191 
00192 #define new CAM_DEBUG_NEW
00193 
00194 MILLIPOINT  OpPathNudge::NudgeStep   = 2835;    // The default size of a single nudge (1mm)
00195 double      OpPathNudge::X_NudgeFactor = 1;     // Distance nudged in x = NudgeStep * X_NudgeFactor;
00196 double      OpPathNudge::Y_NudgeFactor = 0;     // Distance nudged in y = NudgeStep * Y_NudgeFactor;
00197 
00198 
00199 /********************************************************************************************
00200 
00201 >   void OpPathNudge::Do(OpDescriptor* pOpDesc)
00202 
00203     Author:     Peter_Arnold (Xara Group Ltd) <camelotdev@xara.com>
00204     Created:    14/10/94
00205     Inputs:     pOpDesc = ptr to the op descriptor
00206     Purpose:    The path nudge op's Do() function.
00207     SeeAlso:    OpNudge::Do()
00208 
00209 ********************************************************************************************/
00210 
00211 void OpPathNudge::Do(OpDescriptor* pOpDesc)
00212 {
00213     NudgeStep = OpNudge::GetNudgeStep();
00214     
00215     //  Get scaled pixel size 
00216     FIXED16 ScaledPixelWidth, ScaledPixelHeight;
00217     GetWorkingView()->GetScaledPixelSize(&ScaledPixelWidth, &ScaledPixelHeight) ;
00218     PixelNudge=ScaledPixelWidth.MakeDouble() ;
00219 
00220     // Determine the nudge factors based on the OpDescriptor used to invoke the nudge operation
00221     String OpToken = pOpDesc->Token; 
00222 
00223 if      (OpToken == String(OPTOKEN_NUDGEUPPIXEL1))      { PixelNudgeClass = TRUE; }
00224 else if (OpToken == String(OPTOKEN_NUDGEUPPIXEL10))     { PixelNudgeClass = TRUE; }
00225 
00226 else if (OpToken == String(OPTOKEN_NUDGEDOWNPIXEL1))    { PixelNudgeClass = TRUE; }
00227 else if (OpToken == String(OPTOKEN_NUDGEDOWNPIXEL10))   { PixelNudgeClass = TRUE; }
00228 
00229 else if (OpToken == String(OPTOKEN_NUDGELEFTPIXEL1))    { PixelNudgeClass = TRUE; }
00230 else if (OpToken == String(OPTOKEN_NUDGELEFTPIXEL10))   { PixelNudgeClass = TRUE; }
00231 
00232 else if (OpToken == String(OPTOKEN_NUDGERIGHTPIXEL1))   { PixelNudgeClass = TRUE; }
00233 else if (OpToken == String(OPTOKEN_NUDGERIGHTPIXEL10))  { PixelNudgeClass = TRUE; }
00234 
00235 else if (OpToken == String(OPTOKEN_NUDGEUP1))           { PixelNudgeClass = FALSE; }
00236 else if (OpToken == String(OPTOKEN_NUDGEUP10))          { PixelNudgeClass = FALSE; }
00237 
00238 else if (OpToken == String(OPTOKEN_NUDGEDOWN1))         { PixelNudgeClass = FALSE; }
00239 else if (OpToken == String(OPTOKEN_NUDGEDOWN10))        { PixelNudgeClass = FALSE; }
00240 
00241 else if (OpToken == String(OPTOKEN_NUDGELEFT1))         { PixelNudgeClass = FALSE; }
00242 else if (OpToken == String(OPTOKEN_NUDGELEFT10))        { PixelNudgeClass = FALSE; }
00243 
00244 else if (OpToken == String(OPTOKEN_NUDGERIGHT1))        { PixelNudgeClass = FALSE; }
00245 else if (OpToken == String(OPTOKEN_NUDGERIGHT10))       { PixelNudgeClass = FALSE; }
00246     
00247     
00248     
00249     // Setup the Offset DocCoord to contain the X and Y translation values      
00250     DocCoord Offset;
00251 
00252 if (PixelNudgeClass)
00253 {
00254     Offset.x=(INT32)GetXPixelDisplacement();
00255     Offset.y=(INT32)GetYPixelDisplacement();
00256 }
00257 
00258 else if (!PixelNudgeClass)
00259 {
00260     //TypeCast from double to INT32
00261     Offset.x=(INT32) GetXNudgeDisplacement();
00262     Offset.y=(INT32) GetYNudgeDisplacement();
00263 }
00264 
00265 
00266     SelRange*   Selection = GetApplication()->FindSelection();
00267     Node*       pNode = Selection->FindFirst();
00268     NodePath*   ThisPath = NULL;
00269     BOOL        HaveNudged = FALSE;
00270 
00271     DocRect BoundingBox;
00272     BOOL BoundingBoxValid = FALSE;
00273 
00274     PathVerb*   Verbs = NULL;
00275     PathFlags*  Flags = NULL;
00276     DocCoord*   Coords = NULL;
00277     INT32 NumCoords = 0;
00278     BOOL ok = TRUE;
00279     
00280     // Only start an undo record if there is work to do!
00281     if (pNode != NULL)
00282         ok = DoStartTransOp(FALSE, NULL);
00283 
00284     while (ok && pNode!=NULL)
00285     {
00286         //scan tree for all selected paths
00287         if  ( IS_A(pNode,NodePath) && (((NodePath*)pNode)->InkPath.IsSubSelection()) )
00288         {
00289             // for convenience, cast the pointer to a pointer to a NodePath
00290             ThisPath = (NodePath*)pNode;
00291 
00292             // First get pointers to the arrays
00293             ThisPath->InkPath.GetPathArrays(&Verbs, &Coords, &Flags);
00294             NumCoords = ThisPath->InkPath.GetNumCoords();
00295             BOOL ok = TRUE;
00296 
00297             for (INT32 i=0; i<NumCoords && ok; i++)
00298             {
00299                 //  scan path for all selected endpoints
00300                 if (Flags[i].IsEndPoint && Flags[i].IsSelected)
00301                 {   
00302                     DocCoord NewCoord = Coords[i] + Offset;
00303                     ok = DoAlterPathElement(ThisPath, i, NewCoord, Flags[i], Verbs[i]);
00304                     HaveNudged = TRUE;
00305 
00306                     if (BoundingBoxValid)
00307                     {
00308                         BoundingBox.IncludePoint(NewCoord);
00309                     }
00310                     else
00311                     {
00312                         BoundingBox = DocRect(NewCoord, NewCoord);
00313                         BoundingBoxValid = TRUE;
00314                     }
00315 
00316                     // Also need to nudge the associated control points if the rotate flag is set
00317                     // Jim 31/8/96 - Changed to nudge control points always, not just when rotate flag set
00318                     // Exeption Don't do the selected moveto in a closed subpath - the end of the 
00319                     // will alsobe selected, that will handle this situation
00320                     if (!(Verbs[i]== PT_MOVETO && ThisPath->InkPath.IsSubPathClosed(i)))
00321                     {
00322                         INT32 PrevIndex = ThisPath->InkPath.FindPrevControlPoint(i);
00323                         if (PrevIndex!=-1 && ok)
00324                         {
00325                             DocCoord NewCoord = Coords[PrevIndex] + Offset;
00326                             ok = DoAlterPathElement(ThisPath, PrevIndex, NewCoord, Flags[PrevIndex], Verbs[PrevIndex]);
00327                         }
00328 
00329                         INT32 NextIndex = ThisPath->InkPath.FindNextControlPoint(i);
00330                         if (NextIndex!=-1 && ok)
00331                         {
00332                             DocCoord NewCoord = Coords[NextIndex] + Offset;
00333                             ok = DoAlterPathElement(ThisPath, NextIndex, NewCoord, Flags[NextIndex], Verbs[NextIndex]);
00334                         }
00335                     }
00336                 }
00337             }
00338         }
00339 
00340         pNode = Selection->FindNext(pNode);
00341     }
00342 
00343     if (ok && !HaveNudged)
00344     {
00345         // No points were selected so we need to move all the selected paths
00346         pNode = Selection->FindFirst();
00347 
00348         while (ok && pNode!=NULL)
00349         {
00350             if (IS_A(pNode,NodePath))
00351             {
00352                 // Construct the translation matrix
00353                 Trans2DMatrix* TransMat = new Trans2DMatrix(Offset.x, Offset.y);
00354 
00355                 // And apply it to the path
00356                 ok = DoTransformNode((NodeRenderableInk*)pNode, TransMat);
00357             }
00358             pNode = Selection->FindNext(pNode);
00359         }
00360 
00361         if (ok)
00362         {
00363             BoundingBox = Selection->GetBoundingRect();
00364             BoundingBoxValid = TRUE;
00365         }
00366     }
00367     
00368     if (ok)
00369     {
00370         if (BoundingBoxValid)
00371         {
00372             DocView* pDocView = DocView::GetSelected();
00373             if (pDocView != NULL)
00374             {
00375                 pDocView->ScrollToShow(&BoundingBox, Offset);
00376             }
00377         }
00378     }
00379     else
00380     {
00381         InformError();
00382         FailAndExecute();
00383     }
00384 
00385     End();
00386 }
00387 
00388 /********************************************************************************************
00389 
00390 >   void OpPathNudge::GetOpName(String_256* OpName) 
00391 
00392     Author:     Peter_Arnold (Xara Group Ltd) <camelotdev@xara.com>
00393     Created:    8/9/94
00394     Outputs:    The undo string for the operation
00395     Purpose:    The GetOpName fn is overridden so that we return back a description 
00396                 appropriate to the type of attribute that the operation applies. 
00397     SeeAlso:    OpNudge::GetOpName()
00398 
00399 ********************************************************************************************/
00400 
00401 void OpPathNudge::GetOpName(String_256* OpName) 
00402 { 
00403     *OpName = String_256(NudgeUndoIDS);
00404 }  
00405 
00406 
00407 
00408 /********************************************************************************************
00409 >   virtual void OpPathNudge::PerformMergeProcessing()
00410 
00411     Author:     Peter_Arnold (Xara Group Ltd) <camelotdev@xara.com>
00412     Created:    22/1/96
00413     Inputs:     -
00414     Outputs:    -
00415     Returns:    -
00416     Purpose:    If the previous operation was also a path nudge the the undo actions are merged
00417                 to form one undo step.
00418     SeeAlso:    UndoableOperation::PerformMergeProcessing
00419 ********************************************************************************************/
00420 void OpPathNudge::PerformMergeProcessing()
00421 {
00422     if (pOurDoc==NULL)
00423     {
00424         ERROR3("This operation had no document pointer"); 
00425         return;
00426     }
00427 
00428     // Obtain a pointer to the operation history for the current document
00429     OperationHistory* pOpHist = &pOurDoc->GetOpHistory();
00430     if (pOpHist==NULL)
00431     {
00432         ERROR3("This document has no operation history"); 
00433         return;
00434     }
00435     ERROR3IF(pOpHist->FindLastOp() != this, "Last Op should be this op"); 
00436     
00437     // Is operation performed before this a path nudge?  Attempt a merge if so
00438     Operation* pPrevOp = pOpHist->FindPrevToLastOp();
00439     if ((pPrevOp != NULL) && pPrevOp->IS_KIND_OF(OpPathNudge))
00440     {
00441         BOOL PerformMerge = TRUE;
00442         
00443         // Count the number of actions.  They must be the same to merge.
00444         UINT32 NumPrevActions = pPrevOp->GetUndoActionList()->GetCount();
00445         UINT32 NumOurActions = GetUndoActionList()->GetCount();
00446         PerformMerge = (NumPrevActions == NumOurActions);
00447 
00448         // Compare the actions to check we are operating on the same points on the same paths
00449         if (PerformMerge)
00450         {
00451             Action* pPrevAction = (Action*)pPrevOp->GetUndoActionList()->GetHead();
00452             Action* pOurAction = (Action*)GetUndoActionList()->GetHead();
00453 
00454             while (PerformMerge && pPrevAction!=NULL && pOurAction!=NULL)
00455             {
00456                 // Actions must be of the same class
00457                 PerformMerge = (pPrevAction->GetRuntimeClass() == pOurAction->GetRuntimeClass());
00458 
00459                 // ModifyElementActions must reference the same path and index
00460                 if (PerformMerge && IS_A(pPrevAction, ModifyElementAction))
00461                 {
00462                     const ModifyElementAction* pPrevModify = (ModifyElementAction*)pPrevAction;
00463                     const ModifyElementAction* pOurModify = (ModifyElementAction*)pOurAction;
00464 
00465                     PerformMerge = (pPrevModify->GetActionPath()==pOurModify->GetActionPath() &&
00466                                         pPrevModify->GetActionIndex()==pOurModify->GetActionIndex());
00467                 }
00468                                 
00469                 // If the nudge has used TransformNodeActions don't merge to avoid
00470                 // redraw problems
00471                 if (PerformMerge && IS_A(pPrevAction, TransformNodeAction))
00472                     PerformMerge = FALSE;
00473 
00474                 pPrevAction = (Action*)pPrevOp->GetUndoActionList()->GetNext(pPrevAction);
00475                 pOurAction = (Action*)GetUndoActionList()->GetNext(pOurAction);
00476             }
00477 
00478             // Perform the merge
00479             if (PerformMerge)
00480             {
00481                 // In order to merge these two ops we can just throw away this op!
00482                 pOpHist->DeleteLastOp(); 
00483             }
00484         }
00485     }
00486 }

Generated on Sat Nov 10 03:46:26 2007 for Camelot by  doxygen 1.4.4