ListBox.cpp 10 KB

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