/
EditorCtrl.cpp
8969 lines (7407 loc) · 251 KB
/
EditorCtrl.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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************************************************
*
* 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 "EditorCtrl.h"
#include <wx/clipbrd.h>
#include <wx/filename.h>
#include <wx/tipwin.h>
#include <wx/file.h>
#include <algorithm>
#include "pcre.h"
#include "doc_byte_iter.h"
#include "tm_syntaxhandler.h"
#include "EditorFrame.h"
#include "StyleRun.h"
#include "FindCmdDlg.h"
#include "RunCmdDlg.h"
#include "BundleMenu.h"
#include "GutterCtrl.h"
#include "PreviewDlg.h"
#include "RedoDlg.h"
#include "CompletionPopup.h"
#include "MultilineDataObject.h"
#include "eDocumentPath.h"
#include "ShellRunner.h"
#include "Env.h"
#include "Fold.h"
#include "TextTip.h"
#include "RemoteThread.h"
#include "LiveCaret.h"
#include "eSettings.h"
#include "IAppPaths.h"
#include "Strings.h"
#include "ReplaceStringParser.h"
// Document Icons
#include "document.xpm"
inline bool Isalnum(wxChar c) {
#ifdef __WXMSW__
return ::IsCharAlphaNumeric(c) != 0;
#else
return wxIsalnum(c);
#endif
}
// Embedded class: Sort list based on bundle
class CompareActionBundle : public binary_function<size_t, size_t, bool> {
public:
CompareActionBundle(const vector<const tmAction*>& actionList) : m_list(actionList) {};
bool operator() (const size_t a1, const size_t a2) const {
return m_list[a1]->bundle > m_list[a2]->bundle;
}
private:
const vector<const tmAction*>& m_list;
};
class DragDropTarget : public wxDropTarget {
public:
DragDropTarget(EditorCtrl& parent);
wxDragResult OnData(wxCoord x, wxCoord y, wxDragResult def);
wxDragResult OnDragOver(wxCoord x, wxCoord y, wxDragResult def) {
m_parent.OnDragOver(x, y);
return def;
}
private:
EditorCtrl& m_parent;
wxFileDataObject* m_fileObject;
wxTextDataObject* m_textObject;
MultilineDataObject* m_columnObject;
wxDataObjectComposite* m_dataObject;
};
enum ShellOutput {soDISCARD, soREPLACESEL, soREPLACEDOC, soINSERT, soSNIPPET, soHTML, soTOOLTIP, soNEWDOC};
enum EDITORCTL_IDS {
TIMER_FOLDTOOLTIP = 100,
ID_LEFTSCROLL
};
BEGIN_EVENT_TABLE(EditorCtrl, wxControl)
EVT_PAINT(EditorCtrl::OnPaint)
EVT_CHAR(EditorCtrl::OnChar)
EVT_KEY_DOWN(EditorCtrl::OnKeyDown)
EVT_KEY_UP(EditorCtrl::OnKeyUp)
EVT_SIZE(EditorCtrl::OnSize)
EVT_ERASE_BACKGROUND(EditorCtrl::OnEraseBackground)
EVT_LEFT_DOWN(EditorCtrl::OnMouseLeftDown)
EVT_RIGHT_DOWN(EditorCtrl::OnMouseRightDown)
EVT_LEFT_UP(EditorCtrl::OnMouseLeftUp)
EVT_LEFT_DCLICK(EditorCtrl::OnMouseDClick)
EVT_MOTION(EditorCtrl::OnMouseMotion)
EVT_MOUSE_CAPTURE_LOST(EditorCtrl::OnMouseCaptureLost)
EVT_ENTER_WINDOW(EditorCtrl::OnMouseMotion)
EVT_LEAVE_WINDOW(EditorCtrl::OnLeaveWindow)
EVT_MOUSEWHEEL(EditorCtrl::OnMouseWheel)
EVT_SCROLLWIN(EditorCtrl::OnScroll)
EVT_COMMAND_SCROLL(ID_LEFTSCROLL, EditorCtrl::OnScrollBar)
EVT_IDLE(EditorCtrl::OnIdle)
EVT_CLOSE(EditorCtrl::OnClose)
EVT_MENU_RANGE(1000, 1999, EditorCtrl::OnPopupListMenu)
EVT_TIMER(TIMER_FOLDTOOLTIP, EditorCtrl::OnFoldTooltipTimer)
EVT_SET_FOCUS(EditorCtrl::OnFocus)
END_EVENT_TABLE()
// Initialize statics
const unsigned int EditorCtrl::m_caretWidth = 2;
unsigned long EditorCtrl::s_ctrlDownTime = 0;
bool EditorCtrl::s_altGrDown = false;
/// Open a page saved from a previous session
EditorCtrl::EditorCtrl(const int page_id, CatalystWrapper& cw, wxBitmap& bitmap, wxWindow* parent, EditorFrame& parentFrame) :
m_catalyst(cw),
m_doc(cw),
dispatcher(cw.GetDispatcher()),
bitmap(bitmap),
m_parentFrame(parentFrame),
m_syntaxHandler(m_parentFrame.GetSyntaxHandler()),
m_theme(m_syntaxHandler.GetTheme()),
m_lines(mdc, m_doc, *this, m_theme),
m_search_hl_styler(m_doc, m_lines, m_searchRanges, m_cursors, m_theme),
m_syntaxstyler(m_doc, m_lines, &m_syntaxHandler),
m_foldTooltipTimer(this, TIMER_FOLDTOOLTIP),
m_activeTooltip(NULL),
m_beforeRedrawCallback(NULL),
m_afterRedrawCallback(NULL),
m_scrollCallback(NULL),
m_enableDrawing(false),
m_isResizing(true),
scrollPos(0),
m_scrollPosX(0),
topline(-1),
commandMode(false),
m_changeToken(0),
m_savedForPreview(false),
lastpos(0),
m_currentSel(-1),
do_freeze(true),
m_options_cache(0),
m_re(NULL),
m_symbolCacheToken(0),
bookmarks(m_lines),
m_commandHandler(parentFrame, *this)
{
Create(parent, wxID_ANY, wxPoint(-100,-100), wxDefaultSize, wxNO_BORDER|wxWANTS_CHARS|wxCLIP_CHILDREN|wxNO_FULL_REPAINT_ON_RESIZE);
Hide(); // start hidden to avoid flicker
Init();
eFrameSettings& settings = parentFrame.GetFrameSettings();
RestoreSettings(page_id, settings);
}
/// Open a document
EditorCtrl::EditorCtrl(const doc_id di, const wxString& mirrorPath, CatalystWrapper& cw, wxBitmap& bitmap, wxWindow* parent, EditorFrame& parentFrame, const wxPoint& pos, const wxSize& size):
m_catalyst(cw),
m_doc(cw),
dispatcher(cw.GetDispatcher()),
bitmap(bitmap),
m_parentFrame(parentFrame),
m_syntaxHandler(m_parentFrame.GetSyntaxHandler()),
m_theme(m_syntaxHandler.GetTheme()),
m_lines(mdc, m_doc, *this, m_theme),
m_search_hl_styler(m_doc, m_lines, m_searchRanges, m_cursors, m_theme),
m_syntaxstyler(m_doc, m_lines, &m_syntaxHandler),
m_foldTooltipTimer(this, TIMER_FOLDTOOLTIP),
m_activeTooltip(NULL),
m_beforeRedrawCallback(NULL),
m_afterRedrawCallback(NULL),
m_scrollCallback(NULL),
m_enableDrawing(false),
m_isResizing(true),
scrollPos(0),
m_scrollPosX(0),
topline(-1),
commandMode(false),
m_changeToken(0),
m_savedForPreview(false),
lastpos(0),
m_currentSel(-1),
do_freeze(true),
m_options_cache(0),
m_re(NULL),
m_symbolCacheToken(0),
bookmarks(m_lines),
m_commandHandler(parentFrame, *this)
{
Create(parent, wxID_ANY, pos, size, wxNO_BORDER|wxWANTS_CHARS|wxCLIP_CHILDREN|wxNO_FULL_REPAINT_ON_RESIZE);
Init();
cxLOCKDOC_WRITE(m_doc)
// Set the document & settings
doc.SetDocument(di);
doc.SetDocRead();
cxENDLOCK
m_path = mirrorPath;
// Set lines & positions
m_lines.ReLoadText();
// Set the syntax to match the new document
m_syntaxstyler.UpdateSyntax();
// Init Folding
FoldingInvalidate();
}
/// Create a new empty document
EditorCtrl::EditorCtrl(CatalystWrapper& cw, wxBitmap& bitmap, wxWindow* parent, EditorFrame& parentFrame, const wxPoint& pos, const wxSize& size):
m_catalyst(cw),
m_doc(cw, true),
dispatcher(cw.GetDispatcher()),
bitmap(bitmap),
m_parentFrame(parentFrame),
m_syntaxHandler(m_parentFrame.GetSyntaxHandler()),
m_theme(m_syntaxHandler.GetTheme()),
m_lines(mdc, m_doc, *this, m_theme),
m_search_hl_styler(m_doc, m_lines, m_searchRanges, m_cursors, m_theme),
m_syntaxstyler(m_doc, m_lines, &m_syntaxHandler),
m_foldTooltipTimer(this, TIMER_FOLDTOOLTIP),
m_activeTooltip(NULL),
m_beforeRedrawCallback(NULL),
m_afterRedrawCallback(NULL),
m_scrollCallback(NULL),
m_enableDrawing(false),
m_isResizing(true),
scrollPos(0),
m_scrollPosX(0),
topline(-1),
commandMode(false),
m_changeToken(0),
m_savedForPreview(false),
lastpos(0),
m_currentSel(-1),
do_freeze(true),
m_options_cache(0),
m_re(NULL),
m_symbolCacheToken(0),
bookmarks(m_lines),
m_commandHandler(parentFrame, *this)
{
Create(parent, wxID_ANY, pos, size, wxNO_BORDER|wxWANTS_CHARS|wxCLIP_CHILDREN|wxNO_FULL_REPAINT_ON_RESIZE);
Hide(); // start hidden to avoid flicker
cxLOCKDOC_WRITE(m_doc)
// Make sure we start out with a single empty revision
doc.Freeze();
cxENDLOCK
Init();
// Init Folding
FoldingInvalidate();
}
void EditorCtrl::RestoreSettings(unsigned int page_id, eFrameSettings& settings, unsigned int subid) {
wxString mirrorPath;
doc_id di;
int newpos;
int topline;
wxString syntax;
vector<unsigned int> folds;
vector<unsigned int> bookmarks;
// Retrieve the page info
wxASSERT(0 <= page_id && page_id < (int)settings.GetPageCount());
settings.GetPageSettings(page_id, mirrorPath, di, newpos, topline, syntax, folds, bookmarks, (SubPage)subid);
if (eDocumentPath::IsRemotePath(mirrorPath)) {
// If the mirror points to a remote file, we have to download it first.
SetDocument(di, mirrorPath);
}
else {
const bool isBundleItem = eDocumentPath::IsBundlePath(mirrorPath);
if (isBundleItem) m_remotePath = mirrorPath;
else m_path = mirrorPath;
cxLOCKDOC_WRITE(m_doc)
// Set the document & settings
doc.SetDocument(di);
doc.SetDocRead();
cxENDLOCK
m_lines.ReLoadText();
}
// Set the syntax to match the new path
if (syntax.empty()) {
m_syntaxstyler.UpdateSyntax();
FoldingInvalidate(); // Init Folding
}
else SetSyntax(syntax);
SetDocumentAndScrollPosition(newpos, topline);
// Fold lines that were folded in previous session
if (!folds.empty()) {
// We have to make sure all text is syntaxed and all fold markers found
m_syntaxstyler.ParseAll();
UpdateFolds();
for (vector<unsigned int>::const_iterator p = folds.begin(); p != folds.end(); ++p) {
const unsigned int line_id = *p;
const cxFold target(line_id);
vector<cxFold>::iterator f = lower_bound(m_folds.begin(), m_folds.end(), target);
if (f != m_folds.end() && f->line_id == line_id && f->type == cxFOLD_START)
Fold(*p);
}
}
// Set bookmarks
for (vector<unsigned int>::const_iterator p = bookmarks.begin(); p != bookmarks.end(); ++p)
AddBookmark(*p);
}
void EditorCtrl::Init() {
// Initialize the memoryDC for dubblebuffering
mdc.SetFont(m_theme.font);
// We do not yet call mdc.SelectObject(bitmap) since the bitmap
// is shared with the other EditCtrl's. We call it in Show() & OnSize().
m_remoteProfile = NULL;
// Column selection state
m_blockKeyState = BLOCKKEY_NONE;
m_selMode = SEL_NORMAL;
// Settings
bool autopair = true;
m_doAutoWrap = true;
m_wrapAtMargin = false;
bool doShowMargin = false;
int marginChars = 80;
eSettings& settings = eGetSettings();
settings.GetSettingBool(wxT("autoPair"), autopair);
settings.GetSettingBool(wxT("autoWrap"), m_doAutoWrap);
settings.GetSettingBool(wxT("showMargin"), doShowMargin);
settings.GetSettingBool(wxT("wrapMargin"), m_wrapAtMargin);
settings.GetSettingInt(wxT("marginChars"), marginChars);
m_autopair.Enable(autopair);
m_lastScopePos = -1; // scope selection
if (!doShowMargin) m_wrapAtMargin = false;
// Initialize gutter (line numbers)
m_gutterCtrl = new GutterCtrl(*this, wxID_ANY);
m_gutterCtrl->Hide();
m_showGutter = false;
m_gutterLeft = true; // left side is default
m_gutterWidth = 0;
SetShowGutter(m_parentFrame.IsGutterShown());
m_leftScrollbar = NULL; // default is using internal (right) scrollbar
m_leftScrollWidth = 0;
// To keep track of when we should freeze versions
lastpos = 0;
lastaction = ACTION_INSERT;
// Initialize tooltips
//m_revTooltip.Create(this);
// resize the bitmap used for doublebuffering
wxSize size = GetClientSize();
if (bitmap.GetWidth() < size.x || bitmap.GetHeight() < size.y)
bitmap = wxBitmap(size.x, size.y);
// Init the lines
m_lines.SetWordWrap(m_parentFrame.GetWrapMode());
m_lines.ShowIndent(m_parentFrame.IsIndentShown());
m_lines.Init();
//m_lines.AddStyler(m_usersStyler);
m_lines.AddStyler(m_syntaxstyler);
m_lines.AddStyler(m_search_hl_styler);
// Set initial tabsize
SetTabWidth(m_parentFrame.GetTabWidth(), m_parentFrame.IsSoftTabs());
// Should we show margin line?
if (!doShowMargin) marginChars = 0;
m_lines.ShowMargin(marginChars);
// Create a caret to indicate edit position
m_caretHeight = m_lines.GetLineHeight();
caret = new LiveCaret(this, m_caretWidth, m_caretHeight); // will be deleted by window on destroy
caret->Move(0, 0);
SetCaret(caret);
caret->Show();
SetCursor(wxCursor(wxCURSOR_IBEAM));
// Set drop target so files and text can be dragged from explorer to trigger drag commands
DragDropTarget* dropTarget = new DragDropTarget(*this);
SetDropTarget(dropTarget);
// Make sure we get notified if we should display another document
dispatcher.SubscribeC(wxT("WIN_SETDOCUMENT"), (CALL_BACK)OnSetDocument, this);
dispatcher.SubscribeC(wxT("DOC_COMMITED"), (CALL_BACK)OnDocCommited, this);
dispatcher.SubscribeC(wxT("THEME_CHANGED"), (CALL_BACK)OnThemeChanged, this);
dispatcher.SubscribeC(wxT("BUNDLES_RELOADED"), (CALL_BACK)OnBundlesReloaded, this);
dispatcher.SubscribeC(wxT("SETTINGS_CHANGED"), (CALL_BACK)OnSettingsChanged, this);
}
EditorCtrl::~EditorCtrl() {
// Make sure we no longer recieve any notifiers
dispatcher.UnSubscribe(wxT("WIN_SETDOCUMENT"), (CALL_BACK)OnSetDocument, this);
dispatcher.UnSubscribe(wxT("DOC_COMMITED"), (CALL_BACK)OnDocCommited, this);
dispatcher.UnSubscribe(wxT("THEME_CHANGED"), (CALL_BACK)OnThemeChanged, this);
dispatcher.UnSubscribe(wxT("BUNDLES_RELOADED"), (CALL_BACK)OnBundlesReloaded, this);
dispatcher.UnSubscribe(wxT("SETTINGS_CHANGED"), (CALL_BACK)OnSettingsChanged, this);
// Delete the document
//catalyst.DeleteDocument(revision.GetDocumentID());
cxLOCKDOC_WRITE(m_doc)
doc.Close();
cxENDLOCK
NotifyParentMate();
ClearRemoteInfo();
}
// Notify mate that we have finished editing document
void EditorCtrl::NotifyParentMate() {
if (!m_mate.empty()) {
#ifdef __WXMSW__
HWND hWndRecv = ::FindWindow(wxT("wxWindowClassNR"), m_mate);
if (hWndRecv) {
const wxString cmd = wxT("DONE");
const wxCharBuffer msg = cmd.mb_str(wxConvUTF8);
// Send WM_COPYDATA message
COPYDATASTRUCT cds;
cds.dwData = 0;
cds.cbData = (DWORD)(strlen(msg.data()) + 1);
cds.lpData = (void*)msg.data();
::SendMessage(hWndRecv, WM_COPYDATA, (WPARAM)NULL, (LPARAM)&cds );
}
#endif
}
}
void EditorCtrl::SaveSettings(unsigned int i, eFrameSettings& settings) {
SaveSettings(i, settings, 0);
}
void EditorCtrl::SaveSettings(unsigned int i, eFrameSettings& settings, unsigned int subid) {
const wxString& path = GetPath();
const doc_id di = GetDocID();
const int pos = GetPos();
const int topline = GetTopLine();
const wxString& syntax = GetSyntaxName();
const vector<unsigned int> folds = GetFoldedLines();
settings.SetPageSettings(i, path, di, pos, topline, syntax, folds, bookmarks.GetBookmarks(), (SubPage)subid);
//wxLogDebug(wxT(" %d (%d,%d,%d) pos:%d topline:%d"), i, di.type, di.document_id, di.version_id, pos, topline);
}
EditorCtrl* EditorCtrl::GetActiveEditor() { return this; }
const char** EditorCtrl::RecommendedIcon() const { return document_xpm; }
void EditorCtrl::CommandModeEnded() {
ClearSearchRange();
m_commandHandler.Clear();
}
void EditorCtrl::ClearRemoteInfo() {
if (m_remotePath.empty()) return;
m_remotePath.clear();
m_remoteProfile = NULL;
if (m_path.IsOk()) // Clean up temp buffer file
wxRemoveFile(m_path.GetFullPath());
}
unsigned int EditorCtrl::GetLength() const { return m_lines.GetLength(); }
unsigned int EditorCtrl::GetPos() const { return m_lines.GetPos(); }
void EditorCtrl::SetDocumentAndScrollPosition(int pos, int topline) {
m_lines.SetPos(pos);
scrollPos = m_lines.GetYPosFromLine(topline);
}
unsigned int EditorCtrl::GetCurrentLineNumber() {
// Rows count from 1; internally they count from 0.
return m_lines.GetCurrentLine() +1 ;
}
// Gets the current tab-expanded cursor position, counting from 1.
unsigned int EditorCtrl::GetCurrentColumnNumber() {
// Start of current line
const unsigned int line = m_lines.GetCurrentLine();
const unsigned int linestart = m_lines.GetLineStartpos(line);
// Cursor position within line
const unsigned int pos = m_lines.GetPos();
// Calculate pos with tabs expanded.
const unsigned int tab_size = this->m_parentFrame.GetTabWidth();
unsigned int tabpos = 0;
cxLOCKDOC_READ(m_doc)
for (doc_byte_iter dbi(doc, linestart); (unsigned int)dbi.GetIndex() < pos; ++dbi) {
// Only count first byte of UTF-8 multibyte chars
if ((*dbi & 0xC0) == 0x80) continue;
if (*dbi == '\t') {
// Figure out how far to advance to next tab stop
const int over = tabpos % tab_size;
tabpos += (tab_size - over);
}
else tabpos++;
}
cxENDLOCK;
// Columns are 1-indexed.
return tabpos + 1;
}
int EditorCtrl::GetTopLine() {
wxASSERT(scrollPos >= 0);
//wxLogDebug(wxT("GetTopLine() - %d %d - %s"), scrollPos, m_lines.GetLineFromYPos(scrollPos), GetPath());
return m_lines.GetLineFromYPos(scrollPos);
}
void EditorCtrl::Freeze() {
cxLOCKDOC_WRITE(m_doc)
doc.Freeze();
cxENDLOCK
}
doc_id EditorCtrl::GetDocID() const {
cxLOCKDOC_READ(m_doc)
return doc.GetDocument();
cxENDLOCK
}
wxString EditorCtrl::GetName() const {
cxLOCKDOC_READ(m_doc)
return doc.GetPropertyName();
cxENDLOCK
}
wxFontEncoding EditorCtrl::GetEncoding() const {
cxLOCKDOC_READ(m_doc)
return doc.GetPropertyEncoding();
cxENDLOCK
}
void EditorCtrl::SetEncoding(wxFontEncoding enc) {
cxLOCKDOC_WRITE(m_doc)
doc.Freeze();
doc.SetPropertyEncoding(enc);
doc.Freeze();
cxENDLOCK
MarkAsModified();
}
bool EditorCtrl::GetBOM() const {
cxLOCKDOC_READ(m_doc)
return doc.GetPropertyBOM();
cxENDLOCK
}
void EditorCtrl::SetBOM(bool saveBOM) {
cxLOCKDOC_WRITE(m_doc)
doc.Freeze();
doc.SetPropertyBOM(saveBOM);
doc.Freeze();
cxENDLOCK
MarkAsModified();
}
wxTextFileType EditorCtrl::GetEOL() const {
cxLOCKDOC_READ(m_doc)
return doc.GetPropertyEOL();
cxENDLOCK
}
void EditorCtrl::SetEOL(wxTextFileType eol) {
cxLOCKDOC_WRITE(m_doc)
doc.Freeze();
doc.SetPropertyEOL(eol);
doc.Freeze();
cxENDLOCK
MarkAsModified();
}
/*
void EditorCtrl::ActivateHighlight() {
m_usersStyler.Enable();
//DrawLayout();
}
void EditorCtrl::DisableHighlight() {
m_usersStyler.Disable();
//DrawLayout();
}
*/
void EditorCtrl::ClearSearchHighlight() {
m_search_hl_styler.Clear();
DrawLayout();
}
bool EditorCtrl::IsOk() const {
wxSize size = GetClientSize();
// It is ok to have a size 0 window with invalid bitmap
return (size.x == 0 && size.y == 0) || bitmap.Ok();
}
// WARNING: Be aware that during a change grouping, lines is not valid
// Only call functions like lines.RemoveAllSelections() that does not use line info
void EditorCtrl::StartChange() {
do_freeze = false;
change_pos = m_lines.GetPos();
cxLOCKDOC_WRITE(m_doc)
doc.Freeze();
change_doc_id = doc.GetDocument();
cxENDLOCK
change_toppos = m_lines.GetPosFromXY(0, scrollPos+1);
}
void EditorCtrl::EndChange() {
wxASSERT(do_freeze == false);
doc_id di;
cxLOCKDOC_WRITE(m_doc)
doc.Freeze();
di = doc.GetDocument();
cxENDLOCK
// Invalidate all stylers
StylersInvalidate();
if (change_doc_id.SameDoc(di)) {
// If we are still in the same document, we want to find
// matching positions and selections
if (change_doc_id.version_id == di.version_id) return;
vector<interval> oldsel = m_lines.GetSelections();
m_lines.ReLoadText();
// re-set the width
UpdateEditorWidth();
RemapPos(change_doc_id, change_pos, oldsel, change_toppos);
}
else {
scrollPos = 0;
m_lines.ReLoadText();
// re-set the width
UpdateEditorWidth();
}
do_freeze = true;
}
bool EditorCtrl::Show(bool show) {
bool result = wxControl::Show(show);
// All EditorCtrl's share the same bitmap.
// So we have to redraw it when the control is first shown
if (show) {
mdc.SelectObject(bitmap);
DrawLayout();
}
else mdc.SelectObject(wxNullBitmap);
return result;
}
void EditorCtrl::SetShowGutter(bool showGutter) {
if (m_showGutter == showGutter) return;
if (showGutter) m_gutterCtrl->Show();
else {
m_gutterCtrl->Hide();
m_gutterWidth = 0;
}
m_showGutter = showGutter;
DrawLayout();
}
void EditorCtrl::SetShowIndent(bool showIndent) {
m_lines.ShowIndent(showIndent);
DrawLayout();
}
void EditorCtrl::SetWordWrap(cxWrapMode wrapMode) {
if (m_lines.GetWrapMode() == wrapMode) return;
// Make sure we keep the same topline
topline = m_lines.GetLineFromYPos(scrollPos);
m_lines.SetWordWrap(wrapMode);
if (m_wrapAtMargin && wrapMode != cxWRAP_NONE)
m_lines.SetWidth(m_lines.GetMarginPos(), topline);
scrollPos = m_lines.GetYPosFromLine(topline);
DrawLayout();
}
void EditorCtrl::SetTabWidth(unsigned int width, bool soft_tabs) {
// m_indent is the string used for indentation, either a real tab character
// or an appropriate number of spaces
if (soft_tabs)
m_indent = wxString(wxT(' '), width);
else
m_indent = wxString(wxT("\t"));
m_lines.SetTabWidth(width);
FoldingReIndent();
MarkAsModified();
}
const wxFont& EditorCtrl::GetEditorFont() const { return mdc.GetFont(); }
void EditorCtrl::SetGutterRight(bool doMove) {
m_gutterLeft = !doMove;
m_gutterCtrl->SetGutterRight(doMove);
DrawLayout();
}
bool EditorCtrl::HasScrollbar() const {
if (m_leftScrollbar) return m_leftScrollbar->IsShown();
else return (GetScrollThumb(wxVERTICAL) > 0);
}
void EditorCtrl::SetScrollbarLeft(bool doMove) {
if (doMove) {
m_leftScrollbar = new wxScrollBar(this, ID_LEFTSCROLL, wxPoint(0,0), wxDefaultSize, wxSB_VERTICAL);
m_leftScrollbar->SetCursor(*wxSTANDARD_CURSOR); // Set to standard cursor (otherwise it will inherit from editorCtrl)
const bool isShown = (GetScrollThumb(wxVERTICAL) > 0);
if (isShown) {
m_leftScrollWidth = m_leftScrollbar->GetSize().x;
SetScrollbar(wxVERTICAL, 0, 0, 0); // hide windows own scrollbar
return; // removing scrollbar will have sent draw event
}
}
else {
delete m_leftScrollbar;
m_leftScrollbar = NULL;
m_leftScrollWidth = 0;
}
m_isResizing = true;
DrawLayout();
}
//
// Returns true if a scrollbar was added or removed, else false.
//
bool EditorCtrl::UpdateScrollbars(unsigned int x, unsigned int y) {
const unsigned int height = m_lines.GetHeight();
// Check if we need a vertical scrollbar
if (m_leftScrollbar) {
const int scroll_thumb = m_leftScrollbar->GetThumbSize();
if (height > y) {
m_leftScrollbar->Show();
const int scroll_range = m_leftScrollbar->GetRange();
if (scroll_thumb != (int)y || scroll_range != (int)height) {
m_leftScrollbar->SetScrollbar(scrollPos, y, height, y);
if (scroll_thumb == 0) {
ReDraw();
return true; // cancel old redraw
}
}
// Avoid empty space at bottom
if (scrollPos + y > height) scrollPos = wxMax(0, height - y);
m_leftScrollbar->SetSize(-1, GetSize().y);
m_leftScrollbar->SetThumbPosition(scrollPos);
const unsigned int width = m_leftScrollbar->GetSize().x;
if (m_leftScrollWidth != width) {
m_leftScrollWidth = width;
ReDraw();
return true; // cancel old redraw
}
}
else if (scroll_thumb > 0) {
m_leftScrollbar->Hide();
m_leftScrollWidth = 0;
ReDraw();
return true; // cancel old redraw
}
}
else {
const int scroll_thumb = GetScrollThumb(wxVERTICAL);
if (height > y) {
const int scroll_range = GetScrollRange(wxVERTICAL);
if (scroll_thumb != (int)y || scroll_range != (int)height) {
SetScrollbar(wxVERTICAL, scrollPos, y, height);
if (scroll_thumb == 0) return true; // Creation of scrollbar have sent a size event
}
// Avoid empty space at bottom
if (scrollPos + y > height) scrollPos = wxMax(0, height - y);
SetScrollPos(wxVERTICAL, scrollPos);
}
else if (scroll_thumb > 0) {
SetScrollbar(wxVERTICAL, 0, 0, 0);
return true; // Removal of scrollbar have sent a size event
}
}
// Check if we need a horizontal scrollbar
const int scrollThumbX = GetScrollThumb(wxHORIZONTAL);
const unsigned int width = m_lines.GetWidth() + m_caretWidth;
if ((m_lines.GetWrapMode() == cxWRAP_NONE || m_wrapAtMargin) && width > x) {
m_scrollPosX = wxMin(m_scrollPosX, (int)(width-x));
m_scrollPosX = wxMax(0, m_scrollPosX);
SetScrollbar(wxHORIZONTAL, m_scrollPosX, x, width);
}
else if (scrollThumbX > 0) {
m_scrollPosX = 0;
SetScrollbar(wxHORIZONTAL, 0, 0, 0);
return true; // Removal of scrollbar have sent a size event
}
return false; // no scrollbar was added or removed
}
void EditorCtrl::DrawLayout(bool isScrolling) {
wxClientDC dc(this);
DrawLayout(dc, isScrolling);
}
void EditorCtrl::DrawLayout(wxDC& dc, bool WXUNUSED(isScrolling)) {
if (!IsShown() || !m_enableDrawing) return; // No need to draw
wxLogDebug(wxT("DrawLayout() : %d (%d,%d)"), GetId(), m_enableDrawing, IsShown());
//wxLogDebug(wxT("DrawLayout() : %s"), GetName());
if (m_beforeRedrawCallback) m_beforeRedrawCallback(m_callbackData);
wxASSERT(m_scrollPosX >= 0);
// Check if we should cancel a snippet
if (m_snippetHandler.IsActive()) m_snippetHandler.Validate();
// Get view dimensions
const wxSize size = GetClientSize();
if (size.x == 0 || size.y == 0) return; // Nothing to draw
if (m_showGutter) {
m_gutterWidth = m_gutterCtrl->CalcLayout(size.y);
// Move gutter to correct position
const unsigned int gutterxpos = m_gutterLeft ? m_leftScrollWidth : size.x - m_gutterWidth;
if (m_gutterCtrl->GetPosition().x != (int)gutterxpos) {
m_gutterCtrl->SetPosition(wxPoint(gutterxpos, 0));
}
}
const unsigned int editorSizeX = ClientWidthToEditor(size.x);
// There may only be room for the gutter
if (editorSizeX == 0) {
if (m_showGutter) m_gutterCtrl->DrawGutter();
return;
}
// Resize the bitmap used for doublebuffering
if (bitmap.GetWidth() < (int)editorSizeX || bitmap.GetHeight() < size.y) {
// disassociate and release mem for old bitmap
mdc.SelectObjectAsSource(wxNullBitmap);
bitmap = wxNullBitmap;
// Resize bitmap
bitmap = wxBitmap(size.x, size.y);
mdc.SelectObject(bitmap);
}
// We always have to reselect the bitmap if it has been resized
// by another editorCtrl. Otherwise the memorydc will get confused.
if (mdc.GetSize() != wxSize(bitmap.GetWidth(), bitmap.GetHeight())) {
mdc.SelectObject(bitmap);
}
// Verify scrollPos (might come from an un-updated scrollbar)
if (scrollPos < 0) {
// Scroll value of -1 indicates that we should move to the
// bottom of the document.
wxASSERT(scrollPos == -1); // only valid neg. value
//wxASSERT(size.x == m_lines.GetWidth()); // should never come with a size change
m_lines.PrepareAll(); // make sure all line positions are valid
scrollPos = wxMax(0, m_lines.GetHeight()-size.y);
}
else {
scrollPos = wxMin(scrollPos, wxMax(0, m_lines.GetHeight()-size.y));
if (m_isResizing && editorSizeX != m_lines.GetDisplayWidth()) {
const int newtopline = m_lines.GetLineFromYPos(scrollPos);
const unsigned int lineWidth = (m_wrapAtMargin && m_lines.GetWrapMode() != cxWRAP_NONE) ? m_lines.GetMarginPos() : editorSizeX;
m_lines.SetWidth(lineWidth, newtopline);
// If there are folds, we have to make sure all line positions are valid
if (HasFoldedFolds()) m_lines.PrepareAll();
// Lock topline on resize
if (topline == -1) topline = newtopline;
scrollPos = m_lines.GetYPosFromLine(newtopline);
}
else {
topline = -1;
scrollPos += m_lines.PrepareYPos(scrollPos);
scrollPos = wxMin(scrollPos, m_lines.GetHeight()-size.y);
scrollPos = wxMax(0, scrollPos);
}
}
wxASSERT(scrollPos >= 0);
wxASSERT(scrollPos <= m_lines.GetHeight());
// Check if we need to adjust scrollbar
if (UpdateScrollbars(editorSizeX, size.y)) return; // adding/removing scrollbars send size event
// Make sure we remove un-needed stylers
if (!m_parentFrame.IsSearching())
m_search_hl_styler.Clear(); // Only highlight search terms during search
// When scrolling, we can just draw the new parts
wxRect rect(0, scrollPos, editorSizeX, size.y);
/*if (isScrolling && scrollPos != old_scrollPos && this == FindFocus()) {
// If there is overlap, then move the image
if (scrollPos + size.y > old_scrollPos && scrollPos < old_scrollPos + size.y) {
const int top = wxMax(scrollPos, old_scrollPos);
const int bottom = wxMin(scrollPos, old_scrollPos) + size.y;
const int overlap_height = bottom - top;
#ifdef __WXMSW__
::BitBlt(GetHdcOf(mdc), 0, (top - old_scrollPos) + (old_scrollPos - scrollPos), editorSizeX, overlap_height, GetHdcOf(mdc), 0, top - old_scrollPos, SRCCOPY);
#else
mdc.Blit(0, (top - old_scrollPos) + (old_scrollPos - scrollPos), editorSizeX, overlap_height, &mdc, 0, top - old_scrollPos);
#endif
// Calculate rect of newly revealed part
const int new_height = size.y - overlap_height;
const int newtop = (top == scrollPos ? bottom : scrollPos);
rect = wxRect(0,newtop,editorSizeX,new_height);
wxASSERT(newtop <= m_lines.GetHeight());
}
}*/
// Highlight matching brackets
MatchBrackets();
// Set the theme colors
mdc.SetTextForeground(m_theme.foregroundColor);
mdc.SetBrush(wxBrush(m_theme.backgroundColor));
mdc.SetPen(wxPen(m_theme.backgroundColor));
// Clear the background
mdc.DrawRectangle(rect.x, rect.y - scrollPos, rect.width, rect.height);
// Draw the layout to MemoryDC