linecomp.cpp

Go to the documentation of this file.
00001 // $Id: linecomp.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 // Line Component class implementation - abstract sort-of base class from 
00100 // which to derive line components like strokes, brushes.
00101 
00102 #include "camtypes.h"
00103 #include "linecomp.h"
00104 //#include "camfiltr.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00105 //#include "fixmem.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00106 //#include "spread.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00107 #include "layer.h"
00108 //#include "cxfrec.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00109 //#include "cxfrech.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00110 #include "cxftags.h"
00111 
00112 CC_IMPLEMENT_DYNAMIC(LineComponent, DocComponent)
00113 
00114 LineDefinition **LineComponent::m_pLineArray = NULL;
00115 UINT32 LineComponent::m_CurrentSize = 0;
00116 UINT32 LineComponent::m_Used = 0;
00117 
00118 UINT32 LineComponent::m_ImportHandle    = 0;
00119 UINT32 LineComponent::m_ImportFlags = 0;
00120 UINT32 LineComponent::m_ImportData1 = 0;
00121 UINT32 LineComponent::m_ImportData2 = 0;
00122 InsertTreeContext *LineComponent::m_pImportPreviousContext = NULL;
00123 Node *LineComponent::m_pImportNewLine = NULL;
00124 String_32 LineComponent::m_ImportedName;
00125 
00126 /********************************************************************************************
00127 
00128 >   LineComponent::LineComponent()
00129 
00130     Author:     Diccon_Yamanaka (Xara Group Ltd) <camelotdev@xara.com>
00131     Created:    13/12/99
00132 
00133     Purpose:    Constructor
00134 
00135 ********************************************************************************************/
00136 
00137 LineComponent::LineComponent()
00138 {
00139     
00140 }
00141 
00142 
00143 
00144 /********************************************************************************************
00145 
00146 >   LineComponent::~LineComponent()
00147 
00148     Author:     Diccon_Yamanaka (Xara Group Ltd) <camelotdev@xara.com>
00149     Created:    13/12/99
00150 
00151     Purpose:    Destructor
00152 
00153 ********************************************************************************************/
00154 
00155 LineComponent::~LineComponent()
00156 {
00157     if (m_pImportPreviousContext != NULL)
00158         delete m_pImportPreviousContext;
00159     if (m_pImportNewLine != NULL)
00160         delete m_pImportNewLine;
00161 }
00162 
00163 
00164 
00165 /********************************************************************************************
00166 
00167 >   static void LineComponent::DeleteLineList(void)
00168 
00169     Author:     Diccon_Yamanaka (Xara Group Ltd) <camelotdev@xara.com>
00170     Created:    13/12/99
00171 
00172     Purpose:    Static deinitialisation function, called on shutdown.
00173                 Clears out the global Line list. Must be called when all users of the
00174                 Line system (documents) have already been deleted.
00175 
00176 ********************************************************************************************/
00177 
00178 void LineComponent::DeleteLineList(void)
00179 {
00180     if (m_pLineArray != NULL)
00181     {
00182         for (UINT32 i = 0; i < m_Used; i++)
00183         {
00184             if (m_pLineArray[i] != NULL)
00185                 delete m_pLineArray[i];
00186 
00187             m_pLineArray[i] = NULL;
00188         }
00189 
00190         CCFree(m_pLineArray);
00191         m_pLineArray = NULL;
00192     }
00193 }
00194 
00195 
00196 
00197 /********************************************************************************************
00198 
00199 >   static LineHandle LineComponent::AddNewItem(LineDefinition *pLine)
00200 
00201     Author:     Diccon_Yamanaka (Xara Group Ltd) <camelotdev@xara.com>
00202     Created:    13/12/99
00203 
00204     Inputs:     pLine - (may not be NULL) A Line definition to add, which now
00205                           belongs to the LineComponent, so you mustn't delete it!
00206 
00207     Returns:    A handle which uniquely identifies the new Line, or
00208                 LineHandle_NoLine if we ran out of memory for storing Lines
00209 
00210     Purpose:    Adds the given Line to the global Line list.
00211 
00212     Notes:      IMPORTANT!
00213                 The Line definition you pass in belongs to the LineComponent,
00214                 and will be deleted when finished with. 
00215 
00216                 YOU MUST NOT use the pLine pointer after this call! If the call
00217                 fails, or if the Line is merged with an existing one, the object
00218                 you passed in will be IMMEDIATELY DELETED! If you wish to use the
00219                 Line after Adding it, you must call FindLine with the returned
00220                 Line handle!
00221 
00222     Technical:  Lines are recorded in an array of LineDefinition pointers.
00223                 The LineHandle is just an index into this array, for fast lookup.
00224                 When deleted, the array-entry is set NULL and never re-used, so that
00225                 any handles floating around can be safely used (much better than
00226                 getting IndexedColour deleted when in use errors, eh? ;-).
00227                 New entries are thus always appended to the list.
00228 
00229 ********************************************************************************************/
00230 
00231 LineHandle LineComponent::AddNewItem(LineDefinition *pItem, BOOL)
00232 {
00233     // ****!!!!TODO - if we're multi-threadig, this probably needs to be a critical section
00234     // because the list is global
00235 
00236     ERROR3IF(pItem == NULL, "Illegal NULL param");
00237 
00238     // --- First, check if we can merge the given Line with one already in the list
00239     for (UINT32 i = 0; i < m_Used; i++)
00240     {
00241         if (m_pLineArray[i] != NULL && !pItem->IsDifferent(m_pLineArray[i]))
00242         {
00243             // The Line is the same as one we already have in our Line list, so we
00244             // delete the copy that was passed in, and return the existing ones handle
00245             // We copy the import handle value over, though, so that we don't "lose"
00246             // merged Lines during import!
00247             m_pLineArray[i]->SetIOStore(pItem->ReadIOStore());
00248             delete pItem;
00249             return((LineHandle) i);
00250         }
00251     }
00252 
00253     // --- Next, check if we have enough room in our Line array to add a new entry to the end
00254     if (m_Used >= m_CurrentSize)
00255     {
00256         if (!ExpandArray())
00257         {
00258             delete pItem;
00259             return(LineHandle_NoLine);
00260         }
00261     }
00262 
00263     // --- OK, we have a totally new Line, so add it to the end of the list
00264     LineHandle NewHandle = (LineHandle) m_Used;
00265     m_pLineArray[m_Used] = pItem;
00266     m_Used++;
00267 
00268 
00269     // --- And return the new handle
00270     return(NewHandle);
00271 }
00272 
00273 
00274 
00275 /********************************************************************************************
00276 
00277 >   LineDefinition *LineComponent::FindDefinition(UINT32 Handle, BOOL Dummy = FALSE);
00278 
00279     Author:     Diccon_Yamanaka (Xara Group Ltd) <camelotdev@xara.com>
00280     Created:    13/12/99
00281 
00282     Inputs:     Handle - A handle which uniquely identifies the new stroke
00283                     
00284     Returns:    A pointer to a StrokeDefinition for that Stroke.
00285                 If the stroke has been deleted, NULL will be returned, in which case,
00286                 you should gracefully degrade (by rendering an old style line in place of
00287                 the stroke)
00288 
00289     Purpose:    Find a line definition, given its unique identity handle
00290 
00291 ********************************************************************************************/
00292 
00293 LineDefinition *LineComponent::FindDefinition(LineHandle Handle, BOOL)
00294 {
00295     if (Handle == LineHandle_NoLine)
00296         return(NULL);
00297 
00298     if (Handle >= m_Used)
00299     {
00300         ERROR3("Out of range Line handle");
00301         return(NULL);
00302     }
00303 
00304     return(m_pLineArray[Handle]);
00305 }
00306 
00307 
00308 /********************************************************************************************
00309 
00310 >   BOOL LineComponent::StartImport(BaseCamelotFilter *pFilter)
00311 
00312     Author:     Diccon_Yamanaka (Xara Group Ltd) <camelotdev@xara.com>
00313     Created:    13/12/99
00314     Inputs:     pFilter - the BaseCamelotFilter filter that is being used to import a file.
00315     Returns:    TRUE if the component was able to prepare for importing;
00316                 FALSE if not (e.g. out of memory)
00317 
00318     Purpose:    Called before we start to import a file
00319 
00320     SeeAlso:    DocComponent
00321 
00322 ********************************************************************************************/
00323 
00324 BOOL LineComponent::StartImport(BaseCamelotFilter *pFilter)
00325 {
00326     if (pFilter == NULL)
00327     {
00328         ERROR3("LineComponent::StartImport filter is null!");
00329         return(TRUE);
00330     }
00331 
00332     // Set the IOStore in all the Lines to 0xffffffff. During import, this entry is used
00333     // to map imported handles to the real handles they have been assigned.
00334     if (m_pLineArray != NULL)
00335     {
00336         for (UINT32 Index = 0; Index < m_Used; Index++)
00337         {
00338             if (m_pLineArray[Index] != NULL)
00339                 m_pLineArray[Index]->SetIOStore(0xffffffff);
00340         }
00341     }
00342 
00343     return(TRUE);
00344 }
00345 
00346 
00347 
00348 /********************************************************************************************
00349 
00350 >   BOOL LineComponent::EndImport(BaseCamelotFilter *pFilter, BOOL Success)
00351 
00352     Author:     Diccon_Yamanaka (Xara Group Ltd) <camelotdev@xara.com>
00353     Created:    13/12/99
00354 
00355     Inputs:     pFilter - the BaseCamelotFilter filter that is being used to import a file.
00356                 Success - TRUE => The import was successful;
00357                          FALSE => The import failed - abandon any changes.
00358 
00359     Returns:    TRUE if the component was able to end the importing;
00360                 FALSE if not (e.g. out of memory)
00361 
00362     Purpose:    Called on completion of an import
00363 
00364 ********************************************************************************************/
00365 
00366 BOOL LineComponent::EndImport(BaseCamelotFilter *pFilter, BOOL Success)
00367 {
00368     return TRUE;
00369 }
00370 
00371 
00372 /********************************************************************************************
00373 
00374 >   BOOL LineComponent::EndExport(BaseCamelotFilter *pFilter, BOOL Success)
00375 
00376     Author:     Diccon_Yamanaka (Xara Group Ltd) <camelotdev@xara.com>
00377     Created:    13/12/99
00378     Inputs:     pFilter - the BaseCamelotFilter filter that is being used to import a file.
00379                 Success - True if everything went swimmingly, False if just a clean up is required.
00380 
00381     Returns:    TRUE if the component was able to end the exporting;
00382                 FALSE if not (e.g. out of memory)
00383 
00384     Purpose:    Called on completion of file export
00385 
00386 ********************************************************************************************/
00387 
00388 BOOL LineComponent::EndExport(BaseCamelotFilter *pFilter, BOOL Success)
00389 {
00390     BOOL ok = TRUE;
00391     TRACEUSER( "Diccon", _T("End Export Brush\n"));
00392 #if !defined(EXCLUDE_FROM_RALPH)
00393     if (pFilter == NULL)
00394     {
00395         ERROR3("LineComponent::EndExport filter is null!");
00396         return(ok);
00397     }
00398 
00399     if (!Success)       // If we failed to export, then return immediately
00400         return(ok);
00401 
00402 #endif
00403     return(ok);
00404 }
00405 
00406 
00407 
00408 /********************************************************************************************
00409 
00410 >   static BOOL ImportLine(CXaraFileRecord* pRecord)
00411 
00412     Author:     Diccon_Yamanaka (Xara Group Ltd) <camelotdev@xara.com>
00413     Created:    13/12/99
00414 
00415     Inputs:     pRecord - The TAG_LineDEFINITION record to import
00416 
00417     Returns:    TRUE if Import was successful
00418 
00419     Purpose:    To import a vector Line definition
00420 
00421 ********************************************************************************************/
00422 
00423 BOOL LineComponent::StartImportLine(CamelotRecordHandler *pHandler, CXaraFileRecord *pRecord)
00424 {
00425     m_ImportHandle = 0xffffffff;
00426     m_ImportFlags  = 0;
00427     m_ImportData1  = 0;
00428     m_ImportData2  = 0;
00429     m_pImportPreviousContext = NULL;
00430     m_pImportNewLine = NULL;
00431 
00432     String_256 TempName;    // Load the name into a nice large safe buffer
00433 
00434     // Import the data from the record
00435     if (!pRecord->ReadUINT32(&m_ImportHandle) || !pRecord->ReadUINT32(&m_ImportFlags) ||
00436         !pRecord->ReadUINT32(&m_ImportData1) || !pRecord->ReadUINT32(&m_ImportData2))
00437     {
00438         return(FALSE);
00439     }
00440 
00441     // If there is a name to be imported, read it as well
00442     if ((m_ImportFlags & 0x200) != 0 && !pRecord->ReadUnicode(&TempName))
00443         return(FALSE);
00444 
00445     // We remember the imported flags and suchlike, which we will use in EndImportLine.
00446     TempName.Left((StringBase *)&m_ImportedName, 31);
00447 
00448     // Create a spread and set up the import system to import the brush into it
00449     // If this fails, then it'll just find somewhere "sensible" to import into
00450     m_pImportNewLine = new Spread;
00451     if (m_pImportNewLine == NULL)
00452         return(FALSE);
00453 
00454     Layer *pLineLayer = new Layer(m_pImportNewLine, FIRSTCHILD, String_256(TEXT("Jason did this")));
00455     if (pLineLayer == NULL)
00456     {
00457         delete m_pImportNewLine;
00458         m_pImportNewLine = NULL;
00459         return(FALSE);
00460     }
00461 
00462     // Now, remember where we were importing into, and point the importer at our brush tree
00463     m_pImportPreviousContext = pHandler->GetInsertContext();
00464     pHandler->SetInsertContextNode(pLineLayer);
00465     return(TRUE);
00466 }
00467 
00468 
00469 
00470 /********************************************************************************************
00471 
00472 >   LineHandle LineComponent::FindImportedLine(UINT32 ImportedHandle)
00473 
00474     Author:     Diccon_Yamanaka (Xara Group Ltd) <camelotdev@xara.com> 
00475     Created:    13/12/99
00476 
00477     Inputs:     Importedhandle - The handle which is used in the file being imported
00478                                  to reference the Line.
00479 
00480     Returns:    The LineHandle of the loaded Line, or LineHandle_NoLine
00481                 if the Line could not be found.
00482 
00483     Purpose:    To match up a handle from the currently importing file to the
00484                 real internal LineHandle of the imported LineDefinition
00485 
00486 ********************************************************************************************/
00487 
00488 LineHandle LineComponent::FindImportedLine(UINT32 ImportedHandle)
00489 {
00490     ImportedHandle &= 0x00ffffff;   // Only the bottom 24 bits are relevant
00491     for (UINT32 Index = 0; Index < m_Used; Index++)
00492     {
00493         if (m_pLineArray[Index] != NULL && m_pLineArray[Index]->ReadIOStore() == ImportedHandle)
00494             return((LineHandle)Index);
00495     }
00496 
00497     return(LineHandle_NoLine);
00498 }
00499 
00500 
00501 /********************************************************************************************
00502 
00503 >   BOOL LineComponent::NameIsUnique(const String_32& NameString)
00504 
00505     Author:     Diccon_Yamanaka (Xara Group Ltd) <camelotdev@xara.com> 
00506     Created:    3/7/2000
00507 
00508     Inputs:     NameString - the string to test
00509     Returns:    TRUE if none of the definitions in the component have this string as their name,
00510                 FALSE if one of them does
00511 
00512     Purpose:    To find out if someone has already gotten this name
00513 
00514 ********************************************************************************************/
00515 
00516 BOOL LineComponent::NameIsUnique(const String_32& NameString)
00517 {
00518     ERROR3IF(NameString.IsEmpty(), "Empty string in LineComponent::NameIsUnique");
00519     
00520     BOOL Unique = TRUE;
00521     UINT32 Counter = 0;
00522     String_32* pDefName = NULL;
00523     LineDefinition* pDef = NULL;
00524     while (Counter < m_Used)
00525     {
00526         pDef = FindDefinition(Counter);
00527         if (pDef == NULL)
00528         {
00529             ERROR3("null brush definition in LineComponent::NameIsUnique");
00530             break;
00531         }
00532         pDefName = pDef->GetLineName();
00533         if( pDefName != NULL )
00534         {
00535             if (NameString == *pDefName)
00536             {
00537                 Unique = FALSE;
00538                 break;
00539             }
00540         }
00541         Counter++;
00542     }
00543     return Unique;
00544 
00545 }

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