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: wxsitemres.cpp 4850 2008-01-29 21:45:49Z byo $
20
* $HeadURL: svn://svn.berlios.de/codeblocks/tags/8.02/src/plugins/contrib/wxSmith/wxwidgets/wxsitemres.cpp $
23
#include "wxsitemres.h"
24
#include "wxsitemeditor.h"
25
#include "wxsitemfactory.h"
26
#include "wxsitemresdata.h"
27
#include "wxsdeleteitemres.h"
28
#include "../wxscoder.h"
30
#include <projectmanager.h>
31
#include <editormanager.h>
33
IMPLEMENT_CLASS(wxsItemRes,wxWidgetsRes)
37
const wxString CppEmptySource =
39
_T("#include \"$(Include)\"\n")
41
_T("$(InternalHeadersPch)")
42
+ wxsCodeMarks::Beg(wxsCPP,_T("InternalHeaders"),_T("$(ClassName)")) + _T("\n") +
43
+ wxsCodeMarks::End(wxsCPP) + _T("\n")
45
+ wxsCodeMarks::Beg(wxsCPP,_T("IdInit"),_T("$(ClassName)")) + _T("\n") +
46
+ wxsCodeMarks::End(wxsCPP) + _T("\n")
48
_T("BEGIN_EVENT_TABLE($(ClassName),$(BaseClassName))\n")
49
_T("\t") + wxsCodeMarks::Beg(wxsCPP,_T("EventTable"),_T("$(ClassName)")) + _T("\n")
50
_T("\t") + wxsCodeMarks::End(wxsCPP) + _T("\n")
51
_T("END_EVENT_TABLE()\n")
53
_T("$(ClassName)::$(ClassName)($(CtorArgs))\n")
55
_T("$(CtorInit)\t") + wxsCodeMarks::Beg(wxsCPP,_T("Initialize"),_T("$(ClassName)")) + _T("\n")
56
_T("\t") + wxsCodeMarks::End(wxsCPP) + _T("\n")
59
_T("$(ClassName)::~$(ClassName)()\n")
61
_T("\t") + wxsCodeMarks::Beg(wxsCPP,_T("Destroy"),_T("$(ClassName)")) + _T("\n")
62
_T("\t") + wxsCodeMarks::End(wxsCPP) + _T("\n")
66
const wxString CppEmptyHeader =
67
_T("#ifndef $(Guard)\n")
68
_T("#define $(Guard)\n")
71
+ wxsCodeMarks::Beg(wxsCPP,_T("Headers"),_T("$(ClassName)")) + _T("\n")
72
+ wxsCodeMarks::End(wxsCPP) + _T("\n")
74
_T("class $(ClassName): public $(BaseClassName)\n")
78
_T("\t\t$(ClassName)($(CtorArgs));\n")
79
_T("\t\tvirtual ~$(ClassName)();\n")
82
_T("\t\t") + wxsCodeMarks::Beg(wxsCPP,_T("Declarations"),_T("$(ClassName)")) + _T("\n")
83
_T("\t\t") + wxsCodeMarks::End(wxsCPP) + _T("\n")
86
_T("\t\t") + wxsCodeMarks::Beg(wxsCPP,_T("Identifiers"),_T("$(ClassName)")) + _T("\n")
87
_T("\t\t") + wxsCodeMarks::End(wxsCPP) + _T("\n")
89
_T("$(HandlersScope)")
90
_T("\t\t") + wxsCodeMarks::Beg(wxsCPP,_T("Handlers"),_T("$(ClassName)")) + _T("\n")
91
_T("\t\t") + wxsCodeMarks::End(wxsCPP) + _T("\n")
94
_T("\t\tDECLARE_EVENT_TABLE()\n")
99
const wxString EmptyXrc =
100
_T("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
101
_T("<resource xmlns=\"http://www.wxwidgets.org/wxxrc\">\n")
104
const wxString EmptyWxs =
105
_T("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n")
107
_T("\t<object class=\"$(BaseClassName)\" name=\"$(ClassName)\"/>\n")
108
_T("\t<resource_extra/>\n")
112
const long GoToHeaderId = wxNewId();
113
const long GoToSourceId = wxNewId();
116
wxsItemRes::wxsItemRes(wxsProject* Owner,const wxString& Type,bool CanBeMain):
117
wxWidgetsRes(Owner,Type),
118
m_WxsFileName(wxEmptyString),
119
m_SrcFileName(wxEmptyString),
120
m_HdrFileName(wxEmptyString),
121
m_XrcFileName(wxEmptyString),
122
m_UseForwardDeclarations(false),
123
m_CanBeMain(CanBeMain)
128
wxsItemRes::wxsItemRes(const wxString& FileName,const TiXmlElement* XrcElem,const wxString& Type):
129
wxWidgetsRes(0,Type),
130
m_WxsFileName(wxEmptyString),
131
m_SrcFileName(wxEmptyString),
132
m_HdrFileName(wxEmptyString),
133
m_XrcFileName(FileName),
134
m_UseForwardDeclarations(false)
136
SetResourceName(cbC2U(XrcElem->Attribute("name")));
139
wxsItemRes::~wxsItemRes()
143
wxsEditor* wxsItemRes::OnCreateEditor(wxWindow* Parent)
145
return new wxsItemEditor(Parent,this);
148
bool wxsItemRes::OnReadConfig(const TiXmlElement* Node)
150
m_WxsFileName = cbC2U(Node->Attribute("wxs"));
151
m_SrcFileName = cbC2U(Node->Attribute("src"));
152
m_HdrFileName = cbC2U(Node->Attribute("hdr"));
153
m_XrcFileName = cbC2U(Node->Attribute("xrc"));
154
m_UseForwardDeclarations = (cbC2U(Node->Attribute("fwddecl")) == _T("1"));
156
// m_XrcFileName may be empty because it's not used when generating full source code
157
return !m_WxsFileName.empty() &&
158
!m_SrcFileName.empty() &&
159
!m_HdrFileName.empty();
162
bool wxsItemRes::OnWriteConfig(TiXmlElement* Node)
164
Node->SetAttribute("wxs",cbU2C(m_WxsFileName));
165
Node->SetAttribute("src",cbU2C(m_SrcFileName));
166
Node->SetAttribute("hdr",cbU2C(m_HdrFileName));
167
if ( !m_XrcFileName.empty() )
169
Node->SetAttribute("xrc",cbU2C(m_XrcFileName));
171
if ( m_UseForwardDeclarations )
173
Node->SetAttribute("fwddecl","1");
178
bool wxsItemRes::OnCanHandleFile(const wxString& FileName)
180
wxFileName Normalized(GetProjectPath()+m_WxsFileName);
181
Normalized.Normalize(wxPATH_NORM_DOTS);
182
if ( Normalized.GetFullPath() == FileName )
186
if ( m_XrcFileName.empty() )
190
Normalized.Assign(GetProjectPath()+m_XrcFileName);
191
Normalized.Normalize(wxPATH_NORM_DOTS);
192
if ( Normalized.GetFullPath() == FileName )
199
wxString wxsItemRes::OnGetDeclarationFile()
201
return m_HdrFileName;
204
bool wxsItemRes::OnGetUsingXRC()
206
return !m_XrcFileName.empty();
209
bool wxsItemRes::OnGetCanBeMain()
214
bool wxsItemRes::CreateNewResource(NewResourceParams& Params)
216
wxFileName HFN(GetProjectPath()+Params.Hdr);
217
SetLanguage(wxsCodeMarks::IdFromExt(HFN.GetExt()));
219
switch ( GetLanguage() )
223
SetResourceName(Params.Class);
225
// Building arguments for constructor
230
if ( Params.CtorParent )
232
CallArgs << _T("parent");
233
CtorArgs << _T("wxWindow* parent");
234
CtorArgsD << _T("wxWindow* parent");
235
if ( Params.CtorParentDef )
237
CtorArgsD << _T("=0");
243
if ( !CtorArgs.IsEmpty() )
247
CtorArgsD << _T(",");
249
CallArgs << _T("id");
250
CtorArgs << _T("wxWindowID id");
251
CtorArgsD << _T("wxWindowID id");
252
if ( Params.CtorIdDef )
254
CtorArgsD << _T("=wxID_ANY");
258
if ( Params.CtorPos )
260
if ( !CtorArgs.IsEmpty() )
264
CtorArgsD << _T(",");
266
CallArgs << _T("pos");
267
CtorArgs << _T("const wxPoint& pos");
268
CtorArgsD << _T("const wxPoint& pos");
269
if ( Params.CtorPosDef )
271
CtorArgsD << _T("=wxDefaultPosition");
275
if ( Params.CtorSize )
277
if ( !CtorArgs.IsEmpty() )
281
CtorArgsD << _T(",");
283
CallArgs << _T("size");
284
CtorArgs << _T("const wxSize& size");
285
CtorArgsD << _T("const wxSize& size");
286
if ( Params.CtorSizeDef )
288
CtorArgsD << _T("=wxDefaultSize");
292
wxString CtorArgsF = CtorArgs;
294
if ( !Params.CustomCtorArgs.IsEmpty() )
296
if ( !CtorArgs.IsEmpty() )
299
CtorArgsD << _T(",");
301
CtorArgs << Params.CustomCtorArgs;
302
CtorArgsD << Params.CustomCtorArgs;
307
m_HdrFileName = Params.Hdr;
310
wxString Name = GetProjectPath()+Params.Hdr;
311
wxFileName::Mkdir(wxFileName(Name).GetPath(),0777,wxPATH_MKDIR_FULL);
312
wxFile File(Name,wxFile::write);
313
wxString Guard = HFN.GetName().Upper() + _T("_H");
314
wxString Header = CppEmptyHeader;
315
wxString InitFuncDecl;
317
if ( Params.UseInitFunc )
320
_T("\tprotected:\n\n")
321
_T("\t\tvoid ") << Params.InitFunc << _T("(") << CtorArgsF << _T(");\n\n");
323
if ( Params.UsePch && !Params.PchGuard.IsEmpty() )
326
_T("#ifndef ") + Params.PchGuard + _T("\n")
327
_T("\t") + wxsCodeMarks::Beg(wxsCPP,_T("HeadersPCH"),Params.Class) + _T("\n")
328
_T("\t") + wxsCodeMarks::End(wxsCPP) + _T("\n")
331
Header.Replace(_T("$(CtorArgs)"),CtorArgsD);
332
Header.Replace(_T("$(Guard)"),Guard);
333
Header.Replace(_T("$(ClassName)"),Params.Class);
334
Header.Replace(_T("$(BaseClassName)"),Params.BaseClass);
335
Header.Replace(_T("$(InitFuncDecl)"),InitFuncDecl);
336
Header.Replace(_T("$(HeadersPch)"),HeadersPch);
338
wxString Scope = _T("");
339
switch ( Params.ScopeMembers )
341
case NewResourceParams::Private: Scope = _T("\tprivate:\n\n"); break;
342
case NewResourceParams::Protected: Scope = _T("\tprotected:\n\n"); break;
345
Header.Replace(_T("$(MembersScope)"),Scope);
347
if ( Params.ScopeIds != Params.ScopeMembers )
349
switch ( Params.ScopeIds )
351
case NewResourceParams::Public: Scope = _T("\tpublic:\n\n"); break;
352
case NewResourceParams::Private: Scope = _T("\tprivate:\n\n"); break;
353
case NewResourceParams::Protected: Scope = _T("\tprotected:\n\n"); break;
356
Header.Replace(_T("$(IdsScope)"),Scope);
358
if ( Params.ScopeHandlers != Params.ScopeIds )
360
switch ( Params.ScopeHandlers )
362
case NewResourceParams::Public: Scope = _T("\tpublic:\n\n"); break;
363
case NewResourceParams::Private: Scope = _T("\tprivate:\n\n"); break;
364
case NewResourceParams::Protected: Scope = _T("\tprotected:\n\n"); break;
367
Header.Replace(_T("$(HandlersScope)"),Scope);
369
// TODO: Use wxsCoder to save file's content, so it will
370
// have proper encoding and EOL stuff
371
if ( !File.Write(Header) ) return false;
374
m_SrcFileName = Params.Src;
377
wxString Name = GetProjectPath()+Params.Src;
378
wxFileName::Mkdir(wxFileName(Name).GetPath(),0777,wxPATH_MKDIR_FULL);
379
wxFile File(Name,wxFile::write);
380
HFN.MakeRelativeTo(wxFileName(Name).GetPath());
381
wxString Include = HFN.GetFullPath(wxPATH_UNIX);
385
wxFileName PCH(GetProjectPath()+Params.Pch);
386
PCH.MakeRelativeTo(wxFileName(Name).GetPath());
387
PchCode << _T("#include \"") << PCH.GetFullPath(wxPATH_UNIX) << _T("\"\n");
389
wxString CtorInitCode;
390
if ( Params.UseInitFunc )
392
CtorInitCode << _T("\t") << Params.InitFunc << _T("(") << CallArgs << _T(");\n");
393
CtorInitCode << _T("}\n\n");
394
CtorInitCode << _T("void ") << Params.Class << _T("::") << Params.InitFunc << _T("(") << CtorArgsF << _T(")\n");
395
CtorInitCode << _T("{\n");
397
wxString IntHeadersPch;
398
if ( Params.UsePch && !Params.PchGuard.IsEmpty() )
401
_T("#ifndef ") + Params.PchGuard + _T("\n")
402
_T("\t") + wxsCodeMarks::Beg(wxsCPP,_T("InternalHeadersPCH"),Params.Class) + _T("\n")
403
_T("\t") + wxsCodeMarks::End(wxsCPP) + _T("\n")
407
wxString Source = CppEmptySource;
408
Source.Replace(_T("$(PchCode)"),PchCode);
409
Source.Replace(_T("$(CtorArgs)"),CtorArgs);
410
Source.Replace(_T("$(Include)"),Include);
411
Source.Replace(_T("$(ClassName)"),Params.Class);
412
Source.Replace(_T("$(BaseClassName)"),Params.BaseClass);
413
Source.Replace(_T("$(CtorInit)"),CtorInitCode);
414
Source.Replace(_T("$(InternalHeadersPch)"),IntHeadersPch);
415
// TODO: Use wxsCoder to save file's content, so it will
416
// have proper encoding and EOL stuff
417
if ( !File.Write(Source) ) return false;
420
m_XrcFileName = Params.Xrc;
421
if ( !Params.Xrc.IsEmpty() && Params.GenXrc )
423
wxString Name = GetProjectPath()+Params.Xrc;
424
wxFileName::Mkdir(wxFileName(Name).GetPath(),0777,wxPATH_MKDIR_FULL);
425
wxFile File(Name,wxFile::write);
426
if ( !File.Write(EmptyXrc) ) return false;
429
if ( Params.Wxs.IsEmpty() )
431
// Searching for new wxs file name
432
// TODO: Do not use constant folder name
433
wxString WxsNameBase = _T("wxsmith");
435
if ( !wxFileName::DirExists(GetProjectPath()+WxsNameBase) )
437
if ( !wxFileName::Mkdir(GetProjectPath()+WxsNameBase,0777,wxPATH_MKDIR_FULL) )
442
WxsNameBase += _T("/") + Params.Class;
443
WxsName = WxsNameBase + _T(".wxs");
447
if ( !wxFileName::FileExists(GetProjectPath()+WxsName) &&
448
!wxFileName::DirExists(GetProjectPath()+WxsName) )
453
WxsName = wxString::Format(_T("%s%d.wxs"),WxsNameBase.c_str(),Cnt);
456
m_WxsFileName = WxsName;
457
Params.Wxs = WxsName;
459
wxString Name = GetProjectPath()+m_WxsFileName;
460
wxFile File(Name,wxFile::write);
461
wxString Content = EmptyWxs;
462
Content.Replace(_T("$(ClassName)"),Params.Class);
463
Content.Replace(_T("$(BaseClassName)"),GetResourceType());
464
if ( !File.Write(Content) )
472
m_WxsFileName = Params.Wxs;
474
m_UseForwardDeclarations = Params.UseFwdDecl;
481
SetLanguage(wxsUnknownLanguage);
485
wxsItemRes::EditMode wxsItemRes::GetEditMode()
487
if ( m_WxsFileName.empty() ) return File;
488
if ( m_XrcFileName.empty() ) return Source;
492
int wxsItemRes::OnGetTreeIcon()
494
const wxsItemInfo* Info = wxsItemFactory::GetInfo(GetResourceType());
495
if ( Info ) return Info->TreeIconId;
496
return wxsResource::OnGetTreeIcon();
499
wxsItemResData* wxsItemRes::BuildResData(wxsItemEditor* Editor)
501
wxString ProjectPath = GetProjectPath();
503
return new wxsItemResData(
504
ProjectPath + GetWxsFileName(),
505
ProjectPath + GetSrcFileName(),
506
ProjectPath + GetHdrFileName(),
507
GetXrcFileName().empty() ? _T("") : ProjectPath + GetXrcFileName(),
511
m_UseForwardDeclarations,
517
bool wxsItemRes::OnDeleteCleanup(bool ShowDialog)
519
bool PhisDeleteWXS = true;
520
bool DeleteSources = false;
521
bool PhisDeleteSources = false;
525
wxsDeleteItemRes Dlg;
526
if ( Dlg.ShowModal() != wxID_OK )
530
PhisDeleteWXS = Dlg.m_PhisDeleteWXS->GetValue();
531
DeleteSources = Dlg.m_DeleteSources->GetValue();
532
PhisDeleteSources = Dlg.m_PhisDeleteSources->GetValue();
535
ProjectFile* Wxs = GetProject()->GetCBProject()->GetFileByFilename(m_WxsFileName,true);
538
GetProject()->GetCBProject()->RemoveFile(Wxs);
543
wxRemoveFile(GetProjectPath() + m_WxsFileName);
548
ProjectFile* Pf = GetProject()->GetCBProject()->GetFileByFilename(m_SrcFileName,true);
551
GetProject()->GetCBProject()->RemoveFile(Pf);
553
Pf = GetProject()->GetCBProject()->GetFileByFilename(m_HdrFileName,true);
556
GetProject()->GetCBProject()->RemoveFile(Pf);
559
if ( PhisDeleteSources )
561
wxRemoveFile(GetProjectPath() + m_SrcFileName);
562
wxRemoveFile(GetProjectPath() + m_HdrFileName);
566
Manager::Get()->GetProjectManager()->RebuildTree();
568
// TODO: Check if we've deleted main resource of this app
573
void wxsItemRes::OnFillPopupMenu(wxMenu* Menu)
575
if ( !m_SrcFileName.IsEmpty() || !m_HdrFileName.IsEmpty() )
577
Menu->AppendSeparator();
578
Menu->Append(GoToHeaderId,_("Go to header file"));
579
Menu->Append(GoToSourceId,_("Go to source file"));
583
bool wxsItemRes::OnPopupMenu(long Id)
585
if ( Id == GoToHeaderId )
587
if ( !m_HdrFileName.IsEmpty() )
589
Manager::Get()->GetEditorManager()->Open(GetProjectPath() + m_HdrFileName);
593
if ( Id == GoToSourceId )
595
if ( !m_SrcFileName.IsEmpty() )
597
Manager::Get()->GetEditorManager()->Open(GetProjectPath() + m_SrcFileName);