1 /*
2    Copyright (C) 2008 - 2018 by Mark de Wever <koraq@xs4all.nl>
3    Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5    This program is free software; you can redistribute it and/or modify
6    it under the terms of the GNU General Public License as published by
7    the Free Software Foundation; either version 2 of the License, or
8    (at your option) any later version.
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY.
11 
12    See the COPYING file for more details.
13 */
14 
15 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "gui/widgets/helper.hpp"
18 
19 #include "color.hpp"
20 #include "formula/callable.hpp"
21 #include "formula/string_utils.hpp"
22 #include "gui/core/log.hpp"
23 #include "sdl/point.hpp"
24 #include "gui/widgets/settings.hpp"
25 #include "sdl/rect.hpp"
26 #include "tstring.hpp"
27 
28 #include <SDL2/SDL.h>
29 
30 namespace gui2
31 {
create_rect(const point & origin,const point & size)32 SDL_Rect create_rect(const point& origin, const point& size)
33 {
34 	return {origin.x, origin.y, size.x, size.y};
35 }
36 
decode_font_style(const std::string & style)37 font::pango_text::FONT_STYLE decode_font_style(const std::string& style)
38 {
39 	static std::map<std::string, font::pango_text::FONT_STYLE> font_style_map {
40 		{"normal",    font::pango_text::STYLE_NORMAL},
41 		{"bold",      font::pango_text::STYLE_BOLD},
42 		{"italic",    font::pango_text::STYLE_ITALIC},
43 		{"underline", font::pango_text::STYLE_UNDERLINE},
44 	};
45 
46 	if(style.empty()) {
47 		return font::pango_text::STYLE_NORMAL;
48 	}
49 
50 	if(font_style_map.find(style) == font_style_map.end()) {
51 		ERR_GUI_G << "Unknown style '" << style << "', using 'normal' instead." << std::endl;
52 		return font::pango_text::STYLE_NORMAL;
53 	}
54 
55 	return font_style_map[style];
56 }
57 
decode_color(const std::string & color)58 color_t decode_color(const std::string& color)
59 {
60 	return color_t::from_rgba_string(color);
61 }
62 
decode_text_alignment(const std::string & alignment)63 PangoAlignment decode_text_alignment(const std::string& alignment)
64 {
65 	if(alignment == "center") {
66 		return PANGO_ALIGN_CENTER;
67 	} else if(alignment == "right") {
68 		return PANGO_ALIGN_RIGHT;
69 	}
70 
71 	if(!alignment.empty() && alignment != "left") {
72 		ERR_GUI_E << "Invalid text alignment '" << alignment << "', falling back to 'left'." << std::endl;
73 	}
74 
75 	return PANGO_ALIGN_LEFT;
76 }
77 
encode_text_alignment(const PangoAlignment alignment)78 std::string encode_text_alignment(const PangoAlignment alignment)
79 {
80 	switch(alignment) {
81 		case PANGO_ALIGN_LEFT:
82 			return "left";
83 		case PANGO_ALIGN_RIGHT:
84 			return "right";
85 		case PANGO_ALIGN_CENTER:
86 			return "center";
87 	}
88 
89 	assert(false);
90 	// FIXME: without this "styled_widget reaches end of non-void function" in release mode
91 	throw "Control should not reach this point.";
92 }
93 
missing_widget(const std::string & id)94 t_string missing_widget(const std::string& id)
95 {
96 	return t_string(VGETTEXT("Mandatory widget '$id' hasn't been defined.", {{"id", id}}));
97 }
98 
get_screen_size_variables(wfl::map_formula_callable & variable)99 void get_screen_size_variables(wfl::map_formula_callable& variable)
100 {
101 	variable.add("screen_width", wfl::variant(settings::screen_width));
102 	variable.add("screen_height", wfl::variant(settings::screen_height));
103 	variable.add("gamemap_width", wfl::variant(settings::gamemap_width));
104 	variable.add("gamemap_height", wfl::variant(settings::gamemap_height));
105 	variable.add("gamemap_x_offset", wfl::variant(settings::gamemap_x_offset));
106 }
107 
get_screen_size_variables()108 wfl::map_formula_callable get_screen_size_variables()
109 {
110 	wfl::map_formula_callable result;
111 	get_screen_size_variables(result);
112 
113 	return result;
114 }
115 
get_mouse_position()116 point get_mouse_position()
117 {
118 	int x, y;
119 	SDL_GetMouseState(&x, &y);
120 
121 	return point(x, y);
122 }
123 
debug_truncate(const std::string & text)124 std::string debug_truncate(const std::string& text)
125 {
126 	return text.substr(0, 15);
127 }
128 
129 } // namespace gui2
130