File: wxformbuilder.cpp

package info (click to toggle)
codelite 14.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 112,816 kB
  • sloc: cpp: 483,662; ansic: 150,144; php: 9,569; lex: 4,186; python: 3,417; yacc: 2,820; sh: 1,147; makefile: 52; xml: 13
file content (394 lines) | stat: -rw-r--r-- 14,432 bytes parent folder | download
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright            : (C) 2008 by Eran Ifrah
// file name            : wxformbuilder.cpp
//
// -------------------------------------------------------------------------
// A
//              _____           _      _     _ _
//             /  __ \         | |    | |   (_) |
//             | /  \/ ___   __| | ___| |    _| |_ ___
//             | |    / _ \ / _  |/ _ \ |   | | __/ _ )
//             | \__/\ (_) | (_| |  __/ |___| | ||  __/
//              \____/\___/ \__,_|\___\_____/_|\__\___|
//
//                                                  F i l e
//
//    This program is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////

#include "asyncprocess.h"
#include "confformbuilder.h"
#include "event_notifier.h"
#include "formbuildsettingsdlg.h"
#include "globals.h"
#include "processreaderthread.h"
#include "procutils.h"
#include "project.h"
#include "workspace.h"
#include "wxfbitemdlg.h"
#include "wxformbuilder.h"
#include <wx/app.h>
#include <wx/menu.h>
#include <wx/mimetype.h>
#include <wx/msgdlg.h>
#include <wx/xrc/xmlres.h>

static wxFormBuilder* thePlugin = NULL;

// Define the plugin entry point
CL_PLUGIN_API IPlugin* CreatePlugin(IManager* manager)
{
    if(thePlugin == 0) { thePlugin = new wxFormBuilder(manager); }
    return thePlugin;
}

CL_PLUGIN_API PluginInfo* GetPluginInfo()
{
    static PluginInfo info;
    info.SetAuthor(wxT("Eran Ifrah"));
    info.SetName(wxT("wxFormBuilder"));
    info.SetDescription(_("wxFormBuilder integration with CodeLite"));
    info.SetVersion(wxT("v1.0"));
    return &info;
}

CL_PLUGIN_API int GetPluginInterfaceVersion() { return PLUGIN_INTERFACE_VERSION; }

wxFormBuilder::wxFormBuilder(IManager* manager)
    : IPlugin(manager)
    , m_separatorItem(NULL)
    , m_openWithWxFbItem(NULL)
    , m_openWithWxFbSepItem(NULL)
{
    Bind(wxEVT_ASYNC_PROCESS_TERMINATED, &wxFormBuilder::OnWxFBTerminated, this);
    m_longName = _("wxFormBuilder integration with CodeLite");
    m_shortName = wxT("wxFormBuilder");
    m_topWin = m_mgr->GetTheApp();

    m_topWin->Connect(XRCID("wxfb_new_dialog"), wxEVT_COMMAND_MENU_SELECTED,
                      wxCommandEventHandler(wxFormBuilder::OnNewDialog), NULL, this);
    m_topWin->Connect(XRCID("wxfb_new_dialog_with_buttons"), wxEVT_COMMAND_MENU_SELECTED,
                      wxCommandEventHandler(wxFormBuilder::OnNewDialogWithButtons), NULL, this);
    m_topWin->Connect(XRCID("wxfb_new_frame"), wxEVT_COMMAND_MENU_SELECTED,
                      wxCommandEventHandler(wxFormBuilder::OnNewFrame), NULL, this);
    m_topWin->Connect(XRCID("wxfb_new_panel"), wxEVT_COMMAND_MENU_SELECTED,
                      wxCommandEventHandler(wxFormBuilder::OnNewPanel), NULL, this);
    m_topWin->Connect(XRCID("wxfb_open"), wxEVT_COMMAND_MENU_SELECTED,
                      wxCommandEventHandler(wxFormBuilder::OpenWithWxFb), NULL, this);
    EventNotifier::Get()->Connect(wxEVT_TREE_ITEM_FILE_ACTIVATED, clCommandEventHandler(wxFormBuilder::OnOpenFile),
                                  NULL, this);
    EventNotifier::Get()->Bind(wxEVT_CONTEXT_MENU_FILE, &wxFormBuilder::OnShowFileContextMenu, this);
}

