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
18 #define PLAT_GTK_WIN32 0
19 #define PLAT_GTK_MACOSX 0
25 #define PLAT_NCURSES 0
35 #elif defined(NCURSES)
37 #define PLAT_NCURSES 1
39 #elif defined(SCINTILLA_QT)
47 #if defined(__WIN32__) || defined(_MSC_VER)
49 #define PLAT_GTK_WIN32 1
52 #if defined(__APPLE__)
53 #undef PLAT_GTK_MACOSX
54 #define PLAT_GTK_MACOSX 1
57 #elif defined(__APPLE__)
72 typedef float XYPOSITION
;
73 typedef double XYACCUMULATOR
;
74 //#define XYPOSITION int
76 // Underlying the implementation of the platform classes are platform specific types.
77 // Sometimes these need to be passed around by client code so they are defined here
80 typedef void *SurfaceID
;
81 typedef void *WindowID
;
83 typedef void *TickerID
;
84 typedef void *Function
;
85 typedef void *IdlerID
;
88 * A geometric point class.
89 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
96 explicit Point(XYPOSITION x_
=0, XYPOSITION y_
=0) : x(x_
), y(y_
) {
99 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
101 static Point
FromLong(long lpoint
);
105 * A geometric rectangle class.
106 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
107 * PRectangles contain their top and left sides, but not their right and bottom sides.
116 PRectangle(XYPOSITION left_
=0, XYPOSITION top_
=0, XYPOSITION right_
=0, XYPOSITION bottom_
= 0) :
117 left(left_
), top(top_
), right(right_
), bottom(bottom_
) {
120 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
122 bool operator==(PRectangle
&rc
) {
123 return (rc
.left
== left
) && (rc
.right
== right
) &&
124 (rc
.top
== top
) && (rc
.bottom
== bottom
);
126 bool Contains(Point pt
) {
127 return (pt
.x
>= left
) && (pt
.x
<= right
) &&
128 (pt
.y
>= top
) && (pt
.y
<= bottom
);
130 bool Contains(PRectangle rc
) {
131 return (rc
.left
>= left
) && (rc
.right
<= right
) &&
132 (rc
.top
>= top
) && (rc
.bottom
<= bottom
);
134 bool Intersects(PRectangle other
) {
135 return (right
> other
.left
) && (left
< other
.right
) &&
136 (bottom
> other
.top
) && (top
< other
.bottom
);
138 void Move(XYPOSITION xDelta
, XYPOSITION yDelta
) {
144 XYPOSITION
Width() { return right
- left
; }
145 XYPOSITION
Height() { return bottom
- top
; }
147 return (Height() <= 0) || (Width() <= 0);
152 * Holds a desired RGB colour.
154 class ColourDesired
{
157 ColourDesired(long lcol
=0) {
161 ColourDesired(unsigned int red
, unsigned int green
, unsigned int blue
) {
162 Set(red
, green
, blue
);
165 bool operator==(const ColourDesired
&other
) const {
166 return co
== other
.co
;
169 void Set(long lcol
) {
173 void Set(unsigned int red
, unsigned int green
, unsigned int blue
) {
174 co
= red
| (green
<< 8) | (blue
<< 16);
177 static inline unsigned int ValueOfHex(const char ch
) {
178 if (ch
>= '0' && ch
<= '9')
180 else if (ch
>= 'A' && ch
<= 'F')
181 return ch
- 'A' + 10;
182 else if (ch
>= 'a' && ch
<= 'f')
183 return ch
- 'a' + 10;
188 void Set(const char *val
) {
192 unsigned int r
= ValueOfHex(val
[0]) * 16 + ValueOfHex(val
[1]);
193 unsigned int g
= ValueOfHex(val
[2]) * 16 + ValueOfHex(val
[3]);
194 unsigned int b
= ValueOfHex(val
[4]) * 16 + ValueOfHex(val
[5]);
198 long AsLong() const {
202 unsigned int GetRed() {
206 unsigned int GetGreen() {
207 return (co
>> 8) & 0xff;
210 unsigned int GetBlue() {
211 return (co
>> 16) & 0xff;
219 struct FontParameters
{
220 const char *faceName
;
229 const char *faceName_
,
233 int extraFontFlag_
=0,
235 int characterSet_
=0) :
241 extraFontFlag(extraFontFlag_
),
242 technology(technology_
),
243 characterSet(characterSet_
)
255 // Private so Font objects can not be copied
257 Font
&operator=(const Font
&);
262 virtual void Create(const FontParameters
&fp
);
263 virtual void Release();
265 FontID
GetID() { return fid
; }
266 // Alias another font - caller guarantees not to Release
267 void SetID(FontID fid_
) { fid
= fid_
; }
269 void SetAscent(int ascent_
) { ascent
= ascent_
; }
271 friend class Surface
;
272 friend class SurfaceImpl
;
276 * A surface abstracts a place to draw.
280 // Private so Surface objects can not be copied
281 Surface(const Surface
&) {}
282 Surface
&operator=(const Surface
&) { return *this; }
285 virtual ~Surface() {}
286 static Surface
*Allocate(int technology
);
288 virtual void Init(WindowID wid
)=0;
289 virtual void Init(SurfaceID sid
, WindowID wid
)=0;
290 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
)=0;
292 virtual void Release()=0;
293 virtual bool Initialised()=0;
294 virtual void PenColour(ColourDesired fore
)=0;
295 virtual int LogPixelsY()=0;
296 virtual int DeviceHeightFont(int points
)=0;
297 virtual void MoveTo(int x_
, int y_
)=0;
298 virtual void LineTo(int x_
, int y_
)=0;
299 virtual void Polygon(Point
*pts
, int npts
, ColourDesired fore
, ColourDesired back
)=0;
300 virtual void RectangleDraw(PRectangle rc
, ColourDesired fore
, ColourDesired back
)=0;
301 virtual void FillRectangle(PRectangle rc
, ColourDesired back
)=0;
302 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
)=0;
303 virtual void RoundedRectangle(PRectangle rc
, ColourDesired fore
, ColourDesired back
)=0;
304 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourDesired fill
, int alphaFill
,
305 ColourDesired outline
, int alphaOutline
, int flags
)=0;
306 virtual void DrawRGBAImage(PRectangle rc
, int width
, int height
, const unsigned char *pixelsImage
) = 0;
307 virtual void Ellipse(PRectangle rc
, ColourDesired fore
, ColourDesired back
)=0;
308 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
)=0;
310 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, XYPOSITION ybase
, const char *s
, int len
, ColourDesired fore
, ColourDesired back
)=0;
311 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, XYPOSITION ybase
, const char *s
, int len
, ColourDesired fore
, ColourDesired back
)=0;
312 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, XYPOSITION ybase
, const char *s
, int len
, ColourDesired fore
)=0;
313 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, XYPOSITION
*positions
)=0;
314 virtual XYPOSITION
WidthText(Font
&font_
, const char *s
, int len
)=0;
315 virtual XYPOSITION
WidthChar(Font
&font_
, char ch
)=0;
316 virtual XYPOSITION
Ascent(Font
&font_
)=0;
317 virtual XYPOSITION
Descent(Font
&font_
)=0;
318 virtual XYPOSITION
InternalLeading(Font
&font_
)=0;
319 virtual XYPOSITION
ExternalLeading(Font
&font_
)=0;
320 virtual XYPOSITION
Height(Font
&font_
)=0;
321 virtual XYPOSITION
AverageCharWidth(Font
&font_
)=0;
323 virtual void SetClip(PRectangle rc
)=0;
324 virtual void FlushCachedState()=0;
326 virtual void SetUnicodeMode(bool unicodeMode_
)=0;
327 virtual void SetDBCSMode(int codePage
)=0;
331 * A simple callback action passing one piece of untyped user data.
333 typedef void (*CallBackAction
)(void*);
336 * Class to hide the details of window manipulation.
337 * Does not own the window which will normally have a longer life than this object.
347 Window() : wid(0), cursorLast(cursorInvalid
) {
353 Window(const Window
&source
) : wid(source
.wid
), cursorLast(cursorInvalid
) {
360 Window
&operator=(WindowID wid_
) {
364 WindowID
GetID() const { return wid
; }
365 bool Created() const { return wid
!= 0; }
368 PRectangle
GetPosition();
369 void SetPosition(PRectangle rc
);
370 void SetPositionRelative(PRectangle rc
, Window relativeTo
);
371 PRectangle
GetClientPosition();
372 void Show(bool show
=true);
373 void InvalidateAll();
374 void InvalidateRectangle(PRectangle rc
);
375 virtual void SetFont(Font
&font
);
376 enum Cursor
{ cursorInvalid
, cursorText
, cursorArrow
, cursorUp
, cursorWait
, cursorHoriz
, cursorVert
, cursorReverseArrow
, cursorHand
};
377 void SetCursor(Cursor curs
);
378 void SetTitle(const char *s
);
379 PRectangle
GetMonitorRect(Point pt
);
381 void SetWindow(void *ref
) { windowRef
= ref
; }
382 void SetControl(void *_control
) { control
= _control
; }
389 * Listbox management.
392 class ListBox
: public Window
{
396 static ListBox
*Allocate();
398 virtual void SetFont(Font
&font
)=0;
399 virtual void Create(Window
&parent
, int ctrlID
, Point location
, int lineHeight_
, bool unicodeMode_
, int technology_
)=0;
400 virtual void SetAverageCharWidth(int width
)=0;
401 virtual void SetVisibleRows(int rows
)=0;
402 virtual int GetVisibleRows() const=0;
403 virtual PRectangle
GetDesiredRect()=0;
404 virtual int CaretFromEdge()=0;
405 virtual void Clear()=0;
406 virtual void Append(char *s
, int type
= -1)=0;
407 virtual int Length()=0;
408 virtual void Select(int n
)=0;
409 virtual int GetSelection()=0;
410 virtual int Find(const char *prefix
)=0;
411 virtual void GetValue(int n
, char *value
, int len
)=0;
412 virtual void RegisterImage(int type
, const char *xpm_data
)=0;
413 virtual void RegisterRGBAImage(int type
, int width
, int height
, const unsigned char *pixelsImage
) = 0;
414 virtual void ClearRegisteredImages()=0;
415 virtual void SetDoubleClickAction(CallBackAction
, void *)=0;
416 virtual void SetList(const char* list
, char separator
, char typesep
)=0;
426 MenuID
GetID() { return mid
; }
429 void Show(Point pt
, Window
&w
);
437 double Duration(bool reset
=false);
441 * Dynamic Library (DLL/SO/...) loading
443 class DynamicLibrary
{
445 virtual ~DynamicLibrary() {}
447 /// @return Pointer to function "name", or NULL on failure.
448 virtual Function
FindFunction(const char *name
) = 0;
450 /// @return true if the library was loaded successfully.
451 virtual bool IsValid() = 0;
453 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
454 static DynamicLibrary
*Load(const char *modulePath
);
458 * Platform class used to retrieve system wide parameters such as double click speed
459 * and chrome colour. Not a creatable object, more of a module with several functions.
462 // Private so Platform objects can not be copied
463 Platform(const Platform
&) {}
464 Platform
&operator=(const Platform
&) { return *this; }
466 // Should be private because no new Platforms are ever created
467 // but gcc warns about this
470 static ColourDesired
Chrome();
471 static ColourDesired
ChromeHighlight();
472 static const char *DefaultFont();
473 static int DefaultFontSize();
474 static unsigned int DoubleClickTime();
475 static bool MouseButtonBounce();
476 static void DebugDisplay(const char *s
);
477 static bool IsKeyDown(int key
);
478 static long SendScintilla(
479 WindowID w
, unsigned int msg
, unsigned long wParam
=0, long lParam
=0);
480 static long SendScintillaPointer(
481 WindowID w
, unsigned int msg
, unsigned long wParam
=0, void *lParam
=0);
482 static bool IsDBCSLeadByte(int codePage
, char ch
);
483 static int DBCSCharLength(int codePage
, const char *s
);
484 static int DBCSCharMaxLength();
486 // These are utility functions not really tied to a platform
487 static int Minimum(int a
, int b
);
488 static int Maximum(int a
, int b
);
489 // Next three assume 16 bit shorts and 32 bit longs
490 static long LongFromTwoShorts(short a
,short b
) {
491 return (a
) | ((b
) << 16);
493 static short HighShortFromLong(long x
) {
494 return static_cast<short>(x
>> 16);
496 static short LowShortFromLong(long x
) {
497 return static_cast<short>(x
& 0xffff);
499 static void DebugPrintf(const char *format
, ...);
500 static bool ShowAssertionPopUps(bool assertionPopUps_
);
501 static void Assert(const char *c
, const char *file
, int line
);
502 static int Clamp(int val
, int minVal
, int maxVal
);
506 #define PLATFORM_ASSERT(c) ((void)0)
509 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))
511 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
519 // Shut up annoying Visual C++ warnings:
521 #pragma warning(disable: 4244 4309 4514 4710)
524 #if defined(__GNUC__) && defined(SCINTILLA_QT)
525 #pragma GCC diagnostic ignored "-Wmissing-braces"
526 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
527 #pragma GCC diagnostic ignored "-Wchar-subscripts"