79b5a673b45ccc9422fc6c10eb6f2d899b40d68e
[ardour.git] / libs / gtkmm2ext / cairocell.cc
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 #include <algorithm>
21 #include <cmath>
22 #include <iostream>
23
24 #include "gtkmm2ext/cairocell.h"
25 #include "gtkmm2ext/utils.h"
26
27 using std::string;
28 using std::map;
29 using std::max;
30 using std::cerr;
31 using std::endl;
32 using namespace Gtkmm2ext;
33
34 static const double cairo_font_fudge = 1.5;
35
36 CairoFontDescription::CairoFontDescription (Pango::FontDescription& fd)
37 {
38         _size = cairo_font_fudge * (fd.get_size() / PANGO_SCALE);
39
40         switch (fd.get_style()) {
41         case Pango::STYLE_NORMAL:
42                 slant = Cairo::FONT_SLANT_NORMAL;
43                 break;
44         case Pango::STYLE_OBLIQUE:
45                 slant = Cairo::FONT_SLANT_OBLIQUE;
46                 break;
47         case Pango::STYLE_ITALIC:
48                 slant = Cairo::FONT_SLANT_ITALIC;
49                 break;
50         }
51
52         switch (fd.get_weight()) {
53         case Pango::WEIGHT_ULTRALIGHT:
54                 weight = Cairo::FONT_WEIGHT_NORMAL;
55                 break;
56
57         case Pango::WEIGHT_LIGHT:
58                 weight = Cairo::FONT_WEIGHT_NORMAL;
59                 break;
60
61         case Pango::WEIGHT_NORMAL:
62                 weight = Cairo::FONT_WEIGHT_NORMAL;
63                 break;
64
65         case Pango::WEIGHT_SEMIBOLD:
66                 weight = Cairo::FONT_WEIGHT_BOLD;
67                 break;
68
69         case Pango::WEIGHT_BOLD:
70                 weight = Cairo::FONT_WEIGHT_BOLD;
71                 break;
72
73         case Pango::WEIGHT_ULTRABOLD:
74                 weight = Cairo::FONT_WEIGHT_BOLD;
75                 break;
76
77         case Pango::WEIGHT_HEAVY:
78                 weight = Cairo::FONT_WEIGHT_BOLD;
79                 break;
80
81         }
82
83         face = fd.get_family();
84 }       
85
86 CairoCell::CairoCell (int32_t id)
87         : _id (id)
88         , _visible (true)
89         , _xpad (0)
90 {
91         bbox.x = 0;
92         bbox.y = 0;
93         bbox.width = 0;
94         bbox.height = 0;
95 }
96
97 CairoTextCell::CairoTextCell (int32_t id, double wc, boost::shared_ptr<CairoFontDescription> font)
98         : CairoCell (id)
99         , _width_chars (wc)
100         , _font (font)
101         , y_offset (0)
102         , x_offset (0)
103 {
104 }
105
106 void
107 CairoTextCell::set_text (const std::string& txt)
108 {
109         _text = txt;
110 }
111
112 void
113 CairoTextCell::render (Cairo::RefPtr<Cairo::Context>& context)
114 {
115         if (!_visible || _width_chars == 0) {
116                 return;
117         }
118
119         _font->apply (context);
120         context->move_to (bbox.x, bbox.y + bbox.height + y_offset);
121         context->show_text (_text);
122 }
123
124 void
125 CairoTextCell::set_size (Cairo::RefPtr<Cairo::Context>& context)
126 {
127         const uint32_t lim = (uint32_t) ceil (_width_chars);
128         char buf[lim+1];
129         uint32_t n;
130         double max_width = 0.0;
131         double max_height = 0.0;
132         Cairo::TextExtents ext;
133         double bsum = 0;
134
135         buf[lim] = '\0';
136
137         _font->apply (context);
138
139         for (int digit = 0; digit < 10; digit++) {
140
141                 for (n = 0; n < lim; ++n) {
142                         buf[n] = '0' + digit; 
143                 }
144                 
145                 context->get_text_extents (buf, ext);
146                 
147                 max_width = max (ext.width + ext.x_bearing, max_width);
148                 max_height = max (ext.height, max_height);
149                 bsum += ext.x_bearing;
150         }
151
152         /* add the average x-bearing for all digits as right hand side padding */
153
154         bbox.width = max_width + (bsum/10.0);
155
156         /* some fonts and some digits get their extents computed "too small", so fudge this
157            by adding 2
158         */
159         bbox.height = max_height;
160 }
161
162 CairoCharCell::CairoCharCell (int32_t id, char c)
163         : CairoTextCell (id, 1)
164 {
165         _text = c;
166 }
167
168 void
169 CairoCharCell::set_size (Cairo::RefPtr<Cairo::Context>& context)
170 {
171         Cairo::TextExtents ext;
172
173         _font->apply (context);
174         
175         {
176                 const char* buf = "8";
177                 context->get_text_extents (buf, ext);
178                 /* same height as an "8" */
179                 bbox.height = ext.height;
180         }
181
182         {
183                 const char* buf = ":";
184                 context->get_text_extents (buf, ext);
185                 bbox.width = ext.width + (2.0 * ext.x_bearing);
186                 /* center vertically */
187                 y_offset = (ext.height - bbox.height) / 2.0;
188         }
189 }
190
191 CairoEditableText::CairoEditableText (boost::shared_ptr<CairoFontDescription> font)
192         : editing_cell (0)
193         , _draw_bg (true)
194         , max_cell_width (0)
195         , max_cell_height (0)
196         , _corner_radius (9)
197         , _xpad (0)
198         , _ypad (0)
199 {
200         set_font (font);
201
202         add_events (Gdk::POINTER_MOTION_HINT_MASK | Gdk::SCROLL_MASK | Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK |
203                     Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::SCROLL_MASK);
204         set_flags (Gtk::CAN_FOCUS);
205
206         set_can_default (true);
207         set_receives_default (true);
208 }
209
210 CairoEditableText::~CairoEditableText ()
211 {
212         /* we don't own cells */
213 }
214
215 bool
216 CairoEditableText::on_scroll_event (GdkEventScroll* ev)
217 {
218         CairoCell* cell = find_cell (ev->x, ev->y);
219
220         if (cell) {
221                 return scroll (ev, cell);
222         }
223
224         return false;
225 }
226
227 bool
228 CairoEditableText::on_focus_in_event (GdkEventFocus* ev)
229 {
230         return false;
231 }
232
233 bool
234 CairoEditableText::on_focus_out_event (GdkEventFocus* ev)
235 {
236         if (editing_cell) {
237                 queue_draw_cell (editing_cell);
238                 editing_cell = 0;
239         }
240         return false;
241 }
242
243 void
244 CairoEditableText::add_cell (CairoCell* cell)
245 {
246         cells.push_back (cell);
247         
248         CairoTextCell* tc = dynamic_cast<CairoTextCell*>(cell);
249
250         if (tc) {
251                 tc->set_font (_font);
252         }
253
254         queue_resize ();
255 }
256
257 void
258 CairoEditableText::clear_cells ()
259 {
260         cells.clear ();
261         queue_resize ();
262 }
263
264 void
265 CairoEditableText::set_width_chars (CairoTextCell* cell, uint32_t wc)
266 {
267         if (cell) {
268                 cell->set_width_chars (wc);
269                 queue_resize ();
270         }
271 }
272
273 void
274 CairoEditableText::set_text (CairoTextCell* cell, const string& text)
275 {
276         cell->set_text (text);
277         queue_draw_cell (cell);
278 }
279
280 bool
281 CairoEditableText::on_expose_event (GdkEventExpose* ev)
282 {
283         Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
284
285         if (cells.empty()) {
286                 return true;
287         }
288
289         context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
290         context->clip ();
291
292         Gtk::Allocation alloc = get_allocation ();
293         double width = alloc.get_width();
294         double height = alloc.get_height ();
295                 
296         if (_draw_bg) {
297                 context->set_source_rgba (bg_r, bg_g, bg_b, bg_a);
298                 if (_corner_radius) {
299                         rounded_rectangle (context, 0, 0, width, height, _corner_radius);
300                 } else {
301                         context->rectangle (0, 0, width, height);
302                 }
303                 context->fill ();
304         }
305         
306         for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
307
308                 CairoCell* cell = (*i);
309
310                 /* is cell inside the expose area?
311                  */
312                 
313                 if (cell->intersects (ev->area)) {
314                         if (cell == editing_cell) {
315                                 context->set_source_rgba (edit_r, edit_b, edit_g, edit_a);
316                         } else {
317                                 context->set_source_rgba (r, g, b, a);
318                         }
319
320                         cell->render (context);
321                 }
322         }
323
324         return true;
325 }
326
327 void
328 CairoEditableText::queue_draw_cell (CairoCell* cell)
329 {
330         Glib::RefPtr<Gdk::Window> win = get_window();
331
332         if (!win) {
333                 return;
334         }
335
336         Gdk::Rectangle r;
337
338         r.set_x (cell->x());
339         r.set_y (cell->y());
340         r.set_width (cell->width());
341         r.set_height (cell->height());
342
343         Gdk::Region rg (r);
344         win->invalidate_region (rg, true);
345 }
346
347 CairoCell*
348 CairoEditableText::find_cell (uint32_t x, uint32_t y)
349 {
350         for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
351                 if ((*i)->covers (x, y)) {
352                         return (*i);
353                 }
354         }
355
356         return 0;
357 }
358
359 bool
360 CairoEditableText::on_button_press_event (GdkEventButton* ev)
361 {
362         CairoCell* cell = find_cell (ev->x, ev->y);
363         return button_press (ev, cell);
364 }
365
366 bool
367 CairoEditableText::on_button_release_event (GdkEventButton* ev)
368 {
369         CairoCell* cell = find_cell (ev->x, ev->y);
370         return button_release (ev, cell);
371 }
372
373 void
374 CairoEditableText::start_editing (CairoCell* cell)
375 {
376         stop_editing ();
377
378         if (cell) {
379                 editing_cell = cell;
380                 queue_draw_cell (cell);
381                 grab_focus ();
382         }
383 }
384
385 void
386 CairoEditableText::stop_editing ()
387 {
388         if (editing_cell) {
389                 queue_draw_cell (editing_cell);
390                 editing_cell = 0;
391         }
392 }
393
394 void
395 CairoEditableText::set_cell_sizes ()
396 {
397         Glib::RefPtr<Gdk::Window> win = get_window();
398
399         if (!win) {
400                 return;
401         }
402         
403         Cairo::RefPtr<Cairo::Context> context = win->create_cairo_context();
404         
405         if (!context) {
406                 return;
407         }
408
409         for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
410                 (*i)->set_size (context);
411         }
412 }
413
414 void
415 CairoEditableText::on_size_request (GtkRequisition* req)
416 {
417         set_cell_sizes ();
418
419         max_cell_width = 0;
420         max_cell_height = 0;
421         
422         for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
423                 max_cell_width += (*i)->width();
424                 max_cell_height = std::max ((double) (*i)->height(), max_cell_height);
425         }
426
427         req->width = max_cell_width;
428         req->height = max_cell_height;
429 }
430
431 void
432 CairoEditableText::on_size_allocate (Gtk::Allocation& alloc)
433 {
434         Misc::on_size_allocate (alloc);
435
436         /* position each cell so that its centered in the allocated space
437          */
438
439         double x = (alloc.get_width() - max_cell_width)/2.0;
440         double y = (alloc.get_height() - max_cell_height)/2.0;
441
442         CellMap::iterator i = cells.begin();
443
444         while (i != cells.end()) {
445                 CairoCell* cell = (*i);
446
447                 cell->set_position (x, y);
448                 x += cell->width ();
449
450                 if (++i != cells.end()) {
451                         /* only add cell padding intra-cellularly */
452                         x += cell->xpad();
453                 } else {
454                         break;
455                 }
456         }
457 }
458
459 void
460 CairoEditableText::set_font (Pango::FontDescription& fd)
461 {
462         boost::shared_ptr<CairoFontDescription> cd (new CairoFontDescription (fd));
463         set_font (cd);
464 }
465
466 void
467 CairoEditableText::set_font (boost::shared_ptr<CairoFontDescription> fd)
468 {
469         for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
470                 CairoTextCell* tc = dynamic_cast<CairoTextCell*>(*i);
471                 if (tc && (!tc->font() || tc->font() == _font)) {
472                         tc->set_font (fd);
473                 }
474         }
475
476         _font = fd;
477
478         queue_resize ();
479         queue_draw ();
480 }
481