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-2003 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
37 #define PLAT_GTK_WIN32 1
48 #include <wx/object.h> // For the global memory operators, if needed.
52 // Underlying the implementation of the platform classes are platform specific types.
53 // Sometimes these need to be passed around by client code so they are defined here
56 typedef void *SurfaceID
;
57 typedef void *WindowID
;
59 typedef void *TickerID
;
60 typedef void *Function
;
61 typedef void *IdlerID
;
64 * A geometric point class.
65 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
72 Point(int x_
=0, int y_
=0) : x(x_
), y(y_
) {
75 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
77 static Point
FromLong(long lpoint
);
81 * A geometric rectangle class.
82 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
83 * PRectangles contain their top and left sides, but not their right and bottom sides.
92 PRectangle(int left_
=0, int top_
=0, int right_
=0, int bottom_
= 0) :
93 left(left_
), top(top_
), right(right_
), bottom(bottom_
) {
96 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
98 bool operator==(PRectangle
&rc
) {
99 return (rc
.left
== left
) && (rc
.right
== right
) &&
100 (rc
.top
== top
) && (rc
.bottom
== bottom
);
102 bool Contains(Point pt
) {
103 return (pt
.x
>= left
) && (pt
.x
<= right
) &&
104 (pt
.y
>= top
) && (pt
.y
<= bottom
);
106 bool Contains(PRectangle rc
) {
107 return (rc
.left
>= left
) && (rc
.right
<= right
) &&
108 (rc
.top
>= top
) && (rc
.bottom
<= bottom
);
110 bool Intersects(PRectangle other
) {
111 return (right
> other
.left
) && (left
< other
.right
) &&
112 (bottom
> other
.top
) && (top
< other
.bottom
);
114 void Move(int xDelta
, int yDelta
) {
120 int Width() { return right
- left
; }
121 int Height() { return bottom
- top
; }
125 * In some circumstances, including Win32 in paletted mode and GTK+, each colour
126 * must be allocated before use. The desired colours are held in the ColourDesired class,
127 * and after allocation the allocation entry is stored in the ColourAllocated class. In other
128 * circumstances, such as Win32 in true colour mode, the allocation process just copies
129 * the RGB values from the desired to the allocated class.
130 * As each desired colour requires allocation before it can be used, the ColourPair class
131 * holds both a ColourDesired and a ColourAllocated
132 * The Palette class is responsible for managing the palette of colours which contains a
133 * list of ColourPair objects and performs the allocation.
137 * Holds a desired RGB colour.
139 class ColourDesired
{
142 ColourDesired(long lcol
=0) {
146 ColourDesired(unsigned int red
, unsigned int green
, unsigned int blue
) {
147 Set(red
, green
, blue
);
150 bool operator==(const ColourDesired
&other
) const {
151 return co
== other
.co
;
154 void Set(long lcol
) {
158 void Set(unsigned int red
, unsigned int green
, unsigned int blue
) {
159 co
= red
| (green
<< 8) | (blue
<< 16);
162 static inline unsigned int ValueOfHex(const char ch
) {
163 if (ch
>= '0' && ch
<= '9')
165 else if (ch
>= 'A' && ch
<= 'F')
166 return ch
- 'A' + 10;
167 else if (ch
>= 'a' && ch
<= 'f')
168 return ch
- 'a' + 10;
173 void Set(const char *val
) {
177 unsigned int r
= ValueOfHex(val
[0]) * 16 + ValueOfHex(val
[1]);
178 unsigned int g
= ValueOfHex(val
[2]) * 16 + ValueOfHex(val
[3]);
179 unsigned int b
= ValueOfHex(val
[4]) * 16 + ValueOfHex(val
[5]);
183 long AsLong() const {
187 unsigned int GetRed() {
191 unsigned int GetGreen() {
192 return (co
>> 8) & 0xff;
195 unsigned int GetBlue() {
196 return (co
>> 16) & 0xff;
201 * Holds an allocated RGB colour which may be an approximation to the desired colour.
203 class ColourAllocated
{
208 ColourAllocated(long lcol
=0) {
212 void Set(long lcol
) {
216 long AsLong() const {
222 * Colour pairs hold a desired colour and an allocated colour.
225 ColourDesired desired
;
226 ColourAllocated allocated
;
228 ColourPair(ColourDesired desired_
=ColourDesired(0,0,0)) {
230 allocated
.Set(desired
.AsLong());
233 allocated
.Set(desired
.AsLong());
237 class Window
; // Forward declaration for Palette
240 * Colour palette management.
244 enum {numEntries
= 100};
245 ColourPair entries
[numEntries
];
247 void *allocatedPalette
; // GdkColor *
254 bool allowRealization
;
262 * This method either adds a colour to the list of wanted colours (want==true)
263 * or retrieves the allocated colour back to the ColourPair.
264 * This is one method to make it easier to keep the code for wanting and retrieving in sync.
266 void WantFind(ColourPair
&cp
, bool want
);
268 void Allocate(Window
&w
);
280 // Private so Font objects can not be copied
281 Font(const Font
&) {}
282 Font
&operator=(const Font
&) { id
=0; return *this; }
287 virtual void Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
);
288 virtual void Release();
290 FontID
GetID() { return id
; }
291 // Alias another font - caller guarantees not to Release
292 void SetID(FontID id_
) { id
= id_
; }
293 friend class Surface
;
294 friend class SurfaceImpl
;
298 * A surface abstracts a place to draw.
302 // Private so Surface objects can not be copied
303 Surface(const Surface
&) {}
304 Surface
&operator=(const Surface
&) { return *this; }
307 virtual ~Surface() {};
308 static Surface
*Allocate();
310 virtual void Init(WindowID wid
)=0;
311 virtual void Init(SurfaceID sid
, WindowID wid
)=0;
312 virtual void InitPixMap(int width
, int height
, Surface
*surface_
, WindowID wid
)=0;
314 virtual void Release()=0;
315 virtual bool Initialised()=0;
316 virtual void PenColour(ColourAllocated fore
)=0;
317 virtual int LogPixelsY()=0;
318 virtual int DeviceHeightFont(int points
)=0;
319 virtual void MoveTo(int x_
, int y_
)=0;
320 virtual void LineTo(int x_
, int y_
)=0;
321 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
)=0;
322 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
323 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
)=0;
324 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
)=0;
325 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
326 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
327 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
)=0;
329 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
)=0;
330 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
)=0;
331 virtual void DrawTextTransparent(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
)=0;
332 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
)=0;
333 virtual int WidthText(Font
&font_
, const char *s
, int len
)=0;
334 virtual int WidthChar(Font
&font_
, char ch
)=0;
335 virtual int Ascent(Font
&font_
)=0;
336 virtual int Descent(Font
&font_
)=0;
337 virtual int InternalLeading(Font
&font_
)=0;
338 virtual int ExternalLeading(Font
&font_
)=0;
339 virtual int Height(Font
&font_
)=0;
340 virtual int AverageCharWidth(Font
&font_
)=0;
342 virtual int SetPalette(Palette
*pal
, bool inBackGround
)=0;
343 virtual void SetClip(PRectangle rc
)=0;
344 virtual void FlushCachedState()=0;
346 virtual void SetUnicodeMode(bool unicodeMode_
)=0;
347 virtual void SetDBCSMode(int codePage
)=0;
351 * A simple callback action passing one piece of untyped user data.
353 typedef void (*CallBackAction
)(void*);
356 * Class to hide the details of window manipulation.
357 * Does not own the window which will normally have a longer life than this object.
363 Window() : id(0), cursorLast(cursorInvalid
) {}
364 Window(const Window
&source
) : id(source
.id
), cursorLast(cursorInvalid
) {}
366 Window
&operator=(WindowID id_
) {
370 WindowID
GetID() const { return id
; }
371 bool Created() const { return id
!= 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 void SetTitle(const char *s
);
390 * Listbox management.
393 class ListBox
: public Window
{
397 static ListBox
*Allocate();
399 virtual void SetFont(Font
&font
)=0;
400 virtual void Create(Window
&parent
, int ctrlID
, int lineHeight_
, bool unicodeMode_
)=0;
401 virtual void SetAverageCharWidth(int width
)=0;
402 virtual void SetVisibleRows(int rows
)=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 ClearRegisteredImages()=0;
414 virtual void SetDoubleClickAction(CallBackAction
, void *)=0;
424 MenuID
GetID() { return id
; }
427 void Show(Point pt
, Window
&w
);
435 double Duration(bool reset
=false);
439 * Dynamic Library (DLL/SO/...) loading
441 class DynamicLibrary
{
443 virtual ~DynamicLibrary() {};
445 /// @return Pointer to function "name", or NULL on failure.
446 virtual Function
FindFunction(const char *name
) = 0;
448 /// @return true if the library was loaded successfully.
449 virtual bool IsValid() = 0;
451 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
452 static DynamicLibrary
*Load(const char *modulePath
);
456 * Platform class used to retrieve system wide parameters such as double click speed
457 * and chrome colour. Not a creatable object, more of a module with several functions.
460 // Private so Platform objects can not be copied
461 Platform(const Platform
&) {}
462 Platform
&operator=(const Platform
&) { return *this; }
464 // Should be private because no new Platforms are ever created
465 // but gcc warns about this
468 static ColourDesired
Chrome();
469 static ColourDesired
ChromeHighlight();
470 static const char *DefaultFont();
471 static int DefaultFontSize();
472 static unsigned int DoubleClickTime();
473 static bool MouseButtonBounce();
474 static void DebugDisplay(const char *s
);
475 static bool IsKeyDown(int key
);
476 static long SendScintilla(
477 WindowID w
, unsigned int msg
, unsigned long wParam
=0, long lParam
=0);
478 static long SendScintillaPointer(
479 WindowID w
, unsigned int msg
, unsigned long wParam
=0, void *lParam
=0);
480 static bool IsDBCSLeadByte(int codePage
, char ch
);
481 static int DBCSCharLength(int codePage
, const char *s
);
482 static int DBCSCharMaxLength();
484 // These are utility functions not really tied to a platform
485 static int Minimum(int a
, int b
);
486 static int Maximum(int a
, int b
);
487 // Next three assume 16 bit shorts and 32 bit longs
488 static long LongFromTwoShorts(short a
,short b
) {
489 return (a
) | ((b
) << 16);
491 static short HighShortFromLong(long x
) {
492 return static_cast<short>(x
>> 16);
494 static short LowShortFromLong(long x
) {
495 return static_cast<short>(x
& 0xffff);
497 static void DebugPrintf(const char *format
, ...);
498 static bool ShowAssertionPopUps(bool assertionPopUps_
);
499 static void Assert(const char *c
, const char *file
, int line
);
500 static int Clamp(int val
, int minVal
, int maxVal
);
504 #define PLATFORM_ASSERT(c) ((void)0)
506 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
509 // Shut up annoying Visual C++ warnings:
511 #pragma warning(disable: 4244 4309 4514 4710)