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-2009 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 // PLAT_TK = Tcl/TK on Linux or Win32
17 
18 #define PLAT_GTK 0
19 #define PLAT_GTK_WIN32 0
20 #define PLAT_GTK_MACOSX 0
21 #define PLAT_MACOSX 0
22 #define PLAT_WIN 0
23 #define PLAT_WX  0
24 #define PLAT_QT 0
25 #define PLAT_FOX 0
26 #define PLAT_CURSES 0
27 #define PLAT_TK 0
28 
29 #if defined(FOX)
30 #undef PLAT_FOX
31 #define PLAT_FOX 1
32 
33 #elif defined(__WX__)
34 #undef PLAT_WX
35 #define PLAT_WX  1
36 
37 #elif defined(CURSES)
38 #undef PLAT_CURSES
39 #define PLAT_CURSES 1
40 
41 #elif defined(SCINTILLA_QT)
42 #undef PLAT_QT
43 #define PLAT_QT 1
44 
45 #elif defined(TK)
46 #undef PLAT_TK
47 #define PLAT_TK 1
48 
49 #elif defined(GTK)
50 #undef PLAT_GTK
51 #define PLAT_GTK 1
52 
53 #if defined(__WIN32__) || defined(_MSC_VER)
54 #undef PLAT_GTK_WIN32
55 #define PLAT_GTK_WIN32 1
56 #endif
57 
58 #if defined(__APPLE__)
59 #undef PLAT_GTK_MACOSX
60 #define PLAT_GTK_MACOSX 1
61 #endif
62 
63 #elif defined(__APPLE__)
64 
65 #undef PLAT_MACOSX
66 #define PLAT_MACOSX 1
67 
68 #else
69 #undef PLAT_WIN
70 #define PLAT_WIN 1
71 
72 #endif
73 
74 #ifdef SCI_NAMESPACE
75 namespace Scintilla {
76 #endif
77 
78 typedef float XYPOSITION;
79 typedef double XYACCUMULATOR;
RoundXYPosition(XYPOSITION xyPos)80 inline int RoundXYPosition(XYPOSITION xyPos) {
81 	return int(xyPos + 0.5);
82 }
83 
84 // Underlying the implementation of the platform classes are platform specific types.
85 // Sometimes these need to be passed around by client code so they are defined here
86 
87 typedef void *FontID;
88 typedef void *SurfaceID;
89 typedef void *WindowID;
90 typedef void *MenuID;
91 typedef void *TickerID;
92 typedef void *Function;
93 typedef void *IdlerID;
94 
95 /**
96  * A geometric point class.
97  * Point is similar to the Win32 POINT and GTK+ GdkPoint types.
98  */
99 class Point {
100 public:
101 	XYPOSITION x;
102 	XYPOSITION y;
103 
x(x_)104 	explicit Point(XYPOSITION x_=0, XYPOSITION y_=0) : x(x_), y(y_) {
105 	}
106 
FromInts(int x_,int y_)107 	static Point FromInts(int x_, int y_) {
108 		return Point(static_cast<XYPOSITION>(x_), static_cast<XYPOSITION>(y_));
109 	}
110 
111 	// Other automatically defined methods (assignment, copy constructor, destructor) are fine
112 
113 	static Point FromLong(long lpoint);
114 };
115 
116 /**
117  * A geometric rectangle class.
118  * PRectangle is similar to the Win32 RECT.
119  * PRectangles contain their top and left sides, but not their right and bottom sides.
120  */
121 class PRectangle {
122 public:
123 	XYPOSITION left;
124 	XYPOSITION top;
125 	XYPOSITION right;
126 	XYPOSITION bottom;
127 
128 	explicit PRectangle(XYPOSITION left_=0, XYPOSITION top_=0, XYPOSITION right_=0, XYPOSITION bottom_ = 0) :
left(left_)129 		left(left_), top(top_), right(right_), bottom(bottom_) {
130 	}
131 
FromInts(int left_,int top_,int right_,int bottom_)132 	static PRectangle FromInts(int left_, int top_, int right_, int bottom_) {
133 		return PRectangle(static_cast<XYPOSITION>(left_), static_cast<XYPOSITION>(top_),
134 			static_cast<XYPOSITION>(right_), static_cast<XYPOSITION>(bottom_));
135 	}
136 
137 	// Other automatically defined methods (assignment, copy constructor, destructor) are fine
138 
139 	bool operator==(PRectangle &rc) const {
140 		return (rc.left == left) && (rc.right == right) &&
141 			(rc.top == top) && (rc.bottom == bottom);
142 	}
Contains(Point pt)143 	bool Contains(Point pt) const {
144 		return (pt.x >= left) && (pt.x <= right) &&
145 			(pt.y >= top) && (pt.y <= bottom);
146 	}
Contains(PRectangle rc)147 	bool Contains(PRectangle rc) const {
148 		return (rc.left >= left) && (rc.right <= right) &&
149 			(rc.top >= top) && (rc.bottom <= bottom);
150 	}
Intersects(PRectangle other)151 	bool Intersects(PRectangle other) const {
152 		return (right > other.left) && (left < other.right) &&
153 			(bottom > other.top) && (top < other.bottom);
154 	}
Move(XYPOSITION xDelta,XYPOSITION yDelta)155 	void Move(XYPOSITION xDelta, XYPOSITION yDelta) {
156 		left += xDelta;
157 		top += yDelta;
158 		right += xDelta;
159 		bottom += yDelta;
160 	}
Width()161 	XYPOSITION Width() const { return right - left; }
Height()162 	XYPOSITION Height() const { return bottom - top; }
Empty()163 	bool Empty() const {
164 		return (Height() <= 0) || (Width() <= 0);
165 	}
166 };
167 
168 /**
169  * Holds a desired RGB colour.
170  */
171 class ColourDesired {
172 	long co;
173 public:
174 	ColourDesired(long lcol=0) {
175 		co = lcol;
176 	}
177 
ColourDesired(unsigned int red,unsigned int green,unsigned int blue)178 	ColourDesired(unsigned int red, unsigned int green, unsigned int blue) {
179 		Set(red, green, blue);
180 	}
181 
182 	bool operator==(const ColourDesired &other) const {
183 		return co == other.co;
184 	}
185 
Set(long lcol)186 	void Set(long lcol) {
187 		co = lcol;
188 	}
189 
Set(unsigned int red,unsigned int green,unsigned int blue)190 	void Set(unsigned int red, unsigned int green, unsigned int blue) {
191 		co = red | (green << 8) | (blue << 16);
192 	}
193 
ValueOfHex(const char ch)194 	static inline unsigned int ValueOfHex(const char ch) {
195 		if (ch >= '0' && ch <= '9')
196 			return ch - '0';
197 		else if (ch >= 'A' && ch <= 'F')
198 			return ch - 'A' + 10;
199 		else if (ch >= 'a' && ch <= 'f')
200 			return ch - 'a' + 10;
201 		else
202 			return 0;
203 	}
204 
Set(const char * val)205 	void Set(const char *val) {
206 		if (*val == '#') {
207 			val++;
208 		}
209 		unsigned int r = ValueOfHex(val[0]) * 16 + ValueOfHex(val[1]);
210 		unsigned int g = ValueOfHex(val[2]) * 16 + ValueOfHex(val[3]);
211 		unsigned int b = ValueOfHex(val[4]) * 16 + ValueOfHex(val[5]);
212 		Set(r, g, b);
213 	}
214 
AsLong()215 	long AsLong() const {
216 		return co;
217 	}
218 
GetRed()219 	unsigned int GetRed() const {
220 		return co & 0xff;
221 	}
222 
GetGreen()223 	unsigned int GetGreen() const {
224 		return (co >> 8) & 0xff;
225 	}
226 
GetBlue()227 	unsigned int GetBlue() const {
228 		return (co >> 16) & 0xff;
229 	}
230 };
231 
232 /**
233  * Font management.
234  */
235 
236 struct FontParameters {
237 	const char *faceName;
238 	float size;
239 	int weight;
240 	bool italic;
241 	int extraFontFlag;
242 	int technology;
243 	int characterSet;
244 
245 	FontParameters(
246 		const char *faceName_,
247 		float size_=10,
248 		int weight_=400,
249 		bool italic_=false,
250 		int extraFontFlag_=0,
251 		int technology_=0,
252 		int characterSet_=0) :
253 
faceNameFontParameters254 		faceName(faceName_),
255 		size(size_),
256 		weight(weight_),
257 		italic(italic_),
258 		extraFontFlag(extraFontFlag_),
259 		technology(technology_),
260 		characterSet(characterSet_)
261 	{
262 	}
263 
264 };
265 
266 class Font {
267 protected:
268 	FontID fid;
269 #if PLAT_WX
270 	int ascent;
271 #endif
272 	// Private so Font objects can not be copied
273 	Font(const Font &);
274 	Font &operator=(const Font &);
275 public:
276 	Font();
277 	virtual ~Font();
278 
279 	virtual void Create(const FontParameters &fp);
280 	virtual void Release();
281 
GetID()282 	FontID GetID() { return fid; }
283 	// Alias another font - caller guarantees not to Release
SetID(FontID fid_)284 	void SetID(FontID fid_) { fid = fid_; }
285 #if PLAT_WX
SetAscent(int ascent_)286 	void SetAscent(int ascent_) { ascent = ascent_; }
287 #endif
288 	friend class Surface;
289 	friend class SurfaceImpl;
290 };
291 
292 /**
293  * A surface abstracts a place to draw.
294  */
295 class Surface {
296 private:
297 	// Private so Surface objects can not be copied
Surface(const Surface &)298 	Surface(const Surface &) {}
299 	Surface &operator=(const Surface &) { return *this; }
300 public:
Surface()301 	Surface() {}
~Surface()302 	virtual ~Surface() {}
303 	static Surface *Allocate(int technology);
304 
305 	virtual void Init(WindowID wid)=0;
306 	virtual void Init(SurfaceID sid, WindowID wid)=0;
307 	virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid)=0;
308 
309 	virtual void Release()=0;
310 	virtual bool Initialised()=0;
311 	virtual void PenColour(ColourDesired fore)=0;
312 	virtual int LogPixelsY()=0;
313 	virtual int DeviceHeightFont(int points)=0;
314 	virtual void MoveTo(int x_, int y_)=0;
315 	virtual void LineTo(int x_, int y_)=0;
316 	virtual void Polygon(Point *pts, int npts, ColourDesired fore, ColourDesired back)=0;
317 	virtual void RectangleDraw(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
318 	virtual void FillRectangle(PRectangle rc, ColourDesired back)=0;
319 	virtual void FillRectangle(PRectangle rc, Surface &surfacePattern)=0;
320 	virtual void RoundedRectangle(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
321 	virtual void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill, int alphaFill,
322 		ColourDesired outline, int alphaOutline, int flags)=0;
323 	virtual void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage) = 0;
324 	virtual void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back)=0;
325 	virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource)=0;
326 
327 	virtual void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
328 	virtual void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore, ColourDesired back)=0;
329 	virtual void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase, const char *s, int len, ColourDesired fore)=0;
330 	virtual void MeasureWidths(Font &font_, const char *s, int len, XYPOSITION *positions)=0;
331 	virtual XYPOSITION WidthText(Font &font_, const char *s, int len)=0;
332 	virtual XYPOSITION WidthChar(Font &font_, char ch)=0;
333 	virtual XYPOSITION Ascent(Font &font_)=0;
334 	virtual XYPOSITION Descent(Font &font_)=0;
335 	virtual XYPOSITION InternalLeading(Font &font_)=0;
336 	virtual XYPOSITION ExternalLeading(Font &font_)=0;
337 	virtual XYPOSITION Height(Font &font_)=0;
338 	virtual XYPOSITION AverageCharWidth(Font &font_)=0;
339 
340 	virtual void SetClip(PRectangle rc)=0;
341 	virtual void FlushCachedState()=0;
342 
343 	virtual void SetUnicodeMode(bool unicodeMode_)=0;
344 	virtual void SetDBCSMode(int codePage)=0;
345 };
346 
347 /**
348  * A simple callback action passing one piece of untyped user data.
349  */
350 typedef void (*CallBackAction)(void*);
351 
352 /**
353  * Class to hide the details of window manipulation.
354  * Does not own the window which will normally have a longer life than this object.
355  */
356 class Window {
357 protected:
358 	WindowID wid;
359 public:
Window()360 	Window() : wid(0), cursorLast(cursorInvalid) {
361 	}
Window(const Window & source)362 	Window(const Window &source) : wid(source.wid), cursorLast(cursorInvalid) {
363 	}
364 	virtual ~Window();
365 	Window &operator=(WindowID wid_) {
366 		wid = wid_;
367 		return *this;
368 	}
GetID()369 	WindowID GetID() const { return wid; }
Created()370 	bool Created() const { return wid != 0; }
371 	void Destroy();
372 	bool HasFocus();
373 	PRectangle GetPosition();
374 	void SetPosition(PRectangle rc);
375 	void SetPositionRelative(PRectangle rc, Window relativeTo);
376 	PRectangle GetClientPosition();
377 	void Show(bool show=true);
378 	void InvalidateAll();
379 	void InvalidateRectangle(PRectangle rc);
380 	virtual void SetFont(Font &font);
381 	enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand };
382 	void SetCursor(Cursor curs);
383 	void SetTitle(const char *s);
384 	PRectangle GetMonitorRect(Point pt);
385 private:
386 	Cursor cursorLast;
387 };
388 
389 /**
390  * Listbox management.
391  */
392 
393 class ListBox : public Window {
394 public:
395 	ListBox();
396 	virtual ~ListBox();
397 	static ListBox *Allocate();
398 
399 	virtual void SetFont(Font &font)=0;
400 	virtual void Create(Window &parent, int ctrlID, Point location, int lineHeight_, bool unicodeMode_, int technology_)=0;
401 	virtual void SetAverageCharWidth(int width)=0;
402 	virtual void SetVisibleRows(int rows)=0;
403 	virtual int GetVisibleRows() const=0;
404 	virtual PRectangle GetDesiredRect()=0;
405 	virtual int CaretFromEdge()=0;
406 	virtual void Clear()=0;
407 	virtual void Append(char *s, int type = -1)=0;
408 	virtual int Length()=0;
409 	virtual void Select(int n)=0;
410 	virtual int GetSelection()=0;
411 	virtual int Find(const char *prefix)=0;
412 	virtual void GetValue(int n, char *value, int len)=0;
413 	virtual void RegisterImage(int type, const char *xpm_data)=0;
414 	virtual void RegisterRGBAImage(int type, int width, int height, const unsigned char *pixelsImage) = 0;
415 	virtual void ClearRegisteredImages()=0;
416 	virtual void SetDoubleClickAction(CallBackAction, void *)=0;
417 	virtual void SetList(const char* list, char separator, char typesep)=0;
418 };
419 
420 /**
421  * Menu management.
422  */
423 class Menu {
424 	MenuID mid;
425 public:
426 	Menu();
GetID()427 	MenuID GetID() { return mid; }
428 	void CreatePopUp();
429 	void Destroy();
430 	void Show(Point pt, Window &w);
431 };
432 
433 class ElapsedTime {
434 	long bigBit;
435 	long littleBit;
436 public:
437 	ElapsedTime();
438 	double Duration(bool reset=false);
439 };
440 
441 /**
442  * Dynamic Library (DLL/SO/...) loading
443  */
444 class DynamicLibrary {
445 public:
~DynamicLibrary()446 	virtual ~DynamicLibrary() {}
447 
448 	/// @return Pointer to function "name", or NULL on failure.
449 	virtual Function FindFunction(const char *name) = 0;
450 
451 	/// @return true if the library was loaded successfully.
452 	virtual bool IsValid() = 0;
453 
454 	/// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
455 	static DynamicLibrary *Load(const char *modulePath);
456 };
457 
458 #if defined(__clang__)
459 # if __has_feature(attribute_analyzer_noreturn)
460 #  define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
461 # else
462 #  define CLANG_ANALYZER_NORETURN
463 # endif
464 #else
465 # define CLANG_ANALYZER_NORETURN
466 #endif
467 
468 /**
469  * Platform class used to retrieve system wide parameters such as double click speed
470  * and chrome colour. Not a creatable object, more of a module with several functions.
471  */
472 class Platform {
473 	// Private so Platform objects can not be copied
Platform(const Platform &)474 	Platform(const Platform &) {}
475 	Platform &operator=(const Platform &) { return *this; }
476 public:
477 	// Should be private because no new Platforms are ever created
478 	// but gcc warns about this
Platform()479 	Platform() {}
~Platform()480 	~Platform() {}
481 	static ColourDesired Chrome();
482 	static ColourDesired ChromeHighlight();
483 	static const char *DefaultFont();
484 	static int DefaultFontSize();
485 	static unsigned int DoubleClickTime();
486 	static bool MouseButtonBounce();
487 	static void DebugDisplay(const char *s);
488 	static bool IsKeyDown(int key);
489 	static long SendScintilla(
490 		WindowID w, unsigned int msg, unsigned long wParam=0, long lParam=0);
491 	static long SendScintillaPointer(
492 		WindowID w, unsigned int msg, unsigned long wParam=0, void *lParam=0);
493 	static bool IsDBCSLeadByte(int codePage, char ch);
494 	static int DBCSCharLength(int codePage, const char *s);
495 	static int DBCSCharMaxLength();
496 
497 	// These are utility functions not really tied to a platform
498 	static int Minimum(int a, int b);
499 	static int Maximum(int a, int b);
500 	// Next three assume 16 bit shorts and 32 bit longs
LongFromTwoShorts(short a,short b)501 	static long LongFromTwoShorts(short a,short b) {
502 		return (a) | ((b) << 16);
503 	}
HighShortFromLong(long x)504 	static short HighShortFromLong(long x) {
505 		return static_cast<short>(x >> 16);
506 	}
LowShortFromLong(long x)507 	static short LowShortFromLong(long x) {
508 		return static_cast<short>(x & 0xffff);
509 	}
510 	static void DebugPrintf(const char *format, ...);
511 	static bool ShowAssertionPopUps(bool assertionPopUps_);
512 	static void Assert(const char *c, const char *file, int line) CLANG_ANALYZER_NORETURN;
513 	static int Clamp(int val, int minVal, int maxVal);
514 };
515 
516 #ifdef  NDEBUG
517 #define PLATFORM_ASSERT(c) ((void)0)
518 #else
519 #ifdef SCI_NAMESPACE
520 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))
521 #else
522 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
523 #endif
524 #endif
525 
526 #ifdef SCI_NAMESPACE
527 }
528 #endif
529 
530 #if defined(__GNUC__) && defined(SCINTILLA_QT)
531 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
532 #endif
533 
534 #endif
535