-
Notifications
You must be signed in to change notification settings - Fork 0
/
Inno.cpp
431 lines (369 loc) · 11.5 KB
/
Inno.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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <sdk.h> // Code::Blocks SDK
#include <cbeditor.h>
#include <cbproject.h>
#include <configmanager.h>
#include <configurationpanel.h>
#include <editormanager.h>
#include <filefilters.h>
#include <filegroupsandmasks.h>
#include <logmanager.h>
#include "Inno.h"
#include "Wizard.h"
#include "CreateDialog.h"
#include "InnoSettings.h"
std::set<CEditor*> Inno::m_editors;
// Register the plugin with Code::Blocks.
// We are using an anonymous namespace so we don't litter the global one.
namespace
{
PluginRegistrant<Inno> reg(_T("Inno"));
const int ID_INNO_EMPTY = wxNewId();
const int ID_INNO_NEW = wxNewId();
const int ID_INNO_BUILD = wxNewId();
const int ID_INNO_THREAD = wxNewId();
}
// events handling
BEGIN_EVENT_TABLE(Inno, cbMimePlugin)
// add any events you want to handle here
EVT_MENU( ID_INNO_EMPTY, Inno::OnEmpty)
EVT_MENU(ID_INNO_NEW, Inno::OnNew)
EVT_MENU( ID_INNO_BUILD, Inno::OnInnoBuild)
EVT_END_PROCESS(ID_INNO_THREAD, Inno::OnProcessEnd)
END_EVENT_TABLE()
// constructor
Inno::Inno()
{
// Make sure our resources are available.
// In the generated boilerplate code we have no resources but when
// we add some, it will be nice that this code is in place already ;)
if(!Manager::LoadResource(_T("Inno.zip")))
{
NotifyMissingFile(_T("Inno.zip"));
}
m_consume = nullptr;
}
// destructor
Inno::~Inno()
{
if( m_consume != nullptr)
{
m_consume->Delete();
delete m_consume;
}
m_consume = nullptr;
}
#include "Images/Inno.xpm"
void Inno::OnAttach()
{
// do whatever initialization you need for your plugin
// NOTE: after this function, the inherited member variable
// m_IsAttached will be TRUE...
// You should check for it in other functions, because if it
// is FALSE, it means that the application did *not* "load"
// (see: does not need) this plugin...
if( m_IsAttached)
{
m_log_pos = Manager::Get()->GetLogManager()->SetLog(new TextCtrlLogger());
Manager::Get()->GetLogManager()->Slot(m_log_pos).title = L"Inno";
CodeBlocksLogEvent evt(cbEVT_ADD_LOG_WINDOW, m_log_pos, Manager::Get()->GetLogManager()->Slot(m_log_pos).title, new wxBitmap(Inno_xpm));
Manager::Get()->ProcessEvent(evt);
AddFileMasksToProjectManager();
}
}
void Inno::OnRelease(cb_unused bool appShutDown)
{
// do de-initialization for your plugin
// if appShutDown is true, the plugin is unloaded because Code::Blocks is being shut down,
// which means you must not use any of the SDK Managers
// NOTE: after this function, the inherited member variable
// m_IsAttached will be FALSE...
CodeBlocksLogEvent evt(cbEVT_REMOVE_LOG_WINDOW, m_log_pos);
Manager::Get()->ProcessEvent(evt);
}
void Inno::BuildMenu(wxMenuBar* menuBar)
{
//The application is offering its menubar for your plugin,
//to add any menu items you want...
//Append any items you need in the menu...
//NOTE: Be careful in here... The application's menubar is at your disposal.
int pos = menuBar->FindMenu(_("&File"));
wxMenu* FileMenu = menuBar->GetMenu(pos);
pos = FileMenu->FindItem(_("New"));
wxMenu* WizardMenu = new wxMenu;
WizardMenu->Append( ID_INNO_EMPTY, _("Empty File"));
WizardMenu->Append( ID_INNO_NEW, _("standard Wizard"));
FileMenu->Insert(1, wxID_ANY, _("Inno"), WizardMenu);
}
void Inno::BuildModuleMenu(const ModuleType type, wxMenu* menu, const FileTreeData* data)
{
if( type == mtProjectManager)
{
if( data && data->GetKind() == FileTreeData::ftdkFile)
{
if( data->GetProjectFile()->relativeFilename.AfterLast('.').compare(L"iss") == 0)
{
iss_name = data->GetProjectFile()->file.GetFullPath() ;
menu->Append(ID_INNO_BUILD, L"Build");
}
}
}
}
bool Inno::CanHandleFile(const wxString& filename) const
{
wxString fn;
fn = filename;
fn.MakeUpper();
if( fn.Right(4) == _T(".ISS"))
return true;
return false;
}
int Inno::OpenFile(const wxString& filename)
{
new CEditor(filename, m_log_pos);
return 0;
}
bool Inno::HandlesEverything() const
{
return false;
}
cbConfigurationPanel* Inno::GetConfigurationPanel(wxWindow* parent)
{
InnoSettings* dlg = new InnoSettings(parent);
return dlg;
}
/** This method adds Filemasks, such as "*.iss" to the project manager
* This allows the display of all inno setup into 1 virtual folder "Inno"
*/
void Inno::AddFileMasksToProjectManager(void)
{
ProjectManager *pm = Manager::Get()->GetProjectManager();
wxString sFileMasks = _("*.iss");
//add these file masks to the project manager
if (pm)
{
FilesGroupsAndMasks *fm;
fm = (FilesGroupsAndMasks *) pm->GetFilesGroupsAndMasks();
if (fm)
{
//test if the filemasks are already associated
unsigned int i, iMax;
unsigned int iMatch;
bool bMatch = false;
iMax = fm->GetGroupsCount();
for(i=0; i<iMax; i++) //loop over the groups managed by the project manager
{
if (fm->MatchesMask(sFileMasks, i))
{
bMatch= true;
break;
}
}
if (!bMatch)
{
//create a new group
wxString sName;
sName = _("Inno");
while (IsGroupNameExisting(sName, fm)) sName = sName + _("_");
fm->AddGroup(sName);
iMatch = fm->GetGroupsCount() - 1;
}
//add all the filemasks to the group
wxString sMask = fm->GetFileMasks(iMatch);
if (!(fm->MatchesMask(sFileMasks, i))) //to avoid having twice the same mask
{
if (sMask.Len() > 0) sMask = sMask + _(";");
sMask = sMask + sFileMasks;
}
fm->SetFileMasks(iMatch, sMask);
fm->Save();
pm->GetUI().RebuildTree();
}
}
FileFilters::Add(_("Inno Setup Files"), sFileMasks);
}
/** Test if a group name is already existing in the Project Manager
* \param sName : the group name to test. Example: _("Images")
* \param fm : the FilesGroupAndMasks to search
* \return true if the Name already exist, false otherwise
*/
bool Inno::IsGroupNameExisting(wxString sName, const FilesGroupsAndMasks *fm)
{
if (!fm) return(false);
unsigned int i, iMax;
wxString sName2;
iMax = fm->GetGroupsCount();
for(i=0; i<iMax; i++) //loop over the groups managed by the project manager
{
sName2 = fm->GetGroupName(i);
if (sName == sName2) return(true);
}
return(false);
}
void Inno::OnEmpty(cb_unused wxCommandEvent& event)
{
CInno inno;
bool OverwriteFile = true;
if( inno.FileExist( Manager::Get()->GetProjectManager()->GetActiveProject()->GetFilename().BeforeLast('.')))
if( wxID_NO == cbMessageBox( _("Do you want to overwrite the existing File?"), _("File exist"), wxYES_NO | wxICON_QUESTION))
OverwriteFile = false;
if( OverwriteFile)
{
if(Manager::Get()->GetProjectManager()->GetActiveProject())
{
if(wxID_YES == cbMessageBox(_("Do you want to add the inno file to the active project?"),_("New Inno Setup"),wxYES|wxNO))
inno.AddToProject(true);
else
inno.AddToProject(false);
inno.WriteEmptyFile(Manager::Get()->GetProjectManager()->GetActiveProject()->GetFilename().BeforeLast('.'));
}
else
{
wxMessageBox( _T("Open your Project first"));
}
}
}
void Inno::OnNew(cb_unused wxCommandEvent& event)
{
CInno inno;
inno.Create( Manager::Get()->GetAppWindow());
wxString Name;
bool OverwriteFile = true;
if( inno.FileExist( Manager::Get()->GetProjectManager()->GetActiveProject()->GetFilename().BeforeLast('.')))
if( wxID_NO == cbMessageBox( _("Do you want to overwrite the existing File?"), _("File exist"), wxYES_NO | wxICON_QUESTION))
OverwriteFile = false;
if( OverwriteFile)
{
if(Manager::Get()->GetProjectManager()->GetActiveProject())
{
Name = Manager::Get()->GetProjectManager()->GetActiveProject()->GetFilename().BeforeLast('.');
Name = Name.AfterLast( '\\');
inno.Set( Name);
if(wxID_YES == cbMessageBox(_("Do you want to add the inno file to the active project?"),_("New Inno Setup"),wxYES|wxNO))
inno.AddToProject(true);
else
inno.AddToProject(false);
if( inno.ShowStandartWizard())
{
wxString file_name = Manager::Get()->GetProjectManager()->GetActiveProject()->GetFilename().BeforeLast('.');
inno.WriteFile(file_name);
iss_name = file_name + L".iss";
cbEditor* ed = Manager::Get()->GetEditorManager()->Open(iss_name);
ed->Reload();
ed->Activate();
if( wxYES == wxMessageBox( _T( "Do you want to compile the Inno Setup file?"), _T( "Inno Setup"), wxYES_NO | wxICON_QUESTION))
{
wxCommandEvent e;
OnInnoBuild(e);
}
}
}
else
{
wxMessageBox( _T("Open your Project first"));
}
}
inno.Delete();
}
void Inno::OnInnoBuild(cb_unused wxCommandEvent& event)
{
ConfigManager* pCfg = Manager::Get()->GetConfigManager(_T("inno"));
wxString path = pCfg->Read(_T("iscc_path"), _T(""));
if( path.empty())
{
Manager::Get()->GetLogManager()->Log(L"please set the path to the inno compiler first!\nSettings->Environment...->Inno");
return;
}
path = L"\"" + path + L"\"";
wxString command = path + L" \"";
command += iss_name;
command += L"\"";
m_running = true;
wxProcess* compile = new wxProcess(this, ID_INNO_THREAD);
compile->Redirect();
wxExecute(command, wxEXEC_ASYNC, compile);
if( !compile)
{
cbMessageBox(L"can't open a process to the iscc");
return;
}
m_out = compile->GetInputStream();
m_err = compile->GetErrorStream();
if( !m_out || !m_err)
{
cbMessageBox(L"can't read from stdout of iscc");
return;
}
CodeBlocksLogEvent evt(cbEVT_SWITCH_TO_LOG_WINDOW, m_log_pos);
Manager::Get()->ProcessEvent(evt);
if( m_consume == nullptr)
{
m_consume = new Consume(m_out, m_log_pos);
wxThreadError er = m_consume->Run();
if( er != wxTHREAD_NO_ERROR)
{
Manager::Get()->GetLogManager()->Log(wxString::Format(L"Can't create the thread!%d", er), m_log_pos);
delete m_consume;
m_consume = nullptr;
}
}
}
void Inno::OnProcessEnd(cb_unused wxProcessEvent& evt)
{
if( m_running)
{
m_running = false;
if(m_consume != nullptr)
{
if( m_consume->IsRunning())
if( m_consume->Delete() != wxTHREAD_NO_ERROR)
Manager::Get()->GetLogManager()->Log(L"Error at deleting", m_log_pos);
}
m_consume = nullptr;
CodeBlocksLogEvent event(cbEVT_SWITCH_TO_LOG_WINDOW, m_log_pos);
Manager::Get()->ProcessEvent(event);
wxString text;
while( m_out->CanRead())
{
text += m_out->GetC();
}
Manager::Get()->GetLogManager()->Log(text, m_log_pos);
wxString err;
while( m_err->CanRead())
{
err += m_err->GetC();
}
if( !err.empty())
{
size_t pos = err.find(L": ");
size_t spos = err.find(L"in ");
wxString file = err.substr(spos+3, pos-spos-3);
wxString msg = err.substr(pos+2, err.find(L"Compile aborted.")-pos-2);
spos = err.find(L"on line");
int linenr = -1;
if( spos != wxString::npos)
{
wxString line = err.substr(spos+8, err.find(' ', spos+8)-spos-8);
linenr = wxAtoi(line);
}
pos = msg.find('[');
if( pos != wxString::npos)
{
pos++;
wxString section = msg.substr(pos, msg.find(']') - pos);
}
cbEditor* ed = Manager::Get()->GetEditorManager()->Open(file);
ed->Reload();
ed->Activate();
if( linenr != -1)
{
ed->GotoLine(linenr-1);
ed->SetErrorLine(linenr-1);
}
Manager::Get()->GetLogManager()->Log(err, m_log_pos, Logger::error);
}
else
{
Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor()->Activate();
}
}
}