Update Scintilla to version 3.7.1
[TortoiseGit.git] / ext / scintilla / include / Platform.h
blob0d8e0721ce28be5ab52951aca5422b39793e643a
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 // 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 return *this;
368 WindowID GetID() const { return wid; }
369 bool Created() const { return wid != 0; }
370 void Destroy();
371 bool HasFocus();
372 PRectangle GetPosition();
373 void SetPosition(PRectangle rc);
374 void SetPositionRelative(PRectangle rc, Window relativeTo);
375 PRectangle GetClientPosition();
376 void Show(bool show=true);
377 void InvalidateAll();
378 void InvalidateRectangle(PRectangle rc);
379 virtual void SetFont(Font &font);
380 enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand };
381 void SetCursor(Cursor curs);
382 void SetTitle(const char *s);
383 PRectangle GetMonitorRect(Point pt);
384 private:
385 Cursor cursorLast;
389 * Listbox management.
392 class ListBox : public Window {
393 public:
394 ListBox();
395 virtual ~ListBox();
396 static ListBox *Allocate();
398 virtual void SetFont(Font &font)=0;
399 virtual void Create(Window &parent, int ctrlID, Point location, int lineHeight_, bool unicodeMode_, int technology_)=0;
400 virtual void SetAverageCharWidth(int width)=0;
401 virtual void SetVisibleRows(int rows)=0;
402 virtual int GetVisibleRows() const=0;
403 virtual PRectangle GetDesiredRect()=0;
404 virtual int CaretFromEdge()=0;
405 virtual void Clear()=0;
406 virtual void Append(char *s, int type = -1)=0;
407 virtual int Length()=0;
408 virtual void Select(int n)=0;
409 virtual int GetSelection()=0;
410 virtual int Find(const char *prefix)=0;
411 virtual void GetValue(int n, char *value, int len)=0;
412 virtual void RegisterImage(int type, const char *xpm_data)=0;
413 virtual void RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage) = 0;
414 virtual void ClearRegisteredImages()=0;
415 virtual void SetDoubleClickAction(CallBackAction, void *)=0;
416 virtual void SetList(const char* list, char separator, char typesep)=0;
420 * Menu management.
422 class Menu {
423 MenuID mid;
424 public:
425 Menu();
426 MenuID GetID() { return mid; }
427 void CreatePopUp();
428 void Destroy();
429 void Show(Point pt, Window &w);
432 class ElapsedTime {
433 long bigBit;
434 long littleBit;
435 public:
436 ElapsedTime();
437 double Duration(bool reset=false);
441 * Dynamic Library (DLL/SO/...) loading
443 class DynamicLibrary {
444 public:
445 virtual ~DynamicLibrary() {}
447 /// @return Pointer to function "name", or NULL on failure.
448 virtual Function FindFunction(const char *name) = 0;
450 /// @return true if the library was loaded successfully.
451 virtual bool IsValid() = 0;
453 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
454 static DynamicLibrary *Load(const char *modulePath);
457 #if defined(__clang__)
458 # if __has_feature(attribute_analyzer_noreturn)
459 # define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
460 # else
461 # define CLANG_ANALYZER_NORETURN
462 # endif
463 #else
464 # define CLANG_ANALYZER_NORETURN
465 #endif
468 * Platform class used to retrieve system wide parameters such as double click speed
469 * and chrome colour. Not a creatable object, more of a module with several functions.
471 class Platform {
472 // Private so Platform objects can not be copied
473 Platform(const Platform &) {}
474 Platform &operator=(const Platform &) { return *this; }
475 public:
476 // Should be private because no new Platforms are ever created
477 // but gcc warns about this
478 Platform() {}
479 ~Platform() {}
480 static ColourDesired Chrome();
481 static ColourDesired ChromeHighlight();
482 static const char *DefaultFont();
483 static int DefaultFontSize();
484 static unsigned int DoubleClickTime();
485 static bool MouseButtonBounce();
486 static void DebugDisplay(const char *s);
487 static bool IsKeyDown(int key);
488 static long SendScintilla(
489 WindowID w, unsigned int msg, unsigned long wParam=0, long lParam=0);
490 static long SendScintillaPointer(
491 WindowID w, unsigned int msg, unsigned long wParam=0, void *lParam=0);
492 static bool IsDBCSLeadByte(int codePage, char ch);
493 static int DBCSCharLength(int codePage, const char *s);
494 static int DBCSCharMaxLength();
496 // These are utility functions not really tied to a platform
497 static int Minimum(int a, int b);
498 static int Maximum(int a, int b);
499 // Next three assume 16 bit shorts and 32 bit longs
500 static long LongFromTwoShorts(short a,short b) {
501 return (a) | ((b) << 16);
503 static short HighShortFromLong(long x) {
504 return static_cast<short>(x >> 16);
506 static short LowShortFromLong(long x) {
507 return static_cast<short>(x & 0xffff);
509 static void DebugPrintf(const char *format, ...);
510 static bool ShowAssertionPopUps(bool assertionPopUps_);
511 static void Assert(const char *c, const char *file, int line) CLANG_ANALYZER_NORETURN;
512 static int Clamp(int val, int minVal, int maxVal);
515 #ifdef NDEBUG
516 #define PLATFORM_ASSERT(c) ((void)0)
517 #else
518 #ifdef SCI_NAMESPACE
519 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))
520 #else
521 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
522 #endif
523 #endif
525 #ifdef SCI_NAMESPACE
527 #endif
529 #endif