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