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