ListView.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. //
  2. // Urho3D Engine
  3. // Copyright (c) 2008-2011 Lasse Öörni
  4. //
  5. // Permission is hereby granted, free of charge, to any person obtaining a copy
  6. // of this software and associated documentation files (the "Software"), to deal
  7. // in the Software without restriction, including without limitation the rights
  8. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. // copies of the Software, and to permit persons to whom the Software is
  10. // furnished to do so, subject to the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be included in
  13. // all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  16. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  17. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  18. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  19. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  20. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  21. // THE SOFTWARE.
  22. //
  23. #include "Precompiled.h"
  24. #include "BorderImage.h"
  25. #include "InputEvents.h"
  26. #include "ListView.h"
  27. #include "UIEvents.h"
  28. #include "DebugNew.h"
  29. ListView::ListView(const std::string& name) :
  30. ScrollView(name),
  31. mSelection(M_MAX_UNSIGNED),
  32. mShowSelectionAlways(false)
  33. {
  34. UIElement* container = new UIElement();
  35. container->setEnabled(true);
  36. container->setLayout(O_VERTICAL, LM_RESIZECHILDREN, LM_RESIZEELEMENT);
  37. mScrollPanel->setLayout(O_HORIZONTAL, LM_RESIZECHILDREN, LM_FREE);
  38. setContentElement(container);
  39. subscribeToEvent(EVENT_TRYFOCUS, EVENT_HANDLER(ListView, handleTryFocus));
  40. }
  41. ListView::~ListView()
  42. {
  43. }
  44. void ListView::setStyle(const XMLElement& element, ResourceCache* cache)
  45. {
  46. ScrollView::setStyle(element, cache);
  47. UIElement* root = getRootElement();
  48. XMLElement itemElem = element.getChildElement("listitem");
  49. if (root)
  50. {
  51. while (itemElem)
  52. {
  53. if (itemElem.hasAttribute("name"))
  54. addItem(root->getChild(itemElem.getString("name"), true));
  55. itemElem = itemElem.getNextElement("listitem");
  56. }
  57. }
  58. if (element.hasChildElement("selection"))
  59. setSelection(element.getChildElement("selection").getInt("value"));
  60. if (element.hasChildElement("showselectionalways"))
  61. setShowSelectionAlways(element.getChildElement("showselectionalways").getBool("enable"));
  62. }
  63. void ListView::onWheel(int delta, int buttons, int qualifiers)
  64. {
  65. if (delta > 0)
  66. changeSelection(-1);
  67. if (delta < 0)
  68. changeSelection(1);
  69. }
  70. void ListView::onKey(int key, int buttons, int qualifiers)
  71. {
  72. // If no selection, can not move with keys
  73. if ((mSelection == M_MAX_UNSIGNED) || (!getNumItems()))
  74. return;
  75. switch (key)
  76. {
  77. case KEY_UP:
  78. if (qualifiers & QUAL_CTRL)
  79. setSelection(0);
  80. else
  81. changeSelection(-1);
  82. break;
  83. case KEY_DOWN:
  84. if (qualifiers & QUAL_CTRL)
  85. setSelection(getNumItems() - 1);
  86. else
  87. changeSelection(1);
  88. break;
  89. case KEY_PAGEUP:
  90. {
  91. // Convert page step to pixels and see how many items we have to skip to reach that many pixels
  92. int stepPixels = ((int)(mPageStep * mScrollPanel->getHeight())) - getSelectedItem()->getHeight();
  93. unsigned newSelection = mSelection;
  94. while (newSelection)
  95. {
  96. int height = getItem(newSelection)->getHeight();
  97. if (stepPixels < height)
  98. break;
  99. stepPixels -= height;
  100. --newSelection;
  101. }
  102. setSelection(newSelection);
  103. }
  104. break;
  105. case KEY_PAGEDOWN:
  106. {
  107. int stepPixels = ((int)(mPageStep * mScrollPanel->getHeight())) - getSelectedItem()->getHeight();
  108. unsigned newSelection = mSelection;
  109. while (newSelection < getNumItems() - 1)
  110. {
  111. int height = getItem(newSelection)->getHeight();
  112. if (stepPixels < height)
  113. break;
  114. stepPixels -= height;
  115. ++newSelection;
  116. }
  117. setSelection(newSelection);
  118. }
  119. break;
  120. case KEY_HOME:
  121. setSelection(0);
  122. break;
  123. case KEY_END:
  124. setSelection(getNumItems() - 1);
  125. break;
  126. }
  127. }
  128. void ListView::onFocus()
  129. {
  130. updateSelectionEffect();
  131. }
  132. void ListView::onDefocus()
  133. {
  134. updateSelectionEffect();
  135. }
  136. void ListView::addItem(UIElement* item)
  137. {
  138. if ((!item) || (item->getParent() == mContentElement))
  139. return;
  140. // Enable input so that clicking the item can be detected
  141. item->setEnabled(true);
  142. mContentElement->addChild(item);
  143. }
  144. void ListView::removeItem(UIElement* item)
  145. {
  146. std::vector<UIElement*> items = mContentElement->getChildren();
  147. for (unsigned i = 0; i < items.size(); ++i)
  148. {
  149. if (items[i] == item)
  150. {
  151. if (mSelection == i)
  152. clearSelection();
  153. else if (mSelection > i)
  154. changeSelection(-1);
  155. break;
  156. }
  157. }
  158. mContentElement->removeChild(item);
  159. }
  160. void ListView::removeItem(unsigned index)
  161. {
  162. if (index >= getNumItems())
  163. return;
  164. UIElement* item = mContentElement->getChild(index);
  165. if (mSelection == index)
  166. clearSelection();
  167. else if (mSelection > index)
  168. changeSelection(-1);
  169. mContentElement->removeChild(item);
  170. }
  171. void ListView::removeAllItems()
  172. {
  173. mContentElement->removeAllChildren();
  174. clearSelection();
  175. }
  176. void ListView::setSelection(unsigned index)
  177. {
  178. if (index >= getNumItems())
  179. index = M_MAX_UNSIGNED;
  180. bool changed = index != mSelection;
  181. mSelection = index;
  182. updateSelectionEffect();
  183. ensureItemVisibility();
  184. if (changed)
  185. {
  186. using namespace ItemSelected;
  187. VariantMap eventData;
  188. eventData[P_ELEMENT] = (void*)this;
  189. eventData[P_SELECTION] = mSelection;
  190. sendEvent(EVENT_ITEMSELECTED, eventData);
  191. }
  192. }
  193. void ListView::changeSelection(int delta)
  194. {
  195. if (mSelection == M_MAX_UNSIGNED)
  196. return;
  197. int newSelection = clamp((int)mSelection + delta, 0, (int)getNumItems() - 1);
  198. setSelection(newSelection);
  199. }
  200. void ListView::clearSelection()
  201. {
  202. setSelection(M_MAX_UNSIGNED);
  203. }
  204. void ListView::setShowSelectionAlways(bool enable)
  205. {
  206. mShowSelectionAlways = enable;
  207. }
  208. unsigned ListView::getNumItems() const
  209. {
  210. return mContentElement->getNumChildren();
  211. }
  212. UIElement* ListView::getItem(unsigned index) const
  213. {
  214. return mContentElement->getChild(index);
  215. }
  216. std::vector<UIElement*> ListView::getItems() const
  217. {
  218. return mContentElement->getChildren();
  219. }
  220. UIElement* ListView::getSelectedItem() const
  221. {
  222. return mContentElement->getChild(mSelection);
  223. }
  224. void ListView::updateSelectionEffect()
  225. {
  226. unsigned numItems = getNumItems();
  227. for (unsigned i = 0; i < numItems; ++i)
  228. getItem(i)->setSelected((i == mSelection) && ((mFocus) || (mShowSelectionAlways)));
  229. }
  230. void ListView::ensureItemVisibility()
  231. {
  232. UIElement* selected = getSelectedItem();
  233. if (!selected)
  234. return;
  235. IntVector2 currentOffset = selected->getScreenPosition() - mScrollPanel->getScreenPosition() - mContentElement->getPosition();
  236. IntVector2 newView = getViewPosition();
  237. const IntRect& clipBorder = mScrollPanel->getClipBorder();
  238. IntVector2 windowSize(mScrollPanel->getWidth() - clipBorder.mLeft - clipBorder.mRight, mScrollPanel->getHeight() -
  239. clipBorder.mTop - clipBorder.mBottom);
  240. if (currentOffset.mX < 0)
  241. newView.mX += currentOffset.mX;
  242. if (currentOffset.mX + selected->getWidth() > windowSize.mX)
  243. newView.mX += currentOffset.mX + selected->getWidth() - windowSize.mX;
  244. if (currentOffset.mY < 0)
  245. newView.mY += currentOffset.mY;
  246. if (currentOffset.mY + selected->getHeight() > windowSize.mY)
  247. newView.mY += currentOffset.mY + selected->getHeight() - windowSize.mY;
  248. setViewPosition(newView);
  249. }
  250. void ListView::handleTryFocus(StringHash eventType, VariantMap& eventData)
  251. {
  252. using namespace TryFocus;
  253. UIElement* focusElement = static_cast<UIElement*>(eventData[P_ELEMENT].getPtr());
  254. unsigned numItems = getNumItems();
  255. for (unsigned i = 0; i < numItems; ++i)
  256. {
  257. if (focusElement == getItem(i))
  258. {
  259. setSelection(i);
  260. return;
  261. }
  262. }
  263. }