aseprite/src/ui/combobox.cpp

514 lines
10 KiB
C++
Raw Normal View History

// ASEPRITE gui library
2012-01-06 03:52:11 +00:00
// Copyright (C) 2001-2012 David Capello
//
// This source file is distributed under a BSD-like license, please
// read LICENSE.txt for more information.
2007-09-18 23:57:02 +00:00
2009-07-12 20:29:16 +00:00
#include "config.h"
#include "base/compiler_specific.h"
#include "gfx/size.h"
2012-06-18 01:49:58 +00:00
#include "ui/gui.h"
#include "ui/preferred_size_event.h"
2007-09-18 23:57:02 +00:00
#include <allegro.h>
using namespace gfx;
namespace ui {
class ComboBoxButton : public Button
{
public:
ComboBoxButton() : Button("") {
setFocusStop(false);
}
void onPaint(PaintEvent& ev) OVERRIDE {
getTheme()->paintComboBoxButton(ev);
}
};
class ComboBoxEntry : public Entry
{
public:
ComboBoxEntry(ComboBox* comboBox)
: Entry(256, ""),
m_comboBox(comboBox) {
}
protected:
bool onProcessMessage(Message* msg) OVERRIDE;
private:
ComboBox* m_comboBox;
};
class ComboBoxListBox : public ListBox
{
public:
ComboBoxListBox(ComboBox* comboBox)
: m_comboBox(comboBox) {
}
protected:
bool onProcessMessage(Message* msg) OVERRIDE;
void onChangeSelectedItem() OVERRIDE;
private:
bool isValidItem(int index) const {
return (index >= 0 && index < m_comboBox->getItemCount());
}
ComboBox* m_comboBox;
};
struct ComboBox::Item
{
std::string text;
void* data;
Item() : data(NULL) { }
};
ComboBox::ComboBox()
: Widget(JI_COMBOBOX)
2007-09-18 23:57:02 +00:00
{
m_entry = new ComboBoxEntry(this);
m_button = new ComboBoxButton();
m_window = NULL;
m_selected = 0;
m_editable = false;
m_clickopen = true;
m_casesensitive = true;
2007-09-18 23:57:02 +00:00
// TODO this separation should be from the Theme*
this->child_spacing = 0;
2007-09-18 23:57:02 +00:00
m_entry->setExpansive(true);
// When the "m_button" is clicked ("Click" signal) call onButtonClick() method
m_button->Click.connect(&ComboBox::onButtonClick, this);
2007-09-18 23:57:02 +00:00
addChild(m_entry);
addChild(m_button);
2007-09-18 23:57:02 +00:00
setFocusStop(true);
setEditable(m_editable);
initTheme();
2007-09-18 23:57:02 +00:00
}
ComboBox::~ComboBox()
2007-09-18 23:57:02 +00:00
{
removeAllItems();
}
2007-09-18 23:57:02 +00:00
void ComboBox::setEditable(bool state)
{
m_editable = state;
2007-09-18 23:57:02 +00:00
if (state) {
2010-12-08 17:28:13 +00:00
m_entry->setReadOnly(false);
m_entry->showCaret();
2007-09-18 23:57:02 +00:00
}
else {
2010-12-08 17:28:13 +00:00
m_entry->setReadOnly(true);
m_entry->hideCaret();
2007-09-18 23:57:02 +00:00
}
}
void ComboBox::setClickOpen(bool state)
2007-09-18 23:57:02 +00:00
{
m_clickopen = state;
2007-09-18 23:57:02 +00:00
}
void ComboBox::setCaseSensitive(bool state)
2007-09-18 23:57:02 +00:00
{
m_casesensitive = state;
2007-09-18 23:57:02 +00:00
}
bool ComboBox::isEditable()
2007-09-18 23:57:02 +00:00
{
return m_editable;
2007-09-18 23:57:02 +00:00
}
bool ComboBox::isClickOpen()
2007-09-18 23:57:02 +00:00
{
return m_clickopen;
2007-09-18 23:57:02 +00:00
}
bool ComboBox::isCaseSensitive()
2007-09-18 23:57:02 +00:00
{
return m_casesensitive;
2007-09-18 23:57:02 +00:00
}
int ComboBox::addItem(const std::string& text)
2007-09-18 23:57:02 +00:00
{
bool sel_first = m_items.empty();
Item* item = new Item();
item->text = text;
2007-09-18 23:57:02 +00:00
m_items.push_back(item);
2007-09-18 23:57:02 +00:00
if (sel_first)
setSelectedItem(0);
return m_items.size()-1;
2007-09-18 23:57:02 +00:00
}
void ComboBox::insertItem(int itemIndex, const std::string& text)
{
bool sel_first = m_items.empty();
Item* item = new Item();
item->text = text;
m_items.insert(m_items.begin() + itemIndex, item);
if (sel_first)
setSelectedItem(0);
}
void ComboBox::removeItem(int itemIndex)
2007-09-18 23:57:02 +00:00
{
ASSERT(itemIndex >= 0 && (size_t)itemIndex < m_items.size());
2007-09-18 23:57:02 +00:00
Item* item = m_items[itemIndex];
2007-09-18 23:57:02 +00:00
m_items.erase(m_items.begin() + itemIndex);
delete item;
}
void ComboBox::removeAllItems()
{
std::vector<Item*>::iterator it, end = m_items.end();
for (it = m_items.begin(); it != end; ++it)
delete *it;
m_items.clear();
2007-09-18 23:57:02 +00:00
}
int ComboBox::getItemCount()
2007-09-18 23:57:02 +00:00
{
return m_items.size();
}
std::string ComboBox::getItemText(int itemIndex)
{
if (itemIndex >= 0 && (size_t)itemIndex < m_items.size()) {
Item* item = m_items[itemIndex];
return item->text;
2007-09-18 23:57:02 +00:00
}
else
return "";
2007-09-18 23:57:02 +00:00
}
void ComboBox::setItemText(int itemIndex, const std::string& text)
2007-09-18 23:57:02 +00:00
{
ASSERT(itemIndex >= 0 && (size_t)itemIndex < m_items.size());
Item* item = m_items[itemIndex];
item->text = text;
2007-09-18 23:57:02 +00:00
}
int ComboBox::findItemIndex(const std::string& text)
2007-09-18 23:57:02 +00:00
{
int itemIndex = 0;
2007-09-18 23:57:02 +00:00
std::vector<Item*>::iterator it, end = m_items.end();
for (it = m_items.begin(); it != end; ++it) {
Item* item = *it;
if ((m_casesensitive && ustrcmp(item->text.c_str(), text.c_str()) == 0) ||
(!m_casesensitive && ustricmp(item->text.c_str(), text.c_str()) == 0)) {
return itemIndex;
}
itemIndex++;
}
return -1;
2007-09-18 23:57:02 +00:00
}
int ComboBox::getSelectedItem()
2007-09-18 23:57:02 +00:00
{
return !m_items.empty() ? m_selected: -1;
2007-09-18 23:57:02 +00:00
}
void ComboBox::setSelectedItem(int itemIndex)
2007-09-18 23:57:02 +00:00
{
if (itemIndex >= 0 && (size_t)itemIndex < m_items.size()) {
m_selected = itemIndex;
2007-09-18 23:57:02 +00:00
std::vector<Item*>::iterator it = m_items.begin() + itemIndex;
Item* item = *it;
m_entry->setText(item->text.c_str());
}
}
void* ComboBox::getItemData(int itemIndex)
{
if (itemIndex >= 0 && (size_t)itemIndex < m_items.size()) {
Item* item = m_items[itemIndex];
return item->data;
}
2007-09-18 23:57:02 +00:00
else
return NULL;
}
void ComboBox::setItemData(int itemIndex, void* data)
2007-09-18 23:57:02 +00:00
{
ASSERT(itemIndex >= 0 && (size_t)itemIndex < m_items.size());
2007-09-18 23:57:02 +00:00
Item* item = m_items[itemIndex];
item->data = data;
2007-09-18 23:57:02 +00:00
}
2010-12-08 17:28:13 +00:00
Entry* ComboBox::getEntryWidget()
2007-09-18 23:57:02 +00:00
{
return m_entry;
2007-09-18 23:57:02 +00:00
}
Button* ComboBox::getButtonWidget()
2007-09-18 23:57:02 +00:00
{
return m_button;
2007-09-18 23:57:02 +00:00
}
bool ComboBox::onProcessMessage(Message* msg)
{
2007-09-18 23:57:02 +00:00
switch (msg->type) {
case JM_CLOSE:
closeListBox();
break;
case JM_SETPOS: {
JRect cbox = jrect_new_copy(&msg->setpos.rect);
jrect_copy(this->rc, cbox);
// Button
Size buttonSize = m_button->getPreferredSize();
cbox->x1 = msg->setpos.rect.x2 - buttonSize.w;
jwidget_set_rect(m_button, cbox);
// Entry
cbox->x2 = cbox->x1;
cbox->x1 = msg->setpos.rect.x1;
jwidget_set_rect(m_entry, cbox);
jrect_free(cbox);
return true;
}
case JM_WINMOVE:
if (m_window) {
JRect rc = getListBoxPos();
m_window->move_window(rc);
jrect_free(rc);
2007-09-18 23:57:02 +00:00
}
break;
case JM_BUTTONPRESSED:
if (m_window != NULL) {
if (!View::getView(m_listbox)->hasMouse()) {
closeListBox();
return true;
}
2007-09-18 23:57:02 +00:00
}
break;
2007-09-18 23:57:02 +00:00
}
return Widget::onProcessMessage(msg);
2007-09-18 23:57:02 +00:00
}
void ComboBox::onPreferredSize(PreferredSizeEvent& ev)
{
Size reqSize(0, 0);
Size entrySize = m_entry->getPreferredSize();
// Get the text-length of every item and put in 'w' the maximum value
std::vector<Item*>::iterator it, end = m_items.end();
for (it = m_items.begin(); it != end; ++it) {
int item_w =
2*jguiscale()+
text_length(this->getFont(), (*it)->text.c_str())+
10*jguiscale();
reqSize.w = MAX(reqSize.w, item_w);
}
reqSize.w += entrySize.w;
reqSize.h += entrySize.h;
Size buttonSize = m_button->getPreferredSize();
reqSize.w += buttonSize.w;
reqSize.h = MAX(reqSize.h, buttonSize.h);
ev.setPreferredSize(reqSize);
}
bool ComboBoxEntry::onProcessMessage(Message* msg)
2007-09-18 23:57:02 +00:00
{
switch (msg->type) {
case JM_KEYPRESSED:
if (hasFocus()) {
if (!m_comboBox->isEditable()) {
if (msg->key.scancode == KEY_SPACE ||
msg->key.scancode == KEY_ENTER ||
msg->key.scancode == KEY_ENTER_PAD) {
m_comboBox->switchListBox();
return true;
}
}
else {
if (msg->key.scancode == KEY_ENTER ||
msg->key.scancode == KEY_ENTER_PAD) {
m_comboBox->switchListBox();
return true;
}
}
2007-09-18 23:57:02 +00:00
}
break;
case JM_BUTTONPRESSED:
if (m_comboBox->isClickOpen()) {
m_comboBox->switchListBox();
2007-12-04 21:50:31 +00:00
}
2007-09-18 23:57:02 +00:00
if (m_comboBox->isEditable()) {
getManager()->setFocus(this);
2007-09-18 23:57:02 +00:00
}
else
return true;
2007-09-18 23:57:02 +00:00
break;
2009-11-22 00:26:58 +00:00
case JM_DRAW:
getTheme()->draw_combobox_entry(this, &msg->draw.rect);
2009-11-22 00:26:58 +00:00
return true;
2010-12-08 17:28:13 +00:00
2007-09-18 23:57:02 +00:00
}
return Entry::onProcessMessage(msg);
2007-09-18 23:57:02 +00:00
}
bool ComboBoxListBox::onProcessMessage(Message* msg)
2007-09-18 23:57:02 +00:00
{
switch (msg->type) {
case JM_BUTTONRELEASED:
{
int index = m_comboBox->getSelectedItem();
if (isValidItem(index))
m_comboBox->Change();
m_comboBox->closeListBox();
2007-09-18 23:57:02 +00:00
}
return true;
2007-09-18 23:57:02 +00:00
case JM_KEYPRESSED:
if (hasFocus()) {
if (msg->key.scancode == KEY_SPACE ||
msg->key.scancode == KEY_ENTER ||
msg->key.scancode == KEY_ENTER_PAD) {
m_comboBox->closeListBox();
return true;
}
2007-09-18 23:57:02 +00:00
}
break;
}
return ListBox::onProcessMessage(msg);
}
void ComboBoxListBox::onChangeSelectedItem()
{
ListBox::onChangeSelectedItem();
int index = getSelectedIndex();
if (isValidItem(index))
m_comboBox->setSelectedItem(index);
2007-09-18 23:57:02 +00:00
}
// When the mouse is clicked we switch the visibility-status of the list-box
void ComboBox::onButtonClick(Event& ev)
2007-09-18 23:57:02 +00:00
{
switchListBox();
2007-09-18 23:57:02 +00:00
}
void ComboBox::openListBox()
2007-09-18 23:57:02 +00:00
{
if (!m_window) {
2012-07-09 02:24:42 +00:00
m_window = new Window(false, NULL);
View* view = new View();
m_listbox = new ComboBoxListBox(this);
2007-09-18 23:57:02 +00:00
std::vector<Item*>::iterator it, end = m_items.end();
for (it = m_items.begin(); it != end; ++it) {
Item* item = *it;
m_listbox->addChild(new ListBox::Item(item->text.c_str()));
}
2007-09-18 23:57:02 +00:00
m_window->set_ontop(true);
jwidget_noborders(m_window);
2007-09-18 23:57:02 +00:00
Widget* viewport = view->getViewport();
int size = getItemCount();
jwidget_set_min_size
(viewport,
m_button->rc->x2 - m_entry->rc->x1 - view->border_width.l - view->border_width.r,
+viewport->border_width.t
+(2*jguiscale()+jwidget_get_text_height(m_listbox))*MID(1, size, 16)+
+viewport->border_width.b);
2007-09-18 23:57:02 +00:00
m_window->addChild(view);
view->attachToView(m_listbox);
2007-09-18 23:57:02 +00:00
m_listbox->selectIndex(m_selected);
2007-09-18 23:57:02 +00:00
m_window->remap_window();
2007-09-18 23:57:02 +00:00
JRect rc = getListBoxPos();
m_window->position_window(rc->x1, rc->y1);
2007-09-18 23:57:02 +00:00
jrect_free(rc);
getManager()->addMessageFilter(JM_BUTTONPRESSED, this);
2012-07-09 02:24:42 +00:00
m_window->openWindow();
getManager()->setFocus(m_listbox);
2007-09-18 23:57:02 +00:00
}
}
void ComboBox::closeListBox()
2007-09-18 23:57:02 +00:00
{
if (m_window) {
m_window->closeWindow(this);
delete m_window; // window, frame
m_window = NULL;
2007-09-18 23:57:02 +00:00
getManager()->removeMessageFilter(JM_BUTTONPRESSED, this);
getManager()->setFocus(m_entry);
2007-09-18 23:57:02 +00:00
}
}
void ComboBox::switchListBox()
2007-09-18 23:57:02 +00:00
{
if (!m_window)
openListBox();
2007-09-18 23:57:02 +00:00
else
closeListBox();
2007-09-18 23:57:02 +00:00
}
JRect ComboBox::getListBoxPos()
2007-09-18 23:57:02 +00:00
{
JRect rc = jrect_new(m_entry->rc->x1,
m_entry->rc->y2,
m_button->rc->x2,
m_entry->rc->y2+jrect_h(m_window->rc));
2007-09-18 23:57:02 +00:00
if (rc->y2 > JI_SCREEN_H)
jrect_displace(rc, 0, -(jrect_h(rc)+jrect_h(m_entry->rc)));
2007-09-18 23:57:02 +00:00
return rc;
}
} // namespace ui