Scintilla 4.0.3
[TortoiseGit.git] / ext / scintilla / include / Platform.h
blob50b9174933cd99124f9e656fc92d966532522346
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 namespace Scintilla {
76 typedef float XYPOSITION;
77 typedef double XYACCUMULATOR;
78 inline int RoundXYPosition(XYPOSITION xyPos) {
79 return static_cast<int>(xyPos + 0.5);
82 // Underlying the implementation of the platform classes are platform specific types.
83 // Sometimes these need to be passed around by client code so they are defined here
85 typedef void *FontID;
86 typedef void *SurfaceID;
87 typedef void *WindowID;
88 typedef void *MenuID;
89 typedef void *TickerID;
90 typedef void *Function;
91 typedef void *IdlerID;
93 /**
94 * A geometric point class.
95 * Point is similar to the Win32 POINT and GTK+ GdkPoint types.
97 class Point {
98 public:
99 XYPOSITION x;
100 XYPOSITION y;
102 explicit Point(XYPOSITION x_=0, XYPOSITION y_=0) : x(x_), y(y_) {
105 static Point FromInts(int x_, int y_) {
106 return Point(static_cast<XYPOSITION>(x_), static_cast<XYPOSITION>(y_));
109 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
113 * A geometric rectangle class.
114 * PRectangle is similar to the Win32 RECT.
115 * PRectangles contain their top and left sides, but not their right and bottom sides.
117 class PRectangle {
118 public:
119 XYPOSITION left;
120 XYPOSITION top;
121 XYPOSITION right;
122 XYPOSITION bottom;
124 explicit PRectangle(XYPOSITION left_=0, XYPOSITION top_=0, XYPOSITION right_=0, XYPOSITION bottom_ = 0) :
125 left(left_), top(top_), right(right_), bottom(bottom_) {
128 static PRectangle FromInts(int left_, int top_, int right_, int bottom_) {
129 return PRectangle(static_cast<XYPOSITION>(left_), static_cast<XYPOSITION>(top_),
130 static_cast<XYPOSITION>(right_), static_cast<XYPOSITION>(bottom_));
133 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
135 bool operator==(const PRectangle &rc) const {
136 return (rc.left == left) && (rc.right == right) &&
137 (rc.top == top) && (rc.bottom == bottom);
139 bool Contains(Point pt) const {
140 return (pt.x >= left) && (pt.x <= right) &&
141 (pt.y >= top) && (pt.y <= bottom);
143 bool ContainsWholePixel(Point pt) const {
144 // Does the rectangle contain all of the pixel to left/below the point
145 return (pt.x >= left) && ((pt.x+1) <= right) &&
146 (pt.y >= top) && ((pt.y+1) <= bottom);
148 bool Contains(PRectangle rc) const {
149 return (rc.left >= left) && (rc.right <= right) &&
150 (rc.top >= top) && (rc.bottom <= bottom);
152 bool Intersects(PRectangle other) const {
153 return (right > other.left) && (left < other.right) &&
154 (bottom > other.top) && (top < other.bottom);
156 void Move(XYPOSITION xDelta, XYPOSITION yDelta) {
157 left += xDelta;
158 top += yDelta;
159 right += xDelta;
160 bottom += yDelta;
162 XYPOSITION Width() const { return right - left; }
163 XYPOSITION Height() const { return bottom - top; }
164 bool Empty() const {
165 return (Height() <= 0) || (Width() <= 0);
170 * Holds a desired RGB colour.
172 class ColourDesired {
173 long co;
174 public:
175 ColourDesired(long lcol=0) {
176 co = lcol;
179 ColourDesired(unsigned int red, unsigned int green, unsigned int blue) {
180 Set(red, green, blue);
183 bool operator==(const ColourDesired &other) const {
184 return co == other.co;
187 void Set(long lcol) {
188 co = lcol;
191 void Set(unsigned int red, unsigned int green, unsigned int blue) {
192 co = red | (green << 8) | (blue << 16);
195 static inline unsigned int ValueOfHex(const char ch) {
196 if (ch >= '0' && ch <= '9')
197 return ch - '0';
198 else if (ch >= 'A' && ch <= 'F')
199 return ch - 'A' + 10;
200 else if (ch >= 'a' && ch <= 'f')
201 return ch - 'a' + 10;
202 else
203 return 0;
206 void Set(const char *val) {
207 if (*val == '#') {
208 val++;
210 unsigned int r = ValueOfHex(val[0]) * 16 + ValueOfHex(val[1]);
211 unsigned int g = ValueOfHex(val[2]) * 16 + ValueOfHex(val[3]);
212 unsigned int b = ValueOfHex(val[4]) * 16 + ValueOfHex(val[5]);
213 Set(r, g, b);
216 long AsLong() const {
217 return co;
220 unsigned int GetRed() const {
221 return co & 0xff;
224 unsigned int GetGreen() const {
225 return (co >> 8) & 0xff;
228 unsigned int GetBlue() const {
229 return (co >> 16) & 0xff;
234 * Font management.
237 struct FontParameters {
238 const char *faceName;
239 float size;
240 int weight;
241 bool italic;
242 int extraFontFlag;
243 int technology;
244 int characterSet;
246 FontParameters(
247 const char *faceName_,
248 float size_=10,
249 int weight_=400,
250 bool italic_=false,
251 int extraFontFlag_=0,
252 int technology_=0,
253 int characterSet_=0) :
255 faceName(faceName_),
256 size(size_),
257 weight(weight_),
258 italic(italic_),
259 extraFontFlag(extraFontFlag_),
260 technology(technology_),
261 characterSet(characterSet_)
267 class Font {
268 protected:
269 FontID fid;
270 // Private so Font objects can not be copied
271 Font(const Font &);
272 Font &operator=(const Font &);
273 public:
274 Font();
275 virtual ~Font();
277 virtual void Create(const FontParameters &fp);
278 virtual void Release();
280 FontID GetID() { return fid; }
281 // Alias another font - caller guarantees not to Release
282 void SetID(FontID fid_) { fid = fid_; }
283 friend class Surface;
284 friend class SurfaceImpl;
288 * A surface abstracts a place to draw.
290 class Surface {
291 private:
292 // Private so Surface objects can not be copied
293 Surface(const Surface &) {}
294 Surface &operator=(const Surface &) { return *this; }
295 public:
296 Surface() {}
297 virtual ~Surface() {}
298 static Surface *Allocate(int technology);
300 virtual void Init(WindowID wid)=0;
301 virtual void Init(SurfaceID sid, WindowID wid)=0;
302 virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid)=0;
304 virtual void Release()=0;
305 virtual bool Initialised()=0;
306 virtual void PenColour(ColourDesired fore)=0;
307 virtual int LogPixelsY()=0;
308 virtual int DeviceHeightFont(int points)=0;
309 virtual void MoveTo(int x_, int y_)=0;
310 virtual void LineTo(int x_, int y_)=0;
311 virtual void Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back)=0;
312 virtual void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
313 virtual void FillRectangle(PRectangle rc, ColourDesired back)=0;
314 virtual void FillRectangle(PRectangle rc, Surface &surfacePattern)=0;
315 virtual void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
316 virtual void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
317 ColourDesired outline, int alphaOutline, int flags)=0;
318 virtual void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) = 0;
319 virtual void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
320 virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource)=0;
322 virtual void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
323 virtual void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
324 virtual void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore)=0;
325 virtual void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions)=0;
326 virtual XYPOSITION WidthText(Font &font_, const char *s, int len)=0;
327 virtual XYPOSITION WidthChar(Font &font_, char ch)=0;
328 virtual XYPOSITION Ascent(Font &font_)=0;
329 virtual XYPOSITION Descent(Font &font_)=0;
330 virtual XYPOSITION InternalLeading(Font &font_)=0;
331 virtual XYPOSITION Height(Font &font_)=0;
332 virtual XYPOSITION AverageCharWidth(Font &font_)=0;
334 virtual void SetClip(PRectangle rc)=0;
335 virtual void FlushCachedState()=0;
337 virtual void SetUnicodeMode(bool unicodeMode_)=0;
338 virtual void SetDBCSMode(int codePage)=0;
342 * A simple callback action passing one piece of untyped user data.
344 typedef void (*CallBackAction)(void*);
347 * Class to hide the details of window manipulation.
348 * Does not own the window which will normally have a longer life than this object.
350 class Window {
351 protected:
352 WindowID wid;
353 public:
354 Window() : wid(0), cursorLast(cursorInvalid) {
356 Window(const Window &source) : wid(source.wid), cursorLast(cursorInvalid) {
358 virtual ~Window();
359 Window &operator=(WindowID wid_) {
360 wid = wid_;
361 cursorLast = cursorInvalid;
362 return *this;
364 Window &operator=(const Window &other) {
365 if (this != &other) {
366 wid = other.wid;
367 cursorLast = other.cursorLast;
369 return *this;
371 WindowID GetID() const { return wid; }
372 bool Created() const { return wid != 0; }
373 void Destroy();
374 PRectangle GetPosition();
375 void SetPosition(PRectangle rc);
376 void SetPositionRelative(PRectangle rc, Window relativeTo);
377 PRectangle GetClientPosition();
378 void Show(bool show=true);
379 void InvalidateAll();
380 void InvalidateRectangle(PRectangle rc);
381 virtual void SetFont(Font &font);
382 enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand };
383 void SetCursor(Cursor curs);
384 PRectangle GetMonitorRect(Point pt);
385 private:
386 Cursor cursorLast;
390 * Listbox management.
393 // ScintillaBase implements IListBoxDelegate to receive ListBoxEvents from a ListBox
395 struct ListBoxEvent {
396 enum class EventType { selectionChange, doubleClick } event;
397 ListBoxEvent(EventType event_) : event(event_) {
401 class IListBoxDelegate {
402 public:
403 virtual void ListNotify(ListBoxEvent *plbe)=0;
406 class ListBox : public Window {
407 public:
408 ListBox();
409 virtual ~ListBox();
410 static ListBox *Allocate();
412 virtual void SetFont(Font &font)=0;
413 virtual void Create(Window &parent, int ctrlID, Point location, int lineHeight_, bool unicodeMode_, int technology_)=0;
414 virtual void SetAverageCharWidth(int width)=0;
415 virtual void SetVisibleRows(int rows)=0;
416 virtual int GetVisibleRows() const=0;
417 virtual PRectangle GetDesiredRect()=0;
418 virtual int CaretFromEdge()=0;
419 virtual void Clear()=0;
420 virtual void Append(char *s, int type = -1)=0;
421 virtual int Length()=0;
422 virtual void Select(int n)=0;
423 virtual int GetSelection()=0;
424 virtual int Find(const char *prefix)=0;
425 virtual void GetValue(int n, char *value, int len)=0;
426 virtual void RegisterImage(int type, const char *xpm_data)=0;
427 virtual void RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage) = 0;
428 virtual void ClearRegisteredImages()=0;
429 virtual void SetDelegate(IListBoxDelegate *lbDelegate)=0;
430 virtual void SetList(const char* list, char separator, char typesep)=0;
434 * Menu management.
436 class Menu {
437 MenuID mid;
438 public:
439 Menu();
440 MenuID GetID() { return mid; }
441 void CreatePopUp();
442 void Destroy();
443 void Show(Point pt, Window &w);
446 class ElapsedTime {
447 long bigBit;
448 long littleBit;
449 public:
450 ElapsedTime();
451 double Duration(bool reset=false);
455 * Dynamic Library (DLL/SO/...) loading
457 class DynamicLibrary {
458 public:
459 virtual ~DynamicLibrary() {}
461 /// @return Pointer to function "name", or NULL on failure.
462 virtual Function FindFunction(const char *name) = 0;
464 /// @return true if the library was loaded successfully.
465 virtual bool IsValid() = 0;
467 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
468 static DynamicLibrary *Load(const char *modulePath);
471 #if defined(__clang__)
472 # if __has_feature(attribute_analyzer_noreturn)
473 # define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
474 # else
475 # define CLANG_ANALYZER_NORETURN
476 # endif
477 #else
478 # define CLANG_ANALYZER_NORETURN
479 #endif
482 * Platform class used to retrieve system wide parameters such as double click speed
483 * and chrome colour. Not a creatable object, more of a module with several functions.
485 class Platform {
486 // Private so Platform objects can not be copied
487 Platform(const Platform &) {}
488 Platform &operator=(const Platform &) { return *this; }
489 public:
490 // Should be private because no new Platforms are ever created
491 // but gcc warns about this
492 Platform() {}
493 ~Platform() {}
494 static ColourDesired Chrome();
495 static ColourDesired ChromeHighlight();
496 static const char *DefaultFont();
497 static int DefaultFontSize();
498 static unsigned int DoubleClickTime();
499 static void DebugDisplay(const char *s);
500 static long LongFromTwoShorts(short a,short b) {
501 return (a) | ((b) << 16);
504 static void DebugPrintf(const char *format, ...);
505 static bool ShowAssertionPopUps(bool assertionPopUps_);
506 static void Assert(const char *c, const char *file, int line) CLANG_ANALYZER_NORETURN;
509 #ifdef NDEBUG
510 #define PLATFORM_ASSERT(c) ((void)0)
511 #else
512 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))
513 #endif
517 #endif