ListBox.cpp 11 KB

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