GTK: Fix autoc font size on GTK >= 3.21.0
[geany-mirror.git] / scintilla / gtk / PlatGTK.cxx
blob1629818ef54e7c5d6109605123c14b3d379001b8
1 // Scintilla source code edit control
2 // PlatGTK.cxx - implementation of platform facilities on GTK+/Linux
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 <stddef.h>
7 #include <stdlib.h>
8 #include <string.h>
9 #include <stdio.h>
10 #include <math.h>
12 #include <string>
13 #include <vector>
14 #include <map>
15 #include <sstream>
17 #include <glib.h>
18 #include <gmodule.h>
19 #include <gdk/gdk.h>
20 #include <gtk/gtk.h>
21 #include <gdk/gdkkeysyms.h>
23 #include "Platform.h"
25 #include "Scintilla.h"
26 #include "ScintillaWidget.h"
27 #include "StringCopy.h"
28 #include "XPM.h"
29 #include "UniConversion.h"
31 #if defined(__clang__)
32 // Clang 3.0 incorrectly displays sentinel warnings. Fixed by clang 3.1.
33 #pragma GCC diagnostic ignored "-Wsentinel"
34 #endif
36 #include "Converter.h"
38 static const double kPi = 3.14159265358979323846;
40 // The Pango version guard for pango_units_from_double and pango_units_to_double
41 // is more complex than simply implementing these here.
43 static int pangoUnitsFromDouble(double d) {
44 return static_cast<int>(d * PANGO_SCALE + 0.5);
47 static double doubleFromPangoUnits(int pu) {
48 return static_cast<double>(pu) / PANGO_SCALE;
51 static cairo_surface_t *CreateSimilarSurface(GdkWindow *window, cairo_content_t content, int width, int height) {
52 #if GTK_CHECK_VERSION(2,22,0)
53 return gdk_window_create_similar_surface(window, content, width, height);
54 #else
55 cairo_surface_t *window_surface, *surface;
57 g_return_val_if_fail(GDK_IS_WINDOW(window), NULL);
59 window_surface = GDK_DRAWABLE_GET_CLASS(window)->ref_cairo_surface(window);
61 surface = cairo_surface_create_similar(window_surface, content, width, height);
63 cairo_surface_destroy(window_surface);
65 return surface;
66 #endif
69 static GdkWindow *WindowFromWidget(GtkWidget *w) {
70 return gtk_widget_get_window(w);
73 #ifdef _MSC_VER
74 // Ignore unreferenced local functions in GTK+ headers
75 #pragma warning(disable: 4505)
76 #endif
78 #ifdef SCI_NAMESPACE
79 using namespace Scintilla;
80 #endif
82 enum encodingType { singleByte, UTF8, dbcs};
84 // Holds a PangoFontDescription*.
85 class FontHandle {
86 public:
87 PangoFontDescription *pfd;
88 int characterSet;
89 FontHandle() : pfd(0), characterSet(-1) {
91 FontHandle(PangoFontDescription *pfd_, int characterSet_) {
92 pfd = pfd_;
93 characterSet = characterSet_;
95 ~FontHandle() {
96 if (pfd)
97 pango_font_description_free(pfd);
98 pfd = 0;
100 static FontHandle *CreateNewFont(const FontParameters &fp);
103 FontHandle *FontHandle::CreateNewFont(const FontParameters &fp) {
104 PangoFontDescription *pfd = pango_font_description_new();
105 if (pfd) {
106 pango_font_description_set_family(pfd,
107 (fp.faceName[0] == '!') ? fp.faceName+1 : fp.faceName);
108 pango_font_description_set_size(pfd, pangoUnitsFromDouble(fp.size));
109 pango_font_description_set_weight(pfd, static_cast<PangoWeight>(fp.weight));
110 pango_font_description_set_style(pfd, fp.italic ? PANGO_STYLE_ITALIC : PANGO_STYLE_NORMAL);
111 return new FontHandle(pfd,fp.characterSet);
114 return NULL;
117 // X has a 16 bit coordinate space, so stop drawing here to avoid wrapping
118 static const int maxCoordinate = 32000;
120 static FontHandle *PFont(Font &f) {
121 return static_cast<FontHandle *>(f.GetID());
124 static GtkWidget *PWidget(WindowID wid) {
125 return static_cast<GtkWidget *>(wid);
128 Point Point::FromLong(long lpoint) {
129 return Point(
130 Platform::LowShortFromLong(lpoint),
131 Platform::HighShortFromLong(lpoint));
134 Font::Font() : fid(0) {}
136 Font::~Font() {}
138 void Font::Create(const FontParameters &fp) {
139 Release();
140 fid = FontHandle::CreateNewFont(fp);
143 void Font::Release() {
144 if (fid)
145 delete static_cast<FontHandle *>(fid);
146 fid = 0;
149 // Required on OS X
150 #ifdef SCI_NAMESPACE
151 namespace Scintilla {
152 #endif
154 // SurfaceID is a cairo_t*
155 class SurfaceImpl : public Surface {
156 encodingType et;
157 cairo_t *context;
158 cairo_surface_t *psurf;
159 int x;
160 int y;
161 bool inited;
162 bool createdGC;
163 PangoContext *pcontext;
164 PangoLayout *layout;
165 Converter conv;
166 int characterSet;
167 void SetConverter(int characterSet_);
168 public:
169 SurfaceImpl();
170 virtual ~SurfaceImpl();
172 void Init(WindowID wid);
173 void Init(SurfaceID sid, WindowID wid);
174 void InitPixMap(int width, int height, Surface *surface_, WindowID wid);
176 void Release();
177 bool Initialised();
178 void PenColour(ColourDesired fore);
179 int LogPixelsY();
180 int DeviceHeightFont(int points);
181 void MoveTo(int x_, int y_);
182 void LineTo(int x_, int y_);
183 void Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back);
184 void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back);
185 void FillRectangle(PRectangle rc, ColourDesired back);
186 void FillRectangle(PRectangle rc, Surface &surfacePattern);
187 void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back);
188 void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
189 ColourDesired outline, int alphaOutline, int flags);
190 void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage);
191 void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back);
192 void Copy(PRectangle rc, Point from, Surface &surfaceSource);
194 void DrawTextBase(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore);
195 void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back);
196 void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back);
197 void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore);
198 void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions);
199 XYPOSITION WidthText(Font &font_, const char *s, int len);
200 XYPOSITION WidthChar(Font &font_, char ch);
201 XYPOSITION Ascent(Font &font_);
202 XYPOSITION Descent(Font &font_);
203 XYPOSITION InternalLeading(Font &font_);
204 XYPOSITION ExternalLeading(Font &font_);
205 XYPOSITION Height(Font &font_);
206 XYPOSITION AverageCharWidth(Font &font_);
208 void SetClip(PRectangle rc);
209 void FlushCachedState();
211 void SetUnicodeMode(bool unicodeMode_);
212 void SetDBCSMode(int codePage);
214 #ifdef SCI_NAMESPACE
216 #endif
218 const char *CharacterSetID(int characterSet) {
219 switch (characterSet) {
220 case SC_CHARSET_ANSI:
221 return "";
222 case SC_CHARSET_DEFAULT:
223 return "ISO-8859-1";
224 case SC_CHARSET_BALTIC:
225 return "ISO-8859-13";
226 case SC_CHARSET_CHINESEBIG5:
227 return "BIG-5";
228 case SC_CHARSET_EASTEUROPE:
229 return "ISO-8859-2";
230 case SC_CHARSET_GB2312:
231 return "CP936";
232 case SC_CHARSET_GREEK:
233 return "ISO-8859-7";
234 case SC_CHARSET_HANGUL:
235 return "CP949";
236 case SC_CHARSET_MAC:
237 return "MACINTOSH";
238 case SC_CHARSET_OEM:
239 return "ASCII";
240 case SC_CHARSET_RUSSIAN:
241 return "KOI8-R";
242 case SC_CHARSET_OEM866:
243 return "CP866";
244 case SC_CHARSET_CYRILLIC:
245 return "CP1251";
246 case SC_CHARSET_SHIFTJIS:
247 return "SHIFT-JIS";
248 case SC_CHARSET_SYMBOL:
249 return "";
250 case SC_CHARSET_TURKISH:
251 return "ISO-8859-9";
252 case SC_CHARSET_JOHAB:
253 return "CP1361";
254 case SC_CHARSET_HEBREW:
255 return "ISO-8859-8";
256 case SC_CHARSET_ARABIC:
257 return "ISO-8859-6";
258 case SC_CHARSET_VIETNAMESE:
259 return "";
260 case SC_CHARSET_THAI:
261 return "ISO-8859-11";
262 case SC_CHARSET_8859_15:
263 return "ISO-8859-15";
264 default:
265 return "";
269 void SurfaceImpl::SetConverter(int characterSet_) {
270 if (characterSet != characterSet_) {
271 characterSet = characterSet_;
272 conv.Open("UTF-8", CharacterSetID(characterSet), false);
276 SurfaceImpl::SurfaceImpl() : et(singleByte),
277 context(0),
278 psurf(0),
279 x(0), y(0), inited(false), createdGC(false)
280 , pcontext(0), layout(0), characterSet(-1) {
283 SurfaceImpl::~SurfaceImpl() {
284 Release();
287 void SurfaceImpl::Release() {
288 et = singleByte;
289 if (createdGC) {
290 createdGC = false;
291 cairo_destroy(context);
293 context = 0;
294 if (psurf)
295 cairo_surface_destroy(psurf);
296 psurf = 0;
297 if (layout)
298 g_object_unref(layout);
299 layout = 0;
300 if (pcontext)
301 g_object_unref(pcontext);
302 pcontext = 0;
303 conv.Close();
304 characterSet = -1;
305 x = 0;
306 y = 0;
307 inited = false;
308 createdGC = false;
311 bool SurfaceImpl::Initialised() {
312 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 8, 0)
313 if (inited && context) {
314 if (cairo_status(context) == CAIRO_STATUS_SUCCESS) {
315 // Even when status is success, the target surface may have been
316 // finished whch may cause an assertion to fail crashing the application.
317 // The cairo_surface_has_show_text_glyphs call checks the finished flag
318 // and when set, sets the status to CAIRO_STATUS_SURFACE_FINISHED
319 // which leads to warning messages instead of crashes.
320 // Performing the check in this method as it is called rarely and has no
321 // other side effects.
322 cairo_surface_t *psurfContext = cairo_get_target(context);
323 if (psurfContext) {
324 cairo_surface_has_show_text_glyphs(psurfContext);
327 return cairo_status(context) == CAIRO_STATUS_SUCCESS;
329 #endif
330 return inited;
333 void SurfaceImpl::Init(WindowID wid) {
334 Release();
335 PLATFORM_ASSERT(wid);
336 // if we are only created from a window ID, we can't perform drawing
337 psurf = 0;
338 context = 0;
339 createdGC = false;
340 pcontext = gtk_widget_create_pango_context(PWidget(wid));
341 PLATFORM_ASSERT(pcontext);
342 layout = pango_layout_new(pcontext);
343 PLATFORM_ASSERT(layout);
344 inited = true;
347 void SurfaceImpl::Init(SurfaceID sid, WindowID wid) {
348 PLATFORM_ASSERT(sid);
349 Release();
350 PLATFORM_ASSERT(wid);
351 context = cairo_reference(static_cast<cairo_t *>(sid));
352 pcontext = gtk_widget_create_pango_context(PWidget(wid));
353 // update the Pango context in case sid isn't the widget's surface
354 pango_cairo_update_context(context, pcontext);
355 layout = pango_layout_new(pcontext);
356 cairo_set_line_width(context, 1);
357 createdGC = true;
358 inited = true;
361 void SurfaceImpl::InitPixMap(int width, int height, Surface *surface_, WindowID wid) {
362 PLATFORM_ASSERT(surface_);
363 Release();
364 SurfaceImpl *surfImpl = static_cast<SurfaceImpl *>(surface_);
365 PLATFORM_ASSERT(wid);
366 context = cairo_reference(surfImpl->context);
367 pcontext = gtk_widget_create_pango_context(PWidget(wid));
368 // update the Pango context in case surface_ isn't the widget's surface
369 pango_cairo_update_context(context, pcontext);
370 PLATFORM_ASSERT(pcontext);
371 layout = pango_layout_new(pcontext);
372 PLATFORM_ASSERT(layout);
373 if (height > 0 && width > 0)
374 psurf = CreateSimilarSurface(
375 WindowFromWidget(PWidget(wid)),
376 CAIRO_CONTENT_COLOR_ALPHA, width, height);
377 cairo_destroy(context);
378 context = cairo_create(psurf);
379 cairo_rectangle(context, 0, 0, width, height);
380 cairo_set_source_rgb(context, 1.0, 0, 0);
381 cairo_fill(context);
382 // This produces sharp drawing more similar to GDK:
383 //cairo_set_antialias(context, CAIRO_ANTIALIAS_NONE);
384 cairo_set_line_width(context, 1);
385 createdGC = true;
386 inited = true;
387 et = surfImpl->et;
390 void SurfaceImpl::PenColour(ColourDesired fore) {
391 if (context) {
392 ColourDesired cdFore(fore.AsLong());
393 cairo_set_source_rgb(context,
394 cdFore.GetRed() / 255.0,
395 cdFore.GetGreen() / 255.0,
396 cdFore.GetBlue() / 255.0);
400 int SurfaceImpl::LogPixelsY() {
401 return 72;
404 int SurfaceImpl::DeviceHeightFont(int points) {
405 int logPix = LogPixelsY();
406 return (points * logPix + logPix / 2) / 72;
409 void SurfaceImpl::MoveTo(int x_, int y_) {
410 x = x_;
411 y = y_;
414 static int Delta(int difference) {
415 if (difference < 0)
416 return -1;
417 else if (difference > 0)
418 return 1;
419 else
420 return 0;
423 void SurfaceImpl::LineTo(int x_, int y_) {
424 // cairo_line_to draws the end position, unlike Win32 or GDK with GDK_CAP_NOT_LAST.
425 // For simple cases, move back one pixel from end.
426 if (context) {
427 int xDiff = x_ - x;
428 int xDelta = Delta(xDiff);
429 int yDiff = y_ - y;
430 int yDelta = Delta(yDiff);
431 if ((xDiff == 0) || (yDiff == 0)) {
432 // Horizontal or vertical lines can be more precisely drawn as a filled rectangle
433 int xEnd = x_ - xDelta;
434 int left = Platform::Minimum(x, xEnd);
435 int width = abs(x - xEnd) + 1;
436 int yEnd = y_ - yDelta;
437 int top = Platform::Minimum(y, yEnd);
438 int height = abs(y - yEnd) + 1;
439 cairo_rectangle(context, left, top, width, height);
440 cairo_fill(context);
441 } else if ((abs(xDiff) == abs(yDiff))) {
442 // 45 degree slope
443 cairo_move_to(context, x + 0.5, y + 0.5);
444 cairo_line_to(context, x_ + 0.5 - xDelta, y_ + 0.5 - yDelta);
445 } else {
446 // Line has a different slope so difficult to avoid last pixel
447 cairo_move_to(context, x + 0.5, y + 0.5);
448 cairo_line_to(context, x_ + 0.5, y_ + 0.5);
450 cairo_stroke(context);
452 x = x_;
453 y = y_;
456 void SurfaceImpl::Polygon(Point *pts, int npts, ColourDesired fore,
457 ColourDesired back) {
458 PLATFORM_ASSERT(context);
459 PenColour(back);
460 cairo_move_to(context, pts[0].x + 0.5, pts[0].y + 0.5);
461 for (int i = 1; i < npts; i++) {
462 cairo_line_to(context, pts[i].x + 0.5, pts[i].y + 0.5);
464 cairo_close_path(context);
465 cairo_fill_preserve(context);
466 PenColour(fore);
467 cairo_stroke(context);
470 void SurfaceImpl::RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back) {
471 if (context) {
472 cairo_rectangle(context, rc.left + 0.5, rc.top + 0.5,
473 rc.right - rc.left - 1, rc.bottom - rc.top - 1);
474 PenColour(back);
475 cairo_fill_preserve(context);
476 PenColour(fore);
477 cairo_stroke(context);
481 void SurfaceImpl::FillRectangle(PRectangle rc, ColourDesired back) {
482 PenColour(back);
483 if (context && (rc.left < maxCoordinate)) { // Protect against out of range
484 rc.left = lround(rc.left);
485 rc.right = lround(rc.right);
486 cairo_rectangle(context, rc.left, rc.top,
487 rc.right - rc.left, rc.bottom - rc.top);
488 cairo_fill(context);
492 void SurfaceImpl::FillRectangle(PRectangle rc, Surface &surfacePattern) {
493 SurfaceImpl &surfi = static_cast<SurfaceImpl &>(surfacePattern);
494 bool canDraw = surfi.psurf != NULL;
495 if (canDraw) {
496 PLATFORM_ASSERT(context);
497 // Tile pattern over rectangle
498 // Currently assumes 8x8 pattern
499 int widthPat = 8;
500 int heightPat = 8;
501 for (int xTile = rc.left; xTile < rc.right; xTile += widthPat) {
502 int widthx = (xTile + widthPat > rc.right) ? rc.right - xTile : widthPat;
503 for (int yTile = rc.top; yTile < rc.bottom; yTile += heightPat) {
504 int heighty = (yTile + heightPat > rc.bottom) ? rc.bottom - yTile : heightPat;
505 cairo_set_source_surface(context, surfi.psurf, xTile, yTile);
506 cairo_rectangle(context, xTile, yTile, widthx, heighty);
507 cairo_fill(context);
510 } else {
511 // Something is wrong so try to show anyway
512 // Shows up black because colour not allocated
513 FillRectangle(rc, ColourDesired(0));
517 void SurfaceImpl::RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back) {
518 if (((rc.right - rc.left) > 4) && ((rc.bottom - rc.top) > 4)) {
519 // Approximate a round rect with some cut off corners
520 Point pts[] = {
521 Point(rc.left + 2, rc.top),
522 Point(rc.right - 2, rc.top),
523 Point(rc.right, rc.top + 2),
524 Point(rc.right, rc.bottom - 2),
525 Point(rc.right - 2, rc.bottom),
526 Point(rc.left + 2, rc.bottom),
527 Point(rc.left, rc.bottom - 2),
528 Point(rc.left, rc.top + 2),
530 Polygon(pts, ELEMENTS(pts), fore, back);
531 } else {
532 RectangleDraw(rc, fore, back);
536 static void PathRoundRectangle(cairo_t *context, double left, double top, double width, double height, int radius) {
537 double degrees = kPi / 180.0;
539 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1, 2, 0)
540 cairo_new_sub_path(context);
541 #else
542 // First arc is in the top-right corner and starts from a point on the top line
543 cairo_move_to(context, left + width - radius, top);
544 #endif
545 cairo_arc(context, left + width - radius, top + radius, radius, -90 * degrees, 0 * degrees);
546 cairo_arc(context, left + width - radius, top + height - radius, radius, 0 * degrees, 90 * degrees);
547 cairo_arc(context, left + radius, top + height - radius, radius, 90 * degrees, 180 * degrees);
548 cairo_arc(context, left + radius, top + radius, radius, 180 * degrees, 270 * degrees);
549 cairo_close_path(context);
552 void SurfaceImpl::AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
553 ColourDesired outline, int alphaOutline, int flags) {
554 if (context && rc.Width() > 0) {
555 ColourDesired cdFill(fill.AsLong());
556 cairo_set_source_rgba(context,
557 cdFill.GetRed() / 255.0,
558 cdFill.GetGreen() / 255.0,
559 cdFill.GetBlue() / 255.0,
560 alphaFill / 255.0);
561 if (cornerSize > 0)
562 PathRoundRectangle(context, rc.left + 1.0, rc.top + 1.0, rc.right - rc.left - 2.0, rc.bottom - rc.top - 2.0, cornerSize);
563 else
564 cairo_rectangle(context, rc.left + 1.0, rc.top + 1.0, rc.right - rc.left - 2.0, rc.bottom - rc.top - 2.0);
565 cairo_fill(context);
567 ColourDesired cdOutline(outline.AsLong());
568 cairo_set_source_rgba(context,
569 cdOutline.GetRed() / 255.0,
570 cdOutline.GetGreen() / 255.0,
571 cdOutline.GetBlue() / 255.0,
572 alphaOutline / 255.0);
573 if (cornerSize > 0)
574 PathRoundRectangle(context, rc.left + 0.5, rc.top + 0.5, rc.right - rc.left - 1, rc.bottom - rc.top - 1, cornerSize);
575 else
576 cairo_rectangle(context, rc.left + 0.5, rc.top + 0.5, rc.right - rc.left - 1, rc.bottom - rc.top - 1);
577 cairo_stroke(context);
581 void SurfaceImpl::DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) {
582 PLATFORM_ASSERT(context);
583 if (rc.Width() > width)
584 rc.left += (rc.Width() - width) / 2;
585 rc.right = rc.left + width;
586 if (rc.Height() > height)
587 rc.top += (rc.Height() - height) / 2;
588 rc.bottom = rc.top + height;
590 #if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,6,0)
591 int stride = cairo_format_stride_for_width(CAIRO_FORMAT_ARGB32, width);
592 #else
593 int stride = width * 4;
594 #endif
595 int ucs = stride * height;
596 std::vector<unsigned char> image(ucs);
597 for (int iy=0; iy<height; iy++) {
598 for (int ix=0; ix<width; ix++) {
599 unsigned char *pixel = &image[0] + iy*stride + ix * 4;
600 unsigned char alpha = pixelsImage[3];
601 pixel[2] = (*pixelsImage++) * alpha / 255;
602 pixel[1] = (*pixelsImage++) * alpha / 255;
603 pixel[0] = (*pixelsImage++) * alpha / 255;
604 pixel[3] = *pixelsImage++;
608 cairo_surface_t *psurfImage = cairo_image_surface_create_for_data(&image[0], CAIRO_FORMAT_ARGB32, width, height, stride);
609 cairo_set_source_surface(context, psurfImage, rc.left, rc.top);
610 cairo_rectangle(context, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top);
611 cairo_fill(context);
613 cairo_surface_destroy(psurfImage);
616 void SurfaceImpl::Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back) {
617 PLATFORM_ASSERT(context);
618 PenColour(back);
619 cairo_arc(context, (rc.left + rc.right) / 2, (rc.top + rc.bottom) / 2,
620 Platform::Minimum(rc.Width(), rc.Height()) / 2, 0, 2*kPi);
621 cairo_fill_preserve(context);
622 PenColour(fore);
623 cairo_stroke(context);
626 void SurfaceImpl::Copy(PRectangle rc, Point from, Surface &surfaceSource) {
627 SurfaceImpl &surfi = static_cast<SurfaceImpl &>(surfaceSource);
628 bool canDraw = surfi.psurf != NULL;
629 if (canDraw) {
630 PLATFORM_ASSERT(context);
631 cairo_set_source_surface(context, surfi.psurf,
632 rc.left - from.x, rc.top - from.y);
633 cairo_rectangle(context, rc.left, rc.top, rc.right-rc.left, rc.bottom-rc.top);
634 cairo_fill(context);
638 std::string UTF8FromLatin1(const char *s, int len) {
639 std::string utfForm(len*2 + 1, '\0');
640 size_t lenU = 0;
641 for (int i=0; i<len; i++) {
642 unsigned int uch = static_cast<unsigned char>(s[i]);
643 if (uch < 0x80) {
644 utfForm[lenU++] = uch;
645 } else {
646 utfForm[lenU++] = static_cast<char>(0xC0 | (uch >> 6));
647 utfForm[lenU++] = static_cast<char>(0x80 | (uch & 0x3f));
650 utfForm.resize(lenU);
651 return utfForm;
654 static std::string UTF8FromIconv(const Converter &conv, const char *s, int len) {
655 if (conv) {
656 std::string utfForm(len*3+1, '\0');
657 char *pin = const_cast<char *>(s);
658 gsize inLeft = len;
659 char *putf = &utfForm[0];
660 char *pout = putf;
661 gsize outLeft = len*3+1;
662 gsize conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
663 if (conversions != sizeFailure) {
664 *pout = '\0';
665 utfForm.resize(pout - putf);
666 return utfForm;
669 return std::string();
672 // Work out how many bytes are in a character by trying to convert using iconv,
673 // returning the first length that succeeds.
674 static size_t MultiByteLenFromIconv(const Converter &conv, const char *s, size_t len) {
675 for (size_t lenMB=1; (lenMB<4) && (lenMB <= len); lenMB++) {
676 char wcForm[2];
677 char *pin = const_cast<char *>(s);
678 gsize inLeft = lenMB;
679 char *pout = wcForm;
680 gsize outLeft = 2;
681 gsize conversions = conv.Convert(&pin, &inLeft, &pout, &outLeft);
682 if (conversions != sizeFailure) {
683 return lenMB;
686 return 1;
689 void SurfaceImpl::DrawTextBase(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
690 ColourDesired fore) {
691 PenColour(fore);
692 if (context) {
693 XYPOSITION xText = rc.left;
694 if (PFont(font_)->pfd) {
695 std::string utfForm;
696 if (et == UTF8) {
697 pango_layout_set_text(layout, s, len);
698 } else {
699 SetConverter(PFont(font_)->characterSet);
700 utfForm = UTF8FromIconv(conv, s, len);
701 if (utfForm.empty()) { // iconv failed so treat as Latin1
702 utfForm = UTF8FromLatin1(s, len);
704 pango_layout_set_text(layout, utfForm.c_str(), utfForm.length());
706 pango_layout_set_font_description(layout, PFont(font_)->pfd);
707 pango_cairo_update_layout(context, layout);
708 #ifdef PANGO_VERSION
709 PangoLayoutLine *pll = pango_layout_get_line_readonly(layout,0);
710 #else
711 PangoLayoutLine *pll = pango_layout_get_line(layout,0);
712 #endif
713 cairo_move_to(context, xText, ybase);
714 pango_cairo_show_layout_line(context, pll);
719 void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
720 ColourDesired fore, ColourDesired back) {
721 FillRectangle(rc, back);
722 DrawTextBase(rc, font_, ybase, s, len, fore);
725 // On GTK+, exactly same as DrawTextNoClip
726 void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
727 ColourDesired fore, ColourDesired back) {
728 FillRectangle(rc, back);
729 DrawTextBase(rc, font_, ybase, s, len, fore);
732 void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len,
733 ColourDesired fore) {
734 // Avoid drawing spaces in transparent mode
735 for (int i=0; i<len; i++) {
736 if (s[i] != ' ') {
737 DrawTextBase(rc, font_, ybase, s, len, fore);
738 return;
743 class ClusterIterator {
744 PangoLayoutIter *iter;
745 PangoRectangle pos;
746 int lenPositions;
747 public:
748 bool finished;
749 XYPOSITION positionStart;
750 XYPOSITION position;
751 XYPOSITION distance;
752 int curIndex;
753 ClusterIterator(PangoLayout *layout, int len) : lenPositions(len), finished(false),
754 positionStart(0), position(0), distance(0), curIndex(0) {
755 iter = pango_layout_get_iter(layout);
756 pango_layout_iter_get_cluster_extents(iter, NULL, &pos);
758 ~ClusterIterator() {
759 pango_layout_iter_free(iter);
762 void Next() {
763 positionStart = position;
764 if (pango_layout_iter_next_cluster(iter)) {
765 pango_layout_iter_get_cluster_extents(iter, NULL, &pos);
766 position = doubleFromPangoUnits(pos.x);
767 curIndex = pango_layout_iter_get_index(iter);
768 } else {
769 finished = true;
770 position = doubleFromPangoUnits(pos.x + pos.width);
771 curIndex = lenPositions;
773 distance = position - positionStart;
777 void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions) {
778 if (font_.GetID()) {
779 const int lenPositions = len;
780 if (PFont(font_)->pfd) {
781 pango_layout_set_font_description(layout, PFont(font_)->pfd);
782 if (et == UTF8) {
783 // Simple and direct as UTF-8 is native Pango encoding
784 int i = 0;
785 pango_layout_set_text(layout, s, len);
786 ClusterIterator iti(layout, lenPositions);
787 while (!iti.finished) {
788 iti.Next();
789 int places = iti.curIndex - i;
790 while (i < iti.curIndex) {
791 // Evenly distribute space among bytes of this cluster.
792 // Would be better to find number of characters and then
793 // divide evenly between characters with each byte of a character
794 // being at the same position.
795 positions[i] = iti.position - (iti.curIndex - 1 - i) * iti.distance / places;
796 i++;
799 PLATFORM_ASSERT(i == lenPositions);
800 } else {
801 int positionsCalculated = 0;
802 if (et == dbcs) {
803 SetConverter(PFont(font_)->characterSet);
804 std::string utfForm = UTF8FromIconv(conv, s, len);
805 if (!utfForm.empty()) {
806 // Convert to UTF-8 so can ask Pango for widths, then
807 // Loop through UTF-8 and DBCS forms, taking account of different
808 // character byte lengths.
809 Converter convMeasure("UCS-2", CharacterSetID(characterSet), false);
810 pango_layout_set_text(layout, utfForm.c_str(), strlen(utfForm.c_str()));
811 int i = 0;
812 int clusterStart = 0;
813 ClusterIterator iti(layout, strlen(utfForm.c_str()));
814 while (!iti.finished) {
815 iti.Next();
816 int clusterEnd = iti.curIndex;
817 int places = g_utf8_strlen(utfForm.c_str() + clusterStart, clusterEnd - clusterStart);
818 int place = 1;
819 while (clusterStart < clusterEnd) {
820 size_t lenChar = MultiByteLenFromIconv(convMeasure, s+i, len-i);
821 while (lenChar--) {
822 positions[i++] = iti.position - (places - place) * iti.distance / places;
823 positionsCalculated++;
825 clusterStart += UTF8CharLength(static_cast<unsigned char>(utfForm.c_str()[clusterStart]));
826 place++;
829 PLATFORM_ASSERT(i == lenPositions);
832 if (positionsCalculated < 1 ) {
833 // Either 8-bit or DBCS conversion failed so treat as 8-bit.
834 SetConverter(PFont(font_)->characterSet);
835 const bool rtlCheck = PFont(font_)->characterSet == SC_CHARSET_HEBREW ||
836 PFont(font_)->characterSet == SC_CHARSET_ARABIC;
837 std::string utfForm = UTF8FromIconv(conv, s, len);
838 if (utfForm.empty()) {
839 utfForm = UTF8FromLatin1(s, len);
841 pango_layout_set_text(layout, utfForm.c_str(), utfForm.length());
842 int i = 0;
843 int clusterStart = 0;
844 // Each 8-bit input character may take 1 or 2 bytes in UTF-8
845 // and groups of up to 3 may be represented as ligatures.
846 ClusterIterator iti(layout, utfForm.length());
847 while (!iti.finished) {
848 iti.Next();
849 int clusterEnd = iti.curIndex;
850 int ligatureLength = g_utf8_strlen(utfForm.c_str() + clusterStart, clusterEnd - clusterStart);
851 if (rtlCheck && ((clusterEnd <= clusterStart) || (ligatureLength == 0) || (ligatureLength > 3))) {
852 // Something has gone wrong: exit quickly but pretend all the characters are equally spaced:
853 int widthLayout = 0;
854 pango_layout_get_size(layout, &widthLayout, NULL);
855 XYPOSITION widthTotal = doubleFromPangoUnits(widthLayout);
856 for (int bytePos=0; bytePos<lenPositions; bytePos++) {
857 positions[bytePos] = widthTotal / lenPositions * (bytePos + 1);
859 return;
861 PLATFORM_ASSERT(ligatureLength > 0 && ligatureLength <= 3);
862 for (int charInLig=0; charInLig<ligatureLength; charInLig++) {
863 positions[i++] = iti.position - (ligatureLength - 1 - charInLig) * iti.distance / ligatureLength;
865 clusterStart = clusterEnd;
867 while (i < lenPositions) {
868 // If something failed, fill in rest of the positions
869 positions[i++] = clusterStart;
871 PLATFORM_ASSERT(i == lenPositions);
875 } else {
876 // No font so return an ascending range of values
877 for (int i = 0; i < len; i++) {
878 positions[i] = i + 1;
883 XYPOSITION SurfaceImpl::WidthText(Font &font_, const char *s, int len) {
884 if (font_.GetID()) {
885 if (PFont(font_)->pfd) {
886 std::string utfForm;
887 pango_layout_set_font_description(layout, PFont(font_)->pfd);
888 PangoRectangle pos;
889 if (et == UTF8) {
890 pango_layout_set_text(layout, s, len);
891 } else {
892 SetConverter(PFont(font_)->characterSet);
893 utfForm = UTF8FromIconv(conv, s, len);
894 if (utfForm.empty()) { // iconv failed so treat as Latin1
895 utfForm = UTF8FromLatin1(s, len);
897 pango_layout_set_text(layout, utfForm.c_str(), utfForm.length());
899 #ifdef PANGO_VERSION
900 PangoLayoutLine *pangoLine = pango_layout_get_line_readonly(layout,0);
901 #else
902 PangoLayoutLine *pangoLine = pango_layout_get_line(layout,0);
903 #endif
904 pango_layout_line_get_extents(pangoLine, NULL, &pos);
905 return doubleFromPangoUnits(pos.width);
907 return 1;
908 } else {
909 return 1;
913 XYPOSITION SurfaceImpl::WidthChar(Font &font_, char ch) {
914 if (font_.GetID()) {
915 if (PFont(font_)->pfd) {
916 return WidthText(font_, &ch, 1);
918 return 1;
919 } else {
920 return 1;
924 // Ascent and descent determined by Pango font metrics.
926 XYPOSITION SurfaceImpl::Ascent(Font &font_) {
927 if (!(font_.GetID()))
928 return 1;
929 int ascent = 0;
930 if (PFont(font_)->pfd) {
931 PangoFontMetrics *metrics = pango_context_get_metrics(pcontext,
932 PFont(font_)->pfd, pango_context_get_language(pcontext));
933 ascent =
934 doubleFromPangoUnits(pango_font_metrics_get_ascent(metrics));
935 pango_font_metrics_unref(metrics);
937 if (ascent == 0) {
938 ascent = 1;
940 return ascent;
943 XYPOSITION SurfaceImpl::Descent(Font &font_) {
944 if (!(font_.GetID()))
945 return 1;
946 if (PFont(font_)->pfd) {
947 PangoFontMetrics *metrics = pango_context_get_metrics(pcontext,
948 PFont(font_)->pfd, pango_context_get_language(pcontext));
949 int descent = doubleFromPangoUnits(pango_font_metrics_get_descent(metrics));
950 pango_font_metrics_unref(metrics);
951 return descent;
953 return 0;
956 XYPOSITION SurfaceImpl::InternalLeading(Font &) {
957 return 0;
960 XYPOSITION SurfaceImpl::ExternalLeading(Font &) {
961 return 0;
964 XYPOSITION SurfaceImpl::Height(Font &font_) {
965 return Ascent(font_) + Descent(font_);
968 XYPOSITION SurfaceImpl::AverageCharWidth(Font &font_) {
969 return WidthChar(font_, 'n');
972 void SurfaceImpl::SetClip(PRectangle rc) {
973 PLATFORM_ASSERT(context);
974 cairo_rectangle(context, rc.left, rc.top, rc.right, rc.bottom);
975 cairo_clip(context);
978 void SurfaceImpl::FlushCachedState() {}
980 void SurfaceImpl::SetUnicodeMode(bool unicodeMode_) {
981 if (unicodeMode_)
982 et = UTF8;
985 void SurfaceImpl::SetDBCSMode(int codePage) {
986 if (codePage && (codePage != SC_CP_UTF8))
987 et = dbcs;
990 Surface *Surface::Allocate(int) {
991 return new SurfaceImpl();
994 Window::~Window() {}
996 void Window::Destroy() {
997 if (wid) {
998 ListBox *listbox = dynamic_cast<ListBox*>(this);
999 if (listbox) {
1000 gtk_widget_hide(GTK_WIDGET(wid));
1001 // clear up window content
1002 listbox->Clear();
1003 // resize the window to the smallest possible size for it to adapt
1004 // to future content
1005 gtk_window_resize(GTK_WINDOW(wid), 1, 1);
1006 } else {
1007 gtk_widget_destroy(GTK_WIDGET(wid));
1009 wid = 0;
1013 bool Window::HasFocus() {
1014 return gtk_widget_has_focus(GTK_WIDGET(wid));
1017 PRectangle Window::GetPosition() {
1018 // Before any size allocated pretend its 1000 wide so not scrolled
1019 PRectangle rc(0, 0, 1000, 1000);
1020 if (wid) {
1021 GtkAllocation allocation;
1022 gtk_widget_get_allocation(PWidget(wid), &allocation);
1023 rc.left = allocation.x;
1024 rc.top = allocation.y;
1025 if (allocation.width > 20) {
1026 rc.right = rc.left + allocation.width;
1027 rc.bottom = rc.top + allocation.height;
1030 return rc;
1033 void Window::SetPosition(PRectangle rc) {
1034 GtkAllocation alloc;
1035 alloc.x = rc.left;
1036 alloc.y = rc.top;
1037 alloc.width = rc.Width();
1038 alloc.height = rc.Height();
1039 gtk_widget_size_allocate(PWidget(wid), &alloc);
1042 void Window::SetPositionRelative(PRectangle rc, Window relativeTo) {
1043 int ox = 0;
1044 int oy = 0;
1045 gdk_window_get_origin(WindowFromWidget(PWidget(relativeTo.wid)), &ox, &oy);
1046 ox += rc.left;
1047 if (ox < 0)
1048 ox = 0;
1049 oy += rc.top;
1050 if (oy < 0)
1051 oy = 0;
1053 /* do some corrections to fit into screen */
1054 int sizex = rc.right - rc.left;
1055 int sizey = rc.bottom - rc.top;
1056 int screenWidth = gdk_screen_width();
1057 int screenHeight = gdk_screen_height();
1058 if (sizex > screenWidth)
1059 ox = 0; /* the best we can do */
1060 else if (ox + sizex > screenWidth)
1061 ox = screenWidth - sizex;
1062 if (oy + sizey > screenHeight)
1063 oy = screenHeight - sizey;
1065 gtk_window_move(GTK_WINDOW(PWidget(wid)), ox, oy);
1067 gtk_window_resize(GTK_WINDOW(wid), sizex, sizey);
1070 PRectangle Window::GetClientPosition() {
1071 // On GTK+, the client position is the window position
1072 return GetPosition();
1075 void Window::Show(bool show) {
1076 if (show)
1077 gtk_widget_show(PWidget(wid));
1080 void Window::InvalidateAll() {
1081 if (wid) {
1082 gtk_widget_queue_draw(PWidget(wid));
1086 void Window::InvalidateRectangle(PRectangle rc) {
1087 if (wid) {
1088 gtk_widget_queue_draw_area(PWidget(wid),
1089 rc.left, rc.top,
1090 rc.right - rc.left, rc.bottom - rc.top);
1094 void Window::SetFont(Font &) {
1095 // Can not be done generically but only needed for ListBox
1098 void Window::SetCursor(Cursor curs) {
1099 // We don't set the cursor to same value numerous times under gtk because
1100 // it stores the cursor in the window once it's set
1101 if (curs == cursorLast)
1102 return;
1104 cursorLast = curs;
1105 GdkDisplay *pdisplay = gtk_widget_get_display(PWidget(wid));
1107 GdkCursor *gdkCurs;
1108 switch (curs) {
1109 case cursorText:
1110 gdkCurs = gdk_cursor_new_for_display(pdisplay, GDK_XTERM);
1111 break;
1112 case cursorArrow:
1113 gdkCurs = gdk_cursor_new_for_display(pdisplay, GDK_LEFT_PTR);
1114 break;
1115 case cursorUp:
1116 gdkCurs = gdk_cursor_new_for_display(pdisplay, GDK_CENTER_PTR);
1117 break;
1118 case cursorWait:
1119 gdkCurs = gdk_cursor_new_for_display(pdisplay, GDK_WATCH);
1120 break;
1121 case cursorHand:
1122 gdkCurs = gdk_cursor_new_for_display(pdisplay, GDK_HAND2);
1123 break;
1124 case cursorReverseArrow:
1125 gdkCurs = gdk_cursor_new_for_display(pdisplay, GDK_RIGHT_PTR);
1126 break;
1127 default:
1128 gdkCurs = gdk_cursor_new_for_display(pdisplay, GDK_LEFT_PTR);
1129 cursorLast = cursorArrow;
1130 break;
1133 if (WindowFromWidget(PWidget(wid)))
1134 gdk_window_set_cursor(WindowFromWidget(PWidget(wid)), gdkCurs);
1135 #if GTK_CHECK_VERSION(3,0,0)
1136 g_object_unref(gdkCurs);
1137 #else
1138 gdk_cursor_unref(gdkCurs);
1139 #endif
1142 void Window::SetTitle(const char *s) {
1143 gtk_window_set_title(GTK_WINDOW(wid), s);
1146 /* Returns rectangle of monitor pt is on, both rect and pt are in Window's
1147 gdk window coordinates */
1148 PRectangle Window::GetMonitorRect(Point pt) {
1149 gint x_offset, y_offset;
1151 gdk_window_get_origin(WindowFromWidget(PWidget(wid)), &x_offset, &y_offset);
1153 GdkScreen* screen;
1154 gint monitor_num;
1155 GdkRectangle rect;
1157 screen = gtk_widget_get_screen(PWidget(wid));
1158 monitor_num = gdk_screen_get_monitor_at_point(screen, pt.x + x_offset, pt.y + y_offset);
1159 gdk_screen_get_monitor_geometry(screen, monitor_num, &rect);
1160 rect.x -= x_offset;
1161 rect.y -= y_offset;
1162 return PRectangle(rect.x, rect.y, rect.x + rect.width, rect.y + rect.height);
1165 typedef std::map<int, RGBAImage*> ImageMap;
1167 struct ListImage {
1168 const RGBAImage *rgba_data;
1169 GdkPixbuf *pixbuf;
1172 static void list_image_free(gpointer, gpointer value, gpointer) {
1173 ListImage *list_image = static_cast<ListImage *>(value);
1174 if (list_image->pixbuf)
1175 g_object_unref(list_image->pixbuf);
1176 g_free(list_image);
1179 ListBox::ListBox() {
1182 ListBox::~ListBox() {
1185 enum {
1186 PIXBUF_COLUMN,
1187 TEXT_COLUMN,
1188 N_COLUMNS
1191 class ListBoxX : public ListBox {
1192 WindowID widCached;
1193 WindowID frame;
1194 WindowID list;
1195 WindowID scroller;
1196 void *pixhash;
1197 GtkCellRenderer* pixbuf_renderer;
1198 GtkCellRenderer* renderer;
1199 RGBAImageSet images;
1200 int desiredVisibleRows;
1201 unsigned int maxItemCharacters;
1202 unsigned int aveCharWidth;
1203 #if GTK_CHECK_VERSION(3,0,0)
1204 GtkCssProvider *cssProvider;
1205 #endif
1206 public:
1207 CallBackAction doubleClickAction;
1208 void *doubleClickActionData;
1210 ListBoxX() : widCached(0), frame(0), list(0), scroller(0), pixhash(NULL), pixbuf_renderer(0),
1211 renderer(0),
1212 desiredVisibleRows(5), maxItemCharacters(0),
1213 aveCharWidth(1),
1214 #if GTK_CHECK_VERSION(3,0,0)
1215 cssProvider(NULL),
1216 #endif
1217 doubleClickAction(NULL), doubleClickActionData(NULL) {
1219 virtual ~ListBoxX() {
1220 if (pixhash) {
1221 g_hash_table_foreach((GHashTable *) pixhash, list_image_free, NULL);
1222 g_hash_table_destroy((GHashTable *) pixhash);
1224 if (widCached) {
1225 gtk_widget_destroy(GTK_WIDGET(widCached));
1226 wid = widCached = 0;
1228 #if GTK_CHECK_VERSION(3,0,0)
1229 if (cssProvider) {
1230 g_object_unref(cssProvider);
1231 cssProvider = NULL;
1233 #endif
1235 virtual void SetFont(Font &font);
1236 virtual void Create(Window &parent, int ctrlID, Point location_, int lineHeight_, bool unicodeMode_, int technology_);
1237 virtual void SetAverageCharWidth(int width);
1238 virtual void SetVisibleRows(int rows);
1239 virtual int GetVisibleRows() const;
1240 int GetRowHeight();
1241 virtual PRectangle GetDesiredRect();
1242 virtual int CaretFromEdge();
1243 virtual void Clear();
1244 virtual void Append(char *s, int type = -1);
1245 virtual int Length();
1246 virtual void Select(int n);
1247 virtual int GetSelection();
1248 virtual int Find(const char *prefix);
1249 virtual void GetValue(int n, char *value, int len);
1250 void RegisterRGBA(int type, RGBAImage *image);
1251 virtual void RegisterImage(int type, const char *xpm_data);
1252 virtual void RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage);
1253 virtual void ClearRegisteredImages();
1254 virtual void SetDoubleClickAction(CallBackAction action, void *data) {
1255 doubleClickAction = action;
1256 doubleClickActionData = data;
1258 virtual void SetList(const char *listText, char separator, char typesep);
1261 ListBox *ListBox::Allocate() {
1262 ListBoxX *lb = new ListBoxX();
1263 return lb;
1266 static int treeViewGetRowHeight(GtkTreeView *view) {
1267 #if GTK_CHECK_VERSION(3,0,0)
1268 // This version sometimes reports erroneous results on GTK2, but the GTK2
1269 // version is inaccurate for GTK 3.14.
1270 GdkRectangle rect;
1271 GtkTreePath *path = gtk_tree_path_new_first();
1272 gtk_tree_view_get_background_area(view, path, NULL, &rect);
1273 gtk_tree_path_free(path);
1274 return rect.height;
1275 #else
1276 int row_height=0;
1277 int vertical_separator=0;
1278 int expander_size=0;
1279 GtkTreeViewColumn *column = gtk_tree_view_get_column(view, 0);
1280 gtk_tree_view_column_cell_get_size(column, NULL, NULL, NULL, NULL, &row_height);
1281 gtk_widget_style_get(GTK_WIDGET(view),
1282 "vertical-separator", &vertical_separator,
1283 "expander-size", &expander_size, NULL);
1284 row_height += vertical_separator;
1285 row_height = Platform::Maximum(row_height, expander_size);
1286 return row_height;
1287 #endif
1290 // SmallScroller, a GtkScrolledWindow that can shrink very small, as
1291 // gtk_widget_set_size_request() cannot shrink widgets on GTK3
1292 typedef struct {
1293 GtkScrolledWindow parent;
1294 /* Workaround ABI issue with Windows GTK2 bundle and GCC > 3.
1295 See http://lists.geany.org/pipermail/devel/2015-April/thread.html#9379
1297 GtkScrolledWindow contains a bitfield, and GCC 3.4 and 4.8 don't agree
1298 on the size of the structure (regardless of -mms-bitfields):
1299 - GCC 3.4 has sizeof(GtkScrolledWindow)=88
1300 - GCC 4.8 has sizeof(GtkScrolledWindow)=84
1301 As Windows GTK2 bundle is built with GCC 3, it requires types derived
1302 from GtkScrolledWindow to be at least 88 bytes, which means we need to
1303 add some fake padding to fill in the extra 4 bytes.
1304 There is however no other issue with the layout difference as we never
1305 access any GtkScrolledWindow fields ourselves. */
1306 int padding;
1307 } SmallScroller;
1308 typedef GtkScrolledWindowClass SmallScrollerClass;
1310 G_DEFINE_TYPE(SmallScroller, small_scroller, GTK_TYPE_SCROLLED_WINDOW)
1312 #if GTK_CHECK_VERSION(3,0,0)
1313 static void small_scroller_get_preferred_height(GtkWidget *widget, gint *min, gint *nat) {
1314 GtkWidget *child = gtk_bin_get_child(GTK_BIN(widget));
1315 if (GTK_IS_TREE_VIEW(child)) {
1316 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(child));
1317 int n_rows = gtk_tree_model_iter_n_children(model, NULL);
1318 int row_height = treeViewGetRowHeight(GTK_TREE_VIEW(child));
1320 *min = MAX(1, row_height);
1321 *nat = MAX(*min, n_rows * row_height);
1322 } else {
1323 GTK_WIDGET_CLASS(small_scroller_parent_class)->get_preferred_height(widget, min, nat);
1324 if (*min > 1)
1325 *min = 1;
1328 #else
1329 static void small_scroller_size_request(GtkWidget *widget, GtkRequisition *req) {
1330 GTK_WIDGET_CLASS(small_scroller_parent_class)->size_request(widget, req);
1331 req->height = 1;
1333 #endif
1335 static void small_scroller_class_init(SmallScrollerClass *klass) {
1336 #if GTK_CHECK_VERSION(3,0,0)
1337 GTK_WIDGET_CLASS(klass)->get_preferred_height = small_scroller_get_preferred_height;
1338 #else
1339 GTK_WIDGET_CLASS(klass)->size_request = small_scroller_size_request;
1340 #endif
1343 static void small_scroller_init(SmallScroller *){}
1345 static gboolean ButtonPress(GtkWidget *, GdkEventButton* ev, gpointer p) {
1346 try {
1347 ListBoxX* lb = static_cast<ListBoxX*>(p);
1348 if (ev->type == GDK_2BUTTON_PRESS && lb->doubleClickAction != NULL) {
1349 lb->doubleClickAction(lb->doubleClickActionData);
1350 return TRUE;
1353 } catch (...) {
1354 // No pointer back to Scintilla to save status
1356 return FALSE;
1359 /* Change the active color to the selected color so the listbox uses the color
1360 scheme that it would use if it had the focus. */
1361 static void StyleSet(GtkWidget *w, GtkStyle*, void*) {
1363 g_return_if_fail(w != NULL);
1365 /* Copy the selected color to active. Note that the modify calls will cause
1366 recursive calls to this function after the value is updated and w->style to
1367 be set to a new object */
1369 #if GTK_CHECK_VERSION(3,16,0)
1370 // On recent releases of GTK+, it does not appear necessary to set the list box colours.
1371 // This may be because of common themes and may be needed with other themes.
1372 // The *override* calls are deprecated now, so only call them for older versions of GTK+.
1373 #elif GTK_CHECK_VERSION(3,0,0)
1374 GtkStyleContext *styleContext = gtk_widget_get_style_context(w);
1375 if (styleContext == NULL)
1376 return;
1378 GdkRGBA colourForeSelected;
1379 gtk_style_context_get_color(styleContext, GTK_STATE_FLAG_SELECTED, &colourForeSelected);
1380 GdkRGBA colourForeActive;
1381 gtk_style_context_get_color(styleContext, GTK_STATE_FLAG_ACTIVE, &colourForeActive);
1382 if (!gdk_rgba_equal(&colourForeSelected, &colourForeActive))
1383 gtk_widget_override_color(w, GTK_STATE_FLAG_ACTIVE, &colourForeSelected);
1385 styleContext = gtk_widget_get_style_context(w);
1386 if (styleContext == NULL)
1387 return;
1389 GdkRGBA colourBaseSelected;
1390 gtk_style_context_get_background_color(styleContext, GTK_STATE_FLAG_SELECTED, &colourBaseSelected);
1391 GdkRGBA colourBaseActive;
1392 gtk_style_context_get_background_color(styleContext, GTK_STATE_FLAG_ACTIVE, &colourBaseActive);
1393 if (!gdk_rgba_equal(&colourBaseSelected, &colourBaseActive))
1394 gtk_widget_override_background_color(w, GTK_STATE_FLAG_ACTIVE, &colourBaseSelected);
1395 #else
1396 GtkStyle *style = gtk_widget_get_style(w);
1397 if (style == NULL)
1398 return;
1399 if (!gdk_color_equal(&style->base[GTK_STATE_SELECTED], &style->base[GTK_STATE_ACTIVE]))
1400 gtk_widget_modify_base(w, GTK_STATE_ACTIVE, &style->base[GTK_STATE_SELECTED]);
1401 style = gtk_widget_get_style(w);
1402 if (style == NULL)
1403 return;
1404 if (!gdk_color_equal(&style->text[GTK_STATE_SELECTED], &style->text[GTK_STATE_ACTIVE]))
1405 gtk_widget_modify_text(w, GTK_STATE_ACTIVE, &style->text[GTK_STATE_SELECTED]);
1406 #endif
1409 void ListBoxX::Create(Window &, int, Point, int, bool, int) {
1410 if (widCached != 0) {
1411 wid = widCached;
1412 return;
1415 #if GTK_CHECK_VERSION(3,0,0)
1416 if (!cssProvider) {
1417 cssProvider = gtk_css_provider_new();
1419 #endif
1421 wid = widCached = gtk_window_new(GTK_WINDOW_POPUP);
1423 frame = gtk_frame_new(NULL);
1424 gtk_widget_show(PWidget(frame));
1425 gtk_container_add(GTK_CONTAINER(GetID()), PWidget(frame));
1426 gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_OUT);
1427 gtk_container_set_border_width(GTK_CONTAINER(frame), 0);
1429 scroller = g_object_new(small_scroller_get_type(), NULL);
1430 gtk_container_set_border_width(GTK_CONTAINER(scroller), 0);
1431 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroller),
1432 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
1433 gtk_container_add(GTK_CONTAINER(frame), PWidget(scroller));
1434 gtk_widget_show(PWidget(scroller));
1436 /* Tree and its model */
1437 GtkListStore *store =
1438 gtk_list_store_new(N_COLUMNS, GDK_TYPE_PIXBUF, G_TYPE_STRING);
1440 list = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
1441 g_signal_connect(G_OBJECT(list), "style-set", G_CALLBACK(StyleSet), NULL);
1443 #if GTK_CHECK_VERSION(3,0,0)
1444 GtkStyleContext *styleContext = gtk_widget_get_style_context(GTK_WIDGET(list));
1445 if (styleContext) {
1446 gtk_style_context_add_provider(styleContext, GTK_STYLE_PROVIDER(cssProvider),
1447 GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
1449 #endif
1451 GtkTreeSelection *selection =
1452 gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
1453 gtk_tree_selection_set_mode(selection, GTK_SELECTION_SINGLE);
1454 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(list), FALSE);
1455 gtk_tree_view_set_reorderable(GTK_TREE_VIEW(list), FALSE);
1457 /* Columns */
1458 GtkTreeViewColumn *column = gtk_tree_view_column_new();
1459 gtk_tree_view_column_set_sizing(column, GTK_TREE_VIEW_COLUMN_FIXED);
1460 gtk_tree_view_column_set_title(column, "Autocomplete");
1462 pixbuf_renderer = gtk_cell_renderer_pixbuf_new();
1463 gtk_cell_renderer_set_fixed_size(pixbuf_renderer, 0, -1);
1464 gtk_tree_view_column_pack_start(column, pixbuf_renderer, FALSE);
1465 gtk_tree_view_column_add_attribute(column, pixbuf_renderer,
1466 "pixbuf", PIXBUF_COLUMN);
1468 renderer = gtk_cell_renderer_text_new();
1469 gtk_cell_renderer_text_set_fixed_height_from_font(GTK_CELL_RENDERER_TEXT(renderer), 1);
1470 gtk_tree_view_column_pack_start(column, renderer, TRUE);
1471 gtk_tree_view_column_add_attribute(column, renderer,
1472 "text", TEXT_COLUMN);
1474 gtk_tree_view_append_column(GTK_TREE_VIEW(list), column);
1475 if (g_object_class_find_property(G_OBJECT_GET_CLASS(list), "fixed-height-mode"))
1476 g_object_set(G_OBJECT(list), "fixed-height-mode", TRUE, NULL);
1478 GtkWidget *widget = PWidget(list); // No code inside the G_OBJECT macro
1479 gtk_container_add(GTK_CONTAINER(PWidget(scroller)), widget);
1480 gtk_widget_show(widget);
1481 g_signal_connect(G_OBJECT(widget), "button_press_event",
1482 G_CALLBACK(ButtonPress), this);
1485 void ListBoxX::SetFont(Font &scint_font) {
1486 // Only do for Pango font as there have been crashes for GDK fonts
1487 if (Created() && PFont(scint_font)->pfd) {
1488 // Current font is Pango font
1489 #if GTK_CHECK_VERSION(3,0,0)
1490 if (cssProvider) {
1491 PangoFontDescription *pfd = PFont(scint_font)->pfd;
1492 std::ostringstream ssFontSetting;
1493 ssFontSetting << "GtkTreeView, treeview { ";
1494 ssFontSetting << "font-family: " << pango_font_description_get_family(pfd) << "; ";
1495 ssFontSetting << "font-size:";
1496 ssFontSetting << static_cast<double>(pango_font_description_get_size(pfd)) / PANGO_SCALE;
1497 // On GTK < 3.21.0 the units are incorrectly parsed, so a font size in points
1498 // need to use the "px" unit. Normally we only get fonts in points here, so
1499 // don't bother to handle the case the font is actually in pixels on < 3.21.0.
1500 if (gtk_check_version(3, 21, 0) != NULL || // on < 3.21.0
1501 pango_font_description_get_size_is_absolute(pfd)) {
1502 ssFontSetting << "px; ";
1503 } else {
1504 ssFontSetting << "pt; ";
1506 ssFontSetting << "font-weight:"<< pango_font_description_get_weight(pfd) << "; ";
1507 ssFontSetting << "}";
1508 gtk_css_provider_load_from_data(GTK_CSS_PROVIDER(cssProvider),
1509 ssFontSetting.str().c_str(), -1, NULL);
1511 #else
1512 gtk_widget_modify_font(PWidget(list), PFont(scint_font)->pfd);
1513 #endif
1514 gtk_cell_renderer_text_set_fixed_height_from_font(GTK_CELL_RENDERER_TEXT(renderer), -1);
1515 gtk_cell_renderer_text_set_fixed_height_from_font(GTK_CELL_RENDERER_TEXT(renderer), 1);
1519 void ListBoxX::SetAverageCharWidth(int width) {
1520 aveCharWidth = width;
1523 void ListBoxX::SetVisibleRows(int rows) {
1524 desiredVisibleRows = rows;
1527 int ListBoxX::GetVisibleRows() const {
1528 return desiredVisibleRows;
1531 int ListBoxX::GetRowHeight() {
1532 return treeViewGetRowHeight(GTK_TREE_VIEW(list));
1535 PRectangle ListBoxX::GetDesiredRect() {
1536 // Before any size allocated pretend its 100 wide so not scrolled
1537 PRectangle rc(0, 0, 100, 100);
1538 if (wid) {
1539 int rows = Length();
1540 if ((rows == 0) || (rows > desiredVisibleRows))
1541 rows = desiredVisibleRows;
1543 GtkRequisition req;
1544 // This, apparently unnecessary call, ensures gtk_tree_view_column_cell_get_size
1545 // returns reasonable values.
1546 #if GTK_CHECK_VERSION(3,0,0)
1547 gtk_widget_get_preferred_size(GTK_WIDGET(frame), NULL, &req);
1548 #else
1549 gtk_widget_size_request(GTK_WIDGET(frame), &req);
1550 #endif
1551 int height;
1553 // First calculate height of the clist for our desired visible
1554 // row count otherwise it tries to expand to the total # of rows
1555 // Get cell height
1556 int row_height = GetRowHeight();
1557 #if GTK_CHECK_VERSION(3,0,0)
1558 GtkStyleContext *styleContextFrame = gtk_widget_get_style_context(PWidget(frame));
1559 GtkStateFlags stateFlagsFrame = gtk_style_context_get_state(styleContextFrame);
1560 GtkBorder padding, border, border_border = { 0, 0, 0, 0 };
1561 gtk_style_context_get_padding(styleContextFrame, stateFlagsFrame, &padding);
1562 gtk_style_context_get_border(styleContextFrame, stateFlagsFrame, &border);
1564 # if GTK_CHECK_VERSION(3,20,0)
1565 // on GTK 3.20 the frame border is in a sub-node "border".
1566 // Unfortunately we need to be built against 3.20 to be able to support this, as it requires
1567 // new API.
1568 GtkStyleContext *styleContextFrameBorder = gtk_style_context_new();
1569 GtkWidgetPath *widget_path = gtk_widget_path_copy(gtk_style_context_get_path(styleContextFrame));
1570 gtk_widget_path_append_type(widget_path, GTK_TYPE_BORDER); // dummy type
1571 gtk_widget_path_iter_set_object_name(widget_path, -1, "border");
1572 gtk_style_context_set_path(styleContextFrameBorder, widget_path);
1573 gtk_widget_path_free(widget_path);
1574 gtk_style_context_get_border(styleContextFrameBorder, stateFlagsFrame, &border_border);
1575 g_object_unref(styleContextFrameBorder);
1576 # else // < 3.20
1577 if (gtk_check_version(3, 20, 0) == NULL) {
1578 // default to 1px all around as it's likely what it is, and so we don't miss 2px height
1579 // on GTK 3.20 when built against an earlier version.
1580 border_border.top = border_border.bottom = border_border.left = border_border.right = 1;
1582 # endif
1584 height = (rows * row_height
1585 + padding.top + padding.bottom
1586 + border.top + border.bottom
1587 + border_border.top + border_border.bottom
1588 + 2 * gtk_container_get_border_width(GTK_CONTAINER(PWidget(list))));
1589 #else
1590 height = (rows * row_height
1591 + 2 * (PWidget(frame)->style->ythickness
1592 + GTK_CONTAINER(PWidget(list))->border_width));
1593 #endif
1594 rc.bottom = height;
1596 int width = maxItemCharacters;
1597 if (width < 12)
1598 width = 12;
1599 rc.right = width * (aveCharWidth + aveCharWidth / 3);
1600 // Add horizontal padding and borders
1601 int horizontal_separator=0;
1602 gtk_widget_style_get(PWidget(list),
1603 "horizontal-separator", &horizontal_separator, NULL);
1604 rc.right += horizontal_separator;
1605 #if GTK_CHECK_VERSION(3,0,0)
1606 rc.right += (padding.left + padding.right
1607 + border.left + border.right
1608 + border_border.left + border_border.right
1609 + 2 * gtk_container_get_border_width(GTK_CONTAINER(PWidget(list))));
1610 #else
1611 rc.right += 2 * (PWidget(frame)->style->xthickness
1612 + GTK_CONTAINER(PWidget(list))->border_width);
1613 #endif
1614 if (Length() > rows) {
1615 // Add the width of the scrollbar
1616 GtkWidget *vscrollbar =
1617 gtk_scrolled_window_get_vscrollbar(GTK_SCROLLED_WINDOW(scroller));
1618 #if GTK_CHECK_VERSION(3,0,0)
1619 gtk_widget_get_preferred_size(vscrollbar, NULL, &req);
1620 #else
1621 gtk_widget_size_request(vscrollbar, &req);
1622 #endif
1623 rc.right += req.width;
1626 return rc;
1629 int ListBoxX::CaretFromEdge() {
1630 gint renderer_width, renderer_height;
1631 gtk_cell_renderer_get_fixed_size(pixbuf_renderer, &renderer_width,
1632 &renderer_height);
1633 return 4 + renderer_width;
1636 void ListBoxX::Clear() {
1637 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));
1638 gtk_list_store_clear(GTK_LIST_STORE(model));
1639 maxItemCharacters = 0;
1642 static void init_pixmap(ListImage *list_image) {
1643 if (list_image->rgba_data) {
1644 // Drop any existing pixmap/bitmap as data may have changed
1645 if (list_image->pixbuf)
1646 g_object_unref(list_image->pixbuf);
1647 list_image->pixbuf =
1648 gdk_pixbuf_new_from_data(list_image->rgba_data->Pixels(),
1649 GDK_COLORSPACE_RGB,
1650 TRUE,
1652 list_image->rgba_data->GetWidth(),
1653 list_image->rgba_data->GetHeight(),
1654 list_image->rgba_data->GetWidth() * 4,
1655 NULL,
1656 NULL);
1660 #define SPACING 5
1662 void ListBoxX::Append(char *s, int type) {
1663 ListImage *list_image = NULL;
1664 if ((type >= 0) && pixhash) {
1665 list_image = static_cast<ListImage *>(g_hash_table_lookup((GHashTable *) pixhash
1666 , (gconstpointer) GINT_TO_POINTER(type)));
1668 GtkTreeIter iter;
1669 GtkListStore *store =
1670 GTK_LIST_STORE(gtk_tree_view_get_model(GTK_TREE_VIEW(list)));
1671 gtk_list_store_append(GTK_LIST_STORE(store), &iter);
1672 if (list_image) {
1673 if (NULL == list_image->pixbuf)
1674 init_pixmap(list_image);
1675 if (list_image->pixbuf) {
1676 gtk_list_store_set(GTK_LIST_STORE(store), &iter,
1677 PIXBUF_COLUMN, list_image->pixbuf,
1678 TEXT_COLUMN, s, -1);
1680 gint pixbuf_width = gdk_pixbuf_get_width(list_image->pixbuf);
1681 gint renderer_height, renderer_width;
1682 gtk_cell_renderer_get_fixed_size(pixbuf_renderer,
1683 &renderer_width, &renderer_height);
1684 if (pixbuf_width > renderer_width)
1685 gtk_cell_renderer_set_fixed_size(pixbuf_renderer,
1686 pixbuf_width, -1);
1687 } else {
1688 gtk_list_store_set(GTK_LIST_STORE(store), &iter,
1689 TEXT_COLUMN, s, -1);
1691 } else {
1692 gtk_list_store_set(GTK_LIST_STORE(store), &iter,
1693 TEXT_COLUMN, s, -1);
1695 size_t len = strlen(s);
1696 if (maxItemCharacters < len)
1697 maxItemCharacters = len;
1700 int ListBoxX::Length() {
1701 if (wid)
1702 return gtk_tree_model_iter_n_children(gtk_tree_view_get_model
1703 (GTK_TREE_VIEW(list)), NULL);
1704 return 0;
1707 void ListBoxX::Select(int n) {
1708 GtkTreeIter iter;
1709 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));
1710 GtkTreeSelection *selection =
1711 gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
1713 if (n < 0) {
1714 gtk_tree_selection_unselect_all(selection);
1715 return;
1718 bool valid = gtk_tree_model_iter_nth_child(model, &iter, NULL, n) != FALSE;
1719 if (valid) {
1720 gtk_tree_selection_select_iter(selection, &iter);
1722 // Move the scrollbar to show the selection.
1723 int total = Length();
1724 #if GTK_CHECK_VERSION(3,0,0)
1725 GtkAdjustment *adj =
1726 gtk_scrollable_get_vadjustment(GTK_SCROLLABLE(list));
1727 #else
1728 GtkAdjustment *adj =
1729 gtk_tree_view_get_vadjustment(GTK_TREE_VIEW(list));
1730 #endif
1731 gfloat value = ((gfloat)n / total) * (gtk_adjustment_get_upper(adj) - gtk_adjustment_get_lower(adj))
1732 + gtk_adjustment_get_lower(adj) - gtk_adjustment_get_page_size(adj) / 2;
1733 // Get cell height
1734 int row_height = GetRowHeight();
1736 int rows = Length();
1737 if ((rows == 0) || (rows > desiredVisibleRows))
1738 rows = desiredVisibleRows;
1739 if (rows & 0x1) {
1740 // Odd rows to display -- We are now in the middle.
1741 // Align it so that we don't chop off rows.
1742 value += (gfloat)row_height / 2.0;
1744 // Clamp it.
1745 value = (value < 0)? 0 : value;
1746 value = (value > (gtk_adjustment_get_upper(adj) - gtk_adjustment_get_page_size(adj)))?
1747 (gtk_adjustment_get_upper(adj) - gtk_adjustment_get_page_size(adj)) : value;
1749 // Set it.
1750 gtk_adjustment_set_value(adj, value);
1751 } else {
1752 gtk_tree_selection_unselect_all(selection);
1756 int ListBoxX::GetSelection() {
1757 int index = -1;
1758 GtkTreeIter iter;
1759 GtkTreeModel *model;
1760 GtkTreeSelection *selection;
1761 selection = gtk_tree_view_get_selection(GTK_TREE_VIEW(list));
1762 if (gtk_tree_selection_get_selected(selection, &model, &iter)) {
1763 GtkTreePath *path = gtk_tree_model_get_path(model, &iter);
1764 int *indices = gtk_tree_path_get_indices(path);
1765 // Don't free indices.
1766 if (indices)
1767 index = indices[0];
1768 gtk_tree_path_free(path);
1770 return index;
1773 int ListBoxX::Find(const char *prefix) {
1774 GtkTreeIter iter;
1775 GtkTreeModel *model =
1776 gtk_tree_view_get_model(GTK_TREE_VIEW(list));
1777 bool valid = gtk_tree_model_get_iter_first(model, &iter) != FALSE;
1778 int i = 0;
1779 while(valid) {
1780 gchar *s;
1781 gtk_tree_model_get(model, &iter, TEXT_COLUMN, &s, -1);
1782 if (s && (0 == strncmp(prefix, s, strlen(prefix)))) {
1783 g_free(s);
1784 return i;
1786 g_free(s);
1787 valid = gtk_tree_model_iter_next(model, &iter) != FALSE;
1788 i++;
1790 return -1;
1793 void ListBoxX::GetValue(int n, char *value, int len) {
1794 char *text = NULL;
1795 GtkTreeIter iter;
1796 GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(list));
1797 bool valid = gtk_tree_model_iter_nth_child(model, &iter, NULL, n) != FALSE;
1798 if (valid) {
1799 gtk_tree_model_get(model, &iter, TEXT_COLUMN, &text, -1);
1801 if (text && len > 0) {
1802 g_strlcpy(value, text, len);
1803 } else {
1804 value[0] = '\0';
1806 g_free(text);
1809 // g_return_if_fail causes unnecessary compiler warning in release compile.
1810 #ifdef _MSC_VER
1811 #pragma warning(disable: 4127)
1812 #endif
1814 void ListBoxX::RegisterRGBA(int type, RGBAImage *image) {
1815 images.Add(type, image);
1817 if (!pixhash) {
1818 pixhash = g_hash_table_new(g_direct_hash, g_direct_equal);
1820 ListImage *list_image = static_cast<ListImage *>(g_hash_table_lookup((GHashTable *) pixhash,
1821 (gconstpointer) GINT_TO_POINTER(type)));
1822 if (list_image) {
1823 // Drop icon already registered
1824 if (list_image->pixbuf)
1825 g_object_unref(list_image->pixbuf);
1826 list_image->pixbuf = NULL;
1827 list_image->rgba_data = image;
1828 } else {
1829 list_image = g_new0(ListImage, 1);
1830 list_image->rgba_data = image;
1831 g_hash_table_insert((GHashTable *) pixhash, GINT_TO_POINTER(type),
1832 (gpointer) list_image);
1836 void ListBoxX::RegisterImage(int type, const char *xpm_data) {
1837 g_return_if_fail(xpm_data);
1838 XPM xpmImage(xpm_data);
1839 RegisterRGBA(type, new RGBAImage(xpmImage));
1842 void ListBoxX::RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage) {
1843 RegisterRGBA(type, new RGBAImage(width, height, 1.0, pixelsImage));
1846 void ListBoxX::ClearRegisteredImages() {
1847 images.Clear();
1850 void ListBoxX::SetList(const char *listText, char separator, char typesep) {
1851 Clear();
1852 int count = strlen(listText) + 1;
1853 std::vector<char> words(listText, listText+count);
1854 char *startword = &words[0];
1855 char *numword = NULL;
1856 int i = 0;
1857 for (; words[i]; i++) {
1858 if (words[i] == separator) {
1859 words[i] = '\0';
1860 if (numword)
1861 *numword = '\0';
1862 Append(startword, numword?atoi(numword + 1):-1);
1863 startword = &words[0] + i + 1;
1864 numword = NULL;
1865 } else if (words[i] == typesep) {
1866 numword = &words[0] + i;
1869 if (startword) {
1870 if (numword)
1871 *numword = '\0';
1872 Append(startword, numword?atoi(numword + 1):-1);
1876 Menu::Menu() : mid(0) {}
1878 void Menu::CreatePopUp() {
1879 Destroy();
1880 mid = gtk_menu_new();
1881 g_object_ref_sink(G_OBJECT(mid));
1884 void Menu::Destroy() {
1885 if (mid)
1886 g_object_unref(G_OBJECT(mid));
1887 mid = 0;
1890 static void MenuPositionFunc(GtkMenu *, gint *x, gint *y, gboolean *, gpointer userData) {
1891 sptr_t intFromPointer = GPOINTER_TO_INT(userData);
1892 *x = intFromPointer & 0xffff;
1893 *y = intFromPointer >> 16;
1896 void Menu::Show(Point pt, Window &) {
1897 int screenHeight = gdk_screen_height();
1898 int screenWidth = gdk_screen_width();
1899 GtkMenu *widget = static_cast<GtkMenu *>(mid);
1900 gtk_widget_show_all(GTK_WIDGET(widget));
1901 GtkRequisition requisition;
1902 #if GTK_CHECK_VERSION(3,0,0)
1903 gtk_widget_get_preferred_size(GTK_WIDGET(widget), NULL, &requisition);
1904 #else
1905 gtk_widget_size_request(GTK_WIDGET(widget), &requisition);
1906 #endif
1907 if ((pt.x + requisition.width) > screenWidth) {
1908 pt.x = screenWidth - requisition.width;
1910 if ((pt.y + requisition.height) > screenHeight) {
1911 pt.y = screenHeight - requisition.height;
1913 gtk_menu_popup(widget, NULL, NULL, MenuPositionFunc,
1914 GINT_TO_POINTER((static_cast<int>(pt.y) << 16) | static_cast<int>(pt.x)), 0,
1915 gtk_get_current_event_time());
1918 ElapsedTime::ElapsedTime() {
1919 GTimeVal curTime;
1920 g_get_current_time(&curTime);
1921 bigBit = curTime.tv_sec;
1922 littleBit = curTime.tv_usec;
1925 class DynamicLibraryImpl : public DynamicLibrary {
1926 protected:
1927 GModule* m;
1928 public:
1929 explicit DynamicLibraryImpl(const char *modulePath) {
1930 m = g_module_open(modulePath, G_MODULE_BIND_LAZY);
1933 virtual ~DynamicLibraryImpl() {
1934 if (m != NULL)
1935 g_module_close(m);
1938 // Use g_module_symbol to get a pointer to the relevant function.
1939 virtual Function FindFunction(const char *name) {
1940 if (m != NULL) {
1941 gpointer fn_address = NULL;
1942 gboolean status = g_module_symbol(m, name, &fn_address);
1943 if (status)
1944 return static_cast<Function>(fn_address);
1945 else
1946 return NULL;
1947 } else {
1948 return NULL;
1952 virtual bool IsValid() {
1953 return m != NULL;
1957 DynamicLibrary *DynamicLibrary::Load(const char *modulePath) {
1958 return static_cast<DynamicLibrary *>( new DynamicLibraryImpl(modulePath) );
1961 double ElapsedTime::Duration(bool reset) {
1962 GTimeVal curTime;
1963 g_get_current_time(&curTime);
1964 long endBigBit = curTime.tv_sec;
1965 long endLittleBit = curTime.tv_usec;
1966 double result = 1000000.0 * (endBigBit - bigBit);
1967 result += endLittleBit - littleBit;
1968 result /= 1000000.0;
1969 if (reset) {
1970 bigBit = endBigBit;
1971 littleBit = endLittleBit;
1973 return result;
1976 ColourDesired Platform::Chrome() {
1977 return ColourDesired(0xe0, 0xe0, 0xe0);
1980 ColourDesired Platform::ChromeHighlight() {
1981 return ColourDesired(0xff, 0xff, 0xff);
1984 const char *Platform::DefaultFont() {
1985 #ifdef G_OS_WIN32
1986 return "Lucida Console";
1987 #else
1988 return "!Sans";
1989 #endif
1992 int Platform::DefaultFontSize() {
1993 #ifdef G_OS_WIN32
1994 return 10;
1995 #else
1996 return 12;
1997 #endif
2000 unsigned int Platform::DoubleClickTime() {
2001 return 500; // Half a second
2004 bool Platform::MouseButtonBounce() {
2005 return true;
2008 void Platform::DebugDisplay(const char *s) {
2009 fprintf(stderr, "%s", s);
2012 bool Platform::IsKeyDown(int) {
2013 // TODO: discover state of keys in GTK+/X
2014 return false;
2017 long Platform::SendScintilla(
2018 WindowID w, unsigned int msg, unsigned long wParam, long lParam) {
2019 return scintilla_send_message(SCINTILLA(w), msg, wParam, lParam);
2022 long Platform::SendScintillaPointer(
2023 WindowID w, unsigned int msg, unsigned long wParam, void *lParam) {
2024 return scintilla_send_message(SCINTILLA(w), msg, wParam,
2025 reinterpret_cast<sptr_t>(lParam));
2028 bool Platform::IsDBCSLeadByte(int codePage, char ch) {
2029 // Byte ranges found in Wikipedia articles with relevant search strings in each case
2030 unsigned char uch = static_cast<unsigned char>(ch);
2031 switch (codePage) {
2032 case 932:
2033 // Shift_jis
2034 return ((uch >= 0x81) && (uch <= 0x9F)) ||
2035 ((uch >= 0xE0) && (uch <= 0xFC));
2036 // Lead bytes F0 to FC may be a Microsoft addition.
2037 case 936:
2038 // GBK
2039 return (uch >= 0x81) && (uch <= 0xFE);
2040 case 950:
2041 // Big5
2042 return (uch >= 0x81) && (uch <= 0xFE);
2043 // Korean EUC-KR may be code page 949.
2045 return false;
2048 int Platform::DBCSCharLength(int codePage, const char *s) {
2049 if (codePage == 932 || codePage == 936 || codePage == 950) {
2050 return IsDBCSLeadByte(codePage, s[0]) ? 2 : 1;
2051 } else {
2052 int bytes = mblen(s, MB_CUR_MAX);
2053 if (bytes >= 1)
2054 return bytes;
2055 else
2056 return 1;
2060 int Platform::DBCSCharMaxLength() {
2061 return MB_CUR_MAX;
2062 //return 2;
2065 // These are utility functions not really tied to a platform
2067 int Platform::Minimum(int a, int b) {
2068 if (a < b)
2069 return a;
2070 else
2071 return b;
2074 int Platform::Maximum(int a, int b) {
2075 if (a > b)
2076 return a;
2077 else
2078 return b;
2081 //#define TRACE
2083 #ifdef TRACE
2084 void Platform::DebugPrintf(const char *format, ...) {
2085 char buffer[2000];
2086 va_list pArguments;
2087 va_start(pArguments, format);
2088 vsprintf(buffer, format, pArguments);
2089 va_end(pArguments);
2090 Platform::DebugDisplay(buffer);
2092 #else
2093 void Platform::DebugPrintf(const char *, ...) {}
2095 #endif
2097 // Not supported for GTK+
2098 static bool assertionPopUps = true;
2100 bool Platform::ShowAssertionPopUps(bool assertionPopUps_) {
2101 bool ret = assertionPopUps;
2102 assertionPopUps = assertionPopUps_;
2103 return ret;
2106 void Platform::Assert(const char *c, const char *file, int line) {
2107 char buffer[2000];
2108 g_snprintf(buffer, sizeof(buffer), "Assertion [%s] failed at %s %d\r\n", c, file, line);
2109 Platform::DebugDisplay(buffer);
2110 abort();
2113 int Platform::Clamp(int val, int minVal, int maxVal) {
2114 if (val > maxVal)
2115 val = maxVal;
2116 if (val < minVal)
2117 val = minVal;
2118 return val;
2121 void Platform_Initialise() {
2124 void Platform_Finalise() {