Re: Drawing a widget as a drag icon (without going down to C)



On 6/23/19 3:35 PM, Daniel Boles via gtkmm-list wrote:
I have a small widget that can be a drag/drop source/sink. When it's dragged, I want it to be the drag-icon, so you can see that you're dragging that particular widget (and its particular label, etc.)

However, Gtk::Widget::draw(Cairo::RefPtr<Cairo::Context> const&) is protected in gtkmm, so it seems that in order to draw the widget onto a surface that I can then set as drag-icon, I must do:

```
auto const allocation = m_box_label.get_allocation();
auto const surface = Cairo::ImageSurface::create(
  Cairo::FORMAT_ARGB32, allocation.get_width(), allocation.get_height() );
auto const cairoContext = Cairo::Context::create(surface);
gtk_widget_draw( reinterpret_cast<GtkWidget*>( m_box_label.gobj() ), cairoContext->cobj() );
dragContext->set_icon(surface);
```

Is there any better way that I didn't think of, any way to make this nicer in gtkmm, or is it just a fact of life at this point? (Since gtkmm-3 is stable and master is probably very different here.)

I don't know why Widget::draw() is protected. Perhaps someone misunderstood it, and thought that it draws from the cairo context to the widget, instead of the other way.

gtk_widget_draw() and Gtk::Widget::draw() don't exist in gtk4/gtkmm4.

An alternative, not necessarily better, but without C code:

class MyBoxLabel : public TheTypeOf_m_box_label
{
public:
  using TheTypeOf_m_box_label::TheTypeOf_m_box_label // same constructors as the base class
  void draw(const ::Cairo::RefPtr< ::Cairo::Context>& cr)
  {
    Gtk::Widget::draw(cr);
  }
};

This might work, but I'm not sure:

class MyBoxLabel : public TheTypeOf_m_box_label
{
public:
  using TheTypeOf_m_box_label::TheTypeOf_m_box_label // same constructors as the base class
  using Gtk::Widget::draw;
};



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]