00001 // $Id: opsmooth.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 file for operation to smooth whole paths 00099 // or between selected points 00100 00101 /* 00102 */ 00103 00104 #include "camtypes.h" 00105 //#include "resource.h" 00106 //#include "barsdlgs.h" 00107 //#include "mike.h" 00108 //#include "ops.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00109 #include "opsmooth.h" 00110 //#include "app.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00111 //#include "document.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00112 #include "nodepath.h" 00113 #include "ophist.h" 00114 //#include "jim.h" 00115 00116 DECLARE_SOURCE("$Revision: 1282 $"); 00117 CC_IMPLEMENT_DYNCREATE( OpSmoothSelection, SelOperation ) 00118 00119 #define new CAM_DEBUG_NEW 00120 00121 00122 /******************************************************************************************** 00123 00124 > OpSmoothSelection::OpSmoothSelection() 00125 00126 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00127 Created: 20/10/94 00128 Purpose: OpSmoothSelection() constructor 00129 SeeAlso: - 00130 00131 ********************************************************************************************/ 00132 00133 OpSmoothSelection::OpSmoothSelection() 00134 { 00135 // Dummy constructor 00136 } 00137 00138 00139 00140 /******************************************************************************************** 00141 00142 > BOOL OpSmoothSelection::Init() 00143 00144 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00145 Created: 26/7/94 00146 Inputs: - 00147 Outputs: - 00148 Returns: TRUE if the operation could be successfully initialised 00149 FALSE if no more memory could be allocated 00150 00151 Purpose: OpDeletePoints initialiser method 00152 Errors: ERROR will be called if there was insufficient memory to allocate the 00153 operation. 00154 SeeAlso: - 00155 00156 ********************************************************************************************/ 00157 00158 BOOL OpSmoothSelection::Init() 00159 { 00160 return (RegisterOpDescriptor(0, // tool ID 00161 _R(IDS_SMOOTHSELECTIONOP), // string resource ID 00162 CC_RUNTIME_CLASS(OpSmoothSelection),// runtime class for Op 00163 OPTOKEN_SMOOTHSELECTION, // Ptr to token string 00164 OpSmoothSelection::GetState, // GetState function 00165 0, // help ID 00166 _R(IDBBL_SMOOTHSELECTIONOP), // bubble help ID 00167 _R(IDD_BARCONTROLSTORE), // resource ID 00168 _R(IDC_BTN_SMOOTHSELECTIONOP), // control ID 00169 SYSTEMBAR_EDIT, // Group bar ID 00170 TRUE, // Receive messages 00171 FALSE, // Smart 00172 FALSE, // Clean 00173 0, // One open Instance ID 00174 (GREY_WHEN_NO_CURRENT_DOC | GREY_WHEN_NO_SELECTION) 00175 )); 00176 00177 } 00178 00179 00180 /******************************************************************************************** 00181 00182 > OpState OpSmoothSelection::GetState(String_256*, OpDescriptor*) 00183 00184 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00185 Created: 20/10/94 00186 Inputs: - 00187 Outputs: - 00188 Returns: The state of the OpSmoothSelection 00189 Purpose: For finding the OpSmoothSelection state. 00190 Errors: - 00191 SeeAlso: - 00192 00193 ********************************************************************************************/ 00194 00195 OpState OpSmoothSelection::GetState(String_256* UIDescription, OpDescriptor*) 00196 { 00197 OpState OpSt; 00198 String_256 DisableReason; 00199 00200 OpSt.Greyed = FALSE; 00201 BOOL FoundSelected = FALSE; 00202 00203 // Go through the selection until we find a selected point 00204 00205 SelRange* Selected = GetApplication()->FindSelection(); 00206 Node* pNode = Selected->FindFirst(); 00207 00208 while (pNode) 00209 { 00210 if (pNode->GetRuntimeClass() == CC_RUNTIME_CLASS(NodePath)) 00211 { 00212 if (((NodePath*)pNode)->InkPath.NumSplinters() >0 ) 00213 { 00214 FoundSelected = TRUE; 00215 break; 00216 } 00217 00218 } 00219 pNode = Selected->FindNext(pNode); 00220 } 00221 00222 // The operation is disabled if there are no complex paths selected 00223 00224 if (!FoundSelected) 00225 { 00226 OpSt.Greyed = TRUE; 00227 DisableReason = String_256(_R(IDS_NEEDS_SELECTED_POINT)); 00228 *UIDescription = DisableReason; 00229 } 00230 00231 return(OpSt); 00232 } 00233 00234 00235 00236 /******************************************************************************************** 00237 00238 > void OpSmoothSelection::GetOpName(String_256* OpName) 00239 00240 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00241 Created: 20/10/94 00242 Inputs: - 00243 Outputs: The undo string for the operation 00244 Returns: 00245 Purpose: The GetOpName fn is overridden so that we return back a description 00246 appropriate to the type of attribute that the operation applies. 00247 00248 Errors: - 00249 SeeAlso: - 00250 00251 ********************************************************************************************/ 00252 00253 void OpSmoothSelection::GetOpName(String_256* OpName) 00254 { 00255 *OpName = String_256(_R(IDS_UNDO_SMOOTHSELECTIONOP)); 00256 } 00257 00258 00259 00260 00261 00262 00263 /******************************************************************************************** 00264 00265 > void OpSmoothSelection::Do(OpDescriptor* opDesc) 00266 00267 Author: Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com> 00268 Created: 20/10/94 00269 Inputs: pointer to an OpDescriptor (unused) 00270 Outputs: - 00271 Returns: - 00272 Purpose: This operation will smooth all selected paths within the current document 00273 Before smoothing any complete path, the path is searched for selected points 00274 The action of the smooth given a number of selected points is as follows 00275 No sel points: Smooth the whole path 00276 One sel point: Smooth the whole path. 00277 Could smooth from the start of the path to this point but 00278 the user does not generally known which part is the start 00279 of the path. 00280 Two sel points: Smooth the section of path lying between these points. 00281 Even number: Smooth using inside/outside section processing. 00282 (a,b) (c,d) .... (y,z) etc are regions for smoothing 00283 Odd number: Smooth as for even number apart from last region 00284 (a,b) (c,d) ........ (f,g,h) 00285 Errors: failandexecute will be called if the operation fails in some way, most 00286 likely when no memory is available. 00287 00288 ********************************************************************************************/ 00289 00290 void OpSmoothSelection::Do(OpDescriptor*) 00291 { 00292 // Obtain the current selections 00293 SelRange* Selected = GetApplication()->FindSelection(); 00294 NodePath* pSmoothNode; 00295 00296 // Now, because we're going to be doing mad things to the selection, we have to make a list 00297 // of all the selected nodes, so that adding nodes into the tree won't confuse us 00298 00299 List* NodeList = Selected->MakeListOfNodes(); 00300 NodeListItem* CurItem = (NodeListItem*)(NodeList->GetHead()); 00301 00302 if (!CurItem) 00303 goto FailAndDeleteList; 00304 00305 // remember the selection before the operation 00306 if (!DoStartSelOp(FALSE,FALSE)) 00307 goto FailAndDeleteList; 00308 00309 while (CurItem) 00310 { 00311 if (CurItem->pNode->GetRuntimeClass() == CC_RUNTIME_CLASS(NodePath)) 00312 { 00313 // get a pointer to the NodePath 00314 NodePath* pThisNode = (NodePath*)(CurItem->pNode); 00315 00316 BOOL ok; 00317 Node* pnode; 00318 00319 // Copy the nodepath and all its children, without placing the copy in the tree 00320 CALL_WITH_FAIL(pThisNode->NodeCopy(&pnode), this, ok); 00321 if (!ok) goto DeleteList; 00322 pSmoothNode = (NodePath*)pnode; 00323 00324 // ok, smooth the nodepaths path data 00325 double error = SMOOTH_CONSTANT; 00326 CALL_WITH_FAIL(pSmoothNode->InkPath.SmoothRegions(error, FALSE, TRUE), this, ok); 00327 if (!ok) goto DeleteListAndPath; 00328 00329 // update the smoothed paths bounding rectangle 00330 pSmoothNode->InvalidateBoundingRect(); 00331 pSmoothNode->InkPath.ClearSubSelection(); 00332 00333 // Now stick the new path into the tree 00334 CALL_WITH_FAIL(DoInsertNewNode(pSmoothNode, pThisNode, NEXT, TRUE), this, ok); 00335 if (!ok) goto DeleteListAndPath; 00336 00337 // Now we've formed a smoothed path, let's hide the original 00338 CALL_WITH_FAIL(DoHideNode(pThisNode,TRUE), this, ok); 00339 if (!ok) goto DeleteList; 00340 00341 } 00342 CurItem = (NodeListItem*)(NodeList->GetNext(CurItem)); 00343 } 00344 00345 End(); 00346 00347 // delete the nodelist (and all the list items) 00348 while (!NodeList->IsEmpty()) 00349 delete (NodeListItem*)(NodeList->RemoveHead()); 00350 delete NodeList; 00351 return; 00352 00353 00354 FailAndDeleteList: 00355 while (!NodeList->IsEmpty()) 00356 delete (NodeListItem*)(NodeList->RemoveHead()); 00357 delete NodeList; 00358 FailAndExecute(); 00359 End(); 00360 return; 00361 00362 DeleteListAndPath: 00363 pSmoothNode->CascadeDelete(); 00364 delete pSmoothNode; 00365 DeleteList: 00366 while (!NodeList->IsEmpty()) 00367 delete (NodeListItem*)(NodeList->RemoveHead()); 00368 delete NodeList; 00369 End(); 00370 return; 00371 00372 }