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