The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
helper.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2008 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://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 #ifndef GUI_WIDGETS_HELPER_HPP_INCLUDED
16 #define GUI_WIDGETS_HELPER_HPP_INCLUDED
17 
18 #include "global.hpp"
19 
20 #include <pango/pango-layout.h>
21 
22 #include <boost/cstdint.hpp>
23 #include <boost/type_traits.hpp>
24 #include <boost/utility/enable_if.hpp>
25 
26 #include <string>
27 
28 #if defined(_MSC_VER) && _MSC_VER <= 1600
29 /*
30  This is needed because msvc up to 2010 fails to correcty forward declare this struct as a return value this case.
31  And will create corrupt binaries without giving a warning / error.
32 */
33 #include <SDL_video.h>
34 #else
35 struct SDL_Rect;
36 #endif
37 struct surface;
38 class t_string;
39 
40 namespace game_logic
41 {
42 class map_formula_callable;
43 } // namespace game_logic
44 
45 namespace gui2
46 {
47 
48 struct tpoint;
49 
50 /**
51  * Initializes the gui subsystems.
52  *
53  * This function needs to be called before other parts of the gui engine are
54  * used.
55  */
56 bool init();
57 
58 /**
59  * Creates a rectangle.
60  *
61  * @param origin The top left corner.
62  * @param size The width (x) and height (y).
63  *
64  * @returns SDL_Rect with the proper rectangle.
65  */
66 SDL_Rect create_rect(const tpoint& origin, const tpoint& size);
67 
68 /**
69  * Converts a color string to a color.
70  *
71  * @param color A color string see
72  * http://www.wesnoth.org/wiki/GUIVariable for
73  * more info.
74  *
75  * @returns The color.
76  */
78 
79 /**
80  * Converts a text alignment string to a text alignment.
81  *
82  * @param alignment An alignment string see
83  * http://www.wesnoth.org/wiki/GUIVariable for
84  * more info.
85  *
86  * @returns The text alignment.
87  */
88 PangoAlignment decode_text_alignment(const std::string& alignment);
89 
90 /**
91  * Converts a text alignment to its string representation.
92  *
93  * @param alignment An alignment.
94  *
95  * @returns An alignment string see
96  * http://www.wesnoth.org/wiki/GUIVariable for
97  * more info.
98  */
99 std::string encode_text_alignment(const PangoAlignment alignment);
100 
101 /**
102  * Converts a font style string to a font style.
103  *
104  * @param style A font style string see
105  * http://www.wesnoth.org/wiki/GUIVariable for
106  * more info.
107  *
108  * @returns The font style.
109  */
110 unsigned decode_font_style(const std::string& style);
111 
112 /**
113  * Returns a default error message if a mandatory widget is omitted.
114  *
115  * @param id The id of the omitted widget.
116  * @returns The error message.
117  */
119 
120 /**
121  * Gets a formula object with the screen size.
122  *
123  * @param variable A formula object in which the screen_width,
124  * screen_height, gamemap_width and
125  * gamemap_height variable will set to the
126  * current values of these in settings. It
127  * modifies the object send.
128  */
130 
131 /**
132  * Gets a formula object with the screen size.
133  *
134  * @returns Formula object with the screen_width,
135  * screen_height, gamemap_width and
136  * gamemap_height variable set to the current
137  * values of these in settings.
138  */
140 
141 /** Returns the current mouse position. */
142 tpoint get_mouse_position();
143 
144 /**
145  * Returns a truncated version of the text.
146  *
147  * For debugging it's sometimes useful to get a part of the label of the
148  * widget. This function shows the first part.
149  *
150  * @param text The text to truncate.
151  *
152  * @returns The truncated text.
153  */
155 
156 /**
157  * Helper for function wrappers.
158  *
159  * For boost bind the a function sometimes needs to return a value although
160  * the function called doesn't return one. This wrapper function can return a
161  * fixed result for a certain functor.
162  *
163  * @tparam R The return type.
164  * @tparam F The type of the functor.
165  *
166  * @param result The result value.
167  * @param function The functor to call.
168  *
169  * @returns result.
170  */
171 template <class R, class F>
172 R function_wrapper(const R result, const F& function)
173 {
174  function();
175  return result;
176 }
177 
178 } // namespace gui2
179 
180 #endif
std::string encode_text_alignment(const PangoAlignment alignment)
Converts a text alignment to its string representation.
Definition: helper.cpp:107
unsigned decode_font_style(const std::string &style)
Converts a font style string to a font style.
Definition: helper.cpp:55
boost::uint32_t uint32_t
Definition: xbrz.hpp:45
R function_wrapper(const R result, const F &function)
Helper for function wrappers.
Definition: helper.hpp:172
PangoAlignment decode_text_alignment(const std::string &alignment)
Converts a text alignment string to a text alignment.
Definition: helper.cpp:92
SDL_Rect create_rect(const tpoint &origin, const tpoint &size)
Creates a rectangle.
Definition: helper.cpp:50
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
GLuint64EXT * result
Definition: glew.h:10727
tpoint get_mouse_position()
Returns the current mouse position.
Definition: helper.cpp:149
bool init()
Initializes the gui subsystems.
Definition: helper.cpp:37
GLuint color
Definition: glew.h:5801
GLenum GLenum variable
Definition: glew.h:10668
std::string debug_truncate(const std::string &text)
Returns a truncated version of the text.
Definition: helper.cpp:157
boost::uint32_t decode_color(const std::string &color)
Converts a color string to a color.
Definition: helper.cpp:73
GLsizeiptr size
Definition: glew.h:1649
void get_screen_size_variables(game_logic::map_formula_callable &variable)
Gets a formula object with the screen size.
Definition: helper.cpp:132
GLsizei const GLcharARB ** string
Definition: glew.h:4503
t_string missing_widget(const std::string &id)
Returns a default error message if a mandatory widget is omitted.
Definition: helper.cpp:123