gui/widgets/helper.hpp

Go to the documentation of this file.
00001 /* $Id: helper.hpp 54259 2012-05-20 14:00:17Z mordante $ */
00002 /*
00003    Copyright (C) 2008 - 2012 by Mark de Wever <koraq@xs4all.nl>
00004    Part of the Battle for Wesnoth Project http://www.wesnoth.org/
00005 
00006    This program is free software; you can redistribute it and/or modify
00007    it under the terms of the GNU General Public License as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #ifndef GUI_WIDGETS_HELPER_HPP_INCLUDED
00017 #define GUI_WIDGETS_HELPER_HPP_INCLUDED
00018 
00019 #include "SDL.h"
00020 
00021 #include <pango/pango-layout.h>
00022 
00023 #include <boost/type_traits.hpp>
00024 #include <boost/utility/enable_if.hpp>
00025 
00026 #include <string>
00027 
00028 struct surface;
00029 class t_string;
00030 
00031 namespace game_logic {
00032 class map_formula_callable;
00033 } // namespace game_logic
00034 
00035 namespace gui2 {
00036 
00037 /**
00038  * Initializes the gui subsystems.
00039  *
00040  * This function needs to be called before other parts of the gui engine are
00041  * used.
00042  */
00043 bool init();
00044 
00045 /** Holds a 2D point. */
00046 struct tpoint
00047 {
00048     tpoint(const int x_, const int y_) :
00049         x(x_),
00050         y(y_)
00051         {}
00052 
00053     /** x coodinate. */
00054     int x;
00055 
00056     /** y coodinate. */
00057     int y;
00058 
00059     bool operator==(const tpoint& point) const { return x == point.x && y == point.y; }
00060     bool operator!=(const tpoint& point) const { return x != point.x || y != point.y; }
00061     bool operator<(const tpoint& point) const
00062         { return x < point.x || (x == point.x && y < point.y); }
00063 
00064     bool operator<=(const tpoint& point) const
00065         { return x < point.x || (x == point.x && y <= point.y); }
00066 
00067     tpoint operator+(const tpoint& point) const
00068         { return tpoint(x + point.x, y + point.y); }
00069 
00070     tpoint& operator+=(const tpoint& point);
00071 
00072     tpoint operator-(const tpoint& point) const
00073         { return tpoint(x - point.x, y - point.y); }
00074 
00075     tpoint& operator-=(const tpoint& point);
00076 };
00077 
00078 std::ostream &operator<<(std::ostream &stream, const tpoint& point);
00079 
00080 /**
00081  * Creates a rectangle.
00082  *
00083  * @param origin                  The top left corner.
00084  * @param size                    The width (x) and height (y).
00085  *
00086  * @returns                       SDL_Rect with the proper rectangle.
00087  */
00088 SDL_Rect create_rect(const tpoint& origin, const tpoint& size);
00089 
00090 /**
00091  * Converts a color string to a color.
00092  *
00093  * @param color                  A color string see
00094  *                                http://www.wesnoth.org/wiki/GUIVariable for
00095  *                                more info.
00096  *
00097  * @returns                       The color.
00098  */
00099 Uint32 decode_color(const std::string& color);
00100 
00101 /**
00102  * Converts a text alignment string to a text alignment.
00103  *
00104  * @param alignment               An alignment string see
00105  *                                http://www.wesnoth.org/wiki/GUIVariable for
00106  *                                more info.
00107  *
00108  * @returns                       The text alignment.
00109  */
00110 PangoAlignment decode_text_alignment(const std::string& alignment);
00111 
00112 /**
00113  * Converts a text alignment to its string representation.
00114  *
00115  * @param alignment              An alignment.
00116  *
00117  * @returns                       An alignment string see
00118  *                                http://www.wesnoth.org/wiki/GUIVariable for
00119  *                                more info.
00120  */
00121 std::string encode_text_alignment(const PangoAlignment alignment);
00122 
00123 /**
00124  * Converts a font style string to a font style.
00125  *
00126  * @param style                   A font style string see
00127  *                                http://www.wesnoth.org/wiki/GUIVariable for
00128  *                                more info.
00129  *
00130  * @returns                       The font style.
00131  */
00132 unsigned decode_font_style(const std::string& style);
00133 
00134 /**
00135  * Returns a default error message if a mandatory widget is omitted.
00136  *
00137  * @param id                      The id of the omitted widget.
00138  * @returns                       The error message.
00139  */
00140 t_string missing_widget(const std::string& id);
00141 
00142 /**
00143  * Gets a formula object with the screen size.
00144  *
00145  * @param variable                A formula object in which the screen_width,
00146  *                                screen_height, gamemap_width and
00147  *                                gamemap_height variable will set to the
00148  *                                current values of these in settings. It
00149  *                                modifies the object send.
00150  */
00151 void get_screen_size_variables(game_logic::map_formula_callable& variable);
00152 
00153 /**
00154  * Gets a formula object with the screen size.
00155  *
00156  * @returns                       Formula object with the screen_width,
00157  *                                screen_height, gamemap_width and
00158  *                                gamemap_height variable set to the current
00159  *                                values of these in settings.
00160  */
00161 game_logic::map_formula_callable get_screen_size_variables();
00162 
00163 /** Returns the current mouse position. */
00164 tpoint get_mouse_position();
00165 
00166 /**
00167  * Returns a truncated version of the text.
00168  *
00169  * For debugging it's sometimes useful to get a part of the label of the
00170  * widget. This function shows the first part.
00171  *
00172  * @param text                    The text to truncate.
00173  *
00174  * @returns                       The truncated text.
00175  */
00176 std::string debug_truncate(const std::string& text);
00177 
00178 /**
00179  * Helper for function wrappers.
00180  *
00181  * For boost bind the a function sometimes needs to return a value althought
00182  * the function called doesn't return one. This wrapper function can return a
00183  * fixed result for a certain functor.
00184  *
00185  * @tparam R                      The return type.
00186  * @tparam F                      The type of the functor.
00187  *
00188  * @param result                  The result value.
00189  * @param function                The functor to call.
00190  *
00191  * @returns                       result.
00192  */
00193 template<class R, class F>
00194 R function_wrapper(const R result, const F& function)
00195 {
00196     function();
00197     return result;
00198 }
00199 
00200 } // namespace gui2
00201 
00202 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:57 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs