ListBoxQt.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. // This module implements the specialisation of QListBox that handles the
  2. // Scintilla double-click callback.
  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 "ListBoxQt.h"
  21. #include <stdlib.h>
  22. #include "SciClasses.h"
  23. #include "Qsci/qsciscintilla.h"
  24. QsciListBoxQt::QsciListBoxQt()
  25. : cb_action(0), cb_data(0), slb(0), visible_rows(5), utf8(false)
  26. {
  27. }
  28. void QsciListBoxQt::SetFont(QSCI_SCI_NAMESPACE(Font) &font)
  29. {
  30. QFont *f = reinterpret_cast<QFont *>(font.GetID());
  31. if (f)
  32. slb->setFont(*f);
  33. }
  34. void QsciListBoxQt::Create(QSCI_SCI_NAMESPACE(Window) &parent, int,
  35. QSCI_SCI_NAMESPACE(Point), int, bool unicodeMode, int)
  36. {
  37. utf8 = unicodeMode;
  38. // The parent we want is the QsciScintillaBase, not the text area.
  39. wid = slb = new QsciSciListBox(reinterpret_cast<QWidget *>(parent.GetID())->parentWidget(), this);
  40. }
  41. void QsciListBoxQt::SetAverageCharWidth(int)
  42. {
  43. // We rely on sizeHint() for the size of the list box rather than make
  44. // calculations based on the average character width and the number of
  45. // visible rows.
  46. }
  47. void QsciListBoxQt::SetVisibleRows(int vrows)
  48. {
  49. // We only pretend to implement this.
  50. visible_rows = vrows;
  51. }
  52. int QsciListBoxQt::GetVisibleRows() const
  53. {
  54. return visible_rows;
  55. }
  56. QSCI_SCI_NAMESPACE(PRectangle) QsciListBoxQt::GetDesiredRect()
  57. {
  58. QSCI_SCI_NAMESPACE(PRectangle) rc(0, 0, 100, 100);
  59. if (slb)
  60. {
  61. QSize sh = slb->sizeHint();
  62. rc.right = sh.width();
  63. rc.bottom = sh.height();
  64. }
  65. return rc;
  66. }
  67. int QsciListBoxQt::CaretFromEdge()
  68. {
  69. int dist = 0;
  70. // Find the width of the biggest image.
  71. for (xpmMap::const_iterator it = xset.begin(); it != xset.end(); ++it)
  72. {
  73. int w = it.value().width();
  74. if (dist < w)
  75. dist = w;
  76. }
  77. if (slb)
  78. dist += slb->frameWidth();
  79. // Fudge factor - adjust if required.
  80. dist += 3;
  81. return dist;
  82. }
  83. void QsciListBoxQt::Clear()
  84. {
  85. Q_ASSERT(slb);
  86. slb->clear();
  87. }
  88. void QsciListBoxQt::Append(char *s, int type)
  89. {
  90. Q_ASSERT(slb);
  91. QString qs;
  92. if (utf8)
  93. qs = QString::fromUtf8(s);
  94. else
  95. qs = QString::fromLatin1(s);
  96. xpmMap::const_iterator it;
  97. if (type < 0 || (it = xset.find(type)) == xset.end())
  98. slb->addItem(qs);
  99. else
  100. slb->addItemPixmap(it.value(), qs);
  101. }
  102. int QsciListBoxQt::Length()
  103. {
  104. Q_ASSERT(slb);
  105. return slb->count();
  106. }
  107. void QsciListBoxQt::Select(int n)
  108. {
  109. Q_ASSERT(slb);
  110. slb->setCurrentRow(n);
  111. }
  112. int QsciListBoxQt::GetSelection()
  113. {
  114. Q_ASSERT(slb);
  115. return slb->currentRow();
  116. }
  117. int QsciListBoxQt::Find(const char *prefix)
  118. {
  119. Q_ASSERT(slb);
  120. return slb->find(prefix);
  121. }
  122. void QsciListBoxQt::GetValue(int n, char *value, int len)
  123. {
  124. Q_ASSERT(slb);
  125. QString selection = slb->text(n);
  126. bool trim_selection = false;
  127. QObject *sci_obj = slb->parent();
  128. if (sci_obj->inherits("QsciScintilla"))
  129. {
  130. QsciScintilla *sci = static_cast<QsciScintilla *>(sci_obj);
  131. if (sci->isAutoCompletionList())
  132. {
  133. // Save the full selection and trim the value we return.
  134. sci->acSelection = selection;
  135. trim_selection = true;
  136. }
  137. }
  138. if (selection.isEmpty() || len <= 0)
  139. value[0] = '\0';
  140. else
  141. {
  142. const char *s;
  143. int slen;
  144. QByteArray bytes;
  145. if (utf8)
  146. bytes = selection.toUtf8();
  147. else
  148. bytes = selection.toLatin1();
  149. s = bytes.data();
  150. slen = bytes.length();
  151. while (slen-- && len--)
  152. {
  153. if (trim_selection && *s == ' ')
  154. break;
  155. *value++ = *s++;
  156. }
  157. *value = '\0';
  158. }
  159. }
  160. void QsciListBoxQt::Sort()
  161. {
  162. Q_ASSERT(slb);
  163. slb->sortItems();
  164. }
  165. void QsciListBoxQt::RegisterImage(int type, const char *xpm_data)
  166. {
  167. xset.insert(type, *reinterpret_cast<const QPixmap *>(xpm_data));
  168. }
  169. void QsciListBoxQt::RegisterRGBAImage(int type, int, int,
  170. const unsigned char *pixelsImage)
  171. {
  172. QPixmap pm;
  173. #if QT_VERSION >= 0x040700
  174. pm.convertFromImage(*reinterpret_cast<const QImage *>(pixelsImage));
  175. #else
  176. pm = QPixmap::fromImage(*reinterpret_cast<const QImage *>(pixelsImage));
  177. #endif
  178. xset.insert(type, pm);
  179. }
  180. void QsciListBoxQt::ClearRegisteredImages()
  181. {
  182. xset.clear();
  183. }
  184. void QsciListBoxQt::SetDoubleClickAction(
  185. QSCI_SCI_NAMESPACE(CallBackAction) action, void *data)
  186. {
  187. cb_action = action;
  188. cb_data = data;
  189. }
  190. void QsciListBoxQt::SetList(const char *list, char separator, char typesep)
  191. {
  192. char *words;
  193. Clear();
  194. if ((words = qstrdup(list)) != NULL)
  195. {
  196. char *startword = words;
  197. char *numword = NULL;
  198. for (int i = 0; words[i] != '\0'; i++)
  199. {
  200. if (words[i] == separator)
  201. {
  202. words[i] = '\0';
  203. if (numword)
  204. *numword = '\0';
  205. Append(startword, numword ? atoi(numword + 1) : -1);
  206. startword = words + i + 1;
  207. numword = NULL;
  208. }
  209. else if (words[i] == typesep)
  210. {
  211. numword = words + i;
  212. }
  213. }
  214. if (startword)
  215. {
  216. if (numword)
  217. *numword = '\0';
  218. Append(startword, numword ? atoi(numword + 1) : -1);
  219. }
  220. delete[] words;
  221. }
  222. }
  223. // The ListBox methods that need to be implemented explicitly.
  224. QSCI_SCI_NAMESPACE(ListBox)::ListBox()
  225. {
  226. }
  227. QSCI_SCI_NAMESPACE(ListBox)::~ListBox()
  228. {
  229. }
  230. QSCI_SCI_NAMESPACE(ListBox) *QSCI_SCI_NAMESPACE(ListBox)::Allocate()
  231. {
  232. return new QsciListBoxQt();
  233. }