2 // Copyright (C) 2008 by Martin Moracek
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.
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
32 #include "math/vector.h"
62 class RichText
: public Text
{
64 static const uint caretWidth
= 3;
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
);
83 const std::wstring
GetText(void) const;
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
);
104 void RedrawText(bool reset
);
107 * Datatypes for the tree used to store the styled rich text
112 typedef std::vector
<const GlyphInfo
*> GlyphVector
;
120 RichLeaf() : parent(NULL
) {}
121 RichLeaf(RichNode
* p
);
123 void ResetStyle(void);
126 typedef std::list
<RichLeaf
> LeavesList
;
128 struct RichNodeChild
{
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
;
143 ntPreDef
, // TODO: custom styles? extra would contain style name
144 ntLink
, /// the same as ntPreDef, but extra contains link id
159 std::string extra
; /// depends on the node type (link = id)
162 NodeChildVector children
;
168 * Datatypes for the look-up table
171 /// [text chunk, letter number]
172 typedef std::pair
<LeavesList::iterator
, uint
> LetterPair
;
173 typedef std::vector
<LetterPair
> LetterPairVector
;
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
)
193 friend bool operator < (int l
, const Letter
& r
)
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
;
212 GlyphPrepVector glyphs
;
213 LetterPairVector letters
; // needed to create the look-up table
218 uint wspace
; // number of spaces on line (for stretching)
219 HorizAlign align
; // line alignment
222 typedef std::vector
<GlyphLine
> GLineVector
;
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
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
243 Vector2u caret_
; /// caret position (row, col) in lines_
246 const GlyphInfo
* fillerGlyph_
;
247 AttribBufferSet
* caretAttribs_
;
248 IndexBuffer
* caretIndices_
;
249 EffectVars
* caretVars_
;
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
);