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