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.
23 #include "gtkmm2ext/cairocell.h"
24 #include "gtkmm2ext/utils.h"
28 using namespace Gtkmm2ext;
30 CairoCell::CairoCell ()
40 CairoTextCell::CairoTextCell (uint32_t wc)
46 CairoTextCell::render (Cairo::RefPtr<Cairo::Context>& context)
48 if (!_visible || _width_chars == 0) {
52 context->move_to (bbox.x, bbox.y);
53 pango_cairo_update_layout (context->cobj(), layout->gobj());
54 pango_cairo_show_layout (context->cobj(), layout->gobj());
58 CairoTextCell::set_size (Glib::RefPtr<Pango::Context>& context, const Pango::FontDescription& font)
60 layout = Pango::Layout::create (context);
61 layout->set_font_description (font);
63 Pango::FontMetrics metrics = context->get_metrics (font);
65 bbox.width = (_width_chars * metrics.get_approximate_digit_width ()) / PANGO_SCALE;
66 bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE;
70 CairoEditableText::get_cell (uint32_t id)
72 CellMap::iterator i = cells.find (id);
73 if (i == cells.end()) {
79 CairoEditableText::CairoEditableText ()
89 add_events (Gdk::POINTER_MOTION_HINT_MASK | Gdk::SCROLL_MASK | Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK |
90 Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK);
91 set_flags (Gtk::CAN_FOCUS);
92 set_can_default (true);
93 set_receives_default (true);
97 CairoEditableText::on_focus_in_event (GdkEventFocus* ev)
103 CairoEditableText::on_focus_out_event (GdkEventFocus* ev)
106 CairoCell* cell = get_cell (editing_id);
107 queue_draw_cell (cell);
116 CairoEditableText::add_cell (uint32_t id, CairoCell* cell)
119 Glib::RefPtr<Pango::Context> context = get_pango_context ();
120 cell->set_size (context, font);
122 cells[id] = cell; /* we own it */
127 CairoEditableText::set_text (uint32_t id, const string& text)
129 CellMap::iterator i = cells.find (id);
131 if (i == cells.end()) {
135 CairoTextCell* textcell = dynamic_cast<CairoTextCell*> (i->second);
138 set_text (textcell, text);
143 CairoEditableText::set_text (CairoTextCell* cell, const string& text)
145 cell->set_text (text);
146 queue_draw_cell (cell);
150 CairoEditableText::on_expose_event (GdkEventExpose* ev)
152 Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
158 context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
161 context->set_source_rgba (bg_r, bg_g, bg_b, bg_a);
162 rounded_rectangle (context, 0, 0, width, height, corner_radius);
165 for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
167 uint32_t id = i->first;
168 CairoCell* cell = i->second;
170 /* is cell inside the expose area?
173 if (cell->intersects (ev->area)) {
175 if (id == editing_id) {
176 context->set_source_rgba (edit_r, edit_b, edit_g, edit_a);
178 context->set_source_rgba (r, g, b, a);
181 cell->render (context);
188 CairoEditableText::queue_draw_cell (CairoCell* cell)
190 Glib::RefPtr<Gdk::Window> win = get_window();
200 r.set_width (cell->width());
201 r.set_height (cell->height());
204 win->invalidate_region (rg, true);
208 CairoEditableText::find_cell (uint32_t x, uint32_t y, uint32_t& id)
210 for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
211 if (i->second->covers (x, y)) {
221 CairoEditableText::on_button_press_event (GdkEventButton* ev)
227 cell = get_cell (editing_id);
228 /* redraw the old cell */
229 queue_draw_cell (cell);
232 cell = find_cell (ev->x, ev->y, id);
243 /* redraw the new cell (maybe the same as the old - no real cost) */
244 queue_draw_cell (cell);
250 CairoEditableText::on_button_release_event (GdkEventButton* ev)
256 CairoEditableText::on_key_press_event (GdkEventKey* ev)
262 bool commit_change = false;
264 CairoCell* cell = get_cell (editing_id);
270 CairoTextCell* text_cell = dynamic_cast<CairoTextCell*> (cell);
276 string txt = text_cell->get_text ();
278 switch (ev->keyval) {
280 queue_draw_cell (cell);
286 txt[editing_pos] = '0';
287 commit_change = true;
291 txt[editing_pos] = '1';
292 commit_change = true;
296 txt[editing_pos] = '2';
297 commit_change = true;
301 txt[editing_pos] = '3';
302 commit_change = true;
306 txt[editing_pos] = '4';
307 commit_change = true;
311 txt[editing_pos] = '5';
312 commit_change = true;
316 txt[editing_pos] = '6';
317 commit_change = true;
321 txt[editing_pos] = '7';
322 commit_change = true;
326 txt[editing_pos] = '8';
327 commit_change = true;
331 txt[editing_pos] = '9';
332 commit_change = true;
336 if (editing_pos < text_cell->width_chars() - 1) {
342 if (editing_pos > 0) {
352 set_text (text_cell, txt);
354 if (++editing_pos >= text_cell->width_chars()) {
364 CairoEditableText::edit_next_cell ()
367 CairoTextCell* next_text;
368 uint32_t next_id = editing_id + 1;
371 next = get_cell (next_id);
373 if (!next || !next->visible() || (next_text = dynamic_cast<CairoTextCell*> (next)) != 0) {
381 editing_id = next_id;
383 queue_draw_cell (next_text);
391 CairoEditableText::on_key_release_event (GdkEventKey* ev)
397 CairoEditableText::on_size_request (GtkRequisition* req)
405 for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
406 CairoCell* cell = i->second;
408 if (cell->visible()) {
409 cell->set_position (x, ypad);
412 x += cell->width() + cell->xpad();
413 max_cell_height = std::max ((double) cell->height(), max_cell_height);
419 req->height = max_cell_height + (ypad * 2);
423 CairoEditableText::on_size_allocate (Gtk::Allocation& alloc)
425 Misc::on_size_allocate (alloc);
427 width = alloc.get_width();
428 height = alloc.get_height();