forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patheditsnippetsdlg.cpp
183 lines (153 loc) · 5.21 KB
/
editsnippetsdlg.cpp
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
#include "ikeyboard.h"
#include "editsnippetsdlg.h"
#include "snipwiz.h"
#include <wx/msgdlg.h>
#include "AboutHtml.h"
EditSnippetsDlg::EditSnippetsDlg( wxWindow* parent, SnipWiz *plugin, IManager *manager )
: EditSnippetsBaseDlg( parent )
, m_pPlugin(plugin)
, m_manager(manager)
{
Initialize();
m_listBox1->SetFocus();
}
void EditSnippetsDlg::OnItemSelected( wxCommandEvent& event )
{
wxUnusedVar(event);
wxString selection = m_listBox1->GetStringSelection();
m_textCtrlMenuEntry->SetValue(selection);
m_textCtrlSnippet->SetValue(GetStringDb()->GetSnippetString(selection));
MenuItemDataMap accelMap;
m_manager->GetKeyboardManager()->GetAccelerators(accelMap);
MenuItemDataMap::iterator iter = accelMap.find(selection);
if (iter != accelMap.end()) {
MenuItemData mid = iter->second;
m_textCtrlAccelerator->SetValue(mid.accel);
} else {
m_textCtrlAccelerator->SetValue(wxT(""));
}
}
void EditSnippetsDlg::OnAddSnippet( wxCommandEvent& event )
{
if (GetStringDb()->IsSnippetKey(m_textCtrlMenuEntry->GetValue())) {
wxMessageBox(wxT("Menu entry is not unique!"));
return;
}
GetStringDb()->SetSnippetString(m_textCtrlMenuEntry->GetValue(), m_textCtrlSnippet->GetValue());
long index = m_listBox1->Append(m_textCtrlMenuEntry->GetValue());
m_listBox1->SetSelection(index);
m_modified = true;
}
void EditSnippetsDlg::OnAddSnippetUI( wxUpdateUIEvent& event )
{
if (m_textCtrlMenuEntry->GetValue().IsEmpty() || m_textCtrlSnippet->GetValue().IsEmpty())
event.Enable(false);
else
event.Enable(true);
}
void EditSnippetsDlg::OnChangeSnippet( wxCommandEvent& event )
{
wxString curListKey = m_listBox1->GetStringSelection();
int index = m_listBox1->GetSelection();
// check if list key is not equal new menu entry and if it is an used key
if (curListKey.Cmp(m_textCtrlMenuEntry->GetValue()) != 0 && GetStringDb()->IsSnippetKey(m_textCtrlMenuEntry->GetValue())) {
::wxMessageBox(wxT("Menu entry is not unique!"));
return;
}
// if menu entry has changed, delete old entry in list
if (curListKey.Cmp(m_textCtrlMenuEntry->GetValue()) != 0)
GetStringDb()->DeleteSnippetKey(curListKey);
GetStringDb()->SetSnippetString(m_textCtrlMenuEntry->GetValue(), m_textCtrlSnippet->GetValue());
m_listBox1->SetString(index, m_textCtrlMenuEntry->GetValue());
m_modified = true;
}
void EditSnippetsDlg::OnChangeSnippetUI( wxUpdateUIEvent& event )
{
if (m_textCtrlMenuEntry->GetValue().IsEmpty() || m_textCtrlSnippet->GetValue().IsEmpty())
event.Enable(false);
else
event.Enable(true);
}
void EditSnippetsDlg::OnRemoveSnippet( wxCommandEvent& event )
{
wxString key = m_listBox1->GetStringSelection();
int index = m_listBox1->GetSelection();
GetStringDb()->DeleteSnippetKey(key);
m_listBox1->Delete(index);
if (m_listBox1->GetCount())
SelectItem(0);
m_modified = true;
}
void EditSnippetsDlg::OnRemoveSnippetUI( wxUpdateUIEvent& event )
{
if (m_listBox1->GetSelection() != wxNOT_FOUND)
event.Enable(true);
else
event.Enable(false);
}
void EditSnippetsDlg::Initialize()
{
wxTextAttr attribs = m_textCtrlSnippet->GetDefaultStyle();
wxArrayInt tabs = attribs.GetTabs();
int tab = 70;
for (int i = 1; i < 20; i++) {
tabs.Add(tab * i);
}
attribs.SetTabs(tabs);
m_textCtrlSnippet->SetDefaultStyle(attribs);
wxArrayString keys;
GetStringDb()->GetAllSnippetKeys(keys);
m_listBox1->Append(keys);
if (m_listBox1->IsEmpty() == false) {
SelectItem(0);
}
m_htmlWinAbout->SetPage(wxString::FromUTF8(snipwizhtml_txt));
}
void EditSnippetsDlg::SelectItem(long index)
{
m_listBox1->SetSelection(index);
wxString snippetStr = m_listBox1->GetString((unsigned int)index);
m_textCtrlMenuEntry->SetValue(snippetStr);
m_textCtrlSnippet->SetValue(GetStringDb()->GetSnippetString(snippetStr));
MenuItemDataMap accelMap;
m_manager->GetKeyboardManager()->GetAccelerators(accelMap);
MenuItemDataMap::iterator iter = accelMap.find(snippetStr);
if (iter != accelMap.end()) {
MenuItemData mid = iter->second;
m_textCtrlAccelerator->SetValue(mid.accel);
} else {
m_textCtrlAccelerator->SetValue(wxT(""));
}
}
swStringDb* EditSnippetsDlg::GetStringDb()
{
return m_pPlugin->GetStringDb();
}
void EditSnippetsDlg::OnButtonKeyShortcut(wxCommandEvent& e)
{
wxArrayString keys;
GetStringDb()->GetAllSnippetKeys(keys);
keys.Sort();
int index = keys.Index(m_textCtrlMenuEntry->GetValue());
if (index != wxNOT_FOUND) {
int id = 20050 + index;
wxString strId;
strId << id;
MenuItemData mid;
mid.id = strId;
mid.action = m_textCtrlMenuEntry->GetValue();
mid.parent = wxT("2");
MenuItemDataMap accelMap;
m_manager->GetKeyboardManager()->GetAccelerators(accelMap);
if (m_manager->GetKeyboardManager()->PopupNewKeyboardShortcutDlg(this, mid) == wxID_OK) {
if (m_manager->GetKeyboardManager()->IsDuplicate(accelMap, mid.accel) && mid.accel.IsEmpty() == false) {
wxMessageBox(wxT("Accelerator already exist"), wxT("CodeLite"), wxOK|wxCENTRE, this);
return;
}
if (m_manager->GetKeyboardManager()->AddAccelerator(accelMap, mid)) {
m_manager->GetKeyboardManager()->Update(accelMap);
m_textCtrlAccelerator->SetValue(mid.accel);
}
}
}
}