-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathwxCrafterTab.cpp
320 lines (270 loc) · 11.8 KB
/
wxCrafterTab.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
#include <wx/app.h>
#include "wxCrafterTab.h"
#include "wxCrafterCB.h"
#include <projectmanager.h>
#include <cbworkspace.h>
#include "NewWxCrafterFileDlg.h"
#include "wxcHelper.h"
#include "wxcHelper.h"
#include <cbproject.h>
#include <wx/menu.h>
#include "NewCodeBlocksProjectWizard.h"
#include <configmanager.h>
#include "wxCrafterSettingsDlg.h"
class wxCrafterTabItemData : public wxTreeItemData
{
public:
enum {
kKindFile = 0,
kKindProject = 1,
};
protected:
wxString m_filename;
int m_kind;
public:
wxCrafterTabItemData(const wxString& filename) : m_filename(filename), m_kind(kKindFile) {}
wxCrafterTabItemData(int kind) : m_kind(kind) {}
void SetFilename(const wxString& filename) {
this->m_filename = filename;
}
const wxString& GetFilename() const {
return m_filename;
}
int GetKind() const {
return m_kind;
}
};
// -----------------------------------
wxCrafterTab::wxCrafterTab(wxWindow* parent, wxCrafterCB* plugin)
: wxCrafterTabBase(parent)
, m_plugin(plugin)
{
m_treeCtrl->AssignImageList( new wxcImages() );
Manager::Get()->RegisterEventSink(cbEVT_WORKSPACE_CHANGED, new cbEventFunctor<wxCrafterTab, CodeBlocksEvent>(this, &wxCrafterTab::OnWorkspaceChanged));
wxTheApp->Connect(ID_MENU_NEW_FRAME, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(wxCrafterTab::OnNewForm), NULL, this);
wxTheApp->Connect(ID_MENU_NEW_DIALOG, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(wxCrafterTab::OnNewForm), NULL, this);
wxTheApp->Connect(ID_MENU_NEW_IMGLIST, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(wxCrafterTab::OnNewForm), NULL, this);
wxTheApp->Connect(ID_MENU_NEW_PANEL, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(wxCrafterTab::OnNewForm), NULL, this);
wxTheApp->Connect(ID_MENU_NEW_POPUPWINDOW, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(wxCrafterTab::OnNewForm), NULL, this);
wxTheApp->Connect(ID_MENU_NEW_WIZARD, wxEVT_COMMAND_MENU_SELECTED, wxCommandEventHandler(wxCrafterTab::OnNewForm), NULL, this);
}
wxCrafterTab::~wxCrafterTab()
{
}
void wxCrafterTab::OnNewFormUI(wxUpdateUIEvent& event)
{
wxCrafterTabItemData* itemData = GetSelectionItemData();
event.Enable( itemData && itemData->GetKind() == wxCrafterTabItemData::kKindFile );
}
void wxCrafterTab::OnNewWxCrafterFile(wxCommandEvent& event)
{
NewWxCrafterFileDlg dlg(NULL);
if ( dlg.ShowModal() == wxID_OK ) {
wxFileName fnResrouceFile( dlg.GetPath() );
fnResrouceFile.SetExt(wxT("wxcp")); // make sure we got the correct extension
if ( wxcHelper::CreateEmptyFile( fnResrouceFile ) ) {
// Add the file to a project
cbProject* pProj = wxcHelper::GetProject( dlg.GetProjectName() );
if ( pProj ) {
// Add the file to the project
wxcHelper::AddFileToProject(pProj, fnResrouceFile, false);
BuildTree();
}
}
}
}
void wxCrafterTab::OnNewWxCrafterFileUI(wxUpdateUIEvent& event)
{
wxCrafterTabItemData* itemData = GetSelectionItemData();
event.Enable( itemData && itemData->GetKind() == wxCrafterTabItemData::kKindProject );
}
void wxCrafterTab::OnWorkspaceChanged(CodeBlocksEvent& event)
{
event.Skip();
BuildTree();
}
void wxCrafterTab::DoClear()
{
m_treeCtrl->DeleteAllItems();
}
wxTreeItemId wxCrafterTab::GetProjectNode(const wxString& name)
{
wxTreeItemIdValue cookie;
wxTreeItemId child = m_treeCtrl->GetFirstChild(m_treeCtrl->GetRootItem(), cookie);
while ( child.IsOk() ) {
if ( m_treeCtrl->GetItemText(child) == name ) {
return child;
}
child = m_treeCtrl->GetNextChild(m_treeCtrl->GetRootItem(), cookie);
}
return wxTreeItemId();
}
wxCrafterTabItemData* wxCrafterTab::GetSelectionItemData()
{
wxTreeItemId sel = m_treeCtrl->GetSelection();
if ( !sel.IsOk() ) {
return NULL;
}
return dynamic_cast<wxCrafterTabItemData*>( m_treeCtrl->GetItemData(sel) );
}
void wxCrafterTab::BuildTree()
{
m_treeCtrl->Freeze();
m_treeCtrl->DeleteAllItems();
m_treeCtrl->AddRoot(_("Hidden Root"));
// Load all wxcp files
FilesList allWxcpFiles;
wxcHelper::GetAllFiles( allWxcpFiles, wxT("wxcp") );
// Now add the file to the tab view
FilesList::iterator iter = allWxcpFiles.begin();
for(; iter != allWxcpFiles.end(); ++iter ) {
wxFileName fnProjectFile( (*iter)->GetParentProject()->GetFilename() );
wxFileName fnResrouceFile( (*iter)->file );
wxTreeItemId projectItem = GetProjectNode( (*iter)->GetParentProject()->GetTitle() );
if ( !projectItem.IsOk() ) {
// First time, add a project node
projectItem = m_treeCtrl->AppendItem(m_treeCtrl->GetRootItem(), (*iter)->GetParentProject()->GetTitle(), 1, 1, new wxCrafterTabItemData(wxCrafterTabItemData::kKindProject));
}
wxCrafterTabItemData* itemData = new wxCrafterTabItemData(fnResrouceFile.GetFullPath());
fnResrouceFile.MakeRelativeTo( fnProjectFile.GetPath() );
m_treeCtrl->AppendItem(projectItem, fnResrouceFile.GetFullPath(), 0, 0, itemData);
}
m_treeCtrl->ExpandAll();
m_treeCtrl->Thaw();
}
void wxCrafterTab::OnItemActivated(wxTreeEvent& event)
{
wxUnusedVar( event );
wxCrafterTabItemData* itemData = GetSelectionItemData();
if ( itemData && itemData->GetKind() == wxCrafterTabItemData::kKindFile ) {
if( m_plugin->EnsureWxCrafterIsRunning() ) {
m_plugin->GetConnector().LoadFile( itemData->GetFilename() );
}
}
}
void wxCrafterTab::OnNewForm(wxCommandEvent& event)
{
wxUnusedVar( event );
wxCrafterTabItemData* itemData = GetSelectionItemData();
if ( !itemData || (itemData->GetKind() != wxCrafterTabItemData::kKindFile) ) {
return;
}
// Make sure wxCrafter is up and running
if ( ! m_plugin->EnsureWxCrafterIsRunning() ) {
return;
}
wxFileName wxcpFile = itemData->GetFilename();
switch( event.GetId() ) {
case ID_MENU_NEW_DIALOG:
m_plugin->GetConnector().NewForm(wxcpFile, wxCrafter::kCommandTypeNewDialog);
break;
case ID_MENU_NEW_FRAME:
m_plugin->GetConnector().NewForm(wxcpFile, wxCrafter::kCommandTypeNewFrame);
break;
case ID_MENU_NEW_IMGLIST:
m_plugin->GetConnector().NewForm(wxcpFile, wxCrafter::kCommandTypeNewImageList);
break;
case ID_MENU_NEW_PANEL:
m_plugin->GetConnector().NewForm(wxcpFile, wxCrafter::kCommandTypeNewPanel);
break;
case ID_MENU_NEW_POPUPWINDOW:
m_plugin->GetConnector().NewForm(wxcpFile, wxCrafter::kCommandTypeNewPopupWindow);
break;
case ID_MENU_NEW_WIZARD:
m_plugin->GetConnector().NewForm(wxcpFile, wxCrafter::kCommandTypeNewWizard);
break;
default:
break;
}
}
void wxCrafterTab::OnItemMenu(wxTreeEvent& event)
{
m_treeCtrl->SelectItem( event.GetItem() );
wxMenu menu;
wxMenuItem *menuItem(NULL);
menuItem = new wxMenuItem(&menu, ID_MENU_NEW_FRAME, _("New wxFrame..."), _("New wxFrame..."), wxITEM_NORMAL);
menuItem->SetBitmap(wxXmlResource::Get()->LoadBitmap(wxT("wxframe")));
menu.Append(menuItem);
menuItem = new wxMenuItem(&menu, ID_MENU_NEW_DIALOG, _("New wxDialog..."), _("New wxDialog..."), wxITEM_NORMAL);
menuItem->SetBitmap(wxXmlResource::Get()->LoadBitmap(wxT("wxdialog")));
menu.Append(menuItem);
menuItem = new wxMenuItem(&menu, ID_MENU_NEW_PANEL, _("New wxPanel..."), _("New wxPanel..."), wxITEM_NORMAL);
menuItem->SetBitmap(wxXmlResource::Get()->LoadBitmap(wxT("wxpanel")));
menu.Append(menuItem);
menuItem = new wxMenuItem(&menu, ID_MENU_NEW_WIZARD, _("New wxWizard..."), _("New wxWizard..."), wxITEM_NORMAL);
menuItem->SetBitmap(wxXmlResource::Get()->LoadBitmap(wxT("wxwizard")));
menu.Append(menuItem);
menu.AppendSeparator();
menuItem = new wxMenuItem(&menu, ID_MENU_NEW_POPUPWINDOW, _("New wxPopupWindow..."), _("New wxPopupWindow..."), wxITEM_NORMAL);
menuItem->SetBitmap(wxXmlResource::Get()->LoadBitmap(wxT("wxpopupwindow")));
menu.Append(menuItem);
menuItem = new wxMenuItem(&menu, ID_MENU_NEW_IMGLIST, _("New wxImageList..."), _("New wxImageList..."), wxITEM_NORMAL);
menuItem->SetBitmap(wxXmlResource::Get()->LoadBitmap(wxT("wximglist")));
menu.Append(menuItem);
m_treeCtrl->PopupMenu( &menu );
}
void wxCrafterTab::OnNewCBProject(wxCommandEvent& event)
{
NewCodeBlocksProjectWizard wiz(NULL);
if ( wiz.RunWizard( wiz.GetFirstPage() ) ) {
// create the project
ProjectInfo pi = wiz.GetProjectDetails();
DoCreateProject( pi );
}
}
void wxCrafterTab::DoCreateProject(const ProjectInfo& projectInfo)
{
// Create the project folder
wxFileName::Mkdir( projectInfo.cbp_path.GetPath(), 0777, wxPATH_MKDIR_FULL);
// Get the resources Zip file
wxString dataFolder = ConfigManager::GetDataFolder();
wxFileName zipFile(dataFolder, wxT("wxCrafterCB.zip"));
// Extract the files to this folder
wxString dummy;
wxcHelper::ExtractFileFromZip(zipFile.GetFullPath(), wxT("main.cpp"), projectInfo.cbp_path.GetPath(), dummy);
wxcHelper::ExtractFileFromZip(zipFile.GetFullPath(), wxT("MainFrame.cpp"), projectInfo.cbp_path.GetPath(), dummy);
wxcHelper::ExtractFileFromZip(zipFile.GetFullPath(), wxT("MainFrame.h"), projectInfo.cbp_path.GetPath(), dummy);
wxcHelper::ExtractFileFromZip(zipFile.GetFullPath(), wxT("wxcrafter.cpp"), projectInfo.cbp_path.GetPath(), dummy);
wxcHelper::ExtractFileFromZip(zipFile.GetFullPath(), wxT("wxcrafter.h"), projectInfo.cbp_path.GetPath(), dummy);
wxcHelper::ExtractFileFromZip(zipFile.GetFullPath(), wxT("wxcrafter.wxcp"), projectInfo.cbp_path.GetPath(), dummy);
wxcHelper::ExtractFileFromZip(zipFile.GetFullPath(), wxT("wxcrafter_bitmaps.cpp"), projectInfo.cbp_path.GetPath(), dummy);
wxcHelper::ExtractFileFromZip(zipFile.GetFullPath(), wxT("wxCrafterCBTemplate.cbp"), projectInfo.cbp_path.GetPath(), dummy);
wxcHelper::ExtractFileFromZip(zipFile.GetFullPath(), wxT("resources.rc"), projectInfo.cbp_path.GetPath(), dummy);
// Perform a simple string replace on the cbp file
wxFileName projectFile(projectInfo.cbp_path.GetPath(), wxT("wxCrafterCBTemplate.cbp"));
wxString fileContent;
wxcHelper::ReadFileContent( projectFile.GetFullPath(), fileContent );
// Replace the place holders
wxString buildType, components;
if ( projectInfo.wx_build_type.Contains(wxT("Dynamic")) ) {
buildType = wxT("gcc_dll");
} else {
buildType = wxT("gcc_lib");
}
for(size_t i=0; i<projectInfo.wx_components.GetCount(); ++i) {
components << projectInfo.wx_components.Item(i) << wxT(",");
}
if ( !components.IsEmpty() ) {
components.RemoveLast();
}
fileContent.Replace(wxT("${PROJECT_NAME}"), projectInfo.cbp_path.GetName());
fileContent.Replace(wxT("${WX_COMPONENTS}"), components);
#ifdef __WXMSW__
fileContent.Replace(wxT("${WX_BUILD_TYPE}"), buildType);
fileContent.Replace(wxT("${WX_PREFIX}"), projectInfo.wx_prefix);
#else
// Remove these placeholders on Linux
fileContent.Replace(wxT("--prefix=${WX_PREFIX}"), wxT(""));
fileContent.Replace(wxT("--wxcfg=${WX_BUILD_TYPE}/mswud"), wxT(""));
fileContent.Replace(wxT("--wxcfg=${WX_BUILD_TYPE}/mswu"), wxT(""));
#endif
wxcHelper::WriteFileContent( projectFile.GetFullPath(), fileContent );
::wxRenameFile(projectFile.GetFullPath(), projectInfo.cbp_path.GetFullPath());
// And finally, load the project
Manager::Get()->GetProjectManager()->LoadProject( projectInfo.cbp_path.GetFullPath() );
}
void wxCrafterTab::OnSettings(wxCommandEvent& event)
{
wxCrafterSettingsDlg dlg(NULL);
dlg.ShowModal();
}