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