1 // Scintilla source code edit control
\r
3 ** Interface to platform facilities. Also includes some basic utilities.
\r
4 ** Implemented in PlatGTK.cxx for GTK+/Linux, PlatWin.cxx for Windows, and PlatWX.cxx for wxWindows.
\r
6 // Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
\r
7 // The License.txt file describes the conditions under which this software may be distributed.
\r
12 // PLAT_GTK = GTK+ on Linux or Win32
\r
13 // PLAT_GTK_WIN32 is defined additionally when running PLAT_GTK under Win32
\r
14 // PLAT_WIN = Win32 API on Win32 OS
\r
15 // PLAT_WX is wxWindows on any supported platform
\r
18 #define PLAT_GTK_WIN32 0
\r
19 #define PLAT_MACOSX 0
\r
28 #elif defined(__WX__)
\r
36 #if defined(__WIN32__) || defined(_MSC_VER)
\r
37 #undef PLAT_GTK_WIN32
\r
38 #define PLAT_GTK_WIN32 1
\r
41 #elif defined(MACOSX)
\r
43 #define PLAT_MACOSX 1
\r
51 #ifdef SCI_NAMESPACE
\r
52 namespace Scintilla {
\r
55 // Underlying the implementation of the platform classes are platform specific types.
\r
56 // Sometimes these need to be passed around by client code so they are defined here
\r
58 typedef void *FontID;
\r
59 typedef void *SurfaceID;
\r
60 typedef void *WindowID;
\r
61 typedef void *MenuID;
\r
62 typedef void *TickerID;
\r
63 typedef void *Function;
\r
64 typedef void *IdlerID;
\r
67 * A geometric point class.
\r
68 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably.
\r
75 explicit Point(int x_=0, int y_=0) : x(x_), y(y_) {
\r
78 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
\r
80 static Point FromLong(long lpoint);
\r
84 * A geometric rectangle class.
\r
85 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably.
\r
86 * PRectangles contain their top and left sides, but not their right and bottom sides.
\r
95 PRectangle(int left_=0, int top_=0, int right_=0, int bottom_ = 0) :
\r
96 left(left_), top(top_), right(right_), bottom(bottom_) {
\r
99 // Other automatically defined methods (assignment, copy constructor, destructor) are fine
\r
101 bool operator==(PRectangle &rc) {
\r
102 return (rc.left == left) && (rc.right == right) &&
\r
103 (rc.top == top) && (rc.bottom == bottom);
\r
105 bool Contains(Point pt) {
\r
106 return (pt.x >= left) && (pt.x <= right) &&
\r
107 (pt.y >= top) && (pt.y <= bottom);
\r
109 bool Contains(PRectangle rc) {
\r
110 return (rc.left >= left) && (rc.right <= right) &&
\r
111 (rc.top >= top) && (rc.bottom <= bottom);
\r
113 bool Intersects(PRectangle other) {
\r
114 return (right > other.left) && (left < other.right) &&
\r
115 (bottom > other.top) && (top < other.bottom);
\r
117 void Move(int xDelta, int yDelta) {
\r
123 int Width() { return right - left; }
\r
124 int Height() { return bottom - top; }
\r
126 return (Height() <= 0) || (Width() <= 0);
\r
131 * In some circumstances, including Win32 in paletted mode and GTK+, each colour
\r
132 * must be allocated before use. The desired colours are held in the ColourDesired class,
\r
133 * and after allocation the allocation entry is stored in the ColourAllocated class. In other
\r
134 * circumstances, such as Win32 in true colour mode, the allocation process just copies
\r
135 * the RGB values from the desired to the allocated class.
\r
136 * As each desired colour requires allocation before it can be used, the ColourPair class
\r
137 * holds both a ColourDesired and a ColourAllocated
\r
138 * The Palette class is responsible for managing the palette of colours which contains a
\r
139 * list of ColourPair objects and performs the allocation.
\r
143 * Holds a desired RGB colour.
\r
145 class ColourDesired {
\r
148 ColourDesired(long lcol=0) {
\r
152 ColourDesired(unsigned int red, unsigned int green, unsigned int blue) {
\r
153 Set(red, green, blue);
\r
156 bool operator==(const ColourDesired &other) const {
\r
157 return co == other.co;
\r
160 void Set(long lcol) {
\r
164 void Set(unsigned int red, unsigned int green, unsigned int blue) {
\r
165 co = red | (green << 8) | (blue << 16);
\r
168 static inline unsigned int ValueOfHex(const char ch) {
\r
169 if (ch >= '0' && ch <= '9')
\r
171 else if (ch >= 'A' && ch <= 'F')
\r
172 return ch - 'A' + 10;
\r
173 else if (ch >= 'a' && ch <= 'f')
\r
174 return ch - 'a' + 10;
\r
179 void Set(const char *val) {
\r
183 unsigned int r = ValueOfHex(val[0]) * 16 + ValueOfHex(val[1]);
\r
184 unsigned int g = ValueOfHex(val[2]) * 16 + ValueOfHex(val[3]);
\r
185 unsigned int b = ValueOfHex(val[4]) * 16 + ValueOfHex(val[5]);
\r
189 long AsLong() const {
\r
193 unsigned int GetRed() {
\r
197 unsigned int GetGreen() {
\r
198 return (co >> 8) & 0xff;
\r
201 unsigned int GetBlue() {
\r
202 return (co >> 16) & 0xff;
\r
207 * Holds an allocated RGB colour which may be an approximation to the desired colour.
\r
209 class ColourAllocated {
\r
214 ColourAllocated(long lcol=0) {
\r
215 coAllocated = lcol;
\r
218 void Set(long lcol) {
\r
219 coAllocated = lcol;
\r
222 long AsLong() const {
\r
223 return coAllocated;
\r
228 * Colour pairs hold a desired colour and an allocated colour.
\r
230 struct ColourPair {
\r
231 ColourDesired desired;
\r
232 ColourAllocated allocated;
\r
234 ColourPair(ColourDesired desired_=ColourDesired(0,0,0)) {
\r
235 desired = desired_;
\r
236 allocated.Set(desired.AsLong());
\r
239 allocated.Set(desired.AsLong());
\r
243 class Window; // Forward declaration for Palette
\r
246 * Colour palette management.
\r
251 ColourPair *entries;
\r
253 void *allocatedPalette; // GdkColor *
\r
256 // Private so Palette objects can not be copied
\r
257 Palette(const Palette &) {}
\r
258 Palette &operator=(const Palette &) { return *this; }
\r
263 bool allowRealization;
\r
271 * This method either adds a colour to the list of wanted colours (want==true)
\r
272 * or retrieves the allocated colour back to the ColourPair.
\r
273 * This is one method to make it easier to keep the code for wanting and retrieving in sync.
\r
275 void WantFind(ColourPair &cp, bool want);
\r
277 void Allocate(Window &w);
\r
289 // Private so Font objects can not be copied
\r
290 Font(const Font &) {}
\r
291 Font &operator=(const Font &) { id=0; return *this; }
\r
296 virtual void Create(const char *faceName, int characterSet, int size,
\r
297 bool bold, bool italic, bool extraFontFlag=false);
\r
298 virtual void Release();
\r
300 FontID GetID() { return id; }
\r
301 // Alias another font - caller guarantees not to Release
\r
302 void SetID(FontID id_) { id = id_; }
\r
303 friend class Surface;
\r
304 friend class SurfaceImpl;
\r
308 * A surface abstracts a place to draw.
\r
312 // Private so Surface objects can not be copied
\r
313 Surface(const Surface &) {}
\r
314 Surface &operator=(const Surface &) { return *this; }
\r
317 virtual ~Surface() {};
\r
318 static Surface *Allocate();
\r
320 virtual void Init(WindowID wid)=0;
\r
321 virtual void Init(SurfaceID sid, WindowID wid)=0;
\r
322 virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid)=0;
\r
324 virtual void Release()=0;
\r
325 virtual bool Initialised()=0;
\r
326 virtual void PenColour(ColourAllocated fore)=0;
\r
327 virtual int LogPixelsY()=0;
\r
328 virtual int DeviceHeightFont(int points)=0;
\r
329 virtual void MoveTo(int x_, int y_)=0;
\r
330 virtual void LineTo(int x_, int y_)=0;
\r
331 virtual void Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back)=0;
\r
332 virtual void RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0;
\r
333 virtual void FillRectangle(PRectangle rc, ColourAllocated back)=0;
\r
334 virtual void FillRectangle(PRectangle rc, Surface &surfacePattern)=0;
\r
335 virtual void RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0;
\r
336 virtual void AlphaRectangle(PRectangle rc, int cornerSize, ColourAllocated fill, int alphaFill,
\r
337 ColourAllocated outline, int alphaOutline, int flags)=0;
\r
338 virtual void Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0;
\r
339 virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource)=0;
\r
341 virtual void DrawTextNoClip(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back)=0;
\r
342 virtual void DrawTextClipped(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back)=0;
\r
343 virtual void DrawTextTransparent(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore)=0;
\r
344 virtual void MeasureWidths(Font &font_, const char *s, int len, int *positions)=0;
\r
345 virtual int WidthText(Font &font_, const char *s, int len)=0;
\r
346 virtual int WidthChar(Font &font_, char ch)=0;
\r
347 virtual int Ascent(Font &font_)=0;
\r
348 virtual int Descent(Font &font_)=0;
\r
349 virtual int InternalLeading(Font &font_)=0;
\r
350 virtual int ExternalLeading(Font &font_)=0;
\r
351 virtual int Height(Font &font_)=0;
\r
352 virtual int AverageCharWidth(Font &font_)=0;
\r
354 virtual int SetPalette(Palette *pal, bool inBackGround)=0;
\r
355 virtual void SetClip(PRectangle rc)=0;
\r
356 virtual void FlushCachedState()=0;
\r
358 virtual void SetUnicodeMode(bool unicodeMode_)=0;
\r
359 virtual void SetDBCSMode(int codePage)=0;
\r
363 * A simple callback action passing one piece of untyped user data.
\r
365 typedef void (*CallBackAction)(void*);
\r
368 * Class to hide the details of window manipulation.
\r
369 * Does not own the window which will normally have a longer life than this object.
\r
379 Window() : id(0), cursorLast(cursorInvalid) {
\r
385 Window(const Window &source) : id(source.id), cursorLast(cursorInvalid) {
\r
392 Window &operator=(WindowID id_) {
\r
396 WindowID GetID() const { return id; }
\r
397 bool Created() const { return id != 0; }
\r
400 PRectangle GetPosition();
\r
401 void SetPosition(PRectangle rc);
\r
402 void SetPositionRelative(PRectangle rc, Window relativeTo);
\r
403 PRectangle GetClientPosition();
\r
404 void Show(bool show=true);
\r
405 void InvalidateAll();
\r
406 void InvalidateRectangle(PRectangle rc);
\r
407 virtual void SetFont(Font &font);
\r
408 enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand };
\r
409 void SetCursor(Cursor curs);
\r
410 void SetTitle(const char *s);
\r
411 PRectangle GetMonitorRect(Point pt);
\r
413 void SetWindow(void *ref) { windowRef = ref; };
\r
414 void SetControl(void *_control) { control = _control; };
\r
421 * Listbox management.
\r
424 class ListBox : public Window {
\r
427 virtual ~ListBox();
\r
428 static ListBox *Allocate();
\r
430 virtual void SetFont(Font &font)=0;
\r
431 virtual void Create(Window &parent, int ctrlID, Point location, int lineHeight_, bool unicodeMode_)=0;
\r
432 virtual void SetAverageCharWidth(int width)=0;
\r
433 virtual void SetVisibleRows(int rows)=0;
\r
434 virtual int GetVisibleRows() const=0;
\r
435 virtual PRectangle GetDesiredRect()=0;
\r
436 virtual int CaretFromEdge()=0;
\r
437 virtual void Clear()=0;
\r
438 virtual void Append(char *s, int type = -1)=0;
\r
439 virtual int Length()=0;
\r
440 virtual void Select(int n)=0;
\r
441 virtual int GetSelection()=0;
\r
442 virtual int Find(const char *prefix)=0;
\r
443 virtual void GetValue(int n, char *value, int len)=0;
\r
444 virtual void RegisterImage(int type, const char *xpm_data)=0;
\r
445 virtual void ClearRegisteredImages()=0;
\r
446 virtual void SetDoubleClickAction(CallBackAction, void *)=0;
\r
447 virtual void SetList(const char* list, char separator, char typesep)=0;
\r
457 MenuID GetID() { return id; }
\r
458 void CreatePopUp();
\r
460 void Show(Point pt, Window &w);
\r
463 class ElapsedTime {
\r
468 double Duration(bool reset=false);
\r
472 * Dynamic Library (DLL/SO/...) loading
\r
474 class DynamicLibrary {
\r
476 virtual ~DynamicLibrary() {};
\r
478 /// @return Pointer to function "name", or NULL on failure.
\r
479 virtual Function FindFunction(const char *name) = 0;
\r
481 /// @return true if the library was loaded successfully.
\r
482 virtual bool IsValid() = 0;
\r
484 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded.
\r
485 static DynamicLibrary *Load(const char *modulePath);
\r
489 * Platform class used to retrieve system wide parameters such as double click speed
\r
490 * and chrome colour. Not a creatable object, more of a module with several functions.
\r
493 // Private so Platform objects can not be copied
\r
494 Platform(const Platform &) {}
\r
495 Platform &operator=(const Platform &) { return *this; }
\r
497 // Should be private because no new Platforms are ever created
\r
498 // but gcc warns about this
\r
501 static ColourDesired Chrome();
\r
502 static ColourDesired ChromeHighlight();
\r
503 static const char *DefaultFont();
\r
504 static int DefaultFontSize();
\r
505 static unsigned int DoubleClickTime();
\r
506 static bool MouseButtonBounce();
\r
507 static void DebugDisplay(const char *s);
\r
508 static bool IsKeyDown(int key);
\r
509 static long SendScintilla(
\r
510 WindowID w, unsigned int msg, unsigned long wParam=0, long lParam=0);
\r
511 static long SendScintillaPointer(
\r
512 WindowID w, unsigned int msg, unsigned long wParam=0, void *lParam=0);
\r
513 static bool IsDBCSLeadByte(int codePage, char ch);
\r
514 static int DBCSCharLength(int codePage, const char *s);
\r
515 static int DBCSCharMaxLength();
\r
517 // These are utility functions not really tied to a platform
\r
518 static int Minimum(int a, int b);
\r
519 static int Maximum(int a, int b);
\r
520 // Next three assume 16 bit shorts and 32 bit longs
\r
521 static long LongFromTwoShorts(short a,short b) {
\r
522 return (a) | ((b) << 16);
\r
524 static short HighShortFromLong(long x) {
\r
525 return static_cast<short>(x >> 16);
\r
527 static short LowShortFromLong(long x) {
\r
528 return static_cast<short>(x & 0xffff);
\r
530 static void DebugPrintf(const char *format, ...);
\r
531 static bool ShowAssertionPopUps(bool assertionPopUps_);
\r
532 static void Assert(const char *c, const char *file, int line);
\r
533 static int Clamp(int val, int minVal, int maxVal);
\r
537 #define PLATFORM_ASSERT(c) ((void)0)
\r
539 #ifdef SCI_NAMESPACE
\r
540 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Scintilla::Platform::Assert(#c, __FILE__, __LINE__))
\r
542 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__))
\r
546 #ifdef SCI_NAMESPACE
\r
550 // Shut up annoying Visual C++ warnings:
\r
552 #pragma warning(disable: 4244 4309 4514 4710)
\r