TQt conversion of scintilla for Qt3
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Platform.h 14KB

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