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