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
37 #if defined(__WIN32__) || defined(_MSC_VER)
39 #define PLAT_GTK_WIN32 1
42 #if defined(__APPLE__)
43 #undef PLAT_GTK_MACOSX
44 #define PLAT_GTK_MACOSX 1
47 #elif defined(__APPLE__)
62 // Underlying the implementation of the platform classes are platform specific types.
63 // Sometimes these need to be passed around by client code so they are defined here
66 typedef void *SurfaceID
;
67 typedef void *WindowID
;
69 typedef void *TickerID
;
70 typedef void *Function
;
71 typedef void *IdlerID
;
74 * A geometric point class.
75 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
82 explicit Point(int x_
=0, int y_
=0) : x(x_
), y(y_
) {
85 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
87 static Point
FromLong(long lpoint
);
91 * A geometric rectangle class.
92 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
93 * PRectangles contain their top and left sides, but not their right and bottom sides.
102 PRectangle(int left_
=0, int top_
=0, int right_
=0, int bottom_
= 0) :
103 left(left_
), top(top_
), right(right_
), bottom(bottom_
) {
106 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
108 bool operator==(PRectangle
&rc
) {
109 return (rc
.left
== left
) && (rc
.right
== right
) &&
110 (rc
.top
== top
) && (rc
.bottom
== bottom
);
112 bool Contains(Point pt
) {
113 return (pt
.x
>= left
) && (pt
.x
<= right
) &&
114 (pt
.y
>= top
) && (pt
.y
<= bottom
);
116 bool Contains(PRectangle rc
) {
117 return (rc
.left
>= left
) && (rc
.right
<= right
) &&
118 (rc
.top
>= top
) && (rc
.bottom
<= bottom
);
120 bool Intersects(PRectangle other
) {
121 return (right
> other
.left
) && (left
< other
.right
) &&
122 (bottom
> other
.top
) && (top
< other
.bottom
);
124 void Move(int xDelta
, int yDelta
) {
130 int Width() { return right
- left
; }
131 int Height() { return bottom
- top
; }
133 return (Height() <= 0) || (Width() <= 0);
138 * In some circumstances, including Win32 in paletted mode and GTK+, each colour
139 * must be allocated before use. The desired colours are held in the ColourDesired class,
140 * and after allocation the allocation entry is stored in the ColourAllocated class. In other
141 * circumstances, such as Win32 in true colour mode, the allocation process just copies
142 * the RGB values from the desired to the allocated class.
143 * As each desired colour requires allocation before it can be used, the ColourPair class
144 * holds both a ColourDesired and a ColourAllocated
145 * The Palette class is responsible for managing the palette of colours which contains a
146 * list of ColourPair objects and performs the allocation.
150 * Holds a desired RGB colour.
152 class ColourDesired
{
155 ColourDesired(long lcol
=0) {
159 ColourDesired(unsigned int red
, unsigned int green
, unsigned int blue
) {
160 Set(red
, green
, blue
);
163 bool operator==(const ColourDesired
&other
) const {
164 return co
== other
.co
;
167 void Set(long lcol
) {
171 void Set(unsigned int red
, unsigned int green
, unsigned int blue
) {
172 co
= red
| (green
<< 8) | (blue
<< 16);
175 static inline unsigned int ValueOfHex(const char ch
) {
176 if (ch
>= '0' && ch
<= '9')
178 else if (ch
>= 'A' && ch
<= 'F')
179 return ch
- 'A' + 10;
180 else if (ch
>= 'a' && ch
<= 'f')
181 return ch
- 'a' + 10;
186 void Set(const char *val
) {
190 unsigned int r
= ValueOfHex(val
[0]) * 16 + ValueOfHex(val
[1]);
191 unsigned int g
= ValueOfHex(val
[2]) * 16 + ValueOfHex(val
[3]);
192 unsigned int b
= ValueOfHex(val
[4]) * 16 + ValueOfHex(val
[5]);
196 long AsLong() const {
200 unsigned int GetRed() {
204 unsigned int GetGreen() {
205 return (co
>> 8) & 0xff;
208 unsigned int GetBlue() {
209 return (co
>> 16) & 0xff;
214 * Holds an allocated RGB colour which may be an approximation to the desired colour.
216 class ColourAllocated
{
221 ColourAllocated(long lcol
=0) {
225 void Set(long lcol
) {
229 long AsLong() const {
235 * Colour pairs hold a desired colour and an allocated colour.
238 ColourDesired desired
;
239 ColourAllocated allocated
;
241 ColourPair(ColourDesired desired_
=ColourDesired(0,0,0)) {
243 allocated
.Set(desired
.AsLong());
246 allocated
.Set(desired
.AsLong());
250 class Window
; // Forward declaration for Palette
253 * Colour palette management.
260 void *allocatedPalette
; // GdkColor *
263 // Private so Palette objects can not be copied
264 Palette(const Palette
&);
265 Palette
&operator=(const Palette
&);
270 bool allowRealization
;
278 * This method either adds a colour to the list of wanted colours (want==true)
279 * or retrieves the allocated colour back to the ColourPair.
280 * This is one method to make it easier to keep the code for wanting and retrieving in sync.
282 void WantFind(ColourPair
&cp
, bool want
);
284 void Allocate(Window
&w
);
296 // Private so Font objects can not be copied
298 Font
&operator=(const Font
&);
303 virtual void Create(const char *faceName
, int characterSet
, int size
,
304 bool bold
, bool italic
, int extraFontFlag
=0);
305 virtual void Release();
307 FontID
GetID() { return fid
; }
308 // Alias another font - caller guarantees not to Release
309 void SetID(FontID fid_
) { fid
= fid_
; }
311 void SetAscent(int ascent_
) { ascent
= ascent_
; }
313 friend class Surface
;
314 friend class SurfaceImpl
;
318 * A surface abstracts a place to draw.
322 // Private so Surface objects can not be copied
323 Surface(const Surface
&) {}
324 Surface
&operator=(const Surface
&) { return *this; }
327 virtual ~Surface() {}
328 static Surface
*Allocate();
330 virtual void Init(WindowID wid
)=0;
331 virtual void Init(SurfaceID sid
, WindowID wid
)=0;
332 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
)=0;
334 virtual void Release()=0;
335 virtual bool Initialised()=0;
336 virtual void PenColour(ColourAllocated fore
)=0;
337 virtual int LogPixelsY()=0;
338 virtual int DeviceHeightFont(int points
)=0;
339 virtual void MoveTo(int x_
, int y_
)=0;
340 virtual void LineTo(int x_
, int y_
)=0;
341 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
)=0;
342 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
343 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
)=0;
344 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
)=0;
345 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
346 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourAllocated fill
, int alphaFill
,
347 ColourAllocated outline
, int alphaOutline
, int flags
)=0;
348 virtual void DrawRGBAImage(PRectangle rc
, int width
, int height
, const unsigned char *pixelsImage
) = 0;
349 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
350 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
)=0;
352 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
)=0;
353 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
)=0;
354 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
)=0;
355 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
)=0;
356 virtual int WidthText(Font
&font_
, const char *s
, int len
)=0;
357 virtual int WidthChar(Font
&font_
, char ch
)=0;
358 virtual int Ascent(Font
&font_
)=0;
359 virtual int Descent(Font
&font_
)=0;
360 virtual int InternalLeading(Font
&font_
)=0;
361 virtual int ExternalLeading(Font
&font_
)=0;
362 virtual int Height(Font
&font_
)=0;
363 virtual int AverageCharWidth(Font
&font_
)=0;
365 virtual int SetPalette(Palette
*pal
, bool inBackGround
)=0;
366 virtual void SetClip(PRectangle rc
)=0;
367 virtual void FlushCachedState()=0;
369 virtual void SetUnicodeMode(bool unicodeMode_
)=0;
370 virtual void SetDBCSMode(int codePage
)=0;
374 * A simple callback action passing one piece of untyped user data.
376 typedef void (*CallBackAction
)(void*);
379 * Class to hide the details of window manipulation.
380 * Does not own the window which will normally have a longer life than this object.
390 Window() : wid(0), cursorLast(cursorInvalid
) {
396 Window(const Window
&source
) : wid(source
.wid
), cursorLast(cursorInvalid
) {
403 Window
&operator=(WindowID wid_
) {
407 WindowID
GetID() const { return wid
; }
408 bool Created() const { return wid
!= 0; }
411 PRectangle
GetPosition();
412 void SetPosition(PRectangle rc
);
413 void SetPositionRelative(PRectangle rc
, Window relativeTo
);
414 PRectangle
GetClientPosition();
415 void Show(bool show
=true);
416 void InvalidateAll();
417 void InvalidateRectangle(PRectangle rc
);
418 virtual void SetFont(Font
&font
);
419 enum Cursor
{ cursorInvalid
, cursorText
, cursorArrow
, cursorUp
, cursorWait
, cursorHoriz
, cursorVert
, cursorReverseArrow
, cursorHand
};
420 void SetCursor(Cursor curs
);
421 void SetTitle(const char *s
);
422 PRectangle
GetMonitorRect(Point pt
);
424 void SetWindow(void *ref
) { windowRef
= ref
; }
425 void SetControl(void *_control
) { control
= _control
; }
432 * Listbox management.
435 class ListBox
: public Window
{
439 static ListBox
*Allocate();
441 virtual void SetFont(Font
&font
)=0;
442 virtual void Create(Window
&parent
, int ctrlID
, Point location
, int lineHeight_
, bool unicodeMode_
)=0;
443 virtual void SetAverageCharWidth(int width
)=0;
444 virtual void SetVisibleRows(int rows
)=0;
445 virtual int GetVisibleRows() const=0;
446 virtual PRectangle
GetDesiredRect()=0;
447 virtual int CaretFromEdge()=0;
448 virtual void Clear()=0;
449 virtual void Append(char *s
, int type
= -1)=0;
450 virtual int Length()=0;
451 virtual void Select(int n
)=0;
452 virtual int GetSelection()=0;
453 virtual int Find(const char *prefix
)=0;
454 virtual void GetValue(int n
, char *value
, int len
)=0;
455 virtual void RegisterImage(int type
, const char *xpm_data
)=0;
456 virtual void RegisterRGBAImage(int type
, int width
, int height
, const unsigned char *pixelsImage
) = 0;
457 virtual void ClearRegisteredImages()=0;
458 virtual void SetDoubleClickAction(CallBackAction
, void *)=0;
459 virtual void SetList(const char* list
, char separator
, char typesep
)=0;
469 MenuID
GetID() { return mid
; }
472 void Show(Point pt
, Window
&w
);
480 double Duration(bool reset
=false);
484 * Dynamic Library (DLL/SO/...) loading
486 class DynamicLibrary
{
488 virtual ~DynamicLibrary() {}
490 /// @return Pointer to function "name", or NULL on failure.
491 virtual Function
FindFunction(const char *name
) = 0;
493 /// @return true if the library was loaded successfully.
494 virtual bool IsValid() = 0;
496 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
497 static DynamicLibrary
*Load(const char *modulePath
);
501 * Platform class used to retrieve system wide parameters such as double click speed
502 * and chrome colour. Not a creatable object, more of a module with several functions.
505 // Private so Platform objects can not be copied
506 Platform(const Platform
&) {}
507 Platform
&operator=(const Platform
&) { return *this; }
509 // Should be private because no new Platforms are ever created
510 // but gcc warns about this
513 static ColourDesired
Chrome();
514 static ColourDesired
ChromeHighlight();
515 static const char *DefaultFont();
516 static int DefaultFontSize();
517 static unsigned int DoubleClickTime();
518 static bool MouseButtonBounce();
519 static void DebugDisplay(const char *s
);
520 static bool IsKeyDown(int key
);
521 static long SendScintilla(
522 WindowID w
, unsigned int msg
, unsigned long wParam
=0, long lParam
=0);
523 static long SendScintillaPointer(
524 WindowID w
, unsigned int msg
, unsigned long wParam
=0, void *lParam
=0);
525 static bool IsDBCSLeadByte(int codePage
, char ch
);
526 static int DBCSCharLength(int codePage
, const char *s
);
527 static int DBCSCharMaxLength();
529 // These are utility functions not really tied to a platform
530 static int Minimum(int a
, int b
);
531 static int Maximum(int a
, int b
);
532 // Next three assume 16 bit shorts and 32 bit longs
533 static long LongFromTwoShorts(short a
,short b
) {
534 return (a
) | ((b
) << 16);
536 static short HighShortFromLong(long x
) {
537 return static_cast<short>(x
>> 16);
539 static short LowShortFromLong(long x
) {
540 return static_cast<short>(x
& 0xffff);
542 static void DebugPrintf(const char *format
, ...);
543 static bool ShowAssertionPopUps(bool assertionPopUps_
);
544 static void Assert(const char *c
, const char *file
, int line
);
545 static int Clamp(int val
, int minVal
, int maxVal
);
549 #define PLATFORM_ASSERT(c) ((void)0)
552 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))
554 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
562 // Shut up annoying Visual C++ warnings:
564 #pragma warning(disable: 4244 4309 4514 4710)