Allow Lua bindings up to 10 args
[ardour.git] / libs / gtkmm2ext / cairocell.cc
index e2a59832451214d4c226405205801a2c2775a21a..7ce77355b6e883f665e91d7fbe7563bb87004e4e 100644 (file)
 
 #include <algorithm>
 #include <cmath>
+#include <iostream>
 
 #include "gtkmm2ext/cairocell.h"
 #include "gtkmm2ext/utils.h"
 
 using std::string;
+using std::vector;
 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;
+
+       /* to silence warnings when compiling with newer pango versions. */
+       default:
+               _weight = Cairo::FONT_WEIGHT_NORMAL;
+               break;
+       }
+
+       face = fd.get_family();
+}
+
+CairoCell::CairoCell (int32_t id)
+       : _id (id)
+       , _visible (true)
+       , _xpad (0)
 {
        bbox.x = 0;
        bbox.y = 0;
@@ -37,11 +99,21 @@ CairoCell::CairoCell ()
        bbox.height = 0;
 }
 
-CairoTextCell::CairoTextCell (uint32_t wc)
-       : _width_chars (wc)
+CairoTextCell::CairoTextCell (int32_t id, double wc, boost::shared_ptr<CairoFontDescription> font)
+       : CairoCell (id)
+       , _width_chars (wc)
+       , _font (font)
+       , y_offset (0)
+       , x_offset (0)
 {
 }
 
+void
+CairoTextCell::set_text (const std::string& txt)
+{
+       _text = txt;
+}
+
 void
 CairoTextCell::render (Cairo::RefPtr<Cairo::Context>& context)
 {
@@ -49,93 +121,163 @@ CairoTextCell::render (Cairo::RefPtr<Cairo::Context>& 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<Pango::Context>& context, const Pango::FontDescription& font)
+CairoTextCell::set_size (Cairo::RefPtr<Cairo::Context>& context)
 {
-       layout = Pango::Layout::create (context);
-       layout->set_font_description (font);
+       const uint32_t lim = (uint32_t) ceil (_width_chars);
+       vector<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[0], ext);
+
+               max_width = max (ext.width + ext.x_bearing, max_width);
+               max_height = max (ext.height, max_height);
+               bsum += ext.x_bearing;
+       }
 
-       Pango::FontMetrics metrics = context->get_metrics (font);
+       /* add the average x-bearing for all digits as right hand side padding */
 
-       bbox.width = (_width_chars * metrics.get_approximate_digit_width ()) / PANGO_SCALE;
-       bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE;
+       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;
 }
 
-CairoCell*
-CairoEditableText::get_cell (uint32_t id)
+CairoCharCell::CairoCharCell (int32_t id, char c)
+       : CairoTextCell (id, 1)
+{
+       _text = c;
+}
+
+void
+CairoCharCell::set_size (Cairo::RefPtr<Cairo::Context>& context)
 {
-       CellMap::iterator i = cells.find (id);
-       if (i == cells.end()) {
-               return 0;
+       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)
-       , editing_pos (0)
-       , width (0)
+CairoEditableText::CairoEditableText (boost::shared_ptr<CairoFontDescription> font)
+       : editing_cell (0)
+       , _draw_bg (true)
+       , max_cell_width (0)
        , max_cell_height (0)
-       , height (0)
-       , corner_radius (24)
-       , 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::BUTTON_PRESS_MASK | Gdk::BUTTON_RELEASE_MASK | Gdk::SCROLL_MASK);
        set_flags (Gtk::CAN_FOCUS);
+
        set_can_default (true);
-       set_receives_default (true);
 }
 
-bool
-CairoEditableText::on_focus_in_event (GdkEventFocus* ev)
+CairoEditableText::~CairoEditableText ()
 {
-       return false;
+       /* we don't own cells */
 }
 
 bool
