00001 // $Id: bmpexdoc.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 00099 #include "camtypes.h" 00100 00101 #include "bmpexdoc.h" 00102 #include "layer.h" 00103 //#include "spread.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00104 //#include "will2.h" // For layer name 00105 #include "nodebmp.h" 00106 #include "bitmpinf.h" 00107 //#include "fillval.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00108 00109 #include "lineattr.h" // AttrStrokeColour 00110 //#include "fillattr.h" // AttrFillGeometry - in camtypes.h [AUTOMATICALLY REMOVED] 00111 00112 DECLARE_SOURCE("$Revision: 1282 $"); 00113 00114 CC_IMPLEMENT_DYNAMIC(BitmapExportDocument, Document) 00115 00116 #define new CAM_DEBUG_NEW 00117 00118 00119 /******************************************************************************************** 00120 00121 > BitmapExportDocument::BitmapExportDocument(BOOL fHide = TRUE) 00122 00123 Author: Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com> 00124 Created: 11/6/96 00125 Inputs: fHide - whether this document is invisible or not (default is invisible) 00126 Purpose: BitmapExportDocument constructor. 00127 00128 ********************************************************************************************/ 00129 00130 BitmapExportDocument::BitmapExportDocument(BOOL fHide) 00131 : Document(fHide), 00132 pBitmapNode(0) 00133 { 00134 // Empty. 00135 } 00136 00137 00138 00139 /******************************************************************************************** 00140 00141 > BOOL BitmapExportDocument::Init(KernelBitmap* pBitmap, const DocRect& RectToExport) 00142 00143 Author: Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com> 00144 Created: 11/6/96 00145 Inputs: pBitmap - pointer to the kernel bitmap to be inserted into the document 00146 (can't be const 'cos KernelBitmapRef::SetBitmap isn't) 00147 RectToExport - DocRect reference of the rectangle to export 00148 Returns: FALSE if it failed 00149 Purpose: This method initialises the document 00150 It calls down to Document::Init, then adds an insertion Layer node to the 00151 bottom of the basic doc tree. 00152 It then adds a totally transparent NodeRectangle and a NodeBitmap with the 00153 default NodeBitmap attributes referencing the specified KernelBitmap 00154 SeeAlso: Document::Init; Document::InitTree 00155 00156 ********************************************************************************************/ 00157 00158 BOOL BitmapExportDocument::Init(KernelBitmap* pBitmap, const DocRect& RectToExport) 00159 { 00160 // Don't attach any CCamDoc. 00161 if (!Document::Init(0)) return(FALSE); 00162 00163 Node *pSpread = FindFirstSpread(); 00164 ERROR3IF(pSpread == NULL, "No Spread in document!"); 00165 00166 Node *pLayer = pSpread->FindFirstChild(); 00167 ERROR3IF(pLayer == NULL, "No Spread-child in document!"); 00168 00169 // Store away the rectangle 00170 ExportRect = RectToExport; 00171 00172 // Now scan the children of the first spread until we find a layer, or run out of nodes 00173 while (pLayer != NULL && !pLayer->IsLayer()) 00174 pLayer = pLayer->FindNext(); 00175 00176 if (pLayer == NULL) // No Layer, so we'd better add one for ourselves 00177 { 00178 String_256 LayerID = String_256(_R(IDS_K_CLIPINT_LAYERNAME)); 00179 pLayer = new Layer(pSpread, LASTCHILD, LayerID); 00180 if (pLayer == NULL) 00181 return(InitFailed()); 00182 } 00183 00184 // Create a new NodeRect 00185 NodeRect* pRectNode = new NodeRect(pLayer, FIRSTCHILD); 00186 00187 // Failed so cleanup and exit 00188 if (pRectNode == NULL) 00189 return(InitFailed()); 00190 00191 // Initilaise the node 00192 if (!pRectNode->SetUpPath(6,6)) 00193 return(InitFailed()); 00194 00195 // Create the rectangle 00196 pRectNode->CreateShape(ExportRect); 00197 00198 // Give the rectangle a line colour 00199 #if 0 00200 // This memory leaks a StrokeColourAttribute 00201 StrokeColourAttribute* pAttrValue = new StrokeColourAttribute(DocColour(COLOUR_TRANS)); 00202 if (pAttrValue == NULL) 00203 return(InitFailed()); 00204 00205 NodeAttribute* pAttr = pAttrValue->MakeNode(); 00206 if (pAttr == NULL) 00207 return(InitFailed()); 00208 00209 // Attach the attribute to the rectangle 00210 pAttr->AttachNode(pRectNode, FIRSTCHILD); 00211 #else 00212 // Do what ApplyDefaultBitmapAttrs does 00213 Node* pLineColAttr = new AttrStrokeColour(); 00214 if (pLineColAttr == NULL) 00215 return(InitFailed()); 00216 00217 DocColour none(COLOUR_NONE); 00218 ((AttrFillGeometry*)pLineColAttr)->SetStartColour(&none); 00219 pLineColAttr->AttachNode(pRectNode, FIRSTCHILD); 00220 #endif 00221 00222 // Create a NodeBitmap (don't attach it to the tree straight away 'cos we 00223 // have to put the attributes on it first) 00224 00225 pBitmapNode = new NodeBitmap(); 00226 if (pBitmapNode == NULL) 00227 return(InitFailed()); 00228 00229 if (!pBitmapNode->SetUpPath(6,6)) 00230 return(InitFailed()); 00231 00232 pBitmapNode->CreateShape(ExportRect); 00233 00234 if (!SetBitmap(pBitmap)) 00235 return(InitFailed()); 00236 00237 // Set the bitmap's attributes 00238 // This must be done before the NodeBitmap is inserted into the tree 00239 if (!pBitmapNode->ApplyDefaultBitmapAttrs(NULL)) 00240 return(InitFailed()); 00241 00242 // Attach it to the tree as the next sibling of the rectangle 00243 pBitmapNode->AttachNode(pRectNode, NEXT); 00244 00245 // Success... 00246 return(TRUE); 00247 } 00248 00249 00250 00251 /******************************************************************************************** 00252 00253 > BOOL BitmapExportDocument::SetBitmap(KernelBitmap* pBitmap) 00254 00255 Author: Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com> 00256 Created: 12/6/96 00257 Inputs: pBitmap - pointer to the kernel bitmap to be inserted into the document 00258 (can't be const 'cos KernelBitmapRef::SetBitmap isn't) 00259 Returns: FALSE if it failed 00260 Purpose: This function sets the bitmap method initialises the document 00261 It is an internal method, called by Initialise() 00262 It calls down to Document::Init, then adds an insertion Layer node to the 00263 bottom of the basic doc tree. 00264 It then adds a NodeBitmap referencing the specified KernelBitmap 00265 00266 ********************************************************************************************/ 00267 00268 BOOL BitmapExportDocument::SetBitmap(KernelBitmap* pBitmap) 00269 { 00270 TRACEUSER( "Gerry", _T("BitmapExportDocument::SetBitmap()\n")); 00271 00272 ERROR2IF(pBitmap == NULL, FALSE, "NULL bitmap passed to SetBitmap()\n"); 00273 ERROR2IF(pBitmapNode == NULL, FALSE, "pBitmapNode is NULL in SetBitmap()\n"); 00274 00275 // Attach the bitmap to our node 00276 // 00277 // Note: Neville 18/9/97 00278 // If the bitmap is a lone bitmap i.e. not attached to any document, then we 00279 // must attach it properly to this document so that it gets cleaned out 00280 // rather than memory leaking. If not, then we are just temporarily using 00281 // the bitmap and so everything should be ok. We need to work in the following 00282 // situations:- 00283 // - Selecting multiple bitmaps in the bitmap gallery and saving as animated GIF. 00284 // Here we are passed a list of bitmaps which are already in use in the source 00285 // document. 00286 // - Making a new clipart index for bitmaps. Here we are passed a brand spanking 00287 // new bitmap which is not on any lists. This used to memory leak. 00288 BitmapList* pBmpList = pBitmap->GetParentBitmapList(); 00289 if (pBmpList != NULL) 00290 { 00291 // This will memory leak a kernel bitmap each time if it is not on a parent 00292 // document list. You will also end up with a KernelBitmap which contains 00293 // a deleted ActualBitmap pointer, which is very bad! 00294 pBitmapNode->GetBitmapRef()->SetBitmap(pBitmap); 00295 } 00296 else 00297 { 00298 // This is the same technique that the BaseBitmapFilter uses and so must be correct! 00299 pBitmapNode->GetBitmapRef()->Attach(pBitmap, this); 00300 if (pBitmapNode->GetBitmap() != pBitmap) 00301 { 00302 // It didn't use the bitmap we gave it, so we can delete it 00303 delete pBitmap; 00304 } 00305 } 00306 00307 BitmapInfo Info; 00308 pBitmapNode->GetBitmap()->ActualBitmap->GetInfo(&Info); 00309 00310 // Calculate the rectangle for this bitmap 00311 // We make it as large as possible (within the ExportRect) 00312 // without changing the aspect ration 00313 00314 INT32 RectWidth = ExportRect.Width(); 00315 INT32 RectHeight = ExportRect.Height(); 00316 00317 double xScale = (double) RectWidth / (double) Info.RecommendedWidth; 00318 double yScale = (double) RectHeight / (double) Info.RecommendedHeight; 00319 00320 DocRect BitmapRect; 00321 00322 BitmapRect.lo.x = 0; 00323 BitmapRect.lo.y = 0; 00324 00325 if (xScale < yScale) 00326 { 00327 // Bitmap will be full width 00328 BitmapRect.hi.x = RectWidth; 00329 BitmapRect.hi.y = (INT32) ((double) Info.RecommendedHeight * xScale); 00330 00331 // Center the bitmap vertically in the rectangle 00332 BitmapRect.Translate(0, (RectHeight - BitmapRect.Height()) / 2); 00333 } 00334 else 00335 { 00336 // Bitmap will be full height 00337 BitmapRect.hi.x = (INT32) ((double) Info.RecommendedWidth * yScale); 00338 BitmapRect.hi.y = RectHeight; 00339 00340 // Center the bitmap horizontally in the rectangle 00341 BitmapRect.Translate((RectWidth - BitmapRect.Width()) / 2, 0); 00342 } 00343 00344 // Delete the node's path 00345 // Because CreateShape inserts a rectangle at the beginning of the path 00346 pBitmapNode->InkPath.DeleteFromElement(0); 00347 00348 // And create the shape 00349 pBitmapNode->CreateShape(BitmapRect); 00350 00351 return(TRUE); 00352 } 00353 00354 00355 00356 /******************************************************************************************** 00357 00358 > BOOL BitmapExportDocument::InitFailed(void) 00359 00360 Author: Gerry_Iles (Xara Group Ltd) <camelotdev@xara.com> 00361 Created: 12/6/96 00362 Inputs: - 00363 Returns: See Document::InitFailed() 00364 Purpose: This function deletes the NodeBitmap and then calls Document::InitFailed() 00365 SeeAlso: Document::InitFailed 00366 00367 ********************************************************************************************/ 00368 00369 BOOL BitmapExportDocument::InitFailed(void) 00370 { 00371 if (pBitmapNode) 00372 { 00373 delete pBitmapNode; 00374 pBitmapNode = NULL; 00375 } 00376 00377 return(Document::InitFailed()); 00378 }