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