icon(0),
pressed(false)
{
+ input_type = INPUT_NAVIGATION;
set_text(t);
}
}
}
+bool Button::navigate(Navigation nav)
+{
+ if(nav==NAV_ACTIVATE)
+ signal_clicked.emit();
+ else
+ return false;
+
+ return true;
+}
+
void Button::on_style_change()
{
text.set_style(style);
virtual void button_press(int, int, unsigned);
virtual void button_release(int, int, unsigned);
virtual void pointer_motion(int, int);
+ virtual bool navigate(Navigation);
private:
virtual void on_style_change();
};
Widget::focus_out();
}
+bool Container::navigate(Navigation nav)
+{
+ if(input_focus && input_focus->navigate(nav))
+ return true;
+
+ if(nav==NAV_UP || nav==NAV_DOWN || nav==NAV_LEFT || nav==NAV_RIGHT)
+ {
+ int x = geom.w/2;
+ int y = geom.h/2;
+ if(input_focus)
+ {
+ const Geometry &fgeom = input_focus->get_geometry();
+ x = fgeom.x+fgeom.w/2;
+ y = fgeom.y+fgeom.h/2;
+ }
+ else if(nav==NAV_UP)
+ y = 0;
+ else if(nav==NAV_DOWN)
+ y = geom.h;
+ else if(nav==NAV_RIGHT)
+ x = 0;
+ else if(nav==NAV_LEFT)
+ x = geom.w;
+
+ Widget *sibling = 0;
+ int best_score = 0;
+ for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
+ {
+ if((*i)->widget==input_focus || !(*i)->widget->is_focusable())
+ continue;
+
+ const Geometry &cgeom = (*i)->widget->get_geometry();
+ int dx = cgeom.x+cgeom.w/2-x;
+ int dy = cgeom.y+cgeom.h/2-y;
+
+ int score = -1;
+ if(nav==NAV_UP && dy>0)
+ score = dy+abs(dx)*4;
+ else if(nav==NAV_DOWN && dy<0)
+ score = -dy+abs(dx)*4;
+ else if(nav==NAV_RIGHT && dx>0)
+ score = dx+abs(dy)*4;
+ else if(nav==NAV_LEFT && dx<0)
+ score = -dx+abs(dy)*4;
+
+ if(score>0 && (!sibling || score<best_score))
+ {
+ sibling = (*i)->widget;
+ best_score = score;
+ }
+ }
+
+ if(sibling)
+ {
+ set_input_focus(sibling);
+ if(Container *container = dynamic_cast<Container *>(sibling))
+ container->navigate(nav);
+ return true;
+ }
+ }
+
+ return false;
+}
+
void Container::on_reparent()
{
for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
virtual bool key_release(unsigned, unsigned);
virtual bool character(wchar_t);
virtual void focus_out();
+ virtual bool navigate(Navigation);
protected:
virtual void on_reparent();
virtual void on_child_added(Widget &) { }
void Dropdown::init()
{
- // Necessary to have the parent container raise the dropdown on click
- input_type = INPUT_TEXT;
+ input_type = INPUT_NAVIGATION;
dropped = false;
{
Container::button_press(x, y, btn);
if(!click_focus)
- {
- dropped = false;
- list.set_visible(false);
- clear_state(ACTIVE);
- signal_ungrab_pointer.emit();
- }
+ close_list();
}
else if(btn==1)
+ open_list();
+}
+
+bool Dropdown::navigate(Navigation nav)
+{
+ if(dropped)
{
- dropped = true;
- list.set_visible(true);
- resize_list();
- set_state(ACTIVE);
- signal_grab_pointer.emit();
+ if(nav==NAV_CANCEL)
+ close_list();
+ else
+ list.navigate(nav);
}
+ else if(nav==NAV_ACTIVATE)
+ open_list();
+ else
+ return false;
+
+ return true;
}
void Dropdown::on_geometry_change()
resize_list();
}
+void Dropdown::open_list()
+{
+ dropped = true;
+ list.set_visible(true);
+ resize_list();
+ set_state(ACTIVE);
+ set_input_focus(&list);
+ signal_grab_pointer.emit();
+}
+
+void Dropdown::close_list()
+{
+ dropped = false;
+ list.set_visible(false);
+ clear_state(ACTIVE);
+ signal_ungrab_pointer.emit();
+}
+
void Dropdown::list_autosize_changed()
{
if(dropped)
public:
virtual void button_press(int, int, unsigned);
+ virtual bool navigate(Navigation);
private:
virtual void on_geometry_change();
virtual void on_style_change();
+ void open_list();
+ void close_list();
void list_autosize_changed();
void resize_list();
void list_item_selected(unsigned);
bool Entry::key_press(unsigned key, unsigned)
{
got_key_press = true;
- if(key==Input::KEY_LEFT)
- {
- if(edit_pos>0)
- set_edit_position(edit_pos-1);
- }
- else if(key==Input::KEY_RIGHT)
- {
- if(edit_pos<text.size())
- set_edit_position(edit_pos+1);
- }
- else if(key==Input::KEY_DOWN && multiline)
- {
- unsigned row, col;
- text.offset_to_coords(edit_pos, row, col);
- set_edit_position(text.coords_to_offset(row+1, col));
- }
- else if(key==Input::KEY_UP && multiline)
- {
- unsigned row, col;
- text.offset_to_coords(edit_pos, row, col);
- set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
- }
- else if(key==Input::KEY_BACKSPACE)
+ if(key==Input::KEY_BACKSPACE)
{
if(edit_pos>0)
{
rebuild();
}
}
- else if(key==Input::KEY_ENTER)
+ else if(key==Input::KEY_ENTER && multiline)
{
- if(multiline)
- {
- text.insert(edit_pos++, "\n");
- check_view_range();
- rebuild();
- }
- else
- signal_enter.emit();
+ text.insert(edit_pos++, "\n");
+ check_view_range();
+ rebuild();
}
else
return false;
got_key_press = false;
}
+bool Entry::navigate(Navigation nav)
+{
+ if(nav==NAV_LEFT)
+ {
+ if(edit_pos>0)
+ set_edit_position(edit_pos-1);
+ }
+ else if(nav==NAV_RIGHT)
+ {
+ if(edit_pos<text.size())
+ set_edit_position(edit_pos+1);
+ }
+ else if(nav==NAV_DOWN && multiline)
+ {
+ unsigned row, col;
+ text.offset_to_coords(edit_pos, row, col);
+ set_edit_position(text.coords_to_offset(row+1, col));
+ }
+ else if(nav==NAV_UP && multiline)
+ {
+ unsigned row, col;
+ text.offset_to_coords(edit_pos, row, col);
+ set_edit_position(row>0 ? text.coords_to_offset(row-1, col) : 0);
+ }
+ else if(nav==NAV_ACCEPT && !signal_enter.empty())
+ signal_enter.emit();
+ else
+ return false;
+
+ return true;
+}
+
void Entry::on_geometry_change()
{
if(multiline)
void multiline(bool);
};
+ // Deprecated
sigc::signal<void> signal_enter;
private:
virtual bool key_press(unsigned, unsigned);
virtual bool character(wchar_t);
virtual void focus_out();
+ virtual bool navigate(Navigation);
private:
virtual void on_geometry_change();
virtual void on_style_change();
enum InputType
{
INPUT_NONE,
+ INPUT_NAVIGATION,
INPUT_TEXT
};
+enum Navigation
+{
+ NAV_LEFT,
+ NAV_RIGHT,
+ NAV_UP,
+ NAV_DOWN,
+ NAV_NEXT,
+ NAV_PREVIOUS,
+ NAV_ACTIVATE,
+ NAV_ACCEPT,
+ NAV_CANCEL
+};
+
class InputMethod
{
protected:
void List::init()
{
+ input_type = INPUT_NAVIGATION;
+
item_factory = 0;
sel_index = -1;
+ focus_index = -1;
first = 0;
max_scroll = 0;
view_size = 5;
if(i<0)
{
sel_index = -1;
+ focus_index = -1;
+ set_input_focus(0);
signal_selection_cleared.emit();
}
else
{
sel_index = i;
+ focus_index = i;
items[sel_index]->set_active(true);
+ set_input_focus(items[focus_index]);
signal_item_selected.emit(sel_index);
}
}
}
}
+void List::focus_in()
+{
+ if(focus_index<0)
+ focus_index = sel_index;
+
+ if(focus_index>=0)
+ {
+ scroll_to_focus();
+ set_input_focus(items[focus_index]);
+ }
+}
+
+bool List::navigate(Navigation nav)
+{
+ if(nav==NAV_UP)
+ {
+ if(focus_index<0 && !items.empty())
+ focus_index = items.size()-1;
+ else if(focus_index>0)
+ --focus_index;
+ else
+ return false;
+
+ scroll_to_focus();
+ set_input_focus(items[focus_index]);
+ }
+ else if(nav==NAV_DOWN)
+ {
+ if(focus_index<0 && !items.empty())
+ focus_index = 0;
+ else if(static_cast<unsigned>(focus_index+1)<items.size())
+ ++focus_index;
+ else
+ return false;
+
+ scroll_to_focus();
+ set_input_focus(items[focus_index]);
+ }
+ else if(nav==NAV_ACTIVATE)
+ set_selected_index(focus_index);
+ else
+ return false;
+
+ return true;
+}
+
void List::item_autosize_changed(Item *item)
{
item->autosize();
slider.set_value(max_scroll-first);
}
+void List::scroll_to_focus()
+{
+ if(focus_index<0 || items[focus_index]->is_visible())
+ return;
+
+ if(static_cast<unsigned>(focus_index)<first)
+ slider.set_value(max_scroll-focus_index);
+ else
+ slider.set_value(max_scroll-last_to_first(focus_index));
+}
+
void List::slider_value_changed(double value)
{
if(max_scroll>0 && !ignore_slider_change)
}
}
+void List::adjust_index(int &index, int pos, int change)
+{
+ if(index>pos)
+ index += change;
+ else if(index==pos)
+ index = (change>0 ? index+change : -1);
+}
+
List::DataObserver::DataObserver(List &l):
list(l)
void List::DataObserver::item_added(unsigned i)
{
- if(list.sel_index>=static_cast<int>(i))
- ++list.sel_index;
+ adjust_index(list.sel_index, i, 1);
+ adjust_index(list.focus_index, i, 1);
Item *item = list.create_item(i);
list.items.insert(list.items.begin()+i, item);
void List::DataObserver::item_removed(unsigned i)
{
bool had_selection = (list.sel_index>=0);
- if(list.sel_index>static_cast<int>(i))
- --list.sel_index;
- else if(list.sel_index==static_cast<int>(i))
- list.sel_index = -1;
+ adjust_index(list.sel_index, i, -1);
+ adjust_index(list.focus_index, i, -1);
delete list.items[i];
list.items.erase(list.items.begin()+i);
void List::DataObserver::cleared()
{
list.sel_index = -1;
+ list.focus_index = -1;
for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
delete *i;
list.items.clear();
DataObserver *observer;
ItemFactory *item_factory;
int sel_index;
+ int focus_index;
unsigned first;
unsigned max_scroll;
unsigned view_size;
public:
virtual void button_press(int, int, unsigned);
+ virtual void focus_in();
+ virtual bool navigate(Navigation);
private:
void item_autosize_changed(Item *);
unsigned last_to_first(unsigned) const;
void check_view_range();
+ void scroll_to_focus();
void slider_value_changed(double);
+ static void adjust_index(int &, int, int);
};
} // namespace GLtk
Panel::Panel():
layout(0)
{
- input_type = INPUT_TEXT;
+ input_type = INPUT_NAVIGATION;
}
Panel::~Panel()
if(root.key_press(key, 0))
return true;
+ switch(key)
+ {
+ case Input::KEY_LEFT: return root.navigate(NAV_LEFT);
+ case Input::KEY_RIGHT: return root.navigate(NAV_RIGHT);
+ case Input::KEY_UP: return root.navigate(NAV_UP);
+ case Input::KEY_DOWN: return root.navigate(NAV_DOWN);
+ case Input::KEY_TAB: return root.navigate(NAV_NEXT);
+ case Input::KEY_SPACE: return root.navigate(NAV_ACTIVATE);
+ case Input::KEY_ENTER: return root.navigate(NAV_ACCEPT);
+ case Input::KEY_ESC: return root.navigate(NAV_CANCEL);
+ }
+
return false;
}
value(false),
exclusive(false)
{
+ input_type = INPUT_NAVIGATION;
set_text(t);
}
}
}
+bool Toggle::navigate(Navigation nav)
+{
+ if(nav==NAV_ACTIVATE)
+ {
+ if(!value || !exclusive)
+ set_value(!value);
+ return true;
+ }
+
+ return false;
+}
+
void Toggle::on_style_change()
{
text.set_style(style);
public:
virtual void button_press(int, int, unsigned);
virtual void button_release(int, int, unsigned);
+ virtual bool navigate(Navigation);
private:
virtual void on_style_change();
};
virtual bool character(wchar_t) { return false; }
virtual void focus_in();
virtual void focus_out();
+ virtual bool navigate(Navigation) { return false; }
protected:
virtual void on_geometry_change() { }
virtual void on_style_change() { }