]> git.saurik.com Git - wxWidgets.git/blame - src/stc/scintilla/include/Platform.h
moved c-runtime functions for CW to wxchar
[wxWidgets.git] / src / stc / scintilla / include / Platform.h
CommitLineData
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 **/
9e730a78 6// Copyright 1998-2003 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
18b94087
RD
47#if PLAT_WX
48#include <wx/object.h> // For the global memory operators, if needed.
49#endif
50
51
9ce192d4
RD
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
1a2fb4cd
RD
55typedef void *FontID;
56typedef void *SurfaceID;
57typedef void *WindowID;
58typedef void *MenuID;
59typedef void *TickerID;
9ce192d4 60
65ec6247
RD
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 */
9ce192d4
RD
65class Point {
66public:
67 int x;
68 int y;
65ec6247 69
9ce192d4
RD
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
65ec6247 74
9ce192d4
RD
75 static Point FromLong(long lpoint);
76};
77
65ec6247
RD
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 */
9ce192d4
RD
83class PRectangle {
84public:
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
65ec6247
RD
96 bool operator==(PRectangle &rc) {
97 return (rc.left == left) && (rc.right == right) &&
98 (rc.top == top) && (rc.bottom == bottom);
99 }
9ce192d4
RD
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) {
a834585d
RD
109 return (right > other.left) && (left < other.right) &&
110 (bottom > other.top) && (top < other.bottom);
9ce192d4 111 }
9e730a78
RD
112 void Move(int xDelta, int yDelta) {
113 left += xDelta;
114 top += yDelta;
115 right += xDelta;
116 bottom += yDelta;
117 }
9ce192d4
RD
118 int Width() { return right - left; }
119 int Height() { return bottom - top; }
120};
121
1a2fb4cd
RD
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 */
9ce192d4 133
65ec6247 134/**
1a2fb4cd 135 * Holds a desired RGB colour.
65ec6247 136 */
1a2fb4cd
RD
137class ColourDesired {
138 long co;
9ce192d4 139public:
1a2fb4cd
RD
140 ColourDesired(long lcol=0) {
141 co = lcol;
142 }
65ec6247 143
1a2fb4cd 144 ColourDesired(unsigned int red, unsigned int green, unsigned int blue) {
9e730a78 145 Set(red, green, blue);
1a2fb4cd
RD
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
9e730a78
RD
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
1a2fb4cd
RD
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 }
9ce192d4
RD
196};
197
65ec6247 198/**
1a2fb4cd
RD
199 * Holds an allocated RGB colour which may be an approximation to the desired colour.
200 */
201class ColourAllocated {
202 long coAllocated;
203
204public:
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.
65ec6247 221 */
9ce192d4 222struct ColourPair {
1a2fb4cd
RD
223 ColourDesired desired;
224 ColourAllocated allocated;
9ce192d4 225
1a2fb4cd 226 ColourPair(ColourDesired desired_=ColourDesired(0,0,0)) {
9ce192d4 227 desired = desired_;
1a2fb4cd 228 allocated.Set(desired.AsLong());
9ce192d4 229 }
9e730a78
RD
230 void Copy() {
231 allocated.Set(desired.AsLong());
232 }
9ce192d4
RD
233};
234
235class Window; // Forward declaration for Palette
236
65ec6247
RD
237/**
238 * Colour palette management.
239 */
9ce192d4
RD
240class Palette {
241 int used;
242 enum {numEntries = 100};
243 ColourPair entries[numEntries];
244#if PLAT_GTK
1a2fb4cd 245 void *allocatedPalette; // GdkColor *
9ce192d4 246 int allocatedLen;
9ce192d4
RD
247#endif
248public:
1a2fb4cd
RD
249#if PLAT_WIN
250 void *hpal;
251#endif
9ce192d4 252 bool allowRealization;
65ec6247 253
9ce192d4
RD
254 Palette();
255 ~Palette();
256
257 void Release();
65ec6247
RD
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 */
9ce192d4
RD
264 void WantFind(ColourPair &cp, bool want);
265
266 void Allocate(Window &w);
9ce192d4
RD
267};
268
65ec6247
RD
269/**
270 * Font management.
271 */
9ce192d4 272class Font {
d134f170 273protected:
9ce192d4
RD
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; }
281public:
282 Font();
d134f170 283 virtual ~Font();
9ce192d4 284
d134f170
RD
285 virtual void Create(const char *faceName, int characterSet, int size, bool bold, bool italic);
286 virtual void Release();
9ce192d4
RD
287
288 FontID GetID() { return id; }
f6bcfd97
BP
289 // Alias another font - caller guarantees not to Release
290 void SetID(FontID id_) { id = id_; }
9ce192d4 291 friend class Surface;
1a2fb4cd 292 friend class SurfaceImpl;
9ce192d4
RD
293};
294
65ec6247
RD
295/**
296 * A surface abstracts a place to draw.
297 */
9ce192d4
RD
298class Surface {
299private:
9ce192d4
RD
300 // Private so Surface objects can not be copied
301 Surface(const Surface &) {}
302 Surface &operator=(const Surface &) { return *this; }
9ce192d4 303public:
1a2fb4cd
RD
304 Surface() {};
305 virtual ~Surface() {};
306 static Surface *Allocate();
307
9e730a78
RD
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;
1a2fb4cd
RD
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;
9e730a78 329 virtual void DrawTextTransparent(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore)=0;
1a2fb4cd
RD
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;
9e730a78 345 virtual void SetDBCSMode(int codePage)=0;
9ce192d4
RD
346};
347
1a2fb4cd
RD
348/**
349 * A simple callback action passing one piece of untyped user data.
350 */
351typedef void (*CallBackAction)(void*);
352
65ec6247
RD
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 */
9ce192d4 357class Window {
9ce192d4
RD
358protected:
359 WindowID id;
360public:
1a2fb4cd
RD
361 Window() : id(0), cursorLast(cursorInvalid) {}
362 Window(const Window &source) : id(source.id), cursorLast(cursorInvalid) {}
9ce192d4
RD
363 virtual ~Window();
364 Window &operator=(WindowID id_) {
365 id = id_;
366 return *this;
367 }
9e730a78
RD
368 WindowID GetID() const { return id; }
369 bool Created() const { return id != 0; }
9ce192d4
RD
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);
d134f170 379 virtual void SetFont(Font &font);
9e730a78 380 enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand };
9ce192d4
RD
381 void SetCursor(Cursor curs);
382 void SetTitle(const char *s);
1a2fb4cd
RD
383private:
384 Cursor cursorLast;
9ce192d4
RD
385};
386
65ec6247
RD
387/**
388 * Listbox management.
389 */
1a2fb4cd 390
9ce192d4 391class ListBox : public Window {
9ce192d4
RD
392public:
393 ListBox();
394 virtual ~ListBox();
9e730a78
RD
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;
9ce192d4
RD
414};
415
65ec6247
RD
416/**
417 * Menu management.
418 */
9ce192d4
RD
419class Menu {
420 MenuID id;
421public:
422 Menu();
423 MenuID GetID() { return id; }
424 void CreatePopUp();
425 void Destroy();
426 void Show(Point pt, Window &w);
427};
428
1a2fb4cd
RD
429class ElapsedTime {
430 long bigBit;
431 long littleBit;
432public:
433 ElapsedTime();
434 double Duration(bool reset=false);
435};
436
65ec6247
RD
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 */
9ce192d4
RD
441class Platform {
442 // Private so Platform objects can not be copied
443 Platform(const Platform &) {}
444 Platform &operator=(const Platform &) { return *this; }
445public:
446 // Should be private because no new Platforms are ever created
447 // but gcc warns about this
448 Platform() {}
449 ~Platform() {}
1a2fb4cd
RD
450 static ColourDesired Chrome();
451 static ColourDesired ChromeHighlight();
9ce192d4
RD
452 static const char *DefaultFont();
453 static int DefaultFontSize();
454 static unsigned int DoubleClickTime();
9e730a78 455 static bool MouseButtonBounce();
9ce192d4
RD
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);
a834585d
RD
460 static long SendScintillaPointer(
461 WindowID w, unsigned int msg, unsigned long wParam=0, void *lParam=0);
1a2fb4cd 462 static bool IsDBCSLeadByte(int codePage, char ch);
9e730a78
RD
463 static int DBCSCharLength(int codePage, const char *s);
464 static int DBCSCharMaxLength();
65ec6247 465
9ce192d4
RD
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);
d134f170
RD
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 }
9ce192d4 479 static void DebugPrintf(const char *format, ...);
65ec6247
RD
480 static bool ShowAssertionPopUps(bool assertionPopUps_);
481 static void Assert(const char *c, const char *file, int line);
9ce192d4
RD
482 static int Clamp(int val, int minVal, int maxVal);
483};
484
65ec6247
RD
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
1a2fb4cd
RD
491// Shut up annoying Visual C++ warnings:
492#ifdef _MSC_VER
493#pragma warning(disable: 4244 4309 4514 4710)
494#endif
495
9ce192d4 496#endif