-
Notifications
You must be signed in to change notification settings - Fork 25
/
cbvike.cpp
514 lines (441 loc) · 13.1 KB
/
cbvike.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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
// For compilers that support precompilation, includes "wx/wx.h".
#include "sdk.h"
#include "wx/wxprec.h"
#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif
#if not defined(CB_PRECOMP)
#include "sdk_common.h"
#include "sdk_events.h"
#include "logmanager.h"
#endif
// includes
#include <wx/event.h>
#include <codeblocks/editormanager.h>
#include "cbvike.h"
#include "vifunc.h"
#include "debugging.h"
IMPLEMENT_CLASS(cbVike, wxObject)
IMPLEMENT_CLASS(VikeEvtBinder, wxEvtHandler)
/******************* VikeEvtHandler start ***********************/
// event table for cbVike
BEGIN_EVENT_TABLE(VikeEvtBinder, wxEvtHandler)
// wxEVT_KEY_DOWN received some special key like ESC, BACKSPCE etc.
EVT_KEY_DOWN(VikeEvtBinder::OnKeyDown)
// wxEVT_CHAR recevied any ASCII characters
EVT_CHAR(VikeEvtBinder::OnChar)
EVT_SET_FOCUS(VikeEvtBinder::OnFocus)
END_EVENT_TABLE()
VikeEvtBinder::VikeEvtBinder(cbVike *vike, cbStyledTextCtrl* controller, cbEditor *editor)
: m_pVike(vike), m_pTarget(controller), m_pEditor(editor)
{
VikeStatusBar *bar = nullptr;
wxArrayPtrVoid *found = vike->FindHandlerFor(editor);
if(found->Count() == 0){
LOGIT(_T("new status bar"));
bar = new VikeStatusBar(editor, wxSB_NORMAL);
}else{
LOGIT(_T("old status bar"));
bar = ((VikeEvtBinder *)found->Item(0))->GetVikeWin()->GetStatusBar();
}
m_pVikeWin = new VikeWin(controller, editor, bar);
m_pTarget->PushEventHandler(this);
delete found;
}
VikeEvtBinder::~VikeEvtBinder()
{
LOGIT(_T("Deleting VikeEvtBinder"));
if ( m_pTarget ){
bool ret = m_pTarget->RemoveEventHandler(this);
LOGIT(_T("remove handler ret %d"), ret);
}
wxArrayPtrVoid *found = m_pVike->FindHandlerFor(m_pEditor);
if(found->Count() == 0 || (found->Count() == 1 && found->Item(0) == this)){
LOGIT(_T("delete status bar"));
delete m_pVikeWin->GetStatusBar();
}
delete found;
if ( m_pVikeWin ){
delete m_pVikeWin;
}
}
void VikeEvtBinder::ReAttach(cbStyledTextCtrl *controller, cbEditor *editor)
{
assert(m_pTarget != NULL);
if(m_pTarget == controller){
m_pEditor = editor;
m_pVikeWin->LayoutStatusBar(controller, editor);
}
}
void VikeEvtBinder::OnChar(wxKeyEvent &p)
{
m_pVike->OnChar(m_pVikeWin, p);
}
void VikeEvtBinder::OnKeyDown(wxKeyEvent &p)
{
m_pVike->OnKeyDown(m_pVikeWin, p);
}
void VikeEvtBinder::OnFocus(wxFocusEvent &event)
{
LOGIT(_("on focus"));
cbEditor *edBase = Manager::Get()->GetEditorManager()->GetBuiltinActiveEditor();
wxScintilla *editor = NULL;
if(edBase){
editor = (wxScintilla *)edBase->GetControl();
LOGIT(_T("window is %p, edbase is %p, editor is %p"),event.GetWindow(), edBase, editor);
m_pVikeWin->UpdateStatusBar();
m_pVikeWin->UpdateCaret((wxScintilla *)editor);
}
event.Skip();
}
/******************* VikeEvtHandler end ***********************/
/******************* cbVike start *****************************/
void cbVike::ReAttach(cbStyledTextCtrl *p, cbEditor *editor)
{
LOGIT(_T("Re-attach to %p"), p);
VikeEvtBinder *handler = FindHandlerFor(p);
if(handler){
handler->ReAttach(p, editor);
}
}
void cbVike::Attach(cbStyledTextCtrl *p, cbEditor *editor)
{
if(!p){
return;
}
// already attached !!!
if(IsAttachedTo(p)){
ReAttach(p, editor);
return;
}
// do not attach ourselves to temporary windows !!
if (p->GetExtraStyle() & wxWS_EX_TRANSIENT)
return;
// create a new event handler for this window
wxEvtHandler *h = new VikeEvtBinder(this, p, editor);
m_arrHandlers.Add((void*)h);
LOGIT(_T("Add wxEvtHandler %p for controller %p in editor %p"), h, p, editor);
}
int cbVike::FindHandlerIdxFor(cbStyledTextCtrl *p) const
{
for(int i=0; i<(int)m_arrHandlers.GetCount(); i++){
if (((VikeEvtBinder *)m_arrHandlers.Item(i))->IsAttachedTo(p)){
return i;
}
}
return wxNOT_FOUND;
}
wxArrayPtrVoid *cbVike::FindHandlerFor(cbEditor *p) const
{
wxArrayPtrVoid *result = new wxArrayPtrVoid();
for(int i = 0; i < m_arrHandlers.GetCount(); i++){
if(((VikeEvtBinder*)m_arrHandlers[i])->IsAttachedTo(p)){
result->Add(m_arrHandlers[i]);
}
}
return result;
}
void cbVike::Detach(cbEditor *editor)
{
if(!editor){
return;
}
int idx, i;
/* remove all the found handlers */
/* NOTE: I don't know how to remove mutiple items in one iteration */
for(int j = 0; j < 2; j++){
for(i = 0; i < m_arrHandlers.GetCount(); i++){
if(((VikeEvtBinder *)m_arrHandlers[i])->IsAttachedTo(editor)){
break;
}
}
delete (VikeEvtBinder *)m_arrHandlers[i];
m_arrHandlers.RemoveAt(i);
}
LOGIT(wxT("%d handlers left"), m_arrHandlers.GetCount());
}
VikeEvtBinder *cbVike::FindBrother(cbStyledTextCtrl *controller)
{
int idx = FindHandlerIdxFor(controller);
VikeEvtBinder *current, *brother = nullptr;
if(idx != wxNOT_FOUND){
cbEditor *editor = ((VikeEvtBinder *)m_arrHandlers[idx])->GetEditor();
for(int i = 0; i < m_arrHandlers.GetCount(); i++){
current = (VikeEvtBinder *)m_arrHandlers[i];
/* find brother for controller*/
if(current->GetEditor() == editor && current->GetController() != controller){
brother = (VikeEvtBinder *)m_arrHandlers[i];
break;
}
}
}
return brother;
}
void cbVike::DetachBrother(cbStyledTextCtrl *controller)
{
VikeEvtBinder *toRemove = FindBrother(controller);
if(toRemove){
m_arrHandlers.Remove((void *)toRemove);
delete toRemove;
}
LOGIT(wxT("%d handlers left"), m_arrHandlers.GetCount());
}
void cbVike::Detach(cbStyledTextCtrl *p, bool deleteEvtHandler)
{
if (!p || !IsAttachedTo(p))
return; // this is not attached...
LOGIT(wxT("wxKeyBinder::Detach - detaching from [%s] %p"), p->GetName().c_str(),p);
// remove the event handler
int idx = FindHandlerIdxFor(p);
LOGIT(wxT("idx is %d"), idx);
VikeEvtBinder *toremove = (VikeEvtBinder*)m_arrHandlers.Item(idx);
m_arrHandlers.RemoveAt(idx, 1);
// the wxBinderEvtHandler will remove itself from p's event handlers
if (deleteEvtHandler) delete toremove;
LOGIT(wxT("%d handlers left"), m_arrHandlers.GetCount());
}
void cbVike::DetachAll()
{
wxWindow* pwin;
LOGIT(_T("wxVike::DetachAll - detaching from all my [%d] targets"), GetAttachedWndCount());
//- delete all handlers (they will automatically remove themselves from
//- event handler chains)
//- for (int i=0; i < (int)m_arrHandlers.GetCount(); i++)
//- delete (wxBinderEvtHandler*)m_arrHandlers.Item(i);
for (int i=0; i < (int)m_arrHandlers.GetCount(); i++)
{
VikeEvtBinder* pHdlr = (VikeEvtBinder*)m_arrHandlers.Item(i);
delete pHdlr;
}
LOGIT(_T("clear m_arrHandlers"));
// and clear the array
m_arrHandlers.Clear();
}//DetachAll
void cbVike::OnChar(VikeWin *vikeWin, wxKeyEvent &event)
{
//begin trap the keycode
bool skip = vikeWin->OnChar(event);
if(skip){
event.Skip();
}
}
void cbVike::OnKeyDown(VikeWin *vikeWin, wxKeyEvent &event)
{
//begin trap the keycode
bool skip = vikeWin->OnKeyDown(event);
if(skip){
event.Skip();
}
}
void cbVike::OnFocus(wxFocusEvent &event)
{
}
/******************* cbVike end *****************************/
/******************* VikeWin start **************************/
VikeWin::VikeWin(cbStyledTextCtrl* target, cbEditor *editor, VikeStatusBar *bar)
: m_searchCmd('/', m_highlight),
m_generalCmd(':', m_highlight),
m_pBuiltinStatusBar(bar)
{
LOGIT(_T("new VikeWin created"));
//init some params
PushState(VIKE_START);
m_iCaretPos = 0;
func = ViFunc::Instance();
wxWindow *parent = target->GetParent();
//parent->Freeze();
/* Kidnap the sizer of editor */
LayoutStatusBar(target, editor);
//parent->Thaw();
LOGIT(_T("we have resize the window for status bar"));
/* the last step to change mode */
ChangeMode(NORMAL, target);
//UpdateCaret(target);
}
void VikeWin::LayoutStatusBar(cbStyledTextCtrl *controller, cbEditor* editor)
{
wxWindow *parent = controller->GetParent();
/* Rebuild status bar */
wxSizer *sizer = editor->GetSizer();
sizer->Clear();
if(parent == editor){
sizer->Add(controller, 1, wxEXPAND);
}else{
sizer->Add(parent, 1, wxEXPAND);
}
sizer->Add(m_pBuiltinStatusBar, 0, wxEXPAND);
editor->Layout();
}
/* Only call with detach */
VikeWin::~VikeWin()
{
LOGIT(_T("Deleting VikeWin"));
}
void VikeWin::GeneralHandler(int keyCode, bool skip)
{
int state = GetState();
if(state == VIKE_INVALID){
ResetState();
return;
}
/* Clear status on VIKE_END */
if(state == VIKE_END){
ClearKeyStatus();
ResetState();
}
/* Not skip, update key status and status bar */
if(!skip){
if(state == VIKE_SEARCH || state == VIKE_COMMAND){
ClearKeyStatus();
}else if(state != VIKE_END){
AppendKeyStatus(keyCode);
}
UpdateStatusBar();
}
}
bool VikeWin::OnChar(wxKeyEvent &event)
{
bool skip = func->NormalKeyHandler(this, event);
GeneralHandler(event.GetKeyCode(), skip);
return skip;
}
bool VikeWin::OnKeyDown(wxKeyEvent &event)
{
bool skip = func->EspecialKeyHandler(this, event);
GeneralHandler(event.GetKeyCode(), skip);
return skip;
}
void VikeWin::UpdateCaret(wxScintilla *editor)
{
if(m_iMode == INSERT){
editor->SetCaretStyle(wxSCI_CARETSTYLE_LINE);
}else{
editor->SetCaretStyle(wxSCI_CARETSTYLE_BLOCK);
}
}
void VikeWin::Finish(wxScintilla *editor)
{
ClearDupNumber();
SetState(VIKE_END);
m_iCaretPos = editor->GetCurrentPos();
}
void VikeWin::ChangeMode(VikeMode new_mode, wxScintilla *editor)
{
m_iMode = new_mode;
UpdateCaret(editor);
UpdateStatusBar();
}
int VikeWin::GetUndoPos() { return m_iCaretPos; }
int VikeWin::GetMode() const { return m_iMode; }
void VikeWin::AppendKeyStatus(int key_code) { m_arrKey.Add(key_code); }
void VikeWin::ClearKeyStatus() { m_arrKey.Clear(); }
VikeSearchCmd &VikeWin::GetSearchCmd() { return m_searchCmd; }
VikeGeneralCmd &VikeWin::GetGeneralCmd() { return m_generalCmd; }
VikeHighlight &VikeWin::GetHighlight() { return m_highlight; }
void VikeWin::SetState(VikeStateEnum newState)
{
int count = m_state.Count();
assert(count > 0);
m_state[count - 1]->state = newState;
}
void VikeWin::PushState(VikeStateEnum newState)
{
m_state.push_back(new VikeState(newState));
}
void VikeWin::PopState()
{
if(m_state.Count() > 1){
delete m_state.Last();
m_state.pop_back();
}
}
VikeStateEnum VikeWin::GetState()
{
return m_state.Last()->state;
}
int VikeWin::GetStateCount()
{
return m_state.Count();
}
void VikeWin::ResetState()
{
while(m_state.Count() > 1){
PopState();
}
SetState(VIKE_START);
ClearDupNumber();
}
/* Shift the duplicate number and add current num to it */
void VikeWin::ShiftAddDupNumber(int num)
{
VikeState *state = m_state.Last();
state->dupNum = state->dupNum * 10 + num;
}
/* whether the duplicate number is typed */
bool VikeWin::IsDup()
{
VikeState *state;
return m_state.Last()->dupNum != 0;
// for(int i = 0; i < m_state.Count(); i++){
// VikeState *state = m_state.Last();
// if(state->dupNum != 0){
// return true;
// }
// }
// return false;
}
/* Get the duplicate number */
int VikeWin::GetDupNumber()
{
VikeState *state = m_state.Last();
return state->dupNum == 0 ? 1 : state->dupNum;
}
void VikeWin::ClearDupNumber()
{
VikeState *state = m_state.Last();
state->dupNum = 0;
}
void VikeWin::UpdateStatusBar()
{
VikeStatusBar *activeBar;
activeBar = m_pBuiltinStatusBar;
if(!activeBar){
return;
}
int state = GetState();
if(m_iMode == NORMAL && state == VIKE_SEARCH){
activeBar->SetStatusText(m_searchCmd.GetCommandWithPrefix(), STATUS_COMMAND);
activeBar->SetStatusText(_T(""), STATUS_KEY);
}else if(m_iMode == NORMAL && state == VIKE_COMMAND){
activeBar->SetStatusText(m_generalCmd.GetCommandWithPrefix(), STATUS_COMMAND);
activeBar->SetStatusText(_T(""), STATUS_KEY);
}else{
const wxChar *mode_txt = NULL;
switch(m_iMode){
case NORMAL:
mode_txt = _T("-- NORMAL --"); break;
case INSERT:
mode_txt = _T("-- INSERT --"); break;
case VISUAL:
mode_txt = _T("-- VISIUAL --"); break;
default:
break;
}
activeBar->SetStatusText(mode_txt, STATUS_COMMAND);
wxString keyState;
for(int i = 0; i < m_arrKey.GetCount(); i++){
keyState.append((wxChar)m_arrKey[i]);
}
if(m_arrKey.IsEmpty()){
activeBar->SetStatusText(_T(""), STATUS_KEY);
}else{
activeBar->SetStatusText(keyState, STATUS_KEY);
}
}
}
void VikeWin::ClearCmd()
{
m_searchCmd.Clear();
m_generalCmd.Clear();
}
/******************* VikeWin end ****************************/