~efargaspro/+junk/codeblocks-16.01-release

« back to all changes in this revision

Viewing changes to src/plugins/abbreviations/abbreviationsconfigpanel.cpp

  • Committer: damienlmoore at gmail
  • Date: 2016-02-02 02:43:22 UTC
  • Revision ID: damienlmoore@gmail.com-20160202024322-yql5qmtbwdyamdwd
Code::BlocksĀ 16.01

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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
 
4
 *
 
5
 * $Revision: 10024 $
 
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 $
 
8
 */
 
9
 
 
10
#include <sdk.h>
 
11
#include "abbreviationsconfigpanel.h"
 
12
 
 
13
#ifndef CB_PRECOMP
 
14
    #include <wx/xrc/xmlres.h>
 
15
    #include <wx/fontutil.h>
 
16
    #include <wx/choicdlg.h>
 
17
 
 
18
    #include <configmanager.h>
 
19
    #include <editorcolourset.h>
 
20
    #include <editormanager.h>
 
21
    #include <logmanager.h>
 
22
#endif
 
23
 
 
24
#include "cbcolourmanager.h"
 
25
 
 
26
BEGIN_EVENT_TABLE(AbbreviationsConfigPanel, cbConfigurationPanel)
 
27
END_EVENT_TABLE()
 
28
 
 
29
AbbreviationsConfigPanel::AbbreviationsConfigPanel(wxWindow* parent, Abbreviations* plugin) :
 
30
    m_AutoCompTextControl(nullptr),
 
31
    m_Plugin(plugin)
 
32
{
 
33
    wxXmlResource::Get()->LoadObject(this, parent, _T("AbbreviationsConfigPanel"), _T("wxPanel"));
 
34
 
 
35
    m_LanguageCmb = XRCCTRL(*this, "cmbAutoCompLanguage", wxComboBox);
 
36
    FillLangugages();
 
37
    m_LanguageCmb->SetSelection(0);
 
38
    m_LastAutoCompLanguage = defaultLanguageStr;
 
39
 
 
40
    InitCompText();
 
41
    m_Keyword = XRCCTRL(*this, "lstAutoCompKeyword", wxListBox);
 
42
    m_Keyword->Clear();
 
43
    m_pCurrentAutoCompMap = m_Plugin->m_AutoCompLanguageMap[defaultLanguageStr];
 
44
 
 
45
    FillKeywords();
 
46
 
 
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);
 
54
}
 
55
 
 
56
AbbreviationsConfigPanel::~AbbreviationsConfigPanel()
 
57
{
 
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);
 
65
 
 
66
    if (m_AutoCompTextControl)
 
67
        delete m_AutoCompTextControl;
 
68
}
 
69
 
 
70
void AbbreviationsConfigPanel::InitCompText()
 
71
{
 
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));
 
80
 
 
81
    wxColor ccolor = Manager::Get()->GetColourManager()->GetColour(wxT("editor_caret"));
 
82
    m_AutoCompTextControl->SetCaretForeground( ccolor );
 
83
 
 
84
    ApplyColours();
 
85
    wxXmlResource::Get()->AttachUnknownControl(_T("txtAutoCompCode"), m_AutoCompTextControl);
 
86
}
 
87
 
 
88
void AbbreviationsConfigPanel::ApplyColours()
 
89
{
 
90
    EditorColourSet* colour_set = Manager::Get()->GetEditorManager()->GetColourSet();
 
91
    if (colour_set)
 
92
    {
 
93
        wxString FontString = Manager::Get()->GetConfigManager(_T("editor"))->Read(_T("/font"), wxEmptyString);
 
94
        wxFont tmpFont(10, wxFONTFAMILY_MODERN, wxFONTSTYLE_NORMAL, wxFONTWEIGHT_NORMAL);
 
95
 
 
96
        if (!FontString.IsEmpty())
 
97
        {
 
98
            wxNativeFontInfo nfi;
 
99
            nfi.FromString(FontString);
 
100
            tmpFont.SetNativeFontInfo(nfi);
 
101
        }
 
102
        if (m_AutoCompTextControl)
 
103
        {
 
104
            m_AutoCompTextControl->StyleSetFont(wxSCI_STYLE_DEFAULT, tmpFont);
 
105
            colour_set->Apply(colour_set->GetHighlightLanguage(_T("C/C++")), m_AutoCompTextControl);
 
106
        }
 
107
    }
 
108
}
 
