The Battle for Wesnoth  1.19.5+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 std::string encode_text_alignment(const PangoAlignment alignment)
105 {
106  switch(alignment) {
107  case PANGO_ALIGN_LEFT:
108  return "left";
109  case PANGO_ALIGN_RIGHT:
110  return "right";
111  case PANGO_ALIGN_CENTER:
112  return "center";
113  }
114 
115  assert(false);
116  // FIXME: without this "styled_widget reaches end of non-void function" in release mode
117  throw "Control should not reach this point.";
118 }
119 
120 t_string missing_widget(const std::string& id)
121 {
122  return t_string(VGETTEXT("Mandatory widget ‘$id’ hasn't been defined.", {{"id", id}}));
123 }
124 
126 {
127  variable.add("screen_width", wfl::variant(settings::screen_width));
128  variable.add("screen_height", wfl::variant(settings::screen_height));
129  variable.add("screen_pitch_microns", wfl::variant(settings::screen_pitch_microns));
130  variable.add("gamemap_width", wfl::variant(settings::gamemap_width));
131  variable.add("gamemap_height", wfl::variant(settings::gamemap_height));
132  variable.add("gamemap_x_offset", wfl::variant(settings::gamemap_x_offset));
133 }
134 
136 {
139 
140  return result;
141 }
142 
144 {
145  return sdl::get_mouse_location();
146 }
147 
148 std::string_view debug_truncate(std::string_view text)
149 {
150  return text.substr(0, 15);
151 }
152 
153 } // 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
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:125
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:120
point get_mouse_position()
Returns the current mouse position.
Definition: helper.cpp:143
std::string_view debug_truncate(std::string_view text)
Returns a truncated version of the text.
Definition: helper.cpp:148
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 text alignment to its string representation.
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