-
Notifications
You must be signed in to change notification settings - Fork 91
/
SymbolList.cpp
executable file
·323 lines (266 loc) · 8.69 KB
/
SymbolList.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
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
/*******************************************************************************
*
* Copyright (C) 2009, Alexander Stigsen, e-texteditor.com
*
* This software is licensed under the Open Company License as described
* in the file license.txt, which you should have received as part of this
* distribution. The terms are also available at http://opencompany.org/license.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
******************************************************************************/
#include "SymbolList.h"
#include "IFrameSymbolService.h"
#include "eSettings.h"
#include "EditorCtrl.h"
#include "Strings.h"
#include <algorithm>
#ifndef WX_PRECOMP
#include <wx/sizer.h>
#include <wx/dc.h>
#endif
using namespace std;
// Ctrl id's
enum {
CTRL_SEARCH,
CTRL_ALIST
};
BEGIN_EVENT_TABLE(SymbolList, wxPanel)
EVT_IDLE(SymbolList::OnIdle)
EVT_TEXT(CTRL_SEARCH, SymbolList::OnSearch)
EVT_TEXT_ENTER(CTRL_SEARCH, SymbolList::OnAction)
EVT_LISTBOX_DCLICK(CTRL_ALIST, SymbolList::OnAction)
END_EVENT_TABLE()
SymbolList::SymbolList(IFrameSymbolService& services, bool keepOpen):
wxPanel(dynamic_cast<wxWindow*>(&services), wxID_ANY),
m_parentFrame(services),
m_editorSymbols(NULL),
m_keepOpen(keepOpen)
{
// Create ctrls
m_searchCtrl = new wxTextCtrl(this, CTRL_SEARCH, wxEmptyString, wxDefaultPosition, wxDefaultSize, wxTE_PROCESS_ENTER);
m_listBox = new ActionList(this, CTRL_ALIST, m_symbolStrings);
// Add custom event handler (for up/down key events in the search box)
m_searchCtrl->Connect(wxEVT_CHAR, wxKeyEventHandler(SymbolList::OnSearchChar), NULL, this);
// Create Layout
wxBoxSizer *mainSizer = new wxBoxSizer(wxVERTICAL);
mainSizer->Add(m_searchCtrl, 0, wxEXPAND);
mainSizer->Add(m_listBox, 1, wxEXPAND);
SetSizer(mainSizer);
}
bool SymbolList::Destroy() {
// delayed destruction: the panel will be deleted during the next idle
// loop iteration
if ( !wxPendingDelete.Member(this) )
wxPendingDelete.Append(this);
return true;
}
void SymbolList::LoadFilters() {
wxLogDebug(wxT("Load Filters"));
wxString filters;
eGetSettings().GetSettingString(wxT("symbolListFilters"), filters);
m_filters = filters;
const wxArrayString filtersList = wxSplit(m_filters, wxT('\n'), NULL);
m_regexFilters.clear();
for(unsigned int c = 0; c < filtersList.GetCount(); c++) {
//m_regexes.push_back(Compile(filters[c]));
if(filtersList[c].Trim().Len() == 0) continue;
m_regexFilters.push_back(filtersList[c]);
}
}
void SymbolList::OnIdle(wxIdleEvent& WXUNUSED(event)) {
EditorChangeType newStatus;
IEditorSymbols* editorSymbols = dynamic_cast<IEditorSymbols*>(m_parentFrame.GetEditorAndChangeType(this->m_editorChangeState, newStatus));
wxString filters;
eGetSettings().GetSettingString(wxT("symbolListFilters"), filters);
// If we lost the editor, or there was no change, then do nothing.
if(!editorSymbols || (newStatus == ECT_NO_CHANGE && filters == m_filters)) return;
if(filters != m_filters) {
LoadFilters();
}
bool newEditor = (newStatus == ECT_NEW_EDITOR);
this->m_editorSymbols = editorSymbols;
// Reload symbols
m_symbols.clear();
m_symbolStrings.Empty();
const int res = m_editorSymbols->GetSymbols(m_symbols);
std::vector<SymbolRef> symbolsTemp;
if (res == 1) {
// reload symbol strings
wxLogDebug(wxT("Doing Search"));
for (vector<SymbolRef>::const_iterator p = m_symbols.begin(); p != m_symbols.end(); ++p) {
const SymbolRef& sr = *p;
wxString symbol = m_editorSymbols->GetSymbolString(sr);
std::vector<char> symbolVector;
for(unsigned int c = 0; c < symbol.Len(); c++) {
symbolVector.push_back(symbol[c]);
}
bool good = true;
for(unsigned int c = 0; c < m_regexFilters.size(); c++) {
search_result sr = EditorCtrl::RawRegexSearch(m_regexFilters[c].mb_str(wxConvUTF8), symbolVector, 0, NULL);
if(sr.error_code > 0) {
good = false;
break;
}
}
if(!good) continue;
symbolsTemp.push_back(sr);
m_symbolStrings.Add(symbol);
}
m_symbols = symbolsTemp;
m_listBox->SetAllItems();
}
else if (res == 2) { // DEBUG: double implementation to view path in crash dump (remove when bug is solved)
// reload symbol strings
for (vector<SymbolRef>::const_iterator p = m_symbols.begin(); p != m_symbols.end(); ++p) {
const SymbolRef& sr = *p;
m_symbolStrings.Add(m_editorSymbols->GetSymbolString(sr));
}
m_listBox->SetAllItems();
}
else {
m_listBox->SetAllItems(); // clear list
return;
}
// Save the change state for next time around, reseting the change token if we have a new editor.
this->m_editorChangeState = editorSymbols->GetChangeState();
if (newEditor) this->m_editorChangeState.changeToken = 0;
// Keep scrollpos so we can stay at the same pos
const unsigned int scrollPos = m_listBox->GetScrollPos(wxVERTICAL);
// Set current symbol
if (!m_symbols.empty()) {
if (!newEditor) m_listBox->SetScrollPos(wxVERTICAL, scrollPos);
}
}
void SymbolList::OnSearch(wxCommandEvent& event) {
m_listBox->Find(event.GetString());
}
void SymbolList::OnSearchChar(wxKeyEvent& event) {
switch ( event.GetKeyCode() )
{
case WXK_UP:
m_listBox->SelectPrev();
return;
case WXK_DOWN:
m_listBox->SelectNext();
return;
case WXK_ESCAPE:
if (event.ShiftDown()) m_parentFrame.CloseSymbolList();
if (m_editorSymbols) m_parentFrame.FocusEditor(); //m_editorCtrl->SetFocus();
return;
}
// no, we didn't process it
event.Skip();
}
void SymbolList::OnAction(wxCommandEvent& WXUNUSED(event)) {
if (!m_editorSymbols) return;
if(m_listBox->GetSelectedCount() != 1) return;
const int hit = m_listBox->GetSelectedAction();
if (hit != wxNOT_FOUND && hit < (int)m_symbols.size()) {
// Go to symbol
m_editorSymbols->GotoSymbolPos(m_symbols[hit].start);
// Close symbollist if user holds down shift or "m_keepOpen" is false.
if (!m_keepOpen || wxGetKeyState(WXK_SHIFT)) {
m_parentFrame.CloseSymbolList();
}
else if (!m_searchCtrl->IsEmpty()) {
m_searchCtrl->Clear();
}
}
}
// --- ActionList --------------------------------------------------------
BEGIN_EVENT_TABLE(SymbolList::ActionList, SearchListBox)
EVT_LEFT_DOWN(SymbolList::ActionList::OnLeftDown)
END_EVENT_TABLE()
SymbolList::ActionList::ActionList(wxWindow* parent, wxWindowID id, const wxArrayString& actions):
SearchListBox(parent, id),
m_actions(actions)
{
SetAllItems();
}
void SymbolList::ActionList::SetAllItems() {
m_items.clear();
Freeze();
if (m_searchText.empty()) {
m_items.resize(m_actions.size());
for (unsigned int i = 0; i < m_actions.size(); ++i) {
m_items[i].id = i;
m_items[i].action = &m_actions[i];
}
SetItemCount(m_items.size());
}
else {
Find(m_searchText, false);
}
SetSelection(-1);
RefreshAll();
Thaw();
}
void SymbolList::ActionList::OnDrawItem(wxDC& dc, const wxRect& rect, size_t n) const {
const bool isCurrent = IsCurrent(n);
if (isCurrent) dc.SetTextForeground(m_hlTextColor);
else dc.SetTextForeground(m_textColor);
const aItem& ai = m_items[n];
const vector<unsigned int>& hl = ai.hlChars;
const wxString& name = *ai.action;
// Draw action name
DrawItemText(dc, rect, name, hl, isCurrent);
}
void SymbolList::ActionList::Find(const wxString& searchtext, bool refresh) {
m_searchText = searchtext; // cache for later updates
if (searchtext.empty()) {
SetAllItems();
return;
}
// Convert to upper case for case insensitive search
const wxString text = searchtext.Upper();
m_items.clear();
vector<unsigned int> hlChars;
for (unsigned int i = 0; i < m_actions.GetCount(); ++i) {
const wxString& name = m_actions[i];
unsigned int charpos = 0;
wxChar c = text[charpos];
hlChars.clear();
for (unsigned int textpos = 0; textpos < name.size(); ++textpos) {
if ((wxChar)wxToupper(name[textpos]) == c) {
hlChars.push_back(textpos);
++charpos;
if (charpos == text.size()) {
// All chars found.
m_items.push_back(aItem(i, &m_actions[i], hlChars));
break;
}
else c = text[charpos];
}
}
}
sort(m_items.begin(), m_items.end());
Freeze();
SetItemCount(m_items.size());
if (refresh) {
if (m_items.empty()) SetSelection(-1); // deselect
else SetSelection(0);
RefreshAll();
}
Thaw();
}
int SymbolList::ActionList::GetSelectedAction() const {
const int sel = GetSelection();
if (sel == -1) return wxNOT_FOUND;
else return m_items[sel].id;
}
void SymbolList::ActionList::OnLeftDown(wxMouseEvent& event)
{
const int item = HitTest(event.GetPosition());
if ( item != wxNOT_FOUND )
{
SetSelection(item);
// We want to process single clicks like dclick/enter
wxCommandEvent e(wxEVT_COMMAND_LISTBOX_DOUBLECLICKED, GetId());
e.SetEventObject(this);
e.SetInt(item);
(void)GetEventHandler()->ProcessEvent(e);
}
else event.Skip();
}