109
 
 
110
void AbbreviationsConfigPanel::AutoCompUpdate(const wxString& key, const wxString& lang)
 
111
{
 
112
    if (!key.IsEmpty())
 
113
    {
 
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;
 
119
    }
 
120
}
 
121
 
 
122
void AbbreviationsConfigPanel::OnAutoCompAdd(cb_unused wxCommandEvent& event)
 
123
{
 
124
    wxString key = wxGetTextFromUser(_("Please enter the new keyword"), _("Add keyword"));
 
125
    if (!key.IsEmpty())
 
126
    {
 
127
        AutoCompleteMap::iterator it = m_pCurrentAutoCompMap->find(key);
 
128
        if (it != m_pCurrentAutoCompMap->end())
 
129
        {
 
130
            cbMessageBox(_("This keyword already exists!"), _("Error"), wxICON_ERROR, this);
 
131
            return;
 
132
        }
 
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);
 
139
    }
 
140
}
 
141
 
 
142
void AbbreviationsConfigPanel::OnAutoCompDelete(cb_unused wxCommandEvent& event)
 
143
{
 
144
    if (m_Keyword->GetSelection() == -1)
 
145
        return;
 
146
 
 
147
    if (cbMessageBox(_("Are you sure you want to delete this keyword?"), _("Confirmation"),
 
148
                     wxICON_QUESTION | wxYES_NO, this) == wxID_NO)
 
149
        return;
 
150
 
 
151
    int delsel = m_Keyword->GetSelection();
 
152
    AutoCompleteMap::iterator it = m_pCurrentAutoCompMap->find(m_Keyword->GetString(delsel));
 
153
    if (it != m_pCurrentAutoCompMap->end())
 
154
    {
 
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();
 
160
        if (sel >= 0)
 
161
            m_AutoCompTextControl->SetText((*m_pCurrentAutoCompMap)[m_Keyword->GetString(sel)]);
 
162
        else
 
163
            m_AutoCompTextControl->SetText(wxEmptyString);
 
164
        m_Keyword->Delete(delsel);
 
165
        m_pCurrentAutoCompMap->erase(it);
 
166
    }
 
167
}
 
168
 
 
169
void AbbreviationsConfigPanel::OnAutoCompKeyword(cb_unused wxCommandEvent& event)
 
170
{
 
171
    if (   m_LastAutoCompKeyword.IsSameAs(m_Keyword->GetStringSelection())
 
172
        && m_LastAutoCompLanguage.IsSameAs(m_LanguageCmb->GetValue()))
 
173
        return;
 
174
 
 
175
    AutoCompUpdate(m_LastAutoCompKeyword, m_LastAutoCompLanguage);
 
176
 
 
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]);
 
181
    else
 
182
        m_AutoCompTextControl->SetText(wxEmptyString);
 
183
    m_LastAutoCompLanguage = m_LanguageCmb->GetValue();
 
184
}
 
185
 
 
186
void AbbreviationsConfigPanel::OnApply()
 
187
{
 
188
    // save any changes in auto-completion
 
189
    AutoCompUpdate(m_Keyword->GetStringSelection(),m_LanguageCmb->GetValue());
 
190
 
 
191
    for (AutoCompLanguageMap::iterator it = m_Plugin->m_AutoCompLanguageMap.begin(); it != m_Plugin->m_AutoCompLanguageMap.end(); ++it)
 
192
    {
 
193
        Abbreviations::ExchangeTabAndSpaces(*it->second);
 
194
    }
 
195
    //*(m_Plugin->m_AutoCompLanguageMap[defaultLanguageStr]) = m_AutoCompMap;
 
196
}
 
197
 
 
198
void AbbreviationsConfigPanel::OnCancel()
 
199
{}
 
200
 
 
201
wxString AbbreviationsConfigPanel::GetTitle() const
 
202
{ return _("Abbreviations"); }
 
203
 
 
204
wxString AbbreviationsConfigPanel::GetBitmapBaseName() const
 
205
{ return _T("abbrev"); }
 
206
 
 
207
void AbbreviationsConfigPanel::FillLangugages()
 
208
{
 
209
    m_LanguageCmb->Clear();
 
210
 
 
211
    wxSortedArrayString langs;
 
212
    AutoCompLanguageMap::iterator it;
 
213
    for (it = m_Plugin->m_AutoCompLanguageMap.begin(); it != m_Plugin->m_AutoCompLanguageMap.end(); ++it)
 
214
    {
 
215
        langs.Add(it->first);
 
216
    }
 
217
    m_LanguageCmb->Append(langs);
 
218
}
 
