~ubuntu-branches/ubuntu/hardy/codeblocks/hardy-backports

« back to all changes in this revision

Viewing changes to src/plugins/contrib/wxSmith/wxsproject.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michael Casadevall
  • Date: 2008-07-17 04:39:23 UTC
  • Revision ID: james.westby@ubuntu.com-20080717043923-gmsy5cwkdjswghkm
Tags: upstream-8.02
ImportĀ upstreamĀ versionĀ 8.02

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
* This file is part of wxSmith plugin for Code::Blocks Studio
 
3
* Copyright (C) 2006-2007  Bartlomiej Swiecki
 
4
*
 
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.
 
9
*
 
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.
 
14
*
 
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/>.
 
17
*
 
18
* $Revision: 4850 $
 
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 $
 
21
*/
 
22
 
 
23
#include "wxsproject.h"
 
24
#include "wxsmith.h"
 
25
#include "wxsresource.h"
 
26
#include "wxsresourcefactory.h"
 
27
#include "wxsguifactory.h"
 
28
#include "wxsgui.h"
 
29
#include "wxsversionconverter.h"
 
30
 
 
31
#include <wx/string.h>
 
32
#include <logmanager.h>
 
33
 
 
34
namespace
 
35
{
 
36
    const int CurrentVersion = 1;
 
37
    const char* CurrentVersionStr = "1";
 
38
}
 
39
 
 
40
wxsProject::wxsProject(cbProject* Project):
 
41
    m_Project(Project),
 
42
    m_GUI(0),
 
43
    m_UnknownConfig("unknown_config"),
 
44
    m_UnknownResources("unknown_resource"),
 
45
    m_WasModifiedDuringLoad(false)
 
46
{
 
47
    // Creating resource tree entery for this project
 
48
    m_TreeItem = wxsTree()->NewProjectItem(GetCBProject()->GetTitle(),this);
 
49
 
 
50
    // Building paths
 
51
    wxFileName PathBuilder(Project->GetFilename());
 
52
    m_ProjectPath = PathBuilder.GetPath(wxPATH_GET_VOLUME|wxPATH_GET_SEPARATOR);
 
53
}
 
54
 
 
55
wxsProject::~wxsProject()
 
56
{
 
57
    delete m_GUI;
 
58
    m_GUI = 0;
 
59
 
 
60
    for ( size_t i=m_Resources.Count(); i-->0; )
 
61
    {
 
62
        delete m_Resources[i];
 
63
        m_Resources[i] = 0;
 
64
    }
 
65
    m_Resources.Clear();
 
66
 
 
67
    wxsTree()->Delete(m_TreeItem);
 
68
    wxsTree()->Refresh();
 
69
}
 
70
 
 
71
void wxsProject::ReadConfiguration(TiXmlElement* element)
 
