upgraded to scintilla 3.2.0
[TortoiseGit.git] / ext / scintilla / include / Platform.h
blob0f9fb00709efdfcf8140f921e0e664c23727edc9
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
26 #if defined(FOX)
27 #undef PLAT_FOX
28 #define PLAT_FOX 1
30 #elif defined(__WX__)
31 #undef PLAT_WX
32 #define PLAT_WX 1
34 #elif defined(GTK)
35 #undef PLAT_GTK
36 #define PLAT_GTK 1
38 #elif defined(SCINTILLA_QT)
39 #undef PLAT_QT
40 #define PLAT_QT 1
42 #if defined(__WIN32__) || defined(_MSC_VER)
43 #undef PLAT_GTK_WIN32
44 #define PLAT_GTK_WIN32 1
45 #endif
47 #if defined(__APPLE__)
48 #undef PLAT_GTK_MACOSX
49 #define PLAT_GTK_MACOSX 1
50 #endif
52 #elif defined(__APPLE__)
54 #undef PLAT_MACOSX
55 #define PLAT_MACOSX 1
57 #else
58 #undef PLAT_WIN
59 #define PLAT_WIN 1
61 #endif
63 #ifdef SCI_NAMESPACE
64 namespace Scintilla {
65 #endif
67 typedef float XYPOSITION;
68 typedef double XYACCUMULATOR;
69 //#define XYPOSITION int
71 // Underlying the implementation of the platform classes are platform specific types.
72 // Sometimes these need to be passed around by client code so they are defined here
74 typedef void *FontID;
75 typedef void *SurfaceID;
76 typedef void *WindowID;
77 typedef void *MenuID;
78 typedef void *TickerID;
79 typedef void *Function;
80 typedef void *IdlerID;
82 /**
83 * A geometric point class.
84 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
86 class Point {
87 public:
88 XYPOSITION x;
89 XYPOSITION y;
91 explicit Point(XYPOSITION x_=0, XYPOSITION y_=0) : x(x_), y(y_) {
94 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
96 static Point FromLong(long lpoint);
99 /**
100 * A geometric rectangle class.
101 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
102 * PRectangles contain their top and left sides, but not their right and bottom sides.
104 class PRectangle {
105 public:
106 XYPOSITION left;
107 XYPOSITION top;
108 XYPOSITION right;
109 XYPOSITION bottom;
111 PRectangle(XYPOSITION left_=0, XYPOSITION top_=0, XYPOSITION right_=0, XYPOSITION bottom_ = 0) :
112 left(left_), top(top_), right(right_), bottom(bottom_) {
115 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
117 bool operator==(PRectangle &rc) {
118 return (rc.left == left) && (rc.right == right) &&
119 (rc.top == top) && (rc.bottom == bottom);
121 bool Contains(Point pt) {
122 return (pt.x >= left) && (pt.x <= right) &&
123 (pt.y >= top) && (pt.y <= bottom);
125 bool Contains(PRectangle rc) {
126 return (rc.left >= left) && (rc.right <= right) &&
127 (rc.top >= top) && (rc.bottom <= bottom);
129 bool Intersects(PRectangle other) {
130 return (right > other.left) && (left < other.right) &&
131 (bottom > other.top) && (top < other.bottom);
133 void Move(XYPOSITION xDelta, XYPOSITION yDelta) {
134 left += xDelta;
135 top += yDelta;
136 right += xDelta;
137 bottom += yDelta;
139 XYPOSITION Width() { return right - left; }
140 XYPOSITION Height() { return bottom - top; }
141 bool Empty() {
142 return (Height() <= 0) || (Width() <= 0);
147 * Holds a desired RGB colour.
149 class ColourDesired {
150 long co;
151 public:
152 ColourDesired(long lcol=0) {
153 co = lcol;
156 ColourDesired(unsigned int red, unsigned int green, unsigned int blue) {
157 Set(red, green, blue);
160 bool operator==(const ColourDesired &other) const {
161 return co == other.co;
164 void Set(long lcol) {
165 co = lcol;
168 void Set(unsigned int red, unsigned int green, unsigned int blue) {
169 co = red | (green << 8) | (blue << 16);
172 static inline unsigned int ValueOfHex(const char ch) {
173 if (ch >= '0' && ch <= '9')
174 return ch - '0';
175 else if (ch >= 'A' && ch <= 'F')
176 return ch - 'A' + 10;
177 else if (ch >= 'a' && ch <= 'f')
178 return ch - 'a' + 10;
179 else
180 return 0;
183 void Set(const char *val) {
184 if (*val == '#') {
185 val++;
187 unsigned int r = ValueOfHex(val[0]) * 16 + ValueOfHex(val[1]);
188 unsigned int g = ValueOfHex(val[2]) * 16 + ValueOfHex(val[3]);
189 unsigned int b = ValueOfHex(val[4]) * 16 + ValueOfHex(val[5]);
190 Set(r, g, b);
193 long AsLong() const {
194 return co;
197 unsigned int GetRed() {
198 return co & 0xff;
201 unsigned int GetGreen() {
202 return (co >> 8) & 0xff;
205 unsigned int GetBlue() {
206 return (co >> 16) & 0xff;
211 * Font management.
214 struct FontParameters {
215 const char *faceName;
216 float size;
217 int weight;
218 bool italic;
219 int extraFontFlag;
220 int technology;
221 int characterSet;
223 FontParameters(
224 const char *faceName_,
225 float size_=10,
226 int weight_=400,
227 bool italic_=false,
228 int extraFontFlag_=0,
229 int technology_=0,
230 int characterSet_=0) :
232 faceName(faceName_),
233 size(size_),
234 weight(weight_),
235 italic(italic_),
236 extraFontFlag(extraFontFlag_),
237 technology(technology_),
238 characterSet(characterSet_)
244 class Font {
245 protected:
246 FontID fid;
247 #if PLAT_WX
248 int ascent;
249 #endif
250 // Private so Font objects can not be copied
251 Font(const Font &);
252 Font &operator=(const Font &);
253 public:
254 Font();
255 virtual ~Font();
257 virtual void Create(const FontParameters &fp);
258 virtual void Release();
260 FontID GetID() { return fid; }
261 // Alias another font - caller guarantees not to Release
262 void SetID(FontID fid_) { fid = fid_; }
263 #if PLAT_WX
264 void SetAscent(int ascent_) { ascent = ascent_; }
265 #endif
266 friend class Surface;
267 friend class SurfaceImpl;
271 * A surface abstracts a place to draw.
273 class Surface {
274 private:
275 // Private so Surface objects can not be copied
276 Surface(const Surface &) {}
277 Surface &operator=(const Surface &) { return *this; }
278 public:
279 Surface() {}
280 virtual ~Surface() {}
281 static Surface *Allocate(int technology);
283 virtual void Init(WindowID wid)=0;
284 virtual void Init(SurfaceID sid, WindowID wid)=0;
285 virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid)=0;
287 virtual void Release()=0;
288 virtual bool Initialised()=0;
289 virtual void PenColour(ColourDesired fore)=0;
290 virtual int LogPixelsY()=0;
291 virtual int DeviceHeightFont(int points)=0;
292 virtual void MoveTo(int x_, int y_)=0;
293 virtual void LineTo(int x_, int y_)=0;
294 virtual void Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back)=0;
295 virtual void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
296 virtual void FillRectangle(PRectangle rc, ColourDesired back)=0;
297 virtual void FillRectangle(PRectangle rc, Surface &surfacePattern)=0;
298 virtual void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
299 virtual void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
300 ColourDesired outline, int alphaOutline, int flags)=0;
301 virtual void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) = 0;
302 virtual void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
303 virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource)=0;
305 virtual void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
306 virtual void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
307 virtual void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore)=0;
308 virtual void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions)=0;
309 virtual XYPOSITION WidthText(Font &font_, const char *s, int len)=0;
310 virtual XYPOSITION WidthChar(Font &font_, char ch)=0;
311 virtual XYPOSITION Ascent(Font &font_)=0;
312 virtual XYPOSITION Descent(Font &font_)=0;
313 virtual XYPOSITION InternalLeading(Font &font_)=0;
314 virtual XYPOSITION ExternalLeading(Font &font_)=0;
315 virtual XYPOSITION Height(Font &font_)=0;
316 virtual XYPOSITION AverageCharWidth(Font &font_)=0;
318 virtual void SetClip(PRectangle rc)=0;
319 virtual void FlushCachedState()=0;
321 virtual void SetUnicodeMode(bool unicodeMode_)=0;
322 virtual void SetDBCSMode(int codePage)=0;
326 * A simple callback action passing one piece of untyped user data.
328 typedef void (*CallBackAction)(void*);
331 * Class to hide the details of window manipulation.
332 * Does not own the window which will normally have a longer life than this object.
334 class Window {
335 protected:
336 WindowID wid;
337 #if PLAT_MACOSX
338 void *windowRef;
339 void *control;
340 #endif
341 public:
342 Window() : wid(0), cursorLast(cursorInvalid) {
343 #if PLAT_MACOSX
344 windowRef = 0;
345 control = 0;
346 #endif
348 Window(const Window &source) : wid(source.wid), cursorLast(cursorInvalid) {
349 #if PLAT_MACOSX
350 windowRef = 0;
351 control = 0;
352 #endif
354 virtual ~Window();
355 Window &operator=(WindowID wid_) {
356 wid = wid_;
357 return *this;
359 WindowID GetID() const { return wid; }
360 bool Created() const { return wid != 0; }
361 void Destroy();
362 bool HasFocus();
363 PRectangle GetPosition();
364 void SetPosition(PRectangle rc);
365 void SetPositionRelative(PRectangle rc, Window relativeTo);
366 PRectangle GetClientPosition();
367 void Show(bool show=true);
368 void InvalidateAll();
369 void InvalidateRectangle(PRectangle rc);
370 virtual void SetFont(Font &font);
371 enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand };
372 void SetCursor(Cursor curs);
373 void SetTitle(const char *s);
374 PRectangle GetMonitorRect(Point pt);
375 #if PLAT_MACOSX
376 void SetWindow(void *ref) { windowRef = ref; }
377 void SetControl(void *_control) { control = _control; }
378 #endif
379 private:
380 Cursor cursorLast;
384 * Listbox management.
387 class ListBox : public Window {
388 public:
389 ListBox();
390 virtual ~ListBox();
391 static ListBox *Allocate();
393 virtual void SetFont(Font &font)=0;
394 virtual void Create(Window &parent, int ctrlID, Point location, int lineHeight_, bool unicodeMode_, int technology_)=0;
395 virtual void SetAverageCharWidth(int width)=0;
396 virtual void SetVisibleRows(int rows)=0;
397 virtual int GetVisibleRows() const=0;
398 virtual PRectangle GetDesiredRect()=0;
399 virtual int CaretFromEdge()=0;
400 virtual void Clear()=0;
401 virtual void Append(char *s, int type = -1)=0;
402 virtual int Length()=0;
403 virtual void Select(int n)=0;
404 virtual int GetSelection()=0;
405 virtual int Find(const char *prefix)=0;
406 virtual void GetValue(int n, char *value, int len)=0;
407 virtual void RegisterImage(int type, const char *xpm_data)=0;
408 virtual void RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage) = 0;
409 virtual void ClearRegisteredImages()=0;
410 virtual void SetDoubleClickAction(CallBackAction, void *)=0;
411 virtual void SetList(const char* list, char separator, char typesep)=0;
415 * Menu management.
417 class Menu {
418 MenuID mid;
419 public:
420 Menu();
421 MenuID GetID() { return mid; }
422 void CreatePopUp();
423 void Destroy();
424 void Show(Point pt, Window &w);
427 class ElapsedTime {
428 long bigBit;
429 long littleBit;
430 public:
431 ElapsedTime();
432 double Duration(bool reset=false);
436 * Dynamic Library (DLL/SO/...) loading
438 class DynamicLibrary {
439 public:
440 virtual ~DynamicLibrary() {}
442 /// @return Pointer to function "name", or NULL on failure.
443 virtual Function FindFunction(const char *name) = 0;
445 /// @return true if the library was loaded successfully.
446 virtual bool IsValid() = 0;
448 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
449 static DynamicLibrary *Load(const char *modulePath);
453 * Platform class used to retrieve system wide parameters such as double click speed
454 * and chrome colour. Not a creatable object, more of a module with several functions.
456 class Platform {
457 // Private so Platform objects can not be copied
458 Platform(const Platform &) {}
459 Platform &operator=(const Platform &) { return *this; }
460 public:
461 // Should be private because no new Platforms are ever created
462 // but gcc warns about this
463 Platform() {}
464 ~Platform() {}
465 static ColourDesired Chrome();
466 static ColourDesired ChromeHighlight();
467 static const char *DefaultFont();
468 static int DefaultFontSize();
469 static unsigned int DoubleClickTime();
470 static bool MouseButtonBounce();
471 static void DebugDisplay(const char *s);
472 static bool IsKeyDown(int key);
473 static long SendScintilla(
474 WindowID w, unsigned int msg, unsigned long wParam=0, long lParam=0);
475 static long SendScintillaPointer(
476 WindowID w, unsigned int msg, unsigned long wParam=0, void *lParam=0);
477 static bool IsDBCSLeadByte(int codePage, char ch);
478 static int DBCSCharLength(int codePage, const char *s);
479 static int DBCSCharMaxLength();
481 // These are utility functions not really tied to a platform
482 static int Minimum(int a, int b);
483 static int Maximum(int a, int b);
484 // Next three assume 16 bit shorts and 32 bit longs
485 static long LongFromTwoShorts(short a,short b) {
486 return (a) | ((b) << 16);
488 static short HighShortFromLong(long x) {
489 return static_cast<short>(x >> 16);
491 static short LowShortFromLong(long x) {
492 return static_cast<short>(x & 0xffff);
494 static void DebugPrintf(const char *format, ...);
495 static bool ShowAssertionPopUps(bool assertionPopUps_);
496 static void Assert(const char *c, const char *file, int line);
497 static int Clamp(int val, int minVal, int maxVal);
500 #ifdef NDEBUG
501 #define PLATFORM_ASSERT(c) ((void)0)
502 #else
503 #ifdef SCI_NAMESPACE
504 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))
505 #else
506 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
507 #endif
508 #endif
510 #ifdef SCI_NAMESPACE
512 #endif
514 // Shut up annoying Visual C++ warnings:
515 #ifdef _MSC_VER
516 #pragma warning(disable: 4244 4309 4514 4710)
517 #endif
519 #if defined(__GNUC__) && defined(SCINTILLA_QT)
520 #pragma GCC diagnostic ignored "-Wmissing-braces"
521 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
522 #pragma GCC diagnostic ignored "-Wchar-subscripts"
523 #endif
525 #endif