1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 // wxFormBuilder - A Visual Dialog Editor for wxWidgets.
4 // Copyright (C) 2005 José Antonio Hurtado
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19 //
20 // Written by
21 //   José Antonio Hurtado - joseantonio.hurtado@gmail.com
22 //   Juan Antonio Ortega  - jortegalalmolda@gmail.com
23 //
24 ///////////////////////////////////////////////////////////////////////////////
25 
26 #ifndef __OBJ_DATABASE__
27 #define __OBJ_DATABASE__
28 
29 #include <wx/dynlib.h>
30 #include <set>
31 #include <map>
32 #include "model/types.h"
33 #include "utils/wxfbdefs.h"
34 
35 class ObjectDatabase;
36 class ObjectTypeDictionary;
37 class PropertyCategory;
38 
39 typedef boost::shared_ptr<ObjectDatabase> PObjectDatabase;
40 
41 namespace ticpp
42 {
43 	class Element;
44 }
45 
46 /**
47  * Paquete de clases de objetos.
48  * Determinará la agrupación en la paleta de componentes.
49  */
50 class ObjectPackage
51 {
52  private:
53   wxString m_name;    // nombre del paquete
54   wxString m_desc;  // breve descripción del paquete
55   wxBitmap m_icon;	// The icon for the notebook page
56 
57   // Vector con los objetos que están contenidos en el paquete
58   std::vector< PObjectInfo > m_objs;
59 
60  public:
61   /**
62    * Constructor.
63    */
64   ObjectPackage(wxString name, wxString desc, wxBitmap icon);
65 
66   /**
67    * Incluye en el paquete la información de un objeto.
68    */
Add(PObjectInfo obj)69   void Add(PObjectInfo obj) { m_objs.push_back(obj); };
70 
71   /**
72    * Obtiene el nombre del paquete.
73    */
GetPackageName()74   wxString GetPackageName() { return m_name; }
75 
76   /**
77    * Obtiene el texto que describe el paquete.
78    */
GetPackageDescription()79   wxString GetPackageDescription() { return m_desc; }
80 
81   /**
82    * Get Package Icon
83    */
GetPackageIcon()84   wxBitmap GetPackageIcon() { return m_icon; }
85 
86   /**
87    * Obtiene el número de objetos incluidos en el paquete.
88    */
GetObjectCount()89   unsigned int GetObjectCount() { return (unsigned int)m_objs.size(); }
90 
91   /**
92    * Obtiene la información de un objeto incluido en el paquete.
93    */
94   PObjectInfo GetObjectInfo(unsigned int idx);
95 
96   /**
97   If two xml files specify the same package name, then they merged to one package with this.
98   This allows one package to be split across multiple xml files.
99   */
100   void AppendPackage( PObjectPackage package );
101 
102 };
103 
104 class IComponentLibrary;
105 
106 /**
107  * Base de datos de objetos.
108  * Todos las informaciones de objetos importadas de los archivos XML, serán
109  * almacenados por esta clase.
110  */
111 class ObjectDatabase
112 {
113  public:
114   static bool HasCppProperties(wxString type);
115  private:
116   typedef std::vector<PObjectPackage> PackageVector;
117 
118   // Map the property type string to the property type number
119   typedef std::map<wxString,PropertyType> PTMap;
120   typedef std::map<wxString,PObjectType> ObjectTypeMap;
121   #ifdef __WXMAC__
122 	typedef std::vector< void * > LibraryVector;
123   #else
124 	typedef std::vector< wxDynamicLibrary * > LibraryVector;
125   #endif
126   typedef void (*PFFreeComponentLibrary)( IComponentLibrary* lib );
127   typedef std::map< PFFreeComponentLibrary, IComponentLibrary * > ComponentLibraryMap;
128   typedef std::set<wxString> MacroSet;
129   typedef std::map< wxString, PCodeInfo > LangTemplateMap;
130   typedef std::map< PropertyType, LangTemplateMap > PTLangTemplateMap;
131 
132   wxString m_xmlPath;
133   wxString m_iconPath;
134   wxString m_pluginPath;
135   std::map< wxString, PObjectInfo > m_objs;
136   PackageVector m_pkgs;
137   PTMap m_propTypes;
138   LibraryVector m_libs;
139   ComponentLibraryMap m_componentLibs;
140   ObjectTypeMap m_types; // register object types
141 
142   // para comprobar que no se nos han quedado macros sin añadir en las
143   // liberias de componentes, vamos a crear un conjunto con las macros
144   // definidas en los XML, y al importar las librerías vamos a ir eliminando
145   // dichas macros del conjunto, quedando al final las macros que faltan
146   // por registrar en la librería.
147   MacroSet m_macroSet;
148 
149   // used so libraries are only imported once, even if multiple libraries use them
150   std::set< wxString > m_importedLibraries;
151 
152   PTLangTemplateMap m_propertyTypeTemplates;
153 
154   /**
155    * Initialize the property type map.
156    */
157   void InitPropertyTypes();
158 
159   /**
160    * Carga las plantillas de generación de código de un fichero
161    * xml de código dado
162    */
163   void LoadCodeGen( const wxString& file );
164 
165   /**
166    * Carga los objetos de un paquete con todas sus propiedades salvo
167    * los objetos heredados
168    */
169   PObjectPackage LoadPackage( const wxString& file, const wxString& iconPath = wxEmptyString );
170 
171   void ParseProperties( ticpp::Element* elem_obj, PObjectInfo obj_info, PPropertyCategory category, std::set< PropertyType >* types );
172   void ParseEvents    ( ticpp::Element* elem_obj, PObjectInfo obj_info, PPropertyCategory category );
173 
174   /**
175    * Importa una librería de componentes y lo asocia a cada clase.
176    * @throw wxFBException If the library could not be imported.
177    */
178   void ImportComponentLibrary( wxString libfile, PwxFBManager manager );
179 
180   /**
181    * Incluye la información heredada de los objetos de un paquete.
182    * En la segunda pasada configura cada paquete con sus objetos base.
183    */
184   void SetupPackage( const wxString& file, const wxString& path, PwxFBManager manager );
185 
186   /**
187    * Determina si el tipo de objeto hay que incluirlo en la paleta de
188    * componentes.
189    */
190   bool ShowInPalette(wxString type);
191 
192   // rutinas de conversión
193   PropertyType ParsePropertyType (wxString str);
194   wxString       ParseObjectType   (wxString str);
195 
196 
197   PObjectType GetObjectType(wxString name);
198 
199   int CountChildrenWithSameType(PObjectBase parent,PObjectType type);
200 
201   void SetDefaultLayoutProperties(PObjectBase obj);
202 
203  public:
204   ObjectDatabase();
205   ~ObjectDatabase();
206 
207   PObjectBase NewObject(PObjectInfo obj_info);
208 
209   /**
210    * Obtiene la información de un objeto a partir del nombre de la clase.
211    */
212   PObjectInfo GetObjectInfo(wxString class_name);
213 
214   /**
215    * Configura la ruta donde se encuentran los ficheros con la descripción.
216    */
SetXmlPath(const wxString & path)217   void SetXmlPath( const wxString& path ) { m_xmlPath = path; }
218 
219   /**
220    * Configura la ruta donde se encuentran los iconos asociados a los objetos.
221    */
SetIconPath(const wxString & path)222   void SetIconPath( const wxString& path)  { m_iconPath = path; }
SetPluginPath(const wxString & path)223   void SetPluginPath( const wxString& path ) { m_pluginPath = path; }
224 
225   /**
226    * Obtiene la ruta donde se encuentran los ficheros con la descripción de
227    * objetos.
228    */
GetXmlPath()229   wxString GetXmlPath()		{ return m_xmlPath; 		}
GetIconPath()230   wxString GetIconPath()	{ return m_iconPath; 		}
GetPluginPath()231   wxString GetPluginPath()	{ return m_pluginPath; 		}
232 
233   /**
234    * Carga las definiciones de tipos de objetos.
235    */
236   bool LoadObjectTypes();
237 
238   /**
239    * Find and load plugins from the plugins directory
240    */
241   void LoadPlugins( PwxFBManager manager );
242 
243   /**
244    * Fabrica de objetos.
245    * A partir del nombre de la clase se crea una nueva instancia de un objeto.
246    */
247   PObjectBase CreateObject( std::string class_name, PObjectBase parent = PObjectBase());
248 
249   /**
250    * Fábrica de objetos a partir de un objeto XML.
251    * Este método se usará para cargar un proyecto almacenado.
252    */
253   PObjectBase CreateObject( ticpp::Element* obj, PObjectBase parent = PObjectBase());
254 
255   /**
256    * Crea un objeto como copia de otro.
257    */
258 
259   PObjectBase CopyObject(PObjectBase obj);
260 
261   /**
262    * Obtiene un paquete de objetos.
263    */
264   PObjectPackage GetPackage(unsigned int idx);
265 
266   /**
267    * Obtiene el número de paquetes registrados.
268    */
GetPackageCount()269   unsigned int GetPackageCount() { return (unsigned int)m_pkgs.size(); }
270 
271   /**
272    * Resetea los contadores que acompañan al nombre.
273    * La propiedad "name" es una propiedad especial, reservada para el nombre
274    * de la instancia del objeto. Cada clase de objeto tiene asociado un contador
275    * para no duplicar nombre en la creación de nuevos objetos
276    * (p.e. m_button1, m_button2 ...)
277    */
278   void ResetObjectCounters();
279 };
280 
281 
282 
283 #endif //__OBJ_DATABASE__
284