forked from eranif/codelite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathacceltabledlg.cpp
259 lines (217 loc) · 7.62 KB
/
acceltabledlg.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
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
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : acceltabledlg.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 "pluginmanager.h"
#include <wx/stdpaths.h>
#include <wx/tokenzr.h>
#include "globals.h"
#include <wx/ffile.h>
#include "newkeyshortcutdlg.h"
#include "acceltabledlg.h"
#include "manager.h"
#include <algorithm>
//-------------------------------------------------------------------------------
//Helper classes for sorting
//-------------------------------------------------------------------------------
struct AccelSorter {
bool operator()(const MenuItemData &rStart, const MenuItemData &rEnd) {
return rEnd.accel.CmpNoCase(rStart.accel) < 0;
}
};
struct ActionSorter {
bool operator()(const MenuItemData &rStart, const MenuItemData &rEnd) {
return rEnd.action.CmpNoCase(rStart.action) < 0;
}
};
struct ParentSorter {
bool operator()(const MenuItemData &rStart, const MenuItemData &rEnd) {
return rEnd.parent.CmpNoCase(rStart.parent) < 0;
}
};
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
//-------------------------------------------------------------------------------
AccelTableDlg::AccelTableDlg( wxWindow* parent )
:
AccelTableBaseDlg( parent )
{
//add two columns to the list ctrl
m_listCtrl1->InsertColumn(0, wxT("ID"));
m_listCtrl1->InsertColumn(1, wxT("Menu"));
m_listCtrl1->InsertColumn(2, wxT("Action"));
m_listCtrl1->InsertColumn(3, wxT("Accelerator"));
MenuItemDataMap accelMap;
PluginManager::Get()->GetKeyboardManager()->GetAccelerators(accelMap);
PopulateTable(&accelMap);
// center the dialog
Centre();
m_textCtrlFilter->SetFocus();
}
void AccelTableDlg::OnItemActivated( wxListEvent& event )
{
m_selectedItem = event.m_itemIndex;
DoItemActivated();
}
void AccelTableDlg::OnItemSelected( wxListEvent& event )
{
m_selectedItem = event.m_itemIndex;
}
void AccelTableDlg::PopulateTable(MenuItemDataMap *accelMap)
{
m_listCtrl1->Freeze();
m_listCtrl1->DeleteAllItems();
// If we got an accelerator map, replace the current one
if(accelMap) {
MenuItemDataMap::const_iterator iter = accelMap->begin();
m_itemsVec.clear();
// Convert the map into vector
for (; iter != accelMap->end(); iter++ ) {
m_itemsVec.push_back( iter->second );
}
// By default sort the items according to the parent
std::sort(m_itemsVec.begin(), m_itemsVec.end(), ParentSorter());
}
wxString filterString = m_textCtrlFilter->GetValue();
filterString.MakeLower().Trim().Trim(false);
for (size_t i=0; i< m_itemsVec.size(); i++) {
MenuItemData item = m_itemsVec.at(i);
wxString action = item.action;
action.MakeLower();
if(filterString.IsEmpty() == false && action.Find(filterString) != wxNOT_FOUND) {
long row = AppendListCtrlRow(m_listCtrl1);
// We got a filter and there is a match, show this item
SetColumnText(m_listCtrl1, row, 0, item.id);
SetColumnText(m_listCtrl1, row, 1, item.parent);
SetColumnText(m_listCtrl1, row, 2, item.action);
SetColumnText(m_listCtrl1, row, 3, item.accel);
} else if(filterString.IsEmpty()) {
long row = AppendListCtrlRow(m_listCtrl1);
// No filter provided, show all
SetColumnText(m_listCtrl1, row, 0, item.id);
SetColumnText(m_listCtrl1, row, 1, item.parent);
SetColumnText(m_listCtrl1, row, 2, item.action);
SetColumnText(m_listCtrl1, row, 3, item.accel);
}
// else hide this entry
}
m_listCtrl1->SetColumnWidth(0, 0);
m_listCtrl1->SetColumnWidth(1, wxLIST_AUTOSIZE);
m_listCtrl1->SetColumnWidth(2, wxLIST_AUTOSIZE);
m_listCtrl1->SetColumnWidth(3, wxLIST_AUTOSIZE);
m_listCtrl1->SetItemState(0, wxLIST_STATE_SELECTED, wxLIST_STATE_SELECTED);
m_listCtrl1->SetItemState(0, wxLIST_STATE_FOCUSED, wxLIST_STATE_FOCUSED);
m_listCtrl1->Thaw();
}
void AccelTableDlg::OnColClicked(wxListEvent &event)
{
wxUnusedVar(event);
}
void AccelTableDlg::OnButtonOk(wxCommandEvent &e)
{
//export the content of table, and apply the changes
wxString content;
for (size_t i=0; i<m_itemsVec.size(); i++) {
MenuItemData mid = m_itemsVec.at(i);
content << mid.id;
content << wxT("|");
content << mid.parent;
content << wxT("|");
content << mid.action;
content << wxT("|");
content << mid.accel;
content << wxT("\n");
}
wxString fileName;
fileName = wxStandardPaths::Get().GetUserDataDir() + wxFileName::GetPathSeparator() + wxT("config/accelerators.conf");
wxFFile file;
if (!file.Open(fileName, wxT("w+b"))) {
return;
}
file.Write(content);
file.Close();
//apply changes
ManagerST::Get()->UpdateMenuAccelerators();
EndModal( wxID_OK );
}
void AccelTableDlg::OnButtonDefaults(wxCommandEvent& e)
{
// re-load the default key bindings settings
MenuItemDataMap accelMap;
ManagerST::Get()->GetDefaultAcceleratorMap(accelMap);
PopulateTable(&accelMap);
}
void AccelTableDlg::OnEditButton(wxCommandEvent& e)
{
if (m_selectedItem != wxNOT_FOUND) {
DoItemActivated();
}
}
void AccelTableDlg::DoItemActivated()
{
//build the selected entry
MenuItemData mid;
mid.id = GetColumnText(m_listCtrl1, m_selectedItem, 0);
mid.parent = GetColumnText(m_listCtrl1, m_selectedItem, 1);
mid.action = GetColumnText(m_listCtrl1, m_selectedItem, 2);
mid.accel = GetColumnText(m_listCtrl1, m_selectedItem, 3);
if (PluginManager::Get()->GetKeyboardManager()->PopupNewKeyboardShortcutDlg(this, mid) == wxID_OK) {
// search the list for similar accelerator
for (size_t i=0; i<m_itemsVec.size(); i++) {
if (Compare(m_itemsVec.at(i).accel, mid.accel) && m_selectedItem != static_cast<int>(i) && mid.accel.IsEmpty() == false) {
wxString action = m_itemsVec.at(i).action;
wxMessageBox(wxString::Format(wxT("'%s' is already assigned to: '%s'"), mid.accel.c_str(), action.c_str()), wxT("CodeLite"), wxOK|wxCENTER|wxICON_WARNING, this);
return;
}
}
// Update the acceleration table
SetColumnText(m_listCtrl1, m_selectedItem, 3, mid.accel);
m_listCtrl1->SetColumnWidth(3, wxLIST_AUTOSIZE);
// Update the vector as well
for (size_t i=0; i<m_itemsVec.size(); i++) {
if (m_itemsVec.at(i).id == mid.id) {
m_itemsVec.at(i).accel = mid.accel;
break;
}
}
}
}
bool AccelTableDlg::Compare(const wxString& accel1, const wxString& accel2)
{
wxArrayString accel1Tokens = wxStringTokenize(accel1, wxT("-"));
wxArrayString accel2Tokens = wxStringTokenize(accel2, wxT("-"));
if (accel1Tokens.GetCount() != accel2Tokens.GetCount()) {
return false;
}
for (size_t i=0; i<accel1Tokens.GetCount(); i++) {
if (accel2Tokens.Index(accel1Tokens.Item(i), false) == wxNOT_FOUND) {
return false;
}
}
return true;
}
void AccelTableDlg::OnText(wxCommandEvent& event)
{
wxUnusedVar(event);
PopulateTable(NULL);
}