2011-06-01 07:51:32 -04:00
|
|
|
/*
|
2011-06-01 12:50:12 -04:00
|
|
|
Copyright (C) 2011 Paul Davis
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-01 12:50:12 -04:00
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-01 12:50:12 -04:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-01 12:50:12 -04:00
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
2011-06-01 07:51:32 -04:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cmath>
|
2011-06-02 13:47:11 -04:00
|
|
|
#include <iostream>
|
2011-06-01 07:51:32 -04:00
|
|
|
|
|
|
|
#include "gtkmm2ext/cairocell.h"
|
|
|
|
#include "gtkmm2ext/utils.h"
|
|
|
|
|
|
|
|
using std::string;
|
2013-08-26 12:28:03 -04:00
|
|
|
using std::vector;
|
2011-06-01 07:51:32 -04:00
|
|
|
using std::map;
|
2011-06-07 11:23:06 -04:00
|
|
|
using std::max;
|
2011-06-02 13:47:11 -04:00
|
|
|
using std::cerr;
|
|
|
|
using std::endl;
|
2011-06-01 07:51:32 -04:00
|
|
|
using namespace Gtkmm2ext;
|
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
static const double cairo_font_fudge = 1.5;
|
|
|
|
|
|
|
|
CairoFontDescription::CairoFontDescription (Pango::FontDescription& fd)
|
|
|
|
{
|
|
|
|
_size = cairo_font_fudge * (fd.get_size() / PANGO_SCALE);
|
|
|
|
|
|
|
|
switch (fd.get_style()) {
|
|
|
|
case Pango::STYLE_NORMAL:
|
2011-06-08 13:27:26 -04:00
|
|
|
_slant = Cairo::FONT_SLANT_NORMAL;
|
2011-06-07 11:23:06 -04:00
|
|
|
break;
|
|
|
|
case Pango::STYLE_OBLIQUE:
|
2011-06-08 13:27:26 -04:00
|
|
|
_slant = Cairo::FONT_SLANT_OBLIQUE;
|
2011-06-07 11:23:06 -04:00
|
|
|
break;
|
|
|
|
case Pango::STYLE_ITALIC:
|
2011-06-08 13:27:26 -04:00
|
|
|
_slant = Cairo::FONT_SLANT_ITALIC;
|
2011-06-07 11:23:06 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (fd.get_weight()) {
|
|
|
|
case Pango::WEIGHT_ULTRALIGHT:
|
2011-06-08 13:27:26 -04:00
|
|
|
_weight = Cairo::FONT_WEIGHT_NORMAL;
|
2011-06-07 11:23:06 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Pango::WEIGHT_LIGHT:
|
2011-06-08 13:27:26 -04:00
|
|
|
_weight = Cairo::FONT_WEIGHT_NORMAL;
|
2011-06-07 11:23:06 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Pango::WEIGHT_NORMAL:
|
2011-06-08 13:27:26 -04:00
|
|
|
_weight = Cairo::FONT_WEIGHT_NORMAL;
|
2011-06-07 11:23:06 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Pango::WEIGHT_SEMIBOLD:
|
2011-06-08 13:27:26 -04:00
|
|
|
_weight = Cairo::FONT_WEIGHT_BOLD;
|
2011-06-07 11:23:06 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Pango::WEIGHT_BOLD:
|
2011-06-08 13:27:26 -04:00
|
|
|
_weight = Cairo::FONT_WEIGHT_BOLD;
|
2011-06-07 11:23:06 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Pango::WEIGHT_ULTRABOLD:
|
2011-06-08 13:27:26 -04:00
|
|
|
_weight = Cairo::FONT_WEIGHT_BOLD;
|
2011-06-07 11:23:06 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Pango::WEIGHT_HEAVY:
|
2011-06-08 13:27:26 -04:00
|
|
|
_weight = Cairo::FONT_WEIGHT_BOLD;
|
2011-06-07 11:23:06 -04:00
|
|
|
break;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
face = fd.get_family();
|
|
|
|
}
|
|
|
|
|
2011-06-03 18:18:47 -04:00
|
|
|
CairoCell::CairoCell (int32_t id)
|
|
|
|
: _id (id)
|
|
|
|
, _visible (true)
|
2011-06-07 11:23:06 -04:00
|
|
|
, _xpad (0)
|
2011-06-01 07:51:32 -04:00
|
|
|
{
|
2011-06-01 12:50:12 -04:00
|
|
|
bbox.x = 0;
|
|
|
|
bbox.y = 0;
|
|
|
|
bbox.width = 0;
|
|
|
|
bbox.height = 0;
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
CairoTextCell::CairoTextCell (int32_t id, double wc, boost::shared_ptr<CairoFontDescription> font)
|
2011-06-03 18:18:47 -04:00
|
|
|
: CairoCell (id)
|
|
|
|
, _width_chars (wc)
|
2011-06-07 11:23:06 -04:00
|
|
|
, _font (font)
|
|
|
|
, y_offset (0)
|
|
|
|
, x_offset (0)
|
2011-06-01 07:51:32 -04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-06-02 13:47:11 -04:00
|
|
|
void
|
|
|
|
CairoTextCell::set_text (const std::string& txt)
|
|
|
|
{
|
2011-06-07 11:23:06 -04:00
|
|
|
_text = txt;
|
2011-06-02 13:47:11 -04:00
|
|
|
}
|
|
|
|
|
2011-06-01 07:51:32 -04:00
|
|
|
void
|
|
|
|
CairoTextCell::render (Cairo::RefPtr<Cairo::Context>& context)
|
|
|
|
{
|
2011-06-01 12:50:12 -04:00
|
|
|
if (!_visible || _width_chars == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-07 12:42:48 -04:00
|
|
|
context->save ();
|
|
|
|
|
|
|
|
context->rectangle (bbox.x, bbox.y, bbox.width, bbox.height);
|
|
|
|
context->clip ();
|
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
_font->apply (context);
|
|
|
|
context->move_to (bbox.x, bbox.y + bbox.height + y_offset);
|
|
|
|
context->show_text (_text);
|
2011-06-07 12:42:48 -04:00
|
|
|
|
|
|
|
context->restore ();
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-07 11:23:06 -04:00
|
|
|
CairoTextCell::set_size (Cairo::RefPtr<Cairo::Context>& context)
|
2011-06-01 07:51:32 -04:00
|
|
|
{
|
2011-06-07 11:23:06 -04:00
|
|
|
const uint32_t lim = (uint32_t) ceil (_width_chars);
|
2013-08-26 12:28:03 -04:00
|
|
|
vector<char> buf(lim+1);
|
2011-06-07 11:23:06 -04:00
|
|
|
uint32_t n;
|
|
|
|
double max_width = 0.0;
|
|
|
|
double max_height = 0.0;
|
|
|
|
Cairo::TextExtents ext;
|
|
|
|
double bsum = 0;
|
|
|
|
|
|
|
|
buf[lim] = '\0';
|
|
|
|
|
|
|
|
_font->apply (context);
|
|
|
|
|
|
|
|
for (int digit = 0; digit < 10; digit++) {
|
|
|
|
|
|
|
|
for (n = 0; n < lim; ++n) {
|
|
|
|
buf[n] = '0' + digit;
|
|
|
|
}
|
|
|
|
|
2013-08-26 12:28:03 -04:00
|
|
|
context->get_text_extents (&buf[0], ext);
|
2011-06-07 11:23:06 -04:00
|
|
|
|
|
|
|
max_width = max (ext.width + ext.x_bearing, max_width);
|
|
|
|
max_height = max (ext.height, max_height);
|
|
|
|
bsum += ext.x_bearing;
|
2011-06-02 13:47:11 -04:00
|
|
|
}
|
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
/* add the average x-bearing for all digits as right hand side padding */
|
|
|
|
|
|
|
|
bbox.width = max_width + (bsum/10.0);
|
|
|
|
|
|
|
|
/* some fonts and some digits get their extents computed "too small", so fudge this
|
|
|
|
by adding 2
|
|
|
|
*/
|
|
|
|
bbox.height = max_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
CairoCharCell::CairoCharCell (int32_t id, char c)
|
|
|
|
: CairoTextCell (id, 1)
|
|
|
|
{
|
|
|
|
_text = c;
|
|
|
|
}
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
void
|
|
|
|
CairoCharCell::set_size (Cairo::RefPtr<Cairo::Context>& context)
|
|
|
|
{
|
|
|
|
Cairo::TextExtents ext;
|
|
|
|
|
|
|
|
_font->apply (context);
|
|
|
|
|
|
|
|
{
|
|
|
|
const char* buf = "8";
|
|
|
|
context->get_text_extents (buf, ext);
|
|
|
|
/* same height as an "8" */
|
|
|
|
bbox.height = ext.height;
|
|
|
|
}
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
{
|
|
|
|
const char* buf = ":";
|
|
|
|
context->get_text_extents (buf, ext);
|
|
|
|
bbox.width = ext.width + (2.0 * ext.x_bearing);
|
|
|
|
/* center vertically */
|
|
|
|
y_offset = (ext.height - bbox.height) / 2.0;
|
|
|
|
}
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
CairoEditableText::CairoEditableText (boost::shared_ptr<CairoFontDescription> font)
|
2011-06-03 18:18:47 -04:00
|
|
|
: editing_cell (0)
|
|
|
|
, _draw_bg (true)
|
2011-06-07 11:23:06 -04:00
|
|
|
, max_cell_width (0)
|
2011-06-02 13:58:02 -04:00
|
|
|
, max_cell_height (0)
|
2011-06-02 17:43:10 -04:00
|
|
|
, _corner_radius (9)
|
2011-06-07 11:23:06 -04:00
|
|
|
, _xpad (0)
|
|
|
|
, _ypad (0)
|
2011-06-01 07:51:32 -04:00
|
|
|
{
|
2011-06-07 11:23:06 -04:00
|
|
|
set_font (font);
|
|
|
|
|
2011-06-02 13:58:02 -04:00
|
|
|
add_events (Gdk::POINTER_MOTION_HINT_MASK | Gdk::SCROLL_MASK | Gdk::KEY_PRESS_MASK | Gdk::KEY_RELEASE_MASK |
|
|
|
|
Gdk::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::SCROLL_MASK);
|
|
|
|
set_flags (Gtk::CAN_FOCUS);
|
2011-06-02 13:47:11 -04:00
|
|
|
|
2011-06-02 13:58:02 -04:00
|
|
|
set_can_default (true);
|
2011-06-02 13:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
CairoEditableText::~CairoEditableText ()
|
|
|
|
{
|
2011-06-03 18:18:47 -04:00
|
|
|
/* we don't own cells */
|
2011-06-02 13:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CairoEditableText::on_scroll_event (GdkEventScroll* ev)
|
|
|
|
{
|
2011-06-03 18:18:47 -04:00
|
|
|
CairoCell* cell = find_cell (ev->x, ev->y);
|
2011-06-02 13:47:11 -04:00
|
|
|
|
|
|
|
if (cell) {
|
2011-06-03 18:18:47 -04:00
|
|
|
return scroll (ev, cell);
|
2011-06-02 13:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-09-30 13:55:14 -04:00
|
|
|
CairoEditableText::on_focus_in_event (GdkEventFocus*)
|
2011-06-01 07:51:32 -04:00
|
|
|
{
|
2011-06-01 12:50:12 -04:00
|
|
|
return false;
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2011-09-30 13:55:14 -04:00
|
|
|
CairoEditableText::on_focus_out_event (GdkEventFocus*)
|
2011-06-01 07:51:32 -04:00
|
|
|
{
|
2011-06-03 18:18:47 -04:00
|
|
|
if (editing_cell) {
|
|
|
|
queue_draw_cell (editing_cell);
|
|
|
|
editing_cell = 0;
|
2011-06-02 13:58:02 -04:00
|
|
|
}
|
2011-06-01 12:50:12 -04:00
|
|
|
return false;
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-03 18:18:47 -04:00
|
|
|
CairoEditableText::add_cell (CairoCell* cell)
|
2011-06-01 07:51:32 -04:00
|
|
|
{
|
2011-06-03 18:18:47 -04:00
|
|
|
cells.push_back (cell);
|
2011-06-07 11:23:06 -04:00
|
|
|
|
|
|
|
CairoTextCell* tc = dynamic_cast<CairoTextCell*>(cell);
|
|
|
|
|
|
|
|
if (tc) {
|
|
|
|
tc->set_font (_font);
|
|
|
|
}
|
|
|
|
|
2011-06-03 18:18:47 -04:00
|
|
|
queue_resize ();
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-03 18:18:47 -04:00
|
|
|
CairoEditableText::clear_cells ()
|
2011-06-01 07:51:32 -04:00
|
|
|
{
|
2011-06-03 18:18:47 -04:00
|
|
|
cells.clear ();
|
|
|
|
queue_resize ();
|
|
|
|
}
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-03 18:18:47 -04:00
|
|
|
void
|
|
|
|
CairoEditableText::set_width_chars (CairoTextCell* cell, uint32_t wc)
|
|
|
|
{
|
|
|
|
if (cell) {
|
|
|
|
cell->set_width_chars (wc);
|
|
|
|
queue_resize ();
|
2011-06-02 13:58:02 -04:00
|
|
|
}
|
|
|
|
}
|
2011-06-01 07:51:32 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
CairoEditableText::set_text (CairoTextCell* cell, const string& text)
|
|
|
|
{
|
2011-06-01 12:50:12 -04:00
|
|
|
cell->set_text (text);
|
|
|
|
queue_draw_cell (cell);
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CairoEditableText::on_expose_event (GdkEventExpose* ev)
|
|
|
|
{
|
2011-06-11 11:35:34 -04:00
|
|
|
Glib::RefPtr<Gdk::Window> win = get_window ();
|
|
|
|
|
|
|
|
if (!win) {
|
|
|
|
std::cerr << "CET: no window to draw on\n";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cairo::RefPtr<Cairo::Context> context = win->create_cairo_context();
|
2011-06-02 13:58:02 -04:00
|
|
|
|
2011-06-01 12:50:12 -04:00
|
|
|
if (cells.empty()) {
|
|
|
|
return true;
|
|
|
|
}
|
2011-06-01 07:51:32 -04:00
|
|
|
|
|
|
|
context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
|
|
|
|
context->clip ();
|
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
Gtk::Allocation alloc = get_allocation ();
|
|
|
|
double width = alloc.get_width();
|
|
|
|
double height = alloc.get_height ();
|
|
|
|
|
2011-06-03 18:18:47 -04:00
|
|
|
if (_draw_bg) {
|
|
|
|
context->set_source_rgba (bg_r, bg_g, bg_b, bg_a);
|
|
|
|
if (_corner_radius) {
|
|
|
|
rounded_rectangle (context, 0, 0, width, height, _corner_radius);
|
|
|
|
} else {
|
|
|
|
context->rectangle (0, 0, width, height);
|
|
|
|
}
|
|
|
|
context->fill ();
|
2011-06-02 17:43:10 -04:00
|
|
|
}
|
2011-06-07 11:23:06 -04:00
|
|
|
|
2011-06-01 12:50:12 -04:00
|
|
|
for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
|
2011-06-02 13:58:02 -04:00
|
|
|
|
2011-06-03 18:18:47 -04:00
|
|
|
CairoCell* cell = (*i);
|
2011-06-02 13:58:02 -04:00
|
|
|
|
2011-06-01 12:50:12 -04:00
|
|
|
/* is cell inside the expose area?
|
|
|
|
*/
|
2011-06-07 11:23:06 -04:00
|
|
|
|
2011-06-02 13:58:02 -04:00
|
|
|
if (cell->intersects (ev->area)) {
|
2011-06-03 18:18:47 -04:00
|
|
|
if (cell == editing_cell) {
|
2011-06-01 12:50:12 -04:00
|
|
|
context->set_source_rgba (edit_r, edit_b, edit_g, edit_a);
|
|
|
|
} else {
|
|
|
|
context->set_source_rgba (r, g, b, a);
|
|
|
|
}
|
2011-06-02 13:58:02 -04:00
|
|
|
|
2011-06-01 12:50:12 -04:00
|
|
|
cell->render (context);
|
|
|
|
}
|
|
|
|
}
|
2011-06-02 13:47:11 -04:00
|
|
|
|
2011-06-02 13:58:02 -04:00
|
|
|
return true;
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CairoEditableText::queue_draw_cell (CairoCell* cell)
|
|
|
|
{
|
2011-06-01 12:50:12 -04:00
|
|
|
Glib::RefPtr<Gdk::Window> win = get_window();
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-01 12:50:12 -04:00
|
|
|
if (!win) {
|
|
|
|
return;
|
|
|
|
}
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-01 12:50:12 -04:00
|
|
|
Gdk::Rectangle r;
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-01 12:50:12 -04:00
|
|
|
r.set_x (cell->x());
|
|
|
|
r.set_y (cell->y());
|
|
|
|
r.set_width (cell->width());
|
|
|
|
r.set_height (cell->height());
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-01 12:50:12 -04:00
|
|
|
Gdk::Region rg (r);
|
|
|
|
win->invalidate_region (rg, true);
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
2011-06-01 12:50:12 -04:00
|
|
|
CairoCell*
|
2011-06-03 18:18:47 -04:00
|
|
|
CairoEditableText::find_cell (uint32_t x, uint32_t y)
|
2011-06-01 07:51:32 -04:00
|
|
|
{
|
2011-06-01 12:50:12 -04:00
|
|
|
for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
|
2011-06-03 18:18:47 -04:00
|
|
|
if ((*i)->covers (x, y)) {
|
|
|
|
return (*i);
|
2011-06-01 12:50:12 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CairoEditableText::on_button_press_event (GdkEventButton* ev)
|
|
|
|
{
|
2011-06-03 18:18:47 -04:00
|
|
|
CairoCell* cell = find_cell (ev->x, ev->y);
|
|
|
|
return button_press (ev, cell);
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
CairoEditableText::on_button_release_event (GdkEventButton* ev)
|
|
|
|
{
|
2011-06-03 18:18:47 -04:00
|
|
|
CairoCell* cell = find_cell (ev->x, ev->y);
|
|
|
|
return button_release (ev, cell);
|
2011-06-02 13:47:11 -04:00
|
|
|
}
|
2011-06-01 12:50:12 -04:00
|
|
|
|
2011-06-02 13:47:11 -04:00
|
|
|
void
|
2011-06-03 18:18:47 -04:00
|
|
|
CairoEditableText::start_editing (CairoCell* cell)
|
2011-06-02 13:47:11 -04:00
|
|
|
{
|
|
|
|
stop_editing ();
|
2011-06-01 12:50:12 -04:00
|
|
|
|
2011-06-02 13:47:11 -04:00
|
|
|
if (cell) {
|
2011-06-03 18:18:47 -04:00
|
|
|
editing_cell = cell;
|
2011-06-01 12:50:12 -04:00
|
|
|
queue_draw_cell (cell);
|
2011-06-02 13:47:11 -04:00
|
|
|
grab_focus ();
|
2011-06-01 12:50:12 -04:00
|
|
|
}
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-02 13:47:11 -04:00
|
|
|
CairoEditableText::stop_editing ()
|
2011-06-01 07:51:32 -04:00
|
|
|
{
|
2011-06-03 18:18:47 -04:00
|
|
|
if (editing_cell) {
|
|
|
|
queue_draw_cell (editing_cell);
|
|
|
|
editing_cell = 0;
|
2011-06-01 12:50:12 -04:00
|
|
|
}
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
void
|
|
|
|
CairoEditableText::set_cell_sizes ()
|
|
|
|
{
|
|
|
|
Glib::RefPtr<Gdk::Window> win = get_window();
|
|
|
|
|
|
|
|
if (!win) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Cairo::RefPtr<Cairo::Context> context = win->create_cairo_context();
|
|
|
|
|
|
|
|
if (!context) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
|
|
|
|
(*i)->set_size (context);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-01 07:51:32 -04:00
|
|
|
void
|
|
|
|
CairoEditableText::on_size_request (GtkRequisition* req)
|
|
|
|
{
|
2011-06-07 11:23:06 -04:00
|
|
|
set_cell_sizes ();
|
2011-06-01 12:50:12 -04:00
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
max_cell_width = 0;
|
2011-06-01 12:50:12 -04:00
|
|
|
max_cell_height = 0;
|
2011-06-07 11:23:06 -04:00
|
|
|
|
|
|
|
for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
|
|
|
|
max_cell_width += (*i)->width();
|
|
|
|
max_cell_height = std::max ((double) (*i)->height(), max_cell_height);
|
|
|
|
}
|
|
|
|
|
|
|
|
req->width = max_cell_width;
|
|
|
|
req->height = max_cell_height;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
CairoEditableText::on_size_allocate (Gtk::Allocation& alloc)
|
|
|
|
{
|
|
|
|
Misc::on_size_allocate (alloc);
|
|
|
|
|
|
|
|
/* position each cell so that its centered in the allocated space
|
|
|
|
*/
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
double x = (alloc.get_width() - max_cell_width)/2.0;
|
|
|
|
double y = (alloc.get_height() - max_cell_height)/2.0;
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
CellMap::iterator i = cells.begin();
|
2011-06-02 17:43:10 -04:00
|
|
|
|
|
|
|
while (i != cells.end()) {
|
2011-06-03 18:18:47 -04:00
|
|
|
CairoCell* cell = (*i);
|
2011-06-01 07:51:32 -04:00
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
cell->set_position (x, y);
|
|
|
|
x += cell->width ();
|
2011-06-02 17:43:10 -04:00
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
if (++i != cells.end()) {
|
2011-06-02 17:43:10 -04:00
|
|
|
/* only add cell padding intra-cellularly */
|
|
|
|
x += cell->xpad();
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
2011-06-01 12:50:12 -04:00
|
|
|
}
|
2011-06-01 07:51:32 -04:00
|
|
|
}
|
2011-06-02 13:47:11 -04:00
|
|
|
|
|
|
|
void
|
2011-06-07 11:23:06 -04:00
|
|
|
CairoEditableText::set_font (Pango::FontDescription& fd)
|
2011-06-02 13:47:11 -04:00
|
|
|
{
|
2011-06-07 11:23:06 -04:00
|
|
|
boost::shared_ptr<CairoFontDescription> cd (new CairoFontDescription (fd));
|
|
|
|
set_font (cd);
|
2011-06-02 13:47:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-06-07 11:23:06 -04:00
|
|
|
CairoEditableText::set_font (boost::shared_ptr<CairoFontDescription> fd)
|
2011-06-02 13:47:11 -04:00
|
|
|
{
|
2011-06-02 13:58:02 -04:00
|
|
|
for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
|
2011-06-07 11:23:06 -04:00
|
|
|
CairoTextCell* tc = dynamic_cast<CairoTextCell*>(*i);
|
|
|
|
if (tc && (!tc->font() || tc->font() == _font)) {
|
|
|
|
tc->set_font (fd);
|
|
|
|
}
|
2011-06-02 13:47:11 -04:00
|
|
|
}
|
|
|
|
|
2011-06-07 11:23:06 -04:00
|
|
|
_font = fd;
|
|
|
|
|
2011-06-02 13:47:11 -04:00
|
|
|
queue_resize ();
|
|
|
|
queue_draw ();
|
|
|
|
}
|
2011-06-07 11:23:06 -04:00
|
|
|
|