ListBox.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. // zlib open source license
  2. //
  3. // Copyright (c) 2020 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. #include <math.h>
  25. using namespace dsr;
  26. PERSISTENT_DEFINITION(ListBox)
  27. void ListBox::declareAttributes(StructureDefinition &target) const {
  28. VisualComponent::declareAttributes(target);
  29. target.declareAttribute(U"Color");
  30. target.declareAttribute(U"List");
  31. target.declareAttribute(U"SelectedIndex");
  32. target.declareAttribute(U"LockScrolling");
  33. }
  34. Persistent* ListBox::findAttribute(const ReadableString &name) {
  35. if (string_caseInsensitiveMatch(name, U"Color")) {
  36. return &(this->color);
  37. } else if (string_caseInsensitiveMatch(name, U"List")) {
  38. return &(this->list);
  39. } else if (string_caseInsensitiveMatch(name, U"SelectedIndex")) {
  40. return &(this->selectedIndex);
  41. } else if (string_caseInsensitiveMatch(name, U"LockScrolling")) {
  42. return &(this->lockScrolling);
  43. } else {
  44. return VisualComponent::findAttribute(name);
  45. }
  46. }
  47. ListBox::ListBox() {}
  48. bool ListBox::isContainer() const {
  49. return true;
  50. }
  51. static const int textBorderLeft = 4;
  52. static const int textBorderTop = 4;
  53. void ListBox::generateGraphics() {
  54. int width = this->location.width();
  55. int height = this->location.height();
  56. if (width < 1) { width = 1; }
  57. if (height < 1) { height = 1; }
  58. if (!this->hasImages) {
  59. completeAssets();
  60. this->listBox(width, height, this->color.value.red, this->color.value.green, this->color.value.blue)(this->image);
  61. int verticalStep = this->font->size;
  62. int left = textBorderLeft;
  63. int top = textBorderTop;
  64. for (int64_t i = this->firstVisible; 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. this->font->printLine(this->image, this->list.value[i], IVector2D(left, top), textColor);
  77. top += verticalStep;
  78. }
  79. this->hasImages = true;
  80. }
  81. }
  82. void ListBox::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  83. this->generateGraphics();
  84. draw_copy(targetImage, this->image, relativeLocation.left(), relativeLocation.top());
  85. }
  86. void ListBox::receiveMouseEvent(const MouseEvent& event) {
  87. this->inside = this->pointIsInside(event.position);
  88. int64_t maxIndex = this->list.value.length() - 1;
  89. int64_t hoverIndex = this->firstVisible + ((event.position.y - this->location.top() - textBorderTop) / this->font->size);
  90. if (hoverIndex > maxIndex) {
  91. hoverIndex = -1;
  92. }
  93. if (event.mouseEventType == MouseEventType::MouseDown) {
  94. this->pressedIndex = hoverIndex;
  95. this->hasImages = false; // Force redraw
  96. } else if (this->pressedIndex > -1 && event.mouseEventType == MouseEventType::MouseUp) {
  97. if (this->inside && hoverIndex == this->pressedIndex) {
  98. this->selectedIndex.value = hoverIndex;
  99. this->limitScrolling(true);
  100. this->callback_pressedEvent();
  101. }
  102. this->pressedIndex = -1;
  103. this->hasImages = false; // Force redraw
  104. } else if (event.mouseEventType == MouseEventType::Scroll) {
  105. if (event.key == MouseKeyEnum::ScrollUp) {
  106. this->firstVisible--;
  107. } else if (event.key == MouseKeyEnum::ScrollDown) {
  108. this->firstVisible++;
  109. }
  110. this->limitScrolling();
  111. this->hasImages = false; // Force redraw
  112. }
  113. VisualComponent::receiveMouseEvent(event);
  114. }
  115. void ListBox::changedTheme(VisualTheme newTheme) {
  116. this->listBox = theme_getScalableImage(newTheme, U"Panel");
  117. this->hasImages = false; // Force redraw
  118. }
  119. void ListBox::completeAssets() {
  120. if (this->listBox.methodIndex == -1) {
  121. this->listBox = theme_getScalableImage(theme_getDefault(), U"Panel");
  122. }
  123. if (this->font.get() == nullptr) {
  124. this->font = font_getDefault();
  125. }
  126. }
  127. void ListBox::changedLocation(IRect &oldLocation, IRect &newLocation) {
  128. // If the component has changed dimensions then redraw the image
  129. if (oldLocation.size() != newLocation.size()) {
  130. this->hasImages = false;
  131. this->limitScrolling();
  132. }
  133. }
  134. void ListBox::changedAttribute(const ReadableString &name) {
  135. // If an attribute has changed then force the image to be redrawn
  136. this->hasImages = false;
  137. if (string_caseInsensitiveMatch(name, U"List")) {
  138. // Reset selection on full list updates
  139. this->selectedIndex.value = 0;
  140. }
  141. this->limitSelection();
  142. this->limitScrolling();
  143. }
  144. int64_t ListBox::getSelectedIndex() {
  145. int64_t index = this->selectedIndex.value;
  146. if (this->selectedIndex.value < 0 || this->selectedIndex.value >= this->list.value.length()) {
  147. index = -1;
  148. }
  149. return index;
  150. }
  151. void ListBox::limitSelection() {
  152. // Get the maximum index
  153. int maxIndex = this->list.value.length() - 1;
  154. if (maxIndex < 0) {
  155. maxIndex = 0;
  156. }
  157. // Reset selection on out of bound
  158. if (this->selectedIndex.value < 0 || this->selectedIndex.value > maxIndex) {
  159. this->selectedIndex.value = 0;
  160. }
  161. }
  162. // Optional limit of scrolling, to be applied when the user don't explicitly scroll away from the selection
  163. // limitSelection should be called before limitScrolling, because scrolling limits depend on selection
  164. void ListBox::limitScrolling(bool keepSelectedVisible) {
  165. if (this->lockScrolling.value) {
  166. this->firstVisible = 0;
  167. this->hasVerticalScroll = false;
  168. } else {
  169. // Try to load the font before estimating how big the view is
  170. this->completeAssets();
  171. if (this->font.get() == nullptr) {
  172. throwError("Cannot get the font size because ListBox failed to get a font!\n");
  173. }
  174. int itemCount = this->list.value.length();
  175. int verticalStep = this->font->size;
  176. int visibleRange = (this->location.height() - textBorderTop * 2) / verticalStep;
  177. int maxScroll;
  178. int minScroll;
  179. this->hasVerticalScroll = itemCount > visibleRange;
  180. if (keepSelectedVisible) {
  181. maxScroll = this->selectedIndex.value;
  182. minScroll = maxScroll + 1 - visibleRange;
  183. } else {
  184. maxScroll = itemCount - visibleRange;
  185. minScroll = 0;
  186. }
  187. if (this->firstVisible > maxScroll) {
  188. this->firstVisible = maxScroll;
  189. }
  190. if (this->firstVisible < minScroll) {
  191. this->firstVisible = minScroll;
  192. }
  193. }
  194. }
  195. String ListBox::call(const ReadableString &methodName, const ReadableString &arguments) {
  196. if (string_caseInsensitiveMatch(methodName, U"ClearAll")) {
  197. // Remove all elements from the list
  198. this->list.value.clear();
  199. this->hasImages = false;
  200. this->selectedIndex.value = 0;
  201. this->firstVisible = 0;
  202. return U"";
  203. } else if (string_caseInsensitiveMatch(methodName, U"PushElement")) {
  204. // Push a new element to the list
  205. // No quote mangling needed for this single argument.
  206. this->list.value.push(arguments);
  207. this->selectedIndex.value = this->list.value.length() - 1;
  208. this->limitScrolling(true);
  209. this->hasImages = false;
  210. return U"";
  211. } else if (string_caseInsensitiveMatch(methodName, U"RemoveElement")) {
  212. // Remove an element who's index is given in the only input argument
  213. int64_t index = string_parseInteger(arguments);
  214. if (index < 0 || index >= this->list.value.length()) {
  215. throwError("Index (", arguments, " = ", index, ") out of bound in RemoveElement!\n");
  216. }
  217. this->list.value.remove(index);
  218. this->limitSelection();
  219. this->limitScrolling(true);
  220. this->hasImages = false;
  221. return U"";
  222. } else if (string_caseInsensitiveMatch(methodName, U"GetLength")) {
  223. // Returns the length of the list
  224. return string_combine(this->list.value.length());
  225. } else if (string_caseInsensitiveMatch(methodName, U"GetSelectedIndex")) {
  226. // Returns the selected index or -1 if nothing is selected
  227. return string_combine(this->getSelectedIndex());
  228. } else if (string_caseInsensitiveMatch(methodName, U"GetSelectedText")) {
  229. // Returns the selected element's text or an empty string if nothing is selected
  230. int64_t index = getSelectedIndex();
  231. if (index > -1) {
  232. return this->list.value[index];
  233. } else {
  234. return U"";
  235. }
  236. } else {
  237. return VisualComponent::call(methodName, arguments);
  238. }
  239. }