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