qsciscintillabase.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830
  1. // This module implements the "official" low-level API.
  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 "Qsci/qsciscintillabase.h"
  20. #include <QApplication>
  21. #include <QClipboard>
  22. #include <QColor>
  23. #include <QContextMenuEvent>
  24. #include <QDragEnterEvent>
  25. #include <QDragMoveEvent>
  26. #include <QDropEvent>
  27. #include <QDragLeaveEvent>
  28. #include <QFocusEvent>
  29. #include <QKeyEvent>
  30. #include <QList>
  31. #include <QMimeData>
  32. #include <QMouseEvent>
  33. #include <QPaintEvent>
  34. #include <QScrollBar>
  35. #include <QStyle>
  36. #include <QTextCodec>
  37. #include "ScintillaQt.h"
  38. // The #defines in Scintilla.h and the enums in qsciscintillabase.h conflict
  39. // (because we want to use the same names) so we have to undefine those we use
  40. // in this file.
  41. #undef SCI_SETCARETPERIOD
  42. #undef SCK_DOWN
  43. #undef SCK_UP
  44. #undef SCK_LEFT
  45. #undef SCK_RIGHT
  46. #undef SCK_HOME
  47. #undef SCK_END
  48. #undef SCK_PRIOR
  49. #undef SCK_NEXT
  50. #undef SCK_DELETE
  51. #undef SCK_INSERT
  52. #undef SCK_ESCAPE
  53. #undef SCK_BACK
  54. #undef SCK_TAB
  55. #undef SCK_RETURN
  56. #undef SCK_ADD
  57. #undef SCK_SUBTRACT
  58. #undef SCK_DIVIDE
  59. #undef SCK_WIN
  60. #undef SCK_RWIN
  61. #undef SCK_MENU
  62. // Remember if we have linked the lexers.
  63. static bool lexersLinked = false;
  64. // The list of instances.
  65. static QList<QsciScintillaBase *> poolList;
  66. // Mime support.
  67. static const QLatin1String mimeTextPlain("text/plain");
  68. static const QLatin1String mimeRectangularWin("MSDEVColumnSelect");
  69. static const QLatin1String mimeRectangular("text/x-qscintilla-rectangular");
  70. #if (QT_VERSION >= 0x040200 && QT_VERSION < 0x050000 && defined(Q_OS_MAC)) || (QT_VERSION >= 0x050200 && defined(Q_OS_OSX))
  71. extern void initialiseRectangularPasteboardMime();
  72. #endif
  73. // The ctor.
  74. QsciScintillaBase::QsciScintillaBase(QWidget *parent)
  75. : QAbstractScrollArea(parent), preeditPos(-1), preeditNrBytes(0)
  76. #if QT_VERSION >= 0x050000
  77. , clickCausedFocus(false)
  78. #endif
  79. {
  80. connectVerticalScrollBar();
  81. connectHorizontalScrollBar();
  82. setAcceptDrops(true);
  83. setFocusPolicy(Qt::WheelFocus);
  84. setAttribute(Qt::WA_KeyCompression);
  85. setAttribute(Qt::WA_InputMethodEnabled);
  86. #if QT_VERSION >= 0x050100
  87. setInputMethodHints(
  88. Qt::ImhNoAutoUppercase|Qt::ImhNoPredictiveText|Qt::ImhMultiLine);
  89. #elif QT_VERSION >= 0x040600
  90. setInputMethodHints(Qt::ImhNoAutoUppercase|Qt::ImhNoPredictiveText);
  91. #endif
  92. viewport()->setBackgroundRole(QPalette::Base);
  93. viewport()->setMouseTracking(true);
  94. viewport()->setAttribute(Qt::WA_NoSystemBackground);
  95. triple_click.setSingleShot(true);
  96. #if (QT_VERSION >= 0x040200 && QT_VERSION < 0x050000 && defined(Q_OS_MAC)) || (QT_VERSION >= 0x050200 && defined(Q_OS_OSX))
  97. initialiseRectangularPasteboardMime();
  98. #endif
  99. sci = new QsciScintillaQt(this);
  100. SendScintilla(SCI_SETCARETPERIOD, QApplication::cursorFlashTime() / 2);
  101. // Make sure the lexers are linked in.
  102. if (!lexersLinked)
  103. {
  104. Scintilla_LinkLexers();
  105. lexersLinked = true;
  106. }
  107. QClipboard *cb = QApplication::clipboard();
  108. if (cb->supportsSelection())
  109. connect(cb, SIGNAL(selectionChanged()), SLOT(handleSelection()));
  110. // Add it to the pool.
  111. poolList.append(this);
  112. }
  113. // The dtor.
  114. QsciScintillaBase::~QsciScintillaBase()
  115. {
  116. // The QsciScintillaQt object isn't a child so delete it explicitly.
  117. delete sci;
  118. // Remove it from the pool.
  119. poolList.removeAt(poolList.indexOf(this));
  120. }
  121. // Return an instance from the pool.
  122. QsciScintillaBase *QsciScintillaBase::pool()
  123. {
  124. return poolList.first();
  125. }
  126. // Tell Scintilla to update the scroll bars. Scintilla should be doing this
  127. // itself.
  128. void QsciScintillaBase::setScrollBars()
  129. {
  130. sci->SetScrollBars();
  131. }
  132. // Send a message to the real Scintilla widget using the low level Scintilla
  133. // API.
  134. long QsciScintillaBase::SendScintilla(unsigned int msg, unsigned long wParam,
  135. long lParam) const
  136. {
  137. return sci->WndProc(msg, wParam, lParam);
  138. }
  139. // Overloaded message send.
  140. long QsciScintillaBase::SendScintilla(unsigned int msg, unsigned long wParam,
  141. void *lParam) const
  142. {
  143. return sci->WndProc(msg, wParam, reinterpret_cast<sptr_t>(lParam));
  144. }
  145. // Overloaded message send.
  146. long QsciScintillaBase::SendScintilla(unsigned int msg, unsigned long wParam,
  147. const char *lParam) const
  148. {
  149. return sci->WndProc(msg, wParam, reinterpret_cast<sptr_t>(lParam));
  150. }
  151. // Overloaded message send.
  152. long QsciScintillaBase::SendScintilla(unsigned int msg,
  153. const char *lParam) const
  154. {
  155. return sci->WndProc(msg, static_cast<uptr_t>(0),
  156. reinterpret_cast<sptr_t>(lParam));
  157. }
  158. // Overloaded message send.
  159. long QsciScintillaBase::SendScintilla(unsigned int msg, const char *wParam,
  160. const char *lParam) const
  161. {
  162. return sci->WndProc(msg, reinterpret_cast<uptr_t>(wParam),
  163. reinterpret_cast<sptr_t>(lParam));
  164. }
  165. // Overloaded message send.
  166. long QsciScintillaBase::SendScintilla(unsigned int msg, long wParam) const
  167. {
  168. return sci->WndProc(msg, static_cast<uptr_t>(wParam),
  169. static_cast<sptr_t>(0));
  170. }
  171. // Overloaded message send.
  172. long QsciScintillaBase::SendScintilla(unsigned int msg, int wParam) const
  173. {
  174. return sci->WndProc(msg, static_cast<uptr_t>(wParam),
  175. static_cast<sptr_t>(0));
  176. }
  177. // Overloaded message send.
  178. long QsciScintillaBase::SendScintilla(unsigned int msg, long cpMin, long cpMax,
  179. char *lpstrText) const
  180. {
  181. Sci_TextRange tr;
  182. tr.chrg.cpMin = cpMin;
  183. tr.chrg.cpMax = cpMax;
  184. tr.lpstrText = lpstrText;
  185. return sci->WndProc(msg, static_cast<uptr_t>(0),
  186. reinterpret_cast<sptr_t>(&tr));
  187. }
  188. // Overloaded message send.
  189. long QsciScintillaBase::SendScintilla(unsigned int msg, unsigned long wParam,
  190. const QColor &col) const
  191. {
  192. sptr_t lParam = (col.blue() << 16) | (col.green() << 8) | col.red();
  193. return sci->WndProc(msg, wParam, lParam);
  194. }
  195. // Overloaded message send.
  196. long QsciScintillaBase::SendScintilla(unsigned int msg, const QColor &col) const
  197. {
  198. uptr_t wParam = (col.blue() << 16) | (col.green() << 8) | col.red();
  199. return sci->WndProc(msg, wParam, static_cast<sptr_t>(0));
  200. }
  201. // Overloaded message send.
  202. long QsciScintillaBase::SendScintilla(unsigned int msg, unsigned long wParam,
  203. QPainter *hdc, const QRect &rc, long cpMin, long cpMax) const
  204. {
  205. Sci_RangeToFormat rf;
  206. rf.hdc = rf.hdcTarget = reinterpret_cast<QSCI_SCI_NAMESPACE(SurfaceID)>(hdc);
  207. rf.rc.left = rc.left();
  208. rf.rc.top = rc.top();
  209. rf.rc.right = rc.right() + 1;
  210. rf.rc.bottom = rc.bottom() + 1;
  211. rf.chrg.cpMin = cpMin;
  212. rf.chrg.cpMax = cpMax;
  213. return sci->WndProc(msg, wParam, reinterpret_cast<sptr_t>(&rf));
  214. }
  215. // Overloaded message send.
  216. long QsciScintillaBase::SendScintilla(unsigned int msg, unsigned long wParam,
  217. const QPixmap &lParam) const
  218. {
  219. return sci->WndProc(msg, wParam, reinterpret_cast<sptr_t>(&lParam));
  220. }
  221. // Overloaded message send.
  222. long QsciScintillaBase::SendScintilla(unsigned int msg, unsigned long wParam,
  223. const QImage &lParam) const
  224. {
  225. return sci->WndProc(msg, wParam, reinterpret_cast<sptr_t>(&lParam));
  226. }
  227. // Send a message to the real Scintilla widget using the low level Scintilla
  228. // API that returns a pointer result.
  229. void *QsciScintillaBase::SendScintillaPtrResult(unsigned int msg) const
  230. {
  231. return reinterpret_cast<void *>(sci->WndProc(msg, static_cast<uptr_t>(0),
  232. static_cast<sptr_t>(0)));
  233. }
  234. // Re-implemented to handle the context menu.
  235. void QsciScintillaBase::contextMenuEvent(QContextMenuEvent *e)
  236. {
  237. sci->ContextMenu(QSCI_SCI_NAMESPACE(Point)(e->globalX(), e->globalY()));
  238. }
  239. // Re-implemented to tell the widget it has the focus.
  240. void QsciScintillaBase::focusInEvent(QFocusEvent *e)
  241. {
  242. sci->SetFocusState(true);
  243. #if QT_VERSION >= 0x050000
  244. clickCausedFocus = (e->reason() == Qt::MouseFocusReason);
  245. #endif
  246. QAbstractScrollArea::focusInEvent(e);
  247. }
  248. // Re-implemented to tell the widget it has lost the focus.
  249. void QsciScintillaBase::focusOutEvent(QFocusEvent *e)
  250. {
  251. if (e->reason() == Qt::ActiveWindowFocusReason)
  252. {
  253. // Only tell Scintilla we have lost focus if the new active window
  254. // isn't our auto-completion list. This is probably only an issue on
  255. // Linux and there are still problems because subsequent focus out
  256. // events don't always seem to get generated (at least with Qt5).
  257. QWidget *aw = QApplication::activeWindow();
  258. if (!aw || aw->parent() != this || !aw->inherits("QsciSciListBox"))
  259. sci->SetFocusState(false);
  260. }
  261. else
  262. {
  263. sci->SetFocusState(false);
  264. }
  265. QAbstractScrollArea::focusOutEvent(e);
  266. }
  267. // Re-implemented to make sure tabs are passed to the editor.
  268. bool QsciScintillaBase::focusNextPrevChild(bool next)
  269. {
  270. if (!sci->pdoc->IsReadOnly())
  271. return false;
  272. return QAbstractScrollArea::focusNextPrevChild(next);
  273. }
  274. // Handle the selection changing.
  275. void QsciScintillaBase::handleSelection()
  276. {
  277. if (!QApplication::clipboard()->ownsSelection())
  278. sci->UnclaimSelection();
  279. }
  280. // Handle key presses.
  281. void QsciScintillaBase::keyPressEvent(QKeyEvent *e)
  282. {
  283. int modifiers = 0;
  284. if (e->modifiers() & Qt::ShiftModifier)
  285. modifiers |= SCMOD_SHIFT;
  286. if (e->modifiers() & Qt::ControlModifier)
  287. modifiers |= SCMOD_CTRL;
  288. if (e->modifiers() & Qt::AltModifier)
  289. modifiers |= SCMOD_ALT;
  290. if (e->modifiers() & Qt::MetaModifier)
  291. modifiers |= SCMOD_META;
  292. int key = commandKey(e->key(), modifiers);
  293. if (key)
  294. {
  295. bool consumed = false;
  296. sci->KeyDownWithModifiers(key, modifiers, &consumed);
  297. if (consumed)
  298. {
  299. e->accept();
  300. return;
  301. }
  302. }
  303. QString text = e->text();
  304. if (!text.isEmpty() && text[0].isPrint())
  305. {
  306. ScintillaBytes bytes = textAsBytes(text);
  307. sci->AddCharUTF(bytes.data(), bytes.length());
  308. e->accept();
  309. }
  310. else
  311. {
  312. QAbstractScrollArea::keyPressEvent(e);
  313. }
  314. }
  315. // Map a Qt key to a valid Scintilla command key, or 0 if none.
  316. int QsciScintillaBase::commandKey(int qt_key, int &modifiers)
  317. {
  318. int key;
  319. switch (qt_key)
  320. {
  321. case Qt::Key_Down:
  322. key = SCK_DOWN;
  323. break;
  324. case Qt::Key_Up:
  325. key = SCK_UP;
  326. break;
  327. case Qt::Key_Left:
  328. key = SCK_LEFT;
  329. break;
  330. case Qt::Key_Right:
  331. key = SCK_RIGHT;
  332. break;
  333. case Qt::Key_Home:
  334. key = SCK_HOME;
  335. break;
  336. case Qt::Key_End:
  337. key = SCK_END;
  338. break;
  339. case Qt::Key_PageUp:
  340. key = SCK_PRIOR;
  341. break;
  342. case Qt::Key_PageDown:
  343. key = SCK_NEXT;
  344. break;
  345. case Qt::Key_Delete:
  346. key = SCK_DELETE;
  347. break;
  348. case Qt::Key_Insert:
  349. key = SCK_INSERT;
  350. break;
  351. case Qt::Key_Escape:
  352. key = SCK_ESCAPE;
  353. break;
  354. case Qt::Key_Backspace:
  355. key = SCK_BACK;
  356. break;
  357. case Qt::Key_Tab:
  358. key = SCK_TAB;
  359. break;
  360. case Qt::Key_Backtab:
  361. // Scintilla assumes a backtab is shift-tab.
  362. key = SCK_TAB;
  363. modifiers |= SCMOD_SHIFT;
  364. break;
  365. case Qt::Key_Return:
  366. case Qt::Key_Enter:
  367. key = SCK_RETURN;
  368. break;
  369. case Qt::Key_Super_L:
  370. key = SCK_WIN;
  371. break;
  372. case Qt::Key_Super_R:
  373. key = SCK_RWIN;
  374. break;
  375. case Qt::Key_Menu:
  376. key = SCK_MENU;
  377. break;
  378. default:
  379. if ((key = qt_key) > 0x7f)
  380. key = 0;
  381. }
  382. return key;
  383. }
  384. // Encode a QString as bytes.
  385. QsciScintillaBase::ScintillaBytes QsciScintillaBase::textAsBytes(const QString &text) const
  386. {
  387. if (sci->IsUnicodeMode())
  388. return text.toUtf8();
  389. return text.toLatin1();
  390. }
  391. // Decode bytes as a QString.
  392. QString QsciScintillaBase::bytesAsText(const char *bytes) const
  393. {
  394. if (sci->IsUnicodeMode())
  395. return QString::fromUtf8(bytes);
  396. return QString::fromLatin1(bytes);
  397. }
  398. // Handle a mouse button double click.
  399. void QsciScintillaBase::mouseDoubleClickEvent(QMouseEvent *e)
  400. {
  401. if (e->button() != Qt::LeftButton)
  402. {
  403. e->ignore();
  404. return;
  405. }
  406. setFocus();
  407. // Make sure Scintilla will interpret this as a double-click.
  408. unsigned clickTime = sci->lastClickTime + QSCI_SCI_NAMESPACE(Platform)::DoubleClickTime() - 1;
  409. bool shift = e->modifiers() & Qt::ShiftModifier;
  410. bool ctrl = e->modifiers() & Qt::ControlModifier;
  411. bool alt = e->modifiers() & Qt::AltModifier;
  412. sci->ButtonDown(QSCI_SCI_NAMESPACE(Point)(e->x(), e->y()), clickTime,
  413. shift, ctrl, alt);
  414. // Remember the current position and time in case it turns into a triple
  415. // click.
  416. triple_click_at = e->globalPos();
  417. triple_click.start(QApplication::doubleClickInterval());
  418. }
  419. // Handle a mouse move.
  420. void QsciScintillaBase::mouseMoveEvent(QMouseEvent *e)
  421. {
  422. sci->ButtonMove(QSCI_SCI_NAMESPACE(Point)(e->x(), e->y()));
  423. }
  424. // Handle a mouse button press.
  425. void QsciScintillaBase::mousePressEvent(QMouseEvent *e)
  426. {
  427. setFocus();
  428. QSCI_SCI_NAMESPACE(Point) pt(e->x(), e->y());
  429. if (e->button() == Qt::LeftButton)
  430. {
  431. unsigned clickTime;
  432. // It is a triple click if the timer is running and the mouse hasn't
  433. // moved too much.
  434. if (triple_click.isActive() && (e->globalPos() - triple_click_at).manhattanLength() < QApplication::startDragDistance())
  435. clickTime = sci->lastClickTime + QSCI_SCI_NAMESPACE(Platform)::DoubleClickTime() - 1;
  436. else
  437. clickTime = sci->lastClickTime + QSCI_SCI_NAMESPACE(Platform)::DoubleClickTime() + 1;
  438. triple_click.stop();
  439. // Scintilla uses the Alt modifier to initiate rectangular selection.
  440. // However the GTK port (under X11, not Windows) uses the Control
  441. // modifier (by default, although it is configurable). It does this
  442. // because most X11 window managers hijack Alt-drag to move the window.
  443. // We do the same, except that (for the moment at least) we don't allow
  444. // the modifier to be configured.
  445. bool shift = e->modifiers() & Qt::ShiftModifier;
  446. bool ctrl = e->modifiers() & Qt::ControlModifier;
  447. #if defined(Q_OS_MAC) || defined(Q_OS_WIN)
  448. bool alt = e->modifiers() & Qt::AltModifier;
  449. #else
  450. bool alt = ctrl;
  451. #endif
  452. sci->ButtonDown(pt, clickTime, shift, ctrl, alt);
  453. }
  454. else if (e->button() == Qt::MidButton)
  455. {
  456. QClipboard *cb = QApplication::clipboard();
  457. if (cb->supportsSelection())
  458. {
  459. int pos = sci->PositionFromLocation(pt);
  460. sci->sel.Clear();
  461. sci->SetSelection(pos, pos);
  462. sci->pasteFromClipboard(QClipboard::Selection);
  463. }
  464. }
  465. }
  466. // Handle a mouse button releases.
  467. void QsciScintillaBase::mouseReleaseEvent(QMouseEvent *e)
  468. {
  469. if (e->button() != Qt::LeftButton)
  470. return;
  471. QSCI_SCI_NAMESPACE(Point) pt(e->x(), e->y());
  472. if (sci->HaveMouseCapture())
  473. {
  474. bool ctrl = e->modifiers() & Qt::ControlModifier;
  475. sci->ButtonUp(pt, 0, ctrl);
  476. }
  477. #if QT_VERSION >= 0x050000
  478. if (!sci->pdoc->IsReadOnly() && !sci->PointInSelMargin(pt) && qApp->autoSipEnabled())
  479. {
  480. QStyle::RequestSoftwareInputPanel rsip = QStyle::RequestSoftwareInputPanel(style()->styleHint(QStyle::SH_RequestSoftwareInputPanel));
  481. if (!clickCausedFocus || rsip == QStyle::RSIP_OnMouseClick)
  482. qApp->inputMethod()->show();
  483. }
  484. clickCausedFocus = false;
  485. #endif
  486. }
  487. // Handle paint events.
  488. void QsciScintillaBase::paintEvent(QPaintEvent *e)
  489. {
  490. sci->paintEvent(e);
  491. }
  492. // Handle resize events.
  493. void QsciScintillaBase::resizeEvent(QResizeEvent *)
  494. {
  495. sci->ChangeSize();
  496. }
  497. // Re-implemented to suppress the default behaviour as Scintilla works at a
  498. // more fundamental level. Note that this means that replacing the scrollbars
  499. // with custom versions does not work.
  500. void QsciScintillaBase::scrollContentsBy(int, int)
  501. {
  502. }
  503. // Handle the vertical scrollbar.
  504. void QsciScintillaBase::handleVSb(int value)
  505. {
  506. sci->ScrollTo(value);
  507. }
  508. // Handle the horizontal scrollbar.
  509. void QsciScintillaBase::handleHSb(int value)
  510. {
  511. sci->HorizontalScrollTo(value);
  512. }
  513. // Handle drag enters.
  514. void QsciScintillaBase::dragEnterEvent(QDragEnterEvent *e)
  515. {
  516. QsciScintillaBase::dragMoveEvent(e);
  517. }
  518. // Handle drag leaves.
  519. void QsciScintillaBase::dragLeaveEvent(QDragLeaveEvent *)
  520. {
  521. sci->SetDragPosition(QSCI_SCI_NAMESPACE(SelectionPosition)());
  522. }
  523. // Handle drag moves.
  524. void QsciScintillaBase::dragMoveEvent(QDragMoveEvent *e)
  525. {
  526. sci->SetDragPosition(
  527. sci->SPositionFromLocation(
  528. QSCI_SCI_NAMESPACE(Point)(e->pos().x(), e->pos().y()),
  529. false, false, sci->UserVirtualSpace()));
  530. acceptAction(e);
  531. }
  532. // Handle drops.
  533. void QsciScintillaBase::dropEvent(QDropEvent *e)
  534. {
  535. bool moving;
  536. int len;
  537. const char *s;
  538. bool rectangular;
  539. acceptAction(e);
  540. if (!e->isAccepted())
  541. return;
  542. moving = (e->dropAction() == Qt::MoveAction);
  543. QByteArray text = fromMimeData(e->mimeData(), rectangular);
  544. len = text.length();
  545. s = text.data();
  546. std::string dest = QSCI_SCI_NAMESPACE(Document)::TransformLineEnds(s, len,
  547. sci->pdoc->eolMode);
  548. sci->DropAt(sci->posDrop, dest.c_str(), dest.length(), moving,
  549. rectangular);
  550. sci->Redraw();
  551. }
  552. void QsciScintillaBase::acceptAction(QDropEvent *e)
  553. {
  554. if (sci->pdoc->IsReadOnly() || !canInsertFromMimeData(e->mimeData()))
  555. e->ignore();
  556. else
  557. e->acceptProposedAction();
  558. }
  559. // See if a MIME data object can be decoded.
  560. bool QsciScintillaBase::canInsertFromMimeData(const QMimeData *source) const
  561. {
  562. return source->hasFormat(mimeTextPlain);
  563. }
  564. // Create text from a MIME data object.
  565. QByteArray QsciScintillaBase::fromMimeData(const QMimeData *source, bool &rectangular) const
  566. {
  567. // See if it is rectangular. We try all of the different formats that
  568. // Scintilla supports in case we are working across different platforms.
  569. if (source->hasFormat(mimeRectangularWin))
  570. rectangular = true;
  571. else if (source->hasFormat(mimeRectangular))
  572. rectangular = true;
  573. else
  574. rectangular = false;
  575. // Note that we don't support Scintilla's hack of adding a '\0' as Qt
  576. // strips it off under the covers when pasting from another process.
  577. QString utf8 = source->text();
  578. QByteArray text;
  579. if (sci->IsUnicodeMode())
  580. text = utf8.toUtf8();
  581. else
  582. text = utf8.toLatin1();
  583. return text;
  584. }
  585. // Create a MIME data object for some text.
  586. QMimeData *QsciScintillaBase::toMimeData(const QByteArray &text, bool rectangular) const
  587. {
  588. QMimeData *mime = new QMimeData;
  589. QString utf8;
  590. if (sci->IsUnicodeMode())
  591. utf8 = QString::fromUtf8(text.constData(), text.size());
  592. else
  593. utf8 = QString::fromLatin1(text.constData(), text.size());
  594. mime->setText(utf8);
  595. if (rectangular)
  596. {
  597. // Use the platform specific "standard" for specifying a rectangular
  598. // selection.
  599. #if defined(Q_OS_WIN)
  600. mime->setData(mimeRectangularWin, QByteArray());
  601. #else
  602. mime->setData(mimeRectangular, QByteArray());
  603. #endif
  604. }
  605. return mime;
  606. }
  607. // Connect up the vertical scroll bar.
  608. void QsciScintillaBase::connectVerticalScrollBar()
  609. {
  610. connect(verticalScrollBar(), SIGNAL(valueChanged(int)),
  611. SLOT(handleVSb(int)));
  612. }
  613. // Connect up the horizontal scroll bar.
  614. void QsciScintillaBase::connectHorizontalScrollBar()
  615. {
  616. connect(horizontalScrollBar(), SIGNAL(valueChanged(int)),
  617. SLOT(handleHSb(int)));
  618. }
  619. //! Replace the vertical scroll bar.
  620. void QsciScintillaBase::replaceVerticalScrollBar(QScrollBar *scrollBar)
  621. {
  622. setVerticalScrollBar(scrollBar);
  623. connectVerticalScrollBar();
  624. }
  625. // Replace the horizontal scroll bar.
  626. void QsciScintillaBase::replaceHorizontalScrollBar(QScrollBar *scrollBar)
  627. {
  628. setHorizontalScrollBar(scrollBar);
  629. connectHorizontalScrollBar();
  630. }