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
; 
  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         void Move(int xDelta
, int yDelta
) { 
 118         int Width() { return right 
- left
; } 
 119         int Height() { return bottom 
- top
; } 
 123  * In some circumstances, including Win32 in paletted mode and GTK+, each colour 
 124  * must be allocated before use. The desired colours are held in the ColourDesired class, 
 125  * and after allocation the allocation entry is stored in the ColourAllocated class. In other 
 126  * circumstances, such as Win32 in true colour mode, the allocation process just copies 
 127  * the RGB values from the desired to the allocated class. 
 128  * As each desired colour requires allocation before it can be used, the ColourPair class 
 129  * holds both a ColourDesired and a ColourAllocated 
 130  * The Palette class is responsible for managing the palette of colours which contains a 
 131  * list of ColourPair objects and performs the allocation. 
 135  * Holds a desired RGB colour. 
 137 class ColourDesired 
{ 
 140         ColourDesired(long lcol
=0) { 
 144         ColourDesired(unsigned int red
, unsigned int green
, unsigned int blue
) { 
 145                 Set(red
, green
, blue
); 
 148         bool operator==(const ColourDesired 
&other
) const { 
 149                 return co 
== other
.co
; 
 152         void Set(long lcol
) { 
 156         void Set(unsigned int red
, unsigned int green
, unsigned int blue
) { 
 157                 co 
= red 
| (green 
<< 8) | (blue 
<< 16); 
 160         static inline unsigned int ValueOfHex(const char ch
) { 
 161                 if (ch 
>= '0' && ch 
<= '9') 
 163                 else if (ch 
>= 'A' && ch 
<= 'F') 
 164                         return ch 
- 'A' + 10; 
 165                 else if (ch 
>= 'a' && ch 
<= 'f') 
 166                         return ch 
- 'a' + 10; 
 171         void Set(const char *val
) { 
 175                 unsigned int r 
= ValueOfHex(val
[0]) * 16 + ValueOfHex(val
[1]); 
 176                 unsigned int g 
= ValueOfHex(val
[2]) * 16 + ValueOfHex(val
[3]); 
 177                 unsigned int b 
= ValueOfHex(val
[4]) * 16 + ValueOfHex(val
[5]); 
 181         long AsLong() const { 
 185         unsigned int GetRed() { 
 189         unsigned int GetGreen() { 
 190                 return (co 
>> 8) & 0xff; 
 193         unsigned int GetBlue() { 
 194                 return (co 
>> 16) & 0xff; 
 199  * Holds an allocated RGB colour which may be an approximation to the desired colour. 
 201 class ColourAllocated 
{ 
 206         ColourAllocated(long lcol
=0) { 
 210         void Set(long lcol
) { 
 214         long AsLong() const { 
 220  * Colour pairs hold a desired colour and an allocated colour. 
 223         ColourDesired desired
; 
 224         ColourAllocated allocated
; 
 226         ColourPair(ColourDesired desired_
=ColourDesired(0,0,0)) { 
 228                 allocated
.Set(desired
.AsLong()); 
 231                 allocated
.Set(desired
.AsLong()); 
 235 class Window
;   // Forward declaration for Palette 
 238  * Colour palette management. 
 242         enum {numEntries 
= 100}; 
 243         ColourPair entries
[numEntries
]; 
 245         void *allocatedPalette
; // GdkColor * 
 252         bool allowRealization
; 
 260          * This method either adds a colour to the list of wanted colours (want==true) 
 261          * or retrieves the allocated colour back to the ColourPair. 
 262          * This is one method to make it easier to keep the code for wanting and retrieving in sync. 
 264         void WantFind(ColourPair 
&cp
, bool want
); 
 266         void Allocate(Window 
&w
); 
 278         // Private so Font objects can not be copied 
 279         Font(const Font 
&) {} 
 280         Font 
&operator=(const Font 
&) { id
=0; return *this; } 
 285         virtual void Create(const char *faceName
, int characterSet
, int size
, bool bold
, bool italic
); 
 286         virtual void Release(); 
 288         FontID 
GetID() { return id
; } 
 289         // Alias another font - caller guarantees not to Release 
 290         void SetID(FontID id_
) { id 
= id_
; } 
 291         friend class Surface
; 
 292         friend class SurfaceImpl
; 
 296  * A surface abstracts a place to draw. 
 300         // Private so Surface objects can not be copied 
 301         Surface(const Surface 
&) {} 
 302         Surface 
&operator=(const Surface 
&) { return *this; } 
 305         virtual ~Surface() {}; 
 306         static Surface 
*Allocate(); 
 308         virtual void Init(WindowID wid
)=0; 
 309         virtual void Init(SurfaceID sid
, WindowID wid
)=0; 
 310         virtual void InitPixMap(int width
, int height
, Surface 
*surface_
, WindowID wid
)=0; 
 312         virtual void Release()=0; 
 313         virtual bool Initialised()=0; 
 314         virtual void PenColour(ColourAllocated fore
)=0; 
 315         virtual int LogPixelsY()=0; 
 316         virtual int DeviceHeightFont(int points
)=0; 
 317         virtual void MoveTo(int x_
, int y_
)=0; 
 318         virtual void LineTo(int x_
, int y_
)=0; 
 319         virtual void Polygon(Point 
*pts
, int npts
, ColourAllocated fore
, ColourAllocated back
)=0; 
 320         virtual void RectangleDraw(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0; 
 321         virtual void FillRectangle(PRectangle rc
, ColourAllocated back
)=0; 
 322         virtual void FillRectangle(PRectangle rc
, Surface 
&surfacePattern
)=0; 
 323         virtual void RoundedRectangle(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0; 
 324         virtual void Ellipse(PRectangle rc
, ColourAllocated fore
, ColourAllocated back
)=0; 
 325         virtual void Copy(PRectangle rc
, Point from
, Surface 
&surfaceSource
)=0; 
 327         virtual void DrawTextNoClip(PRectangle rc
, Font 
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
)=0; 
 328         virtual void DrawTextClipped(PRectangle rc
, Font 
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
, ColourAllocated back
)=0; 
 329         virtual void DrawTextTransparent(PRectangle rc
, Font 
&font_
, int ybase
, const char *s
, int len
, ColourAllocated fore
)=0; 
 330         virtual void MeasureWidths(Font 
&font_
, const char *s
, int len
, int *positions
)=0; 
 331         virtual int WidthText(Font 
&font_
, const char *s
, int len
)=0; 
 332         virtual int WidthChar(Font 
&font_
, char ch
)=0; 
 333         virtual int Ascent(Font 
&font_
)=0; 
 334         virtual int Descent(Font 
&font_
)=0; 
 335         virtual int InternalLeading(Font 
&font_
)=0; 
 336         virtual int ExternalLeading(Font 
&font_
)=0; 
 337         virtual int Height(Font 
&font_
)=0; 
 338         virtual int AverageCharWidth(Font 
&font_
)=0; 
 340         virtual int SetPalette(Palette 
*pal
, bool inBackGround
)=0; 
 341         virtual void SetClip(PRectangle rc
)=0; 
 342         virtual void FlushCachedState()=0; 
 344         virtual void SetUnicodeMode(bool unicodeMode_
)=0; 
 345         virtual void SetDBCSMode(int codePage
)=0; 
 349  * A simple callback action passing one piece of untyped user data. 
 351 typedef void (*CallBackAction
)(void*); 
 354  * Class to hide the details of window manipulation. 
 355  * Does not own the window which will normally have a longer life than this object. 
 361         Window() : id(0), cursorLast(cursorInvalid
) {} 
 362         Window(const Window 
&source
) : id(source
.id
), cursorLast(cursorInvalid
) {} 
 364         Window 
&operator=(WindowID id_
) { 
 368         WindowID 
GetID() const { return id
; } 
 369         bool Created() const { return id 
!= 0; } 
 372         PRectangle 
GetPosition(); 
 373         void SetPosition(PRectangle rc
); 
 374         void SetPositionRelative(PRectangle rc
, Window relativeTo
); 
 375         PRectangle 
GetClientPosition(); 
 376         void Show(bool show
=true); 
 377         void InvalidateAll(); 
 378         void InvalidateRectangle(PRectangle rc
); 
 379         virtual void SetFont(Font 
&font
); 
 380         enum Cursor 
{ cursorInvalid
, cursorText
, cursorArrow
, cursorUp
, cursorWait
, cursorHoriz
, cursorVert
, cursorReverseArrow
, cursorHand 
}; 
 381         void SetCursor(Cursor curs
); 
 382         void SetTitle(const char *s
); 
 388  * Listbox management. 
 391 class ListBox 
: public Window 
{ 
 395         static ListBox 
*Allocate(); 
 397         virtual void SetFont(Font 
&font
)=0; 
 398         virtual void Create(Window 
&parent
, int ctrlID
, int lineHeight_
, bool unicodeMode_
)=0; 
 399         virtual void SetAverageCharWidth(int width
)=0; 
 400         virtual void SetVisibleRows(int rows
)=0; 
 401         virtual PRectangle 
GetDesiredRect()=0; 
 402         virtual int CaretFromEdge()=0; 
 403         virtual void Clear()=0; 
 404         virtual void Append(char *s
, int type 
= -1)=0; 
 405         virtual int Length()=0; 
 406         virtual void Select(int n
)=0; 
 407         virtual int GetSelection()=0; 
 408         virtual int Find(const char *prefix
)=0; 
 409         virtual void GetValue(int n
, char *value
, int len
)=0; 
 410         virtual void Sort()=0; 
 411         virtual void RegisterImage(int type
, const char *xpm_data
)=0; 
 412         virtual void ClearRegisteredImages()=0; 
 413         virtual void SetDoubleClickAction(CallBackAction
, void *)=0; 
 423         MenuID 
GetID() { return id
; } 
 426         void Show(Point pt
, Window 
&w
); 
 434         double Duration(bool reset
=false); 
 438  * Platform class used to retrieve system wide parameters such as double click speed 
 439  * and chrome colour. Not a creatable object, more of a module with several functions. 
 442         // Private so Platform objects can not be copied 
 443         Platform(const Platform 
&) {} 
 444         Platform 
&operator=(const Platform 
&) { return *this; } 
 446         // Should be private because no new Platforms are ever created 
 447         // but gcc warns about this 
 450         static ColourDesired 
Chrome(); 
 451         static ColourDesired 
ChromeHighlight(); 
 452         static const char *DefaultFont(); 
 453         static int DefaultFontSize(); 
 454         static unsigned int DoubleClickTime(); 
 455         static bool MouseButtonBounce(); 
 456         static void DebugDisplay(const char *s
); 
 457         static bool IsKeyDown(int key
); 
 458         static long SendScintilla( 
 459                 WindowID w
, unsigned int msg
, unsigned long wParam
=0, long lParam
=0); 
 460         static long SendScintillaPointer( 
 461                 WindowID w
, unsigned int msg
, unsigned long wParam
=0, void *lParam
=0); 
 462         static bool IsDBCSLeadByte(int codePage
, char ch
); 
 463         static int DBCSCharLength(int codePage
, const char *s
); 
 464         static int DBCSCharMaxLength(); 
 466         // These are utility functions not really tied to a platform 
 467         static int Minimum(int a
, int b
); 
 468         static int Maximum(int a
, int b
); 
 469         // Next three assume 16 bit shorts and 32 bit longs 
 470         static long LongFromTwoShorts(short a
,short b
) { 
 471                 return (a
) | ((b
) << 16); 
 473         static short HighShortFromLong(long x
) { 
 474                 return static_cast<short>(x 
>> 16); 
 476         static short LowShortFromLong(long x
) { 
 477                 return static_cast<short>(x 
& 0xffff); 
 479         static void DebugPrintf(const char *format
, ...); 
 480         static bool ShowAssertionPopUps(bool assertionPopUps_
); 
 481         static void Assert(const char *c
, const char *file
, int line
); 
 482         static int Clamp(int val
, int minVal
, int maxVal
); 
 486 #define PLATFORM_ASSERT(c) ((void)0) 
 488 #define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__)) 
 491 // Shut up annoying Visual C++ warnings: 
 493 #pragma warning(disable: 4244 4309 4514 4710)