| 1 |
#include "codedesigner.h" |
| 2 |
#include "codedesignerdlg.h" |
| 3 |
#include "project.h" |
| 4 |
#include "confcodedesigner.h" |
| 5 |
#include "procutils.h" |
| 6 |
#include "workspace.h" |
| 7 |
#include "processreaderthread.h" |
| 8 |
#include "asyncprocess.h" |
| 9 |
#include "globals.h" |
| 10 |
#include "virtualdirectoryselector.h" |
| 11 |
#include <wx/menu.h> |
| 12 |
#include <wx/xrc/xmlres.h> |
| 13 |
#include <wx/mimetype.h> |
| 14 |
#include <wx/file.h> |
| 15 |
#include <wx/listimpl.cpp> |
| 16 |
#include <wx/tokenzr.h> |
| 17 |
|
| 18 |
#include <vector> |
| 19 |
#include <algorithm> |
| 20 |
|
| 21 |
#include "icons/ClassDiag.xpm" |
| 22 |
#include "icons/StateChartDiag.xpm" |
| 23 |
#include "icons/SimpleStateChartDiag.xpm" |
| 24 |
|
| 25 |
#define TOPIC wxT("CODEDESIGNER SESSION") |
| 26 |
|
| 27 |
wxEventType wxEVT_IPC_POKE = wxNewEventType(); |
| 28 |
|
| 29 |
WX_DEFINE_LIST(ConnectionList); |
| 30 |
|
| 31 |
static CodeDesigner* thePlugin = NULL; |
| 32 |
|
| 33 |
// Define the plugin entry point |
| 34 |
extern "C" EXPORT IPlugin* CreatePlugin(IManager* manager) |
| 35 |
{ |
| 36 |
if(thePlugin == 0) { |
| 37 |
thePlugin = new CodeDesigner(manager); |
| 38 |
} |
| 39 |
return thePlugin; |
| 40 |
} |
| 41 |
|
| 42 |
extern "C" EXPORT PluginInfo GetPluginInfo() |
| 43 |
{ |
| 44 |
PluginInfo info; |
| 45 |
info.SetAuthor(wxT("Michal Bližňák")); |
| 46 |
info.SetName(wxT("CodeDesigner")); |
| 47 |
info.SetDescription(_("CodeDesigner RAD integration with CodeLite")); |
| 48 |
info.SetVersion(wxT("v1.0")); |
| 49 |
return info; |
| 50 |
} |
| 51 |
|
| 52 |
extern "C" EXPORT int GetPluginInterfaceVersion() { return PLUGIN_INTERFACE_VERSION; } |
| 53 |
|
| 54 |
CodeDesigner::CodeDesigner(IManager* manager) |
| 55 |
: IPlugin(manager) |
| 56 |
, m_addFileMenu(true) |
| 57 |
{ |
| 58 |
m_longName = _("CodeDesigner RAD integration with CodeLite"); |
| 59 |
m_shortName = wxT("CodeDesigner"); |
| 60 |
|
| 61 |
m_Server = NULL; |
| 62 |
|
| 63 |
m_mgr->GetTheApp()->Connect(XRCID("cd_open"), wxEVT_COMMAND_MENU_SELECTED, |
| 64 |
wxCommandEventHandler(CodeDesigner::OnOpenWithCD), NULL, this); |
| 65 |
m_mgr->GetTheApp()->Connect(XRCID("cd_open"), wxEVT_UPDATE_UI, |
| 66 |
wxUpdateUIEventHandler(CodeDesigner::OnUpdateOpenWithCD), NULL, this); |
| 67 |
m_mgr->GetTheApp()->Connect(XRCID("cd_new_simplestatechart"), wxEVT_COMMAND_MENU_SELECTED, |
| 68 |
wxCommandEventHandler(CodeDesigner::OnNewSCH), NULL, this); |
| 69 |
m_mgr->GetTheApp()->Connect(XRCID("cd_new_hierarchicalstatechart"), wxEVT_COMMAND_MENU_SELECTED, |
| 70 |
wxCommandEventHandler(CodeDesigner::OnNewHSCH), NULL, this); |
| 71 |
m_mgr->GetTheApp()->Connect(XRCID("cd_new_classdiagram"), wxEVT_COMMAND_MENU_SELECTED, |
| 72 |
wxCommandEventHandler(CodeDesigner::OnNewCD), NULL, this); |
| 73 |
|
| 74 |
m_mgr->GetTheApp()->Connect(wxEVT_TREE_ITEM_FILE_ACTIVATED, wxCommandEventHandler(CodeDesigner::OnOpenFile), NULL, |
| 75 |
this); |
| 76 |
|
| 77 |
m_mgr->GetTheApp()->Connect(wxID_ANY, wxEVT_PROC_TERMINATED, wxCommandEventHandler(CodeDesigner::OnCDTerminated), |
| 78 |
NULL, this); |
| 79 |
|
| 80 |
Connect(wxID_ANY, wxEVT_IPC_POKE, wxCommandEventHandler(CodeDesigner::OnPokeData)); |
| 81 |
|
| 82 |
// start IPC server |
| 83 |
StartServer(); |
| 84 |
} |
| 85 |
|
| 86 |
CodeDesigner::~CodeDesigner() { delete m_Server; } |
| 87 |
|
| 88 |
clToolBar* CodeDesigner::CreateToolBar(wxWindow* parent) |
| 89 |
{ |
| 90 |
// Create the toolbar to be used by the plugin |
| 91 |
clToolBar* tb(NULL); |
| 92 |
|
| 93 |
/* |
| 94 |
// You can use the below code a snippet: |
| 95 |
// First, check that CodeLite allows plugin to register plugins |
| 96 |
if (m_mgr->AllowToolbar()) { |
| 97 |
// Support both toolbars icon size |
| 98 |
int size = m_mgr->GetToolbarIconSize(); |
| 99 |
|
| 100 |
// Allocate new toolbar, which will be freed later by CodeLite |
| 101 |
tb = new clToolBar(parent, wxID_ANY, wxDefaultPosition, wxDefaultSize, clTB_DEFAULT_STYLE); |
| 102 |
|
| 103 |
// Set the toolbar size |
| 104 |
tb->SetToolBitmapSize(wxSize(size, size)); |
| 105 |
|
| 106 |
// Add tools to the plugins toolbar. You must provide 2 sets of icons: 24x24 and 16x16 |
| 107 |
if (size == 24) { |
| 108 |
tb->AddTool(XRCID("new_plugin"), wxT("New CodeLite Plugin Project"), |
| 109 |
wxXmlResource::Get()->LoadBitmap(wxT("plugin24")), wxT("New Plugin Wizard...")); |
| 110 |
tb->AddTool(XRCID("new_class"), wxT("Create New Class"), |
| 111 |
wxXmlResource::Get()->LoadBitmap(wxT("class24")), wxT("New Class...")); |
| 112 |
tb->AddTool(XRCID("new_wx_project"), wxT("New wxWidget Project"), |
| 113 |
wxXmlResource::Get()->LoadBitmap(wxT("new_wx_project24")), wxT("New wxWidget Project")); |
| 114 |
} else { |
| 115 |
tb->AddTool(XRCID("new_plugin"), wxT("New CodeLite Plugin Project"), |
| 116 |
wxXmlResource::Get()->LoadBitmap(wxT("plugin16")), wxT("New Plugin Wizard...")); |
| 117 |
tb->AddTool(XRCID("new_class"), wxT("Create New Class"), |
| 118 |
wxXmlResource::Get()->LoadBitmap(wxT("class16")), wxT("New Class...")); |
| 119 |
tb->AddTool(XRCID("new_wx_project"), wxT("New wxWidget Project"), |
| 120 |
wxXmlResource::Get()->LoadBitmap(wxT("new_wx_project16")), wxT("New wxWidget Project")); |
| 121 |
} |
| 122 |
// And finally, we must call 'Realize()' |
| 123 |
tb->Realize(); |
| 124 |
} |
| 125 |
|
| 126 |
// return the toolbar, it can be NULL if CodeLite does not allow plugins to register toolbars |
| 127 |
// or in case the plugin simply does not require toolbar |
| 128 |
*/ |
| 129 |
return tb; |
| 130 |
} |
| 131 |
|
| 132 |
void CodeDesigner::CreatePluginMenu(wxMenu* pluginsMenu) |
| 133 |
{ |
| 134 |
// You can use the below code a snippet: |
| 135 |
wxMenu* menu = new wxMenu(); |
| 136 |
wxMenuItem* item(NULL); |
| 137 |
|
| 138 |
item = new wxMenuItem(menu, XRCID("cd_settings"), _("Settings..."), wxEmptyString, wxITEM_NORMAL); |
| 139 |
menu->Append(item); |
| 140 |
|
| 141 |
pluginsMenu->Append(wxID_ANY, _("CodeDesigner"), menu); |
| 142 |
|
| 143 |
m_mgr->GetTheApp()->Connect(XRCID("cd_settings"), wxEVT_COMMAND_MENU_SELECTED, |
| 144 |
wxCommandEventHandler(CodeDesigner::OnSettings), NULL, this); |
| 145 |
} |
| 146 |
|
| 147 |
void CodeDesigner::HookPopupMenu(wxMenu* menu, MenuType type) |
| 148 |
{ |
| 149 |
if(type == MenuTypeEditor) { |
| 150 |
// TODO::Append items for the editor context menu |
| 151 |
} else if(type == MenuTypeFileExplorer) { |
| 152 |
// TODO::Append items for the file explorer context menu |
| 153 |
} else if(type == MenuTypeFileView_Workspace) { |
| 154 |
// TODO::Append items for the file view / workspace context menu |
| 155 |
} else if(type == MenuTypeFileView_Project) { |
| 156 |
// TODO::Append items for the file view/Project context menu |
| 157 |
} else if(type == MenuTypeFileView_Folder) { |
| 158 |
// TODO::Append items for the file view/Virtual folder context menu |
| 159 |
if(!menu->FindItem(XRCID("cd_popup"))) { |
| 160 |
m_separatorItem = menu->AppendSeparator(); |
| 161 |
menu->Append(XRCID("cd_popup"), _("CodeDesigner"), CreatePopupMenu()); |
| 162 |
} |
| 163 |
} else if(type == MenuTypeFileView_File) { |
| 164 |
// TODO::Append items for the file view/file context menu |
| 165 |
if(m_addFileMenu) { |
| 166 |
wxMenuItem* item = |
| 167 |
new wxMenuItem(menu, XRCID("cd_open"), _("Open with CodeDesigner..."), wxEmptyString, wxITEM_NORMAL); |
| 168 |
|
| 169 |
menu->PrependSeparator(); |
| 170 |
menu->Prepend(item); |
| 171 |
|
| 172 |
m_addFileMenu = false; |
| 173 |
} |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
void CodeDesigner::UnHookPopupMenu(wxMenu* menu, MenuType type) |
| 178 |
{ |
| 179 |
if(type == MenuTypeEditor) { |
| 180 |
// TODO::Unhook items for the editor context menu |
| 181 |
} else if(type == MenuTypeFileExplorer) { |
| 182 |
// TODO::Unhook items for the file explorer context menu |
| 183 |
} else if(type == MenuTypeFileView_Workspace) { |
| 184 |
// TODO::Unhook items for the file view / workspace context menu |
| 185 |
} else if(type == MenuTypeFileView_Project) { |
| 186 |
// TODO::Unhook items for the file view/Project context menu |
| 187 |
} else if(type == MenuTypeFileView_Folder) { |
| 188 |
// TODO::Unhook items for the file view/Virtual folder context menu |
| 189 |
} else if(type == MenuTypeFileView_File) { |
| 190 |
// TODO::Unhook items for the file view/file context menu |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
void CodeDesigner::UnPlug() |
| 195 |
{ |
| 196 |
// TODO:: perform the unplug action for this plugin |
| 197 |
} |
| 198 |
|
| 199 |
void CodeDesigner::OnSettings(wxCommandEvent& e) |
| 200 |
{ |
| 201 |
CodeDesignerDlg dlg(m_mgr->GetTheApp()->GetTopWindow()); |
| 202 |
|
| 203 |
ConfCodeDesigner data; |
| 204 |
m_mgr->GetConfigTool()->ReadObject(wxT("CodeDesigner"), &data); |
| 205 |
|
| 206 |
dlg.SetPort(data.GetPort()); |
| 207 |
dlg.SetPath(data.GetPath()); |
| 208 |
|
| 209 |
if(dlg.ShowModal() == wxID_OK) { |
| 210 |
bool fRestartServer = false; |
| 211 |
|
| 212 |
if(data.GetPort() != dlg.GetPort()) fRestartServer = true; |
| 213 |
|
| 214 |
data.SetPort(dlg.GetPort()); |
| 215 |
data.SetPath(dlg.GetPath()); |
| 216 |
|
| 217 |
m_mgr->GetConfigTool()->WriteObject(wxT("CodeDesigner"), &data); |
| 218 |
|
| 219 |
if(fRestartServer) StartServer(); |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
wxMenu* CodeDesigner::CreatePopupMenu() |
| 224 |
{ |
| 225 |
// Create the popup menu for the file explorer |
| 226 |
// The only menu that we are interseted is the file explorer menu |
| 227 |
wxMenu* menu = new wxMenu(); |
| 228 |
wxMenuItem* item(NULL); |
| 229 |
|
| 230 |
item = new wxMenuItem(menu, XRCID("cd_new_simplestatechart"), _("New simple state chart..."), wxEmptyString, |
| 231 |
wxITEM_NORMAL); |
| 232 |
item->SetBitmap(wxBitmap(SimpleStateChartDiag_xpm)); |
| 233 |
menu->Append(item); |
| 234 |
|
| 235 |
item = new wxMenuItem(menu, XRCID("cd_new_hierarchicalstatechart"), _("New hierarchical state chart..."), |
| 236 |
wxEmptyString, wxITEM_NORMAL); |
| 237 |
item->SetBitmap(wxBitmap(StateChartDiag_xpm)); |
| 238 |
menu->Append(item); |
| 239 |
|
| 240 |
item = new wxMenuItem(menu, XRCID("cd_new_classdiagram"), _("New class diagram..."), wxEmptyString, wxITEM_NORMAL); |
| 241 |
item->SetBitmap(wxBitmap(ClassDiag_xpm)); |
| 242 |
menu->Append(item); |
| 243 |
|
| 244 |
return menu; |
| 245 |
} |
| 246 |
|
| 247 |
void CodeDesigner::OnOpenFile(wxCommandEvent& e) |
| 248 |
{ |
| 249 |
wxString* fn = (wxString*)e.GetClientData(); |
| 250 |
if(fn) { |
| 251 |
// launch it with the default application |
| 252 |
wxFileName fullpath(*fn); |
| 253 |
if(fullpath.GetExt().MakeLower() != wxT("cdp")) { |
| 254 |
e.Skip(); |
| 255 |
return; |
| 256 |
} |
| 257 |
#ifdef __WXGTK__ |
| 258 |
// Under Linux, use xdg-open |
| 259 |
wxString cmd; |
| 260 |
cmd << wxT("/bin/sh -c 'xdg-open \"") << fullpath.GetFullPath() << wxT("\"' 2> /dev/null"); |
| 261 |
wxExecute(cmd); |
| 262 |
return; |
| 263 |
#else |
| 264 |
|
| 265 |
wxMimeTypesManager* mgr = wxTheMimeTypesManager; |
| 266 |
wxFileType* type = mgr->GetFileTypeFromExtension(fullpath.GetExt()); |
| 267 |
if(type) { |
| 268 |
wxString cmd = type->GetOpenCommand(fullpath.GetFullPath()); |
| 269 |
delete type; |
| 270 |
|
| 271 |
if(cmd.IsEmpty() == false) { |
| 272 |
wxExecute(cmd); |
| 273 |
return; |
| 274 |
} |
| 275 |
} |
| 276 |
#endif |
| 277 |
} |
| 278 |
|
| 279 |
// we failed, call event.Skip() |
| 280 |
e.Skip(); |
| 281 |
} |
| 282 |
|
| 283 |
void CodeDesigner::OnOpenWithCD(wxCommandEvent& e) |
| 284 |
{ |
| 285 |
// get the file name |
| 286 |
TreeItemInfo item = m_mgr->GetSelectedTreeItemInfo(TreeFileView); |
| 287 |
if(item.m_item.IsOk() && item.m_itemType == ProjectItem::TypeFile) { |
| 288 |
if(item.m_fileName.GetExt() == wxT("cdp")) { |
| 289 |
DoLaunchCD(item.m_fileName.GetFullPath()); |
| 290 |
} else { |
| 291 |
wxMessageBox(_("Please select a 'cdp' (CodeDesigner Project) file only"), _("CodeLite"), |
| 292 |
wxOK | wxCENTER | wxICON_INFORMATION); |
| 293 |
return; |
| 294 |
} |
| 295 |
} |
| 296 |
} |
| 297 |
|
| 298 |
void CodeDesigner::DoLaunchCD(const wxString& file) |
| 299 |
{ |
| 300 |
wxString cdpath = GetCDPath(); |
| 301 |
|
| 302 |
if(cdpath.IsEmpty()) { |
| 303 |
wxMessageBox(_("Failed to launch CodeDesigner, no path specified\nPlease set CodeDesigner path from Plugins -> " |
| 304 |
"CodeDesigner -> Settings..."), |
| 305 |
_("CodeLite"), wxOK | wxCENTER | wxICON_WARNING); |
| 306 |
return; |
| 307 |
} |
| 308 |
|
| 309 |
wxString cmd = wxT("$(cd) $(cd_project)"); |
| 310 |
cmd.Replace(wxT("$(cd)"), cdpath); |
| 311 |
cmd.Replace(wxT("$(cd_project)"), wxString::Format(wxT("\"%s\""), file.c_str())); |
| 312 |
|
| 313 |
#ifdef __WXMSW__ |
| 314 |
wxExecute(cmd, wxEXEC_ASYNC); |
| 315 |
#else |
| 316 |
WrapInShell(cmd); |
| 317 |
CreateAsyncProcess(this, cmd, IProcessCreateWithHiddenConsole); |
| 318 |
#endif |
| 319 |
} |
| 320 |
|
| 321 |
wxString CodeDesigner::GetCDPath() |
| 322 |
{ |
| 323 |
// Launch wxFB |
| 324 |
ConfCodeDesigner confData; |
| 325 |
m_mgr->GetConfigTool()->ReadObject(wxT("CodeDesigner"), &confData); |
| 326 |
wxString cdpath = confData.GetPath(); |
| 327 |
|
| 328 |
#ifdef __WXGTK__ |
| 329 |
if(cdpath.IsEmpty()) { |
| 330 |
// try to locate the file at '/usr/bin' or '/usr/local/bin' |
| 331 |
if(wxFileName::FileExists(wxT("/usr/local/bin/codedesigner"))) { |
| 332 |
cdpath = wxT("/usr/local/bin/codedesigner"); |
| 333 |
} else if(wxFileName::FileExists(wxT("/usr/bin/codedesigner"))) { |
| 334 |
cdpath = wxT("/usr/bin/codedesigner"); |
| 335 |
} |
| 336 |
} |
| 337 |
#endif |
| 338 |
return cdpath; |
| 339 |
} |
| 340 |
|
| 341 |
void CodeDesigner::OnUpdateOpenWithCD(wxUpdateUIEvent& e) |
| 342 |
{ |
| 343 |
TreeItemInfo item = m_mgr->GetSelectedTreeItemInfo(TreeFileView); |
| 344 |
if(item.m_item.IsOk() && item.m_itemType == ProjectItem::TypeFile) { |
| 345 |
e.Enable(item.m_fileName.GetExt() == wxT("cdp")); |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
void CodeDesigner::OnCDTerminated(wxCommandEvent& e) |
| 350 |
{ |
| 351 |
ProcessEventData* ped = (ProcessEventData*)e.GetClientData(); |
| 352 |
if(ped) { |
| 353 |
if(ped->GetProcess()) delete ped->GetProcess(); |
| 354 |
delete ped; |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
void CodeDesigner::OnNewCD(wxCommandEvent& e) |
| 359 |
{ |
| 360 |
CodeDesignerItemDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr, false); |
| 361 |
dlg.SetTitle(_("New Class Diagram")); |
| 362 |
|
| 363 |
CDItemInfo info; |
| 364 |
info.m_PackageName = wxT("Classes"); |
| 365 |
|
| 366 |
dlg.SetCDInfo(info); |
| 367 |
if(dlg.ShowModal() == wxID_OK) { |
| 368 |
info = dlg.GetCDInfo(); |
| 369 |
info.m_Type = cdtClassDiagram; |
| 370 |
|
| 371 |
DoCreateCDProject(info); |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
void CodeDesigner::OnNewHSCH(wxCommandEvent& e) |
| 376 |
{ |
| 377 |
CodeDesignerItemDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr, true); |
| 378 |
dlg.SetTitle(_("New Hierarchical State Chart")); |
| 379 |
|
| 380 |
CDItemInfo info; |
| 381 |
info.m_PackageName = wxT("State charts"); |
| 382 |
|
| 383 |
dlg.SetCDInfo(info); |
| 384 |
if(dlg.ShowModal() == wxID_OK) { |
| 385 |
info = dlg.GetCDInfo(); |
| 386 |
info.m_Type = cdtHierarchicalStateChart; |
| 387 |
|
| 388 |
DoCreateCDProject(info); |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
void CodeDesigner::OnNewSCH(wxCommandEvent& e) |
| 393 |
{ |
| 394 |
CodeDesignerItemDlg dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr, true); |
| 395 |
dlg.SetTitle(_("New Simple State Chart")); |
| 396 |
|
| 397 |
CDItemInfo info; |
| 398 |
info.m_PackageName = wxT("State charts"); |
| 399 |
|
| 400 |
dlg.SetCDInfo(info); |
| 401 |
if(dlg.ShowModal() == wxID_OK) { |
| 402 |
info = dlg.GetCDInfo(); |
| 403 |
info.m_Type = cdtStateChart; |
| 404 |
|
| 405 |
DoCreateCDProject(info); |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
void CodeDesigner::DoCreateCDProject(CDItemInfo& info) |
| 410 |
{ |
| 411 |
// add new virtual folder to the selected virtual directory |
| 412 |
wxString codedesignerVD; |
| 413 |
codedesignerVD = info.m_TreeFolderName.BeforeFirst(wxT(':')); |
| 414 |
|
| 415 |
m_mgr->CreateVirtualDirectory(codedesignerVD, wxT("codedesigner")); |
| 416 |
wxString templateFile(m_mgr->GetInstallDirectory() + wxT("/templates/codedesigner/")); |
| 417 |
// wxString |
| 418 |
// templateFile(wxT("/home/michal/Src/Projekty/CodeLite_dev/CLInstall/share/codelite/templates/codedesigner/")); |
| 419 |
|
| 420 |
switch(info.m_Type) { |
| 421 |
default: |
| 422 |
case cdtClassDiagram: |
| 423 |
templateFile << wxT("CDTemplate.cdp"); |
| 424 |
break; |
| 425 |
case cdtStateChart: |
| 426 |
if(info.m_UseMain) |
| 427 |
templateFile << wxT("SCHTemplateWithMain.cdp"); |
| 428 |
else |
| 429 |
templateFile << wxT("SCHTemplate.cdp"); |
| 430 |
break; |
| 431 |
case cdtHierarchicalStateChart: |
| 432 |
if(info.m_UseMain) |
| 433 |
templateFile << wxT("HSCHTemplateWithMain.cdp"); |
| 434 |
else |
| 435 |
templateFile << wxT("HSCHTemplate.cdp"); |
| 436 |
break; |
| 437 |
} |
| 438 |
|
| 439 |
wxFileName tmplFile(templateFile); |
| 440 |
if(!tmplFile.FileExists()) { |
| 441 |
wxMessageBox(wxString::Format(_("Can't find CodeDesigner template file '%s'"), tmplFile.GetFullPath().c_str()), |
| 442 |
_("CodeLite"), wxOK | wxCENTER | wxICON_WARNING); |
| 443 |
return; |
| 444 |
} |
| 445 |
|
| 446 |
// place the files under the VD's project owner |
| 447 |
wxString err_msg; |
| 448 |
wxString project = info.m_TreeFolderName.BeforeFirst(wxT(':')); |
| 449 |
ProjectPtr proj = m_mgr->GetWorkspace()->FindProjectByName(project, err_msg); |
| 450 |
if(proj) { |
| 451 |
wxString files_path = proj->GetFileName().GetPath(wxPATH_GET_SEPARATOR | wxPATH_GET_VOLUME); |
| 452 |
// copy the file to here |
| 453 |
wxFileName cdpFile(files_path, info.m_DiagramName.Lower() + wxT(".cdp")); |
| 454 |
if(!wxCopyFile(tmplFile.GetFullPath(), cdpFile.GetFullPath())) { |
| 455 |
wxMessageBox(wxString::Format(_("Failed to copy template file to '%s'"), cdpFile.GetFullPath().c_str()), |
| 456 |
_("CodeLite"), wxOK | wxCENTER | wxICON_WARNING); |
| 457 |
return; |
| 458 |
} |
| 459 |
// open the file, and replace expand its macros |
| 460 |
wxString content; |
| 461 |
if(!ReadFileWithConversion(cdpFile.GetFullPath().c_str(), content)) { |
| 462 |
wxMessageBox(wxString::Format(_("Failed to read file '%s'"), cdpFile.GetFullPath().c_str()), _("CodeLite"), |
| 463 |
wxOK | wxCENTER | wxICON_WARNING); |
| 464 |
return; |
| 465 |
} |
| 466 |
|
| 467 |
wxString fileName = info.m_DiagramName.Lower(); |
| 468 |
|
| 469 |
content.Replace(wxT("$(BaseFileName)"), fileName); |
| 470 |
content.Replace(wxT("$(CIFileName)"), fileName + wxT("_ci")); |
| 471 |
content.Replace(wxT("$(PackageName)"), info.m_PackageName); |
| 472 |
content.Replace(wxT("$(DiagramName)"), info.m_DiagramName); |
| 473 |
content.Replace(wxT("$(Language)"), info.m_Language); |
| 474 |
|
| 475 |
if(!WriteFileWithBackup(cdpFile.GetFullPath().c_str(), content, false)) { |
| 476 |
wxMessageBox(wxString::Format(_("Failed to write file '%s'"), cdpFile.GetFullPath().c_str()), _("CodeLite"), |
| 477 |
wxOK | wxCENTER | wxICON_WARNING); |
| 478 |
return; |
| 479 |
} |
| 480 |
|
| 481 |
// add the file to the project |
| 482 |
wxArrayString paths; |
| 483 |
paths.Add(cdpFile.GetFullPath()); |
| 484 |
m_mgr->AddFilesToVirtualFolder(project + wxT(":codedesigner"), paths); |
| 485 |
|
| 486 |
if(info.m_GenerateCode) { |
| 487 |
// first we launch CodeDesigner with the -g flag set |
| 488 |
wxString genFileCmd; |
| 489 |
genFileCmd << GetCDPath() << wxT(" -g ") << cdpFile.GetFullPath(); |
| 490 |
|
| 491 |
wxArrayString dummy; |
| 492 |
ProcUtils::SafeExecuteCommand(genFileCmd, dummy); |
| 493 |
|
| 494 |
wxFileName cFile(cdpFile.GetPath(), fileName + wxT(".c")); |
| 495 |
wxFileName cppFile(cdpFile.GetPath(), fileName + wxT(".cpp")); |
| 496 |
wxFileName cCiFile(cdpFile.GetPath(), fileName + wxT("_ci.c")); |
| 497 |
wxFileName cppCiFile(cdpFile.GetPath(), fileName + wxT("_ci.cpp")); |
| 498 |
wxFileName headerFile(cdpFile.GetPath(), fileName + wxT(".h")); |
| 499 |
wxFileName headerCiFile(cdpFile.GetPath(), fileName + wxT("_ci.h")); |
| 500 |
wxFileName pyFile(cdpFile.GetPath(), fileName + wxT(".py")); |
| 501 |
wxFileName pyCiFile(cdpFile.GetPath(), fileName + wxT("_ci.py")); |
| 502 |
|
| 503 |
paths.Clear(); |
| 504 |
|
| 505 |
if(cFile.FileExists()) paths.Add(cFile.GetFullPath()); |
| 506 |
if(cppFile.FileExists()) paths.Add(cppFile.GetFullPath()); |
| 507 |
if(headerFile.FileExists()) paths.Add(headerFile.GetFullPath()); |
| 508 |
if(cCiFile.FileExists()) paths.Add(cCiFile.GetFullPath()); |
| 509 |
if(cppCiFile.FileExists()) paths.Add(cppCiFile.GetFullPath()); |
| 510 |
if(headerCiFile.FileExists()) paths.Add(headerCiFile.GetFullPath()); |
| 511 |
if(pyFile.FileExists()) paths.Add(pyFile.GetFullPath()); |
| 512 |
if(pyCiFile.FileExists()) paths.Add(pyCiFile.GetFullPath()); |
| 513 |
|
| 514 |
if(paths.GetCount()) { |
| 515 |
m_mgr->AddFilesToVirtualFolder(info.m_TreeFolderName, paths); |
| 516 |
} |
| 517 |
|
| 518 |
RetagWorkspace(); |
| 519 |
} |
| 520 |
|
| 521 |
DoLaunchCD(cdpFile.GetFullPath()); |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
void CodeDesigner::StartServer() |
| 526 |
{ |
| 527 |
ConfCodeDesigner data; |
| 528 |
m_mgr->GetConfigTool()->ReadObject(wxT("CodeDesigner"), &data); |
| 529 |
|
| 530 |
delete m_Server; |
| 531 |
|
| 532 |
m_Server = new CDServer(); |
| 533 |
if(!m_Server->Create(data.GetPort())) { |
| 534 |
delete m_Server; |
| 535 |
m_Server = NULL; |
| 536 |
|
| 537 |
// wxMessageBox( _("Unable to create CodeDesigner plugin's IPC server. Please, check communication port value"), |
| 538 |
// _("CodeLite"), wxOK | wxICON_WARNING ); |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
void CodeDesigner::RetagWorkspace() |
| 543 |
{ |
| 544 |
wxCommandEvent e(wxEVT_COMMAND_MENU_SELECTED, XRCID("retag_workspace")); |
| 545 |
m_mgr->GetTheApp()->GetTopWindow()->GetEventHandler()->AddPendingEvent(e); |
| 546 |
} |
| 547 |
|
| 548 |
bool CodeDesigner::AddFilesToWorkspace(wxArrayString& paths, CDConnection* connection) |
| 549 |
{ |
| 550 |
// get tree folder if not specified |
| 551 |
if(connection->GetTreeFolder().IsEmpty()) { |
| 552 |
m_mgr->GetTheApp()->GetTopWindow()->Raise(); |
| 553 |
|
| 554 |
VirtualDirectorySelector dlg(m_mgr->GetTheApp()->GetTopWindow(), m_mgr->GetWorkspace(), wxT("")); |
| 555 |
dlg.SetTitle(_("Insert generated files into...")); |
| 556 |
|
| 557 |
if(dlg.ShowModal() == wxID_OK) { |
| 558 |
connection->SetTreeFolder(dlg.GetVirtualDirectoryPath()); |
| 559 |
} else |
| 560 |
return false; |
| 561 |
} |
| 562 |
|
| 563 |
// check whether given files are already included in the project |
| 564 |
wxArrayString arrFilesToAdd; |
| 565 |
|
| 566 |
wxString err_msg; |
| 567 |
wxString project = connection->GetTreeFolder().BeforeFirst(wxT(':')); |
| 568 |
ProjectPtr proj = m_mgr->GetWorkspace()->FindProjectByName(project, err_msg); |
| 569 |
if(proj) { |
| 570 |
std::vector<wxFileName> vctPresentFiles; |
| 571 |
proj->GetFiles(vctPresentFiles, true); |
| 572 |
|
| 573 |
for(size_t i = 0; i < paths.GetCount(); ++i) { |
| 574 |
wxFileName file(paths[i]); |
| 575 |
if(std::find(vctPresentFiles.begin(), vctPresentFiles.end(), file) == vctPresentFiles.end()) { |
| 576 |
arrFilesToAdd.Add(paths[i]); |
| 577 |
} |
| 578 |
} |
| 579 |
} |
| 580 |
|
| 581 |
// update CodeLite workspace |
| 582 |
if(!arrFilesToAdd.IsEmpty()) m_mgr->AddFilesToVirtualFolder(connection->GetTreeFolder(), arrFilesToAdd); |
| 583 |
|
| 584 |
RetagWorkspace(); |
| 585 |
|
| 586 |
return true; |
| 587 |
} |
| 588 |
|
| 589 |
void CodeDesigner::OnPokeData(wxCommandEvent& e) |
| 590 |
{ |
| 591 |
wxArrayString arrFiles; |
| 592 |
wxStringTokenizer tokens(e.GetString(), wxT("\n"), wxTOKEN_STRTOK); |
| 593 |
|
| 594 |
while(tokens.HasMoreTokens()) { |
| 595 |
arrFiles.Add(tokens.GetNextToken()); |
| 596 |
} |
| 597 |
|
| 598 |
AddFilesToWorkspace(arrFiles, (CDConnection*)e.GetClientData()); |
| 599 |
} |
| 600 |
|
| 601 |
// IPC ///////////////////////////////////////////////////////////////////////// |
| 602 |
|
| 603 |
CDConnection::CDConnection(CDServer* server) { m_Server = server; } |
| 604 |
|
| 605 |
CDConnection::~CDConnection() { Disconnect(); } |
| 606 |
|
| 607 |
bool CDConnection::OnDisconnect() |
| 608 |
{ |
| 609 |
m_Server->Disconnect(this); |
| 610 |
return true; |
| 611 |
} |
| 612 |
|
| 613 |
bool CDConnection::OnPoke(const wxString& topic, const wxString& item, wxChar* data, int size, wxIPCFormat format) |
| 614 |
{ |
| 615 |
if(topic == TOPIC) { |
| 616 |
if(item == wxT("ADD FILES")) { |
| 617 |
wxCommandEvent e(wxEVT_IPC_POKE, wxID_ANY); |
| 618 |
e.SetString(wxString(data)); |
| 619 |
e.SetClientData(this); |
| 620 |
|
| 621 |
wxPostEvent(thePlugin, e); |
| 622 |
} |
| 623 |
|
| 624 |
return true; |
| 625 |
} else |
| 626 |
return false; |
| 627 |
} |
| 628 |
|
| 629 |
CDServer::CDServer() { m_Connections.DeleteContents(true); } |
| 630 |
|
| 631 |
CDServer::~CDServer() { m_Connections.Clear(); } |
| 632 |
|
| 633 |
wxConnectionBase* CDServer::OnAcceptConnection(const wxString& topic) |
| 634 |
{ |
| 635 |
if(topic == TOPIC) { |
| 636 |
CDConnection* connection = new CDConnection(this); |
| 637 |
m_Connections.Append(connection); |
| 638 |
|
| 639 |
return connection; |
| 640 |
} else |
| 641 |
return NULL; |
| 642 |
} |
| 643 |
|
| 644 |
void CDServer::Disconnect(CDConnection* connection) |
| 645 |
{ |
| 646 |
if(connection) { |
| 647 |
m_Connections.DeleteObject(connection); |
| 648 |
} |
| 649 |
} |