ListBox.cpp 11 KB

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