The Battle for Wesnoth  1.19.7+dev
helper.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2024
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #define GETTEXT_DOMAIN "wesnoth-lib"
17 
18 #include "gui/widgets/helper.hpp"
19 
20 #include "color.hpp"
21 #include "formula/callable.hpp"
22 #include "formula/string_utils.hpp"
23 #include "gui/core/log.hpp"
24 #include "sdl/point.hpp"
25 #include "gui/widgets/settings.hpp"
26 #include "tstring.hpp"
27 #include "sdl/input.hpp" // get_mouse_location
28 
29 namespace gui2
30 {
32 {
33  if(style == "bold") {
35  } else if(style == "italic") {
37  } else if(style == "underline") {
39  }
40 
41  if(!style.empty() && style != "normal") {
42  ERR_GUI_G << "Unknown style '" << style << "', using 'normal' instead.";
43  }
44 
46 }
47 
48 color_t decode_color(const std::string& color)
49 {
50  return color_t::from_rgba_string(color);
51 }
52 
53 PangoWeight decode_text_weight(const std::string& weight)
54 {
55  if(weight == "thin") {
56  return PANGO_WEIGHT_THIN;
57  } else if (weight == "light") {
58  return PANGO_WEIGHT_LIGHT;
59  } else if (weight == "semibold") {
60  return PANGO_WEIGHT_SEMIBOLD;
61  } else if (weight == "bold") {
62  return PANGO_WEIGHT_BOLD;
63  } else if (weight == "heavy") {
64  return PANGO_WEIGHT_HEAVY;
65  }
66 
67  if(!weight.empty() && weight != "normal") {
68  ERR_GUI_E << "Invalid text weight '" << weight << "', falling back to 'normal'.";
69  }
70 
71  return PANGO_WEIGHT_NORMAL;
72 }
73 
74 PangoStyle decode_text_style(const std::string& style)
75 {
76  if(style == "italic") {
77  return PANGO_STYLE_ITALIC;
78  } else if(style == "oblique") {
79  return PANGO_STYLE_OBLIQUE;
80  }
81 
82  if(!style.empty() && style != "normal") {
83  ERR_GUI_E << "Invalid text style '" << style << "', falling back to 'normal'.";
84  }
85 
86  return PANGO_STYLE_NORMAL;
87 }
88 
89 PangoAlignment decode_text_alignment(const std::string& alignment)
90 {
91  if(alignment == "center") {
92  return PANGO_ALIGN_CENTER;
93  } else if(alignment == "right") {
94  return PANGO_ALIGN_RIGHT;
95  }
96 
97  if(!alignment.empty() && alignment != "left") {
98  ERR_GUI_E << "Invalid text alignment '" << alignment << "', falling back to 'left'.";
99  }
100 
101  return PANGO_ALIGN_LEFT;
102 }
103 
104 PangoEllipsizeMode decode_ellipsize_mode(const std::string& ellipsize_mode)
105 {
106  if(ellipsize_mode == "start") {
107  return PANGO_ELLIPSIZE_START;
108  } else if(ellipsize_mode == "middle") {
109  return PANGO_ELLIPSIZE_MIDDLE;
110  } else if(ellipsize_mode == "end") {
111  return PANGO_ELLIPSIZE_END;
112  }
113 
114  if(!ellipsize_mode.empty() && ellipsize_mode != "none") {
115  ERR_GUI_E << "Invalid text ellipsization mode '" << ellipsize_mode << "', falling back to 'none'.";
116  }
117 
118  return PANGO_ELLIPSIZE_NONE;
119 }
120 
121 std::string encode_ellipsize_mode(const PangoEllipsizeMode ellipsize_mode)
122 {
123  switch(ellipsize_mode) {
124  case PANGO_ELLIPSIZE_START:
125  return "start";
126  case PANGO_ELLIPSIZE_MIDDLE:
127  return "middle";
128  case PANGO_ELLIPSIZE_END:
129  return "end";
130  default:
131  return "none";
132  }
133 }
134 
135 std::string encode_text_alignment(const PangoAlignment alignment)
136 {
137  switch(alignment) {
138  case PANGO_ALIGN_LEFT:
139  return "left";
140  case PANGO_ALIGN_RIGHT:
141  return "right";
142  case PANGO_ALIGN_CENTER:
143  return "center";
144  }
145 
146  assert(false);
147  // FIXME: without this "styled_widget reaches end of non-void function" in release mode
148  throw "Control should not reach this point.";
149 }
150 
151 t_string missing_widget(const std::string& id)
152 {
153  return t_string(VGETTEXT("Mandatory widget ‘$id’ hasn't been defined.", {{"id", id}}));
154 }
155 
157 {
158  variable.add("screen_width", wfl::variant(settings::screen_width));
159  variable.add("screen_height", wfl::variant(settings::screen_height));
160  variable.add("gamemap_width", wfl::variant(settings::gamemap_width));
161  variable.add("gamemap_height", wfl::variant(settings::gamemap_height));
162  variable.add("gamemap_x_offset", wfl::variant(settings::gamemap_x_offset));
163 }
164 
166 {
169 
170  return result;
171 }
172 
174 {
175  return sdl::get_mouse_location();
176 }
177 
178 std::string_view debug_truncate(std::string_view text)
179 {
180  return text.substr(0, 15);
181 }
182 
183 } // namespace gui2
map_formula_callable & add(const std::string &key, const variant &value)
Definition: callable.hpp:253
#define VGETTEXT(msgid,...)
Handy wrappers around interpolate_variables_into_string and gettext.
Define the common log macros for the gui toolkit.
#define ERR_GUI_G
Definition: log.hpp:44
#define ERR_GUI_E
Definition: log.hpp:38
Contains functions for cleanly handling SDL input.
unsigned screen_width
The screen resolution and pixel pitch should be available for all widgets since their drawing method ...
Definition: settings.cpp:27
unsigned screen_height
Definition: settings.cpp:28
unsigned gamemap_x_offset
The offset between the left edge of the screen and the gamemap.
Definition: settings.cpp:29
unsigned gamemap_height
Definition: settings.cpp:32
unsigned gamemap_width
The size of the map area, if not available equal to the screen size.
Definition: settings.cpp:31
Generic file dialog.
void get_screen_size_variables(wfl::map_formula_callable &variable)
Gets a formula object with the screen size.
Definition: helper.cpp:156
color_t decode_color(const std::string &color)
Converts a color string to a color.
Definition: helper.cpp:48
t_string missing_widget(const std::string &id)
Returns a default error message if a mandatory widget is omitted.
Definition: helper.cpp:151
std::string encode_ellipsize_mode(const PangoEllipsizeMode ellipsize_mode)
Converts a PangoEllipsizeMode to its string representation.
Definition: helper.cpp:121
point get_mouse_position()
Returns the current mouse position.
Definition: helper.cpp:173
std::string_view debug_truncate(std::string_view text)
Returns a truncated version of the text.
Definition: helper.cpp:178
PangoWeight decode_text_weight(const std::string &weight)
Converts a text weight string to a PangoWeight.
Definition: helper.cpp:53
font::pango_text::FONT_STYLE decode_font_style(const std::string &style)
Converts a font style string to a font style.
Definition: helper.cpp:31
PangoAlignment decode_text_alignment(const std::string &alignment)
Converts a text alignment string to a text alignment.
Definition: helper.cpp:89
PangoStyle decode_text_style(const std::string &style)
Converts a text style string to a PangoStyle.
Definition: helper.cpp:74
std::string encode_text_alignment(const PangoAlignment alignment)
Converts a PangoAlignment to its string representation.
Definition: helper.cpp:135
PangoEllipsizeMode decode_ellipsize_mode(const std::string &ellipsize_mode)
Converts a text ellipsize mode string to a PangoEllipsizeMode.
Definition: helper.cpp:104
point get_mouse_location()
Returns the current mouse location in draw space.
Definition: input.cpp:54
This file contains the settings handling of the widget library.
The basic class for representing 8-bit RGB or RGBA colour values.
Definition: color.hpp:59
static color_t from_rgba_string(std::string_view c)
Creates a new color_t object from a string variable in "R,G,B,A" format.
Definition: color.cpp:23
Holds a 2D point.
Definition: point.hpp:25