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