Re: deprecated gtk_cairo_create



Hi;

On 30 December 2016 at 11:15, Sergei Kolomeeyets <kolomeeyets gmail com> wrote:
Hi, everyone
I'm struggling with migration from gtk2. Well gtk_cairo_create is deprecated
in gtk3. And it looks  the changes are very serious (or I'm sach silly that
even do not understand what should I do instead of it ). Is there anybody
who use Drawing Area in GTK3 and can explain the right way of its cairo
context creation and drawing on it to me. It would be so appreciated.

Connect to the GtkWidget::draw signal (or subclass GtkDrawingArea and
override the GtkWidgetClass.draw virtual function, which is
essentially the same) and use the Cairo context that's given to you.

Drawing *on widgets* outside of their rendering cycle is not possible
in GTK+ 3.x.

If you want to draw something off screen and then composite it inside
your widget, you'll need to create a Cairo surface yourself, using
something like:

  GdkWindow *window = gtk_widget_get_window (widget);
  cairo_surface_t *surface = gdk_window_create_similar_surface
(window, CAIRO_CONTENT_COLOR_ALPHA, width, height);

then create a Cairo context from it, and draw:

  cairo_t *cr = cairo_create (surface);
  // draw
  cairo_destroy (cr);

Now you can use the surface as a source on the Cairo context that
GtkWIdget::draw gives you:

  static gboolean
  on_draw (GtkWidget *widget, cairo_t *cr)
  {
    // render your surface on the widget's context
    cairo_set_source_surface (cr, surface, x, y);
    cairo_paint (cr);

    // do your own drawing
  }

Documentation says the following:
***gtk_cairo_create
gdk_cairo_create has been deprecated since version 3.22 and should not be
used in newly-written code. Use gdk_window_begin_draw_frame() and
gdk_drawing_context_get_cairo_context() instead.

This is only needed if you're literally implementing a toolkit (like
GTK) on top of GDK.


gboolean draw_callback(GtkWidget *area, GdkEventExpose *event, GArray
*ptLinePoints) {
...
cairo_t *cr = gdk_drawing_context_get_cairo_context (area);
...
}

This is *not* a GtkWidget::draw callback. You should read the API reference:

  https://developer.gnome.org/gtk3/stable/GtkWidget.html#GtkWidget-draw

The second argument of the callback is a Cairo context; use it.

But linker (gcc package) writes  "undefined reference to
`gdk_drawing_context_get_cairo_context"... :-(
Everything indicated that I missed several lines of code before
gdk_drawing_context_get_cairo_context. But what lines exactly? Googling
gives nothing...

It means you're not using GTK+ 3.22, but likely an earlier version.

In any case, you should just forget about the drawing context, and use
the Cairo context that GTK itself gives you.

Ciao,
 Emmanuele.

-- 
https://www.bassi.io
[@] ebassi [@gmail.com]


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