ardour
cairocell.h
Go to the documentation of this file.
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 
27 #include <boost/shared_ptr.hpp>
28 
29 #include <cairomm/cairomm.h>
30 #include <gtkmm/misc.h>
31 
32 #include "gtkmm2ext/visibility.h"
33 
35 {
36  public:
37  CairoCell(int32_t id);
38  virtual ~CairoCell() {}
39 
40  int32_t id() const { return _id; }
41 
42  virtual void render (Cairo::RefPtr<Cairo::Context>&) = 0;
43 
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; }
48 
49  void set_position (double x, double y) {
50  bbox.x = x;
51  bbox.y = y;
52  }
53 
54  bool intersects (GdkRectangle& r) const {
55  return gdk_rectangle_intersect (&r, &bbox, 0);
56  }
57 
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;
61  }
62 
63  double xpad() const { return _xpad; }
64  void set_xpad (double x) { _xpad = x; }
65 
66  void set_visible (bool yn) { _visible = yn; }
67  bool visible() const { return _visible; }
68  virtual void set_size (Cairo::RefPtr<Cairo::Context>&) {}
69 
70  protected:
71  int32_t _id;
72  GdkRectangle bbox;
73  bool _visible;
74  uint32_t _xpad;
75 };
76 
78  public:
79  CairoFontDescription (const std::string& f,
80  Cairo::FontSlant s,
81  Cairo::FontWeight w,
82  double sz)
83  : face (f)
84  , _slant (s)
85  , _weight (w)
86  , _size (sz)
87  {}
88  CairoFontDescription (Pango::FontDescription&);
89 
90  void apply (Cairo::RefPtr<Cairo::Context> context) {
91  context->select_font_face (face, _slant, _weight);
92  context->set_font_size (_size);
93  }
94 
95  void set_size (double sz) { _size = sz; }
96  double size() const { return _size; }
97 
98  Cairo::FontSlant slant() const { return _slant; }
99  void set_slant (Cairo::FontSlant sl) { _slant = sl; }
100 
101  Cairo::FontWeight weight() const { return _weight; }
102  void set_weight (Cairo::FontWeight w) { _weight = w; }
103 
104  private:
105  std::string face;
106  Cairo::FontSlant _slant;
107  Cairo::FontWeight _weight;
108  double _size;
109 };
110 
112 {
113  public:
116 
117  virtual void set_size (Cairo::RefPtr<Cairo::Context>&);
118 
120 
121  std::string get_text() const {
122  return _text;
123  }
124 
125  double width_chars() const { return _width_chars; }
126  void render (Cairo::RefPtr<Cairo::Context>&);
127 
128  protected:
129  friend class CairoEditableText;
130  void set_width_chars (double wc) { _width_chars = wc; }
131  void set_text (const std::string& txt);
133  _font = font;
134  }
135 
136  protected:
137  double _width_chars;
138  std::string _text;
140  double y_offset;
141  double x_offset;
142 };
143 
145 {
146  public:
147  CairoCharCell(int32_t id, char c);
148 
149  void set_size (Cairo::RefPtr<Cairo::Context>& context);
150 };
151 
152 class LIBGTKMM2EXT_API CairoEditableText : public Gtk::Misc
153 {
154 public:
156  ~CairoEditableText ();
157 
158  void add_cell (CairoCell*);
159  void clear_cells ();
160 
161  void start_editing (CairoCell*);
162  void stop_editing ();
163 
164  void set_text (CairoTextCell* cell, const std::string&);
165  void set_width_chars (CairoTextCell* cell, uint32_t);
166 
167  void set_draw_background (bool yn) { _draw_bg = yn; }
168 
169  void set_colors (double cr, double cg, double cb, double ca) {
170  r = cr;
171  g = cg;
172  b = cb;
173  a = ca;
174  queue_draw ();
175  }
176 
177  void set_edit_colors (double cr, double cg, double cb, double ca) {
178  edit_r = cr;
179  edit_g = cg;
180  edit_b = cb;
181  edit_a = ca;
182  queue_draw ();
183  }
184 
185  void set_bg (double r, double g, double b, double a) {
186  bg_r = r;
187  bg_g = g;
188  bg_b = b;
189  bg_a = a;
190  queue_draw ();
191  }
192 
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(); }
197 
198  double corner_radius() const { return _corner_radius; }
199  void set_corner_radius (double r) { _corner_radius = r; queue_draw (); }
200 
202  void set_font (boost::shared_ptr<CairoFontDescription> font);
203  void set_font (Pango::FontDescription& font);
204 
205  sigc::signal<bool,GdkEventScroll*,CairoCell*> scroll;
206  sigc::signal<bool,GdkEventButton*,CairoCell*> button_press;
207  sigc::signal<bool,GdkEventButton*,CairoCell*> button_release;
208 
209 protected:
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&);
218 
219 private:
220  typedef std::vector<CairoCell*> CellMap;
221 
222  CellMap cells;
225  bool _draw_bg;
229  double _xpad;
230  double _ypad;
231  double r;
232  double g;
233  double b;
234  double a;
235  double edit_r;
236  double edit_g;
237  double edit_b;
238  double edit_a;
239  double bg_r;
240  double bg_g;
241  double bg_b;
242  double bg_a;
243 
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 ();
248 };
249 
250 #endif /* __libgtmm2ext_cairocell_h__ */
boost::shared_ptr< CairoFontDescription > font() const
Definition: cairocell.h:119
std::string _text
Definition: cairocell.h:138
Cairo::FontSlant _slant
Definition: cairocell.h:106
virtual void set_size(Cairo::RefPtr< Cairo::Context > &)
Definition: cairocell.cc:133
double size() const
Definition: cairocell.h:96
virtual ~CairoCell()
Definition: cairocell.h:38
bool _visible
Definition: cairocell.h:73
void set_xpad(double x)
Definition: cairocell.h:194
std::vector< CairoCell * > CellMap
Definition: cairocell.h:220
void set_xpad(double x)
Definition: cairocell.h:64
double corner_radius() const
Definition: cairocell.h:198
LIBGTKMM2EXT_API
void set_font(boost::shared_ptr< CairoFontDescription > font)
Definition: cairocell.h:132
tuple f
Definition: signals.py:35
virtual void set_size(Cairo::RefPtr< Cairo::Context > &)
Definition: cairocell.h:68
boost::shared_ptr< CairoFontDescription > _font
Definition: cairocell.h:223
double height() const
Definition: cairocell.h:47
void set_edit_colors(double cr, double cg, double cb, double ca)
Definition: cairocell.h:177
void set_corner_radius(double r)
Definition: cairocell.h:199
double _corner_radius
Definition: cairocell.h:228
sigc::signal< bool, GdkEventScroll *, CairoCell * > scroll
Definition: cairocell.h:205
double max_cell_height
Definition: cairocell.h:227
sigc::signal< bool, GdkEventButton *, CairoCell * > button_press
Definition: cairocell.h:206
Cairo::FontWeight weight() const
Definition: cairocell.h:101
CairoCell * editing_cell
Definition: cairocell.h:224
double max_cell_width
Definition: cairocell.h:226
double y_offset
Definition: cairocell.h:140
int32_t id() const
Definition: cairocell.h:40
void set_position(double x, double y)
Definition: cairocell.h:49
bool covers(double x, double y) const
Definition: cairocell.h:58
void set_ypad(double y)
Definition: cairocell.h:196
void set_width_chars(double wc)
Definition: cairocell.h:130
std::string face
Definition: cairocell.h:105
double xpad() const
Definition: cairocell.h:193
uint32_t _xpad
Definition: cairocell.h:74
double x() const
Definition: cairocell.h:44
void set_size(double sz)
Definition: cairocell.h:95
Cairo::FontWeight _weight
Definition: cairocell.h:107
bool intersects(GdkRectangle &r) const
Definition: cairocell.h:54
void set_draw_background(bool yn)
Definition: cairocell.h:167
void set_bg(double r, double g, double b, double a)
Definition: cairocell.h:185
void set_colors(double cr, double cg, double cb, double ca)
Definition: cairocell.h:169
double width_chars() const
Definition: cairocell.h:125
virtual void render(Cairo::RefPtr< Cairo::Context > &)=0
GdkRectangle bbox
Definition: cairocell.h:72
boost::shared_ptr< CairoFontDescription > _font
Definition: cairocell.h:139
void set_visible(bool yn)
Definition: cairocell.h:66
bool visible() const
Definition: cairocell.h:67
void set_slant(Cairo::FontSlant sl)
Definition: cairocell.h:99
double _width_chars
Definition: cairocell.h:137
double width() const
Definition: cairocell.h:46
double y() const
Definition: cairocell.h:45
CairoFontDescription(const std::string &f, Cairo::FontSlant s, Cairo::FontWeight w, double sz)
Definition: cairocell.h:79
std::string get_text() const
Definition: cairocell.h:121
int32_t _id
Definition: cairocell.h:71
double ypad() const
Definition: cairocell.h:195
sigc::signal< bool, GdkEventButton *, CairoCell * > button_release
Definition: cairocell.h:207
boost::shared_ptr< CairoFontDescription > font() const
Definition: cairocell.h:201
void set_weight(Cairo::FontWeight w)
Definition: cairocell.h:102
void apply(Cairo::RefPtr< Cairo::Context > context)
Definition: cairocell.h:90
double xpad() const
Definition: cairocell.h:63
void set_text(CairoTextCell *cell, const std::string &)
Definition: cairocell.cc:281
Cairo::FontSlant slant() const
Definition: cairocell.h:98
double x_offset
Definition: cairocell.h:141