2
* This file is part of the Code::Blocks IDE and licensed under the GNU General Public License, version 3
3
* http://www.gnu.org/licenses/gpl-3.0.html
6
* $Id: abbreviationsconfigpanel.cpp 10024 2014-11-06 09:23:26Z jenslody $
7
* $HeadURL: http://svn.code.sf.net/p/codeblocks/code/branches/release-16.xx/src/plugins/abbreviations/abbreviationsconfigpanel.cpp $
11
#include "abbreviationsconfigpanel.h"
14
#include <wx/xrc/xmlres.h>
15
#include <wx/fontutil.h>
16
#include <wx/choicdlg.h>
18
#include <configmanager.h>
19
#include <editorcolourset.h>
20
#include <editormanager.h>
21
#include <logmanager.h>
24
#include "cbcolourmanager.h"
26
BEGIN_EVENT_TABLE(AbbreviationsConfigPanel, cbConfigurationPanel)
29
AbbreviationsConfigPanel::AbbreviationsConfigPanel(wxWindow* parent, Abbreviations* plugin) :
30
m_AutoCompTextControl(nullptr),
33
wxXmlResource::Get()->LoadObject(this, parent, _T("AbbreviationsConfigPanel"), _T("wxPanel"));
35
m_LanguageCmb = XRCCTRL(*this, "cmbAutoCompLanguage", wxComboBox);
37
m_LanguageCmb->SetSelection(0);
38
m_LastAutoCompLanguage = defaultLanguageStr;
41
m_Keyword = XRCCTRL(*this, "lstAutoCompKeyword", wxListBox);
43
m_pCurrentAutoCompMap = m_Plugin->m_AutoCompLanguageMap[defaultLanguageStr];
47
Connect(XRCID("lstAutoCompKeyword"), wxEVT_COMMAND_LISTBOX_SELECTED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnAutoCompKeyword);
48
Connect(XRCID("btnAutoCompAdd"), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnAutoCompAdd);
49
Connect(XRCID("btnAutoCompDelete"), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnAutoCompDelete);
50
Connect(XRCID("cmbAutoCompLanguage"), wxEVT_COMMAND_COMBOBOX_SELECTED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnLanguageSelect);
51
Connect(XRCID("btnAutoCompAddLanguage"), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnLanguageAdd);
52
Connect(XRCID("btnAutoCompCopyLanguage"), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnLanguageCopy);
53
Connect(XRCID("btnAutoCompDelLanguage"), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnLanguageDelete);
56
AbbreviationsConfigPanel::~AbbreviationsConfigPanel()
58
Disconnect(XRCID("lstAutoCompKeyword"), wxEVT_COMMAND_LISTBOX_SELECTED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnAutoCompKeyword);
59
Disconnect(XRCID("btnAutoCompAdd"), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnAutoCompAdd);
60
Disconnect(XRCID("btnAutoCompDelete"), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnAutoCompDelete);
61
Disconnect(XRCID("cmbAutoCompLanguage"), wxEVT_COMMAND_COMBOBOX_SELECTED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnLanguageSelect);
62
Disconnect(XRCID("btnAutoCompAddLanguage"), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnLanguageAdd);
63
Disconnect(XRCID("btnAutoCompCopyLanguage"), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnLanguageCopy);
64
Disconnect(XRCID("btnAutoCompDelLanguage"), wxEVT_COMMAND_BUTTON_CLICKED, (wxObjectEventFunction)&AbbreviationsConfigPanel::OnLanguageDelete);
66
if (m_AutoCompTextControl)
67
delete m_AutoCompTextControl;
70
void AbbreviationsConfigPanel::InitCompText()
72
if (m_AutoCompTextControl)
73
delete m_AutoCompTextControl;
74
m_AutoCompTextControl = new cbStyledTextCtrl(this, wxID_ANY);
75
m_AutoCompTextControl->SetTabWidth(4);
76
m_AutoCompTextControl->SetMarginType(0, wxSCI_MARGIN_NUMBER);
77
m_AutoCompTextControl->SetMarginWidth(0, 32);
78
m_AutoCompTextControl->SetViewWhiteSpace(1);
79
m_AutoCompTextControl->SetMinSize(wxSize(50, 50));
81
wxColor ccolor = Manager::Get()->GetColourManager()->GetColour(wxT("editor_caret"));
82
m_AutoCompTextControl->SetCaretForeground( ccolor );
85
wxXmlResource::Get()->AttachUnknownControl(_T("txtAutoCompCode"), m_AutoCompTextControl);
88
void AbbreviationsConfigPanel::ApplyColours()
90
EditorColourSet* colour_set = Manager::Get()->GetEditorManager()->GetColourSet();
93
wxString FontString = Manager::Get()->GetConfigManager(_T("editor"))->Read(_T("/font"), wxEmptyString);
94
wxFont tmpFont(10, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
96
if (!FontString.IsEmpty())
99
nfi.FromString(FontString);
100
tmpFont.SetNativeFontInfo(nfi);
102
if (m_AutoCompTextControl)
104
m_AutoCompTextControl->StyleSetFont(wxSCI_STYLE_DEFAULT, tmpFont);
105
colour_set->Apply(colour_set->GetHighlightLanguage(_T("C/C++")), m_AutoCompTextControl);
110
void AbbreviationsConfigPanel::AutoCompUpdate(const wxString& key, const wxString& lang)
114
AutoCompleteMap* compMap = m_Plugin->m_AutoCompLanguageMap[lang];
115
wxString code = m_AutoCompTextControl->GetText();
116
code.Replace(wxT("\r\n"), wxT("\n"));
117
code.Replace(wxT("\r"), wxT("\n"));
118
(*compMap)[key] = code;
122
void AbbreviationsConfigPanel::OnAutoCompAdd(cb_unused wxCommandEvent& event)
124
wxString key = wxGetTextFromUser(_("Please enter the new keyword"), _("Add keyword"));
127
AutoCompleteMap::iterator it = m_pCurrentAutoCompMap->find(key);
128
if (it != m_pCurrentAutoCompMap->end())
130
cbMessageBox(_("This keyword already exists!"), _("Error"), wxICON_ERROR, this);
133
(*m_pCurrentAutoCompMap)[key] = wxEmptyString;
134
int sel = m_Keyword->Append(key);
135
m_Keyword->SetSelection(sel);
136
m_LastAutoCompKeyword = m_Keyword->GetStringSelection();
137
AutoCompUpdate(m_LastAutoCompKeyword,m_LanguageCmb->GetValue());
138
m_AutoCompTextControl->SetText(wxEmptyString);
142
void AbbreviationsConfigPanel::OnAutoCompDelete(cb_unused wxCommandEvent& event)
144
if (m_Keyword->GetSelection() == -1)
147
if (cbMessageBox(_("Are you sure you want to delete this keyword?"), _("Confirmation"),
148
wxICON_QUESTION | wxYES_NO, this) == wxID_NO)
151
int delsel = m_Keyword->GetSelection();
152
AutoCompleteMap::iterator it = m_pCurrentAutoCompMap->find(m_Keyword->GetString(delsel));
153
if (it != m_pCurrentAutoCompMap->end())
155
int sel = delsel + 1;
156
if (sel >= (int)(m_Keyword->GetCount()))
157
sel = m_Keyword->GetCount() - 2;
158
m_Keyword->SetSelection(sel);
159
m_LastAutoCompKeyword = m_Keyword->GetStringSelection();
161
m_AutoCompTextControl->SetText((*m_pCurrentAutoCompMap)[m_Keyword->GetString(sel)]);
163
m_AutoCompTextControl->SetText(wxEmptyString);
164
m_Keyword->Delete(delsel);
165
m_pCurrentAutoCompMap->erase(it);
169
void AbbreviationsConfigPanel::OnAutoCompKeyword(cb_unused wxCommandEvent& event)
171
if ( m_LastAutoCompKeyword.IsSameAs(m_Keyword->GetStringSelection())
172
&& m_LastAutoCompLanguage.IsSameAs(m_LanguageCmb->GetValue()))
175
AutoCompUpdate(m_LastAutoCompKeyword, m_LastAutoCompLanguage);
177
// list new keyword's code
178
m_LastAutoCompKeyword = m_Keyword->GetStringSelection();
179
if (!m_LastAutoCompKeyword.IsEmpty())
180
m_AutoCompTextControl->SetText((*m_pCurrentAutoCompMap)[m_LastAutoCompKeyword]);
182
m_AutoCompTextControl->SetText(wxEmptyString);
183
m_LastAutoCompLanguage = m_LanguageCmb->GetValue();
186
void AbbreviationsConfigPanel::OnApply()
188
// save any changes in auto-completion
189
AutoCompUpdate(m_Keyword->GetStringSelection(),m_LanguageCmb->GetValue());
191
for (AutoCompLanguageMap::iterator it = m_Plugin->m_AutoCompLanguageMap.begin(); it != m_Plugin->m_AutoCompLanguageMap.end(); ++it)
193
Abbreviations::ExchangeTabAndSpaces(*it->second);
195
//*(m_Plugin->m_AutoCompLanguageMap[defaultLanguageStr]) = m_AutoCompMap;
198
void AbbreviationsConfigPanel::OnCancel()
201
wxString AbbreviationsConfigPanel::GetTitle() const
202
{ return _("Abbreviations"); }
204
wxString AbbreviationsConfigPanel::GetBitmapBaseName() const
205
{ return _T("abbrev"); }
207
void AbbreviationsConfigPanel::FillLangugages()
209
m_LanguageCmb->Clear();
211
wxSortedArrayString langs;
212
AutoCompLanguageMap::iterator it;
213
for (it = m_Plugin->m_AutoCompLanguageMap.begin(); it != m_Plugin->m_AutoCompLanguageMap.end(); ++it)
215
langs.Add(it->first);
217
m_LanguageCmb->Append(langs);
220
void AbbreviationsConfigPanel::FillKeywords()
224
for (AutoCompleteMap::iterator it = m_pCurrentAutoCompMap->begin(); it != m_pCurrentAutoCompMap->end(); ++it)
225
m_Keyword->Append(it->first);
227
if (!m_pCurrentAutoCompMap->empty())
229
m_Keyword->SetSelection(0);
230
m_LastAutoCompKeyword = m_Keyword->GetStringSelection();
231
m_AutoCompTextControl->SetText((*m_pCurrentAutoCompMap)[m_Keyword->GetString(m_Keyword->GetSelection())]);
235
m_LastAutoCompKeyword = wxEmptyString;
236
m_AutoCompTextControl->SetText(wxEmptyString);
240
void AbbreviationsConfigPanel::OnLanguageSelect(cb_unused wxCommandEvent& event)
245
void AbbreviationsConfigPanel::LanguageSelected()
247
AutoCompUpdate(m_LastAutoCompKeyword, m_LastAutoCompLanguage);
248
Abbreviations::ExchangeTabAndSpaces(*m_pCurrentAutoCompMap);
249
wxString lang = m_LanguageCmb->GetValue();
250
m_pCurrentAutoCompMap = m_Plugin->m_AutoCompLanguageMap[lang];
252
m_LastAutoCompLanguage = lang;
255
void AbbreviationsConfigPanel::OnLanguageAdd(cb_unused wxCommandEvent& event)
260
int AbbreviationsConfigPanel::LanguageAdd()
262
EditorColourSet* colSet = Manager::Get()->GetEditorManager()->GetColourSet();
264
colSet = new EditorColourSet(*colSet);
266
colSet = new EditorColourSet();
268
wxSortedArrayString newLangs;
269
wxArrayString langs = colSet->GetAllHighlightLanguages();
270
for (unsigned int i = 0; i < langs.GetCount(); ++i)
272
if (m_Plugin->m_AutoCompLanguageMap.find(langs[i]) == m_Plugin->m_AutoCompLanguageMap.end() &&
273
!langs[i].IsSameAs(_T("Fortran77")))
274
newLangs.Add(langs[i]);
277
int sel = wxGetSingleChoiceIndex(_("Select language:"), _("Languages"), newLangs, this);
281
m_Plugin->m_AutoCompLanguageMap[newLangs[sel]] = new AutoCompleteMap();
283
m_LanguageCmb->SetValue(newLangs[sel]);
288
void AbbreviationsConfigPanel::OnLanguageCopy(cb_unused wxCommandEvent& event)
290
wxString langCopy = m_LanguageCmb->GetValue();
291
if (LanguageAdd() == -1)
293
wxString langNew = m_LanguageCmb->GetValue();
294
AutoCompleteMap* pMapCopy = m_Plugin->m_AutoCompLanguageMap[langCopy];
295
AutoCompleteMap* pMapNew = m_Plugin->m_AutoCompLanguageMap[langNew];
296
for (AutoCompleteMap::iterator it = pMapCopy->begin(); it != pMapCopy->end(); ++it)
298
(*pMapNew)[it->first] = it->second;
303
void AbbreviationsConfigPanel::OnLanguageDelete(cb_unused wxCommandEvent& event)
305
wxString lang = m_LanguageCmb->GetValue();
306
if (lang.IsSameAs(defaultLanguageStr) || lang.IsSameAs(_T("Fortran")))
308
cbMessageBox(_("Abbreviations for ") + lang + _(" language can't be deleted!"), _("Error"),
309
wxICON_ERROR | wxOK, this);
313
if (cbMessageBox(_("Are you sure you want to delete abbreviations for ") + lang + _(" language?"), _("Confirmation"),
314
wxICON_QUESTION | wxYES_NO | wxNO_DEFAULT, this) == wxID_NO)
317
m_LanguageCmb->SetSelection(0);
320
AutoCompleteMap* pAutoCompleteMap = m_Plugin->m_AutoCompLanguageMap[lang];
321
pAutoCompleteMap->clear();
322
delete pAutoCompleteMap;
323
m_Plugin->m_AutoCompLanguageMap.erase(lang);
325
m_LanguageCmb->SetSelection(0);