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 /** 27 @file 28 @author José Antonio Hurtado - joseantonio.hurtado@gmail.com 29 @author Juan Antonio Ortega - jortegalalmolda@gmail.com 30 @note 31 The implementation of the generation of relative paths is a little hacky, and not a solution. 32 The value of all properties that are file or a directory paths must be absolute, otherwise the code generation will not work. 33 */ 34 35 #ifndef _CPP_CODE_GEN_ 36 #define _CPP_CODE_GEN_ 37 38 #include <set> 39 #include "codegen.h" 40 #include <wx/string.h> 41 #include "codeparser.h" 42 43 /** 44 * Parse the C++ templates. 45 */ 46 class CppTemplateParser : public TemplateParser 47 { 48 private: 49 bool m_i18n; 50 bool m_useRelativePath; 51 wxString m_basePath; 52 53 public: 54 CppTemplateParser( PObjectBase obj, wxString _template, bool useI18N, bool useRelativePath, wxString basePath ); 55 CppTemplateParser( const CppTemplateParser & that, wxString _template ); 56 57 // overrides for C++ 58 PTemplateParser CreateParser( const TemplateParser* oldparser, wxString _template ); 59 wxString RootWxParentToCode(); 60 wxString ValueToCode( PropertyType type, wxString value); 61 62 }; 63 64 /** 65 * Generate the C++ code 66 */ 67 class CppCodeGenerator : public CodeGenerator 68 { 69 private: 70 typedef enum 71 { 72 P_PRIVATE, 73 P_PROTECTED, 74 P_PUBLIC 75 } Permission; 76 77 CCodeParser m_inheritedCodeParser; 78 79 PCodeWriter m_header; 80 PCodeWriter m_source; 81 82 bool m_useRelativePath; 83 bool m_i18n; 84 wxString m_basePath; 85 unsigned int m_firstID; 86 bool m_useConnect; 87 bool m_disconnectEvents; 88 89 /** 90 * Predefined macros won't generate defines. 91 */ 92 std::set<wxString> m_predMacros; 93 94 void SetupPredefinedMacros(); 95 96 /** 97 * Given an object and the name for a template, obtains the code. 98 */ 99 wxString GetCode( PObjectBase obj, wxString name); 100 101 /** 102 * Stores the project's objects classes set, for generating the includes. 103 */ 104 void FindDependencies( PObjectBase obj, std::set< PObjectInfo >& info_set ); 105 106 /** 107 * Stores the needed "includes" set for the PT_BITMAP properties. 108 */ 109 void FindEmbeddedBitmapProperties( PObjectBase obj, std::set< wxString >& embedset); 110 111 /** 112 * Stores all the properties for "macro" type objects, so that their 113 * related '#define' can be generated subsequently. 114 */ 115 void FindMacros( PObjectBase obj, std::vector< wxString >* macros ); 116 117 /** 118 * Looks for "non-null" event handlers (PEvent) and collects it into a vector. 119 */ 120 void FindEventHandlers(PObjectBase obj, EventVector &events); 121 122 /** 123 * Generates classes declarations inside the header file. 124 */ 125 void GenClassDeclaration( PObjectBase class_obj, bool use_enum, const wxString& classDecoration, const EventVector &events ); 126 127 /** 128 * Generates the event table. 129 */ 130 void GenEvents( PObjectBase class_obj, const EventVector &events, bool disconnect = false ); 131 132 /** 133 * helper function to find the event table entry template in the class or its base classes 134 */ 135 bool GenEventEntry( PObjectBase obj, PObjectInfo obj_info, const wxString& templateName, const wxString& handlerName, bool disconnect = false ); 136 137 /** 138 * Recursive function for the attributes declaration, used inside GenClassDeclaration. 139 */ 140 void GenAttributeDeclaration( PObjectBase obj, Permission perm); 141 142 /** 143 * Recursive function for the validators' variables declaration, used inside GenClassDeclaration. 144 */ 145 void GenValidatorVariables( PObjectBase obj); 146 /** 147 * Recursive function for the validators' variables declaration, used inside GenClassDeclaration. 148 */ 149 void GenValVarsBase( PObjectInfo info, PObjectBase obj); 150 151 /** 152 * Generates the generated_event_handlers template 153 */ 154 void GetGenEventHandlers( PObjectBase obj ); 155 /** 156 * Generates the generated_event_handlers template 157 */ 158 void GenDefinedEventHandlers( PObjectInfo info, PObjectBase obj ); 159 160 /** 161 * Generates the '#include' section for files. 162 */ 163 void GenIncludes( PObjectBase project, std::vector< wxString >* includes, std::set< wxString >* templates ); 164 void GenObjectIncludes( PObjectBase project, std::vector< wxString >* includes, std::set< wxString >* templates ); 165 void GenBaseIncludes( PObjectInfo info, PObjectBase obj, std::vector< wxString >* includes, std::set< wxString >* templates ); 166 void AddUniqueIncludes( const wxString& include, std::vector< wxString >* includes ); 167 168 /** 169 * Generate a set of all subclasses to forward declare in the generated header file. 170 * Also generate sets of header files to be include in either the source or header file. 171 */ 172 void GenSubclassSets( PObjectBase obj, std::set< wxString >* subclasses, std::set< wxString >* sourceIncludes, std::vector< wxString >* headerIncludes ); 173 174 /** 175 * Generates the '#include' section for the embedded bitmap properties. 176 */ 177 void GenEmbeddedBitmapIncludes( PObjectBase project); 178 179 /** 180 * Generates the '#define' section for macros. 181 */ 182 void GenDefines( PObjectBase project); 183 184 /** 185 * Generates an enum with wxWindow identifiers. 186 */ 187 void GenEnumIds( PObjectBase class_obj); 188 189 /** 190 * Generates the constructor for a class 191 */ 192 void GenConstructor( PObjectBase class_obj, const EventVector &events ); 193 194 /** 195 * Generates the destructor for a class 196 */ 197 void GenDestructor( PObjectBase class_obj, const EventVector &events ); 198 199 /** 200 * Makes the objects construction, setting up the objects' and Layout properties. 201 * The algorithm is simmilar to that used in the designer preview generation. 202 */ 203 void GenConstruction( PObjectBase obj, bool is_widget ); 204 205 /** 206 * Makes the objects destructions. 207 */ 208 void GenDestruction( PObjectBase obj); 209 210 /** 211 * Configures the object properties, both own and inherited ones. 212 * Information for the class is given, because it will recursively make the 213 * configuration in the "super-classes". 214 */ 215 void GenSettings( PObjectInfo info, PObjectBase obj); 216 217 /** 218 * Adds a control for a toolbar. Needs the objectinfo (wxWindow type) where 219 * the template is found, and the objectbase for the control. 220 */ 221 void GenAddToolbar( PObjectInfo info, PObjectBase obj ); 222 void GetAddToolbarCode( PObjectInfo info, PObjectBase obj, wxArrayString& codelines ); 223 224 void GenPrivateEventHandlers(const EventVector &events); 225 226 void GenVirtualEventHandlers( const EventVector &events, const wxString& eventHandlerPrefix, const wxString& eventHandlerPostfix ); 227 228 public: 229 /** 230 * Convert a wxString to the "C/C++" format. 231 */ 232 static wxString ConvertCppString( wxString text); 233 234 /** 235 * Convert a path to a relative path. 236 */ 237 //static wxString ConvertToRelativePath( wxString path, wxString basePath); 238 239 /** 240 * Convert an Embedded Bitmap filename to the name of the character array. 241 */ 242 static wxString ConvertEmbeddedBitmapName( const wxString& text ); 243 244 CppCodeGenerator(); 245 246 /** 247 * Set the codewriter for the header file 248 */ SetHeaderWriter(PCodeWriter cw)249 void SetHeaderWriter( PCodeWriter cw ) 250 { 251 m_header = cw; 252 } 253 254 /** 255 * Set the codewriter for the source file 256 */ SetSourceWriter(PCodeWriter cw)257 void SetSourceWriter( PCodeWriter cw ) 258 { 259 m_source = cw; 260 } 261 262 263 /** 264 * Parse existing source/header files 265 */ ParseFiles(wxString headerFile,wxString sourceFile)266 void ParseFiles(wxString headerFile, wxString sourceFile) 267 { 268 m_inheritedCodeParser = CCodeParser(headerFile, sourceFile); 269 } 270 271 /** 272 * Configures the reference path for generating relative paths to 273 * that passed as parameter. 274 * 275 * @note path is generated with the separators, '/', since on Windows 276 * the compilers interpret path correctly. 277 */ 278 void UseRelativePath( bool relative = false, wxString basePath = wxString() ); 279 280 /** 281 * Set the First ID used during Code Generation. 282 */ SetFirstID(const unsigned int id)283 void SetFirstID( const unsigned int id ){ m_firstID = id; } 284 285 /** 286 * Generate the project's code 287 */ 288 bool GenerateCode( PObjectBase project ); 289 290 /** 291 * Generate an inherited class 292 */ 293 void GenerateInheritedClass( PObjectBase userClasses, PObjectBase form ); 294 }; 295 296 297 #endif //_CPP_CODE_GEN_ 298