2
* This file is part of wxSmith plugin for Code::Blocks Studio
3
* Copyright (C) 2006-2007 Bartlomiej Swiecki
5
* wxSmith is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 3 of the License, or
8
* (at your option) any later version.
10
* wxSmith is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with wxSmith. If not, see <http://www.gnu.org/licenses/>.
19
* $Id: wxsproject.cpp 4850 2008-01-29 21:45:49Z byo $
20
* $HeadURL: svn://svn.berlios.de/codeblocks/tags/8.02/src/plugins/contrib/wxSmith/wxsproject.cpp $
23
#include "wxsproject.h"
25
#include "wxsresource.h"
26
#include "wxsresourcefactory.h"
27
#include "wxsguifactory.h"
29
#include "wxsversionconverter.h"
31
#include <wx/string.h>
32
#include <logmanager.h>
36
const int CurrentVersion = 1;
37
const char* CurrentVersionStr = "1";
40
wxsProject::wxsProject(cbProject* Project):
43
m_UnknownConfig("unknown_config"),
44
m_UnknownResources("unknown_resource"),
45
m_WasModifiedDuringLoad(false)
47
// Creating resource tree entery for this project
48
m_TreeItem = wxsTree()->NewProjectItem(GetCBProject()->GetTitle(),this);
51
wxFileName PathBuilder(Project->GetFilename());
52
m_ProjectPath = PathBuilder.GetPath(wxPATH_GET_VOLUME|wxPATH_GET_SEPARATOR);
55
wxsProject::~wxsProject()
60
for ( size_t i=m_Resources.Count(); i-->0; )
62
delete m_Resources[i];
67
wxsTree()->Delete(m_TreeItem);
71
void wxsProject::ReadConfiguration(TiXmlElement* element)
73
TiXmlElement* SmithNode = element->FirstChildElement("wxsmith");
74
if ( !SmithNode ) return;
76
TiXmlDocument TempDoc;
79
if ( wxsVersionConverter::Get().DetectOldConfig(SmithNode,this) )
81
TiXmlElement* ConvertedSmithNode = wxsVersionConverter::Get().ConvertFromOldConfig(SmithNode,&TempDoc,this);
82
if ( !ConvertedSmithNode )
84
for ( TiXmlNode* Node = SmithNode->FirstChild(); Node; Node=Node->NextSibling() )
86
m_UnknownConfig.InsertEndChild(*Node);
92
SmithNode = ConvertedSmithNode;
93
m_WasModifiedDuringLoad = true;
97
const char* VersionStr = SmithNode->Attribute("version");
98
int Version = VersionStr ? atoi(VersionStr) : 1;
100
if ( Version > CurrentVersion )
102
// TODO: Show some dialog box that resources were created by newer version,
103
// store all configuration for later save and return
107
if ( Version < CurrentVersion )
109
SmithNode = wxsVersionConverter::Get().Convert(SmithNode,&TempDoc,this);
112
// TODO: Show some dialog box that resources were created by newer version,
113
// store all configuration for later save and return
118
m_WasModifiedDuringLoad = true;
122
// Iterating through elements
123
for ( TiXmlElement* Node = SmithNode->FirstChildElement(); Node; Node=Node->NextSiblingElement() )
125
wxString NodeValue = cbC2U(Node->Value());
126
if ( NodeValue == _T("gui") )
128
wxString GUIName = cbC2U(Node->Attribute("name"));
129
wxsGUI* NewGUI = wxsGUIFactory::Build(GUIName,this);
132
m_UnknownConfig.InsertEndChild(*Node);
140
NewGUI->ReadConfig(Node);
144
else if ( NodeValue == _T("resources") )
146
for ( TiXmlElement* ResNode = Node->FirstChildElement(); ResNode; ResNode = ResNode->NextSiblingElement() )
148
wxString Type = cbC2U(ResNode->Value());
149
wxsResource* Res = wxsResourceFactory::Build(Type,this);
153
// Storing unknown Xml Element
154
if ( !Res->ReadConfig(ResNode) )
156
m_UnknownResources.InsertEndChild(*ResNode);
161
m_Resources.Add(Res);
162
Res->BuildTreeEntry(GetResourceTypeTreeId(Type));
167
m_UnknownResources.InsertEndChild(*ResNode);
173
m_UnknownConfig.InsertEndChild(*Node);
178
void wxsProject::WriteConfiguration(TiXmlElement* element)
180
TiXmlElement* SmithElement = element->FirstChildElement("wxsmith");
182
if ( !m_GUI && m_Resources.empty() && m_UnknownConfig.NoChildren() && m_UnknownResources.NoChildren() )
187
element->RemoveChild(SmithElement);
194
SmithElement = element->InsertEndChild(TiXmlElement("wxsmith"))->ToElement();
196
SmithElement->Clear();
197
SmithElement->SetAttribute("version",CurrentVersionStr);
202
TiXmlElement* GUIElement = SmithElement->InsertEndChild(TiXmlElement("gui"))->ToElement();
203
GUIElement->SetAttribute("name",cbU2C(m_GUI->GetName()));
204
m_GUI->WriteConfig(GUIElement);
208
if ( !m_Resources.empty() || !m_UnknownResources.NoChildren() )
210
TiXmlElement* ResElement = SmithElement->InsertEndChild(TiXmlElement("resources"))->ToElement();
211
size_t Count = m_Resources.Count();
212
for ( size_t i=0; i<Count; i++ )
214
const wxString& Name = m_Resources[i]->GetResourceName();
215
const wxString& Type = m_Resources[i]->GetResourceType();
216
TiXmlElement* Element = ResElement->InsertEndChild(TiXmlElement(cbU2C(Type)))->ToElement();
217
// TODO: Check value returned from WriteConfig
218
m_Resources[i]->WriteConfig(Element);
219
Element->SetAttribute("name",cbU2C(Name));
222
// Saving all unknown resources
223
for ( TiXmlNode* Node = m_UnknownResources.FirstChild(); Node; Node=Node->NextSibling() )
225
SmithElement->InsertEndChild(*Node);
229
// Saving all unknown configuration nodes
230
for ( TiXmlNode* Node = m_UnknownConfig.FirstChild(); Node; Node=Node->NextSibling() )
232
SmithElement->InsertEndChild(*Node);
237
bool wxsProject::AddResource(wxsResource* NewResource)
239
if ( NewResource == 0 )
244
const wxString& Type = NewResource->GetResourceType();
245
const wxString& Name = NewResource->GetResourceName();
247
if ( FindResource(Name) != 0 )
252
m_Resources.Add(NewResource);
253
wxsResourceItemId Id = GetResourceTypeTreeId(Type);
254
NewResource->BuildTreeEntry(Id);
255
m_Project->SetModified(true);
259
bool wxsProject::DelResource(wxsResource* Resource)
261
int Index = m_Resources.Index(Resource);
262
if ( Index == wxNOT_FOUND ) return false;
265
m_Resources.RemoveAt(Index);
266
m_Project->SetModified(true);
270
wxsResource* wxsProject::FindResource(const wxString& Name)
272
for ( size_t i = m_Resources.Count(); i-->0; )
274
if ( m_Resources[i]->GetResourceName() == Name )
276
return m_Resources[i];
283
void wxsProject::Configure()
287
m_GUI = wxsGUIFactory::SelectNew(_("wxSmith does not manage any GUI for this project.\nPlease select GUI you want to be managed in wxSmith."),this);
296
if ( !m_GUI->CheckIfApplicationManaged() )
298
// TODO: Prepare better communicate, consider chancing to cbAnnoyingDiaog
299
if ( wxMessageBox(_("wxSmith does not manage this application's source.\n"
300
"Should I create proper bindings?"),_("wxSmith"),wxYES_NO) == wxNO ) return;
301
if ( !m_GUI->CreateApplicationBinding() ) return;
303
cbConfigurationDialog Dlg(0,-1,_("Configuring wxSmith"));
304
Dlg.AttachConfigurationPanel(m_GUI->BuildConfigurationPanel(&Dlg));
309
cbConfigurationPanel* wxsProject::GetProjectConfigurationPanel(wxWindow* parent)
313
if ( m_GUI->CheckIfApplicationManaged() )
315
return m_GUI->BuildConfigurationPanel(parent);
321
wxString wxsProject::GetProjectPath()
323
return m_ProjectPath;
326
bool wxsProject::CanOpenEditor(const wxString& FileName)
328
for ( size_t i=m_Resources.Count(); i-->0; )
330
if ( m_Resources[i]->OnCanHandleFile(FileName) )
338
bool wxsProject::TryOpenEditor(const wxString& FileName)
340
for ( size_t i=m_Resources.Count(); i-->0; )
342
if ( m_Resources[i]->OnCanHandleFile(FileName) )
344
m_Resources[i]->EditOpen();
351
void wxsProject::SetGUI(wxsGUI* NewGUI)
353
if ( m_GUI != NewGUI )
360
void wxsProject::NotifyChange()
362
return m_Project->SetModified(true);
365
wxsResourceItemId wxsProject::GetResourceTypeTreeId(const wxString& Name)
367
if ( m_ResBrowserIds.find(Name) != m_ResBrowserIds.end() )
369
return m_ResBrowserIds[Name];
371
return m_ResBrowserIds[Name] = wxsTree()->AppendItem(m_TreeItem,Name,wxsResourceFactory::ResourceTreeIcon(Name));
374
void wxsProject::UpdateName()
376
wxsResourceTree::Get()->SetItemText(m_TreeItem,GetCBProject()->GetTitle());