The Battle for Wesnoth  1.19.0+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 
30 namespace gui2
31 {
33 {
34  static const std::map<std::string, font::pango_text::FONT_STYLE> font_style_map {
38  {"underline", font::pango_text::STYLE_UNDERLINE},
39  };
40 
41  if(style.empty()) {
43  }
44 
45  if(const auto i = font_style_map.find(style); i != font_style_map.end()) {
46  return i->second;
47  }
48 
49  ERR_GUI_G << "Unknown style '" << style << "', using 'normal' instead.";
51 }
52 
53 color_t decode_color(const std::string& color)
54 {
55  return color_t::from_rgba_string(color);
56 }
57 
58 PangoWeight decode_text_weight(const std::string& weight)
59 {
60  if(weight == "thin") {
61  return PANGO_WEIGHT_THIN;
62  } else if (weight == "light") {
63  return PANGO_WEIGHT_LIGHT;
64  } else if (weight == "semibold") {
65  return PANGO_WEIGHT_SEMIBOLD;
66  } else if (weight == "bold") {
67  return PANGO_WEIGHT_BOLD;
68  } else if (weight == "heavy") {
69  return PANGO_WEIGHT_HEAVY;
70  }
71 
72  if(!weight.empty() && weight != "normal") {
73  ERR_GUI_E << "Invalid text weight '" << weight << "', falling back to 'normal'.";
74  }
75 
76  return PANGO_WEIGHT_NORMAL;
77 }
78 
79 PangoStyle decode_text_style(const std::string& style)
80 {
81  if(style == "italic") {
82  return PANGO_STYLE_ITALIC;
83  } else if(style == "oblique") {
84  return PANGO_STYLE_OBLIQUE;
85  }
86 
87  if(!style.empty() && style != "normal") {
88  ERR_GUI_E << "Invalid text style '" << style << "', falling back to 'normal'.";
89  }
90 
91  return PANGO_STYLE_NORMAL;
92 }
93 
94 PangoAlignment decode_text_alignment(const std::string& alignment)
95 {
96  if(alignment == "center") {
97  return PANGO_ALIGN_CENTER;
98  } else if(alignment == "right") {
99  return PANGO_ALIGN_RIGHT;
100  }
101 
102  if(!alignment.empty() && alignment != "left") {
103  ERR_GUI_E << "Invalid text alignment '" << alignment << "', falling back to 'left'.";
104  }
105 
106  return PANGO_ALIGN_LEFT;
107 }
108 
109 std::string encode_text_alignment(const PangoAlignment alignment)
110 {
111  switch(alignment) {
112  case PANGO_ALIGN_LEFT:
113  return "left";
114  case PANGO_ALIGN_RIGHT:
115  return "right";
116  case PANGO_ALIGN_CENTER:
117  return "center";
118  }
119 
120  assert(false);
121  // FIXME: without this "styled_widget reaches end of non-void function" in release mode
122  throw "Control should not reach this point.";
123 }
124 
125 t_string missing_widget(const std::string& id)
126 {
127  return t_string(VGETTEXT("Mandatory widget '$id' hasn't been defined.", {{"id", id}}));
128 }
129 
131 {
132  variable.add("screen_width", wfl::variant(settings::screen_width));
133  variable.add("screen_height", wfl::variant(settings::screen_height));
134  variable.add("screen_pitch_microns", wfl::variant(settings::screen_pitch_microns));
135  variable.add("gamemap_width", wfl::variant(settings::gamemap_width));
136  variable.add("gamemap_height", wfl::variant(settings::gamemap_height));
137  variable.add("gamemap_x_offset", wfl::variant(settings::gamemap_x_offset));
138 }
139 
141 {
144 
145  return result;
146 }
147 
149 {
150  return sdl::get_mouse_location();
151 }
152 
153 std::string_view debug_truncate(std::string_view text)
154 {
155  return text.substr(0, 15);
156 }
157 
158 } // 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.
std::size_t i
Definition: function.cpp:968
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
const unsigned screen_pitch_microns
screen_pitch_microns is deprecated.
Definition: settings.cpp:36
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:37
unsigned gamemap_height
Definition: settings.cpp:40
unsigned gamemap_width
The size of the map area, if not available equal to the screen size.
Definition: settings.cpp:39
Generic file dialog.
void get_screen_size_variables(wfl::map_formula_callable &variable)
Gets a formula object with the screen size.
Definition: helper.cpp:130
color_t decode_color(const std::string &color)
Converts a color string to a color.
Definition: helper.cpp:53
t_string missing_widget(const std::string &id)
Returns a default error message if a mandatory widget is omitted.
Definition: helper.cpp:125
point get_mouse_position()
Returns the current mouse position.
Definition: helper.cpp:148
std::string_view debug_truncate(std::string_view text)
Returns a truncated version of the text.
Definition: helper.cpp:153
PangoWeight decode_text_weight(const std::string &weight)
Converts a text weight string to a PangoWeight.
Definition: helper.cpp:58
font::pango_text::FONT_STYLE decode_font_style(const std::string &style)
Converts a font style string to a font style.
Definition: helper.cpp:32
PangoAlignment decode_text_alignment(const std::string &alignment)
Converts a text alignment string to a text alignment.
Definition: helper.cpp:94
PangoStyle decode_text_style(const std::string &style)
Converts a text style string to a PangoStyle.
Definition: helper.cpp:79
std::string encode_text_alignment(const PangoAlignment alignment)
Converts a text alignment to its string representation.
Definition: helper.cpp:109
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(const std::string &c)
Creates a new color_t object from a string variable in "R,G,B,A" format.
Definition: color.cpp:21
Holds a 2D point.
Definition: point.hpp:25