Initial commit, includes Lua with broken Luabind as a backup for branching purposes
[terrastrategy.git] / include / gui / richtext.h
blobdafb79d035c5796bb3d947d32f115a7d1541ac03
1 //
2 // Copyright (C) 2008 by Martin Moracek
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 /**
20 * @file richtext.h
22 * Ble.
25 #pragma once
27 #include <list>
28 #include <vector>
29 #include <string>
31 #include "types.h"
32 #include "math/vector.h"
34 #include "text.h"
36 class TiXmlElement;
38 namespace tre {
40 enum StyleSheet {
41 ssBase = 0,
42 ssLink,
43 ssLinkHover,
44 ssLinkPressed,
45 ssEmph,
46 ssLast
49 enum CaretTarget {
50 ctTextStart,
51 ctTextEnd,
52 ctLineStart,
53 ctLineEnd,
54 ctWordLeft,
55 ctWordRight,
56 ctLeft,
57 ctRight,
58 ctUp,
59 ctDown
62 class RichText : public Text {
63 public:
64 static const uint caretWidth = 3;
66 public:
67 RichText();
68 ~RichText();
70 void SetStyleSheet(StyleSheet sheet, const TextStyle & style);
71 const TextStyle & GetStyleSheet(StyleSheet sheet) const
72 {return styles_[sheet];}
74 void ResetStyles(void);
76 void SetStyle(const TextStyle & style);
77 const TextStyle & GetStyle(void) const {return styles_[ssBase];}
79 void SetText(const std::string & text);
80 void SetText(const std::wstring & text);
82 void ClearText(void);
83 const std::wstring GetText(void) const;
85 // auto-format
86 bool GetWrap(void) const {return wrap_;}
87 void SetWrap(bool wrap);
89 void SetParagraphSpacing(int space);
90 void SetLineSpacing(int space);
92 const std::wstring GetWord(const Vector2f & pos) const;
93 const std::string GetLink(const Vector2f & pos) const;
95 void SetHoverLink(const std::string & id);
96 void SetPressedLink(const std::string & id);
98 void ShowCaret(bool show);
99 bool IsCaretVisible(void) const {return showCaret_;}
100 void SetCaretToCursor(const Vector2f & pos);
101 void MoveCaret(CaretTarget pos);
103 protected:
104 void RedrawText(bool reset);
107 * Datatypes for the tree used to store the styled rich text
109 private:
110 struct RichNode;
112 typedef std::vector<const GlyphInfo*> GlyphVector;
114 struct RichLeaf {
115 RichNode * parent;
116 std::wstring text;
117 GlyphVector glyphs;
118 TextStyle style;
120 RichLeaf() : parent(NULL) {}
121 RichLeaf(RichNode * p);
123 void ResetStyle(void);
126 typedef std::list<RichLeaf> LeavesList;
128 struct RichNodeChild {
129 union {
130 RichNode * node;
131 RichLeaf * leaf;
133 bool isNode;
135 RichNodeChild() : node(NULL), isNode(true) {}
136 RichNodeChild(RichNode * n) : node(n), isNode(true) {}
137 RichNodeChild(RichLeaf * l) : leaf(l), isNode(false) {}
140 typedef std::vector<RichNodeChild> NodeChildVector;
142 enum NodeType {
143 ntPreDef, // TODO: custom styles? extra would contain style name
144 ntLink, /// the same as ntPreDef, but extra contains link id
145 ntColour,
146 ntSize,
147 ntFontStyle
150 struct RichNode {
151 union {
152 TextStyle * predef;
153 float colour[4];
154 float size;
155 FontStyle fstyle;
157 NodeType type;
159 std::string extra; /// depends on the node type (link = id)
161 RichNode * parent;
162 NodeChildVector children;
164 ~RichNode();
168 * Datatypes for the look-up table
170 private:
171 /// [text chunk, letter number]
172 typedef std::pair<LeavesList::iterator, uint> LetterPair;
173 typedef std::vector<LetterPair> LetterPairVector;
175 struct Letter {
176 int start;
177 uint width;
179 LetterPair letter;
181 Letter(uint s, uint w, LetterPair p) : start(s), width(w), letter(p) {}
183 bool operator < (const Letter & r) const
185 return start < r.start;
188 friend bool operator < (const Letter & l, int r)
190 return l.start < r;
193 friend bool operator < (int l, const Letter & r)
195 return l < r.start;
199 typedef std::vector<Letter> LetterVector;
201 /// letters x [line offset, line height]
202 typedef std::pair<LetterVector, Vector2u> LinePair;
203 typedef std::vector<LinePair> LineVector;
205 class LineLess;
208 * Text processing
210 private:
211 struct GlyphLine {
212 GlyphPrepVector glyphs;
213 LetterPairVector letters; // needed to create the look-up table
214 int offset;
215 int baseline;
216 uint height;
217 uint width;
218 uint wspace; // number of spaces on line (for stretching)
219 HorizAlign align; // line alignment
222 typedef std::vector<GlyphLine> GLineVector;
225 * Private attributes
227 private:
228 /// predefined styles
229 TextStyle styles_[ssLast];
231 // auto-format parameters
232 bool wrap_; /// word wrap
233 int lineSpace_; /// distance between two lines
234 int parSpace_; /// distance between two paragraphs
236 // style tree
237 RichNode root_; /// root node of the style tree
238 LeavesList leaves_; /// list of leaves of the style tree
240 LineVector lines_; /// look-up table for position (x,y) based search
242 // caret
243 Vector2u caret_; /// caret position (row, col) in lines_
244 bool showCaret_;
246 const GlyphInfo * fillerGlyph_;
247 AttribBufferSet * caretAttribs_;
248 IndexBuffer * caretIndices_;
249 EffectVars * caretVars_;
251 private:
252 const LetterPair * GetLetter(const Vector2f & pos) const;
254 void ParseText(TiXmlElement & root, RichNode * rnode, bool & prev_spc);
256 void RedrawCaret(void);
258 void CalcLineHeight(GlyphLine & line);
259 void WriteText(const GLineVector & lines, uint width, uint height);
260 void WriteLine(const GlyphPrepVector & glyphs, const LetterPairVector
261 & letters, int x_off, int y_off, uint ws_fill, uint ws);