PlatQt.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098
  1. // This module implements the portability layer for the Qt port of Scintilla.
  2. //
  3. // Copyright (c) 2017 Riverbank Computing Limited <info@riverbankcomputing.com>
  4. //
  5. // This file is part of QScintilla.
  6. //
  7. // This file may be used under the terms of the GNU General Public License
  8. // version 3.0 as published by the Free Software Foundation and appearing in
  9. // the file LICENSE included in the packaging of this file. Please review the
  10. // following information to ensure the GNU General Public License version 3.0
  11. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  12. //
  13. // If you do not wish to use this file under the terms of the GPL version 3.0
  14. // then you may purchase a commercial license. For more information contact
  15. // info@riverbankcomputing.com.
  16. //
  17. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  18. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. #include <string.h>
  22. #include <qapplication.h>
  23. #include <qcursor.h>
  24. #include <qdatetime.h>
  25. #include <qdesktopwidget.h>
  26. #include <qfont.h>
  27. #include <qimage.h>
  28. #include <qlibrary.h>
  29. #include <qpainter.h>
  30. #include <qpixmap.h>
  31. #include <qpolygon.h>
  32. #include <qstring.h>
  33. #include <qtextlayout.h>
  34. #include <qwidget.h>
  35. #include "SciNamespace.h"
  36. #include "include/Platform.h"
  37. #include "XPM.h"
  38. #include "Qsci/qsciscintillabase.h"
  39. #include "SciClasses.h"
  40. #include "FontQuality.h"
  41. QSCI_BEGIN_SCI_NAMESPACE
  42. // Type convertors.
  43. static QFont *PFont(FontID fid)
  44. {
  45. return reinterpret_cast<QFont *>(fid);
  46. }
  47. static QWidget *PWindow(WindowID wid)
  48. {
  49. return reinterpret_cast<QWidget *>(wid);
  50. }
  51. static QsciSciPopup *PMenu(MenuID mid)
  52. {
  53. return reinterpret_cast<QsciSciPopup *>(mid);
  54. }
  55. // Create a Point instance from a long value.
  56. Point Point::FromLong(long lpoint)
  57. {
  58. return Point(Platform::LowShortFromLong(lpoint),
  59. Platform::HighShortFromLong(lpoint));
  60. }
  61. // Font management.
  62. Font::Font() : fid(0)
  63. {
  64. }
  65. Font::~Font()
  66. {
  67. }
  68. void Font::Create(const FontParameters &fp)
  69. {
  70. Release();
  71. QFont *f = new QFont();
  72. QFont::StyleStrategy strategy;
  73. switch (fp.extraFontFlag & SC_EFF_QUALITY_MASK)
  74. {
  75. case SC_EFF_QUALITY_NON_ANTIALIASED:
  76. strategy = QFont::NoAntialias;
  77. break;
  78. case SC_EFF_QUALITY_ANTIALIASED:
  79. strategy = QFont::PreferAntialias;
  80. break;
  81. default:
  82. strategy = QFont::PreferDefault;
  83. }
  84. #if defined(Q_OS_MAC)
  85. #if QT_VERSION >= 0x040700
  86. strategy = static_cast<QFont::StyleStrategy>(strategy | QFont::ForceIntegerMetrics);
  87. #else
  88. #warning "Correct handling of QFont metrics requires Qt v4.7.0 or later"
  89. #endif
  90. #endif
  91. f->setStyleStrategy(strategy);
  92. // If name of the font begins with a '-', assume, that it is an XLFD.
  93. if (fp.faceName[0] == '-')
  94. {
  95. f->setRawName(fp.faceName);
  96. }
  97. else
  98. {
  99. f->setFamily(fp.faceName);
  100. f->setPointSizeF(fp.size);
  101. // See if the Qt weight has been passed via the back door. Otherwise
  102. // map Scintilla weights to Qt weights ensuring that the SC_WEIGHT_*
  103. // values get mapped to the correct QFont::Weight values.
  104. int qt_weight;
  105. if (fp.weight < 0)
  106. qt_weight = -fp.weight;
  107. else if (fp.weight <= 200)
  108. qt_weight = QFont::Light;
  109. else if (fp.weight <= QsciScintillaBase::SC_WEIGHT_NORMAL)
  110. qt_weight = QFont::Normal;
  111. else if (fp.weight <= 600)
  112. qt_weight = QFont::DemiBold;
  113. else if (fp.weight <= 850)
  114. qt_weight = QFont::Bold;
  115. else
  116. qt_weight = QFont::Black;
  117. f->setWeight(qt_weight);
  118. f->setItalic(fp.italic);
  119. }
  120. fid = f;
  121. }
  122. void Font::Release()
  123. {
  124. if (fid)
  125. {
  126. delete PFont(fid);
  127. fid = 0;
  128. }
  129. }
  130. // A surface abstracts a place to draw.
  131. class SurfaceImpl : public Surface
  132. {
  133. public:
  134. SurfaceImpl();
  135. virtual ~SurfaceImpl();
  136. void Init(WindowID wid);
  137. void Init(SurfaceID sid, WindowID);
  138. void Init(QPainter *p);
  139. void InitPixMap(int width, int height, Surface *, WindowID wid);
  140. void Release();
  141. bool Initialised() {return painter;}
  142. void PenColour(ColourDesired fore);
  143. int LogPixelsY() {return pd->logicalDpiY();}
  144. int DeviceHeightFont(int points) {return points;}
  145. void MoveTo(int x_,int y_);
  146. void LineTo(int x_,int y_);
  147. void Polygon(Point *pts, int npts, ColourDesired fore,
  148. ColourDesired back);
  149. void RectangleDraw(PRectangle rc, ColourDesired fore,
  150. ColourDesired back);
  151. void FillRectangle(PRectangle rc, ColourDesired back);
  152. void FillRectangle(PRectangle rc, Surface &surfacePattern);
  153. void RoundedRectangle(PRectangle rc, ColourDesired fore,
  154. ColourDesired back);
  155. void AlphaRectangle(PRectangle rc, int cornerSize, ColourDesired fill,
  156. int alphaFill, ColourDesired outline, int alphaOutline,
  157. int flags);
  158. void DrawRGBAImage(PRectangle rc, int width, int height, const unsigned char *pixelsImage);
  159. void Ellipse(PRectangle rc, ColourDesired fore, ColourDesired back);
  160. void Copy(PRectangle rc, Point from, Surface &surfaceSource);
  161. void DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase,
  162. const char *s, int len, ColourDesired fore, ColourDesired back);
  163. void DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase,
  164. const char *s, int len, ColourDesired fore, ColourDesired back);
  165. void DrawTextTransparent(PRectangle rc, Font &font_, XYPOSITION ybase,
  166. const char *s, int len, ColourDesired fore);
  167. void MeasureWidths(Font &font_, const char *s, int len,
  168. XYPOSITION *positions);
  169. XYPOSITION WidthText(Font &font_, const char *s, int len);
  170. XYPOSITION WidthChar(Font &font_, char ch);
  171. XYPOSITION Ascent(Font &font_);
  172. XYPOSITION Descent(Font &font_);
  173. XYPOSITION InternalLeading(Font &font_) {return 0;}
  174. XYPOSITION ExternalLeading(Font &font_);
  175. XYPOSITION Height(Font &font_);
  176. XYPOSITION AverageCharWidth(Font &font_);
  177. void SetClip(PRectangle rc);
  178. void FlushCachedState();
  179. void SetUnicodeMode(bool unicodeMode_) {unicodeMode = unicodeMode_;}
  180. void SetDBCSMode(int codePage) {}
  181. void DrawXPM(PRectangle rc, const XPM *xpm);
  182. private:
  183. void drawRect(const PRectangle &rc);
  184. void drawText(const PRectangle &rc, Font &font_, XYPOSITION ybase,
  185. const char *s, int len, ColourDesired fore);
  186. static QFont convertQFont(Font &font);
  187. QFontMetricsF metrics(Font &font_);
  188. QString convertText(const char *s, int len);
  189. static QColor convertQColor(const ColourDesired &col,
  190. unsigned alpha = 255);
  191. bool unicodeMode;
  192. QPaintDevice *pd;
  193. QPainter *painter;
  194. bool my_resources;
  195. int pen_x, pen_y;
  196. };
  197. Surface *Surface::Allocate(int)
  198. {
  199. return new SurfaceImpl;
  200. }
  201. SurfaceImpl::SurfaceImpl()
  202. : unicodeMode(false), pd(0), painter(0), my_resources(false), pen_x(0),
  203. pen_y(0)
  204. {
  205. }
  206. SurfaceImpl::~SurfaceImpl()
  207. {
  208. Release();
  209. }
  210. void SurfaceImpl::Init(WindowID wid)
  211. {
  212. Release();
  213. pd = reinterpret_cast<QWidget *>(wid);
  214. }
  215. void SurfaceImpl::Init(SurfaceID sid, WindowID)
  216. {
  217. Release();
  218. // This method, and the SurfaceID type, is only used when printing. As it
  219. // is actually a void * we pass (when using SCI_FORMATRANGE) a pointer to a
  220. // QPainter rather than a pointer to a SurfaceImpl as might be expected.
  221. QPainter *p = reinterpret_cast<QPainter *>(sid);
  222. pd = p->device();
  223. painter = p;
  224. }
  225. void SurfaceImpl::Init(QPainter *p)
  226. {
  227. Release();
  228. pd = p->device();
  229. painter = p;
  230. }
  231. void SurfaceImpl::InitPixMap(int width, int height, Surface *, WindowID wid)
  232. {
  233. Release();
  234. #if QT_VERSION >= 0x050100
  235. int dpr = PWindow(wid)->devicePixelRatio();
  236. QPixmap *pixmap = new QPixmap(width * dpr, height * dpr);
  237. pixmap->setDevicePixelRatio(dpr);
  238. #else
  239. QPixmap *pixmap = new QPixmap(width, height);
  240. Q_UNUSED(wid);
  241. #endif
  242. pd = pixmap;
  243. painter = new QPainter(pd);
  244. my_resources = true;
  245. }
  246. void SurfaceImpl::Release()
  247. {
  248. if (my_resources)
  249. {
  250. if (painter)
  251. delete painter;
  252. if (pd)
  253. delete pd;
  254. my_resources = false;
  255. }
  256. painter = 0;
  257. pd = 0;
  258. }
  259. void SurfaceImpl::MoveTo(int x_, int y_)
  260. {
  261. Q_ASSERT(painter);
  262. pen_x = x_;
  263. pen_y = y_;
  264. }
  265. void SurfaceImpl::LineTo(int x_, int y_)
  266. {
  267. Q_ASSERT(painter);
  268. painter->drawLine(pen_x, pen_y, x_, y_);
  269. pen_x = x_;
  270. pen_y = y_;
  271. }
  272. void SurfaceImpl::PenColour(ColourDesired fore)
  273. {
  274. Q_ASSERT(painter);
  275. painter->setPen(convertQColor(fore));
  276. }
  277. void SurfaceImpl::Polygon(Point *pts, int npts, ColourDesired fore,
  278. ColourDesired back)
  279. {
  280. Q_ASSERT(painter);
  281. QPolygonF qpts(npts);
  282. for (int i = 0; i < npts; ++i)
  283. qpts[i] = QPointF(pts[i].x, pts[i].y);
  284. painter->setPen(convertQColor(fore));
  285. painter->setBrush(convertQColor(back));
  286. painter->drawPolygon(qpts);
  287. }
  288. void SurfaceImpl::RectangleDraw(PRectangle rc, ColourDesired fore,
  289. ColourDesired back)
  290. {
  291. Q_ASSERT(painter);
  292. painter->setPen(convertQColor(fore));
  293. painter->setBrush(convertQColor(back));
  294. drawRect(rc);
  295. }
  296. void SurfaceImpl::FillRectangle(PRectangle rc, ColourDesired back)
  297. {
  298. Q_ASSERT(painter);
  299. painter->setPen(Qt::NoPen);
  300. painter->setBrush(convertQColor(back));
  301. drawRect(rc);
  302. }
  303. void SurfaceImpl::FillRectangle(PRectangle rc, Surface &surfacePattern)
  304. {
  305. Q_ASSERT(painter);
  306. SurfaceImpl &si = static_cast<SurfaceImpl &>(surfacePattern);
  307. QPixmap *pm = static_cast<QPixmap *>(si.pd);
  308. if (pm)
  309. {
  310. QBrush brsh(Qt::black, *pm);
  311. painter->setPen(Qt::NoPen);
  312. painter->setBrush(brsh);
  313. drawRect(rc);
  314. }
  315. else
  316. {
  317. FillRectangle(rc, ColourDesired(0));
  318. }
  319. }
  320. void SurfaceImpl::RoundedRectangle(PRectangle rc, ColourDesired fore,
  321. ColourDesired back)
  322. {
  323. Q_ASSERT(painter);
  324. painter->setPen(convertQColor(fore));
  325. painter->setBrush(convertQColor(back));
  326. painter->drawRoundRect(
  327. QRectF(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top));
  328. }
  329. void SurfaceImpl::AlphaRectangle(PRectangle rc, int cornerSize,
  330. ColourDesired fill, int alphaFill, ColourDesired outline,
  331. int alphaOutline, int)
  332. {
  333. Q_ASSERT(painter);
  334. QColor outline_colour = convertQColor(outline, alphaOutline);
  335. QColor fill_colour = convertQColor(fill, alphaFill);
  336. // There was a report of Qt seeming to ignore the alpha value of the pen so
  337. // so we disable the pen if the outline and fill colours are the same.
  338. if (outline_colour == fill_colour)
  339. painter->setPen(Qt::NoPen);
  340. else
  341. painter->setPen(outline_colour);
  342. painter->setBrush(fill_colour);
  343. const int radius = (cornerSize ? 25 : 0);
  344. painter->drawRoundRect(
  345. QRectF(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top),
  346. radius, radius);
  347. }
  348. void SurfaceImpl::drawRect(const PRectangle &rc)
  349. {
  350. painter->drawRect(
  351. QRectF(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top));
  352. }
  353. void SurfaceImpl::Ellipse(PRectangle rc, ColourDesired fore,
  354. ColourDesired back)
  355. {
  356. Q_ASSERT(painter);
  357. painter->setPen(convertQColor(fore));
  358. painter->setBrush(convertQColor(back));
  359. painter->drawEllipse(
  360. QRectF(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top));
  361. }
  362. void SurfaceImpl::Copy(PRectangle rc, Point from, Surface &surfaceSource)
  363. {
  364. Q_ASSERT(painter);
  365. SurfaceImpl &si = static_cast<SurfaceImpl &>(surfaceSource);
  366. if (si.pd)
  367. {
  368. QPixmap *pm = static_cast<QPixmap *>(si.pd);
  369. qreal x = from.x;
  370. qreal y = from.y;
  371. qreal width = rc.right - rc.left;
  372. qreal height = rc.bottom - rc.top;
  373. #if QT_VERSION >= 0x050100
  374. qreal dpr = pm->devicePixelRatio();
  375. x *= dpr;
  376. y *= dpr;
  377. width *= dpr;
  378. height *= dpr;
  379. #endif
  380. painter->drawPixmap(QPointF(rc.left, rc.top), *pm,
  381. QRectF(x, y, width, height));
  382. }
  383. }
  384. void SurfaceImpl::DrawTextNoClip(PRectangle rc, Font &font_, XYPOSITION ybase,
  385. const char *s, int len, ColourDesired fore, ColourDesired back)
  386. {
  387. Q_ASSERT(painter);
  388. FillRectangle(rc, back);
  389. drawText(rc, font_, ybase, s, len, fore);
  390. }
  391. void SurfaceImpl::DrawTextClipped(PRectangle rc, Font &font_, XYPOSITION ybase,
  392. const char *s, int len, ColourDesired fore, ColourDesired back)
  393. {
  394. Q_ASSERT(painter);
  395. SetClip(rc);
  396. DrawTextNoClip(rc, font_, ybase, s, len, fore, back);
  397. painter->setClipping(false);
  398. }
  399. void SurfaceImpl::DrawTextTransparent(PRectangle rc, Font &font_,
  400. XYPOSITION ybase, const char *s, int len, ColourDesired fore)
  401. {
  402. // Only draw if there is a non-space.
  403. for (int i = 0; i < len; ++i)
  404. if (s[i] != ' ')
  405. {
  406. drawText(rc, font_, ybase, s, len, fore);
  407. return;
  408. }
  409. }
  410. void SurfaceImpl::drawText(const PRectangle &rc, Font &font_, XYPOSITION ybase,
  411. const char *s, int len, ColourDesired fore)
  412. {
  413. QString qs = convertText(s, len);
  414. QFont *f = PFont(font_.GetID());
  415. if (f)
  416. painter->setFont(*f);
  417. painter->setPen(convertQColor(fore));
  418. painter->drawText(QPointF(rc.left, ybase), qs);
  419. }
  420. void SurfaceImpl::DrawXPM(PRectangle rc, const XPM *xpm)
  421. {
  422. Q_ASSERT(painter);
  423. XYPOSITION x, y;
  424. const QPixmap &qpm = xpm->Pixmap();
  425. x = rc.left + (rc.Width() - qpm.width()) / 2.0;
  426. y = rc.top + (rc.Height() - qpm.height()) / 2.0;
  427. painter->drawPixmap(QPointF(x, y), qpm);
  428. }
  429. void SurfaceImpl::DrawRGBAImage(PRectangle rc, int width, int height,
  430. const unsigned char *pixelsImage)
  431. {
  432. Q_ASSERT(painter);
  433. const QImage *qim = reinterpret_cast<const QImage *>(pixelsImage);
  434. painter->drawImage(QPointF(rc.left, rc.top), *qim);
  435. }
  436. void SurfaceImpl::MeasureWidths(Font &font_, const char *s, int len,
  437. XYPOSITION *positions)
  438. {
  439. QString qs = convertText(s, len);
  440. QTextLayout text_layout(qs, convertQFont(font_), pd);
  441. text_layout.beginLayout();
  442. QTextLine text_line = text_layout.createLine();
  443. text_layout.endLayout();
  444. if (unicodeMode)
  445. {
  446. int i_char = 0, i_byte = 0;;
  447. while (i_char < qs.size())
  448. {
  449. unsigned char byte = s[i_byte];
  450. int nbytes, code_units;
  451. // Work out character sizes by looking at the byte stream.
  452. if (byte >= 0xf0)
  453. {
  454. nbytes = 4;
  455. code_units = 2;
  456. }
  457. else
  458. {
  459. if (byte >= 0xe0)
  460. nbytes = 3;
  461. else if (byte >= 0x80)
  462. nbytes = 2;
  463. else
  464. nbytes = 1;
  465. code_units = 1;
  466. }
  467. XYPOSITION position = text_line.cursorToX(i_char + code_units);
  468. // Set the same position for each byte of the character.
  469. for (int i = 0; i < nbytes && i_byte < len; ++i)
  470. positions[i_byte++] = position;
  471. i_char += code_units;
  472. }
  473. // This shouldn't be necessary...
  474. XYPOSITION last_position = ((i_byte > 0) ? positions[i_byte - 1] : 0);
  475. while (i_byte < len)
  476. positions[i_byte++] = last_position;
  477. }
  478. else
  479. {
  480. for (int i = 0; i < len; ++i)
  481. positions[i] = text_line.cursorToX(i + 1);
  482. }
  483. }
  484. XYPOSITION SurfaceImpl::WidthText(Font &font_, const char *s, int len)
  485. {
  486. return metrics(font_).width(convertText(s, len));
  487. }
  488. XYPOSITION SurfaceImpl::WidthChar(Font &font_, char ch)
  489. {
  490. return metrics(font_).width(ch);
  491. }
  492. XYPOSITION SurfaceImpl::Ascent(Font &font_)
  493. {
  494. return metrics(font_).ascent();
  495. }
  496. XYPOSITION SurfaceImpl::Descent(Font &font_)
  497. {
  498. // Qt doesn't include the baseline in the descent, so add it. Note that
  499. // a descent from Qt4 always seems to be 2 pixels larger (irrespective of
  500. // font or size) than the same descent from Qt3. This means that text is a
  501. // little more spaced out with Qt4 - and will be more noticeable with
  502. // smaller fonts.
  503. return metrics(font_).descent() + 1;
  504. }
  505. XYPOSITION SurfaceImpl::ExternalLeading(Font &font_)
  506. {
  507. // Scintilla doesn't use this at the moment, which is good because Qt4 can
  508. // return a negative value.
  509. return metrics(font_).leading();
  510. }
  511. XYPOSITION SurfaceImpl::Height(Font &font_)
  512. {
  513. return metrics(font_).height();
  514. }
  515. XYPOSITION SurfaceImpl::AverageCharWidth(Font &font_)
  516. {
  517. #if QT_VERSION >= 0x040200
  518. return metrics(font_).averageCharWidth();
  519. #else
  520. return WidthChar(font_, 'n');
  521. #endif
  522. }
  523. void SurfaceImpl::SetClip(PRectangle rc)
  524. {
  525. Q_ASSERT(painter);
  526. painter->setClipRect(
  527. QRectF(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top));
  528. }
  529. void SurfaceImpl::FlushCachedState()
  530. {
  531. }
  532. // Return the QFont for a Font.
  533. QFont SurfaceImpl::convertQFont(Font &font)
  534. {
  535. QFont *f = PFont(font.GetID());
  536. if (f)
  537. return *f;
  538. return QApplication::font();
  539. }
  540. // Get the metrics for a font.
  541. QFontMetricsF SurfaceImpl::metrics(Font &font_)
  542. {
  543. QFont fnt = convertQFont(font_);
  544. return QFontMetricsF(fnt, pd);
  545. }
  546. // Convert a Scintilla string to a Qt Unicode string.
  547. QString SurfaceImpl::convertText(const char *s, int len)
  548. {
  549. if (unicodeMode)
  550. return QString::fromUtf8(s, len);
  551. return QString::fromLatin1(s, len);
  552. }
  553. // Convert a Scintilla colour, and alpha component, to a Qt QColor.
  554. QColor SurfaceImpl::convertQColor(const ColourDesired &col, unsigned alpha)
  555. {
  556. long c = col.AsLong();
  557. unsigned r = c & 0xff;
  558. unsigned g = (c >> 8) & 0xff;
  559. unsigned b = (c >> 16) & 0xff;
  560. return QColor(r, g, b, alpha);
  561. }
  562. // Window (widget) management.
  563. Window::~Window()
  564. {
  565. }
  566. void Window::Destroy()
  567. {
  568. QWidget *w = PWindow(wid);
  569. if (w)
  570. {
  571. // Delete the widget next time round the event loop rather than
  572. // straight away. This gets round a problem when auto-completion lists
  573. // are cancelled after an entry has been double-clicked, ie. the list's
  574. // dtor is called from one of the list's slots. There are other ways
  575. // around the problem but this is the simplest and doesn't seem to
  576. // cause problems of its own.
  577. w->deleteLater();
  578. wid = 0;
  579. }
  580. }
  581. bool Window::HasFocus()
  582. {
  583. return PWindow(wid)->hasFocus();
  584. }
  585. PRectangle Window::GetPosition()
  586. {
  587. QWidget *w = PWindow(wid);
  588. // Before any size allocated pretend its big enough not to be scrolled.
  589. PRectangle rc(0,0,5000,5000);
  590. if (w)
  591. {
  592. const QRect &r = w->geometry();
  593. rc.right = r.right() - r.left() + 1;
  594. rc.bottom = r.bottom() - r.top() + 1;
  595. }
  596. return rc;
  597. }
  598. void Window::SetPosition(PRectangle rc)
  599. {
  600. PWindow(wid)->setGeometry(rc.left, rc.top, rc.right - rc.left,
  601. rc.bottom - rc.top);
  602. }
  603. void Window::SetPositionRelative(PRectangle rc, Window relativeTo)
  604. {
  605. QWidget *rel = PWindow(relativeTo.wid);
  606. QPoint pos = rel->mapToGlobal(rel->pos());
  607. int x = pos.x() + rc.left;
  608. int y = pos.y() + rc.top;
  609. PWindow(wid)->setGeometry(x, y, rc.right - rc.left, rc.bottom - rc.top);
  610. }
  611. PRectangle Window::GetClientPosition()
  612. {
  613. return GetPosition();
  614. }
  615. void Window::Show(bool show)
  616. {
  617. QWidget *w = PWindow(wid);
  618. if (show)
  619. w->show();
  620. else
  621. w->hide();
  622. }
  623. void Window::InvalidateAll()
  624. {
  625. QWidget *w = PWindow(wid);
  626. if (w)
  627. w->update();
  628. }
  629. void Window::InvalidateRectangle(PRectangle rc)
  630. {
  631. QWidget *w = PWindow(wid);
  632. if (w)
  633. w->update(rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top);
  634. }
  635. void Window::SetFont(Font &font)
  636. {
  637. PWindow(wid)->setFont(*PFont(font.GetID()));
  638. }
  639. void Window::SetCursor(Cursor curs)
  640. {
  641. Qt::CursorShape qc;
  642. switch (curs)
  643. {
  644. case cursorText:
  645. qc = Qt::IBeamCursor;
  646. break;
  647. case cursorUp:
  648. qc = Qt::UpArrowCursor;
  649. break;
  650. case cursorWait:
  651. qc = Qt::WaitCursor;
  652. break;
  653. case cursorHoriz:
  654. qc = Qt::SizeHorCursor;
  655. break;
  656. case cursorVert:
  657. qc = Qt::SizeVerCursor;
  658. break;
  659. case cursorHand:
  660. qc = Qt::PointingHandCursor;
  661. break;
  662. default:
  663. // Note that Qt doesn't have a standard cursor that could be used to
  664. // implement cursorReverseArrow.
  665. qc = Qt::ArrowCursor;
  666. }
  667. PWindow(wid)->setCursor(qc);
  668. }
  669. void Window::SetTitle(const char *s)
  670. {
  671. PWindow(wid)->setWindowTitle(s);
  672. }
  673. PRectangle Window::GetMonitorRect(Point pt)
  674. {
  675. QPoint qpt = PWindow(wid)->mapToGlobal(QPoint(pt.x, pt.y));
  676. QRect qr = QApplication::desktop()->availableGeometry(qpt);
  677. qpt = PWindow(wid)->mapFromGlobal(qr.topLeft());
  678. return PRectangle(qpt.x(), qpt.y(), qpt.x() + qr.width(), qpt.y() + qr.height());
  679. }
  680. // Menu management.
  681. Menu::Menu() : mid(0)
  682. {
  683. }
  684. void Menu::CreatePopUp()
  685. {
  686. Destroy();
  687. mid = new QsciSciPopup();
  688. }
  689. void Menu::Destroy()
  690. {
  691. QsciSciPopup *m = PMenu(mid);
  692. if (m)
  693. {
  694. delete m;
  695. mid = 0;
  696. }
  697. }
  698. void Menu::Show(Point pt, Window &)
  699. {
  700. PMenu(mid)->popup(QPoint(pt.x, pt.y));
  701. }
  702. class DynamicLibraryImpl : public DynamicLibrary
  703. {
  704. public:
  705. DynamicLibraryImpl(const char *modulePath)
  706. {
  707. m = new QLibrary(modulePath);
  708. m->load();
  709. }
  710. virtual ~DynamicLibraryImpl()
  711. {
  712. if (m)
  713. delete m;
  714. }
  715. virtual Function FindFunction(const char *name)
  716. {
  717. if (m)
  718. return (Function)m->resolve(name);
  719. return 0;
  720. }
  721. virtual bool IsValid()
  722. {
  723. return m && m->isLoaded();
  724. }
  725. private:
  726. QLibrary* m;
  727. };
  728. DynamicLibrary *DynamicLibrary::Load(const char *modulePath)
  729. {
  730. return new DynamicLibraryImpl(modulePath);
  731. }
  732. // Elapsed time. This implementation assumes that the maximum elapsed time is
  733. // less than 48 hours.
  734. ElapsedTime::ElapsedTime()
  735. {
  736. QTime now = QTime::currentTime();
  737. bigBit = now.hour() * 60 * 60 + now.minute() * 60 + now.second();
  738. littleBit = now.msec();
  739. }
  740. double ElapsedTime::Duration(bool reset)
  741. {
  742. long endBigBit, endLittleBit;
  743. QTime now = QTime::currentTime();
  744. endBigBit = now.hour() * 60 * 60 + now.minute() * 60 + now.second();
  745. endLittleBit = now.msec();
  746. double duration = endBigBit - bigBit;
  747. if (duration < 0 || (duration == 0 && endLittleBit < littleBit))
  748. duration += 24 * 60 * 60;
  749. duration += (endLittleBit - littleBit) / 1000.0;
  750. if (reset)
  751. {
  752. bigBit = endBigBit;
  753. littleBit = endLittleBit;
  754. }
  755. return duration;
  756. }
  757. // Manage system wide parameters.
  758. ColourDesired Platform::Chrome()
  759. {
  760. return ColourDesired(0xe0,0xe0,0xe0);
  761. }
  762. ColourDesired Platform::ChromeHighlight()
  763. {
  764. return ColourDesired(0xff,0xff,0xff);
  765. }
  766. const char *Platform::DefaultFont()
  767. {
  768. static QByteArray def_font;
  769. def_font = QApplication::font().family().toLatin1();
  770. return def_font.constData();
  771. }
  772. int Platform::DefaultFontSize()
  773. {
  774. return QApplication::font().pointSize();
  775. }
  776. unsigned int Platform::DoubleClickTime()
  777. {
  778. return QApplication::doubleClickInterval();
  779. }
  780. bool Platform::MouseButtonBounce()
  781. {
  782. return true;
  783. }
  784. void Platform::DebugDisplay(const char *s)
  785. {
  786. qDebug("%s", s);
  787. }
  788. bool Platform::IsKeyDown(int)
  789. {
  790. return false;
  791. }
  792. long Platform::SendScintilla(WindowID w, unsigned int msg,
  793. unsigned long wParam, long lParam)
  794. {
  795. // This is never called.
  796. return 0;
  797. }
  798. long Platform::SendScintillaPointer(WindowID w, unsigned int msg,
  799. unsigned long wParam, void *lParam)
  800. {
  801. // This is never called.
  802. return 0;
  803. }
  804. bool Platform::IsDBCSLeadByte(int, char)
  805. {
  806. // We don't support DBCS.
  807. return false;
  808. }
  809. int Platform::DBCSCharLength(int, const char *)
  810. {
  811. // We don't support DBCS.
  812. return 1;
  813. }
  814. int Platform::DBCSCharMaxLength()
  815. {
  816. // We don't support DBCS.
  817. return 2;
  818. }
  819. int Platform::Minimum(int a, int b)
  820. {
  821. return (a < b) ? a : b;
  822. }
  823. int Platform::Maximum(int a, int b)
  824. {
  825. return (a > b) ? a : b;
  826. }
  827. int Platform::Clamp(int val, int minVal, int maxVal)
  828. {
  829. if (val > maxVal)
  830. val = maxVal;
  831. if (val < minVal)
  832. val = minVal;
  833. return val;
  834. }
  835. //#define TRACE
  836. #ifdef TRACE
  837. void Platform::DebugPrintf(const char *format, ...)
  838. {
  839. char buffer[2000];
  840. va_list pArguments;
  841. va_start(pArguments, format);
  842. vsprintf(buffer, format, pArguments);
  843. va_end(pArguments);
  844. DebugDisplay(buffer);
  845. }
  846. #else
  847. void Platform::DebugPrintf(const char *, ...)
  848. {
  849. }
  850. #endif
  851. static bool assertionPopUps = true;
  852. bool Platform::ShowAssertionPopUps(bool assertionPopUps_)
  853. {
  854. bool ret = assertionPopUps;
  855. assertionPopUps = assertionPopUps_;
  856. return ret;
  857. }
  858. void Platform::Assert(const char *c, const char *file, int line)
  859. {
  860. qFatal("Assertion [%s] failed at %s %d\n", c, file, line);
  861. }
  862. QSCI_END_SCI_NAMESPACE