]> git.saurik.com Git - wxWidgets.git/blame - contrib/src/stc/scintilla/include/Platform.h
use #ifdef and not #if (fixes -Wundef warnings reported in bug 1273111)
[wxWidgets.git] / contrib / src / stc / scintilla / include / Platform.h
CommitLineData
9ce192d4 1// Scintilla source code edit control
65ec6247
RD
2/** @file Platform.h
3 ** Interface to platform facilities. Also includes some basic utilities.
be5a51fb 4 ** Implemented in PlatGTK.cxx for GTK+/Linux, PlatWin.cxx for Windows, and PlatWX.cxx for wxWidgets.
65ec6247 5 **/
9e730a78 6// Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
9ce192d4
RD
7// The License.txt file describes the conditions under which this software may be distributed.
8
9#ifndef PLATFORM_H
10#define PLATFORM_H
11
65ec6247
RD
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
be5a51fb 15// PLAT_WX is wxWidgets on any supported platform
9ce192d4
RD
16
17#define PLAT_GTK 0
65ec6247 18#define PLAT_GTK_WIN32 0
9ce192d4
RD
19#define PLAT_WIN 0
20#define PLAT_WX 0
1a2fb4cd 21#define PLAT_FOX 0
9ce192d4 22
1a2fb4cd
RD
23#if defined(FOX)
24#undef PLAT_FOX
25#define PLAT_FOX 1
26
27#elif defined(__WX__)
9ce192d4
RD
28#undef PLAT_WX
29#define PLAT_WX 1
30
31#elif defined(GTK)
32#undef PLAT_GTK
33#define PLAT_GTK 1
34
65ec6247
RD
35#ifdef _MSC_VER
36#undef PLAT_GTK_WIN32
37#define PLAT_GTK_WIN32 1
38#endif
39
9ce192d4
RD
40#else
41#undef PLAT_WIN
42#define PLAT_WIN 1
43
44#endif
45
46
9ce192d4
RD
47// Underlying the implementation of the platform classes are platform specific types.
48// Sometimes these need to be passed around by client code so they are defined here
49
1a2fb4cd
RD
50typedef void *FontID;
51typedef void *SurfaceID;
52typedef void *WindowID;
53typedef void *MenuID;
54typedef void *TickerID;
e14d10b0 55typedef void *Function;
8e54aaed 56typedef void *IdlerID;
9ce192d4 57
65ec6247
RD
58/**
59 * A geometric point class.
60 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
61 */
9ce192d4
RD
62class Point {
63public:
64 int x;
65 int y;
65ec6247 66
a33203cb 67 explicit Point(int x_=0, int y_=0) : x(x_), y(y_) {
9ce192d4
RD
68 }
69
70 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
65ec6247 71
9ce192d4
RD
72 static Point FromLong(long lpoint);
73};
74
65ec6247
RD
75/**
76 * A geometric rectangle class.
77 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
78 * PRectangles contain their top and left sides, but not their right and bottom sides.
79 */
9ce192d4
RD
80class PRectangle {
81public:
82 int left;
83 int top;
84 int right;
85 int bottom;
86
87 PRectangle(int left_=0, int top_=0, int right_=0, int bottom_ = 0) :
88 left(left_), top(top_), right(right_), bottom(bottom_) {
89 }
90
91 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
92
65ec6247
RD
93 bool operator==(PRectangle &rc) {
94 return (rc.left == left) && (rc.right == right) &&
95 (rc.top == top) && (rc.bottom == bottom);
96 }
9ce192d4
RD
97 bool Contains(Point pt) {
98 return (pt.x >= left) && (pt.x <= right) &&
99 (pt.y >= top) && (pt.y <= bottom);
100 }
101 bool Contains(PRectangle rc) {
102 return (rc.left >= left) && (rc.right <= right) &&
103 (rc.top >= top) && (rc.bottom <= bottom);
104 }
105 bool Intersects(PRectangle other) {
a834585d
RD
106 return (right > other.left) && (left < other.right) &&
107 (bottom > other.top) && (top < other.bottom);
9ce192d4 108 }
9e730a78
RD
109 void Move(int xDelta, int yDelta) {
110 left += xDelta;
111 top += yDelta;
112 right += xDelta;
113 bottom += yDelta;
114 }
9ce192d4
RD
115 int Width() { return right - left; }
116 int Height() { return bottom - top; }
117};
118
1a2fb4cd
RD
119/**
120 * In some circumstances, including Win32 in paletted mode and GTK+, each colour
121 * must be allocated before use. The desired colours are held in the ColourDesired class,
122 * and after allocation the allocation entry is stored in the ColourAllocated class. In other
123 * circumstances, such as Win32 in true colour mode, the allocation process just copies
124 * the RGB values from the desired to the allocated class.
125 * As each desired colour requires allocation before it can be used, the ColourPair class
126 * holds both a ColourDesired and a ColourAllocated
127 * The Palette class is responsible for managing the palette of colours which contains a
128 * list of ColourPair objects and performs the allocation.
129 */
9ce192d4 130
65ec6247 131/**
1a2fb4cd 132 * Holds a desired RGB colour.
65ec6247 133 */
1a2fb4cd
RD
134class ColourDesired {
135 long co;
9ce192d4 136public:
1a2fb4cd
RD
137 ColourDesired(long lcol=0) {
138 co = lcol;
139 }
65ec6247 140
1a2fb4cd 141 ColourDesired(unsigned int red, unsigned int green, unsigned int blue) {
9e730a78 142 Set(red, green, blue);
1a2fb4cd
RD
143 }
144
145 bool operator==(const ColourDesired &other) const {
146 return co == other.co;
147 }
148
149 void Set(long lcol) {
150 co = lcol;
151 }
152
9e730a78
RD
153 void Set(unsigned int red, unsigned int green, unsigned int blue) {
154 co = red | (green << 8) | (blue << 16);
155 }
156
157 static inline unsigned int ValueOfHex(const char ch) {
158 if (ch >= '0' && ch <= '9')
159 return ch - '0';
160 else if (ch >= 'A' && ch <= 'F')
161 return ch - 'A' + 10;
162 else if (ch >= 'a' && ch <= 'f')
163 return ch - 'a' + 10;
164 else
165 return 0;
166 }
167
168 void Set(const char *val) {
169 if (*val == '#') {
170 val++;
171 }
172 unsigned int r = ValueOfHex(val[0]) * 16 + ValueOfHex(val[1]);
173 unsigned int g = ValueOfHex(val[2]) * 16 + ValueOfHex(val[3]);
174 unsigned int b = ValueOfHex(val[4]) * 16 + ValueOfHex(val[5]);
175 Set(r, g, b);
176 }
177
1a2fb4cd
RD
178 long AsLong() const {
179 return co;
180 }
181
182 unsigned int GetRed() {
183 return co & 0xff;
184 }
185
186 unsigned int GetGreen() {
187 return (co >> 8) & 0xff;
188 }
189
190 unsigned int GetBlue() {
191 return (co >> 16) & 0xff;
192 }
9ce192d4
RD
193};
194
65ec6247 195/**
1a2fb4cd
RD
196 * Holds an allocated RGB colour which may be an approximation to the desired colour.
197 */
198class ColourAllocated {
199 long coAllocated;
200
201public:
202
203 ColourAllocated(long lcol=0) {
204 coAllocated = lcol;
205 }
206
207 void Set(long lcol) {
208 coAllocated = lcol;
209 }
210
211 long AsLong() const {
212 return coAllocated;
213 }
214};
215
216/**
217 * Colour pairs hold a desired colour and an allocated colour.
65ec6247 218 */
9ce192d4 219struct ColourPair {
1a2fb4cd
RD
220 ColourDesired desired;
221 ColourAllocated allocated;
9ce192d4 222
1a2fb4cd 223 ColourPair(ColourDesired desired_=ColourDesired(0,0,0)) {
9ce192d4 224 desired = desired_;
1a2fb4cd 225 allocated.Set(desired.AsLong());
9ce192d4 226 }
9e730a78
RD
227 void Copy() {
228 allocated.Set(desired.AsLong());
229 }
9ce192d4
RD
230};
231
232class Window; // Forward declaration for Palette
233
65ec6247
RD
234/**
235 * Colour palette management.
236 */
9ce192d4
RD
237class Palette {
238 int used;
239 enum {numEntries = 100};
240 ColourPair entries[numEntries];
241#if PLAT_GTK
1a2fb4cd 242 void *allocatedPalette; // GdkColor *
9ce192d4 243 int allocatedLen;
9ce192d4
RD
244#endif
245public:
1a2fb4cd
RD
246#if PLAT_WIN
247 void *hpal;
248#endif
9ce192d4 249 bool allowRealization;
65ec6247 250
9ce192d4
RD
251 Palette();
252 ~Palette();
253
254 void Release();
65ec6247
RD
255
256 /**
257 * This method either adds a colour to the list of wanted colours (want==true)
258 * or retrieves the allocated colour back to the ColourPair.
259 * This is one method to make it easier to keep the code for wanting and retrieving in sync.
260 */
9ce192d4
RD
261 void WantFind(ColourPair &cp, bool want);
262
263 void Allocate(Window &w);
9ce192d4
RD
264};
265
65ec6247
RD
266/**
267 * Font management.
268 */
9ce192d4 269class Font {
d134f170 270protected:
9ce192d4
RD
271 FontID id;
272#if PLAT_WX
273 int ascent;
274#endif
275 // Private so Font objects can not be copied
276 Font(const Font &) {}
277 Font &operator=(const Font &) { id=0; return *this; }
278public:
279 Font();
d134f170 280 virtual ~Font();
9ce192d4 281
591d01be
RD
282 virtual void Create(const char *faceName, int characterSet, int size,
283 bool bold, bool italic, bool extraFontFlag=false);
d134f170 284 virtual void Release();
9ce192d4
RD
285
286 FontID GetID() { return id; }
f6bcfd97
BP
287 // Alias another font - caller guarantees not to Release
288 void SetID(FontID id_) { id = id_; }
9ce192d4 289 friend class Surface;
1a2fb4cd 290 friend class SurfaceImpl;
9ce192d4
RD
291};
292
65ec6247
RD
293/**
294 * A surface abstracts a place to draw.
295 */
9ce192d4
RD
296class Surface {
297private:
9ce192d4
RD
298 // Private so Surface objects can not be copied
299 Surface(const Surface &) {}
300 Surface &operator=(const Surface &) { return *this; }
9ce192d4 301public:
1a2fb4cd
RD
302 Surface() {};
303 virtual ~Surface() {};
304 static Surface *Allocate();
305
9e730a78
RD
306 virtual void Init(WindowID wid)=0;
307 virtual void Init(SurfaceID sid, WindowID wid)=0;
308 virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid)=0;
1a2fb4cd
RD
309
310 virtual void Release()=0;
311 virtual bool Initialised()=0;
312 virtual void PenColour(ColourAllocated fore)=0;
313 virtual int LogPixelsY()=0;
314 virtual int DeviceHeightFont(int points)=0;
315 virtual void MoveTo(int x_, int y_)=0;
316 virtual void LineTo(int x_, int y_)=0;
317 virtual void Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back)=0;
318 virtual void RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0;
319 virtual void FillRectangle(PRectangle rc, ColourAllocated back)=0;
320 virtual void FillRectangle(PRectangle rc, Surface &surfacePattern)=0;
321 virtual void RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0;
322 virtual void Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0;
323 virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource)=0;
324
325 virtual void DrawTextNoClip(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back)=0;
326 virtual void DrawTextClipped(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back)=0;
9e730a78 327 virtual void DrawTextTransparent(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore)=0;
1a2fb4cd
RD
328 virtual void MeasureWidths(Font &font_, const char *s, int len, int *positions)=0;
329 virtual int WidthText(Font &font_, const char *s, int len)=0;
330 virtual int WidthChar(Font &font_, char ch)=0;
331 virtual int Ascent(Font &font_)=0;
332 virtual int Descent(Font &font_)=0;
333 virtual int InternalLeading(Font &font_)=0;
334 virtual int ExternalLeading(Font &font_)=0;
335 virtual int Height(Font &font_)=0;
336 virtual int AverageCharWidth(Font &font_)=0;
337
338 virtual int SetPalette(Palette *pal, bool inBackGround)=0;
339 virtual void SetClip(PRectangle rc)=0;
340 virtual void FlushCachedState()=0;
341
342 virtual void SetUnicodeMode(bool unicodeMode_)=0;
9e730a78 343 virtual void SetDBCSMode(int codePage)=0;
9ce192d4
RD
344};
345
1a2fb4cd
RD
346/**
347 * A simple callback action passing one piece of untyped user data.
348 */
349typedef void (*CallBackAction)(void*);
350
65ec6247
RD
351/**
352 * Class to hide the details of window manipulation.
353 * Does not own the window which will normally have a longer life than this object.
354 */
9ce192d4 355class Window {
9ce192d4
RD
356protected:
357 WindowID id;
358public:
1a2fb4cd
RD
359 Window() : id(0), cursorLast(cursorInvalid) {}
360 Window(const Window &source) : id(source.id), cursorLast(cursorInvalid) {}
9ce192d4
RD
361 virtual ~Window();
362 Window &operator=(WindowID id_) {
363 id = id_;
364 return *this;
365 }
9e730a78
RD
366 WindowID GetID() const { return id; }
367 bool Created() const { return id != 0; }
9ce192d4
RD
368 void Destroy();
369 bool HasFocus();
370 PRectangle GetPosition();
371 void SetPosition(PRectangle rc);
372 void SetPositionRelative(PRectangle rc, Window relativeTo);
373 PRectangle GetClientPosition();
374 void Show(bool show=true);
375 void InvalidateAll();
376 void InvalidateRectangle(PRectangle rc);
d134f170 377 virtual void SetFont(Font &font);
9e730a78 378 enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand };
9ce192d4
RD
379 void SetCursor(Cursor curs);
380 void SetTitle(const char *s);
1a2fb4cd
RD
381private:
382 Cursor cursorLast;
9ce192d4
RD
383};
384
65ec6247
RD
385/**
386 * Listbox management.
387 */
1a2fb4cd 388
9ce192d4 389class ListBox : public Window {
9ce192d4
RD
390public:
391 ListBox();
392 virtual ~ListBox();
9e730a78
RD
393 static ListBox *Allocate();
394
395 virtual void SetFont(Font &font)=0;
396 virtual void Create(Window &parent, int ctrlID, int lineHeight_, bool unicodeMode_)=0;
397 virtual void SetAverageCharWidth(int width)=0;
398 virtual void SetVisibleRows(int rows)=0;
399 virtual PRectangle GetDesiredRect()=0;
400 virtual int CaretFromEdge()=0;
401 virtual void Clear()=0;
402 virtual void Append(char *s, int type = -1)=0;
403 virtual int Length()=0;
404 virtual void Select(int n)=0;
405 virtual int GetSelection()=0;
406 virtual int Find(const char *prefix)=0;
407 virtual void GetValue(int n, char *value, int len)=0;
9e730a78
RD
408 virtual void RegisterImage(int type, const char *xpm_data)=0;
409 virtual void ClearRegisteredImages()=0;
410 virtual void SetDoubleClickAction(CallBackAction, void *)=0;
9ce192d4
RD
411};
412
65ec6247
RD
413/**
414 * Menu management.
415 */
9ce192d4
RD
416class Menu {
417 MenuID id;
418public:
419 Menu();
420 MenuID GetID() { return id; }
421 void CreatePopUp();
422 void Destroy();
423 void Show(Point pt, Window &w);
424};
425
1a2fb4cd
RD
426class ElapsedTime {
427 long bigBit;
428 long littleBit;
429public:
430 ElapsedTime();
431 double Duration(bool reset=false);
432};
433
e14d10b0
RD
434/**
435 * Dynamic Library (DLL/SO/...) loading
436 */
437class DynamicLibrary {
438public:
439 virtual ~DynamicLibrary() {};
440
441 /// @return Pointer to function "name", or NULL on failure.
442 virtual Function FindFunction(const char *name) = 0;
443
444 /// @return true if the library was loaded successfully.
445 virtual bool IsValid() = 0;
446
447 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
448 static DynamicLibrary *Load(const char *modulePath);
449};
450
65ec6247
RD
451/**
452 * Platform class used to retrieve system wide parameters such as double click speed
453 * and chrome colour. Not a creatable object, more of a module with several functions.
454 */
9ce192d4
RD
455class Platform {
456 // Private so Platform objects can not be copied
457 Platform(const Platform &) {}
458 Platform &operator=(const Platform &) { return *this; }
459public:
460 // Should be private because no new Platforms are ever created
461 // but gcc warns about this
462 Platform() {}
463 ~Platform() {}
1a2fb4cd
RD
464 static ColourDesired Chrome();
465 static ColourDesired ChromeHighlight();
9ce192d4
RD
466 static const char *DefaultFont();
467 static int DefaultFontSize();
468 static unsigned int DoubleClickTime();
9e730a78 469 static bool MouseButtonBounce();
9ce192d4
RD
470 static void DebugDisplay(const char *s);
471 static bool IsKeyDown(int key);
472 static long SendScintilla(
473 WindowID w, unsigned int msg, unsigned long wParam=0, long lParam=0);
a834585d
RD
474 static long SendScintillaPointer(
475 WindowID w, unsigned int msg, unsigned long wParam=0, void *lParam=0);
1a2fb4cd 476 static bool IsDBCSLeadByte(int codePage, char ch);
9e730a78
RD
477 static int DBCSCharLength(int codePage, const char *s);
478 static int DBCSCharMaxLength();
65ec6247 479
9ce192d4
RD
480 // These are utility functions not really tied to a platform
481 static int Minimum(int a, int b);
482 static int Maximum(int a, int b);
d134f170
RD
483 // Next three assume 16 bit shorts and 32 bit longs
484 static long LongFromTwoShorts(short a,short b) {
485 return (a) | ((b) << 16);
486 }
487 static short HighShortFromLong(long x) {
488 return static_cast<short>(x >> 16);
489 }
490 static short LowShortFromLong(long x) {
491 return static_cast<short>(x & 0xffff);
492 }
9ce192d4 493 static void DebugPrintf(const char *format, ...);
65ec6247
RD
494 static bool ShowAssertionPopUps(bool assertionPopUps_);
495 static void Assert(const char *c, const char *file, int line);
9ce192d4
RD
496 static int Clamp(int val, int minVal, int maxVal);
497};
498
65ec6247
RD
499#ifdef NDEBUG
500#define PLATFORM_ASSERT(c) ((void)0)
501#else
502#define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
503#endif
504
1a2fb4cd
RD
505// Shut up annoying Visual C++ warnings:
506#ifdef _MSC_VER
507#pragma warning(disable: 4244 4309 4514 4710)
508#endif
509
9ce192d4 510#endif