Update Scintilla to version 3.7.5 (#1503)
[geany-mirror.git] / scintilla / include / Platform.h
blob570ba738bb4028c9dc39e877b1659a70f8a37fcd
1 // Scintilla source code edit control
2 /** @file Platform.h
3 ** Interface to platform facilities. Also includes some basic utilities.
4 ** Implemented in PlatGTK.cxx for GTK+/Linux, PlatWin.cxx for Windows, and PlatWX.cxx for wxWindows.
5 **/
6 // Copyright 1998-2009 by Neil Hodgson <neilh@scintilla.org>
7 // The License.txt file describes the conditions under which this software may be distributed.
9 #ifndef PLATFORM_H
10 #define PLATFORM_H
12 // PLAT_GTK = GTK+ on Linux or Win32
13 // PLAT_GTK_WIN32 is defined additionally when running PLAT_GTK under Win32
14 // PLAT_WIN = Win32 API on Win32 OS
15 // PLAT_WX is wxWindows on any supported platform
16 // PLAT_TK = Tcl/TK on Linux or Win32
18 #define PLAT_GTK 0
19 #define PLAT_GTK_WIN32 0
20 #define PLAT_GTK_MACOSX 0
21 #define PLAT_MACOSX 0
22 #define PLAT_WIN 0
23 #define PLAT_WX 0
24 #define PLAT_QT 0
25 #define PLAT_FOX 0
26 #define PLAT_CURSES 0
27 #define PLAT_TK 0
29 #if defined(FOX)
30 #undef PLAT_FOX
31 #define PLAT_FOX 1
33 #elif defined(__WX__)
34 #undef PLAT_WX
35 #define PLAT_WX 1
37 #elif defined(CURSES)
38 #undef PLAT_CURSES
39 #define PLAT_CURSES 1
41 #elif defined(SCINTILLA_QT)
42 #undef PLAT_QT
43 #define PLAT_QT 1
45 #elif defined(TK)
46 #undef PLAT_TK
47 #define PLAT_TK 1
49 #elif defined(GTK)
50 #undef PLAT_GTK
51 #define PLAT_GTK 1
53 #if defined(__WIN32__) || defined(_MSC_VER)
54 #undef PLAT_GTK_WIN32
55 #define PLAT_GTK_WIN32 1
56 #endif
58 #if defined(__APPLE__)
59 #undef PLAT_GTK_MACOSX
60 #define PLAT_GTK_MACOSX 1
61 #endif
63 #elif defined(__APPLE__)
65 #undef PLAT_MACOSX
66 #define PLAT_MACOSX 1
68 #else
69 #undef PLAT_WIN
70 #define PLAT_WIN 1
72 #endif
74 #ifdef SCI_NAMESPACE
75 namespace Scintilla {
76 #endif
78 typedef float XYPOSITION;
79 typedef double XYACCUMULATOR;
80 inline int RoundXYPosition(XYPOSITION xyPos) {
81 return static_cast<int>(xyPos + 0.5);
84 // Underlying the implementation of the platform classes are platform specific types.
85 // Sometimes these need to be passed around by client code so they are defined here
87 typedef void *FontID;
88 typedef void *SurfaceID;
89 typedef void *WindowID;
90 typedef void *MenuID;
91 typedef void *TickerID;
92 typedef void *Function;
93 typedef void *IdlerID;
95 /**
96 * A geometric point class.
97 * Point is similar to the Win32 POINT and GTK+ GdkPoint types.
99 class Point {
100 public:
101 XYPOSITION x;
102 XYPOSITION y;
104 explicit Point(XYPOSITION x_=0, XYPOSITION y_=0) : x(x_), y(y_) {
107 static Point FromInts(int x_, int y_) {
108 return Point(static_cast<XYPOSITION>(x_), static_cast<XYPOSITION>(y_));
111 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
113 static Point FromLong(long lpoint);
117 * A geometric rectangle class.
118 * PRectangle is similar to the Win32 RECT.
119 * PRectangles contain their top and left sides, but not their right and bottom sides.
121 class PRectangle {
122 public:
123 XYPOSITION left;
124 XYPOSITION top;
125 XYPOSITION right;
126 XYPOSITION bottom;
128 explicit PRectangle(XYPOSITION left_=0, XYPOSITION top_=0, XYPOSITION right_=0, XYPOSITION bottom_ = 0) :
129 left(left_), top(top_), right(right_), bottom(bottom_) {
132 static PRectangle FromInts(int left_, int top_, int right_, int bottom_) {
133 return PRectangle(static_cast<XYPOSITION>(left_), static_cast<XYPOSITION>(top_),
134 static_cast<XYPOSITION>(right_), static_cast<XYPOSITION>(bottom_));
137 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
139 bool operator==(const PRectangle &rc) const {
140 return (rc.left == left) && (rc.right == right) &&
141 (rc.top == top) && (rc.bottom == bottom);
143 bool Contains(Point pt) const {
144 return (pt.x >= left) && (pt.x <= right) &&
145 (pt.y >= top) && (pt.y <= bottom);
147 bool ContainsWholePixel(Point pt) const {
148 // Does the rectangle contain all of the pixel to left/below the point
149 return (pt.x >= left) && ((pt.x+1) <= right) &&
150 (pt.y >= top) && ((pt.y+1) <= bottom);
152 bool Contains(PRectangle rc) const {
153 return (rc.left >= left) && (rc.right <= right) &&
154 (rc.top >= top) && (rc.bottom <= bottom);
156 bool Intersects(PRectangle other) const {
157 return (right > other.left) && (left < other.right) &&
158 (bottom > other.top) && (top < other.bottom);
160 void Move(XYPOSITION xDelta, XYPOSITION yDelta) {
161 left += xDelta;
162 top += yDelta;
163 right += xDelta;
164 bottom += yDelta;
166 XYPOSITION Width() const { return right - left; }
167 XYPOSITION Height() const { return bottom - top; }
168 bool Empty() const {
169 return (Height() <= 0) || (Width() <= 0);
174 * Holds a desired RGB colour.
176 class ColourDesired {
177 long co;
178 public:
179 ColourDesired(long lcol=0) {
180 co = lcol;
183 ColourDesired(unsigned int red, unsigned int green, unsigned int blue) {
184 Set(red, green, blue);
187 bool operator==(const ColourDesired &other) const {
188 return co == other.co;
191 void Set(long lcol) {
192 co = lcol;
195 void Set(unsigned int red, unsigned int green, unsigned int blue) {
196 co = red | (green << 8) | (blue << 16);
199 static inline unsigned int ValueOfHex(const char ch) {
200 if (ch >= '0' && ch <= '9')
201 return ch - '0';
202 else if (ch >= 'A' && ch <= 'F')
203 return ch - 'A' + 10;
204 else if (ch >= 'a' && ch <= 'f')
205 return ch - 'a' + 10;
206 else
207 return 0;
210 void Set(const char *val) {
211 if (*val == '#') {
212 val++;
214 unsigned int r = ValueOfHex(val[0]) * 16 + ValueOfHex(val[1]);
215 unsigned int g = ValueOfHex(val[2]) * 16 + ValueOfHex(val[3]);
216 unsigned int b = ValueOfHex(val[4]) * 16 + ValueOfHex(val[5]);
217 Set(r, g, b);
220 long AsLong() const {
221 return co;
224 unsigned int GetRed() const {
225 return co & 0xff;
228 unsigned int GetGreen() const {
229 return (co >> 8) & 0xff;
232 unsigned int GetBlue() const {
233 return (co >> 16) & 0xff;
238 * Font management.
241 struct FontParameters {
242 const char *faceName;
243 float size;
244 int weight;
245 bool italic;
246 int extraFontFlag;
247 int technology;
248 int characterSet;
250 FontParameters(
251 const char *faceName_,
252 float size_=10,
253 int weight_=400,
254 bool italic_=false,
255 int extraFontFlag_=0,
256 int technology_=0,
257 int characterSet_=0) :
259 faceName(faceName_),
260 size(size_),
261 weight(weight_),
262 italic(italic_),
263 extraFontFlag(extraFontFlag_),
264 technology(technology_),
265 characterSet(characterSet_)
271 class Font {
272 protected:
273 FontID fid;
274 // Private so Font objects can not be copied
275 Font(const Font &);
276 Font &operator=(const Font &);
277 public:
278 Font();
279 virtual ~Font();
281 virtual void Create(const FontParameters &fp);
282 virtual void Release();
284 FontID GetID() { return fid; }
285 // Alias another font - caller guarantees not to Release
286 void SetID(FontID fid_) { fid = fid_; }
287 friend class Surface;
288 friend class SurfaceImpl;
292 * A surface abstracts a place to draw.
294 class Surface {
295 private:
296 // Private so Surface objects can not be copied
297 Surface(const Surface &) {}
298 Surface &operator=(const Surface &) { return *this; }
299 public:
300 Surface() {}
301 virtual ~Surface() {}
302 static Surface *Allocate(int technology);
304 virtual void Init(WindowID wid)=0;
305 virtual void Init(SurfaceID sid, WindowID wid)=0;
306 virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid)=0;
308 virtual void Release()=0;
309 virtual bool Initialised()=0;
310 virtual void PenColour(ColourDesired fore)=0;
311 virtual int LogPixelsY()=0;
312 virtual int DeviceHeightFont(int points)=0;
313 virtual void MoveTo(int x_, int y_)=0;
314 virtual void LineTo(int x_, int y_)=0;
315 virtual void Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back)=0;
316 virtual void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
317 virtual void FillRectangle(PRectangle rc, ColourDesired back)=0;
318 virtual void FillRectangle(PRectangle rc, Surface &surfacePattern)=0;
319 virtual void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
320 virtual void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
321 ColourDesired outline, int alphaOutline, int flags)=0;
322 virtual void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) = 0;
323 virtual void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
324 virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource)=0;
326 virtual void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
327 virtual void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
328 virtual void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore)=0;
329 virtual void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions)=0;
330 virtual XYPOSITION WidthText(Font &font_, const char *s, int len)=0;
331 virtual XYPOSITION WidthChar(Font &font_, char ch)=0;
332 virtual XYPOSITION Ascent(Font &font_)=0;
333 virtual XYPOSITION Descent(Font &font_)=0;
334 virtual XYPOSITION InternalLeading(Font &font_)=0;
335 virtual XYPOSITION ExternalLeading(Font &font_)=0;
336 virtual XYPOSITION Height(Font &font_)=0;
337 virtual XYPOSITION AverageCharWidth(Font &font_)=0;
339 virtual void SetClip(PRectangle rc)=0;
340 virtual void FlushCachedState()=0;
342 virtual void SetUnicodeMode(bool unicodeMode_)=0;
343 virtual void SetDBCSMode(int codePage)=0;
347 * A simple callback action passing one piece of untyped user data.
349 typedef void (*CallBackAction)(void*);
352 * Class to hide the details of window manipulation.
353 * Does not own the window which will normally have a longer life than this object.
355 class Window {
356 protected:
357 WindowID wid;
358 public:
359 Window() : wid(0), cursorLast(cursorInvalid) {
361 Window(const Window &source) : wid(source.wid), cursorLast(cursorInvalid) {
363 virtual ~Window();
364 Window &operator=(WindowID wid_) {
365 wid = wid_;
366 cursorLast = cursorInvalid;
367 return *this;
369 Window &operator=(const Window &other) {
370 if (this != &other) {
371 wid = other.wid;
372 cursorLast = other.cursorLast;
374 return *this;
376 WindowID GetID() const { return wid; }
377 bool Created() const { return wid != 0; }
378 void Destroy();
379 bool HasFocus();
380 PRectangle GetPosition();
381 void SetPosition(PRectangle rc);
382 void SetPositionRelative(PRectangle rc, Window relativeTo);
383 PRectangle GetClientPosition();
384 void Show(bool show=true);
385 void InvalidateAll();
386 void InvalidateRectangle(PRectangle rc);
387 virtual void SetFont(Font &font);
388 enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand };
389 void SetCursor(Cursor curs);
390 void SetTitle(const char *s);
391 PRectangle GetMonitorRect(Point pt);
392 private:
393 Cursor cursorLast;
397 * Listbox management.
400 class ListBox : public Window {
401 public:
402 ListBox();
403 virtual ~ListBox();
404 static ListBox *Allocate();
406 virtual void SetFont(Font &font)=0;
407 virtual void Create(Window &parent, int ctrlID, Point location, int lineHeight_, bool unicodeMode_, int technology_)=0;
408 virtual void SetAverageCharWidth(int width)=0;
409 virtual void SetVisibleRows(int rows)=0;
410 virtual int GetVisibleRows() const=0;
411 virtual PRectangle GetDesiredRect()=0;
412 virtual int CaretFromEdge()=0;
413 virtual void Clear()=0;
414 virtual void Append(char *s, int type = -1)=0;
415 virtual int Length()=0;
416 virtual void Select(int n)=0;
417 virtual int GetSelection()=0;
418 virtual int Find(const char *prefix)=0;
419 virtual void GetValue(int n, char *value, int len)=0;
420 virtual void RegisterImage(int type, const char *xpm_data)=0;
421 virtual void RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage) = 0;
422 virtual void ClearRegisteredImages()=0;
423 virtual void SetDoubleClickAction(CallBackAction, void *)=0;
424 virtual void SetList(const char* list, char separator, char typesep)=0;
428 * Menu management.
430 class Menu {
431 MenuID mid;
432 public:
433 Menu();
434 MenuID GetID() { return mid; }
435 void CreatePopUp();
436 void Destroy();
437 void Show(Point pt, Window &w);
440 class ElapsedTime {
441 long bigBit;
442 long littleBit;
443 public:
444 ElapsedTime();
445 double Duration(bool reset=false);
449 * Dynamic Library (DLL/SO/...) loading
451 class DynamicLibrary {
452 public:
453 virtual ~DynamicLibrary() {}
455 /// @return Pointer to function "name", or NULL on failure.
456 virtual Function FindFunction(const char *name) = 0;
458 /// @return true if the library was loaded successfully.
459 virtual bool IsValid() = 0;
461 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
462 static DynamicLibrary *Load(const char *modulePath);
465 #if defined(__clang__)
466 # if __has_feature(attribute_analyzer_noreturn)
467 # define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
468 # else
469 # define CLANG_ANALYZER_NORETURN
470 # endif
471 #else
472 # define CLANG_ANALYZER_NORETURN
473 #endif
476 * Platform class used to retrieve system wide parameters such as double click speed
477 * and chrome colour. Not a creatable object, more of a module with several functions.
479 class Platform {
480 // Private so Platform objects can not be copied
481 Platform(const Platform &) {}
482 Platform &operator=(const Platform &) { return *this; }
483 public:
484 // Should be private because no new Platforms are ever created
485 // but gcc warns about this
486 Platform() {}
487 ~Platform() {}
488 static ColourDesired Chrome();
489 static ColourDesired ChromeHighlight();
490 static const char *DefaultFont();
491 static int DefaultFontSize();
492 static unsigned int DoubleClickTime();
493 static bool MouseButtonBounce();
494 static void DebugDisplay(const char *s);
495 static bool IsKeyDown(int key);
496 static long SendScintilla(
497 WindowID w, unsigned int msg, unsigned long wParam=0, long lParam=0);
498 static long SendScintillaPointer(
499 WindowID w, unsigned int msg, unsigned long wParam=0, void *lParam=0);
500 static bool IsDBCSLeadByte(int codePage, char ch);
501 static int DBCSCharLength(int codePage, const char *s);
502 static int DBCSCharMaxLength();
504 // These are utility functions not really tied to a platform
505 static int Minimum(int a, int b);
506 static int Maximum(int a, int b);
507 // Next three assume 16 bit shorts and 32 bit longs
508 static long LongFromTwoShorts(short a,short b) {
509 return (a) | ((b) << 16);
511 static short HighShortFromLong(long x) {
512 return static_cast<short>(x >> 16);
514 static short LowShortFromLong(long x) {
515 return static_cast<short>(x & 0xffff);
517 static void DebugPrintf(const char *format, ...);
518 static bool ShowAssertionPopUps(bool assertionPopUps_);
519 static void Assert(const char *c, const char *file, int line) CLANG_ANALYZER_NORETURN;
520 static int Clamp(int val, int minVal, int maxVal);
523 #ifdef NDEBUG
524 #define PLATFORM_ASSERT(c) ((void)0)
525 #else
526 #ifdef SCI_NAMESPACE
527 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))
528 #else
529 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
530 #endif
531 #endif
533 #ifdef SCI_NAMESPACE
535 #endif
537 #endif