2012-01-06 04:12:57 +00:00
|
|
|
// ASEPRITE gui library
|
2012-01-06 03:52:11 +00:00
|
|
|
// Copyright (C) 2001-2012 David Capello
|
2010-09-27 22:18:17 +00:00
|
|
|
//
|
|
|
|
// This source file is ditributed 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"
|
|
|
|
|
2011-03-27 23:42:16 +00:00
|
|
|
#include "base/compiler_specific.h"
|
2010-09-25 19:22:32 +00:00
|
|
|
#include "gfx/size.h"
|
2011-01-30 22:12:10 +00:00
|
|
|
#include "gui/gui.h"
|
2010-09-29 20:14:11 +00:00
|
|
|
#include "gui/preferred_size_event.h"
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2011-03-27 23:42:16 +00:00
|
|
|
#include <allegro.h>
|
|
|
|
|
2010-09-25 19:22:32 +00:00
|
|
|
using namespace gfx;
|
|
|
|
|
2012-06-18 01:02:54 +00:00
|
|
|
namespace ui {
|
|
|
|
|
2011-03-06 19:15:05 +00:00
|
|
|
class ComboBoxButton : public Button
|
|
|
|
{
|
|
|
|
public:
|
2012-06-16 03:06:54 +00:00
|
|
|
ComboBoxButton() : Button("") {
|
|
|
|
setFocusStop(false);
|
|
|
|
}
|
2011-03-06 19:15:05 +00:00
|
|
|
|
2012-06-16 02:37:59 +00:00
|
|
|
void onPaint(PaintEvent& ev) OVERRIDE {
|
2011-03-06 19:15:05 +00:00
|
|
|
getTheme()->paintComboBoxButton(ev);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2012-06-16 02:37:59 +00:00
|
|
|
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;
|
|
|
|
};
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
struct ComboBox::Item
|
2008-02-04 02:37:26 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
std::string text;
|
2010-01-26 00:38:05 +00:00
|
|
|
void* data;
|
2010-06-25 02:44:59 +00:00
|
|
|
|
|
|
|
Item() : data(NULL) { }
|
2010-01-26 00:38:05 +00:00
|
|
|
};
|
2008-02-04 02:37:26 +00:00
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
ComboBox::ComboBox()
|
|
|
|
: Widget(JI_COMBOBOX)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2012-06-16 02:37:59 +00:00
|
|
|
m_entry = new ComboBoxEntry(this);
|
2011-03-06 19:15:05 +00:00
|
|
|
m_button = new ComboBoxButton();
|
2010-06-25 02:44:59 +00:00
|
|
|
m_window = NULL;
|
|
|
|
m_selected = 0;
|
|
|
|
m_editable = false;
|
|
|
|
m_clickopen = true;
|
|
|
|
m_casesensitive = true;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2011-01-21 20:50:04 +00:00
|
|
|
// TODO this separation should be from the Theme*
|
2010-06-25 02:44:59 +00:00
|
|
|
this->child_spacing = 0;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2012-04-05 22:00:19 +00:00
|
|
|
m_entry->setExpansive(true);
|
2010-08-23 20:41:19 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2011-03-30 00:35:17 +00:00
|
|
|
addChild(m_entry);
|
|
|
|
addChild(m_button);
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2012-06-16 02:37:59 +00:00
|
|
|
setFocusStop(true);
|
2010-06-25 02:44:59 +00:00
|
|
|
setEditable(m_editable);
|
2008-02-04 02:37:26 +00:00
|
|
|
|
2011-02-15 12:00:29 +00:00
|
|
|
initTheme();
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
ComboBox::~ComboBox()
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
removeAllItems();
|
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-06-25 02:44:59 +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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
void ComboBox::setClickOpen(bool state)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
m_clickopen = state;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
void ComboBox::setCaseSensitive(bool state)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
m_casesensitive = state;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
bool ComboBox::isEditable()
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
return m_editable;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
bool ComboBox::isClickOpen()
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
return m_clickopen;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
bool ComboBox::isCaseSensitive()
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
return m_casesensitive;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
int ComboBox::addItem(const std::string& text)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
bool sel_first = m_items.empty();
|
|
|
|
Item* item = new Item();
|
|
|
|
item->text = text;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
m_items.push_back(item);
|
2007-09-18 23:57:02 +00:00
|
|
|
|
|
|
|
if (sel_first)
|
2010-06-25 02:44:59 +00:00
|
|
|
setSelectedItem(0);
|
|
|
|
|
|
|
|
return m_items.size()-1;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
void ComboBox::insertItem(int itemIndex, const std::string& text)
|
2010-03-08 01:48:01 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
bool sel_first = m_items.empty();
|
|
|
|
Item* item = new Item();
|
|
|
|
item->text = text;
|
2010-03-08 01:48:01 +00:00
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
m_items.insert(m_items.begin() + itemIndex, item);
|
2010-03-08 01:48:01 +00:00
|
|
|
|
|
|
|
if (sel_first)
|
2010-06-25 02:44:59 +00:00
|
|
|
setSelectedItem(0);
|
2010-03-08 01:48:01 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
void ComboBox::removeItem(int itemIndex)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-08-04 02:33:44 +00:00
|
|
|
ASSERT(itemIndex >= 0 && (size_t)itemIndex < m_items.size());
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
Item* item = m_items[itemIndex];
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
m_items.erase(m_items.begin() + itemIndex);
|
|
|
|
delete item;
|
2008-02-04 02:37:26 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
void ComboBox::removeAllItems()
|
2008-02-04 02:37:26 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
std::vector<Item*>::iterator it, end = m_items.end();
|
|
|
|
for (it = m_items.begin(); it != end; ++it)
|
|
|
|
delete *it;
|
2008-02-04 02:37:26 +00:00
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
m_items.clear();
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
int ComboBox::getItemCount()
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
return m_items.size();
|
|
|
|
}
|
2008-02-04 02:37:26 +00:00
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
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
|
|
|
}
|
2010-06-25 02:44:59 +00:00
|
|
|
else
|
|
|
|
return "";
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
void ComboBox::setItemText(int itemIndex, const std::string& text)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-08-04 02:33:44 +00:00
|
|
|
ASSERT(itemIndex >= 0 && (size_t)itemIndex < m_items.size());
|
2010-06-25 02:44:59 +00:00
|
|
|
|
|
|
|
Item* item = m_items[itemIndex];
|
|
|
|
item->text = text;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
int ComboBox::findItemIndex(const std::string& text)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
int itemIndex = 0;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-06-25 02:44:59 +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) ||
|
2012-01-05 22:45:03 +00:00
|
|
|
(!m_casesensitive && ustricmp(item->text.c_str(), text.c_str()) == 0)) {
|
2010-06-25 02:44:59 +00:00
|
|
|
return itemIndex;
|
|
|
|
}
|
|
|
|
|
|
|
|
itemIndex++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
int ComboBox::getSelectedItem()
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
return !m_items.empty() ? m_selected: -1;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
void ComboBox::setSelectedItem(int itemIndex)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
if (itemIndex >= 0 && (size_t)itemIndex < m_items.size()) {
|
|
|
|
m_selected = itemIndex;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
std::vector<Item*>::iterator it = m_items.begin() + itemIndex;
|
|
|
|
Item* item = *it;
|
|
|
|
m_entry->setText(item->text.c_str());
|
2008-02-04 02:37:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
void* ComboBox::getItemData(int itemIndex)
|
2008-02-04 02:37:26 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
if (itemIndex >= 0 && (size_t)itemIndex < m_items.size()) {
|
|
|
|
Item* item = m_items[itemIndex];
|
2008-02-04 02:37:26 +00:00
|
|
|
return item->data;
|
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
else
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
void ComboBox::setItemData(int itemIndex, void* data)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-08-04 02:33:44 +00:00
|
|
|
ASSERT(itemIndex >= 0 && (size_t)itemIndex < m_items.size());
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-06-25 02:44:59 +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
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
return m_entry;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-08-23 20:41:19 +00:00
|
|
|
Button* ComboBox::getButtonWidget()
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
return m_button;
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2011-04-02 16:14:07 +00:00
|
|
|
bool ComboBox::onProcessMessage(Message* msg)
|
2008-02-04 02:37:26 +00:00
|
|
|
{
|
2007-09-18 23:57:02 +00:00
|
|
|
switch (msg->type) {
|
|
|
|
|
|
|
|
case JM_CLOSE:
|
2010-06-25 02:44:59 +00:00
|
|
|
closeListBox();
|
2008-02-04 02:37:26 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case JM_SETPOS: {
|
|
|
|
JRect cbox = jrect_new_copy(&msg->setpos.rect);
|
2010-06-25 02:44:59 +00:00
|
|
|
jrect_copy(this->rc, cbox);
|
2008-02-04 02:37:26 +00:00
|
|
|
|
2010-08-03 01:57:41 +00:00
|
|
|
// Button
|
|
|
|
Size buttonSize = m_button->getPreferredSize();
|
|
|
|
cbox->x1 = msg->setpos.rect.x2 - buttonSize.w;
|
2010-06-25 02:44:59 +00:00
|
|
|
jwidget_set_rect(m_button, cbox);
|
2008-02-04 02:37:26 +00:00
|
|
|
|
2010-08-03 01:57:41 +00:00
|
|
|
// Entry
|
2008-02-04 02:37:26 +00:00
|
|
|
cbox->x2 = cbox->x1;
|
|
|
|
cbox->x1 = msg->setpos.rect.x1;
|
2010-06-25 02:44:59 +00:00
|
|
|
jwidget_set_rect(m_entry, cbox);
|
2008-02-04 02:37:26 +00:00
|
|
|
|
|
|
|
jrect_free(cbox);
|
2009-08-17 18:00:38 +00:00
|
|
|
return true;
|
2008-02-04 02:37:26 +00:00
|
|
|
}
|
2012-01-05 22:45:03 +00:00
|
|
|
|
2008-02-04 02:37:26 +00:00
|
|
|
case JM_WINMOVE:
|
2010-06-25 02:44:59 +00:00
|
|
|
if (m_window) {
|
2012-01-05 22:45:03 +00:00
|
|
|
JRect rc = getListBoxPos();
|
|
|
|
m_window->move_window(rc);
|
|
|
|
jrect_free(rc);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2008-02-04 02:37:26 +00:00
|
|
|
case JM_BUTTONPRESSED:
|
2010-06-25 02:44:59 +00:00
|
|
|
if (m_window != NULL) {
|
2012-01-05 22:45:03 +00:00
|
|
|
if (!View::getView(m_listbox)->hasMouse()) {
|
|
|
|
closeListBox();
|
|
|
|
return true;
|
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
break;
|
2008-02-04 02:37:26 +00:00
|
|
|
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-08-03 00:29:56 +00:00
|
|
|
return Widget::onProcessMessage(msg);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-08-03 01:57:41 +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);
|
|
|
|
}
|
|
|
|
|
2012-06-16 02:37:59 +00:00
|
|
|
bool ComboBoxEntry::onProcessMessage(Message* msg)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
|
|
|
switch (msg->type) {
|
|
|
|
|
2008-02-29 19:29:49 +00:00
|
|
|
case JM_KEYPRESSED:
|
2012-06-16 02:37:59 +00:00
|
|
|
if (hasFocus()) {
|
|
|
|
if (!m_comboBox->isEditable()) {
|
2012-01-05 22:45:03 +00:00
|
|
|
if (msg->key.scancode == KEY_SPACE ||
|
|
|
|
msg->key.scancode == KEY_ENTER ||
|
|
|
|
msg->key.scancode == KEY_ENTER_PAD) {
|
2012-06-16 02:37:59 +00:00
|
|
|
m_comboBox->switchListBox();
|
2012-01-05 22:45:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (msg->key.scancode == KEY_ENTER ||
|
|
|
|
msg->key.scancode == KEY_ENTER_PAD) {
|
2012-06-16 02:37:59 +00:00
|
|
|
m_comboBox->switchListBox();
|
2012-01-05 22:45:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case JM_BUTTONPRESSED:
|
2012-06-16 02:37:59 +00:00
|
|
|
if (m_comboBox->isClickOpen()) {
|
|
|
|
m_comboBox->switchListBox();
|
2007-12-04 21:50:31 +00:00
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2012-06-16 02:37:59 +00:00
|
|
|
if (m_comboBox->isEditable()) {
|
|
|
|
getManager()->setFocus(this);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
else
|
2012-01-05 22:45:03 +00:00
|
|
|
return true;
|
2007-09-18 23:57:02 +00:00
|
|
|
break;
|
2009-11-22 00:26:58 +00:00
|
|
|
|
|
|
|
case JM_DRAW:
|
2012-06-16 02:37:59 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2012-06-16 02:37:59 +00:00
|
|
|
return Entry::onProcessMessage(msg);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2012-06-16 02:37:59 +00:00
|
|
|
bool ComboBoxListBox::onProcessMessage(Message* msg)
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
|
|
|
switch (msg->type) {
|
|
|
|
|
|
|
|
case JM_BUTTONRELEASED:
|
|
|
|
{
|
2012-06-16 02:37:59 +00:00
|
|
|
int index = m_comboBox->getSelectedItem();
|
|
|
|
if (isValidItem(index))
|
|
|
|
m_comboBox->Change();
|
2008-02-10 18:52:42 +00:00
|
|
|
|
2012-06-16 02:37:59 +00:00
|
|
|
m_comboBox->closeListBox();
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
2009-08-17 18:00:38 +00:00
|
|
|
return true;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2008-02-29 19:29:49 +00:00
|
|
|
case JM_KEYPRESSED:
|
2012-06-16 02:37:59 +00:00
|
|
|
if (hasFocus()) {
|
2012-01-05 22:45:03 +00:00
|
|
|
if (msg->key.scancode == KEY_SPACE ||
|
|
|
|
msg->key.scancode == KEY_ENTER ||
|
|
|
|
msg->key.scancode == KEY_ENTER_PAD) {
|
2012-06-16 02:37:59 +00:00
|
|
|
m_comboBox->closeListBox();
|
2012-01-05 22:45:03 +00:00
|
|
|
return true;
|
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-06-16 02:37:59 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-08-23 20:41:19 +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
|
|
|
{
|
2010-08-23 20:41:19 +00:00
|
|
|
switchListBox();
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
void ComboBox::openListBox()
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
if (!m_window) {
|
|
|
|
m_window = new Frame(false, NULL);
|
2011-02-20 21:35:21 +00:00
|
|
|
View* view = new View();
|
2012-06-16 02:37:59 +00:00
|
|
|
m_listbox = new ComboBoxListBox(this);
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
std::vector<Item*>::iterator it, end = m_items.end();
|
|
|
|
for (it = m_items.begin(); it != end; ++it) {
|
|
|
|
Item* item = *it;
|
2012-04-06 02:19:32 +00:00
|
|
|
m_listbox->addChild(new ListBox::Item(item->text.c_str()));
|
2008-02-04 02:37:26 +00:00
|
|
|
}
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
m_window->set_ontop(true);
|
|
|
|
jwidget_noborders(m_window);
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2011-02-20 21:35:21 +00:00
|
|
|
Widget* viewport = view->getViewport();
|
2010-06-25 02:44:59 +00:00
|
|
|
int size = getItemCount();
|
2007-12-06 20:05:32 +00:00
|
|
|
jwidget_set_min_size
|
2010-03-22 01:35:55 +00:00
|
|
|
(viewport,
|
2010-06-25 02:44:59 +00:00
|
|
|
m_button->rc->x2 - m_entry->rc->x1 - view->border_width.l - view->border_width.r,
|
2010-03-22 01:35:55 +00:00
|
|
|
+viewport->border_width.t
|
2010-06-25 02:44:59 +00:00
|
|
|
+(2*jguiscale()+jwidget_get_text_height(m_listbox))*MID(1, size, 16)+
|
2010-03-22 01:35:55 +00:00
|
|
|
+viewport->border_width.b);
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2011-03-30 00:35:17 +00:00
|
|
|
m_window->addChild(view);
|
2011-02-20 21:35:21 +00:00
|
|
|
view->attachToView(m_listbox);
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2012-04-06 02:19:32 +00:00
|
|
|
m_listbox->selectIndex(m_selected);
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
m_window->remap_window();
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
JRect rc = getListBoxPos();
|
|
|
|
m_window->position_window(rc->x1, rc->y1);
|
2007-09-18 23:57:02 +00:00
|
|
|
jrect_free(rc);
|
|
|
|
|
2012-04-15 18:21:24 +00:00
|
|
|
getManager()->addMessageFilter(JM_BUTTONPRESSED, this);
|
2008-02-04 02:37:26 +00:00
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
m_window->open_window_bg();
|
2012-04-15 18:21:24 +00:00
|
|
|
getManager()->setFocus(m_listbox);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
void ComboBox::closeListBox()
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
if (m_window) {
|
|
|
|
m_window->closeWindow(this);
|
2012-01-05 22:45:03 +00:00
|
|
|
delete m_window; // window, frame
|
2010-06-25 02:44:59 +00:00
|
|
|
m_window = NULL;
|
2007-09-18 23:57:02 +00:00
|
|
|
|
2012-04-15 18:21:24 +00:00
|
|
|
getManager()->removeMessageFilter(JM_BUTTONPRESSED, this);
|
|
|
|
getManager()->setFocus(m_entry);
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
void ComboBox::switchListBox()
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
if (!m_window)
|
|
|
|
openListBox();
|
2007-09-18 23:57:02 +00:00
|
|
|
else
|
2010-06-25 02:44:59 +00:00
|
|
|
closeListBox();
|
2007-09-18 23:57:02 +00:00
|
|
|
}
|
|
|
|
|
2010-06-25 02:44:59 +00:00
|
|
|
JRect ComboBox::getListBoxPos()
|
2007-09-18 23:57:02 +00:00
|
|
|
{
|
2010-06-25 02:44:59 +00:00
|
|
|
JRect rc = jrect_new(m_entry->rc->x1,
|
2012-01-05 22:45:03 +00:00
|
|
|
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)
|
2010-06-25 02:44:59 +00:00
|
|
|
jrect_displace(rc, 0, -(jrect_h(rc)+jrect_h(m_entry->rc)));
|
2007-09-18 23:57:02 +00:00
|
|
|
return rc;
|
|
|
|
}
|
2012-06-18 01:02:54 +00:00
|
|
|
|
|
|
|
} // namespace ui
|