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>&,
62 const Pango::FontDescription&) {}
70 class CairoBarCell : public CairoCell
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);
82 void set_size (Glib::RefPtr<Pango::Context>& context,
83 const Pango::FontDescription& font) {
84 Pango::FontMetrics metrics = context->get_metrics (font);
86 bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE;
92 class CairoColonCell : public CairoCell
97 void render (Cairo::RefPtr<Cairo::Context>& context);
98 void set_size (Glib::RefPtr<Pango::Context>& context,
99 const Pango::FontDescription& font);
102 class CairoTextCell : public CairoCell
105 CairoTextCell (double width_chars);
106 void set_size (Glib::RefPtr<Pango::Context>&, const Pango::FontDescription&);
108 void set_text (const std::string& txt);
110 std::string get_text() const {
111 return layout->get_text ();
113 double width_chars() const { return _width_chars; }
114 void render (Cairo::RefPtr<Cairo::Context>&);
118 Glib::RefPtr<Pango::Layout> layout;
121 class CairoEditableText : public Gtk::Misc
124 CairoEditableText ();
125 ~CairoEditableText ();
127 void add_cell (uint32_t id, CairoCell*);
128 CairoCell* get_cell (uint32_t id);
130 void start_editing (uint32_t id);
131 void stop_editing ();
133 void set_text (uint32_t id, const std::string& text);
134 void set_text (CairoTextCell* cell, const std::string&);
136 void set_colors (double cr, double cg, double cb, double ca) {
144 void set_edit_colors (double cr, double cg, double cb, double ca) {
152 void set_bg (double r, double g, double b, double a) {
160 void set_font (const std::string& str);
161 void set_font (const Pango::FontDescription&);
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(); }
168 double corner_radius() const { return _corner_radius; }
169 void set_corner_radius (double r) { _corner_radius = r; queue_draw (); }
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;
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*);
186 typedef std::map<uint32_t,CairoCell*> CellMap;
189 Pango::FontDescription font;
192 double max_cell_height;
194 double _corner_radius;
210 CairoCell* find_cell (uint32_t x, uint32_t y, uint32_t& cell_id);
211 void queue_draw_cell (CairoCell* target);
214 #endif /* __libgtmm2ext_cairocell_h__ */