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"
31 using namespace Gtkmm2ext;
33 CairoCell::CairoCell (int32_t id)
45 CairoColonCell::render (Cairo::RefPtr<Cairo::Context>& context)
47 /* two very small circles */
48 context->arc (bbox.x, bbox.y + (bbox.height/3.0), bbox.width/2.0, 0.0, M_PI*2.0);
50 context->arc (bbox.x, bbox.y + (2.0 * bbox.height/3.0), bbox.width/2.0, 0.0, M_PI*2.0);
55 CairoColonCell::set_size (Glib::RefPtr<Pango::Context>& context, const Pango::FontDescription& font)
57 Pango::FontMetrics metrics = context->get_metrics (font);
58 bbox.width = std::max (3.0, (0.25 * metrics.get_approximate_char_width() / PANGO_SCALE));
59 bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE;
62 CairoTextCell::CairoTextCell (int32_t id, double wc)
69 CairoTextCell::set_text (const std::string& txt)
71 layout->set_text (txt);
75 CairoTextCell::render (Cairo::RefPtr<Cairo::Context>& context)
77 if (!_visible || _width_chars == 0) {
81 context->move_to (bbox.x, bbox.y);
82 pango_cairo_update_layout (context->cobj(), layout->gobj());
83 pango_cairo_show_layout (context->cobj(), layout->gobj());
87 CairoTextCell::set_size (Glib::RefPtr<Pango::Context>& context, const Pango::FontDescription& font)
90 layout = Pango::Layout::create (context);
93 layout->set_font_description (font);
95 Pango::FontMetrics metrics = context->get_metrics (font);
97 bbox.width = (_width_chars * metrics.get_approximate_digit_width ()) / PANGO_SCALE;
98 bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE;
101 CairoEditableText::CairoEditableText ()
105 , max_cell_height (0)
111 add_events (Gdk::POINTER_MOTION_HINT_MASK | Gdk::SCROLL_MASK | Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK |
112 Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::SCROLL_MASK);
113 set_flags (Gtk::CAN_FOCUS);
115 set_can_default (true);
116 set_receives_default (true);
119 CairoEditableText::~CairoEditableText ()
121 /* we don't own cells */
125 CairoEditableText::on_scroll_event (GdkEventScroll* ev)
127 CairoCell* cell = find_cell (ev->x, ev->y);
130 return scroll (ev, cell);
137 CairoEditableText::on_focus_in_event (GdkEventFocus* ev)
143 CairoEditableText::on_focus_out_event (GdkEventFocus* ev)
146 queue_draw_cell (editing_cell);
153 CairoEditableText::add_cell (CairoCell* cell)
155 Glib::RefPtr<Pango::Context> context = get_pango_context ();
156 cell->set_size (context, _font);
157 cells.push_back (cell);
163 CairoEditableText::clear_cells ()
171 CairoEditableText::set_width_chars (CairoTextCell* cell, uint32_t wc)
174 cell->set_width_chars (wc);
180 CairoEditableText::set_text (CairoTextCell* cell, const string& text)
182 cell->set_text (text);
183 queue_draw_cell (cell);
187 CairoEditableText::on_expose_event (GdkEventExpose* ev)
189 Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
195 context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
199 context->set_source_rgba (bg_r, bg_g, bg_b, bg_a);
200 if (_corner_radius) {
201 rounded_rectangle (context, 0, 0, width, height, _corner_radius);
203 context->rectangle (0, 0, width, height);
208 for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
210 CairoCell* cell = (*i);
212 /* is cell inside the expose area?
215 if (cell->intersects (ev->area)) {
216 if (cell == editing_cell) {
217 context->set_source_rgba (edit_r, edit_b, edit_g, edit_a);
219 context->set_source_rgba (r, g, b, a);
222 cell->render (context);
230 CairoEditableText::queue_draw_cell (CairoCell* cell)
232 Glib::RefPtr<Gdk::Window> win = get_window();
242 r.set_width (cell->width());
243 r.set_height (cell->height());
246 win->invalidate_region (rg, true);
250 CairoEditableText::find_cell (uint32_t x, uint32_t y)
252 for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
253 if ((*i)->covers (x, y)) {
262 CairoEditableText::on_button_press_event (GdkEventButton* ev)
264 CairoCell* cell = find_cell (ev->x, ev->y);
265 return button_press (ev, cell);
269 CairoEditableText::on_button_release_event (GdkEventButton* ev)
271 CairoCell* cell = find_cell (ev->x, ev->y);
272 return button_release (ev, cell);
276 CairoEditableText::start_editing (CairoCell* cell)
282 queue_draw_cell (cell);
288 CairoEditableText::stop_editing ()
291 queue_draw_cell (editing_cell);
297 CairoEditableText::on_size_request (GtkRequisition* req)
305 CellMap::iterator i = cells.begin();
307 while (i != cells.end()) {
308 CairoCell* cell = (*i);
310 if (cell->visible()) {
311 cell->set_position (x, _ypad);
315 max_cell_height = std::max ((double) cell->height(), max_cell_height);
319 if (i != cells.end()) {
320 /* only add cell padding intra-cellularly */
330 req->height = max_cell_height + (_ypad * 2);
334 CairoEditableText::on_size_allocate (Gtk::Allocation& alloc)
336 Misc::on_size_allocate (alloc);
338 width = alloc.get_width();
339 height = alloc.get_height();
343 CairoEditableText::set_font (const std::string& str)
345 set_font (Pango::FontDescription (str));
349 CairoEditableText::set_font (const Pango::FontDescription& fd)
351 Glib::RefPtr<Pango::Context> context = get_pango_context ();
355 for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
356 (*i)->set_size (context, _font);