ListBox.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2020 to 2022 David Forsgren Piuva
  4. //
  5. // This software is provided 'as-is', without any express or implied
  6. // warranty. In no event will the authors be held liable for any damages
  7. // arising from the use of this software.
  8. //
  9. // Permission is granted to anyone to use this software for any purpose,
  10. // including commercial applications, and to alter it and redistribute it
  11. // freely, subject to the following restrictions:
  12. //
  13. // 1. The origin of this software must not be misrepresented; you must not
  14. // claim that you wrote the original software. If you use this software
  15. // in a product, an acknowledgment in the product documentation would be
  16. // appreciated but is not required.
  17. //
  18. // 2. Altered source versions must be plainly marked as such, and must not be
  19. // misrepresented as being the original software.
  20. //
  21. // 3. This notice may not be removed or altered from any source
  22. // distribution.
  23. #include "ListBox.h"
  24. using namespace dsr;
  25. PERSISTENT_DEFINITION(ListBox)
  26. void ListBox::declareAttributes(StructureDefinition &target) const {
  27. VisualComponent::declareAttributes(target);
  28. target.declareAttribute(U"BackColor");
  29. target.declareAttribute(U"ForeColor");
  30. target.declareAttribute(U"List");
  31. target.declareAttribute(U"SelectedIndex");
  32. }
  33. Persistent* ListBox::findAttribute(const ReadableString &name) {
  34. if (string_caseInsensitiveMatch(name, U"Color") || string_caseInsensitiveMatch(name, U"BackColor")) {
  35. return &(this->backColor);
  36. } else if (string_caseInsensitiveMatch(name, U"ForeColor")) {
  37. return &(this->foreColor);
  38. } else if (string_caseInsensitiveMatch(name, U"List")) {
  39. return &(this->list);
  40. } else if (string_caseInsensitiveMatch(name, U"SelectedIndex")) {
  41. return &(this->selectedIndex);
  42. } else {
  43. return VisualComponent::findAttribute(name);
  44. }
  45. }
  46. ListBox::ListBox() {
  47. // Changed from nothing to zero
  48. this->callback_selectEvent(0);
  49. }
  50. bool ListBox::isContainer() const {
  51. return false;
  52. }
  53. static const int textBorderLeft = 6;
  54. static const int textBorderTop = 4;
  55. void ListBox::generateGraphics() {
  56. int32_t width = this->location.width();
  57. int32_t height = this->location.height();
  58. if (width < 1) { width = 1; }
  59. if (height < 1) { height = 1; }
  60. if (!this->hasImages) {
  61. this->completeAssets();
  62. ColorRgbI32 backColor = this->backColor.value;
  63. ColorRgbI32 foreColor = this->foreColor.value;
  64. component_generateImage(this->theme, this->scalableImage_listBox, width, height, backColor.red, backColor.green, backColor.blue)(this->image);
  65. int32_t verticalStep = font_getSize(this->font);
  66. int32_t left = textBorderLeft;
  67. int32_t top = textBorderTop;
  68. for (int64_t i = this->verticalScrollBar.getValue(); i < this->list.value.length() && top < height; i++) {
  69. ColorRgbaI32 textColor;
  70. if (i == this->pressedIndex || i == this->selectedIndex.value) {
  71. textColor = ColorRgbaI32(255, 255, 255, 255);
  72. } else {
  73. textColor = ColorRgbaI32(foreColor, 255);
  74. }
  75. if (i == this->selectedIndex.value) {
  76. draw_rectangle(this->image, IRect(left, top, width - (textBorderLeft * 2), verticalStep), ColorRgbaI32(0, 0, 0, 255));
  77. }
  78. font_printLine(this->image, this->font, this->list.value[i], IVector2D(left, top), textColor);
  79. top += verticalStep;
  80. }
  81. this->verticalScrollBar.draw(this->image, this->theme, backColor);
  82. this->hasImages = true;
  83. }
  84. }
  85. void ListBox::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  86. this->generateGraphics();
  87. draw_copy(targetImage, this->image, relativeLocation.left(), relativeLocation.top());
  88. }
  89. void ListBox::updateScrollRange() {
  90. this->loadFont();
  91. // How high is one element?
  92. int64_t verticalStep = font_getSize(this->font);
  93. // How many elements are visible at the same time?
  94. int64_t visibleRange = (this->location.height() - textBorderTop * 2) / verticalStep;
  95. if (visibleRange < 1) visibleRange = 1;
  96. // How many elements are there in total to see.
  97. int64_t itemCount = this->list.value.length();
  98. // The range of indices that the listbox can start viewing from.
  99. int64_t minScroll = 0;
  100. int64_t maxScroll = itemCount - visibleRange;
  101. // If visible range exceeds the collection, we should still allow starting element zero to get a valid range.
  102. if (maxScroll < 0) maxScroll = 0;
  103. // Apply the scroll range.
  104. this->verticalScrollBar.updateScrollRange(ScrollRange(minScroll, maxScroll, visibleRange));
  105. }
  106. void ListBox::limitScrolling(bool keepSelectedVisible) {
  107. // Update the scroll range.
  108. this->updateScrollRange();
  109. // Limit scrolling with the updated range.
  110. this->verticalScrollBar.limitScrolling(this->location, keepSelectedVisible, this->selectedIndex.value);
  111. }
  112. void ListBox::receiveMouseEvent(const MouseEvent& event) {
  113. bool supressEvent = false;
  114. this->inside = this->pointIsInside(event.position);
  115. IVector2D localPosition = event.position - this->location.upperLeft();
  116. bool verticalScrollIntercepted = this->verticalScrollBar.receiveMouseEvent(this->location, event);
  117. int64_t maxIndex = this->list.value.length() - 1;
  118. int64_t hoverIndex = this->verticalScrollBar.getValue() + ((localPosition.y - textBorderTop) / font_getSize(this->font));
  119. if (hoverIndex < 0 || hoverIndex > maxIndex) {
  120. hoverIndex = -1;
  121. }
  122. if (event.mouseEventType == MouseEventType::MouseDown) {
  123. if (verticalScrollIntercepted) {
  124. this->pressedIndex = -1;
  125. } else {
  126. this->pressedIndex = hoverIndex;
  127. }
  128. this->hasImages = false; // Force redraw on item selection
  129. } else if (event.mouseEventType == MouseEventType::MouseUp) {
  130. if (this->pressedIndex > -1 && this->inside && hoverIndex == this->pressedIndex) {
  131. this->setSelectedIndex(hoverIndex, false);
  132. this->limitScrolling(true);
  133. this->callback_pressedEvent();
  134. }
  135. this->pressedIndex = -1;
  136. }
  137. if (verticalScrollIntercepted) {
  138. this->hasImages = false; // Force redraw on scrollbar interception
  139. } else {
  140. VisualComponent::receiveMouseEvent(event);
  141. }
  142. }
  143. void ListBox::receiveKeyboardEvent(const KeyboardEvent& event) {
  144. if (event.keyboardEventType == KeyboardEventType::KeyType) {
  145. int64_t contentLength = this->list.value.length();
  146. int64_t oldIndex = this->selectedIndex.value;
  147. if (contentLength > 1) {
  148. if (oldIndex > 0 && event.dsrKey == DsrKey::DsrKey_UpArrow) {
  149. this->setSelectedIndex(oldIndex - 1, true);
  150. } else if (oldIndex < contentLength - 1 && event.dsrKey == DsrKey::DsrKey_DownArrow) {
  151. this->setSelectedIndex(oldIndex + 1, true);
  152. }
  153. }
  154. }
  155. VisualComponent::receiveKeyboardEvent(event);
  156. }
  157. void ListBox::loadTheme(VisualTheme theme) {
  158. this->scalableImage_listBox = theme_getScalableImage(theme, U"ListBox");
  159. this->verticalScrollBar.loadTheme(theme, this->backColor.value);
  160. }
  161. void ListBox::changedTheme(VisualTheme newTheme) {
  162. this->loadTheme(newTheme);
  163. this->hasImages = false; // Force redraw
  164. }
  165. void ListBox::loadFont() {
  166. if (!font_exists(this->font)) {
  167. this->font = font_getDefault();
  168. }
  169. if (!font_exists(this->font)) {
  170. throwError("Failed to load the default font for a ListBox!\n");
  171. }
  172. }
  173. void ListBox::completeAssets() {
  174. if (this->scalableImage_listBox.methodIndex == -1) {
  175. this->loadTheme(theme_getDefault());
  176. }
  177. this->loadFont();
  178. }
  179. void ListBox::changedLocation(const IRect &oldLocation, const IRect &newLocation) {
  180. // If the component has changed dimensions then redraw the image
  181. if (oldLocation.size() != newLocation.size()) {
  182. this->hasImages = false;
  183. this->limitScrolling();
  184. }
  185. }
  186. void ListBox::changedAttribute(const ReadableString &name) {
  187. if (!string_caseInsensitiveMatch(name, U"Visible")) {
  188. this->hasImages = false;
  189. }
  190. if (string_caseInsensitiveMatch(name, U"List")) {
  191. // Reset selection on full list updates
  192. this->setSelectedIndex(0, true);
  193. }
  194. this->limitSelection(false);
  195. this->limitScrolling();
  196. }
  197. void ListBox::setSelectedIndex(int64_t index, bool forceUpdate) {
  198. if (forceUpdate || this->selectedIndex.value != index) {
  199. this->selectedIndex.value = index;
  200. this->hasImages = false;
  201. this->callback_selectEvent(index);
  202. this->limitScrolling(true);
  203. }
  204. }
  205. int64_t ListBox::getSelectedIndex() {
  206. int64_t index = this->selectedIndex.value;
  207. if (this->selectedIndex.value < 0 || this->selectedIndex.value >= this->list.value.length()) {
  208. index = -1;
  209. }
  210. return index;
  211. }
  212. void ListBox::limitSelection(bool indexChangedMeaning) {
  213. // Get the maximum index
  214. int64_t maxIndex = this->list.value.length() - 1;
  215. if (maxIndex < 0) {
  216. maxIndex = 0;
  217. }
  218. // Reset selection on out of bound
  219. if (this->selectedIndex.value < 0 || this->selectedIndex.value > maxIndex) {
  220. setSelectedIndex(0, indexChangedMeaning);
  221. }
  222. }
  223. String ListBox::call(const ReadableString &methodName, const ReadableString &arguments) {
  224. if (string_caseInsensitiveMatch(methodName, U"ClearAll")) {
  225. // Remove all elements from the list
  226. this->list.value.clear();
  227. this->hasImages = false;
  228. this->selectedIndex.value = 0;
  229. this->limitScrolling();
  230. this->verticalScrollBar.setValue(0);
  231. return U"";
  232. } else if (string_caseInsensitiveMatch(methodName, U"PushElement")) {
  233. // Push a new element to the list
  234. // No quote mangling needed for this single argument.
  235. this->list.value.push(arguments);
  236. this->selectedIndex.value = this->list.value.length() - 1;
  237. this->limitScrolling(true);
  238. this->hasImages = false;
  239. return U"";
  240. } else if (string_caseInsensitiveMatch(methodName, U"RemoveElement")) {
  241. // Remove an element who's index is given in the only input argument
  242. int64_t index = string_toInteger(arguments);
  243. if (index < 0 || index >= this->list.value.length()) {
  244. throwError("Index (", arguments, " = ", index, ") out of bound in RemoveElement!\n");
  245. }
  246. this->list.value.remove(index);
  247. this->limitSelection(true);
  248. this->limitScrolling(true);
  249. this->hasImages = false;
  250. return U"";
  251. } else if (string_caseInsensitiveMatch(methodName, U"GetLength")) {
  252. // Returns the length of the list
  253. return string_combine(this->list.value.length());
  254. } else if (string_caseInsensitiveMatch(methodName, U"GetSelectedIndex")) {
  255. // Returns the selected index or -1 if nothing is selected
  256. return string_combine(this->getSelectedIndex());
  257. } else if (string_caseInsensitiveMatch(methodName, U"GetSelectedText")) {
  258. // Returns the selected element's text or an empty string if nothing is selected
  259. int64_t index = getSelectedIndex();
  260. if (index > -1) {
  261. return this->list.value[index];
  262. } else {
  263. return U"";
  264. }
  265. } else {
  266. return VisualComponent::call(methodName, arguments);
  267. }
  268. }