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
36 #if defined(__WIN32__) || defined(_MSC_VER)
38 #define PLAT_GTK_WIN32 1
41 #elif defined(__APPLE__)
56 // Underlying the implementation of the platform classes are platform specific types.
57 // Sometimes these need to be passed around by client code so they are defined here
60 typedef void *SurfaceID
;
61 typedef void *WindowID
;
63 typedef void *TickerID
;
64 typedef void *Function
;
65 typedef void *IdlerID
;
68 * A geometric point class.
69 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
76 explicit Point(int x_
=0, int y_
=0) : x(x_
), y(y_
) {
79 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
81 static Point
FromLong(long lpoint
);
85 * A geometric rectangle class.
86 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
87 * PRectangles contain their top and left sides, but not their right and bottom sides.
96 PRectangle(int left_
=0, int top_
=0, int right_
=0, int bottom_
= 0) :
97 left(left_
), top(top_
), right(right_
), bottom(bottom_
) {
100 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
102 bool operator==(PRectangle
&rc
) {
103 return (rc
.left
== left
) && (rc
.right
== right
) &&
104 (rc
.top
== top
) && (rc
.bottom
== bottom
);
106 bool Contains(Point pt
) {
107 return (pt
.x
>= left
) && (pt
.x
<= right
) &&
108 (pt
.y
>= top
) && (pt
.y
<= bottom
);
110 bool Contains(PRectangle rc
) {
111 return (rc
.left
>= left
) && (rc
.right
<= right
) &&
112 (rc
.top
>= top
) && (rc
.bottom
<= bottom
);
114 bool Intersects(PRectangle other
) {
115 return (right
> other
.left
) && (left
< other
.right
) &&
116 (bottom
> other
.top
) && (top
< other
.bottom
);
118 void Move(int xDelta
, int yDelta
) {
124 int Width() { return right
- left
; }
125 int Height() { return bottom
- top
; }
127 return (Height() <= 0) || (Width() <= 0);
132 * In some circumstances, including Win32 in paletted mode and GTK+, each colour
133 * must be allocated before use. The desired colours are held in the ColourDesired class,
134 * and after allocation the allocation entry is stored in the ColourAllocated class. In other
135 * circumstances, such as Win32 in true colour mode, the allocation process just copies
136 * the RGB values from the desired to the allocated class.
137 * As each desired colour requires allocation before it can be used, the ColourPair class
138 * holds both a ColourDesired and a ColourAllocated
139 * The Palette class is responsible for managing the palette of colours which contains a
140 * list of ColourPair objects and performs the allocation.
144 * Holds a desired RGB colour.
146 class ColourDesired
{
149 ColourDesired(long lcol
=0) {
153 ColourDesired(unsigned int red
, unsigned int green
, unsigned int blue
) {
154 Set(red
, green
, blue
);
157 bool operator==(const ColourDesired
&other
) const {
158 return co
== other
.co
;
161 void Set(long lcol
) {
165 void Set(unsigned int red
, unsigned int green
, unsigned int blue
) {
166 co
= red
| (green
<< 8) | (blue
<< 16);
169 static inline unsigned int ValueOfHex(const char ch
) {
170 if (ch
>= '0' && ch
<= '9')
172 else if (ch
>= 'A' && ch
<= 'F')
173 return ch
- 'A' + 10;
174 else if (ch
>= 'a' && ch
<= 'f')
175 return ch
- 'a' + 10;
180 void Set(const char *val
) {
184 unsigned int r
= ValueOfHex(val
[0]) * 16 + ValueOfHex(val
[1]);
185 unsigned int g
= ValueOfHex(val
[2]) * 16 + ValueOfHex(val
[3]);
186 unsigned int b
= ValueOfHex(val
[4]) * 16 + ValueOfHex(val
[5]);
190 long AsLong() const {
194 unsigned int GetRed() {
198 unsigned int GetGreen() {
199 return (co
>> 8) & 0xff;
202 unsigned int GetBlue() {
203 return (co
>> 16) & 0xff;
208 * Holds an allocated RGB colour which may be an approximation to the desired colour.
210 class ColourAllocated
{
215 ColourAllocated(long lcol
=0) {
219 void Set(long lcol
) {
223 long AsLong() const {
229 * Colour pairs hold a desired colour and an allocated colour.
232 ColourDesired desired
;
233 ColourAllocated allocated
;
235 ColourPair(ColourDesired desired_
=ColourDesired(0,0,0)) {
237 allocated
.Set(desired
.AsLong());
240 allocated
.Set(desired
.AsLong());
244 class Window
; // Forward declaration for Palette
247 * Colour palette management.
254 void *allocatedPalette
; // GdkColor *
257 // Private so Palette objects can not be copied
258 Palette(const Palette
&) {}
259 Palette
&operator=(const Palette
&) { return *this; }
264 bool allowRealization
;
272 * This method either adds a colour to the list of wanted colours (want==true)
273 * or retrieves the allocated colour back to the ColourPair.
274 * This is one method to make it easier to keep the code for wanting and retrieving in sync.
276 void WantFind(ColourPair
&cp
, bool want
);
278 void Allocate(Window
&w
);
290 // Private so Font objects can not be copied
291 Font(const Font
&) {}
292 Font
&operator=(const Font
&) { fid
=0; return *this; }
297 virtual void Create(const char *faceName
, int characterSet
, int size
,
298 bool bold
, bool italic
, int extraFontFlag
=0);
299 virtual void Release();
301 FontID
GetID() { return fid
; }
302 // Alias another font - caller guarantees not to Release
303 void SetID(FontID fid_
) { fid
= fid_
; }
304 friend class Surface
;
305 friend class SurfaceImpl
;
309 * A surface abstracts a place to draw.
313 // Private so Surface objects can not be copied
314 Surface(const Surface
&) {}
315 Surface
&operator=(const Surface
&) { return *this; }
318 virtual ~Surface() {};
319 static Surface
*Allocate();
321 virtual void Init(WindowID wid
)=0;
322 virtual void Init(SurfaceID sid
, WindowID wid
)=0;
323 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
)=0;
325 virtual void Release()=0;
326 virtual bool Initialised()=0;
327 virtual void PenColour(ColourAllocated fore
)=0;
328 virtual int LogPixelsY()=0;
329 virtual int DeviceHeightFont(int points
)=0;
330 virtual void MoveTo(int x_
, int y_
)=0;
331 virtual void LineTo(int x_
, int y_
)=0;
332 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
)=0;
333 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
334 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
)=0;
335 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
)=0;
336 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
337 virtual void AlphaRectangle(PRectangle rc
, int cornerSize
, ColourAllocated fill
, int alphaFill
,
338 ColourAllocated outline
, int alphaOutline
, int flags
)=0;
339 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
340 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
)=0;
342 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
)=0;
343 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
)=0;
344 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
)=0;
345 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
)=0;
346 virtual int WidthText(Font
&font_
, const char *s
, int len
)=0;
347 virtual int WidthChar(Font
&font_
, char ch
)=0;
348 virtual int Ascent(Font
&font_
)=0;
349 virtual int Descent(Font
&font_
)=0;
350 virtual int InternalLeading(Font
&font_
)=0;
351 virtual int ExternalLeading(Font
&font_
)=0;
352 virtual int Height(Font
&font_
)=0;
353 virtual int AverageCharWidth(Font
&font_
)=0;
355 virtual int SetPalette(Palette
*pal
, bool inBackGround
)=0;
356 virtual void SetClip(PRectangle rc
)=0;
357 virtual void FlushCachedState()=0;
359 virtual void SetUnicodeMode(bool unicodeMode_
)=0;
360 virtual void SetDBCSMode(int codePage
)=0;
364 * A simple callback action passing one piece of untyped user data.
366 typedef void (*CallBackAction
)(void*);
369 * Class to hide the details of window manipulation.
370 * Does not own the window which will normally have a longer life than this object.
380 Window() : wid(0), cursorLast(cursorInvalid
) {
386 Window(const Window
&source
) : wid(source
.wid
), cursorLast(cursorInvalid
) {
393 Window
&operator=(WindowID wid_
) {
397 WindowID
GetID() const { return wid
; }
398 bool Created() const { return wid
!= 0; }
401 PRectangle
GetPosition();
402 void SetPosition(PRectangle rc
);
403 void SetPositionRelative(PRectangle rc
, Window relativeTo
);
404 PRectangle
GetClientPosition();
405 void Show(bool show
=true);
406 void InvalidateAll();
407 void InvalidateRectangle(PRectangle rc
);
408 virtual void SetFont(Font
&font
);
409 enum Cursor
{ cursorInvalid
, cursorText
, cursorArrow
, cursorUp
, cursorWait
, cursorHoriz
, cursorVert
, cursorReverseArrow
, cursorHand
};
410 void SetCursor(Cursor curs
);
411 void SetTitle(const char *s
);
412 PRectangle
GetMonitorRect(Point pt
);
414 void SetWindow(void *ref
) { windowRef
= ref
; };
415 void SetControl(void *_control
) { control
= _control
; };
422 * Listbox management.
425 class ListBox
: public Window
{
429 static ListBox
*Allocate();
431 virtual void SetFont(Font
&font
)=0;
432 virtual void Create(Window
&parent
, int ctrlID
, Point location
, int lineHeight_
, bool unicodeMode_
)=0;
433 virtual void SetAverageCharWidth(int width
)=0;
434 virtual void SetVisibleRows(int rows
)=0;
435 virtual int GetVisibleRows() const=0;
436 virtual PRectangle
GetDesiredRect()=0;
437 virtual int CaretFromEdge()=0;
438 virtual void Clear()=0;
439 virtual void Append(char *s
, int type
= -1)=0;
440 virtual int Length()=0;
441 virtual void Select(int n
)=0;
442 virtual int GetSelection()=0;
443 virtual int Find(const char *prefix
)=0;
444 virtual void GetValue(int n
, char *value
, int len
)=0;
445 virtual void RegisterImage(int type
, const char *xpm_data
)=0;
446 virtual void ClearRegisteredImages()=0;
447 virtual void SetDoubleClickAction(CallBackAction
, void *)=0;
448 virtual void SetList(const char* list
, char separator
, char typesep
)=0;
458 MenuID
GetID() { return mid
; }
461 void Show(Point pt
, Window
&w
);
469 double Duration(bool reset
=false);
473 * Dynamic Library (DLL/SO/...) loading
475 class DynamicLibrary
{
477 virtual ~DynamicLibrary() {};
479 /// @return Pointer to function "name", or NULL on failure.
480 virtual Function
FindFunction(const char *name
) = 0;
482 /// @return true if the library was loaded successfully.
483 virtual bool IsValid() = 0;
485 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
486 static DynamicLibrary
*Load(const char *modulePath
);
490 * Platform class used to retrieve system wide parameters such as double click speed
491 * and chrome colour. Not a creatable object, more of a module with several functions.
494 // Private so Platform objects can not be copied
495 Platform(const Platform
&) {}
496 Platform
&operator=(const Platform
&) { return *this; }
498 // Should be private because no new Platforms are ever created
499 // but gcc warns about this
502 static ColourDesired
Chrome();
503 static ColourDesired
ChromeHighlight();
504 static const char *DefaultFont();
505 static int DefaultFontSize();
506 static unsigned int DoubleClickTime();
507 static bool MouseButtonBounce();
508 static void DebugDisplay(const char *s
);
509 static bool IsKeyDown(int key
);
510 static long SendScintilla(
511 WindowID w
, unsigned int msg
, unsigned long wParam
=0, long lParam
=0);
512 static long SendScintillaPointer(
513 WindowID w
, unsigned int msg
, unsigned long wParam
=0, void *lParam
=0);
514 static bool IsDBCSLeadByte(int codePage
, char ch
);
515 static int DBCSCharLength(int codePage
, const char *s
);
516 static int DBCSCharMaxLength();
518 // These are utility functions not really tied to a platform
519 static int Minimum(int a
, int b
);
520 static int Maximum(int a
, int b
);
521 // Next three assume 16 bit shorts and 32 bit longs
522 static long LongFromTwoShorts(short a
,short b
) {
523 return (a
) | ((b
) << 16);
525 static short HighShortFromLong(long x
) {
526 return static_cast<short>(x
>> 16);
528 static short LowShortFromLong(long x
) {
529 return static_cast<short>(x
& 0xffff);
531 static void DebugPrintf(const char *format
, ...);
532 static bool ShowAssertionPopUps(bool assertionPopUps_
);
533 static void Assert(const char *c
, const char *file
, int line
);
534 static int Clamp(int val
, int minVal
, int maxVal
);
538 #define PLATFORM_ASSERT(c) ((void)0)
541 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))
543 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
551 // Shut up annoying Visual C++ warnings:
553 #pragma warning(disable: 4244 4309 4514 4710)