updated Scintilla to 2.29
[TortoiseGit.git] / ext / scintilla / include / Platform.h
blob99a23027a707e69a9d0217f26a7e015c2463b1b5
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_FOX 0
25 #if defined(FOX)
26 #undef PLAT_FOX
27 #define PLAT_FOX 1
29 #elif defined(__WX__)
30 #undef PLAT_WX
31 #define PLAT_WX 1
33 #elif defined(GTK)
34 #undef PLAT_GTK
35 #define PLAT_GTK 1
37 #if defined(__WIN32__) || defined(_MSC_VER)
38 #undef PLAT_GTK_WIN32
39 #define PLAT_GTK_WIN32 1
40 #endif
42 #if defined(__APPLE__)
43 #undef PLAT_GTK_MACOSX
44 #define PLAT_GTK_MACOSX 1
45 #endif
47 #elif defined(__APPLE__)
49 #undef PLAT_MACOSX
50 #define PLAT_MACOSX 1
52 #else
53 #undef PLAT_WIN
54 #define PLAT_WIN 1
56 #endif
58 #ifdef SCI_NAMESPACE
59 namespace Scintilla {
60 #endif
62 // Underlying the implementation of the platform classes are platform specific types.
63 // Sometimes these need to be passed around by client code so they are defined here
65 typedef void *FontID;
66 typedef void *SurfaceID;
67 typedef void *WindowID;
68 typedef void *MenuID;
69 typedef void *TickerID;
70 typedef void *Function;
71 typedef void *IdlerID;
73 /**
74 * A geometric point class.
75 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
77 class Point {
78 public:
79 int x;
80 int y;
82 explicit Point(int x_=0, int y_=0) : x(x_), y(y_) {
85 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
87 static Point FromLong(long lpoint);
90 /**
91 * A geometric rectangle class.
92 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
93 * PRectangles contain their top and left sides, but not their right and bottom sides.
95 class PRectangle {
96 public:
97 int left;
98 int top;
99 int right;
100 int bottom;
102 PRectangle(int left_=0, int top_=0, int right_=0, int bottom_ = 0) :
103 left(left_), top(top_), right(right_), bottom(bottom_) {
106 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
108 bool operator==(PRectangle &rc) {
109 return (rc.left == left) && (rc.right == right) &&
110 (rc.top == top) && (rc.bottom == bottom);
112 bool Contains(Point pt) {
113 return (pt.x >= left) && (pt.x <= right) &&
114 (pt.y >= top) && (pt.y <= bottom);
116 bool Contains(PRectangle rc) {
117 return (rc.left >= left) && (rc.right <= right) &&
118 (rc.top >= top) && (rc.bottom <= bottom);
120 bool Intersects(PRectangle other) {
121 return (right > other.left) && (left < other.right) &&
122 (bottom > other.top) && (top < other.bottom);
124 void Move(int xDelta, int yDelta) {
125 left += xDelta;
126 top += yDelta;
127 right += xDelta;
128 bottom += yDelta;
130 int Width() { return right - left; }
131 int Height() { return bottom - top; }
132 bool Empty() {
133 return (Height() <= 0) || (Width() <= 0);
138 * In some circumstances, including Win32 in paletted mode and GTK+, each colour
139 * must be allocated before use. The desired colours are held in the ColourDesired class,
140 * and after allocation the allocation entry is stored in the ColourAllocated class. In other
141 * circumstances, such as Win32 in true colour mode, the allocation process just copies
142 * the RGB values from the desired to the allocated class.
143 * As each desired colour requires allocation before it can be used, the ColourPair class
144 * holds both a ColourDesired and a ColourAllocated
145 * The Palette class is responsible for managing the palette of colours which contains a
146 * list of ColourPair objects and performs the allocation.
150 * Holds a desired RGB colour.
152 class ColourDesired {
153 long co;
154 public:
155 ColourDesired(long lcol=0) {
156 co = lcol;
159 ColourDesired(unsigned int red, unsigned int green, unsigned int blue) {
160 Set(red, green, blue);
163 bool operator==(const ColourDesired &other) const {
164 return co == other.co;
167 void Set(long lcol) {
168 co = lcol;
171 void Set(unsigned int red, unsigned int green, unsigned int blue) {
172 co = red | (green << 8) | (blue << 16);
175 static inline unsigned int ValueOfHex(const char ch) {
176 if (ch >= '0' && ch <= '9')
177 return ch - '0';
178 else if (ch >= 'A' && ch <= 'F')
179 return ch - 'A' + 10;
180 else if (ch >= 'a' && ch <= 'f')
181 return ch - 'a' + 10;
182 else
183 return 0;
186 void Set(const char *val) {
187 if (*val == '#') {
188 val++;
190 unsigned int r = ValueOfHex(val[0]) * 16 + ValueOfHex(val[1]);
191 unsigned int g = ValueOfHex(val[2]) * 16 + ValueOfHex(val[3]);
192 unsigned int b = ValueOfHex(val[4]) * 16 + ValueOfHex(val[5]);
193 Set(r, g, b);
196 long AsLong() const {
197 return co;
200 unsigned int GetRed() {
201 return co & 0xff;
204 unsigned int GetGreen() {
205 return (co >> 8) & 0xff;
208 unsigned int GetBlue() {
209 return (co >> 16) & 0xff;
214 * Holds an allocated RGB colour which may be an approximation to the desired colour.
216 class ColourAllocated {
217 long coAllocated;
219 public:
221 ColourAllocated(long lcol=0) {
222 coAllocated = lcol;
225 void Set(long lcol) {
226 coAllocated = lcol;
229 long AsLong() const {
230 return coAllocated;
235 * Colour pairs hold a desired colour and an allocated colour.
237 struct ColourPair {
238 ColourDesired desired;
239 ColourAllocated allocated;
241 ColourPair(ColourDesired desired_=ColourDesired(0,0,0)) {
242 desired = desired_;
243 allocated.Set(desired.AsLong());
245 void Copy() {
246 allocated.Set(desired.AsLong());
250 class Window; // Forward declaration for Palette
253 * Colour palette management.
255 class Palette {
256 int used;
257 int size;
258 ColourPair *entries;
259 #if PLAT_GTK
260 void *allocatedPalette; // GdkColor *
261 int allocatedLen;
262 #endif
263 // Private so Palette objects can not be copied
264 Palette(const Palette &);
265 Palette &operator=(const Palette &);
266 public:
267 #if PLAT_WIN
268 void *hpal;
269 #endif
270 bool allowRealization;
272 Palette();
273 ~Palette();
275 void Release();
278 * This method either adds a colour to the list of wanted colours (want==true)
279 * or retrieves the allocated colour back to the ColourPair.
280 * This is one method to make it easier to keep the code for wanting and retrieving in sync.
282 void WantFind(ColourPair &cp, bool want);
284 void Allocate(Window &w);
288 * Font management.
290 class Font {
291 protected:
292 FontID fid;
293 #if PLAT_WX
294 int ascent;
295 #endif
296 // Private so Font objects can not be copied
297 Font(const Font &);
298 Font &operator=(const Font &);
299 public:
300 Font();
301 virtual ~Font();
303 virtual void Create(const char *faceName, int characterSet, int size,
304 bool bold, bool italic, int extraFontFlag=0);
305 virtual void Release();
307 FontID GetID() { return fid; }
308 // Alias another font - caller guarantees not to Release
309 void SetID(FontID fid_) { fid = fid_; }
310 #if PLAT_WX
311 void SetAscent(int ascent_) { ascent = ascent_; }
312 #endif
313 friend class Surface;
314 friend class SurfaceImpl;
318 * A surface abstracts a place to draw.
320 class Surface {
321 private:
322 // Private so Surface objects can not be copied
323 Surface(const Surface &) {}
324 Surface &operator=(const Surface &) { return *this; }
325 public:
326 Surface() {}
327 virtual ~Surface() {}
328 static Surface *Allocate();
330 virtual void Init(WindowID wid)=0;
331 virtual void Init(SurfaceID sid, WindowID wid)=0;
332 virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid)=0;
334 virtual void Release()=0;
335 virtual bool Initialised()=0;
336 virtual void PenColour(ColourAllocated fore)=0;
337 virtual int LogPixelsY()=0;
338 virtual int DeviceHeightFont(int points)=0;
339 virtual void MoveTo(int x_, int y_)=0;
340 virtual void LineTo(int x_, int y_)=0;
341 virtual void Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back)=0;
342 virtual void RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0;
343 virtual void FillRectangle(PRectangle rc, ColourAllocated back)=0;
344 virtual void FillRectangle(PRectangle rc, Surface &surfacePattern)=0;
345 virtual void RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0;
346 virtual void AlphaRectangle(PRectangle rc, int cornerSize, ColourAllocated fill, int alphaFill,
347 ColourAllocated outline, int alphaOutline, int flags)=0;
348 virtual void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) = 0;
349 virtual void Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0;
350 virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource)=0;
352 virtual void DrawTextNoClip(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back)=0;
353 virtual void DrawTextClipped(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back)=0;
354 virtual void DrawTextTransparent(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore)=0;
355 virtual void MeasureWidths(Font &font_, const char *s, int len, int *positions)=0;
356 virtual int WidthText(Font &font_, const char *s, int len)=0;
357 virtual int WidthChar(Font &font_, char ch)=0;
358 virtual int Ascent(Font &font_)=0;
359 virtual int Descent(Font &font_)=0;
360 virtual int InternalLeading(Font &font_)=0;
361 virtual int ExternalLeading(Font &font_)=0;
362 virtual int Height(Font &font_)=0;
363 virtual int AverageCharWidth(Font &font_)=0;
365 virtual int SetPalette(Palette *pal, bool inBackGround)=0;
366 virtual void SetClip(PRectangle rc)=0;
367 virtual void FlushCachedState()=0;
369 virtual void SetUnicodeMode(bool unicodeMode_)=0;
370 virtual void SetDBCSMode(int codePage)=0;
374 * A simple callback action passing one piece of untyped user data.
376 typedef void (*CallBackAction)(void*);
379 * Class to hide the details of window manipulation.
380 * Does not own the window which will normally have a longer life than this object.
382 class Window {
383 protected:
384 WindowID wid;
385 #if PLAT_MACOSX
386 void *windowRef;
387 void *control;
388 #endif
389 public:
390 Window() : wid(0), cursorLast(cursorInvalid) {
391 #if PLAT_MACOSX
392 windowRef = 0;
393 control = 0;
394 #endif
396 Window(const Window &source) : wid(source.wid), cursorLast(cursorInvalid) {
397 #if PLAT_MACOSX
398 windowRef = 0;
399 control = 0;
400 #endif
402 virtual ~Window();
403 Window &operator=(WindowID wid_) {
404 wid = wid_;
405 return *this;
407 WindowID GetID() const { return wid; }
408 bool Created() const { return wid != 0; }
409 void Destroy();
410 bool HasFocus();
411 PRectangle GetPosition();
412 void SetPosition(PRectangle rc);
413 void SetPositionRelative(PRectangle rc, Window relativeTo);
414 PRectangle GetClientPosition();
415 void Show(bool show=true);
416 void InvalidateAll();
417 void InvalidateRectangle(PRectangle rc);
418 virtual void SetFont(Font &font);
419 enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand };
420 void SetCursor(Cursor curs);
421 void SetTitle(const char *s);
422 PRectangle GetMonitorRect(Point pt);
423 #if PLAT_MACOSX
424 void SetWindow(void *ref) { windowRef = ref; }
425 void SetControl(void *_control) { control = _control; }
426 #endif
427 private:
428 Cursor cursorLast;
432 * Listbox management.
435 class ListBox : public Window {
436 public:
437 ListBox();
438 virtual ~ListBox();
439 static ListBox *Allocate();
441 virtual void SetFont(Font &font)=0;
442 virtual void Create(Window &parent, int ctrlID, Point location, int lineHeight_, bool unicodeMode_)=0;
443 virtual void SetAverageCharWidth(int width)=0;
444 virtual void SetVisibleRows(int rows)=0;
445 virtual int GetVisibleRows() const=0;
446 virtual PRectangle GetDesiredRect()=0;
447 virtual int CaretFromEdge()=0;
448 virtual void Clear()=0;
449 virtual void Append(char *s, int type = -1)=0;
450 virtual int Length()=0;
451 virtual void Select(int n)=0;
452 virtual int GetSelection()=0;
453 virtual int Find(const char *prefix)=0;
454 virtual void GetValue(int n, char *value, int len)=0;
455 virtual void RegisterImage(int type, const char *xpm_data)=0;
456 virtual void RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage) = 0;
457 virtual void ClearRegisteredImages()=0;
458 virtual void SetDoubleClickAction(CallBackAction, void *)=0;
459 virtual void SetList(const char* list, char separator, char typesep)=0;
463 * Menu management.
465 class Menu {
466 MenuID mid;
467 public:
468 Menu();
469 MenuID GetID() { return mid; }
470 void CreatePopUp();
471 void Destroy();
472 void Show(Point pt, Window &w);
475 class ElapsedTime {
476 long bigBit;
477 long littleBit;
478 public:
479 ElapsedTime();
480 double Duration(bool reset=false);
484 * Dynamic Library (DLL/SO/...) loading
486 class DynamicLibrary {
487 public:
488 virtual ~DynamicLibrary() {}
490 /// @return Pointer to function "name", or NULL on failure.
491 virtual Function FindFunction(const char *name) = 0;
493 /// @return true if the library was loaded successfully.
494 virtual bool IsValid() = 0;
496 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
497 static DynamicLibrary *Load(const char *modulePath);
501 * Platform class used to retrieve system wide parameters such as double click speed
502 * and chrome colour. Not a creatable object, more of a module with several functions.
504 class Platform {
505 // Private so Platform objects can not be copied
506 Platform(const Platform &) {}
507 Platform &operator=(const Platform &) { return *this; }
508 public:
509 // Should be private because no new Platforms are ever created
510 // but gcc warns about this
511 Platform() {}
512 ~Platform() {}
513 static ColourDesired Chrome();
514 static ColourDesired ChromeHighlight();
515 static const char *DefaultFont();
516 static int DefaultFontSize();
517 static unsigned int DoubleClickTime();
518 static bool MouseButtonBounce();
519 static void DebugDisplay(const char *s);
520 static bool IsKeyDown(int key);
521 static long SendScintilla(
522 WindowID w, unsigned int msg, unsigned long wParam=0, long lParam=0);
523 static long SendScintillaPointer(
524 WindowID w, unsigned int msg, unsigned long wParam=0, void *lParam=0);
525 static bool IsDBCSLeadByte(int codePage, char ch);
526 static int DBCSCharLength(int codePage, const char *s);
527 static int DBCSCharMaxLength();
529 // These are utility functions not really tied to a platform
530 static int Minimum(int a, int b);
531 static int Maximum(int a, int b);
532 // Next three assume 16 bit shorts and 32 bit longs
533 static long LongFromTwoShorts(short a,short b) {
534 return (a) | ((b) << 16);
536 static short HighShortFromLong(long x) {
537 return static_cast<short>(x >> 16);
539 static short LowShortFromLong(long x) {
540 return static_cast<short>(x & 0xffff);
542 static void DebugPrintf(const char *format, ...);
543 static bool ShowAssertionPopUps(bool assertionPopUps_);
544 static void Assert(const char *c, const char *file, int line);
545 static int Clamp(int val, int minVal, int maxVal);
548 #ifdef NDEBUG
549 #define PLATFORM_ASSERT(c) ((void)0)
550 #else
551 #ifdef SCI_NAMESPACE
552 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))
553 #else
554 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
555 #endif
556 #endif
558 #ifdef SCI_NAMESPACE
560 #endif
562 // Shut up annoying Visual C++ warnings:
563 #ifdef _MSC_VER
564 #pragma warning(disable: 4244 4309 4514 4710)
565 #endif
567 #endif