72
{
 
73
    TiXmlElement* SmithNode = element->FirstChildElement("wxsmith");
 
74
    if ( !SmithNode ) return;
 
75
 
 
76
    TiXmlDocument TempDoc;
 
77
 
 
78
    // Checking version
 
79
    if ( wxsVersionConverter::Get().DetectOldConfig(SmithNode,this) )
 
80
    {
 
81
        TiXmlElement* ConvertedSmithNode = wxsVersionConverter::Get().ConvertFromOldConfig(SmithNode,&TempDoc,this);
 
82
        if ( !ConvertedSmithNode )
 
83
        {
 
84
            for ( TiXmlNode* Node = SmithNode->FirstChild(); Node; Node=Node->NextSibling() )
 
85
            {
 
86
                m_UnknownConfig.InsertEndChild(*Node);
 
87
            }
 
88
            return;
 
89
        }
 
90
        else
 
91
        {
 
92
            SmithNode = ConvertedSmithNode;
 
93
            m_WasModifiedDuringLoad = true;
 
94
        }
 
95
    }
 
96
 
 
97
    const char* VersionStr = SmithNode->Attribute("version");
 
98
    int Version = VersionStr ? atoi(VersionStr) : 1;
 
99
 
 
100
    if ( Version > CurrentVersion )
 
101
    {
 
102
        // TODO: Show some dialog box that resources were created by newer version,
 
103
        //       store all configuration for later save and return
 
104
        return;
 
105
    }
 
106
 
 
107
    if ( Version < CurrentVersion )
 
108
    {
 
109
        SmithNode = wxsVersionConverter::Get().Convert(SmithNode,&TempDoc,this);
 
110
        if ( !SmithNode )
 
111
        {
 
112
            // TODO: Show some dialog box that resources were created by newer version,
 
113
            //       store all configuration for later save and return
 
114
            return;
 
115
        }
 
116
        else
 
117
        {
 
118
            m_WasModifiedDuringLoad = true;
 
119
        }
 
120
    }
 
121
 
 
122
    // Iterating through elements
 
123
    for ( TiXmlElement* Node = SmithNode->FirstChildElement(); Node; Node=Node->NextSiblingElement() )
 
124
    {
 
125
        wxString NodeValue = cbC2U(Node->Value());
 
126
        if ( NodeValue == _T("gui") )
 
127
        {
 
128
            wxString GUIName = cbC2U(Node->Attribute("name"));
 
129
            wxsGUI* NewGUI = wxsGUIFactory::Build(GUIName,this);
 
130
            if ( !NewGUI )
 
131
            {
 
132
                m_UnknownConfig.InsertEndChild(*Node);
 
133
            }
 
134
            else
 
135
            {
 
136
                delete m_GUI;
 
137
                m_GUI = NewGUI;
 
138
                if ( NewGUI )
 
139
                {
 
140
                    NewGUI->ReadConfig(Node);
 
141
                }
 
142
            }
 
143
        }
 
144
        else if ( NodeValue == _T("resources") )
 
145
        {
 
146
            for ( TiXmlElement* ResNode = Node->FirstChildElement(); ResNode; ResNode = ResNode->NextSiblingElement() )
 
147
            {
 
148
                wxString Type = cbC2U(ResNode->Value());
 
149
                wxsResource* Res = wxsResourceFactory::Build(Type,this);
 
150
 
 
151
                if ( Res )
 
152
                {
 
153
                    // Storing unknown Xml Element
 
154
                    if ( !Res->ReadConfig(ResNode) )
 
155
                    {
 
156
                        m_UnknownResources.InsertEndChild(*ResNode);
 
157
                        delete Res;
 
158
                    }
 
159
                    else
 
160
                    {
 
161
                        m_Resources.Add(Res);
 
162
                        Res->BuildTreeEntry(GetResourceTypeTreeId(Type));
 
163
                    }
 
164
                }
 
165
                else
 
166
                {
 
167
                    m_UnknownResources.InsertEndChild(*ResNode);
 
168
                }
 
169
            }
 
170
        }
 
171
        else
 
172
        {
 
173
            m_UnknownConfig.InsertEndChild(*Node);
 
174
        }
 
175
    }
 
176
}
 
177
 
 
178
void wxsProject::WriteConfiguration(TiXmlElement* element)
 
179
{
 
180
    TiXmlElement* SmithElement = element->FirstChildElement("wxsmith");
 
181
 
 
182
    if ( !m_GUI && m_Resources.empty() && m_UnknownConfig.NoChildren() && m_UnknownResources.NoChildren() )
 
183
    {
 
184
        // Nothing to write
 
185
        if ( SmithElement )
 
186
        {
 
187
            element->RemoveChild(SmithElement);
 
188
        }
 
189
        return;
 
190
    }
 
191
 
 
192
    if ( !SmithElement )
 
193
    {
 
194
                SmithElement = element->InsertEndChild(TiXmlElement("wxsmith"))->ToElement();
 
195
    }
 
196
        SmithElement->Clear();
 
197
    SmithElement->SetAttribute("version",CurrentVersionStr);
 
198
 
 
199
    // saving GUI item
 
200
    if ( m_GUI )
 
201
    {
 
202
        TiXmlElement* GUIElement = SmithElement->InsertEndChild(TiXmlElement("gui"))->ToElement();
 
203
        GUIElement->SetAttribute("name",cbU2C(m_GUI->GetName()));
 
204
        m_GUI->WriteConfig(GUIElement);
 
205
    }
 
206
 
 
207
    // saving resources
 
208
    if ( !m_Resources.empty() || !m_UnknownResources.NoChildren() )
 
209
    {
 
210
        TiXmlElement* ResElement = SmithElement->InsertEndChild(TiXmlElement("resources"))->ToElement();
 
211
        size_t Count = m_Resources.Count();
 
212
        for ( size_t i=0; i<Count; i++ )
 
213
        {
 
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));
 
220
        }
 
221
 
 
222
        // Saving all unknown resources
 
223
        for ( TiXmlNode* Node = m_UnknownResources.FirstChild(); Node; Node=Node->NextSibling() )
 
224
        {
 
225
            SmithElement->InsertEndChild(*Node);
 
226
        }
 
227
    }
 
228
 
 
229
    // Saving all unknown configuration nodes
 
230
    for ( TiXmlNode* Node = m_UnknownConfig.FirstChild(); Node; Node=Node->NextSibling() )
 
231
    {
 
232
        SmithElement->InsertEndChild(*Node);
 
233
    }
 
234
 
 
235
}
 
236
 
 
237
bool wxsProject::AddResource(wxsResource* NewResource)
 
