First pass at wxPython for Mac (darwin only so far). It
[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-2001 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
22 #if defined(__WX__)
23 #undef PLAT_WX
24 #define PLAT_WX 1
25
26 #elif defined(GTK)
27 #undef PLAT_GTK
28 #define PLAT_GTK 1
29
30 #ifdef _MSC_VER
31 #undef PLAT_GTK_WIN32
32 #define PLAT_GTK_WIN32 1
33 #endif
34
35 #else
36 #undef PLAT_WIN
37 #define PLAT_WIN 1
38
39 #endif
40
41
42 // Include the main header for each platform
43
44 #if PLAT_GTK
45 #ifdef _MSC_VER
46 #pragma warning(disable: 4505 4514 4710 4800)
47 #endif
48 #include <gtk/gtk.h>
49 #include <gdk/gdkkeysyms.h>
50 #endif
51
52 #if PLAT_WIN
53 #define _WIN32_WINNT 0x0400 // Otherwise some required stuff gets ifdef'd out
54 // Vassili Bourdo: shut up annoying Visual C++ warnings:
55 #ifdef _MSC_VER
56 #pragma warning(disable: 4244 4309 4710 4800)
57 #endif
58 #include <windows.h>
59 #include <commctrl.h>
60 #include <richedit.h>
61 #endif
62
63 #if PLAT_WX
64 #include <wx/wx.h>
65 #endif
66
67 #define ColourID scColourID
68 #define FontID scFontID
69 #define SurfaceID scSurfaceID
70 #define WindowID scWindowID
71 #define MenuID scMenuID
72 #define Point scPoint
73 #define PRectangle scPRectangle
74 #define Colour scColour
75 #define ColourPair scColourPair
76 #define Window scWindow
77 #define Palette scPalette
78 #define Font scFont
79 #define Surface scSurface
80 #define Window scWindow
81 #define ListBox scListBox
82 #define Menu scMenu
83 #define Platform scPlatform
84 #define TextRange scTextRange
85 #define KeyMap scKeyMap
86 #define Style scStyle
87
88
89 // Underlying the implementation of the platform classes are platform specific types.
90 // Sometimes these need to be passed around by client code so they are defined here
91
92 #if PLAT_GTK
93 typedef GdkColor ColourID;
94 typedef GdkFont* FontID;
95 typedef GdkDrawable* SurfaceID;
96 typedef GtkWidget* WindowID;
97 typedef GtkItemFactory* MenuID;
98 #endif
99
100 #if PLAT_WIN
101 typedef COLORREF ColourID;
102 typedef HFONT FontID;
103 typedef HDC SurfaceID;
104 typedef HWND WindowID;
105 typedef HMENU MenuID;
106 #endif
107
108 #if PLAT_WX
109 typedef wxColour ColourID;
110 typedef wxFont* FontID;
111 typedef wxDC* SurfaceID;
112 typedef wxWindow* WindowID;
113 typedef wxMenu* MenuID;
114 #endif
115
116 /**
117 * A geometric point class.
118 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
119 */
120 class Point {
121 public:
122 int x;
123 int y;
124
125 Point(int x_=0, int y_=0) : x(x_), y(y_) {
126 }
127
128 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
129
130 static Point FromLong(long lpoint);
131 };
132
133 /**
134 * A geometric rectangle class.
135 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
136 * PRectangles contain their top and left sides, but not their right and bottom sides.
137 */
138 class PRectangle {
139 public:
140 int left;
141 int top;
142 int right;
143 int bottom;
144
145 PRectangle(int left_=0, int top_=0, int right_=0, int bottom_ = 0) :
146 left(left_), top(top_), right(right_), bottom(bottom_) {
147 }
148
149 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
150
151 bool operator==(PRectangle &rc) {
152 return (rc.left == left) && (rc.right == right) &&
153 (rc.top == top) && (rc.bottom == bottom);
154 }
155 bool Contains(Point pt) {
156 return (pt.x >= left) && (pt.x <= right) &&
157 (pt.y >= top) && (pt.y <= bottom);
158 }
159 bool Contains(PRectangle rc) {
160 return (rc.left >= left) && (rc.right <= right) &&
161 (rc.top >= top) && (rc.bottom <= bottom);
162 }
163 bool Intersects(PRectangle other) {
164 return (right >= other.left) && (left <= other.right) &&
165 (bottom >= other.top) && (top <= other.bottom);
166 }
167 int Width() { return right - left; }
168 int Height() { return bottom - top; }
169 };
170
171 #if PLAT_WX
172 wxRect wxRectFromPRectangle(PRectangle prc);
173 PRectangle PRectangleFromwxRect(wxRect rc);
174 #endif
175
176 /**
177 * A colour class.
178 */
179 class Colour {
180 ColourID co;
181 public:
182 Colour(long lcol=0);
183 Colour(unsigned int red, unsigned int green, unsigned int blue);
184 bool operator==(const Colour &other) const;
185 long AsLong() const;
186 unsigned int GetRed();
187 unsigned int GetGreen();
188 unsigned int GetBlue();
189
190 friend class Surface;
191 friend class Palette;
192 };
193
194 /**
195 * Colour pairs hold a desired colour and the colour that the graphics engine
196 * allocates to approximate the desired colour.
197 * To make palette management more automatic, ColourPairs could register at
198 * construction time with a palette management object.
199 */
200 struct ColourPair {
201 Colour desired;
202 Colour allocated;
203
204 ColourPair(Colour desired_=Colour(0,0,0)) {
205 desired = desired_;
206 allocated = desired;
207 }
208 };
209
210 class Window; // Forward declaration for Palette
211
212 /**
213 * Colour palette management.
214 */
215 class Palette {
216 int used;
217 enum {numEntries = 100};
218 ColourPair entries[numEntries];
219 #if PLAT_GTK
220 GdkColor *allocatedPalette;
221 int allocatedLen;
222 #elif PLAT_WIN
223 HPALETTE hpal;
224 #elif PLAT_WX
225 // wxPalette* pal; // **** Is this needed?
226 #endif
227 public:
228 bool allowRealization;
229
230 Palette();
231 ~Palette();
232
233 void Release();
234
235 /**
236 * This method either adds a colour to the list of wanted colours (want==true)
237 * or retrieves the allocated colour back to the ColourPair.
238 * This is one method to make it easier to keep the code for wanting and retrieving in sync.
239 */
240 void WantFind(ColourPair &cp, bool want);
241
242 void Allocate(Window &w);
243
244 friend class Surface;
245 };
246
247 /**
248 * Font management.
249 */
250 class Font {
251 protected:
252 FontID id;
253 #if PLAT_WX
254 int ascent;
255 #endif
256 // Private so Font objects can not be copied
257 Font(const Font &) {}
258 Font &operator=(const Font &) { id=0; return *this; }
259 public:
260 Font();
261 virtual ~Font();
262
263 virtual void Create(const char *faceName, int characterSet, int size, bool bold, bool italic);
264 virtual void Release();
265
266 FontID GetID() { return id; }
267 // Alias another font - caller guarantees not to Release
268 void SetID(FontID id_) { id = id_; }
269 friend class Surface;
270 };
271
272 /**
273 * A surface abstracts a place to draw.
274 */
275 class Surface {
276 private:
277 bool unicodeMode;
278 #if PLAT_GTK
279 GdkDrawable *drawable;
280 GdkGC *gc;
281 GdkPixmap *ppixmap;
282 int x;
283 int y;
284 bool inited;
285 bool createdGC;
286 #elif PLAT_WIN
287 HDC hdc;
288 bool hdcOwned;
289 HPEN pen;
290 HPEN penOld;
291 HBRUSH brush;
292 HBRUSH brushOld;
293 HFONT font;
294 HFONT fontOld;
295 HBITMAP bitmap;
296 HBITMAP bitmapOld;
297 HPALETTE paletteOld;
298 #elif PLAT_WX
299 wxDC* hdc;
300 bool hdcOwned;
301 wxBitmap* bitmap;
302 int x;
303 int y;
304 #endif
305
306 // Private so Surface objects can not be copied
307 Surface(const Surface &) {}
308 Surface &operator=(const Surface &) { return *this; }
309 #if PLAT_WIN || PLAT_WX
310 void BrushColor(Colour back);
311 void SetFont(Font &font_);
312 #endif
313 public:
314 Surface();
315 ~Surface();
316
317 void Init();
318 void Init(SurfaceID hdc_);
319 void InitPixMap(int width, int height, Surface *surface_);
320
321 void Release();
322 bool Initialised();
323 void PenColour(Colour fore);
324 int LogPixelsY();
325 int DeviceHeightFont(int points);
326 void MoveTo(int x_, int y_);
327 void LineTo(int x_, int y_);
328 void Polygon(Point *pts, int npts, Colour fore, Colour back);
329 void RectangleDraw(PRectangle rc, Colour fore, Colour back);
330 void FillRectangle(PRectangle rc, Colour back);
331 void FillRectangle(PRectangle rc, Surface &surfacePattern);
332 void RoundedRectangle(PRectangle rc, Colour fore, Colour back);
333 void Ellipse(PRectangle rc, Colour fore, Colour back);
334 void Copy(PRectangle rc, Point from, Surface &surfaceSource);
335
336 void DrawText(PRectangle rc, Font &font_, int ybase, const char *s, int len, Colour fore, Colour back);
337 void DrawTextClipped(PRectangle rc, Font &font_, int ybase, const char *s, int len, Colour fore, Colour back);
338 void MeasureWidths(Font &font_, const char *s, int len, int *positions);
339 int WidthText(Font &font_, const char *s, int len);
340 int WidthChar(Font &font_, char ch);
341 int Ascent(Font &font_);
342 int Descent(Font &font_);
343 int InternalLeading(Font &font_);
344 int ExternalLeading(Font &font_);
345 int Height(Font &font_);
346 int AverageCharWidth(Font &font_);
347
348 int SetPalette(Palette *pal, bool inBackGround);
349 void SetClip(PRectangle rc);
350 void FlushCachedState();
351
352 void SetUnicodeMode(bool unicodeMode_) {
353 unicodeMode=unicodeMode_;
354 }
355 };
356
357 /**
358 * Class to hide the details of window manipulation.
359 * Does not own the window which will normally have a longer life than this object.
360 */
361 class Window {
362 friend class ListBox;
363 protected:
364 WindowID id;
365 public:
366 Window() : id(0) {}
367 Window(const Window &source) : id(source.id) {}
368 virtual ~Window();
369 Window &operator=(WindowID id_) {
370 id = id_;
371 return *this;
372 }
373 WindowID GetID() { return id; }
374 bool Created() { return id != 0; }
375 void Destroy();
376 bool HasFocus();
377 PRectangle GetPosition();
378 void SetPosition(PRectangle rc);
379 void SetPositionRelative(PRectangle rc, Window relativeTo);
380 PRectangle GetClientPosition();
381 void Show(bool show=true);
382 void InvalidateAll();
383 void InvalidateRectangle(PRectangle rc);
384 virtual void SetFont(Font &font);
385 enum Cursor { cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow };
386 void SetCursor(Cursor curs);
387 void SetTitle(const char *s);
388 #if PLAT_WIN
389 LRESULT SendMessage(UINT msg, WPARAM wParam=0, LPARAM lParam=0);
390 int GetDlgCtrlID();
391 HINSTANCE GetInstance();
392 #endif
393 };
394
395 /**
396 * Listbox management.
397 */
398 class ListBox : public Window {
399 #if PLAT_GTK
400 WindowID list;
401 WindowID scroller;
402 int current;
403 #endif
404 int desiredVisibleRows;
405 unsigned int maxItemCharacters;
406 unsigned int aveCharWidth;
407 public:
408 ListBox();
409 virtual ~ListBox();
410 void Create(Window &parent, int ctrlID);
411 virtual void SetFont(Font &font);
412 void SetAverageCharWidth(int width);
413 void SetVisibleRows(int rows);
414 PRectangle GetDesiredRect();
415 void Clear();
416 void Append(char *s);
417 int Length();
418 void Select(int n);
419 int GetSelection();
420 int Find(const char *prefix);
421 void GetValue(int n, char *value, int len);
422 void Sort();
423 };
424
425 /**
426 * Menu management.
427 */
428 class Menu {
429 MenuID id;
430 public:
431 Menu();
432 MenuID GetID() { return id; }
433 void CreatePopUp();
434 void Destroy();
435 void Show(Point pt, Window &w);
436 };
437
438 /**
439 * Platform class used to retrieve system wide parameters such as double click speed
440 * and chrome colour. Not a creatable object, more of a module with several functions.
441 */
442 class Platform {
443 // Private so Platform objects can not be copied
444 Platform(const Platform &) {}
445 Platform &operator=(const Platform &) { return *this; }
446 public:
447 // Should be private because no new Platforms are ever created
448 // but gcc warns about this
449 Platform() {}
450 ~Platform() {}
451 static Colour Chrome();
452 static Colour ChromeHighlight();
453 static const char *DefaultFont();
454 static int DefaultFontSize();
455 static unsigned int DoubleClickTime();
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
461 // These are utility functions not really tied to a platform
462 static int Minimum(int a, int b);
463 static int Maximum(int a, int b);
464 // Next three assume 16 bit shorts and 32 bit longs
465 static long LongFromTwoShorts(short a,short b) {
466 return (a) | ((b) << 16);
467 }
468 static short HighShortFromLong(long x) {
469 return static_cast<short>(x >> 16);
470 }
471 static short LowShortFromLong(long x) {
472 return static_cast<short>(x & 0xffff);
473 }
474 static void DebugPrintf(const char *format, ...);
475 static bool ShowAssertionPopUps(bool assertionPopUps_);
476 static void Assert(const char *c, const char *file, int line);
477 static int Clamp(int val, int minVal, int maxVal);
478 };
479
480 #ifdef NDEBUG
481 #define PLATFORM_ASSERT(c) ((void)0)
482 #else
483 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
484 #endif
485
486 #endif