The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
helper.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2017 by Mark de Wever <koraq@xs4all.nl>
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 
19 #include "config.hpp"
20 #include "gui/core/log.hpp"
21 #include "gui/widgets/grid.hpp"
22 #include "gui/widgets/window.hpp"
23 
24 namespace gui2
25 {
26 
27 namespace implementation
28 {
29 
30 static std::map<std::string, scrollbar_mode> scrollbar_mode_map {
31  { "always", scrollbar_mode::ALWAYS_VISIBLE },
32  { "never", scrollbar_mode::ALWAYS_INVISIBLE },
33  { "auto", scrollbar_mode::AUTO_VISIBLE },
34  { "initial_auto", scrollbar_mode::AUTO_VISIBLE_FIRST_RUN },
35 };
36 
37 unsigned get_v_align(const std::string& v_align)
38 {
39  if(v_align == "top") {
41  } else if(v_align == "bottom") {
43  } else {
44  if(!v_align.empty() && v_align != "center") {
45  ERR_GUI_E << "Invalid vertical alignment '" << v_align
46  << "' falling back to 'center'.\n";
47  }
49  }
50 }
51 
52 unsigned get_h_align(const std::string& h_align)
53 {
54  if(h_align == "left") {
56  } else if(h_align == "right") {
58  } else {
59  if(!h_align.empty() && h_align != "center") {
60  ERR_GUI_E << "Invalid horizontal alignment '" << h_align
61  << "' falling back to 'center'.\n";
62  }
64  }
65 }
66 
67 unsigned get_border(const std::vector<std::string>& borders)
68 {
69  unsigned result = 0;
70  for(const auto & border : borders)
71  {
72  if(border == "all") {
73  return grid::BORDER_ALL;
74  } else if(border == "top") {
75  result |= grid::BORDER_TOP;
76  } else if(border == "bottom") {
77  result |= grid::BORDER_BOTTOM;
78  } else if(border == "left") {
79  result |= grid::BORDER_LEFT;
80  } else if(border == "right") {
81  result |= grid::BORDER_RIGHT;
82  }
83  }
84  return result;
85 }
86 
87 unsigned read_flags(const config& cfg)
88 {
89  unsigned flags = 0;
90 
91  const unsigned v_flags = get_v_align(cfg["vertical_alignment"]);
92  const unsigned h_flags = get_h_align(cfg["horizontal_alignment"]);
93  flags |= get_border(utils::split(cfg["border"]));
94 
95  if(cfg["vertical_grow"].to_bool()) {
97 
98  if(!(cfg["vertical_alignment"]).empty()) {
99  ERR_GUI_P << "vertical_grow and vertical_alignment "
100  "can't be combined, alignment is ignored.\n";
101  }
102  } else {
103  flags |= v_flags;
104  }
105 
106  if(cfg["horizontal_grow"].to_bool()) {
108 
109  if(!(cfg["horizontal_alignment"]).empty()) {
110  ERR_GUI_P << "horizontal_grow and horizontal_alignment "
111  "can't be combined, alignment is ignored.\n";
112  }
113  } else {
114  flags |= h_flags;
115  }
116 
117  return flags;
118 }
119 
121 {
122  if(scrollbar_mode.empty()) {
124  }
125 
126  if(scrollbar_mode_map.find(scrollbar_mode) == scrollbar_mode_map.end()) {
127  ERR_GUI_E << "Invalid scrollbar mode '" << scrollbar_mode << "'."
128  << "Falling back to 'initial_auto'." << std::endl;
129 
131  }
132 
134 }
135 
136 int get_retval(const std::string& retval_id,
137  const int retval,
138  const std::string& id)
139 {
140  if(!retval_id.empty()) {
141  int result = window::get_retval_by_id(retval_id);
142  if(result) {
143  return result;
144  } else {
145  ERR_GUI_E << "Window builder: retval_id '" << retval_id
146  << "' is unknown.\n";
147  }
148  }
149 
150  if(retval) {
151  return retval;
152  } else {
153  return window::get_retval_by_id(id);
154  }
155 }
156 
157 } // namespace implementation
158 
159 } // namespace gui2
Define the common log macros for the gui toolkit.
#define ERR_GUI_E
Definition: log.hpp:37
static const unsigned BORDER_TOP
Definition: grid.hpp:59
std::vector< char_t > string
Like AUTO_VISIBLE, but when not needed upon the initial layout phase, the bars are not shown and no s...
unsigned read_flags(const config &cfg)
Returns the placement/resize flags.
Definition: helper.cpp:87
This file contains the window object, this object is a top level container which has the event manage...
static const unsigned BORDER_ALL
Definition: grid.hpp:63
unsigned get_border(const std::vector< std::string > &borders)
Returns the border flags.
Definition: helper.cpp:67
int get_retval(const std::string &retval_id, const int retval, const std::string &id)
Returns the return value for a widget.
Definition: helper.cpp:136
Definitions for the interface to Wesnoth Markup Language (WML).
std::vector< std::string > split(const std::string &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
#define ERR_GUI_P
Definition: log.hpp:71
Generic file dialog.
Definition: text.hpp:37
static std::map< std::string, scrollbar_mode > scrollbar_mode_map
Definition: helper.cpp:31
static const unsigned HORIZONTAL_ALIGN_RIGHT
Definition: grid.hpp:56
static const unsigned BORDER_BOTTOM
Definition: grid.hpp:60
static const unsigned VERTICAL_ALIGN_TOP
Definition: grid.hpp:47
scrollbar_container::scrollbar_mode scrollbar_mode
Definition: helper.hpp:30
static const unsigned BORDER_RIGHT
Definition: grid.hpp:62
static const unsigned VERTICAL_ALIGN_CENTER
Definition: grid.hpp:48
static const unsigned HORIZONTAL_ALIGN_CENTER
Definition: grid.hpp:55
static const unsigned HORIZONTAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:53
static const unsigned VERTICAL_GROW_SEND_TO_CLIENT
Definition: grid.hpp:46
scrollbar_mode get_scrollbar_mode(const std::string &scrollbar_mode)
Returns the scrollbar mode flags.
Definition: helper.cpp:120
static std::map< std::string, scrollbar_mode > unsigned get_v_align(const std::string &v_align)
Returns the vertical alignment.
Definition: helper.cpp:37
static const unsigned VERTICAL_ALIGN_BOTTOM
Definition: grid.hpp:49
scrollbar_mode
The way to handle the showing or hiding of the scrollbar.
static const unsigned HORIZONTAL_ALIGN_LEFT
Definition: grid.hpp:54
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
unsigned get_h_align(const std::string &h_align)
Returns the horizontal alignment.
Definition: helper.cpp:52
static const unsigned BORDER_LEFT
Definition: grid.hpp:61
static retval get_retval_by_id(const std::string &id)
Gets the retval for the default buttons.
Definition: window.cpp:471
Contains the implementation details for lexical_cast and shouldn't be used directly.