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__
27 #include <boost/shared_ptr.hpp>
29 #include <cairomm/cairomm.h>
30 #include <gtkmm/misc.h>
32 #include "gtkmm2ext/visibility.h"
34 class LIBGTKMM2EXT_API CairoCell
37 CairoCell(int32_t id);
38 virtual ~CairoCell() {}
40 int32_t id() const { return _id; }
42 virtual void render (Cairo::RefPtr<Cairo::Context>&) = 0;
44 double x() const { return bbox.x; }
45 double y() const { return bbox.y; }
46 double width() const { return bbox.width; }
47 double height() const { return bbox.height; }
49 void set_position (double x, double y) {
54 bool intersects (GdkRectangle& r) const {
55 return gdk_rectangle_intersect (&r, &bbox, 0);
58 bool covers (double x, double y) const {
59 return bbox.x <= x && bbox.x + bbox.width > x &&
60 bbox.y <= y && bbox.y + bbox.height > y;
63 double xpad() const { return _xpad; }
64 void set_xpad (double x) { _xpad = x; }
66 void set_visible (bool yn) { _visible = yn; }
67 bool visible() const { return _visible; }
68 virtual void set_size (Cairo::RefPtr<Cairo::Context>&) {}
77 class LIBGTKMM2EXT_API CairoFontDescription {
79 CairoFontDescription (const std::string& f,
88 CairoFontDescription (Pango::FontDescription&);
90 void apply (Cairo::RefPtr<Cairo::Context> context) {
91 context->select_font_face (face, _slant, _weight);
92 context->set_font_size (_size);
95 void set_size (double sz) { _size = sz; }
96 double size() const { return _size; }
98 Cairo::FontSlant slant() const { return _slant; }
99 void set_slant (Cairo::FontSlant sl) { _slant = sl; }
101 Cairo::FontWeight weight() const { return _weight; }
102 void set_weight (Cairo::FontWeight w) { _weight = w; }
106 Cairo::FontSlant _slant;
107 Cairo::FontWeight _weight;
111 class LIBGTKMM2EXT_API CairoTextCell : public CairoCell
114 CairoTextCell (int32_t id, double width_chars, boost::shared_ptr<CairoFontDescription> font = boost::shared_ptr<CairoFontDescription>());
117 virtual void set_size (Cairo::RefPtr<Cairo::Context>&);
119 boost::shared_ptr<CairoFontDescription> font() const { return _font; }
121 std::string get_text() const {
125 double width_chars() const { return _width_chars; }
126 void render (Cairo::RefPtr<Cairo::Context>&);
129 friend class CairoEditableText;
130 void set_width_chars (double wc) { _width_chars = wc; }
131 void set_text (const std::string& txt);
132 void set_font (boost::shared_ptr<CairoFontDescription> font) {
139 boost::shared_ptr<CairoFontDescription> _font;
144 class LIBGTKMM2EXT_API CairoCharCell : public CairoTextCell
147 CairoCharCell(int32_t id, char c);
149 void set_size (Cairo::RefPtr<Cairo::Context>& context);
152 class LIBGTKMM2EXT_API CairoEditableText : public Gtk::Misc
155 CairoEditableText (boost::shared_ptr<CairoFontDescription> font = boost::shared_ptr<CairoFontDescription>());
156 ~CairoEditableText ();
158 void add_cell (CairoCell*);
161 void start_editing (CairoCell*);
162 void stop_editing ();
164 void set_text (CairoTextCell* cell, const std::string&);
165 void set_width_chars (CairoTextCell* cell, uint32_t);
167 void set_draw_background (bool yn) { _draw_bg = yn; }
169 void set_colors (double cr, double cg, double cb, double ca) {
177 void set_edit_colors (double cr, double cg, double cb, double ca) {
185 void set_bg (double r, double g, double b, double a) {
193 double xpad() const { return _xpad; }
194 void set_xpad (double x) { _xpad = x; queue_resize(); }
195 double ypad() const { return _ypad; }
196 void set_ypad (double y) { _ypad = y; queue_resize(); }
198 double corner_radius() const { return _corner_radius; }
199 void set_corner_radius (double r) { _corner_radius = r; queue_draw (); }
201 boost::shared_ptr<CairoFontDescription> font() const { return _font; }
202 void set_font (boost::shared_ptr<CairoFontDescription> font);
203 void set_font (Pango::FontDescription& font);
205 sigc::signal<bool,GdkEventScroll*,CairoCell*> scroll;
206 sigc::signal<bool,GdkEventButton*,CairoCell*> button_press;
207 sigc::signal<bool,GdkEventButton*,CairoCell*> button_release;
210 bool on_expose_event (GdkEventExpose*);
211 bool on_button_press_event (GdkEventButton*);
212 bool on_button_release_event (GdkEventButton*);
213 void on_size_request (GtkRequisition*);
214 bool on_focus_in_event (GdkEventFocus*);
215 bool on_focus_out_event (GdkEventFocus*);
216 bool on_scroll_event (GdkEventScroll*);
217 void on_size_allocate (Gtk::Allocation&);
220 typedef std::vector<CairoCell*> CellMap;
223 boost::shared_ptr<CairoFontDescription> _font;
224 CairoCell* editing_cell;
226 double max_cell_width;
227 double max_cell_height;
228 double _corner_radius;
244 CairoCell* find_cell (uint32_t x, uint32_t y);
245 void queue_draw_cell (CairoCell* target);
246 void position_cells_and_get_bbox (GdkRectangle&);
247 void set_cell_sizes ();
250 #endif /* __libgtmm2ext_cairocell_h__ */