/
ScintillaGTK.cxx
3163 lines (2811 loc) · 95.3 KB
/
ScintillaGTK.cxx
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
// Scintilla source code edit control
// ScintillaGTK.cxx - GTK+ specific subclass of ScintillaBase
// Copyright 1998-2004 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <assert.h>
#include <ctype.h>
#include <stdexcept>
#include <new>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <glib.h>
#include <gmodule.h>
#include <gdk/gdk.h>
#include <gtk/gtk.h>
#include <gdk/gdkkeysyms.h>
#if defined(GDK_WINDOWING_WAYLAND)
#include <gdk/gdkwayland.h>
#endif
#if defined(__WIN32__) || defined(_MSC_VER)
#include <windows.h>
#endif
#include "Platform.h"
#include "ILexer.h"
#include "Scintilla.h"
#include "ScintillaWidget.h"
#ifdef SCI_LEXER
#include "SciLexer.h"
#endif
#include "StringCopy.h"
#ifdef SCI_LEXER
#include "LexerModule.h"
#endif
#include "Position.h"
#include "SplitVector.h"
#include "Partitioning.h"
#include "RunStyles.h"
#include "ContractionState.h"
#include "CellBuffer.h"
#include "CallTip.h"
#include "KeyMap.h"
#include "Indicator.h"
#include "XPM.h"
#include "LineMarker.h"
#include "Style.h"
#include "ViewStyle.h"
#include "CharClassify.h"
#include "Decoration.h"
#include "CaseFolder.h"
#include "Document.h"
#include "CaseConvert.h"
#include "UniConversion.h"
#include "UnicodeFromUTF8.h"
#include "Selection.h"
#include "PositionCache.h"
#include "EditModel.h"
#include "MarginView.h"
#include "EditView.h"
#include "Editor.h"
#include "AutoComplete.h"
#include "ScintillaBase.h"
#ifdef SCI_LEXER
#include "ExternalLexer.h"
#endif
#include "ScintillaGTK.h"
#include "scintilla-marshal.h"
#include "ScintillaGTKAccessible.h"
#include "Converter.h"
#if GTK_CHECK_VERSION(2,20,0)
#define IS_WIDGET_REALIZED(w) (gtk_widget_get_realized(GTK_WIDGET(w)))
#define IS_WIDGET_MAPPED(w) (gtk_widget_get_mapped(GTK_WIDGET(w)))
#else
#define IS_WIDGET_REALIZED(w) (GTK_WIDGET_REALIZED(w))
#define IS_WIDGET_MAPPED(w) (GTK_WIDGET_MAPPED(w))
#endif
#define SC_INDICATOR_INPUT INDIC_IME
#define SC_INDICATOR_TARGET INDIC_IME+1
#define SC_INDICATOR_CONVERTED INDIC_IME+2
#define SC_INDICATOR_UNKNOWN INDIC_IME_MAX
static GdkWindow *WindowFromWidget(GtkWidget *w) {
return gtk_widget_get_window(w);
}
#ifdef _MSC_VER
// Constant conditional expressions are because of GTK+ headers
#pragma warning(disable: 4127)
// Ignore unreferenced local functions in GTK+ headers
#pragma warning(disable: 4505)
#endif
#ifdef SCI_NAMESPACE
using namespace Scintilla;
#endif
static GdkWindow *PWindow(const Window &w) {
GtkWidget *widget = static_cast<GtkWidget *>(w.GetID());
return gtk_widget_get_window(widget);
}
extern std::string UTF8FromLatin1(const char *s, int len);
enum {
COMMAND_SIGNAL,
NOTIFY_SIGNAL,
LAST_SIGNAL
};
static gint scintilla_signals[LAST_SIGNAL] = { 0 };
enum {
TARGET_STRING,
TARGET_TEXT,
TARGET_COMPOUND_TEXT,
TARGET_UTF8_STRING,
TARGET_URI
};
GdkAtom ScintillaGTK::atomClipboard = 0;
GdkAtom ScintillaGTK::atomUTF8 = 0;
GdkAtom ScintillaGTK::atomString = 0;
GdkAtom ScintillaGTK::atomUriList = 0;
GdkAtom ScintillaGTK::atomDROPFILES_DND = 0;
static const GtkTargetEntry clipboardCopyTargets[] = {
{ (gchar *) "UTF8_STRING", 0, TARGET_UTF8_STRING },
{ (gchar *) "STRING", 0, TARGET_STRING },
};
static const gint nClipboardCopyTargets = ELEMENTS(clipboardCopyTargets);
static const GtkTargetEntry clipboardPasteTargets[] = {
{ (gchar *) "text/uri-list", 0, TARGET_URI },
{ (gchar *) "UTF8_STRING", 0, TARGET_UTF8_STRING },
{ (gchar *) "STRING", 0, TARGET_STRING },
};
static const gint nClipboardPasteTargets = ELEMENTS(clipboardPasteTargets);
static GtkWidget *PWidget(Window &w) {
return static_cast<GtkWidget *>(w.GetID());
}
ScintillaGTK *ScintillaGTK::FromWidget(GtkWidget *widget) {
ScintillaObject *scio = SCINTILLA(widget);
return static_cast<ScintillaGTK *>(scio->pscin);
}
ScintillaGTK::ScintillaGTK(_ScintillaObject *sci_) :
adjustmentv(0), adjustmenth(0),
verticalScrollBarWidth(30), horizontalScrollBarHeight(30),
evbtn(0), capturedMouse(false), dragWasDropped(false),
lastKey(0), rectangularSelectionModifier(SCMOD_CTRL), parentClass(0),
im_context(NULL), lastNonCommonScript(PANGO_SCRIPT_INVALID_CODE),
lastWheelMouseDirection(0),
wheelMouseIntensity(0),
smoothScrollY(0),
smoothScrollX(0),
rgnUpdate(0),
repaintFullWindow(false),
styleIdleID(0),
accessible(0) {
sci = sci_;
wMain = GTK_WIDGET(sci);
#if PLAT_GTK_WIN32
rectangularSelectionModifier = SCMOD_ALT;
#else
rectangularSelectionModifier = SCMOD_CTRL;
#endif
#if PLAT_GTK_WIN32
// There does not seem to be a real standard for indicating that the clipboard
// contains a rectangular selection, so copy Developer Studio.
cfColumnSelect = static_cast<CLIPFORMAT>(
::RegisterClipboardFormat("MSDEVColumnSelect"));
// Get intellimouse parameters when running on win32; otherwise use
// reasonable default
#ifndef SPI_GETWHEELSCROLLLINES
#define SPI_GETWHEELSCROLLLINES 104
#endif
::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &linesPerScroll, 0);
#else
linesPerScroll = 4;
#endif
lastWheelMouseTime.tv_sec = 0;
lastWheelMouseTime.tv_usec = 0;
Initialise();
}
ScintillaGTK::~ScintillaGTK() {
if (styleIdleID) {
g_source_remove(styleIdleID);
styleIdleID = 0;
}
if (evbtn) {
gdk_event_free(reinterpret_cast<GdkEvent *>(evbtn));
evbtn = 0;
}
wPreedit.Destroy();
}
static void UnRefCursor(GdkCursor *cursor) {
#if GTK_CHECK_VERSION(3,0,0)
g_object_unref(cursor);
#else
gdk_cursor_unref(cursor);
#endif
}
void ScintillaGTK::RealizeThis(GtkWidget *widget) {
//Platform::DebugPrintf("ScintillaGTK::realize this\n");
#if GTK_CHECK_VERSION(2,20,0)
gtk_widget_set_realized(widget, TRUE);
#else
GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
#endif
GdkWindowAttr attrs;
attrs.window_type = GDK_WINDOW_CHILD;
GtkAllocation allocation;
gtk_widget_get_allocation(widget, &allocation);
attrs.x = allocation.x;
attrs.y = allocation.y;
attrs.width = allocation.width;
attrs.height = allocation.height;
attrs.wclass = GDK_INPUT_OUTPUT;
attrs.visual = gtk_widget_get_visual(widget);
#if !GTK_CHECK_VERSION(3,0,0)
attrs.colormap = gtk_widget_get_colormap(widget);
#endif
attrs.event_mask = gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK;
GdkDisplay *pdisplay = gtk_widget_get_display(widget);
GdkCursor *cursor = gdk_cursor_new_for_display(pdisplay, GDK_XTERM);
attrs.cursor = cursor;
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_set_window(widget, gdk_window_new(gtk_widget_get_parent_window(widget), &attrs,
GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_CURSOR));
#if GTK_CHECK_VERSION(3,8,0)
gtk_widget_register_window(widget, gtk_widget_get_window(widget));
#else
gdk_window_set_user_data(gtk_widget_get_window(widget), widget);
#endif
#if !GTK_CHECK_VERSION(3,18,0)
gtk_style_context_set_background(gtk_widget_get_style_context(widget),
gtk_widget_get_window(widget));
#endif
gdk_window_show(gtk_widget_get_window(widget));
UnRefCursor(cursor);
#else
widget->window = gdk_window_new(gtk_widget_get_parent_window(widget), &attrs,
GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP | GDK_WA_CURSOR);
gdk_window_set_user_data(widget->window, widget);
widget->style = gtk_style_attach(widget->style, widget->window);
gdk_window_set_background(widget->window, &widget->style->bg[GTK_STATE_NORMAL]);
gdk_window_show(widget->window);
UnRefCursor(cursor);
#endif
gtk_widget_realize(PWidget(wPreedit));
gtk_widget_realize(PWidget(wPreeditDraw));
im_context = gtk_im_multicontext_new();
g_signal_connect(G_OBJECT(im_context), "commit",
G_CALLBACK(Commit), this);
g_signal_connect(G_OBJECT(im_context), "preedit_changed",
G_CALLBACK(PreeditChanged), this);
gtk_im_context_set_client_window(im_context, WindowFromWidget(widget));
GtkWidget *widtxt = PWidget(wText); // // No code inside the G_OBJECT macro
g_signal_connect_after(G_OBJECT(widtxt), "style_set",
G_CALLBACK(ScintillaGTK::StyleSetText), NULL);
g_signal_connect_after(G_OBJECT(widtxt), "realize",
G_CALLBACK(ScintillaGTK::RealizeText), NULL);
gtk_widget_realize(widtxt);
gtk_widget_realize(PWidget(scrollbarv));
gtk_widget_realize(PWidget(scrollbarh));
cursor = gdk_cursor_new_for_display(pdisplay, GDK_XTERM);
gdk_window_set_cursor(PWindow(wText), cursor);
UnRefCursor(cursor);
cursor = gdk_cursor_new_for_display(pdisplay, GDK_LEFT_PTR);
gdk_window_set_cursor(PWindow(scrollbarv), cursor);
UnRefCursor(cursor);
cursor = gdk_cursor_new_for_display(pdisplay, GDK_LEFT_PTR);
gdk_window_set_cursor(PWindow(scrollbarh), cursor);
UnRefCursor(cursor);
gtk_selection_add_targets(widget, GDK_SELECTION_PRIMARY,
clipboardCopyTargets, nClipboardCopyTargets);
}
void ScintillaGTK::Realize(GtkWidget *widget) {
ScintillaGTK *sciThis = FromWidget(widget);
sciThis->RealizeThis(widget);
}
void ScintillaGTK::UnRealizeThis(GtkWidget *widget) {
try {
gtk_selection_clear_targets(widget, GDK_SELECTION_PRIMARY);
if (IS_WIDGET_MAPPED(widget)) {
gtk_widget_unmap(widget);
}
#if GTK_CHECK_VERSION(2,20,0)
gtk_widget_set_realized(widget, FALSE);
#else
GTK_WIDGET_UNSET_FLAGS(widget, GTK_REALIZED);
#endif
gtk_widget_unrealize(PWidget(wText));
if (PWidget(scrollbarv))
gtk_widget_unrealize(PWidget(scrollbarv));
if (PWidget(scrollbarh))
gtk_widget_unrealize(PWidget(scrollbarh));
gtk_widget_unrealize(PWidget(wPreedit));
gtk_widget_unrealize(PWidget(wPreeditDraw));
g_object_unref(im_context);
im_context = NULL;
if (GTK_WIDGET_CLASS(parentClass)->unrealize)
GTK_WIDGET_CLASS(parentClass)->unrealize(widget);
Finalise();
} catch (...) {
errorStatus = SC_STATUS_FAILURE;
}
}
void ScintillaGTK::UnRealize(GtkWidget *widget) {
ScintillaGTK *sciThis = FromWidget(widget);
sciThis->UnRealizeThis(widget);
}
static void MapWidget(GtkWidget *widget) {
if (widget &&
gtk_widget_get_visible(GTK_WIDGET(widget)) &&
!IS_WIDGET_MAPPED(widget)) {
gtk_widget_map(widget);
}
}
void ScintillaGTK::MapThis() {
try {
//Platform::DebugPrintf("ScintillaGTK::map this\n");
#if GTK_CHECK_VERSION(2,20,0)
gtk_widget_set_mapped(PWidget(wMain), TRUE);
#else
GTK_WIDGET_SET_FLAGS(PWidget(wMain), GTK_MAPPED);
#endif
MapWidget(PWidget(wText));
MapWidget(PWidget(scrollbarh));
MapWidget(PWidget(scrollbarv));
wMain.SetCursor(Window::cursorArrow);
scrollbarv.SetCursor(Window::cursorArrow);
scrollbarh.SetCursor(Window::cursorArrow);
ChangeSize();
gdk_window_show(PWindow(wMain));
} catch (...) {
errorStatus = SC_STATUS_FAILURE;
}
}
void ScintillaGTK::Map(GtkWidget *widget) {
ScintillaGTK *sciThis = FromWidget(widget);
sciThis->MapThis();
}
void ScintillaGTK::UnMapThis() {
try {
//Platform::DebugPrintf("ScintillaGTK::unmap this\n");
#if GTK_CHECK_VERSION(2,20,0)
gtk_widget_set_mapped(PWidget(wMain), FALSE);
#else
GTK_WIDGET_UNSET_FLAGS(PWidget(wMain), GTK_MAPPED);
#endif
DropGraphics(false);
gdk_window_hide(PWindow(wMain));
gtk_widget_unmap(PWidget(wText));
if (PWidget(scrollbarh))
gtk_widget_unmap(PWidget(scrollbarh));
if (PWidget(scrollbarv))
gtk_widget_unmap(PWidget(scrollbarv));
} catch (...) {
errorStatus = SC_STATUS_FAILURE;
}
}
void ScintillaGTK::UnMap(GtkWidget *widget) {
ScintillaGTK *sciThis = FromWidget(widget);
sciThis->UnMapThis();
}
void ScintillaGTK::ForAll(GtkCallback callback, gpointer callback_data) {
try {
(*callback) (PWidget(wText), callback_data);
if (PWidget(scrollbarv))
(*callback) (PWidget(scrollbarv), callback_data);
if (PWidget(scrollbarh))
(*callback) (PWidget(scrollbarh), callback_data);
} catch (...) {
errorStatus = SC_STATUS_FAILURE;
}
}
void ScintillaGTK::MainForAll(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data) {
ScintillaGTK *sciThis = FromWidget((GtkWidget *)container);
if (callback != NULL && include_internals) {
sciThis->ForAll(callback, callback_data);
}
}
namespace {
class PreEditString {
public:
gchar *str;
gint cursor_pos;
PangoAttrList *attrs;
gboolean validUTF8;
glong uniStrLen;
gunichar *uniStr;
PangoScript pscript;
explicit PreEditString(GtkIMContext *im_context) {
gtk_im_context_get_preedit_string(im_context, &str, &attrs, &cursor_pos);
validUTF8 = g_utf8_validate(str, strlen(str), NULL);
uniStr = g_utf8_to_ucs4_fast(str, strlen(str), &uniStrLen);
pscript = pango_script_for_unichar(uniStr[0]);
}
~PreEditString() {
g_free(str);
g_free(uniStr);
pango_attr_list_unref(attrs);
}
};
}
gint ScintillaGTK::FocusInThis(GtkWidget *) {
try {
SetFocusState(true);
if (im_context != NULL) {
PreEditString pes(im_context);
if (PWidget(wPreedit) != NULL) {
if (strlen(pes.str) > 0) {
gtk_widget_show(PWidget(wPreedit));
} else {
gtk_widget_hide(PWidget(wPreedit));
}
}
gtk_im_context_focus_in(im_context);
}
} catch (...) {
errorStatus = SC_STATUS_FAILURE;
}
return FALSE;
}
gint ScintillaGTK::FocusIn(GtkWidget *widget, GdkEventFocus * /*event*/) {
ScintillaGTK *sciThis = FromWidget(widget);
return sciThis->FocusInThis(widget);
}
gint ScintillaGTK::FocusOutThis(GtkWidget *) {
try {
SetFocusState(false);
if (PWidget(wPreedit) != NULL)
gtk_widget_hide(PWidget(wPreedit));
if (im_context != NULL)
gtk_im_context_focus_out(im_context);
} catch (...) {
errorStatus = SC_STATUS_FAILURE;
}
return FALSE;
}
gint ScintillaGTK::FocusOut(GtkWidget *widget, GdkEventFocus * /*event*/) {
ScintillaGTK *sciThis = FromWidget(widget);
return sciThis->FocusOutThis(widget);
}
void ScintillaGTK::SizeRequest(GtkWidget *widget, GtkRequisition *requisition) {
ScintillaGTK *sciThis = FromWidget(widget);
requisition->width = 1;
requisition->height = 1;
GtkRequisition child_requisition;
#if GTK_CHECK_VERSION(3,0,0)
gtk_widget_get_preferred_size(PWidget(sciThis->scrollbarh), NULL, &child_requisition);
gtk_widget_get_preferred_size(PWidget(sciThis->scrollbarv), NULL, &child_requisition);
#else
gtk_widget_size_request(PWidget(sciThis->scrollbarh), &child_requisition);
gtk_widget_size_request(PWidget(sciThis->scrollbarv), &child_requisition);
#endif
}
#if GTK_CHECK_VERSION(3,0,0)
void ScintillaGTK::GetPreferredWidth(GtkWidget *widget, gint *minimalWidth, gint *naturalWidth) {
GtkRequisition requisition;
SizeRequest(widget, &requisition);
*minimalWidth = *naturalWidth = requisition.width;
}
void ScintillaGTK::GetPreferredHeight(GtkWidget *widget, gint *minimalHeight, gint *naturalHeight) {
GtkRequisition requisition;
SizeRequest(widget, &requisition);
*minimalHeight = *naturalHeight = requisition.height;
}
#endif
void ScintillaGTK::SizeAllocate(GtkWidget *widget, GtkAllocation *allocation) {
ScintillaGTK *sciThis = FromWidget(widget);
try {
gtk_widget_set_allocation(widget, allocation);
if (IS_WIDGET_REALIZED(widget))
gdk_window_move_resize(WindowFromWidget(widget),
allocation->x,
allocation->y,
allocation->width,
allocation->height);
sciThis->Resize(allocation->width, allocation->height);
} catch (...) {
sciThis->errorStatus = SC_STATUS_FAILURE;
}
}
void ScintillaGTK::Initialise() {
//Platform::DebugPrintf("ScintillaGTK::Initialise\n");
parentClass = reinterpret_cast<GtkWidgetClass *>(
g_type_class_ref(gtk_container_get_type()));
gint maskSmooth = 0;
#if defined(GDK_WINDOWING_WAYLAND)
GdkDisplay *pdisplay = gdk_display_get_default();
if (GDK_IS_WAYLAND_DISPLAY(pdisplay)) {
// On Wayland, touch pads only produce smooth scroll events
maskSmooth = GDK_SMOOTH_SCROLL_MASK;
}
#endif
gtk_widget_set_can_focus(PWidget(wMain), TRUE);
gtk_widget_set_sensitive(PWidget(wMain), TRUE);
gtk_widget_set_events(PWidget(wMain),
GDK_EXPOSURE_MASK
| GDK_SCROLL_MASK
| maskSmooth
| GDK_STRUCTURE_MASK
| GDK_KEY_PRESS_MASK
| GDK_KEY_RELEASE_MASK
| GDK_FOCUS_CHANGE_MASK
| GDK_LEAVE_NOTIFY_MASK
| GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK
| GDK_POINTER_MOTION_MASK
| GDK_POINTER_MOTION_HINT_MASK);
wText = gtk_drawing_area_new();
gtk_widget_set_parent(PWidget(wText), PWidget(wMain));
GtkWidget *widtxt = PWidget(wText); // No code inside the G_OBJECT macro
gtk_widget_show(widtxt);
#if GTK_CHECK_VERSION(3,0,0)
g_signal_connect(G_OBJECT(widtxt), "draw",
G_CALLBACK(ScintillaGTK::DrawText), this);
#else
g_signal_connect(G_OBJECT(widtxt), "expose_event",
G_CALLBACK(ScintillaGTK::ExposeText), this);
#endif
#if GTK_CHECK_VERSION(3,0,0)
// we need a runtime check because we don't want double buffering when
// running on >= 3.9.2
if (gtk_check_version(3,9,2) != NULL /* on < 3.9.2 */)
#endif
{
#if !GTK_CHECK_VERSION(3,14,0)
// Avoid background drawing flash/missing redraws
gtk_widget_set_double_buffered(widtxt, FALSE);
#endif
}
gtk_widget_set_events(widtxt, GDK_EXPOSURE_MASK);
gtk_widget_set_size_request(widtxt, 100, 100);
adjustmentv = GTK_ADJUSTMENT(gtk_adjustment_new(0.0, 0.0, 201.0, 1.0, 20.0, 20.0));
#if GTK_CHECK_VERSION(3,0,0)
scrollbarv = gtk_scrollbar_new(GTK_ORIENTATION_VERTICAL, GTK_ADJUSTMENT(adjustmentv));
#else
scrollbarv = gtk_vscrollbar_new(GTK_ADJUSTMENT(adjustmentv));
#endif
gtk_widget_set_can_focus(PWidget(scrollbarv), FALSE);
g_signal_connect(G_OBJECT(adjustmentv), "value_changed",
G_CALLBACK(ScrollSignal), this);
gtk_widget_set_parent(PWidget(scrollbarv), PWidget(wMain));
gtk_widget_show(PWidget(scrollbarv));
adjustmenth = GTK_ADJUSTMENT(gtk_adjustment_new(0.0, 0.0, 101.0, 1.0, 20.0, 20.0));
#if GTK_CHECK_VERSION(3,0,0)
scrollbarh = gtk_scrollbar_new(GTK_ORIENTATION_HORIZONTAL, GTK_ADJUSTMENT(adjustmenth));
#else
scrollbarh = gtk_hscrollbar_new(GTK_ADJUSTMENT(adjustmenth));
#endif
gtk_widget_set_can_focus(PWidget(scrollbarh), FALSE);
g_signal_connect(G_OBJECT(adjustmenth), "value_changed",
G_CALLBACK(ScrollHSignal), this);
gtk_widget_set_parent(PWidget(scrollbarh), PWidget(wMain));
gtk_widget_show(PWidget(scrollbarh));
gtk_widget_grab_focus(PWidget(wMain));
gtk_drag_dest_set(GTK_WIDGET(PWidget(wMain)),
GTK_DEST_DEFAULT_ALL, clipboardPasteTargets, nClipboardPasteTargets,
static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE));
/* create pre-edit window */
wPreedit = gtk_window_new(GTK_WINDOW_POPUP);
wPreeditDraw = gtk_drawing_area_new();
GtkWidget *predrw = PWidget(wPreeditDraw); // No code inside the G_OBJECT macro
#if GTK_CHECK_VERSION(3,0,0)
g_signal_connect(G_OBJECT(predrw), "draw",
G_CALLBACK(DrawPreedit), this);
#else
g_signal_connect(G_OBJECT(predrw), "expose_event",
G_CALLBACK(ExposePreedit), this);
#endif
gtk_container_add(GTK_CONTAINER(PWidget(wPreedit)), predrw);
gtk_widget_show(predrw);
// Set caret period based on GTK settings
gboolean blinkOn = false;
if (g_object_class_find_property(G_OBJECT_GET_CLASS(
G_OBJECT(gtk_settings_get_default())), "gtk-cursor-blink")) {
g_object_get(G_OBJECT(
gtk_settings_get_default()), "gtk-cursor-blink", &blinkOn, NULL);
}
if (blinkOn &&
g_object_class_find_property(G_OBJECT_GET_CLASS(
G_OBJECT(gtk_settings_get_default())), "gtk-cursor-blink-time")) {
gint value;
g_object_get(G_OBJECT(
gtk_settings_get_default()), "gtk-cursor-blink-time", &value, NULL);
caret.period = gint(value / 1.75);
} else {
caret.period = 0;
}
for (TickReason tr = tickCaret; tr <= tickDwell; tr = static_cast<TickReason>(tr + 1)) {
timers[tr].reason = tr;
timers[tr].scintilla = this;
}
vs.indicators[SC_INDICATOR_UNKNOWN] = Indicator(INDIC_HIDDEN, ColourDesired(0, 0, 0xff));
vs.indicators[SC_INDICATOR_INPUT] = Indicator(INDIC_DOTS, ColourDesired(0, 0, 0xff));
vs.indicators[SC_INDICATOR_CONVERTED] = Indicator(INDIC_COMPOSITIONTHICK, ColourDesired(0, 0, 0xff));
vs.indicators[SC_INDICATOR_TARGET] = Indicator(INDIC_STRAIGHTBOX, ColourDesired(0, 0, 0xff));
}
void ScintillaGTK::Finalise() {
for (TickReason tr = tickCaret; tr <= tickDwell; tr = static_cast<TickReason>(tr + 1)) {
FineTickerCancel(tr);
}
if (accessible) {
gtk_accessible_set_widget(GTK_ACCESSIBLE(accessible), NULL);
g_object_unref(accessible);
accessible = 0;
}
ScintillaBase::Finalise();
}
bool ScintillaGTK::AbandonPaint() {
if ((paintState == painting) && !paintingAllText) {
repaintFullWindow = true;
}
return false;
}
void ScintillaGTK::DisplayCursor(Window::Cursor c) {
if (cursorMode == SC_CURSORNORMAL)
wText.SetCursor(c);
else
wText.SetCursor(static_cast<Window::Cursor>(cursorMode));
}
bool ScintillaGTK::DragThreshold(Point ptStart, Point ptNow) {
return gtk_drag_check_threshold(GTK_WIDGET(PWidget(wMain)),
ptStart.x, ptStart.y, ptNow.x, ptNow.y);
}
void ScintillaGTK::StartDrag() {
PLATFORM_ASSERT(evbtn != 0);
dragWasDropped = false;
inDragDrop = ddDragging;
GtkTargetList *tl = gtk_target_list_new(clipboardCopyTargets, nClipboardCopyTargets);
#if GTK_CHECK_VERSION(3,10,0)
gtk_drag_begin_with_coordinates(GTK_WIDGET(PWidget(wMain)),
tl,
static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE),
evbtn->button,
reinterpret_cast<GdkEvent *>(evbtn),
-1, -1);
#else
gtk_drag_begin(GTK_WIDGET(PWidget(wMain)),
tl,
static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE),
evbtn->button,
reinterpret_cast<GdkEvent *>(evbtn));
#endif
}
#ifdef SCI_NAMESPACE
namespace Scintilla {
#endif
std::string ConvertText(const char *s, size_t len, const char *charSetDest,
const char *charSetSource, bool transliterations, bool silent) {
// s is not const because of different versions of iconv disagreeing about const
std::string destForm;
Converter conv(charSetDest, charSetSource, transliterations);
if (conv) {
gsize outLeft = len*3+1;
destForm = std::string(outLeft, '\0');
// g_iconv does not actually write to its input argument so safe to cast away const
char *pin = const_cast<char *>(s);
gsize inLeft = len;
char *putf = &destForm[0];
char *pout = putf;
gsize conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
if (conversions == sizeFailure) {
if (!silent) {
if (len == 1)
fprintf(stderr, "iconv %s->%s failed for %0x '%s'\n",
charSetSource, charSetDest, (unsigned char)(*s), s);
else
fprintf(stderr, "iconv %s->%s failed for %s\n",
charSetSource, charSetDest, s);
}
destForm = std::string();
} else {
destForm.resize(pout - putf);
}
} else {
fprintf(stderr, "Can not iconv %s %s\n", charSetDest, charSetSource);
}
return destForm;
}
#ifdef SCI_NAMESPACE
}
#endif
// Returns the target converted to UTF8.
// Return the length in bytes.
int ScintillaGTK::TargetAsUTF8(char *text) {
int targetLength = targetEnd - targetStart;
if (IsUnicodeMode()) {
if (text) {
pdoc->GetCharRange(text, targetStart, targetLength);
}
} else {
// Need to convert
const char *charSetBuffer = CharacterSetID();
if (*charSetBuffer) {
std::string s = RangeText(targetStart, targetEnd);
std::string tmputf = ConvertText(&s[0], targetLength, "UTF-8", charSetBuffer, false);
if (text) {
memcpy(text, tmputf.c_str(), tmputf.length());
}
return tmputf.length();
} else {
if (text) {
pdoc->GetCharRange(text, targetStart, targetLength);
}
}
}
return targetLength;
}
// Translates a nul terminated UTF8 string into the document encoding.
// Return the length of the result in bytes.
int ScintillaGTK::EncodedFromUTF8(char *utf8, char *encoded) const {
int inputLength = (lengthForEncode >= 0) ? lengthForEncode : strlen(utf8);
if (IsUnicodeMode()) {
if (encoded) {
memcpy(encoded, utf8, inputLength);
}
return inputLength;
} else {
// Need to convert
const char *charSetBuffer = CharacterSetID();
if (*charSetBuffer) {
std::string tmpEncoded = ConvertText(utf8, inputLength, charSetBuffer, "UTF-8", true);
if (encoded) {
memcpy(encoded, tmpEncoded.c_str(), tmpEncoded.length());
}
return tmpEncoded.length();
} else {
if (encoded) {
memcpy(encoded, utf8, inputLength);
}
return inputLength;
}
}
// Fail
return 0;
}
bool ScintillaGTK::ValidCodePage(int codePage) const {
return codePage == 0
|| codePage == SC_CP_UTF8
|| codePage == 932
|| codePage == 936
|| codePage == 949
|| codePage == 950
|| codePage == 1361;
}
sptr_t ScintillaGTK::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
try {
switch (iMessage) {
case SCI_GRABFOCUS:
gtk_widget_grab_focus(PWidget(wMain));
break;
case SCI_GETDIRECTFUNCTION:
return reinterpret_cast<sptr_t>(DirectFunction);
case SCI_GETDIRECTPOINTER:
return reinterpret_cast<sptr_t>(this);
#ifdef SCI_LEXER
case SCI_LOADLEXERLIBRARY:
LexerManager::GetInstance()->Load(reinterpret_cast<const char*>(lParam));
break;
#endif
case SCI_TARGETASUTF8:
return TargetAsUTF8(reinterpret_cast<char*>(lParam));
case SCI_ENCODEDFROMUTF8:
return EncodedFromUTF8(reinterpret_cast<char*>(wParam),
reinterpret_cast<char*>(lParam));
case SCI_SETRECTANGULARSELECTIONMODIFIER:
rectangularSelectionModifier = wParam;
break;
case SCI_GETRECTANGULARSELECTIONMODIFIER:
return rectangularSelectionModifier;
case SCI_SETREADONLY: {
sptr_t ret = ScintillaBase::WndProc(iMessage, wParam, lParam);
if (accessible) {
ScintillaGTKAccessible *sciAccessible = ScintillaGTKAccessible::FromAccessible(accessible);
if (sciAccessible) {
sciAccessible->NotifyReadOnly();
}
}
return ret;
}
default:
return ScintillaBase::WndProc(iMessage, wParam, lParam);
}
} catch (std::bad_alloc&) {
errorStatus = SC_STATUS_BADALLOC;
} catch (...) {
errorStatus = SC_STATUS_FAILURE;
}
return 0l;
}
sptr_t ScintillaGTK::DefWndProc(unsigned int, uptr_t, sptr_t) {
return 0;
}
/**
* Report that this Editor subclass has a working implementation of FineTickerStart.
*/
bool ScintillaGTK::FineTickerAvailable() {
return true;
}
bool ScintillaGTK::FineTickerRunning(TickReason reason) {
return timers[reason].timer != 0;
}
void ScintillaGTK::FineTickerStart(TickReason reason, int millis, int /* tolerance */) {
FineTickerCancel(reason);
timers[reason].timer = gdk_threads_add_timeout(millis, TimeOut, &timers[reason]);
}
void ScintillaGTK::FineTickerCancel(TickReason reason) {
if (timers[reason].timer) {
g_source_remove(timers[reason].timer);
timers[reason].timer = 0;
}
}
bool ScintillaGTK::SetIdle(bool on) {
if (on) {
// Start idler, if it's not running.
if (!idler.state) {
idler.state = true;
idler.idlerID = reinterpret_cast<IdlerID>(
gdk_threads_add_idle_full(G_PRIORITY_DEFAULT_IDLE, IdleCallback, this, NULL));
}
} else {
// Stop idler, if it's running
if (idler.state) {
idler.state = false;
g_source_remove(GPOINTER_TO_UINT(idler.idlerID));
}
}
return true;
}
void ScintillaGTK::SetMouseCapture(bool on) {
if (mouseDownCaptures) {
if (on) {
gtk_grab_add(GTK_WIDGET(PWidget(wMain)));
} else {
gtk_grab_remove(GTK_WIDGET(PWidget(wMain)));
}
}
capturedMouse = on;
}
bool ScintillaGTK::HaveMouseCapture() {
return capturedMouse;
}
#if GTK_CHECK_VERSION(3,0,0)
// Is crcTest completely in crcContainer?
static bool CRectContains(const cairo_rectangle_t &crcContainer, const cairo_rectangle_t &crcTest) {
return
(crcTest.x >= crcContainer.x) && ((crcTest.x + crcTest.width) <= (crcContainer.x + crcContainer.width)) &&
(crcTest.y >= crcContainer.y) && ((crcTest.y + crcTest.height) <= (crcContainer.y + crcContainer.height));
}
// Is crcTest completely in crcListContainer?
// May incorrectly return false if complex shape
static bool CRectListContains(const cairo_rectangle_list_t *crcListContainer, const cairo_rectangle_t &crcTest) {
for (int r=0; r<crcListContainer->num_rectangles; r++) {
if (CRectContains(crcListContainer->rectangles[r], crcTest))
return true;
}
return false;
}
#endif
bool ScintillaGTK::PaintContains(PRectangle rc) {
// This allows optimization when a rectangle is completely in the update region.
// It is OK to return false when too difficult to determine as that just performs extra drawing
bool contains = true;
if (paintState == painting) {
if (!rcPaint.Contains(rc)) {
contains = false;
} else if (rgnUpdate) {
#if GTK_CHECK_VERSION(3,0,0)
cairo_rectangle_t grc = {rc.left, rc.top,
rc.right - rc.left, rc.bottom - rc.top};
contains = CRectListContains(rgnUpdate, grc);
#else
GdkRectangle grc = {static_cast<gint>(rc.left), static_cast<gint>(rc.top),
static_cast<gint>(rc.right - rc.left), static_cast<gint>(rc.bottom - rc.top)};
if (gdk_region_rect_in(rgnUpdate, &grc) != GDK_OVERLAP_RECTANGLE_IN) {
contains = false;
}
#endif
}
}
return contains;
}
// Redraw all of text area. This paint will not be abandoned.
void ScintillaGTK::FullPaint() {
wText.InvalidateAll();
}
PRectangle ScintillaGTK::GetClientRectangle() const {
Window win = wMain;
PRectangle rc = win.GetClientPosition();