X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fdropdown.cpp;h=ef0fa22f97c8f150009988b3487c716b1e576cf2;hb=HEAD;hp=e48303317b809601514ce71db7dded49dffbc068;hpb=96df5b14a964da5a592a36e10a23022d5649462a;p=libs%2Fgltk.git diff --git a/source/dropdown.cpp b/source/dropdown.cpp index e483033..ab00c4c 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -25,12 +25,13 @@ Dropdown::Dropdown(ListData &d): void Dropdown::init() { - dropped = false; + input_type = INPUT_NAVIGATION; add(list); list.set_visible(false); list.set_view_all(); list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected)); + list.signal_selection_cleared.connect(sigc::mem_fun(this, &Dropdown::list_selection_cleared)); list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed)); } @@ -50,7 +51,7 @@ void Dropdown::autosize_special(const Part &part, Geometry &ageom) const unsigned max_w = 0; const ListData &data = list.get_data(); - for(unsigned i=0; i(font.get_string_width(data.get_string(i))*font_size); max_w = max(max_w, w); @@ -62,13 +63,6 @@ void Dropdown::autosize_special(const Part &part, Geometry &ageom) const } } -void Dropdown::set_selected_index(int index) -{ - list.set_selected_index(index); - if(index<0) - text.set(string()); -} - void Dropdown::rebuild_special(const Part &part) { if(part.get_name()=="text") @@ -89,24 +83,30 @@ void Dropdown::button_press(int x, int y, unsigned btn) { 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() +void Dropdown::on_size_change() { if(dropped) resize_list(); @@ -119,6 +119,24 @@ void Dropdown::on_style_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) @@ -159,7 +177,7 @@ void Dropdown::resize_list() list.set_geometry(lgeom); } -void Dropdown::list_item_selected(unsigned index) +void Dropdown::list_item_selected(size_t index) { if(dropped) { @@ -171,7 +189,12 @@ void Dropdown::list_item_selected(unsigned index) text.set(list.get_data().get_string(index)); signal_item_selected.emit(index); - rebuild(); + mark_rebuild(); +} + +void Dropdown::list_selection_cleared() +{ + text.set(string()); }