PObjectBase wxFBDataObject::GetObj() { if ( m_data.empty() ) { return PObjectBase(); } // Read Object from xml try { ticpp::Document doc; doc.Parse( m_data, true, TIXML_ENCODING_UTF8 ); ticpp::Element* element = doc.FirstChildElement(); int major, minor; element->GetAttribute( "fbp_version_major", &major ); element->GetAttribute( "fbp_version_minor", &minor ); if ( major > AppData()->m_fbpVerMajor || ( AppData()->m_fbpVerMajor == major && minor > AppData()->m_fbpVerMinor ) ) { wxLogError( _("This object cannot be pasted because it is from a newer version of wxFormBuilder") ); } if ( major < AppData()->m_fbpVerMajor || ( AppData()->m_fbpVerMajor == major && minor < AppData()->m_fbpVerMinor ) ) { AppData()->ConvertObject( element, major, minor ); } PObjectDatabase db = AppData()->GetObjectDatabase(); return db->CreateObject( element ); } catch( ticpp::Exception& ex ) { wxLogError( _WXSTR( ex.m_details ) ); return PObjectBase(); } }
PObjectBase XrcLoader::GetObject( ticpp::Element *xrcObj, PObjectBase parent ) { // First, create the object by the name, the modify the properties std::string className = xrcObj->GetAttribute( "class" ); if ( parent->GetObjectTypeName() == wxT( "project" ) ) { if ( className == "wxBitmap" ) { PProperty bitmapsProp = parent->GetProperty( _( "bitmaps" ) ); if ( bitmapsProp ) { wxString value = bitmapsProp->GetValue(); wxString text = _WXSTR( xrcObj->GetText() ); text.Replace( wxT( "\'" ), wxT( "\'\'" ), true ); value << wxT( "\'" ) << text << wxT( "\' " ); bitmapsProp->SetValue( value ); return PObjectBase(); } } if ( className == "wxIcon" ) { PProperty iconsProp = parent->GetProperty( _( "icons" ) ); if ( iconsProp ) { wxString value = iconsProp->GetValue(); wxString text = _WXSTR( xrcObj->GetText() ); text.Replace( wxT( "\'" ), wxT( "\'\'" ), true ); value << wxT( "\'" ) << text << wxT( "\' " ); iconsProp->SetValue( value ); return PObjectBase(); } } // Forms wxPanel, wxFrame, wxDialog are stored internally as Panel, Frame, and Dialog // to prevent conflicts with wxPanel as a container className = className.substr( 2, className.size() - 2 ); } // Well, this is not nice. wxMenu class name is ambiguous, so we'll get the // correct class by the context. If the parent of a wxMenu is another wxMenu // then the class name will be "submenu" else if ( className == "wxMenu" && ( parent->GetClassName() == wxT( "wxMenu" ) || parent->GetClassName() == wxT( "submenu" ) ) ) { className = "submenu"; } // "separator" is also ambiguous - could be a toolbar separator or a menu separator else if ( className == "separator" ) { if ( parent->GetClassName() == wxT( "wxToolBar" ) ) { className = "toolSeparator"; } } // replace "spacer" with "sizeritem" so it will be imported as a "sizeritem" // "sizeritem" is ambiguous - could also be a grid bag sizeritem else if ( className == "spacer" || className == "sizeritem" ) { if ( parent->GetClassName() == wxT( "wxGridBagSizer" ) ) { className = "gbsizeritem"; } else { className = "sizeritem"; } } PObjectBase object; PObjectInfo objInfo = m_objDb->GetObjectInfo( _WXSTR( className ) ); if ( objInfo ) { IComponent *comp = objInfo->GetComponent(); if ( !comp ) { wxLogError( _("No component found for class \"%s\", found on line %i."), _WXSTR( className ).c_str(), xrcObj->Row() ); } else { ticpp::Element *fbObj = comp->ImportFromXrc( xrcObj ); if ( !fbObj ) { wxLogError( _("ImportFromXrc returned NULL for class \"%s\", found on line %i."), _WXSTR( className ).c_str(), xrcObj->Row() ); } else { object = m_objDb->CreateObject( fbObj, parent ); if ( !object ) { // Unable to create the object and add it to the parent - probably needs a sizer PObjectBase newsizer = m_objDb->CreateObject( "wxBoxSizer", parent ); if ( newsizer ) { // It is possible the CreateObject returns an "item" containing the object, e.g. SizerItem or SplitterItem // If that is the case, reassign "object" to the actual object PObjectBase sizer = newsizer; if ( sizer->GetChildCount() > 0 ) { sizer = sizer->GetChild( 0 ); } if ( sizer ) { object = m_objDb->CreateObject( fbObj, sizer ); if ( object ) { parent->AddChild( newsizer ); newsizer->SetParent( parent ); } } } } if ( !object ) { wxLogError( wxT( "CreateObject failed for class \"%s\", with parent \"%s\", found on line %i" ), _WXSTR( className ).c_str(), parent->GetClassName().c_str(), xrcObj->Row() ); } else { // It is possible the CreateObject returns an "item" containing the object, e.g. SizerItem or SplitterItem // If that is the case, reassign "object" to the actual object if ( object && object->GetChildCount() > 0 ) object = object->GetChild( 0 ); if ( object ) { // Recursively import the children ticpp::Element *element = xrcObj->FirstChildElement( "object", false ); while ( element ) { GetObject( element, object ); element = element->NextSiblingElement( "object", false ); } } } } } } else { // Create a wxPanel to represent unknown classes object = m_objDb->CreateObject( "wxPanel", parent ); if ( object ) { parent->AddChild( object ); object->SetParent( parent ); wxLogError( wxT( "Unknown class \"%s\" found on line %i, replaced with a wxPanel" ), _WXSTR( className ).c_str(), xrcObj->Row() ); } else { wxString msg( wxString::Format( wxT( "Unknown class \"%s\" found on line %i, and could not replace with a wxPanel as child of \"%s:%s\"" ), _WXSTR( className ).c_str(), xrcObj->Row(), parent->GetPropertyAsString( wxT( "name" ) ).c_str(), parent->GetClassName().c_str() ) ); wxLogError( msg ); } } return object; }
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; } }
bool XrcCodeGenerator::GenerateCode( PObjectBase project ) { m_cw->Clear(); m_contextMenus.clear(); ticpp::Document doc; ticpp::Declaration decl( "1.0", "UTF-8", "yes" ); doc.LinkEndChild( &decl ); ticpp::Element element( "resource" ); element.SetAttribute( "xmlns", "http://www.wxwindows.org/wxxrc" ); element.SetAttribute( "version", "2.3.0.1" ); // If project is not actually a "Project", generate it if ( project->GetClassName() == wxT("Project") ) { for( unsigned int i = 0; i < project->GetChildCount(); i++ ) { ticpp::Element* child = GetElement( project->GetChild( i ) ); if ( child ) { element.LinkEndChild( child ); delete child; } } } else { ticpp::Element* child = GetElement( project ); if ( child ) { element.LinkEndChild( child ); delete child; } } // generate context menus as top-level menus for( std::vector<ticpp::Element*>::iterator it = m_contextMenus.begin(); it != m_contextMenus.end(); ++it ) { element.LinkEndChild( *it ); delete *it; } doc.LinkEndChild( &element ); TiXmlPrinter printer; printer.SetIndent( "\t" ); #if defined( __WXMSW__ ) printer.SetLineBreak( "\r\n" ); #elif defined( __WXMAC__ ) printer.SetLineBreak( "\r" ); #else printer.SetLineBreak( "\n" ); #endif doc.Accept( &printer ); const std::string& xrcFile = printer.Str(); m_cw->Write( _WXSTR( xrcFile ) ); return true; }