add extended info fields to clocks; clock tweaks
[ardour.git] / libs / gtkmm2ext / gtkmm2ext / cairocell.h
1 /*
2   Copyright (C) 2011 Paul Davis
3
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.
8
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.
13
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #ifndef __libgtmm2ext_cairocell_h__
21 #define __libgtmm2ext_cairocell_h__
22
23 #include <map>
24
25 #include <stdint.h>
26 #include <cairomm/cairomm.h>
27 #include <gtkmm.h>
28
29 class CairoCell
30 {
31 public:
32         CairoCell();
33         virtual ~CairoCell() {}
34
35         virtual void render (Cairo::RefPtr<Cairo::Context>&) = 0;
36
37         double x() const { return bbox.x; }
38         double y() const { return bbox.y; }
39         double width() const { return bbox.width; }
40         double height() const { return bbox.height; }
41
42         void set_position (double x, double y) {
43                 bbox.x = x;
44                 bbox.y = y;
45         }
46
47         bool intersects (GdkRectangle& r) const {
48                 return gdk_rectangle_intersect (&r, &bbox, 0);
49         }
50
51         bool covers (double x, double y) const {
52                 return bbox.x <= x && bbox.x + bbox.width > x &&
53                         bbox.y <= y && bbox.y + bbox.height > y;
54         }
55
56         double xpad() const { return _xpad; }
57         void   set_xpad (double x) { _xpad = x; }
58
59         void set_visible (bool yn) { _visible = yn; }
60         bool visible() const { return _visible; }
61         virtual void set_size (Glib::RefPtr<Pango::Context>&,
62                                const Pango::FontDescription&) {}
63
64 protected:
65         GdkRectangle bbox;
66         bool _visible;
67         uint32_t _xpad;
68 };
69
70 class CairoBarCell : public CairoCell
71 {
72 public:
73         CairoBarCell() {};
74
75         void render (Cairo::RefPtr<Cairo::Context>& context) {
76                 context->move_to (bbox.x, bbox.y);
77                 context->set_line_width (bbox.width);
78                 context->rel_line_to (0, bbox.height);
79                 context->stroke ();
80         }
81
82         void set_size (Glib::RefPtr<Pango::Context>& context,
83                        const Pango::FontDescription& font) {
84                 Pango::FontMetrics metrics = context->get_metrics (font);
85                 bbox.width = 2;
86                 bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE;
87         }
88
89 private:
90 };
91
92 class CairoColonCell : public CairoCell
93 {
94 public:
95         CairoColonCell() {};
96
97         void render (Cairo::RefPtr<Cairo::Context>& context);
98         void set_size (Glib::RefPtr<Pango::Context>& context,
99                        const Pango::FontDescription& font);
100 };
101
102 class CairoTextCell : public CairoCell
103 {
104 public:
105         CairoTextCell (double  width_chars);
106         void set_size (Glib::RefPtr<Pango::Context>&, const Pango::FontDescription&);
107
108         void   set_text (const std::string& txt);
109
110         std::string get_text() const {
111                 return layout->get_text ();
112         }
113         double width_chars() const { return _width_chars; }
114         void render (Cairo::RefPtr<Cairo::Context>&);
115
116 protected:
117         double _width_chars;
118         Glib::RefPtr<Pango::Layout> layout;
119 };
120
121 class CairoEditableText : public Gtk::Misc
122 {
123 public:
124         CairoEditableText ();
125         ~CairoEditableText ();
126
127         void add_cell (uint32_t id, CairoCell*);
128         CairoCell* get_cell (uint32_t id);
129
130         void start_editing (uint32_t id);
131         void stop_editing ();
132
133         void set_text (uint32_t id, const std::string& text);
134         void set_text (CairoTextCell* cell, const std::string&);
135
136         void set_colors (double cr, double cg, double cb, double ca) {
137                 r = cr;
138                 g = cg;
139                 b = cb;
140                 a = ca;
141                 queue_draw ();
142         }
143
144         void set_edit_colors (double cr, double cg, double cb, double ca) {
145                 edit_r = cr;
146                 edit_g = cg;
147                 edit_b = cb;
148                 edit_a = ca;
149                 queue_draw ();
150         }
151
152         void set_bg (double r, double g, double b, double a) {
153                 bg_r = r;
154                 bg_g = g;
155                 bg_b = b;
156                 bg_a = a;
157                 queue_draw ();
158         }
159
160         void set_font (const std::string& str);
161         void set_font (const Pango::FontDescription&);
162
163         double xpad() const { return _xpad; }
164         void set_xpad (double x) { _xpad = x; queue_resize(); }
165         double ypad() const { return _ypad; }
166         void set_ypad (double y) { _ypad = y; queue_resize(); }
167         
168         double corner_radius() const { return _corner_radius; }
169         void set_corner_radius (double r) { _corner_radius = r; queue_draw (); }
170         
171         sigc::signal<bool,GdkEventScroll*,uint32_t> scroll;
172         sigc::signal<bool,GdkEventButton*,uint32_t> button_press;
173         sigc::signal<bool,GdkEventButton*,uint32_t> button_release;
174
175 protected:
176         bool on_expose_event (GdkEventExpose*);
177         bool on_button_press_event (GdkEventButton*);
178         bool on_button_release_event (GdkEventButton*);
179         void on_size_request (GtkRequisition*);
180         void on_size_allocate (Gtk::Allocation&);
181         bool on_focus_in_event (GdkEventFocus*);
182         bool on_focus_out_event (GdkEventFocus*);
183         bool on_scroll_event (GdkEventScroll*);
184
185 private:
186         typedef std::map<uint32_t,CairoCell*> CellMap;
187
188         CellMap cells;
189         Pango::FontDescription font;
190         uint32_t editing_id;
191         double width;
192         double max_cell_height;
193         double height;
194         double _corner_radius;
195         double _xpad;
196         double _ypad;
197         double r;
198         double g;
199         double b;
200         double a;
201         double edit_r;
202         double edit_g;
203         double edit_b;
204         double edit_a;
205         double bg_r;
206         double bg_g;
207         double bg_b;
208         double bg_a;
209
210         CairoCell* find_cell (uint32_t x, uint32_t y, uint32_t& cell_id);
211         void queue_draw_cell (CairoCell* target);
212 };
213
214 #endif /* __libgtmm2ext_cairocell_h__ */