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