wxFormBuilder::~wxFormBuilder() {}

void wxFormBuilder::CreateToolBar(clToolBar* toolbar) { wxUnusedVar(toolbar); }

void wxFormBuilder::CreatePluginMenu(wxMenu* pluginsMenu)
{
    wxMenu* menu = new wxMenu();
    wxMenuItem* item(NULL);

    item = new wxMenuItem(menu, XRCID("wxfb_settings"), _("Settings..."), _("Settings..."), wxITEM_NORMAL);
    menu->Append(item);

    pluginsMenu->Append(wxID_ANY, wxT("wxFormBuilder"), menu);

    m_topWin->Connect(XRCID("wxfb_settings"), wxEVT_COMMAND_MENU_SELECTED,
                      wxCommandEventHandler(wxFormBuilder::OnSettings), NULL, this);
}

void wxFormBuilder::HookPopupMenu(wxMenu* menu, MenuType type)
{
    if(type == MenuTypeFileView_Folder) {
        menu->AppendSeparator();
        menu->Append(XRCID("WXFB_POPUP"), wxT("wxFormBuilder"), CreatePopupMenu());
    }
}

void wxFormBuilder::UnPlug()
{
    EventNotifier::Get()->Unbind(wxEVT_CONTEXT_MENU_FILE, &wxFormBuilder::OnShowFileContextMenu, this);
}

void wxFormBuilder::OnSettings(wxCommandEvent& e)
{
    FormBuildSettingsDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr);
    dlg.ShowModal();
}

wxMenu* wxFormBuilder::CreatePopupMenu()
{
    // Create the popup menu for the file explorer
    // The only menu that we are interseted is the file explorer menu
    wxMenu* menu = new wxMenu();
    wxMenuItem* item(NULL);

    item = new wxMenuItem(menu, XRCID("wxfb_new_dialog"), _("New wxDialog..."), wxEmptyString, wxITEM_NORMAL);
    menu->Append(item);

    item = new wxMenuItem(menu, XRCID("wxfb_new_dialog_with_buttons"), _("New wxDialog with Default Buttons..."),
                          wxEmptyString, wxITEM_NORMAL);
    menu->Append(item);

    item = new wxMenuItem(menu, XRCID("wxfb_new_frame"), _("New wxFrame..."), wxEmptyString, wxITEM_NORMAL);
    menu->Append(item);

    item = new wxMenuItem(menu, XRCID("wxfb_new_panel"), _("New wxPanel..."), wxEmptyString, wxITEM_NORMAL);
    menu->Append(item);

    return menu;
}

void wxFormBuilder::OnNewDialog(wxCommandEvent& e)
{
    wxFBItemDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr);
    dlg.SetTitle(_("New wxDialog"));
    if(dlg.ShowModal() == wxID_OK) {
        wxFBItemInfo info;
        info = dlg.GetData();
        info.kind = wxFBItemKind_Dialog;

        DoCreateWxFormBuilderProject(info);
    }
}

void wxFormBuilder::OnNewFrame(wxCommandEvent& e)
{
    wxFBItemDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr);
    dlg.SetTitle(_("New wxFrame"));
    if(dlg.ShowModal() == wxID_OK) {
        wxFBItemInfo info;
        info = dlg.GetData();
        info.kind = wxFBItemKind_Frame;
        DoCreateWxFormBuilderProject(info);
    }
}

void wxFormBuilder::OnNewPanel(wxCommandEvent& e)
{
    wxFBItemDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr);
    dlg.SetTitle(_("New wxPanel"));

    // no need for title in wxPanel
    dlg.DisableTitleField();

    if(dlg.ShowModal() == wxID_OK) {
        wxFBItemInfo info;
        info = dlg.GetData();
        info.kind = wxFBItemKind_Panel;
        DoCreateWxFormBuilderProject(info);
    }
}

