ListView.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779
  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 "Context.h"
  26. #include "InputEvents.h"
  27. #include "ListView.h"
  28. #include "Log.h"
  29. #include "StringUtils.h"
  30. #include "UIEvents.h"
  31. #include "DebugNew.h"
  32. static const ShortStringHash indentHash("Indent");
  33. static const String highlightModes[] =
  34. {
  35. "never",
  36. "focus",
  37. "always",
  38. ""
  39. };
  40. int GetItemIndent(UIElement* item)
  41. {
  42. if (!item)
  43. return 0;
  44. return item->vars_[indentHash].GetInt();
  45. }
  46. OBJECTTYPESTATIC(ListView);
  47. ListView::ListView(Context* context) :
  48. ScrollView(context),
  49. highlightMode_(HM_FOCUS),
  50. multiselect_(false),
  51. hierarchyMode_(false),
  52. clearSelectionOnDefocus_(false),
  53. doubleClickInterval_(0.5f),
  54. doubleClickTimer_(0.0f),
  55. lastClickedItem_(M_MAX_UNSIGNED)
  56. {
  57. UIElement* container = new UIElement(context_);
  58. container->SetActive(true);
  59. container->SetLayout(LM_VERTICAL);
  60. SetContentElement(container);
  61. SubscribeToEvent(E_UIMOUSECLICK, HANDLER(ListView, HandleUIMouseClick));
  62. }
  63. ListView::~ListView()
  64. {
  65. }
  66. void ListView::RegisterObject(Context* context)
  67. {
  68. context->RegisterFactory<ListView>();
  69. }
  70. void ListView::SetStyle(const XMLElement& element)
  71. {
  72. ScrollView::SetStyle(element);
  73. UIElement* root = GetRoot();
  74. XMLElement itemElem = element.GetChild("listitem");
  75. if (root)
  76. {
  77. while (itemElem)
  78. {
  79. if (itemElem.HasAttribute("name"))
  80. {
  81. UIElement* item = root->GetChild(itemElem.GetString("name"), true);
  82. AddItem(item);
  83. if (itemElem.HasAttribute("indent"))
  84. item->vars_[indentHash] = itemElem.GetInt("indent");
  85. itemElem = itemElem.GetNext("listitem");
  86. }
  87. }
  88. }
  89. if (element.HasChild("highlight"))
  90. {
  91. String highlight = element.GetChild("highlight").GetStringLower("value");
  92. SetHighlightMode((HighlightMode)GetStringListIndex(highlight, highlightModes, HM_FOCUS));
  93. }
  94. if (element.HasChild("multiselect"))
  95. SetMultiselect(element.GetChild("multiselect").GetBool("enable"));
  96. if (element.HasChild("hierarchy"))
  97. SetHierarchyMode(element.GetChild("hierarchy").GetBool("enable"));
  98. if (element.HasChild("clearselection"))
  99. SetClearSelectionOnDefocus(element.GetChild("clearselection").GetBool("enable"));
  100. if (element.HasChild("doubleclickinterval"))
  101. SetDoubleClickInterval(element.GetChild("doubleclickinterval").GetFloat("value"));
  102. XMLElement selectionElem = element.GetChild("selection");
  103. while (selectionElem)
  104. {
  105. AddSelection(selectionElem.GetInt("value"));
  106. selectionElem = selectionElem.GetNext("selection");
  107. }
  108. // Set the container's layout border to match scroll panel clipping, so that elements are not left partially hidden
  109. contentElement_->SetLayoutBorder(scrollPanel_->GetClipBorder());
  110. }
  111. void ListView::Update(float timeStep)
  112. {
  113. if (doubleClickTimer_ > 0.0f)
  114. doubleClickTimer_ = Max(doubleClickTimer_ - timeStep, 0.0f);
  115. }
  116. void ListView::OnKey(int key, int buttons, int qualifiers)
  117. {
  118. // If no selection, can not move with keys
  119. unsigned numItems = GetNumItems();
  120. unsigned selection = GetSelection();
  121. if (selection != M_MAX_UNSIGNED && numItems)
  122. {
  123. // If either shift or ctrl held down, add to selection if multiselect enabled
  124. bool additive = multiselect_ && qualifiers != 0;
  125. switch (key)
  126. {
  127. case KEY_LEFT:
  128. if (hierarchyMode_)
  129. {
  130. SetChildItemsVisible(selection, false);
  131. return;
  132. }
  133. break;
  134. case KEY_RIGHT:
  135. if (hierarchyMode_)
  136. {
  137. SetChildItemsVisible(selection, true);
  138. return;
  139. }
  140. break;
  141. case KEY_RETURN:
  142. if (hierarchyMode_)
  143. {
  144. ToggleChildItemsVisible(selection);
  145. return;
  146. }
  147. break;
  148. case KEY_UP:
  149. ChangeSelection(-1, additive);
  150. return;
  151. case KEY_DOWN:
  152. ChangeSelection(1, additive);
  153. return;
  154. case KEY_PAGEUP:
  155. {
  156. // Convert page step to pixels and see how many items have to be skipped to reach that many pixels
  157. int stepPixels = ((int)(pageStep_ * scrollPanel_->GetHeight())) - GetSelectedItem()->GetHeight();
  158. unsigned newSelection = selection;
  159. unsigned okSelection = selection;
  160. while (newSelection < numItems)
  161. {
  162. UIElement* item = GetItem(newSelection);
  163. int height = 0;
  164. if (item->IsVisible())
  165. {
  166. height = item->GetHeight();
  167. okSelection = newSelection;
  168. }
  169. if (stepPixels < height)
  170. break;
  171. stepPixels -= height;
  172. --newSelection;
  173. }
  174. if (!additive)
  175. SetSelection(okSelection);
  176. else
  177. AddSelection(okSelection);
  178. }
  179. return;
  180. case KEY_PAGEDOWN:
  181. {
  182. int stepPixels = ((int)(pageStep_ * scrollPanel_->GetHeight())) - GetSelectedItem()->GetHeight();
  183. unsigned newSelection = selection;
  184. unsigned okSelection = selection;
  185. while (newSelection < numItems)
  186. {
  187. UIElement* item = GetItem(newSelection);
  188. int height = 0;
  189. if (item->IsVisible())
  190. {
  191. height = item->GetHeight();
  192. okSelection = newSelection;
  193. }
  194. if (stepPixels < height)
  195. break;
  196. stepPixels -= height;
  197. ++newSelection;
  198. }
  199. if (!additive)
  200. SetSelection(okSelection);
  201. else
  202. AddSelection(okSelection);
  203. }
  204. return;
  205. case KEY_HOME:
  206. ChangeSelection(-(int)GetNumItems(), additive);
  207. return;
  208. case KEY_END:
  209. ChangeSelection(GetNumItems(), additive);
  210. return;
  211. }
  212. }
  213. using namespace UnhandledKey;
  214. VariantMap eventData;
  215. eventData[P_ELEMENT] = (void*)this;
  216. eventData[P_KEY] = key;
  217. eventData[P_BUTTONS] = buttons;
  218. eventData[P_QUALIFIERS] = qualifiers;
  219. SendEvent(E_UNHANDLEDKEY, eventData);
  220. }
  221. void ListView::OnResize()
  222. {
  223. ScrollView::OnResize();
  224. // Set the content element width to match the scrollpanel
  225. contentElement_->SetWidth(scrollPanel_->GetWidth());
  226. }
  227. void ListView::OnFocus()
  228. {
  229. UpdateSelectionEffect();
  230. }
  231. void ListView::OnDefocus()
  232. {
  233. if (clearSelectionOnDefocus_)
  234. ClearSelection();
  235. UpdateSelectionEffect();
  236. }
  237. void ListView::AddItem(UIElement* item)
  238. {
  239. InsertItem(contentElement_->GetNumChildren(), item);
  240. }
  241. void ListView::InsertItem(unsigned index, UIElement* item)
  242. {
  243. if (!item || item->GetParent() == contentElement_)
  244. return;
  245. // Enable input so that clicking the item can be detected
  246. item->SetActive(true);
  247. item->SetSelected(false);
  248. contentElement_->InsertChild(index, item);
  249. // If necessary, shift the following selections
  250. Set<unsigned> prevSelections;
  251. selections_.Clear();
  252. for (Set<unsigned>::Iterator j = prevSelections.Begin(); j != prevSelections.End(); ++j)
  253. {
  254. if (*j >= index)
  255. selections_.Insert(*j + 1);
  256. else
  257. selections_.Insert(*j);
  258. }
  259. UpdateSelectionEffect();
  260. }
  261. void ListView::RemoveItem(UIElement* item)
  262. {
  263. unsigned numItems = GetNumItems();
  264. for (unsigned i = 0; i < numItems; ++i)
  265. {
  266. if (GetItem(i) == item)
  267. {
  268. item->SetSelected(false);
  269. selections_.Erase(i);
  270. // Remove any child items in hierarchy mode
  271. unsigned removed = 1;
  272. if (hierarchyMode_)
  273. {
  274. int baseIndent = GetItemIndent(item);
  275. int j = i + 1;
  276. for (;;)
  277. {
  278. UIElement* childItem = GetItem(i + 1);
  279. if (!childItem)
  280. break;
  281. if (GetItemIndent(childItem) > baseIndent)
  282. {
  283. childItem->SetSelected(false);
  284. selections_.Erase(j);
  285. contentElement_->RemoveChild(childItem);
  286. ++removed;
  287. }
  288. else
  289. break;
  290. ++j;
  291. }
  292. }
  293. // If necessary, shift the following selections
  294. Set<unsigned> prevSelections;
  295. selections_.Clear();
  296. for (Set<unsigned>::Iterator j = prevSelections.Begin(); j != prevSelections.End(); ++j)
  297. {
  298. if (*j > i)
  299. selections_.Insert(*j - removed);
  300. else
  301. selections_.Insert(*j);
  302. }
  303. UpdateSelectionEffect();
  304. break;
  305. }
  306. }
  307. contentElement_->RemoveChild(item);
  308. }
  309. void ListView::RemoveItem(unsigned index)
  310. {
  311. RemoveItem(GetItem(index));
  312. }
  313. void ListView::RemoveAllItems()
  314. {
  315. contentElement_->DisableLayoutUpdate();
  316. unsigned numItems = GetNumItems();
  317. for (unsigned i = 0; i < numItems; ++i)
  318. contentElement_->GetChild(i)->SetSelected(false);
  319. contentElement_->RemoveAllChildren();
  320. ClearSelection();
  321. contentElement_->EnableLayoutUpdate();
  322. contentElement_->UpdateLayout();
  323. OnResize();
  324. }
  325. void ListView::SetSelection(unsigned index)
  326. {
  327. Set<unsigned> indices;
  328. indices.Insert(index);
  329. SetSelections(indices);
  330. EnsureItemVisibility(index);
  331. }
  332. void ListView::SetSelections(const Set<unsigned>& indices)
  333. {
  334. unsigned numItems = GetNumItems();
  335. // Remove first items that should no longer be selected
  336. for (Set<unsigned>::Iterator i = selections_.Begin(); i != selections_.End();)
  337. {
  338. unsigned index = *i;
  339. if (indices.Find(index) == indices.End())
  340. {
  341. Set<unsigned>::Iterator current = i++;
  342. selections_.Erase(current);
  343. using namespace Iteselected_;
  344. VariantMap eventData;
  345. eventData[P_ELEMENT] = (void*)this;
  346. eventData[P_SELECTION] = index;
  347. SendEvent(E_ITEMDESELECTED, eventData);
  348. }
  349. else
  350. ++i;
  351. }
  352. // Then add missing items
  353. for (Set<unsigned>::ConstIterator i = indices.Begin(); i != indices.End(); ++i)
  354. {
  355. unsigned index = *i;
  356. if (index < numItems)
  357. {
  358. // In singleselect mode, resend the event even for the same selection
  359. if (selections_.Find(index) == selections_.End() || !multiselect_)
  360. {
  361. selections_.Insert(*i);
  362. using namespace Iteselected_;
  363. VariantMap eventData;
  364. eventData[P_ELEMENT] = (void*)this;
  365. eventData[P_SELECTION] = *i;
  366. SendEvent(E_ITEMSELECTED, eventData);
  367. }
  368. }
  369. // If no multiselect enabled, allow setting only one item
  370. if (!multiselect_)
  371. break;
  372. }
  373. UpdateSelectionEffect();
  374. }
  375. void ListView::AddSelection(unsigned index)
  376. {
  377. if (!multiselect_)
  378. SetSelection(index);
  379. else
  380. {
  381. if (index >= GetNumItems())
  382. return;
  383. Set<unsigned> newSelections = selections_;
  384. newSelections.Insert(index);
  385. SetSelections(newSelections);
  386. EnsureItemVisibility(index);
  387. }
  388. }
  389. void ListView::RemoveSelection(unsigned index)
  390. {
  391. if (index >= GetNumItems())
  392. return;
  393. Set<unsigned> newSelections = selections_;
  394. newSelections.Erase(index);
  395. SetSelections(newSelections);
  396. EnsureItemVisibility(index);
  397. }
  398. void ListView::ToggleSelection(unsigned index)
  399. {
  400. unsigned numItems = GetNumItems();
  401. if (index >= numItems)
  402. return;
  403. if (selections_.Find(index) != selections_.End())
  404. RemoveSelection(index);
  405. else
  406. AddSelection(index);
  407. }
  408. void ListView::ChangeSelection(int delta, bool additive)
  409. {
  410. if (selections_.Empty())
  411. return;
  412. if (!multiselect_)
  413. additive = false;
  414. // If going downwards, use the last selection as a base. Otherwise use first
  415. unsigned selection = delta > 0 ? selections_.Back() : selections_.Front();
  416. unsigned numItems = GetNumItems();
  417. unsigned newSelection = selection;
  418. unsigned okSelection = selection;
  419. while (delta != 0)
  420. {
  421. if (delta > 0)
  422. {
  423. ++newSelection;
  424. if (newSelection >= numItems)
  425. break;
  426. }
  427. if (delta < 0)
  428. {
  429. --newSelection;
  430. if (newSelection >= numItems)
  431. break;
  432. }
  433. UIElement* item = GetItem(newSelection);
  434. if (item->IsVisible())
  435. {
  436. okSelection = newSelection;
  437. if (delta > 0)
  438. --delta;
  439. if (delta < 0)
  440. ++delta;
  441. }
  442. }
  443. if (!additive)
  444. SetSelection(okSelection);
  445. else
  446. AddSelection(okSelection);
  447. }
  448. void ListView::ClearSelection()
  449. {
  450. SetSelections(Set<unsigned>());
  451. UpdateSelectionEffect();
  452. }
  453. void ListView::SetHighlightMode(HighlightMode mode)
  454. {
  455. highlightMode_ = mode;
  456. UpdateSelectionEffect();
  457. }
  458. void ListView::SetMultiselect(bool enable)
  459. {
  460. multiselect_ = enable;
  461. }
  462. void ListView::SetHierarchyMode(bool enable)
  463. {
  464. hierarchyMode_ = enable;
  465. }
  466. void ListView::SetClearSelectionOnDefocus(bool enable)
  467. {
  468. clearSelectionOnDefocus_ = enable;
  469. }
  470. void ListView::SetDoubleClickInterval(float interval)
  471. {
  472. doubleClickInterval_ = interval;
  473. }
  474. void ListView::SetChildItemsVisible(unsigned index, bool enable)
  475. {
  476. unsigned numItems = GetNumItems();
  477. if (!hierarchyMode_ || index >= numItems)
  478. return;
  479. contentElement_->DisableLayoutUpdate();
  480. int baseIndent = GetItemIndent(GetItem(index));
  481. for (unsigned i = index + 1; i < numItems; ++i)
  482. {
  483. UIElement* item = GetItem(i);
  484. if (GetItemIndent(item) > baseIndent)
  485. item->SetVisible(enable);
  486. else
  487. break;
  488. }
  489. contentElement_->EnableLayoutUpdate();
  490. contentElement_->UpdateLayout();
  491. OnResize();
  492. }
  493. void ListView::SetChildItemsVisible(bool enable)
  494. {
  495. unsigned numItems = GetNumItems();
  496. for (unsigned i = 0; i < numItems; ++i)
  497. {
  498. if (!GetItemIndent(GetItem(i)))
  499. SetChildItemsVisible(i, enable);
  500. }
  501. if (GetSelections().Size() == 1)
  502. EnsureItemVisibility(GetSelection());
  503. }
  504. void ListView::ToggleChildItemsVisible(unsigned index)
  505. {
  506. unsigned numItems = GetNumItems();
  507. if (!hierarchyMode_ || index >= numItems)
  508. return;
  509. int baseIndent = GetItemIndent(GetItem(index));
  510. bool firstChild = true;
  511. UIElement* prevItem = 0;
  512. for (unsigned i = index + 1; i < numItems; ++i)
  513. {
  514. UIElement* item = GetItem(i);
  515. if (GetItemIndent(item) > baseIndent)
  516. {
  517. if (firstChild)
  518. {
  519. item->SetVisible(!item->IsVisible());
  520. firstChild = false;
  521. }
  522. else
  523. item->SetVisible(prevItem->IsVisible());
  524. }
  525. else
  526. break;
  527. prevItem = item;
  528. }
  529. }
  530. unsigned ListView::GetNumItems() const
  531. {
  532. return contentElement_->GetNumChildren();
  533. }
  534. UIElement* ListView::GetItem(unsigned index) const
  535. {
  536. return contentElement_->GetChild(index);
  537. }
  538. PODVector<UIElement*> ListView::GetItems() const
  539. {
  540. return contentElement_->GetChildren();
  541. }
  542. unsigned ListView::GetSelection() const
  543. {
  544. if (selections_.Empty())
  545. return M_MAX_UNSIGNED;
  546. else
  547. return *selections_.Begin();
  548. }
  549. UIElement* ListView::GetSelectedItem() const
  550. {
  551. return contentElement_->GetChild(GetSelection());
  552. }
  553. PODVector<UIElement*> ListView::GetSelectedItems() const
  554. {
  555. PODVector<UIElement*> ret;
  556. for (Set<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
  557. {
  558. UIElement* item = GetItem(*i);
  559. if (item)
  560. ret.Push(item);
  561. }
  562. return ret;
  563. }
  564. void ListView::UpdateSelectionEffect()
  565. {
  566. unsigned numItems = GetNumItems();
  567. for (unsigned i = 0; i < numItems; ++i)
  568. {
  569. UIElement* item = GetItem(i);
  570. if (highlightMode_ != HM_NEVER && selections_.Find(i) != selections_.End())
  571. item->SetSelected(focus_ || highlightMode_ == HM_ALWAYS);
  572. else
  573. item->SetSelected(false);
  574. }
  575. }
  576. void ListView::EnsureItemVisibility(unsigned index)
  577. {
  578. UIElement* item = GetItem(index);
  579. if (!item || !item->IsVisible())
  580. return;
  581. IntVector2 currentOffset = item->GetScreenPosition() - scrollPanel_->GetScreenPosition() - contentElement_->GetPosition();
  582. IntVector2 newView = GetViewPosition();
  583. const IntRect& clipBorder = scrollPanel_->GetClipBorder();
  584. IntVector2 windowSize(scrollPanel_->GetWidth() - clipBorder.left_ - clipBorder.right_, scrollPanel_->GetHeight() -
  585. clipBorder.top_ - clipBorder.bottom_);
  586. if (currentOffset.y_ < 0)
  587. newView.y_ += currentOffset.y_;
  588. if (currentOffset.y_ + item->GetHeight() > windowSize.y_)
  589. newView.y_ += currentOffset.y_ + item->GetHeight() - windowSize.y_;
  590. SetViewPosition(newView);
  591. }
  592. void ListView::HandleUIMouseClick(StringHash eventType, VariantMap& eventData)
  593. {
  594. if (eventData[UIMouseClick::P_BUTTON].GetInt() != MOUSEB_LEFT)
  595. return;
  596. int qualifiers = eventData[UIMouseClick::P_QUALIFIERS].GetInt();
  597. UIElement* element = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  598. unsigned numItems = GetNumItems();
  599. for (unsigned i = 0; i < numItems; ++i)
  600. {
  601. if (element == GetItem(i))
  602. {
  603. // Check doubleclick
  604. bool isDoubleClick = false;
  605. if (!multiselect_ || !qualifiers)
  606. {
  607. if (doubleClickTimer_ > 0.0f && lastClickedItem_ == i)
  608. {
  609. isDoubleClick = true;
  610. doubleClickTimer_ = 0.0f;
  611. }
  612. else
  613. {
  614. doubleClickTimer_ = doubleClickInterval_;
  615. lastClickedItem_ = i;
  616. }
  617. SetSelection(i);
  618. }
  619. // Check multiselect with shift & ctrl
  620. if (multiselect_)
  621. {
  622. if (qualifiers & QUAL_SHIFT)
  623. {
  624. if (selections_.Empty())
  625. SetSelection(i);
  626. else
  627. {
  628. unsigned first = selections_.Front();
  629. unsigned last = selections_.Back();
  630. Set<unsigned> newSelections = selections_;
  631. if (i == first || i == last)
  632. {
  633. for (unsigned j = first; j <= last; ++j)
  634. newSelections.Insert(j);
  635. }
  636. else if (i < first)
  637. {
  638. for (unsigned j = i; j <= first; ++j)
  639. newSelections.Insert(j);
  640. }
  641. else if (i < last)
  642. {
  643. if ((abs((int)i - (int)first)) <= (abs((int)i - (int)last)))
  644. {
  645. for (unsigned j = first; j <= i; ++j)
  646. newSelections.Insert(j);
  647. }
  648. else
  649. {
  650. for (unsigned j = i; j <= last; ++j)
  651. newSelections.Insert(j);
  652. }
  653. }
  654. else if (i > last)
  655. {
  656. for (unsigned j = last; j <= i; ++j)
  657. newSelections.Insert(j);
  658. }
  659. SetSelections(newSelections);
  660. }
  661. }
  662. else if (qualifiers & QUAL_CTRL)
  663. ToggleSelection(i);
  664. }
  665. if (isDoubleClick)
  666. {
  667. if (hierarchyMode_)
  668. ToggleChildItemsVisible(i);
  669. VariantMap eventData;
  670. eventData[ItemDoubleClicked::P_ELEMENT] = (void*)this;
  671. eventData[ItemDoubleClicked::P_SELECTION] = i;
  672. SendEvent(E_ITEMDOUBLECLICKED, eventData);
  673. }
  674. return;
  675. }
  676. }
  677. }