]>
Commit | Line | Data |
---|---|---|
9ce192d4 RD |
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 | friend class Surface; | |
217 | }; | |
218 | ||
219 | // A surface abstracts a place to draw | |
220 | class Surface { | |
221 | private: | |
222 | #if PLAT_GTK | |
223 | GdkDrawable *drawable; | |
224 | GdkGC *gc; | |
225 | GdkPixmap *ppixmap; | |
226 | int x; | |
227 | int y; | |
228 | bool inited; | |
229 | bool createdGC; | |
230 | #elif PLAT_WIN | |
231 | HDC hdc; | |
232 | bool hdcOwned; | |
233 | HPEN pen; | |
234 | HPEN penOld; | |
235 | HBRUSH brush; | |
236 | HBRUSH brushOld; | |
237 | HFONT font; | |
238 | HFONT fontOld; | |
239 | HBITMAP bitmap; | |
240 | HBITMAP bitmapOld; | |
241 | HPALETTE paletteOld; | |
242 | #elif PLAT_WX | |
243 | wxDC* hdc; | |
244 | bool hdcOwned; | |
245 | wxBitmap* bitmap; | |
246 | int x; | |
247 | int y; | |
248 | #endif | |
249 | ||
250 | // Private so Surface objects can not be copied | |
251 | Surface(const Surface &) {} | |
252 | Surface &operator=(const Surface &) { return *this; } | |
253 | #if PLAT_WIN || PLAT_WX | |
254 | void BrushColor(Colour back); | |
255 | void SetFont(Font &font_); | |
256 | #endif | |
257 | public: | |
258 | Surface(); | |
259 | ~Surface(); | |
260 | ||
261 | void Init(); | |
262 | void Init(SurfaceID hdc_); | |
263 | void InitPixMap(int width, int height, Surface *surface_); | |
264 | ||
265 | void Release(); | |
266 | bool Initialised(); | |
267 | void PenColour(Colour fore); | |
268 | int LogPixelsY(); | |
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 | }; | |
294 | ||
295 | // Class to hide the details of window manipulation | |
296 | // Does not own the window which will normally have a longer life than this object | |
297 | class Window { | |
298 | friend class ListBox; | |
299 | protected: | |
300 | WindowID id; | |
301 | public: | |
302 | Window() : id(0) {} | |
303 | virtual ~Window(); | |
304 | Window &operator=(WindowID id_) { | |
305 | id = id_; | |
306 | return *this; | |
307 | } | |
308 | WindowID GetID() { return id; } | |
309 | bool Created() { return id != 0; } | |
310 | void Destroy(); | |
311 | bool HasFocus(); | |
312 | PRectangle GetPosition(); | |
313 | void SetPosition(PRectangle rc); | |
314 | void SetPositionRelative(PRectangle rc, Window relativeTo); | |
315 | PRectangle GetClientPosition(); | |
316 | void Show(bool show=true); | |
317 | void InvalidateAll(); | |
318 | void InvalidateRectangle(PRectangle rc); | |
319 | void SetFont(Font &font); | |
320 | enum Cursor { cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow }; | |
321 | void SetCursor(Cursor curs); | |
322 | void SetTitle(const char *s); | |
323 | #if PLAT_WIN | |
324 | LRESULT SendMessage(UINT msg, WPARAM wParam=0, LPARAM lParam=0); | |
325 | int GetDlgCtrlID(); | |
326 | HINSTANCE GetInstance(); | |
327 | #endif | |
328 | }; | |
329 | ||
330 | class ListBox : public Window { | |
331 | #if PLAT_GTK | |
332 | WindowID list; | |
333 | WindowID scroller; | |
334 | int current; | |
335 | #endif | |
336 | public: | |
337 | ListBox(); | |
338 | virtual ~ListBox(); | |
339 | ListBox &operator=(WindowID id_) { | |
340 | id = id_; | |
341 | return *this; | |
342 | } | |
343 | void Create(Window &parent, int ctrlID); | |
344 | void Clear(); | |
345 | void Append(char *s); | |
346 | int Length(); | |
347 | void Select(int n); | |
348 | int GetSelection(); | |
349 | int Find(const char *prefix); | |
350 | void GetValue(int n, char *value, int len); | |
351 | void Sort(); | |
352 | }; | |
353 | ||
354 | class Menu { | |
355 | MenuID id; | |
356 | public: | |
357 | Menu(); | |
358 | MenuID GetID() { return id; } | |
359 | void CreatePopUp(); | |
360 | void Destroy(); | |
361 | void Show(Point pt, Window &w); | |
362 | }; | |
363 | ||
364 | // Platform class used to retrieve system wide parameters such as double click speed | |
365 | // and chrome colour. Not a creatable object, more of a module with several functions. | |
366 | class Platform { | |
367 | // Private so Platform objects can not be copied | |
368 | Platform(const Platform &) {} | |
369 | Platform &operator=(const Platform &) { return *this; } | |
370 | public: | |
371 | // Should be private because no new Platforms are ever created | |
372 | // but gcc warns about this | |
373 | Platform() {} | |
374 | ~Platform() {} | |
375 | static Colour Chrome(); | |
376 | static Colour ChromeHighlight(); | |
377 | static const char *DefaultFont(); | |
378 | static int DefaultFontSize(); | |
379 | static unsigned int DoubleClickTime(); | |
380 | static void DebugDisplay(const char *s); | |
381 | static bool IsKeyDown(int key); | |
382 | static long SendScintilla( | |
383 | WindowID w, unsigned int msg, unsigned long wParam=0, long lParam=0); | |
384 | ||
385 | // These are utility functions not really tied to a platform | |
386 | static int Minimum(int a, int b); | |
387 | static int Maximum(int a, int b); | |
388 | static void DebugPrintf(const char *format, ...); | |
389 | static int Clamp(int val, int minVal, int maxVal); | |
390 | }; | |
391 | ||
392 | #endif |