void wxFormBuilder::DoCreateWxFormBuilderProject(const wxFBItemInfo& data)
{
    // add new virtual folder to the selected virtual directory
    wxString formbuilderVD;
    formbuilderVD = data.virtualFolder.BeforeFirst(wxT(':'));

    m_mgr->CreateVirtualDirectory(formbuilderVD, wxT("formbuilder"));
    wxString templateFile(m_mgr->GetInstallDirectory() + wxT("/templates/formbuilder/"));

    switch(data.kind) {
    default:
    case wxFBItemKind_Dialog:
        templateFile << wxT("DialogTemplate.fbp");
        break;
    case wxFBItemKind_Frame:
        templateFile << wxT("FrameTemplate.fbp");
        break;
    case wxFBItemKind_Panel:
        templateFile << wxT("PanelTemplate.fbp");
        break;
    case wxFBItemKind_Dialog_With_Buttons:
        templateFile << wxT("DialogTemplateWithButtons.fbp");
        break;
    }

    wxFileName tmplFile(templateFile);
    if(!tmplFile.FileExists()) {
        wxMessageBox(wxString::Format(_("Can't find wxFormBuilder template file '%s'"), tmplFile.GetFullPath().c_str()),
                     _("CodeLite"), wxOK | wxCENTER | wxICON_WARNING);
        return;
    }

    // place the files under the VD's project owner
    wxString err_msg;
    wxString project = data.virtualFolder.BeforeFirst(wxT(':'));
    ProjectPtr proj = m_mgr->GetWorkspace()->FindProjectByName(project, err_msg);
    if(proj) {
        wxString files_path = proj->GetFileName().GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME);
        // copy the file to here
        wxFileName fbpFile(files_path, data.file + wxT(".fbp"));
        if(!wxCopyFile(tmplFile.GetFullPath(), fbpFile.GetFullPath())) {
            wxMessageBox(wxString::Format(_("Failed to copy template file to '%s'"), fbpFile.GetFullPath().c_str()),
                         _("CodeLite"), wxOK | wxCENTER | wxICON_WARNING);
            return;
        }
        // open the file, and replace expand its macros
        wxString content;
        if(!ReadFileWithConversion(fbpFile.GetFullPath().c_str(), content)) {
            wxMessageBox(wxString::Format(_("Failed to read file '%s'"), fbpFile.GetFullPath().c_str()), _("CodeLite"),
                         wxOK | wxCENTER | wxICON_WARNING);
            return;
        }

        content.Replace(wxT("$(BaseFileName)"), data.file);
        content.Replace(wxT("$(ProjectName)"), data.className);
        content.Replace(wxT("$(Title)"), data.title);
        content.Replace(wxT("$(ClassName)"), data.className);

        if(!WriteFileWithBackup(fbpFile.GetFullPath().c_str(), content, false)) {
            wxMessageBox(wxString::Format(_("Failed to write file '%s'"), fbpFile.GetFullPath().c_str()), _("CodeLite"),
                         wxOK | wxCENTER | wxICON_WARNING);
            return;
        }

        // add the file to the project
        wxArrayString paths;
        paths.Add(fbpFile.GetFullPath());
        m_mgr->AddFilesToVirtualFolder(project + wxT(":formbuilder"), paths);

        // // first we launch wxFB with the -g flag set
        wxString genFileCmd;
        genFileCmd << GetWxFBPath() << wxT(" -g ") << fbpFile.GetFullPath();

        wxArrayString dummy, filesToAdd;
        ProcUtils::SafeExecuteCommand(genFileCmd, dummy);

        wxFileName cppFile(fbpFile.GetPath(), data.file + wxT(".cpp"));
        wxFileName headerFile(fbpFile.GetPath(), data.file + wxT(".h"));

        if(cppFile.FileExists()) { filesToAdd.Add(cppFile.GetFullPath()); }

        if(headerFile.FileExists()) { filesToAdd.Add(headerFile.GetFullPath()); }

        if(filesToAdd.GetCount()) { m_mgr->AddFilesToVirtualFolder(data.virtualFolder, filesToAdd); }

        DoLaunchWxFB(fbpFile.GetFullPath());
    }
}

void wxFormBuilder::OpenWithWxFb(wxCommandEvent& e)
{
    // get the file name
    TreeItemInfo item = m_mgr->GetSelectedTreeItemInfo(TreeFileView);
    if(item.m_item.IsOk() && item.m_itemType == ProjectItem::TypeFile) {
        if(item.m_fileName.GetExt() == wxT("fbp")) {
            DoLaunchWxFB(item.m_fileName.GetFullPath());
        } else {
            wxMessageBox(_("Please select a 'fbp' (Form Builder Project) file only"), _("CodeLite"),
                         wxOK | wxCENTER | wxICON_INFORMATION);
            return;
        }
    }
}

