The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
helper.cpp
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 #define GETTEXT_DOMAIN "wesnoth-lib"
16 
17 #include "gui/widgets/helper.hpp"
18 
19 #include "gui/core/log.hpp"
20 #include "gui/core/point.hpp"
21 #include "gui/widgets/settings.hpp"
22 #include "sdl/rect.hpp"
23 #include "formula/callable.hpp"
24 #include "formula/string_utils.hpp"
25 #include "tstring.hpp"
26 
27 #include <SDL_ttf.h>
28 
29 namespace gui2
30 {
31 
32 namespace
33 {
34 static bool initialized_ = false;
35 }
36 
37 bool init()
38 {
39  if(initialized_) {
40  return true;
41  }
42 
43  load_settings();
44 
45  initialized_ = true;
46 
47  return initialized_;
48 }
49 
50 SDL_Rect create_rect(const tpoint& origin, const tpoint& size)
51 {
52  return sdl::create_rect(origin.x, origin.y, size.x, size.y);
53 }
54 
55 unsigned decode_font_style(const std::string& style)
56 {
57  if(style == "bold") {
58  return TTF_STYLE_BOLD;
59  } else if(style == "italic") {
60  return TTF_STYLE_ITALIC;
61  } else if(style == "underline") {
62  return TTF_STYLE_UNDERLINE;
63  } else if(style.empty() || style == "normal") {
64  return TTF_STYLE_NORMAL;
65  }
66 
67  ERR_GUI_G << "Unknown style '" << style << "' using 'normal' instead."
68  << std::endl;
69 
70  return TTF_STYLE_NORMAL;
71 }
72 
74 {
75  std::vector<std::string> fields = utils::split(color);
76 
77  // make sure we have four fields
78  while(fields.size() < 4)
79  fields.push_back("0");
80 
82  for(int i = 0; i < 4; ++i) {
83  // shift the previous value before adding, since it's a nop on the
84  // first run there's no need for an if.
85  result = result << 8;
86  result |= lexical_cast_default<int>(fields[i]);
87  }
88 
89  return result;
90 }
91 
92 PangoAlignment decode_text_alignment(const std::string& alignment)
93 {
94  if(alignment == "center") {
95  return PANGO_ALIGN_CENTER;
96  } else if(alignment == "right") {
97  return PANGO_ALIGN_RIGHT;
98  } else {
99  if(!alignment.empty() && alignment != "left") {
100  ERR_GUI_E << "Invalid text alignment '" << alignment
101  << "' falling back to 'left'.\n";
102  }
103  return PANGO_ALIGN_LEFT;
104  }
105 }
106 
107 std::string encode_text_alignment(const PangoAlignment alignment)
108 {
109  switch(alignment) {
110  case PANGO_ALIGN_LEFT:
111  return "left";
112  case PANGO_ALIGN_RIGHT:
113  return "right";
114  case PANGO_ALIGN_CENTER:
115  return "center";
116  }
117  assert(false);
118  // FIXME: without this "control reaches end of non-void function" in release
119  // mode
120  throw "Control should not reach this point.";
121 }
122 
124 {
125  utils::string_map symbols;
126  symbols["id"] = id;
127 
128  return t_string(
129  vgettext("Mandatory widget '$id' hasn't been defined.", symbols));
130 }
131 
133 {
134  variable.add("screen_width", variant(settings::screen_width));
135  variable.add("screen_height", variant(settings::screen_height));
136  variable.add("gamemap_width", variant(settings::gamemap_width));
137  variable.add("gamemap_height", variant(settings::gamemap_height));
138  variable.add("gamemap_x_offset", variant(settings::gamemap_x_offset));
139 }
140 
142 {
145 
146  return result;
147 }
148 
150 {
151  int x, y;
152  SDL_GetMouseState(&x, &y);
153 
154  return tpoint(x, y);
155 }
156 
158 {
159  return text.substr(0, 15);
160 }
161 
162 } // namespace gui2
Define the common log macros for the gui toolkit.
#define ERR_GUI_E
Definition: log.hpp:38
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
bool initialized_
Definition: font.cpp:609
boost::uint32_t uint32_t
Definition: xbrz.hpp:45
#define ERR_GUI_G
Definition: log.hpp:44
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
unsigned gamemap_width
The size of the map area, if not available equal to the screen size.
Definition: settings.cpp:49
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
std::map< std::string, t_string > string_map
This file contains the settings handling of the widget library.
int y
y coordinate.
Definition: point.hpp:34
unsigned gamemap_height
Definition: settings.cpp:50
tpoint get_mouse_position()
Returns the current mouse position.
Definition: helper.cpp:149
static size_t id
Ids for the timers.
Definition: timer.cpp:39
bool init()
Initializes the gui subsystems.
Definition: helper.cpp:37
GLuint color
Definition: glew.h:5801
int x
x coordinate.
Definition: point.hpp:31
GLenum GLenum variable
Definition: glew.h:10668
unsigned gamemap_x_offset
The offset between the left edge of the screen and the gamemap.
Definition: settings.cpp:47
std::string debug_truncate(const std::string &text)
Returns a truncated version of the text.
Definition: helper.cpp:157
size_t i
Definition: function.cpp:1057
Holds a 2D point.
Definition: point.hpp:24
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
unsigned screen_width
The screen resolution should be available for all widgets since their drawing method will depend on i...
Definition: settings.cpp:44
map_formula_callable & add(const std::string &key, const variant &value)
Definition: formula.cpp:41
boost::uint32_t decode_color(const std::string &color)
Converts a color string to a color.
Definition: helper.cpp:73
std::string vgettext(const char *msgid, const utils::string_map &symbols)
SDL_Rect create_rect(const int x, const int y, const int w, const int h)
Creates an empty SDL_Rect.
Definition: rect.cpp:28
GLsizeiptr size
Definition: glew.h:1649
Contains the SDL_Rect helper code.
void load_settings()
Loads the setting for the theme.
Definition: settings.cpp:484
unsigned screen_height
Definition: settings.cpp:45
std::vector< std::string > split(std::string const &val, const char c, const int flags)
Splits a (comma-)separated string into a vector of pieces.
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