mirror of
https://github.com/aseprite/aseprite.git
synced 2025-02-19 15:40:31 +00:00
Separate ui::ListBox::Item into ui::ListItem class
This commit is contained in:
parent
b8c3d39c35
commit
dcd8627406
@ -293,7 +293,7 @@ Widget* WidgetLoader::convertXmlElementToWidget(const TiXmlElement* elem, Widget
|
|||||||
else if (ustrcmp(elem_name, "listitem") == 0) {
|
else if (ustrcmp(elem_name, "listitem") == 0) {
|
||||||
const char *text = elem->Attribute("text");
|
const char *text = elem->Attribute("text");
|
||||||
|
|
||||||
widget = new ListBox::Item(text ? TRANSLATE_ATTR(text): NULL);
|
widget = new ListItem(text ? TRANSLATE_ATTR(text): NULL);
|
||||||
}
|
}
|
||||||
/* splitter */
|
/* splitter */
|
||||||
else if (ustrcmp(elem_name, "splitter") == 0) {
|
else if (ustrcmp(elem_name, "splitter") == 0) {
|
||||||
|
@ -36,6 +36,7 @@
|
|||||||
#include "ui/button.h"
|
#include "ui/button.h"
|
||||||
#include "ui/label.h"
|
#include "ui/label.h"
|
||||||
#include "ui/listbox.h"
|
#include "ui/listbox.h"
|
||||||
|
#include "ui/listitem.h"
|
||||||
#include "ui/slider.h"
|
#include "ui/slider.h"
|
||||||
#include "ui/view.h"
|
#include "ui/view.h"
|
||||||
#include "ui/widget.h"
|
#include "ui/widget.h"
|
||||||
@ -99,7 +100,7 @@ private:
|
|||||||
for (ConvolutionMatrixStock::iterator it = m_stock.begin(), end = m_stock.end();
|
for (ConvolutionMatrixStock::iterator it = m_stock.begin(), end = m_stock.end();
|
||||||
it != end; ++it) {
|
it != end; ++it) {
|
||||||
SharedPtr<ConvolutionMatrix> matrix = *it;
|
SharedPtr<ConvolutionMatrix> matrix = *it;
|
||||||
ListBox::Item* listitem = new ListBox::Item(matrix->getName());
|
ListItem* listitem = new ListItem(matrix->getName());
|
||||||
m_stockListBox->addChild(listitem);
|
m_stockListBox->addChild(listitem);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -131,7 +132,7 @@ private:
|
|||||||
|
|
||||||
void onMatrixChange()
|
void onMatrixChange()
|
||||||
{
|
{
|
||||||
ListBox::Item* selected = m_stockListBox->getSelectedChild();
|
ListItem* selected = m_stockListBox->getSelectedChild();
|
||||||
SharedPtr<ConvolutionMatrix> matrix = m_stock.getByName(selected->getText());
|
SharedPtr<ConvolutionMatrix> matrix = m_stock.getByName(selected->getText());
|
||||||
Target newTarget = matrix->getDefaultTarget();
|
Target newTarget = matrix->getDefaultTarget();
|
||||||
|
|
||||||
|
@ -26,6 +26,7 @@ add_library(ui-lib
|
|||||||
label.cpp
|
label.cpp
|
||||||
link_label.cpp
|
link_label.cpp
|
||||||
listbox.cpp
|
listbox.cpp
|
||||||
|
listitem.cpp
|
||||||
manager.cpp
|
manager.cpp
|
||||||
menu.cpp
|
menu.cpp
|
||||||
message.cpp
|
message.cpp
|
||||||
|
@ -9,6 +9,7 @@
|
|||||||
#include "base/compiler_specific.h"
|
#include "base/compiler_specific.h"
|
||||||
#include "gfx/size.h"
|
#include "gfx/size.h"
|
||||||
#include "ui/gui.h"
|
#include "ui/gui.h"
|
||||||
|
#include "ui/listitem.h"
|
||||||
#include "ui/preferred_size_event.h"
|
#include "ui/preferred_size_event.h"
|
||||||
|
|
||||||
#include <allegro.h>
|
#include <allegro.h>
|
||||||
@ -446,7 +447,7 @@ void ComboBox::openListBox()
|
|||||||
std::vector<Item*>::iterator it, end = m_items.end();
|
std::vector<Item*>::iterator it, end = m_items.end();
|
||||||
for (it = m_items.begin(); it != end; ++it) {
|
for (it = m_items.begin(); it != end; ++it) {
|
||||||
Item* item = *it;
|
Item* item = *it;
|
||||||
m_listbox->addChild(new ListBox::Item(item->text.c_str()));
|
m_listbox->addChild(new ListItem(item->text.c_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
m_window->setOnTop(true);
|
m_window->setOnTop(true);
|
||||||
|
@ -32,6 +32,7 @@
|
|||||||
#include "ui/layout_io.h"
|
#include "ui/layout_io.h"
|
||||||
#include "ui/link_label.h"
|
#include "ui/link_label.h"
|
||||||
#include "ui/listbox.h"
|
#include "ui/listbox.h"
|
||||||
|
#include "ui/listitem.h"
|
||||||
#include "ui/load_layout_event.h"
|
#include "ui/load_layout_event.h"
|
||||||
#include "ui/manager.h"
|
#include "ui/manager.h"
|
||||||
#include "ui/menu.h"
|
#include "ui/menu.h"
|
||||||
|
@ -8,6 +8,7 @@
|
|||||||
|
|
||||||
#include "ui/listbox.h"
|
#include "ui/listbox.h"
|
||||||
|
|
||||||
|
#include "ui/listitem.h"
|
||||||
#include "ui/message.h"
|
#include "ui/message.h"
|
||||||
#include "ui/preferred_size_event.h"
|
#include "ui/preferred_size_event.h"
|
||||||
#include "ui/system.h"
|
#include "ui/system.h"
|
||||||
@ -27,21 +28,13 @@ ListBox::ListBox()
|
|||||||
initTheme();
|
initTheme();
|
||||||
}
|
}
|
||||||
|
|
||||||
ListBox::Item::Item(const char* text)
|
ListItem* ListBox::getSelectedChild()
|
||||||
: Widget(JI_LISTITEM)
|
|
||||||
{
|
|
||||||
setAlign(JI_LEFT | JI_MIDDLE);
|
|
||||||
setText(text);
|
|
||||||
initTheme();
|
|
||||||
}
|
|
||||||
|
|
||||||
ListBox::Item* ListBox::getSelectedChild()
|
|
||||||
{
|
{
|
||||||
UI_FOREACH_WIDGET(getChildren(), it) {
|
UI_FOREACH_WIDGET(getChildren(), it) {
|
||||||
ASSERT(dynamic_cast<Item*>(*it) != NULL);
|
ASSERT(dynamic_cast<ListItem*>(*it) != NULL);
|
||||||
|
|
||||||
if (static_cast<Item*>(*it)->isSelected())
|
if (static_cast<ListItem*>(*it)->isSelected())
|
||||||
return static_cast<Item*>(*it);
|
return static_cast<ListItem*>(*it);
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@ -51,7 +44,7 @@ int ListBox::getSelectedIndex()
|
|||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
UI_FOREACH_WIDGET(getChildren(), it) {
|
UI_FOREACH_WIDGET(getChildren(), it) {
|
||||||
if (static_cast<Item*>(*it)->isSelected())
|
if (static_cast<ListItem*>(*it)->isSelected())
|
||||||
return i;
|
return i;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@ -59,10 +52,10 @@ int ListBox::getSelectedIndex()
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void ListBox::selectChild(Item* item)
|
void ListBox::selectChild(ListItem* item)
|
||||||
{
|
{
|
||||||
UI_FOREACH_WIDGET(getChildren(), it) {
|
UI_FOREACH_WIDGET(getChildren(), it) {
|
||||||
Item* child = static_cast<Item*>(*it);
|
ListItem* child = static_cast<ListItem*>(*it);
|
||||||
|
|
||||||
if (child->isSelected()) {
|
if (child->isSelected()) {
|
||||||
if (item && child == item)
|
if (item && child == item)
|
||||||
@ -100,7 +93,7 @@ void ListBox::selectIndex(int index)
|
|||||||
if (index < 0 || index >= (int)children.size())
|
if (index < 0 || index >= (int)children.size())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
Item* child = static_cast<Item*>(children[index]);
|
ListItem* child = static_cast<ListItem*>(children[index]);
|
||||||
ASSERT(child);
|
ASSERT(child);
|
||||||
selectChild(child);
|
selectChild(child);
|
||||||
}
|
}
|
||||||
@ -114,7 +107,7 @@ size_t ListBox::getItemsCount() const
|
|||||||
void ListBox::centerScroll()
|
void ListBox::centerScroll()
|
||||||
{
|
{
|
||||||
View* view = View::getView(this);
|
View* view = View::getView(this);
|
||||||
Item* item = getSelectedChild();
|
ListItem* item = getSelectedChild();
|
||||||
|
|
||||||
if (view && item) {
|
if (view && item) {
|
||||||
gfx::Rect vp = view->getViewportBounds();
|
gfx::Rect vp = view->getViewportBounds();
|
||||||
@ -179,7 +172,7 @@ bool ListBox::onProcessMessage(Message* msg)
|
|||||||
|
|
||||||
/* if the picked widget is a child of the list, select it */
|
/* if the picked widget is a child of the list, select it */
|
||||||
if (picked && hasChild(picked)) {
|
if (picked && hasChild(picked)) {
|
||||||
if (Item* pickedItem = dynamic_cast<Item*>(picked))
|
if (ListItem* pickedItem = dynamic_cast<ListItem*>(picked))
|
||||||
selectChild(pickedItem);
|
selectChild(pickedItem);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -271,7 +264,7 @@ void ListBox::onPreferredSize(PreferredSizeEvent& ev)
|
|||||||
int w = 0, h = 0;
|
int w = 0, h = 0;
|
||||||
|
|
||||||
UI_FOREACH_WIDGET_WITH_END(getChildren(), it, end) {
|
UI_FOREACH_WIDGET_WITH_END(getChildren(), it, end) {
|
||||||
Size reqSize = static_cast<Item*>(*it)->getPreferredSize();
|
Size reqSize = static_cast<ListItem*>(*it)->getPreferredSize();
|
||||||
|
|
||||||
w = MAX(w, reqSize.w);
|
w = MAX(w, reqSize.w);
|
||||||
h += reqSize.h + (it+1 != end ? this->child_spacing: 0);
|
h += reqSize.h + (it+1 != end ? this->child_spacing: 0);
|
||||||
@ -315,54 +308,4 @@ void ListBox::layoutListBox(JRect rect)
|
|||||||
jrect_free(cpos);
|
jrect_free(cpos);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ListBox::Item::onProcessMessage(Message* msg)
|
|
||||||
{
|
|
||||||
switch (msg->type) {
|
|
||||||
|
|
||||||
case JM_SETPOS: {
|
|
||||||
JRect crect;
|
|
||||||
|
|
||||||
jrect_copy(this->rc, &msg->setpos.rect);
|
|
||||||
crect = jwidget_get_child_rect(this);
|
|
||||||
|
|
||||||
UI_FOREACH_WIDGET(getChildren(), it)
|
|
||||||
jwidget_set_rect(*it, crect);
|
|
||||||
|
|
||||||
jrect_free(crect);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
case JM_DRAW:
|
|
||||||
this->getTheme()->draw_listitem(this, &msg->draw.rect);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Widget::onProcessMessage(msg);
|
|
||||||
}
|
|
||||||
|
|
||||||
void ListBox::Item::onPreferredSize(PreferredSizeEvent& ev)
|
|
||||||
{
|
|
||||||
int w = 0, h = 0;
|
|
||||||
Size maxSize;
|
|
||||||
|
|
||||||
if (hasText()) {
|
|
||||||
maxSize.w = jwidget_get_text_length(this);
|
|
||||||
maxSize.h = jwidget_get_text_height(this);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
maxSize.w = maxSize.h = 0;
|
|
||||||
|
|
||||||
UI_FOREACH_WIDGET(getChildren(), it) {
|
|
||||||
Size reqSize = (*it)->getPreferredSize();
|
|
||||||
|
|
||||||
maxSize.w = MAX(maxSize.w, reqSize.w);
|
|
||||||
maxSize.h = MAX(maxSize.h, reqSize.h);
|
|
||||||
}
|
|
||||||
|
|
||||||
w = this->border_width.l + maxSize.w + this->border_width.r;
|
|
||||||
h = this->border_width.t + maxSize.h + this->border_width.b;
|
|
||||||
|
|
||||||
ev.setPreferredSize(Size(w, h));
|
|
||||||
}
|
|
||||||
|
|
||||||
} // namespace ui
|
} // namespace ui
|
||||||
|
@ -13,25 +13,17 @@
|
|||||||
|
|
||||||
namespace ui {
|
namespace ui {
|
||||||
|
|
||||||
|
class ListItem;
|
||||||
|
|
||||||
class ListBox : public Widget
|
class ListBox : public Widget
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
class Item : public Widget
|
|
||||||
{
|
|
||||||
public:
|
|
||||||
Item(const char* text);
|
|
||||||
|
|
||||||
protected:
|
|
||||||
bool onProcessMessage(Message* msg) OVERRIDE;
|
|
||||||
void onPreferredSize(PreferredSizeEvent& ev) OVERRIDE;
|
|
||||||
};
|
|
||||||
|
|
||||||
ListBox();
|
ListBox();
|
||||||
|
|
||||||
Item* getSelectedChild();
|
ListItem* getSelectedChild();
|
||||||
int getSelectedIndex();
|
int getSelectedIndex();
|
||||||
|
|
||||||
void selectChild(Item* item);
|
void selectChild(ListItem* item);
|
||||||
void selectIndex(int index);
|
void selectIndex(int index);
|
||||||
|
|
||||||
size_t getItemsCount() const;
|
size_t getItemsCount() const;
|
||||||
|
78
src/ui/listitem.cpp
Normal file
78
src/ui/listitem.cpp
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
// ASEPRITE gui library
|
||||||
|
// Copyright (C) 2001-2013 David Capello
|
||||||
|
//
|
||||||
|
// This source file is distributed under a BSD-like license, please
|
||||||
|
// read LICENSE.txt for more information.
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include "ui/listitem.h"
|
||||||
|
|
||||||
|
#include "ui/message.h"
|
||||||
|
#include "ui/preferred_size_event.h"
|
||||||
|
#include "ui/theme.h"
|
||||||
|
#include "ui/view.h"
|
||||||
|
|
||||||
|
using namespace gfx;
|
||||||
|
|
||||||
|
namespace ui {
|
||||||
|
|
||||||
|
ListItem::ListItem(const char* text)
|
||||||
|
: Widget(JI_LISTITEM)
|
||||||
|
{
|
||||||
|
setAlign(JI_LEFT | JI_MIDDLE);
|
||||||
|
setText(text);
|
||||||
|
initTheme();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ListItem::onProcessMessage(Message* msg)
|
||||||
|
{
|
||||||
|
switch (msg->type) {
|
||||||
|
|
||||||
|
case JM_SETPOS: {
|
||||||
|
JRect crect;
|
||||||
|
|
||||||
|
jrect_copy(this->rc, &msg->setpos.rect);
|
||||||
|
crect = jwidget_get_child_rect(this);
|
||||||
|
|
||||||
|
UI_FOREACH_WIDGET(getChildren(), it)
|
||||||
|
jwidget_set_rect(*it, crect);
|
||||||
|
|
||||||
|
jrect_free(crect);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
case JM_DRAW:
|
||||||
|
this->getTheme()->draw_listitem(this, &msg->draw.rect);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Widget::onProcessMessage(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
void ListItem::onPreferredSize(PreferredSizeEvent& ev)
|
||||||
|
{
|
||||||
|
int w = 0, h = 0;
|
||||||
|
Size maxSize;
|
||||||
|
|
||||||
|
if (hasText()) {
|
||||||
|
maxSize.w = jwidget_get_text_length(this);
|
||||||
|
maxSize.h = jwidget_get_text_height(this);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
maxSize.w = maxSize.h = 0;
|
||||||
|
|
||||||
|
UI_FOREACH_WIDGET(getChildren(), it) {
|
||||||
|
Size reqSize = (*it)->getPreferredSize();
|
||||||
|
|
||||||
|
maxSize.w = MAX(maxSize.w, reqSize.w);
|
||||||
|
maxSize.h = MAX(maxSize.h, reqSize.h);
|
||||||
|
}
|
||||||
|
|
||||||
|
w = this->border_width.l + maxSize.w + this->border_width.r;
|
||||||
|
h = this->border_width.t + maxSize.h + this->border_width.b;
|
||||||
|
|
||||||
|
ev.setPreferredSize(Size(w, h));
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace ui
|
27
src/ui/listitem.h
Normal file
27
src/ui/listitem.h
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
// ASEPRITE gui library
|
||||||
|
// Copyright (C) 2001-2013 David Capello
|
||||||
|
//
|
||||||
|
// This source file is distributed under a BSD-like license, please
|
||||||
|
// read LICENSE.txt for more information.
|
||||||
|
|
||||||
|
#ifndef UI_LISTITEM_H_INCLUDED
|
||||||
|
#define UI_LISTITEM_H_INCLUDED
|
||||||
|
|
||||||
|
#include "base/compiler_specific.h"
|
||||||
|
#include "ui/widget.h"
|
||||||
|
|
||||||
|
namespace ui {
|
||||||
|
|
||||||
|
class ListItem : public Widget
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
ListItem(const char* text);
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool onProcessMessage(Message* msg) OVERRIDE;
|
||||||
|
void onPreferredSize(PreferredSizeEvent& ev) OVERRIDE;
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace ui
|
||||||
|
|
||||||
|
#endif
|
Loading…
x
Reference in New Issue
Block a user