-CairoEditableText::on_focus_out_event (GdkEventFocus* ev)
+CairoEditableText::on_scroll_event (GdkEventScroll* ev)
 {
-       if (editing_id) {
-               CairoCell* cell = get_cell (editing_id);
-               queue_draw_cell (cell);
-               editing_id = 0;
-               editing_pos = 0;
+       CairoCell* cell = find_cell (ev->x, ev->y);
+
+       if (cell) {
+               return scroll (ev, cell);
        }
 
        return false;
 }
 
-void
-CairoEditableText::add_cell (uint32_t id, CairoCell* cell)
+bool
+CairoEditableText::on_focus_in_event (GdkEventFocus*)
 {
-       if (id > 0) {
-               Glib::RefPtr<Pango::Context> context = get_pango_context ();
-               cell->set_size (context, font);
+       return false;
+}
 
-               cells[id] = cell; /* we own it */
+bool
+CairoEditableText::on_focus_out_event (GdkEventFocus*)
+{
+       if (editing_cell) {
+               queue_draw_cell (editing_cell);
+               editing_cell = 0;
        }
+       return false;
 }
 
 void
-CairoEditableText::set_text (uint32_t id, const string& text)
+CairoEditableText::add_cell (CairoCell* cell)
 {
-       CellMap::iterator i = cells.find (id);
+       cells.push_back (cell);
 
-       if (i == cells.end()) {
-               return;
+       CairoTextCell* tc = dynamic_cast<CairoTextCell*>(cell);
+
+       if (tc) {
+               tc->set_font (_font);
        }
 
-       CairoTextCell* textcell = dynamic_cast<CairoTextCell*> (i->second);
+       queue_resize ();
+}
 
-       if (textcell) {
-               set_text (textcell, text);
+void
+CairoEditableText::clear_cells ()
+{
+       cells.clear ();
+       queue_resize ();
+}
+
+void
+CairoEditableText::set_width_chars (CairoTextCell* cell, uint32_t wc)
+{
+       if (cell) {
+               cell->set_width_chars (wc);
+               queue_resize ();
        }
 }
 
@@ -149,7 +291,14 @@ CairoEditableText::set_text (CairoTextCell* cell, const string& text)
 bool
 CairoEditableText::on_expose_event (GdkEventExpose* ev)
 {
-       Cairo::RefPtr<Cairo::Context> context = get_window()->create_cairo_context();
+       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();
 
        if (cells.empty()) {
                return true;
@@ -158,21 +307,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);
@@ -181,6 +338,7 @@ CairoEditableText::on_expose_event (GdkEventExpose* ev)
                        cell->render (context);
                }
        }
+
        return true;
 }
 
@@ -205,12 +363,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);
                }
        }
 
@@ -220,210 +377,123 @@ 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;
-
-       if (editing_id) {
-               cell = get_cell (editing_id);
-               /* redraw the old cell */
-               queue_draw_cell (cell);
-       }
-
-       cell = find_cell (ev->x, ev->y, id);
-
-       if (!cell) {
-               editing_id = 0;
-               return false;
-       }
-
-       grab_focus ();
-       editing_id = id;
-       editing_pos = 0;
-
-       /* redraw the new cell (maybe the same as the old - no real cost) */
-       queue_draw_cell (cell);
-
-       return true;
+       CairoCell* cell = find_cell (ev->x, ev->y);
+       return button_press (ev, cell);
 }
 
 bool
 CairoEditableText::on_button_release_event (GdkEventButton* ev)
 {
-       return true;
+       CairoCell* cell = find_cell (ev->x, ev->y);
+       return button_release (ev, cell);
 }
 
