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.
24 #include "gtkmm2ext/cairocell.h"
25 #include "gtkmm2ext/utils.h"
33 using namespace Gtkmm2ext;
35 static const double cairo_font_fudge = 1.5;
37 CairoFontDescription::CairoFontDescription (Pango::FontDescription& fd)
39 _size = cairo_font_fudge * (fd.get_size() / PANGO_SCALE);
41 switch (fd.get_style()) {
42 case Pango::STYLE_NORMAL:
43 _slant = Cairo::FONT_SLANT_NORMAL;
45 case Pango::STYLE_OBLIQUE:
46 _slant = Cairo::FONT_SLANT_OBLIQUE;
48 case Pango::STYLE_ITALIC:
49 _slant = Cairo::FONT_SLANT_ITALIC;
53 switch (fd.get_weight()) {
54 case Pango::WEIGHT_ULTRALIGHT:
55 _weight = Cairo::FONT_WEIGHT_NORMAL;
58 case Pango::WEIGHT_LIGHT:
59 _weight = Cairo::FONT_WEIGHT_NORMAL;
62 case Pango::WEIGHT_NORMAL:
63 _weight = Cairo::FONT_WEIGHT_NORMAL;
66 case Pango::WEIGHT_SEMIBOLD:
67 _weight = Cairo::FONT_WEIGHT_BOLD;
70 case Pango::WEIGHT_BOLD:
71 _weight = Cairo::FONT_WEIGHT_BOLD;
74 case Pango::WEIGHT_ULTRABOLD:
75 _weight = Cairo::FONT_WEIGHT_BOLD;
78 case Pango::WEIGHT_HEAVY:
79 _weight = Cairo::FONT_WEIGHT_BOLD;
82 /* to silence warnings when compiling with newer pango versions. */
84 _weight = Cairo::FONT_WEIGHT_NORMAL;
88 face = fd.get_family();
91 CairoCell::CairoCell (int32_t id)
102 CairoTextCell::CairoTextCell (int32_t id, double wc, boost::shared_ptr<CairoFontDescription> font)
112 CairoTextCell::set_text (const std::string& txt)
118 CairoTextCell::render (Cairo::RefPtr<Cairo::Context>& context)
120 if (!_visible || _width_chars == 0) {
126 context->rectangle (bbox.x, bbox.y, bbox.width, bbox.height);
129 _font->apply (context);
130 context->move_to (bbox.x, bbox.y + bbox.height + y_offset);
131 context->show_text (_text);
137 CairoTextCell::set_size (Cairo::RefPtr<Cairo::Context>& context)
139 const uint32_t lim = (uint32_t) ceil (_width_chars);
140 vector<char> buf(lim+1);
142 double max_width = 0.0;
143 double max_height = 0.0;
144 Cairo::TextExtents ext;
149 _font->apply (context);
151 for (int digit = 0; digit < 10; digit++) {
153 for (n = 0; n < lim; ++n) {
154 buf[n] = '0' + digit;
157 context->get_text_extents (&buf[0], ext);
159 max_width = max (ext.width + ext.x_bearing, max_width);
160 max_height = max (ext.height, max_height);
161 bsum += ext.x_bearing;
164 /* add the average x-bearing for all digits as right hand side padding */
166 bbox.width = max_width + (bsum/10.0);
168 /* some fonts and some digits get their extents computed "too small", so fudge this
171 bbox.height = max_height;
174 CairoCharCell::CairoCharCell (int32_t id, char c)
175 : CairoTextCell (id, 1)
181 CairoCharCell::set_size (Cairo::RefPtr<Cairo::Context>& context)
183 Cairo::TextExtents ext;
185 _font->apply (context);
188 const char* buf = "8";
189 context->get_text_extents (buf, ext);
190 /* same height as an "8" */
191 bbox.height = ext.height;
195 const char* buf = ":";
196 context->get_text_extents (buf, ext);
197 bbox.width = ext.width + (2.0 * ext.x_bearing);
198 /* center vertically */
199 y_offset = (ext.height - bbox.height) / 2.0;
203 CairoEditableText::CairoEditableText (boost::shared_ptr<CairoFontDescription> font)
207 , max_cell_height (0)
214 add_events (Gdk::POINTER_MOTION_HINT_MASK | Gdk::SCROLL_MASK | Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK |
215 Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::SCROLL_MASK);
216 set_flags (Gtk::CAN_FOCUS);
218 set_can_default (true);
221 CairoEditableText::~CairoEditableText ()
223 /* we don't own cells */
227 CairoEditableText::on_scroll_event (GdkEventScroll* ev)
229 CairoCell* cell = find_cell (ev->x, ev->y);
232 return scroll (ev, cell);
239 CairoEditableText::on_focus_in_event (GdkEventFocus*)
245 CairoEditableText::on_focus_out_event (GdkEventFocus*)
248 queue_draw_cell (editing_cell);
255 CairoEditableText::add_cell (CairoCell* cell)
257 cells.push_back (cell);
259 CairoTextCell* tc = dynamic_cast<CairoTextCell*>(cell);
262 tc->set_font (_font);
269 CairoEditableText::clear_cells ()
276 CairoEditableText::set_width_chars (CairoTextCell* cell, uint32_t wc)
279 cell->set_width_chars (wc);
285 CairoEditableText::set_text (CairoTextCell* cell, const string& text)
287 cell->set_text (text);
288 queue_draw_cell (cell);
292 CairoEditableText::on_expose_event (GdkEventExpose* ev)
294 Glib::RefPtr<Gdk::Window> win = get_window ();
297 std::cerr << "CET: no window to draw on\n";
301 Cairo::RefPtr<Cairo::Context> context = win->create_cairo_context();
307 context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
310 Gtk::Allocation alloc = get_allocation ();
311 double width = alloc.get_width();
312 double height = alloc.get_height ();
315 context->set_source_rgba (bg_r, bg_g, bg_b, bg_a);
316 if (_corner_radius) {
317 rounded_rectangle (context, 0, 0, width, height, _corner_radius);
319 context->rectangle (0, 0, width, height);
324 for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
326 CairoCell* cell = (*i);
328 /* is cell inside the expose area?
331 if (cell->intersects (ev->area)) {
332 if (cell == editing_cell) {
333 context->set_source_rgba (edit_r, edit_b, edit_g, edit_a);
335 context->set_source_rgba (r, g, b, a);
338 cell->render (context);
346 CairoEditableText::queue_draw_cell (CairoCell* cell)
348 Glib::RefPtr<Gdk::Window> win = get_window();
358 r.set_width (cell->width());
359 r.set_height (cell->height());
362 win->invalidate_region (rg, true);
366 CairoEditableText::find_cell (uint32_t x, uint32_t y)
368 for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
369 if ((*i)->covers (x, y)) {
378 CairoEditableText::on_button_press_event (GdkEventButton* ev)
380 CairoCell* cell = find_cell (ev->x, ev->y);
381 return button_press (ev, cell);
385 CairoEditableText::on_button_release_event (GdkEventButton* ev)
387 CairoCell* cell = find_cell (ev->x, ev->y);
388 return button_release (ev, cell);
392 CairoEditableText::start_editing (CairoCell* cell)
398 queue_draw_cell (cell);
404 CairoEditableText::stop_editing ()
407 queue_draw_cell (editing_cell);
413 CairoEditableText::set_cell_sizes ()
415 Glib::RefPtr<Gdk::Window> win = get_window();
421 Cairo::RefPtr<Cairo::Context> context = win->create_cairo_context();
427 for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
428 (*i)->set_size (context);
433 CairoEditableText::on_size_request (GtkRequisition* req)
440 for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
441 max_cell_width += (*i)->width();
442 max_cell_height = std::max ((double) (*i)->height(), max_cell_height);
445 req->width = max_cell_width;
446 req->height = max_cell_height;
450 CairoEditableText::on_size_allocate (Gtk::Allocation& alloc)
452 Misc::on_size_allocate (alloc);
454 /* position each cell so that its centered in the allocated space
457 double x = (alloc.get_width() - max_cell_width)/2.0;
458 double y = (alloc.get_height() - max_cell_height)/2.0;
460 CellMap::iterator i = cells.begin();
462 while (i != cells.end()) {
463 CairoCell* cell = (*i);
465 cell->set_position (x, y);
468 if (++i != cells.end()) {
469 /* only add cell padding intra-cellularly */
478 CairoEditableText::set_font (Pango::FontDescription& fd)
480 boost::shared_ptr<CairoFontDescription> cd (new CairoFontDescription (fd));
485 CairoEditableText::set_font (boost::shared_ptr<CairoFontDescription> fd)
487 for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
488 CairoTextCell* tc = dynamic_cast<CairoTextCell*>(*i);
489 if (tc && (!tc->font() || tc->font() == _font)) {