2011-06-01 11:51:32 +00:00
|
|
|
/*
|
2011-06-01 16:50:12 +00:00
|
|
|
Copyright (C) 2011 Paul Davis
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-01 16:50:12 +00: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 11:51:32 +00:00
|
|
|
|
2011-06-01 16:50:12 +00: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 11:51:32 +00:00
|
|
|
|
2011-06-01 16:50:12 +00: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 11:51:32 +00:00
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cmath>
|
2011-06-02 17:47:11 +00:00
|
|
|
#include <iostream>
|
2011-06-01 11:51:32 +00:00
|
|
|
|
|
|
|
|
#include "gtkmm2ext/cairocell.h"
|
|
|
|
|
#include "gtkmm2ext/utils.h"
|
|
|
|
|
|
|
|
|
|
using std::string;
|
|
|
|
|
using std::map;
|
2011-06-02 17:47:11 +00:00
|
|
|
using std::cerr;
|
|
|
|
|
using std::endl;
|
2011-06-01 11:51:32 +00:00
|
|
|
using namespace Gtkmm2ext;
|
|
|
|
|
|
2011-06-03 22:18:47 +00:00
|
|
|
CairoCell::CairoCell (int32_t id)
|
|
|
|
|
: _id (id)
|
|
|
|
|
, _visible (true)
|
|
|
|
|
, _xpad (2)
|
2011-06-01 11:51:32 +00:00
|
|
|
{
|
2011-06-01 16:50:12 +00:00
|
|
|
bbox.x = 0;
|
|
|
|
|
bbox.y = 0;
|
|
|
|
|
bbox.width = 0;
|
|
|
|
|
bbox.height = 0;
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-02 17:47:11 +00:00
|
|
|
void
|
|
|
|
|
CairoColonCell::render (Cairo::RefPtr<Cairo::Context>& context)
|
|
|
|
|
{
|
|
|
|
|
/* two very small circles */
|
|
|
|
|
context->arc (bbox.x, bbox.y + (bbox.height/3.0), bbox.width/2.0, 0.0, M_PI*2.0);
|
|
|
|
|
context->fill ();
|
|
|
|
|
context->arc (bbox.x, bbox.y + (2.0 * bbox.height/3.0), bbox.width/2.0, 0.0, M_PI*2.0);
|
|
|
|
|
context->fill ();
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-02 17:58:02 +00:00
|
|
|
void
|
2011-06-02 17:47:11 +00:00
|
|
|
CairoColonCell::set_size (Glib::RefPtr<Pango::Context>& context, const Pango::FontDescription& font)
|
|
|
|
|
{
|
|
|
|
|
Pango::FontMetrics metrics = context->get_metrics (font);
|
|
|
|
|
bbox.width = std::max (3.0, (0.25 * metrics.get_approximate_char_width() / PANGO_SCALE));
|
|
|
|
|
bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE;
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-03 22:18:47 +00:00
|
|
|
CairoTextCell::CairoTextCell (int32_t id, double wc)
|
|
|
|
|
: CairoCell (id)
|
|
|
|
|
, _width_chars (wc)
|
2011-06-01 11:51:32 +00:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-02 17:47:11 +00:00
|
|
|
void
|
|
|
|
|
CairoTextCell::set_text (const std::string& txt)
|
|
|
|
|
{
|
2011-06-02 17:58:02 +00:00
|
|
|
layout->set_text (txt);
|
2011-06-02 17:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-01 11:51:32 +00:00
|
|
|
void
|
|
|
|
|
CairoTextCell::render (Cairo::RefPtr<Cairo::Context>& context)
|
|
|
|
|
{
|
2011-06-01 16:50:12 +00:00
|
|
|
if (!_visible || _width_chars == 0) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
context->move_to (bbox.x, bbox.y);
|
|
|
|
|
pango_cairo_update_layout (context->cobj(), layout->gobj());
|
|
|
|
|
pango_cairo_show_layout (context->cobj(), layout->gobj());
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2011-06-01 16:50:12 +00:00
|
|
|
CairoTextCell::set_size (Glib::RefPtr<Pango::Context>& context, const Pango::FontDescription& font)
|
2011-06-01 11:51:32 +00:00
|
|
|
{
|
2011-06-02 17:47:11 +00:00
|
|
|
if (!layout) {
|
|
|
|
|
layout = Pango::Layout::create (context);
|
|
|
|
|
}
|
|
|
|
|
|
2011-06-02 17:58:02 +00:00
|
|
|
layout->set_font_description (font);
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
Pango::FontMetrics metrics = context->get_metrics (font);
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
bbox.width = (_width_chars * metrics.get_approximate_digit_width ()) / PANGO_SCALE;
|
|
|
|
|
bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE;
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CairoEditableText::CairoEditableText ()
|
2011-06-03 22:18:47 +00:00
|
|
|
: editing_cell (0)
|
|
|
|
|
, _draw_bg (true)
|
2011-06-02 17:58:02 +00:00
|
|
|
, width (0)
|
|
|
|
|
, max_cell_height (0)
|
|
|
|
|
, height (0)
|
2011-06-02 21:43:10 +00:00
|
|
|
, _corner_radius (9)
|
|
|
|
|
, _xpad (5)
|
|
|
|
|
, _ypad (5)
|
2011-06-01 11:51:32 +00:00
|
|
|
{
|
2011-06-02 17:58:02 +00: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 17:47:11 +00:00
|
|
|
|
2011-06-02 17:58:02 +00:00
|
|
|
set_can_default (true);
|
|
|
|
|
set_receives_default (true);
|
2011-06-02 17:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
CairoEditableText::~CairoEditableText ()
|
|
|
|
|
{
|
2011-06-03 22:18:47 +00:00
|
|
|
/* we don't own cells */
|
2011-06-02 17:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
CairoEditableText::on_scroll_event (GdkEventScroll* ev)
|
|
|
|
|
{
|
2011-06-03 22:18:47 +00:00
|
|
|
CairoCell* cell = find_cell (ev->x, ev->y);
|
2011-06-02 17:47:11 +00:00
|
|
|
|
|
|
|
|
if (cell) {
|
2011-06-03 22:18:47 +00:00
|
|
|
return scroll (ev, cell);
|
2011-06-02 17:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
CairoEditableText::on_focus_in_event (GdkEventFocus* ev)
|
|
|
|
|
{
|
2011-06-01 16:50:12 +00:00
|
|
|
return false;
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
CairoEditableText::on_focus_out_event (GdkEventFocus* ev)
|
|
|
|
|
{
|
2011-06-03 22:18:47 +00:00
|
|
|
if (editing_cell) {
|
|
|
|
|
queue_draw_cell (editing_cell);
|
|
|
|
|
editing_cell = 0;
|
2011-06-02 17:58:02 +00:00
|
|
|
}
|
2011-06-01 16:50:12 +00:00
|
|
|
return false;
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2011-06-03 22:18:47 +00:00
|
|
|
CairoEditableText::add_cell (CairoCell* cell)
|
2011-06-01 11:51:32 +00:00
|
|
|
{
|
2011-06-03 22:18:47 +00:00
|
|
|
Glib::RefPtr<Pango::Context> context = get_pango_context ();
|
|
|
|
|
cell->set_size (context, _font);
|
|
|
|
|
cells.push_back (cell);
|
|
|
|
|
queue_resize ();
|
|
|
|
|
queue_draw ();
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2011-06-03 22:18:47 +00:00
|
|
|
CairoEditableText::clear_cells ()
|
2011-06-01 11:51:32 +00:00
|
|
|
{
|
2011-06-03 22:18:47 +00:00
|
|
|
cells.clear ();
|
|
|
|
|
queue_resize ();
|
|
|
|
|
queue_draw ();
|
|
|
|
|
}
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-03 22:18:47 +00:00
|
|
|
void
|
|
|
|
|
CairoEditableText::set_width_chars (CairoTextCell* cell, uint32_t wc)
|
|
|
|
|
{
|
|
|
|
|
if (cell) {
|
|
|
|
|
cell->set_width_chars (wc);
|
|
|
|
|
queue_resize ();
|
2011-06-02 17:58:02 +00:00
|
|
|
}
|
|
|
|
|
}
|
2011-06-01 11:51:32 +00:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CairoEditableText::set_text (CairoTextCell* cell, const string& text)
|
|
|
|
|
{
|
2011-06-01 16:50:12 +00:00
|
|
|
cell->set_text (text);
|
|
|
|
|
queue_draw_cell (cell);
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
CairoEditableText::on_expose_event (GdkEventExpose* ev)
|
|
|
|
|
{
|
2011-06-01 16:50:12 +00:00
|
|
|
Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
|
2011-06-02 17:58:02 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
if (cells.empty()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2011-06-01 11:51:32 +00:00
|
|
|
|
|
|
|
|
context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height);
|
|
|
|
|
context->clip ();
|
|
|
|
|
|
2011-06-03 22:18:47 +00: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 21:43:10 +00:00
|
|
|
}
|
2011-06-02 17:58:02 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
|
2011-06-02 17:58:02 +00:00
|
|
|
|
2011-06-03 22:18:47 +00:00
|
|
|
CairoCell* cell = (*i);
|
2011-06-02 17:58:02 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
/* is cell inside the expose area?
|
|
|
|
|
*/
|
|
|
|
|
|
2011-06-02 17:58:02 +00:00
|
|
|
if (cell->intersects (ev->area)) {
|
2011-06-03 22:18:47 +00:00
|
|
|
if (cell == editing_cell) {
|
2011-06-01 16:50:12 +00: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 17:58:02 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
cell->render (context);
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-02 17:47:11 +00:00
|
|
|
|
2011-06-02 17:58:02 +00:00
|
|
|
return true;
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CairoEditableText::queue_draw_cell (CairoCell* cell)
|
|
|
|
|
{
|
2011-06-01 16:50:12 +00:00
|
|
|
Glib::RefPtr<Gdk::Window> win = get_window();
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
if (!win) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
Gdk::Rectangle r;
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
r.set_x (cell->x());
|
|
|
|
|
r.set_y (cell->y());
|
|
|
|
|
r.set_width (cell->width());
|
|
|
|
|
r.set_height (cell->height());
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
Gdk::Region rg (r);
|
|
|
|
|
win->invalidate_region (rg, true);
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
CairoCell*
|
2011-06-03 22:18:47 +00:00
|
|
|
CairoEditableText::find_cell (uint32_t x, uint32_t y)
|
2011-06-01 11:51:32 +00:00
|
|
|
{
|
2011-06-01 16:50:12 +00:00
|
|
|
for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
|
2011-06-03 22:18:47 +00:00
|
|
|
if ((*i)->covers (x, y)) {
|
|
|
|
|
return (*i);
|
2011-06-01 16:50:12 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
CairoEditableText::on_button_press_event (GdkEventButton* ev)
|
|
|
|
|
{
|
2011-06-03 22:18:47 +00:00
|
|
|
CairoCell* cell = find_cell (ev->x, ev->y);
|
|
|
|
|
return button_press (ev, cell);
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool
|
|
|
|
|
CairoEditableText::on_button_release_event (GdkEventButton* ev)
|
|
|
|
|
{
|
2011-06-03 22:18:47 +00:00
|
|
|
CairoCell* cell = find_cell (ev->x, ev->y);
|
|
|
|
|
return button_release (ev, cell);
|
2011-06-02 17:47:11 +00:00
|
|
|
}
|
2011-06-01 16:50:12 +00:00
|
|
|
|
2011-06-02 17:47:11 +00:00
|
|
|
void
|
2011-06-03 22:18:47 +00:00
|
|
|
CairoEditableText::start_editing (CairoCell* cell)
|
2011-06-02 17:47:11 +00:00
|
|
|
{
|
|
|
|
|
stop_editing ();
|
2011-06-01 16:50:12 +00:00
|
|
|
|
2011-06-02 17:47:11 +00:00
|
|
|
if (cell) {
|
2011-06-03 22:18:47 +00:00
|
|
|
editing_cell = cell;
|
2011-06-01 16:50:12 +00:00
|
|
|
queue_draw_cell (cell);
|
2011-06-02 17:47:11 +00:00
|
|
|
grab_focus ();
|
2011-06-01 16:50:12 +00:00
|
|
|
}
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2011-06-02 17:47:11 +00:00
|
|
|
CairoEditableText::stop_editing ()
|
2011-06-01 11:51:32 +00:00
|
|
|
{
|
2011-06-03 22:18:47 +00:00
|
|
|
if (editing_cell) {
|
|
|
|
|
queue_draw_cell (editing_cell);
|
|
|
|
|
editing_cell = 0;
|
2011-06-01 16:50:12 +00:00
|
|
|
}
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CairoEditableText::on_size_request (GtkRequisition* req)
|
|
|
|
|
{
|
2011-06-01 16:50:12 +00:00
|
|
|
double x = 0;
|
|
|
|
|
|
|
|
|
|
max_cell_height = 0;
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-02 21:43:10 +00:00
|
|
|
x = _xpad;
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-02 21:43:10 +00:00
|
|
|
CellMap::iterator i = cells.begin();
|
|
|
|
|
|
|
|
|
|
while (i != cells.end()) {
|
2011-06-03 22:18:47 +00:00
|
|
|
CairoCell* cell = (*i);
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
if (cell->visible()) {
|
2011-06-02 21:43:10 +00:00
|
|
|
cell->set_position (x, _ypad);
|
2011-06-01 16:50:12 +00:00
|
|
|
}
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-02 21:43:10 +00:00
|
|
|
x += cell->width();
|
2011-06-01 16:50:12 +00:00
|
|
|
max_cell_height = std::max ((double) cell->height(), max_cell_height);
|
2011-06-02 21:43:10 +00:00
|
|
|
|
|
|
|
|
++i;
|
|
|
|
|
|
|
|
|
|
if (i != cells.end()) {
|
|
|
|
|
/* only add cell padding intra-cellularly */
|
|
|
|
|
x += cell->xpad();
|
|
|
|
|
} else {
|
|
|
|
|
break;
|
|
|
|
|
}
|
2011-06-01 16:50:12 +00:00
|
|
|
}
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-02 21:43:10 +00:00
|
|
|
x += _xpad;
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
req->width = x;
|
2011-06-02 21:43:10 +00:00
|
|
|
req->height = max_cell_height + (_ypad * 2);
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CairoEditableText::on_size_allocate (Gtk::Allocation& alloc)
|
|
|
|
|
{
|
2011-06-01 16:50:12 +00:00
|
|
|
Misc::on_size_allocate (alloc);
|
2011-06-01 11:51:32 +00:00
|
|
|
|
2011-06-01 16:50:12 +00:00
|
|
|
width = alloc.get_width();
|
|
|
|
|
height = alloc.get_height();
|
2011-06-01 11:51:32 +00:00
|
|
|
}
|
2011-06-02 17:47:11 +00:00
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CairoEditableText::set_font (const std::string& str)
|
|
|
|
|
{
|
|
|
|
|
set_font (Pango::FontDescription (str));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
CairoEditableText::set_font (const Pango::FontDescription& fd)
|
|
|
|
|
{
|
|
|
|
|
Glib::RefPtr<Pango::Context> context = get_pango_context ();
|
|
|
|
|
|
2011-06-03 22:18:47 +00:00
|
|
|
_font = fd;
|
|
|
|
|
|
2011-06-02 17:58:02 +00:00
|
|
|
for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
|
2011-06-03 22:18:47 +00:00
|
|
|
(*i)->set_size (context, _font);
|
2011-06-02 17:47:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
queue_resize ();
|
|
|
|
|
queue_draw ();
|
|
|
|
|
}
|