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)) {
494  const t_string& text = luaW_checktstring(L, 1);
495  text_box->set_value(text.str());
496  } else if (gui2::slider* slider = dynamic_cast<gui2::slider*>(w)) {
497  const int v = luaL_checkinteger(L, 1);
498  const int m = slider->get_minimum_value();
499  const int n = slider->get_maximum_value();
500  if(m <= v && v <= n) {
501  slider->set_value(v);
502  } else {
503  return luaL_argerror(L, 1, "out of bounds");
504  }
505  } else if(gui2::progress_bar* progress_bar = dynamic_cast<gui2::progress_bar*>(w)) {
506  const int v = luaL_checkinteger(L, 1);
507  if(0 <= v && v <= 100) {
508  progress_bar->set_percentage(v);
509  } else {
510  return luaL_argerror(L, 1, "out of bounds");
511  }
512  } else if(gui2::stacked_widget* stacked_widget = dynamic_cast<gui2::stacked_widget*>(w)) {
513  if(lua_istable(L, 1)) {
514  boost::dynamic_bitset<> states;
515  states.resize(stacked_widget->get_layer_count());
516  for(unsigned i : lua_check<std::vector<unsigned>>(L, 1)) {
517  if(i > 0 && i <= stacked_widget->get_layer_count()) {
518  states[i] = true;
519  }
520  }
521  stacked_widget->select_layers(states);
522  } else {
523  const int v = luaL_checkinteger(L, 1);
524  const int n = stacked_widget->get_layer_count();
525  if(v >= 0 && v <= n) {
526  stacked_widget->select_layer(v - 1);
527  }
528  }
529  } else if(gui2::unit_preview_pane* unit_preview_pane = dynamic_cast<gui2::unit_preview_pane*>(w)) {
530  if(const unit_type* ut = luaW_tounittype(L, 1)) {
531  unit_preview_pane->set_displayed_type(*ut);
532  } else if(unit* u = luaW_tounit(L, 1)) {
533  unit_preview_pane->set_displayed_unit(*u);
534  } else {
535  return luaW_type_error(L, 1, "unit or unit type");
536  }
537  } else if(gui2::tree_view_node* node = dynamic_cast<gui2::tree_view_node*>(w)) {
538  const bool unfolded = luaW_toboolean(L, 1);
539  if(unfolded) {
540  node->unfold();
541  } else {
542  node->fold();
543  }
544  } else if(gui2::multimenu_button* menu = dynamic_cast<gui2::multimenu_button*>(w)) {
545  if(lua_istable(L, 1)) {
546  boost::dynamic_bitset<> states;
547  states.resize(menu->num_options());
548  for(unsigned i : lua_check<std::vector<unsigned>>(L, 1)) {
549  if(i > 0 && i <= menu->num_options()) {
550  states[i] = true;
551  }
552  }
553  menu->select_options(states);
554  } else {
555  int v = luaL_checkinteger(L, 1);
556  int n = menu->num_options();
557  if(1 <= v && v <= n) {
558  menu->select_option(v - 1);
559  } else {
560  return luaL_argerror(L, 1, "out of bounds");
561  }
562  }
563  } else {
564  t_string v = luaW_checktstring(L, 1);
565  gui2::styled_widget* c = dynamic_cast<gui2::styled_widget*>(w);
566  if(!c) {
567  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
568  }
569  c->set_label(v);
570  }
571 
572  return 0;
573 }
574 
575 /**
576  * Gets the value of a widget on the current dialog.
577  * - Args 1..n: path of strings and integers.
578  * - Ret 1: scalar.
579  */
581 {
582  gui2::widget *w = find_widget(L, 1, true);
583  int num_rets = 1;
584 
585  if(gui2::listbox* list = dynamic_cast<gui2::listbox*>(w))
586  {
587  lua_pushinteger(L, list->get_selected_row() + 1);
588  num_rets = 2;
589  lua_newtable(L);
590  int count = 0;
591  for(unsigned i = 0; i < list->get_item_count(); i++) {
592  if(list->row_selected(i)) {
593  count++;
594  lua_pushnumber(L, i + 1);
595  lua_rawseti(L, -2, count);
596  }
597  }
598  } else if(gui2::multi_page* multi_page = dynamic_cast<gui2::multi_page*>(w)) {
599  lua_pushinteger(L, multi_page->get_selected_page() + 1);
600  } else if(gui2::selectable_item* selectable = dynamic_cast<gui2::selectable_item*>(w)) {
601  if(selectable->num_states() == 2) {
602  lua_pushboolean(L, selectable->get_value_bool());
603  } else {
604  lua_pushinteger(L, selectable->get_value() + 1);
605  }
606  } else if(gui2::text_box* text_box = dynamic_cast<gui2::text_box*>(w)) {
607  lua_pushstring(L, text_box->get_value().c_str());
608  } else if(gui2::slider* slider = dynamic_cast<gui2::slider*>(w)) {
609  lua_pushinteger(L, slider->get_value());
610  } else if(gui2::progress_bar* progress_bar = dynamic_cast<gui2::progress_bar*>(w)) {
611  lua_pushinteger(L, progress_bar->get_percentage());
612  } else if(gui2::tree_view* tree_view = dynamic_cast<gui2::tree_view*>(w)) {
613  std::vector<int> path = tree_view->selected_item()->describe_path();
614  lua_createtable(L, path.size(), 0);
615  for(std::size_t i = 0; i < path.size(); ++i) {
616  lua_pushinteger(L, path[i] + 1);
617  lua_rawseti(L, -2, i + 1);
618  }
619  } else if(gui2::stacked_widget* stacked_widget = dynamic_cast<gui2::stacked_widget*>(w)) {
620  lua_pushinteger(L, stacked_widget->current_layer());
621  num_rets = 2;
622  lua_newtable(L);
623  int count = 0;
624  for(unsigned i = 0; i < stacked_widget->get_layer_count(); i++) {
625  if(stacked_widget->layer_selected(i)) {
626  count++;
627  lua_pushnumber(L, i + 1);
628  lua_rawseti(L, -2, count);
629  }
630  }
631  } else if(gui2::multimenu_button* menu = dynamic_cast<gui2::multimenu_button*>(w)) {
632  auto selected = menu->get_toggle_states();
633  int count = 0;
634  lua_newtable(L);
635  for(unsigned i = 0; i < selected.size(); i++) {
636  if(selected[i]) {
637  count++;
638  lua_pushnumber(L, i + 1);
639  lua_rawseti(L, -2, count);
640  }
641  }
642  } else {
643  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
644  }
645 
646  return num_rets;
647 }
648 namespace
649 {
650  void remove_treeview_node(gui2::tree_view_node& node, std::size_t pos, int number)
651  {
652  //Not tested yet.
653  gui2::tree_view& tv = node.get_tree_view();
654  if(pos >= node.count_children()) {
655  return;
656  }
657  if(number <= 0 || number + pos > node.count_children()) {
658  number = node.count_children() - pos;
659  }
660  for(int i = 0; i < number; ++i) {
661  tv.remove_node(&node.get_child_at(pos));
662  }
663  }
664 }
665 /**
666  * Removes an entry from a list.
667  * - Arg 1: number, index of the element to delete.
668  * - Arg 2: number, number of the elements to delete. (0 to delete all elements after index)
669  * - Args 2..n: path of strings and integers.
670  */
672 {
673  int pos = luaL_checkinteger(L, 1) - 1;
674  int number = luaL_checkinteger(L, 2);
675  gui2::widget* w = find_widget(L, 3, true);
676 
677  if(gui2::listbox* list = dynamic_cast<gui2::listbox*>(w))
678  {
679  list->remove_row(pos, number);
680  } else if(gui2::multi_page* multi_page = dynamic_cast<gui2::multi_page*>(w)) {
681  multi_page->remove_page(pos, number);
682  } else if(gui2::tree_view* tree_view = dynamic_cast<gui2::tree_view*>(w)) {
683  remove_treeview_node(tree_view->get_root_node(), pos, number);
684  } else if(gui2::tree_view_node* tree_view_node = dynamic_cast<gui2::tree_view_node*>(w)) {
685  remove_treeview_node(*tree_view_node, pos, number);
686  } else {
687  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
688  }
689 
690  return 1;
691 }
692 
693 namespace { // helpers of intf_set_dialog_callback()
694  void dialog_callback(gui2::widget& w)
695  {
696  int cb;
697  {
698  scoped_dialog::callback_map &m = scoped_dialog::current->callbacks;
699  scoped_dialog::callback_map::const_iterator i = m.find(&w);
700  if(i == m.end()) {
701  return;
702  }
703  cb = i->second;
704  }
705  lua_State *L = scoped_dialog::current->L;
708  lua_rawgeti(L, -1, cb);
709  lua_remove(L, -2);
710  lua_call(L, 0, 0);
711  }
712 
713  /** Helper struct for intf_set_dialog_callback. */
714  struct dialog_callback_wrapper
715  {
716  void forward(gui2::widget* widget)
717  {
718  assert(widget);
719  dialog_callback(*widget);
720  }
721  };
722 }//unnamed namespace for helpers of intf_set_dialog_callback()
723 
724 /**
725  * Sets a callback on a widget of the current dialog.
726  * - Arg 1: function.
727  * - Args 2..n: path of strings and integers.
728  */
730 {
731  gui2::widget* w = find_widget(L, 2, true);
732 
733  scoped_dialog::callback_map &m = scoped_dialog::current->callbacks;
735  if(i != m.end()) {
738  lua_pushnil(L);
739  lua_rawseti(L, -2, i->second);
740  lua_pop(L, 1);
741  m.erase(i);
742  }
743 
744  if(lua_isnil(L, 1)) {
745  return 0;
746  }
747 
748  if(gui2::clickable_item* c = dynamic_cast<gui2::clickable_item*>(w)) {
749  static dialog_callback_wrapper wrapper;
750  c->connect_click_handler(std::bind(&dialog_callback_wrapper::forward, wrapper, w));
751  } else if(gui2::selectable_item* si = dynamic_cast<gui2::selectable_item*>(w)) {
752  connect_signal_notify_modified(dynamic_cast<gui2::widget&>(*si), std::bind(dialog_callback, _1));
753  } else if(gui2::integer_selector* is = dynamic_cast<gui2::integer_selector*>(w)) {
754  connect_signal_notify_modified(dynamic_cast<gui2::widget&>(*is), std::bind(dialog_callback, _1));
755  } else if(gui2::listbox* l = dynamic_cast<gui2::listbox*>(w)) {
756  static dialog_callback_wrapper wrapper;
757  connect_signal_notify_modified(*l, std::bind(&dialog_callback_wrapper::forward, wrapper, w));
758  } else if(gui2::tree_view* tv = dynamic_cast<gui2::tree_view*>(w)) {
759  tv->set_selection_change_callback(&dialog_callback);
760  } else {
761  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
762  }
763 
766  int n = lua_rawlen(L, -1) + 1;
767  m[w] = n;
768  lua_pushvalue(L, 1);
769  lua_rawseti(L, -2, n);
770  lua_pop(L, 1);
771 
772  return 0;
773 }
774 
775 /**
776  * Enables/disables Pango markup on the label of a widget of the current dialog.
777  * - Arg 1: boolean.
778  * - Args 2..n: path of strings and integers.
779  */
781 {
782  bool b = luaW_toboolean(L, 1);
783  gui2::widget* w = find_widget(L, 2, true);
784  gui2::styled_widget* c = dynamic_cast<gui2::styled_widget*>(w);
785  if(!c) {
786  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
787  }
788 
789  c->set_use_markup(b);
790  return 0;
791 }
792 
793 /**
794  * Sets a canvas on a widget of the current dialog.
795  * - Arg 1: integer.
796  * - Arg 2: WML table.
797  * - Args 3..n: path of strings and integers.
798  */
800 {
801  int i = luaL_checkinteger(L, 1);
802  gui2::widget* w = find_widget(L, 3, true);
803  gui2::styled_widget* c = dynamic_cast<gui2::styled_widget*>(w);
804  if(!c) {
805  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
806  }
807 
808  std::vector<gui2::canvas> &cv = c->get_canvases();
809  if(i < 1 || unsigned(i) > cv.size()) {
810  return luaL_argerror(L, 1, "out of bounds");
811  }
812 
813  config cfg = luaW_checkconfig(L, 2);
814  cv[i - 1].set_cfg(cfg);
815  c->set_is_dirty(true);
816  return 0;
817 }
818 
819 /**
820  * Sets a widget to have the focus
821  * - Args 1..n: path of strings and integers.
822  */
824 {
825  gui2::widget* w = find_widget(L, 1, true);
826  scoped_dialog::current->window->keyboard_capture(w);
827  return 0;
828 }
829 
830 /**
831  * Sets a widget's state to active or inactive
832  * - Arg 1: boolean.
833  * - Args 2..n: path of strings and integers.
834  */
836 {
837  const bool b = luaW_toboolean(L, 1);
838  gui2::widget* w = find_widget(L, 2, true);
839  gui2::styled_widget* c = dynamic_cast<gui2::styled_widget*>(w);
840  if(!c) {
841  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
842  }
843 
844  c->set_active(b);
845  return 0;
846 }
847 
848 /**
849  * Sets the visiblity of a widget in the current dialog.
850  * - Arg 1: boolean.
851  * - Args 2..n: path of strings and integers.
852  */
854 {
855  typedef gui2::styled_widget::visibility visibility;
856 
857  visibility flag = visibility::visible;
858 
859  switch(lua_type(L, 1)) {
860  case LUA_TBOOLEAN:
861  flag = luaW_toboolean(L, 1)
862  ? visibility::visible
863  : visibility::invisible;
864  break;
865  case LUA_TSTRING:
866  {
867  const std::string& str = lua_tostring(L, 1);
868  if(str == "visible") {
869  flag = visibility::visible;
870  } else if(str == "hidden") {
871  flag = visibility::hidden;
872  } else if(str == "invisible") {
873  flag = visibility::invisible;
874  } else {
875  return luaL_argerror(L, 1, "string must be one of: visible, hidden, invisible");
876  }
877  }
878  break;
879  default:
880  return luaW_type_error(L, 1, "boolean or string");
881  }
882 
883  gui2::widget* w = find_widget(L, 2, true);
884  gui2::styled_widget* c = dynamic_cast<gui2::styled_widget*>(w);
885  if(!c) {
886  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
887  }
888 
889  c->set_visible(flag);
890 
891  if(flag == visibility::hidden) {
892  // HACK: this is needed to force the widget to be repainted immediately
893  // to get rid of its ghost image.
894  scoped_dialog::current->window->invalidate_layout();
895  }
896 
897  return 0;
898 }
899 
901 {
903  return 0;
904 }
905 
906 int show_gamestate_inspector(const vconfig& cfg, const game_data& data, const game_state& state)
907 {
908  gui2::dialogs::gamestate_inspector inspect_dialog(data.get_variables(), *state.events_manager_, state.board_, cfg["name"]);
909  inspect_dialog.show();
910  return 0;
911 }
912 
913 /**
914  * Sets a widget's state to active or inactive
915  * - Arg 1: string, the type (id of [node_definition]) of the new node.
916  * - Arg 3: integer, where to insert the new node.
917  * - Args 3..n: path of strings and integers.
918  */
919 
921 {
922  const std::string node_type = luaL_checkstring(L, 1);
923  const int insert_pos = luaL_checkinteger(L, 2);
924  static const std::map<std::string, string_map> data;
925  gui2::widget* w = find_widget(L, 3, false);
926 
927  if(gui2::tree_view_node* twn = dynamic_cast<gui2::tree_view_node*>(w)) {
928  twn->add_child(node_type, data, insert_pos);
929  } else if(gui2::tree_view* tw = dynamic_cast<gui2::tree_view*>(w)) {
930  tw->get_root_node().add_child(node_type, data, insert_pos);
931  } else if(gui2::multi_page* mp = dynamic_cast<gui2::multi_page*>(w)) {
932  mp->add_page(node_type, insert_pos, data);
933  } else {
934  return luaL_argerror(L, lua_gettop(L), "unsupported widget");
935  }
936  return 0;
937 }
938 
939 /**
940  * - Arg 1: string, widget type
941  * - Arg 3: string, id
942  * - Arg 3: config,
943  */
944 
946 {
947  std::string type = luaL_checkstring(L, 1);
948  std::string id = luaL_checkstring(L, 2);
949  try {
951  lua_kernel_base::get_lua_kernel<lua_kernel_base>(L).add_widget_definition(type, id);
952  }
953  } catch(const std::invalid_argument& e) {
954  return luaL_argerror(L, 1, e.what());
955  }
956  return 0;
957 }
958 } // 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:835
#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:900
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:920
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:217
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:671
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:780
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:945
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:823
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:906
LUALIB_API int luaL_argerror(lua_State *L, int arg, const char *extramsg)
Definition: lauxlib.cpp:164
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:232
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...
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:799
#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 * 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:729
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.
#define e
int intf_get_dialog_value(lua_State *L)
Gets the value of a widget on the current dialog.
Definition: lua_gui2.cpp:580
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:886
#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:853
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