Update Scintilla to version 3.5.3
[TortoiseGit.git] / ext / scintilla / include / Platform.h
blob5f2ba0d70bb35822c12f39699674d14cbe2b6b99
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 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==(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 #if PLAT_WX
275 int ascent;
276 #endif
277 // Private so Font objects can not be copied
278 Font(const Font &);
279 Font &operator=(const Font &);
280 public:
281 Font();
282 virtual ~Font();
284 virtual void Create(const FontParameters &fp);
285 virtual void Release();
287 FontID GetID() { return fid; }
288 // Alias another font - caller guarantees not to Release
289 void SetID(FontID fid_) { fid = fid_; }
290 #if PLAT_WX
291 void SetAscent(int ascent_) { ascent = ascent_; }
292 #endif
293 friend class Surface;
294 friend class SurfaceImpl;
298 * A surface abstracts a place to draw.
300 class Surface {
301 private:
302 // Private so Surface objects can not be copied
303 Surface(const Surface &) {}
304 Surface &operator=(const Surface &) { return *this; }
305 public:
306 Surface() {}
307 virtual ~Surface() {}
308 static Surface *Allocate(int technology);
310 virtual void Init(WindowID wid)=0;
311 virtual void Init(SurfaceID sid, WindowID wid)=0;
312 virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid)=0;
314 virtual void Release()=0;
315 virtual bool Initialised()=0;
316 virtual void PenColour(ColourDesired fore)=0;
317 virtual int LogPixelsY()=0;
318 virtual int DeviceHeightFont(int points)=0;
319 virtual void MoveTo(int x_, int y_)=0;
320 virtual void LineTo(int x_, int y_)=0;
321 virtual void Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back)=0;
322 virtual void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
323 virtual void FillRectangle(PRectangle rc, ColourDesired back)=0;
324 virtual void FillRectangle(PRectangle rc, Surface &surfacePattern)=0;
325 virtual void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
326 virtual void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
327 ColourDesired outline, int alphaOutline, int flags)=0;
328 virtual void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) = 0;
329 virtual void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
330 virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource)=0;
332 virtual void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
333 virtual void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
334 virtual void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore)=0;
335 virtual void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions)=0;
336 virtual XYPOSITION WidthText(Font &font_, const char *s, int len)=0;
337 virtual XYPOSITION WidthChar(Font &font_, char ch)=0;
338 virtual XYPOSITION Ascent(Font &font_)=0;
339 virtual XYPOSITION Descent(Font &font_)=0;
340 virtual XYPOSITION InternalLeading(Font &font_)=0;
341 virtual XYPOSITION ExternalLeading(Font &font_)=0;
342 virtual XYPOSITION Height(Font &font_)=0;
343 virtual XYPOSITION AverageCharWidth(Font &font_)=0;
345 virtual void SetClip(PRectangle rc)=0;
346 virtual void FlushCachedState()=0;
348 virtual void SetUnicodeMode(bool unicodeMode_)=0;
349 virtual void SetDBCSMode(int codePage)=0;
353 * A simple callback action passing one piece of untyped user data.
355 typedef void (*CallBackAction)(void*);
358 * Class to hide the details of window manipulation.
359 * Does not own the window which will normally have a longer life than this object.
361 class Window {
362 protected:
363 WindowID wid;
364 public:
365 Window() : wid(0), cursorLast(cursorInvalid) {
367 Window(const Window &source) : wid(source.wid), cursorLast(cursorInvalid) {
369 virtual ~Window();
370 Window &operator=(WindowID wid_) {
371 wid = wid_;
372 return *this;
374 WindowID GetID() const { return wid; }
375 bool Created() const { return wid != 0; }
376 void Destroy();
377 bool HasFocus();
378 PRectangle GetPosition();
379 void SetPosition(PRectangle rc);
380 void SetPositionRelative(PRectangle rc, Window relativeTo);
381 PRectangle GetClientPosition();
382 void Show(bool show=true);
383 void InvalidateAll();
384 void InvalidateRectangle(PRectangle rc);
385 virtual void SetFont(Font &font);
386 enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand };
387 void SetCursor(Cursor curs);
388 void SetTitle(const char *s);
389 PRectangle GetMonitorRect(Point pt);
390 private:
391 Cursor cursorLast;
395 * Listbox management.
398 class ListBox : public Window {
399 public:
400 ListBox();
401 virtual ~ListBox();
402 static ListBox *Allocate();
404 virtual void SetFont(Font &font)=0;
405 virtual void Create(Window &parent, int ctrlID, Point location, int lineHeight_, bool unicodeMode_, int technology_)=0;
406 virtual void SetAverageCharWidth(int width)=0;
407 virtual void SetVisibleRows(int rows)=0;
408 virtual int GetVisibleRows() const=0;
409 virtual PRectangle GetDesiredRect()=0;
410 virtual int CaretFromEdge()=0;
411 virtual void Clear()=0;
412 virtual void Append(char *s, int type = -1)=0;
413 virtual int Length()=0;
414 virtual void Select(int n)=0;
415 virtual int GetSelection()=0;
416 virtual int Find(const char *prefix)=0;
417 virtual void GetValue(int n, char *value, int len)=0;
418 virtual void RegisterImage(int type, const char *xpm_data)=0;
419 virtual void RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage) = 0;
420 virtual void ClearRegisteredImages()=0;
421 virtual void SetDoubleClickAction(CallBackAction, void *)=0;
422 virtual void SetList(const char* list, char separator, char typesep)=0;
426 * Menu management.
428 class Menu {
429 MenuID mid;
430 public:
431 Menu();
432 MenuID GetID() { return mid; }
433 void CreatePopUp();
434 void Destroy();
435 void Show(Point pt, Window &w);
438 class ElapsedTime {
439 long bigBit;
440 long littleBit;
441 public:
442 ElapsedTime();
443 double Duration(bool reset=false);
447 * Dynamic Library (DLL/SO/...) loading
449 class DynamicLibrary {
450 public:
451 virtual ~DynamicLibrary() {}
453 /// @return Pointer to function "name", or NULL on failure.
454 virtual Function FindFunction(const char *name) = 0;
456 /// @return true if the library was loaded successfully.
457 virtual bool IsValid() = 0;
459 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
460 static DynamicLibrary *Load(const char *modulePath);
463 #if defined(__clang__)
464 # if __has_feature(attribute_analyzer_noreturn)
465 # define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
466 # else
467 # define CLANG_ANALYZER_NORETURN
468 # endif
469 #else
470 # define CLANG_ANALYZER_NORETURN
471 #endif
474 * Platform class used to retrieve system wide parameters such as double click speed
475 * and chrome colour. Not a creatable object, more of a module with several functions.
477 class Platform {
478 // Private so Platform objects can not be copied
479 Platform(const Platform &) {}
480 Platform &operator=(const Platform &) { return *this; }
481 public:
482 // Should be private because no new Platforms are ever created
483 // but gcc warns about this
484 Platform() {}
485 ~Platform() {}
486 static ColourDesired Chrome();
487 static ColourDesired ChromeHighlight();
488 static const char *DefaultFont();
489 static int DefaultFontSize();
490 static unsigned int DoubleClickTime();
491 static bool MouseButtonBounce();
492 static void DebugDisplay(const char *s);
493 static bool IsKeyDown(int key);
494 static long SendScintilla(
495 WindowID w, unsigned int msg, unsigned long wParam=0, long lParam=0);
496 static long SendScintillaPointer(
497 WindowID w, unsigned int msg, unsigned long wParam=0, void *lParam=0);
498 static bool IsDBCSLeadByte(int codePage, char ch);
499 static int DBCSCharLength(int codePage, const char *s);
500 static int DBCSCharMaxLength();
502 // These are utility functions not really tied to a platform
503 static int Minimum(int a, int b);
504 static int Maximum(int a, int b);
505 // Next three assume 16 bit shorts and 32 bit longs
506 static long LongFromTwoShorts(short a,short b) {
507 return (a) | ((b) << 16);
509 static short HighShortFromLong(long x) {
510 return static_cast<short>(x >> 16);
512 static short LowShortFromLong(long x) {
513 return static_cast<short>(x & 0xffff);
515 static void DebugPrintf(const char *format, ...);
516 static bool ShowAssertionPopUps(bool assertionPopUps_);
517 static void Assert(const char *c, const char *file, int line) CLANG_ANALYZER_NORETURN;
518 static int Clamp(int val, int minVal, int maxVal);
521 #ifdef NDEBUG
522 #define PLATFORM_ASSERT(c) ((void)0)
523 #else
524 #ifdef SCI_NAMESPACE
525 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))
526 #else
527 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
528 #endif
529 #endif
531 #ifdef SCI_NAMESPACE
533 #endif
535 #if defined(__GNUC__) && defined(SCINTILLA_QT)
536 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
537 #endif
539 #endif