fraclist.cpp

Go to the documentation of this file.
00001 // $Id: fraclist.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 // Kernel level parts of bitmap lists
00100 
00101 /*
00102 */
00103 
00104 #include "camtypes.h"
00105 //#include "fraclist.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00106 //#include "list.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00107 //#include "errors.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00108 //#include "fillattr.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00109 //#include "app.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00110 #include "bitmpinf.h"
00111 #include "fillattr2.h"
00112 
00113 CC_IMPLEMENT_MEMDUMP( CachedFractal, ListItem )
00114 CC_IMPLEMENT_MEMDUMP( GlobalFractalList, List )
00115 
00116 // Declare smart memory handling in Debug builds
00117 #define new CAM_DEBUG_NEW
00118 
00119 /**********************************************************************************************
00120 
00121 >   CachedFractal::CachedFractal()
00122 
00123     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00124     Created:    14/12/94
00125     Inputs:     -
00126     Outputs:    -
00127     Returns:    -
00128     Purpose:    CachedFractal constructor
00129 
00130 **********************************************************************************************/
00131 
00132 CachedFractal::CachedFractal()
00133 {
00134     Fractal     = NULL;
00135     UsageCount  = 0;
00136     m_bIsFake   = FALSE;
00137 }
00138 
00139 /**********************************************************************************************
00140 
00141 >   CachedFractal::~CachedFractal()
00142 
00143     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00144     Created:    14/12/94
00145     Inputs:     -
00146     Outputs:    -
00147     Returns:    -
00148     Purpose:    CachedFractal destructor
00149 
00150 **********************************************************************************************/
00151 
00152 CachedFractal::~CachedFractal()
00153 {
00154     if (IsFakeFractal())
00155     {
00156         if (Fractal->GetBitmap())
00157         {
00158             delete Fractal->GetBitmapRef()->GetBitmap();
00159             Fractal->GetBitmapRef()->SetBitmap(NULL);
00160         }
00161 
00162         delete Fractal;
00163         return;
00164     }
00165     
00166     if (UsageCount > 0)
00167         TRACEUSER( "Mike", _T("Deleting cached fractal while it's still in use\n"));
00168 
00169     if (Fractal)
00170     {
00171         GlobalFractalList* pFracList = GetApplication()->GetGlobalFractalList();
00172         BOOL StillUsed = FALSE;
00173 
00174         if (pFracList)
00175         {
00176             CachedFractal* pCachedFractal = (CachedFractal*)pFracList->GetHead();
00177 
00178             while (pCachedFractal != NULL)
00179             {
00180                 OILBitmap* pBmp = NULL;
00181                 if (pCachedFractal->GetBitmap())
00182                     pBmp = pCachedFractal->GetBitmap()->ActualBitmap;
00183 
00184                 // Are the bitmaps the same ?
00185                 if (Fractal->GetBitmap() && 
00186                     Fractal->GetBitmap()->ActualBitmap == pBmp )
00187                 {
00188                     // This fractal bitmap is still being used
00189                     StillUsed = TRUE;
00190                     break;
00191                 }
00192 
00193                 pCachedFractal = (CachedFractal*)pFracList->GetNext(pCachedFractal);
00194             }   
00195         }
00196 
00197         if (Fractal->GetBitmap())
00198         {
00199             // Only delete the actual bitmap, if it's not used for any other fractals
00200             if (!StillUsed) 
00201             {
00202                 delete Fractal->GetBitmapRef()->GetBitmap()->ActualBitmap;
00203                 Fractal->GetBitmapRef()->GetBitmap()->ActualBitmap = NULL;
00204             }
00205 
00206             delete Fractal->GetBitmapRef()->GetBitmap();
00207             Fractal->GetBitmapRef()->SetBitmap(NULL);
00208         }
00209 
00210         delete Fractal;
00211     }
00212 }
00213 
00214 /**********************************************************************************************
00215 
00216 >   INT32 CachedFractal::operator==(const FillGeometryAttribute& Attrib)
00217 
00218     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00219     Created:    14/12/94
00220     Inputs:     -
00221     Outputs:    -
00222     Returns:    -
00223     Purpose:    -
00224 
00225 **********************************************************************************************/
00226 
00227 INT32 CachedFractal::operator==(const FillGeometryAttribute& Attrib)
00228 {
00229     FillGeometryAttribute* CachedFrac = GetCachedFractal();
00230     FillGeometryAttribute* OtherFrac = (FillGeometryAttribute*)&Attrib;
00231 
00232     if (OtherFrac->GetRuntimeClass() != CachedFrac->GetRuntimeClass())
00233         return FALSE;
00234 
00235     return ( OtherFrac->IsSameAsCachedFractal(CachedFrac) );
00236 }
00237 
00238 /**********************************************************************************************
00239 
00240 >   void CachedFractal::SetCachedFractal(FillGeometryAttribute* NewFrac)
00241 
00242     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00243     Created:    14/12/94
00244     Inputs:     -
00245     Outputs:    -
00246     Returns:    -
00247     Purpose:    -
00248 
00249 **********************************************************************************************/
00250 
00251 void CachedFractal::SetCachedFractal(FillGeometryAttribute* NewFrac)
00252 {
00253     CCRuntimeClass* ObjectType = NewFrac->GetRuntimeClass();
00254     FillGeometryAttribute* FracClone = (FillGeometryAttribute*)ObjectType->CreateObject();
00255 
00256     NewFrac->CacheFractalData(FracClone);
00257 
00258     KernelBitmap* Bmp = new KernelBitmap(NewFrac->GetBitmap()->ActualBitmap, TRUE);
00259     FracClone->GetBitmapRef()->SetBitmap(Bmp);
00260 //  Bmp->IncUsageCount();
00261 
00262     Fractal = FracClone;
00263 }
00264 
00265 /**********************************************************************************************
00266 
00267 >   FillGeometryAttribute* CachedFractal::GetCachedFractal()
00268 
00269     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00270     Created:    14/12/94
00271     Inputs:     -
00272     Outputs:    -
00273     Returns:    -
00274     Purpose:    -
00275 
00276 **********************************************************************************************/
00277 
00278 FillGeometryAttribute* CachedFractal::GetCachedFractal()
00279 {
00280     return Fractal;
00281 }
00282 
00283 /**********************************************************************************************
00284 
00285 >   void CachedFractal::IncUsageCount()
00286 
00287     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00288     Created:    14/12/94
00289     Outputs:    Increaments the usage count of this cached fractal.
00290     Purpose:    -
00291 
00292 **********************************************************************************************/
00293 
00294 void CachedFractal::IncUsageCount()
00295 {
00296     UsageCount++;
00297     TRACEUSER( "Mike", _T("Inc Cached Fractal @ %x, Usage to %d\n"),this, UsageCount);
00298 }
00299 
00300 /**********************************************************************************************
00301 
00302 >   void CachedFractal::DecUsageCount()
00303 
00304     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00305     Created:    14/12/94
00306     Outputs:    Decreaments the usage count of this cached fractal.
00307     Purpose:    -
00308 
00309 **********************************************************************************************/
00310 
00311 void CachedFractal::DecUsageCount()
00312 {
00313     UsageCount--;
00314     TRACEUSER( "Mike", _T("Dec Cached Fractal @ %x, Usage to %d\n"),this, UsageCount);
00315 }
00316 
00317 /**********************************************************************************************
00318 
00319 >   INT32 CachedFractal::GetSize()
00320 
00321     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00322     Created:    14/12/94
00323     Returns:    The memory used by this fractal.
00324     Purpose:    -
00325 
00326 **********************************************************************************************/
00327 
00328 INT32 CachedFractal::GetSize()
00329 {
00330     ERROR2IF(Fractal->GetBitmap() == NULL ||
00331              Fractal->GetBitmap()->ActualBitmap == NULL, 0, "GetSize called when bitmap is NULL");
00332 
00333     BitmapInfo Info;
00334     Info.MemoryUsed = 0;
00335 
00336     if (Fractal->GetBitmap() &&
00337         Fractal->GetBitmap()->ActualBitmap)
00338     {
00339         Fractal->GetBitmap()->ActualBitmap->GetInfo(&Info);
00340     }
00341 
00342     return Info.MemoryUsed;
00343 }
00344 
00345 /**********************************************************************************************
00346 
00347 >   KernelBitmap* CachedFractal::GetBitmap()
00348 
00349     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00350     Created:    26/8/96
00351     Returns:    The fractal bitmap 
00352     Purpose:    -
00353 
00354 **********************************************************************************************/
00355 
00356 KernelBitmap* CachedFractal::GetBitmap()
00357 {
00358     if (Fractal == NULL)
00359         return NULL;
00360 
00361     return Fractal->GetBitmap();
00362 }
00363 
00364 /**********************************************************************************************
00365 
00366 >   GlobalBitmapList::GlobalFractalList()
00367 
00368     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00369     Created:    14/12/94
00370     Inputs:     -
00371     Outputs:    -
00372     Returns:    -
00373     Purpose:    GlobalFractalList constructor
00374 
00375 **********************************************************************************************/
00376 
00377 GlobalFractalList::GlobalFractalList()
00378 {
00379 }
00380 
00381 /**********************************************************************************************
00382 
00383 >   GlobalFractalList::~GlobalFractalList()
00384 
00385     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00386     Created:    14/12/94
00387     Inputs:     -
00388     Outputs:    -
00389     Returns:    -
00390     Purpose:    GlobalFractalList destructor
00391 
00392 **********************************************************************************************/
00393 
00394 GlobalFractalList::~GlobalFractalList()
00395 {
00396     if (!IsEmpty())
00397     {
00398         TRACEALL( _T("Trying to delete global fractal list when it's not empty !!\n") );
00399         Destroy();
00400     }
00401 }
00402 
00403 /**********************************************************************************************
00404 
00405 >   void GlobalFractalList::Destroy()
00406 
00407     Author:     Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com>
00408     Created:    18/2/97
00409     Inputs:     -
00410     Outputs:    -
00411     Returns:    -
00412     Purpose:    Destroy the contents of the fractal list
00413 
00414 **********************************************************************************************/
00415 
00416 void GlobalFractalList::Destroy()
00417 {
00418     ListItem* Fractal;
00419     while ((Fractal=RemoveHead())!=NULL)
00420         delete Fractal;
00421 }
00422 
00423 
00424 /**********************************************************************************************
00425 
00426 >   BOOL GlobalFractalList::Init()
00427 
00428     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00429     Created:    6/2/95
00430     Returns:    FALSE if failed.
00431     Purpose:    Initialise the GlobalFractalList
00432 
00433 **********************************************************************************************/
00434 
00435 BOOL GlobalFractalList::Init()
00436 {
00437     return TRUE;
00438 }
00439 
00440 /**********************************************************************************************
00441 
00442 >   static BOOL GlobalFractalList::DeInit()
00443 
00444     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00445     Created:    6/2/95
00446     Returns:    FALSE if failed.
00447     Purpose:    DeInitialise the GlobalFractalList. Static function so it finds the global
00448                 fractal list first
00449 
00450 **********************************************************************************************/
00451 
00452 BOOL GlobalFractalList::DeInit()
00453 {
00454     GlobalFractalList* FracList = GetApplication()->GetGlobalFractalList();
00455     if (FracList)
00456         FracList->Destroy();
00457     return TRUE;
00458 }
00459 
00460 /**********************************************************************************************
00461 
00462 >   BOOL GlobalFractalList::AddFractal(FillGeometryAttribute* NewFractal)
00463 
00464     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00465     Created:    14/12/94
00466     Returns:    FALSE if this fractal already exits.
00467     Purpose:    Adds an item to the tail of the GlobalFractallist if it doesn't already
00468                 exist.
00469 
00470 **********************************************************************************************/
00471 
00472 BOOL GlobalFractalList::AddFractal(FillGeometryAttribute* NewFractal)
00473 {
00474     CachedFractal* ExistingFrac = CheckFractalBitmap(NewFractal);
00475 
00476     if (ExistingFrac != NULL)
00477     {
00478         ExistingFrac->IncUsageCount();
00479         return FALSE;
00480     }
00481 
00482     CachedFractal* Fractal = new CachedFractal();
00483 
00484     if (Fractal == NULL)
00485         return FALSE;
00486 
00487     TRACEUSER( "Mike", _T("Adding Cached Fractal @ %x\n"),Fractal);
00488 
00489     Fractal->SetCachedFractal(NewFractal);
00490     Fractal->IncUsageCount();
00491 
00492     AddTail((ListItem*)Fractal);
00493 
00494     if (this != GetApplication()->GetGlobalFractalList())
00495         Fractal->MakeFakeFractal();
00496 
00497     TRACEUSER( "Mike", _T("Cached Fractal Count = %d\n"),GetFractalCacheCount());
00498     TRACEUSER( "Mike", _T("Cached Fractal Size  = %d\n"),GetFractalCacheSize());
00499 
00500     return(TRUE);
00501 }
00502 
00503 /**********************************************************************************************
00504 
00505 >   BOOL GlobalFractalList::RemoveFractal(FillGeometryAttribute* Frac)
00506 
00507     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00508     Created:    14/12/94
00509     Returns:    TRUE if the fractal was really removed (or doesn't exist)
00510     Purpose:    Called when a fractal is destroyed.
00511 
00512 **********************************************************************************************/
00513 
00514 BOOL GlobalFractalList::RemoveFractal(FillGeometryAttribute* Frac)
00515 {
00516     CachedFractal* ExistingFrac = CheckFractalBitmap(Frac);
00517 
00518     if (ExistingFrac == NULL)
00519         return TRUE;        // This will be the case when we delete the cached fractal
00520 
00521     ExistingFrac->DecUsageCount();
00522 
00523     if (ExistingFrac->GetUsageCount() <= 0)
00524     {
00525         TRACEUSER( "Mike", _T("Removing Cached Fractal @ %x\n"),ExistingFrac);
00526         RemoveItem((ListItem*)ExistingFrac);
00527         delete ExistingFrac;
00528 
00529         return TRUE;
00530     }
00531 
00532     TRACEUSER( "Mike", _T("Cached Fractal Count = %d\n"),GetFractalCacheCount());
00533     TRACEUSER( "Mike", _T("Cached Fractal Size  = %d\n"),GetFractalCacheSize());
00534 
00535     return FALSE;   // The fractal is still there
00536 }
00537 
00538 /**********************************************************************************************
00539 
00540 >   CachedFractal* GlobalFractalList::CheckFractalBitmap(FillGeometryAttribute*)
00541 
00542     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00543     Created:    14/12/94
00544     Returns:    The existing Fractal if one exists.
00545     Purpose:    Checks to see if this fractal fill is already in the list.
00546 
00547 **********************************************************************************************/
00548 
00549 CachedFractal* GlobalFractalList::CheckFractalBitmap(FillGeometryAttribute* FractalFill)
00550 {
00551     ListItem* Fractal = GetHead();
00552     while (Fractal!=NULL)
00553     {
00554         // Are the fractals the same ?
00555         if ( *((CachedFractal*)Fractal) == *FractalFill )
00556             return ((CachedFractal*)Fractal);
00557         
00558         Fractal = GetNext(Fractal);
00559     }   
00560     return NULL;
00561 }
00562 
00563 /**********************************************************************************************
00564 
00565 >   INT32 GlobalFractalList::GetFractalCacheCount()
00566 
00567     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00568     Created:    30/3/94
00569     Returns:    The total number of cached fractals.
00570     Purpose:    Calculates the total number of cached fractals.
00571 
00572 **********************************************************************************************/
00573 
00574 INT32 GlobalFractalList::GetFractalCacheCount()
00575 {
00576     return GetCount();
00577 }
00578 
00579 /**********************************************************************************************
00580 
00581 >   INT32 GlobalFractalList::GetFractalCacheSize()
00582 
00583     Author:     Will_Cowling (Xara Group Ltd) <camelotdev@xara.com>
00584     Created:    30/3/94
00585     Returns:    The total memory used by the cached fractals.
00586     Purpose:    Calculates the total memory used by the cached fractals.
00587 
00588 **********************************************************************************************/
00589 
00590 INT32 GlobalFractalList::GetFractalCacheSize()
00591 {
00592     INT32 FracSize = 0;
00593     ListItem* Fractal = GetHead();
00594     while (Fractal != NULL)
00595     {
00596         FracSize += ((CachedFractal*)Fractal)->GetSize();
00597         Fractal = GetNext(Fractal);
00598     }   
00599     return FracSize;
00600 }
00601 
00602 
00603 /**********************************************************************************************
00604 
00605 >   void GlobalFractalList::GetDocumentFractalData(Document* pDoc, EnumFractalData *pData)
00606 
00607     Author:     Mike_Kenny (Xara Group Ltd) <camelotdev@xara.com>
00608     Created:    30/3/94
00609     Inputs:     pDoc  = a pointer to a document
00610                 pData = a pointer to a structure to fill
00611     Outputs:    pData is updated with information about the fractals used in this document
00612     Returns:    -
00613     Purpose:    Updates the fields in the EnumFractalData with information about fractals
00614                 in the specified document
00615 
00616 **********************************************************************************************/
00617 
00618 void GlobalFractalList::GetDocumentFractalData(Document* pDoc, EnumFractalData *pData)
00619 {
00620     BitmapInfo Info;
00621     GlobalFractalList FoundList;
00622 
00623     if (pDoc != NULL)
00624     {
00625         // Now Scan this document's tree for any references to a bitmap
00626         Node* pNode = Node::DocFindFirstDepthFirst(pDoc);
00627         while (pNode != NULL)
00628         {
00629             // First of all is this a NodeHidden ?
00630             // If so we want to look at the actual hidden node it's pointing at,
00631             // and scan the subtree, for Hidden bitmap references.
00632             if (!pNode->IsNodeHidden())
00633             {
00634                 if (pNode->IsAnAttribute())     // Is this an Attribute ?
00635                 {
00636                     // Is it a Fractal Fill ?
00637                     if (((NodeAttribute*)pNode)->IsAFractalFill())
00638                     {
00639                         AttrFractalFill* pFrac = (AttrFractalFill*)pNode;
00640                         FillGeometryAttribute* pVal = (FillGeometryAttribute*)pFrac->GetAttributeValue();
00641                         BOOL NewFractal = FoundList.AddFractal(pVal);
00642 
00643                         if (NewFractal)
00644                         {
00645                             pData->Count++;
00646 
00647                             if (pVal->GetBitmap() &&
00648                                 pVal->GetBitmap()->ActualBitmap)
00649                             {
00650                                 pVal->GetBitmap()->ActualBitmap->GetInfo(&Info);
00651                                 pData->Size += Info.MemoryUsed;
00652                             }
00653                         }
00654                     }
00655                 }
00656             }
00657 
00658             // Move onto the next node in the tree
00659             pNode = pNode->DocFindNextDepthFirst(); 
00660         }
00661     }
00662 
00663     FoundList.Destroy();
00664 }

Generated on Sat Nov 10 03:45:22 2007 for Camelot by  doxygen 1.4.4