#include <msp/debug/demangle.h>
#include <msp/gl/matrix.h>
#include <msp/gl/meshbuilder.h>
+#include <msp/input/keys.h>
#include "graphic.h"
#include "list.h"
#include "part.h"
max_scroll = 0;
view_size = 5;
ignore_slider_change = false;
+ dragging = false;
+ drag_start_x = 0;
+ drag_start_y = 0;
observer = new DataObserver(*this);
sel_index = i;
focus_index = i;
items[sel_index]->set_active(true);
- set_input_focus(items[focus_index]);
+ if(state&FOCUS)
+ set_input_focus(items[focus_index]);
signal_item_selected.emit(sel_index);
}
}
+void List::set_selected_item(Widget *item)
+{
+ for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
+ if(item==items[i])
+ return set_selected_index(i);
+}
+
void List::rebuild_special(const Part &part)
{
if(part.get_name()=="slider")
slider.render(renderer);
}
+bool List::key_press(unsigned key, unsigned mod)
+{
+ if(key==Input::KEY_UP && mod==MOD_CTRL)
+ move_focus(NAV_UP, false);
+ else if(key==Input::KEY_DOWN && mod==MOD_CTRL)
+ move_focus(NAV_DOWN, false);
+ else
+ return false;
+
+ return true;
+}
+
void List::button_press(int x, int y, unsigned btn)
{
if(btn==4 || btn==5)
{
Container::button_press(x, y, btn);
if(click_focus && btn==1)
- {
- for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
- if(click_focus==items[i])
- {
- set_selected_index(i);
- break;
- }
- }
+ set_selected_item(click_focus);
}
}
-void List::focus_in()
+void List::touch_press(int x, int y, unsigned finger)
{
- if(focus_index<0)
- focus_index = sel_index;
-
- if(focus_index>=0)
+ if(finger==0)
{
- scroll_to_focus();
- set_input_focus(items[focus_index]);
+ dragging = true;
+ drag_start_x = x;
+ drag_start_y = y;
}
}
-bool List::navigate(Navigation nav)
+void List::touch_release(int x, int y, unsigned finger)
{
- if(nav==NAV_UP)
+ if(finger==0)
{
- 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]);
+ int dx = x-drag_start_x;
+ int dy = y-drag_start_y;
+ if(dx*dx+dy*dy<25)
+ {
+ Container::touch_press(drag_start_x, drag_start_y, finger);
+ if(touch_focus)
+ set_selected_item(touch_focus);
+ Container::touch_motion(x, y, finger);
+ Container::touch_release(x, y, finger);
+ }
+ dragging = false;
}
- else if(nav==NAV_DOWN)
+}
+
+void List::touch_motion(int, int y, unsigned finger)
+{
+ if(finger==0 && !items.empty() && dragging)
{
- if(focus_index<0 && !items.empty())
- focus_index = 0;
- else if(static_cast<unsigned>(focus_index+1)<items.size())
- ++focus_index;
- else
- return false;
+ int dy = y-drag_start_y;
+ if(dy>0 && first<max_scroll)
+ {
+ int item_h = items[first]->get_geometry().h;
+ if(dy>item_h)
+ {
+ drag_start_y += item_h;
+ slider.set_value(max_scroll-(first+1));
+ }
+ }
+ else if(dy<0 && first>0)
+ {
+ int item_h = items[first-1]->get_geometry().h;
+ if(-dy>item_h)
+ {
+ drag_start_y -= item_h;
+ slider.set_value(max_scroll-(first-1));
+ }
+ }
+ }
+}
- scroll_to_focus();
+void List::focus_in()
+{
+ Container::focus_in();
+ if(focus_index>=0 && items[focus_index]->is_visible())
set_input_focus(items[focus_index]);
+ else
+ {
+ if(sel_index>=0 && items[sel_index]->is_visible())
+ set_focus_index(sel_index);
+ else if(!items.empty())
+ set_focus_index(first);
}
+}
+
+bool List::navigate(Navigation nav)
+{
+ if((nav==NAV_UP || nav==NAV_DOWN) && !items.empty())
+ move_focus(nav, true);
else if(nav==NAV_ACTIVATE)
set_selected_index(focus_index);
else
return true;
}
+void List::move_focus(Navigation nav, bool select)
+{
+ if(nav==NAV_UP)
+ {
+ if(focus_index>0)
+ set_focus_index(focus_index-1);
+ }
+ else if(nav==NAV_DOWN)
+ {
+ if(static_cast<unsigned>(focus_index+1)<items.size())
+ set_focus_index(focus_index+1);
+ }
+
+ if(select)
+ set_selected_index(focus_index);
+}
+
+void List::set_focus_index(int i)
+{
+ focus_index = i;
+ if(focus_index>=0)
+ {
+ scroll_to_focus();
+ if(state&FOCUS)
+ set_input_focus(items[focus_index]);
+ }
+}
+
void List::item_autosize_changed(Item *item)
{
item->autosize();