The Battle for Wesnoth  1.17.17+dev
helper.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2023
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 "sdl/rect.hpp"
27 #include "tstring.hpp"
28 #include "sdl/input.hpp" // get_mouse_location
29 
30 #include <SDL2/SDL.h>
31 
32 namespace gui2
33 {
35 {
36  static const std::map<std::string, font::pango_text::FONT_STYLE> font_style_map {
40  {"underline", font::pango_text::STYLE_UNDERLINE},
41  };
42 
43  if(style.empty()) {
45  }
46 
47  if(const auto i = font_style_map.find(style); i != font_style_map.end()) {
48  return i->second;
49  }
50 
51  ERR_GUI_G << "Unknown style '" << style << "', using 'normal' instead.";
53 }
54 
55 color_t decode_color(const std::string& color)
56 {
57  return color_t::from_rgba_string(color);
58 }
59 
60 PangoAlignment decode_text_alignment(const std::string& alignment)
61 {
62  if(alignment == "center") {
63  return PANGO_ALIGN_CENTER;
64  } else if(alignment == "right") {
65  return PANGO_ALIGN_RIGHT;
66  }
67 
68  if(!alignment.empty() && alignment != "left") {
69  ERR_GUI_E << "Invalid text alignment '" << alignment << "', falling back to 'left'.";
70  }
71 
72  return PANGO_ALIGN_LEFT;
73 }
74 
75 std::string encode_text_alignment(const PangoAlignment alignment)
76 {
77  switch(alignment) {
78  case PANGO_ALIGN_LEFT:
79  return "left";
80  case PANGO_ALIGN_RIGHT:
81  return "right";
82  case PANGO_ALIGN_CENTER:
83  return "center";
84  }
85 
86  assert(false);
87  // FIXME: without this "styled_widget reaches end of non-void function" in release mode
88  throw "Control should not reach this point.";
89 }
90 
91 t_string missing_widget(const std::string& id)
92 {
93  return t_string(VGETTEXT("Mandatory widget '$id' hasn't been defined.", {{"id", id}}));
94 }
95 
97 {
98  variable.add("screen_width", wfl::variant(settings::screen_width));
99  variable.add("screen_height", wfl::variant(settings::screen_height));
100  variable.add("screen_pitch_microns", wfl::variant(settings::screen_pitch_microns));
101  variable.add("gamemap_width", wfl::variant(settings::gamemap_width));
102  variable.add("gamemap_height", wfl::variant(settings::gamemap_height));
103  variable.add("gamemap_x_offset", wfl::variant(settings::gamemap_x_offset));
104 }
105 
107 {
110 
111  return result;
112 }
113 
115 {
116  return sdl::get_mouse_location();
117 }
118 
119 std::string_view debug_truncate(std::string_view text)
120 {
121  return text.substr(0, 15);
122 }
123 
124 } // 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:96
color_t decode_color(const std::string &color)
Converts a color string to a color.
Definition: helper.cpp:55
t_string missing_widget(const std::string &id)
Returns a default error message if a mandatory widget is omitted.
Definition: helper.cpp:91
point get_mouse_position()
Returns the current mouse position.
Definition: helper.cpp:114
std::string_view debug_truncate(std::string_view text)
Returns a truncated version of the text.
Definition: helper.cpp:119
font::pango_text::FONT_STYLE decode_font_style(const std::string &style)
Converts a font style string to a font style.
Definition: helper.cpp:34
PangoAlignment decode_text_alignment(const std::string &alignment)
Converts a text alignment string to a text alignment.
Definition: helper.cpp:60
std::string encode_text_alignment(const PangoAlignment alignment)
Converts a text alignment to its string representation.
Definition: helper.cpp:75
point get_mouse_location()
Returns the current mouse location in draw space.
Definition: input.cpp:54
Contains the SDL_Rect helper code.
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