The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
construct_dialog.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2016 by Patrick Parker <[email protected]>
3  wesnoth widget Copyright (C) 2003-5 by David White <[email protected]>
4  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #ifndef CONSTRUCT_DIALOG_H_INCLUDED
17 #define CONSTRUCT_DIALOG_H_INCLUDED
18 
19 #include "show_dialog.hpp"
20 
21 #include "widgets/label.hpp"
22 #include "widgets/textbox.hpp"
23 #include "key.hpp"
24 
25 namespace gui {
26 
28 {
29 public:
31  key(),
32  left_button(true),
33  right_button(true),
34  key_down(true),
35  first_time(true),
36  double_clicked(false),
37  new_left_button(false),
38  new_right_button(false),
39  new_key_down(false),
40  selection(-1),
41  clear_buttons_(false)
42  {}
43 
44  void clear_buttons() {
45  clear_buttons_ = true;
46  }
47 
48  void cycle() {
49  if(clear_buttons_) {
50  left_button = true;
51  right_button = true;
52  key_down = true;
53  clear_buttons_ = false;
54  } else {
58  }
59  }
63  int selection;
64 private:
66 };
67 
68 class dialog_image : public widget {
69 public:
70  dialog_image(label *const caption, CVideo &video, surface img) : widget(video, false),
71  surf_(img), caption_(caption)
72  {
73  if(!img.null()) {
74  set_measurements(img->w, img->h);
75  }
76  }
77  ~dialog_image() { delete caption_; }
78 
79  //surface surface() const { return surf_; }
80  label *caption() const { return caption_; }
81  void draw_contents();
82 
85  if(caption_) h.push_back(caption_);
86  return h;
87  }
88 private:
89 
92 };
93 
94 class dialog_textbox : public textbox {
95 public:
96  dialog_textbox(label *const label_widget, CVideo &video, int width, const std::string& text="", bool editable=true, size_t max_size = 256, int font_size = font::SIZE_PLUS, double alpha = 0.4, double alpha_focus = 0.2)
97  : textbox(video, width, text, editable, max_size, font_size, alpha, alpha_focus, false),
98  label_(label_widget)
99  {}
100  virtual ~dialog_textbox();
101 
102  label *get_label() const { return label_; }
103 
106  if(label_) h.push_back(label_);
107  return h;
108  }
109 private:
110  //forbidden operations
112  void operator=(const dialog_textbox&);
113 
115 };
116 
117 class dialog;
118 
120 public:
122  const std::vector<std::string>& items,
123  const std::vector<std::string>& items_to_filter, size_t header_row,
124  dialog& dialog, int width = 250) :
125  dialog_textbox(new label(video, header), video, width),
126  items_(items),
127  items_to_filter_(items_to_filter),
128  filtered_items_(),
129  index_map_(),
130  last_words(1, ""), // dummy word to trigger an update
131  header_row_(header_row),
132  dialog_(dialog)
133  {
134  set_text("");
135  }
136 
137  // current menu selection is based on a possibly filtered view,
138  // and thus may differ from the original, unfiltered index
139  int get_index(int selection) const;
140  void delete_item(int selection);
141 
142 private:
143  std::vector<std::string> items_, items_to_filter_, filtered_items_;
144  std::vector<int> index_map_;
145  std::vector<std::string> last_words;
146  size_t header_row_;
148  virtual void handle_text_changed(const ucs4::string& text);
149 };
150 
151 class dialog_button : public button {
152 public:
154  int simple_result=CONTINUE_DIALOG, dialog_button_action *handler=nullptr)
155  : button(video,label,type,"",DEFAULT_SPACE,false), simple_result_(simple_result),
156  parent_(nullptr), handler_(handler)
157  {}
158  void set_parent(class dialog *parent) {
159  parent_ = parent;
160  }
161  bool is_option() const {
162  return (type_ == TYPE_CHECK);
163  }
164  virtual int action(dialog_process_info &info);
165 protected:
166  class dialog *dialog() const { return parent_; }
167  const int simple_result_;
168 private:
169  class dialog *parent_;
171 };
172 
174 public:
175  standard_dialog_button(CVideo& video, const std::string& label, const int index, const bool is_last)
176  : dialog_button(video,label,TYPE_PRESS,index), is_last_(is_last)
177  {}
179 private:
180  const bool is_last_;
181 };
182 
183 
184 class dialog {
185 public:
189  int x, y;
191  unsigned int menu_width;
192  std::map<preview_pane *, SDL_Rect > panes;
196  std::map<dialog_button *, std::pair<int,int> > buttons;
197  //use get_frame().get_layout() to check frame dimensions
198  };
200 
201 private:
203  typedef std::vector<preview_pane *>::const_iterator pp_const_iterator;
205  typedef std::vector<dialog_button *>::const_iterator button_const_iterator;
206  typedef std::vector< std::pair<dialog_button *, BUTTON_LOCATION> >::iterator button_pool_iterator;
207  typedef std::vector< std::pair<dialog_button *, BUTTON_LOCATION> >::const_iterator button_pool_const_iterator;
208 
209 public:
210 
211  //Static members
212  static const style& default_style;
213  static const style& message_style;
214  static const style hotkeys_style;
215  static const int message_font_size;
216  static const int caption_font_size;
217  static const int max_menu_width;
218  static const size_t left_padding;
219  static const size_t right_padding;
220  static const size_t image_h_pad;
221  static const size_t top_padding;
222  static const size_t bottom_padding;
223 
224  //Constructor & destructor
225  //dialog - throws button::error() if standard buttons fail to initialize
226  // throws utf8::invalid_utf8_exception() if message is invalid
227  dialog(CVideo& video,
228  const std::string& title="",
229  const std::string& message="",
230  const DIALOG_TYPE type=MESSAGE,
231  const style& dialog_style=default_style);
232  virtual ~dialog();
233 
234  //Adding components - the dialog will manage the memory of
235  //these widgets, therefore do not attempt to reference its
236  //widgets after destroying it
237  void set_image(dialog_image *const img) { delete image_; image_ = img; }
238  void set_image(surface surf, const std::string &caption="");
239  void set_menu(menu *const m) { if ( menu_ != empty_menu ) delete menu_;
240  menu_ = m == nullptr ? empty_menu : m; }
241  void set_menu(const std::vector<std::string> & menu_items, menu::sorter* sorter=nullptr);
242  void set_menu_items(const std::vector<std::string> &menu_items, bool keep_selection=false);
243 
244  //add_pane - preview panes are not currently memory managed
245  //(for backwards compatibility)
246  void add_pane(preview_pane *const pp) { preview_panes_.push_back(pp); }
247  void set_panes(std::vector<preview_pane*> panes) { preview_panes_ = panes; }
248  void set_textbox(dialog_textbox *const box) {
249  delete text_widget_;
250  text_widget_ = box;
251  }
252  void set_textbox(const std::string& text_widget_label="",
253  const std::string &text_widget_text="",
254  const int text_widget_max_chars = 256,
255  const unsigned int text_box_width = font::relative_size(350));
256  void add_button(dialog_button *const btn, BUTTON_LOCATION loc);
258  void add_option(const std::string& label, bool checked=false, BUTTON_LOCATION loc=BUTTON_CHECKBOX, const std::string& help_string = "");
259 
260  //Specific preparations
261  //layout - determines dialog measurements based on all components
262  virtual dimension_measurements layout(int xloc=-1, int yloc=-1);
263  void set_layout(dimension_measurements &new_dim);
267 
268  //Launching the dialog
269  //show - the return value of this method should be the same as result()
270  int show(int xloc, int yloc);
271  int show();
272 
273  //Results
274  int result() const { return result_; }
275  menu &get_menu() { return *menu_; }
276  bool done() const { return (result_ != CONTINUE_DIALOG); }
279  bool option_checked(unsigned int option_index=0);
280  CVideo& get_video() { return video_; }
281 
282  /// Explicit freeing of class static resources.
283  /// Must not be called if any instances of this class exist.
284  /// Should be called if the display goes out of scope.
285  /// (Currently called by ~game_launcher.)
286  static void delete_empty_menu() { delete empty_menu; empty_menu = nullptr; }
287 
288 protected:
289  void set_result(const int result) { result_ = result; }
290 
291  //action - invoked at the end of the dialog-processing loop
292  virtual void action(dialog_process_info &dp_info);
293  //refresh - forces the display to refresh
294  void refresh();
295 
296  label& get_message() const { return *message_; }
297 
298 private:
299  void clear_background();
300  void draw_frame();
302  void draw_contents();
303 
304  //process - execute a single dialog processing loop and return the result
306 
307  /// A pointer to this empty menu is used instead of nullptr (for menu_).
308  static menu * empty_menu;
309  /// Provides create-on-use semantics for empty_menu.
310  static menu * get_empty_menu(CVideo& video);
311 
312  //Members
316  const style& style_;
319  gui::menu *menu_; // Never nullptr; it equals empty_menu if there is currently no menu.
320  std::vector<preview_pane*> preview_panes_;
321  std::vector< std::pair<dialog_button*,BUTTON_LOCATION> > button_pool_;
322  std::vector<dialog_button*> standard_buttons_;
323  std::vector<dialog_button*> extra_buttons_;
324  std::vector<dialog_button*> top_buttons_;
325  std::vector<button*> frame_buttons_;
331  int result_;
332 };
333 
334 typedef Uint32 msecs;
335 
336 } //end namespace gui
337 #endif
Definition: plugin.cc:260
Uint32 msecs
int font_size() const
Definition: textbox.cpp:262
dialog_textbox & get_textbox() const
bool null() const
Definition: utils.hpp:104
std::vector< std::string > last_words
static thandler * handler
Definition: handler.cpp:60
sdl_handler_vector handler_members()
void set_result(const int result)
std::vector< char_t > string
bool done() const
void update_widget_positions()
void add_option(const std::string &label, bool checked=false, BUTTON_LOCATION loc=BUTTON_CHECKBOX, const std::string &help_string="")
int get_index(int selection) const
dialog_button_action * handler_
DIALOG_TYPE type_
std::vector< events::sdl_handler * > sdl_handler_vector
Definition: events.hpp:163
static const int message_font_size
void set_textbox(dialog_textbox *const box)
class dialog * parent_
dimension_measurements dim_
dialog_image(label *const caption, CVideo &video, surface img)
std::vector< dialog_button * > standard_buttons_
std::vector< dialog_button * > extra_buttons_
virtual void action(dialog_process_info &dp_info)
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
logger & info()
Definition: log.cpp:91
virtual sdl_handler_vector handler_members()
Definition: scrollarea.cpp:40
Definition: video.hpp:58
class dialog * dialog() const
int relative_size(int size)
Definition: font.hpp:72
static const style & default_style
static const size_t image_h_pad
const style & style_
void add_button(dialog_button *const btn, BUTTON_LOCATION loc)
CVideo & video() const
Definition: widget.hpp:83
General purpose widgets.
sdl_handler_vector handler_members()
void set_text(const std::string &text, const SDL_Color &color=font::NORMAL_COLOR)
Definition: textbox.cpp:76
bool editable() const
Definition: textbox.cpp:257
dialog(CVideo &video, const std::string &title="", const std::string &message="", const DIALOG_TYPE type=MESSAGE, const style &dialog_style=default_style)
std::vector< button * > frame_buttons_
dialog_textbox * text_widget_
static const size_t bottom_padding
#define h
const std::vector< std::string > items
const std::string text() const
Definition: textbox.cpp:69
const int SIZE_PLUS
Definition: font.hpp:65
std::string textbox_text() const
std::vector< std::string > filtered_items_
static menu * get_empty_menu(CVideo &video)
Provides create-on-use semantics for empty_menu.
filter_textbox(CVideo &video, const std::string &header, const std::vector< std::string > &items, const std::vector< std::string > &items_to_filter, size_t header_row, dialog &dialog, int width=250)
dialog_frame * frame_
void set_measurements(int w, int h)
Definition: widget.cpp:129
static const size_t right_padding
std::vector< std::pair< dialog_button *, BUTTON_LOCATION > > button_pool_
std::string topic_
std::vector< dialog_button * > top_buttons_
std::vector< std::string > items_to_filter_
void add_pane(preview_pane *const pp)
static const style & message_style
GLuint64EXT * result
Definition: glew.h:10727
int action(dialog_process_info &info)
std::vector< preview_pane * >::iterator pp_iterator
std::string title_
void set_menu(menu *const m)
dialog_button * help_button_
TYPE type_
Definition: button.hpp:68
virtual dimension_measurements layout(int xloc=-1, int yloc=-1)
GLclampf GLclampf GLclampf alpha
Definition: glew.h:1488
static const size_t top_padding
std::vector< dialog_button * >::iterator button_iterator
void set_menu_items(const std::vector< std::string > &menu_items, bool keep_selection=false)
Changes (or sets, if not previously set) the dialog's menu items to the provided list.
dimension_measurements get_layout() const
static const style hotkeys_style
surf
Definition: filter.cpp:143
void set_image(dialog_image *const img)
dialog_frame & get_frame()
void set_basic_behavior(DIALOG_TYPE type)
std::vector< std::pair< dialog_button *, BUTTON_LOCATION > >::const_iterator button_pool_const_iterator
std::vector< preview_pane * >::const_iterator pp_const_iterator
int result() const
void operator=(const dialog_textbox &)
CVideo & get_video()
GLuint index
Definition: glew.h:1782
static const size_t left_padding
std::map< dialog_button *, std::pair< int, int > > buttons
void set_parent(class dialog *parent)
virtual void handle_text_changed(const ucs4::string &text)
dialog_button(CVideo &video, const std::string &label, TYPE type=TYPE_PRESS, int simple_result=CONTINUE_DIALOG, dialog_button_action *handler=nullptr)
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
static menu * empty_menu
A pointer to this empty menu is used instead of nullptr (for menu_).
label * get_label() const
virtual int action(dialog_process_info &info)
std::map< preview_pane *, SDL_Rect > panes
std::vector< preview_pane * > preview_panes_
std::vector< std::string > items_
std::vector< int > index_map_
bool option_checked(unsigned int option_index=0)
static const int caption_font_size
std::vector< dialog_button * >::const_iterator button_const_iterator
const GLdouble * m
Definition: glew.h:6968
label * caption() const
static const int max_menu_width
GLint GLvoid * img
Definition: glew.h:1353
int process(dialog_process_info &info)
gui::menu * menu_
GLsizei GLenum GLuint GLuint GLsizei char * message
Definition: glew.h:2499
std::vector< std::pair< dialog_button *, BUTTON_LOCATION > >::iterator button_pool_iterator
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
dialog_textbox(label *const label_widget, CVideo &video, int width, const std::string &text="", bool editable=true, size_t max_size=256, int font_size=font::SIZE_PLUS, double alpha=0.4, double alpha_focus=0.2)
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
dialog_frame::style style
void delete_item(int selection)
Class that keeps track of all the keys on the keyboard.
Definition: key.hpp:27
standard_dialog_button(CVideo &video, const std::string &label, const int index, const bool is_last)
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void set_panes(std::vector< preview_pane * > panes)
void set_layout(dimension_measurements &new_dim)
dialog_image * image_
label & get_message() const
static void delete_empty_menu()
Explicit freeing of class static resources.