ListBox.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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. }
  33. Persistent* ListBox::findAttribute(const ReadableString &name) {
  34. if (string_caseInsensitiveMatch(name, U"Color")) {
  35. return &(this->color);
  36. } else if (string_caseInsensitiveMatch(name, U"List")) {
  37. return &(this->list);
  38. } else if (string_caseInsensitiveMatch(name, U"SelectedIndex")) {
  39. return &(this->selectedIndex);
  40. } else {
  41. return VisualComponent::findAttribute(name);
  42. }
  43. }
  44. ListBox::ListBox() {}
  45. bool ListBox::isContainer() const {
  46. return true;
  47. }
  48. static const int textBorderLeft = 6;
  49. static const int textBorderTop = 4;
  50. static const int scrollWidth = 15; // The width of the scroll bar
  51. static const int scrollEndHeight = 15; // The height of upper and lower scroll buttons
  52. static const int border = 1; // Scroll-bar edge thickness
  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. if (this->hasVerticalScroll) {
  80. ColorRgbaI32 buttonColor = ColorRgbaI32(this->color.value, 255);
  81. ColorRgbaI32 barColor = ColorRgbaI32(this->color.value.red / 2, this->color.value.green / 2, this->color.value.blue / 2, 255);
  82. ColorRgbaI32 borderColor = ColorRgbaI32(0, 0, 0, 255);
  83. IRect whole = IRect(this->location.width() - scrollWidth, 0, scrollWidth, this->location.height());
  84. IRect upper = IRect(whole.left() + border, whole.top() + border, whole.width() - border * 2, scrollEndHeight - border * 2);
  85. IRect middle = IRect(whole.left() + border, whole.top() + scrollEndHeight + border, whole.width() - border * 2, whole.height() - (border + scrollEndHeight) * 2);
  86. IRect lower = IRect(whole.left() + border, whole.bottom() - scrollEndHeight + border, whole.width() - border * 2, scrollEndHeight - border * 2);
  87. // Upper button
  88. draw_rectangle(this->image, whole, borderColor);
  89. draw_rectangle(this->image, upper, buttonColor);
  90. draw_rectangle(this->image, middle, barColor);
  91. draw_rectangle(this->image, lower, buttonColor);
  92. }
  93. this->hasImages = true;
  94. }
  95. }
  96. void ListBox::drawSelf(ImageRgbaU8& targetImage, const IRect &relativeLocation) {
  97. this->generateGraphics();
  98. draw_copy(targetImage, this->image, relativeLocation.left(), relativeLocation.top());
  99. }
  100. void ListBox::pressScrollBar(int64_t localY) {
  101. int64_t maxScroll = this->list.value.length() - this->getVisibleScrollRange();
  102. // The height of the scroll-bar excluding upper and lower buttons
  103. int64_t barHeight = this->location.height() - (scrollEndHeight * 2);
  104. this->firstVisible = ((localY - scrollEndHeight) * maxScroll) / barHeight;
  105. this->limitScrolling();
  106. this->hasImages = false; // Force redraw
  107. }
  108. void ListBox::receiveMouseEvent(const MouseEvent& event) {
  109. bool supressEvent = false;
  110. this->inside = this->pointIsInside(event.position);
  111. bool onScrollBar = this->hasVerticalScroll && event.position.x >= this->location.width() - scrollWidth;
  112. int64_t maxIndex = this->list.value.length() - 1;
  113. int64_t hoverIndex = this->firstVisible + ((event.position.y - this->location.top() - textBorderTop) / this->font->size);
  114. if (hoverIndex > maxIndex) {
  115. hoverIndex = -1;
  116. }
  117. if (event.mouseEventType == MouseEventType::MouseDown) {
  118. if (onScrollBar) {
  119. this->pressedIndex = -1;
  120. if (event.position.y < scrollEndHeight) {
  121. // Upper scroll button
  122. } else if (event.position.y > this->location.height() - scrollEndHeight) {
  123. // Lower scroll button
  124. } else {
  125. // Start scrolling with the mouse using the relative height on the scroll bar.
  126. this->pressScrollBar(event.position.y);
  127. // Repeat on mouse move
  128. this->holdingScrollBar = true;
  129. }
  130. } else {
  131. this->pressedIndex = hoverIndex;
  132. }
  133. this->hasImages = false; // Force redraw
  134. } else if (this->pressedIndex > -1 && event.mouseEventType == MouseEventType::MouseUp) {
  135. if (this->inside && !onScrollBar && hoverIndex == this->pressedIndex) {
  136. this->selectedIndex.value = hoverIndex;
  137. this->limitScrolling(true);
  138. this->callback_pressedEvent();
  139. }
  140. this->pressedIndex = -1;
  141. this->holdingScrollBar = false;
  142. this->hasImages = false; // Force redraw
  143. } else if (event.mouseEventType == MouseEventType::Scroll) {
  144. if (event.key == MouseKeyEnum::ScrollUp) {
  145. this->firstVisible--;
  146. } else if (event.key == MouseKeyEnum::ScrollDown) {
  147. this->firstVisible++;
  148. }
  149. this->limitScrolling();
  150. this->hasImages = false; // Force redraw
  151. } else if (event.mouseEventType == MouseEventType::MouseMove) {
  152. if (this->holdingScrollBar) {
  153. supressEvent = true;
  154. this->pressScrollBar(event.position.y);
  155. }
  156. }
  157. if (!supressEvent) {
  158. VisualComponent::receiveMouseEvent(event);
  159. }
  160. }
  161. void ListBox::changedTheme(VisualTheme newTheme) {
  162. this->listBox = theme_getScalableImage(newTheme, U"Panel");
  163. this->hasImages = false; // Force redraw
  164. }
  165. void ListBox::completeAssets() {
  166. if (this->listBox.methodIndex == -1) {
  167. this->listBox = theme_getScalableImage(theme_getDefault(), U"Panel");
  168. }
  169. if (this->font.get() == nullptr) {
  170. this->font = font_getDefault();
  171. }
  172. }
  173. void ListBox::changedLocation(IRect &oldLocation, IRect &newLocation) {
  174. // If the component has changed dimensions then redraw the image
  175. if (oldLocation.size() != newLocation.size()) {
  176. this->hasImages = false;
  177. this->limitScrolling();
  178. }
  179. }
  180. void ListBox::changedAttribute(const ReadableString &name) {
  181. // If an attribute has changed then force the image to be redrawn
  182. this->hasImages = false;
  183. if (string_caseInsensitiveMatch(name, U"List")) {
  184. // Reset selection on full list updates
  185. this->selectedIndex.value = 0;
  186. }
  187. this->limitSelection();
  188. this->limitScrolling();
  189. }
  190. int64_t ListBox::getSelectedIndex() {
  191. int64_t index = this->selectedIndex.value;
  192. if (this->selectedIndex.value < 0 || this->selectedIndex.value >= this->list.value.length()) {
  193. index = -1;
  194. }
  195. return index;
  196. }
  197. void ListBox::limitSelection() {
  198. // Get the maximum index
  199. int maxIndex = this->list.value.length() - 1;
  200. if (maxIndex < 0) {
  201. maxIndex = 0;
  202. }
  203. // Reset selection on out of bound
  204. if (this->selectedIndex.value < 0 || this->selectedIndex.value > maxIndex) {
  205. this->selectedIndex.value = 0;
  206. }
  207. }
  208. int64_t ListBox::getVisibleScrollRange() {
  209. int64_t verticalStep = this->font->size;
  210. return (this->location.height() - textBorderTop * 2) / verticalStep;
  211. }
  212. // Optional limit of scrolling, to be applied when the user don't explicitly scroll away from the selection
  213. // limitSelection should be called before limitScrolling, because scrolling limits depend on selection
  214. void ListBox::limitScrolling(bool keepSelectedVisible) {
  215. // Try to load the font before estimating how big the view is
  216. this->completeAssets();
  217. if (this->font.get() == nullptr) {
  218. throwError("Cannot get the font size because ListBox failed to get a font!\n");
  219. }
  220. int64_t itemCount = this->list.value.length();
  221. int64_t visibleRange = this->getVisibleScrollRange();
  222. int64_t maxScroll;
  223. int64_t minScroll;
  224. // Big enough list to need scrolling but big enough list-box to fit two buttons inside
  225. this->hasVerticalScroll = itemCount > visibleRange && this->location.width() >= scrollWidth * 2 && this->location.height() >= scrollEndHeight * 2;
  226. if (keepSelectedVisible) {
  227. maxScroll = this->selectedIndex.value;
  228. minScroll = maxScroll + 1 - visibleRange;
  229. } else {
  230. maxScroll = itemCount - visibleRange;
  231. minScroll = 0;
  232. }
  233. if (this->firstVisible > maxScroll) {
  234. this->firstVisible = maxScroll;
  235. }
  236. if (this->firstVisible < minScroll) {
  237. this->firstVisible = minScroll;
  238. }
  239. }
  240. String ListBox::call(const ReadableString &methodName, const ReadableString &arguments) {
  241. if (string_caseInsensitiveMatch(methodName, U"ClearAll")) {
  242. // Remove all elements from the list
  243. this->list.value.clear();
  244. this->hasImages = false;
  245. this->selectedIndex.value = 0;
  246. this->limitScrolling();
  247. this->firstVisible = 0;
  248. return U"";
  249. } else if (string_caseInsensitiveMatch(methodName, U"PushElement")) {
  250. // Push a new element to the list
  251. // No quote mangling needed for this single argument.
  252. this->list.value.push(arguments);
  253. this->selectedIndex.value = this->list.value.length() - 1;
  254. this->limitScrolling(true);
  255. this->hasImages = false;
  256. return U"";
  257. } else if (string_caseInsensitiveMatch(methodName, U"RemoveElement")) {
  258. // Remove an element who's index is given in the only input argument
  259. int64_t index = string_parseInteger(arguments);
  260. if (index < 0 || index >= this->list.value.length()) {
  261. throwError("Index (", arguments, " = ", index, ") out of bound in RemoveElement!\n");
  262. }
  263. this->list.value.remove(index);
  264. this->limitSelection();
  265. this->limitScrolling(true);
  266. this->hasImages = false;
  267. return U"";
  268. } else if (string_caseInsensitiveMatch(methodName, U"GetLength")) {
  269. // Returns the length of the list
  270. return string_combine(this->list.value.length());
  271. } else if (string_caseInsensitiveMatch(methodName, U"GetSelectedIndex")) {
  272. // Returns the selected index or -1 if nothing is selected
  273. return string_combine(this->getSelectedIndex());
  274. } else if (string_caseInsensitiveMatch(methodName, U"GetSelectedText")) {
  275. // Returns the selected element's text or an empty string if nothing is selected
  276. int64_t index = getSelectedIndex();
  277. if (index > -1) {
  278. return this->list.value[index];
  279. } else {
  280. return U"";
  281. }
  282. } else {
  283. return VisualComponent::call(methodName, arguments);
  284. }
  285. }