Towards packing CairoWidgets on ArdourCanvas.

This commit is contained in:
Robin Gareus 2016-12-19 13:36:42 +01:00
parent 218d376154
commit 004431426d
4 changed files with 111 additions and 3 deletions

View file

@ -44,6 +44,8 @@ public:
private: private:
CairoWidget& _widget; CairoWidget& _widget;
bool event_proxy (GdkEvent*); bool event_proxy (GdkEvent*);
bool queue_draw ();
bool queue_resize ();
}; };
} }

View file

@ -34,6 +34,9 @@ Widget::Widget (Canvas* c, CairoWidget& w)
, _widget (w) , _widget (w)
{ {
Event.connect (sigc::mem_fun (*this, &Widget::event_proxy)); Event.connect (sigc::mem_fun (*this, &Widget::event_proxy));
w.set_canvas_widget ();
w.QueueDraw.connect (sigc::mem_fun(*this, &Widget::queue_draw));
w.QueueResize.connect (sigc::mem_fun(*this, &Widget::queue_resize));
} }
Widget::Widget (Item* parent, CairoWidget& w) Widget::Widget (Item* parent, CairoWidget& w)
@ -41,6 +44,9 @@ Widget::Widget (Item* parent, CairoWidget& w)
, _widget (w) , _widget (w)
{ {
Event.connect (sigc::mem_fun (*this, &Widget::event_proxy)); Event.connect (sigc::mem_fun (*this, &Widget::event_proxy));
w.set_canvas_widget ();
w.QueueDraw.connect (sigc::mem_fun(*this, &Widget::queue_draw));
w.QueueResize.connect (sigc::mem_fun(*this, &Widget::queue_resize));
} }
bool bool
@ -50,6 +56,22 @@ Widget::event_proxy (GdkEvent* ev)
return _widget.event (ev); return _widget.event (ev);
} }
bool
Widget::queue_draw ()
{
begin_visual_change ();
end_visual_change ();
return true;
}
bool
Widget::queue_resize ()
{
begin_change ();
end_change ();
return true;
}
void void
Widget::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const Widget::render (Rect const & area, Cairo::RefPtr<Cairo::Context> context) const
{ {

View file

@ -48,15 +48,57 @@ CairoWidget::CairoWidget ()
, _grabbed (false) , _grabbed (false)
, _name_proxy (this, X_("name")) , _name_proxy (this, X_("name"))
, _current_parent (0) , _current_parent (0)
, _canvas_widget (false)
{ {
_name_proxy.connect (sigc::mem_fun (*this, &CairoWidget::on_name_changed)); _name_proxy.connect (sigc::mem_fun (*this, &CairoWidget::on_name_changed));
} }
CairoWidget::~CairoWidget () CairoWidget::~CairoWidget ()
{ {
if (_canvas_widget) {
gtk_widget_set_realized (GTK_WIDGET(gobj()), false);
}
if (_parent_style_change) _parent_style_change.disconnect(); if (_parent_style_change) _parent_style_change.disconnect();
} }
void
CairoWidget::set_canvas_widget ()
{
assert (!_canvas_widget);
ensure_style ();
gtk_widget_set_realized (GTK_WIDGET(gobj()), true);
_canvas_widget = true;
}
int
CairoWidget::get_width () const
{
if (_canvas_widget) {
return _allocation.get_width ();
}
return Gtk::EventBox::get_width ();
}
int
CairoWidget::get_height () const
{
if (_canvas_widget) {
return _allocation.get_height ();
}
return Gtk::EventBox::get_height ();
}
void
CairoWidget::size_allocate (Gtk::Allocation& alloc)
{
if (_canvas_widget) {
memcpy (&_allocation, &alloc, sizeof(Gtk::Allocation));
return;
}
Gtk::EventBox::size_allocate (alloc);
}
bool bool
CairoWidget::on_button_press_event (GdkEventButton*) CairoWidget::on_button_press_event (GdkEventButton*)
{ {
@ -241,17 +283,43 @@ CairoWidget::set_dirty (cairo_rectangle_t *area)
if (!area) { if (!area) {
queue_draw (); queue_draw ();
} else { } else {
// TODO emit QueueDrawArea -> ArdourCanvas::Widget
if (QueueDraw ()) {
return;
}
queue_draw_area (area->x, area->y, area->width, area->height); queue_draw_area (area->x, area->y, area->width, area->height);
} }
} }
void
CairoWidget::queue_draw ()
{
if (QueueDraw ()) {
return;
}
Gtk::EventBox::queue_draw ();
}
void
CairoWidget::queue_resize ()
{
if (QueueResize ()) {
return;
}
Gtk::EventBox::queue_resize ();
}
/** Handle a size allocation. /** Handle a size allocation.
* @param alloc GTK allocation. * @param alloc GTK allocation.
*/ */
void void
CairoWidget::on_size_allocate (Gtk::Allocation& alloc) CairoWidget::on_size_allocate (Gtk::Allocation& alloc)
{ {
Gtk::EventBox::on_size_allocate (alloc); if (!_canvas_widget) {
Gtk::EventBox::on_size_allocate (alloc);
} else {
memcpy (&_allocation, &alloc, sizeof(Gtk::Allocation));
}
#ifdef OPTIONAL_CAIRO_IMAGE_SURFACE #ifdef OPTIONAL_CAIRO_IMAGE_SURFACE
if (getenv("ARDOUR_IMAGE_SURFACE")) { if (getenv("ARDOUR_IMAGE_SURFACE")) {
@ -263,6 +331,9 @@ CairoWidget::on_size_allocate (Gtk::Allocation& alloc)
} }
#endif #endif
if (_canvas_widget) {
return;
}
set_dirty (); set_dirty ();
} }
@ -340,7 +411,7 @@ CairoWidget::set_active (bool yn)
void void
CairoWidget::on_style_changed (const Glib::RefPtr<Gtk::Style>&) CairoWidget::on_style_changed (const Glib::RefPtr<Gtk::Style>&)
{ {
queue_draw(); set_dirty ();
} }
void void
@ -356,7 +427,7 @@ CairoWidget::on_state_changed (Gtk::StateType)
set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive)); set_visual_state (Gtkmm2ext::VisualState (visual_state() & ~Gtkmm2ext::Insensitive));
} }
queue_draw (); set_dirty ();
} }
void void

