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-2002 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
;
62 * A geometric point class.
63 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
70 Point(int x_
=0, int y_
=0) : x(x_
), y(y_
) {
73 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
75 static Point
FromLong(long lpoint
);
79 * A geometric rectangle class.
80 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
81 * PRectangles contain their top and left sides, but not their right and bottom sides.
90 PRectangle(int left_
=0, int top_
=0, int right_
=0, int bottom_
= 0) :
91 left(left_
), top(top_
), right(right_
), bottom(bottom_
) {
94 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
96 bool operator==(PRectangle
&rc
) {
97 return (rc
.left
== left
) && (rc
.right
== right
) &&
98 (rc
.top
== top
) && (rc
.bottom
== bottom
);
100 bool Contains(Point pt
) {
101 return (pt
.x
>= left
) && (pt
.x
<= right
) &&
102 (pt
.y
>= top
) && (pt
.y
<= bottom
);
104 bool Contains(PRectangle rc
) {
105 return (rc
.left
>= left
) && (rc
.right
<= right
) &&
106 (rc
.top
>= top
) && (rc
.bottom
<= bottom
);
108 bool Intersects(PRectangle other
) {
109 return (right
>= other
.left
) && (left
<= other
.right
) &&
110 (bottom
>= other
.top
) && (top
<= other
.bottom
);
112 int Width() { return right
- left
; }
113 int Height() { return bottom
- top
; }
117 * In some circumstances, including Win32 in paletted mode and GTK+, each colour
118 * must be allocated before use. The desired colours are held in the ColourDesired class,
119 * and after allocation the allocation entry is stored in the ColourAllocated class. In other
120 * circumstances, such as Win32 in true colour mode, the allocation process just copies
121 * the RGB values from the desired to the allocated class.
122 * As each desired colour requires allocation before it can be used, the ColourPair class
123 * holds both a ColourDesired and a ColourAllocated
124 * The Palette class is responsible for managing the palette of colours which contains a
125 * list of ColourPair objects and performs the allocation.
129 * Holds a desired RGB colour.
131 class ColourDesired
{
134 ColourDesired(long lcol
=0) {
138 ColourDesired(unsigned int red
, unsigned int green
, unsigned int blue
) {
139 co
= red
| (green
<< 8) | (blue
<< 16);
142 bool operator==(const ColourDesired
&other
) const {
143 return co
== other
.co
;
146 void Set(long lcol
) {
150 long AsLong() const {
154 unsigned int GetRed() {
158 unsigned int GetGreen() {
159 return (co
>> 8) & 0xff;
162 unsigned int GetBlue() {
163 return (co
>> 16) & 0xff;
168 * Holds an allocated RGB colour which may be an approximation to the desired colour.
170 class ColourAllocated
{
175 ColourAllocated(long lcol
=0) {
179 void Set(long lcol
) {
183 long AsLong() const {
189 * Colour pairs hold a desired colour and an allocated colour.
192 ColourDesired desired
;
193 ColourAllocated allocated
;
195 ColourPair(ColourDesired desired_
=ColourDesired(0,0,0)) {
197 allocated
.Set(desired
.AsLong());
201 class Window
; // Forward declaration for Palette
204 * Colour palette management.
208 enum {numEntries
= 100};
209 ColourPair entries
[numEntries
];
211 void *allocatedPalette
; // GdkColor *
218 bool allowRealization
;
226 * This method either adds a colour to the list of wanted colours (want==true)
227 * or retrieves the allocated colour back to the ColourPair.
228 * This is one method to make it easier to keep the code for wanting and retrieving in sync.
230 void WantFind(ColourPair
&cp
, bool want
);
232 void Allocate(Window
&w
);
244 // Private so Font objects can not be copied
245 Font(const Font
&) {}
246 Font
&operator=(const Font
&) { id
=0; return *this; }
251 virtual void Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
);
252 virtual void Release();
254 FontID
GetID() { return id
; }
255 // Alias another font - caller guarantees not to Release
256 void SetID(FontID id_
) { id
= id_
; }
257 friend class Surface
;
258 friend class SurfaceImpl
;
262 * A surface abstracts a place to draw.
266 // Private so Surface objects can not be copied
267 Surface(const Surface
&) {}
268 Surface
&operator=(const Surface
&) { return *this; }
271 virtual ~Surface() {};
272 static Surface
*Allocate();
274 virtual void Init()=0;
275 virtual void Init(SurfaceID sid
)=0;
276 virtual void InitPixMap(int width
, int height
, Surface
*surface_
)=0;
278 virtual void Release()=0;
279 virtual bool Initialised()=0;
280 virtual void PenColour(ColourAllocated fore
)=0;
281 virtual int LogPixelsY()=0;
282 virtual int DeviceHeightFont(int points
)=0;
283 virtual void MoveTo(int x_
, int y_
)=0;
284 virtual void LineTo(int x_
, int y_
)=0;
285 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
)=0;
286 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
287 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
)=0;
288 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
)=0;
289 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
290 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
291 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
)=0;
293 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
)=0;
294 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
)=0;
295 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
)=0;
296 virtual int WidthText(Font
&font_
, const char *s
, int len
)=0;
297 virtual int WidthChar(Font
&font_
, char ch
)=0;
298 virtual int Ascent(Font
&font_
)=0;
299 virtual int Descent(Font
&font_
)=0;
300 virtual int InternalLeading(Font
&font_
)=0;
301 virtual int ExternalLeading(Font
&font_
)=0;
302 virtual int Height(Font
&font_
)=0;
303 virtual int AverageCharWidth(Font
&font_
)=0;
305 virtual int SetPalette(Palette
*pal
, bool inBackGround
)=0;
306 virtual void SetClip(PRectangle rc
)=0;
307 virtual void FlushCachedState()=0;
309 virtual void SetUnicodeMode(bool unicodeMode_
)=0;
313 * A simple callback action passing one piece of untyped user data.
315 typedef void (*CallBackAction
)(void*);
318 * Class to hide the details of window manipulation.
319 * Does not own the window which will normally have a longer life than this object.
325 Window() : id(0), cursorLast(cursorInvalid
) {}
326 Window(const Window
&source
) : id(source
.id
), cursorLast(cursorInvalid
) {}
328 Window
&operator=(WindowID id_
) {
332 WindowID
GetID() { return id
; }
333 bool Created() { return id
!= 0; }
336 PRectangle
GetPosition();
337 void SetPosition(PRectangle rc
);
338 void SetPositionRelative(PRectangle rc
, Window relativeTo
);
339 PRectangle
GetClientPosition();
340 void Show(bool show
=true);
341 void InvalidateAll();
342 void InvalidateRectangle(PRectangle rc
);
343 virtual void SetFont(Font
&font
);
344 enum Cursor
{ cursorInvalid
, cursorText
, cursorArrow
, cursorUp
, cursorWait
, cursorHoriz
, cursorVert
, cursorReverseArrow
};
345 void SetCursor(Cursor curs
);
346 void SetTitle(const char *s
);
352 * Listbox management.
355 class ListBox
: public Window
{
362 int desiredVisibleRows
;
363 unsigned int maxItemCharacters
;
364 unsigned int aveCharWidth
;
366 CallBackAction doubleClickAction
;
367 void *doubleClickActionData
;
371 void Create(Window
&parent
, int ctrlID
);
372 virtual void SetFont(Font
&font
);
373 void SetAverageCharWidth(int width
);
374 void SetVisibleRows(int rows
);
375 PRectangle
GetDesiredRect();
377 void Append(char *s
);
381 int Find(const char *prefix
);
382 void GetValue(int n
, char *value
, int len
);
384 void SetDoubleClickAction(CallBackAction action
, void *data
) {
385 doubleClickAction
= action
;
386 doubleClickActionData
= data
;
397 MenuID
GetID() { return id
; }
400 void Show(Point pt
, Window
&w
);
408 double Duration(bool reset
=false);
412 * Platform class used to retrieve system wide parameters such as double click speed
413 * and chrome colour. Not a creatable object, more of a module with several functions.
416 // Private so Platform objects can not be copied
417 Platform(const Platform
&) {}
418 Platform
&operator=(const Platform
&) { return *this; }
420 // Should be private because no new Platforms are ever created
421 // but gcc warns about this
424 static ColourDesired
Chrome();
425 static ColourDesired
ChromeHighlight();
426 static const char *DefaultFont();
427 static int DefaultFontSize();
428 static unsigned int DoubleClickTime();
429 static void DebugDisplay(const char *s
);
430 static bool IsKeyDown(int key
);
431 static long SendScintilla(
432 WindowID w
, unsigned int msg
, unsigned long wParam
=0, long lParam
=0);
433 static bool IsDBCSLeadByte(int codePage
, char ch
);
435 // These are utility functions not really tied to a platform
436 static int Minimum(int a
, int b
);
437 static int Maximum(int a
, int b
);
438 // Next three assume 16 bit shorts and 32 bit longs
439 static long LongFromTwoShorts(short a
,short b
) {
440 return (a
) | ((b
) << 16);
442 static short HighShortFromLong(long x
) {
443 return static_cast<short>(x
>> 16);
445 static short LowShortFromLong(long x
) {
446 return static_cast<short>(x
& 0xffff);
448 static void DebugPrintf(const char *format
, ...);
449 static bool ShowAssertionPopUps(bool assertionPopUps_
);
450 static void Assert(const char *c
, const char *file
, int line
);
451 static int Clamp(int val
, int minVal
, int maxVal
);
455 #define PLATFORM_ASSERT(c) ((void)0)
457 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
460 // Shut up annoying Visual C++ warnings:
462 #pragma warning(disable: 4244 4309 4514 4710)