The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
help_text_area.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[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 HELP_TEXT_AREA_HPP
16 #define HELP_TEXT_AREA_HPP
17 
18 #include <list> // for list
19 #include <string> // for string
20 #include <utility> // for pair
21 #include <SDL_video.h> // for SDL_Color (ptr only), etc
22 #include "font.hpp" // for NORMAL_COLOR
23 #include "sdl/utils.hpp" // for surface
24 #include "widgets/scrollarea.hpp" // for scrollarea
25 class CVideo;
26 class config;
27 namespace help { struct section; }
28 namespace help { struct topic; }
29 
30 namespace help {
31 
32 
33 /// The area where the content is shown in the help browser.
35 {
36 public:
38  /// Display the topic.
39  void show_topic(const topic &t);
40 
41  /// Return the ID that is cross-referenced at the (screen)
42  /// coordinates x, y. If no cross-reference is there, return the
43  /// empty string.
44  std::string ref_at(const int x, const int y);
45 
46 protected:
47  virtual void scroll(unsigned int pos);
48  virtual void set_inner_location(const SDL_Rect& rect);
49 
50 private:
52  /// Convert a string to an alignment. Throw parse_error if
53  /// unsuccessful.
55 
56  /// An item that is displayed in the text area. Contains the surface
57  /// that should be blitted along with some other information.
58  struct item {
59 
60  item(surface surface, int x, int y, const std::string& text="",
61  const std::string& reference_to="", bool floating=false,
62  bool box=false, ALIGNMENT alignment=HERE);
63 
64  item(surface surface, int x, int y,
65  bool floating, bool box=false, ALIGNMENT=HERE);
66 
67  /// Relative coordinates of this item.
68  SDL_Rect rect;
69 
70  surface surf;
71 
72  // If this item contains text, this will contain that text.
74 
75  // If this item contains a cross-reference, this is the id
76  // of the referenced topic.
78 
79  // If this item is floating, that is, if things should be filled
80  // around it.
81  bool floating;
82  bool box;
84  };
85 
86  /// Function object to find an item at the specified coordinates.
87  class item_at {
88  public:
89  item_at(const int x, const int y) : x_(x), y_(y) {}
90  bool operator()(const item&) const;
91  private:
92  const int x_, y_;
93  };
94 
95  /// Update the vector with the items of the shown topic, creating
96  /// surfaces for everything and putting things where they belong.
97  void set_items();
98 
99  // Create appropriate items from configs. Items will be added to the
100  // internal vector. These methods check that the necessary
101  // attributes are specified.
102  void handle_ref_cfg(const config &cfg);
103  void handle_img_cfg(const config &cfg);
104  void handle_bold_cfg(const config &cfg);
105  void handle_italic_cfg(const config &cfg);
106  void handle_header_cfg(const config &cfg);
107  void handle_jump_cfg(const config &cfg);
108  void handle_format_cfg(const config &cfg);
109 
110  void draw_contents();
111 
112  /// Add an item with text. If ref_dst is something else than the
113  /// empty string, the text item will be underlined to show that it
114  /// is a cross-reference. The item will also remember what the
115  /// reference points to. If font_size is below zero, the default
116  /// will be used.
117  void add_text_item(const std::string& text, const std::string& ref_dst="",
118  bool broken_link = false,
119  int font_size=-1, bool bold=false, bool italic=false,
120  SDL_Color color=font::NORMAL_COLOR);
121 
122  /// Add an image item with the specified attributes.
123  void add_img_item(const std::string& path, const std::string& alignment, const bool floating,
124  const bool box);
125 
126  /// Move the current input point to the next line.
127  void down_one_line();
128 
129  /// Adjust the heights of the items in the last row to make it look
130  /// good .
131  void adjust_last_row();
132 
133  /// Return the width that remain on the line the current input point is at.
134  int get_remaining_width();
135 
136  /// Return the least x coordinate at which something of the
137  /// specified height can be drawn at the specified y coordinate
138  /// without interfering with floating images.
139  int get_min_x(const int y, const int height=0);
140 
141  /// Analogous with get_min_x but return the maximum X.
142  int get_max_x(const int y, const int height=0);
143 
144  /// Find the lowest y coordinate where a floating img of the
145  /// specified width and at the specified x coordinate can be
146  /// placed. Start looking at desired_y and continue downwards. Only
147  /// check against other floating things, since text and inline
148  /// images only can be above this place if called correctly.
149  int get_y_for_floating_img(const int width, const int x, const int desired_y);
150 
151  /// Add an item to the internal list, update the locations and row
152  /// height.
153  void add_item(const item& itm);
154 
155  std::list<item> items_;
156  std::list<item *> last_row_;
159  const int title_spacing_;
160  // The current input location when creating items.
161  std::pair<int, int> curr_loc_;
162  const unsigned min_row_height_;
164  /// The height of all items in total.
166 };
167 
168 } // end namespace help
169 
170 #endif
int get_max_x(const int y, const int height=0)
Analogous with get_min_x but return the maximum X.
help_text_area(CVideo &video, const section &toplevel)
virtual void set_inner_location(const SDL_Rect &rect)
void add_img_item(const std::string &path, const std::string &alignment, const bool floating, const bool box)
Add an image item with the specified attributes.
int pos
Definition: formula.cpp:800
A section contains topics and sections along with title and ID.
Definition: help_impl.hpp:143
int get_remaining_width()
Return the width that remain on the line the current input point is at.
int get_y_for_floating_img(const int width, const int x, const int desired_y)
Find the lowest y coordinate where a floating img of the specified width and at the specified x coord...
Definition: video.hpp:58
help::section toplevel
Definition: help_impl.cpp:66
CVideo & video() const
Definition: widget.hpp:83
std::list< item * > last_row_
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
void add_text_item(const std::string &text, const std::string &ref_dst="", bool broken_link=false, int font_size=-1, bool bold=false, bool italic=false, SDL_Color color=font::NORMAL_COLOR)
Add an item with text.
void set_items()
Update the vector with the items of the shown topic, creating surfaces for everything and putting thi...
const SDL_Color NORMAL_COLOR
Definition: font.cpp:564
GLdouble GLdouble t
Definition: glew.h:1366
std::string bold(const std::string &s)
Definition: help_impl.hpp:395
topic const * shown_topic_
GLsizei const char ** path
Definition: glew.h:4654
void add_item(const item &itm)
Add an item to the internal list, update the locations and row height.
std::string ref_at(const int x, const int y)
Return the ID that is cross-referenced at the (screen) coordinates x, y.
The area where the content is shown in the help browser.
virtual void scroll(unsigned int pos)
std::list< item > items_
const section & toplevel_
void handle_ref_cfg(const config &cfg)
const unsigned min_row_height_
void handle_img_cfg(const config &cfg)
GLuint color
Definition: glew.h:5801
void handle_header_cfg(const config &cfg)
An item that is displayed in the text area.
void handle_italic_cfg(const config &cfg)
void handle_bold_cfg(const config &cfg)
void show_topic(const topic &t)
Display the topic.
void adjust_last_row()
Adjust the heights of the items in the last row to make it look good .
item_at(const int x, const int y)
std::pair< int, int > curr_loc_
item(surface surface, int x, int y, const std::string &text="", const std::string &reference_to="", bool floating=false, bool box=false, ALIGNMENT alignment=HERE)
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
int get_min_x(const int y, const int height=0)
Return the least x coordinate at which something of the specified height can be drawn at the specifie...
void down_one_line()
Move the current input point to the next line.
GLint GLint GLint GLint GLint GLint GLsizei GLsizei height
Definition: glew.h:1220
void handle_format_cfg(const config &cfg)
bool operator()(const item &) const
A topic contains a title, an id and some text.
Definition: help_impl.hpp:111
ALIGNMENT str_to_align(const std::string &s)
Convert a string to an alignment.
GLint GLint GLint GLint GLint GLint GLsizei width
Definition: glew.h:1220
Definition: help.cpp:57
SDL_Rect rect
Relative coordinates of this item.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLdouble s
Definition: glew.h:1358
const int font_size
int contents_height_
The height of all items in total.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
void handle_jump_cfg(const config &cfg)
Function object to find an item at the specified coordinates.