| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140 |
- //
- // Copyright (c) 2008-2015 the Urho3D project.
- //
- // Permission is hereby granted, free of charge, to any person obtaining a copy
- // of this software and associated documentation files (the "Software"), to deal
- // in the Software without restriction, including without limitation the rights
- // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- // copies of the Software, and to permit persons to whom the Software is
- // furnished to do so, subject to the following conditions:
- //
- // The above copyright notice and this permission notice shall be included in
- // all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- // THE SOFTWARE.
- //
- #include "../Precompiled.h"
- #include "../Core/Context.h"
- #include "../Input/InputEvents.h"
- #include "../IO/Log.h"
- #include "../UI/CheckBox.h"
- #include "../UI/ListView.h"
- #include "../UI/Text.h"
- #include "../UI/UI.h"
- #include "../UI/UIEvents.h"
- #include "../DebugNew.h"
- namespace Urho3D
- {
- static const char* highlightModes[] =
- {
- "Never",
- "Focus",
- "Always",
- 0
- };
- static const StringHash expandedHash("Expanded");
- extern const char* UI_CATEGORY;
- bool GetItemExpanded(UIElement* item)
- {
- return item ? item->GetVar(expandedHash).GetBool() : false;
- }
- void SetItemExpanded(UIElement* item, bool enable)
- {
- item->SetVar(expandedHash, enable);
- }
- static const StringHash hierarchyParentHash("HierarchyParent");
- bool GetItemHierarchyParent(UIElement* item)
- {
- return item ? item->GetVar(hierarchyParentHash).GetBool() : false;
- }
- void SetItemHierarchyParent(UIElement* item, bool enable)
- {
- item->SetVar(hierarchyParentHash, enable);
- }
- /// Hierarchy container (used by ListView internally when in hierarchy mode).
- class HierarchyContainer : public UIElement
- {
- URHO3D_OBJECT(HierarchyContainer, UIElement);
- public:
- /// Construct.
- HierarchyContainer(Context* context, ListView* listView, UIElement* overlayContainer) :
- UIElement(context),
- listView_(listView),
- overlayContainer_(overlayContainer)
- {
- SubscribeToEvent(this, E_LAYOUTUPDATED, URHO3D_HANDLER(HierarchyContainer, HandleLayoutUpdated));
- SubscribeToEvent(overlayContainer->GetParent(), E_VIEWCHANGED, URHO3D_HANDLER(HierarchyContainer, HandleViewChanged));
- SubscribeToEvent(E_UIMOUSECLICK, URHO3D_HANDLER(HierarchyContainer, HandleUIMouseClick));
- }
- /// Register object factory.
- static void RegisterObject(Context* context);
- /// Handle layout updated by adjusting the position of the overlays.
- void HandleLayoutUpdated(StringHash eventType, VariantMap& eventData)
- {
- // Adjust the container size for child clipping effect
- overlayContainer_->SetSize(GetParent()->GetSize());
- for (unsigned i = 0; i < children_.Size(); ++i)
- {
- const IntVector2& position = children_[i]->GetPosition();
- CheckBox* overlay = static_cast<CheckBox*>(overlayContainer_->GetChild(i));
- bool visible = children_[i]->IsVisible() && GetItemHierarchyParent(children_[i]);
- overlay->SetVisible(visible);
- if (visible)
- {
- overlay->SetPosition(position.x_, position.y_);
- overlay->SetChecked(GetItemExpanded(children_[i]));
- }
- }
- }
- /// Handle view changed by scrolling the overlays in tandem.
- void HandleViewChanged(StringHash eventType, VariantMap& eventData)
- {
- using namespace ViewChanged;
- int x = eventData[P_X].GetInt();
- int y = eventData[P_Y].GetInt();
- IntRect panelBorder = GetParent()->GetClipBorder();
- overlayContainer_->SetChildOffset(IntVector2(-x + panelBorder.left_, -y + panelBorder.top_));
- }
- /// Handle mouse click on overlays by toggling the expansion state of the corresponding item
- void HandleUIMouseClick(StringHash eventType, VariantMap& eventData)
- {
- using namespace UIMouseClick;
- UIElement* overlay = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
- if (overlay)
- {
- const Vector<SharedPtr<UIElement> >& children = overlayContainer_->GetChildren();
- Vector<SharedPtr<UIElement> >::ConstIterator i = children.Find(SharedPtr<UIElement>(overlay));
- if (i != children.End())
- listView_->ToggleExpand((unsigned)(i - children.Begin()));
- }
- }
- /// Insert a child element into a specific position in the child list.
- void InsertChild(unsigned index, UIElement* element)
- {
- // Insert the overlay at the same index position to the overlay container
- CheckBox* overlay = static_cast<CheckBox*>(overlayContainer_->CreateChild(CheckBox::GetTypeStatic(), String::EMPTY, index));
- overlay->SetStyle("HierarchyListViewOverlay");
- int baseIndent = listView_->GetBaseIndent();
- int indent = element->GetIndent() - baseIndent - 1;
- overlay->SetIndent(indent);
- overlay->SetFixedWidth((indent + 1) * element->GetIndentSpacing());
- // Then insert the element as child as per normal
- UIElement::InsertChild(index, element);
- }
- private:
- // Parent list view.
- ListView* listView_;
- // Container for overlay checkboxes.
- UIElement* overlayContainer_;
- };
- void HierarchyContainer::RegisterObject(Context* context)
- {
- COPY_BASE_ATTRIBUTES(UIElement);
- }
- ListView::ListView(Context* context) :
- ScrollView(context),
- highlightMode_(HM_FOCUS),
- multiselect_(false),
- hierarchyMode_(true), // Init to true here so that the setter below takes effect
- baseIndent_(0),
- clearSelectionOnDefocus_(false),
- selectOnClickEnd_(false)
- {
- resizeContentWidth_ = true;
- // By default list view is set to non-hierarchy mode
- SetHierarchyMode(false);
- SubscribeToEvent(E_UIMOUSEDOUBLECLICK, URHO3D_HANDLER(ListView, HandleUIMouseDoubleClick));
- SubscribeToEvent(E_FOCUSCHANGED, URHO3D_HANDLER(ListView, HandleItemFocusChanged));
- SubscribeToEvent(this, E_DEFOCUSED, URHO3D_HANDLER(ListView, HandleFocusChanged));
- SubscribeToEvent(this, E_FOCUSED, URHO3D_HANDLER(ListView, HandleFocusChanged));
- UpdateUIClickSubscription();
- }
- ListView::~ListView()
- {
- }
- void ListView::RegisterObject(Context* context)
- {
- context->RegisterFactory<ListView>(UI_CATEGORY);
- HierarchyContainer::RegisterObject(context);
- COPY_BASE_ATTRIBUTES(ScrollView);
- ENUM_ACCESSOR_ATTRIBUTE("Highlight Mode", GetHighlightMode, SetHighlightMode, HighlightMode, highlightModes, HM_FOCUS, AM_FILE);
- ACCESSOR_ATTRIBUTE("Multiselect", GetMultiselect, SetMultiselect, bool, false, AM_FILE);
- ACCESSOR_ATTRIBUTE("Hierarchy Mode", GetHierarchyMode, SetHierarchyMode, bool, false, AM_FILE);
- ACCESSOR_ATTRIBUTE("Base Indent", GetBaseIndent, SetBaseIndent, int, 0, AM_FILE);
- ACCESSOR_ATTRIBUTE("Clear Sel. On Defocus", GetClearSelectionOnDefocus, SetClearSelectionOnDefocus, bool, false, AM_FILE);
- ACCESSOR_ATTRIBUTE("Select On Click End", GetSelectOnClickEnd, SetSelectOnClickEnd, bool, false, AM_FILE);
- }
- void ListView::OnKey(int key, int buttons, int qualifiers)
- {
- // If no selection, can not move with keys
- unsigned numItems = GetNumItems();
- unsigned selection = GetSelection();
- // If either shift or ctrl held down, add to selection if multiselect enabled
- bool additive = multiselect_ && qualifiers & (QUAL_SHIFT | QUAL_CTRL);
- int delta = M_MAX_INT;
- int pageDirection = 1;
- if (numItems)
- {
- if (selection != M_MAX_UNSIGNED && qualifiers & QUAL_CTRL && key == KEY_C)
- {
- CopySelectedItemsToClipboard();
- return;
- }
- switch (key)
- {
- case KEY_LEFT:
- case KEY_RIGHT:
- if (selection != M_MAX_UNSIGNED && hierarchyMode_)
- {
- Expand(selection, key == KEY_RIGHT);
- return;
- }
- break;
- case KEY_RETURN:
- case KEY_RETURN2:
- case KEY_KP_ENTER:
- if (selection != M_MAX_UNSIGNED && hierarchyMode_)
- {
- ToggleExpand(selection);
- return;
- }
- break;
- case KEY_UP:
- delta = -1;
- break;
- case KEY_DOWN:
- delta = 1;
- break;
- case KEY_PAGEUP:
- pageDirection = -1;
- // Fallthru
- case KEY_PAGEDOWN:
- {
- // Convert page step to pixels and see how many items have to be skipped to reach that many pixels
- if (selection == M_MAX_UNSIGNED)
- selection = 0; // Assume as if first item is selected
- int stepPixels = ((int)(pageStep_ * scrollPanel_->GetHeight())) - contentElement_->GetChild(selection)->GetHeight();
- unsigned newSelection = selection;
- unsigned okSelection = selection;
- unsigned invisible = 0;
- while (newSelection < numItems)
- {
- UIElement* item = GetItem(newSelection);
- int height = 0;
- if (item->IsVisible())
- {
- height = item->GetHeight();
- okSelection = newSelection;
- }
- else
- ++invisible;
- if (stepPixels < height)
- break;
- stepPixels -= height;
- newSelection += pageDirection;
- }
- delta = okSelection - selection - pageDirection * invisible;
- }
- break;
- case KEY_HOME:
- delta = -(int)GetNumItems();
- break;
- case KEY_END:
- delta = GetNumItems();
- break;
- default: break;
- }
- }
- if (delta != M_MAX_INT)
- {
- ChangeSelection(delta, additive);
- return;
- }
- using namespace UnhandledKey;
- VariantMap& eventData = GetEventDataMap();
- eventData[P_ELEMENT] = this;
- eventData[P_KEY] = key;
- eventData[P_BUTTONS] = buttons;
- eventData[P_QUALIFIERS] = qualifiers;
- SendEvent(E_UNHANDLEDKEY, eventData);
- }
- void ListView::OnResize()
- {
- ScrollView::OnResize();
- // When in hierarchy mode also need to resize the overlay container
- if (hierarchyMode_)
- overlayContainer_->SetSize(scrollPanel_->GetSize());
- }
- void ListView::AddItem(UIElement* item)
- {
- InsertItem(M_MAX_UNSIGNED, item);
- }
- void ListView::InsertItem(unsigned index, UIElement* item, UIElement* parentItem)
- {
- if (!item || item->GetParent() == contentElement_)
- return;
- // Enable input so that clicking the item can be detected
- item->SetEnabled(true);
- item->SetSelected(false);
- unsigned numItems = contentElement_->GetNumChildren();
- if (hierarchyMode_)
- {
- int baseIndent = baseIndent_;
- if (parentItem)
- {
- baseIndent = parentItem->GetIndent();
- SetItemHierarchyParent(parentItem, true);
- // Adjust the index to ensure it is within the children index limit of the parent item
- unsigned indexLimit = FindItem(parentItem);
- if (index <= indexLimit)
- index = indexLimit + 1;
- else
- {
- while (++indexLimit < numItems)
- {
- if (contentElement_->GetChild(indexLimit)->GetIndent() <= baseIndent)
- break;
- }
- if (index > indexLimit)
- index = indexLimit;
- }
- }
- item->SetIndent(baseIndent + 1);
- SetItemExpanded(item, item->IsVisible());
- // Use the 'overrided' version to insert the child item
- static_cast<HierarchyContainer*>(contentElement_.Get())->InsertChild(index, item);
- }
- else
- {
- if (index > numItems)
- index = numItems;
- contentElement_->InsertChild(index, item);
- }
- // If necessary, shift the following selections
- if (!selections_.Empty())
- {
- for (unsigned i = 0; i < selections_.Size(); ++i)
- {
- if (selections_[i] >= index)
- ++selections_[i];
- }
- UpdateSelectionEffect();
- }
- }
- void ListView::RemoveItem(UIElement* item, unsigned index)
- {
- if (!item)
- return;
- unsigned numItems = GetNumItems();
- for (unsigned i = index; i < numItems; ++i)
- {
- if (GetItem(i) == item)
- {
- item->SetSelected(false);
- selections_.Remove(i);
- unsigned removed = 1;
- if (hierarchyMode_)
- {
- // Remove any child items in hierarchy mode
- if (GetItemHierarchyParent(item))
- {
- int baseIndent = item->GetIndent();
- for (unsigned j = i + 1; ; ++j)
- {
- UIElement* childItem = GetItem(i + 1);
- if (!childItem)
- break;
- if (childItem->GetIndent() > baseIndent)
- {
- childItem->SetSelected(false);
- selections_.Erase(j);
- contentElement_->RemoveChildAtIndex(i + 1);
- overlayContainer_->RemoveChildAtIndex(i + 1);
- ++removed;
- }
- else
- break;
- }
- }
- // Check if the parent of removed item still has other children
- if (i > 0)
- {
- int baseIndent = item->GetIndent();
- UIElement* prevKin = GetItem(i - 1); // Could be parent or sibling
- if (prevKin->GetIndent() < baseIndent)
- {
- UIElement* nextKin = GetItem(i + 1); // Could be sibling or parent-sibling or 0 if index out of bound
- if (!nextKin || nextKin->GetIndent() < baseIndent)
- {
- // If we reach here then the parent has no other children
- SetItemHierarchyParent(prevKin, false);
- }
- }
- }
- // Remove the overlay at the same index
- overlayContainer_->RemoveChildAtIndex(i);
- }
- // If necessary, shift the following selections
- if (!selections_.Empty())
- {
- for (unsigned j = 0; j < selections_.Size(); ++j)
- {
- if (selections_[j] > i)
- selections_[j] -= removed;
- }
- UpdateSelectionEffect();
- }
- contentElement_->RemoveChildAtIndex(i);
- break;
- }
- }
- }
- void ListView::RemoveItem(unsigned index)
- {
- RemoveItem(GetItem(index), index);
- }
- void ListView::RemoveAllItems()
- {
- contentElement_->DisableLayoutUpdate();
- ClearSelection();
- contentElement_->RemoveAllChildren();
- if (hierarchyMode_)
- overlayContainer_->RemoveAllChildren();
- contentElement_->EnableLayoutUpdate();
- contentElement_->UpdateLayout();
- }
- void ListView::SetSelection(unsigned index)
- {
- PODVector<unsigned> indices;
- indices.Push(index);
- SetSelections(indices);
- EnsureItemVisibility(index);
- }
- void ListView::SetSelections(const PODVector<unsigned>& indices)
- {
- // Make a weak pointer to self to check for destruction as a response to events
- WeakPtr<ListView> self(this);
- unsigned numItems = GetNumItems();
- // Remove first items that should no longer be selected
- for (PODVector<unsigned>::Iterator i = selections_.Begin(); i != selections_.End();)
- {
- unsigned index = *i;
- if (!indices.Contains(index))
- {
- i = selections_.Erase(i);
- using namespace ItemSelected;
- VariantMap& eventData = GetEventDataMap();
- eventData[P_ELEMENT] = this;
- eventData[P_SELECTION] = index;
- SendEvent(E_ITEMDESELECTED, eventData);
- if (self.Expired())
- return;
- }
- else
- ++i;
- }
- bool added = false;
- // Then add missing items
- for (PODVector<unsigned>::ConstIterator i = indices.Begin(); i != indices.End(); ++i)
- {
- unsigned index = *i;
- if (index < numItems)
- {
- // In singleselect mode, resend the event even for the same selection
- bool duplicate = selections_.Contains(index);
- if (!duplicate || !multiselect_)
- {
- if (!duplicate)
- {
- selections_.Push(index);
- added = true;
- }
- using namespace ItemSelected;
- VariantMap& eventData = GetEventDataMap();
- eventData[P_ELEMENT] = this;
- eventData[P_SELECTION] = *i;
- SendEvent(E_ITEMSELECTED, eventData);
- if (self.Expired())
- return;
- }
- }
- // If no multiselect enabled, allow setting only one item
- if (!multiselect_)
- break;
- }
- // Re-sort selections if necessary
- if (added)
- Sort(selections_.Begin(), selections_.End());
- UpdateSelectionEffect();
- SendEvent(E_SELECTIONCHANGED);
- }
- void ListView::AddSelection(unsigned index)
- {
- // Make a weak pointer to self to check for destruction as a response to events
- WeakPtr<ListView> self(this);
- if (!multiselect_)
- SetSelection(index);
- else
- {
- if (index >= GetNumItems())
- return;
- if (!selections_.Contains(index))
- {
- selections_.Push(index);
- using namespace ItemSelected;
- VariantMap& eventData = GetEventDataMap();
- eventData[P_ELEMENT] = this;
- eventData[P_SELECTION] = index;
- SendEvent(E_ITEMSELECTED, eventData);
- if (self.Expired())
- return;
- Sort(selections_.Begin(), selections_.End());
- }
- EnsureItemVisibility(index);
- UpdateSelectionEffect();
- SendEvent(E_SELECTIONCHANGED);
- }
- }
- void ListView::RemoveSelection(unsigned index)
- {
- if (index >= GetNumItems())
- return;
- if (selections_.Remove(index))
- {
- using namespace ItemSelected;
- VariantMap& eventData = GetEventDataMap();
- eventData[P_ELEMENT] = this;
- eventData[P_SELECTION] = index;
- SendEvent(E_ITEMDESELECTED, eventData);
- }
- EnsureItemVisibility(index);
- UpdateSelectionEffect();
- SendEvent(E_SELECTIONCHANGED);
- }
- void ListView::ToggleSelection(unsigned index)
- {
- unsigned numItems = GetNumItems();
- if (index >= numItems)
- return;
- if (selections_.Contains(index))
- RemoveSelection(index);
- else
- AddSelection(index);
- }
- void ListView::ChangeSelection(int delta, bool additive)
- {
- unsigned numItems = GetNumItems();
- if (selections_.Empty())
- {
- // Select first item if there is no selection yet
- if (numItems > 0)
- SetSelection(0);
- if (abs(delta) == 1)
- return;
- }
- if (!multiselect_)
- additive = false;
- // If going downwards, use the last selection as a base. Otherwise use first
- unsigned selection = delta > 0 ? selections_.Back() : selections_.Front();
- int direction = delta > 0 ? 1 : -1;
- unsigned newSelection = selection;
- unsigned okSelection = selection;
- PODVector<unsigned> indices = selections_;
- while (delta != 0)
- {
- newSelection += direction;
- if (newSelection >= numItems)
- break;
- UIElement* item = GetItem(newSelection);
- if (item->IsVisible())
- {
- indices.Push(okSelection = newSelection);
- delta -= direction;
- }
- }
- if (!additive)
- SetSelection(okSelection);
- else
- SetSelections(indices);
- }
- void ListView::ClearSelection()
- {
- SetSelections(PODVector<unsigned>());
- }
- void ListView::SetHighlightMode(HighlightMode mode)
- {
- highlightMode_ = mode;
- UpdateSelectionEffect();
- }
- void ListView::SetMultiselect(bool enable)
- {
- multiselect_ = enable;
- }
- void ListView::SetHierarchyMode(bool enable)
- {
- if (enable == hierarchyMode_)
- return;
- hierarchyMode_ = enable;
- UIElement* container;
- if (enable)
- {
- overlayContainer_ = new UIElement(context_);
- overlayContainer_->SetName("LV_OverlayContainer");
- overlayContainer_->SetInternal(true);
- AddChild(overlayContainer_);
- overlayContainer_->SetSortChildren(false);
- overlayContainer_->SetClipChildren(true);
- container = new HierarchyContainer(context_, this, overlayContainer_);
- }
- else
- {
- if (overlayContainer_)
- {
- RemoveChild(overlayContainer_);
- overlayContainer_.Reset();
- }
- container = new UIElement(context_);
- }
- container->SetName("LV_ItemContainer");
- container->SetInternal(true);
- SetContentElement(container);
- container->SetEnabled(true);
- container->SetSortChildren(false);
- }
- void ListView::SetBaseIndent(int baseIndent)
- {
- baseIndent_ = baseIndent;
- UpdateLayout();
- }
- void ListView::SetClearSelectionOnDefocus(bool enable)
- {
- if (enable != clearSelectionOnDefocus_)
- {
- clearSelectionOnDefocus_ = enable;
- if (clearSelectionOnDefocus_ && !HasFocus())
- ClearSelection();
- }
- }
- void ListView::SetSelectOnClickEnd(bool enable)
- {
- if (enable != selectOnClickEnd_)
- {
- selectOnClickEnd_ = enable;
- UpdateUIClickSubscription();
- }
- }
- void ListView::Expand(unsigned index, bool enable, bool recursive)
- {
- if (!hierarchyMode_)
- return;
- unsigned numItems = GetNumItems();
- if (index >= numItems)
- return;
- UIElement* item = GetItem(index++);
- SetItemExpanded(item, enable);
- int baseIndent = item->GetIndent();
- PODVector<bool> expanded((unsigned)(baseIndent + 1));
- expanded[baseIndent] = enable;
- contentElement_->DisableLayoutUpdate();
- while (index < numItems)
- {
- item = GetItem(index++);
- int indent = item->GetIndent();
- if (indent <= baseIndent)
- break;
- // Propagate the state to children when it is recursive
- if (recursive)
- SetItemExpanded(item, enable);
- // Use the parent expanded flag to influence the visibility of its children
- bool visible = enable && expanded[indent - 1];
- item->SetVisible(visible);
- if (indent >= (int)expanded.Size())
- expanded.Resize((unsigned)(indent + 1));
- expanded[indent] = visible && GetItemExpanded(item);
- }
- contentElement_->EnableLayoutUpdate();
- contentElement_->UpdateLayout();
- }
- void ListView::ToggleExpand(unsigned index, bool recursive)
- {
- if (!hierarchyMode_)
- return;
- unsigned numItems = GetNumItems();
- if (index >= numItems)
- return;
- UIElement* item = GetItem(index);
- Expand(index, !GetItemExpanded(item), recursive);
- }
- unsigned ListView::GetNumItems() const
- {
- return contentElement_->GetNumChildren();
- }
- UIElement* ListView::GetItem(unsigned index) const
- {
- return contentElement_->GetChild(index);
- }
- PODVector<UIElement*> ListView::GetItems() const
- {
- PODVector<UIElement*> items;
- contentElement_->GetChildren(items);
- return items;
- }
- unsigned ListView::FindItem(UIElement* item) const
- {
- if (!item)
- return M_MAX_UNSIGNED;
- // Early-out by checking if the item belongs to the listview hierarchy at all
- if (item->GetParent() != contentElement_)
- return M_MAX_UNSIGNED;
- const Vector<SharedPtr<UIElement> >& children = contentElement_->GetChildren();
- // Binary search for list item based on screen coordinate Y
- if (contentElement_->GetLayoutMode() == LM_VERTICAL && item->GetHeight())
- {
- int itemY = item->GetScreenPosition().y_;
- int left = 0;
- int right = children.Size() - 1;
- while (right >= left)
- {
- int mid = (left + right) / 2;
- if (children[mid] == item)
- return (unsigned)mid;
- if (itemY < children[mid]->GetScreenPosition().y_)
- right = mid - 1;
- else
- left = mid + 1;
- }
- }
- // Fallback to linear search in case the coordinates/sizes were not yet initialized
- for (unsigned i = 0; i < children.Size(); ++i)
- {
- if (children[i] == item)
- return i;
- }
- return M_MAX_UNSIGNED;
- }
- unsigned ListView::GetSelection() const
- {
- if (selections_.Empty())
- return M_MAX_UNSIGNED;
- else
- return GetSelections().Front();
- }
- UIElement* ListView::GetSelectedItem() const
- {
- return contentElement_->GetChild(GetSelection());
- }
- PODVector<UIElement*> ListView::GetSelectedItems() const
- {
- PODVector<UIElement*> ret;
- for (PODVector<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
- {
- UIElement* item = GetItem(*i);
- if (item)
- ret.Push(item);
- }
- return ret;
- }
- void ListView::CopySelectedItemsToClipboard() const
- {
- String selectedText;
- for (PODVector<unsigned>::ConstIterator i = selections_.Begin(); i != selections_.End(); ++i)
- {
- // Only handle Text UI element
- Text* text = dynamic_cast<Text*>(GetItem(*i));
- if (text)
- selectedText.Append(text->GetText()).Append("\n");
- }
- GetSubsystem<UI>()->SetClipboardText(selectedText);
- }
- bool ListView::IsSelected(unsigned index) const
- {
- return selections_.Contains(index);
- }
- bool ListView::IsExpanded(unsigned index) const
- {
- return GetItemExpanded(contentElement_->GetChild(index));
- }
- bool ListView::FilterImplicitAttributes(XMLElement& dest) const
- {
- if (!ScrollView::FilterImplicitAttributes(dest))
- return false;
- XMLElement childElem = dest.GetChild("element"); // Horizontal scroll bar
- if (!childElem)
- return false;
- childElem = childElem.GetNext("element"); // Vertical scroll bar
- if (!childElem)
- return false;
- childElem = childElem.GetNext("element"); // Scroll panel
- if (!childElem)
- return false;
- XMLElement containerElem = childElem.GetChild("element"); // Item container
- if (!containerElem)
- return false;
- if (!RemoveChildXML(containerElem, "Name", "LV_ItemContainer"))
- return false;
- if (!RemoveChildXML(containerElem, "Is Enabled", "true"))
- return false;
- if (!RemoveChildXML(containerElem, "Layout Mode", "Vertical"))
- return false;
- if (!RemoveChildXML(containerElem, "Size"))
- return false;
- if (hierarchyMode_)
- {
- containerElem = childElem.GetNext("element"); // Overlay container
- if (!containerElem)
- return false;
- if (!RemoveChildXML(containerElem, "Name", "LV_OverlayContainer"))
- return false;
- if (!RemoveChildXML(containerElem, "Clip Children", "true"))
- return false;
- if (!RemoveChildXML(containerElem, "Size"))
- return false;
- }
- return true;
- }
- void ListView::UpdateSelectionEffect()
- {
- unsigned numItems = GetNumItems();
- bool highlighted = highlightMode_ == HM_ALWAYS || HasFocus();
- for (unsigned i = 0; i < numItems; ++i)
- {
- UIElement* item = GetItem(i);
- if (highlightMode_ != HM_NEVER && selections_.Contains(i))
- item->SetSelected(highlighted);
- else
- item->SetSelected(false);
- }
- }
- void ListView::EnsureItemVisibility(unsigned index)
- {
- EnsureItemVisibility(GetItem(index));
- }
- void ListView::EnsureItemVisibility(UIElement* item)
- {
- if (!item || !item->IsVisible())
- return;
- IntVector2 newView = GetViewPosition();
- IntVector2 currentOffset = item->GetPosition() - newView;
- const IntRect& clipBorder = scrollPanel_->GetClipBorder();
- IntVector2 windowSize(scrollPanel_->GetWidth() - clipBorder.left_ - clipBorder.right_,
- scrollPanel_->GetHeight() - clipBorder.top_ - clipBorder.bottom_);
- if (currentOffset.y_ < 0)
- newView.y_ += currentOffset.y_;
- if (currentOffset.y_ + item->GetHeight() > windowSize.y_)
- newView.y_ += currentOffset.y_ + item->GetHeight() - windowSize.y_;
- SetViewPosition(newView);
- }
- void ListView::HandleUIMouseClick(StringHash eventType, VariantMap& eventData)
- {
- // Disregard the click end if a drag is going on
- if (selectOnClickEnd_ && GetSubsystem<UI>()->IsDragging())
- return;
- int button = eventData[UIMouseClick::P_BUTTON].GetInt();
- int buttons = eventData[UIMouseClick::P_BUTTONS].GetInt();
- int qualifiers = eventData[UIMouseClick::P_QUALIFIERS].GetInt();
- UIElement* element = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
- // Check if the clicked element belongs to the list
- unsigned i = FindItem(element);
- if (i >= GetNumItems())
- return;
- // If not editable, repeat the previous selection. This will send an event and allow eg. a dropdownlist to close
- if (!editable_)
- {
- SetSelections(selections_);
- return;
- }
- if (button == MOUSEB_LEFT)
- {
- // Single selection
- if (!multiselect_ || !qualifiers)
- SetSelection(i);
- // Check multiselect with shift & ctrl
- if (multiselect_)
- {
- if (qualifiers & QUAL_SHIFT)
- {
- if (selections_.Empty())
- SetSelection(i);
- else
- {
- unsigned first = selections_.Front();
- unsigned last = selections_.Back();
- PODVector<unsigned> newSelections = selections_;
- if (i == first || i == last)
- {
- for (unsigned j = first; j <= last; ++j)
- newSelections.Push(j);
- }
- else if (i < first)
- {
- for (unsigned j = i; j <= first; ++j)
- newSelections.Push(j);
- }
- else if (i < last)
- {
- if ((abs((int)i - (int)first)) <= (abs((int)i - (int)last)))
- {
- for (unsigned j = first; j <= i; ++j)
- newSelections.Push(j);
- }
- else
- {
- for (unsigned j = i; j <= last; ++j)
- newSelections.Push(j);
- }
- }
- else if (i > last)
- {
- for (unsigned j = last; j <= i; ++j)
- newSelections.Push(j);
- }
- SetSelections(newSelections);
- }
- }
- else if (qualifiers & QUAL_CTRL)
- ToggleSelection(i);
- }
- }
- // Propagate the click as an event. Also include right-clicks
- VariantMap& clickEventData = GetEventDataMap();
- clickEventData[ItemClicked::P_ELEMENT] = this;
- clickEventData[ItemClicked::P_ITEM] = element;
- clickEventData[ItemClicked::P_SELECTION] = i;
- clickEventData[ItemClicked::P_BUTTON] = button;
- clickEventData[ItemClicked::P_BUTTONS] = buttons;
- clickEventData[ItemClicked::P_QUALIFIERS] = qualifiers;
- SendEvent(E_ITEMCLICKED, clickEventData);
- }
- void ListView::HandleUIMouseDoubleClick(StringHash eventType, VariantMap& eventData)
- {
- int button = eventData[UIMouseClick::P_BUTTON].GetInt();
- int buttons = eventData[UIMouseClick::P_BUTTONS].GetInt();
- int qualifiers = eventData[UIMouseClick::P_QUALIFIERS].GetInt();
- UIElement* element = static_cast<UIElement*>(eventData[UIMouseClick::P_ELEMENT].GetPtr());
- // Check if the clicked element belongs to the list
- unsigned i = FindItem(element);
- if (i >= GetNumItems())
- return;
- VariantMap& clickEventData = GetEventDataMap();
- clickEventData[ItemDoubleClicked::P_ELEMENT] = this;
- clickEventData[ItemDoubleClicked::P_ITEM] = element;
- clickEventData[ItemDoubleClicked::P_SELECTION] = i;
- clickEventData[ItemDoubleClicked::P_BUTTON] = button;
- clickEventData[ItemDoubleClicked::P_BUTTONS] = buttons;
- clickEventData[ItemDoubleClicked::P_QUALIFIERS] = qualifiers;
- SendEvent(E_ITEMDOUBLECLICKED, clickEventData);
- }
- void ListView::HandleItemFocusChanged(StringHash eventType, VariantMap& eventData)
- {
- using namespace FocusChanged;
- UIElement* element = static_cast<UIElement*>(eventData[P_ELEMENT].GetPtr());
- while (element)
- {
- // If the focused element or its parent is in the list, scroll the list to make the item visible
- UIElement* parent = element->GetParent();
- if (parent == contentElement_)
- {
- EnsureItemVisibility(element);
- return;
- }
- element = parent;
- }
- }
- void ListView::HandleFocusChanged(StringHash eventType, VariantMap& eventData)
- {
- scrollPanel_->SetSelected(eventType == E_FOCUSED);
- if (clearSelectionOnDefocus_ && eventType == E_DEFOCUSED)
- ClearSelection();
- else if (highlightMode_ == HM_FOCUS)
- UpdateSelectionEffect();
- }
- void ListView::UpdateUIClickSubscription()
- {
- UnsubscribeFromEvent(E_UIMOUSECLICK);
- UnsubscribeFromEvent(E_UIMOUSECLICKEND);
- SubscribeToEvent(selectOnClickEnd_ ? E_UIMOUSECLICKEND : E_UIMOUSECLICK, URHO3D_HANDLER(ListView, HandleUIMouseClick));
- }
- }
|