00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #include "a2dprec.h"
00015
00016 #ifdef __BORLANDC__
00017 #pragma hdrstop
00018 #endif
00019
00020 #ifndef WX_PRECOMP
00021 #include "wx/wx.h"
00022 #endif
00023
00024 #include "a2dprivate.h"
00025
00026 #if wxART2D_USE_CVGIO
00027
00028 #include "wx/canvas/canglob.h"
00029 #include "wx/canvas/algos.h"
00030 #include "wx/canvas/xmlpars.h"
00031 #include "wx/canvas/candoc.h"
00032
00033
00034
00035
00036
00037 a2dIOHandlerCVGIn::a2dIOHandlerCVGIn()
00038 {
00039
00040 m_objectCreate[ wxT("a2dOneColourFill") ] = wxT("a2dFill");
00041 m_objectCreate[ wxT("a2dLinearGradientFill") ] = wxT("a2dFill");
00042 m_objectCreate[ wxT("a2dRadialGradientFill") ] = wxT("a2dFill");
00043 m_objectCreate[ wxT("a2dOneColourStroke") ] = wxT("a2dStroke");
00044 m_docClassInfo = &a2dCanvasDocument::ms_classInfo;
00045 }
00046
00047
00048 a2dIOHandlerCVGIn::~a2dIOHandlerCVGIn()
00049 {
00050 }
00051
00052 bool a2dIOHandlerCVGIn::CanLoad( a2dDocumentInputStream& stream, const wxObject* obj, wxClassInfo* docClassInfo )
00053 {
00054 if ( obj && !wxDynamicCast( obj, a2dCanvasDocument) && !wxDynamicCast( obj, a2dLayers) )
00055 return false;
00056
00057 if ( docClassInfo && m_docClassInfo && !docClassInfo->IsKindOf( m_docClassInfo ) )
00058 return false;
00059
00060 m_streami = &stream;
00061 SeekI( 0 );
00062 char cheader[170];
00063 cheader[169] = 0;
00064
00065 Read(cheader, 169);
00066 SeekI(0);
00067
00068
00069 if ( memcmp(cheader, "<?xml ", 6) == 0)
00070 {
00071 wxString buf = XmlDecodeStringToString( cheader, 169 );
00072
00073
00074 int pos;
00075 pos = buf.Find(wxT("<cvg"));
00076 if (pos == wxNOT_FOUND)
00077 return false;
00078
00079 pos = buf.Find(wxT("classname"));
00080 if (pos == wxNOT_FOUND)
00081 return false;
00082
00083 #if wxUSE_STD_IOSTREAM
00084 return (m_streami->fail() || m_streami->bad()) == 0;
00085 #else
00086 return true;
00087 #endif
00088 }
00089
00090 return false;
00091 }
00092
00093 bool a2dIOHandlerCVGIn::LoadLayers( a2dDocumentInputStream& stream, a2dLayers* layers )
00094 {
00095 bool oke = true;
00096
00097 m_streami = &stream;
00098
00099 InitializeLoad();
00100
00101 try
00102 {
00103
00104 Next();
00105 Require( START_TAG, wxT("cvg") );
00106 Next();
00107 if ( GetTagName() == wxT("title") )
00108 {
00109 Next();
00110 Require( END_TAG, wxT("title") );
00111 Next();
00112 }
00113 if ( GetTagName() == wxT("desc") )
00114 {
00115 Next();
00116 Require( END_TAG, wxT("desc") );
00117 Next();
00118 }
00119
00120 if ( GetTagName() == wxT("o") && GetAttributeValue( wxT("classname") ) == wxT("a2dLayers") )
00121 {
00122
00123 a2dResolveIdList toResolve;
00124 layers->Load( NULL, *this );
00125 }
00126
00127 Require( END_TAG, wxT("cvg") );
00128 Next();
00129
00130
00131
00132 LinkReferences();
00133 layers->UpdateIndexes();
00134 }
00135 catch (a2dIOHandlerXMLException& error)
00136 {
00137 a2dGeneralGlobals->ReportErrorF( a2dError_XMLparse, _("CVG parsing error: '%s' at line %d column %d"), error.getMessage().c_str(), error.getLineNumber(), error.getColumnNumber() );
00138 oke = false;
00139 }
00140
00141 ResetLoad();
00142 return oke;
00143 }
00144
00145
00146
00147 bool a2dIOHandlerCVGIn::Load( a2dDocumentStringInputStream& stream, a2dCanvasDocument* doc, a2dCanvasObject* parent )
00148 {
00149 a2dCanvasObject* readobject;
00150 m_streami = &stream;
00151
00152 bool oke = true;
00153
00154 InitializeLoad();
00155
00156 try
00157 {
00158 a2dResolveIdList toResolve;
00159
00160 Next();
00161 Require( START_TAG, wxT("cvg") );
00162 Next();
00163 if ( GetTagName() == wxT("title") )
00164 {
00165 doc->SetTitle(GetContent());
00166 Next();
00167 Require( END_TAG, wxT("title") );
00168 Next();
00169 }
00170 if ( GetTagName() == wxT("desc") )
00171 {
00172 doc->SetDescription(GetContent());
00173 Next();
00174 Require( END_TAG, wxT("desc") );
00175 Next();
00176 }
00177
00178 if ( GetTagName() == wxT("a2dLayers") )
00179 {
00180
00181 m_doc->GetLayerSetup()->Load( NULL, *this );
00182 }
00183
00184
00185
00186
00187 while( GetEventType() == START_TAG && GetTagName() == wxT("o") )
00188 {
00189 wxString classname = GetAttributeValue( wxT("classname") );
00190 readobject = (a2dCanvasObject*) CreateObject( classname );
00191 if ( !readobject )
00192 {
00193 a2dGeneralGlobals->ReportErrorF( a2dError_XMLparse, _("could not create a2dCanvasObject %s, will be skipped line %d"),
00194 GetAttributeValue( wxT("classname") ).c_str(), GetCurrentLineNumber() );
00195 return false;
00196 }
00197 else
00198 {
00199 parent->Append( readobject );
00200 readobject->Load( NULL, *this );
00201 }
00202 }
00203
00204 Require( END_TAG, wxT("cvg") );
00205 Next();
00206
00207
00208
00209 LinkReferences();
00210 }
00211 catch (a2dIOHandlerXMLException& error)
00212 {
00213 a2dGeneralGlobals->ReportErrorF( a2dError_XMLparse, _("CVG parsing error: '%s' at line %d column %d"), error.getMessage().c_str(), error.getLineNumber(), error.getColumnNumber() );
00214 oke = false;
00215 }
00216
00217 ResetLoad();
00218
00219 return oke;
00220 }
00221
00222
00223
00224
00225
00226 a2dIOHandlerCVGOut::a2dIOHandlerCVGOut()
00227 {
00228 }
00229
00230
00231 a2dIOHandlerCVGOut::~a2dIOHandlerCVGOut()
00232 {
00233 }
00234
00235 bool a2dIOHandlerCVGOut::SaveLayers( a2dDocumentOutputStream& stream, a2dLayers* layers )
00236 {
00237 m_streamo = &stream;
00238
00239 #if wxUSE_STD_IOSTREAM
00240 if (stream.fail() || stream.bad())
00241 {
00242 #else
00243 if ( !stream.IsOk() )
00244 {
00245 #endif
00246 a2dGeneralGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open stream for saving") );
00247 return false;
00248 }
00249 InitializeSave();
00250
00251
00252 a2dWalker_SetBoolProperty setp( PROPID_Check, false );
00253 setp.Start( layers );
00254
00255 WriteStartDocument( wxT("1.0"), wxT("UTF-8") , true );
00256
00257 WriteStartElementAttributes( wxT("cvg") );
00258 WriteAttribute( wxT("classname"), GetClassInfo()->GetClassName() );
00259 WriteEndAttributes();
00260
00261 WriteElement( wxT("title"), wxT("layersettings") );
00262 WriteElement( wxT("desc"), wxT("description") );
00263
00264 a2dObjectList towritelayer;
00265 layers->Save( NULL, *this, &towritelayer );
00266
00267 WriteEndElement();
00268 WriteEndDocument();
00269
00270 ResetSave();
00271
00272 return true;
00273 }
00274
00275 bool a2dIOHandlerCVGOut::SaveStartAt( a2dDocumentOutputStream& stream, const a2dCanvasDocument* doc, a2dCanvasObject* start )
00276 {
00277 m_doc = (a2dCanvasDocument*) doc;
00278 m_streamo = &stream;
00279
00280 #if wxUSE_STD_IOSTREAM
00281 if (stream.fail() || stream.bad())
00282 {
00283 #else
00284 if ( !stream.IsOk() )
00285 {
00286 #endif
00287 a2dGeneralGlobals->ReportErrorF( a2dError_FileCouldNotOpen, _("Sorry, could not open stream for saving") );
00288 return false;
00289 }
00290
00291 InitializeSave();
00292 m_doc->Save( *this, start );
00293 ResetSave();
00294
00295 return true;
00296 }
00297
00298 void a2dIOHandlerCVGOut::WriteCvgStartDocument( a2dDocumentOutputStream& stream )
00299 {
00300 m_streamo = &stream;
00301
00302 InitializeSave();
00303
00304 WriteStartDocument( wxT("1.0"), wxT("UTF-8") , true );
00305
00306 WriteStartElementAttributes( wxT("cvg") );
00307 WriteAttribute( wxT("classname"), GetClassInfo()->GetClassName() );
00308 WriteEndAttributes();
00309
00310 WriteElement( wxT("title"), wxT("") );
00311 WriteElement( wxT("desc"), wxT("") );
00312 }
00313
00314 void a2dIOHandlerCVGOut::WriteCvgEndDocument()
00315 {
00316 WriteEndElement();
00317 WriteEndDocument();
00318 ResetSave( );
00319 }
00320
00321 void a2dIOHandlerCVGOut::WriteObject( a2dCanvasObject* start )
00322 {
00323
00324 a2dWalker_SetBoolProperty setp( PROPID_Check, false );
00325 setp.Start( start );
00326
00327 a2dObjectList towrite;
00328 towrite.push_back( start );
00329
00330 a2dObjectList::iterator iter = towrite.begin();
00331 while ( towrite.size() )
00332 {
00333 a2dObject *obj = *iter;
00334 obj->Save( NULL, *this, &towrite );
00335 towrite.erase( iter );
00336 iter = towrite.begin();
00337 }
00338 }
00339
00340 #endif