2 Copyright (C) 2011 Paul Davis
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.
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.
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.
20 #ifndef __libgtmm2ext_cairocell_h__
21 #define __libgtmm2ext_cairocell_h__
26 #include <cairomm/cairomm.h>
33 virtual ~CairoCell() {}
35 virtual void render (Cairo::RefPtr<Cairo::Context>&) = 0;
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; }
42 void set_position (double x, double y) {
47 bool intersects (GdkRectangle& r) const {
48 return gdk_rectangle_intersect (&r, &bbox, 0);
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;
56 double xpad() const { return _xpad; }
57 void set_xpad (double x) { _xpad = x; }
59 void set_visible (bool yn) { _visible = yn; }
60 bool visible() const { return _visible; }
61 virtual void set_size (Glib::RefPtr<Pango::Context>&, const Pango::FontDescription&) {}
69 class CairoBarCell : public CairoCell
74 void render (Cairo::RefPtr<Cairo::Context>& context) {
75 context->move_to (bbox.x, bbox.y);
76 context->set_line_width (bbox.width);
77 context->rel_line_to (0, bbox.height);
81 void set_size (Glib::RefPtr<Pango::Context>& context, const Pango::FontDescription& font) {
82 Pango::FontMetrics metrics = context->get_metrics (font);
84 bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE;
90 class CairoColonCell : public CairoCell
95 void render (Cairo::RefPtr<Cairo::Context>& context) {
96 /* two very small circles, at 1/3 and 2/3 of the height */
97 context->arc (bbox.x, bbox.y + (bbox.height/3.0), 1.5, 0.0, M_PI*2.0);
99 context->arc (bbox.x, bbox.y + (2.0 * bbox.height/3.0), 1.5, 0.0, M_PI*2.0);
103 void set_size (Glib::RefPtr<Pango::Context>& context, const Pango::FontDescription& font) {
104 Pango::FontMetrics metrics = context->get_metrics (font);
106 bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE;
112 class CairoTextCell : public CairoCell
115 CairoTextCell (uint32_t width_chars);
117 void set_size (Glib::RefPtr<Pango::Context>&, const Pango::FontDescription&);
119 void set_text (const std::string& txt) {
120 layout->set_text (txt);
122 std::string get_text() const {
123 return layout->get_text ();
125 uint32_t width_chars() const { return _width_chars; }
127 void render (Cairo::RefPtr<Cairo::Context>&);
130 uint32_t _width_chars;
131 Glib::RefPtr<Pango::Layout> layout;
134 class CairoEditableText : public Gtk::Misc
137 CairoEditableText ();
139 void add_cell (uint32_t id, CairoCell*);
140 CairoCell* get_cell (uint32_t id);
142 void set_text (uint32_t id, const std::string& text);
144 void set_colors (double cr, double cg, double cb, double ca) {
152 void set_edit_colors (double cr, double cg, double cb, double ca) {
160 void set_bg (double r, double g, double b, double a) {
167 bool on_expose_event (GdkEventExpose*);
168 bool on_key_press_event (GdkEventKey*);
169 bool on_key_release_event (GdkEventKey*);
170 bool on_button_press_event (GdkEventButton*);
171 bool on_button_release_event (GdkEventButton*);
172 void on_size_request (GtkRequisition*);
173 void on_size_allocate (Gtk::Allocation&);
174 bool on_focus_in_event (GdkEventFocus*);
175 bool on_focus_out_event (GdkEventFocus*);
177 void set_font (const std::string& str) {
178 font = Pango::FontDescription (str);
182 typedef std::map<uint32_t,CairoCell*> CellMap;
185 Pango::FontDescription font;
187 uint32_t editing_pos;
189 double max_cell_height;
191 double corner_radius;
208 CairoCell* find_cell (uint32_t x, uint32_t y, uint32_t& cell_id);
209 void edit_next_cell ();
210 void queue_draw_cell (CairoCell* target);
211 void set_text (CairoTextCell* cell, const std::string&);
214 #endif /* __libgtmm2ext_cairocell_h__ */