The Battle for Wesnoth  1.15.0-dev
lua_gui2.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2014 - 2018 by Chris Beck <render787@gmail.com>
3  Part of the Battle for Wesnoth Project https://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 #include "scripting/lua_gui2.hpp"
16 
18 #include "gui/core/canvas.hpp"
27 #include "gui/dialogs/message.hpp"
30 #include "gui/widgets/listbox.hpp"
35 #include "gui/widgets/slider.hpp"
37 #include "gui/widgets/text_box.hpp"
41 #include "gui/widgets/widget.hpp"
42 #include "gui/widgets/window.hpp"
43 
44 #include "config.hpp"
45 #include "log.hpp"
46 #include "scripting/lua_common.hpp"
48 #include "scripting/lua_unit.hpp"
50 #include "scripting/push_check.hpp"
52 #include "tstring.hpp"
53 #include "game_data.hpp"
54 #include "game_state.hpp"
55 
56 #include "utils/functional.hpp"
57 
58 #include <map>
59 #include <utility>
60 #include <vector>
61 #include <boost/optional.hpp>
62 
63 #include "lua/lauxlib.h" // for luaL_checkinteger, etc
64 #include "lua/lua.h" // for lua_setfield, etc
65 
66 static lg::log_domain log_scripting_lua("scripting/lua");
67 #define ERR_LUA LOG_STREAM(err, log_scripting_lua)
68 
69 static const char dlgclbkKey[] = "dialog callback";
70 
71 namespace {
72  struct scoped_dialog
73  {
74  lua_State* L;
75  scoped_dialog* prev;
76  static scoped_dialog* current;
77  std::unique_ptr<gui2::window> window;
78  typedef std::map<gui2::widget*, int> callback_map;
79  callback_map callbacks;
80 
81  scoped_dialog(lua_State* l, gui2::window* w);
82  ~scoped_dialog();
83  private:
84  scoped_dialog(const scoped_dialog&) = delete;
85  };
86 
87  scoped_dialog* scoped_dialog::current = nullptr;
88 
89  scoped_dialog::scoped_dialog(lua_State* l, gui2::window* w)
90  : L(l), prev(current), window(w), callbacks()
91  {
93  lua_createtable(L, 1, 0);
94  lua_pushvalue(L, -2);
96  lua_rawseti(L, -2, 1);
98  current = this;
99  }
100 
101  scoped_dialog::~scoped_dialog()
102  {
103  current = prev;
105  lua_pushvalue(L, -1);
107  lua_rawgeti(L, -1, 1);
108  lua_remove(L, -2);
110  }
111 }//unnamed namespace for scoped_dialog
112 
113 static gui2::widget* find_widget(lua_State* L, int i, bool readonly)
114 {
115  if(!scoped_dialog::current) {
116  luaL_error(L, "no visible dialog");
117  error_oob_call_dtors:
118  luaL_argerror(L, i, "out of bounds");
119  error_not_str_call_dtors:
120  luaW_type_error(L, i, "string");
121  error_no_wgt_call_dtors:
122  luaL_argerror(L, i, "widget not found");
123  return nullptr;
124  }
125 
126  gui2::widget* w = scoped_dialog::current->window.get();
127  for(; !lua_isnoneornil(L, i); ++i)
128  {
129  if(gui2::listbox* list = dynamic_cast<gui2::listbox*>(w))
130  {
131  int v = lua_tointeger(L, i);
132  if(v < 1) {
133  goto error_oob_call_dtors;
134  }
135  int n = list->get_item_count();
136  if(v > n) {
137  if(readonly) {
138  goto error_oob_call_dtors;
139  }
141  for(; n < v; ++n) {
142  list->add_row(dummy);
143  }
144  }
145  w = list->get_row_grid(v - 1);
146  } else if(gui2::multi_page* multi_page = dynamic_cast<gui2::multi_page*>(w)) {
147  int v = lua_tointeger(L, i);
148  if(v < 1) {
149  goto error_oob_call_dtors;
150  }
151  int n = multi_page->get_page_count();
152  if(v > n) {
153  if(readonly) {
154  goto error_oob_call_dtors;
155  }
157  for(; n < v; ++n) {
158  multi_page->add_page(dummy);
159  }
160  }
161  w = &multi_page->page_grid(v - 1);
162  } else if(gui2::tree_view* tree_view = dynamic_cast<gui2::tree_view*>(w)) {
163  gui2::tree_view_node& tvn = tree_view->get_root_node();
164  if(lua_isnumber(L, i)) {
165  int v = lua_tointeger(L, i);
166  if(v < 1) {
167  goto error_oob_call_dtors;
168  }
169  int n = tvn.count_children();
170  if(v > n) {
171  goto error_oob_call_dtors;
172  }
173  w = &tvn.get_child_at(v - 1);
174 
175  } else {
176  std::string m = luaL_checkstring(L, i);
177  w = tvn.find(m, false);
178  }
179  } else if(gui2::tree_view_node* tree_view_node = dynamic_cast<gui2::tree_view_node*>(w)) {
180  if(lua_isnumber(L, i)) {
181  int v = lua_tointeger(L, i);
182  if(v < 1) {
183  goto error_oob_call_dtors;
184  }
185  int n = tree_view_node->count_children();
186  if(v > n) {
187  goto error_oob_call_dtors;
188  }
189  w = &tree_view_node->get_child_at(v - 1);
190 
191  } else {
192  std::string m = luaL_checkstring(L, i);
193  w = tree_view_node->find(m, false);
194  }
195  } else if(gui2::stacked_widget* stacked_widget = dynamic_cast<gui2::stacked_widget*>(w)) {
196  if(lua_isnumber(L, i)) {
197  int v = lua_tointeger(L, i);
198  if(v < 1) {
199  goto error_oob_call_dtors;
200  }
201  int n = stacked_widget->get_layer_count();
202  if(v > n) {
203  goto error_oob_call_dtors;
204  }
205  w = stacked_widget->get_layer_grid(v - 1);
206  } else {
207  std::string m = luaL_checkstring(L, i);
208  w = stacked_widget->find(m, false);
209  }
210  } else {
211  char const *m = lua_tostring(L, i);
212  if(!m) {
213  goto error_not_str_call_dtors;
214  }
215  w = w->find(m, false);
216  }
217  if(!w) {
218  goto error_no_wgt_call_dtors;
219  }
220  }
221 
222  return w;
223 }
224 
225 namespace lua_gui2 {
226 
227 /**
228  * Displays a window.
229  * - Arg 1: WML table describing the window.
230  * - Arg 2: function called at pre-show.
231  * - Arg 3: function called at post-show.
232  * - Ret 1: integer.
233  */
235 {
236  config def_cfg = luaW_checkconfig(L, 1);
237 
239  scoped_dialog w(L, gui2::build(&def));
240 
241  if(!lua_isnoneornil(L, 2)) {
242  lua_pushvalue(L, 2);
243  lua_call(L, 0, 0);
244  }
245 
246  int v = scoped_dialog::current->window->show(true, 0);
247 
248  if (!lua_isnoneornil(L, 3)) {
249  lua_pushvalue(L, 3);
250  lua_call(L, 0, 0);
251  }
252 
253  lua_pushinteger(L, v);
254  return 1;
255 }
256 
257 /**
258  * Displays a message window
259  * - Arg 1: Table describing the window
260  * - Arg 2: List of options (nil or empty table - no options)
261  * - Arg 3: Text input specifications (nil or empty table - no text input)
262  * - Ret 1: option chosen (if no options: 0 if there's text input, -2 if escape pressed, else -1)
263  * - Ret 2: string entered (empty if none, nil if no text input)
264  */
266 {
267  config txt_cfg;
268  const bool has_input = !lua_isnoneornil(L, 3) && luaW_toconfig(L, 3, txt_cfg) && !txt_cfg.empty();
269 
271  input.caption = txt_cfg["label"].str();
272  input.text = txt_cfg["text"].str();
273  input.maximum_length = txt_cfg["max_length"].to_int(256);
274  input.text_input_was_specified = has_input;
275 
277  if(!lua_isnoneornil(L, 2)) {
278  luaL_checktype(L, 2, LUA_TTABLE);
279  std::size_t n = lua_rawlen(L, 2);
280  for(std::size_t i = 1; i <= n; i++) {
281  lua_rawgeti(L, 2, i);
282  t_string short_opt;
283  config opt;
284  if(luaW_totstring(L, -1, short_opt)) {
285  // Note: Although this currently uses the legacy_menu_item class
286  // for the deprecated syntax, this branch should still be retained
287  // when the deprecated syntax is removed, as a simpler method
288  // of specifying options when only a single string is needed.
289  const std::string& opt_str = short_opt;
290  gui2::legacy_menu_item item(opt_str, "Use image=, label=, description=, default= instead.");
291  opt["image"] = item.icon();
292  opt["label"] = item.label();
293  opt["description"] = item.description();
294  opt["default"] = item.is_default();
295  } else if(!luaW_toconfig(L, -1, opt)) {
296  std::ostringstream error;
297  error << "expected array of config and/or translatable strings, but index ";
298  error << i << " was a " << lua_typename(L, lua_type(L, -1));
299  return luaL_argerror(L, 2, error.str().c_str());
300  }
301  gui2::dialogs::wml_message_option option(opt["label"], opt["description"], opt["image"]);
302  if(opt["default"].to_bool(false)) {
303  options.chosen_option = i - 1;
304  }
305  options.option_list.push_back(option);
306  lua_pop(L, 1);
307  }
308  lua_getfield(L, 2, "default");
309  if(lua_isnumber(L, -1)) {
310  int i = lua_tointeger(L, -1);
311  if(i < 1 || std::size_t(i) > n) {
312  std::ostringstream error;
313  error << "default= key in options list is not a valid option index (1-" << n << ")";
314  return luaL_argerror(L, 2, error.str().c_str());
315  }
316  options.chosen_option = i - 1;
317  }
318  lua_pop(L, 1);
319  }
320 
321  const config& def_cfg = luaW_checkconfig(L, 1);
322  const std::string& title = def_cfg["title"];
323  const std::string& message = def_cfg["message"];
324 
325  using portrait = gui2::dialogs::wml_message_portrait;
326  std::unique_ptr<portrait> left;
327  std::unique_ptr<portrait> right;
328  const bool is_double = def_cfg.has_attribute("second_portrait");
329  const bool left_side = def_cfg["left_side"].to_bool(true);
330  if(is_double || left_side) {
331  left.reset(new portrait {def_cfg["portrait"], def_cfg["mirror"].to_bool(false)});
332  } else {
333  // This means right side only.
334  right.reset(new portrait {def_cfg["portrait"], def_cfg["mirror"].to_bool(false)});
335  }
336  if(is_double) {
337  right.reset(new portrait {def_cfg["second_portrait"], def_cfg["second_mirror"].to_bool(false)});
338  }
339 
340  int dlg_result = gui2::dialogs::show_wml_message(title, message, left.get(), right.get(), options, input);
341 
342  if(!has_input && options.option_list.empty()) {
343  lua_pushinteger(L, dlg_result);
344  } else {
345  lua_pushinteger(L, options.chosen_option + 1);
346  }
347 
348  if(has_input) {
349  lua_pushlstring(L, input.text.c_str(), input.text.length());
350  } else {
351  lua_pushnil(L);
352  }
353 
354  return 2;
355 }
356 
357 /**
358  * Displays a popup message
359  * - Arg 1: Title (allows Pango markup)
360  * - Arg 2: Message (allows Pango markup)
361  * - Arg 3: Image (optional)
362  */
364  t_string title = luaW_checktstring(L, 1);
366  std::string image = lua_isnoneornil(L, 3) ? "" : luaL_checkstring(L, 3);
367 
368  gui2::show_transient_message(title, msg, image, true, true);
369  return 0;
370 }
371 
372 /**
373  * Displays a story screen
374  * - Arg 1: The story config
375  * - Arg 2: The default title
376  */
378  config story = luaW_checkconfig(L, 1);
379  t_string title = luaW_checktstring(L, 2);
381  return 0;
382 }
383 
384 /**
385  * Displays a popup menu at the current mouse position
386  * Best used from a [set_menu_item], to show a submenu
387  * - Arg 1: Configs defining each item, with keys icon, image/label, second_label, tooltip
388  * - Args 2, 3: Initial selection (integer); whether to parse markup (boolean)
389  */
391  std::vector<config> items = lua_check<std::vector<config>>(L, 1);
392  SDL_Rect pos {1,1,1,1};
393  SDL_GetMouseState(&pos.x, &pos.y);
394 
395  int initial = -1;
396  bool markup = false;
397  if(lua_isnumber(L, 2)) {
398  initial = lua_tointeger(L, 2) - 1;
399  markup = luaW_toboolean(L, 3);
400  } else if(lua_isnumber(L, 3)) {
401  initial = lua_tointeger(L, 3) - 1;
402  markup = luaW_toboolean(L, 2);
403  } else if(lua_isboolean(L, 2)) {
404  markup = luaW_toboolean(L, 2);
405  }
406 
407  gui2::dialogs::drop_down_menu menu(pos, items, initial, markup, false);
408  menu.show();
409  lua_pushinteger(L, menu.selected_item() + 1);
410  return 1;
411 }
412 
413 /**
414  * Displays a simple message box.
415  */
417  const t_string title = luaW_checktstring(L, 1), message = luaW_checktstring(L, 2);
418  std::string button = luaL_optstring(L, 3, "ok"), btn_style;
419  std::transform(button.begin(), button.end(), std::inserter(btn_style, btn_style.begin()), [](char c) { return std::tolower(c); });
420  bool markup = lua_isnoneornil(L, 3) ? luaW_toboolean(L, 3) : luaW_toboolean(L, 4);
421  using button_style = gui2::dialogs::message::button_style;
422  boost::optional<button_style> style;
423  if(btn_style.empty()) {
424  style = button_style::auto_close;
425  } else if(btn_style == "ok") {
426  style = button_style::ok_button;
427  } else if(btn_style == "close") {
428  style = button_style::close_button;
429  } else if(btn_style == "ok_cancel") {
430  style = button_style::ok_cancel_buttons;
431  } else if(btn_style == "cancel") {
432  style = button_style::cancel_button;
433  } else if(btn_style == "yes_no") {
434  style = button_style::yes_no_buttons;
435  }
436  if(style) {
437  int result = gui2::show_message(title, message, *style, markup, markup);
438  if(style == button_style::ok_cancel_buttons || style == button_style::yes_no_buttons) {
439  lua_pushboolean(L, result == gui2::retval::OK);
440  return 1;
441  }
442  } else {
443  gui2::show_message(title, message, button, false, markup, markup);
444  }
445  return 0;
446 }
447 
448 /**
449  * Sets the value of a widget on the current dialog.
450  * - Arg 1: scalar.
451  * - Args 2..n: path of strings and integers.
452  */
454 {
455  gui2::widget *w = find_widget(L, 2, false);
456 
457  if(gui2::listbox* list = dynamic_cast<gui2::listbox*>(w))
458  {
459  if(lua_istable(L, 1)) {
460  // Do two passes in case has_minimum is true
461  // Probably not the best way to do it, but should work in the majority of cases.
462  std::vector<int> selected_vec = lua_check<std::vector<int>>(L, 1);
463  std::set<int> selected(selected_vec.begin(), selected_vec.end());
464  for(unsigned i = 0; i < list->get_item_count(); i++) {
465  list->select_row(i, selected.count(i + 1) > 0);
466  }
467  for(unsigned i = 0; i < list->get_item_count(); i++) {
468  list->select_row(i, selected.count(i + 1) > 0);
469  }
470  } else {
471  int v = luaL_checkinteger(L, 1);
472  int n = list->get_item_count();
473  if(1 <= v && v <= n) {
474  list->select_row(v - 1);
475  } else {
476  return luaL_argerror(L, 1, "out of bounds");
477  }
478  }
479  } else if(gui2::multi_page* multi_page = dynamic_cast<gui2::multi_page*>(w)) {
480  int v = luaL_checkinteger(L, 1);
481  int n = multi_page->get_page_count();
482  if(1 <= v && v <= n) {
483  multi_page->select_page(v - 1);
484  } else {
485  return luaL_argerror(L, 1, "out of bounds");
486  }
487  } else if (gui2::selectable_item* selectable = dynamic_cast<gui2::selectable_item*>(w)) {
488  if(selectable->num_states() == 2) {
489  selectable->set_value_bool(luaW_toboolean(L, 1));
490  } else {
491  selectable->set_value(luaL_checkinteger(L, 1) -1);
492  }
493  } else if (gui2::text_box* text_box = dynamic_cast<gui2::text_box*>(w)) {
495  const t_string& text = luaW_checktstring(L, 1);
496  text_box->set_value(text.str());
497  } else if (gui2::slider* slider = dynamic_cast<gui2::slider*>(w)) {
498  const int v = luaL_checkinteger(L, 1);
499  const int m = slider->get_minimum_value();
500  const int n = slider->get_maximum_value();
501  if(m <= v && v <= n) {
502  slider->set_value(v);
503  } else {
504  return luaL_argerror(L, 1, "out of bounds");
505  }
506  } else if(gui2::progress_bar* progress_bar = dynamic_cast<gui2::progress_bar*>(w)) {
507  const int v = luaL_checkinteger(L, 1);
508  if(0 <= v && v <= 100) {
509  progress_bar->set_percentage(v);
510  } else {
511  return luaL_argerror(L, 1, "out of bounds");
512  }
513  } else if(gui2::stacked_widget* stacked_widget = dynamic_cast<gui2::stacked_widget*>(w)) {
514  if(lua_istable(L, 1)) {
515  boost::dynamic_bitset<> states;
516  states.resize(stacked_widget->get_layer_count());
517  for(unsigned i : lua_check<std::vector<unsigned>>(L, 1)) {
518  if(i > 0 && i <= stacked_widget->get_layer_count()) {
519  states[i] = true;
520  }
521  }
522  stacked_widget->select_layers(states);
523  } else {
524  const int v = luaL_checkinteger(L, 1);
525  const int n = stacked_widget->get_layer_count();
526  if(v >= 0 && v <= n) {
527  stacked_widget->select_layer(v - 1);
528  }
529  }
530  } else if(gui2::unit_preview_pane* unit_preview_pane = dynamic_cast<gui2::unit_preview_pane*>(w)) {
532  if(const unit_type* ut = luaW_tounittype(L, 1)) {
533  unit_preview_pane->set_displayed_type(*ut);
534  } else if(unit* u = luaW_tounit(L, 1)) {
535  unit_preview_pane->set_displayed_unit(*u);
536  } else {
537  return luaW_type_error(L, 1, "unit or unit type");
538  }
539  } else if(gui2::tree_view_node* node = dynamic_cast<gui2::tree_view_node*>(w)) {
540  const bool unfolded = luaW_toboolean(L, 1);
541  if(unfolded) {
542  node->unfold();
543  } else {
544  node->fold();
545  }
546  } else if(gui2::multimenu_button* menu = dynamic_cast<gui2::multimenu_button*>(w)) {
547  if(lua_istable(L, 1)) {
548  boost::dynamic_bitset<> states;
549  states.resize(menu->num_options());
550  for(unsigned i : lua_check<std::vector<unsigned>>(L, 1)) {
551  if(i > 0 && i <= menu->num_options()) {
552  states[i] = true;
553  }
554  }
555  menu->select_options(states);
556  } else {
557  int v = luaL_checkinteger(L, 1);
558  int n = menu->num_options();
559  if(1 <= v && v <= n) {
560  menu->select_option(v - 1);
561  } else {
562  return luaL_argerror(L, 1, "out of bounds");
563  }
564  }
565  } else {
567  t_string v = luaW_checktstring(L, 1);
568  gui2::styled_widget* c = dynamic_cast<gui2::styled_widget*>(w);
569  if(!c) {
570  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
571  }
572  c->set_label(v);
573  }
574 
575  return 0;
576 }
577 /**
578  * Sets the value of a widget on the current dialog.
579  * - Arg 1: text.
580  * - Args 2..n: path of strings and integers.
581  */
583 {
584  t_string v = luaW_checktstring(L, 1);
585  gui2::widget *w = find_widget(L, 2, false);
586  gui2::styled_widget* c = dynamic_cast<gui2::styled_widget*>(w);
587  if(!c) {
588  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
589  }
590  c->set_tooltip(v);
591 
592  return 0;
593 }
594 
595 /**
596  * Gets the value of a widget on the current dialog.
597  * - Args 1..n: path of strings and integers.
598  * - Ret 1: scalar.
599  */
601 {
602  gui2::widget *w = find_widget(L, 1, true);
603  int num_rets = 1;
604 
605  if(gui2::listbox* list = dynamic_cast<gui2::listbox*>(w))
606  {
607  lua_pushinteger(L, list->get_selected_row() + 1);
608  num_rets = 2;
609  lua_newtable(L);
610  int count = 0;
611  for(unsigned i = 0; i < list->get_item_count(); i++) {
612  if(list->row_selected(i)) {
613  count++;
614  lua_pushnumber(L, i + 1);
615  lua_rawseti(L, -2, count);
616  }
617  }
618  } else if(gui2::multi_page* multi_page = dynamic_cast<gui2::multi_page*>(w)) {
619  lua_pushinteger(L, multi_page->get_selected_page() + 1);
620  } else if(gui2::selectable_item* selectable = dynamic_cast<gui2::selectable_item*>(w)) {
621  if(selectable->num_states() == 2) {
622  lua_pushboolean(L, selectable->get_value_bool());
623  } else {
624  lua_pushinteger(L, selectable->get_value() + 1);
625  }
626  } else if(gui2::text_box* text_box = dynamic_cast<gui2::text_box*>(w)) {
627  lua_pushstring(L, text_box->get_value().c_str());
628  } else if(gui2::slider* slider = dynamic_cast<gui2::slider*>(w)) {
629  lua_pushinteger(L, slider->get_value());
630  } else if(gui2::progress_bar* progress_bar = dynamic_cast<gui2::progress_bar*>(w)) {
631  lua_pushinteger(L, progress_bar->get_percentage());
632  } else if(gui2::tree_view* tree_view = dynamic_cast<gui2::tree_view*>(w)) {
633  std::vector<int> path = tree_view->selected_item()->describe_path();
634  lua_createtable(L, path.size(), 0);
635  for(std::size_t i = 0; i < path.size(); ++i) {
636  lua_pushinteger(L, path[i] + 1);
637  lua_rawseti(L, -2, i + 1);
638  }
639  } else if(gui2::stacked_widget* stacked_widget = dynamic_cast<gui2::stacked_widget*>(w)) {
640  lua_pushinteger(L, stacked_widget->current_layer());
641  num_rets = 2;
642  lua_newtable(L);
643  int count = 0;
644  for(unsigned i = 0; i < stacked_widget->get_layer_count(); i++) {
645  if(stacked_widget->layer_selected(i)) {
646  count++;
647  lua_pushnumber(L, i + 1);
648  lua_rawseti(L, -2, count);
649  }
650  }
651  } else if(gui2::multimenu_button* menu = dynamic_cast<gui2::multimenu_button*>(w)) {
652  auto selected = menu->get_toggle_states();
653  int count = 0;
654  lua_newtable(L);
655  for(unsigned i = 0; i < selected.size(); i++) {
656  if(selected[i]) {
657  count++;
658  lua_pushnumber(L, i + 1);
659  lua_rawseti(L, -2, count);
660  }
661  }
662  } else {
663  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
664  }
665 
666  return num_rets;
667 }
668 namespace
669 {
670  void remove_treeview_node(gui2::tree_view_node& node, std::size_t pos, int number)
671  {
672  //Not tested yet.
673  gui2::tree_view& tv = node.get_tree_view();
674  if(pos >= node.count_children()) {
675  return;
676  }
677  if(number <= 0 || number + pos > node.count_children()) {
678  number = node.count_children() - pos;
679  }
680  for(int i = 0; i < number; ++i) {
681  tv.remove_node(&node.get_child_at(pos));
682  }
683  }
684 }
685 /**
686  * Removes an entry from a list.
687  * - Arg 1: number, index of the element to delete.
688  * - Arg 2: number, number of the elements to delete. (0 to delete all elements after index)
689  * - Args 2..n: path of strings and integers.
690  */
692 {
693  int pos = luaL_checkinteger(L, 1) - 1;
694  int number = luaL_checkinteger(L, 2);
695  gui2::widget* w = find_widget(L, 3, true);
696 
697  if(gui2::listbox* list = dynamic_cast<gui2::listbox*>(w))
698  {
699  list->remove_row(pos, number);
700  } else if(gui2::multi_page* multi_page = dynamic_cast<gui2::multi_page*>(w)) {
701  multi_page->remove_page(pos, number);
702  } else if(gui2::tree_view* tree_view = dynamic_cast<gui2::tree_view*>(w)) {
703  remove_treeview_node(tree_view->get_root_node(), pos, number);
704  } else if(gui2::tree_view_node* tree_view_node = dynamic_cast<gui2::tree_view_node*>(w)) {
705  remove_treeview_node(*tree_view_node, pos, number);
706  } else {
707  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
708  }
709 
710  return 1;
711 }
712 
713 namespace { // helpers of intf_set_dialog_callback()
714  void dialog_callback(gui2::widget& w)
715  {
716  int cb;
717  {
718  scoped_dialog::callback_map &m = scoped_dialog::current->callbacks;
719  scoped_dialog::callback_map::const_iterator i = m.find(&w);
720  if(i == m.end()) {
721  return;
722  }
723  cb = i->second;
724  }
725  lua_State *L = scoped_dialog::current->L;
728  lua_rawgeti(L, -1, cb);
729  lua_remove(L, -2);
730  lua_call(L, 0, 0);
731  }
732 
733  /** Helper struct for intf_set_dialog_callback. */
734  struct dialog_callback_wrapper
735  {
736  void forward(gui2::widget* widget, bool& handled, bool& halt)
737  {
738  assert(widget);
739  dialog_callback(*widget);
740  handled = true;
741  halt = true;
742  }
743  };
744 }//unnamed namespace for helpers of intf_set_dialog_callback()
745 
746 /**
747  * Sets a callback on a widget of the current dialog.
748  * - Arg 1: function.
749  * - Args 2..n: path of strings and integers.
750  */
752 {
753  gui2::widget* w = find_widget(L, 2, true);
754 
755  scoped_dialog::callback_map &m = scoped_dialog::current->callbacks;
757  if(i != m.end()) {
760  lua_pushnil(L);
761  lua_rawseti(L, -2, i->second);
762  lua_pop(L, 1);
763  m.erase(i);
764  }
765 
766  if(lua_isnil(L, 1)) {
767  return 0;
768  }
769 
770  if(gui2::clickable_item* c = dynamic_cast<gui2::clickable_item*>(w)) {
771  static dialog_callback_wrapper wrapper;
772  c->connect_click_handler(std::bind(&dialog_callback_wrapper::forward, wrapper, w, _3, _4));
773  } else if(gui2::selectable_item* si = dynamic_cast<gui2::selectable_item*>(w)) {
774  connect_signal_notify_modified(dynamic_cast<gui2::widget&>(*si), std::bind(dialog_callback, _1));
775  } else if(gui2::integer_selector* is = dynamic_cast<gui2::integer_selector*>(w)) {
776  connect_signal_notify_modified(dynamic_cast<gui2::widget&>(*is), std::bind(dialog_callback, _1));
777  } else if(gui2::listbox* l = dynamic_cast<gui2::listbox*>(w)) {
778  static dialog_callback_wrapper wrapper;
779  connect_signal_notify_modified(*l, std::bind(&dialog_callback_wrapper::forward, wrapper, w, _3, _4));
780  } else if(gui2::tree_view* tv = dynamic_cast<gui2::tree_view*>(w)) {
781  tv->set_selection_change_callback(&dialog_callback);
782  } else {
783  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
784  }
785 
788  int n = lua_rawlen(L, -1) + 1;
789  m[w] = n;
790  lua_pushvalue(L, 1);
791  lua_rawseti(L, -2, n);
792  lua_pop(L, 1);
793 
794  return 0;
795 }
796 
797 /**
798  * Enables/disables Pango markup on the label of a widget of the current dialog.
799  * - Arg 1: boolean.
800  * - Args 2..n: path of strings and integers.
801  */
803 {
804  bool b = luaW_toboolean(L, 1);
805  gui2::widget* w = find_widget(L, 2, true);
806  gui2::styled_widget* c = dynamic_cast<gui2::styled_widget*>(w);
807  if(!c) {
808  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
809  }
810 
811  c->set_use_markup(b);
812  return 0;
813 }
814 
815 /**
816  * Sets a canvas on a widget of the current dialog.
817  * - Arg 1: integer.
818  * - Arg 2: WML table.
819  * - Args 3..n: path of strings and integers.
820  */
822 {
823  int i = luaL_checkinteger(L, 1);
824  gui2::widget* w = find_widget(L, 3, true);
825  gui2::styled_widget* c = dynamic_cast<gui2::styled_widget*>(w);
826  if(!c) {
827  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
828  }
829 
830  std::vector<gui2::canvas> &cv = c->get_canvases();
831  if(i < 1 || static_cast<unsigned>(i) > cv.size()) {
832  return luaL_argerror(L, 1, "out of bounds");
833  }
834 
835  config cfg = luaW_checkconfig(L, 2);
836  cv[i - 1].set_cfg(cfg);
837  c->set_is_dirty(true);
838  return 0;
839 }
840 
841 /**
842  * Sets a widget to have the focus
843  * - Args 1..n: path of strings and integers.
844  */
846 {
847  gui2::widget* w = find_widget(L, 1, true);
848  scoped_dialog::current->window->keyboard_capture(w);
849  return 0;
850 }
851 
852 /**
853  * Sets a widget's state to active or inactive
854  * - Arg 1: boolean.
855  * - Args 2..n: path of strings and integers.
856  */
858 {
859  const bool b = luaW_toboolean(L, 1);
860  gui2::widget* w = find_widget(L, 2, true);
861  gui2::styled_widget* c = dynamic_cast<gui2::styled_widget*>(w);
862  if(!c) {
863  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
864  }
865 
866  c->set_active(b);
867  return 0;
868 }
869 
870 /**
871  * Sets the visiblity of a widget in the current dialog.
872  * - Arg 1: boolean.
873  * - Args 2..n: path of strings and integers.
874  */
876 {
877  typedef gui2::styled_widget::visibility visibility;
878 
879  visibility flag = visibility::visible;
880 
881  switch(lua_type(L, 1)) {
882  case LUA_TBOOLEAN:
883  flag = luaW_toboolean(L, 1)
884  ? visibility::visible
885  : visibility::invisible;
886  break;
887  case LUA_TSTRING:
888  {
889  const std::string& str = lua_tostring(L, 1);
890  if(str == "visible") {
891  flag = visibility::visible;
892  } else if(str == "hidden") {
893  flag = visibility::hidden;
894  } else if(str == "invisible") {
895  flag = visibility::invisible;
896  } else {
897  return luaL_argerror(L, 1, "string must be one of: visible, hidden, invisible");
898  }
899  }
900  break;
901  default:
902  return luaW_type_error(L, 1, "boolean or string");
903  }
904 
905  gui2::widget* w = find_widget(L, 2, true);
906  gui2::styled_widget* c = dynamic_cast<gui2::styled_widget*>(w);
907  if(!c) {
908  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
909  }
910 
911  c->set_visible(flag);
912 
913  if(flag == visibility::hidden) {
914  // HACK: this is needed to force the widget to be repainted immediately
915  // to get rid of its ghost image.
916  scoped_dialog::current->window->invalidate_layout();
917  }
918 
919  return 0;
920 }
921 
923 {
925  return 0;
926 }
927 
928 int show_gamestate_inspector(const vconfig& cfg, const game_data& data, const game_state& state)
929 {
930  gui2::dialogs::gamestate_inspector inspect_dialog(data.get_variables(), *state.events_manager_, state.board_, cfg["name"]);
931  inspect_dialog.show();
932  return 0;
933 }
934 
935 /**
936  * Sets a widget's state to active or inactive
937  * - Arg 1: string, the type (id of [node_definition]) of the new node.
938  * - Arg 3: integer, where to insert the new node.
939  * - Args 3..n: path of strings and integers.
940  */
941 
943 {
944  const std::string node_type = luaL_checkstring(L, 1);
945  const int insert_pos = luaL_checkinteger(L, 2);
946  static const std::map<std::string, string_map> data;
947  gui2::widget* w = find_widget(L, 3, false);
948 
949  if(gui2::tree_view_node* twn = dynamic_cast<gui2::tree_view_node*>(w)) {
950  twn->add_child(node_type, data, insert_pos);
951  } else if(gui2::tree_view* tw = dynamic_cast<gui2::tree_view*>(w)) {
952  tw->get_root_node().add_child(node_type, data, insert_pos);
953  } else if(gui2::multi_page* mp = dynamic_cast<gui2::multi_page*>(w)) {
954  mp->add_page(node_type, insert_pos, data);
955  } else {
956  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
957  }
958  return 0;
959 }
960 
961 /**
962  * - Arg 1: string, widget type
963  * - Arg 3: string, id
964  * - Arg 3: config,
965  */
966 
968 {
969  std::string type = luaL_checkstring(L, 1);
970  std::string id = luaL_checkstring(L, 2);
971  try {
973  lua_kernel_base::get_lua_kernel<lua_kernel_base>(L).add_widget_definition(type, id);
974  }
975  } catch(const std::invalid_argument& e) {
976  return luaL_argerror(L, 1, e.what());
977  }
978  return 0;
979 }
980 } // end namespace lua_gui2
int intf_set_dialog_active(lua_State *L)
Sets a widget&#39;s state to active or inactive.
Definition: lua_gui2.cpp:857
#define lua_isnoneornil(L, n)
Definition: lua.h:359
Parameter pack for message portrait.
Small abstract helper class.
LUA_API void lua_createtable(lua_State *L, int narray, int nrec)
Definition: lapi.cpp:684
void show_message(const std::string &title, const std::string &msg, const std::string &button_caption, const bool auto_close, const bool message_use_markup, const bool title_use_markup)
Shows a message to the user.
Definition: message.cpp:152
int show_lua_console(lua_State *, lua_kernel_base *lk)
Definition: lua_gui2.cpp:922
bool text_input_was_specified
True when [text_input] appeared in [message].
int intf_add_dialog_tree_node(lua_State *L)
Sets a widget&#39;s state to active or inactive.
Definition: lua_gui2.cpp:942
static void display(const std::string &scenario_name, const config &story)
int chosen_option
The initially chosen option.
std::map< std::string, t_string > string_map
int show_story(lua_State *L)
Displays a story screen.
Definition: lua_gui2.cpp:377
LUA_API int lua_type(lua_State *L, int idx)
Definition: lapi.cpp:251
This class represents a single unit of a specific type.
Definition: unit.hpp:99
int dummy
Definition: lstrlib.cpp:1125
virtual widget * find(const std::string &id, const bool must_be_active)
Returns a widget with the wanted id.
Definition: widget.cpp:581
int luaW_type_error(lua_State *L, int narg, const char *tname)
std::string caption
The caption for the optional input text box.
LUA_API void lua_pushboolean(lua_State *L, int b)
Definition: lapi.cpp:557
LUA_API int lua_rawgeti(lua_State *L, int idx, lua_Integer n)
Definition: lapi.cpp:658
const std::string & icon() const
Definition: old_markup.hpp:44
std::string text
The initial text value.
static l_noret error(LoadState *S, const char *why)
Definition: lundump.cpp:39
bool has_attribute(config_key_type key) const
Definition: config.cpp:213
int show_dialog(lua_State *L)
Displays a window.
Definition: lua_gui2.cpp:234
LUA_API void lua_rawseti(lua_State *L, int idx, lua_Integer n)
Definition: lapi.cpp:817
LUA_API int lua_gettop(lua_State *L)
Definition: lapi.cpp:167
This file contains the window object, this object is a top level container which has the event manage...
void show_transient_message(const std::string &title, const std::string &message, const std::string &image, const bool message_use_markup, const bool title_use_markup, const bool restore_background)
Shows a transient message to the user.
Base class for all widgets.
Definition: widget.hpp:47
#define lua_remove(L, idx)
Definition: lua.h:371
button_style
Selects the style of the buttons to be shown.
Definition: message.hpp:68
static gui2::widget * find_widget(lua_State *L, int i, bool readonly)
Definition: lua_gui2.cpp:113
#define lua_tointeger(L, i)
Definition: lua.h:342
Parameter pack for message list input options.
LUA_API int lua_rawget(lua_State *L, int idx)
Definition: lapi.cpp:647
unsigned maximum_length
The maximum length of the text.
static void msg(const char *act, debug_info &i, const char *to="", const char *result="")
Definition: debugger.cpp:109
const std::vector< std::string > items
const config & get_variables() const
Definition: game_data.hpp:35
int show_message_dialog(lua_State *L)
Displays a message window.
Definition: lua_gui2.cpp:265
int intf_remove_dialog_item(lua_State *L)
Removes an entry from a list.
Definition: lua_gui2.cpp:691
Simple push button.
widget * find(const std::string &id, const bool must_be_active) override
See widget::find.
int intf_set_dialog_markup(lua_State *L)
Enables/disables Pango markup on the label of a widget of the current dialog.
Definition: lua_gui2.cpp:802
bool show(const unsigned auto_close_time=0)
Shows the window.
Small abstract helper class.
Definitions for the interface to Wesnoth Markup Language (WML).
int intf_add_widget_definition(lua_State *L)
Definition: lua_gui2.cpp:967
std::pair< tree_view_node::ptr_t, int > remove_node(tree_view_node *node)
Removes the given node as a child of its parent node.
Definition: tree_view.cpp:69
A single unit type that the player may recruit.
Definition: types.hpp:42
const std::unique_ptr< game_events::manager > events_manager_
Definition: game_state.hpp:52
Class for a single line text area.
Definition: text_box.hpp:121
#define lua_pop(L, n)
Definition: lua.h:344
int intf_set_dialog_focus(lua_State *L)
Sets a widget to have the focus.
Definition: lua_gui2.cpp:845
static void display(lua_kernel_base *lk)
Display a new console, using given video and lua kernel.
std::size_t count_children() const
The number of children in this widget.
#define b
Pubic entry points for the MP workflow.
Definition: lobby_data.cpp:49
virtual void set_label(const t_string &label)
The listbox class.
Definition: listbox.hpp:40
const config & options()
Definition: game.cpp:568
#define LUA_TSTRING
Definition: lua.h:68
int show_gamestate_inspector(const vconfig &cfg, const game_data &data, const game_state &state)
Definition: lua_gui2.cpp:928
LUALIB_API int luaL_argerror(lua_State *L, int arg, const char *extramsg)
Definition: lauxlib.cpp:164
void set_tooltip(const t_string &tooltip)
void connect_signal_notify_modified(dispatcher &dispatcher, const signal_notification_function &signal)
Connects a signal handler for getting a notification upon modification.
Definition: dispatcher.cpp:248
bool luaW_toboolean(lua_State *L, int n)
Definition: lua_common.cpp:870
unit * luaW_tounit(lua_State *L, int index, bool only_on_map)
Converts a Lua value to a unit pointer.
Definition: lua_unit.cpp:143
void set_visible(const visibility visible)
Definition: widget.cpp:473
void set_is_dirty(const bool is_dirty)
Definition: widget.cpp:463
t_string luaW_checktstring(lua_State *L, int index)
Converts a scalar to a translatable string.
Definition: lua_common.cpp:602
Used by the menu_button widget.
std::string selected
This file contains the canvas object which is the part where the widgets draw (temporally) images on...
int intf_set_dialog_tooltip(lua_State *L)
Sets the value of a widget on the current dialog.
Definition: lua_gui2.cpp:582
LUA_API const char * lua_pushlstring(lua_State *L, const char *s, size_t len)
Definition: lapi.cpp:479
std::string path
Definition: game_config.cpp:39
int show_menu(lua_State *L)
Displays a popup menu at the current mouse position Best used from a [set_menu_item], to show a submenu.
Definition: lua_gui2.cpp:390
bool is_default() const
Definition: old_markup.hpp:59
std::vector< canvas > & get_canvases()
virtual void set_use_markup(bool use_markup)
int intf_set_dialog_canvas(lua_State *L)
Sets a canvas on a widget of the current dialog.
Definition: lua_gui2.cpp:821
#define lua_newtable(L)
Definition: lua.h:346
LUA_API void lua_pushnil(lua_State *L)
Definition: lapi.cpp:450
LUA_API void lua_pushnumber(lua_State *L, lua_Number n)
Definition: lapi.cpp:458
LUALIB_API void luaL_checktype(lua_State *L, int arg, int t)
Definition: lauxlib.cpp:378
lu_byte right
Definition: lparser.cpp:1027
const unit_type * luaW_tounittype(lua_State *L, int idx)
Test if a stack element is a unit type, and return it if so.
#define lua_isboolean(L, n)
Definition: lua.h:356
static lg::log_domain log_scripting_lua("scripting/lua")
bool luaW_toconfig(lua_State *L, int index, config &cfg)
Converts an optional table or vconfig to a config object.
Definition: lua_common.cpp:724
int show_popup_dialog(lua_State *L)
Displays a popup message.
Definition: lua_gui2.cpp:363
bool luaW_totstring(lua_State *L, int index, t_string &str)
Converts a scalar to a translatable string.
Definition: lua_common.cpp:577
#define lua_isnil(L, n)
Definition: lua.h:355
std::size_t i
Definition: function.cpp:933
int intf_set_dialog_value(lua_State *L)
Sets the value of a widget on the current dialog.
Definition: lua_gui2.cpp:453
LUALIB_API lua_Integer luaL_checkinteger(lua_State *L, int arg)
Definition: lauxlib.cpp:430
int show_wml_message(const std::string &title, const std::string &message, const wml_message_portrait *left, const wml_message_portrait *right, const wml_message_options &options, const wml_message_input &input)
Helper function to show a portrait.
LUA_API void lua_rawset(lua_State *L, int idx)
Definition: lapi.cpp:801
tree_view_node & get_child_at(int index)
config luaW_checkconfig(lua_State *L, int index)
Converts an optional table or vconfig to a config object.
Definition: lua_common.cpp:799
window * get_window()
Get the parent window.
Definition: widget.cpp:114
window * build(const builder_window::window_resolution *definition)
Builds a window.
Helper class for message options.
Definition: wml_message.hpp:27
lua_check_impl::remove_constref< T > lua_check(lua_State *L, int n)
Definition: push_check.hpp:357
const std::string & label() const
Definition: old_markup.hpp:49
#define lua_tostring(L, i)
Definition: lua.h:366
visibility
Visibility settings done by the user.
Definition: widget.hpp:57
int w
LUA_API void lua_pushvalue(lua_State *L, int idx)
Definition: lapi.cpp:237
Base class for all visible items.
LUA_API int lua_isnumber(lua_State *L, int idx)
Definition: lapi.cpp:276
#define lua_call(L, n, r)
Definition: lua.h:274
LUALIB_API int luaL_error(lua_State *L, const char *fmt,...)
Definition: lauxlib.cpp:223
static const char dlgclbkKey[]
Definition: lua_gui2.cpp:69
int intf_set_dialog_callback(lua_State *L)
Sets a callback on a widget of the current dialog.
Definition: lua_gui2.cpp:751
tree_view & get_tree_view()
LUA_API size_t lua_rawlen(lua_State *L, int idx)
Definition: lapi.cpp:392
#define LUA_REGISTRYINDEX
Definition: lua.h:42
std::vector< wml_message_option > option_list
A list of options to select in the dialog.
The multi page class.
Definition: multi_page.hpp:35
#define lua_istable(L, n)
Definition: lua.h:353
virtual void set_active(const bool active)=0
Sets the styled_widget&#39;s state.
map_location prev
Definition: astarsearch.cpp:65
lu_byte left
Definition: lparser.cpp:1026
A slider.
Definition: slider.hpp:33
game_board board_
Definition: game_state.hpp:46
A variable-expanding proxy for the config class.
Definition: variable.hpp:42
this module manages the cache of images.
Standard logging facilities (interface).
Implements simple parsing of legacy GUI1 item markup.
Definition: old_markup.hpp:25
bool add_single_widget_definition(const std::string &widget_type, const std::string &definition_id, const config &cfg)
Adds a widget definition to the default GUI.
Small concept class.
void invalidate_layout()
Updates the size of the window.
Definition: window.cpp:806
#define e
int intf_get_dialog_value(lua_State *L)
Gets the value of a widget on the current dialog.
Definition: lua_gui2.cpp:600
Dialog was closed with the OK button.
Definition: retval.hpp:34
int show_message_box(lua_State *L)
Displays a simple message box.
Definition: lua_gui2.cpp:416
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:92
mock_char c
const std::string & str() const
Definition: tstring.hpp:186
LUA_API int lua_getfield(lua_State *L, int idx, const char *k)
Definition: lapi.cpp:622
#define LUA_TTABLE
Definition: lua.h:69
static map_location::DIRECTION n
LUA_API void lua_pushinteger(lua_State *L, lua_Integer n)
Definition: lapi.cpp:466
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:63
#define luaL_optstring(L, n, d)
Definition: lauxlib.h:125
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
LUA_API const char * lua_pushstring(lua_State *L, const char *s)
Definition: lapi.cpp:491
bool empty() const
Definition: config.cpp:884
#define LUA_TBOOLEAN
Definition: lua.h:65
int intf_set_dialog_visible(lua_State *L)
Sets the visiblity of a widget in the current dialog.
Definition: lua_gui2.cpp:875
LUA_API const char * lua_typename(lua_State *L, int t)
Definition: lapi.cpp:257
Parameter pack for message text input options.
#define luaL_checkstring(L, n)
Definition: lauxlib.h:124
const std::string & description() const
Definition: old_markup.hpp:54