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
47 // Underlying the implementation of the platform classes are platform specific types.
48 // Sometimes these need to be passed around by client code so they are defined here
51 typedef void *SurfaceID
;
52 typedef void *WindowID
;
54 typedef void *TickerID
;
57 * A geometric point class.
58 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
65 Point(int x_
=0, int y_
=0) : x(x_
), y(y_
) {
68 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
70 static Point
FromLong(long lpoint
);
74 * A geometric rectangle class.
75 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
76 * PRectangles contain their top and left sides, but not their right and bottom sides.
85 PRectangle(int left_
=0, int top_
=0, int right_
=0, int bottom_
= 0) :
86 left(left_
), top(top_
), right(right_
), bottom(bottom_
) {
89 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
91 bool operator==(PRectangle
&rc
) {
92 return (rc
.left
== left
) && (rc
.right
== right
) &&
93 (rc
.top
== top
) && (rc
.bottom
== bottom
);
95 bool Contains(Point pt
) {
96 return (pt
.x
>= left
) && (pt
.x
<= right
) &&
97 (pt
.y
>= top
) && (pt
.y
<= bottom
);
99 bool Contains(PRectangle rc
) {
100 return (rc
.left
>= left
) && (rc
.right
<= right
) &&
101 (rc
.top
>= top
) && (rc
.bottom
<= bottom
);
103 bool Intersects(PRectangle other
) {
104 return (right
>= other
.left
) && (left
<= other
.right
) &&
105 (bottom
>= other
.top
) && (top
<= other
.bottom
);
107 int Width() { return right
- left
; }
108 int Height() { return bottom
- top
; }
112 * In some circumstances, including Win32 in paletted mode and GTK+, each colour
113 * must be allocated before use. The desired colours are held in the ColourDesired class,
114 * and after allocation the allocation entry is stored in the ColourAllocated class. In other
115 * circumstances, such as Win32 in true colour mode, the allocation process just copies
116 * the RGB values from the desired to the allocated class.
117 * As each desired colour requires allocation before it can be used, the ColourPair class
118 * holds both a ColourDesired and a ColourAllocated
119 * The Palette class is responsible for managing the palette of colours which contains a
120 * list of ColourPair objects and performs the allocation.
124 * Holds a desired RGB colour.
126 class ColourDesired
{
129 ColourDesired(long lcol
=0) {
133 ColourDesired(unsigned int red
, unsigned int green
, unsigned int blue
) {
134 co
= red
| (green
<< 8) | (blue
<< 16);
137 bool operator==(const ColourDesired
&other
) const {
138 return co
== other
.co
;
141 void Set(long lcol
) {
145 long AsLong() const {
149 unsigned int GetRed() {
153 unsigned int GetGreen() {
154 return (co
>> 8) & 0xff;
157 unsigned int GetBlue() {
158 return (co
>> 16) & 0xff;
163 * Holds an allocated RGB colour which may be an approximation to the desired colour.
165 class ColourAllocated
{
170 ColourAllocated(long lcol
=0) {
174 void Set(long lcol
) {
178 long AsLong() const {
184 * Colour pairs hold a desired colour and an allocated colour.
187 ColourDesired desired
;
188 ColourAllocated allocated
;
190 ColourPair(ColourDesired desired_
=ColourDesired(0,0,0)) {
192 allocated
.Set(desired
.AsLong());
196 class Window
; // Forward declaration for Palette
199 * Colour palette management.
203 enum {numEntries
= 100};
204 ColourPair entries
[numEntries
];
206 void *allocatedPalette
; // GdkColor *
213 bool allowRealization
;
221 * This method either adds a colour to the list of wanted colours (want==true)
222 * or retrieves the allocated colour back to the ColourPair.
223 * This is one method to make it easier to keep the code for wanting and retrieving in sync.
225 void WantFind(ColourPair
&cp
, bool want
);
227 void Allocate(Window
&w
);
239 // Private so Font objects can not be copied
240 Font(const Font
&) {}
241 Font
&operator=(const Font
&) { id
=0; return *this; }
246 virtual void Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
);
247 virtual void Release();
249 FontID
GetID() { return id
; }
250 // Alias another font - caller guarantees not to Release
251 void SetID(FontID id_
) { id
= id_
; }
252 friend class Surface
;
253 friend class SurfaceImpl
;
257 * A surface abstracts a place to draw.
261 // Private so Surface objects can not be copied
262 Surface(const Surface
&) {}
263 Surface
&operator=(const Surface
&) { return *this; }
266 virtual ~Surface() {};
267 static Surface
*Allocate();
269 virtual void Init()=0;
270 virtual void Init(SurfaceID sid
)=0;
271 virtual void InitPixMap(int width
, int height
, Surface
*surface_
)=0;
273 virtual void Release()=0;
274 virtual bool Initialised()=0;
275 virtual void PenColour(ColourAllocated fore
)=0;
276 virtual int LogPixelsY()=0;
277 virtual int DeviceHeightFont(int points
)=0;
278 virtual void MoveTo(int x_
, int y_
)=0;
279 virtual void LineTo(int x_
, int y_
)=0;
280 virtual void Polygon(Point
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
)=0;
281 virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
282 virtual void FillRectangle(PRectangle rc
, ColourAllocated back
)=0;
283 virtual void FillRectangle(PRectangle rc
, Surface
&surfacePattern
)=0;
284 virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
285 virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0;
286 virtual void Copy(PRectangle rc
, Point from
, Surface
&surfaceSource
)=0;
288 virtual void DrawTextNoClip(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
)=0;
289 virtual void DrawTextClipped(PRectangle rc
, Font
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
)=0;
290 virtual void MeasureWidths(Font
&font_
, const char *s
, int len
, int *positions
)=0;
291 virtual int WidthText(Font
&font_
, const char *s
, int len
)=0;
292 virtual int WidthChar(Font
&font_
, char ch
)=0;
293 virtual int Ascent(Font
&font_
)=0;
294 virtual int Descent(Font
&font_
)=0;
295 virtual int InternalLeading(Font
&font_
)=0;
296 virtual int ExternalLeading(Font
&font_
)=0;
297 virtual int Height(Font
&font_
)=0;
298 virtual int AverageCharWidth(Font
&font_
)=0;
300 virtual int SetPalette(Palette
*pal
, bool inBackGround
)=0;
301 virtual void SetClip(PRectangle rc
)=0;
302 virtual void FlushCachedState()=0;
304 virtual void SetUnicodeMode(bool unicodeMode_
)=0;
308 * A simple callback action passing one piece of untyped user data.
310 typedef void (*CallBackAction
)(void*);
313 * Class to hide the details of window manipulation.
314 * Does not own the window which will normally have a longer life than this object.
320 Window() : id(0), cursorLast(cursorInvalid
) {}
321 Window(const Window
&source
) : id(source
.id
), cursorLast(cursorInvalid
) {}
323 Window
&operator=(WindowID id_
) {
327 WindowID
GetID() { return id
; }
328 bool Created() { return id
!= 0; }
331 PRectangle
GetPosition();
332 void SetPosition(PRectangle rc
);
333 void SetPositionRelative(PRectangle rc
, Window relativeTo
);
334 PRectangle
GetClientPosition();
335 void Show(bool show
=true);
336 void InvalidateAll();
337 void InvalidateRectangle(PRectangle rc
);
338 virtual void SetFont(Font
&font
);
339 enum Cursor
{ cursorInvalid
, cursorText
, cursorArrow
, cursorUp
, cursorWait
, cursorHoriz
, cursorVert
, cursorReverseArrow
};
340 void SetCursor(Cursor curs
);
341 void SetTitle(const char *s
);
347 * Listbox management.
350 class ListBox
: public Window
{
357 int desiredVisibleRows
;
358 unsigned int maxItemCharacters
;
359 unsigned int aveCharWidth
;
361 CallBackAction doubleClickAction
;
362 void *doubleClickActionData
;
366 void Create(Window
&parent
, int ctrlID
);
367 virtual void SetFont(Font
&font
);
368 void SetAverageCharWidth(int width
);
369 void SetVisibleRows(int rows
);
370 PRectangle
GetDesiredRect();
372 void Append(char *s
);
376 int Find(const char *prefix
);
377 void GetValue(int n
, char *value
, int len
);
379 void SetDoubleClickAction(CallBackAction action
, void *data
) {
380 doubleClickAction
= action
;
381 doubleClickActionData
= data
;
392 MenuID
GetID() { return id
; }
395 void Show(Point pt
, Window
&w
);
403 double Duration(bool reset
=false);
407 * Platform class used to retrieve system wide parameters such as double click speed
408 * and chrome colour. Not a creatable object, more of a module with several functions.
411 // Private so Platform objects can not be copied
412 Platform(const Platform
&) {}
413 Platform
&operator=(const Platform
&) { return *this; }
415 // Should be private because no new Platforms are ever created
416 // but gcc warns about this
419 static ColourDesired
Chrome();
420 static ColourDesired
ChromeHighlight();
421 static const char *DefaultFont();
422 static int DefaultFontSize();
423 static unsigned int DoubleClickTime();
424 static void DebugDisplay(const char *s
);
425 static bool IsKeyDown(int key
);
426 static long SendScintilla(
427 WindowID w
, unsigned int msg
, unsigned long wParam
=0, long lParam
=0);
428 static bool IsDBCSLeadByte(int codePage
, char ch
);
430 // These are utility functions not really tied to a platform
431 static int Minimum(int a
, int b
);
432 static int Maximum(int a
, int b
);
433 // Next three assume 16 bit shorts and 32 bit longs
434 static long LongFromTwoShorts(short a
,short b
) {
435 return (a
) | ((b
) << 16);
437 static short HighShortFromLong(long x
) {
438 return static_cast<short>(x
>> 16);
440 static short LowShortFromLong(long x
) {
441 return static_cast<short>(x
& 0xffff);
443 static void DebugPrintf(const char *format
, ...);
444 static bool ShowAssertionPopUps(bool assertionPopUps_
);
445 static void Assert(const char *c
, const char *file
, int line
);
446 static int Clamp(int val
, int minVal
, int maxVal
);
450 #define PLATFORM_ASSERT(c) ((void)0)
452 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
455 // Shut up annoying Visual C++ warnings:
457 #pragma warning(disable: 4244 4309 4514 4710)