The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
drop_down_list.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
18 #include "gui/widgets/listbox.hpp"
20 #include "gui/dialogs/dialog.hpp"
22 #include "gui/widgets/window.hpp"
23 #include "gui/widgets/settings.hpp"
24 
25 #include "utils/functional.hpp"
26 
27 namespace gui2
28 {
29 
30 REGISTER_DIALOG(drop_down_list)
31 
32 namespace {
33  void click_callback(twindow& window, bool& handled, bool& halt, tpoint coordinate)
34  {
35  SDL_Rect rect = window.get_rectangle();
36  if(coordinate.x < rect.x || coordinate.x > rect.x + rect.w || coordinate.y < rect.y || coordinate.y > rect.y + rect.h ) {
37  halt = handled = true;
38  window.set_retval(twindow::CANCEL);
39  window.close();
40  }
41  }
42  void resize_callback(twindow& window)
43  {
44  window.set_retval(twindow::CANCEL);
45  window.close();
46  }
47 }
48 
50 {
51  window.set_variable("button_x", variant(button_pos_.x));
52  window.set_variable("button_y", variant(button_pos_.y));
53  window.set_variable("button_w", variant(button_pos_.w));
54  window.set_variable("button_h", variant(button_pos_.h));
55  tlistbox& list = find_widget<tlistbox>(&window, "list", true);
56  std::map<std::string, string_map> data;
57  t_string& label = data["label"]["label"];
58  t_string& use_markup = data["label"]["use_markup"];
59  for(const auto& str : items_) {
60  label = str;
61  use_markup = use_markup_ ? "true" : "false";
62  list.add_row(data);
63  }
65 
66  list.set_callback_item_change(std::bind(&tdrop_down_list::item_change_callback, this, std::ref(window), _1));
67  //Dismiss on click outside the window
68  window.connect_signal<event::SDL_LEFT_BUTTON_UP>(std::bind(&click_callback, std::ref(window), _3, _4, _5), event::tdispatcher::front_child);
69  //Dismiss on resize
70  window.connect_signal<event::SDL_VIDEO_RESIZE>(std::bind(&resize_callback, std::ref(window)), event::tdispatcher::front_child);
71 }
72 
74 {
75  selected_item_ = item;
76  window.set_retval(twindow::OK);
77  window.close();
78 }
80 {
81 }
82 
83 } // namespace gui2
std::vector< std::string > items_
void item_change_callback(twindow &window, size_t item)
This file contains the window object, this object is a top level container which has the event manage...
bool select_row(const unsigned row, const bool select=true)
Selectes a row.
Definition: listbox.cpp:228
REGISTER_DIALOG(label_settings)
SDL_Rect button_pos_
The screen location of the combobox button that triggred this droplist.
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
base class of top level items, the only item which needs to store the final canvases to draw on ...
Definition: window.hpp:62
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
Dialog is closed with ok button.
Definition: window.hpp:125
A SDL resize request, coordinate is the new window size.
Definition: handler.hpp:59
This file contains the settings handling of the widget library.
void add_row(const string_map &item, const int index=-1)
When an item in the list is selected by the user we need to update the state.
Definition: listbox.cpp:74
Dialog is closed with the cancel button.
Definition: window.hpp:126
void set_variable(const std::string &key, const variant &value)
Definition: window.hpp:449
void pre_show(twindow &window)
Inherited from tdialog.
void set_retval(const int retval, const bool close_window=true)
Sets there return value of the window.
Definition: window.hpp:422
A SDL left mouse button up event.
Definition: handler.hpp:74
void post_show(twindow &window)
Inherited from tdialog.
void close()
Requests to close the window.
Definition: window.hpp:235
The listbox class.
Definition: listbox.hpp:39
GLenum GLint ref
Definition: glew.h:1813
void set_callback_item_change(const std::function< void(size_t)> &callback)
Definition: listbox.hpp:219