void menu::key_press(SDLKey key) { if (!click_selects_) { switch(key) { case SDLK_UP: move_selection_up(1); break; case SDLK_DOWN: move_selection_down(1); break; case SDLK_PAGEUP: move_selection_up(max_items_onscreen()); break; case SDLK_PAGEDOWN: move_selection_down(max_items_onscreen()); break; case SDLK_HOME: set_selection_pos(0); break; case SDLK_END: set_selection_pos(items_.size() - 1); break; //case SDLK_RETURN: // double_clicked_ = true; // break; default: break; } } if (num_selects_ && key >= SDLK_1 && key <= SDLK_9) set_selection_pos(key - SDLK_1); }
void menu::update_size() { unsigned int h = heading_height(); for(size_t i = get_position(), i_end = std::min(items_.size(), i + max_items_onscreen()); i < i_end; ++i) h += get_item_rect(i).h; h = std::max(h, height()); if (max_height_ > 0 && h > static_cast<unsigned>(max_height_)) { h = max_height_; } use_ellipsis_ = false; std::vector<int> const &widths = column_widths(); unsigned w = std::accumulate(widths.begin(), widths.end(), 0); if (items_.size() > max_items_onscreen()) w += scrollbar_width(); w = std::max(w, width()); if (max_width_ > 0 && w > static_cast<unsigned>(max_width_)) { use_ellipsis_ = true; w = max_width_; } update_scrollbar_grip_height(); set_measurements(w, h); }
SDL_Rect menu::get_item_rect_internal(size_t item) const { //unsigned int first_item_on_screen = get_position(); unsigned int first_item_on_screen = get_position() / item_height_; unsigned int leftovers = get_position() - (first_item_on_screen * item_height_); if (item < first_item_on_screen || size_t(item) >= first_item_on_screen + max_items_onscreen() + 2) { return empty_rect; } const std::map<int,SDL_Rect>::const_iterator i = itemRects_.find(item); if(i != itemRects_.end()) return i->second; SDL_Rect const &loc = inner_location(); int y = loc.y + heading_height(); if (item != first_item_on_screen) { const SDL_Rect& prev = get_item_rect_internal(item-1); y = prev.y + prev.h; } else { // KP: sub-item scrolling y -= leftovers; } SDL_Rect res = { loc.x, y, loc.w, get_item_height(item) }; SDL_Rect const &screen_area = ::screen_area(); if(res.x > screen_area.w) { return empty_rect; } else if(res.x + res.w > screen_area.w) { res.w = screen_area.w - res.x; } if(res.y > screen_area.h) { return empty_rect; } else if(res.y + res.h > screen_area.h) { res.h = screen_area.h - res.y; } //only insert into the cache if the menu's co-ordinates have //been initialized if (loc.x > 0 && loc.y > 0) itemRects_.insert(std::pair<int,SDL_Rect>(item,res)); return res; }
void menu::update_scrollbar_grip_height() { set_full_size(items_.size()); set_shown_size(max_items_onscreen()); }