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
|
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : templateclassdlg.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 "templateclassdlg.h"
#include "ColoursAndFontsManager.h"
#include "VirtualDirectorySelectorDlg.h"
#include "editor_config.h"
#include "event_notifier.h"
#include "imanager.h"
#include "project.h"
#include "snipwiz.h"
#include "swGlobals.h"
#include "swStringDb.h"
#include "workspace.h"
#include <wx/dirdlg.h>
#include <wx/filefn.h>
#include <wx/msgdlg.h>
#include <wx/textbuf.h>
#include <wx/textfile.h>
static const wxString swHeader = wxT("header");
static const wxString swSource = wxT("source");
static const wxString swPhClass = wxT("%CLASS%");
TemplateClassDlg::TemplateClassDlg(wxWindow* parent, SnipWiz* plugin, IManager* manager)
: TemplateClassBaseDlg(parent)
, m_plugin(plugin)
, m_pManager(manager)
{
Initialize();
GetSizer()->Fit(this);
}
void TemplateClassDlg::Initialize()
{
LexerConf::Ptr_t cppLexer = ColoursAndFontsManager::Get().GetLexer("c++");
if(cppLexer) {
cppLexer->Apply(m_textCtrlHeader, true);
cppLexer->Apply(m_textCtrlImpl, true);
}
GetStringDb()->Load(m_configPath + defaultTmplFile);
wxArrayString templates;
GetStringDb()->GetAllSets(templates);
for(wxUint32 i = 0; i < templates.GetCount(); i++) {
m_comboxTemplates->AppendString(templates[i]);
m_comboxCurrentTemplate->AppendString(templates[i]);
}
if(templates.GetCount()) {
m_comboxTemplates->Select(0);
wxString set = m_comboxTemplates->GetValue();
m_textCtrlHeader->SetValue(GetStringDb()->GetString(set, swHeader));
m_textCtrlImpl->SetValue(GetStringDb()->GetString(set, swSource));
m_comboxCurrentTemplate->Select(0);
}
TreeItemInfo item = m_pManager->GetSelectedTreeItemInfo(TreeFileView);
if(item.m_item.IsOk() && item.m_itemType == ProjectItem::TypeVirtualDirectory) {
m_virtualFolder = VirtualDirectorySelectorDlg::DoGetPath(m_pManager->GetWorkspaceTree(), item.m_item, false);
m_projectPath = item.m_fileName.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR);
}
m_textCtrlVD->SetValue(m_virtualFolder);
if(clCxxWorkspaceST::Get()->IsOpen()) {
wxString project, vd;
project = m_virtualFolder.BeforeFirst(wxT(':'));
vd = m_virtualFolder.AfterFirst(wxT(':'));
ProjectPtr proj = clCxxWorkspaceST::Get()->GetProject(project);
if(proj) {
m_projectPath = proj->GetBestPathForVD(vd);
}
}
if(m_virtualFolder.IsEmpty() == false) {
m_staticProjectTreeFolder->SetForegroundColour(wxColour(0, 128, 0));
}
m_textCtrlFilePath->SetValue(m_projectPath);
m_textCtrlClassName->SetFocus();
}
void TemplateClassDlg::OnClassNameEntered(wxCommandEvent& event)
{
wxUnusedVar(event);
wxString buffer = m_textCtrlClassName->GetValue();
if(buffer.IsEmpty()) {
m_textCtrlHeaderFile->SetValue(wxT(""));
m_textCtrlCppFile->SetValue(wxT(""));
return;
} else {
m_textCtrlHeaderFile->SetValue(buffer + ".hpp");
m_textCtrlCppFile->SetValue(buffer + ".cpp");
}
}
void TemplateClassDlg::OnBrowseVD(wxCommandEvent& event)
{
wxUnusedVar(event);
VirtualDirectorySelectorDlg dlg(this, m_pManager->GetWorkspace(), m_textCtrlVD->GetValue());
if(dlg.ShowModal() == wxID_OK) {
m_textCtrlVD->SetValue(dlg.GetVirtualDirectoryPath());
m_staticProjectTreeFolder->SetForegroundColour(wxColour(0, 128, 0));
m_staticProjectTreeFolder->Refresh();
}
}
void TemplateClassDlg::OnBrowseFilePath(wxCommandEvent& event)
{
wxUnusedVar(event);
wxString dir = wxT("");
if(wxFileName::DirExists(m_projectPath)) {
dir = m_projectPath;
}
dir = wxDirSelector(_("Select output folder"), dir, wxDD_DEFAULT_STYLE, wxDefaultPosition, this);
if(!dir.IsEmpty()) {
m_projectPath = dir;
m_textCtrlFilePath->SetValue(m_projectPath);
}
}
void TemplateClassDlg::OnGenerate(wxCommandEvent& event)
{
wxUnusedVar(event);
wxArrayString files;
wxString newClassName = m_textCtrlClassName->GetValue();
wxString baseClass = m_comboxCurrentTemplate->GetValue();
if(!wxEndsWithPathSeparator(m_projectPath))
m_projectPath += wxFILE_SEP_PATH;
wxString header_file_name = m_textCtrlHeaderFile->GetValue();
wxString impl_file_name = m_textCtrlCppFile->GetValue();
wxString header_file_content = GetStringDb()->GetString(baseClass, swHeader);
wxString impl_file_content = GetStringDb()->GetString(baseClass, swSource);
header_file_content.Trim();
impl_file_content.Trim();
wxString vdpath = m_textCtrlVD->GetValue();
// prepare the header file content + name
// and save it to disk
wxString buffer = header_file_content;
buffer.Replace(swPhClass, newClassName);
buffer.Replace(wxT("\v"), eol[m_curEol]);
files.Add(m_projectPath + header_file_name);
SaveBufferToFile(files.Item(0), buffer);
// prepare the implementation file content + name
// and save it to disk
if(!impl_file_content.empty()) {
buffer = wxString::Format(wxT("#include \"%s\"%s%s"), header_file_name, eol[m_curEol], eol[m_curEol]);
buffer += impl_file_content;
buffer.Replace(swPhClass, newClassName);
buffer.Replace(wxT("\v"), eol[m_curEol]);
files.Add(m_projectPath + impl_file_name);
SaveBufferToFile(files.Item(1), buffer);
}
// if virtual directory is provided, add the files there
if(!vdpath.empty()) {
// Create the Success message first, as 'files' may be altered during creation
wxString msg;
msg << wxString::Format(wxT("%s%s"), files.Item(0), eol[m_curEol])
<< wxString::Format(wxT("%s%s%s"), files.Item(1), eol[m_curEol], eol[m_curEol])
<< _("Files successfully created.");
// We have a .cpp and an .h file, and there may well be a :src and an :include folder available
// So try to place the files appropriately. If that fails, dump both in the selected folder
bool smartAddFiles = true;
if((smartAddFiles && m_pManager->AddFilesToVirtualFolderIntelligently(m_textCtrlVD->GetValue(), files)) ||
m_pManager->AddFilesToVirtualFolder(m_textCtrlVD->GetValue(), files)) {
wxMessageBox(msg, _("Add template class"), wxOK | wxCENTER | wxICON_INFORMATION, this);
} else {
EndModal(wxID_CANCEL);
}
}
EndModal(wxID_OK);
}
void TemplateClassDlg::OnGenerateUI(wxUpdateUIEvent& event)
{
wxUnusedVar(event);
// list of conditions for generation
bool has_name = !m_textCtrlClassName->GetValue().empty();
bool has_header_name = !m_textCtrlHeaderFile->GetValue().empty();
bool has_template = m_comboxCurrentTemplate->GetSelection() != wxNOT_FOUND;
bool has_path = !m_textCtrlFilePath->GetValue().empty();
bool has_vd_path = !m_textCtrlVD->GetValue().empty();
bool enable_cond = has_name && has_header_name && has_template && has_path;
if(clCxxWorkspaceST::Get()->IsOpen()) {
enable_cond = enable_cond && has_vd_path;
}
event.Enable(enable_cond);
}
void TemplateClassDlg::OnQuit(wxCommandEvent& event)
{
wxUnusedVar(event);
GetStringDb()->Save(m_configPath + defaultTmplFile);
EndModal(wxID_CANCEL);
}
void TemplateClassDlg::OnTemplateClassSelected(wxCommandEvent& event)
{
wxUnusedVar(event);
wxString set = m_comboxTemplates->GetValue();
if(GetStringDb()->IsSet(set)) {
m_textCtrlHeader->SetValue(GetStringDb()->GetString(set, swHeader));
m_textCtrlImpl->SetValue(GetStringDb()->GetString(set, swSource));
}
}
void TemplateClassDlg::OnButtonAdd(wxCommandEvent& event)
{
wxUnusedVar(event);
wxString set = m_comboxTemplates->GetValue();
bool isSet = GetStringDb()->IsSet(set);
if(isSet) {
int ret = wxMessageBox(_("Class exists!\nOverwrite?"), _("Add class"), wxYES_NO | wxICON_QUESTION);
if(ret == wxNO)
return;
}
GetStringDb()->SetString(set, swHeader, m_textCtrlHeader->GetValue());
GetStringDb()->SetString(set, swSource, m_textCtrlImpl->GetValue());
if(!isSet) {
m_comboxTemplates->AppendString(set);
}
RefreshTemplateList();
m_modified = true;
}
void TemplateClassDlg::OnButtonAddUI(wxUpdateUIEvent& event)
{
// we need to have name and header content
event.Enable(!m_comboxTemplates->GetValue().IsEmpty() && !m_textCtrlHeader->GetValue().IsEmpty());
}
void TemplateClassDlg::OnButtonChange(wxCommandEvent& event)
{
wxUnusedVar(event);
wxString set = m_comboxTemplates->GetValue();
bool isSet = GetStringDb()->IsSet(set);
if(!isSet) {
int ret =
::wxMessageBox(_("That class doesn't exist!\nTry again?"), _("Change class"), wxYES_NO | wxICON_QUESTION);
if(ret == wxNO)
return;
}
GetStringDb()->SetString(set, swHeader, m_textCtrlHeader->GetValue());
GetStringDb()->SetString(set, swSource, m_textCtrlImpl->GetValue());
if(!isSet)
m_comboxTemplates->AppendString(set);
RefreshTemplateList();
m_modified = true;
}
void TemplateClassDlg::OnButtonChangeUI(wxUpdateUIEvent& event)
{
if(m_comboxTemplates->GetSelection() == wxNOT_FOUND)
event.Enable(false);
else
event.Enable(true);
if(m_textCtrlHeader->IsModified() == false && m_textCtrlImpl->IsModified() == false)
event.Enable(false);
}
void TemplateClassDlg::OnButtonRemove(wxCommandEvent& event)
{
wxUnusedVar(event);
wxString set = m_comboxTemplates->GetValue();
bool isSet = GetStringDb()->IsSet(set);
if(!isSet) {
::wxMessageBox(wxT("Class not found!\nNothing to remove."), wxT("Remove class"));
return;
}
GetStringDb()->DeleteKey(set, swHeader);
GetStringDb()->DeleteKey(set, swSource);
long index = m_comboxTemplates->FindString(set);
m_comboxTemplates->Delete(index);
RefreshTemplateList();
m_modified = true;
}
void TemplateClassDlg::OnButtonRemoveUI(wxUpdateUIEvent& event)
{
if(m_comboxTemplates->GetSelection() == wxNOT_FOUND)
event.Enable(false);
else
event.Enable(true);
}
void TemplateClassDlg::OnButtonClear(wxCommandEvent& e)
{
m_comboxTemplates->SetValue(wxT(""));
m_textCtrlImpl->SetValue(wxT(""));
m_textCtrlHeader->SetValue(wxT(""));
}
void TemplateClassDlg::OnButtonClearUI(wxUpdateUIEvent& e) { e.Enable(true); }
void TemplateClassDlg::OnInsertClassKeyword(wxCommandEvent& event)
{
wxUnusedVar(event);
long from, to;
if(m_notebookFiles->GetSelection() == 0) {
m_textCtrlHeader->GetSelection(&from, &to);
m_textCtrlHeader->Replace(from, to, swPhClass);
m_textCtrlHeader->SetFocus();
} else {
m_textCtrlImpl->GetSelection(&from, &to);
m_textCtrlImpl->Replace(from, to, swPhClass);
m_textCtrlImpl->SetFocus();
}
}
void TemplateClassDlg::OnInsertClassKeywordUI(wxUpdateUIEvent& event) { event.Enable(true); }
swStringDb* TemplateClassDlg::GetStringDb() { return m_plugin->GetStringDb(); }
void TemplateClassDlg::RefreshTemplateList()
{
wxArrayString templates;
GetStringDb()->GetAllSets(templates);
m_comboxCurrentTemplate->Clear();
for(wxUint32 i = 0; i < templates.GetCount(); i++) {
m_comboxCurrentTemplate->AppendString(templates[i]);
}
if(templates.GetCount()) {
m_comboxCurrentTemplate->Select(0);
}
}
bool TemplateClassDlg::SaveBufferToFile(const wxString filename, const wxString buffer, int eolType)
{
wxTextFile file(filename);
wxTextFileType tft = wxTextFileType_Dos;
if(file.Exists()) {
int ret = wxMessageBox(_("File already exists!\n\n Overwrite?"), _("Generate class files"),
wxYES_NO | wxYES_DEFAULT | wxICON_EXCLAMATION);
if(ret == wxID_NO)
return false;
}
switch(m_curEol) {
case 0:
tft = wxTextFileType_Dos;
break;
case 1:
tft = wxTextFileType_Mac;
break;
case 2:
tft = wxTextFileType_Unix;
break;
}
file.Create();
file.AddLine(buffer, tft);
file.Write(tft);
file.Close();
// notify about file creation
clFileSystemEvent create_event(wxEVT_FILE_CREATED);
create_event.SetPath(filename);
EventNotifier::Get()->AddPendingEvent(create_event);
return true;
}
void TemplateClassDlg::OnStcHeaderFileContentChnaged(wxStyledTextEvent& event) { event.Skip(); }
void TemplateClassDlg::OnStcImplFileContentChnaged(wxStyledTextEvent& event) { event.Skip(); }
void TemplateClassDlg::OnVirtualDirUI(wxUpdateUIEvent& event) { event.Enable(clCxxWorkspaceST::Get()->IsOpen()); }
void TemplateClassDlg::SetConfigPath(const wxString& configPath) { this->m_configPath = configPath; }
void TemplateClassDlg::SetProjectPath(const wxString& projectPath)
{
this->m_projectPath = projectPath;
m_textCtrlFilePath->SetValue(projectPath);
}
|