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