The Battle for Wesnoth 1.21.0-dev
 
Loading...
Searching...
No Matches
helper.cpp
Go to the documentation of this file.
1/*
2 Copyright (C) 2008 - 2025
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
19
20#include "color.hpp"
21#include "formula/callable.hpp"
23#include "gui/core/log.hpp"
24#include "sdl/point.hpp"
26#include "tstring.hpp"
27#include "sdl/input.hpp" // get_mouse_location
28
29namespace 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
48PangoWeight decode_text_weight(const std::string& weight)
49{
50 if(weight == "thin") {
51 return PANGO_WEIGHT_THIN;
52 } else if (weight == "light") {
53 return PANGO_WEIGHT_LIGHT;
54 } else if (weight == "semibold") {
55 return PANGO_WEIGHT_SEMIBOLD;
56 } else if (weight == "bold") {
57 return PANGO_WEIGHT_BOLD;
58 } else if (weight == "heavy") {
59 return PANGO_WEIGHT_HEAVY;
60 }
61
62 if(!weight.empty() && weight != "normal") {
63 ERR_GUI_E << "Invalid text weight '" << weight << "', falling back to 'normal'.";
64 }
65
66 return PANGO_WEIGHT_NORMAL;
67}
68
69PangoStyle decode_text_style(const std::string& style)
70{
71 if(style == "italic") {
72 return PANGO_STYLE_ITALIC;
73 } else if(style == "oblique") {
74 return PANGO_STYLE_OBLIQUE;
75 }
76
77 if(!style.empty() && style != "normal") {
78 ERR_GUI_E << "Invalid text style '" << style << "', falling back to 'normal'.";
79 }
80
81 return PANGO_STYLE_NORMAL;
82}
83
84PangoAlignment decode_text_alignment(const std::string& alignment)
85{
86 if(alignment == "center") {
87 return PANGO_ALIGN_CENTER;
88 } else if(alignment == "right") {
89 return PANGO_ALIGN_RIGHT;
90 }
91
92 if(!alignment.empty() && alignment != "left") {
93 ERR_GUI_E << "Invalid text alignment '" << alignment << "', falling back to 'left'.";
94 }
95
96 return PANGO_ALIGN_LEFT;
97}
98
99PangoEllipsizeMode decode_ellipsize_mode(const std::string& ellipsize_mode)
100{
101 if(ellipsize_mode == "start") {
102 return PANGO_ELLIPSIZE_START;
103 } else if(ellipsize_mode == "middle") {
104 return PANGO_ELLIPSIZE_MIDDLE;
105 } else if(ellipsize_mode == "end") {
106 return PANGO_ELLIPSIZE_END;
107 }
108
109 if(!ellipsize_mode.empty() && ellipsize_mode != "none") {
110 ERR_GUI_E << "Invalid text ellipsization mode '" << ellipsize_mode << "', falling back to 'none'.";
111 }
112
113 return PANGO_ELLIPSIZE_NONE;
114}
115
116std::string encode_ellipsize_mode(const PangoEllipsizeMode ellipsize_mode)
117{
118 switch(ellipsize_mode) {
119 case PANGO_ELLIPSIZE_START:
120 return "start";
121 case PANGO_ELLIPSIZE_MIDDLE:
122 return "middle";
123 case PANGO_ELLIPSIZE_END:
124 return "end";
125 default:
126 return "none";
127 }
128}
129
130std::string encode_text_alignment(const PangoAlignment alignment)
131{
132 switch(alignment) {
133 case PANGO_ALIGN_LEFT:
134 return "left";
135 case PANGO_ALIGN_RIGHT:
136 return "right";
137 case PANGO_ALIGN_CENTER:
138 return "center";
139 }
140
141 // FIXME: without this "styled_widget reaches end of non-void function" in release mode
142 throw "Control should not reach this point.";
143}
144
145t_string missing_widget(const std::string& id)
146{
147 return t_string(VGETTEXT("Mandatory widget ‘$id’ hasn't been defined.", {{"id", id}}));
148}
149
151{
152 variable.add("screen_width", wfl::variant(settings::screen_width));
153 variable.add("screen_height", wfl::variant(settings::screen_height));
154 variable.add("gamemap_width", wfl::variant(settings::gamemap_width));
155 variable.add("gamemap_height", wfl::variant(settings::gamemap_height));
156 variable.add("gamemap_x_offset", wfl::variant(settings::gamemap_x_offset));
157}
158
166
171
172std::string_view debug_truncate(std::string_view text)
173{
174 return text.substr(0, 15);
175}
176
177} // namespace gui2
map_formula_callable & add(const std::string &key, const variant &value)
Definition callable.hpp:249
#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.
t_string missing_widget(const std::string &id)
Returns a default error message if a mandatory widget is omitted.
Definition helper.cpp:145
std::string encode_ellipsize_mode(const PangoEllipsizeMode ellipsize_mode)
Converts a PangoEllipsizeMode to its string representation.
Definition helper.cpp:116
point get_mouse_position()
Returns the current mouse position.
Definition helper.cpp:167
std::string_view debug_truncate(std::string_view text)
Returns a truncated version of the text.
Definition helper.cpp:172
PangoWeight decode_text_weight(const std::string &weight)
Converts a text weight string to a PangoWeight.
Definition helper.cpp:48
wfl::map_formula_callable get_screen_size_variables()
Gets a formula object with the screen size.
Definition helper.cpp:159
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:84
PangoStyle decode_text_style(const std::string &style)
Converts a text style string to a PangoStyle.
Definition helper.cpp:69
std::string encode_text_alignment(const PangoAlignment alignment)
Converts a PangoAlignment to its string representation.
Definition helper.cpp:130
PangoEllipsizeMode decode_ellipsize_mode(const std::string &ellipsize_mode)
Converts a text ellipsize mode string to a PangoEllipsizeMode.
Definition helper.cpp:99
point get_mouse_location()
Returns the current mouse location in draw space.
Definition input.cpp:49
This file contains the settings handling of the widget library.
Holds a 2D point.
Definition point.hpp:25