Build section: minor rewording, formatting fixes; some sentences
[geany-mirror.git] / scintilla / ScintillaGTK.cxx
bloba0f55fd2bc03d5aa488a3472c797a4fa41cbbf2a
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 <new>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <ctype.h>
11 #include <time.h>
13 #include <string>
14 #include <vector>
16 #include <gtk/gtk.h>
17 #include <gdk/gdkkeysyms.h>
19 #include "Platform.h"
21 #if PLAT_GTK_WIN32
22 #include "windows.h"
23 #endif
25 #include "Scintilla.h"
26 #include "ScintillaWidget.h"
27 #ifdef SCI_LEXER
28 #include "SciLexer.h"
29 #include "PropSet.h"
30 #include "PropSetSimple.h"
31 #include "Accessor.h"
32 #include "KeyWords.h"
33 #endif
34 #include "SVector.h"
35 #include "SplitVector.h"
36 #include "Partitioning.h"
37 #include "RunStyles.h"
38 #include "ContractionState.h"
39 #include "CellBuffer.h"
40 #include "CallTip.h"
41 #include "KeyMap.h"
42 #include "Indicator.h"
43 #include "XPM.h"
44 #include "LineMarker.h"
45 #include "Style.h"
46 #include "AutoComplete.h"
47 #include "ViewStyle.h"
48 #include "Decoration.h"
49 #include "CharClassify.h"
50 #include "Document.h"
51 #include "Selection.h"
52 #include "PositionCache.h"
53 #include "Editor.h"
54 #include "ScintillaBase.h"
55 #include "UniConversion.h"
57 #include "gtk/gtksignal.h"
58 #include "gtk/gtkmarshal.h"
59 #include "scintilla-marshal.h"
61 #ifdef SCI_LEXER
62 #include <glib.h>
63 #include <gmodule.h>
64 #include "ExternalLexer.h"
65 #endif
67 #include "Converter.h"
69 #ifdef _MSC_VER
70 // Constant conditional expressions are because of GTK+ headers
71 #pragma warning(disable: 4127)
72 // Ignore unreferenced local functions in GTK+ headers
73 #pragma warning(disable: 4505)
74 #endif
76 #if GTK_CHECK_VERSION(2,6,0)
77 #define USE_GTK_CLIPBOARD
78 #endif
80 #define OBJECT_CLASS GObjectClass
82 #ifdef SCI_NAMESPACE
83 using namespace Scintilla;
84 #endif
86 extern char *UTF8FromLatin1(const char *s, int &len);
88 class ScintillaGTK : public ScintillaBase {
89 _ScintillaObject *sci;
90 Window wText;
91 Window scrollbarv;
92 Window scrollbarh;
93 GtkObject *adjustmentv;
94 GtkObject *adjustmenth;
95 int scrollBarWidth;
96 int scrollBarHeight;
98 // Because clipboard access is asynchronous, copyText is created by Copy
99 #ifndef USE_GTK_CLIPBOARD
100 SelectionText copyText;
101 #endif
103 SelectionText primary;
105 GdkEventButton evbtn;
106 bool capturedMouse;
107 bool dragWasDropped;
108 int lastKey;
109 int rectangularSelectionModifier;
111 GtkWidgetClass *parentClass;
113 static GdkAtom atomClipboard;
114 static GdkAtom atomUTF8;
115 static GdkAtom atomString;
116 static GdkAtom atomUriList;
117 static GdkAtom atomDROPFILES_DND;
118 GdkAtom atomSought;
120 #if PLAT_GTK_WIN32
121 CLIPFORMAT cfColumnSelect;
122 #endif
124 Window wPreedit;
125 Window wPreeditDraw;
126 GtkIMContext *im_context;
128 // Wheel mouse support
129 unsigned int linesPerScroll;
130 GTimeVal lastWheelMouseTime;
131 gint lastWheelMouseDirection;
132 gint wheelMouseIntensity;
134 GdkRegion *rgnUpdate;
136 // Private so ScintillaGTK objects can not be copied
137 ScintillaGTK(const ScintillaGTK &);
138 ScintillaGTK &operator=(const ScintillaGTK &);
140 public:
141 ScintillaGTK(_ScintillaObject *sci_);
142 virtual ~ScintillaGTK();
143 static void ClassInit(OBJECT_CLASS* object_class, GtkWidgetClass *widget_class, GtkContainerClass *container_class);
144 private:
145 virtual void Initialise();
146 virtual void Finalise();
147 virtual void DisplayCursor(Window::Cursor c);
148 virtual bool DragThreshold(Point ptStart, Point ptNow);
149 virtual void StartDrag();
150 int TargetAsUTF8(char *text);
151 int EncodedFromUTF8(char *utf8, char *encoded);
152 virtual bool ValidCodePage(int codePage) const;
153 public: // Public for scintilla_send_message
154 virtual sptr_t WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
155 private:
156 virtual sptr_t DefWndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam);
157 virtual void SetTicking(bool on);
158 virtual bool SetIdle(bool on);
159 virtual void SetMouseCapture(bool on);
160 virtual bool HaveMouseCapture();
161 virtual bool PaintContains(PRectangle rc);
162 void FullPaint();
163 virtual PRectangle GetClientRectangle();
164 void SyncPaint(PRectangle rc);
165 virtual void ScrollText(int linesToMove);
166 virtual void SetVerticalScrollPos();
167 virtual void SetHorizontalScrollPos();
168 virtual bool ModifyScrollBars(int nMax, int nPage);
169 void ReconfigureScrollBars();
170 virtual void NotifyChange();
171 virtual void NotifyFocus(bool focus);
172 virtual void NotifyParent(SCNotification scn);
173 void NotifyKey(int key, int modifiers);
174 void NotifyURIDropped(const char *list);
175 const char *CharacterSetID() const;
176 virtual CaseFolder *CaseFolderForEncoding();
177 virtual std::string CaseMapString(const std::string &s, int caseMapping);
178 virtual int KeyDefault(int key, int modifiers);
179 virtual void CopyToClipboard(const SelectionText &selectedText);
180 virtual void Copy();
181 virtual void Paste();
182 virtual void CreateCallTipWindow(PRectangle rc);
183 virtual void AddToPopUp(const char *label, int cmd = 0, bool enabled = true);
184 bool OwnPrimarySelection();
185 virtual void ClaimSelection();
186 void GetGtkSelectionText(GtkSelectionData *selectionData, SelectionText &selText);
187 void ReceivedSelection(GtkSelectionData *selection_data);
188 void ReceivedDrop(GtkSelectionData *selection_data);
189 static void GetSelection(GtkSelectionData *selection_data, guint info, SelectionText *selected);
190 #ifdef USE_GTK_CLIPBOARD
191 void StoreOnClipboard(SelectionText *clipText);
192 static void ClipboardGetSelection(GtkClipboard* clip, GtkSelectionData *selection_data, guint info, void *data);
193 static void ClipboardClearSelection(GtkClipboard* clip, void *data);
194 #endif
196 void UnclaimSelection(GdkEventSelection *selection_event);
197 void Resize(int width, int height);
199 // Callback functions
200 void RealizeThis(GtkWidget *widget);
201 static void Realize(GtkWidget *widget);
202 void UnRealizeThis(GtkWidget *widget);
203 static void UnRealize(GtkWidget *widget);
204 void MapThis();
205 static void Map(GtkWidget *widget);
206 void UnMapThis();
207 static void UnMap(GtkWidget *widget);
208 static gint CursorMoved(GtkWidget *widget, int xoffset, int yoffset, ScintillaGTK *sciThis);
209 gint FocusInThis(GtkWidget *widget);
210 static gint FocusIn(GtkWidget *widget, GdkEventFocus *event);
211 gint FocusOutThis(GtkWidget *widget);
212 static gint FocusOut(GtkWidget *widget, GdkEventFocus *event);
213 static void SizeRequest(GtkWidget *widget, GtkRequisition *requisition);
214 static void SizeAllocate(GtkWidget *widget, GtkAllocation *allocation);
215 gint Expose(GtkWidget *widget, GdkEventExpose *ose);
216 static gint ExposeMain(GtkWidget *widget, GdkEventExpose *ose);
217 static void Draw(GtkWidget *widget, GdkRectangle *area);
218 void ForAll(GtkCallback callback, gpointer callback_data);
219 static void MainForAll(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data);
221 static void ScrollSignal(GtkAdjustment *adj, ScintillaGTK *sciThis);
222 static void ScrollHSignal(GtkAdjustment *adj, ScintillaGTK *sciThis);
223 gint PressThis(GdkEventButton *event);
224 static gint Press(GtkWidget *widget, GdkEventButton *event);
225 static gint MouseRelease(GtkWidget *widget, GdkEventButton *event);
226 static gint ScrollEvent(GtkWidget *widget, GdkEventScroll *event);
227 static gint Motion(GtkWidget *widget, GdkEventMotion *event);
228 gboolean KeyThis(GdkEventKey *event);
229 static gboolean KeyPress(GtkWidget *widget, GdkEventKey *event);
230 static gboolean KeyRelease(GtkWidget *widget, GdkEventKey *event);
231 gboolean ExposePreeditThis(GtkWidget *widget, GdkEventExpose *ose);
232 static gboolean ExposePreedit(GtkWidget *widget, GdkEventExpose *ose, ScintillaGTK *sciThis);
233 void CommitThis(char *str);
234 static void Commit(GtkIMContext *context, char *str, ScintillaGTK *sciThis);
235 void PreeditChangedThis();
236 static void PreeditChanged(GtkIMContext *context, ScintillaGTK *sciThis);
237 static gint StyleSetText(GtkWidget *widget, GtkStyle *previous, void*);
238 static gint RealizeText(GtkWidget *widget, void*);
239 static void Destroy(GObject *object);
240 static void SelectionReceived(GtkWidget *widget, GtkSelectionData *selection_data,
241 guint time);
242 static void SelectionGet(GtkWidget *widget, GtkSelectionData *selection_data,
243 guint info, guint time);
244 static gint SelectionClear(GtkWidget *widget, GdkEventSelection *selection_event);
245 static void DragBegin(GtkWidget *widget, GdkDragContext *context);
246 gboolean DragMotionThis(GdkDragContext *context, gint x, gint y, guint dragtime);
247 static gboolean DragMotion(GtkWidget *widget, GdkDragContext *context,
248 gint x, gint y, guint dragtime);
249 static void DragLeave(GtkWidget *widget, GdkDragContext *context,
250 guint time);
251 static void DragEnd(GtkWidget *widget, GdkDragContext *context);
252 static gboolean Drop(GtkWidget *widget, GdkDragContext *context,
253 gint x, gint y, guint time);
254 static void DragDataReceived(GtkWidget *widget, GdkDragContext *context,
255 gint x, gint y, GtkSelectionData *selection_data, guint info, guint time);
256 static void DragDataGet(GtkWidget *widget, GdkDragContext *context,
257 GtkSelectionData *selection_data, guint info, guint time);
258 static gint TimeOut(ScintillaGTK *sciThis);
259 static gint IdleCallback(ScintillaGTK *sciThis);
260 static void PopUpCB(ScintillaGTK *sciThis, guint action, GtkWidget *widget);
262 gint ExposeTextThis(GtkWidget *widget, GdkEventExpose *ose);
263 static gint ExposeText(GtkWidget *widget, GdkEventExpose *ose, ScintillaGTK *sciThis);
265 static gint ExposeCT(GtkWidget *widget, GdkEventExpose *ose, CallTip *ct);
266 static gint PressCT(GtkWidget *widget, GdkEventButton *event, ScintillaGTK *sciThis);
268 static sptr_t DirectFunction(ScintillaGTK *sciThis,
269 unsigned int iMessage, uptr_t wParam, sptr_t lParam);
272 enum {
273 COMMAND_SIGNAL,
274 NOTIFY_SIGNAL,
275 LAST_SIGNAL
278 static gint scintilla_signals[LAST_SIGNAL] = { 0 };
280 enum {
281 TARGET_STRING,
282 TARGET_TEXT,
283 TARGET_COMPOUND_TEXT,
284 TARGET_UTF8_STRING,
285 TARGET_URI
288 GdkAtom ScintillaGTK::atomClipboard = 0;
289 GdkAtom ScintillaGTK::atomUTF8 = 0;
290 GdkAtom ScintillaGTK::atomString = 0;
291 GdkAtom ScintillaGTK::atomUriList = 0;
292 GdkAtom ScintillaGTK::atomDROPFILES_DND = 0;
294 static const GtkTargetEntry clipboardCopyTargets[] = {
295 { (gchar *) "UTF8_STRING", 0, TARGET_UTF8_STRING },
296 { (gchar *) "STRING", 0, TARGET_STRING },
298 static const gint nClipboardCopyTargets = sizeof(clipboardCopyTargets) / sizeof(clipboardCopyTargets[0]);
300 static const GtkTargetEntry clipboardPasteTargets[] = {
301 { (gchar *) "text/uri-list", 0, TARGET_URI },
302 { (gchar *) "UTF8_STRING", 0, TARGET_UTF8_STRING },
303 { (gchar *) "STRING", 0, TARGET_STRING },
305 static const gint nClipboardPasteTargets = sizeof(clipboardPasteTargets) / sizeof(clipboardPasteTargets[0]);
307 static GtkWidget *PWidget(Window &w) {
308 return reinterpret_cast<GtkWidget *>(w.GetID());
311 static ScintillaGTK *ScintillaFromWidget(GtkWidget *widget) {
312 ScintillaObject *scio = reinterpret_cast<ScintillaObject *>(widget);
313 return reinterpret_cast<ScintillaGTK *>(scio->pscin);
316 ScintillaGTK::ScintillaGTK(_ScintillaObject *sci_) :
317 adjustmentv(0), adjustmenth(0),
318 scrollBarWidth(30), scrollBarHeight(30),
319 capturedMouse(false), dragWasDropped(false),
320 lastKey(0), rectangularSelectionModifier(SCMOD_CTRL), parentClass(0),
321 im_context(NULL),
322 lastWheelMouseDirection(0),
323 wheelMouseIntensity(0),
324 rgnUpdate(0) {
325 sci = sci_;
326 wMain = GTK_WIDGET(sci);
328 #if PLAT_GTK_WIN32
329 rectangularSelectionModifier = SCMOD_ALT;
330 #else
331 rectangularSelectionModifier = SCMOD_CTRL;
332 #endif
334 #if PLAT_GTK_WIN32
335 // There does not seem to be a real standard for indicating that the clipboard
336 // contains a rectangular selection, so copy Developer Studio.
337 cfColumnSelect = static_cast<CLIPFORMAT>(
338 ::RegisterClipboardFormat("MSDEVColumnSelect"));
340 // Get intellimouse parameters when running on win32; otherwise use
341 // reasonable default
342 #ifndef SPI_GETWHEELSCROLLLINES
343 #define SPI_GETWHEELSCROLLLINES 104
344 #endif
345 ::SystemParametersInfo(SPI_GETWHEELSCROLLLINES, 0, &linesPerScroll, 0);
346 #else
347 linesPerScroll = 4;
348 #endif
349 lastWheelMouseTime.tv_sec = 0;
350 lastWheelMouseTime.tv_usec = 0;
352 Initialise();
355 ScintillaGTK::~ScintillaGTK() {
358 void ScintillaGTK::RealizeThis(GtkWidget *widget) {
359 //Platform::DebugPrintf("ScintillaGTK::realize this\n");
360 GTK_WIDGET_SET_FLAGS(widget, GTK_REALIZED);
361 GdkWindowAttr attrs;
362 attrs.window_type = GDK_WINDOW_CHILD;
363 attrs.x = widget->allocation.x;
364 attrs.y = widget->allocation.y;
365 attrs.width = widget->allocation.width;
366 attrs.height = widget->allocation.height;
367 attrs.wclass = GDK_INPUT_OUTPUT;
368 attrs.visual = gtk_widget_get_visual(widget);
369 attrs.colormap = gtk_widget_get_colormap(widget);
370 attrs.event_mask = gtk_widget_get_events(widget) | GDK_EXPOSURE_MASK;
371 GdkCursor *cursor = gdk_cursor_new(GDK_XTERM);
372 attrs.cursor = cursor;
373 widget->window = gdk_window_new(gtk_widget_get_parent_window(widget), &attrs,
374 GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP | GDK_WA_CURSOR);
375 gdk_window_set_user_data(widget->window, widget);
376 gdk_window_set_background(widget->window, &widget->style->bg[GTK_STATE_NORMAL]);
377 gdk_window_show(widget->window);
378 gdk_cursor_destroy(cursor);
379 widget->style = gtk_style_attach(widget->style, widget->window);
380 wPreedit = gtk_window_new(GTK_WINDOW_POPUP);
381 wPreeditDraw = gtk_drawing_area_new();
382 GtkWidget *predrw = PWidget(wPreeditDraw); // No code inside the G_OBJECT macro
383 g_signal_connect(G_OBJECT(predrw), "expose_event",
384 G_CALLBACK(ExposePreedit), this);
385 gtk_container_add(GTK_CONTAINER(PWidget(wPreedit)), predrw);
386 gtk_widget_realize(PWidget(wPreedit));
387 gtk_widget_realize(predrw);
388 gtk_widget_show(predrw);
390 im_context = gtk_im_multicontext_new();
391 g_signal_connect(G_OBJECT(im_context), "commit",
392 G_CALLBACK(Commit), this);
393 g_signal_connect(G_OBJECT(im_context), "preedit_changed",
394 G_CALLBACK(PreeditChanged), this);
395 gtk_im_context_set_client_window(im_context, widget->window);
396 GtkWidget *widtxt = PWidget(wText); // // No code inside the G_OBJECT macro
397 g_signal_connect_after(G_OBJECT(widtxt), "style_set",
398 G_CALLBACK(ScintillaGTK::StyleSetText), NULL);
399 g_signal_connect_after(G_OBJECT(widtxt), "realize",
400 G_CALLBACK(ScintillaGTK::RealizeText), NULL);
401 gtk_widget_realize(widtxt);
402 gtk_widget_realize(PWidget(scrollbarv));
403 gtk_widget_realize(PWidget(scrollbarh));
406 void ScintillaGTK::Realize(GtkWidget *widget) {
407 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
408 sciThis->RealizeThis(widget);
411 void ScintillaGTK::UnRealizeThis(GtkWidget *widget) {
412 try {
413 if (GTK_WIDGET_MAPPED(widget)) {
414 gtk_widget_unmap(widget);
416 GTK_WIDGET_UNSET_FLAGS(widget, GTK_REALIZED);
417 gtk_widget_unrealize(PWidget(wText));
418 gtk_widget_unrealize(PWidget(scrollbarv));
419 gtk_widget_unrealize(PWidget(scrollbarh));
420 gtk_widget_unrealize(PWidget(wPreedit));
421 gtk_widget_unrealize(PWidget(wPreeditDraw));
422 g_object_unref(im_context);
423 im_context = NULL;
424 if (GTK_WIDGET_CLASS(parentClass)->unrealize)
425 GTK_WIDGET_CLASS(parentClass)->unrealize(widget);
427 Finalise();
428 } catch (...) {
429 errorStatus = SC_STATUS_FAILURE;
433 void ScintillaGTK::UnRealize(GtkWidget *widget) {
434 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
435 sciThis->UnRealizeThis(widget);
438 static void MapWidget(GtkWidget *widget) {
439 if (widget &&
440 GTK_WIDGET_VISIBLE(widget) &&
441 !GTK_WIDGET_MAPPED(widget)) {
442 gtk_widget_map(widget);
446 void ScintillaGTK::MapThis() {
447 try {
448 //Platform::DebugPrintf("ScintillaGTK::map this\n");
449 GTK_WIDGET_SET_FLAGS(PWidget(wMain), GTK_MAPPED);
450 MapWidget(PWidget(wText));
451 MapWidget(PWidget(scrollbarh));
452 MapWidget(PWidget(scrollbarv));
453 wMain.SetCursor(Window::cursorArrow);
454 scrollbarv.SetCursor(Window::cursorArrow);
455 scrollbarh.SetCursor(Window::cursorArrow);
456 ChangeSize();
457 gdk_window_show(PWidget(wMain)->window);
458 } catch (...) {
459 errorStatus = SC_STATUS_FAILURE;
463 void ScintillaGTK::Map(GtkWidget *widget) {
464 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
465 sciThis->MapThis();
468 void ScintillaGTK::UnMapThis() {
469 try {
470 //Platform::DebugPrintf("ScintillaGTK::unmap this\n");
471 GTK_WIDGET_UNSET_FLAGS(PWidget(wMain), GTK_MAPPED);
472 DropGraphics();
473 gdk_window_hide(PWidget(wMain)->window);
474 gtk_widget_unmap(PWidget(wText));
475 gtk_widget_unmap(PWidget(scrollbarh));
476 gtk_widget_unmap(PWidget(scrollbarv));
477 } catch (...) {
478 errorStatus = SC_STATUS_FAILURE;
482 void ScintillaGTK::UnMap(GtkWidget *widget) {
483 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
484 sciThis->UnMapThis();
487 void ScintillaGTK::ForAll(GtkCallback callback, gpointer callback_data) {
488 try {
489 (*callback) (PWidget(wText), callback_data);
490 (*callback) (PWidget(scrollbarv), callback_data);
491 (*callback) (PWidget(scrollbarh), callback_data);
492 } catch (...) {
493 errorStatus = SC_STATUS_FAILURE;
497 void ScintillaGTK::MainForAll(GtkContainer *container, gboolean include_internals, GtkCallback callback, gpointer callback_data) {
498 ScintillaGTK *sciThis = ScintillaFromWidget((GtkWidget *)container);
500 if (callback != NULL && include_internals) {
501 sciThis->ForAll(callback, callback_data);
505 gint ScintillaGTK::CursorMoved(GtkWidget *, int xoffset, int yoffset, ScintillaGTK *sciThis) {
506 GdkRectangle area;
507 area.x = xoffset;
508 area.y = yoffset;
509 area.width = 1;
510 area.height = 1;
511 gtk_im_context_set_cursor_location(sciThis->im_context, &area);
512 return FALSE;
515 gint ScintillaGTK::FocusInThis(GtkWidget *widget) {
516 try {
517 GTK_WIDGET_SET_FLAGS(widget, GTK_HAS_FOCUS);
518 SetFocusState(true);
519 if (im_context != NULL) {
520 gchar *str = NULL;
521 gint cursor_pos;
523 gtk_im_context_get_preedit_string(im_context, &str, NULL, &cursor_pos);
524 if (PWidget(wPreedit) != NULL) {
525 if (strlen(str) > 0) {
526 gtk_widget_show(PWidget(wPreedit));
527 } else {
528 gtk_widget_hide(PWidget(wPreedit));
531 g_free(str);
532 gtk_im_context_focus_in(im_context);
535 } catch (...) {
536 errorStatus = SC_STATUS_FAILURE;
538 return FALSE;
541 gint ScintillaGTK::FocusIn(GtkWidget *widget, GdkEventFocus * /*event*/) {
542 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
543 return sciThis->FocusInThis(widget);
546 gint ScintillaGTK::FocusOutThis(GtkWidget *widget) {
547 try {
548 GTK_WIDGET_UNSET_FLAGS(widget, GTK_HAS_FOCUS);
549 SetFocusState(false);
551 if (PWidget(wPreedit) != NULL)
552 gtk_widget_hide(PWidget(wPreedit));
553 if (im_context != NULL)
554 gtk_im_context_focus_out(im_context);
556 } catch (...) {
557 errorStatus = SC_STATUS_FAILURE;
559 return FALSE;
562 gint ScintillaGTK::FocusOut(GtkWidget *widget, GdkEventFocus * /*event*/) {
563 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
564 return sciThis->FocusOutThis(widget);
567 void ScintillaGTK::SizeRequest(GtkWidget *widget, GtkRequisition *requisition) {
568 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
569 requisition->width = 600;
570 requisition->height = gdk_screen_height();
571 GtkRequisition child_requisition;
572 gtk_widget_size_request(PWidget(sciThis->scrollbarh), &child_requisition);
573 gtk_widget_size_request(PWidget(sciThis->scrollbarv), &child_requisition);
576 void ScintillaGTK::SizeAllocate(GtkWidget *widget, GtkAllocation *allocation) {
577 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
578 try {
579 widget->allocation = *allocation;
580 if (GTK_WIDGET_REALIZED(widget))
581 gdk_window_move_resize(widget->window,
582 widget->allocation.x,
583 widget->allocation.y,
584 widget->allocation.width,
585 widget->allocation.height);
587 sciThis->Resize(allocation->width, allocation->height);
589 } catch (...) {
590 sciThis->errorStatus = SC_STATUS_FAILURE;
594 void ScintillaGTK::Initialise() {
595 //Platform::DebugPrintf("ScintillaGTK::Initialise\n");
596 parentClass = reinterpret_cast<GtkWidgetClass *>(
597 gtk_type_class(gtk_container_get_type()));
599 GTK_WIDGET_SET_FLAGS(PWidget(wMain), GTK_CAN_FOCUS);
600 GTK_WIDGET_SET_FLAGS(GTK_WIDGET(PWidget(wMain)), GTK_SENSITIVE);
601 gtk_widget_set_events(PWidget(wMain),
602 GDK_EXPOSURE_MASK
603 | GDK_STRUCTURE_MASK
604 | GDK_KEY_PRESS_MASK
605 | GDK_KEY_RELEASE_MASK
606 | GDK_FOCUS_CHANGE_MASK
607 | GDK_LEAVE_NOTIFY_MASK
608 | GDK_BUTTON_PRESS_MASK
609 | GDK_BUTTON_RELEASE_MASK
610 | GDK_POINTER_MOTION_MASK
611 | GDK_POINTER_MOTION_HINT_MASK);
613 wText = gtk_drawing_area_new();
614 gtk_widget_set_parent(PWidget(wText), PWidget(wMain));
615 GtkWidget *widtxt = PWidget(wText); // No code inside the G_OBJECT macro
616 gtk_widget_show(widtxt);
617 g_signal_connect(G_OBJECT(widtxt), "expose_event",
618 G_CALLBACK(ScintillaGTK::ExposeText), this);
619 gtk_widget_set_events(widtxt, GDK_EXPOSURE_MASK);
620 // Avoid background drawing flash
621 gtk_widget_set_double_buffered(widtxt, FALSE);
622 gtk_drawing_area_size(GTK_DRAWING_AREA(widtxt),
623 100,100);
624 adjustmentv = gtk_adjustment_new(0.0, 0.0, 201.0, 1.0, 20.0, 20.0);
625 scrollbarv = gtk_vscrollbar_new(GTK_ADJUSTMENT(adjustmentv));
626 GTK_WIDGET_UNSET_FLAGS(PWidget(scrollbarv), GTK_CAN_FOCUS);
627 g_signal_connect(G_OBJECT(adjustmentv), "value_changed",
628 G_CALLBACK(ScrollSignal), this);
629 gtk_widget_set_parent(PWidget(scrollbarv), PWidget(wMain));
630 gtk_widget_show(PWidget(scrollbarv));
632 adjustmenth = gtk_adjustment_new(0.0, 0.0, 101.0, 1.0, 20.0, 20.0);
633 scrollbarh = gtk_hscrollbar_new(GTK_ADJUSTMENT(adjustmenth));
634 GTK_WIDGET_UNSET_FLAGS(PWidget(scrollbarh), GTK_CAN_FOCUS);
635 g_signal_connect(G_OBJECT(adjustmenth), "value_changed",
636 G_CALLBACK(ScrollHSignal), this);
637 gtk_widget_set_parent(PWidget(scrollbarh), PWidget(wMain));
638 gtk_widget_show(PWidget(scrollbarh));
640 gtk_widget_grab_focus(PWidget(wMain));
642 gtk_selection_add_targets(GTK_WIDGET(PWidget(wMain)), GDK_SELECTION_PRIMARY,
643 clipboardCopyTargets, nClipboardCopyTargets);
645 #ifndef USE_GTK_CLIPBOARD
646 gtk_selection_add_targets(GTK_WIDGET(PWidget(wMain)), atomClipboard,
647 clipboardPasteTargets, nClipboardPasteTargets);
648 #endif
650 gtk_drag_dest_set(GTK_WIDGET(PWidget(wMain)),
651 GTK_DEST_DEFAULT_ALL, clipboardPasteTargets, nClipboardPasteTargets,
652 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE));
654 // Set caret period based on GTK settings
655 gboolean blinkOn = false;
656 if (g_object_class_find_property(G_OBJECT_GET_CLASS(
657 G_OBJECT(gtk_settings_get_default())), "gtk-cursor-blink")) {
658 g_object_get(G_OBJECT(
659 gtk_settings_get_default()), "gtk-cursor-blink", &blinkOn, NULL);
661 if (blinkOn &&
662 g_object_class_find_property(G_OBJECT_GET_CLASS(
663 G_OBJECT(gtk_settings_get_default())), "gtk-cursor-blink-time")) {
664 gint value;
665 g_object_get(G_OBJECT(
666 gtk_settings_get_default()), "gtk-cursor-blink-time", &value, NULL);
667 caret.period = gint(value / 1.75);
668 } else {
669 caret.period = 0;
672 SetTicking(true);
675 void ScintillaGTK::Finalise() {
676 SetTicking(false);
677 ScintillaBase::Finalise();
680 void ScintillaGTK::DisplayCursor(Window::Cursor c) {
681 if (cursorMode == SC_CURSORNORMAL)
682 wText.SetCursor(c);
683 else
684 wText.SetCursor(static_cast<Window::Cursor>(cursorMode));
687 bool ScintillaGTK::DragThreshold(Point ptStart, Point ptNow) {
688 return gtk_drag_check_threshold(GTK_WIDGET(PWidget(wMain)),
689 ptStart.x, ptStart.y, ptNow.x, ptNow.y);
692 void ScintillaGTK::StartDrag() {
693 dragWasDropped = false;
694 inDragDrop = ddDragging;
695 GtkTargetList *tl = gtk_target_list_new(clipboardCopyTargets, nClipboardCopyTargets);
696 gtk_drag_begin(GTK_WIDGET(PWidget(wMain)),
698 static_cast<GdkDragAction>(GDK_ACTION_COPY | GDK_ACTION_MOVE),
699 evbtn.button,
700 reinterpret_cast<GdkEvent *>(&evbtn));
703 static char *ConvertText(int *lenResult, char *s, size_t len, const char *charSetDest,
704 const char *charSetSource, bool transliterations) {
705 // s is not const because of different versions of iconv disagreeing about const
706 *lenResult = 0;
707 char *destForm = 0;
708 Converter conv(charSetDest, charSetSource, transliterations);
709 if (conv) {
710 destForm = new char[len*3+1];
711 char *pin = s;
712 size_t inLeft = len;
713 char *pout = destForm;
714 size_t outLeft = len*3+1;
715 size_t conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
716 if (conversions == ((size_t)(-1))) {
717 fprintf(stderr, "iconv %s->%s failed for %s\n", charSetSource, charSetDest, static_cast<char *>(s));
718 delete []destForm;
719 destForm = 0;
720 } else {
721 //fprintf(stderr, "iconv OK %s %d\n", destForm, pout - destForm);
722 *pout = '\0';
723 *lenResult = pout - destForm;
725 } else {
726 fprintf(stderr, "Can not iconv %s %s\n", charSetDest, charSetSource);
728 if (!destForm) {
729 destForm = new char[1];
730 destForm[0] = '\0';
731 *lenResult = 0;
733 return destForm;
736 // Returns the target converted to UTF8.
737 // Return the length in bytes.
738 int ScintillaGTK::TargetAsUTF8(char *text) {
739 int targetLength = targetEnd - targetStart;
740 if (IsUnicodeMode()) {
741 if (text) {
742 pdoc->GetCharRange(text, targetStart, targetLength);
744 } else {
745 // Need to convert
746 const char *charSetBuffer = CharacterSetID();
747 if (*charSetBuffer) {
748 //~ fprintf(stderr, "AsUTF8 %s %d %0d-%0d\n", charSetBuffer, targetLength, targetStart, targetEnd);
749 char *s = new char[targetLength];
750 if (s) {
751 pdoc->GetCharRange(s, targetStart, targetLength);
752 //~ fprintf(stderr, " \"%s\"\n", s);
753 if (text) {
754 char *tmputf = ConvertText(&targetLength, s, targetLength, "UTF-8", charSetBuffer, false);
755 memcpy(text, tmputf, targetLength);
756 delete []tmputf;
757 //~ fprintf(stderr, " \"%s\"\n", text);
759 delete []s;
761 } else {
762 if (text) {
763 pdoc->GetCharRange(text, targetStart, targetLength);
767 //~ fprintf(stderr, "Length = %d bytes\n", targetLength);
768 return targetLength;
771 // Translates a nul terminated UTF8 string into the document encoding.
772 // Return the length of the result in bytes.
773 int ScintillaGTK::EncodedFromUTF8(char *utf8, char *encoded) {
774 int inputLength = (lengthForEncode >= 0) ? lengthForEncode : strlen(utf8);
775 if (IsUnicodeMode()) {
776 if (encoded) {
777 memcpy(encoded, utf8, inputLength);
779 return inputLength;
780 } else {
781 // Need to convert
782 const char *charSetBuffer = CharacterSetID();
783 if (*charSetBuffer) {
784 int outLength = 0;
785 char *tmpEncoded = ConvertText(&outLength, utf8, inputLength, charSetBuffer, "UTF-8", true);
786 if (tmpEncoded) {
787 if (encoded) {
788 memcpy(encoded, tmpEncoded, outLength);
790 delete []tmpEncoded;
792 return outLength;
793 } else {
794 if (encoded) {
795 memcpy(encoded, utf8, inputLength);
797 return inputLength;
800 // Fail
801 return 0;
804 bool ScintillaGTK::ValidCodePage(int codePage) const {
805 return codePage == 0
806 || codePage == SC_CP_UTF8
807 || codePage == 932
808 || codePage == 936
809 || codePage == 950
810 || codePage == SC_CP_DBCS;
813 sptr_t ScintillaGTK::WndProc(unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
814 try {
815 switch (iMessage) {
817 case SCI_GRABFOCUS:
818 gtk_widget_grab_focus(PWidget(wMain));
819 break;
821 case SCI_GETDIRECTFUNCTION:
822 return reinterpret_cast<sptr_t>(DirectFunction);
824 case SCI_GETDIRECTPOINTER:
825 return reinterpret_cast<sptr_t>(this);
827 #ifdef SCI_LEXER
828 case SCI_LOADLEXERLIBRARY:
829 LexerManager::GetInstance()->Load(reinterpret_cast<const char*>(wParam));
830 break;
831 #endif
832 case SCI_TARGETASUTF8:
833 return TargetAsUTF8(reinterpret_cast<char*>(lParam));
835 case SCI_ENCODEDFROMUTF8:
836 return EncodedFromUTF8(reinterpret_cast<char*>(wParam),
837 reinterpret_cast<char*>(lParam));
839 case SCI_SETRECTANGULARSELECTIONMODIFIER:
840 rectangularSelectionModifier = wParam;
841 break;
843 case SCI_GETRECTANGULARSELECTIONMODIFIER:
844 return rectangularSelectionModifier;
846 default:
847 return ScintillaBase::WndProc(iMessage, wParam, lParam);
849 } catch (std::bad_alloc&) {
850 errorStatus = SC_STATUS_BADALLOC;
851 } catch (...) {
852 errorStatus = SC_STATUS_FAILURE;
854 return 0l;
857 sptr_t ScintillaGTK::DefWndProc(unsigned int, uptr_t, sptr_t) {
858 return 0;
861 void ScintillaGTK::SetTicking(bool on) {
862 if (timer.ticking != on) {
863 timer.ticking = on;
864 if (timer.ticking) {
865 timer.tickerID = reinterpret_cast<TickerID>(gtk_timeout_add(timer.tickSize, (GtkFunction)TimeOut, this));
866 } else {
867 gtk_timeout_remove(GPOINTER_TO_UINT(timer.tickerID));
870 timer.ticksToWait = caret.period;
873 bool ScintillaGTK::SetIdle(bool on) {
874 if (on) {
875 // Start idler, if it's not running.
876 if (idler.state == false) {
877 idler.state = true;
878 idler.idlerID = reinterpret_cast<IdlerID>
879 (gtk_idle_add((GtkFunction)IdleCallback, this));
881 } else {
882 // Stop idler, if it's running
883 if (idler.state == true) {
884 idler.state = false;
885 gtk_idle_remove(GPOINTER_TO_UINT(idler.idlerID));
888 return true;
891 void ScintillaGTK::SetMouseCapture(bool on) {
892 if (mouseDownCaptures) {
893 if (on) {
894 gtk_grab_add(GTK_WIDGET(PWidget(wMain)));
895 } else {
896 gtk_grab_remove(GTK_WIDGET(PWidget(wMain)));
899 capturedMouse = on;
902 bool ScintillaGTK::HaveMouseCapture() {
903 return capturedMouse;
906 bool ScintillaGTK::PaintContains(PRectangle rc) {
907 bool contains = true;
908 if (paintState == painting) {
909 if (!rcPaint.Contains(rc)) {
910 contains = false;
911 } else if (rgnUpdate) {
912 GdkRectangle grc = {rc.left, rc.top,
913 rc.right - rc.left, rc.bottom - rc.top};
914 if (gdk_region_rect_in(rgnUpdate, &grc) != GDK_OVERLAP_RECTANGLE_IN) {
915 contains = false;
919 return contains;
922 // Redraw all of text area. This paint will not be abandoned.
923 void ScintillaGTK::FullPaint() {
924 wText.InvalidateAll();
927 PRectangle ScintillaGTK::GetClientRectangle() {
928 PRectangle rc = wMain.GetClientPosition();
929 if (verticalScrollBarVisible)
930 rc.right -= scrollBarWidth;
931 if (horizontalScrollBarVisible && (wrapState == eWrapNone))
932 rc.bottom -= scrollBarHeight;
933 // Move to origin
934 rc.right -= rc.left;
935 rc.bottom -= rc.top;
936 rc.left = 0;
937 rc.top = 0;
938 return rc;
941 // Synchronously paint a rectangle of the window.
942 void ScintillaGTK::SyncPaint(PRectangle rc) {
943 paintState = painting;
944 rcPaint = rc;
945 PRectangle rcClient = GetClientRectangle();
946 paintingAllText = rcPaint.Contains(rcClient);
947 if ((PWidget(wText))->window) {
948 Surface *sw = Surface::Allocate();
949 if (sw) {
950 sw->Init(PWidget(wText)->window, PWidget(wText));
951 Paint(sw, rc);
952 sw->Release();
953 delete sw;
956 if (paintState == paintAbandoned) {
957 // Painting area was insufficient to cover new styling or brace highlight positions
958 FullPaint();
960 paintState = notPainting;
963 void ScintillaGTK::ScrollText(int linesToMove) {
964 int diff = vs.lineHeight * -linesToMove;
965 //Platform::DebugPrintf("ScintillaGTK::ScrollText %d %d %0d,%0d %0d,%0d\n", linesToMove, diff,
966 // rc.left, rc.top, rc.right, rc.bottom);
967 GtkWidget *wi = PWidget(wText);
969 gdk_window_scroll(wi->window, 0, -diff);
970 gdk_window_process_updates(wi->window, FALSE);
973 void ScintillaGTK::SetVerticalScrollPos() {
974 DwellEnd(true);
975 gtk_adjustment_set_value(GTK_ADJUSTMENT(adjustmentv), topLine);
978 void ScintillaGTK::SetHorizontalScrollPos() {
979 DwellEnd(true);
980 gtk_adjustment_set_value(GTK_ADJUSTMENT(adjustmenth), xOffset / 2);
983 bool ScintillaGTK::ModifyScrollBars(int nMax, int nPage) {
984 bool modified = false;
985 int pageScroll = LinesToScroll();
987 if (GTK_ADJUSTMENT(adjustmentv)->upper != (nMax + 1) ||
988 GTK_ADJUSTMENT(adjustmentv)->page_size != nPage ||
989 GTK_ADJUSTMENT(adjustmentv)->page_increment != pageScroll) {
990 GTK_ADJUSTMENT(adjustmentv)->upper = nMax + 1;
991 GTK_ADJUSTMENT(adjustmentv)->page_size = nPage;
992 GTK_ADJUSTMENT(adjustmentv)->page_increment = pageScroll;
993 gtk_adjustment_changed(GTK_ADJUSTMENT(adjustmentv));
994 modified = true;
997 PRectangle rcText = GetTextRectangle();
998 int horizEndPreferred = scrollWidth;
999 if (horizEndPreferred < 0)
1000 horizEndPreferred = 0;
1001 unsigned int pageWidth = rcText.Width();
1002 unsigned int pageIncrement = pageWidth / 3;
1003 unsigned int charWidth = vs.styles[STYLE_DEFAULT].aveCharWidth;
1004 if (GTK_ADJUSTMENT(adjustmenth)->upper != horizEndPreferred ||
1005 GTK_ADJUSTMENT(adjustmenth)->page_size != pageWidth ||
1006 GTK_ADJUSTMENT(adjustmenth)->page_increment != pageIncrement ||
1007 GTK_ADJUSTMENT(adjustmenth)->step_increment != charWidth) {
1008 GTK_ADJUSTMENT(adjustmenth)->upper = horizEndPreferred;
1009 GTK_ADJUSTMENT(adjustmenth)->step_increment = charWidth;
1010 GTK_ADJUSTMENT(adjustmenth)->page_size = pageWidth;
1011 GTK_ADJUSTMENT(adjustmenth)->page_increment = pageIncrement;
1012 gtk_adjustment_changed(GTK_ADJUSTMENT(adjustmenth));
1013 modified = true;
1015 return modified;
1018 void ScintillaGTK::ReconfigureScrollBars() {
1019 PRectangle rc = wMain.GetClientPosition();
1020 Resize(rc.Width(), rc.Height());
1023 void ScintillaGTK::NotifyChange() {
1024 g_signal_emit(G_OBJECT(sci), scintilla_signals[COMMAND_SIGNAL], 0,
1025 Platform::LongFromTwoShorts(GetCtrlID(), SCEN_CHANGE), PWidget(wMain));
1028 void ScintillaGTK::NotifyFocus(bool focus) {
1029 g_signal_emit(G_OBJECT(sci), scintilla_signals[COMMAND_SIGNAL], 0,
1030 Platform::LongFromTwoShorts
1031 (GetCtrlID(), focus ? SCEN_SETFOCUS : SCEN_KILLFOCUS), PWidget(wMain));
1034 void ScintillaGTK::NotifyParent(SCNotification scn) {
1035 scn.nmhdr.hwndFrom = PWidget(wMain);
1036 scn.nmhdr.idFrom = GetCtrlID();
1037 g_signal_emit(G_OBJECT(sci), scintilla_signals[NOTIFY_SIGNAL], 0,
1038 GetCtrlID(), &scn);
1041 void ScintillaGTK::NotifyKey(int key, int modifiers) {
1042 SCNotification scn = {0};
1043 scn.nmhdr.code = SCN_KEY;
1044 scn.ch = key;
1045 scn.modifiers = modifiers;
1047 NotifyParent(scn);
1050 void ScintillaGTK::NotifyURIDropped(const char *list) {
1051 SCNotification scn = {0};
1052 scn.nmhdr.code = SCN_URIDROPPED;
1053 scn.text = list;
1055 NotifyParent(scn);
1058 const char *CharacterSetID(int characterSet);
1060 const char *ScintillaGTK::CharacterSetID() const {
1061 return ::CharacterSetID(vs.styles[STYLE_DEFAULT].characterSet);
1064 class CaseFolderUTF8 : public CaseFolderTable {
1065 public:
1066 CaseFolderUTF8() {
1067 StandardASCII();
1069 virtual size_t Fold(char *folded, size_t sizeFolded, const char *mixed, size_t lenMixed) {
1070 if ((lenMixed == 1) && (sizeFolded > 0)) {
1071 folded[0] = mapping[static_cast<unsigned char>(mixed[0])];
1072 return 1;
1073 } else {
1074 gchar *mapped = g_utf8_casefold(mixed, lenMixed);
1075 size_t lenMapped = strlen(mapped);
1076 if (lenMapped < sizeFolded) {
1077 memcpy(folded, mapped, lenMapped);
1078 } else {
1079 lenMapped = 0;
1081 g_free(mapped);
1082 return lenMapped;
1087 CaseFolder *ScintillaGTK::CaseFolderForEncoding() {
1088 if (pdoc->dbcsCodePage == SC_CP_UTF8) {
1089 return new CaseFolderUTF8();
1090 } else {
1091 CaseFolderTable *pcf = new CaseFolderTable();
1092 const char *charSetBuffer = CharacterSetID();
1093 if ((pdoc->dbcsCodePage == 0) && charSetBuffer) {
1094 pcf->StandardASCII();
1095 // Only for single byte encodings
1096 for (int i=0x80; i<0x100; i++) {
1097 char sCharacter[2] = "A";
1098 sCharacter[0] = i;
1099 int convertedLength = 1;
1100 const char *sUTF8 = ConvertText(&convertedLength, sCharacter, 1,
1101 "UTF-8", charSetBuffer, false);
1102 if (sUTF8) {
1103 gchar *mapped = g_utf8_casefold(sUTF8, strlen(sUTF8));
1104 if (mapped) {
1105 int mappedLength = strlen(mapped);
1106 const char *mappedBack = ConvertText(&mappedLength, mapped,
1107 mappedLength, charSetBuffer, "UTF-8", false);
1108 if (mappedBack && (strlen(mappedBack) == 1) && (mappedBack[0] != sCharacter[0])) {
1109 pcf->SetTranslation(sCharacter[0], mappedBack[0]);
1111 delete []mappedBack;
1112 g_free(mapped);
1115 delete []sUTF8;
1118 return pcf;
1122 std::string ScintillaGTK::CaseMapString(const std::string &s, int caseMapping) {
1123 if (s.size() == 0)
1124 return std::string();
1126 if (caseMapping == cmSame)
1127 return s;
1129 const char *needsFree1 = 0; // Must be freed with delete []
1130 const char *charSetBuffer = CharacterSetID();
1131 const char *sUTF8 = s.c_str();
1132 int rangeBytes = s.size();
1134 int convertedLength = rangeBytes;
1135 // Change text to UTF-8
1136 if (!IsUnicodeMode()) {
1137 // Need to convert
1138 if (*charSetBuffer) {
1139 sUTF8 = ConvertText(&convertedLength, const_cast<char *>(s.c_str()), rangeBytes,
1140 "UTF-8", charSetBuffer, false);
1141 needsFree1 = sUTF8;
1144 gchar *mapped; // Must be freed with g_free
1145 if (caseMapping == cmUpper) {
1146 mapped = g_utf8_strup(sUTF8, convertedLength);
1147 } else {
1148 mapped = g_utf8_strdown(sUTF8, convertedLength);
1150 int mappedLength = strlen(mapped);
1151 char *mappedBack = mapped;
1153 char *needsFree2 = 0; // Must be freed with delete []
1154 if (!IsUnicodeMode()) {
1155 if (*charSetBuffer) {
1156 mappedBack = ConvertText(&mappedLength, mapped, mappedLength, charSetBuffer, "UTF-8", false);
1157 needsFree2 = mappedBack;
1161 std::string ret(mappedBack, mappedLength);
1162 g_free(mapped);
1163 delete []needsFree1;
1164 delete []needsFree2;
1165 return ret;
1168 int ScintillaGTK::KeyDefault(int key, int modifiers) {
1169 if (!(modifiers & SCI_CTRL) && !(modifiers & SCI_ALT)) {
1170 if (key < 256) {
1171 NotifyKey(key, modifiers);
1172 return 0;
1173 } else {
1174 // Pass up to container in case it is an accelerator
1175 NotifyKey(key, modifiers);
1176 return 0;
1178 } else {
1179 // Pass up to container in case it is an accelerator
1180 NotifyKey(key, modifiers);
1181 return 0;
1183 //Platform::DebugPrintf("SK-key: %d %x %x\n",key, modifiers);
1186 void ScintillaGTK::CopyToClipboard(const SelectionText &selectedText) {
1187 #ifndef USE_GTK_CLIPBOARD
1188 copyText.Copy(selectedText);
1189 gtk_selection_owner_set(GTK_WIDGET(PWidget(wMain)),
1190 atomClipboard,
1191 GDK_CURRENT_TIME);
1192 #else
1193 SelectionText *clipText = new SelectionText();
1194 clipText->Copy(selectedText);
1195 StoreOnClipboard(clipText);
1196 #endif
1199 void ScintillaGTK::Copy() {
1200 if (!sel.Empty()) {
1201 #ifndef USE_GTK_CLIPBOARD
1202 CopySelectionRange(&copyText);
1203 gtk_selection_owner_set(GTK_WIDGET(PWidget(wMain)),
1204 atomClipboard,
1205 GDK_CURRENT_TIME);
1206 #else
1207 SelectionText *clipText = new SelectionText();
1208 CopySelectionRange(clipText);
1209 StoreOnClipboard(clipText);
1210 #endif
1211 #if PLAT_GTK_WIN32
1212 if (sel.IsRectangular()) {
1213 ::OpenClipboard(NULL);
1214 ::SetClipboardData(cfColumnSelect, 0);
1215 ::CloseClipboard();
1217 #endif
1221 void ScintillaGTK::Paste() {
1222 atomSought = atomUTF8;
1223 gtk_selection_convert(GTK_WIDGET(PWidget(wMain)),
1224 atomClipboard, atomSought, GDK_CURRENT_TIME);
1227 void ScintillaGTK::CreateCallTipWindow(PRectangle rc) {
1228 if (!ct.wCallTip.Created()) {
1229 ct.wCallTip = gtk_window_new(GTK_WINDOW_POPUP);
1230 ct.wDraw = gtk_drawing_area_new();
1231 GtkWidget *widcdrw = PWidget(ct.wDraw); // // No code inside the G_OBJECT macro
1232 gtk_container_add(GTK_CONTAINER(PWidget(ct.wCallTip)), widcdrw);
1233 g_signal_connect(G_OBJECT(widcdrw), "expose_event",
1234 G_CALLBACK(ScintillaGTK::ExposeCT), &ct);
1235 g_signal_connect(G_OBJECT(widcdrw), "button_press_event",
1236 G_CALLBACK(ScintillaGTK::PressCT), static_cast<void *>(this));
1237 gtk_widget_set_events(widcdrw,
1238 GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK);
1240 gtk_drawing_area_size(GTK_DRAWING_AREA(PWidget(ct.wDraw)),
1241 rc.Width(), rc.Height());
1242 ct.wDraw.Show();
1243 if (PWidget(ct.wCallTip)->window) {
1244 gdk_window_resize(PWidget(ct.wCallTip)->window, rc.Width(), rc.Height());
1248 void ScintillaGTK::AddToPopUp(const char *label, int cmd, bool enabled) {
1249 char fulllabel[200];
1250 strcpy(fulllabel, "/");
1251 strcat(fulllabel, label);
1252 GtkItemFactoryCallback menuSig = GtkItemFactoryCallback(PopUpCB);
1253 GtkItemFactoryEntry itemEntry = {
1254 fulllabel, NULL,
1255 menuSig,
1256 cmd,
1257 const_cast<gchar *>(label[0] ? "<Item>" : "<Separator>"),
1258 NULL
1260 gtk_item_factory_create_item(GTK_ITEM_FACTORY(popup.GetID()),
1261 &itemEntry, this, 1);
1262 if (cmd) {
1263 GtkWidget *item = gtk_item_factory_get_widget_by_action(
1264 reinterpret_cast<GtkItemFactory *>(popup.GetID()), cmd);
1265 if (item)
1266 gtk_widget_set_sensitive(item, enabled);
1270 bool ScintillaGTK::OwnPrimarySelection() {
1271 return ((gdk_selection_owner_get(GDK_SELECTION_PRIMARY)
1272 == GTK_WIDGET(PWidget(wMain))->window) &&
1273 (GTK_WIDGET(PWidget(wMain))->window != NULL));
1276 void ScintillaGTK::ClaimSelection() {
1277 // X Windows has a 'primary selection' as well as the clipboard.
1278 // Whenever the user selects some text, we become the primary selection
1279 if (!sel.Empty() && GTK_WIDGET_REALIZED(GTK_WIDGET(PWidget(wMain)))) {
1280 primarySelection = true;
1281 gtk_selection_owner_set(GTK_WIDGET(PWidget(wMain)),
1282 GDK_SELECTION_PRIMARY, GDK_CURRENT_TIME);
1283 primary.Free();
1284 } else if (OwnPrimarySelection()) {
1285 primarySelection = true;
1286 if (primary.s == NULL)
1287 gtk_selection_owner_set(NULL, GDK_SELECTION_PRIMARY, GDK_CURRENT_TIME);
1288 } else {
1289 primarySelection = false;
1290 primary.Free();
1294 // Detect rectangular text, convert line ends to current mode, convert from or to UTF-8
1295 void ScintillaGTK::GetGtkSelectionText(GtkSelectionData *selectionData, SelectionText &selText) {
1296 char *data = reinterpret_cast<char *>(selectionData->data);
1297 int len = selectionData->length;
1298 GdkAtom selectionTypeData = selectionData->type;
1300 // Return empty string if selection is not a string
1301 if ((selectionTypeData != GDK_TARGET_STRING) && (selectionTypeData != atomUTF8)) {
1302 char *empty = new char[1];
1303 empty[0] = '\0';
1304 selText.Set(empty, 0, SC_CP_UTF8, 0, false, false);
1305 return;
1308 // Check for "\n\0" ending to string indicating that selection is rectangular
1309 bool isRectangular;
1310 #if PLAT_GTK_WIN32
1311 isRectangular = ::IsClipboardFormatAvailable(cfColumnSelect) != 0;
1312 #else
1313 isRectangular = ((len > 2) && (data[len - 1] == 0 && data[len - 2] == '\n'));
1314 if (isRectangular)
1315 len--; // Forget the extra '\0'
1316 #endif
1318 char *dest;
1319 if (selectionTypeData == GDK_TARGET_STRING) {
1320 dest = Document::TransformLineEnds(&len, data, len, pdoc->eolMode);
1321 if (IsUnicodeMode()) {
1322 // Unknown encoding so assume in Latin1
1323 char *destPrevious = dest;
1324 dest = UTF8FromLatin1(dest, len);
1325 selText.Set(dest, len, SC_CP_UTF8, 0, selText.rectangular, false);
1326 delete []destPrevious;
1327 } else {
1328 // Assume buffer is in same encoding as selection
1329 selText.Set(dest, len, pdoc->dbcsCodePage,
1330 vs.styles[STYLE_DEFAULT].characterSet, isRectangular, false);
1332 } else { // UTF-8
1333 dest = Document::TransformLineEnds(&len, data, len, pdoc->eolMode);
1334 selText.Set(dest, len, SC_CP_UTF8, 0, isRectangular, false);
1335 const char *charSetBuffer = CharacterSetID();
1336 if (!IsUnicodeMode() && *charSetBuffer) {
1337 // Convert to locale
1338 dest = ConvertText(&len, selText.s, selText.len, charSetBuffer, "UTF-8", true);
1339 selText.Set(dest, len, pdoc->dbcsCodePage,
1340 vs.styles[STYLE_DEFAULT].characterSet, selText.rectangular, false);
1345 void ScintillaGTK::ReceivedSelection(GtkSelectionData *selection_data) {
1346 try {
1347 if ((selection_data->selection == atomClipboard) ||
1348 (selection_data->selection == GDK_SELECTION_PRIMARY)) {
1349 if ((atomSought == atomUTF8) && (selection_data->length <= 0)) {
1350 atomSought = atomString;
1351 gtk_selection_convert(GTK_WIDGET(PWidget(wMain)),
1352 selection_data->selection, atomSought, GDK_CURRENT_TIME);
1353 } else if ((selection_data->length > 0) &&
1354 ((selection_data->type == GDK_TARGET_STRING) || (selection_data->type == atomUTF8))) {
1355 SelectionText selText;
1356 GetGtkSelectionText(selection_data, selText);
1358 UndoGroup ug(pdoc);
1359 if (selection_data->selection != GDK_SELECTION_PRIMARY) {
1360 ClearSelection();
1362 SelectionPosition selStart = sel.IsRectangular() ?
1363 sel.Rectangular().Start() :
1364 sel.Range(sel.Main()).Start();
1366 if (selText.rectangular) {
1367 PasteRectangular(selStart, selText.s, selText.len);
1368 } else {
1369 InsertPaste(selStart, selText.s, selText.len);
1371 EnsureCaretVisible();
1374 // else fprintf(stderr, "Target non string %d %d\n", (int)(selection_data->type),
1375 // (int)(atomUTF8));
1376 Redraw();
1377 } catch (...) {
1378 errorStatus = SC_STATUS_FAILURE;
1382 void ScintillaGTK::ReceivedDrop(GtkSelectionData *selection_data) {
1383 dragWasDropped = true;
1384 if (selection_data->type == atomUriList || selection_data->type == atomDROPFILES_DND) {
1385 char *ptr = new char[selection_data->length + 1];
1386 ptr[selection_data->length] = '\0';
1387 memcpy(ptr, selection_data->data, selection_data->length);
1388 NotifyURIDropped(ptr);
1389 delete []ptr;
1390 } else if ((selection_data->type == GDK_TARGET_STRING) || (selection_data->type == atomUTF8)) {
1391 if (selection_data->length > 0) {
1392 SelectionText selText;
1393 GetGtkSelectionText(selection_data, selText);
1394 DropAt(posDrop, selText.s, false, selText.rectangular);
1396 } else if (selection_data->length > 0) {
1397 //~ fprintf(stderr, "ReceivedDrop other %p\n", static_cast<void *>(selection_data->type));
1399 Redraw();
1404 void ScintillaGTK::GetSelection(GtkSelectionData *selection_data, guint info, SelectionText *text) {
1405 #if PLAT_GTK_WIN32
1406 // GDK on Win32 expands any \n into \r\n, so make a copy of
1407 // the clip text now with newlines converted to \n. Use { } to hide symbols
1408 // from code below
1409 SelectionText *newline_normalized = NULL;
1411 int tmpstr_len;
1412 char *tmpstr = Document::TransformLineEnds(&tmpstr_len, text->s, text->len, SC_EOL_LF);
1413 newline_normalized = new SelectionText();
1414 newline_normalized->Set(tmpstr, tmpstr_len, SC_CP_UTF8, 0, text->rectangular, false);
1415 text = newline_normalized;
1417 #endif
1419 // Convert text to utf8 if it isn't already
1420 SelectionText *converted = 0;
1421 if ((text->codePage != SC_CP_UTF8) && (info == TARGET_UTF8_STRING)) {
1422 const char *charSet = ::CharacterSetID(text->characterSet);
1423 if (*charSet) {
1424 int new_len;
1425 char* tmputf = ConvertText(&new_len, text->s, text->len, "UTF-8", charSet, false);
1426 converted = new SelectionText();
1427 converted->Set(tmputf, new_len, SC_CP_UTF8, 0, text->rectangular, false);
1428 text = converted;
1432 // Here is a somewhat evil kludge.
1433 // As I can not work out how to store data on the clipboard in multiple formats
1434 // and need some way to mark the clipping as being stream or rectangular,
1435 // the terminating \0 is included in the length for rectangular clippings.
1436 // All other tested aplications behave benignly by ignoring the \0.
1437 // The #if is here because on Windows cfColumnSelect clip entry is used
1438 // instead as standard indicator of rectangularness (so no need to kludge)
1439 const char *textData = text->s ? text->s : "";
1440 int len = strlen(textData);
1441 #if PLAT_GTK_WIN32 == 0
1442 if (text->rectangular)
1443 len++;
1444 #endif
1446 if (info == TARGET_UTF8_STRING) {
1447 gtk_selection_data_set_text(selection_data, textData, len);
1448 } else {
1449 gtk_selection_data_set(selection_data,
1450 static_cast<GdkAtom>(GDK_SELECTION_TYPE_STRING),
1451 8, reinterpret_cast<const unsigned char *>(textData), len);
1453 delete converted;
1455 #if PLAT_GTK_WIN32
1456 delete newline_normalized;
1457 #endif
1460 #ifdef USE_GTK_CLIPBOARD
1461 void ScintillaGTK::StoreOnClipboard(SelectionText *clipText) {
1462 GtkClipboard *clipBoard =
1463 gtk_widget_get_clipboard(GTK_WIDGET(PWidget(wMain)), atomClipboard);
1464 if (clipBoard == NULL) // Occurs if widget isn't in a toplevel
1465 return;
1467 if (gtk_clipboard_set_with_data(clipBoard, clipboardCopyTargets, nClipboardCopyTargets,
1468 ClipboardGetSelection, ClipboardClearSelection, clipText)) {
1469 gtk_clipboard_set_can_store(clipBoard, clipboardCopyTargets, nClipboardCopyTargets);
1473 void ScintillaGTK::ClipboardGetSelection(GtkClipboard *, GtkSelectionData *selection_data, guint info, void *data) {
1474 GetSelection(selection_data, info, static_cast<SelectionText*>(data));
1477 void ScintillaGTK::ClipboardClearSelection(GtkClipboard *, void *data) {
1478 SelectionText *obj = static_cast<SelectionText*>(data);
1479 delete obj;
1481 #endif
1483 void ScintillaGTK::UnclaimSelection(GdkEventSelection *selection_event) {
1484 try {
1485 //Platform::DebugPrintf("UnclaimSelection\n");
1486 if (selection_event->selection == GDK_SELECTION_PRIMARY) {
1487 //Platform::DebugPrintf("UnclaimPrimarySelection\n");
1488 if (!OwnPrimarySelection()) {
1489 primary.Free();
1490 primarySelection = false;
1491 FullPaint();
1494 } catch (...) {
1495 errorStatus = SC_STATUS_FAILURE;
1499 void ScintillaGTK::Resize(int width, int height) {
1500 //Platform::DebugPrintf("Resize %d %d\n", width, height);
1501 //printf("Resize %d %d\n", width, height);
1503 // Not always needed, but some themes can have different sizes of scrollbars
1504 scrollBarWidth = GTK_WIDGET(PWidget(scrollbarv))->requisition.width;
1505 scrollBarHeight = GTK_WIDGET(PWidget(scrollbarh))->requisition.height;
1507 // These allocations should never produce negative sizes as they would wrap around to huge
1508 // unsigned numbers inside GTK+ causing warnings.
1509 bool showSBHorizontal = horizontalScrollBarVisible && (wrapState == eWrapNone);
1510 int horizontalScrollBarHeight = scrollBarHeight;
1511 if (!showSBHorizontal)
1512 horizontalScrollBarHeight = 0;
1514 GtkAllocation alloc;
1515 if (showSBHorizontal) {
1516 gtk_widget_show(GTK_WIDGET(PWidget(scrollbarh)));
1517 alloc.x = 0;
1518 alloc.y = height - scrollBarHeight;
1519 alloc.width = Platform::Maximum(1, width - scrollBarWidth) + 1;
1520 alloc.height = horizontalScrollBarHeight;
1521 gtk_widget_size_allocate(GTK_WIDGET(PWidget(scrollbarh)), &alloc);
1522 } else {
1523 gtk_widget_hide(GTK_WIDGET(PWidget(scrollbarh)));
1526 if (verticalScrollBarVisible) {
1527 gtk_widget_show(GTK_WIDGET(PWidget(scrollbarv)));
1528 alloc.x = width - scrollBarWidth;
1529 alloc.y = 0;
1530 alloc.width = scrollBarWidth;
1531 alloc.height = Platform::Maximum(1, height - scrollBarHeight) + 1;
1532 if (!showSBHorizontal)
1533 alloc.height += scrollBarWidth-1;
1534 gtk_widget_size_allocate(GTK_WIDGET(PWidget(scrollbarv)), &alloc);
1535 } else {
1536 gtk_widget_hide(GTK_WIDGET(PWidget(scrollbarv)));
1538 if (GTK_WIDGET_MAPPED(PWidget(wMain))) {
1539 ChangeSize();
1542 alloc.x = 0;
1543 alloc.y = 0;
1544 alloc.width = Platform::Maximum(1, width - scrollBarWidth);
1545 alloc.height = Platform::Maximum(1, height - scrollBarHeight);
1546 if (!showSBHorizontal)
1547 alloc.height += scrollBarHeight;
1548 if (!verticalScrollBarVisible)
1549 alloc.width += scrollBarWidth;
1550 gtk_widget_size_allocate(GTK_WIDGET(PWidget(wText)), &alloc);
1553 static void SetAdjustmentValue(GtkObject *object, int value) {
1554 GtkAdjustment *adjustment = GTK_ADJUSTMENT(object);
1555 int maxValue = static_cast<int>(
1556 adjustment->upper - adjustment->page_size);
1557 if (value > maxValue)
1558 value = maxValue;
1559 if (value < 0)
1560 value = 0;
1561 gtk_adjustment_set_value(adjustment, value);
1564 static int modifierTranslated(int sciModifier) {
1565 switch (sciModifier) {
1566 case SCMOD_SHIFT:
1567 return GDK_SHIFT_MASK;
1568 case SCMOD_CTRL:
1569 return GDK_CONTROL_MASK;
1570 case SCMOD_ALT:
1571 return GDK_MOD1_MASK;
1572 case SCMOD_SUPER:
1573 return GDK_MOD4_MASK;
1574 default:
1575 return 0;
1579 gint ScintillaGTK::PressThis(GdkEventButton *event) {
1580 try {
1581 //Platform::DebugPrintf("Press %x time=%d state = %x button = %x\n",this,event->time, event->state, event->button);
1582 // Do not use GTK+ double click events as Scintilla has its own double click detection
1583 if (event->type != GDK_BUTTON_PRESS)
1584 return FALSE;
1586 evbtn = *event;
1587 Point pt;
1588 pt.x = int(event->x);
1589 pt.y = int(event->y);
1590 PRectangle rcClient = GetClientRectangle();
1591 //Platform::DebugPrintf("Press %0d,%0d in %0d,%0d %0d,%0d\n",
1592 // pt.x, pt.y, rcClient.left, rcClient.top, rcClient.right, rcClient.bottom);
1593 if ((pt.x > rcClient.right) || (pt.y > rcClient.bottom)) {
1594 Platform::DebugPrintf("Bad location\n");
1595 return FALSE;
1598 bool ctrl = (event->state & GDK_CONTROL_MASK) != 0;
1600 gtk_widget_grab_focus(PWidget(wMain));
1601 if (event->button == 1) {
1602 // On X, instead of sending literal modifiers use the user specified
1603 // modifier, defaulting to control instead of alt.
1604 // This is because most X window managers grab alt + click for moving
1605 ButtonDown(pt, event->time,
1606 (event->state & GDK_SHIFT_MASK) != 0,
1607 (event->state & GDK_CONTROL_MASK) != 0,
1608 (event->state & modifierTranslated(rectangularSelectionModifier)) != 0);
1609 } else if (event->button == 2) {
1610 // Grab the primary selection if it exists
1611 SelectionPosition pos = SPositionFromLocation(pt, false, false, UserVirtualSpace());
1612 if (OwnPrimarySelection() && primary.s == NULL)
1613 CopySelectionRange(&primary);
1615 SetSelection(pos, pos);
1616 atomSought = atomUTF8;
1617 gtk_selection_convert(GTK_WIDGET(PWidget(wMain)), GDK_SELECTION_PRIMARY,
1618 atomSought, event->time);
1619 } else if (event->button == 3) {
1620 if (displayPopupMenu) {
1621 // PopUp menu
1622 // Convert to screen
1623 int ox = 0;
1624 int oy = 0;
1625 gdk_window_get_origin(PWidget(wMain)->window, &ox, &oy);
1626 ContextMenu(Point(pt.x + ox, pt.y + oy));
1627 } else {
1628 return FALSE;
1630 } else if (event->button == 4) {
1631 // Wheel scrolling up (only GTK 1.x does it this way)
1632 if (ctrl)
1633 SetAdjustmentValue(adjustmenth, (xOffset / 2) - 6);
1634 else
1635 SetAdjustmentValue(adjustmentv, topLine - 3);
1636 } else if (event->button == 5) {
1637 // Wheel scrolling down (only GTK 1.x does it this way)
1638 if (ctrl)
1639 SetAdjustmentValue(adjustmenth, (xOffset / 2) + 6);
1640 else
1641 SetAdjustmentValue(adjustmentv, topLine + 3);
1643 } catch (...) {
1644 errorStatus = SC_STATUS_FAILURE;
1646 return TRUE;
1649 gint ScintillaGTK::Press(GtkWidget *widget, GdkEventButton *event) {
1650 if (event->window != widget->window)
1651 return FALSE;
1652 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
1653 return sciThis->PressThis(event);
1656 gint ScintillaGTK::MouseRelease(GtkWidget *widget, GdkEventButton *event) {
1657 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
1658 try {
1659 //Platform::DebugPrintf("Release %x %d %d\n",sciThis,event->time,event->state);
1660 if (!sciThis->HaveMouseCapture())
1661 return FALSE;
1662 if (event->button == 1) {
1663 Point pt;
1664 pt.x = int(event->x);
1665 pt.y = int(event->y);
1666 //Platform::DebugPrintf("Up %x %x %d %d %d\n",
1667 // sciThis,event->window,event->time, pt.x, pt.y);
1668 if (event->window != PWidget(sciThis->wMain)->window)
1669 // If mouse released on scroll bar then the position is relative to the
1670 // scrollbar, not the drawing window so just repeat the most recent point.
1671 pt = sciThis->ptMouseLast;
1672 sciThis->ButtonUp(pt, event->time, (event->state & 4) != 0);
1674 } catch (...) {
1675 sciThis->errorStatus = SC_STATUS_FAILURE;
1677 return FALSE;
1680 // win32gtk and GTK >= 2 use SCROLL_* events instead of passing the
1681 // button4/5/6/7 events to the GTK app
1682 gint ScintillaGTK::ScrollEvent(GtkWidget *widget,
1683 GdkEventScroll *event) {
1684 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
1685 try {
1687 if (widget == NULL || event == NULL)
1688 return FALSE;
1690 // Compute amount and direction to scroll (even tho on win32 there is
1691 // intensity of scrolling info in the native message, gtk doesn't
1692 // support this so we simulate similarly adaptive scrolling)
1693 // Note that this is disabled on OS X (Darwin) where the X11 server already has
1694 // and adaptive scrolling algorithm that fights with this one
1695 int cLineScroll;
1696 #if defined(__MWERKS__) || defined(__APPLE_CPP__) || defined(__APPLE_CC__)
1697 cLineScroll = sciThis->linesPerScroll;
1698 if (cLineScroll == 0)
1699 cLineScroll = 4;
1700 sciThis->wheelMouseIntensity = cLineScroll;
1701 #else
1702 int timeDelta = 1000000;
1703 GTimeVal curTime;
1704 g_get_current_time(&curTime);
1705 if (curTime.tv_sec == sciThis->lastWheelMouseTime.tv_sec)
1706 timeDelta = curTime.tv_usec - sciThis->lastWheelMouseTime.tv_usec;
1707 else if (curTime.tv_sec == sciThis->lastWheelMouseTime.tv_sec + 1)
1708 timeDelta = 1000000 + (curTime.tv_usec - sciThis->lastWheelMouseTime.tv_usec);
1709 if ((event->direction == sciThis->lastWheelMouseDirection) && (timeDelta < 250000)) {
1710 if (sciThis->wheelMouseIntensity < 12)
1711 sciThis->wheelMouseIntensity++;
1712 cLineScroll = sciThis->wheelMouseIntensity;
1713 } else {
1714 cLineScroll = sciThis->linesPerScroll;
1715 if (cLineScroll == 0)
1716 cLineScroll = 4;
1717 sciThis->wheelMouseIntensity = cLineScroll;
1719 #endif
1720 if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_LEFT) {
1721 cLineScroll *= -1;
1723 g_get_current_time(&sciThis->lastWheelMouseTime);
1724 sciThis->lastWheelMouseDirection = event->direction;
1726 // Note: Unpatched versions of win32gtk don't set the 'state' value so
1727 // only regular scrolling is supported there. Also, unpatched win32gtk
1728 // issues spurious button 2 mouse events during wheeling, which can cause
1729 // problems (a patch for both was submitted by archaeopteryx.com on 13Jun2001)
1731 // Data zoom not supported
1732 if (event->state & GDK_SHIFT_MASK) {
1733 return FALSE;
1736 // Horizontal scrolling
1737 if (event->direction == GDK_SCROLL_LEFT || event->direction == GDK_SCROLL_RIGHT) {
1738 sciThis->HorizontalScrollTo(sciThis->xOffset + cLineScroll);
1740 // Text font size zoom
1741 } else if (event->state & GDK_CONTROL_MASK) {
1742 if (cLineScroll < 0) {
1743 sciThis->KeyCommand(SCI_ZOOMIN);
1744 } else {
1745 sciThis->KeyCommand(SCI_ZOOMOUT);
1748 // Regular scrolling
1749 } else {
1750 sciThis->ScrollTo(sciThis->topLine + cLineScroll);
1752 return TRUE;
1753 } catch (...) {
1754 sciThis->errorStatus = SC_STATUS_FAILURE;
1756 return FALSE;
1759 gint ScintillaGTK::Motion(GtkWidget *widget, GdkEventMotion *event) {
1760 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
1761 try {
1762 //Platform::DebugPrintf("Motion %x %d\n",sciThis,event->time);
1763 if (event->window != widget->window)
1764 return FALSE;
1765 int x = 0;
1766 int y = 0;
1767 GdkModifierType state;
1768 if (event->is_hint) {
1769 gdk_window_get_pointer(event->window, &x, &y, &state);
1770 } else {
1771 x = static_cast<int>(event->x);
1772 y = static_cast<int>(event->y);
1773 state = static_cast<GdkModifierType>(event->state);
1775 //Platform::DebugPrintf("Move %x %x %d %c %d %d\n",
1776 // sciThis,event->window,event->time,event->is_hint? 'h' :'.', x, y);
1777 Point pt(x, y);
1778 sciThis->ButtonMove(pt);
1779 } catch (...) {
1780 sciThis->errorStatus = SC_STATUS_FAILURE;
1782 return FALSE;
1785 // Map the keypad keys to their equivalent functions
1786 static int KeyTranslate(int keyIn) {
1787 switch (keyIn) {
1788 case GDK_ISO_Left_Tab:
1789 return SCK_TAB;
1790 case GDK_KP_Down:
1791 return SCK_DOWN;
1792 case GDK_KP_Up:
1793 return SCK_UP;
1794 case GDK_KP_Left:
1795 return SCK_LEFT;
1796 case GDK_KP_Right:
1797 return SCK_RIGHT;
1798 case GDK_KP_Home:
1799 return SCK_HOME;
1800 case GDK_KP_End:
1801 return SCK_END;
1802 case GDK_KP_Page_Up:
1803 return SCK_PRIOR;
1804 case GDK_KP_Page_Down:
1805 return SCK_NEXT;
1806 case GDK_KP_Delete:
1807 return SCK_DELETE;
1808 case GDK_KP_Insert:
1809 return SCK_INSERT;
1810 case GDK_KP_Enter:
1811 return SCK_RETURN;
1813 case GDK_Down:
1814 return SCK_DOWN;
1815 case GDK_Up:
1816 return SCK_UP;
1817 case GDK_Left:
1818 return SCK_LEFT;
1819 case GDK_Right:
1820 return SCK_RIGHT;
1821 case GDK_Home:
1822 return SCK_HOME;
1823 case GDK_End:
1824 return SCK_END;
1825 case GDK_Page_Up:
1826 return SCK_PRIOR;
1827 case GDK_Page_Down:
1828 return SCK_NEXT;
1829 case GDK_Delete:
1830 return SCK_DELETE;
1831 case GDK_Insert:
1832 return SCK_INSERT;
1833 case GDK_Escape:
1834 return SCK_ESCAPE;
1835 case GDK_BackSpace:
1836 return SCK_BACK;
1837 case GDK_Tab:
1838 return SCK_TAB;
1839 case GDK_Return:
1840 return SCK_RETURN;
1841 case GDK_KP_Add:
1842 return SCK_ADD;
1843 case GDK_KP_Subtract:
1844 return SCK_SUBTRACT;
1845 case GDK_KP_Divide:
1846 return SCK_DIVIDE;
1847 case GDK_Super_L:
1848 return SCK_WIN;
1849 case GDK_Super_R:
1850 return SCK_RWIN;
1851 case GDK_Menu:
1852 return SCK_MENU;
1853 default:
1854 return keyIn;
1858 gboolean ScintillaGTK::KeyThis(GdkEventKey *event) {
1859 try {
1860 //fprintf(stderr, "SC-key: %d %x [%s]\n",
1861 // event->keyval, event->state, (event->length > 0) ? event->string : "empty");
1862 if (gtk_im_context_filter_keypress(im_context, event)) {
1863 return 1;
1865 if (!event->keyval) {
1866 return true;
1869 bool shift = (event->state & GDK_SHIFT_MASK) != 0;
1870 bool ctrl = (event->state & GDK_CONTROL_MASK) != 0;
1871 bool alt = (event->state & GDK_MOD1_MASK) != 0;
1872 guint key = event->keyval;
1873 if (ctrl && (key < 128))
1874 key = toupper(key);
1875 else if (!ctrl && (key >= GDK_KP_Multiply && key <= GDK_KP_9))
1876 key &= 0x7F;
1877 // Hack for keys over 256 and below command keys but makes Hungarian work.
1878 // This will have to change for Unicode
1879 else if (key >= 0xFE00)
1880 key = KeyTranslate(key);
1882 bool consumed = false;
1883 bool added = KeyDown(key, shift, ctrl, alt, &consumed) != 0;
1884 if (!consumed)
1885 consumed = added;
1886 //fprintf(stderr, "SK-key: %d %x %x\n",event->keyval, event->state, consumed);
1887 if (event->keyval == 0xffffff && event->length > 0) {
1888 ClearSelection();
1889 if (pdoc->InsertCString(CurrentPosition(), event->string)) {
1890 MovePositionTo(CurrentPosition() + event->length);
1893 return consumed;
1894 } catch (...) {
1895 errorStatus = SC_STATUS_FAILURE;
1897 return FALSE;
1900 gboolean ScintillaGTK::KeyPress(GtkWidget *widget, GdkEventKey *event) {
1901 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
1902 return sciThis->KeyThis(event);
1905 gboolean ScintillaGTK::KeyRelease(GtkWidget *, GdkEventKey * /*event*/) {
1906 //Platform::DebugPrintf("SC-keyrel: %d %x %3s\n",event->keyval, event->state, event->string);
1907 return FALSE;
1910 gboolean ScintillaGTK::ExposePreeditThis(GtkWidget *widget, GdkEventExpose *ose) {
1911 try {
1912 gchar *str;
1913 gint cursor_pos;
1914 PangoAttrList *attrs;
1916 gtk_im_context_get_preedit_string(im_context, &str, &attrs, &cursor_pos);
1917 PangoLayout *layout = gtk_widget_create_pango_layout(PWidget(wText), str);
1918 pango_layout_set_attributes(layout, attrs);
1920 GdkGC *gc = gdk_gc_new(widget->window);
1921 GdkColor color[2] = { {0, 0x0000, 0x0000, 0x0000},
1922 {0, 0xffff, 0xffff, 0xffff}
1924 gdk_color_alloc(gdk_colormap_get_system(), color);
1925 gdk_color_alloc(gdk_colormap_get_system(), color + 1);
1927 gdk_gc_set_foreground(gc, color + 1);
1928 gdk_draw_rectangle(widget->window, gc, TRUE, ose->area.x, ose->area.y,
1929 ose->area.width, ose->area.height);
1931 gdk_gc_set_foreground(gc, color);
1932 gdk_gc_set_background(gc, color + 1);
1933 gdk_draw_layout(widget->window, gc, 0, 0, layout);
1935 gdk_gc_unref(gc);
1936 g_free(str);
1937 pango_attr_list_unref(attrs);
1938 g_object_unref(layout);
1939 } catch (...) {
1940 errorStatus = SC_STATUS_FAILURE;
1942 return TRUE;
1945 gboolean ScintillaGTK::ExposePreedit(GtkWidget *widget, GdkEventExpose *ose, ScintillaGTK *sciThis) {
1946 return sciThis->ExposePreeditThis(widget, ose);
1949 void ScintillaGTK::CommitThis(char *utfVal) {
1950 try {
1951 //~ fprintf(stderr, "Commit '%s'\n", utfVal);
1952 if (IsUnicodeMode()) {
1953 AddCharUTF(utfVal, strlen(utfVal));
1954 } else {
1955 const char *source = CharacterSetID();
1956 if (*source) {
1957 Converter conv(source, "UTF-8", true);
1958 if (conv) {
1959 char localeVal[4] = "\0\0\0";
1960 char *pin = utfVal;
1961 size_t inLeft = strlen(utfVal);
1962 char *pout = localeVal;
1963 size_t outLeft = sizeof(localeVal);
1964 size_t conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
1965 if (conversions != ((size_t)(-1))) {
1966 *pout = '\0';
1967 for (int i = 0; localeVal[i]; i++) {
1968 AddChar(localeVal[i]);
1970 } else {
1971 fprintf(stderr, "Conversion failed '%s'\n", utfVal);
1976 } catch (...) {
1977 errorStatus = SC_STATUS_FAILURE;
1981 void ScintillaGTK::Commit(GtkIMContext *, char *str, ScintillaGTK *sciThis) {
1982 sciThis->CommitThis(str);
1985 void ScintillaGTK::PreeditChangedThis() {
1986 try {
1987 gchar *str;
1988 PangoAttrList *attrs;
1989 gint cursor_pos;
1990 gtk_im_context_get_preedit_string(im_context, &str, &attrs, &cursor_pos);
1991 if (strlen(str) > 0) {
1992 PangoLayout *layout = gtk_widget_create_pango_layout(PWidget(wText), str);
1993 pango_layout_set_attributes(layout, attrs);
1995 gint w, h;
1996 pango_layout_get_pixel_size(layout, &w, &h);
1997 g_object_unref(layout);
1999 gint x, y;
2000 gdk_window_get_origin((PWidget(wText))->window, &x, &y);
2002 Point pt = PointMainCaret();
2003 if (pt.x < 0)
2004 pt.x = 0;
2005 if (pt.y < 0)
2006 pt.y = 0;
2008 gtk_window_move(GTK_WINDOW(PWidget(wPreedit)), x + pt.x, y + pt.y);
2009 gtk_window_resize(GTK_WINDOW(PWidget(wPreedit)), w, h);
2010 gtk_widget_show(PWidget(wPreedit));
2011 gtk_widget_queue_draw_area(PWidget(wPreeditDraw), 0, 0, w, h);
2012 } else {
2013 gtk_widget_hide(PWidget(wPreedit));
2015 g_free(str);
2016 pango_attr_list_unref(attrs);
2017 } catch (...) {
2018 errorStatus = SC_STATUS_FAILURE;
2022 void ScintillaGTK::PreeditChanged(GtkIMContext *, ScintillaGTK *sciThis) {
2023 sciThis->PreeditChangedThis();
2026 gint ScintillaGTK::StyleSetText(GtkWidget *widget, GtkStyle *, void*) {
2027 if (widget->window != NULL)
2028 gdk_window_set_back_pixmap(widget->window, NULL, FALSE);
2029 return FALSE;
2032 gint ScintillaGTK::RealizeText(GtkWidget *widget, void*) {
2033 if (widget->window != NULL)
2034 gdk_window_set_back_pixmap(widget->window, NULL, FALSE);
2035 return FALSE;
2038 void ScintillaGTK::Destroy(GObject *object) {
2039 try {
2040 ScintillaObject *scio = reinterpret_cast<ScintillaObject *>(object);
2041 // This avoids a double destruction
2042 if (!scio->pscin)
2043 return;
2044 ScintillaGTK *sciThis = reinterpret_cast<ScintillaGTK *>(scio->pscin);
2045 //Platform::DebugPrintf("Destroying %x %x\n", sciThis, object);
2046 sciThis->Finalise();
2048 delete sciThis;
2049 scio->pscin = 0;
2050 } catch (...) {
2051 // Its dead so nowhere to save the status
2055 static void DrawChild(GtkWidget *widget, GdkRectangle *area) {
2056 GdkRectangle areaIntersect;
2057 if (widget &&
2058 GTK_WIDGET_DRAWABLE(widget) &&
2059 gtk_widget_intersect(widget, area, &areaIntersect)) {
2060 gtk_widget_draw(widget, &areaIntersect);
2064 void ScintillaGTK::Draw(GtkWidget *widget, GdkRectangle *area) {
2065 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2066 try {
2067 //Platform::DebugPrintf("Draw %p %0d,%0d %0d,%0d\n", widget, area->x, area->y, area->width, area->height);
2068 PRectangle rcPaint(area->x, area->y, area->x + area->width, area->y + area->height);
2069 sciThis->SyncPaint(rcPaint);
2070 if (GTK_WIDGET_DRAWABLE(PWidget(sciThis->wMain))) {
2071 DrawChild(PWidget(sciThis->scrollbarh), area);
2072 DrawChild(PWidget(sciThis->scrollbarv), area);
2075 Point pt = sciThis->PointMainCaret();
2076 pt.y += sciThis->vs.lineHeight - 2;
2077 if (pt.x < 0) pt.x = 0;
2078 if (pt.y < 0) pt.y = 0;
2079 CursorMoved(widget, pt.x, pt.y, sciThis);
2080 } catch (...) {
2081 sciThis->errorStatus = SC_STATUS_FAILURE;
2085 gint ScintillaGTK::ExposeTextThis(GtkWidget * /*widget*/, GdkEventExpose *ose) {
2086 try {
2087 paintState = painting;
2089 rcPaint.left = ose->area.x;
2090 rcPaint.top = ose->area.y;
2091 rcPaint.right = ose->area.x + ose->area.width;
2092 rcPaint.bottom = ose->area.y + ose->area.height;
2094 PLATFORM_ASSERT(rgnUpdate == NULL);
2095 rgnUpdate = gdk_region_copy(ose->region);
2096 PRectangle rcClient = GetClientRectangle();
2097 paintingAllText = rcPaint.Contains(rcClient);
2098 Surface *surfaceWindow = Surface::Allocate();
2099 if (surfaceWindow) {
2100 surfaceWindow->Init(PWidget(wText)->window, PWidget(wText));
2101 Paint(surfaceWindow, rcPaint);
2102 surfaceWindow->Release();
2103 delete surfaceWindow;
2105 if (paintState == paintAbandoned) {
2106 // Painting area was insufficient to cover new styling or brace highlight positions
2107 FullPaint();
2109 paintState = notPainting;
2111 if (rgnUpdate) {
2112 gdk_region_destroy(rgnUpdate);
2114 rgnUpdate = 0;
2115 } catch (...) {
2116 errorStatus = SC_STATUS_FAILURE;
2119 return FALSE;
2122 gint ScintillaGTK::ExposeText(GtkWidget *widget, GdkEventExpose *ose, ScintillaGTK *sciThis) {
2123 return sciThis->ExposeTextThis(widget, ose);
2126 gint ScintillaGTK::ExposeMain(GtkWidget *widget, GdkEventExpose *ose) {
2127 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2128 //Platform::DebugPrintf("Expose Main %0d,%0d %0d,%0d\n",
2129 //ose->area.x, ose->area.y, ose->area.width, ose->area.height);
2130 return sciThis->Expose(widget, ose);
2133 gint ScintillaGTK::Expose(GtkWidget *, GdkEventExpose *ose) {
2134 try {
2135 //fprintf(stderr, "Expose %0d,%0d %0d,%0d\n",
2136 //ose->area.x, ose->area.y, ose->area.width, ose->area.height);
2138 // The text is painted in ExposeText
2139 gtk_container_propagate_expose(
2140 GTK_CONTAINER(PWidget(wMain)), PWidget(scrollbarh), ose);
2141 gtk_container_propagate_expose(
2142 GTK_CONTAINER(PWidget(wMain)), PWidget(scrollbarv), ose);
2144 } catch (...) {
2145 errorStatus = SC_STATUS_FAILURE;
2147 return FALSE;
2150 void ScintillaGTK::ScrollSignal(GtkAdjustment *adj, ScintillaGTK *sciThis) {
2151 try {
2152 sciThis->ScrollTo(static_cast<int>(adj->value), false);
2153 } catch (...) {
2154 sciThis->errorStatus = SC_STATUS_FAILURE;
2158 void ScintillaGTK::ScrollHSignal(GtkAdjustment *adj, ScintillaGTK *sciThis) {
2159 try {
2160 sciThis->HorizontalScrollTo(static_cast<int>(adj->value * 2));
2161 } catch (...) {
2162 sciThis->errorStatus = SC_STATUS_FAILURE;
2166 void ScintillaGTK::SelectionReceived(GtkWidget *widget,
2167 GtkSelectionData *selection_data, guint) {
2168 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2169 //Platform::DebugPrintf("Selection received\n");
2170 sciThis->ReceivedSelection(selection_data);
2173 void ScintillaGTK::SelectionGet(GtkWidget *widget,
2174 GtkSelectionData *selection_data, guint info, guint) {
2175 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2176 try {
2177 //Platform::DebugPrintf("Selection get\n");
2178 if (selection_data->selection == GDK_SELECTION_PRIMARY) {
2179 if (sciThis->primary.s == NULL) {
2180 sciThis->CopySelectionRange(&sciThis->primary);
2182 sciThis->GetSelection(selection_data, info, &sciThis->primary);
2184 #ifndef USE_GTK_CLIPBOARD
2185 else {
2186 sciThis->GetSelection(selection_data, info, &sciThis->copyText);
2188 #endif
2189 } catch (...) {
2190 sciThis->errorStatus = SC_STATUS_FAILURE;
2194 gint ScintillaGTK::SelectionClear(GtkWidget *widget, GdkEventSelection *selection_event) {
2195 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2196 //Platform::DebugPrintf("Selection clear\n");
2197 sciThis->UnclaimSelection(selection_event);
2198 return gtk_selection_clear(widget, selection_event);
2201 void ScintillaGTK::DragBegin(GtkWidget *, GdkDragContext *) {
2202 //Platform::DebugPrintf("DragBegin\n");
2205 gboolean ScintillaGTK::DragMotionThis(GdkDragContext *context,
2206 gint x, gint y, guint dragtime) {
2207 try {
2208 Point npt(x, y);
2209 SetDragPosition(SPositionFromLocation(npt, false, false, UserVirtualSpace()));
2210 GdkDragAction preferredAction = context->suggested_action;
2211 SelectionPosition pos = SPositionFromLocation(npt);
2212 if ((inDragDrop == ddDragging) && (PositionInSelection(pos.Position()))) {
2213 // Avoid dragging selection onto itself as that produces a move
2214 // with no real effect but which creates undo actions.
2215 preferredAction = static_cast<GdkDragAction>(0);
2216 } else if (context->actions == static_cast<GdkDragAction>
2217 (GDK_ACTION_COPY | GDK_ACTION_MOVE)) {
2218 preferredAction = GDK_ACTION_MOVE;
2220 gdk_drag_status(context, preferredAction, dragtime);
2221 } catch (...) {
2222 errorStatus = SC_STATUS_FAILURE;
2224 return FALSE;
2227 gboolean ScintillaGTK::DragMotion(GtkWidget *widget, GdkDragContext *context,
2228 gint x, gint y, guint dragtime) {
2229 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2230 return sciThis->DragMotionThis(context, x, y, dragtime);
2233 void ScintillaGTK::DragLeave(GtkWidget *widget, GdkDragContext * /*context*/, guint) {
2234 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2235 try {
2236 sciThis->SetDragPosition(SelectionPosition(invalidPosition));
2237 //Platform::DebugPrintf("DragLeave %x\n", sciThis);
2238 } catch (...) {
2239 sciThis->errorStatus = SC_STATUS_FAILURE;
2243 void ScintillaGTK::DragEnd(GtkWidget *widget, GdkDragContext * /*context*/) {
2244 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2245 try {
2246 // If drag did not result in drop here or elsewhere
2247 if (!sciThis->dragWasDropped)
2248 sciThis->SetEmptySelection(sciThis->posDrag);
2249 sciThis->SetDragPosition(SelectionPosition(invalidPosition));
2250 //Platform::DebugPrintf("DragEnd %x %d\n", sciThis, sciThis->dragWasDropped);
2251 sciThis->inDragDrop = ddNone;
2252 } catch (...) {
2253 sciThis->errorStatus = SC_STATUS_FAILURE;
2257 gboolean ScintillaGTK::Drop(GtkWidget *widget, GdkDragContext * /*context*/,
2258 gint, gint, guint) {
2259 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2260 try {
2261 //Platform::DebugPrintf("Drop %x\n", sciThis);
2262 sciThis->SetDragPosition(SelectionPosition(invalidPosition));
2263 } catch (...) {
2264 sciThis->errorStatus = SC_STATUS_FAILURE;
2266 return FALSE;
2269 void ScintillaGTK::DragDataReceived(GtkWidget *widget, GdkDragContext * /*context*/,
2270 gint, gint, GtkSelectionData *selection_data, guint /*info*/, guint) {
2271 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2272 try {
2273 sciThis->ReceivedDrop(selection_data);
2274 sciThis->SetDragPosition(SelectionPosition(invalidPosition));
2275 } catch (...) {
2276 sciThis->errorStatus = SC_STATUS_FAILURE;
2280 void ScintillaGTK::DragDataGet(GtkWidget *widget, GdkDragContext *context,
2281 GtkSelectionData *selection_data, guint info, guint) {
2282 ScintillaGTK *sciThis = ScintillaFromWidget(widget);
2283 try {
2284 sciThis->dragWasDropped = true;
2285 if (!sciThis->sel.Empty()) {
2286 sciThis->GetSelection(selection_data, info, &sciThis->drag);
2288 if (context->action == GDK_ACTION_MOVE) {
2289 for (size_t r=0; r<sciThis->sel.Count(); r++) {
2290 if (sciThis->posDrop >= sciThis->sel.Range(r).Start()) {
2291 if (sciThis->posDrop > sciThis->sel.Range(r).End()) {
2292 sciThis->posDrop.Add(-sciThis->sel.Range(r).Length());
2293 } else {
2294 sciThis->posDrop.Add(-SelectionRange(sciThis->posDrop, sciThis->sel.Range(r).Start()).Length());
2298 sciThis->ClearSelection();
2300 sciThis->SetDragPosition(SelectionPosition(invalidPosition));
2301 } catch (...) {
2302 sciThis->errorStatus = SC_STATUS_FAILURE;
2306 int ScintillaGTK::TimeOut(ScintillaGTK *sciThis) {
2307 sciThis->Tick();
2308 return 1;
2311 int ScintillaGTK::IdleCallback(ScintillaGTK *sciThis) {
2312 // Idler will be automatically stoped, if there is nothing
2313 // to do while idle.
2314 bool ret = sciThis->Idle();
2315 if (ret == false) {
2316 // FIXME: This will remove the idler from GTK, we don't want to
2317 // remove it as it is removed automatically when this function
2318 // returns false (although, it should be harmless).
2319 sciThis->SetIdle(false);
2321 return ret;
2324 void ScintillaGTK::PopUpCB(ScintillaGTK *sciThis, guint action, GtkWidget *) {
2325 if (action) {
2326 sciThis->Command(action);
2330 gint ScintillaGTK::PressCT(GtkWidget *widget, GdkEventButton *event, ScintillaGTK *sciThis) {
2331 try {
2332 if (event->window != widget->window)
2333 return FALSE;
2334 if (event->type != GDK_BUTTON_PRESS)
2335 return FALSE;
2336 Point pt;
2337 pt.x = int(event->x);
2338 pt.y = int(event->y);
2339 sciThis->ct.MouseClick(pt);
2340 sciThis->CallTipClick();
2341 } catch (...) {
2343 return TRUE;
2346 gint ScintillaGTK::ExposeCT(GtkWidget *widget, GdkEventExpose * /*ose*/, CallTip *ctip) {
2347 try {
2348 Surface *surfaceWindow = Surface::Allocate();
2349 if (surfaceWindow) {
2350 surfaceWindow->Init(widget->window, widget);
2351 surfaceWindow->SetUnicodeMode(SC_CP_UTF8 == ctip->codePage);
2352 surfaceWindow->SetDBCSMode(ctip->codePage);
2353 ctip->PaintCT(surfaceWindow);
2354 surfaceWindow->Release();
2355 delete surfaceWindow;
2357 } catch (...) {
2358 // No pointer back to Scintilla to save status
2360 return TRUE;
2363 sptr_t ScintillaGTK::DirectFunction(
2364 ScintillaGTK *sciThis, unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
2365 return sciThis->WndProc(iMessage, wParam, lParam);
2368 sptr_t scintilla_send_message(ScintillaObject *sci, unsigned int iMessage, uptr_t wParam, sptr_t lParam) {
2369 ScintillaGTK *psci = reinterpret_cast<ScintillaGTK *>(sci->pscin);
2370 return psci->WndProc(iMessage, wParam, lParam);
2373 static void scintilla_class_init(ScintillaClass *klass);
2374 static void scintilla_init(ScintillaObject *sci);
2376 extern void Platform_Initialise();
2377 extern void Platform_Finalise();
2379 GType scintilla_get_type() {
2380 static GType scintilla_type = 0;
2381 try {
2383 if (!scintilla_type) {
2384 scintilla_type = g_type_from_name("Scintilla");
2385 if (!scintilla_type) {
2386 static GTypeInfo scintilla_info = {
2387 (guint16) sizeof (ScintillaClass),
2388 NULL, //(GBaseInitFunc)
2389 NULL, //(GBaseFinalizeFunc)
2390 (GClassInitFunc) scintilla_class_init,
2391 NULL, //(GClassFinalizeFunc)
2392 NULL, //gconstpointer data
2393 (guint16) sizeof (ScintillaObject),
2394 0, //n_preallocs
2395 (GInstanceInitFunc) scintilla_init,
2396 NULL //(GTypeValueTable*)
2399 scintilla_type = g_type_register_static(
2400 GTK_TYPE_CONTAINER, "Scintilla", &scintilla_info, (GTypeFlags) 0);
2404 } catch (...) {
2406 return scintilla_type;
2409 void ScintillaGTK::ClassInit(OBJECT_CLASS* object_class, GtkWidgetClass *widget_class, GtkContainerClass *container_class) {
2410 Platform_Initialise();
2411 atomClipboard = gdk_atom_intern("CLIPBOARD", FALSE);
2412 atomUTF8 = gdk_atom_intern("UTF8_STRING", FALSE);
2413 atomString = GDK_SELECTION_TYPE_STRING;
2414 atomUriList = gdk_atom_intern("text/uri-list", FALSE);
2415 atomDROPFILES_DND = gdk_atom_intern("DROPFILES_DND", FALSE);
2417 // Define default signal handlers for the class: Could move more
2418 // of the signal handlers here (those that currently attached to wDraw
2419 // in Initialise() may require coordinate translation?)
2421 object_class->finalize = Destroy;
2422 widget_class->size_request = SizeRequest;
2423 widget_class->size_allocate = SizeAllocate;
2424 widget_class->expose_event = ExposeMain;
2425 widget_class->motion_notify_event = Motion;
2426 widget_class->button_press_event = Press;
2427 widget_class->button_release_event = MouseRelease;
2428 widget_class->scroll_event = ScrollEvent;
2429 widget_class->key_press_event = KeyPress;
2430 widget_class->key_release_event = KeyRelease;
2431 widget_class->focus_in_event = FocusIn;
2432 widget_class->focus_out_event = FocusOut;
2433 widget_class->selection_received = SelectionReceived;
2434 widget_class->selection_get = SelectionGet;
2435 widget_class->selection_clear_event = SelectionClear;
2437 widget_class->drag_data_received = DragDataReceived;
2438 widget_class->drag_motion = DragMotion;
2439 widget_class->drag_leave = DragLeave;
2440 widget_class->drag_end = DragEnd;
2441 widget_class->drag_drop = Drop;
2442 widget_class->drag_data_get = DragDataGet;
2444 widget_class->realize = Realize;
2445 widget_class->unrealize = UnRealize;
2446 widget_class->map = Map;
2447 widget_class->unmap = UnMap;
2449 container_class->forall = MainForAll;
2452 #define SIG_MARSHAL scintilla_marshal_NONE__INT_POINTER
2453 #define MARSHAL_ARGUMENTS G_TYPE_INT, G_TYPE_POINTER
2455 static void scintilla_class_init(ScintillaClass *klass) {
2456 try {
2457 OBJECT_CLASS *object_class = (OBJECT_CLASS*) klass;
2458 GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
2459 GtkContainerClass *container_class = (GtkContainerClass*) klass;
2461 GSignalFlags sigflags = GSignalFlags(G_SIGNAL_ACTION | G_SIGNAL_RUN_LAST);
2462 scintilla_signals[COMMAND_SIGNAL] = g_signal_new(
2463 "command",
2464 G_TYPE_FROM_CLASS(object_class),
2465 sigflags,
2466 G_STRUCT_OFFSET(ScintillaClass, command),
2467 NULL, //(GSignalAccumulator)
2468 NULL, //(gpointer)
2469 SIG_MARSHAL,
2470 G_TYPE_NONE,
2471 2, MARSHAL_ARGUMENTS);
2473 scintilla_signals[NOTIFY_SIGNAL] = g_signal_new(
2474 SCINTILLA_NOTIFY,
2475 G_TYPE_FROM_CLASS(object_class),
2476 sigflags,
2477 G_STRUCT_OFFSET(ScintillaClass, notify),
2478 NULL,
2479 NULL,
2480 SIG_MARSHAL,
2481 G_TYPE_NONE,
2482 2, MARSHAL_ARGUMENTS);
2484 klass->command = NULL;
2485 klass->notify = NULL;
2487 ScintillaGTK::ClassInit(object_class, widget_class, container_class);
2488 } catch (...) {
2492 static void scintilla_init(ScintillaObject *sci) {
2493 try {
2494 GTK_WIDGET_SET_FLAGS(sci, GTK_CAN_FOCUS);
2495 sci->pscin = new ScintillaGTK(sci);
2496 } catch (...) {
2500 GtkWidget* scintilla_new() {
2501 return GTK_WIDGET(g_object_new(scintilla_get_type(), NULL));
2504 void scintilla_set_id(ScintillaObject *sci, uptr_t id) {
2505 ScintillaGTK *psci = reinterpret_cast<ScintillaGTK *>(sci->pscin);
2506 psci->ctrlID = id;
2509 void scintilla_release_resources(void) {
2510 try {
2511 Platform_Finalise();
2512 } catch (...) {