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