~ubuntu-branches/ubuntu/utopic/ardour3/utopic

« back to all changes in this revision

Viewing changes to libs/gtkmm2ext/cairocell.cc

  • Committer: Package Import Robot
  • Author(s): Felipe Sateler
  • Date: 2013-09-21 19:05:02 UTC
  • Revision ID: package-import@ubuntu.com-20130921190502-8gsftrku6jnzhd7v
Tags: upstream-3.4~dfsg
ImportĀ upstreamĀ versionĀ 3.4~dfsg

Show diffs side-by-side

added added

removed removed

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