void XMLUtils::LoadXMLFile( ticpp::Document& doc, bool condenseWhiteSpace, const wxString& path ) { try { if ( path.empty() ) { THROW_WXFBEX( _("LoadXMLFile needs a path") ) } if ( !::wxFileExists( path ) ) { THROW_WXFBEX( _("The file does not exist.\nFile: ") << path ) } TiXmlBase::SetCondenseWhiteSpace( condenseWhiteSpace ); doc.SetValue( std::string( path.mb_str( wxConvFile ) ) ); doc.LoadFile(); } catch ( ticpp::Exception& ) { // Ask user to all wxFB to convert the file to UTF-8 and add the XML declaration wxString msg = _("This xml file could not be loaded. This could be the result of an unsupported encoding.\n"); msg += _("Would you like wxFormBuilder to backup the file and convert it to UTF-8\?\n"); msg += _("You will be prompted for the original encoding.\n\n"); msg += _("Path: "); msg += path; if ( wxNO == wxMessageBox( msg, _("Unable to load file"), wxICON_QUESTION | wxYES_NO | wxYES_DEFAULT, wxTheApp->GetTopWindow() ) ) { // User declined, give up THROW_WXFBEX( _("Unable to load file: ") << path ); } // User accepted, convert the file wxFontEncoding chosenEncoding = StringUtils::GetEncodingFromUser( _("Please choose the original encoding.") ); if ( wxFONTENCODING_MAX == chosenEncoding ) { THROW_WXFBEX( _("Unable to load file: ") << path ); } ConvertAndAddDeclaration( path, chosenEncoding ); LoadXMLFile( doc, condenseWhiteSpace, path ); } ticpp::Declaration* declaration; try { ticpp::Node* firstChild = doc.FirstChild(); declaration = firstChild->ToDeclaration(); } catch( ticpp::Exception& ) { declaration = NULL; } LoadXMLFileImp( doc, condenseWhiteSpace, path, declaration ); }
void FileCodeWriter::Clear() { StringCodeWriter::Clear(); if ( ::wxFileExists( m_filename ) ) { // check for write access to the target file if ( !wxFile::Access( m_filename, wxFile::write ) ) { THROW_WXFBEX( _("Unable to write file: ") << m_filename ); } } else { wxFile file; if ( !file.Create( m_filename, true ) ) { THROW_WXFBEX( _("Unable to create file: ") << m_filename ); } } }
TemplateParser::Ident TemplateParser::SearchIdent(wxString ident) { // LogDebug("Parsing command %s",ident.c_str()); if (ident == wxT("wxparent") ) return ID_WXPARENT; else if (ident == wxT("ifnotnull") ) return ID_IFNOTNULL; else if (ident == wxT("ifnull") ) return ID_IFNULL; else if (ident == wxT("foreach") ) return ID_FOREACH; else if (ident == wxT("pred") ) return ID_PREDEFINED; else if (ident == wxT("npred") ) return ID_PREDEFINED_INDEX; else if (ident == wxT("child") ) return ID_CHILD; else if (ident == wxT("parent") ) return ID_PARENT; else if (ident == wxT("nl") ) return ID_NEWLINE; else if (ident == wxT("ifequal") ) return ID_IFEQUAL; else if (ident == wxT("ifnotequal") ) return ID_IFNOTEQUAL; else if (ident == wxT("ifparenttypeequal") ) return ID_IFPARENTTYPEEQUAL; else if (ident == wxT("ifparentclassequal") ) return ID_IFPARENTCLASSEQUAL; else if (ident == wxT("ifparenttypenotequal") ) return ID_IFPARENTTYPENOTEQUAL; else if (ident == wxT("ifparentclassnotequal") ) return ID_IFPARENTCLASSNOTEQUAL; else if (ident == wxT("append") ) return ID_APPEND; else if (ident == wxT("class") ) return ID_CLASS; else if (ident == wxT("form") || ident == wxT("wizard")) return ID_FORM; else if (ident == wxT("indent") ) return ID_INDENT; else if (ident == wxT("unindent") ) return ID_UNINDENT; else if (ident == wxT("iftypeequal") ) return ID_IFTYPEEQUAL; else if (ident == wxT("iftypenotequal") ) return ID_IFTYPENOTEQUAL; else if(ident == wxT("utbl")) return ID_UTBL; else THROW_WXFBEX( wxString::Format( wxT("Unknown macro: \"%s\""), ident.c_str() ) ); }
/** * Generates wxObjects from ObjectBase * * @param obj ObjectBase to generate. * @param parent wxWindow parent, necessary to instantiate a widget. * @param parentObject ObjectBase parent - not always the same as the wxparent (e.g. an abstract component). */ void VisualEditor::Generate( PObjectBase obj, wxWindow* wxparent, wxObject* parentObject ) { // Get Component PObjectInfo obj_info = obj->GetObjectInfo(); IComponent* comp = obj_info->GetComponent(); if ( NULL == comp ) { THROW_WXFBEX( wxString::Format( wxT("Component for %s not found!"), obj->GetClassName().c_str() ) ); } // Create Object wxObject* createdObject = comp->Create( obj.get(), wxparent ); wxWindow* createdWindow = NULL; wxSizer* createdSizer = NULL; switch ( comp->GetComponentType() ) { case COMPONENT_TYPE_WINDOW: createdWindow = wxDynamicCast( createdObject, wxWindow ); if ( NULL == createdWindow ) { THROW_WXFBEX( wxString::Format( wxT("Component for %s was registered as a window component, but this is not a wxWindow!"), obj->GetClassName().c_str() ) ); } SetupWindow( obj, createdWindow ); // Push event handler in order to respond to Paint and Mouse events createdWindow->PushEventHandler( new VObjEvtHandler( createdWindow, obj ) ); break; case COMPONENT_TYPE_SIZER: createdSizer = wxDynamicCast( createdObject, wxSizer ); if ( NULL == createdSizer ) { THROW_WXFBEX( wxString::Format( wxT("Component for %s was registered as a sizer component, but this is not a wxSizer!"), obj->GetClassName().c_str() ) ); } SetupSizer( obj, createdSizer ); break; default: break; } // Associate the wxObject* with the PObjectBase m_wxobjects.insert( wxObjectMap::value_type( createdObject, obj ) ); m_baseobjects.insert( ObjectBaseMap::value_type( obj.get(), createdObject ) ); // New wxparent for the window's children wxWindow* new_wxparent = ( createdWindow ? createdWindow : wxparent ); // Recursively generate the children for ( unsigned int i = 0; i < obj->GetChildCount(); i++ ) { Generate( obj->GetChild( i ), new_wxparent, createdObject ); } comp->OnCreated( createdObject, new_wxparent ); // If the created object is a sizer and the parent object is a window, set the sizer to the window if ( ( createdSizer != NULL && NULL != wxDynamicCast( parentObject, wxWindow ) ) || ( NULL == parentObject && createdSizer != NULL ) ) { wxparent->SetSizer( createdSizer ); if ( parentObject ) createdSizer->SetSizeHints( wxparent ); wxparent->SetAutoLayout(true); wxparent->Layout(); } }
bool TemplateParser::ParseMacro() { Ident ident; ident = ParseIdent(); switch (ident) { case ID_WXPARENT: return ParseWxParent(); break; case ID_PARENT: return ParseParent(); break; case ID_FORM: return ParseForm(); break; case ID_IFNOTNULL: return ParseIfNotNull(); break; case ID_IFNULL: return ParseIfNull(); break; case ID_FOREACH: return ParseForEach(); break; case ID_PREDEFINED: return ParsePred(); break; case ID_PREDEFINED_INDEX: return ParseNPred(); break; case ID_CHILD: return ParseChild(); break; case ID_NEWLINE: return ParseNewLine(); break; case ID_IFEQUAL: ParseIfEqual(); break; case ID_IFNOTEQUAL: ParseIfNotEqual(); break; case ID_IFPARENTTYPEEQUAL: ParseIfParentTypeEqual(); break; case ID_IFPARENTCLASSEQUAL: ParseIfParentClassEqual(); break; case ID_IFPARENTTYPENOTEQUAL: ParseIfParentTypeNotEqual(); break; case ID_IFPARENTCLASSNOTEQUAL: ParseIfParentClassNotEqual(); break; case ID_APPEND: ParseAppend(); break; case ID_CLASS: ParseClass(); break; case ID_INDENT: ParseIndent(); break; case ID_UNINDENT: ParseUnindent(); break; case ID_IFTYPEEQUAL: ParseIfTypeEqual(); break; case ID_IFTYPENOTEQUAL: ParseIfTypeNotEqual(); break; case ID_UTBL: ParseLuaTable(); break; default: THROW_WXFBEX( wxT("Invalid Macro Type") ); break; } return true; }
int MyApp::OnRun() { // Using a space so the initial 'w' will not be capitalized in wxLogGUI dialogs wxApp::SetAppName( wxT( " wxFormBuilder" ) ); // Creating the wxConfig manually so there will be no space // The old config (if any) is returned, delete it delete wxConfigBase::Set( new wxConfig( wxT("wxFormBuilder") ) ); // Get the data directory wxStandardPathsBase& stdPaths = wxStandardPaths::Get(); wxString dataDir = stdPaths.GetDataDir(); dataDir.Replace( GetAppName().c_str(), wxT("wxformbuilder") ); // Log to stderr while working on the command line delete wxLog::SetActiveTarget( new wxLogStderr ); // Message output to the same as the log target delete wxMessageOutput::Set( new wxMessageOutputLog ); // Parse command line wxCmdLineParser parser( s_cmdLineDesc, argc, argv ); if ( 0 != parser.Parse() ) { return 1; } // Get project to load wxString projectToLoad = wxEmptyString; if ( parser.GetParamCount() > 0 ) { projectToLoad = parser.GetParam(); } bool justGenerate = false; wxString language; bool hasLanguage = parser.Found( wxT("l"), &language ); if ( parser.Found( wxT("g") ) ) { if ( projectToLoad.empty() ) { wxLogError( _("You must pass a path to a project file. Nothing to generate.") ); return 2; } if ( hasLanguage ) { if ( language.empty() ) { wxLogError( _("Empty language option. Nothing generated.") ); return 3; } language.Replace( wxT(","), wxT("|"), true ); } // generate code justGenerate = true; } else { delete wxLog::SetActiveTarget( new wxLogGui ); } // Create singleton AppData - wait to initialize until sure that this is not the second // instance of a project file. AppDataCreate( dataDir ); // Make passed project name absolute try { if ( !projectToLoad.empty() ) { wxFileName projectPath( projectToLoad ); if ( !projectPath.IsOk() ) { THROW_WXFBEX( wxT("This path is invalid: ") << projectToLoad ); } if ( !projectPath.IsAbsolute() ) { if ( !projectPath.MakeAbsolute() ) { THROW_WXFBEX( wxT("Could not make path absolute: ") << projectToLoad ); } } projectToLoad = projectPath.GetFullPath(); } } catch ( wxFBException& ex ) { wxLogError( ex.what() ); } // If the project is already loaded in another instance, switch to that instance and quit if ( !projectToLoad.empty() && !justGenerate ) { if ( ::wxFileExists( projectToLoad ) ) { if ( !AppData()->VerifySingleInstance( projectToLoad ) ) { return 4; } } } // Init handlers wxInitAllImageHandlers(); wxXmlResource::Get()->InitAllHandlers(); #if wxVERSION_NUMBER >= 2905 wxXmlResource::Get()->AddHandler(new wxAuiNotebookXmlHandler); #endif // Init AppData try { AppDataInit(); } catch( wxFBException& ex ) { wxLogError( _("Error loading application: %s\nwxFormBuilder cannot continue."), ex.what() ); wxLog::FlushActive(); return 5; } wxSystemOptions::SetOption( wxT( "msw.remap" ), 0 ); wxSystemOptions::SetOption( wxT( "msw.staticbox.optimized-paint" ), 0 ); m_frame = NULL; #ifndef __WXFB_DEBUG__ wxBitmap bitmap; std::unique_ptr< cbSplashScreen > splash; if ( !justGenerate ) { if ( bitmap.LoadFile( dataDir + wxFILE_SEP_PATH + wxT( "resources" ) + wxFILE_SEP_PATH + wxT( "splash.png" ), wxBITMAP_TYPE_PNG ) ) { splash = std::unique_ptr< cbSplashScreen >( new cbSplashScreen( bitmap, -1, 0, wxNewId() ) ); } } #endif wxYield(); // Read size and position from config file wxConfigBase *config = wxConfigBase::Get(); config->SetPath( wxT("/mainframe") ); int x, y, w, h; x = y = w = h = -1; config->Read( wxT( "PosX" ), &x ); config->Read( wxT( "PosY" ), &y ); config->Read( wxT( "SizeW" ), &w ); config->Read( wxT( "SizeH" ), &h ); long style = config->Read( wxT("style"), wxFB_WIDE_GUI ); if ( style != wxFB_CLASSIC_GUI ) { style = wxFB_WIDE_GUI; } config->SetPath( wxT("/") ); m_frame = new MainFrame( NULL ,-1, (int)style, wxPoint( x, y ), wxSize( w, h ) ); if ( !justGenerate ) { m_frame->Show( TRUE ); SetTopWindow( m_frame ); #ifndef __WXFB_DEBUG__ // turn off the splash screen delete splash.release(); #endif #ifdef __WXFB_DEBUG__ wxLogWindow* log = dynamic_cast< wxLogWindow* >( AppData()->GetDebugLogTarget() ); if ( log ) { m_frame->AddChild( log->GetFrame() ); } #endif //__WXFB_DEBUG__ } // This is not necessary for wxFB to work. However, Windows sets the Current Working Directory // to the directory from which a .fbp file was opened, if opened from Windows Explorer. // This puts an unneccessary lock on the directory. // This changes the CWD to the already locked app directory as a workaround #ifdef __WXMSW__ ::wxSetWorkingDirectory( dataDir ); #endif if ( !projectToLoad.empty() ) { if ( AppData()->LoadProject( projectToLoad, justGenerate ) ) { if ( justGenerate ) { if ( hasLanguage ) { PObjectBase project = AppData()->GetProjectData(); PProperty codeGen = project->GetProperty( _("code_generation") ); if ( codeGen ) { codeGen->SetValue( language ); } } AppData()->GenerateCode( false, true ); return 0; } else { m_frame->InsertRecentProject( projectToLoad ); return wxApp::OnRun(); } } else { wxLogError( wxT("Unable to load project: %s"), projectToLoad.c_str() ); } } if ( justGenerate ) { return 6; } AppData()->NewProject(); #ifdef __WXMAC__ // document to open on startup if(!m_mac_file_name.IsEmpty()) { if ( AppData()->LoadProject( m_mac_file_name ) ) m_frame->InsertRecentProject( m_mac_file_name ); } #endif return wxApp::OnRun(); }
void LoadXMLFileImp( T& doc, bool condenseWhiteSpace, const wxString& path, U* declaration ) { if ( NULL == declaration ) { // Ask user to all wxFB to convert the file to UTF-8 and add the XML declaration wxString msg = _("This xml file has no declaration.\n"); msg += _("Would you like wxFormBuilder to backup the file and convert it to UTF-8\?\n"); msg += _("You will be prompted for an encoding.\n\n"); msg += _("Path: "); msg += path; int result = wxMessageBox( msg, _("Missing Declaration"), wxICON_QUESTION | wxYES_NO | wxYES_DEFAULT, wxTheApp->GetTopWindow() ); if ( wxNO == result ) { // User declined, give up THROW_WXFBEX( _("Missing Declaration on XML File: ") << path ); } // User accepted, convert the file wxFontEncoding chosenEncoding = StringUtils::GetEncodingFromUser( _("Please choose the original encoding.") ); if ( wxFONTENCODING_MAX == chosenEncoding ) { THROW_WXFBEX( _("Missing Declaration on XML File: ") << path ); } ConvertAndAddDeclaration( path, chosenEncoding ); // Reload LoadXMLFile( doc, condenseWhiteSpace, path ); return; } // The file will have a declaration at this point wxString version = _WXSTR( declaration->Version() ); if ( version.empty() ) { version = wxT("1.0"); } wxString standalone = _WXSTR( declaration->Standalone() ); if ( standalone.empty() ) { standalone = wxT("yes"); } wxString encodingName = _WXSTR( declaration->Encoding() ); if ( encodingName.empty() ) { // Ask user to all wxFB to convert the file to UTF-8 and add the XML declaration wxString msg = _("This xml file has no encoding specified.\n"); msg += _("Would you like wxFormBuilder to backup the file and convert it to UTF-8\?\n"); msg += _("You will be prompted for an encoding.\n\n"); msg += _("Path: "); msg += path; if ( wxNO == wxMessageBox( msg, _("Unknown Encoding"), wxICON_QUESTION | wxYES_NO | wxYES_DEFAULT, wxTheApp->GetTopWindow() ) ) { // User declined, give up THROW_WXFBEX( _("Unknown Encoding for XML File: ") << path ); } // User accepted, convert the file wxFontEncoding chosenEncoding = StringUtils::GetEncodingFromUser( _("Please choose the original encoding.") ); if ( wxFONTENCODING_MAX == chosenEncoding ) { THROW_WXFBEX( _("Unknown Encoding for XML File: ") << path ); } ConvertAndChangeDeclaration( path, version, standalone, chosenEncoding ); // Reload LoadXMLFile( doc, condenseWhiteSpace, path ); return; } // The file will have an encoding at this point wxFontEncoding encoding = wxFontMapperBase::GetEncodingFromName( encodingName.MakeLower() ); if ( wxFONTENCODING_UTF8 == encoding ) { // This is what we want return; } else if ( wxFONTENCODING_MAX == encoding ) { wxString msg = wxString::Format( _("The encoding of this xml file is not supported.\n\nFile: %s\nEncoding: %s\nSupported Encodings:\n\n%s"), path.c_str(), encodingName.c_str(), StringUtils::GetSupportedEncodings().c_str() ); wxMessageBox( msg, wxString::Format( _("Unsupported Encoding: %s"), encodingName.c_str() ) ); THROW_WXFBEX( _("Unsupported encoding for XML File: ") << path ); } else { // Ask user to all wxFB to convert the file to UTF-8 and add the XML declaration wxString msg = wxString::Format( _("This xml file has specified encoding %s. wxFormBuilder only works with UTF-8.\n"), wxFontMapper::GetEncodingDescription( encoding ).c_str() ); msg += _("Would you like wxFormBuilder to backup the file and convert it to UTF-8\?\n\n"); msg += _("Path: "); msg += path; if ( wxNO == wxMessageBox( msg, _("Not UTF-8"), wxICON_QUESTION | wxYES_NO | wxYES_DEFAULT, wxTheApp->GetTopWindow() ) ) { // User declined, give up THROW_WXFBEX( _("Wrong Encoding for XML File: ") << path ); } // User accepted, convert the file ConvertAndChangeDeclaration( path, version, standalone, encoding ); // Reload LoadXMLFile( doc, condenseWhiteSpace, path ); return; } }