The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
menu.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef WIDGET_MENU_HPP_INCLUDED
16 #define WIDGET_MENU_HPP_INCLUDED
17 
18 #include <set>
19 
20 #include "scrollarea.hpp"
21 
22 namespace image{
23  class locator;
24 }
25 
26 namespace gui {
27 
28 class menu : public scrollarea
29 {
30 public:
31 
33  //basic menu style
34  class style
35  {
36  public:
37  style();
38  virtual ~style();
39  virtual void init() {}
40 
41  virtual SDL_Rect item_size(const std::string& item) const;
42  virtual void draw_row_bg(menu& menu_ref, const size_t row_index, const SDL_Rect& rect, ROW_TYPE type);
43  virtual void draw_row(menu& menu_ref, const size_t row_index, const SDL_Rect& rect, ROW_TYPE type);
44  void scale_images(int max_width, int max_height);
45 #ifdef SDL_GPU
46  sdl::timage get_item_image(const image::locator &i_locator) const;
47 #else
48  surface get_item_image(const image::locator &i_locator) const;
49 #endif
50  size_t get_font_size() const;
51  size_t get_cell_padding() const;
52  size_t get_thickness() const;
53 
54  protected:
55  size_t font_size_;
56  size_t cell_padding_;
57  size_t thickness_; //additional cell padding for style use only
58 
60 #ifdef SDL_GPU
62 #else
64 #endif
66  };
67 
68  //image-border selection style
69  class imgsel_style : public style
70  {
71  public:
72  imgsel_style(const std::string &img_base, bool has_bg,
73  int normal_rgb, int selected_rgb, int heading_rgb,
74  double normal_alpha, double selected_alpha, double heading_alpha);
75  virtual ~imgsel_style();
76 
77  virtual SDL_Rect item_size(const std::string& item) const;
78  virtual void draw_row_bg(menu& menu_ref, const size_t row_index, const SDL_Rect& rect, ROW_TYPE type);
79  virtual void draw_row(menu& menu_ref, const size_t row_index, const SDL_Rect& rect, ROW_TYPE type);
80 
81  virtual void init() { load_images(); }
82  bool load_images();
83 
84  protected:
86 #ifdef SDL_GPU
87  std::map<std::string, sdl::timage> img_map_;
88 #else
89  std::map<std::string,surface> img_map_;
90 #endif
91 
92  private:
93  bool load_image(const std::string &img_sub);
99 #ifdef SDL_GPU
100  sdl::timage background_image_;
101 #endif
102  //FIXME: why is this better than a plain surface?
103  struct bg_cache
104  {
105  bg_cache() : surf(), width(-1), height(-1)
106  {}
107 
109  int width, height;
110  };
112  };
113 
114  friend class style;
115  friend class imgsel_style;
119 
120  struct item
121  {
122  item() : fields(), help(), id(0)
123  {}
124 
125  item(const std::vector<std::string>& fields, size_t id)
126  : fields(fields), help(), id(id)
127  {}
128 
129  std::vector<std::string> fields;
130  std::vector<std::string> help;
131  size_t id;
132  };
133 
134  class sorter
135  {
136  public:
137  virtual ~sorter() {}
138  virtual bool column_sortable(int column) const = 0;
139  virtual bool less(int column, const item& row1, const item& row2) const = 0;
140  };
141 
142  class basic_sorter : public sorter
143  {
144  public:
145  basic_sorter();
146  virtual ~basic_sorter() {}
147 
151  basic_sorter& set_level_sort(int level_column, int xp_column);
153  basic_sorter& set_redirect_sort(int column, int to);
154  basic_sorter& set_position_sort(int column, const std::vector<int>& pos);
155  protected:
156  virtual bool column_sortable(int column) const;
157  virtual bool less(int column, const item& row1, const item& row2) const;
158 
159  private:
161  std::map<int,int> redirect_sort_;
162  std::map<int,std::vector<int> > pos_sort_;
163  int xp_col_; //used by level sort
164  };
165 
166  menu(CVideo& video, const std::vector<std::string>& items,
167  bool click_selects=false, int max_height=-1, int max_width=-1,
168  const sorter* sorter_obj=nullptr, style *menu_style=nullptr, const bool auto_join=true);
169 
170  /** Default implementation, but defined out-of-line for efficiency reasons. */
171  ~menu();
172 
173  int selection() const;
174 
175  void move_selection(size_t id);
176  void move_selection_keeping_viewport(size_t id);
177  void reset_selection();
178 
179  const item& get_item(int index) const;
180  const item& get_selected_item() const;
181 
182  // allows user to change_item while running (dangerous)
183  void change_item(int pos1,int pos2,const std::string& str);
184 
185  virtual void erase_item(size_t index);
186 
187  void set_heading(const std::vector<std::string>& heading);
188 
189  /// Set new items to show and redraw/recalculate everything. If
190  /// strip_spaces is false, spaces will remain at the item edges. If
191  /// keep_viewport is true, the menu tries to keep the selection at
192  /// the same position as it were before the items were set.
193  virtual void set_items(const std::vector<std::string>& items, bool strip_spaces=true,
194  bool keep_viewport=false);
195 
196  /// Set a new max height for this menu. Note that this does not take
197  /// effect immediately, only after certain operations that clear
198  /// everything, such as set_items().
199  void set_max_height(const int new_max_height);
200  void set_max_width(const int new_max_width);
201 
202  int get_max_height() { return max_height_; }
203  int get_max_width() { return max_width_; }
204 
205  size_t number_of_items() const { return items_.size(); }
206 
207  int process();
208 
209  bool double_clicked();
210 
211  void set_click_selects(bool value);
213 
214  void scroll(unsigned int pos);
215 
216  //currently, menus do not manage the memory of their sorter
217  //this should be changed to a more object-oriented approach
218  void set_sorter(sorter *s);
219  void sort_by(int column);
220  int get_sort_by() {return sortby_;}
222 
223 protected:
224  bool item_ends_with_image(const std::string& item) const;
225  virtual void handle_event(const SDL_Event& event);
226  void set_inner_location(const SDL_Rect& rect);
227 
228  bool requires_event_focus(const SDL_Event *event=nullptr) const;
229  const std::vector<int>& column_widths() const;
230  virtual void draw_row(const size_t row_index, const SDL_Rect& rect, ROW_TYPE type);
231 
233  bool silent_;
234 
235  int hit(int x, int y) const;
236 
237  std::pair<int,int> hit_cell(int x, int y) const;
238  int hit_column(int x) const;
239 
240  int hit_heading(int x, int y) const;
241 
242  void invalidate_row(size_t id);
243  void invalidate_row_pos(size_t pos);
244  void invalidate_heading();
245 
246 private:
247  size_t max_items_onscreen() const;
248 
249  size_t heading_height() const;
250 
252  mutable int max_items_, item_height_;
253 
255  void key_press(SDLKey key);
256 
257  std::vector<item> items_;
258  std::vector<size_t> item_pos_;
259 
260  std::vector<std::string> heading_;
261  mutable int heading_height_;
262 
263  void create_help_strings();
264  void process_help_string(int mousex, int mousey);
265 
266  std::pair<int,int> cur_help_;
268 
269  mutable std::vector<int> column_widths_;
270 
271  size_t selected_;
273  bool out_;
275  //std::set<size_t> undrawn_items_;
276 
278 
280 
281  void column_widths_item(const std::vector<std::string>& row, std::vector<int>& widths) const;
282 
283  void clear_item(int item);
284  void draw_contents();
285  void draw();
286 
287  mutable std::map<int,SDL_Rect> itemRects_;
288 
289  SDL_Rect get_item_rect(int item) const;
290  SDL_Rect get_item_rect_internal(size_t pos) const;
291  size_t get_item_height_internal(const std::vector<std::string>& item) const;
292  size_t get_item_height(int item) const;
293  int items_start() const;
294 
295  int items_end() const;
296  int items_height() const;
297 
299 
300  ///variable which determines whether a numeric keypress should
301  ///select an item on the dialog
303  // These two variables are used to get the correct double click
304  // behavior so that a click that causes one double click wont be
305  // counted as a first click in the "next" double click.
308 
309  //ellipsis calculation is slightly off, so default to false
311 
312  const sorter* sorter_;
313  int sortby_;
316 
317  /// Set new items to show. If strip_spaces is false, spaces will
318  /// remain at the item edges.
319  void fill_items(const std::vector<std::string>& items, bool strip_spaces);
320 
321  void do_sort();
322  void recalculate_pos();
323  void assert_pos();
324 
325  void update_size();
327  void set_selection_pos(size_t pos, bool silent=false, SELECTION_MOVE_VIEWPORT move_viewport=MOVE_VIEWPORT);
328  void move_selection_to(size_t id, bool silent=false, SELECTION_MOVE_VIEWPORT move_viewport=MOVE_VIEWPORT);
329  void move_selection_up(size_t dep);
330  void move_selection_down(size_t dep);
331 
332  std::set<int> invalid_;
333 };
334 
335 
336 
337 }
338 
339 #endif
static style simple_style
Definition: menu.hpp:117
basic_sorter & set_alpha_sort(int column)
Definition: menu.cpp:48
int items_start() const
size_t max_items_onscreen() const
Definition: menu.cpp:493
int items_end() const
int hit_heading(int x, int y) const
Definition: menu.cpp:1199
void sort_by(int column)
Definition: menu.cpp:783
size_t get_font_size() const
Definition: menu_style.cpp:60
size_t thickness_
Definition: menu.hpp:57
bool last_was_doubleclick_
Definition: menu.hpp:307
virtual void draw_row(menu &menu_ref, const size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: menu_style.cpp:207
int hit(int x, int y) const
Definition: menu.cpp:1158
virtual void draw_row_bg(menu &menu_ref, const size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: menu.cpp:844
const std::vector< int > & column_widths() const
Definition: menu.cpp:919
void set_numeric_keypress_selection(bool value)
Definition: menu.cpp:761
imgsel_style(const std::string &img_base, bool has_bg, int normal_rgb, int selected_rgb, int heading_rgb, double normal_alpha, double selected_alpha, double heading_alpha)
Definition: menu_style.cpp:50
virtual void init()
Definition: menu.hpp:81
std::set< int > level_sort_
Definition: menu.hpp:160
void invalidate_row(size_t id)
Definition: menu.cpp:1322
virtual void init()
Definition: menu.hpp:39
size_t selected_
Definition: menu.hpp:271
int heading_rgb_
Definition: menu.hpp:59
int max_height_
Definition: menu.hpp:251
int pos
Definition: formula.cpp:800
int max_img_w_
Definition: menu.hpp:65
bool num_selects_
variable which determines whether a numeric keypress should select an item on the dialog ...
Definition: menu.hpp:302
double heading_alpha_
Definition: menu.hpp:63
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
std::map< int, int > redirect_sort_
Definition: menu.hpp:161
Definition: video.hpp:58
int sortby_
Definition: menu.hpp:313
ROW_TYPE
Definition: menu.hpp:32
int selected_rgb_
Definition: menu.hpp:59
basic_sorter & set_position_sort(int column, const std::vector< int > &pos)
Definition: menu.cpp:88
std::vector< std::string > heading_
Definition: menu.hpp:260
CVideo & video() const
Definition: widget.hpp:83
General purpose widgets.
bool load_image(const std::string &img_sub)
Definition: menu_style.cpp:113
virtual bool less(int column, const item &row1, const item &row2) const =0
virtual SDL_Rect item_size(const std::string &item) const
Definition: menu_style.cpp:347
void do_sort()
Definition: menu.cpp:277
size_t font_size_
Definition: menu.hpp:55
basic_sorter & set_redirect_sort(int column, int to)
Definition: menu.cpp:79
void move_selection_up(size_t dep)
Definition: menu.cpp:547
SELECTION_MOVE_VIEWPORT
Definition: menu.hpp:326
bool show_result_
Definition: menu.hpp:277
size_t get_item_height(int item) const
Definition: menu.cpp:1279
virtual void erase_item(size_t index)
Definition: menu.cpp:406
bool item_ends_with_image(const std::string &item) const
Definition: menu.cpp:912
virtual bool column_sortable(int column) const =0
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
const std::vector< std::string > items
std::vector< int > column_widths_
Definition: menu.hpp:269
std::vector< std::string > help
Definition: menu.hpp:130
virtual void handle_event(const SDL_Event &event)
Definition: menu.cpp:654
surface get_item_image(const image::locator &i_locator) const
Definition: menu_style.cpp:92
size_t number_of_items() const
Definition: menu.hpp:205
item(const std::vector< std::string > &fields, size_t id)
Definition: menu.hpp:125
void adjust_viewport_to_selection()
Definition: menu.cpp:519
double normal_alpha_
Definition: menu.hpp:63
void set_sorter(sorter *s)
Definition: menu.cpp:772
static imgsel_style bluebg_style
Definition: menu.hpp:118
#define SDLKey
Definition: compat.hpp:29
int item_height_
Definition: menu.hpp:252
size_t get_item_height_internal(const std::vector< std::string > &item) const
Definition: menu.cpp:1259
void update_size()
Definition: menu.cpp:342
virtual void draw_row(const size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: menu.cpp:939
void set_click_selects(bool value)
Definition: menu.cpp:756
bool ignore_next_doubleclick_
Definition: menu.hpp:306
virtual bool column_sortable(int column) const
Definition: menu.cpp:94
void reset_selection()
Definition: menu.cpp:582
bool sortreversed_
Definition: menu.hpp:314
virtual void draw_row_bg(menu &menu_ref, const size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: menu_style.cpp:180
void create_help_strings()
Definition: menu.cpp:316
virtual ~sorter()
Definition: menu.hpp:137
void move_selection_keeping_viewport(size_t id)
Definition: menu.cpp:575
void draw_contents()
Definition: menu.cpp:1104
GLuint id
Definition: glew.h:1647
bool requires_event_focus(const SDL_Event *event=nullptr) const
Definition: menu.cpp:621
bool get_sort_reversed()
Definition: menu.hpp:221
int get_max_width()
Definition: menu.hpp:203
void fill_items(const std::vector< std::string > &items, bool strip_spaces)
Set new items to show.
Definition: menu.cpp:220
bool out_
Definition: menu.hpp:273
void update_scrollbar_grip_height()
Definition: menu.cpp:336
std::pair< int, int > cur_help_
Definition: menu.hpp:266
GLsizei const GLfloat * value
Definition: glew.h:1817
basic_sorter & set_id_sort(int column)
Definition: menu.cpp:73
virtual bool less(int column, const item &row1, const item &row2) const
Definition: menu.cpp:125
int get_sort_by()
Definition: menu.hpp:220
std::set< int > id_sort_
Definition: menu.hpp:160
std::vector< std::string > fields
Definition: menu.hpp:129
int process()
Definition: menu.cpp:739
size_t get_thickness() const
Definition: menu_style.cpp:62
int max_width_
Definition: menu.hpp:251
void set_heading(const std::vector< std::string > &heading)
Definition: menu.cpp:435
static style & default_style
Definition: menu.hpp:116
int highlight_heading_
Definition: menu.hpp:315
void set_max_width(const int new_max_width)
Definition: menu.cpp:485
double selected_alpha2_
Definition: menu.hpp:98
void set_inner_location(const SDL_Rect &rect)
Definition: menu.cpp:378
const std::string img_base_
Definition: menu.hpp:85
double selected_alpha_
Definition: menu.hpp:63
void key_press(SDLKey key)
Definition: menu.cpp:587
int get_max_height()
Definition: menu.hpp:202
std::set< int > numeric_sort_
Definition: menu.hpp:160
virtual void set_items(const std::vector< std::string > &items, bool strip_spaces=true, bool keep_viewport=false)
Set new items to show and redraw/recalculate everything.
Definition: menu.cpp:446
void move_selection(size_t id)
Definition: menu.cpp:567
SDL_Rect get_item_rect_internal(size_t pos) const
Definition: menu.cpp:1215
GLenum GLenum GLvoid GLvoid * column
Definition: glew.h:3805
void change_item(int pos1, int pos2, const std::string &str)
Definition: menu.cpp:395
void move_selection_down(size_t dep)
Definition: menu.cpp:552
const item & get_item(int index) const
Definition: menu.cpp:385
int help_string_
Definition: menu.hpp:267
bool previous_button_
Definition: menu.hpp:274
void assert_pos()
Definition: menu.cpp:307
size_t heading_height() const
Definition: menu.cpp:1270
std::vector< size_t > item_pos_
Definition: menu.hpp:258
int items_height() const
void invalidate_heading()
Definition: menu.cpp:1340
GLuint index
Definition: glew.h:1782
void scroll(unsigned int pos)
Definition: menu.cpp:766
bool use_ellipsis_
Definition: menu.hpp:310
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
int normal_rgb_
Definition: menu.hpp:59
void set_selection_pos(size_t pos, bool silent=false, SELECTION_MOVE_VIEWPORT move_viewport=MOVE_VIEWPORT)
Definition: menu.cpp:526
bool double_clicked()
Definition: menu.cpp:749
std::set< int > invalid_
Definition: menu.hpp:332
basic_sorter & set_numeric_sort(int column)
Definition: menu.cpp:54
void column_widths_item(const std::vector< std::string > &row, std::vector< int > &widths) const
Definition: menu.cpp:898
double heading_alpha2_
Definition: menu.hpp:98
void invalidate_row_pos(size_t pos)
Definition: menu.cpp:1331
size_t id
Definition: menu.hpp:131
void scale_images(int max_width, int max_height)
Definition: menu_style.cpp:64
std::pair< int, int > hit_cell(int x, int y) const
Definition: menu.cpp:1184
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1220
void draw()
Definition: menu.cpp:1116
SDL_Rect get_item_rect(int item) const
Definition: menu.cpp:1210
std::set< int > alpha_sort_
Definition: menu.hpp:160
virtual ~style()
Definition: menu_style.cpp:48
std::map< std::string, surface > img_map_
Definition: menu.hpp:89
GLenum GLenum GLvoid * row
Definition: glew.h:3805
std::map< int, std::vector< int > > pos_sort_
Definition: menu.hpp:162
void move_selection_to(size_t id, bool silent=false, SELECTION_MOVE_VIEWPORT move_viewport=MOVE_VIEWPORT)
Definition: menu.cpp:559
basic_sorter & set_level_sort(int level_column, int xp_column)
Definition: menu.cpp:66
bool click_selects_
Definition: menu.hpp:272
~menu()
Default implementation, but defined out-of-line for efficiency reasons.
Definition: menu.cpp:216
cl_event event
Definition: glew.h:3070
std::vector< item > items_
Definition: menu.hpp:257
basic_sorter & set_xp_sort(int column)
Definition: menu.cpp:60
this module manages the cache of images.
Definition: image.cpp:75
const sorter * sorter_
Definition: menu.hpp:312
int hit_column(int x) const
Definition: menu.cpp:1172
bool double_clicked_
Definition: menu.hpp:279
virtual ~basic_sorter()
Definition: menu.hpp:146
int max_img_h_
Definition: menu.hpp:65
void process_help_string(int mousex, int mousey)
Definition: menu.cpp:1292
void clear_item(int item)
Definition: menu.cpp:931
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
bool silent_
Definition: menu.hpp:233
Definition: help.cpp:57
int heading_height_
Definition: menu.hpp:261
menu(CVideo &video, const std::vector< std::string > &items, bool click_selects=false, int max_height=-1, int max_width=-1, const sorter *sorter_obj=nullptr, style *menu_style=nullptr, const bool auto_join=true)
Definition: menu.cpp:195
GLdouble s
Definition: glew.h:1358
void recalculate_pos()
Definition: menu.cpp:298
std::set< int > xp_sort_
Definition: menu.hpp:160
virtual void draw_row(menu &menu_ref, const size_t row_index, const SDL_Rect &rect, ROW_TYPE type)
Definition: menu.cpp:879
int max_items_
Definition: menu.hpp:252
GLsizei const GLcharARB ** string
Definition: glew.h:4503
size_t cell_padding_
Definition: menu.hpp:56
void set_max_height(const int new_max_height)
Set a new max height for this menu.
Definition: menu.cpp:477
std::map< int, SDL_Rect > itemRects_
Definition: menu.hpp:287
size_t get_cell_padding() const
Definition: menu_style.cpp:61
int selection() const
Definition: menu.cpp:369
const item & get_selected_item() const
Definition: menu.cpp:390
style * style_
Definition: menu.hpp:232
virtual SDL_Rect item_size(const std::string &item) const
Definition: menu.cpp:804