Fixed issue #2175: TortoiseGitBlame fails to search if line has non-ascii chars and...
[TortoiseGit.git] / ext / scintilla / include / Platform.h
blobdf7728df26959454e6dc1a7014d17e98089853f7
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 //#define XYPOSITION int
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 exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
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 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
107 static Point FromLong(long lpoint);
111 * A geometric rectangle class.
112 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
113 * PRectangles contain their top and left sides, but not their right and bottom sides.
115 class PRectangle {
116 public:
117 XYPOSITION left;
118 XYPOSITION top;
119 XYPOSITION right;
120 XYPOSITION bottom;
122 PRectangle(XYPOSITION left_=0, XYPOSITION top_=0, XYPOSITION right_=0, XYPOSITION bottom_ = 0) :
123 left(left_), top(top_), right(right_), bottom(bottom_) {
126 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
128 bool operator==(PRectangle &rc) const {
129 return (rc.left == left) && (rc.right == right) &&
130 (rc.top == top) && (rc.bottom == bottom);
132 bool Contains(Point pt) const {
133 return (pt.x >= left) && (pt.x <= right) &&
134 (pt.y >= top) && (pt.y <= bottom);
136 bool Contains(PRectangle rc) const {
137 return (rc.left >= left) && (rc.right <= right) &&
138 (rc.top >= top) && (rc.bottom <= bottom);
140 bool Intersects(PRectangle other) const {
141 return (right > other.left) && (left < other.right) &&
142 (bottom > other.top) && (top < other.bottom);
144 void Move(XYPOSITION xDelta, XYPOSITION yDelta) {
145 left += xDelta;
146 top += yDelta;
147 right += xDelta;
148 bottom += yDelta;
150 XYPOSITION Width() const { return right - left; }
151 XYPOSITION Height() const { return bottom - top; }
152 bool Empty() const {
153 return (Height() <= 0) || (Width() <= 0);
158 * Holds a desired RGB colour.
160 class ColourDesired {
161 long co;
162 public:
163 ColourDesired(long lcol=0) {
164 co = lcol;
167 ColourDesired(unsigned int red, unsigned int green, unsigned int blue) {
168 Set(red, green, blue);
171 bool operator==(const ColourDesired &other) const {
172 return co == other.co;
175 void Set(long lcol) {
176 co = lcol;
179 void Set(unsigned int red, unsigned int green, unsigned int blue) {
180 co = red | (green << 8) | (blue << 16);
183 static inline unsigned int ValueOfHex(const char ch) {
184 if (ch >= '0' && ch <= '9')
185 return ch - '0';
186 else if (ch >= 'A' && ch <= 'F')
187 return ch - 'A' + 10;
188 else if (ch >= 'a' && ch <= 'f')
189 return ch - 'a' + 10;
190 else
191 return 0;
194 void Set(const char *val) {
195 if (*val == '#') {
196 val++;
198 unsigned int r = ValueOfHex(val[0]) * 16 + ValueOfHex(val[1]);
199 unsigned int g = ValueOfHex(val[2]) * 16 + ValueOfHex(val[3]);
200 unsigned int b = ValueOfHex(val[4]) * 16 + ValueOfHex(val[5]);
201 Set(r, g, b);
204 long AsLong() const {
205 return co;
208 unsigned int GetRed() const {
209 return co & 0xff;
212 unsigned int GetGreen() const {
213 return (co >> 8) & 0xff;
216 unsigned int GetBlue() const {
217 return (co >> 16) & 0xff;
222 * Font management.
225 struct FontParameters {
226 const char *faceName;
227 float size;
228 int weight;
229 bool italic;
230 int extraFontFlag;
231 int technology;
232 int characterSet;
234 FontParameters(
235 const char *faceName_,
236 float size_=10,
237 int weight_=400,
238 bool italic_=false,
239 int extraFontFlag_=0,
240 int technology_=0,
241 int characterSet_=0) :
243 faceName(faceName_),
244 size(size_),
245 weight(weight_),
246 italic(italic_),
247 extraFontFlag(extraFontFlag_),
248 technology(technology_),
249 characterSet(characterSet_)
255 class Font {
256 protected:
257 FontID fid;
258 #if PLAT_WX
259 int ascent;
260 #endif
261 // Private so Font objects can not be copied
262 Font(const Font &);
263 Font &operator=(const Font &);
264 public:
265 Font();
266 virtual ~Font();
268 virtual void Create(const FontParameters &fp);
269 virtual void Release();
271 FontID GetID() { return fid; }
272 // Alias another font - caller guarantees not to Release
273 void SetID(FontID fid_) { fid = fid_; }
274 #if PLAT_WX
275 void SetAscent(int ascent_) { ascent = ascent_; }
276 #endif
277 friend class Surface;
278 friend class SurfaceImpl;
282 * A surface abstracts a place to draw.
284 class Surface {
285 private:
286 // Private so Surface objects can not be copied
287 Surface(const Surface &) {}
288 Surface &operator=(const Surface &) { return *this; }
289 public:
290 Surface() {}
291 virtual ~Surface() {}
292 static Surface *Allocate(int technology);
294 virtual void Init(WindowID wid)=0;
295 virtual void Init(SurfaceID sid, WindowID wid)=0;
296 virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid)=0;
298 virtual void Release()=0;
299 virtual bool Initialised()=0;
300 virtual void PenColour(ColourDesired fore)=0;
301 virtual int LogPixelsY()=0;
302 virtual int DeviceHeightFont(int points)=0;
303 virtual void MoveTo(int x_, int y_)=0;
304 virtual void LineTo(int x_, int y_)=0;
305 virtual void Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back)=0;
306 virtual void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
307 virtual void FillRectangle(PRectangle rc, ColourDesired back)=0;
308 virtual void FillRectangle(PRectangle rc, Surface &surfacePattern)=0;
309 virtual void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
310 virtual void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
311 ColourDesired outline, int alphaOutline, int flags)=0;
312 virtual void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) = 0;
313 virtual void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
314 virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource)=0;
316 virtual void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
317 virtual void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
318 virtual void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore)=0;
319 virtual void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions)=0;
320 virtual XYPOSITION WidthText(Font &font_, const char *s, int len)=0;
321 virtual XYPOSITION WidthChar(Font &font_, char ch)=0;
322 virtual XYPOSITION Ascent(Font &font_)=0;
323 virtual XYPOSITION Descent(Font &font_)=0;
324 virtual XYPOSITION InternalLeading(Font &font_)=0;
325 virtual XYPOSITION ExternalLeading(Font &font_)=0;
326 virtual XYPOSITION Height(Font &font_)=0;
327 virtual XYPOSITION AverageCharWidth(Font &font_)=0;
329 virtual void SetClip(PRectangle rc)=0;
330 virtual void FlushCachedState()=0;
332 virtual void SetUnicodeMode(bool unicodeMode_)=0;
333 virtual void SetDBCSMode(int codePage)=0;
337 * A simple callback action passing one piece of untyped user data.
339 typedef void (*CallBackAction)(void*);
342 * Class to hide the details of window manipulation.
343 * Does not own the window which will normally have a longer life than this object.
345 class Window {
346 protected:
347 WindowID wid;
348 public:
349 Window() : wid(0), cursorLast(cursorInvalid) {
351 Window(const Window &source) : wid(source.wid), cursorLast(cursorInvalid) {
353 virtual ~Window();
354 Window &operator=(WindowID wid_) {
355 wid = wid_;
356 return *this;
358 WindowID GetID() const { return wid; }
359 bool Created() const { return wid != 0; }
360 void Destroy();
361 bool HasFocus();
362 PRectangle GetPosition();
363 void SetPosition(PRectangle rc);
364 void SetPositionRelative(PRectangle rc, Window relativeTo);
365 PRectangle GetClientPosition();
366 void Show(bool show=true);
367 void InvalidateAll();
368 void InvalidateRectangle(PRectangle rc);
369 virtual void SetFont(Font &font);
370 enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand };
371 void SetCursor(Cursor curs);
372 void SetTitle(const char *s);
373 PRectangle GetMonitorRect(Point pt);
374 private:
375 Cursor cursorLast;
379 * Listbox management.
382 class ListBox : public Window {
383 public:
384 ListBox();
385 virtual ~ListBox();
386 static ListBox *Allocate();
388 virtual void SetFont(Font &font)=0;
389 virtual void Create(Window &parent, int ctrlID, Point location, int lineHeight_, bool unicodeMode_, int technology_)=0;
390 virtual void SetAverageCharWidth(int width)=0;
391 virtual void SetVisibleRows(int rows)=0;
392 virtual int GetVisibleRows() const=0;
393 virtual PRectangle GetDesiredRect()=0;
394 virtual int CaretFromEdge()=0;
395 virtual void Clear()=0;
396 virtual void Append(char *s, int type = -1)=0;
397 virtual int Length()=0;
398 virtual void Select(int n)=0;
399 virtual int GetSelection()=0;
400 virtual int Find(const char *prefix)=0;
401 virtual void GetValue(int n, char *value, int len)=0;
402 virtual void RegisterImage(int type, const char *xpm_data)=0;
403 virtual void RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage) = 0;
404 virtual void ClearRegisteredImages()=0;
405 virtual void SetDoubleClickAction(CallBackAction, void *)=0;
406 virtual void SetList(const char* list, char separator, char typesep)=0;
410 * Menu management.
412 class Menu {
413 MenuID mid;
414 public:
415 Menu();
416 MenuID GetID() { return mid; }
417 void CreatePopUp();
418 void Destroy();
419 void Show(Point pt, Window &w);
422 class ElapsedTime {
423 long bigBit;
424 long littleBit;
425 public:
426 ElapsedTime();
427 double Duration(bool reset=false);
431 * Dynamic Library (DLL/SO/...) loading
433 class DynamicLibrary {
434 public:
435 virtual ~DynamicLibrary() {}
437 /// @return Pointer to function "name", or NULL on failure.
438 virtual Function FindFunction(const char *name) = 0;
440 /// @return true if the library was loaded successfully.
441 virtual bool IsValid() = 0;
443 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
444 static DynamicLibrary *Load(const char *modulePath);
447 #if defined(__clang__)
448 # if __has_feature(attribute_analyzer_noreturn)
449 # define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
450 # else
451 # define CLANG_ANALYZER_NORETURN
452 # endif
453 #else
454 # define CLANG_ANALYZER_NORETURN
455 #endif
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) CLANG_ANALYZER_NORETURN;
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-field-initializers"
526 #endif
528 #endif