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. }
  324. void ListView::SetSelection(unsigned index)
  325. {
  326. Set<unsigned> indices;
  327. indices.Insert(index);
  328. SetSelections(indices);
  329. EnsureItemVisibility(index);
  330. }
  331. void ListView::SetSelections(const Set<unsigned>& indices)
  332. {
  333. unsigned numItems = GetNumItems();
  334. // Remove first items that should no longer be selected
  335. for (Set<unsigned>::Iterator i = selections_.Begin(); i != selections_.End();)
  336. {
  337. unsigned index = *i;
  338. if (indices.Find(index) == indices.End())
  339. {
  340. Set<unsigned>::Iterator current = i++;
  341. selections_.Erase(current);
  342. using namespace ItemSelected;
  343. VariantMap eventData;
  344. eventData[P_ELEMENT] = (void*)this;
  345. eventData[P_SELECTION] = index;
  346. SendEvent(E_ITEMDESELECTED, eventData);
  347. }
  348. else
  349. ++i;
  350. }
  351. // Then add missing items
  352. for (Set<unsigned>::ConstIterator i = indices.Begin(); i != indices.End(); ++i)
  353. {
  354. unsigned index = *i;
  355. if (index < numItems)
  356. {
  357. // In singleselect mode, resend the event even for the same selection
  358. if (selections_.Find(index) == selections_.End() || !multiselect_)
  359. {
  360. selections_.Insert(*i);
  361. using namespace ItemSelected;
  362. VariantMap eventData;
  363. eventData[P_ELEMENT] = (void*)this;
  364. eventData[P_SELECTION] = *i;
  365. SendEvent(E_ITEMSELECTED, eventData);
  366. }
  367. }
  368. // If no multiselect enabled, allow setting only one item
  369. if (!multiselect_)
  370. break;
  371. }
  372. UpdateSelectionEffect();
  373. }
  374. void ListView::AddSelection(unsigned index)
  375. {
  376. if (!multiselect_)
  377. SetSelection(index);
  378. else
  379. {
  380. if (index >= GetNumItems())
  381. return;
  382. Set<unsigned> newSelections = selections_;
  383. newSelections.Insert(index);
  384. SetSelections(newSelections);
  385. EnsureItemVisibility(index);
  386. }
  387. }
  388. void ListView::RemoveSelection(unsigned index)
  389. {
  390. if (index >= GetNumItems())
  391. return;
  392. Set<unsigned> newSelections = selections_;
  393. newSelections.Erase(index);
  394. SetSelections(newSelections);
  395. EnsureItemVisibility(index);
  396. }
  397. void ListView::ToggleSelection(unsigned index)
  398. {
  399. unsigned numItems = GetNumItems();
  400. if (index >= numItems)
  401. return;
  402. if (selections_.Find(index) != selections_.End())
  403. RemoveSelection(index);
  404. else
  405. AddSelection(index);
  406. }
  407. void ListView::ChangeSelection(int delta, bool additive)
  408. {
  409. if (selections_.Empty())
  410. return;
  411. if (!multiselect_)
  412. additive = false;
  413. // If going downwards, use the last selection as a base. Otherwise use first
  414. unsigned selection = delta > 0 ? selections_.Back() : selections_.Front();
  415. unsigned numItems = GetNumItems();
  416. unsigned newSelection = selection;
  417. unsigned okSelection = selection;
  418. while (delta != 0)
  419. {
  420. if (delta > 0)
  421. {
  422. ++newSelection;
  423. if (newSelection >= numItems)
  424. break;
  425. }
  426. if (delta < 0)
  427. {
  428. --newSelection;
  429. if (newSelection >= numItems)
  430. break;
  431. }
  432. UIElement* item = GetItem(newSelection);
  433. if (item->IsVisible())
  434. {
  435. okSelection = newSelection;
  436. if (delta > 0)
  437. --delta;
  438. if (delta < 0)
  439. ++delta;
  440. }
  441. }
  442. if (!additive)
  443. SetSelection(okSelection);
  444. else
  445. AddSelection(okSelection);
  446. }
  447. void ListView::ClearSelection()
  448. {
  449. SetSelections(Set<unsigned>());
  450. UpdateSelectionEffect();
  451. }
  452. void ListView::SetHighlightMode(HighlightMode mode)
  453. {
  454. highlightMode_ = mode;
  455. UpdateSelectionEffect();
  456. }
  457. void ListView::SetMultiselect(bool enable)
  458. {
  459. multiselect_ = enable;
  460. }
  461. void ListView::SetHierarchyMode(bool enable)
  462. {
  463. hierarchyMode_ = enable;
  464. }
  465. void ListView::SetClearSelectionOnDefocus(bool enable)
  466. {
  467. clearSelectionOnDefocus_ = enable;
  468. }
  469. void ListView::SetDoubleClickInterval(float interval)
  470. {
  471. doubleClickInterval_ = interval;
  472. }
  473. void ListView::SetChildItemsVisible(unsigned index, bool enable)
  474. {
  475. unsigned numItems = GetNumItems();
  476. if (!hierarchyMode_ || index >= numItems)
  477. return;
  478. contentElement_->DisableLayoutUpdate();
  479. int baseIndent = GetItemIndent(GetItem(index));
  480. for (unsigned i = index + 1; i < numItems; ++i)
  481. {
  482. UIElement* item = GetItem(i);
  483. if (GetItemIndent(item) > baseIndent)
  484. item->SetVisible(enable);
  485. else
  486. break;
  487. }
  488. contentElement_->EnableLayoutUpdate();
  489. contentElement_->UpdateLayout();
  490. }
  491. void ListView::SetChildItemsVisible(bool enable)
  492. {
  493. unsigned numItems = GetNumItems();
  494. for (unsigned i = 0; i < numItems; ++i)
  495. {
  496. if (!GetItemIndent(GetItem(i)))
  497. SetChildItemsVisible(i, enable);
  498. }
  499. if (GetSelections().Size() == 1)
  500. EnsureItemVisibility(GetSelection());
  501. }
  502. void ListView::ToggleChildItemsVisible(unsigned index)
  503. {
  504. unsigned numItems = GetNumItems();
  505. if (!hierarchyMode_ || index >= numItems)
  506. return;
  507. contentElement_->DisableLayoutUpdate();
  508. int baseIndent = GetItemIndent(GetItem(index));
  509. bool firstChild = true;
  510. UIElement* prevItem = 0;
  511. for (unsigned i = index + 1; i < numItems; ++i)
  512. {
  513. UIElement* item = GetItem(i);
  514. if (GetItemIndent(item) > baseIndent)
  515. {
  516. if (firstChild)
  517. {
  518. item->SetVisible(!item->IsVisible());
  519. firstChild = false;
  520. }
  521. else
  522. item->SetVisible(prevItem->IsVisible());
  523. }
  524. else
  525. break;
  526. prevItem = item;
  527. }
  528. contentElement_->EnableLayoutUpdate();
  529. contentElement_->UpdateLayout();
  530. }
  531. unsigned ListView::GetNumItems() const
  532. {
  533. return contentElement_->GetNumChildren();
  534. }
  535. UIElement* ListView::GetItem(unsigned index) const
  536. {
  537. return contentElement_->GetChild(index);
  538. }
  539. PODVector<UIElement*> ListView::GetItems() const
  540. {
  541. return contentElement_->GetChildren();
  542. }
  543. unsigned ListView::GetSelection() const
  544. {
  545. if (selections_.Empty())
  546. return M_MAX_UNSIGNED;
  547. else
  548. return *selections_.Begin();
  549. }
  550. UIElement* ListView::GetSelectedItem() const
  551. {
  552. return contentElement_->GetChild(GetSelection());
  553. }
  554. PODVector<UIElement*> ListView::GetSelectedItems() const
  555. {
  556. PODVector<UIElement*> ret;
  557. for (Set<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
  558. {
  559. UIElement* item = GetItem(*i);
  560. if (item)
  561. ret.Push(item);
  562. }
  563. return ret;
  564. }
  565. void ListView::UpdateSelectionEffect()
  566. {
  567. unsigned numItems = GetNumItems();
  568. for (unsigned i = 0; i < numItems; ++i)
  569. {
  570. UIElement* item = GetItem(i);
  571. if (highlightMode_ != HM_NEVER && selections_.Find(i) != selections_.End())
  572. item->SetSelected(focus_ || highlightMode_ == HM_ALWAYS);
  573. else
  574. item->SetSelected(false);
  575. }
  576. }
  577. void ListView::EnsureItemVisibility(unsigned index)
  578. {
  579. UIElement* item = GetItem(index);
  580. if (!item || !item->IsVisible())
  581. return;
  582. IntVector2 currentOffset = item->GetScreenPosition() - scrollPanel_->GetScreenPosition() - contentElement_->GetPosition();
  583. IntVector2 newView = GetViewPosition();
  584. const IntRect& clipBorder = scrollPanel_->GetClipBorder();
  585. IntVector2 windowSize(scrollPanel_->GetWidth() - clipBorder.left_ - clipBorder.right_, scrollPanel_->GetHeight() -
  586. clipBorder.top_ - clipBorder.bottom_);
  587. if (currentOffset.y_ < 0)
  588. newView.y_ += currentOffset.y_;
  589. if (currentOffset.y_ + item->GetHeight() > windowSize.y_)
  590. newView.y_ += currentOffset.y_ + item->GetHeight() - windowSize.y_;
  591. SetViewPosition(newView);
  592. }
  593. void ListView::HandleUIMouseClick(StringHash eventType, VariantMap& eventData)
  594. {
  595. if (eventData[UIMouseClick::P_BUTTON].GetInt() != MOUSEB_LEFT)
  596. return;
  597. int qualifiers = eventData[UIMouseClick::P_QUALIFIERS].GetInt();
  598. UIElement* element = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
  599. unsigned numItems = GetNumItems();
  600. for (unsigned i = 0; i < numItems; ++i)
  601. {
  602. if (element == GetItem(i))
  603. {
  604. // Check doubleclick
  605. bool isDoubleClick = false;
  606. if (!multiselect_ || !qualifiers)
  607. {
  608. if (doubleClickTimer_ > 0.0f && lastClickedItem_ == i)
  609. {
  610. isDoubleClick = true;
  611. doubleClickTimer_ = 0.0f;
  612. }
  613. else
  614. {
  615. doubleClickTimer_ = doubleClickInterval_;
  616. lastClickedItem_ = i;
  617. }
  618. SetSelection(i);
  619. }
  620. // Check multiselect with shift & ctrl
  621. if (multiselect_)
  622. {
  623. if (qualifiers & QUAL_SHIFT)
  624. {
  625. if (selections_.Empty())
  626. SetSelection(i);
  627. else
  628. {
  629. unsigned first = selections_.Front();
  630. unsigned last = selections_.Back();
  631. Set<unsigned> newSelections = selections_;
  632. if (i == first || i == last)
  633. {
  634. for (unsigned j = first; j <= last; ++j)
  635. newSelections.Insert(j);
  636. }
  637. else if (i < first)
  638. {
  639. for (unsigned j = i; j <= first; ++j)
  640. newSelections.Insert(j);
  641. }
  642. else if (i < last)
  643. {
  644. if ((abs((int)i - (int)first)) <= (abs((int)i - (int)last)))
  645. {
  646. for (unsigned j = first; j <= i; ++j)
  647. newSelections.Insert(j);
  648. }
  649. else
  650. {
  651. for (unsigned j = i; j <= last; ++j)
  652. newSelections.Insert(j);
  653. }
  654. }
  655. else if (i > last)
  656. {
  657. for (unsigned j = last; j <= i; ++j)
  658. newSelections.Insert(j);
  659. }
  660. SetSelections(newSelections);
  661. }
  662. }
  663. else if (qualifiers & QUAL_CTRL)
  664. ToggleSelection(i);
  665. }
  666. if (isDoubleClick)
  667. {
  668. VariantMap eventData;
  669. eventData[ItemDoubleClicked::P_ELEMENT] = (void*)this;
  670. eventData[ItemDoubleClicked::P_SELECTION] = i;
  671. SendEvent(E_ITEMDOUBLECLICKED, eventData);
  672. }
  673. return;
  674. }
  675. }
  676. }