View file

@ -35,6 +35,15 @@ public:
CairoWidget (); CairoWidget ();
virtual ~CairoWidget (); virtual ~CairoWidget ();
void set_canvas_widget ();
/* swizzle Gtk::Widget methods for Canvas::Widget */
void queue_draw ();
void queue_resize ();
int get_width () const;
int get_height () const;
void size_allocate (Gtk::Allocation&);
void set_dirty (cairo_rectangle_t *area = 0); void set_dirty (cairo_rectangle_t *area = 0);
Gtkmm2ext::ActiveState active_state() const { return _active_state; } Gtkmm2ext::ActiveState active_state() const { return _active_state; }
@ -65,6 +74,8 @@ public:
void set_draw_background (bool yn); void set_draw_background (bool yn);
sigc::signal<void> StateChanged; sigc::signal<void> StateChanged;
sigc::signal<bool> QueueDraw;
sigc::signal<bool> QueueResize;
static void provide_background_for_cairo_widget (Gtk::Widget& w, const Gdk::Color& bg); static void provide_background_for_cairo_widget (Gtk::Widget& w, const Gdk::Color& bg);
@ -125,6 +136,8 @@ protected:
Glib::SignalProxyProperty _name_proxy; Glib::SignalProxyProperty _name_proxy;
sigc::connection _parent_style_change; sigc::connection _parent_style_change;
Widget * _current_parent; Widget * _current_parent;
bool _canvas_widget;
Gdk::Rectangle _allocation;
}; };