238
{
 
239
    if ( NewResource == 0 )
 
240
    {
 
241
        return false;
 
242
    }
 
243
 
 
244
    const wxString& Type = NewResource->GetResourceType();
 
245
    const wxString& Name = NewResource->GetResourceName();
 
246
 
 
247
    if ( FindResource(Name) != 0 )
 
248
    {
 
249
        return false;
 
250
    }
 
251
 
 
252
    m_Resources.Add(NewResource);
 
253
    wxsResourceItemId Id = GetResourceTypeTreeId(Type);
 
254
    NewResource->BuildTreeEntry(Id);
 
255
    m_Project->SetModified(true);
 
256
    return true;
 
257
}
 
258
 
 
259
bool wxsProject::DelResource(wxsResource* Resource)
 
260
{
 
261
    int Index = m_Resources.Index(Resource);
 
262
    if ( Index == wxNOT_FOUND ) return false;
 
263
 
 
264
    delete Resource;
 
265
    m_Resources.RemoveAt(Index);
 
266
    m_Project->SetModified(true);
 
267
    return true;
 
268
}
 
269
 
 
270
wxsResource* wxsProject::FindResource(const wxString& Name)
 
271
{
 
272
    for ( size_t i = m_Resources.Count(); i-->0; )
 
273
    {
 
274
        if ( m_Resources[i]->GetResourceName() == Name )
 
275
        {
 
276
            return m_Resources[i];
 
277
        }
 
278
    }
 
279
 
 
280
    return 0;
 
281
}
 
282
 
 
283
void wxsProject::Configure()
 
284
{
 
285
    if ( !m_GUI )
 
286
    {
 
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);
 
288
        if ( m_GUI )
 
289
        {
 
290
            NotifyChange();
 
291
        }
 
292
    }
 
293
 
 
294
    if ( m_GUI )
 
295
    {
 
296
        if ( !m_GUI->CheckIfApplicationManaged() )
 
297
        {
 
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;
 
302
        }
 
303
        cbConfigurationDialog Dlg(0,-1,_("Configuring wxSmith"));
 
304
        Dlg.AttachConfigurationPanel(m_GUI->BuildConfigurationPanel(&Dlg));
 
305
        Dlg.ShowModal();
 
306
    }
 
307
}
 
308
 
 
309
cbConfigurationPanel* wxsProject::GetProjectConfigurationPanel(wxWindow* parent)
 
310
{
 
311
    if ( m_GUI )
 
312
    {
 
313
        if ( m_GUI->CheckIfApplicationManaged() )
 
314
        {
 
315
            return m_GUI->BuildConfigurationPanel(parent);
 
316
        }
 
317
    }
 
318
    return 0;
 
319
}
 
320
 
 
321
wxString wxsProject::GetProjectPath()
 
322
{
 
323
    return m_ProjectPath;
 
324
}
 
325
 
 
326
bool wxsProject::CanOpenEditor(const wxString& FileName)
 
327
{
 
328
    for ( size_t i=m_Resources.Count(); i-->0; )
 
329
    {
 
330
        if ( m_Resources[i]->OnCanHandleFile(FileName) )
 
331
        {
 
332
            return true;
 
333
        }
 
334
    }
 
335
    return false;
 
336
}
 
337
 
 
338
bool wxsProject::TryOpenEditor(const wxString& FileName)
 
339
{
 
340
    for ( size_t i=m_Resources.Count(); i-->0; )
 
341
    {
 
342
        if ( m_Resources[i]->OnCanHandleFile(FileName) )
 
343
        {
 
344
            m_Resources[i]->EditOpen();
 
345
            return true;
 
346
        }
 
347
    }
 
348
    return false;
 
349
}
 
350
 
 
351
void wxsProject::SetGUI(wxsGUI* NewGUI)
 
352
{
 
353
    if ( m_GUI != NewGUI )
 
354
    {
 
355
        delete m_GUI;
 
356
        m_GUI = NewGUI;
 
357
    }
 
358
}
 
359
 
 
360
void wxsProject::NotifyChange()
 
361
{
 
362
    return m_Project->SetModified(true);
 
363
}
 
364
 
 
365
wxsResourceItemId wxsProject::GetResourceTypeTreeId(const wxString& Name)
 
366
{
 
367
    if ( m_ResBrowserIds.find(Name) != m_ResBrowserIds.end() )
 
368
    {
 
369
        return m_ResBrowserIds[Name];
 
370
    }
 
371
    return m_ResBrowserIds[Name] = wxsTree()->AppendItem(m_TreeItem,Name,wxsResourceFactory::ResourceTreeIcon(Name));
 
372
}
 
373
 
 
374
void wxsProject::UpdateName()
 
375
{
 
376
    wxsResourceTree::Get()->SetItemText(m_TreeItem,GetCBProject()->GetTitle());
 
377
}