X-Git-Url: https://main.carlh.net/gitweb/?a=blobdiff_plain;f=libs%2Fgtkmm2ext%2Fcairocell.cc;h=f20f537e1b972f2ebafadde4b55258fed6d14ef8;hb=cfe42bc4ea9a5a6234f43c173e14fdd89af39589;hp=6f9a53e314968ae21a854c2c841df1b3c1421147;hpb=811acc5e6ed88a8268791f7a0520d36b350f6414;p=ardour.git diff --git a/libs/gtkmm2ext/cairocell.cc b/libs/gtkmm2ext/cairocell.cc index 6f9a53e314..f20f537e1b 100644 --- a/libs/gtkmm2ext/cairocell.cc +++ b/libs/gtkmm2ext/cairocell.cc @@ -26,13 +26,67 @@ using std::string; using std::map; +using std::max; using std::cerr; using std::endl; using namespace Gtkmm2ext; -CairoCell::CairoCell () - : _visible (true) - , _xpad (5) +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: + _slant = Cairo::FONT_SLANT_NORMAL; + break; + case Pango::STYLE_OBLIQUE: + _slant = Cairo::FONT_SLANT_OBLIQUE; + break; + case Pango::STYLE_ITALIC: + _slant = Cairo::FONT_SLANT_ITALIC; + break; + } + + switch (fd.get_weight()) { + case Pango::WEIGHT_ULTRALIGHT: + _weight = Cairo::FONT_WEIGHT_NORMAL; + break; + + case Pango::WEIGHT_LIGHT: + _weight = Cairo::FONT_WEIGHT_NORMAL; + break; + + case Pango::WEIGHT_NORMAL: + _weight = Cairo::FONT_WEIGHT_NORMAL; + break; + + case Pango::WEIGHT_SEMIBOLD: + _weight = Cairo::FONT_WEIGHT_BOLD; + break; + + case Pango::WEIGHT_BOLD: + _weight = Cairo::FONT_WEIGHT_BOLD; + break; + + case Pango::WEIGHT_ULTRABOLD: + _weight = Cairo::FONT_WEIGHT_BOLD; + break; + + case Pango::WEIGHT_HEAVY: + _weight = Cairo::FONT_WEIGHT_BOLD; + break; + + } + + face = fd.get_family(); +} + +CairoCell::CairoCell (int32_t id) + : _id (id) + , _visible (true) + , _xpad (0) { bbox.x = 0; bbox.y = 0; @@ -40,33 +94,19 @@ CairoCell::CairoCell () bbox.height = 0; } -void -CairoColonCell::render (Cairo::RefPtr& 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 (); -} - -void -CairoColonCell::set_size (Glib::RefPtr& 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; -} - -CairoTextCell::CairoTextCell (double wc) - : _width_chars (wc) +CairoTextCell::CairoTextCell (int32_t id, double wc, boost::shared_ptr font) + : CairoCell (id) + , _width_chars (wc) + , _font (font) + , y_offset (0) + , x_offset (0) { } void CairoTextCell::set_text (const std::string& txt) { - layout->set_text (txt); + _text = txt; } void @@ -76,114 +116,163 @@ CairoTextCell::render (Cairo::RefPtr& context) return; } - context->move_to (bbox.x, bbox.y); - pango_cairo_update_layout (context->cobj(), layout->gobj()); - pango_cairo_show_layout (context->cobj(), layout->gobj()); + context->save (); + + context->rectangle (bbox.x, bbox.y, bbox.width, bbox.height); + context->clip (); + + _font->apply (context); + context->move_to (bbox.x, bbox.y + bbox.height + y_offset); + context->show_text (_text); + + context->restore (); } void -CairoTextCell::set_size (Glib::RefPtr& context, const Pango::FontDescription& font) +CairoTextCell::set_size (Cairo::RefPtr& context) { - if (!layout) { - layout = Pango::Layout::create (context); + const uint32_t lim = (uint32_t) ceil (_width_chars); + char buf[lim+1]; + 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; + } + + context->get_text_extents (buf, ext); + + max_width = max (ext.width + ext.x_bearing, max_width); + max_height = max (ext.height, max_height); + bsum += ext.x_bearing; } - layout->set_font_description (font); + /* add the average x-bearing for all digits as right hand side padding */ - Pango::FontMetrics metrics = context->get_metrics (font); + bbox.width = max_width + (bsum/10.0); - bbox.width = (_width_chars * metrics.get_approximate_digit_width ()) / PANGO_SCALE; - bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE; + /* some fonts and some digits get their extents computed "too small", so fudge this + by adding 2 + */ + bbox.height = max_height; } -CairoCell* -CairoEditableText::get_cell (uint32_t id) +CairoCharCell::CairoCharCell (int32_t id, char c) + : CairoTextCell (id, 1) { - CellMap::iterator i = cells.find (id); - if (i == cells.end()) { - return 0; + _text = c; +} + +void +CairoCharCell::set_size (Cairo::RefPtr& 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; + } + + { + 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; } - return i->second; } -CairoEditableText::CairoEditableText () - : editing_id (0) - , width (0) +CairoEditableText::CairoEditableText (boost::shared_ptr font) + : editing_cell (0) + , _draw_bg (true) + , max_cell_width (0) , max_cell_height (0) - , height (0) - , corner_radius (18) - , xpad (10) - , ypad (5) + , _corner_radius (9) + , _xpad (0) + , _ypad (0) { + set_font (font); + 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); set_can_default (true); - set_receives_default (true); } CairoEditableText::~CairoEditableText () { - for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) { - delete i->second; - } + /* we don't own cells */ } bool CairoEditableText::on_scroll_event (GdkEventScroll* ev) { - uint32_t id; - CairoCell* cell = find_cell (ev->x, ev->y, id); + CairoCell* cell = find_cell (ev->x, ev->y); if (cell) { - return scroll (ev, id); + return scroll (ev, cell); } return false; } bool -CairoEditableText::on_focus_in_event (GdkEventFocus* ev) +CairoEditableText::on_focus_in_event (GdkEventFocus*) { return false; } bool -CairoEditableText::on_focus_out_event (GdkEventFocus* ev) +CairoEditableText::on_focus_out_event (GdkEventFocus*) { - if (editing_id) { - CairoCell* cell = get_cell (editing_id); - queue_draw_cell (cell); - editing_id = 0; + if (editing_cell) { + queue_draw_cell (editing_cell); + editing_cell = 0; } return false; } void -CairoEditableText::add_cell (uint32_t id, CairoCell* cell) +CairoEditableText::add_cell (CairoCell* cell) { - if (id > 0) { - Glib::RefPtr context = get_pango_context (); - cell->set_size (context, font); + cells.push_back (cell); + + CairoTextCell* tc = dynamic_cast(cell); - cells[id] = cell; /* we own it */ + if (tc) { + tc->set_font (_font); } + + queue_resize (); } void -CairoEditableText::set_text (uint32_t id, const string& text) +CairoEditableText::clear_cells () { - CellMap::iterator i = cells.find (id); - - if (i == cells.end()) { - return; - } - - CairoTextCell* textcell = dynamic_cast (i->second); + cells.clear (); + queue_resize (); +} - if (textcell) { - set_text (textcell, text); +void +CairoEditableText::set_width_chars (CairoTextCell* cell, uint32_t wc) +{ + if (cell) { + cell->set_width_chars (wc); + queue_resize (); } } @@ -197,7 +286,14 @@ CairoEditableText::set_text (CairoTextCell* cell, const string& text) bool CairoEditableText::on_expose_event (GdkEventExpose* ev) { - Cairo::RefPtr context = get_window()->create_cairo_context(); + Glib::RefPtr win = get_window (); + + if (!win) { + std::cerr << "CET: no window to draw on\n"; + return false; + } + + Cairo::RefPtr context = win->create_cairo_context(); if (cells.empty()) { return true; @@ -206,20 +302,29 @@ CairoEditableText::on_expose_event (GdkEventExpose* ev) context->rectangle (ev->area.x, ev->area.y, ev->area.width, ev->area.height); context->clip (); - context->set_source_rgba (bg_r, bg_g, bg_b, bg_a); - rounded_rectangle (context, 0, 0, width, height, corner_radius); - context->fill (); - + Gtk::Allocation alloc = get_allocation (); + double width = alloc.get_width(); + double height = alloc.get_height (); + + 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 (); + } + for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) { - uint32_t id = i->first; - CairoCell* cell = i->second; + CairoCell* cell = (*i); /* is cell inside the expose area? */ - + if (cell->intersects (ev->area)) { - if (id == editing_id) { + if (cell == editing_cell) { context->set_source_rgba (edit_r, edit_b, edit_g, edit_a); } else { context->set_source_rgba (r, g, b, a); @@ -253,12 +358,11 @@ CairoEditableText::queue_draw_cell (CairoCell* cell) } CairoCell* -CairoEditableText::find_cell (uint32_t x, uint32_t y, uint32_t& id) +CairoEditableText::find_cell (uint32_t x, uint32_t y) { for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) { - if (i->second->covers (x, y)) { - id = i->first; - return i->second; + if ((*i)->covers (x, y)) { + return (*i); } } @@ -268,38 +372,24 @@ CairoEditableText::find_cell (uint32_t x, uint32_t y, uint32_t& id) bool CairoEditableText::on_button_press_event (GdkEventButton* ev) { - uint32_t id; - CairoCell* cell = find_cell (ev->x, ev->y, id); - - if (!cell) { - return false; - } - - return button_press (ev, id); + CairoCell* cell = find_cell (ev->x, ev->y); + return button_press (ev, cell); } bool CairoEditableText::on_button_release_event (GdkEventButton* ev) { - uint32_t id; - CairoCell* cell = find_cell (ev->x, ev->y, id); - - if (!cell) { - return false; - } - - return button_release (ev, id); + CairoCell* cell = find_cell (ev->x, ev->y); + return button_release (ev, cell); } void -CairoEditableText::start_editing (uint32_t id) +CairoEditableText::start_editing (CairoCell* cell) { - CairoCell* cell = get_cell (id); - stop_editing (); if (cell) { - editing_id = id; + editing_cell = cell; queue_draw_cell (cell); grab_focus (); } @@ -308,39 +398,47 @@ CairoEditableText::start_editing (uint32_t id) void CairoEditableText::stop_editing () { - if (editing_id) { - CairoCell* cell; - if ((cell = get_cell (editing_id))) { - queue_draw_cell (cell); - } - editing_id = 0; + if (editing_cell) { + queue_draw_cell (editing_cell); + editing_cell = 0; } } void -CairoEditableText::on_size_request (GtkRequisition* req) +CairoEditableText::set_cell_sizes () { - double x = 0; - - max_cell_height = 0; + Glib::RefPtr win = get_window(); - x = xpad; + if (!win) { + return; + } + + Cairo::RefPtr context = win->create_cairo_context(); + + if (!context) { + return; + } for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) { - CairoCell* cell = i->second; + (*i)->set_size (context); + } +} - if (cell->visible()) { - cell->set_position (x, ypad); - } +void +CairoEditableText::on_size_request (GtkRequisition* req) +{ + set_cell_sizes (); - x += cell->width() + cell->xpad(); - max_cell_height = std::max ((double) cell->height(), max_cell_height); + max_cell_width = 0; + max_cell_height = 0; + + 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); } - x += xpad; - - req->width = x; - req->height = max_cell_height + (ypad * 2); + req->width = max_cell_width; + req->height = max_cell_height; } void @@ -348,25 +446,49 @@ CairoEditableText::on_size_allocate (Gtk::Allocation& alloc) { Misc::on_size_allocate (alloc); - width = alloc.get_width(); - height = alloc.get_height(); + /* position each cell so that its centered in the allocated space + */ + + double x = (alloc.get_width() - max_cell_width)/2.0; + double y = (alloc.get_height() - max_cell_height)/2.0; + + CellMap::iterator i = cells.begin(); + + while (i != cells.end()) { + CairoCell* cell = (*i); + + cell->set_position (x, y); + x += cell->width (); + + if (++i != cells.end()) { + /* only add cell padding intra-cellularly */ + x += cell->xpad(); + } else { + break; + } + } } void -CairoEditableText::set_font (const std::string& str) +CairoEditableText::set_font (Pango::FontDescription& fd) { - set_font (Pango::FontDescription (str)); + boost::shared_ptr cd (new CairoFontDescription (fd)); + set_font (cd); } void -CairoEditableText::set_font (const Pango::FontDescription& fd) +CairoEditableText::set_font (boost::shared_ptr fd) { - Glib::RefPtr context = get_pango_context (); - for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) { - i->second->set_size (context, fd); + CairoTextCell* tc = dynamic_cast(*i); + if (tc && (!tc->font() || tc->font() == _font)) { + tc->set_font (fd); + } } + _font = fd; + queue_resize (); queue_draw (); } +