void wxFormBuilder::DoLaunchWxFB(const wxString& file)
{
    wxString fbpath = GetWxFBPath();
    //	if (fbpath.IsEmpty()) {
    //		wxMessageBox(_("Failed to launch wxFormBuilder, no path specified\nPlease set wxFormBuilder path from
    // Plugins
    //-> wxFormBuilder -> Settings..."),
    //		             _("CodeLite"), wxOK|wxCENTER|wxICON_WARNING);
    //		return;
    //	}
    ConfFormBuilder confData;
    m_mgr->GetConfigTool()->ReadObject(wxT("wxFormBuilder"), &confData);
    wxString cmd = confData.GetCommand();
    cmd.Replace(wxT("$(wxfb)"), fbpath);
    cmd.Replace(wxT("$(wxfb_project)"), wxString::Format(wxT("\"%s\""), file.c_str()));

    WrapInShell(cmd);
    CreateAsyncProcess(this, cmd, IProcessCreateWithHiddenConsole);
}

wxString wxFormBuilder::GetWxFBPath()
{
    // Launch wxFB
    ConfFormBuilder confData;
    m_mgr->GetConfigTool()->ReadObject(wxT("wxFormBuilder"), &confData);
    wxString fbpath = confData.GetFbPath();

#ifdef __WXGTK__
    if(fbpath.IsEmpty()) {
        // try to locate the file at '/usr/bin' or '/usr/local/bin'
        if(wxFileName::FileExists(wxT("/usr/local/bin/wxformbuilder"))) {
            fbpath = wxT("/usr/local/bin/wxformbuilder");
        } else if(wxFileName::FileExists(wxT("/usr/bin/wxformbuilder"))) {
            fbpath = wxT("/usr/bin/wxformbuilder");
        }
    }
#endif
    if(fbpath.IsEmpty()) {
        fbpath = wxT("wxformbuilder");
    }
    return fbpath;
}

void wxFormBuilder::OnNewDialogWithButtons(wxCommandEvent& e)
{
    wxFBItemDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr);
    dlg.SetTitle(_("New wxDialog with Default Buttons"));
    if(dlg.ShowModal() == wxID_OK) {
        wxFBItemInfo info;
        info = dlg.GetData();
        info.kind = wxFBItemKind_Dialog_With_Buttons;

        DoCreateWxFormBuilderProject(info);
    }
}

void wxFormBuilder::OnOpenFile(clCommandEvent& e)
{
    e.Skip();
    // launch it with the default application
    wxFileName fullpath(e.GetFileName());
    if(fullpath.GetExt().MakeLower() != wxT("fbp")) { return; }

#ifdef __WXGTK__
    e.Skip(false);
    // Under Linux, use xdg-open
    wxString cmd;
    cmd << wxT("/bin/sh -c 'xdg-open \"") << fullpath.GetFullPath() << wxT("\"' 2> /dev/null");
    wxExecute(cmd);
    return;
#else
    wxMimeTypesManager* mgr = wxTheMimeTypesManager;
    wxFileType* type = mgr->GetFileTypeFromExtension(fullpath.GetExt());
    if(type) {
        wxString cmd = type->GetOpenCommand(fullpath.GetFullPath());
        wxDELETE(type);
        if(cmd.IsEmpty() == false) {
            e.Skip(false);
            wxExecute(cmd);
        }
    }
#endif
}

void wxFormBuilder::OnWxFBTerminated(clProcessEvent& e)
{
    if(e.GetProcess()) { delete e.GetProcess(); }
}

void wxFormBuilder::OnShowFileContextMenu(clContextMenuEvent& event)
{
    event.Skip();
    wxFileName file(event.GetFileName());
    if(file.GetExt() == "fbp") {
        wxMenu* menu = event.GetMenu();
        menu->PrependSeparator();
        menu->Prepend(
            new wxMenuItem(menu, XRCID("wxfb_open"), _("Open with wxFormBuilder..."), wxEmptyString, wxITEM_NORMAL));
    }
}