]> git.tdb.fi Git - libs/gltk.git/blob - source/text.h
6ddbb231b37871089f90e6cc90ecfa3ba1a735e9
[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 bytes;
26                 unsigned length;
27                 unsigned width;
28         };
29
30         struct RenderData;
31         struct CoordsToGeomData;
32
33         const Style *style;
34         std::string text;
35         std::vector<Line> lines;
36
37 public:
38         Text();
39         Text(const Style &, const std::string &);
40
41         void set_style(const Style *);
42
43         unsigned get_width() const;
44         unsigned get_height() const;
45         void autosize(const Part &, Geometry &) const;
46
47         void set(const std::string &);
48         void erase(unsigned, unsigned);
49         void insert(unsigned, const std::string &);
50         const std::string &get() const { return text; }
51         unsigned size() const { return text.size(); }
52         unsigned get_n_lines() const { return lines.size(); }
53         unsigned get_visible_lines(const Part &, const Geometry &, unsigned *) const;
54         unsigned get_line_length(unsigned) const;
55         unsigned move_offset(unsigned, int) const;
56         void offset_to_coords(unsigned, unsigned &, unsigned &) const;
57         unsigned coords_to_offset(unsigned, unsigned) const;
58         Geometry coords_to_geometry(const Part &, const Geometry &, unsigned, unsigned, unsigned) const;
59
60         void build(const Part &, State, const Geometry &, PartCache &) const;
61         void build(const Part &, State, const Geometry &, unsigned, PartCache &) const;
62
63         Text &operator=(const std::string &);
64 private:
65         void find_lines();
66         unsigned count_characters(unsigned, unsigned) const;
67         void check_alignment(unsigned) const;
68
69         template<typename T>
70         void process_lines(const Part &, const Geometry &, unsigned, void (Text::*)(unsigned, const Geometry &, T &) const, T &) const;
71
72         void build_line(unsigned, const Geometry &, RenderData &) const;
73         void coords_to_geom_line(unsigned, const Geometry &, CoordsToGeomData &) const;
74 };
75
76 } // namespace GLtk
77 } // namespace Msp
78
79 #endif