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