-bool
-CairoEditableText::on_key_press_event (GdkEventKey* ev)
+void
+CairoEditableText::start_editing (CairoCell* cell)
 {
-       if (!editing_id) {
-               return true;
-       }
-
-       bool commit_change = false;
-
-       CairoCell* cell = get_cell (editing_id);
+       stop_editing ();
 
-       if (!cell) {
-               return true;
+       if (cell) {
+               editing_cell = cell;
+               queue_draw_cell (cell);
+               grab_focus ();
        }
+}
 
-       CairoTextCell* text_cell = dynamic_cast<CairoTextCell*> (cell);
-
-       if (!text_cell) {
-               return true;
+void
+CairoEditableText::stop_editing ()
+{
+       if (editing_cell) {
+               queue_draw_cell (editing_cell);
+               editing_cell = 0;
        }
+}
 
-       string txt = text_cell->get_text ();
-
-       switch (ev->keyval) {
-       case GDK_Tab:
-               queue_draw_cell (cell);
-               edit_next_cell ();
-               break;
+void
+CairoEditableText::set_cell_sizes ()
+{
+       Glib::RefPtr<Gdk::Window> win = get_window();
 
-       case GDK_0:
-       case GDK_KP_0:
-               txt[editing_pos] = '0';
-               commit_change = true;
-               break;
-       case GDK_1:
-       case GDK_KP_1:
-               txt[editing_pos] = '1';
-               commit_change = true;
-               break;
-       case GDK_2:
-       case GDK_KP_2:
-               txt[editing_pos] = '2';
-               commit_change = true;
-               break;
-       case GDK_3:
-       case GDK_KP_3:
-               txt[editing_pos] = '3';
-               commit_change = true;
-               break;
-       case GDK_4:
-       case GDK_KP_4:
-               txt[editing_pos] = '4';
-               commit_change = true;
-               break;
-       case GDK_5:
-       case GDK_KP_5:
-               txt[editing_pos] = '5';
-               commit_change = true;
-               break;
-       case GDK_6:
-       case GDK_KP_6:
-               txt[editing_pos] = '6';
-               commit_change = true;
-               break;
-       case GDK_7:
-       case GDK_KP_7:
-               txt[editing_pos] = '7';
-               commit_change = true;
-               break;
-       case GDK_8:
-       case GDK_KP_8:
-               txt[editing_pos] = '8';
-               commit_change = true;
-               break;
-       case GDK_9:
-       case GDK_KP_9:
-               txt[editing_pos] = '9';
-               commit_change = true;
-               break;
+       if (!win) {
+               return;
+       }
 
-       case GDK_Right:
-               if (editing_pos < text_cell->width_chars() - 1) {
-                       editing_pos++;
-               }
-               break;
+       Cairo::RefPtr<Cairo::Context> context = win->create_cairo_context();
 
-       case GDK_Left:
-               if (editing_pos > 0) {
-                       editing_pos--;
-               }
-               break;
+       if (!context) {
+               return;
+       }
 
-       default:
-               break;
+       for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
+               (*i)->set_size (context);
        }
+}
+
+void
+CairoEditableText::on_size_request (GtkRequisition* req)
+{
+       set_cell_sizes ();
 
-       if (commit_change) {
-               set_text (text_cell, txt);
+       max_cell_width = 0;
+       max_cell_height = 0;
 
-               if (++editing_pos >= text_cell->width_chars()) {
-                       edit_next_cell ();
-               }
+       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);
        }
 
-
-       return true;
+       req->width = max_cell_width;
+       req->height = max_cell_height;
 }
 
 void
-CairoEditableText::edit_next_cell ()
+CairoEditableText::on_size_allocate (Gtk::Allocation& alloc)
 {
-       CairoCell* next;
-       CairoTextCell* next_text;
-       uint32_t next_id = editing_id + 1;
+       Misc::on_size_allocate (alloc);
 
-       while (true) {
-               next = get_cell (next_id);
+       /* position each cell so that its centered in the allocated space
+        */
 
-               if (!next || !next->visible() || (next_text = dynamic_cast<CairoTextCell*> (next)) != 0) {
-                       break;
-               }
+       double x = (alloc.get_width() - max_cell_width)/2.0;
+       double y = (alloc.get_height() - max_cell_height)/2.0;
 
-               next_id += 1;
-       }
+       CellMap::iterator i = cells.begin();
+
+       while (i != cells.end()) {
+               CairoCell* cell = (*i);
 
-       if (next) {
-               editing_id = next_id;
-               editing_pos = 0;
-               queue_draw_cell (next_text);
-       } else {
-               editing_id = 0;
-               editing_pos = 0;
+               cell->set_position (x, y);
+               x += cell->width ();
+
+               if (++i != cells.end()) {
+                       /* only add cell padding intra-cellularly */
+                       x += cell->xpad();
+               } else {
+                       break;
+               }
        }
 }
 
-bool
-CairoEditableText::on_key_release_event (GdkEventKey* ev)
+void
+CairoEditableText::set_font (Pango::FontDescription& fd)
 {
-       return true;
+       boost::shared_ptr<CairoFontDescription> cd (new CairoFontDescription (fd));
+       set_font (cd);
 }
 
 void
-CairoEditableText::on_size_request (GtkRequisition* req)
+CairoEditableText::set_font (boost::shared_ptr<CairoFontDescription> fd)
 {
-       double x = 0;
-
-       max_cell_height = 0;
-
-       x = xpad;
-
        for (CellMap::iterator i = cells.begin(); i != cells.end(); ++i) {
-               CairoCell* cell = i->second;
-
-               if (cell->visible()) {
-                       cell->set_position (x, ypad);
+               CairoTextCell* tc = dynamic_cast<CairoTextCell*>(*i);
+               if (tc && (!tc->font() || tc->font() == _font)) {
+                       tc->set_font (fd);
                }
-
-               x += cell->width() + cell->xpad();
-               max_cell_height = std::max ((double) cell->height(), max_cell_height);
        }
 
-       x += xpad;
+       _font = fd;
 
-       req->width = x;
-       req->height = max_cell_height + (ypad * 2);
+       queue_resize ();
+       queue_draw ();
 }
 
-void
-CairoEditableText::on_size_allocate (Gtk::Allocation& alloc)
-{
-       Misc::on_size_allocate (alloc);
-
-       width = alloc.get_width();
-       height = alloc.get_height();
-}