219
 
 
220
void AbbreviationsConfigPanel::FillKeywords()
 
221
{
 
222
    m_Keyword->Clear();
 
223
 
 
224
    for (AutoCompleteMap::iterator it = m_pCurrentAutoCompMap->begin(); it != m_pCurrentAutoCompMap->end(); ++it)
 
225
        m_Keyword->Append(it->first);
 
226
 
 
227
    if (!m_pCurrentAutoCompMap->empty())
 
228
    {
 
229
        m_Keyword->SetSelection(0);
 
230
        m_LastAutoCompKeyword = m_Keyword->GetStringSelection();
 
231
        m_AutoCompTextControl->SetText((*m_pCurrentAutoCompMap)[m_Keyword->GetString(m_Keyword->GetSelection())]);
 
232
    }
 
233
    else
 
234
    {
 
235
        m_LastAutoCompKeyword = wxEmptyString;
 
236
        m_AutoCompTextControl->SetText(wxEmptyString);
 
237
    }
 
238
}
 
239
 
 
240
void AbbreviationsConfigPanel::OnLanguageSelect(cb_unused wxCommandEvent& event)
 
241
{
 
242
    LanguageSelected();
 
243
}
 
244
 
 
245
void AbbreviationsConfigPanel::LanguageSelected()
 
246
{
 
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];
 
251
    FillKeywords();
 
252
    m_LastAutoCompLanguage = lang;
 
253
}
 
254
 
 
255
void AbbreviationsConfigPanel::OnLanguageAdd(cb_unused wxCommandEvent& event)
 
256
{
 
257
    LanguageAdd();
 
258
}
 
259
 
 
260
int AbbreviationsConfigPanel::LanguageAdd()
 
261
{
 
262
    EditorColourSet* colSet = Manager::Get()->GetEditorManager()->GetColourSet();
 
263
    if (colSet)
 
264
        colSet = new EditorColourSet(*colSet);
 
265
    else
 
266
        colSet = new EditorColourSet();
 
267
 
 
268
    wxSortedArrayString newLangs;
 
269
    wxArrayString langs = colSet->GetAllHighlightLanguages();
 
270
    for (unsigned int i = 0; i < langs.GetCount(); ++i)
 
271
    {
 
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]);
 
275
    }
 
276
 
 
277
    int sel = wxGetSingleChoiceIndex(_("Select language:"), _("Languages"), newLangs, this);
 
278
    if (sel == -1)
 
279
        return sel;
 
280
 
 
281
    m_Plugin->m_AutoCompLanguageMap[newLangs[sel]] = new AutoCompleteMap();
 
282
    FillLangugages();
 
283
    m_LanguageCmb->SetValue(newLangs[sel]);
 
284
    LanguageSelected();
 
285
    return 0;
 
286
}
 
287
 
 
288
void AbbreviationsConfigPanel::OnLanguageCopy(cb_unused wxCommandEvent& event)
 
289
{
 
290
    wxString langCopy = m_LanguageCmb->GetValue();
 
291
    if (LanguageAdd() == -1)
 
292
        return;
 
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)
 
297
    {
 
298
        (*pMapNew)[it->first] = it->second;
 
299
    }
 
300
    FillKeywords();
 
301
}
 
302
 
 
303
void AbbreviationsConfigPanel::OnLanguageDelete(cb_unused wxCommandEvent& event)
 
304
{
 
305
    wxString lang = m_LanguageCmb->GetValue();
 
306
    if (lang.IsSameAs(defaultLanguageStr) || lang.IsSameAs(_T("Fortran")))
 
307
    {
 
308
        cbMessageBox(_("Abbreviations for ") + lang + _(" language can't be deleted!"), _("Error"),
 
309
                     wxICON_ERROR | wxOK, this);
 
310
        return;
 
311
    }
 
312
 
 
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)
 
315
        return;
 
316
 
 
317
    m_LanguageCmb->SetSelection(0);
 
318
    LanguageSelected();
 
319
 
 
320
    AutoCompleteMap* pAutoCompleteMap = m_Plugin->m_AutoCompLanguageMap[lang];
 
321
    pAutoCompleteMap->clear();
 
322
    delete pAutoCompleteMap;
 
323
    m_Plugin->m_AutoCompLanguageMap.erase(lang);
 
324
    FillLangugages();
 
325
    m_LanguageCmb->SetSelection(0);
 
326
}