From 523b6a0a51462e23a5f954f9856746d153ae26c0 Mon Sep 17 00:00:00 2001 From: David Capello Date: Tue, 23 May 2017 13:41:11 -0300 Subject: [PATCH] Show a DitheringSelector item in the combobox when it's closed In this way we can show listbox/custom items in the combobox when we select an item instead of showing text-only (the ui::Entry). --- src/app/ui/dithering_selector.cpp | 2 ++ src/ui/combobox.cpp | 59 +++++++++++++++++++++++++++---- src/ui/combobox.h | 9 ++++- 3 files changed, 63 insertions(+), 7 deletions(-) diff --git a/src/app/ui/dithering_selector.cpp b/src/app/ui/dithering_selector.cpp index b957bfe10..4389af8ae 100644 --- a/src/app/ui/dithering_selector.cpp +++ b/src/app/ui/dithering_selector.cpp @@ -114,6 +114,8 @@ private: DitheringSelector::DitheringSelector() { + setUseCustomWidget(true); + addItem(new DitherItem(render::DitheringAlgorithm::None, render::DitheringMatrix(), "No Dithering")); addItem(new DitherItem(render::DitheringAlgorithm::Ordered, diff --git a/src/ui/combobox.cpp b/src/ui/combobox.cpp index a9c27d868..27b9faca4 100644 --- a/src/ui/combobox.cpp +++ b/src/ui/combobox.cpp @@ -56,8 +56,11 @@ class ComboBoxListBox : public ListBox { public: ComboBoxListBox(ComboBox* comboBox) : m_comboBox(comboBox) { - for (auto it=comboBox->begin(), end=comboBox->end(); it!=end; ++it) - addChild(*it); + for (auto item : *comboBox) { + if (item->parent()) + item->parent()->removeChild(item); + addChild(item); + } } void clean() { @@ -88,6 +91,8 @@ ComboBox::ComboBox() , m_editable(false) , m_clickopen(true) , m_casesensitive(true) + , m_filtering(false) + , m_useCustomWidget(false) { // TODO this separation should be from the Theme* this->setChildSpacing(0); @@ -108,6 +113,7 @@ ComboBox::ComboBox() ComboBox::~ComboBox() { + removeMessageFilters(); removeAllItems(); } @@ -135,6 +141,11 @@ void ComboBox::setCaseSensitive(bool state) m_casesensitive = state; } +void ComboBox::setUseCustomWidget(bool state) +{ + m_useCustomWidget = state; +} + int ComboBox::addItem(ListItem* item) { bool sel_first = m_items.empty(); @@ -398,6 +409,8 @@ void ComboBox::onResize(ResizeEvent& ev) // Entry m_entry->setBounds(Rect(bounds.x, bounds.y, bounds.w - buttonSize.w, bounds.h)); + + putSelectedItemAsCustomWidget(); } void ComboBox::onSizeHint(SizeHintEvent& ev) @@ -609,8 +622,7 @@ void ComboBox::openListBox() m_window->positionWindow(rc.x, rc.y); m_window->openWindow(); - manager()->addMessageFilter(kMouseDownMessage, this); - manager()->addMessageFilter(kKeyDownMessage, this); + filterMessages(); if (isEditable()) m_entry->requestFocus(); @@ -630,8 +642,8 @@ void ComboBox::closeListBox() m_window = nullptr; m_listbox = nullptr; - manager()->removeMessageFilter(kMouseDownMessage, this); - manager()->removeMessageFilter(kKeyDownMessage, this); + removeMessageFilters(); + putSelectedItemAsCustomWidget(); m_entry->requestFocus(); onCloseListBox(); @@ -675,4 +687,39 @@ void ComboBox::onCloseListBox() CloseListBox(); } +void ComboBox::filterMessages() +{ + if (!m_filtering) { + manager()->addMessageFilter(kMouseDownMessage, this); + manager()->addMessageFilter(kKeyDownMessage, this); + m_filtering = true; + } +} + +void ComboBox::removeMessageFilters() +{ + if (m_filtering) { + manager()->removeMessageFilter(kMouseDownMessage, this); + manager()->removeMessageFilter(kKeyDownMessage, this); + m_filtering = false; + } +} + +void ComboBox::putSelectedItemAsCustomWidget() +{ + if (!useCustomWidget()) + return; + + ListItem* item = getSelectedItem(); + if (item && item->parent() == nullptr) { + if (!m_listbox) { + item->setBounds(m_entry->childrenBounds()); + m_entry->addChild(item); + } + else { + m_entry->removeChild(item); + } + } +} + } // namespace ui diff --git a/src/ui/combobox.h b/src/ui/combobox.h index 81e6b19f0..b01f8f089 100644 --- a/src/ui/combobox.h +++ b/src/ui/combobox.h @@ -1,5 +1,5 @@ // Aseprite UI Library -// Copyright (C) 2001-2016 David Capello +// Copyright (C) 2001-2017 David Capello // // This file is released under the terms of the MIT license. // Read LICENSE.txt for more information. @@ -42,10 +42,12 @@ namespace ui { void setEditable(bool state); void setClickOpen(bool state); void setCaseSensitive(bool state); + void setUseCustomWidget(bool state); bool isEditable() const { return m_editable; } bool isClickOpen() const { return m_clickopen; } bool isCaseSensitive() const { return m_casesensitive; } + bool useCustomWidget() const { return m_useCustomWidget; } int addItem(ListItem* item); int addItem(const std::string& text); @@ -100,6 +102,9 @@ namespace ui { private: void onButtonClick(Event& ev); + void filterMessages(); + void removeMessageFilters(); + void putSelectedItemAsCustomWidget(); ComboBoxEntry* m_entry; Button* m_button; @@ -110,6 +115,8 @@ namespace ui { bool m_editable; bool m_clickopen; bool m_casesensitive; + bool m_filtering; + bool m_useCustomWidget; }; } // namespace ui