mirror of
https://github.com/aseprite/aseprite.git
synced 2025-01-30 15:32:38 +00:00
Refactor: Convert jlistbox and jlistitem to ListBox and ListBox::Item classes.
This commit is contained in:
parent
aa0555e662
commit
46bc974d3b
@ -68,7 +68,8 @@ NewFileCommand::NewFileCommand()
|
|||||||
*/
|
*/
|
||||||
void NewFileCommand::onExecute(Context* context)
|
void NewFileCommand::onExecute(Context* context)
|
||||||
{
|
{
|
||||||
JWidget width, height, radio1, radio2, radio3, colors, ok, bg_box;
|
JWidget width, height, radio1, radio2, radio3, colors, ok;
|
||||||
|
ListBox* bg_box;
|
||||||
PixelFormat format;
|
PixelFormat format;
|
||||||
int w, h, bg, ncolors;
|
int w, h, bg, ncolors;
|
||||||
char buf[1024];
|
char buf[1024];
|
||||||
@ -123,7 +124,7 @@ void NewFileCommand::onExecute(Context* context)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Select background color
|
// Select background color
|
||||||
jlistbox_select_index(bg_box, bg);
|
bg_box->selectIndex(bg);
|
||||||
|
|
||||||
// Open the window
|
// Open the window
|
||||||
window->open_window_fg();
|
window->open_window_fg();
|
||||||
@ -139,7 +140,7 @@ void NewFileCommand::onExecute(Context* context)
|
|||||||
w = width->getTextInt();
|
w = width->getTextInt();
|
||||||
h = height->getTextInt();
|
h = height->getTextInt();
|
||||||
ncolors = colors->getTextInt();
|
ncolors = colors->getTextInt();
|
||||||
bg = jlistbox_get_selected_index(bg_box);
|
bg = bg_box->getSelectedIndex();
|
||||||
|
|
||||||
w = MID(1, w, 9999);
|
w = MID(1, w, 9999);
|
||||||
h = MID(1, h, 9999);
|
h = MID(1, h, 9999);
|
||||||
|
@ -101,7 +101,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;
|
||||||
Widget* listitem = jlistitem_new(matrix->getName()); // TODO convert listitem to a class
|
ListBox::Item* listitem = new ListBox::Item(matrix->getName());
|
||||||
m_stockListBox->addChild(listitem);
|
m_stockListBox->addChild(listitem);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -134,7 +134,7 @@ private:
|
|||||||
|
|
||||||
void onMatrixChange()
|
void onMatrixChange()
|
||||||
{
|
{
|
||||||
Widget* selected = jlistbox_get_selected_child(m_stockListBox);
|
ListBox::Item* 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();
|
||||||
|
|
||||||
@ -157,7 +157,7 @@ private:
|
|||||||
WidgetPtr m_controlsWidget;
|
WidgetPtr m_controlsWidget;
|
||||||
ConvolutionMatrixStock& m_stock;
|
ConvolutionMatrixStock& m_stock;
|
||||||
View* m_view;
|
View* m_view;
|
||||||
Widget* m_stockListBox;
|
ListBox* m_stockListBox;
|
||||||
Button* m_reloadButton;
|
Button* m_reloadButton;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -377,7 +377,7 @@ static bool combobox_listbox_msg_proc(JWidget widget, Message* msg)
|
|||||||
|
|
||||||
case JM_SIGNAL:
|
case JM_SIGNAL:
|
||||||
if (msg->signal.num == JI_SIGNAL_LISTBOX_CHANGE) {
|
if (msg->signal.num == JI_SIGNAL_LISTBOX_CHANGE) {
|
||||||
int index = jlistbox_get_selected_index(widget);
|
int index = static_cast<ListBox*>(widget)->getSelectedIndex();
|
||||||
|
|
||||||
if (IS_VALID_ITEM(combobox, index))
|
if (IS_VALID_ITEM(combobox, index))
|
||||||
combobox->setSelectedItem(index);
|
combobox->setSelectedItem(index);
|
||||||
@ -438,7 +438,7 @@ void ComboBox::openListBox()
|
|||||||
if (!m_window) {
|
if (!m_window) {
|
||||||
m_window = new Frame(false, NULL);
|
m_window = new Frame(false, NULL);
|
||||||
View* view = new View();
|
View* view = new View();
|
||||||
m_listbox = jlistbox_new();
|
m_listbox = new ListBox();
|
||||||
|
|
||||||
m_listbox->user_data[0] = this;
|
m_listbox->user_data[0] = this;
|
||||||
jwidget_add_hook(m_listbox, JI_WIDGET,
|
jwidget_add_hook(m_listbox, JI_WIDGET,
|
||||||
@ -447,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(jlistitem_new(item->text.c_str()));
|
m_listbox->addChild(new ListBox::Item(item->text.c_str()));
|
||||||
}
|
}
|
||||||
|
|
||||||
m_window->set_ontop(true);
|
m_window->set_ontop(true);
|
||||||
@ -466,7 +466,7 @@ void ComboBox::openListBox()
|
|||||||
view->attachToView(m_listbox);
|
view->attachToView(m_listbox);
|
||||||
|
|
||||||
jwidget_signal_off(m_listbox);
|
jwidget_signal_off(m_listbox);
|
||||||
jlistbox_select_index(m_listbox, m_selected);
|
m_listbox->selectIndex(m_selected);
|
||||||
jwidget_signal_on(m_listbox);
|
jwidget_signal_on(m_listbox);
|
||||||
|
|
||||||
m_window->remap_window();
|
m_window->remap_window();
|
||||||
|
@ -17,6 +17,7 @@
|
|||||||
class Button;
|
class Button;
|
||||||
class Entry;
|
class Entry;
|
||||||
class Frame;
|
class Frame;
|
||||||
|
class ListBox;
|
||||||
|
|
||||||
class ComboBox : public Widget
|
class ComboBox : public Widget
|
||||||
{
|
{
|
||||||
@ -72,7 +73,7 @@ private:
|
|||||||
Entry* m_entry;
|
Entry* m_entry;
|
||||||
Button* m_button;
|
Button* m_button;
|
||||||
Frame* m_window;
|
Frame* m_window;
|
||||||
Widget* m_listbox;
|
ListBox* m_listbox;
|
||||||
std::vector<Item*> m_items;
|
std::vector<Item*> m_items;
|
||||||
int m_selected;
|
int m_selected;
|
||||||
bool m_editable : 1;
|
bool m_editable : 1;
|
||||||
|
@ -6,69 +6,51 @@
|
|||||||
|
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
|
|
||||||
#include <allegro/keyboard.h>
|
#include "gui/listbox.h"
|
||||||
|
|
||||||
#include "gfx/point.h"
|
|
||||||
#include "gfx/size.h"
|
|
||||||
#include "gui/list.h"
|
#include "gui/list.h"
|
||||||
#include "gui/manager.h"
|
|
||||||
#include "gui/message.h"
|
#include "gui/message.h"
|
||||||
#include "gui/rect.h"
|
#include "gui/preferred_size_event.h"
|
||||||
#include "gui/system.h"
|
#include "gui/system.h"
|
||||||
#include "gui/theme.h"
|
#include "gui/theme.h"
|
||||||
#include "gui/view.h"
|
#include "gui/view.h"
|
||||||
#include "gui/widget.h"
|
|
||||||
|
#include <allegro/keyboard.h>
|
||||||
|
|
||||||
using namespace gfx;
|
using namespace gfx;
|
||||||
|
|
||||||
static bool listbox_msg_proc(JWidget widget, Message* msg);
|
ListBox::ListBox()
|
||||||
static void listbox_request_size(JWidget widget, int *w, int *h);
|
: Widget(JI_LISTBOX)
|
||||||
static void listbox_set_position(JWidget widget, JRect rect);
|
|
||||||
static void listbox_dirty_children(JWidget widget);
|
|
||||||
|
|
||||||
static bool listitem_msg_proc(JWidget widget, Message* msg);
|
|
||||||
static void listitem_request_size(JWidget widget, int *w, int *h);
|
|
||||||
|
|
||||||
JWidget jlistbox_new()
|
|
||||||
{
|
{
|
||||||
JWidget widget = new Widget(JI_LISTBOX);
|
setFocusStop(true);
|
||||||
|
initTheme();
|
||||||
jwidget_add_hook(widget, JI_LISTBOX, listbox_msg_proc, NULL);
|
|
||||||
widget->setFocusStop(true);
|
|
||||||
widget->initTheme();
|
|
||||||
|
|
||||||
return widget;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JWidget jlistitem_new(const char *text)
|
ListBox::Item::Item(const char* text)
|
||||||
|
: Widget(JI_LISTITEM)
|
||||||
{
|
{
|
||||||
JWidget widget = new Widget(JI_LISTITEM);
|
setAlign(JI_LEFT | JI_MIDDLE);
|
||||||
|
setText(text);
|
||||||
jwidget_add_hook(widget, JI_LISTITEM, listitem_msg_proc, NULL);
|
initTheme();
|
||||||
widget->setAlign(JI_LEFT | JI_MIDDLE);
|
|
||||||
widget->setText(text);
|
|
||||||
widget->initTheme();
|
|
||||||
|
|
||||||
return widget;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
JWidget jlistbox_get_selected_child(JWidget widget)
|
ListBox::Item* ListBox::getSelectedChild()
|
||||||
{
|
{
|
||||||
JLink link;
|
JLink link;
|
||||||
JI_LIST_FOR_EACH(widget->children, link) {
|
JI_LIST_FOR_EACH(this->children, link) {
|
||||||
if (((JWidget)link->data)->isSelected())
|
if (((Item*)link->data)->isSelected())
|
||||||
return (JWidget)link->data;
|
return (Item*)link->data;
|
||||||
}
|
}
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int jlistbox_get_selected_index(JWidget widget)
|
int ListBox::getSelectedIndex()
|
||||||
{
|
{
|
||||||
JLink link;
|
JLink link;
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
JI_LIST_FOR_EACH(widget->children, link) {
|
JI_LIST_FOR_EACH(this->children, link) {
|
||||||
if (((JWidget)link->data)->isSelected())
|
if (((Item*)link->data)->isSelected())
|
||||||
return i;
|
return i;
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
@ -76,104 +58,100 @@ int jlistbox_get_selected_index(JWidget widget)
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void jlistbox_select_child(JWidget widget, JWidget listitem)
|
void ListBox::selectChild(Item* item)
|
||||||
{
|
{
|
||||||
JWidget child;
|
Item* child;
|
||||||
JLink link;
|
JLink link;
|
||||||
|
|
||||||
JI_LIST_FOR_EACH(widget->children, link) {
|
JI_LIST_FOR_EACH(this->children, link) {
|
||||||
child = (JWidget)link->data;
|
child = (Item*)link->data;
|
||||||
|
|
||||||
if (child->isSelected()) {
|
if (child->isSelected()) {
|
||||||
if ((listitem) && (child == listitem))
|
if (item && child == item)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
child->setSelected(false);
|
child->setSelected(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (listitem) {
|
if (item) {
|
||||||
View* view = View::getView(widget);
|
View* view = View::getView(this);
|
||||||
|
|
||||||
listitem->setSelected(true);
|
item->setSelected(true);
|
||||||
|
|
||||||
if (view) {
|
if (view) {
|
||||||
gfx::Rect vp = view->getViewportBounds();
|
gfx::Rect vp = view->getViewportBounds();
|
||||||
gfx::Point scroll = view->getViewScroll();
|
gfx::Point scroll = view->getViewScroll();
|
||||||
|
|
||||||
if (listitem->rc->y1 < vp.y)
|
if (item->rc->y1 < vp.y)
|
||||||
scroll.y = listitem->rc->y1 - widget->rc->y1;
|
scroll.y = item->rc->y1 - this->rc->y1;
|
||||||
else if (listitem->rc->y1 > vp.y + vp.h - jrect_h(listitem->rc))
|
else if (item->rc->y1 > vp.y + vp.h - jrect_h(item->rc))
|
||||||
scroll.y = (listitem->rc->y1 - widget->rc->y1
|
scroll.y = (item->rc->y1 - this->rc->y1
|
||||||
- vp.h + jrect_h(listitem->rc));
|
- vp.h + jrect_h(item->rc));
|
||||||
|
|
||||||
view->setViewScroll(scroll);
|
view->setViewScroll(scroll);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
jwidget_emit_signal(widget, JI_SIGNAL_LISTBOX_CHANGE);
|
jwidget_emit_signal(this, JI_SIGNAL_LISTBOX_CHANGE);
|
||||||
}
|
}
|
||||||
|
|
||||||
void jlistbox_select_index(JWidget widget, int index)
|
void ListBox::selectIndex(int index)
|
||||||
{
|
{
|
||||||
JWidget child = reinterpret_cast<JWidget>(jlist_nth_data(widget->children, index));
|
Item* child = reinterpret_cast<Item*>(jlist_nth_data(this->children, index));
|
||||||
if (child)
|
if (child)
|
||||||
jlistbox_select_child(widget, child);
|
selectChild(child);
|
||||||
}
|
}
|
||||||
|
|
||||||
int jlistbox_get_items_count(JWidget widget)
|
int ListBox::getItemsCount()
|
||||||
{
|
{
|
||||||
return jlist_length(widget->children);
|
return jlist_length(this->children);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setup the scroll to center the selected item in the viewport */
|
/* setup the scroll to center the selected item in the viewport */
|
||||||
void jlistbox_center_scroll(JWidget widget)
|
void ListBox::centerScroll()
|
||||||
{
|
{
|
||||||
View* view = View::getView(widget);
|
View* view = View::getView(this);
|
||||||
Widget* listitem = jlistbox_get_selected_child(widget);
|
Item* item = getSelectedChild();
|
||||||
|
|
||||||
if (view && listitem) {
|
if (view && item) {
|
||||||
gfx::Rect vp = view->getViewportBounds();
|
gfx::Rect vp = view->getViewportBounds();
|
||||||
gfx::Point scroll = view->getViewScroll();
|
gfx::Point scroll = view->getViewScroll();
|
||||||
|
|
||||||
scroll.y = ((listitem->rc->y1 - widget->rc->y1)
|
scroll.y = ((item->rc->y1 - this->rc->y1)
|
||||||
- vp.h/2 + jrect_h(listitem->rc)/2);
|
- vp.h/2 + jrect_h(item->rc)/2);
|
||||||
|
|
||||||
view->setViewScroll(scroll);
|
view->setViewScroll(scroll);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool listbox_msg_proc(JWidget widget, Message* msg)
|
bool ListBox::onProcessMessage(Message* msg)
|
||||||
{
|
{
|
||||||
switch (msg->type) {
|
switch (msg->type) {
|
||||||
|
|
||||||
case JM_REQSIZE:
|
|
||||||
listbox_request_size(widget, &msg->reqsize.w, &msg->reqsize.h);
|
|
||||||
return true;
|
|
||||||
|
|
||||||
case JM_SETPOS:
|
case JM_SETPOS:
|
||||||
listbox_set_position(widget, &msg->setpos.rect);
|
layoutListBox(&msg->setpos.rect);
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case JM_DRAW:
|
case JM_DRAW:
|
||||||
widget->getTheme()->draw_listbox(widget, &msg->draw.rect);
|
this->getTheme()->draw_listbox(this, &msg->draw.rect);
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case JM_DIRTYCHILDREN:
|
case JM_DIRTYCHILDREN:
|
||||||
listbox_dirty_children(widget);
|
dirtyChildren();
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
case JM_OPEN:
|
case JM_OPEN:
|
||||||
jlistbox_center_scroll(widget);
|
centerScroll();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JM_BUTTONPRESSED:
|
case JM_BUTTONPRESSED:
|
||||||
widget->captureMouse();
|
captureMouse();
|
||||||
|
|
||||||
case JM_MOTION:
|
case JM_MOTION:
|
||||||
if (widget->hasCapture()) {
|
if (hasCapture()) {
|
||||||
int select = jlistbox_get_selected_index(widget);
|
int select = getSelectedIndex();
|
||||||
View* view = View::getView(widget);
|
View* view = View::getView(this);
|
||||||
bool pick_item = true;
|
bool pick_item = true;
|
||||||
|
|
||||||
if (view) {
|
if (view) {
|
||||||
@ -181,12 +159,12 @@ static bool listbox_msg_proc(JWidget widget, Message* msg)
|
|||||||
|
|
||||||
if (msg->mouse.y < vp.y) {
|
if (msg->mouse.y < vp.y) {
|
||||||
int num = MAX(1, (vp.y - msg->mouse.y) / 8);
|
int num = MAX(1, (vp.y - msg->mouse.y) / 8);
|
||||||
jlistbox_select_index(widget, select-num);
|
selectIndex(select-num);
|
||||||
pick_item = false;
|
pick_item = false;
|
||||||
}
|
}
|
||||||
else if (msg->mouse.y >= vp.y + vp.h) {
|
else if (msg->mouse.y >= vp.y + vp.h) {
|
||||||
int num = MAX(1, (msg->mouse.y - (vp.y+vp.h-1)) / 8);
|
int num = MAX(1, (msg->mouse.y - (vp.y+vp.h-1)) / 8);
|
||||||
jlistbox_select_index(widget, select+num);
|
selectIndex(select+num);
|
||||||
pick_item = false;
|
pick_item = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -198,12 +176,14 @@ static bool listbox_msg_proc(JWidget widget, Message* msg)
|
|||||||
picked = view->getViewport()->pick(msg->mouse.x, msg->mouse.y);
|
picked = view->getViewport()->pick(msg->mouse.x, msg->mouse.y);
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
picked = widget->pick(msg->mouse.x, msg->mouse.y);
|
picked = this->pick(msg->mouse.x, msg->mouse.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* 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 && widget->hasChild(picked))
|
if (picked && hasChild(picked)) {
|
||||||
jlistbox_select_child(widget, picked);
|
if (Item* pickedItem = dynamic_cast<Item*>(picked))
|
||||||
|
selectChild(pickedItem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
@ -211,24 +191,24 @@ static bool listbox_msg_proc(JWidget widget, Message* msg)
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case JM_BUTTONRELEASED:
|
case JM_BUTTONRELEASED:
|
||||||
widget->releaseMouse();
|
releaseMouse();
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JM_WHEEL: {
|
case JM_WHEEL: {
|
||||||
View* view = View::getView(widget);
|
View* view = View::getView(this);
|
||||||
if (view) {
|
if (view) {
|
||||||
gfx::Point scroll = view->getViewScroll();
|
gfx::Point scroll = view->getViewScroll();
|
||||||
scroll.y += (jmouse_z(1) - jmouse_z(0)) * jwidget_get_text_height(widget)*3;
|
scroll.y += (jmouse_z(1) - jmouse_z(0)) * jwidget_get_text_height(this)*3;
|
||||||
view->setViewScroll(scroll);
|
view->setViewScroll(scroll);
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case JM_KEYPRESSED:
|
case JM_KEYPRESSED:
|
||||||
if (widget->hasFocus() && !jlist_empty(widget->children)) {
|
if (this->hasFocus() && !jlist_empty(this->children)) {
|
||||||
int select = jlistbox_get_selected_index(widget);
|
int select = getSelectedIndex();
|
||||||
View* view = View::getView(widget);
|
View* view = View::getView(this);
|
||||||
int bottom = MAX(0, jlist_length(widget->children)-1);
|
int bottom = MAX(0, jlist_length(this->children)-1);
|
||||||
|
|
||||||
switch (msg->key.scancode) {
|
switch (msg->key.scancode) {
|
||||||
case KEY_UP:
|
case KEY_UP:
|
||||||
@ -246,7 +226,7 @@ static bool listbox_msg_proc(JWidget widget, Message* msg)
|
|||||||
case KEY_PGUP:
|
case KEY_PGUP:
|
||||||
if (view) {
|
if (view) {
|
||||||
gfx::Rect vp = view->getViewportBounds();
|
gfx::Rect vp = view->getViewportBounds();
|
||||||
select -= vp.h / jwidget_get_text_height(widget);
|
select -= vp.h / jwidget_get_text_height(this);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
select = 0;
|
select = 0;
|
||||||
@ -254,7 +234,7 @@ static bool listbox_msg_proc(JWidget widget, Message* msg)
|
|||||||
case KEY_PGDN:
|
case KEY_PGDN:
|
||||||
if (view) {
|
if (view) {
|
||||||
gfx::Rect vp = view->getViewportBounds();
|
gfx::Rect vp = view->getViewportBounds();
|
||||||
select += vp.h / jwidget_get_text_height(widget);
|
select += vp.h / jwidget_get_text_height(this);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
select = bottom;
|
select = bottom;
|
||||||
@ -275,48 +255,50 @@ static bool listbox_msg_proc(JWidget widget, Message* msg)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
jlistbox_select_index(widget, MID(0, select, bottom));
|
selectIndex(MID(0, select, bottom));
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JM_DOUBLECLICK:
|
case JM_DOUBLECLICK:
|
||||||
jwidget_emit_signal(widget, JI_SIGNAL_LISTBOX_SELECT);
|
jwidget_emit_signal(this, JI_SIGNAL_LISTBOX_SELECT);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return Widget::onProcessMessage(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void listbox_request_size(JWidget widget, int *w, int *h)
|
void ListBox::onPreferredSize(PreferredSizeEvent& ev)
|
||||||
{
|
{
|
||||||
Size reqSize;
|
Size reqSize;
|
||||||
JLink link;
|
JLink link;
|
||||||
|
|
||||||
*w = *h = 0;
|
int w = 0, h = 0;
|
||||||
|
|
||||||
JI_LIST_FOR_EACH(widget->children, link) {
|
JI_LIST_FOR_EACH(this->children, link) {
|
||||||
reqSize = reinterpret_cast<Widget*>(link->data)->getPreferredSize();
|
reqSize = reinterpret_cast<Item*>(link->data)->getPreferredSize();
|
||||||
|
|
||||||
*w = MAX(*w, reqSize.w);
|
w = MAX(w, reqSize.w);
|
||||||
*h += reqSize.h + ((link->next)? widget->child_spacing: 0);
|
h += reqSize.h + ((link->next)? this->child_spacing: 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
*w += widget->border_width.l + widget->border_width.r;
|
w += this->border_width.l + this->border_width.r;
|
||||||
*h += widget->border_width.t + widget->border_width.b;
|
h += this->border_width.t + this->border_width.b;
|
||||||
|
|
||||||
|
ev.setPreferredSize(Size(w, h));
|
||||||
}
|
}
|
||||||
|
|
||||||
static void listbox_set_position(JWidget widget, JRect rect)
|
void ListBox::layoutListBox(JRect rect)
|
||||||
{
|
{
|
||||||
Size reqSize;
|
Size reqSize;
|
||||||
JWidget child;
|
JWidget child;
|
||||||
JRect cpos;
|
JRect cpos;
|
||||||
JLink link;
|
JLink link;
|
||||||
|
|
||||||
jrect_copy(widget->rc, rect);
|
jrect_copy(this->rc, rect);
|
||||||
cpos = jwidget_get_child_rect(widget);
|
cpos = jwidget_get_child_rect(this);
|
||||||
|
|
||||||
JI_LIST_FOR_EACH(widget->children, link) {
|
JI_LIST_FOR_EACH(this->children, link) {
|
||||||
child = (JWidget)link->data;
|
child = (JWidget)link->data;
|
||||||
|
|
||||||
reqSize = child->getPreferredSize();
|
reqSize = child->getPreferredSize();
|
||||||
@ -324,27 +306,27 @@ static void listbox_set_position(JWidget widget, JRect rect)
|
|||||||
cpos->y2 = cpos->y1+reqSize.h;
|
cpos->y2 = cpos->y1+reqSize.h;
|
||||||
jwidget_set_rect(child, cpos);
|
jwidget_set_rect(child, cpos);
|
||||||
|
|
||||||
cpos->y1 += jrect_h(child->rc) + widget->child_spacing;
|
cpos->y1 += jrect_h(child->rc) + this->child_spacing;
|
||||||
}
|
}
|
||||||
|
|
||||||
jrect_free(cpos);
|
jrect_free(cpos);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void listbox_dirty_children(JWidget widget)
|
void ListBox::dirtyChildren()
|
||||||
{
|
{
|
||||||
View* view = View::getView(widget);
|
View* view = View::getView(this);
|
||||||
JWidget child;
|
Item* child;
|
||||||
JLink link;
|
JLink link;
|
||||||
|
|
||||||
if (!view) {
|
if (!view) {
|
||||||
JI_LIST_FOR_EACH(widget->children, link)
|
JI_LIST_FOR_EACH(this->children, link)
|
||||||
reinterpret_cast<JWidget>(link->data)->invalidate();
|
reinterpret_cast<Item*>(link->data)->invalidate();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
gfx::Rect vp = view->getViewportBounds();
|
gfx::Rect vp = view->getViewportBounds();
|
||||||
|
|
||||||
JI_LIST_FOR_EACH(widget->children, link) {
|
JI_LIST_FOR_EACH(this->children, link) {
|
||||||
child = reinterpret_cast<JWidget>(link->data);
|
child = reinterpret_cast<Item*>(link->data);
|
||||||
|
|
||||||
if (child->rc->y2 <= vp.y)
|
if (child->rc->y2 <= vp.y)
|
||||||
continue;
|
continue;
|
||||||
@ -356,56 +338,55 @@ static void listbox_dirty_children(JWidget widget)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool listitem_msg_proc(JWidget widget, Message* msg)
|
bool ListBox::Item::onProcessMessage(Message* msg)
|
||||||
{
|
{
|
||||||
switch (msg->type) {
|
switch (msg->type) {
|
||||||
|
|
||||||
case JM_REQSIZE:
|
|
||||||
listitem_request_size(widget, &msg->reqsize.w, &msg->reqsize.h);
|
|
||||||
return true;
|
|
||||||
|
|
||||||
case JM_SETPOS: {
|
case JM_SETPOS: {
|
||||||
JRect crect;
|
JRect crect;
|
||||||
JLink link;
|
JLink link;
|
||||||
|
|
||||||
jrect_copy(widget->rc, &msg->setpos.rect);
|
jrect_copy(this->rc, &msg->setpos.rect);
|
||||||
crect = jwidget_get_child_rect(widget);
|
crect = jwidget_get_child_rect(this);
|
||||||
|
|
||||||
JI_LIST_FOR_EACH(widget->children, link)
|
JI_LIST_FOR_EACH(this->children, link)
|
||||||
jwidget_set_rect(reinterpret_cast<JWidget>(link->data), crect);
|
jwidget_set_rect(reinterpret_cast<Widget*>(link->data), crect);
|
||||||
|
|
||||||
jrect_free(crect);
|
jrect_free(crect);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
case JM_DRAW:
|
case JM_DRAW:
|
||||||
widget->getTheme()->draw_listitem(widget, &msg->draw.rect);
|
this->getTheme()->draw_listitem(this, &msg->draw.rect);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return Widget::onProcessMessage(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void listitem_request_size(JWidget widget, int *w, int *h)
|
void ListBox::Item::onPreferredSize(PreferredSizeEvent& ev)
|
||||||
{
|
{
|
||||||
|
int w = 0, h = 0;
|
||||||
Size maxSize;
|
Size maxSize;
|
||||||
Size reqSize;
|
Size reqSize;
|
||||||
JLink link;
|
JLink link;
|
||||||
|
|
||||||
if (widget->hasText()) {
|
if (hasText()) {
|
||||||
maxSize.w = jwidget_get_text_length(widget);
|
maxSize.w = jwidget_get_text_length(this);
|
||||||
maxSize.h = jwidget_get_text_height(widget);
|
maxSize.h = jwidget_get_text_height(this);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
maxSize.w = maxSize.h = 0;
|
maxSize.w = maxSize.h = 0;
|
||||||
|
|
||||||
JI_LIST_FOR_EACH(widget->children, link) {
|
JI_LIST_FOR_EACH(this->children, link) {
|
||||||
reqSize = reinterpret_cast<Widget*>(link->data)->getPreferredSize();
|
reqSize = reinterpret_cast<Widget*>(link->data)->getPreferredSize();
|
||||||
|
|
||||||
maxSize.w = MAX(maxSize.w, reqSize.w);
|
maxSize.w = MAX(maxSize.w, reqSize.w);
|
||||||
maxSize.h = MAX(maxSize.h, reqSize.h);
|
maxSize.h = MAX(maxSize.h, reqSize.h);
|
||||||
}
|
}
|
||||||
|
|
||||||
*w = widget->border_width.l + maxSize.w + widget->border_width.r;
|
w = this->border_width.l + maxSize.w + this->border_width.r;
|
||||||
*h = widget->border_width.t + maxSize.h + widget->border_width.b;
|
h = this->border_width.t + maxSize.h + this->border_width.b;
|
||||||
|
|
||||||
|
ev.setPreferredSize(Size(w, h));
|
||||||
}
|
}
|
||||||
|
@ -7,18 +7,41 @@
|
|||||||
#ifndef GUI_LISTBOX_H_INCLUDED
|
#ifndef GUI_LISTBOX_H_INCLUDED
|
||||||
#define GUI_LISTBOX_H_INCLUDED
|
#define GUI_LISTBOX_H_INCLUDED
|
||||||
|
|
||||||
#include "gui/base.h"
|
#include "base/compiler_specific.h"
|
||||||
|
#include "gui/widget.h"
|
||||||
|
|
||||||
JWidget jlistbox_new();
|
class ListBox : public Widget
|
||||||
JWidget jlistitem_new(const char *text);
|
{
|
||||||
|
public:
|
||||||
|
class Item : public Widget
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Item(const char* text);
|
||||||
|
|
||||||
JWidget jlistbox_get_selected_child(JWidget listbox);
|
protected:
|
||||||
int jlistbox_get_selected_index(JWidget listbox);
|
bool onProcessMessage(Message* msg) OVERRIDE;
|
||||||
void jlistbox_select_child(JWidget listbox, JWidget listitem);
|
void onPreferredSize(PreferredSizeEvent& ev) OVERRIDE;
|
||||||
void jlistbox_select_index(JWidget listbox, int index);
|
};
|
||||||
|
|
||||||
int jlistbox_get_items_count(JWidget listbox);
|
ListBox();
|
||||||
|
|
||||||
void jlistbox_center_scroll(JWidget listbox);
|
Item* getSelectedChild();
|
||||||
|
int getSelectedIndex();
|
||||||
|
|
||||||
|
void selectChild(Item* item);
|
||||||
|
void selectIndex(int index);
|
||||||
|
|
||||||
|
int getItemsCount();
|
||||||
|
|
||||||
|
void centerScroll();
|
||||||
|
|
||||||
|
protected:
|
||||||
|
bool onProcessMessage(Message* msg) OVERRIDE;
|
||||||
|
void onPreferredSize(PreferredSizeEvent& ev) OVERRIDE;
|
||||||
|
|
||||||
|
private:
|
||||||
|
void layoutListBox(JRect rect);
|
||||||
|
void dirtyChildren();
|
||||||
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
@ -231,13 +231,13 @@ static Widget* convert_xmlelement_to_widget(TiXmlElement* elem, Widget* root)
|
|||||||
}
|
}
|
||||||
/* listbox */
|
/* listbox */
|
||||||
else if (ustrcmp(elem_name, "listbox") == 0) {
|
else if (ustrcmp(elem_name, "listbox") == 0) {
|
||||||
widget = jlistbox_new();
|
widget = new ListBox();
|
||||||
}
|
}
|
||||||
/* listitem */
|
/* listitem */
|
||||||
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 = jlistitem_new(text ? TRANSLATE_ATTR(text): NULL);
|
widget = new ListBox::Item(text ? TRANSLATE_ATTR(text): NULL);
|
||||||
}
|
}
|
||||||
/* splitter */
|
/* splitter */
|
||||||
else if (ustrcmp(elem_name, "splitter") == 0) {
|
else if (ustrcmp(elem_name, "splitter") == 0) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user