SciClasses.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // The implementation of various Qt version independent classes used by the
  2. // rest of the port.
  3. //
  4. // Copyright (c) 2017 Riverbank Computing Limited <info@riverbankcomputing.com>
  5. //
  6. // This file is part of QScintilla.
  7. //
  8. // This file may be used under the terms of the GNU General Public License
  9. // version 3.0 as published by the Free Software Foundation and appearing in
  10. // the file LICENSE included in the packaging of this file. Please review the
  11. // following information to ensure the GNU General Public License version 3.0
  12. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  13. //
  14. // If you do not wish to use this file under the terms of the GPL version 3.0
  15. // then you may purchase a commercial license. For more information contact
  16. // info@riverbankcomputing.com.
  17. //
  18. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  19. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. #include "SciNamespace.h"
  21. #include "SciClasses.h"
  22. #include <QCoreApplication>
  23. #include <QKeyEvent>
  24. #include <QListWidgetItem>
  25. #include <QMouseEvent>
  26. #include <QPainter>
  27. #include <QPaintEvent>
  28. #include "ScintillaQt.h"
  29. #include "ListBoxQt.h"
  30. // Create a call tip.
  31. QsciSciCallTip::QsciSciCallTip(QWidget *parent, QsciScintillaQt *sci_)
  32. : QWidget(parent, Qt::WindowFlags(Qt::Popup|Qt::FramelessWindowHint|Qt::WA_StaticContents)),
  33. sci(sci_)
  34. {
  35. // Ensure that the main window keeps the focus (and the caret flashing)
  36. // when this is displayed.
  37. setFocusProxy(parent);
  38. }
  39. // Destroy a call tip.
  40. QsciSciCallTip::~QsciSciCallTip()
  41. {
  42. // Ensure that the main window doesn't receive a focus out event when
  43. // this is destroyed.
  44. setFocusProxy(0);
  45. }
  46. // Paint a call tip.
  47. void QsciSciCallTip::paintEvent(QPaintEvent *)
  48. {
  49. QSCI_SCI_NAMESPACE(Surface) *surfaceWindow = QSCI_SCI_NAMESPACE(Surface)::Allocate(SC_TECHNOLOGY_DEFAULT);
  50. if (!surfaceWindow)
  51. return;
  52. QPainter p(this);
  53. surfaceWindow->Init(&p);
  54. surfaceWindow->SetUnicodeMode(sci->CodePage() == SC_CP_UTF8);
  55. sci->ct.PaintCT(surfaceWindow);
  56. delete surfaceWindow;
  57. }
  58. // Handle a mouse press in a call tip.
  59. void QsciSciCallTip::mousePressEvent(QMouseEvent *e)
  60. {
  61. QSCI_SCI_NAMESPACE(Point) pt;
  62. pt.x = e->x();
  63. pt.y = e->y();
  64. sci->ct.MouseClick(pt);
  65. sci->CallTipClick();
  66. update();
  67. }
  68. // Create the popup instance.
  69. QsciSciPopup::QsciSciPopup()
  70. {
  71. // Set up the mapper.
  72. connect(&mapper, SIGNAL(mapped(int)), this, SLOT(on_triggered(int)));
  73. }
  74. // Add an item and associated command to the popup and enable it if required.
  75. void QsciSciPopup::addItem(const QString &label, int cmd, bool enabled,
  76. QsciScintillaQt *sci_)
  77. {
  78. QAction *act = addAction(label, &mapper, SLOT(map()));
  79. mapper.setMapping(act, cmd);
  80. act->setEnabled(enabled);
  81. sci = sci_;
  82. }
  83. // A slot to handle a menu action being triggered.
  84. void QsciSciPopup::on_triggered(int cmd)
  85. {
  86. sci->Command(cmd);
  87. }
  88. QsciSciListBox::QsciSciListBox(QWidget *parent, QsciListBoxQt *lbx_)
  89. : QListWidget(parent), lbx(lbx_)
  90. {
  91. setAttribute(Qt::WA_StaticContents);
  92. #if defined(Q_OS_WIN)
  93. setWindowFlags(Qt::Tool|Qt::FramelessWindowHint);
  94. // This stops the main widget losing focus when the user clicks on this one
  95. // (which prevents this one being destroyed).
  96. setFocusPolicy(Qt::NoFocus);
  97. #else
  98. // This is the root of the focus problems under Gnome's window manager. We
  99. // have tried many flag combinations in the past. The consensus now seems
  100. // to be that the following works. However it might now work because of a
  101. // change in Qt so we only enable it for recent versions in order to
  102. // reduce the risk of breaking something that works with earlier versions.
  103. #if QT_VERSION >= 0x040500
  104. setWindowFlags(Qt::ToolTip|Qt::WindowStaysOnTopHint);
  105. #else
  106. setWindowFlags(Qt::Tool|Qt::FramelessWindowHint);
  107. #endif
  108. // This may not be needed.
  109. setFocusProxy(parent);
  110. #endif
  111. setFrameShape(StyledPanel);
  112. setFrameShadow(Plain);
  113. connect(this, SIGNAL(itemDoubleClicked(QListWidgetItem *)),
  114. SLOT(handleSelection()));
  115. }
  116. void QsciSciListBox::addItemPixmap(const QPixmap &pm, const QString &txt)
  117. {
  118. new QListWidgetItem(pm, txt, this);
  119. }
  120. int QsciSciListBox::find(const QString &prefix)
  121. {
  122. QList<QListWidgetItem *> itms = findItems(prefix,
  123. Qt::MatchStartsWith|Qt::MatchCaseSensitive);
  124. if (itms.size() == 0)
  125. return -1;
  126. return row(itms[0]);
  127. }
  128. QString QsciSciListBox::text(int n)
  129. {
  130. QListWidgetItem *itm = item(n);
  131. if (!itm)
  132. return QString();
  133. return itm->text();
  134. }
  135. // Reimplemented to close the list when the user presses Escape.
  136. void QsciSciListBox::keyPressEvent(QKeyEvent *e)
  137. {
  138. if (e->key() == Qt::Key_Escape)
  139. {
  140. e->accept();
  141. close();
  142. }
  143. else
  144. {
  145. QListWidget::keyPressEvent(e);
  146. if (!e->isAccepted())
  147. QCoreApplication::sendEvent(parent(), e);
  148. }
  149. }
  150. QsciSciListBox::~QsciSciListBox()
  151. {
  152. // Ensure that the main widget doesn't get a focus out event when this is
  153. // destroyed.
  154. setFocusProxy(0);
  155. }
  156. void QsciSciListBox::handleSelection()
  157. {
  158. if (lbx && lbx->cb_action)
  159. lbx->cb_action(lbx->cb_data);
  160. }