1 // Scintilla source code edit control
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.
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.
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
19 #define PLAT_GTK_WIN32 0
20 #define PLAT_GTK_MACOSX 0
41 #elif defined(SCINTILLA_QT)
53 #if defined(__WIN32__) || defined(_MSC_VER)
55 #define PLAT_GTK_WIN32 1
58 #if defined(__APPLE__)
59 #undef PLAT_GTK_MACOSX
60 #define PLAT_GTK_MACOSX 1
63 #elif defined(__APPLE__)
78 typedef float XYPOSITION
;
79 typedef double XYACCUMULATOR
;
80 inline int RoundXYPosition(XYPOSITION xyPos
) {
81 return static_cast<int>(xyPos
+ 0.5);
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
88 typedef void *SurfaceID
;
89 typedef void *WindowID
;
91 typedef void *TickerID
;
92 typedef void *Function
;
93 typedef void *IdlerID
;
96 * A geometric point class.
97 * Point is similar to the Win32 POINT and GTK+ GdkPoint types.
104 explicit Point(XYPOSITION x_
=0, XYPOSITION y_
=0) : x(x_
), y(y_
) {
107 static Point
FromInts(int x_
, int y_
) {
108 return Point(static_cast<XYPOSITION
>(x_
), static_cast<XYPOSITION
>(y_
));
111 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
113 static Point
FromLong(long lpoint
);
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.
128 explicit PRectangle(XYPOSITION left_
=0, XYPOSITION top_
=0, XYPOSITION right_
=0, XYPOSITION bottom_
= 0) :
129 left(left_
), top(top_
), right(right_
), bottom(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_
));
137 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
139 bool operator==(const PRectangle
&rc
) const {
140 return (rc
.left
== left
) && (rc
.right
== right
) &&
141 (rc
.top
== top
) && (rc
.bottom
== bottom
);
143 bool Contains(Point pt
) const {
144 return (pt
.x
>= left
) && (pt
.x
<= right
) &&
145 (pt
.y
>= top
) && (pt
.y
<= bottom
);
147 bool ContainsWholePixel(Point pt
) const {
148 // Does the rectangle contain all of the pixel to left/below the point
149 return (pt
.x
>= left
) && ((pt
.x
+1) <= right
) &&
150 (pt
.y
>= top
) && ((pt
.y
+1) <= bottom
);
152 bool Contains(PRectangle rc
) const {
153 return (rc
.left
>= left
) && (rc
.right
<= right
) &&
154 (rc
.top
>= top
) && (rc
.bottom
<= bottom
);
156 bool Intersects(PRectangle other
) const {
157 return (right
> other
.left
) && (left
< other
.right
) &&
158 (bottom
> other
.top
) && (top
< other
.bottom
);
160 void Move(XYPOSITION xDelta
, XYPOSITION yDelta
) {
166 XYPOSITION
Width() const { return right
- left
; }
167 XYPOSITION
Height() const { return bottom
- top
; }
169 return (Height() <= 0) || (Width() <= 0);
174 * Holds a desired RGB colour.
176 class ColourDesired
{
179 ColourDesired(long lcol
=0) {
183 ColourDesired(unsigned int red
, unsigned int green
, unsigned int blue
) {
184 Set(red
, green
, blue
);
187 bool operator==(const ColourDesired
&other
) const {
188 return co
== other
.co
;
191 void Set(long lcol
) {
195 void Set(unsigned int red
, unsigned int green
, unsigned int blue
) {
196 co
= red
| (green
<< 8) | (blue
<< 16);
199 static inline unsigned int ValueOfHex(const char ch
) {
200 if (ch
>= '0' && ch
<= '9')
202 else if (ch
>= 'A' && ch
<= 'F')
203 return ch
- 'A' + 10;
204 else if (ch
>= 'a' && ch
<= 'f')
205 return ch
- 'a' + 10;
210 void Set(const char *val
) {
214 unsigned int r
= ValueOfHex(val
[0]) * 16 + ValueOfHex(val
[1]);
215 unsigned int g
= ValueOfHex(val
[2]) * 16 + ValueOfHex(val
[3]);
216 unsigned int b
= ValueOfHex(val
[4]) * 16 + ValueOfHex(val
[5]);
220 long AsLong() const {
224 unsigned int GetRed() const {
228 unsigned int GetGreen() const {
229 return (co
>> 8) & 0xff;
232 unsigned int GetBlue() const {
233 return (co
>> 16) & 0xff;
241 struct FontParameters
{
242 const char *faceName
;
251 const char *faceName_
,
255 int extraFontFlag_
=0,
257 int characterSet_
=0) :
263 extraFontFlag(extraFontFlag_
),
264 technology(technology_
),
265 characterSet(characterSet_
)
274 // Private so Font objects can not be copied
276 Font
&operator=(const Font
&);
281 virtual void Create(const FontParameters
&fp
);
282 virtual void Release();
284 FontID
GetID() { return fid
; }
285 // Alias another font - caller guarantees not to Release
286 void SetID(FontID fid_
) { fid
= fid_
; }
287 friend class Surface
;
288 friend class SurfaceImpl
;
292 * A surface abstracts a place to draw.
296 // Private so Surface objects can not be copied
297 Surface(const Surface
&) {}
298 Surface
&operator=(const Surface
&) { return *this; }
301 virtual ~Surface() {}
302 static Surface
*Allocate(int technology
);
304 virtual void Init(WindowID wid
)=0;
305 virtual void Init(SurfaceID sid
, WindowID wid
)=0;
306 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
)=0;
308 virtual void Release()=0;
309 virtual bool Initialised()=0;
310 virtual void PenColour(ColourDesired fore
)=0;
311 virtual int LogPixelsY()=0;
312 virtual int DeviceHeightFont(int points
)=0;
313 virtual void MoveTo(int x_
, int y_
)=0;
314 virtual void LineTo(int x_
, int y_
)=0;
315 virtual void Polygon(Point
*pts
, int npts
, ColourDesired fore
, ColourDesired back
)=0;
316 virtual void RectangleDraw(PRectangle rc
, ColourDesired fore
, ColourDesired back
)=0;
317 virtual void FillRectangle(PRectangle rc
, ColourDesired back
)=0;
318 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
)=0;
319 virtual void RoundedRectangle(PRectangle rc
, ColourDesired fore
, ColourDesired back
)=0;
320 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourDesired fill
, int alphaFill
,
321 ColourDesired outline
, int alphaOutline
, int flags
)=0;
322 virtual void DrawRGBAImage(PRectangle rc
, int width
, int height
, const unsigned char *pixelsImage
) = 0;
323 virtual void Ellipse(PRectangle rc
, ColourDesired fore
, ColourDesired back
)=0;
324 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
)=0;
326 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, XYPOSITION ybase
, const char *s
, int len
, ColourDesired fore
, ColourDesired back
)=0;
327 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, XYPOSITION ybase
, const char *s
, int len
, ColourDesired fore
, ColourDesired back
)=0;
328 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, XYPOSITION ybase
, const char *s
, int len
, ColourDesired fore
)=0;
329 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, XYPOSITION
*positions
)=0;
330 virtual XYPOSITION
WidthText(Font
&font_
, const char *s
, int len
)=0;
331 virtual XYPOSITION
WidthChar(Font
&font_
, char ch
)=0;
332 virtual XYPOSITION
Ascent(Font
&font_
)=0;
333 virtual XYPOSITION
Descent(Font
&font_
)=0;
334 virtual XYPOSITION
InternalLeading(Font
&font_
)=0;
335 virtual XYPOSITION
ExternalLeading(Font
&font_
)=0;
336 virtual XYPOSITION
Height(Font
&font_
)=0;
337 virtual XYPOSITION
AverageCharWidth(Font
&font_
)=0;
339 virtual void SetClip(PRectangle rc
)=0;
340 virtual void FlushCachedState()=0;
342 virtual void SetUnicodeMode(bool unicodeMode_
)=0;
343 virtual void SetDBCSMode(int codePage
)=0;
347 * A simple callback action passing one piece of untyped user data.
349 typedef void (*CallBackAction
)(void*);
352 * Class to hide the details of window manipulation.
353 * Does not own the window which will normally have a longer life than this object.
359 Window() : wid(0), cursorLast(cursorInvalid
) {
361 Window(const Window
&source
) : wid(source
.wid
), cursorLast(cursorInvalid
) {
364 Window
&operator=(WindowID wid_
) {
366 cursorLast
= cursorInvalid
;
369 Window
&operator=(const Window
&other
) {
370 if (this != &other
) {
372 cursorLast
= other
.cursorLast
;
376 WindowID
GetID() const { return wid
; }
377 bool Created() const { return wid
!= 0; }
380 PRectangle
GetPosition();
381 void SetPosition(PRectangle rc
);
382 void SetPositionRelative(PRectangle rc
, Window relativeTo
);
383 PRectangle
GetClientPosition();
384 void Show(bool show
=true);
385 void InvalidateAll();
386 void InvalidateRectangle(PRectangle rc
);
387 virtual void SetFont(Font
&font
);
388 enum Cursor
{ cursorInvalid
, cursorText
, cursorArrow
, cursorUp
, cursorWait
, cursorHoriz
, cursorVert
, cursorReverseArrow
, cursorHand
};
389 void SetCursor(Cursor curs
);
390 void SetTitle(const char *s
);
391 PRectangle
GetMonitorRect(Point pt
);
397 * Listbox management.
400 class ListBox
: public Window
{
404 static ListBox
*Allocate();
406 virtual void SetFont(Font
&font
)=0;
407 virtual void Create(Window
&parent
, int ctrlID
, Point location
, int lineHeight_
, bool unicodeMode_
, int technology_
)=0;
408 virtual void SetAverageCharWidth(int width
)=0;
409 virtual void SetVisibleRows(int rows
)=0;
410 virtual int GetVisibleRows() const=0;
411 virtual PRectangle
GetDesiredRect()=0;
412 virtual int CaretFromEdge()=0;
413 virtual void Clear()=0;
414 virtual void Append(char *s
, int type
= -1)=0;
415 virtual int Length()=0;
416 virtual void Select(int n
)=0;
417 virtual int GetSelection()=0;
418 virtual int Find(const char *prefix
)=0;
419 virtual void GetValue(int n
, char *value
, int len
)=0;
420 virtual void RegisterImage(int type
, const char *xpm_data
)=0;
421 virtual void RegisterRGBAImage(int type
, int width
, int height
, const unsigned char *pixelsImage
) = 0;
422 virtual void ClearRegisteredImages()=0;
423 virtual void SetDoubleClickAction(CallBackAction
, void *)=0;
424 virtual void SetList(const char* list
, char separator
, char typesep
)=0;
434 MenuID
GetID() { return mid
; }
437 void Show(Point pt
, Window
&w
);
445 double Duration(bool reset
=false);
449 * Dynamic Library (DLL/SO/...) loading
451 class DynamicLibrary
{
453 virtual ~DynamicLibrary() {}
455 /// @return Pointer to function "name", or NULL on failure.
456 virtual Function
FindFunction(const char *name
) = 0;
458 /// @return true if the library was loaded successfully.
459 virtual bool IsValid() = 0;
461 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
462 static DynamicLibrary
*Load(const char *modulePath
);
465 #if defined(__clang__)
466 # if __has_feature(attribute_analyzer_noreturn)
467 # define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
469 # define CLANG_ANALYZER_NORETURN
472 # define CLANG_ANALYZER_NORETURN
476 * Platform class used to retrieve system wide parameters such as double click speed
477 * and chrome colour. Not a creatable object, more of a module with several functions.
480 // Private so Platform objects can not be copied
481 Platform(const Platform
&) {}
482 Platform
&operator=(const Platform
&) { return *this; }
484 // Should be private because no new Platforms are ever created
485 // but gcc warns about this
488 static ColourDesired
Chrome();
489 static ColourDesired
ChromeHighlight();
490 static const char *DefaultFont();
491 static int DefaultFontSize();
492 static unsigned int DoubleClickTime();
493 static bool MouseButtonBounce();
494 static void DebugDisplay(const char *s
);
495 static bool IsKeyDown(int key
);
496 static long SendScintilla(
497 WindowID w
, unsigned int msg
, unsigned long wParam
=0, long lParam
=0);
498 static long SendScintillaPointer(
499 WindowID w
, unsigned int msg
, unsigned long wParam
=0, void *lParam
=0);
500 static bool IsDBCSLeadByte(int codePage
, char ch
);
501 static int DBCSCharLength(int codePage
, const char *s
);
502 static int DBCSCharMaxLength();
504 // These are utility functions not really tied to a platform
505 static int Minimum(int a
, int b
);
506 static int Maximum(int a
, int b
);
507 // Next three assume 16 bit shorts and 32 bit longs
508 static long LongFromTwoShorts(short a
,short b
) {
509 return (a
) | ((b
) << 16);
511 static short HighShortFromLong(long x
) {
512 return static_cast<short>(x
>> 16);
514 static short LowShortFromLong(long x
) {
515 return static_cast<short>(x
& 0xffff);
517 static void DebugPrintf(const char *format
, ...);
518 static bool ShowAssertionPopUps(bool assertionPopUps_
);
519 static void Assert(const char *c
, const char *file
, int line
) CLANG_ANALYZER_NORETURN
;
520 static int Clamp(int val
, int minVal
, int maxVal
);
524 #define PLATFORM_ASSERT(c) ((void)0)
527 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))
529 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))