diff --git a/source/gui/widgets/listbox.cpp b/source/gui/widgets/listbox.cpp index a0c2db52..398ca3b9 100644 --- a/source/gui/widgets/listbox.cpp +++ b/source/gui/widgets/listbox.cpp @@ -1273,6 +1273,7 @@ namespace nana } //Backward + n = -n; dpos = pos; if (good(dpos.cat)) { @@ -1618,7 +1619,7 @@ namespace nana } /// return absolute positions, no relative to display - index_pairs pick_items(bool for_selection) const + index_pairs pick_items(bool for_selection, bool find_first = false) const { index_pairs results; index_pair id; @@ -1629,7 +1630,11 @@ namespace nana for (auto & m : cat.items) { if (for_selection ? m.flags.selected : m.flags.checked) + { results.push_back(id); // absolute positions, no relative to display + if (find_first) + return results; + } ++id.item; } ++id.cat; @@ -4336,8 +4341,9 @@ namespace nana void trigger::key_press(graph_reference graph, const arg_keyboard& arg) { + auto & list = essence_->lister; // Exit if list is empty - if (essence_->lister.first().empty()) + if (list.first().empty()) return; bool upward = false; @@ -4347,12 +4353,12 @@ namespace nana case keyboard::os_arrow_up: upward = true; case keyboard::os_arrow_down: - essence_->lister.move_select(upward, !arg.shift, true); + list.move_select(upward, !arg.shift, true); break; case L' ': { index_pairs s; - bool ck = ! essence_->lister.item_selected_all_checked(s); + bool ck = ! list.item_selected_all_checked(s); for(auto i : s) item_proxy(essence_, i).check(ck); } @@ -4361,30 +4367,69 @@ namespace nana upward = true; case keyboard::os_pagedown: { - //Turns page, then returns if no change occurs - if (!essence_->content_view->turn_page(!upward, false)) - return; + auto const item_px = essence_->item_height(); + auto picked_items = list.pick_items(true, true); + index_pair init_idx = (picked_items.empty() ? list.first() : picked_items[0]); essence_->lister.select_for_all(false); - auto idx = essence_->first_display(); + //Get the pixels between the init item and top edge or bottom edge + auto logic_top = static_cast(list.distance(list.first(), init_idx) * item_px); - if (!upward) - idx = essence_->lister.advance(idx, static_cast(essence_->count_of_exposed(false)) - 1); + auto const screen_top = essence_->content_view->origin().y; + auto const screen_bottom = screen_top + essence_->content_view->view_area().height; + index_pair target_idx; - if (!idx.is_category()) - item_proxy::from_display(essence_, idx).select(true); - else if (!essence_->lister.single_status(true)) //not selected - essence_->lister.cat_status(idx.cat, true, true); + //Check if it scrolls in current screen window + //condition: top of target item is not less than top edge of content view and + //the bottom of target item is not greater than bottom edge of content view. + if ((screen_top + item_px <= logic_top) && (logic_top + item_px + item_px <= screen_bottom)) + { + int offset = (static_cast(upward ? screen_top : screen_bottom - item_px) - logic_top) / static_cast(item_px); + target_idx = list.advance(init_idx, offset); + } + else + { + //turn page + auto page_item_count = (std::max)(1, static_cast(essence_->count_of_exposed(false))); - break; + auto origin = essence_->content_view->origin(); + if (upward) + { + target_idx = list.advance(init_idx, -page_item_count); + if (target_idx.empty()) + target_idx = list.first(); + + origin.y = list.distance(list.first(), target_idx) * item_px; + } + else + { + target_idx = list.advance(init_idx, page_item_count); + if (target_idx.empty()) + target_idx = list.last(); + + origin.y = list.distance(list.first(), target_idx) * item_px + item_px; + if (origin.y >= (screen_bottom - screen_top)) + origin.y -= (screen_bottom - screen_top); + else + origin.y = 0; + } + + essence_->content_view->move_origin(origin - essence_->content_view->origin()); + } + + if (!target_idx.is_category()) + item_proxy::from_display(essence_, target_idx).select(true); + else if (!list.single_status(true)) //not selected + list.cat_status(target_idx.cat, true, true); } + break; case keyboard::os_home: case keyboard::os_end: { - essence_->lister.select_for_all(false); + list.select_for_all(false); - auto pos = (keyboard::os_home == arg.key ? essence_->lister.first() : essence_->lister.last()); + auto pos = (keyboard::os_home == arg.key ? list.first() : list.last()); if (!pos.empty()) { //When the pos indicates an empty category, then search forwards/backwards(depending on arg.key whether it is Home or End) for a non empty category. @@ -4393,9 +4438,9 @@ namespace nana { if (keyboard::os_home == arg.key) { - while (0 == essence_->lister.size_item(pos.cat)) + while (0 == list.size_item(pos.cat)) { - if (++pos.cat >= essence_->lister.cat_container().size()) + if (++pos.cat >= list.cat_container().size()) { pos = index_pair{ npos, npos }; break; @@ -4404,7 +4449,7 @@ namespace nana } else { - while (0 == essence_->lister.size_item(pos.cat)) + while (0 == list.size_item(pos.cat)) { if (pos.cat-- == 0) { @@ -4416,7 +4461,7 @@ namespace nana if (!pos.empty()) { - if (essence_->lister.expand(pos.cat)) + if (list.expand(pos.cat)) pos.item = 0; } } @@ -4425,8 +4470,8 @@ namespace nana { if (pos.is_category()) { - if (!essence_->lister.single_status(true)) //multiple selection is not enabled - essence_->lister.cat_status(pos.cat, true, true); + if (!list.single_status(true)) //multiple selection is not enabled + list.cat_status(pos.cat, true, true); } else item_proxy::from_display(essence_, pos).select(true); diff --git a/source/gui/widgets/scroll.cpp b/source/gui/widgets/scroll.cpp index b59e4721..acc2c8b6 100644 --- a/source/gui/widgets/scroll.cpp +++ b/source/gui/widgets/scroll.cpp @@ -1,7 +1,7 @@ /* * A Scroll Implementation * Nana C++ Library(http://www.nanapro.org) - * Copyright(C) 2003-2014 Jinhao(cnjinhao@hotmail.com) + * Copyright(C) 2003-2018 Jinhao(cnjinhao@hotmail.com) * * Distributed under the Boost Software License, Version 1.0. * (See accompanying file LICENSE_1_0.txt or copy at @@ -20,9 +20,15 @@ namespace nana namespace scroll { //struct metrics_type - metrics_type::metrics_type() - :peak(1), range(1), step(1), value(0), - what(buttons::none), pressed(false), scroll_length(0), scroll_pos(0) + metrics_type::metrics_type(): + peak(1), + range(1), + step(1), + value(0), + what(buttons::none), + pressed(false), + scroll_length(0), + scroll_pos(0) {} //end struct metrics_type