1// Scintilla source code edit control 2/** @file Platform.h 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 wxWidgets. 5 **/ 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. 8 9#ifndef PLATFORM_H 10#define PLATFORM_H 11 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 wxWidgets on any supported platform 16 17#define PLAT_GTK 0 18#define PLAT_GTK_WIN32 0 19#define PLAT_WIN 0 20#define PLAT_WX 0 21#define PLAT_FOX 0 22 23#if defined(FOX) 24#undef PLAT_FOX 25#define PLAT_FOX 1 26 27#elif defined(__WX__) 28#undef PLAT_WX 29#define PLAT_WX 1 30 31#elif defined(GTK) 32#undef PLAT_GTK 33#define PLAT_GTK 1 34 35#ifdef _MSC_VER 36#undef PLAT_GTK_WIN32 37#define PLAT_GTK_WIN32 1 38#endif 39 40#else 41#undef PLAT_WIN 42#define PLAT_WIN 1 43 44#endif 45 46 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 49 50typedef void *FontID; 51typedef void *SurfaceID; 52typedef void *WindowID; 53typedef void *MenuID; 54typedef void *TickerID; 55typedef void *Function; 56typedef void *IdlerID; 57 58/** 59 * A geometric point class. 60 * Point is exactly the same as the Win32 POINT and GTK+ GdkPoint so can be used interchangeably. 61 */ 62class Point { 63public: 64 int x; 65 int y; 66 67 explicit Point(int x_=0, int y_=0) : x(x_), y(y_) { 68 } 69 70 // Other automatically defined methods (assignment, copy constructor, destructor) are fine 71 72 static Point FromLong(long lpoint); 73}; 74 75/** 76 * A geometric rectangle class. 77 * PRectangle is exactly the same as the Win32 RECT so can be used interchangeably. 78 * PRectangles contain their top and left sides, but not their right and bottom sides. 79 */ 80class PRectangle { 81public: 82 int left; 83 int top; 84 int right; 85 int bottom; 86 87 PRectangle(int left_=0, int top_=0, int right_=0, int bottom_ = 0) : 88 left(left_), top(top_), right(right_), bottom(bottom_) { 89 } 90 91 // Other automatically defined methods (assignment, copy constructor, destructor) are fine 92 93 bool operator==(PRectangle &rc) { 94 return (rc.left == left) && (rc.right == right) && 95 (rc.top == top) && (rc.bottom == bottom); 96 } 97 bool Contains(Point pt) { 98 return (pt.x >= left) && (pt.x <= right) && 99 (pt.y >= top) && (pt.y <= bottom); 100 } 101 bool Contains(PRectangle rc) { 102 return (rc.left >= left) && (rc.right <= right) && 103 (rc.top >= top) && (rc.bottom <= bottom); 104 } 105 bool Intersects(PRectangle other) { 106 return (right > other.left) && (left < other.right) && 107 (bottom > other.top) && (top < other.bottom); 108 } 109 void Move(int xDelta, int yDelta) { 110 left += xDelta; 111 top += yDelta; 112 right += xDelta; 113 bottom += yDelta; 114 } 115 int Width() { return right - left; } 116 int Height() { return bottom - top; } 117}; 118 119/** 120 * In some circumstances, including Win32 in paletted mode and GTK+, each colour 121 * must be allocated before use. The desired colours are held in the ColourDesired class, 122 * and after allocation the allocation entry is stored in the ColourAllocated class. In other 123 * circumstances, such as Win32 in true colour mode, the allocation process just copies 124 * the RGB values from the desired to the allocated class. 125 * As each desired colour requires allocation before it can be used, the ColourPair class 126 * holds both a ColourDesired and a ColourAllocated 127 * The Palette class is responsible for managing the palette of colours which contains a 128 * list of ColourPair objects and performs the allocation. 129 */ 130 131/** 132 * Holds a desired RGB colour. 133 */ 134class ColourDesired { 135 long co; 136public: 137 ColourDesired(long lcol=0) { 138 co = lcol; 139 } 140 141 ColourDesired(unsigned int red, unsigned int green, unsigned int blue) { 142 Set(red, green, blue); 143 } 144 145 bool operator==(const ColourDesired &other) const { 146 return co == other.co; 147 } 148 149 void Set(long lcol) { 150 co = lcol; 151 } 152 153 void Set(unsigned int red, unsigned int green, unsigned int blue) { 154 co = red | (green << 8) | (blue << 16); 155 } 156 157 static inline unsigned int ValueOfHex(const char ch) { 158 if (ch >= '0' && ch <= '9') 159 return ch - '0'; 160 else if (ch >= 'A' && ch <= 'F') 161 return ch - 'A' + 10; 162 else if (ch >= 'a' && ch <= 'f') 163 return ch - 'a' + 10; 164 else 165 return 0; 166 } 167 168 void Set(const char *val) { 169 if (*val == '#') { 170 val++; 171 } 172 unsigned int r = ValueOfHex(val[0]) * 16 + ValueOfHex(val[1]); 173 unsigned int g = ValueOfHex(val[2]) * 16 + ValueOfHex(val[3]); 174 unsigned int b = ValueOfHex(val[4]) * 16 + ValueOfHex(val[5]); 175 Set(r, g, b); 176 } 177 178 long AsLong() const { 179 return co; 180 } 181 182 unsigned int GetRed() { 183 return co & 0xff; 184 } 185 186 unsigned int GetGreen() { 187 return (co >> 8) & 0xff; 188 } 189 190 unsigned int GetBlue() { 191 return (co >> 16) & 0xff; 192 } 193}; 194 195/** 196 * Holds an allocated RGB colour which may be an approximation to the desired colour. 197 */ 198class ColourAllocated { 199 long coAllocated; 200 201public: 202 203 ColourAllocated(long lcol=0) { 204 coAllocated = lcol; 205 } 206 207 void Set(long lcol) { 208 coAllocated = lcol; 209 } 210 211 long AsLong() const { 212 return coAllocated; 213 } 214}; 215 216/** 217 * Colour pairs hold a desired colour and an allocated colour. 218 */ 219struct ColourPair { 220 ColourDesired desired; 221 ColourAllocated allocated; 222 223 ColourPair(ColourDesired desired_=ColourDesired(0,0,0)) { 224 desired = desired_; 225 allocated.Set(desired.AsLong()); 226 } 227 void Copy() { 228 allocated.Set(desired.AsLong()); 229 } 230}; 231 232class Window; // Forward declaration for Palette 233 234/** 235 * Colour palette management. 236 */ 237class Palette { 238 int used; 239 int size; 240 ColourPair *entries; 241#if PLAT_GTK 242 void *allocatedPalette; // GdkColor * 243 int allocatedLen; 244#endif 245 // Private so Palette objects can not be copied 246 Palette(const Palette &) {} 247 Palette &operator=(const Palette &) { return *this; } 248public: 249#if PLAT_WIN 250 void *hpal; 251#endif 252 bool allowRealization; 253 254 Palette(); 255 ~Palette(); 256 257 void Release(); 258 259 /** 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. 263 */ 264 void WantFind(ColourPair &cp, bool want); 265 266 void Allocate(Window &w); 267}; 268 269/** 270 * Font management. 271 */ 272class Font { 273protected: 274 FontID id; 275#if PLAT_WX 276 int ascent; 277#endif 278 // Private so Font objects can not be copied 279 Font(const Font &) {} 280 Font &operator=(const Font &) { id=0; return *this; } 281public: 282 Font(); 283 virtual ~Font(); 284 285 virtual void Create(const char *faceName, int characterSet, int size, 286 bool bold, bool italic, bool extraFontFlag=false); 287 virtual void Release(); 288 289 FontID GetID() { return id; } 290 // Alias another font - caller guarantees not to Release 291 void SetID(FontID id_) { id = id_; } 292 friend class Surface; 293 friend class SurfaceImpl; 294}; 295 296/** 297 * A surface abstracts a place to draw. 298 */ 299class Surface { 300private: 301 // Private so Surface objects can not be copied 302 Surface(const Surface &) {} 303 Surface &operator=(const Surface &) { return *this; } 304public: 305 Surface() {}; 306 virtual ~Surface() {}; 307 static Surface *Allocate(); 308 309 virtual void Init(WindowID wid)=0; 310 virtual void Init(SurfaceID sid, WindowID wid)=0; 311 virtual void InitPixMap(int width, int height, Surface *surface_, WindowID wid)=0; 312 313 virtual void Release()=0; 314 virtual bool Initialised()=0; 315 virtual void PenColour(ColourAllocated fore)=0; 316 virtual int LogPixelsY()=0; 317 virtual int DeviceHeightFont(int points)=0; 318 virtual void MoveTo(int x_, int y_)=0; 319 virtual void LineTo(int x_, int y_)=0; 320 virtual void Polygon(Point *pts, int npts, ColourAllocated fore, ColourAllocated back)=0; 321 virtual void RectangleDraw(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0; 322 virtual void FillRectangle(PRectangle rc, ColourAllocated back)=0; 323 virtual void FillRectangle(PRectangle rc, Surface &surfacePattern)=0; 324 virtual void RoundedRectangle(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0; 325 virtual void AlphaRectangle(PRectangle rc, int cornerSize, ColourAllocated fill, int alphaFill, 326 ColourAllocated outline, int alphaOutline, int flags)=0; 327 virtual void Ellipse(PRectangle rc, ColourAllocated fore, ColourAllocated back)=0; 328 virtual void Copy(PRectangle rc, Point from, Surface &surfaceSource)=0; 329 330 virtual void DrawTextNoClip(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back)=0; 331 virtual void DrawTextClipped(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore, ColourAllocated back)=0; 332 virtual void DrawTextTransparent(PRectangle rc, Font &font_, int ybase, const char *s, int len, ColourAllocated fore)=0; 333 virtual void MeasureWidths(Font &font_, const char *s, int len, int *positions)=0; 334 virtual int WidthText(Font &font_, const char *s, int len)=0; 335 virtual int WidthChar(Font &font_, char ch)=0; 336 virtual int Ascent(Font &font_)=0; 337 virtual int Descent(Font &font_)=0; 338 virtual int InternalLeading(Font &font_)=0; 339 virtual int ExternalLeading(Font &font_)=0; 340 virtual int Height(Font &font_)=0; 341 virtual int AverageCharWidth(Font &font_)=0; 342 343 virtual int SetPalette(Palette *pal, bool inBackGround)=0; 344 virtual void SetClip(PRectangle rc)=0; 345 virtual void FlushCachedState()=0; 346 347 virtual void SetUnicodeMode(bool unicodeMode_)=0; 348 virtual void SetDBCSMode(int codePage)=0; 349}; 350 351/** 352 * A simple callback action passing one piece of untyped user data. 353 */ 354typedef void (*CallBackAction)(void*); 355 356/** 357 * Class to hide the details of window manipulation. 358 * Does not own the window which will normally have a longer life than this object. 359 */ 360class Window { 361protected: 362 WindowID id; 363public: 364 Window() : id(0), cursorLast(cursorInvalid) {} 365 Window(const Window &source) : id(source.id), cursorLast(cursorInvalid) {} 366 virtual ~Window(); 367 Window &operator=(WindowID id_) { 368 id = id_; 369 return *this; 370 } 371 WindowID GetID() const { return id; } 372 bool Created() const { return id != 0; } 373 void Destroy(); 374 bool HasFocus(); 375 PRectangle GetPosition(); 376 void SetPosition(PRectangle rc); 377 void SetPositionRelative(PRectangle rc, Window relativeTo); 378 PRectangle GetClientPosition(); 379 void Show(bool show=true); 380 void InvalidateAll(); 381 void InvalidateRectangle(PRectangle rc); 382 virtual void SetFont(Font &font); 383 enum Cursor { cursorInvalid, cursorText, cursorArrow, cursorUp, cursorWait, cursorHoriz, cursorVert, cursorReverseArrow, cursorHand }; 384 void SetCursor(Cursor curs); 385 void SetTitle(const char *s); 386private: 387 Cursor cursorLast; 388}; 389 390/** 391 * Listbox management. 392 */ 393 394class ListBox : public Window { 395public: 396 ListBox(); 397 virtual ~ListBox(); 398 static ListBox *Allocate(); 399 400 virtual void SetFont(Font &font)=0; 401 virtual void Create(Window &parent, int ctrlID, Point location, int lineHeight_, bool unicodeMode_)=0; 402 virtual void SetAverageCharWidth(int width)=0; 403 virtual void SetVisibleRows(int rows)=0; 404 virtual int GetVisibleRows() const=0; 405 virtual PRectangle GetDesiredRect()=0; 406 virtual int CaretFromEdge()=0; 407 virtual void Clear()=0; 408 virtual void Append(char *s, int type = -1)=0; 409 virtual int Length()=0; 410 virtual void Select(int n)=0; 411 virtual int GetSelection()=0; 412 virtual int Find(const char *prefix)=0; 413 virtual void GetValue(int n, char *value, int len)=0; 414 virtual void RegisterImage(int type, const char *xpm_data)=0; 415 virtual void ClearRegisteredImages()=0; 416 virtual void SetDoubleClickAction(CallBackAction, void *)=0; 417 virtual void SetList(const char* list, char separator, char typesep)=0; 418}; 419 420/** 421 * Menu management. 422 */ 423class Menu { 424 MenuID id; 425public: 426 Menu(); 427 MenuID GetID() { return id; } 428 void CreatePopUp(); 429 void Destroy(); 430 void Show(Point pt, Window &w); 431}; 432 433class ElapsedTime { 434 long bigBit; 435 long littleBit; 436public: 437 ElapsedTime(); 438 double Duration(bool reset=false); 439}; 440 441/** 442 * Dynamic Library (DLL/SO/...) loading 443 */ 444class DynamicLibrary { 445public: 446 virtual ~DynamicLibrary() {}; 447 448 /// @return Pointer to function "name", or NULL on failure. 449 virtual Function FindFunction(const char *name) = 0; 450 451 /// @return true if the library was loaded successfully. 452 virtual bool IsValid() = 0; 453 454 /// @return An instance of a DynamicLibrary subclass with "modulePath" loaded. 455 static DynamicLibrary *Load(const char *modulePath); 456}; 457 458/** 459 * Platform class used to retrieve system wide parameters such as double click speed 460 * and chrome colour. Not a creatable object, more of a module with several functions. 461 */ 462class Platform { 463 // Private so Platform objects can not be copied 464 Platform(const Platform &) {} 465 Platform &operator=(const Platform &) { return *this; } 466public: 467 // Should be private because no new Platforms are ever created 468 // but gcc warns about this 469 Platform() {} 470 ~Platform() {} 471 static ColourDesired Chrome(); 472 static ColourDesired ChromeHighlight(); 473 static const char *DefaultFont(); 474 static int DefaultFontSize(); 475 static unsigned int DoubleClickTime(); 476 static bool MouseButtonBounce(); 477 static void DebugDisplay(const char *s); 478 static bool IsKeyDown(int key); 479 static long SendScintilla( 480 WindowID w, unsigned int msg, unsigned long wParam=0, long lParam=0); 481 static long SendScintillaPointer( 482 WindowID w, unsigned int msg, unsigned long wParam=0, void *lParam=0); 483 static bool IsDBCSLeadByte(int codePage, char ch); 484 static int DBCSCharLength(int codePage, const char *s); 485 static int DBCSCharMaxLength(); 486 487 // These are utility functions not really tied to a platform 488 static int Minimum(int a, int b); 489 static int Maximum(int a, int b); 490 // Next three assume 16 bit shorts and 32 bit longs 491 static long LongFromTwoShorts(short a,short b) { 492 return (a) | ((b) << 16); 493 } 494 static short HighShortFromLong(long x) { 495 return static_cast<short>(x >> 16); 496 } 497 static short LowShortFromLong(long x) { 498 return static_cast<short>(x & 0xffff); 499 } 500 static void DebugPrintf(const char *format, ...); 501 static bool ShowAssertionPopUps(bool assertionPopUps_); 502 static void Assert(const char *c, const char *file, int line); 503 static int Clamp(int val, int minVal, int maxVal); 504}; 505 506#ifdef NDEBUG 507#define PLATFORM_ASSERT(c) ((void)0) 508#else 509#define PLATFORM_ASSERT(c) ((c) ? (void)(0) : Platform::Assert(#c, __FILE__, __LINE__)) 510#endif 511 512// Shut up annoying Visual C++ warnings: 513#ifdef _MSC_VER 514#pragma warning(disable: 4244 4309 4514 4710) 515#endif 516 517#endif 518