change FastMeter drawing implementation to use Cairo instead of GDK; some subtle...
[ardour.git] / libs / gtkmm2ext / gtkmm2ext / cairocell.h
index 336a440c5b4c2f7dab458cc4b5b1a945f734d392..2839d4266bdf8e327a670d2f60737b86b2919f04 100644 (file)
 #include <map>
 
 #include <stdint.h>
+
+#include <boost/shared_ptr.hpp>
+
 #include <cairomm/cairomm.h>
-#include <gtkmm.h>
+#include <gtkmm/misc.h>
 
 class CairoCell
 {
-public:
-       CairoCell();
+  public:
+       CairoCell(int32_t id);
        virtual ~CairoCell() {}
+       
+       int32_t id() const { return _id; }
 
        virtual void render (Cairo::RefPtr<Cairo::Context>&) = 0;
 
@@ -39,7 +44,7 @@ public:
        double width() const { return bbox.width; }
        double height() const { return bbox.height; }
 
-       void set_position (double x, double y) {
+        void set_position (double x, double y) {
                bbox.x = x;
                bbox.y = y;
        }
@@ -58,81 +63,107 @@ public:
 
        void set_visible (bool yn) { _visible = yn; }
        bool visible() const { return _visible; }
-       virtual void set_size (Glib::RefPtr<Pango::Context>&,
-                              const Pango::FontDescription&) {}
+       virtual void set_size (Cairo::RefPtr<Cairo::Context>&) {}
 
-protected:
+  protected:
+       int32_t _id;
        GdkRectangle bbox;
        bool _visible;
        uint32_t _xpad;
 };
 
-class CairoBarCell : public CairoCell
-{
-public:
-       CairoBarCell() {};
-
-       void render (Cairo::RefPtr<Cairo::Context>& context) {
-               context->move_to (bbox.x, bbox.y);
-               context->set_line_width (bbox.width);
-               context->rel_line_to (0, bbox.height);
-               context->stroke ();
+class CairoFontDescription {
+  public:
+       CairoFontDescription (const std::string& f,
+                             Cairo::FontSlant s,
+                             Cairo::FontWeight w,
+                             double sz)
+               : face (f)
+               , _slant (s)
+               , _weight (w)
+               , _size (sz)
+       {}
+       CairoFontDescription (Pango::FontDescription&);
+
+       void apply (Cairo::RefPtr<Cairo::Context> context) {
+               context->select_font_face (face, _slant, _weight);
+               context->set_font_size (_size);
        }
 
-       void set_size (Glib::RefPtr<Pango::Context>& context,
-                      const Pango::FontDescription& font) {
-               Pango::FontMetrics metrics = context->get_metrics (font);
-               bbox.width = 2;
-               bbox.height = (metrics.get_ascent() + metrics.get_descent()) / PANGO_SCALE;
-       }
+       void set_size (double sz) { _size = sz; }
+       double size() const { return _size; }
 
-private:
-};
+       Cairo::FontSlant slant() const { return _slant; }
+       void set_slant (Cairo::FontSlant sl) { _slant = sl; }
 
-class CairoColonCell : public CairoCell
-{
-public:
-       CairoColonCell() {};
+       Cairo::FontWeight weight() const { return _weight; }
+       void set_weight (Cairo::FontWeight w) { _weight = w; }
 
-       void render (Cairo::RefPtr<Cairo::Context>& context);
-       void set_size (Glib::RefPtr<Pango::Context>& context,
-                      const Pango::FontDescription& font);
+  private:
+       std::string face;
+       Cairo::FontSlant _slant;
+        Cairo::FontWeight _weight;
+       double _size;
 };
 
 class CairoTextCell : public CairoCell
 {
-public:
-       CairoTextCell (double  width_chars);
-       void set_size (Glib::RefPtr<Pango::Context>&, const Pango::FontDescription&);
+  public:
+       CairoTextCell (int32_t id, double width_chars, boost::shared_ptr<CairoFontDescription> font = boost::shared_ptr<CairoFontDescription>());
+       ~CairoTextCell() {}
 
-       void   set_text (const std::string& txt);
+       virtual void set_size (Cairo::RefPtr<Cairo::Context>&);
+
+       boost::shared_ptr<CairoFontDescription> font() const { return _font; }
 
        std::string get_text() const {
-               return layout->get_text ();
+               return _text;
        }
+
        double width_chars() const { return _width_chars; }
        void render (Cairo::RefPtr<Cairo::Context>&);
 
-protected:
+  protected:
+       friend class CairoEditableText;
+       void set_width_chars (double wc) { _width_chars = wc; }
+       void set_text (const std::string& txt);
+       void set_font (boost::shared_ptr<CairoFontDescription> font) {
+               _font = font;
+       }
+
+  protected:
        double _width_chars;
-       Glib::RefPtr<Pango::Layout> layout;
+       std::string _text;
+       boost::shared_ptr<CairoFontDescription> _font;
+        double y_offset;
+        double x_offset;
+};
+
+class CairoCharCell : public CairoTextCell
+{
+  public:
+        CairoCharCell(int32_t id, char c);
+
+        void set_size (Cairo::RefPtr<Cairo::Context>& context);
 };
 
 class CairoEditableText : public Gtk::Misc
 {
 public:
-       CairoEditableText ();
+       CairoEditableText (boost::shared_ptr<CairoFontDescription> font  = boost::shared_ptr<CairoFontDescription>());
        ~CairoEditableText ();
 
-       void add_cell (uint32_t id, CairoCell*);
-       CairoCell* get_cell (uint32_t id);
+       void add_cell (CairoCell*);
+       void clear_cells ();
 
-       void start_editing (uint32_t id);
+       void start_editing (CairoCell*);
        void stop_editing ();
 
-       void set_text (uint32_t id, const std::string& text);
        void set_text (CairoTextCell* cell, const std::string&);
+       void set_width_chars (CairoTextCell* cell, uint32_t);
 
+       void set_draw_background (bool yn) { _draw_bg = yn; }
+       
        void set_colors (double cr, double cg, double cb, double ca) {
                r = cr;
                g = cg;
@@ -154,37 +185,47 @@ public:
                bg_g = g;
                bg_b = b;
                bg_a = a;
+               queue_draw ();
        }
 
-       void set_font (const std::string& str);
-       void set_font (const Pango::FontDescription&);
+       double xpad() const { return _xpad; }
+       void set_xpad (double x) { _xpad = x; queue_resize(); }
+       double ypad() const { return _ypad; }
+       void set_ypad (double y) { _ypad = y; queue_resize(); }
+       
+       double corner_radius() const { return _corner_radius; }
+       void set_corner_radius (double r) { _corner_radius = r; queue_draw (); }
+
+       boost::shared_ptr<CairoFontDescription> font() const { return _font; }
+       void set_font (boost::shared_ptr<CairoFontDescription> font);
+       void set_font (Pango::FontDescription& font);
 
-       sigc::signal<bool,GdkEventScroll*,uint32_t> scroll;
-       sigc::signal<bool,GdkEventButton*,uint32_t> button_press;
-       sigc::signal<bool,GdkEventButton*,uint32_t> button_release;
+       sigc::signal<bool,GdkEventScroll*,CairoCell*> scroll;
+       sigc::signal<bool,GdkEventButton*,CairoCell*> button_press;
+       sigc::signal<bool,GdkEventButton*,CairoCell*> button_release;
 
 protected:
        bool on_expose_event (GdkEventExpose*);
        bool on_button_press_event (GdkEventButton*);
        bool on_button_release_event (GdkEventButton*);
        void on_size_request (GtkRequisition*);
-       void on_size_allocate (Gtk::Allocation&);
        bool on_focus_in_event (GdkEventFocus*);
        bool on_focus_out_event (GdkEventFocus*);
        bool on_scroll_event (GdkEventScroll*);
+        void on_size_allocate (Gtk::Allocation&);
 
 private:
-       typedef std::map<uint32_t,CairoCell*> CellMap;
+       typedef std::vector<CairoCell*> CellMap;
 
        CellMap cells;
-       Pango::FontDescription font;
-       uint32_t editing_id;
-       double width;
+       boost::shared_ptr<CairoFontDescription> _font;
+       CairoCell* editing_cell;
+       bool _draw_bg;
+       double max_cell_width;
        double max_cell_height;
-       double height;
-       double corner_radius;
-       double xpad;
-       double ypad;
+       double _corner_radius;
+       double _xpad;
+       double _ypad;
        double r;
        double g;
        double b;
@@ -198,8 +239,10 @@ private:
        double bg_b;
        double bg_a;
 
-       CairoCell* find_cell (uint32_t x, uint32_t y, uint32_t& cell_id);
+       CairoCell* find_cell (uint32_t x, uint32_t y);
        void queue_draw_cell (CairoCell* target);
+        void position_cells_and_get_bbox (GdkRectangle&);
+        void set_cell_sizes (); 
 };
 
 #endif /* __libgtmm2ext_cairocell_h__ */