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__)
76 typedef float XYPOSITION
;
77 typedef double XYACCUMULATOR
;
78 inline int RoundXYPosition(XYPOSITION xyPos
) {
79 return static_cast<int>(xyPos
+ 0.5);
82 // Underlying the implementation of the platform classes are platform specific types.
83 // Sometimes these need to be passed around by client code so they are defined here
86 typedef void *SurfaceID
;
87 typedef void *WindowID
;
89 typedef void *TickerID
;
90 typedef void *Function
;
91 typedef void *IdlerID
;
94 * A geometric point class.
95 * Point is similar to the Win32 POINT and GTK+ GdkPoint types.
102 explicit Point(XYPOSITION x_
=0, XYPOSITION y_
=0) : x(x_
), y(y_
) {
105 static Point
FromInts(int x_
, int y_
) {
106 return Point(static_cast<XYPOSITION
>(x_
), static_cast<XYPOSITION
>(y_
));
109 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
113 * A geometric rectangle class.
114 * PRectangle is similar to the Win32 RECT.
115 * PRectangles contain their top and left sides, but not their right and bottom sides.
124 explicit PRectangle(XYPOSITION left_
=0, XYPOSITION top_
=0, XYPOSITION right_
=0, XYPOSITION bottom_
= 0) :
125 left(left_
), top(top_
), right(right_
), bottom(bottom_
) {
128 static PRectangle
FromInts(int left_
, int top_
, int right_
, int bottom_
) {
129 return PRectangle(static_cast<XYPOSITION
>(left_
), static_cast<XYPOSITION
>(top_
),
130 static_cast<XYPOSITION
>(right_
), static_cast<XYPOSITION
>(bottom_
));
133 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
135 bool operator==(const PRectangle
&rc
) const {
136 return (rc
.left
== left
) && (rc
.right
== right
) &&
137 (rc
.top
== top
) && (rc
.bottom
== bottom
);
139 bool Contains(Point pt
) const {
140 return (pt
.x
>= left
) && (pt
.x
<= right
) &&
141 (pt
.y
>= top
) && (pt
.y
<= bottom
);
143 bool ContainsWholePixel(Point pt
) const {
144 // Does the rectangle contain all of the pixel to left/below the point
145 return (pt
.x
>= left
) && ((pt
.x
+1) <= right
) &&
146 (pt
.y
>= top
) && ((pt
.y
+1) <= bottom
);
148 bool Contains(PRectangle rc
) const {
149 return (rc
.left
>= left
) && (rc
.right
<= right
) &&
150 (rc
.top
>= top
) && (rc
.bottom
<= bottom
);
152 bool Intersects(PRectangle other
) const {
153 return (right
> other
.left
) && (left
< other
.right
) &&
154 (bottom
> other
.top
) && (top
< other
.bottom
);
156 void Move(XYPOSITION xDelta
, XYPOSITION yDelta
) {
162 XYPOSITION
Width() const { return right
- left
; }
163 XYPOSITION
Height() const { return bottom
- top
; }
165 return (Height() <= 0) || (Width() <= 0);
170 * Holds a desired RGB colour.
172 class ColourDesired
{
175 ColourDesired(long lcol
=0) {
179 ColourDesired(unsigned int red
, unsigned int green
, unsigned int blue
) {
180 Set(red
, green
, blue
);
183 bool operator==(const ColourDesired
&other
) const {
184 return co
== other
.co
;
187 void Set(long lcol
) {
191 void Set(unsigned int red
, unsigned int green
, unsigned int blue
) {
192 co
= red
| (green
<< 8) | (blue
<< 16);
195 static inline unsigned int ValueOfHex(const char ch
) {
196 if (ch
>= '0' && ch
<= '9')
198 else if (ch
>= 'A' && ch
<= 'F')
199 return ch
- 'A' + 10;
200 else if (ch
>= 'a' && ch
<= 'f')
201 return ch
- 'a' + 10;
206 void Set(const char *val
) {
210 unsigned int r
= ValueOfHex(val
[0]) * 16 + ValueOfHex(val
[1]);
211 unsigned int g
= ValueOfHex(val
[2]) * 16 + ValueOfHex(val
[3]);
212 unsigned int b
= ValueOfHex(val
[4]) * 16 + ValueOfHex(val
[5]);
216 long AsLong() const {
220 unsigned int GetRed() const {
224 unsigned int GetGreen() const {
225 return (co
>> 8) & 0xff;
228 unsigned int GetBlue() const {
229 return (co
>> 16) & 0xff;
237 struct FontParameters
{
238 const char *faceName
;
247 const char *faceName_
,
251 int extraFontFlag_
=0,
253 int characterSet_
=0) :
259 extraFontFlag(extraFontFlag_
),
260 technology(technology_
),
261 characterSet(characterSet_
)
270 // Private so Font objects can not be copied
272 Font
&operator=(const Font
&);
277 virtual void Create(const FontParameters
&fp
);
278 virtual void Release();
280 FontID
GetID() { return fid
; }
281 // Alias another font - caller guarantees not to Release
282 void SetID(FontID fid_
) { fid
= fid_
; }
283 friend class Surface
;
284 friend class SurfaceImpl
;
288 * A surface abstracts a place to draw.
292 // Private so Surface objects can not be copied
293 Surface(const Surface
&) {}
294 Surface
&operator=(const Surface
&) { return *this; }
297 virtual ~Surface() {}
298 static Surface
*Allocate(int technology
);
300 virtual void Init(WindowID wid
)=0;
301 virtual void Init(SurfaceID sid
, WindowID wid
)=0;
302 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
)=0;
304 virtual void Release()=0;
305 virtual bool Initialised()=0;
306 virtual void PenColour(ColourDesired fore
)=0;
307 virtual int LogPixelsY()=0;
308 virtual int DeviceHeightFont(int points
)=0;
309 virtual void MoveTo(int x_
, int y_
)=0;
310 virtual void LineTo(int x_
, int y_
)=0;
311 virtual void Polygon(Point
*pts
, int npts
, ColourDesired fore
, ColourDesired back
)=0;
312 virtual void RectangleDraw(PRectangle rc
, ColourDesired fore
, ColourDesired back
)=0;
313 virtual void FillRectangle(PRectangle rc
, ColourDesired back
)=0;
314 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
)=0;
315 virtual void RoundedRectangle(PRectangle rc
, ColourDesired fore
, ColourDesired back
)=0;
316 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourDesired fill
, int alphaFill
,
317 ColourDesired outline
, int alphaOutline
, int flags
)=0;
318 virtual void DrawRGBAImage(PRectangle rc
, int width
, int height
, const unsigned char *pixelsImage
) = 0;
319 virtual void Ellipse(PRectangle rc
, ColourDesired fore
, ColourDesired back
)=0;
320 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
)=0;
322 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, XYPOSITION ybase
, const char *s
, int len
, ColourDesired fore
, ColourDesired back
)=0;
323 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, XYPOSITION ybase
, const char *s
, int len
, ColourDesired fore
, ColourDesired back
)=0;
324 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, XYPOSITION ybase
, const char *s
, int len
, ColourDesired fore
)=0;
325 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, XYPOSITION
*positions
)=0;
326 virtual XYPOSITION
WidthText(Font
&font_
, const char *s
, int len
)=0;
327 virtual XYPOSITION
WidthChar(Font
&font_
, char ch
)=0;
328 virtual XYPOSITION
Ascent(Font
&font_
)=0;
329 virtual XYPOSITION
Descent(Font
&font_
)=0;
330 virtual XYPOSITION
InternalLeading(Font
&font_
)=0;
331 virtual XYPOSITION
Height(Font
&font_
)=0;
332 virtual XYPOSITION
AverageCharWidth(Font
&font_
)=0;
334 virtual void SetClip(PRectangle rc
)=0;
335 virtual void FlushCachedState()=0;
337 virtual void SetUnicodeMode(bool unicodeMode_
)=0;
338 virtual void SetDBCSMode(int codePage
)=0;
342 * A simple callback action passing one piece of untyped user data.
344 typedef void (*CallBackAction
)(void*);
347 * Class to hide the details of window manipulation.
348 * Does not own the window which will normally have a longer life than this object.
354 Window() : wid(0), cursorLast(cursorInvalid
) {
356 Window(const Window
&source
) : wid(source
.wid
), cursorLast(cursorInvalid
) {
359 Window
&operator=(WindowID wid_
) {
361 cursorLast
= cursorInvalid
;
364 Window
&operator=(const Window
&other
) {
365 if (this != &other
) {
367 cursorLast
= other
.cursorLast
;
371 WindowID
GetID() const { return wid
; }
372 bool Created() const { return wid
!= 0; }
374 PRectangle
GetPosition();
375 void SetPosition(PRectangle rc
);
376 void SetPositionRelative(PRectangle rc
, Window relativeTo
);
377 PRectangle
GetClientPosition();
378 void Show(bool show
=true);
379 void InvalidateAll();
380 void InvalidateRectangle(PRectangle rc
);
381 virtual void SetFont(Font
&font
);
382 enum Cursor
{ cursorInvalid
, cursorText
, cursorArrow
, cursorUp
, cursorWait
, cursorHoriz
, cursorVert
, cursorReverseArrow
, cursorHand
};
383 void SetCursor(Cursor curs
);
384 PRectangle
GetMonitorRect(Point pt
);
390 * Listbox management.
393 // ScintillaBase implements IListBoxDelegate to receive ListBoxEvents from a ListBox
395 struct ListBoxEvent
{
396 enum class EventType
{ selectionChange
, doubleClick
} event
;
397 ListBoxEvent(EventType event_
) : event(event_
) {
401 class IListBoxDelegate
{
403 virtual void ListNotify(ListBoxEvent
*plbe
)=0;
406 class ListBox
: public Window
{
410 static ListBox
*Allocate();
412 virtual void SetFont(Font
&font
)=0;
413 virtual void Create(Window
&parent
, int ctrlID
, Point location
, int lineHeight_
, bool unicodeMode_
, int technology_
)=0;
414 virtual void SetAverageCharWidth(int width
)=0;
415 virtual void SetVisibleRows(int rows
)=0;
416 virtual int GetVisibleRows() const=0;
417 virtual PRectangle
GetDesiredRect()=0;
418 virtual int CaretFromEdge()=0;
419 virtual void Clear()=0;
420 virtual void Append(char *s
, int type
= -1)=0;
421 virtual int Length()=0;
422 virtual void Select(int n
)=0;
423 virtual int GetSelection()=0;
424 virtual int Find(const char *prefix
)=0;
425 virtual void GetValue(int n
, char *value
, int len
)=0;
426 virtual void RegisterImage(int type
, const char *xpm_data
)=0;
427 virtual void RegisterRGBAImage(int type
, int width
, int height
, const unsigned char *pixelsImage
) = 0;
428 virtual void ClearRegisteredImages()=0;
429 virtual void SetDelegate(IListBoxDelegate
*lbDelegate
)=0;
430 virtual void SetList(const char* list
, char separator
, char typesep
)=0;
440 MenuID
GetID() { return mid
; }
443 void Show(Point pt
, Window
&w
);
451 double Duration(bool reset
=false);
455 * Dynamic Library (DLL/SO/...) loading
457 class DynamicLibrary
{
459 virtual ~DynamicLibrary() {}
461 /// @return Pointer to function "name", or NULL on failure.
462 virtual Function
FindFunction(const char *name
) = 0;
464 /// @return true if the library was loaded successfully.
465 virtual bool IsValid() = 0;
467 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
468 static DynamicLibrary
*Load(const char *modulePath
);
471 #if defined(__clang__)
472 # if __has_feature(attribute_analyzer_noreturn)
473 # define CLANG_ANALYZER_NORETURN __attribute__((analyzer_noreturn))
475 # define CLANG_ANALYZER_NORETURN
478 # define CLANG_ANALYZER_NORETURN
482 * Platform class used to retrieve system wide parameters such as double click speed
483 * and chrome colour. Not a creatable object, more of a module with several functions.
486 // Private so Platform objects can not be copied
487 Platform(const Platform
&) {}
488 Platform
&operator=(const Platform
&) { return *this; }
490 // Should be private because no new Platforms are ever created
491 // but gcc warns about this
494 static ColourDesired
Chrome();
495 static ColourDesired
ChromeHighlight();
496 static const char *DefaultFont();
497 static int DefaultFontSize();
498 static unsigned int DoubleClickTime();
499 static void DebugDisplay(const char *s
);
500 static long LongFromTwoShorts(short a
,short b
) {
501 return (a
) | ((b
) << 16);
504 static void DebugPrintf(const char *format
, ...);
505 static bool ShowAssertionPopUps(bool assertionPopUps_
);
506 static void Assert(const char *c
, const char *file
, int line
) CLANG_ANALYZER_NORETURN
;
510 #define PLATFORM_ASSERT(c) ((void)0)
512 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))