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 PangoAlignment decode_text_alignment(const std::string& alignment)
59 {
60  if(alignment == "center") {
61  return PANGO_ALIGN_CENTER;
62  } else if(alignment == "right") {
63  return PANGO_ALIGN_RIGHT;
64  }
65 
66  if(!alignment.empty() && alignment != "left") {
67  ERR_GUI_E << "Invalid text alignment '" << alignment << "', falling back to 'left'.";
68  }
69 
70  return PANGO_ALIGN_LEFT;
71 }
72 
73 std::string encode_text_alignment(const PangoAlignment alignment)
74 {
75  switch(alignment) {
76  case PANGO_ALIGN_LEFT:
77  return "left";
78  case PANGO_ALIGN_RIGHT:
79  return "right";
80  case PANGO_ALIGN_CENTER:
81  return "center";
82  }
83 
84  assert(false);
85  // FIXME: without this "styled_widget reaches end of non-void function" in release mode
86  throw "Control should not reach this point.";
87 }
88 
89 t_string missing_widget(const std::string& id)
90 {
91  return t_string(VGETTEXT("Mandatory widget '$id' hasn't been defined.", {{"id", id}}));
92 }
93 
95 {
96  variable.add("screen_width", wfl::variant(settings::screen_width));
97  variable.add("screen_height", wfl::variant(settings::screen_height));
98  variable.add("screen_pitch_microns", wfl::variant(settings::screen_pitch_microns));
99  variable.add("gamemap_width", wfl::variant(settings::gamemap_width));
100  variable.add("gamemap_height", wfl::variant(settings::gamemap_height));
101  variable.add("gamemap_x_offset", wfl::variant(settings::gamemap_x_offset));
102 }
103 
105 {
108 
109  return result;
110 }
111 
113 {
114  return sdl::get_mouse_location();
115 }
116 
117 std::string_view debug_truncate(std::string_view text)
118 {
119  return text.substr(0, 15);
120 }
121 
122 } // 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:94
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:89
point get_mouse_position()
Returns the current mouse position.
Definition: helper.cpp:112
std::string_view debug_truncate(std::string_view text)
Returns a truncated version of the text.
Definition: helper.cpp:117
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:58
std::string encode_text_alignment(const PangoAlignment alignment)
Converts a text alignment to its string representation.
Definition: helper.cpp:73
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