]> git.tdb.fi Git - libs/gltk.git/blob - source/text.h
Support different font colors in different states
[libs/gltk.git] / source / text.h
1 #ifndef MSP_GLTK_TEXT_H_
2 #define MSP_GLTK_TEXT_H_
3
4 #include <string>
5 #include <vector>
6 #include "state.h"
7
8 namespace Msp {
9 namespace GLtk {
10
11 class Geometry;
12 class Part;
13 class PartCache;
14 class Style;
15
16 /**
17 Stores and renders text.  Supports multiline text.
18 */
19 class Text
20 {
21 private:
22         struct Line
23         {
24                 unsigned start;
25                 unsigned length;
26                 unsigned width;
27         };
28
29         struct RenderData;
30         struct CoordsToGeomData;
31
32         const Style *style;
33         std::string text;
34         std::vector<Line> lines;
35
36 public:
37         Text();
38         Text(const Style &, const std::string &);
39
40         void set_style(const Style *);
41
42         unsigned get_width() const;
43         unsigned get_height() const;
44         void autosize(const Part &, Geometry &) const;
45
46         void set(const std::string &);
47         void erase(unsigned, unsigned);
48         void insert(unsigned, const std::string &);
49         const std::string &get() const { return text; }
50         unsigned size() const { return text.size(); }
51         unsigned get_n_lines() const { return lines.size(); }
52         unsigned get_line_length(unsigned) const;
53         void offset_to_coords(unsigned, unsigned &, unsigned &) const;
54         unsigned coords_to_offset(unsigned, unsigned) const;
55         Geometry coords_to_geometry(const Part &, const Geometry &, unsigned, unsigned, unsigned) const;
56
57         void build(const Part &, State, const Geometry &, PartCache &) const;
58         void build(const Part &, State, const Geometry &, unsigned, PartCache &) const;
59
60         Text &operator=(const std::string &);
61 private:
62         void find_lines();
63
64         template<typename T, void (Text::*)(unsigned, const Geometry &, T &) const>
65         void process_lines(const Part &, const Geometry &, unsigned, T &) const;
66
67         void build_line(unsigned, const Geometry &, RenderData &) const;
68         void coords_to_geom_line(unsigned, const Geometry &, CoordsToGeomData &) const;
69 };
70
71 } // namespace GLtk
72 } // namespace Msp
73
74 #endif