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