Add an UI to enable/disable specific overlay handlers.
[TortoiseGit.git] / ext / scintilla / gtk / ScintillaGTK.cxx
blob9332a8fbad2bccef4f76e1ec73b1fc7c50800058
1 // Scintilla source code edit control
2 // ScintillaGTK.cxx - GTK+ specific subclass of ScintillaBase
3 // Copyright 1998-2004 by Neil Hodgson <neilh@scintilla.org>
4 // The License.txt file describes the conditions under which this software may be distributed.
6 #include <stdlib.h>
7 #include <string.h>
8 #include <stdio.h>
9 #include <ctype.h>
10 #include <time.h>
12 #include <gtk/gtk.h>
13 #include <gdk/gdkkeysyms.h>
15 #include "Platform.h"
17 #if PLAT_GTK_WIN32
18 #include "windows.h"
19 #endif
21 #include "Scintilla.h"
22 #include "ScintillaWidget.h"
23 #ifdef SCI_LEXER
24 #include "SciLexer.h"
25 #include "PropSet.h"
26 #include "Accessor.h"
27 #include "KeyWords.h"
28 #endif
29 #include "SVector.h"
30 #include "SplitVector.h"
31 #include "Partitioning.h"
32 #include "RunStyles.h"
33 #include "ContractionState.h"
34 #include "CellBuffer.h"
35 #include "CallTip.h"
36 #include "KeyMap.h"
37 #include "Indicator.h"
38 #include "XPM.h"
39 #include "LineMarker.h"
40 #include "Style.h"
41 #include "AutoComplete.h"
42 #include "ViewStyle.h"
43 #include "Decoration.h"
44 #include "CharClassify.h"
45 #include "Document.h"
46 #include "PositionCache.h"
47 #include "Editor.h"
48 #include "SString.h"
49 #include "ScintillaBase.h"
50 #include "UniConversion.h"
52 #include "gtk/gtksignal.h"
53 #include "gtk/gtkmarshal.h"
54 #if GLIB_MAJOR_VERSION >= 2
55 #include "scintilla-marshal.h"
56 #endif
58 #ifdef SCI_LEXER
59 #include <glib.h>
60 #include <gmodule.h>
61 #include "ExternalLexer.h"
62 #endif
64 #define INTERNATIONAL_INPUT
66 #if !PLAT_GTK_WIN32 || GTK_MAJOR_VERSION >= 2
67 #define USE_CONVERTER
68 #endif
70 #ifdef USE_CONVERTER
71 #include "Converter.h"
72 #endif
74 #ifdef _MSC_VER
75 // Constant conditional expressions are because of GTK+ headers
76 #pragma warning(disable: 4127)
77 // Ignore unreferenced local functions in GTK+ headers
78 #pragma warning(disable: 4505)
79 #endif
81 #if GTK_CHECK_VERSION(2,6,0)
82 #define USE_GTK_CLIPBOARD
83 #endif
85 #if GLIB_MAJOR_VERSION < 2
86 #define OBJECT_CLASS GtkObjectClass
87 #else
88 #define OBJECT_CLASS GObjectClass
89 #endif
91 extern char *UTF8FromLatin1(const char *s, int &len);
93 class ScintillaGTK : public ScintillaBase {
94 _ScintillaObject *sci;
95 Window wText;
96 Window scrollbarv;
97 Window scrollbarh;
98 GtkObject *adjustmentv;
99 GtkObject *adjustmenth;
100 int scrollBarWidth;
101 int scrollBarHeight;
103 // Because clipboard access is asynchronous, copyText is created by Copy
104 #ifndef USE_GTK_CLIPBOARD
105 SelectionText copyText;
106 #endif
108 SelectionText primary;
110 GdkEventButton evbtn;
111 bool capturedMouse;
112 bool dragWasDropped;
113 int lastKey;
115 GtkWidgetClass *parentClass;
117 static GdkAtom atomClipboard;
118 static GdkAtom atomUTF8;
119 static GdkAtom atomString;
120 static GdkAtom atomUriList;
121 static GdkAtom atomDROPFILES_DND;
122 GdkAtom atomSought;
124 #if PLAT_GTK_WIN32
125 CLIPFORMAT cfColumnSelect;
126 #endif
128 #ifdef INTERNATIONAL_INPUT
129 #if GTK_MAJOR_VERSION < 2
130 // Input context used for supporting internationalized key entry
131 GdkIC *ic;
132 GdkICAttr *ic_attr;
133 #else
134 Window wPreedit;
135 Window wPreeditDraw;
136 GtkIMContext *im_context;
137 #endif
138 #endif
140 // Wheel mouse support
141 unsigned int linesPerScroll;
142 GTimeVal lastWheelMouseTime;
143 gint lastWheelMouseDirection;
144 gint wheelMouseIntensity;
146 GdkRegion *rgnUpdate;
148 // Private so ScintillaGTK objects can not be copied
149 ScintillaGTK(const ScintillaGTK &) : ScintillaBase() {}
150 ScintillaGTK &operator=(const ScintillaGTK &) { return * this; }
152 public:
153 ScintillaGTK(_ScintillaObject *sci_);
154 virtual ~ScintillaGTK();
155 static void ClassInit(OBJECT_CLASS* object_class, GtkWidgetClass *widget_class, GtkContainerClass *container_class);
156 private:
157 virtual void Initialise();
158 virtual void Finalise();
159 virtual void DisplayCursor(Window::Cursor c);
160 virtual bool DragThreshold(Point ptStart, Point ptNow);
161 virtual void StartDrag();
162 int TargetAsUTF8(char *text);
163 int EncodedFromUTF8(char *utf8, char *encoded);
164 virtual bool ValidCodePage(int codePage) const;
165 public: // Public for scintilla_send_message
166 virtual sptr_t WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
167 private:
168 virtual sptr_t DefWndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
169 virtual void SetTicking(bool on);
170 virtual bool SetIdle(bool on);
171 virtual void SetMouseCapture(bool on);
172 virtual bool HaveMouseCapture();
173 virtual bool PaintContains(PRectangle rc);
174 void FullPaint();
175 virtual PRectangle GetClientRectangle();
176 void SyncPaint(PRectangle rc);
177 virtual void ScrollText(int linesToMove);
178 virtual void SetVerticalScrollPos();
179 virtual void SetHorizontalScrollPos();
180 virtual bool ModifyScrollBars(int nMax, int nPage);
181 void ReconfigureScrollBars();
182 virtual void NotifyChange();
183 virtual void NotifyFocus(bool focus);
184 virtual void NotifyParent(SCNotification scn);
185 void NotifyKey(int key, int modifiers);
186 void NotifyURIDropped(const char *list);
187 const char *CharacterSetID() const;
188 virtual int KeyDefault(int key, int modifiers);
189 virtual void CopyToClipboard(const SelectionText &selectedText);
190 virtual void Copy();
191 virtual void Paste();
192 virtual void CreateCallTipWindow(PRectangle rc);
193 virtual void AddToPopUp(const char *label, int cmd = 0, bool enabled = true);
194 bool OwnPrimarySelection();
195 virtual void ClaimSelection();
196 void GetGtkSelectionText(GtkSelectionData *selectionData, SelectionText &selText);
197 void ReceivedSelection(GtkSelectionData *selection_data);
198 void ReceivedDrop(GtkSelectionData *selection_data);
199 static void GetSelection(GtkSelectionData *selection_data, guint info, SelectionText *selected);
200 #ifdef USE_GTK_CLIPBOARD
201 void StoreOnClipboard(SelectionText *clipText);
202 static void ClipboardGetSelection(GtkClipboard* clip, GtkSelectionData *selection_data, guint info, void *data);
203 static void ClipboardClearSelection(GtkClipboard* clip, void *data);
204 #endif
206 void UnclaimSelection(GdkEventSelection *selection_event);
207 void Resize(int width, int height);
209 // Callback functions
210 void RealizeThis(GtkWidget *widget);
211 static void Realize(GtkWidget *widget);
212 void UnRealizeThis(GtkWidget *widget);
213 static void UnRealize(GtkWidget *widget);
214 void MapThis();
215 static void Map(GtkWidget *widget);
216 void UnMapThis();
217 static void UnMap(GtkWidget *widget);
218 static gint CursorMoved(GtkWidget *widget, int xoffset, int yoffset, ScintillaGTK *sciThis);
219 static gint FocusIn(GtkWidget *widget, GdkEventFocus *event);
220 static gint FocusOut(GtkWidget *widget, GdkEventFocus *event);
221 static void SizeRequest(GtkWidget *widget, GtkRequisition *requisition);
222 static void SizeAllocate(GtkWidget *widget, GtkAllocation *allocation);
223 gint Expose(GtkWidget *widget, GdkEventExpose *ose);
224 static gint ExposeMain(GtkWidget *widget, GdkEventExpose *ose);
225 static void Draw(GtkWidget *widget, GdkRectangle *area);
226 void ForAll(GtkCallback callback, gpointer callback_data);
227 static void MainForAll(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data);
229 static void ScrollSignal(GtkAdjustment *adj, ScintillaGTK *sciThis);
230 static void ScrollHSignal(GtkAdjustment *adj, ScintillaGTK *sciThis);
231 gint PressThis(GdkEventButton *event);
232 static gint Press(GtkWidget *widget, GdkEventButton *event);
233 static gint MouseRelease(GtkWidget *widget, GdkEventButton *event);
234 #if PLAT_GTK_WIN32 || (GTK_MAJOR_VERSION >= 2)
235 static gint ScrollEvent(GtkWidget *widget, GdkEventScroll *event);
236 #endif
237 static gint Motion(GtkWidget *widget, GdkEventMotion *event);
238 gboolean KeyThis(GdkEventKey *event);
239 static gboolean KeyPress(GtkWidget *widget, GdkEventKey *event);
240 static gboolean KeyRelease(GtkWidget *widget, GdkEventKey *event);
241 #if GTK_MAJOR_VERSION >= 2
242 static gboolean ExposePreedit(GtkWidget *widget, GdkEventExpose *ose, ScintillaGTK *sciThis);
243 gboolean ExposePreeditThis(GtkWidget *widget, GdkEventExpose *ose);
244 static void Commit(GtkIMContext *context, char *str, ScintillaGTK *sciThis);
245 void CommitThis(char *str);
246 static void PreeditChanged(GtkIMContext *context, ScintillaGTK *sciThis);
247 void PreeditChangedThis();
248 #endif
249 static gint StyleSetText(GtkWidget *widget, GtkStyle *previous, void*);
250 static gint RealizeText(GtkWidget *widget, void*);
251 #if GLIB_MAJOR_VERSION < 2
252 static void Destroy(GtkObject *object);
253 #else
254 static void Destroy(GObject *object);
255 #endif
256 static void SelectionReceived(GtkWidget *widget, GtkSelectionData *selection_data,
257 guint time);
258 static void SelectionGet(GtkWidget *widget, GtkSelectionData *selection_data,
259 guint info, guint time);
260 static gint SelectionClear(GtkWidget *widget, GdkEventSelection *selection_event);
261 #if GTK_MAJOR_VERSION < 2
262 static gint SelectionNotify(GtkWidget *widget, GdkEventSelection *selection_event);
263 #endif
264 static void DragBegin(GtkWidget *widget, GdkDragContext *context);
265 gboolean DragMotionThis(GdkDragContext *context, gint x, gint y, guint dragtime);
266 static gboolean DragMotion(GtkWidget *widget, GdkDragContext *context,
267 gint x, gint y, guint dragtime);
268 static void DragLeave(GtkWidget *widget, GdkDragContext *context,
269 guint time);
270 static void DragEnd(GtkWidget *widget, GdkDragContext *context);
271 static gboolean Drop(GtkWidget *widget, GdkDragContext *context,
272 gint x, gint y, guint time);
273 static void DragDataReceived(GtkWidget *widget, GdkDragContext *context,
274 gint x, gint y, GtkSelectionData *selection_data, guint info, guint time);
275 static void DragDataGet(GtkWidget *widget, GdkDragContext *context,
276 GtkSelectionData *selection_data, guint info, guint time);
277 static gint TimeOut(ScintillaGTK *sciThis);
278 static gint IdleCallback(ScintillaGTK *sciThis);
279 static void PopUpCB(ScintillaGTK *sciThis, guint action, GtkWidget *widget);
281 gint ExposeTextThis(GtkWidget *widget, GdkEventExpose *ose);
282 static gint ExposeText(GtkWidget *widget, GdkEventExpose *ose, ScintillaGTK *sciThis);
284 static gint ExposeCT(GtkWidget *widget, GdkEventExpose *ose, CallTip *ct);
285 static gint PressCT(GtkWidget *widget, GdkEventButton *event, ScintillaGTK *sciThis);
287 static sptr_t DirectFunction(ScintillaGTK *sciThis,
288 unsigned int iMessage, uptr_t wParam, sptr_t lParam);
291 enum {
292 COMMAND_SIGNAL,
293 NOTIFY_SIGNAL,
294 LAST_SIGNAL
297 static gint scintilla_signals[LAST_SIGNAL] = { 0 };
298 #if GLIB_MAJOR_VERSION < 2
299 static GtkWidgetClass *parent_class = NULL;
300 #endif
302 enum {
303 TARGET_STRING,
304 TARGET_TEXT,
305 TARGET_COMPOUND_TEXT,
306 TARGET_UTF8_STRING,
307 TARGET_URI
310 GdkAtom ScintillaGTK::atomClipboard = 0;
311 GdkAtom ScintillaGTK::atomUTF8 = 0;
312 GdkAtom ScintillaGTK::atomString = 0;
313 GdkAtom ScintillaGTK::atomUriList = 0;
314 GdkAtom ScintillaGTK::atomDROPFILES_DND = 0;
316 static const GtkTargetEntry clipboardCopyTargets[] = {
317 { (gchar *) "UTF8_STRING", 0, TARGET_UTF8_STRING },
318 { (gchar *) "STRING", 0, TARGET_STRING },
320 static const gint nClipboardCopyTargets = sizeof(clipboardCopyTargets) / sizeof(clipboardCopyTargets[0]);
322 static const GtkTargetEntry clipboardPasteTargets[] = {
323 { (gchar *) "text/uri-list", 0, TARGET_URI },
324 { (gchar *) "UTF8_STRING", 0, TARGET_UTF8_STRING },
325 { (gchar *) "STRING", 0, TARGET_STRING },
327 static const gint nClipboardPasteTargets = sizeof(clipboardPasteTargets) / sizeof(clipboardPasteTargets[0]);
329 static GtkWidget *PWidget(Window &w) {
330 return reinterpret_cast<GtkWidget *>(w.GetID());
333 static ScintillaGTK *ScintillaFromWidget(GtkWidget *widget) {
334 ScintillaObject *scio = reinterpret_cast<ScintillaObject *>(widget);
335 return reinterpret_cast<ScintillaGTK *>(scio->pscin);
338 ScintillaGTK::ScintillaGTK(_ScintillaObject *sci_) :
339 adjustmentv(0), adjustmenth(0),
340 scrollBarWidth(30), scrollBarHeight(30),
341 capturedMouse(false), dragWasDropped(false),
342 lastKey(0), parentClass(0),
343 #ifdef INTERNATIONAL_INPUT
344 #if GTK_MAJOR_VERSION < 2
345 ic(NULL),
346 ic_attr(NULL),
347 #else
348 im_context(NULL),
349 #endif
350 #endif
351 lastWheelMouseDirection(0),
352 wheelMouseIntensity(0),
353 rgnUpdate(0) {
354 sci = sci_;
355 wMain = GTK_WIDGET(sci);
357 #if PLAT_GTK_WIN32
358 // There does not seem to be a real standard for indicating that the clipboard
359 // contains a rectangular selection, so copy Developer Studio.
360 cfColumnSelect = static_cast<CLIPFORMAT>(
361 ::RegisterClipboardFormat("MSDEVColumnSelect"));
363 // Get intellimouse parameters when running on win32; otherwise use
364 // reasonable default
365 #ifndef SPI_GETWHEELSCROLLLINES
366 #define SPI_GETWHEELSCROLLLINES 104
367 #endif
368 ::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &linesPerScroll, 0);
369 #else
370 linesPerScroll = 4;
371 #endif
372 lastWheelMouseTime.tv_sec = 0;
373 lastWheelMouseTime.tv_usec = 0;
375 Initialise();
378 ScintillaGTK::~ScintillaGTK() {
381 void ScintillaGTK::RealizeThis(GtkWidget *widget) {
382 //Platform::DebugPrintf("ScintillaGTK::realize this\n");
383 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
384 GdkWindowAttr attrs;
385 attrs.window_type = GDK_WINDOW_CHILD;
386 attrs.x = widget->allocation.x;
387 attrs.y = widget->allocation.y;
388 attrs.width = widget->allocation.width;
389 attrs.height = widget->allocation.height;
390 attrs.wclass = GDK_INPUT_OUTPUT;
391 attrs.visual = gtk_widget_get_visual(widget);
392 attrs.colormap = gtk_widget_get_colormap(widget);
393 attrs.event_mask = gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK;
394 GdkCursor *cursor = gdk_cursor_new(GDK_XTERM);
395 attrs.cursor = cursor;
396 widget->window = gdk_window_new(gtk_widget_get_parent_window(widget), &attrs,
397 GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP | GDK_WA_CURSOR);
398 gdk_window_set_user_data(widget->window, widget);
399 gdk_window_set_background(widget->window, &widget->style->bg[GTK_STATE_NORMAL]);
400 gdk_window_show(widget->window);
401 gdk_cursor_destroy(cursor);
402 widget->style = gtk_style_attach(widget->style, widget->window);
403 #ifdef INTERNATIONAL_INPUT
404 #if GTK_MAJOR_VERSION < 2
405 if (gdk_im_ready() && (ic_attr = gdk_ic_attr_new()) != NULL) {
406 gint width, height;
407 GdkColormap *colormap;
408 GdkEventMask mask;
409 GdkICAttr *attr = ic_attr;
410 GdkICAttributesType attrmask = GDK_IC_ALL_REQ;
411 GdkIMStyle style;
412 GdkIMStyle supported_style = (GdkIMStyle) (GDK_IM_PREEDIT_NONE |
413 GDK_IM_PREEDIT_NOTHING |
414 GDK_IM_PREEDIT_POSITION |
415 GDK_IM_STATUS_NONE |
416 GDK_IM_STATUS_NOTHING);
418 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET)
419 supported_style = (GdkIMStyle) ((int) supported_style & ~GDK_IM_PREEDIT_POSITION);
421 attr->style = style = gdk_im_decide_style(supported_style);
422 attr->client_window = widget->window;
424 if ((colormap = gtk_widget_get_colormap (widget)) != gtk_widget_get_default_colormap ()) {
425 attrmask = (GdkICAttributesType) ((int) attrmask | GDK_IC_PREEDIT_COLORMAP);
426 attr->preedit_colormap = colormap;
429 switch (style & GDK_IM_PREEDIT_MASK) {
430 case GDK_IM_PREEDIT_POSITION:
431 if (widget->style && widget->style->font->type != GDK_FONT_FONTSET) {
432 g_warning("over-the-spot style requires fontset");
433 break;
436 attrmask = (GdkICAttributesType) ((int) attrmask | GDK_IC_PREEDIT_POSITION_REQ);
437 gdk_window_get_size(widget->window, &width, &height);
438 attr->spot_location.x = 0;
439 attr->spot_location.y = height;
440 attr->preedit_area.x = 0;
441 attr->preedit_area.y = 0;
442 attr->preedit_area.width = width;
443 attr->preedit_area.height = height;
444 attr->preedit_fontset = widget->style->font;
446 break;
448 ic = gdk_ic_new(attr, attrmask);
450 if (ic == NULL) {
451 g_warning("Can't create input context.");
452 } else {
453 mask = gdk_window_get_events(widget->window);
454 mask = (GdkEventMask) ((int) mask | gdk_ic_get_events(ic));
455 gdk_window_set_events(widget->window, mask);
457 if (GTK_WIDGET_HAS_FOCUS(widget))
458 gdk_im_begin(ic, widget->window);
461 #else
462 wPreedit = gtk_window_new(GTK_WINDOW_POPUP);
463 wPreeditDraw = gtk_drawing_area_new();
464 GtkWidget *predrw = PWidget(wPreeditDraw); // No code inside the G_OBJECT macro
465 g_signal_connect(G_OBJECT(predrw), "expose_event",
466 G_CALLBACK(ExposePreedit), this);
467 gtk_container_add(GTK_CONTAINER(PWidget(wPreedit)), predrw);
468 gtk_widget_realize(PWidget(wPreedit));
469 gtk_widget_realize(predrw);
470 gtk_widget_show(predrw);
472 im_context = gtk_im_multicontext_new();
473 g_signal_connect(G_OBJECT(im_context), "commit",
474 G_CALLBACK(Commit), this);
475 g_signal_connect(G_OBJECT(im_context), "preedit_changed",
476 G_CALLBACK(PreeditChanged), this);
477 gtk_im_context_set_client_window(im_context, widget->window);
478 #endif
479 #endif
480 GtkWidget *widtxt = PWidget(wText); // // No code inside the G_OBJECT macro
481 #if GLIB_MAJOR_VERSION < 2
482 gtk_signal_connect_after(GTK_OBJECT(widtxt), "style_set",
483 GtkSignalFunc(ScintillaGTK::StyleSetText), NULL);
484 gtk_signal_connect_after(GTK_OBJECT(widtxt), "realize",
485 GtkSignalFunc(ScintillaGTK::RealizeText), NULL);
486 #else
487 g_signal_connect_after(G_OBJECT(widtxt), "style_set",
488 G_CALLBACK(ScintillaGTK::StyleSetText), NULL);
489 g_signal_connect_after(G_OBJECT(widtxt), "realize",
490 G_CALLBACK(ScintillaGTK::RealizeText), NULL);
491 #endif
492 gtk_widget_realize(widtxt);
493 gtk_widget_realize(PWidget(scrollbarv));
494 gtk_widget_realize(PWidget(scrollbarh));
497 void ScintillaGTK::Realize(GtkWidget *widget) {
498 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
499 sciThis->RealizeThis(widget);
502 void ScintillaGTK::UnRealizeThis(GtkWidget *widget) {
503 if (GTK_WIDGET_MAPPED(widget)) {
504 gtk_widget_unmap(widget);
506 GTK_WIDGET_UNSET_FLAGS(widget, GTK_REALIZED);
507 gtk_widget_unrealize(PWidget(wText));
508 gtk_widget_unrealize(PWidget(scrollbarv));
509 gtk_widget_unrealize(PWidget(scrollbarh));
510 #ifdef INTERNATIONAL_INPUT
511 #if GTK_MAJOR_VERSION < 2
512 if (ic) {
513 gdk_ic_destroy(ic);
514 ic = NULL;
516 if (ic_attr) {
517 gdk_ic_attr_destroy(ic_attr);
518 ic_attr = NULL;
520 #else
521 gtk_widget_unrealize(PWidget(wPreedit));
522 gtk_widget_unrealize(PWidget(wPreeditDraw));
523 g_object_unref(im_context);
524 im_context = NULL;
525 #endif
526 #endif
527 if (GTK_WIDGET_CLASS(parentClass)->unrealize)
528 GTK_WIDGET_CLASS(parentClass)->unrealize(widget);
530 Finalise();
533 void ScintillaGTK::UnRealize(GtkWidget *widget) {
534 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
535 sciThis->UnRealizeThis(widget);
538 static void MapWidget(GtkWidget *widget) {
539 if (widget &&
540 GTK_WIDGET_VISIBLE(widget) &&
541 !GTK_WIDGET_MAPPED(widget)) {
542 gtk_widget_map(widget);
546 void ScintillaGTK::MapThis() {
547 //Platform::DebugPrintf("ScintillaGTK::map this\n");
548 GTK_WIDGET_SET_FLAGS(PWidget(wMain), GTK_MAPPED);
549 MapWidget(PWidget(wText));
550 MapWidget(PWidget(scrollbarh));
551 MapWidget(PWidget(scrollbarv));
552 wMain.SetCursor(Window::cursorArrow);
553 scrollbarv.SetCursor(Window::cursorArrow);
554 scrollbarh.SetCursor(Window::cursorArrow);
555 ChangeSize();
556 gdk_window_show(PWidget(wMain)->window);
559 void ScintillaGTK::Map(GtkWidget *widget) {
560 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
561 sciThis->MapThis();
564 void ScintillaGTK::UnMapThis() {
565 //Platform::DebugPrintf("ScintillaGTK::unmap this\n");
566 GTK_WIDGET_UNSET_FLAGS(PWidget(wMain), GTK_MAPPED);
567 DropGraphics();
568 gdk_window_hide(PWidget(wMain)->window);
569 gtk_widget_unmap(PWidget(wText));
570 gtk_widget_unmap(PWidget(scrollbarh));
571 gtk_widget_unmap(PWidget(scrollbarv));
574 void ScintillaGTK::UnMap(GtkWidget *widget) {
575 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
576 sciThis->UnMapThis();
579 void ScintillaGTK::ForAll(GtkCallback callback, gpointer callback_data) {
580 (*callback) (PWidget(wText), callback_data);
581 (*callback) (PWidget(scrollbarv), callback_data);
582 (*callback) (PWidget(scrollbarh), callback_data);
585 void ScintillaGTK::MainForAll(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data) {
586 ScintillaGTK *sciThis = ScintillaFromWidget((GtkWidget *)container);
588 if (callback != NULL && include_internals) {
589 sciThis->ForAll(callback, callback_data);
593 #ifdef INTERNATIONAL_INPUT
594 #if GTK_MAJOR_VERSION < 2
595 gint ScintillaGTK::CursorMoved(GtkWidget *widget, int xoffset, int yoffset, ScintillaGTK *sciThis) {
596 if (GTK_WIDGET_HAS_FOCUS(widget) && gdk_im_ready() && sciThis->ic &&
597 (gdk_ic_get_style(sciThis->ic) & GDK_IM_PREEDIT_POSITION)) {
598 sciThis->ic_attr->spot_location.x = xoffset;
599 sciThis->ic_attr->spot_location.y = yoffset;
600 gdk_ic_set_attr(sciThis->ic, sciThis->ic_attr, GDK_IC_SPOT_LOCATION);
602 return FALSE;
604 #else
605 gint ScintillaGTK::CursorMoved(GtkWidget *, int xoffset, int yoffset, ScintillaGTK *sciThis) {
606 GdkRectangle area;
607 area.x = xoffset;
608 area.y = yoffset;
609 area.width = 1;
610 area.height = 1;
611 gtk_im_context_set_cursor_location(sciThis->im_context, &area);
612 return FALSE;
614 #endif
615 #else
616 gint ScintillaGTK::CursorMoved(GtkWidget *, int, int, ScintillaGTK *) {
617 return FALSE;
619 #endif
621 gint ScintillaGTK::FocusIn(GtkWidget *widget, GdkEventFocus * /*event*/) {
622 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
623 //Platform::DebugPrintf("ScintillaGTK::focus in %x\n", sciThis);
624 GTK_WIDGET_SET_FLAGS(widget, GTK_HAS_FOCUS);
625 sciThis->SetFocusState(true);
627 #ifdef INTERNATIONAL_INPUT
628 #if GTK_MAJOR_VERSION < 2
629 if (sciThis->ic)
630 gdk_im_begin(sciThis->ic, widget->window);
631 #else
632 if (sciThis->im_context != NULL) {
633 gchar *str = NULL;
634 gint cursor_pos;
636 gtk_im_context_get_preedit_string(sciThis->im_context, &str, NULL, &cursor_pos);
637 if (PWidget(sciThis->wPreedit) != NULL) {
638 if (strlen(str) > 0) {
639 gtk_widget_show(PWidget(sciThis->wPreedit));
640 } else {
641 gtk_widget_hide(PWidget(sciThis->wPreedit));
644 g_free(str);
645 gtk_im_context_focus_in(sciThis->im_context);
647 #endif
648 #endif
650 return FALSE;
653 gint ScintillaGTK::FocusOut(GtkWidget *widget, GdkEventFocus * /*event*/) {
654 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
655 //Platform::DebugPrintf("ScintillaGTK::focus out %x\n", sciThis);
656 GTK_WIDGET_UNSET_FLAGS(widget, GTK_HAS_FOCUS);
657 sciThis->SetFocusState(false);
659 #ifdef INTERNATIONAL_INPUT
660 #if GTK_MAJOR_VERSION < 2
661 gdk_im_end();
662 #else
663 if (PWidget(sciThis->wPreedit) != NULL)
664 gtk_widget_hide(PWidget(sciThis->wPreedit));
665 if (sciThis->im_context != NULL)
666 gtk_im_context_focus_out(sciThis->im_context);
667 #endif
668 #endif
670 return FALSE;
673 void ScintillaGTK::SizeRequest(GtkWidget *widget, GtkRequisition *requisition) {
674 requisition->width = 600;
675 requisition->height = gdk_screen_height();
676 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
677 GtkRequisition child_requisition;
678 gtk_widget_size_request(PWidget(sciThis->scrollbarh), &child_requisition);
679 gtk_widget_size_request(PWidget(sciThis->scrollbarv), &child_requisition);
682 void ScintillaGTK::SizeAllocate(GtkWidget *widget, GtkAllocation *allocation) {
683 widget->allocation = *allocation;
684 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
685 if (GTK_WIDGET_REALIZED(widget))
686 gdk_window_move_resize(widget->window,
687 widget->allocation.x,
688 widget->allocation.y,
689 widget->allocation.width,
690 widget->allocation.height);
692 sciThis->Resize(allocation->width, allocation->height);
694 #ifdef INTERNATIONAL_INPUT
695 #if GTK_MAJOR_VERSION < 2
696 if (sciThis->ic && (gdk_ic_get_style(sciThis->ic) & GDK_IM_PREEDIT_POSITION)) {
697 gint width, height;
699 gdk_window_get_size(widget->window, &width, &height);
700 sciThis->ic_attr->preedit_area.width = width;
701 sciThis->ic_attr->preedit_area.height = height;
703 gdk_ic_set_attr(sciThis->ic, sciThis->ic_attr, GDK_IC_PREEDIT_AREA);
705 #endif
706 #endif
709 void ScintillaGTK::Initialise() {
710 //Platform::DebugPrintf("ScintillaGTK::Initialise\n");
711 parentClass = reinterpret_cast<GtkWidgetClass *>(
712 gtk_type_class(gtk_container_get_type()));
714 GTK_WIDGET_SET_FLAGS(PWidget(wMain), GTK_CAN_FOCUS);
715 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(PWidget(wMain)), GTK_SENSITIVE);
716 gtk_widget_set_events(PWidget(wMain),
717 GDK_EXPOSURE_MASK
718 | GDK_STRUCTURE_MASK
719 | GDK_KEY_PRESS_MASK
720 | GDK_KEY_RELEASE_MASK
721 | GDK_FOCUS_CHANGE_MASK
722 | GDK_LEAVE_NOTIFY_MASK
723 | GDK_BUTTON_PRESS_MASK
724 | GDK_BUTTON_RELEASE_MASK
725 | GDK_POINTER_MOTION_MASK
726 | GDK_POINTER_MOTION_HINT_MASK);
728 wText = gtk_drawing_area_new();
729 gtk_widget_set_parent(PWidget(wText), PWidget(wMain));
730 GtkWidget *widtxt = PWidget(wText); // No code inside the G_OBJECT macro
731 gtk_widget_show(widtxt);
732 #if GLIB_MAJOR_VERSION < 2
733 gtk_signal_connect(GTK_OBJECT(widtxt), "expose_event",
734 GtkSignalFunc(ScintillaGTK::ExposeText), this);
735 #else
736 g_signal_connect(G_OBJECT(widtxt), "expose_event",
737 G_CALLBACK(ScintillaGTK::ExposeText), this);
738 #endif
739 gtk_widget_set_events(widtxt, GDK_EXPOSURE_MASK);
740 #if GTK_MAJOR_VERSION >= 2
741 // Avoid background drawing flash
742 gtk_widget_set_double_buffered(widtxt, FALSE);
743 #endif
744 gtk_drawing_area_size(GTK_DRAWING_AREA(widtxt),
745 100,100);
746 adjustmentv = gtk_adjustment_new(0.0, 0.0, 201.0, 1.0, 20.0, 20.0);
747 scrollbarv = gtk_vscrollbar_new(GTK_ADJUSTMENT(adjustmentv));
748 GTK_WIDGET_UNSET_FLAGS(PWidget(scrollbarv), GTK_CAN_FOCUS);
749 #if GLIB_MAJOR_VERSION < 2
750 gtk_signal_connect(adjustmentv, "value_changed",
751 GtkSignalFunc(ScrollSignal), this);
752 #else
753 g_signal_connect(G_OBJECT(adjustmentv), "value_changed",
754 G_CALLBACK(ScrollSignal), this);
755 #endif
756 gtk_widget_set_parent(PWidget(scrollbarv), PWidget(wMain));
757 gtk_widget_show(PWidget(scrollbarv));
759 adjustmenth = gtk_adjustment_new(0.0, 0.0, 101.0, 1.0, 20.0, 20.0);
760 scrollbarh = gtk_hscrollbar_new(GTK_ADJUSTMENT(adjustmenth));
761 GTK_WIDGET_UNSET_FLAGS(PWidget(scrollbarh), GTK_CAN_FOCUS);
762 #if GLIB_MAJOR_VERSION < 2
763 gtk_signal_connect(adjustmenth, "value_changed",
764 GtkSignalFunc(ScrollHSignal), this);
765 #else
766 g_signal_connect(G_OBJECT(adjustmenth), "value_changed",
767 G_CALLBACK(ScrollHSignal), this);
768 #endif
769 gtk_widget_set_parent(PWidget(scrollbarh), PWidget(wMain));
770 gtk_widget_show(PWidget(scrollbarh));
772 gtk_widget_grab_focus(PWidget(wMain));
774 gtk_selection_add_targets(GTK_WIDGET(PWidget(wMain)), GDK_SELECTION_PRIMARY,
775 clipboardCopyTargets, nClipboardCopyTargets);
777 #ifndef USE_GTK_CLIPBOARD
778 gtk_selection_add_targets(GTK_WIDGET(PWidget(wMain)), atomClipboard,
779 clipboardPasteTargets, nClipboardPasteTargets);
780 #endif
782 gtk_drag_dest_set(GTK_WIDGET(PWidget(wMain)),
783 GTK_DEST_DEFAULT_ALL, clipboardPasteTargets, nClipboardPasteTargets,
784 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE));
786 #if GLIB_MAJOR_VERSION >= 2
787 // Set caret period based on GTK settings
788 gboolean blinkOn = false;
789 if (g_object_class_find_property(G_OBJECT_GET_CLASS(
790 G_OBJECT(gtk_settings_get_default())), "gtk-cursor-blink")) {
791 g_object_get(G_OBJECT(
792 gtk_settings_get_default()), "gtk-cursor-blink", &blinkOn, NULL);
794 if (blinkOn &&
795 g_object_class_find_property(G_OBJECT_GET_CLASS(
796 G_OBJECT(gtk_settings_get_default())), "gtk-cursor-blink-time")) {
797 gint value;
798 g_object_get(G_OBJECT(
799 gtk_settings_get_default()), "gtk-cursor-blink-time", &value, NULL);
800 caret.period = gint(value / 1.75);
801 } else {
802 caret.period = 0;
804 #endif
806 SetTicking(true);
809 void ScintillaGTK::Finalise() {
810 SetTicking(false);
811 ScintillaBase::Finalise();
814 void ScintillaGTK::DisplayCursor(Window::Cursor c) {
815 if (cursorMode == SC_CURSORNORMAL)
816 wText.SetCursor(c);
817 else
818 wText.SetCursor(static_cast<Window::Cursor>(cursorMode));
821 bool ScintillaGTK::DragThreshold(Point ptStart, Point ptNow) {
822 #if GTK_MAJOR_VERSION < 2
823 return Editor::DragThreshold(ptStart, ptNow);
824 #else
825 return gtk_drag_check_threshold(GTK_WIDGET(PWidget(wMain)),
826 ptStart.x, ptStart.y, ptNow.x, ptNow.y);
827 #endif
830 void ScintillaGTK::StartDrag() {
831 dragWasDropped = false;
832 inDragDrop = ddDragging;
833 GtkTargetList *tl = gtk_target_list_new(clipboardCopyTargets, nClipboardCopyTargets);
834 gtk_drag_begin(GTK_WIDGET(PWidget(wMain)),
836 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE),
837 evbtn.button,
838 reinterpret_cast<GdkEvent *>(&evbtn));
841 #ifdef USE_CONVERTER
842 static char *ConvertText(int *lenResult, char *s, size_t len, const char *charSetDest,
843 const char *charSetSource, bool transliterations) {
844 *lenResult = 0;
845 char *destForm = 0;
846 Converter conv(charSetDest, charSetSource, transliterations);
847 if (conv) {
848 destForm = new char[len*3+1];
849 char *pin = s;
850 size_t inLeft = len;
851 char *pout = destForm;
852 size_t outLeft = len*3+1;
853 size_t conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
854 if (conversions == ((size_t)(-1))) {
855 fprintf(stderr, "iconv %s->%s failed for %s\n", charSetSource, charSetDest, static_cast<char *>(s));
856 delete []destForm;
857 destForm = 0;
858 } else {
859 //fprintf(stderr, "iconv OK %s %d\n", destForm, pout - destForm);
860 *pout = '\0';
861 *lenResult = pout - destForm;
863 } else {
864 fprintf(stderr, "Can not iconv %s %s\n", charSetDest, charSetSource);
866 if (!destForm) {
867 destForm = new char[1];
868 destForm[0] = '\0';
869 *lenResult = 0;
871 return destForm;
873 #endif
875 // Returns the target converted to UTF8.
876 // Return the length in bytes.
877 int ScintillaGTK::TargetAsUTF8(char *text) {
878 int targetLength = targetEnd - targetStart;
879 if (IsUnicodeMode()) {
880 if (text) {
881 pdoc->GetCharRange(text, targetStart, targetLength);
883 } else {
884 // Need to convert
885 #ifdef USE_CONVERTER
886 const char *charSetBuffer = CharacterSetID();
887 if (*charSetBuffer) {
888 //~ fprintf(stderr, "AsUTF8 %s %d %0d-%0d\n", charSetBuffer, targetLength, targetStart, targetEnd);
889 char *s = new char[targetLength];
890 if (s) {
891 pdoc->GetCharRange(s, targetStart, targetLength);
892 //~ fprintf(stderr, " \"%s\"\n", s);
893 if (text) {
894 char *tmputf = ConvertText(&targetLength, s, targetLength, "UTF-8", charSetBuffer, false);
895 memcpy(text, tmputf, targetLength);
896 delete []tmputf;
897 //~ fprintf(stderr, " \"%s\"\n", text);
899 delete []s;
901 } else {
902 if (text) {
903 pdoc->GetCharRange(text, targetStart, targetLength);
906 #else
907 // Fail
908 return 0;
909 #endif
911 //~ fprintf(stderr, "Length = %d bytes\n", targetLength);
912 return targetLength;
915 // Translates a nul terminated UTF8 string into the document encoding.
916 // Return the length of the result in bytes.
917 int ScintillaGTK::EncodedFromUTF8(char *utf8, char *encoded) {
918 int inputLength = (lengthForEncode >= 0) ? lengthForEncode : strlen(utf8);
919 if (IsUnicodeMode()) {
920 if (encoded) {
921 memcpy(encoded, utf8, inputLength);
923 return inputLength;
924 } else {
925 // Need to convert
926 #ifdef USE_CONVERTER
927 const char *charSetBuffer = CharacterSetID();
928 if (*charSetBuffer) {
929 //~ fprintf(stderr, "Encode %s %d\n", charSetBuffer, inputLength);
930 int outLength = 0;
931 char *tmpEncoded = ConvertText(&outLength, utf8, inputLength, charSetBuffer, "UTF-8", true);
932 if (tmpEncoded) {
933 //~ fprintf(stderr, " \"%s\"\n", tmpEncoded);
934 if (encoded) {
935 memcpy(encoded, tmpEncoded, outLength);
937 delete []tmpEncoded;
939 return outLength;
940 } else {
941 if (encoded) {
942 memcpy(encoded, utf8, inputLength);
944 return inputLength;
946 #endif
948 // Fail
949 return 0;
952 bool ScintillaGTK::ValidCodePage(int codePage) const {
953 return codePage == 0 || codePage == SC_CP_UTF8 || codePage == SC_CP_DBCS;
956 sptr_t ScintillaGTK::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
957 switch (iMessage) {
959 case SCI_GRABFOCUS:
960 gtk_widget_grab_focus(PWidget(wMain));
961 break;
963 case SCI_GETDIRECTFUNCTION:
964 return reinterpret_cast<sptr_t>(DirectFunction);
966 case SCI_GETDIRECTPOINTER:
967 return reinterpret_cast<sptr_t>(this);
969 #ifdef SCI_LEXER
970 case SCI_LOADLEXERLIBRARY:
971 LexerManager::GetInstance()->Load(reinterpret_cast<const char*>(wParam));
972 break;
973 #endif
974 case SCI_TARGETASUTF8:
975 return TargetAsUTF8(reinterpret_cast<char*>(lParam));
977 case SCI_ENCODEDFROMUTF8:
978 return EncodedFromUTF8(reinterpret_cast<char*>(wParam),
979 reinterpret_cast<char*>(lParam));
981 default:
982 return ScintillaBase::WndProc(iMessage, wParam, lParam);
984 return 0l;
987 sptr_t ScintillaGTK::DefWndProc(unsigned int, uptr_t, sptr_t) {
988 return 0;
991 void ScintillaGTK::SetTicking(bool on) {
992 if (timer.ticking != on) {
993 timer.ticking = on;
994 if (timer.ticking) {
995 timer.tickerID = reinterpret_cast<TickerID>(gtk_timeout_add(timer.tickSize, (GtkFunction)TimeOut, this));
996 } else {
997 gtk_timeout_remove(GPOINTER_TO_UINT(timer.tickerID));
1000 timer.ticksToWait = caret.period;
1003 bool ScintillaGTK::SetIdle(bool on) {
1004 if (on) {
1005 // Start idler, if it's not running.
1006 if (idler.state == false) {
1007 idler.state = true;
1008 idler.idlerID = reinterpret_cast<IdlerID>
1009 (gtk_idle_add((GtkFunction)IdleCallback, this));
1011 } else {
1012 // Stop idler, if it's running
1013 if (idler.state == true) {
1014 idler.state = false;
1015 gtk_idle_remove(GPOINTER_TO_UINT(idler.idlerID));
1018 return true;
1021 void ScintillaGTK::SetMouseCapture(bool on) {
1022 if (mouseDownCaptures) {
1023 if (on) {
1024 gtk_grab_add(GTK_WIDGET(PWidget(wMain)));
1025 } else {
1026 gtk_grab_remove(GTK_WIDGET(PWidget(wMain)));
1029 capturedMouse = on;
1032 bool ScintillaGTK::HaveMouseCapture() {
1033 return capturedMouse;
1036 bool ScintillaGTK::PaintContains(PRectangle rc) {
1037 bool contains = true;
1038 if (paintState == painting) {
1039 if (!rcPaint.Contains(rc)) {
1040 contains = false;
1041 } else if (rgnUpdate) {
1042 GdkRectangle grc = {rc.left, rc.top,
1043 rc.right - rc.left, rc.bottom - rc.top};
1044 if (gdk_region_rect_in(rgnUpdate, &grc) != GDK_OVERLAP_RECTANGLE_IN) {
1045 contains = false;
1049 return contains;
1052 // Redraw all of text area. This paint will not be abandoned.
1053 void ScintillaGTK::FullPaint() {
1054 #if GTK_MAJOR_VERSION < 2
1055 paintState = painting;
1056 rcPaint = GetClientRectangle();
1057 //Platform::DebugPrintf("ScintillaGTK::FullPaint %0d,%0d %0d,%0d\n",
1058 // rcPaint.left, rcPaint.top, rcPaint.right, rcPaint.bottom);
1059 paintingAllText = true;
1060 if ((PWidget(wText))->window) {
1061 Surface *sw = Surface::Allocate();
1062 if (sw) {
1063 sw->Init(PWidget(wText)->window, PWidget(wText));
1064 Paint(sw, rcPaint);
1065 sw->Release();
1066 delete sw;
1069 paintState = notPainting;
1070 #else
1071 wText.InvalidateAll();
1072 #endif
1075 PRectangle ScintillaGTK::GetClientRectangle() {
1076 PRectangle rc = wMain.GetClientPosition();
1077 if (verticalScrollBarVisible)
1078 rc.right -= scrollBarWidth;
1079 if (horizontalScrollBarVisible && (wrapState == eWrapNone))
1080 rc.bottom -= scrollBarHeight;
1081 // Move to origin
1082 rc.right -= rc.left;
1083 rc.bottom -= rc.top;
1084 rc.left = 0;
1085 rc.top = 0;
1086 return rc;
1089 // Synchronously paint a rectangle of the window.
1090 void ScintillaGTK::SyncPaint(PRectangle rc) {
1091 paintState = painting;
1092 rcPaint = rc;
1093 PRectangle rcClient = GetClientRectangle();
1094 paintingAllText = rcPaint.Contains(rcClient);
1095 if ((PWidget(wText))->window) {
1096 Surface *sw = Surface::Allocate();
1097 if (sw) {
1098 sw->Init(PWidget(wText)->window, PWidget(wText));
1099 Paint(sw, rc);
1100 sw->Release();
1101 delete sw;
1104 if (paintState == paintAbandoned) {
1105 // Painting area was insufficient to cover new styling or brace highlight positions
1106 FullPaint();
1108 paintState = notPainting;
1111 void ScintillaGTK::ScrollText(int linesToMove) {
1112 int diff = vs.lineHeight * -linesToMove;
1113 //Platform::DebugPrintf("ScintillaGTK::ScrollText %d %d %0d,%0d %0d,%0d\n", linesToMove, diff,
1114 // rc.left, rc.top, rc.right, rc.bottom);
1115 GtkWidget *wi = PWidget(wText);
1117 #if GTK_MAJOR_VERSION < 2
1118 PRectangle rc = GetClientRectangle();
1119 GdkGC *gc = gdk_gc_new(wi->window);
1121 // Set up gc so we get GraphicsExposures from gdk_draw_pixmap
1122 // which calls XCopyArea
1123 gdk_gc_set_exposures(gc, TRUE);
1125 // Redraw exposed bit : scrolling upwards
1126 if (diff > 0) {
1127 gdk_draw_pixmap(wi->window,
1128 gc, wi->window,
1129 0, diff,
1130 0, 0,
1131 rc.Width()-1, rc.Height() - diff);
1132 SyncPaint(PRectangle(0, rc.Height() - diff,
1133 rc.Width(), rc.Height()+1));
1135 // Redraw exposed bit : scrolling downwards
1136 } else {
1137 gdk_draw_pixmap(wi->window,
1138 gc, wi->window,
1139 0, 0,
1140 0, -diff,
1141 rc.Width()-1, rc.Height() + diff);
1142 SyncPaint(PRectangle(0, 0, rc.Width(), -diff));
1145 // Look for any graphics expose
1146 GdkEvent* event;
1147 while ((event = gdk_event_get_graphics_expose(wi->window)) != NULL) {
1148 gtk_widget_event(wi, event);
1149 if (event->expose.count == 0) {
1150 gdk_event_free(event);
1151 break;
1153 gdk_event_free(event);
1156 gdk_gc_unref(gc);
1157 #else
1158 gdk_window_scroll(wi->window, 0, -diff);
1159 gdk_window_process_updates(wi->window, FALSE);
1160 #endif
1163 void ScintillaGTK::SetVerticalScrollPos() {
1164 DwellEnd(true);
1165 gtk_adjustment_set_value(GTK_ADJUSTMENT(adjustmentv), topLine);
1168 void ScintillaGTK::SetHorizontalScrollPos() {
1169 DwellEnd(true);
1170 gtk_adjustment_set_value(GTK_ADJUSTMENT(adjustmenth), xOffset / 2);
1173 bool ScintillaGTK::ModifyScrollBars(int nMax, int nPage) {
1174 bool modified = false;
1175 int pageScroll = LinesToScroll();
1177 if (GTK_ADJUSTMENT(adjustmentv)->upper != (nMax + 1) ||
1178 GTK_ADJUSTMENT(adjustmentv)->page_size != nPage ||
1179 GTK_ADJUSTMENT(adjustmentv)->page_increment != pageScroll) {
1180 GTK_ADJUSTMENT(adjustmentv)->upper = nMax + 1;
1181 GTK_ADJUSTMENT(adjustmentv)->page_size = nPage;
1182 GTK_ADJUSTMENT(adjustmentv)->page_increment = pageScroll;
1183 gtk_adjustment_changed(GTK_ADJUSTMENT(adjustmentv));
1184 modified = true;
1187 PRectangle rcText = GetTextRectangle();
1188 int horizEndPreferred = scrollWidth;
1189 if (horizEndPreferred < 0)
1190 horizEndPreferred = 0;
1191 unsigned int pageWidth = rcText.Width();
1192 unsigned int pageIncrement = pageWidth / 3;
1193 unsigned int charWidth = vs.styles[STYLE_DEFAULT].aveCharWidth;
1194 if (GTK_ADJUSTMENT(adjustmenth)->upper != horizEndPreferred ||
1195 GTK_ADJUSTMENT(adjustmenth)->page_size != pageWidth ||
1196 GTK_ADJUSTMENT(adjustmenth)->page_increment != pageIncrement ||
1197 GTK_ADJUSTMENT(adjustmenth)->step_increment != charWidth) {
1198 GTK_ADJUSTMENT(adjustmenth)->upper = horizEndPreferred;
1199 GTK_ADJUSTMENT(adjustmenth)->step_increment = charWidth;
1200 GTK_ADJUSTMENT(adjustmenth)->page_size = pageWidth;
1201 GTK_ADJUSTMENT(adjustmenth)->page_increment = pageIncrement;
1202 gtk_adjustment_changed(GTK_ADJUSTMENT(adjustmenth));
1203 modified = true;
1205 return modified;
1208 void ScintillaGTK::ReconfigureScrollBars() {
1209 PRectangle rc = wMain.GetClientPosition();
1210 Resize(rc.Width(), rc.Height());
1213 void ScintillaGTK::NotifyChange() {
1214 #if GLIB_MAJOR_VERSION < 2
1215 gtk_signal_emit(GTK_OBJECT(sci), scintilla_signals[COMMAND_SIGNAL],
1216 Platform::LongFromTwoShorts(GetCtrlID(), SCEN_CHANGE), PWidget(wMain));
1217 #else
1218 g_signal_emit(G_OBJECT(sci), scintilla_signals[COMMAND_SIGNAL], 0,
1219 Platform::LongFromTwoShorts(GetCtrlID(), SCEN_CHANGE), PWidget(wMain));
1220 #endif
1223 void ScintillaGTK::NotifyFocus(bool focus) {
1224 #if GLIB_MAJOR_VERSION < 2
1225 gtk_signal_emit(GTK_OBJECT(sci), scintilla_signals[COMMAND_SIGNAL],
1226 Platform::LongFromTwoShorts
1227 (GetCtrlID(), focus ? SCEN_SETFOCUS : SCEN_KILLFOCUS), PWidget(wMain));
1228 #else
1229 g_signal_emit(G_OBJECT(sci), scintilla_signals[COMMAND_SIGNAL], 0,
1230 Platform::LongFromTwoShorts
1231 (GetCtrlID(), focus ? SCEN_SETFOCUS : SCEN_KILLFOCUS), PWidget(wMain));
1232 #endif
1235 void ScintillaGTK::NotifyParent(SCNotification scn) {
1236 scn.nmhdr.hwndFrom = PWidget(wMain);
1237 scn.nmhdr.idFrom = GetCtrlID();
1238 #if GLIB_MAJOR_VERSION < 2
1239 gtk_signal_emit(GTK_OBJECT(sci), scintilla_signals[NOTIFY_SIGNAL],
1240 GetCtrlID(), &scn);
1241 #else
1242 g_signal_emit(G_OBJECT(sci), scintilla_signals[NOTIFY_SIGNAL], 0,
1243 GetCtrlID(), &scn);
1244 #endif
1247 void ScintillaGTK::NotifyKey(int key, int modifiers) {
1248 SCNotification scn = {0};
1249 scn.nmhdr.code = SCN_KEY;
1250 scn.ch = key;
1251 scn.modifiers = modifiers;
1253 NotifyParent(scn);
1256 void ScintillaGTK::NotifyURIDropped(const char *list) {
1257 SCNotification scn = {0};
1258 scn.nmhdr.code = SCN_URIDROPPED;
1259 scn.text = list;
1261 NotifyParent(scn);
1264 const char *CharacterSetID(int characterSet);
1266 const char *ScintillaGTK::CharacterSetID() const {
1267 return ::CharacterSetID(vs.styles[STYLE_DEFAULT].characterSet);
1270 int ScintillaGTK::KeyDefault(int key, int modifiers) {
1271 if (!(modifiers & SCI_CTRL) && !(modifiers & SCI_ALT)) {
1272 if (key < 256) {
1273 NotifyKey(key, modifiers);
1274 return 0;
1275 } else {
1276 // Pass up to container in case it is an accelerator
1277 NotifyKey(key, modifiers);
1278 return 0;
1280 } else {
1281 // Pass up to container in case it is an accelerator
1282 NotifyKey(key, modifiers);
1283 return 0;
1285 //Platform::DebugPrintf("SK-key: %d %x %x\n",key, modifiers);
1288 void ScintillaGTK::CopyToClipboard(const SelectionText &selectedText) {
1289 #ifndef USE_GTK_CLIPBOARD
1290 copyText.Copy(selectedText);
1291 gtk_selection_owner_set(GTK_WIDGET(PWidget(wMain)),
1292 atomClipboard,
1293 GDK_CURRENT_TIME);
1294 #else
1295 SelectionText *clipText = new SelectionText();
1296 clipText->Copy(selectedText);
1297 StoreOnClipboard(clipText);
1298 #endif
1301 void ScintillaGTK::Copy() {
1302 if (currentPos != anchor) {
1303 #ifndef USE_GTK_CLIPBOARD
1304 CopySelectionRange(&copyText);
1305 gtk_selection_owner_set(GTK_WIDGET(PWidget(wMain)),
1306 atomClipboard,
1307 GDK_CURRENT_TIME);
1308 #else
1309 SelectionText *clipText = new SelectionText();
1310 CopySelectionRange(clipText);
1311 StoreOnClipboard(clipText);
1312 #endif
1313 #if PLAT_GTK_WIN32
1314 if (selType == selRectangle) {
1315 ::OpenClipboard(NULL);
1316 ::SetClipboardData(cfColumnSelect, 0);
1317 ::CloseClipboard();
1319 #endif
1323 void ScintillaGTK::Paste() {
1324 atomSought = atomUTF8;
1325 gtk_selection_convert(GTK_WIDGET(PWidget(wMain)),
1326 atomClipboard, atomSought, GDK_CURRENT_TIME);
1329 void ScintillaGTK::CreateCallTipWindow(PRectangle rc) {
1330 if (!ct.wCallTip.Created()) {
1331 ct.wCallTip = gtk_window_new(GTK_WINDOW_POPUP);
1332 ct.wDraw = gtk_drawing_area_new();
1333 GtkWidget *widcdrw = PWidget(ct.wDraw); // // No code inside the G_OBJECT macro
1334 gtk_container_add(GTK_CONTAINER(PWidget(ct.wCallTip)), widcdrw);
1335 #if GLIB_MAJOR_VERSION < 2
1336 gtk_signal_connect(GTK_OBJECT(widcdrw), "expose_event",
1337 GtkSignalFunc(ScintillaGTK::ExposeCT), &ct);
1338 gtk_signal_connect(GTK_OBJECT(widcdrw), "button_press_event",
1339 GtkSignalFunc(ScintillaGTK::PressCT), static_cast<void *>(this));
1340 #else
1341 g_signal_connect(G_OBJECT(widcdrw), "expose_event",
1342 G_CALLBACK(ScintillaGTK::ExposeCT), &ct);
1343 g_signal_connect(G_OBJECT(widcdrw), "button_press_event",
1344 G_CALLBACK(ScintillaGTK::PressCT), static_cast<void *>(this));
1345 #endif
1346 gtk_widget_set_events(widcdrw,
1347 GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
1349 gtk_drawing_area_size(GTK_DRAWING_AREA(PWidget(ct.wDraw)),
1350 rc.Width(), rc.Height());
1351 ct.wDraw.Show();
1352 if (PWidget(ct.wCallTip)->window) {
1353 gdk_window_resize(PWidget(ct.wCallTip)->window, rc.Width(), rc.Height());
1357 void ScintillaGTK::AddToPopUp(const char *label, int cmd, bool enabled) {
1358 char fulllabel[200];
1359 strcpy(fulllabel, "/");
1360 strcat(fulllabel, label);
1361 GtkItemFactoryCallback menuSig = GtkItemFactoryCallback(PopUpCB);
1362 GtkItemFactoryEntry itemEntry = {
1363 fulllabel, NULL,
1364 menuSig,
1365 cmd,
1366 const_cast<gchar *>(label[0] ? "<Item>" : "<Separator>"),
1367 #if GTK_MAJOR_VERSION >= 2
1368 NULL
1369 #endif
1371 gtk_item_factory_create_item(GTK_ITEM_FACTORY(popup.GetID()),
1372 &itemEntry, this, 1);
1373 if (cmd) {
1374 GtkWidget *item = gtk_item_factory_get_widget_by_action(
1375 reinterpret_cast<GtkItemFactory *>(popup.GetID()), cmd);
1376 if (item)
1377 gtk_widget_set_sensitive(item, enabled);
1381 bool ScintillaGTK::OwnPrimarySelection() {
1382 return ((gdk_selection_owner_get(GDK_SELECTION_PRIMARY)
1383 == GTK_WIDGET(PWidget(wMain))->window) &&
1384 (GTK_WIDGET(PWidget(wMain))->window != NULL));
1387 void ScintillaGTK::ClaimSelection() {
1388 // X Windows has a 'primary selection' as well as the clipboard.
1389 // Whenever the user selects some text, we become the primary selection
1390 if (currentPos != anchor && GTK_WIDGET_REALIZED(GTK_WIDGET(PWidget(wMain)))) {
1391 primarySelection = true;
1392 gtk_selection_owner_set(GTK_WIDGET(PWidget(wMain)),
1393 GDK_SELECTION_PRIMARY, GDK_CURRENT_TIME);
1394 primary.Free();
1395 } else if (OwnPrimarySelection()) {
1396 primarySelection = true;
1397 if (primary.s == NULL)
1398 gtk_selection_owner_set(NULL, GDK_SELECTION_PRIMARY, GDK_CURRENT_TIME);
1399 } else {
1400 primarySelection = false;
1401 primary.Free();
1405 // Detect rectangular text, convert line ends to current mode, convert from or to UTF-8
1406 void ScintillaGTK::GetGtkSelectionText(GtkSelectionData *selectionData, SelectionText &selText) {
1407 char *data = reinterpret_cast<char *>(selectionData->data);
1408 int len = selectionData->length;
1409 GdkAtom selectionTypeData = selectionData->type;
1411 // Return empty string if selection is not a string
1412 if ((selectionTypeData != GDK_TARGET_STRING) && (selectionTypeData != atomUTF8)) {
1413 char *empty = new char[1];
1414 empty[0] = '\0';
1415 selText.Set(empty, 0, SC_CP_UTF8, 0, false, false);
1416 return;
1419 // Check for "\n\0" ending to string indicating that selection is rectangular
1420 bool isRectangular;
1421 #if PLAT_GTK_WIN32
1422 isRectangular = ::IsClipboardFormatAvailable(cfColumnSelect) != 0;
1423 #else
1424 isRectangular = ((len > 2) && (data[len - 1] == 0 && data[len - 2] == '\n'));
1425 #endif
1427 char *dest;
1428 if (selectionTypeData == GDK_TARGET_STRING) {
1429 dest = Document::TransformLineEnds(&len, data, len, pdoc->eolMode);
1430 if (IsUnicodeMode()) {
1431 // Unknown encoding so assume in Latin1
1432 char *destPrevious = dest;
1433 dest = UTF8FromLatin1(dest, len);
1434 selText.Set(dest, len, SC_CP_UTF8, 0, selText.rectangular, false);
1435 delete []destPrevious;
1436 } else {
1437 // Assume buffer is in same encoding as selection
1438 selText.Set(dest, len, pdoc->dbcsCodePage,
1439 vs.styles[STYLE_DEFAULT].characterSet, isRectangular, false);
1441 } else { // UTF-8
1442 dest = Document::TransformLineEnds(&len, data, len, pdoc->eolMode);
1443 selText.Set(dest, len, SC_CP_UTF8, 0, isRectangular, false);
1444 #ifdef USE_CONVERTER
1445 const char *charSetBuffer = CharacterSetID();
1446 if (!IsUnicodeMode() && *charSetBuffer) {
1447 //fprintf(stderr, "Convert to locale %s\n", CharacterSetID());
1448 // Convert to locale
1449 dest = ConvertText(&len, selText.s, selText.len, charSetBuffer, "UTF-8", true);
1450 selText.Set(dest, len, pdoc->dbcsCodePage,
1451 vs.styles[STYLE_DEFAULT].characterSet, selText.rectangular, false);
1453 #endif
1457 void ScintillaGTK::ReceivedSelection(GtkSelectionData *selection_data) {
1458 if ((selection_data->selection == atomClipboard) ||
1459 (selection_data->selection == GDK_SELECTION_PRIMARY)) {
1460 if ((atomSought == atomUTF8) && (selection_data->length <= 0)) {
1461 atomSought = atomString;
1462 gtk_selection_convert(GTK_WIDGET(PWidget(wMain)),
1463 selection_data->selection, atomSought, GDK_CURRENT_TIME);
1464 } else if ((selection_data->length > 0) &&
1465 ((selection_data->type == GDK_TARGET_STRING) || (selection_data->type == atomUTF8))) {
1466 SelectionText selText;
1467 GetGtkSelectionText(selection_data, selText);
1469 pdoc->BeginUndoAction();
1470 if (selection_data->selection != GDK_SELECTION_PRIMARY) {
1471 ClearSelection();
1473 int selStart = SelectionStart();
1475 if (selText.rectangular) {
1476 PasteRectangular(selStart, selText.s, selText.len);
1477 } else {
1478 pdoc->InsertString(currentPos, selText.s, selText.len);
1479 SetEmptySelection(currentPos + selText.len);
1481 pdoc->EndUndoAction();
1482 EnsureCaretVisible();
1485 // else fprintf(stderr, "Target non string %d %d\n", (int)(selection_data->type),
1486 // (int)(atomUTF8));
1487 Redraw();
1490 void ScintillaGTK::ReceivedDrop(GtkSelectionData *selection_data) {
1491 dragWasDropped = true;
1492 if (selection_data->type == atomUriList || selection_data->type == atomDROPFILES_DND) {
1493 char *ptr = new char[selection_data->length + 1];
1494 ptr[selection_data->length] = '\0';
1495 memcpy(ptr, selection_data->data, selection_data->length);
1496 NotifyURIDropped(ptr);
1497 delete []ptr;
1498 } else if ((selection_data->type == GDK_TARGET_STRING) || (selection_data->type == atomUTF8)) {
1499 if (selection_data->length > 0) {
1500 SelectionText selText;
1501 GetGtkSelectionText(selection_data, selText);
1502 DropAt(posDrop, selText.s, false, selText.rectangular);
1504 } else if (selection_data->length > 0) {
1505 //~ fprintf(stderr, "ReceivedDrop other %p\n", static_cast<void *>(selection_data->type));
1507 Redraw();
1512 void ScintillaGTK::GetSelection(GtkSelectionData *selection_data, guint info, SelectionText *text) {
1513 #if PLAT_GTK_WIN32
1514 // GDK on Win32 expands any \n into \r\n, so make a copy of
1515 // the clip text now with newlines converted to \n. Use { } to hide symbols
1516 // from code below
1517 SelectionText *newline_normalized = NULL;
1519 int tmpstr_len;
1520 char *tmpstr = Document::TransformLineEnds(&tmpstr_len, text->s, text->len, SC_EOL_LF);
1521 newline_normalized = new SelectionText();
1522 newline_normalized->Set(tmpstr, tmpstr_len, SC_CP_UTF8, 0, text->rectangular, false);
1523 text = newline_normalized;
1525 #endif
1527 #if GTK_MAJOR_VERSION >= 2
1528 // Convert text to utf8 if it isn't already
1529 SelectionText *converted = 0;
1530 if ((text->codePage != SC_CP_UTF8) && (info == TARGET_UTF8_STRING)) {
1531 const char *charSet = ::CharacterSetID(text->characterSet);
1532 if (*charSet) {
1533 int new_len;
1534 char* tmputf = ConvertText(&new_len, text->s, text->len, "UTF-8", charSet, false);
1535 converted = new SelectionText();
1536 converted->Set(tmputf, new_len, SC_CP_UTF8, 0, text->rectangular, false);
1537 text = converted;
1541 // Here is a somewhat evil kludge.
1542 // As I can not work out how to store data on the clipboard in multiple formats
1543 // and need some way to mark the clipping as being stream or rectangular,
1544 // the terminating \0 is included in the length for rectangular clippings.
1545 // All other tested aplications behave benignly by ignoring the \0.
1546 // The #if is here because on Windows cfColumnSelect clip entry is used
1547 // instead as standard indicator of rectangularness (so no need to kludge)
1548 int len = strlen(text->s);
1549 #if PLAT_GTK_WIN32 == 0
1550 if (text->rectangular)
1551 len++;
1552 #endif
1554 if (info == TARGET_UTF8_STRING) {
1555 gtk_selection_data_set_text(selection_data, text->s, len);
1556 } else {
1557 gtk_selection_data_set(selection_data,
1558 static_cast<GdkAtom>(GDK_SELECTION_TYPE_STRING),
1559 8, reinterpret_cast<unsigned char *>(text->s), len);
1561 delete converted;
1563 #else /* Gtk 1 */
1564 char *selBuffer = text->s;
1566 char *tmputf = 0;
1567 if ((info == TARGET_UTF8_STRING) || (info == TARGET_STRING)) {
1568 int len = strlen(selBuffer);
1569 #ifdef USE_CONVERTER
1570 // Possible character set conversion
1571 const char *charSetBuffer = ::CharacterSetID(text->characterSet);
1572 if (info == TARGET_UTF8_STRING) {
1573 //fprintf(stderr, "Copy to clipboard as UTF-8\n");
1574 if (text->codePage != SC_CP_UTF8) {
1575 // Convert to UTF-8
1576 //fprintf(stderr, "Convert to UTF-8 from %s\n", charSetBuffer);
1577 tmputf = ConvertText(&len, selBuffer, len, "UTF-8", charSetBuffer, false);
1578 selBuffer = tmputf;
1580 } else if (info == TARGET_STRING) {
1581 if (text->codePage == SC_CP_UTF8) {
1582 //fprintf(stderr, "Convert to locale %s\n", charSetBuffer);
1583 // Convert to locale
1584 tmputf = ConvertText(&len, selBuffer, len, charSetBuffer, "UTF-8", true);
1585 selBuffer = tmputf;
1588 #endif
1590 // Here is a somewhat evil kludge.
1591 // As I can not work out how to store data on the clipboard in multiple formats
1592 // and need some way to mark the clipping as being stream or rectangular,
1593 // the terminating \0 is included in the length for rectangular clippings.
1594 // All other tested aplications behave benignly by ignoring the \0.
1595 // The #if is here because on Windows cfColumnSelect clip entry is used
1596 // instead as standard indicator of rectangularness (so no need to kludge)
1597 #if PLAT_GTK_WIN32 == 0
1598 if (text->rectangular)
1599 len++;
1600 #endif
1601 gtk_selection_data_set(selection_data,
1602 (info == TARGET_STRING) ?
1603 static_cast<GdkAtom>(GDK_SELECTION_TYPE_STRING) : atomUTF8,
1604 8, reinterpret_cast<unsigned char *>(selBuffer),
1605 len);
1606 } else if ((info == TARGET_TEXT) || (info == TARGET_COMPOUND_TEXT)) {
1607 guchar *text;
1608 GdkAtom encoding;
1609 gint format;
1610 gint new_length;
1612 gdk_string_to_compound_text(reinterpret_cast<char *>(selBuffer),
1613 &encoding, &format, &text, &new_length);
1614 gtk_selection_data_set(selection_data, encoding, format, text, new_length);
1615 gdk_free_compound_text(text);
1618 delete []tmputf;
1619 #endif /* Gtk >= 2 */
1621 #if PLAT_GTK_WIN32
1622 delete newline_normalized;
1623 #endif
1626 #ifdef USE_GTK_CLIPBOARD
1627 void ScintillaGTK::StoreOnClipboard(SelectionText *clipText) {
1628 GtkClipboard *clipBoard =
1629 gtk_widget_get_clipboard(GTK_WIDGET(PWidget(wMain)), atomClipboard);
1630 if (clipBoard == NULL) // Occurs if widget isn't in a toplevel
1631 return;
1633 if (gtk_clipboard_set_with_data(clipBoard, clipboardCopyTargets, nClipboardCopyTargets,
1634 ClipboardGetSelection, ClipboardClearSelection, clipText)) {
1635 gtk_clipboard_set_can_store(clipBoard, clipboardCopyTargets, nClipboardCopyTargets);
1639 void ScintillaGTK::ClipboardGetSelection(GtkClipboard *, GtkSelectionData *selection_data, guint info, void *data) {
1640 GetSelection(selection_data, info, static_cast<SelectionText*>(data));
1643 void ScintillaGTK::ClipboardClearSelection(GtkClipboard *, void *data) {
1644 SelectionText *obj = static_cast<SelectionText*>(data);
1645 delete obj;
1647 #endif
1649 void ScintillaGTK::UnclaimSelection(GdkEventSelection *selection_event) {
1650 //Platform::DebugPrintf("UnclaimSelection\n");
1651 if (selection_event->selection == GDK_SELECTION_PRIMARY) {
1652 //Platform::DebugPrintf("UnclaimPrimarySelection\n");
1653 if (!OwnPrimarySelection()) {
1654 primary.Free();
1655 primarySelection = false;
1656 FullPaint();
1661 void ScintillaGTK::Resize(int width, int height) {
1662 //Platform::DebugPrintf("Resize %d %d\n", width, height);
1663 //printf("Resize %d %d\n", width, height);
1665 // Not always needed, but some themes can have different sizes of scrollbars
1666 scrollBarWidth = GTK_WIDGET(PWidget(scrollbarv))->requisition.width;
1667 scrollBarHeight = GTK_WIDGET(PWidget(scrollbarh))->requisition.height;
1669 // These allocations should never produce negative sizes as they would wrap around to huge
1670 // unsigned numbers inside GTK+ causing warnings.
1671 bool showSBHorizontal = horizontalScrollBarVisible && (wrapState == eWrapNone);
1672 int horizontalScrollBarHeight = scrollBarHeight;
1673 if (!showSBHorizontal)
1674 horizontalScrollBarHeight = 0;
1675 int verticalScrollBarHeight = scrollBarWidth;
1676 if (!verticalScrollBarVisible)
1677 verticalScrollBarHeight = 0;
1679 GtkAllocation alloc;
1680 if (showSBHorizontal) {
1681 gtk_widget_show(GTK_WIDGET(PWidget(scrollbarh)));
1682 alloc.x = 0;
1683 alloc.y = height - scrollBarHeight;
1684 alloc.width = Platform::Maximum(1, width - scrollBarWidth) + 1;
1685 alloc.height = horizontalScrollBarHeight;
1686 gtk_widget_size_allocate(GTK_WIDGET(PWidget(scrollbarh)), &alloc);
1687 } else {
1688 gtk_widget_hide(GTK_WIDGET(PWidget(scrollbarh)));
1691 if (verticalScrollBarVisible) {
1692 gtk_widget_show(GTK_WIDGET(PWidget(scrollbarv)));
1693 alloc.x = width - scrollBarWidth;
1694 alloc.y = 0;
1695 alloc.width = scrollBarWidth;
1696 alloc.height = Platform::Maximum(1, height - scrollBarHeight) + 1;
1697 if (!showSBHorizontal)
1698 alloc.height += scrollBarWidth-1;
1699 gtk_widget_size_allocate(GTK_WIDGET(PWidget(scrollbarv)), &alloc);
1700 } else {
1701 gtk_widget_hide(GTK_WIDGET(PWidget(scrollbarv)));
1703 if (GTK_WIDGET_MAPPED(PWidget(wMain))) {
1704 ChangeSize();
1707 alloc.x = 0;
1708 alloc.y = 0;
1709 alloc.width = Platform::Maximum(1, width - scrollBarWidth);
1710 alloc.height = Platform::Maximum(1, height - scrollBarHeight);
1711 if (!showSBHorizontal)
1712 alloc.height += scrollBarHeight;
1713 if (!verticalScrollBarVisible)
1714 alloc.width += scrollBarWidth;
1715 gtk_widget_size_allocate(GTK_WIDGET(PWidget(wText)), &alloc);
1718 static void SetAdjustmentValue(GtkObject *object, int value) {
1719 GtkAdjustment *adjustment = GTK_ADJUSTMENT(object);
1720 int maxValue = static_cast<int>(
1721 adjustment->upper - adjustment->page_size);
1722 if (value > maxValue)
1723 value = maxValue;
1724 if (value < 0)
1725 value = 0;
1726 gtk_adjustment_set_value(adjustment, value);
1729 gint ScintillaGTK::PressThis(GdkEventButton *event) {
1730 //Platform::DebugPrintf("Press %x time=%d state = %x button = %x\n",this,event->time, event->state, event->button);
1731 // Do not use GTK+ double click events as Scintilla has its own double click detection
1732 if (event->type != GDK_BUTTON_PRESS)
1733 return FALSE;
1735 evbtn = *event;
1736 Point pt;
1737 pt.x = int(event->x);
1738 pt.y = int(event->y);
1739 PRectangle rcClient = GetClientRectangle();
1740 //Platform::DebugPrintf("Press %0d,%0d in %0d,%0d %0d,%0d\n",
1741 // pt.x, pt.y, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
1742 if ((pt.x > rcClient.right) || (pt.y > rcClient.bottom)) {
1743 Platform::DebugPrintf("Bad location\n");
1744 return FALSE;
1747 bool ctrl = (event->state & GDK_CONTROL_MASK) != 0;
1749 gtk_widget_grab_focus(PWidget(wMain));
1750 if (event->button == 1) {
1751 // On X, instead of sending literal modifiers use control instead of alt
1752 // This is because most X window managers grab alt + click for moving
1753 #if !PLAT_GTK_WIN32
1754 ButtonDown(pt, event->time,
1755 (event->state & GDK_SHIFT_MASK) != 0,
1756 (event->state & GDK_CONTROL_MASK) != 0,
1757 (event->state & GDK_CONTROL_MASK) != 0);
1758 #else
1759 ButtonDown(pt, event->time,
1760 (event->state & GDK_SHIFT_MASK) != 0,
1761 (event->state & GDK_CONTROL_MASK) != 0,
1762 (event->state & GDK_MOD1_MASK) != 0);
1763 #endif
1764 } else if (event->button == 2) {
1765 // Grab the primary selection if it exists
1766 Position pos = PositionFromLocation(pt);
1767 if (OwnPrimarySelection() && primary.s == NULL)
1768 CopySelectionRange(&primary);
1770 SetSelection(pos, pos);
1771 atomSought = atomUTF8;
1772 gtk_selection_convert(GTK_WIDGET(PWidget(wMain)), GDK_SELECTION_PRIMARY,
1773 atomSought, event->time);
1774 } else if (event->button == 3) {
1775 if (displayPopupMenu) {
1776 // PopUp menu
1777 // Convert to screen
1778 int ox = 0;
1779 int oy = 0;
1780 gdk_window_get_origin(PWidget(wMain)->window, &ox, &oy);
1781 ContextMenu(Point(pt.x + ox, pt.y + oy));
1782 } else {
1783 return FALSE;
1785 } else if (event->button == 4) {
1786 // Wheel scrolling up (only GTK 1.x does it this way)
1787 if (ctrl)
1788 SetAdjustmentValue(adjustmenth, (xOffset / 2) - 6);
1789 else
1790 SetAdjustmentValue(adjustmentv, topLine - 3);
1791 } else if (event->button == 5) {
1792 // Wheel scrolling down (only GTK 1.x does it this way)
1793 if (ctrl)
1794 SetAdjustmentValue(adjustmenth, (xOffset / 2) + 6);
1795 else
1796 SetAdjustmentValue(adjustmentv, topLine + 3);
1798 #if GTK_MAJOR_VERSION >= 2
1799 return TRUE;
1800 #else
1801 return FALSE;
1802 #endif
1805 gint ScintillaGTK::Press(GtkWidget *widget, GdkEventButton *event) {
1806 if (event->window != widget->window)
1807 return FALSE;
1808 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
1809 return sciThis->PressThis(event);
1812 gint ScintillaGTK::MouseRelease(GtkWidget *widget, GdkEventButton *event) {
1813 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
1814 //Platform::DebugPrintf("Release %x %d %d\n",sciThis,event->time,event->state);
1815 if (!sciThis->HaveMouseCapture())
1816 return FALSE;
1817 if (event->button == 1) {
1818 Point pt;
1819 pt.x = int(event->x);
1820 pt.y = int(event->y);
1821 //Platform::DebugPrintf("Up %x %x %d %d %d\n",
1822 // sciThis,event->window,event->time, pt.x, pt.y);
1823 if (event->window != PWidget(sciThis->wMain)->window)
1824 // If mouse released on scroll bar then the position is relative to the
1825 // scrollbar, not the drawing window so just repeat the most recent point.
1826 pt = sciThis->ptMouseLast;
1827 sciThis->ButtonUp(pt, event->time, (event->state & 4) != 0);
1829 return FALSE;
1832 // win32gtk and GTK >= 2 use SCROLL_* events instead of passing the
1833 // button4/5/6/7 events to the GTK app
1834 #if PLAT_GTK_WIN32 || (GTK_MAJOR_VERSION >= 2)
1835 gint ScintillaGTK::ScrollEvent(GtkWidget *widget,
1836 GdkEventScroll *event) {
1837 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
1839 if (widget == NULL || event == NULL)
1840 return FALSE;
1842 // Compute amount and direction to scroll (even tho on win32 there is
1843 // intensity of scrolling info in the native message, gtk doesn't
1844 // support this so we simulate similarly adaptive scrolling)
1845 // Note that this is disabled on OS X (Darwin) where the X11 server already has
1846 // and adaptive scrolling algorithm that fights with this one
1847 int cLineScroll;
1848 #if defined(__MWERKS__) || defined(__APPLE_CPP__) || defined(__APPLE_CC__)
1849 cLineScroll = sciThis->linesPerScroll;
1850 if (cLineScroll == 0)
1851 cLineScroll = 4;
1852 sciThis->wheelMouseIntensity = cLineScroll;
1853 #else
1854 int timeDelta = 1000000;
1855 GTimeVal curTime;
1856 g_get_current_time(&curTime);
1857 if (curTime.tv_sec == sciThis->lastWheelMouseTime.tv_sec)
1858 timeDelta = curTime.tv_usec - sciThis->lastWheelMouseTime.tv_usec;
1859 else if (curTime.tv_sec == sciThis->lastWheelMouseTime.tv_sec + 1)
1860 timeDelta = 1000000 + (curTime.tv_usec - sciThis->lastWheelMouseTime.tv_usec);
1861 if ((event->direction == sciThis->lastWheelMouseDirection) && (timeDelta < 250000)) {
1862 if (sciThis->wheelMouseIntensity < 12)
1863 sciThis->wheelMouseIntensity++;
1864 cLineScroll = sciThis->wheelMouseIntensity;
1865 } else {
1866 cLineScroll = sciThis->linesPerScroll;
1867 if (cLineScroll == 0)
1868 cLineScroll = 4;
1869 sciThis->wheelMouseIntensity = cLineScroll;
1871 #endif
1872 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_LEFT) {
1873 cLineScroll *= -1;
1875 g_get_current_time(&sciThis->lastWheelMouseTime);
1876 sciThis->lastWheelMouseDirection = event->direction;
1878 // Note: Unpatched versions of win32gtk don't set the 'state' value so
1879 // only regular scrolling is supported there. Also, unpatched win32gtk
1880 // issues spurious button 2 mouse events during wheeling, which can cause
1881 // problems (a patch for both was submitted by archaeopteryx.com on 13Jun2001)
1883 // Data zoom not supported
1884 if (event->state & GDK_SHIFT_MASK) {
1885 return FALSE;
1888 // Horizontal scrolling
1889 if (event->direction == GDK_SCROLL_LEFT || event->direction == GDK_SCROLL_RIGHT) {
1890 sciThis->HorizontalScrollTo(sciThis->xOffset + cLineScroll);
1892 // Text font size zoom
1893 } else if (event->state & GDK_CONTROL_MASK) {
1894 if (cLineScroll < 0) {
1895 sciThis->KeyCommand(SCI_ZOOMIN);
1896 } else {
1897 sciThis->KeyCommand(SCI_ZOOMOUT);
1900 // Regular scrolling
1901 } else {
1902 sciThis->ScrollTo(sciThis->topLine + cLineScroll);
1904 return TRUE;
1906 #endif
1908 gint ScintillaGTK::Motion(GtkWidget *widget, GdkEventMotion *event) {
1909 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
1910 //Platform::DebugPrintf("Motion %x %d\n",sciThis,event->time);
1911 if (event->window != widget->window)
1912 return FALSE;
1913 int x = 0;
1914 int y = 0;
1915 GdkModifierType state;
1916 if (event->is_hint) {
1917 gdk_window_get_pointer(event->window, &x, &y, &state);
1918 } else {
1919 x = static_cast<int>(event->x);
1920 y = static_cast<int>(event->y);
1921 state = static_cast<GdkModifierType>(event->state);
1923 //Platform::DebugPrintf("Move %x %x %d %c %d %d\n",
1924 // sciThis,event->window,event->time,event->is_hint? 'h' :'.', x, y);
1925 Point pt(x, y);
1926 sciThis->ButtonMove(pt);
1927 return FALSE;
1930 // Map the keypad keys to their equivalent functions
1931 static int KeyTranslate(int keyIn) {
1932 switch (keyIn) {
1933 case GDK_ISO_Left_Tab:
1934 return SCK_TAB;
1935 case GDK_KP_Down:
1936 return SCK_DOWN;
1937 case GDK_KP_Up:
1938 return SCK_UP;
1939 case GDK_KP_Left:
1940 return SCK_LEFT;
1941 case GDK_KP_Right:
1942 return SCK_RIGHT;
1943 case GDK_KP_Home:
1944 return SCK_HOME;
1945 case GDK_KP_End:
1946 return SCK_END;
1947 case GDK_KP_Page_Up:
1948 return SCK_PRIOR;
1949 case GDK_KP_Page_Down:
1950 return SCK_NEXT;
1951 case GDK_KP_Delete:
1952 return SCK_DELETE;
1953 case GDK_KP_Insert:
1954 return SCK_INSERT;
1955 case GDK_KP_Enter:
1956 return SCK_RETURN;
1958 case GDK_Down:
1959 return SCK_DOWN;
1960 case GDK_Up:
1961 return SCK_UP;
1962 case GDK_Left:
1963 return SCK_LEFT;
1964 case GDK_Right:
1965 return SCK_RIGHT;
1966 case GDK_Home:
1967 return SCK_HOME;
1968 case GDK_End:
1969 return SCK_END;
1970 case GDK_Page_Up:
1971 return SCK_PRIOR;
1972 case GDK_Page_Down:
1973 return SCK_NEXT;
1974 case GDK_Delete:
1975 return SCK_DELETE;
1976 case GDK_Insert:
1977 return SCK_INSERT;
1978 case GDK_Escape:
1979 return SCK_ESCAPE;
1980 case GDK_BackSpace:
1981 return SCK_BACK;
1982 case GDK_Tab:
1983 return SCK_TAB;
1984 case GDK_Return:
1985 return SCK_RETURN;
1986 case GDK_KP_Add:
1987 return SCK_ADD;
1988 case GDK_KP_Subtract:
1989 return SCK_SUBTRACT;
1990 case GDK_KP_Divide:
1991 return SCK_DIVIDE;
1992 case GDK_Super_L:
1993 return SCK_WIN;
1994 case GDK_Super_R:
1995 return SCK_RWIN;
1996 case GDK_Menu:
1997 return SCK_MENU;
1998 default:
1999 return keyIn;
2003 gboolean ScintillaGTK::KeyThis(GdkEventKey *event) {
2004 //fprintf(stderr, "SC-key: %d %x [%s]\n",
2005 // event->keyval, event->state, (event->length > 0) ? event->string : "empty");
2006 #if GTK_MAJOR_VERSION >= 2
2007 if (gtk_im_context_filter_keypress(im_context, event)) {
2008 return 1;
2010 #endif
2011 if (!event->keyval) {
2012 return true;
2015 bool shift = (event->state & GDK_SHIFT_MASK) != 0;
2016 bool ctrl = (event->state & GDK_CONTROL_MASK) != 0;
2017 bool alt = (event->state & GDK_MOD1_MASK) != 0;
2018 guint key = event->keyval;
2019 if (ctrl && (key < 128))
2020 key = toupper(key);
2021 else if (!ctrl && (key >= GDK_KP_Multiply && key <= GDK_KP_9))
2022 key &= 0x7F;
2023 // Hack for keys over 256 and below command keys but makes Hungarian work.
2024 // This will have to change for Unicode
2025 else if (key >= 0xFE00)
2026 key = KeyTranslate(key);
2027 #if GTK_MAJOR_VERSION < 2
2028 else if (!IsUnicodeMode() && (key >= 0x100) && (key < 0x1000))
2029 key &= 0xff;
2030 #endif
2032 bool consumed = false;
2033 bool added = KeyDown(key, shift, ctrl, alt, &consumed) != 0;
2034 if (!consumed)
2035 consumed = added;
2036 //fprintf(stderr, "SK-key: %d %x %x\n",event->keyval, event->state, consumed);
2037 if (event->keyval == 0xffffff && event->length > 0) {
2038 ClearSelection();
2039 if (pdoc->InsertCString(CurrentPosition(), event->string)) {
2040 MovePositionTo(CurrentPosition() + event->length);
2043 return consumed;
2046 gboolean ScintillaGTK::KeyPress(GtkWidget *widget, GdkEventKey *event) {
2047 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2048 return sciThis->KeyThis(event);
2051 gboolean ScintillaGTK::KeyRelease(GtkWidget *, GdkEventKey * /*event*/) {
2052 //Platform::DebugPrintf("SC-keyrel: %d %x %3s\n",event->keyval, event->state, event->string);
2053 return FALSE;
2056 #if GTK_MAJOR_VERSION >= 2
2057 gboolean ScintillaGTK::ExposePreedit(GtkWidget *widget, GdkEventExpose *ose, ScintillaGTK *sciThis) {
2058 return sciThis->ExposePreeditThis(widget, ose);
2061 gboolean ScintillaGTK::ExposePreeditThis(GtkWidget *widget, GdkEventExpose *ose) {
2062 gchar *str;
2063 gint cursor_pos;
2064 PangoAttrList *attrs;
2066 gtk_im_context_get_preedit_string(im_context, &str, &attrs, &cursor_pos);
2067 PangoLayout *layout = gtk_widget_create_pango_layout(PWidget(wText), str);
2068 pango_layout_set_attributes(layout, attrs);
2070 GdkGC *gc = gdk_gc_new(widget->window);
2071 GdkColor color[2] = { {0, 0x0000, 0x0000, 0x0000},
2072 {0, 0xffff, 0xffff, 0xffff}};
2073 gdk_color_alloc(gdk_colormap_get_system(), color);
2074 gdk_color_alloc(gdk_colormap_get_system(), color + 1);
2076 gdk_gc_set_foreground(gc, color + 1);
2077 gdk_draw_rectangle(widget->window, gc, TRUE, ose->area.x, ose->area.y,
2078 ose->area.width, ose->area.height);
2080 gdk_gc_set_foreground(gc, color);
2081 gdk_gc_set_background(gc, color + 1);
2082 gdk_draw_layout(widget->window, gc, 0, 0, layout);
2084 gdk_gc_unref(gc);
2085 g_free(str);
2086 pango_attr_list_unref(attrs);
2087 g_object_unref(layout);
2088 return TRUE;
2091 void ScintillaGTK::Commit(GtkIMContext *, char *str, ScintillaGTK *sciThis) {
2092 sciThis->CommitThis(str);
2095 void ScintillaGTK::CommitThis(char *utfVal) {
2096 //~ fprintf(stderr, "Commit '%s'\n", utfVal);
2097 if (IsUnicodeMode()) {
2098 AddCharUTF(utfVal,strlen(utfVal));
2099 } else {
2100 const char *source = CharacterSetID();
2101 if (*source) {
2102 Converter conv(source, "UTF-8", true);
2103 if (conv) {
2104 char localeVal[4]="\0\0\0";
2105 char *pin = utfVal;
2106 size_t inLeft = strlen(utfVal);
2107 char *pout = localeVal;
2108 size_t outLeft = sizeof(localeVal);
2109 size_t conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
2110 if (conversions != ((size_t)(-1))) {
2111 *pout = '\0';
2112 for (int i=0; localeVal[i]; i++) {
2113 AddChar(localeVal[i]);
2115 } else {
2116 fprintf(stderr, "Conversion failed '%s'\n", utfVal);
2123 void ScintillaGTK::PreeditChanged(GtkIMContext *, ScintillaGTK *sciThis) {
2124 sciThis->PreeditChangedThis();
2127 void ScintillaGTK::PreeditChangedThis() {
2128 gchar *str;
2129 PangoAttrList *attrs;
2130 gint cursor_pos;
2131 gtk_im_context_get_preedit_string(im_context, &str, &attrs, &cursor_pos);
2132 if (strlen(str) > 0){
2133 PangoLayout *layout = gtk_widget_create_pango_layout(PWidget(wText), str);
2134 pango_layout_set_attributes(layout, attrs);
2136 gint w, h;
2137 pango_layout_get_pixel_size(layout, &w, &h);
2138 g_object_unref(layout);
2140 gint x, y;
2141 gdk_window_get_origin((PWidget(wText))->window, &x, &y);
2143 Point pt = LocationFromPosition(currentPos);
2144 if (pt.x < 0)
2145 pt.x = 0;
2146 if (pt.y < 0)
2147 pt.y = 0;
2149 gtk_window_move(GTK_WINDOW(PWidget(wPreedit)), x+pt.x, y+pt.y);
2150 gtk_window_resize(GTK_WINDOW(PWidget(wPreedit)), w, h);
2151 gtk_widget_show(PWidget(wPreedit));
2152 gtk_widget_queue_draw_area(PWidget(wPreeditDraw), 0, 0, w, h);
2153 } else {
2154 gtk_widget_hide(PWidget(wPreedit));
2156 g_free(str);
2157 pango_attr_list_unref(attrs);
2159 #endif
2161 gint ScintillaGTK::StyleSetText(GtkWidget *widget, GtkStyle *, void*) {
2162 if (widget->window != NULL)
2163 gdk_window_set_back_pixmap(widget->window, NULL, FALSE);
2164 return FALSE;
2167 gint ScintillaGTK::RealizeText(GtkWidget *widget, void*) {
2168 if (widget->window != NULL)
2169 gdk_window_set_back_pixmap(widget->window, NULL, FALSE);
2170 return FALSE;
2173 #if GLIB_MAJOR_VERSION < 2
2174 void ScintillaGTK::Destroy(GtkObject *object)
2175 #else
2176 void ScintillaGTK::Destroy(GObject *object)
2177 #endif
2179 ScintillaObject *scio = reinterpret_cast<ScintillaObject *>(object);
2180 // This avoids a double destruction
2181 if (!scio->pscin)
2182 return;
2183 ScintillaGTK *sciThis = reinterpret_cast<ScintillaGTK *>(scio->pscin);
2184 //Platform::DebugPrintf("Destroying %x %x\n", sciThis, object);
2185 sciThis->Finalise();
2187 #if GLIB_MAJOR_VERSION < 2
2188 if (GTK_OBJECT_CLASS(parent_class)->destroy)
2189 (* GTK_OBJECT_CLASS(parent_class)->destroy)(object);
2190 #else
2191 // IS ANYTHING NEEDED ?
2192 #endif
2194 delete sciThis;
2195 scio->pscin = 0;
2198 static void DrawChild(GtkWidget *widget, GdkRectangle *area) {
2199 GdkRectangle areaIntersect;
2200 if (widget &&
2201 GTK_WIDGET_DRAWABLE(widget) &&
2202 gtk_widget_intersect(widget, area, &areaIntersect)) {
2203 gtk_widget_draw(widget, &areaIntersect);
2207 void ScintillaGTK::Draw(GtkWidget *widget, GdkRectangle *area) {
2208 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2209 //Platform::DebugPrintf("Draw %p %0d,%0d %0d,%0d\n", widget, area->x, area->y, area->width, area->height);
2210 PRectangle rcPaint(area->x, area->y, area->x + area->width, area->y + area->height);
2211 sciThis->SyncPaint(rcPaint);
2212 if (GTK_WIDGET_DRAWABLE(PWidget(sciThis->wMain))) {
2213 DrawChild(PWidget(sciThis->scrollbarh), area);
2214 DrawChild(PWidget(sciThis->scrollbarv), area);
2217 #ifdef INTERNATIONAL_INPUT
2218 Point pt = sciThis->LocationFromPosition(sciThis->currentPos);
2219 pt.y += sciThis->vs.lineHeight - 2;
2220 if (pt.x < 0) pt.x = 0;
2221 if (pt.y < 0) pt.y = 0;
2222 CursorMoved(widget, pt.x, pt.y, sciThis);
2223 #endif
2226 gint ScintillaGTK::ExposeTextThis(GtkWidget * /*widget*/, GdkEventExpose *ose) {
2227 paintState = painting;
2229 rcPaint.left = ose->area.x;
2230 rcPaint.top = ose->area.y;
2231 rcPaint.right = ose->area.x + ose->area.width;
2232 rcPaint.bottom = ose->area.y + ose->area.height;
2234 PLATFORM_ASSERT(rgnUpdate == NULL);
2235 #if GTK_MAJOR_VERSION >= 2
2236 rgnUpdate = gdk_region_copy(ose->region);
2237 #endif
2238 PRectangle rcClient = GetClientRectangle();
2239 paintingAllText = rcPaint.Contains(rcClient);
2240 Surface *surfaceWindow = Surface::Allocate();
2241 if (surfaceWindow) {
2242 surfaceWindow->Init(PWidget(wText)->window, PWidget(wText));
2243 Paint(surfaceWindow, rcPaint);
2244 surfaceWindow->Release();
2245 delete surfaceWindow;
2247 if (paintState == paintAbandoned) {
2248 // Painting area was insufficient to cover new styling or brace highlight positions
2249 FullPaint();
2251 paintState = notPainting;
2253 if (rgnUpdate) {
2254 gdk_region_destroy(rgnUpdate);
2256 rgnUpdate = 0;
2258 return FALSE;
2261 gint ScintillaGTK::ExposeText(GtkWidget *widget, GdkEventExpose *ose, ScintillaGTK *sciThis) {
2262 return sciThis->ExposeTextThis(widget, ose);
2265 gint ScintillaGTK::ExposeMain(GtkWidget *widget, GdkEventExpose *ose) {
2266 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2267 //Platform::DebugPrintf("Expose Main %0d,%0d %0d,%0d\n",
2268 //ose->area.x, ose->area.y, ose->area.width, ose->area.height);
2269 return sciThis->Expose(widget, ose);
2272 gint ScintillaGTK::Expose(GtkWidget *, GdkEventExpose *ose) {
2273 //fprintf(stderr, "Expose %0d,%0d %0d,%0d\n",
2274 //ose->area.x, ose->area.y, ose->area.width, ose->area.height);
2276 #if GTK_MAJOR_VERSION < 2
2278 paintState = painting;
2280 rcPaint.left = ose->area.x;
2281 rcPaint.top = ose->area.y;
2282 rcPaint.right = ose->area.x + ose->area.width;
2283 rcPaint.bottom = ose->area.y + ose->area.height;
2285 PRectangle rcClient = GetClientRectangle();
2286 paintingAllText = rcPaint.Contains(rcClient);
2287 Surface *surfaceWindow = Surface::Allocate();
2288 if (surfaceWindow) {
2289 surfaceWindow->Init(PWidget(wMain)->window, PWidget(wMain));
2291 // Fill the corner between the scrollbars
2292 if (verticalScrollBarVisible) {
2293 if (horizontalScrollBarVisible && (wrapState == eWrapNone)) {
2294 PRectangle rcCorner = wMain.GetClientPosition();
2295 rcCorner.left = rcCorner.right - scrollBarWidth + 1;
2296 rcCorner.top = rcCorner.bottom - scrollBarHeight + 1;
2297 //fprintf(stderr, "Corner %0d,%0d %0d,%0d\n",
2298 //rcCorner.left, rcCorner.top, rcCorner.right, rcCorner.bottom);
2299 surfaceWindow->FillRectangle(rcCorner,
2300 vs.styles[STYLE_LINENUMBER].back.allocated);
2304 //Paint(surfaceWindow, rcPaint);
2305 surfaceWindow->Release();
2306 delete surfaceWindow;
2308 if (paintState == paintAbandoned) {
2309 // Painting area was insufficient to cover new styling or brace highlight positions
2310 FullPaint();
2312 paintState = notPainting;
2314 #else
2315 // For GTK+ 2, the text is painted in ExposeText
2316 gtk_container_propagate_expose(
2317 GTK_CONTAINER(PWidget(wMain)), PWidget(scrollbarh), ose);
2318 gtk_container_propagate_expose(
2319 GTK_CONTAINER(PWidget(wMain)), PWidget(scrollbarv), ose);
2320 #endif
2322 return FALSE;
2325 void ScintillaGTK::ScrollSignal(GtkAdjustment *adj, ScintillaGTK *sciThis) {
2326 sciThis->ScrollTo(static_cast<int>(adj->value), false);
2329 void ScintillaGTK::ScrollHSignal(GtkAdjustment *adj, ScintillaGTK *sciThis) {
2330 sciThis->HorizontalScrollTo(static_cast<int>(adj->value * 2));
2333 void ScintillaGTK::SelectionReceived(GtkWidget *widget,
2334 GtkSelectionData *selection_data, guint) {
2335 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2336 //Platform::DebugPrintf("Selection received\n");
2337 sciThis->ReceivedSelection(selection_data);
2340 void ScintillaGTK::SelectionGet(GtkWidget *widget,
2341 GtkSelectionData *selection_data, guint info, guint) {
2342 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2343 //Platform::DebugPrintf("Selection get\n");
2344 if (selection_data->selection == GDK_SELECTION_PRIMARY) {
2345 if (sciThis->primary.s == NULL) {
2346 sciThis->CopySelectionRange(&sciThis->primary);
2348 sciThis->GetSelection(selection_data, info, &sciThis->primary);
2350 #ifndef USE_GTK_CLIPBOARD
2351 else {
2352 sciThis->GetSelection(selection_data, info, &sciThis->copyText);
2354 #endif
2357 gint ScintillaGTK::SelectionClear(GtkWidget *widget, GdkEventSelection *selection_event) {
2358 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2359 //Platform::DebugPrintf("Selection clear\n");
2360 sciThis->UnclaimSelection(selection_event);
2361 return gtk_selection_clear(widget, selection_event);
2364 #if GTK_MAJOR_VERSION < 2
2365 gint ScintillaGTK::SelectionNotify(GtkWidget *widget, GdkEventSelection *selection_event) {
2366 //Platform::DebugPrintf("Selection notify\n");
2367 return gtk_selection_notify(widget, selection_event);
2369 #endif
2371 void ScintillaGTK::DragBegin(GtkWidget *, GdkDragContext *) {
2372 //Platform::DebugPrintf("DragBegin\n");
2375 gboolean ScintillaGTK::DragMotionThis(GdkDragContext *context,
2376 gint x, gint y, guint dragtime) {
2377 Point npt(x, y);
2378 SetDragPosition(PositionFromLocation(npt));
2379 GdkDragAction preferredAction = context->suggested_action;
2380 int pos = PositionFromLocation(npt);
2381 if ((inDragDrop == ddDragging) && (0 == PositionInSelection(pos))) {
2382 // Avoid dragging selection onto itself as that produces a move
2383 // with no real effect but which creates undo actions.
2384 preferredAction = static_cast<GdkDragAction>(0);
2385 } else if (context->actions == static_cast<GdkDragAction>
2386 (GDK_ACTION_COPY | GDK_ACTION_MOVE)) {
2387 preferredAction = GDK_ACTION_MOVE;
2389 gdk_drag_status(context, preferredAction, dragtime);
2390 return FALSE;
2393 gboolean ScintillaGTK::DragMotion(GtkWidget *widget, GdkDragContext *context,
2394 gint x, gint y, guint dragtime) {
2395 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2396 return sciThis->DragMotionThis(context, x, y, dragtime);
2399 void ScintillaGTK::DragLeave(GtkWidget *widget, GdkDragContext * /*context*/, guint) {
2400 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2401 sciThis->SetDragPosition(invalidPosition);
2402 //Platform::DebugPrintf("DragLeave %x\n", sciThis);
2405 void ScintillaGTK::DragEnd(GtkWidget *widget, GdkDragContext * /*context*/) {
2406 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2407 // If drag did not result in drop here or elsewhere
2408 if (!sciThis->dragWasDropped)
2409 sciThis->SetEmptySelection(sciThis->posDrag);
2410 sciThis->SetDragPosition(invalidPosition);
2411 //Platform::DebugPrintf("DragEnd %x %d\n", sciThis, sciThis->dragWasDropped);
2412 sciThis->inDragDrop = ddNone;
2415 gboolean ScintillaGTK::Drop(GtkWidget *widget, GdkDragContext * /*context*/,
2416 gint, gint, guint) {
2417 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2418 //Platform::DebugPrintf("Drop %x\n", sciThis);
2419 sciThis->SetDragPosition(invalidPosition);
2420 return FALSE;
2423 void ScintillaGTK::DragDataReceived(GtkWidget *widget, GdkDragContext * /*context*/,
2424 gint, gint, GtkSelectionData *selection_data, guint /*info*/, guint) {
2425 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2426 sciThis->ReceivedDrop(selection_data);
2427 sciThis->SetDragPosition(invalidPosition);
2430 void ScintillaGTK::DragDataGet(GtkWidget *widget, GdkDragContext *context,
2431 GtkSelectionData *selection_data, guint info, guint) {
2432 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2433 sciThis->dragWasDropped = true;
2434 if (sciThis->currentPos != sciThis->anchor) {
2435 sciThis->GetSelection(selection_data, info, &sciThis->drag);
2437 if (context->action == GDK_ACTION_MOVE) {
2438 int selStart = sciThis->SelectionStart();
2439 int selEnd = sciThis->SelectionEnd();
2440 if (sciThis->posDrop > selStart) {
2441 if (sciThis->posDrop > selEnd)
2442 sciThis->posDrop = sciThis->posDrop - (selEnd - selStart);
2443 else
2444 sciThis->posDrop = selStart;
2445 sciThis->posDrop = sciThis->pdoc->ClampPositionIntoDocument(sciThis->posDrop);
2447 sciThis->ClearSelection();
2449 sciThis->SetDragPosition(invalidPosition);
2452 int ScintillaGTK::TimeOut(ScintillaGTK *sciThis) {
2453 sciThis->Tick();
2454 return 1;
2457 int ScintillaGTK::IdleCallback(ScintillaGTK *sciThis) {
2458 // Idler will be automatically stoped, if there is nothing
2459 // to do while idle.
2460 bool ret = sciThis->Idle();
2461 if (ret == false) {
2462 // FIXME: This will remove the idler from GTK, we don't want to
2463 // remove it as it is removed automatically when this function
2464 // returns false (although, it should be harmless).
2465 sciThis->SetIdle(false);
2467 return ret;
2470 void ScintillaGTK::PopUpCB(ScintillaGTK *sciThis, guint action, GtkWidget *) {
2471 if (action) {
2472 sciThis->Command(action);
2476 gint ScintillaGTK::PressCT(GtkWidget *widget, GdkEventButton *event, ScintillaGTK *sciThis) {
2477 if (event->window != widget->window)
2478 return FALSE;
2479 if (event->type != GDK_BUTTON_PRESS)
2480 return FALSE;
2481 Point pt;
2482 pt.x = int(event->x);
2483 pt.y = int(event->y);
2484 sciThis->ct.MouseClick(pt);
2485 sciThis->CallTipClick();
2486 #if GTK_MAJOR_VERSION >= 2
2487 return TRUE;
2488 #else
2489 return FALSE;
2490 #endif
2493 gint ScintillaGTK::ExposeCT(GtkWidget *widget, GdkEventExpose * /*ose*/, CallTip *ctip) {
2494 Surface *surfaceWindow = Surface::Allocate();
2495 if (surfaceWindow) {
2496 surfaceWindow->Init(widget->window, widget);
2497 ctip->PaintCT(surfaceWindow);
2498 surfaceWindow->Release();
2499 delete surfaceWindow;
2501 return TRUE;
2504 sptr_t ScintillaGTK::DirectFunction(
2505 ScintillaGTK *sciThis, unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
2506 return sciThis->WndProc(iMessage, wParam, lParam);
2509 sptr_t scintilla_send_message(ScintillaObject *sci, unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
2510 ScintillaGTK *psci = reinterpret_cast<ScintillaGTK *>(sci->pscin);
2511 return psci->WndProc(iMessage, wParam, lParam);
2514 static void scintilla_class_init(ScintillaClass *klass);
2515 static void scintilla_init(ScintillaObject *sci);
2517 extern void Platform_Initialise();
2518 extern void Platform_Finalise();
2520 #if GLIB_MAJOR_VERSION < 2
2521 GtkType scintilla_get_type() {
2522 static GtkType scintilla_type = 0;
2524 if (!scintilla_type) {
2525 Platform_Initialise();
2526 static GtkTypeInfo scintilla_info = {
2527 "Scintilla",
2528 sizeof (ScintillaObject),
2529 sizeof (ScintillaClass),
2530 (GtkClassInitFunc) scintilla_class_init,
2531 (GtkObjectInitFunc) scintilla_init,
2532 (gpointer) NULL,
2533 (gpointer) NULL,
2537 scintilla_type = gtk_type_unique(gtk_container_get_type(), &scintilla_info);
2540 return scintilla_type;
2542 #else
2543 GType scintilla_get_type() {
2544 static GType scintilla_type = 0;
2546 if (!scintilla_type) {
2547 scintilla_type = g_type_from_name("Scintilla");
2548 if (!scintilla_type) {
2549 static GTypeInfo scintilla_info = {
2550 (guint16) sizeof (ScintillaClass),
2551 NULL, //(GBaseInitFunc)
2552 NULL, //(GBaseFinalizeFunc)
2553 (GClassInitFunc) scintilla_class_init,
2554 NULL, //(GClassFinalizeFunc)
2555 NULL, //gconstpointer data
2556 (guint16) sizeof (ScintillaObject),
2557 0, //n_preallocs
2558 (GInstanceInitFunc) scintilla_init,
2559 NULL //(GTypeValueTable*)
2562 scintilla_type = g_type_register_static(
2563 GTK_TYPE_CONTAINER, "Scintilla", &scintilla_info, (GTypeFlags) 0);
2567 return scintilla_type;
2569 #endif
2571 void ScintillaGTK::ClassInit(OBJECT_CLASS* object_class, GtkWidgetClass *widget_class, GtkContainerClass *container_class) {
2572 #if GLIB_MAJOR_VERSION >= 2
2573 Platform_Initialise();
2574 #endif
2575 atomClipboard = gdk_atom_intern("CLIPBOARD", FALSE);
2576 atomUTF8 = gdk_atom_intern("UTF8_STRING", FALSE);
2577 atomString = GDK_SELECTION_TYPE_STRING;
2578 atomUriList = gdk_atom_intern("text/uri-list", FALSE);
2579 atomDROPFILES_DND = gdk_atom_intern("DROPFILES_DND", FALSE);
2581 // Define default signal handlers for the class: Could move more
2582 // of the signal handlers here (those that currently attached to wDraw
2583 // in Initialise() may require coordinate translation?)
2585 #if GLIB_MAJOR_VERSION < 2
2586 object_class->destroy = Destroy;
2587 #else
2588 object_class->finalize = Destroy;
2589 #endif
2590 widget_class->size_request = SizeRequest;
2591 widget_class->size_allocate = SizeAllocate;
2592 widget_class->expose_event = ExposeMain;
2593 #if GTK_MAJOR_VERSION < 2
2594 widget_class->draw = Draw;
2595 #endif
2596 widget_class->motion_notify_event = Motion;
2597 widget_class->button_press_event = Press;
2598 widget_class->button_release_event = MouseRelease;
2599 #if PLAT_GTK_WIN32 || (GTK_MAJOR_VERSION >= 2)
2600 widget_class->scroll_event = ScrollEvent;
2601 #endif
2602 widget_class->key_press_event = KeyPress;
2603 widget_class->key_release_event = KeyRelease;
2604 widget_class->focus_in_event = FocusIn;
2605 widget_class->focus_out_event = FocusOut;
2606 widget_class->selection_received = SelectionReceived;
2607 widget_class->selection_get = SelectionGet;
2608 widget_class->selection_clear_event = SelectionClear;
2609 #if GTK_MAJOR_VERSION < 2
2610 widget_class->selection_notify_event = SelectionNotify;
2611 #endif
2613 widget_class->drag_data_received = DragDataReceived;
2614 widget_class->drag_motion = DragMotion;
2615 widget_class->drag_leave = DragLeave;
2616 widget_class->drag_end = DragEnd;
2617 widget_class->drag_drop = Drop;
2618 widget_class->drag_data_get = DragDataGet;
2620 widget_class->realize = Realize;
2621 widget_class->unrealize = UnRealize;
2622 widget_class->map = Map;
2623 widget_class->unmap = UnMap;
2625 container_class->forall = MainForAll;
2628 #if GLIB_MAJOR_VERSION < 2
2629 #define GTK_CLASS_TYPE(c) (c->type)
2630 #define SIG_MARSHAL gtk_marshal_NONE__INT_POINTER
2631 #define MARSHAL_ARGUMENTS GTK_TYPE_INT, GTK_TYPE_POINTER
2632 #else
2633 #define SIG_MARSHAL scintilla_marshal_NONE__INT_POINTER
2634 #define MARSHAL_ARGUMENTS G_TYPE_INT, G_TYPE_POINTER
2635 #endif
2637 static void scintilla_class_init(ScintillaClass *klass) {
2638 OBJECT_CLASS *object_class = (OBJECT_CLASS*) klass;
2639 GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
2640 GtkContainerClass *container_class = (GtkContainerClass*) klass;
2642 #if GLIB_MAJOR_VERSION < 2
2643 parent_class = (GtkWidgetClass*) gtk_type_class(gtk_container_get_type());
2645 scintilla_signals[COMMAND_SIGNAL] = gtk_signal_new(
2646 "command",
2647 GTK_RUN_LAST,
2648 GTK_CLASS_TYPE(object_class),
2649 GTK_SIGNAL_OFFSET(ScintillaClass, command),
2650 SIG_MARSHAL,
2651 GTK_TYPE_NONE,
2652 2, MARSHAL_ARGUMENTS);
2654 scintilla_signals[NOTIFY_SIGNAL] = gtk_signal_new(
2655 SCINTILLA_NOTIFY,
2656 GTK_RUN_LAST,
2657 GTK_CLASS_TYPE(object_class),
2658 GTK_SIGNAL_OFFSET(ScintillaClass, notify),
2659 SIG_MARSHAL,
2660 GTK_TYPE_NONE,
2661 2, MARSHAL_ARGUMENTS);
2662 gtk_object_class_add_signals(object_class,
2663 reinterpret_cast<unsigned int *>(scintilla_signals), LAST_SIGNAL);
2664 #else
2665 GSignalFlags sigflags = GSignalFlags(G_SIGNAL_ACTION | G_SIGNAL_RUN_LAST);
2666 scintilla_signals[COMMAND_SIGNAL] = g_signal_new(
2667 "command",
2668 G_TYPE_FROM_CLASS(object_class),
2669 sigflags,
2670 G_STRUCT_OFFSET(ScintillaClass, command),
2671 NULL, //(GSignalAccumulator)
2672 NULL, //(gpointer)
2673 SIG_MARSHAL,
2674 G_TYPE_NONE,
2675 2, MARSHAL_ARGUMENTS);
2677 scintilla_signals[NOTIFY_SIGNAL] = g_signal_new(
2678 SCINTILLA_NOTIFY,
2679 G_TYPE_FROM_CLASS(object_class),
2680 sigflags,
2681 G_STRUCT_OFFSET(ScintillaClass, notify),
2682 NULL,
2683 NULL,
2684 SIG_MARSHAL,
2685 G_TYPE_NONE,
2686 2, MARSHAL_ARGUMENTS);
2687 #endif
2688 klass->command = NULL;
2689 klass->notify = NULL;
2691 ScintillaGTK::ClassInit(object_class, widget_class, container_class);
2694 static void scintilla_init(ScintillaObject *sci) {
2695 GTK_WIDGET_SET_FLAGS(sci, GTK_CAN_FOCUS);
2696 sci->pscin = new ScintillaGTK(sci);
2699 GtkWidget* scintilla_new() {
2700 #if GLIB_MAJOR_VERSION < 2
2701 return GTK_WIDGET(gtk_type_new(scintilla_get_type()));
2702 #else
2703 return GTK_WIDGET(g_object_new(scintilla_get_type(), NULL));
2704 #endif
2707 void scintilla_set_id(ScintillaObject *sci, uptr_t id) {
2708 ScintillaGTK *psci = reinterpret_cast<ScintillaGTK *>(sci->pscin);
2709 psci->ctrlID = id;
2712 void scintilla_release_resources(void) {
2713 Platform_Finalise();