Use static method
[TortoiseGit.git] / ext / scintilla / include / Platform.h
bloba481595a22e854667e4dd534ef3afe4d66de7a2a
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
17 #define PLAT_GTK 0
18 #define PLAT_GTK_WIN32 0
19 #define PLAT_GTK_MACOSX 0
20 #define PLAT_MACOSX 0
21 #define PLAT_WIN 0
22 #define PLAT_WX 0
23 #define PLAT_QT 0
24 #define PLAT_FOX 0
25 #define PLAT_NCURSES 0
27 #if defined(FOX)
28 #undef PLAT_FOX
29 #define PLAT_FOX 1
31 #elif defined(__WX__)
32 #undef PLAT_WX
33 #define PLAT_WX 1
35 #elif defined(NCURSES)
36 #undef PLAT_NCURSES
37 #define PLAT_NCURSES 1
39 #elif defined(SCINTILLA_QT)
40 #undef PLAT_QT
41 #define PLAT_QT 1
43 #elif defined(GTK)
44 #undef PLAT_GTK
45 #define PLAT_GTK 1
47 #if defined(__WIN32__) || defined(_MSC_VER)
48 #undef PLAT_GTK_WIN32
49 #define PLAT_GTK_WIN32 1
50 #endif
52 #if defined(__APPLE__)
53 #undef PLAT_GTK_MACOSX
54 #define PLAT_GTK_MACOSX 1
55 #endif
57 #elif defined(__APPLE__)
59 #undef PLAT_MACOSX
60 #define PLAT_MACOSX 1
62 #else
63 #undef PLAT_WIN
64 #define PLAT_WIN 1
66 #endif
68 #ifdef SCI_NAMESPACE
69 namespace Scintilla {
70 #endif
72 typedef float XYPOSITION;
73 typedef double XYACCUMULATOR;
74 //#define XYPOSITION int
76 // Underlying the implementation of the platform classes are platform specific types.
77 // Sometimes these need to be passed around by client code so they are defined here
79 typedef void *FontID;
80 typedef void *SurfaceID;
81 typedef void *WindowID;
82 typedef void *MenuID;
83 typedef void *TickerID;
84 typedef void *Function;
85 typedef void *IdlerID;
87 /**
88 * A geometric point class.
89 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
91 class Point {
92 public:
93 XYPOSITION x;
94 XYPOSITION y;
96 explicit Point(XYPOSITION x_=0, XYPOSITION y_=0) : x(x_), y(y_) {
99 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
101 static Point FromLong(long lpoint);
105 * A geometric rectangle class.
106 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
107 * PRectangles contain their top and left sides, but not their right and bottom sides.
109 class PRectangle {
110 public:
111 XYPOSITION left;
112 XYPOSITION top;
113 XYPOSITION right;
114 XYPOSITION bottom;
116 PRectangle(XYPOSITION left_=0, XYPOSITION top_=0, XYPOSITION right_=0, XYPOSITION bottom_ = 0) :
117 left(left_), top(top_), right(right_), bottom(bottom_) {
120 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
122 bool operator==(PRectangle &rc) {
123 return (rc.left == left) && (rc.right == right) &&
124 (rc.top == top) && (rc.bottom == bottom);
126 bool Contains(Point pt) {
127 return (pt.x >= left) && (pt.x <= right) &&
128 (pt.y >= top) && (pt.y <= bottom);
130 bool Contains(PRectangle rc) {
131 return (rc.left >= left) && (rc.right <= right) &&
132 (rc.top >= top) && (rc.bottom <= bottom);
134 bool Intersects(PRectangle other) {
135 return (right > other.left) && (left < other.right) &&
136 (bottom > other.top) && (top < other.bottom);
138 void Move(XYPOSITION xDelta, XYPOSITION yDelta) {
139 left += xDelta;
140 top += yDelta;
141 right += xDelta;
142 bottom += yDelta;
144 XYPOSITION Width() { return right - left; }
145 XYPOSITION Height() { return bottom - top; }
146 bool Empty() {
147 return (Height() <= 0) || (Width() <= 0);
152 * Holds a desired RGB colour.
154 class ColourDesired {
155 long co;
156 public:
157 ColourDesired(long lcol=0) {
158 co = lcol;
161 ColourDesired(unsigned int red, unsigned int green, unsigned int blue) {
162 Set(red, green, blue);
165 bool operator==(const ColourDesired &other) const {
166 return co == other.co;
169 void Set(long lcol) {
170 co = lcol;
173 void Set(unsigned int red, unsigned int green, unsigned int blue) {
174 co = red | (green << 8) | (blue << 16);
177 static inline unsigned int ValueOfHex(const char ch) {
178 if (ch >= '0' && ch <= '9')
179 return ch - '0';
180 else if (ch >= 'A' && ch <= 'F')
181 return ch - 'A' + 10;
182 else if (ch >= 'a' && ch <= 'f')
183 return ch - 'a' + 10;
184 else
185 return 0;
188 void Set(const char *val) {
189 if (*val == '#') {
190 val++;
192 unsigned int r = ValueOfHex(val[0]) * 16 + ValueOfHex(val[1]);
193 unsigned int g = ValueOfHex(val[2]) * 16 + ValueOfHex(val[3]);
194 unsigned int b = ValueOfHex(val[4]) * 16 + ValueOfHex(val[5]);
195 Set(r, g, b);
198 long AsLong() const {
199 return co;
202 unsigned int GetRed() {
203 return co & 0xff;
206 unsigned int GetGreen() {
207 return (co >> 8) & 0xff;
210 unsigned int GetBlue() {
211 return (co >> 16) & 0xff;
216 * Font management.
219 struct FontParameters {
220 const char *faceName;
221 float size;
222 int weight;
223 bool italic;
224 int extraFontFlag;
225 int technology;
226 int characterSet;
228 FontParameters(
229 const char *faceName_,
230 float size_=10,
231 int weight_=400,
232 bool italic_=false,
233 int extraFontFlag_=0,
234 int technology_=0,
235 int characterSet_=0) :
237 faceName(faceName_),
238 size(size_),
239 weight(weight_),
240 italic(italic_),
241 extraFontFlag(extraFontFlag_),
242 technology(technology_),
243 characterSet(characterSet_)
249 class Font {
250 protected:
251 FontID fid;
252 #if PLAT_WX
253 int ascent;
254 #endif
255 // Private so Font objects can not be copied
256 Font(const Font &);
257 Font &operator=(const Font &);
258 public:
259 Font();
260 virtual ~Font();
262 virtual void Create(const FontParameters &fp);
263 virtual void Release();
265 FontID GetID() { return fid; }
266 // Alias another font - caller guarantees not to Release
267 void SetID(FontID fid_) { fid = fid_; }
268 #if PLAT_WX
269 void SetAscent(int ascent_) { ascent = ascent_; }
270 #endif
271 friend class Surface;
272 friend class SurfaceImpl;
276 * A surface abstracts a place to draw.
278 class Surface {
279 private:
280 // Private so Surface objects can not be copied
281 Surface(const Surface &) {}
282 Surface &operator=(const Surface &) { return *this; }
283 public:
284 Surface() {}
285 virtual ~Surface() {}
286 static Surface *Allocate(int technology);
288 virtual void Init(WindowID wid)=0;
289 virtual void Init(SurfaceID sid, WindowID wid)=0;
290 virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid)=0;
292 virtual void Release()=0;
293 virtual bool Initialised()=0;
294 virtual void PenColour(ColourDesired fore)=0;
295 virtual int LogPixelsY()=0;
296 virtual int DeviceHeightFont(int points)=0;
297 virtual void MoveTo(int x_, int y_)=0;
298 virtual void LineTo(int x_, int y_)=0;
299 virtual void Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back)=0;
300 virtual void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
301 virtual void FillRectangle(PRectangle rc, ColourDesired back)=0;
302 virtual void FillRectangle(PRectangle rc, Surface &surfacePattern)=0;
303 virtual void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
304 virtual void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
305 ColourDesired outline, int alphaOutline, int flags)=0;
306 virtual void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) = 0;
307 virtual void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
308 virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource)=0;
310 virtual void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
311 virtual void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
312 virtual void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore)=0;
313 virtual void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions)=0;
314 virtual XYPOSITION WidthText(Font &font_, const char *s, int len)=0;
315 virtual XYPOSITION WidthChar(Font &font_, char ch)=0;
316 virtual XYPOSITION Ascent(Font &font_)=0;
317 virtual XYPOSITION Descent(Font &font_)=0;
318 virtual XYPOSITION InternalLeading(Font &font_)=0;
319 virtual XYPOSITION ExternalLeading(Font &font_)=0;
320 virtual XYPOSITION Height(Font &font_)=0;
321 virtual XYPOSITION AverageCharWidth(Font &font_)=0;
323 virtual void SetClip(PRectangle rc)=0;
324 virtual void FlushCachedState()=0;
326 virtual void SetUnicodeMode(bool unicodeMode_)=0;
327 virtual void SetDBCSMode(int codePage)=0;
331 * A simple callback action passing one piece of untyped user data.
333 typedef void (*CallBackAction)(void*);
336 * Class to hide the details of window manipulation.
337 * Does not own the window which will normally have a longer life than this object.
339 class Window {
340 protected:
341 WindowID wid;
342 #if PLAT_MACOSX
343 void *windowRef;
344 void *control;
345 #endif
346 public:
347 Window() : wid(0), cursorLast(cursorInvalid) {
348 #if PLAT_MACOSX
349 windowRef = 0;
350 control = 0;
351 #endif
353 Window(const Window &source) : wid(source.wid), cursorLast(cursorInvalid) {
354 #if PLAT_MACOSX
355 windowRef = 0;
356 control = 0;
357 #endif
359 virtual ~Window();
360 Window &operator=(WindowID wid_) {
361 wid = wid_;
362 return *this;
364 WindowID GetID() const { return wid; }
365 bool Created() const { return wid != 0; }
366 void Destroy();
367 bool HasFocus();
368 PRectangle GetPosition();
369 void SetPosition(PRectangle rc);
370 void SetPositionRelative(PRectangle rc, Window relativeTo);
371 PRectangle GetClientPosition();
372 void Show(bool show=true);
373 void InvalidateAll();
374 void InvalidateRectangle(PRectangle rc);
375 virtual void SetFont(Font &font);
376 enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand };
377 void SetCursor(Cursor curs);
378 void SetTitle(const char *s);
379 PRectangle GetMonitorRect(Point pt);
380 #if PLAT_MACOSX
381 void SetWindow(void *ref) { windowRef = ref; }
382 void SetControl(void *_control) { control = _control; }
383 #endif
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);
458 * Platform class used to retrieve system wide parameters such as double click speed
459 * and chrome colour. Not a creatable object, more of a module with several functions.
461 class Platform {
462 // Private so Platform objects can not be copied
463 Platform(const Platform &) {}
464 Platform &operator=(const Platform &) { return *this; }
465 public:
466 // Should be private because no new Platforms are ever created
467 // but gcc warns about this
468 Platform() {}
469 ~Platform() {}
470 static ColourDesired Chrome();
471 static ColourDesired ChromeHighlight();
472 static const char *DefaultFont();
473 static int DefaultFontSize();
474 static unsigned int DoubleClickTime();
475 static bool MouseButtonBounce();
476 static void DebugDisplay(const char *s);
477 static bool IsKeyDown(int key);
478 static long SendScintilla(
479 WindowID w, unsigned int msg, unsigned long wParam=0, long lParam=0);
480 static long SendScintillaPointer(
481 WindowID w, unsigned int msg, unsigned long wParam=0, void *lParam=0);
482 static bool IsDBCSLeadByte(int codePage, char ch);
483 static int DBCSCharLength(int codePage, const char *s);
484 static int DBCSCharMaxLength();
486 // These are utility functions not really tied to a platform
487 static int Minimum(int a, int b);
488 static int Maximum(int a, int b);
489 // Next three assume 16 bit shorts and 32 bit longs
490 static long LongFromTwoShorts(short a,short b) {
491 return (a) | ((b) << 16);
493 static short HighShortFromLong(long x) {
494 return static_cast<short>(x >> 16);
496 static short LowShortFromLong(long x) {
497 return static_cast<short>(x & 0xffff);
499 static void DebugPrintf(const char *format, ...);
500 static bool ShowAssertionPopUps(bool assertionPopUps_);
501 static void Assert(const char *c, const char *file, int line);
502 static int Clamp(int val, int minVal, int maxVal);
505 #ifdef NDEBUG
506 #define PLATFORM_ASSERT(c) ((void)0)
507 #else
508 #ifdef SCI_NAMESPACE
509 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))
510 #else
511 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
512 #endif
513 #endif
515 #ifdef SCI_NAMESPACE
517 #endif
519 // Shut up annoying Visual C++ warnings:
520 #ifdef _MSC_VER
521 #pragma warning(disable: 4244 4309 4514 4710)
522 #endif
524 #if defined(__GNUC__) && defined(SCINTILLA_QT)
525 #pragma GCC diagnostic ignored "-Wmissing-braces"
526 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
527 #pragma GCC diagnostic ignored "-Wchar-subscripts"
528 #endif
530 #endif