00001 // $Id: module.cpp 1464 2006-07-18 12:32:26Z gerry $ 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 /* 00100 */ 00101 00102 #include "camtypes.h" 00103 00104 #include "oilmods.h" 00105 #include "module.h" 00106 #include "modlist.h" 00107 //#include "tool.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00108 //#include "ensure.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00109 //#include "viewrc.h" 00110 //#include "errors.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00111 00112 #include "zoomtool.h" 00113 //#include "pushtool.h" 00114 00115 00116 DECLARE_SOURCE("$Revision: 1464 $"); 00117 00118 ModuleList *Module::Modules = NULL; 00119 00120 00121 CC_IMPLEMENT_MEMDUMP(ModInfo_v1, CCObject) 00122 CC_IMPLEMENT_MEMDUMP(ModInfo, ModInfo_v1) 00123 CC_IMPLEMENT_MEMDUMP(Module_v1, CCObject) 00124 CC_IMPLEMENT_MEMDUMP(Module, Module_v1) 00125 00126 // Declare smart memory handling in Debug builds 00127 #define new CAM_DEBUG_NEW 00128 00129 /******************************************************************************************** 00130 00131 > void Module::InitModules() 00132 00133 Author: Tim_Browse (Xara Group Ltd) <camelotdev@xara.com> 00134 Created: 30/6/93 00135 Inputs: - 00136 Outputs: - 00137 Returns: TRUE if OK, FALSE for catastrophic failure 00138 Purpose: Called at the end of initialisation (as it may well need all other 00139 initialisations to have been done) to add Tools and Modules to the Kernel's 00140 knowledgebase. 00141 Errors: None 00142 Scope: Public 00143 00144 ********************************************************************************************/ 00145 00146 BOOL Module::InitModules() 00147 { 00148 // Initialise the list of Modules. 00149 Modules = new ModuleList; 00150 if (!Modules) 00151 return FALSE; 00152 00153 if (!OILModule::Init()) // get OIL's list of modules. This works 00154 return FALSE; // by calling back to DescribeModule. 00155 // NB. OILModule::Init does it own error 00156 BOOL Finished; // reporting 00157 00158 do 00159 { 00160 // All the modules have been 'new'ed now. We can now ask them to initialise themselves. 00161 // If they don't want to, they get 'delete'ed. 00162 00163 ModuleListItem *Item = (ModuleListItem *) Modules->GetHead(); 00164 00165 Finished = TRUE; 00166 00167 while (Item != NULL) 00168 { 00169 Module *Module = Item->m_pModule; 00170 00171 // If this module exists and has not been initialised yet, then try to 00172 // initialise it. If it initialises ok, set the item's flag, and force 00173 // another scan of the remaining modules in case one of them depends on 00174 // this module being present. 00175 if ((Module != NULL) && (!Item->m_Initialised) && (Module->Init())) 00176 { 00177 Item->m_Initialised = TRUE; // Tool initialised ok. 00178 Finished = FALSE; // At least one more loop to try 00179 } 00180 00181 // Try the next module 00182 Item = (ModuleListItem *) Modules->GetNext(Item); 00183 } 00184 00185 } while (!Finished); 00186 00187 // Delete any un-initialised modules 00188 ModuleListItem *Item = (ModuleListItem *) Modules->GetHead(); 00189 00190 while (Item != NULL) 00191 { 00192 if ((Item->m_pModule != NULL) && (!Item->m_Initialised)) 00193 { 00194 delete Item->m_pModule; 00195 Item->m_pModule = NULL; 00196 } 00197 00198 // Try the next module 00199 Item = (ModuleListItem *) Modules->GetNext(Item); 00200 } 00201 00202 // Scan all the modules and create (but do not initialise) any tools they provide. 00203 if (!Tool::InitToolList() || !CreateTools()) 00204 return FALSE; 00205 00206 return TRUE; 00207 } 00208 00209 00210 /******************************************************************************************** 00211 00212 > ModuleListItem *Module::Declare(Module *NewModule) 00213 00214 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00215 Created: 21/6/93 00216 Inputs: NewModule - pointer to a new Tool and its ID 00217 pOILModule - pointer to the OILModule object to be associated with this 00218 module. 00219 Outputs: None 00220 Returns: Pointer to new module list item if module. 00221 Purpose: Called by OIL code to declare new modules. 00222 Errors: Will return NULL if invalid ID or same module ID used twice, and NewModule 00223 will have been deleted. 00224 00225 ********************************************************************************************/ 00226 00227 ModuleListItem *Module::Declare(Module *NewModule, OILModule *pOILModule) 00228 { 00229 // Try to add the module to the kernel's list. 00230 ModuleListItem *NewItem = Modules->Add(NewModule, pOILModule); 00231 00232 if (NewItem == NULL) 00233 { 00234 // Either out of memory or Module has invalid ID - print error message and 00235 // destroy module. 00236 #ifdef _DEBUG 00237 ModInfo Info; 00238 NewModule->Describe(&Info); 00239 TRACE( _T("Module %u is invalid or has no memory to initialise with.\n"), Info.ID ); 00240 #endif 00241 00242 delete NewModule; 00243 } 00244 00245 return NewItem; 00246 } 00247 00248 00249 /******************************************************************************************** 00250 00251 > ModuleListItem *Module::Find(UINT32 ModuleID) 00252 00253 Author: Tim_Browse (Xara Group Ltd) <camelotdev@xara.com> 00254 Created: 30/6/93 00255 Inputs: Module ID 00256 Outputs: None 00257 Returns: Pointer to list item of the required module, or NULL if not found. 00258 Purpose: Anyone can ask about a module's presence by its ID. 00259 Errors: None. 00260 00261 ********************************************************************************************/ 00262 00263 ModuleListItem *Module::Find(UINT32 ModuleID) 00264 { 00265 // Simple search of list for ID 00266 00267 ModuleListItem *Item = (ModuleListItem *) Modules->GetHead(); 00268 00269 while (Item != NULL) 00270 { 00271 if (Item->m_ModInfo.ID == ModuleID) 00272 // Found ID - return pointer to this list item. 00273 return Item; 00274 00275 Item = (ModuleListItem *) Modules->GetNext(Item); 00276 } 00277 00278 // Not found 00279 return NULL; 00280 } 00281 00282 /******************************************************************************************** 00283 00284 > void Module::DeinitModules() 00285 00286 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00287 Created: 21/6/93 00288 Inputs: None 00289 Outputs: None 00290 Returns: None 00291 Purpose: Deletes all allocated modules (and hence tools), then asks OIL to tidy up. 00292 Safe to call more than once. 00293 00294 Do NOT try to use any tools or modules after calling this. 00295 00296 Errors: None 00297 00298 ********************************************************************************************/ 00299 00300 void Module::DeinitModules() 00301 { 00302 if (Modules != NULL) 00303 { 00304 // Delete the list itself. 00305 delete Modules; 00306 Modules = NULL; 00307 00308 // Perform any clean-up required by the OIL layer. 00309 PORTNOTE("other","Removed OILModule usage") 00310 #ifndef EXCLUDE_FROM_XARALX 00311 OILModule::Deinit(); 00312 #endif 00313 } 00314 } 00315 00316 /******************************************************************************************** 00317 00318 > BOOL Module::CreateTools() 00319 00320 Author: Tim_Browse (Xara Group Ltd) <camelotdev@xara.com> 00321 Created: 1/7/93 00322 Inputs: - 00323 Outputs: - 00324 Returns: TRUE => Tools created OK. 00325 Purpose: Scan all modules known to the kernel, and try to create an instance of each 00326 tool they contain, and add this tool to the kernel's list. 00327 Errors: - 00328 SeeAlso: - 00329 00330 ********************************************************************************************/ 00331 00332 BOOL Module::CreateTools() 00333 { 00334 // Scan the list of modules 00335 ModuleListItem *Item = (ModuleListItem *) Modules->GetHead(); 00336 00337 while (Item != NULL) 00338 { 00339 // Interrogate module 00340 ModInfo Info; 00341 Item->m_pModule->Describe(&Info); 00342 00343 // Try to create an instance of each tool in this module 00344 for (unsigned tool = 1; tool <= Info.NumTools; tool++) 00345 { 00346 Tool *NewTool = (Tool *) Item->m_pModule->CreateTool(tool); 00347 00348 if (NewTool != NULL) 00349 { 00350 // Tool was created - add it to the list 00351 Tool::Declare(NewTool, Info.ID); 00352 00353 } 00354 } 00355 00356 // Try the next module in the list. 00357 Item = (ModuleListItem *) Modules->GetNext(Item); 00358 } 00359 // Tool* NewTool ; 00360 // NewTool = (Tool*)new ZoomTool; if ( NewTool ) Tool::Declare(NewTool,1); 00361 // NewTool = (Tool*)new PushTool; if ( NewTool ) Tool::Declare(NewTool,Info.ID); 00362 00363 // No error codes yet 00364 return TRUE; 00365 } 00366 00367 00368 00369 // Empty functions for the default module. 00370 00371 /******************************************************************************************** 00372 00373 > BOOL Module_v1::Init() 00374 00375 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00376 Created: 21/6/93 00377 Inputs: - 00378 Outputs: - 00379 Returns: TRUE if OK, FALSE if module doesn't want to initialise. 00380 Purpose: Called after module's constructor to ask it whether it wants to exist or 00381 not. Each module is asked in two passes, so that its existence can depend 00382 on another module. If a module does not want to exist, it should return FALSE, 00383 whereupon it will be deleted. 00384 Errors: None 00385 00386 ********************************************************************************************/ 00387 00388 00389 BOOL Module_v1::Init() 00390 { 00391 return FALSE; // Should never try to instantiate a module base class 00392 } 00393 00394 00395 /******************************************************************************************** 00396 00397 > Module_v1::~Module_v1() 00398 00399 Author: Tim_Browse (Xara Group Ltd) <camelotdev@xara.com> 00400 Created: 7/7/93 00401 Inputs: - 00402 Outputs: - 00403 Returns: - 00404 Purpose: Destroys the module, performing any cleanup necessary. 00405 Errors: - 00406 SeeAlso: - 00407 00408 ********************************************************************************************/ 00409 00410 Module_v1::~Module_v1() 00411 { 00412 // Null destructor 00413 } 00414 00415 /******************************************************************************************** 00416 00417 > BOOL Module_v1::Describe(void *Infoptr) 00418 00419 Author: Andy_Pennell (Xara Group Ltd) <camelotdev@xara.com> 00420 Created: 21/6/93 00421 Inputs: Pointer to area to recieve info 00422 Outputs: Info area is updated 00423 Returns: None 00424 Purpose: Asks a module for more information. The pointer points to a struct such as 00425 ModInfo_v1 which the module should fill in. All fields should be filled 00426 (NULL is OK for char* variables). The type of the pointer is not explicitly 00427 defined so the structure can evolve while keeping backward compatibility. 00428 Errors: None 00429 00430 ********************************************************************************************/ 00431 00432 void Module_v1::Describe(void *InfoPtr) 00433 { 00434 // Cast structure into the latest one we understand. 00435 ModInfo_v1 *Info = (ModInfo_v1 *) InfoPtr; 00436 00437 Info->InterfaceVersion = GetInterfaceVersion(); // You should always have this line. 00438 00439 // These are all garbage as we should never try to create one of these... 00440 Info->Version = 1; 00441 Info->ID = MODULEID_INVALID; // so we never get instantiated 00442 Info->NumTools = 0; 00443 Info->Name = Info->Purpose = Info->Author = NULL; 00444 } 00445 00446 /******************************************************************************************** 00447 00448 > void *Module_v1::CreateTool(UINT32 ToolNumber) 00449 00450 Author: Tim_Browse (Xara Group Ltd) <camelotdev@xara.com> 00451 Created: 30/6/93 00452 Inputs: ToolNumber - The number of the tool to create (1 to NumTools) 00453 Outputs: - 00454 Returns: Pointer to the new instantiation of the desired tool. 00455 Purpose: Provide a clean interface from module to kernel code for the creation 00456 of tools. 00457 Errors: - 00458 00459 ********************************************************************************************/ 00460 00461 void *Module_v1::CreateTool(UINT32) 00462 { 00463 return NULL; // Should never call this for the base class 00464 } 00465 00466 00467 00468