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