Web lists-archives.com

Re: deprecated gtk_cairo_create




Hi;

On 30 December 2016 at 14:19, Chris Moller <moller@xxxxxxxxxxxxxx> wrote:
> Generally, what you need is something like:

No, not "generally" at all. This is only useful if you want to do
offscreen/off-the-main-thread rendering, which are very niche use
cases, compared to the vast majority of cases where you should just
draw on the Cairo context you're given.

Creating an intermediate surface on configure-event (something that's
there only for backward compatibility with GTK+ 1.2, and it's an
X11-ism) just to source it again is much more expensive than drawing
on the provided Cairo context; the cost is offset if you're going to
do things like rendering content in a separate thread to avoid
blocking the main thread, or if the data you're rendering does not
have a direct influence over the size of the widget.

> cairo_surface_t *surface;
>
> static gboolean
> da_configure_cb (GtkWidget *widget,
>                  GdkEvent  *event,
>                  gpointer   user_data)
> {
>   surface = gdk_window_create_similar_surface (gtk_widget_get_window
> (widget),
>                                    CAIRO_CONTENT_COLOR,
>                                    gtk_widget_get_allocated_width (widget),
>                                    gtk_widget_get_allocated_height
> (widget));
> }
>
> static gboolean
> da_draw_cb (GtkWidget *widget, cairo_t *cr, gpointer data)
> {
>   cairo_set_source_surface (cr, surface, 0.0, 0.0);
>
>   // cairo drawing stuff
> }

Your example also leaks the surface every time it gets resized.

> whatever()
> {
>   da = gtk_drawing_area_new ();
>   g_signal_connect (da, "configure-event",
>                     G_CALLBACK (da_configure_cb), whatever);

Use size-allocate, not configure-event, if you want to size something
depending on the allocation.

Ciao,
 Emmanuele.

> On 12/30/16 06:15, Sergei Kolomeeyets 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.
>
> 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.
>
> ***gdk_window_begin_draw_frame
> [bla-bla-bla...] When using GTK+, the widget system automatically places
> calls to gdk_window_begin_draw_frame() and gdk_window_end_draw_frame()
> around emissions of the GtkWidget::draw signal. That is, if you’re drawing
> the contents of the widget yourself, you can assume that the widget has a
> cleared background, is already set as the clip region, and already has a
> backing store. Therefore in most cases, application code in GTK does not
> need to call gdk_window_begin_draw_frame() explicitly.
>
> I use GTK+, so I can forget about gdk_window_begin_draw_frame for some time.
> Right?
> And there is only gdk_drawing_context_get_cairo_context in the rest of
> options. So I write the following:
>
> {main}
> ...
> /*Drawing area and text_entry at the bottom of it */
> area = gtk_drawing_area_new();
> text_entry = gtk_entry_new();
> vbox = gtk_vbox_new (FALSE, 0);
>
> gtk_box_pack_start(vbox, area, TRUE, TRUE, 0);
> gtk_box_pack_start(vbox, text_entry,  FALSE, FALSE, 0);
>
> gtk_container_add(GTK_CONTAINER(window), vbox);
> gtk_widget_show_all(window);
> gtk_main();
> ...
>
> {draw_callback}
>
> gboolean draw_callback(GtkWidget *area, GdkEventExpose *event, GArray
> *ptLinePoints) {
> ...
> cairo_t *cr = gdk_drawing_context_get_cairo_context (area);
> ...
> }
>
> 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...
>
> 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.
>
> Thanks in advance!
>
> Best Regards, (and Happy New Year for everyone here!!!)
> Sergey.
>
>
>
>
>
> _______________________________________________
> gtk-list mailing list
> gtk-list@xxxxxxxxx
> https://mail.gnome.org/mailman/listinfo/gtk-list
>
>
>
> _______________________________________________
> gtk-list mailing list
> gtk-list@xxxxxxxxx
> https://mail.gnome.org/mailman/listinfo/gtk-list
>



-- 
https://www.bassi.io
[@] ebassi [@gmail.com]
_______________________________________________
gtk-list mailing list
gtk-list@xxxxxxxxx
https://mail.gnome.org/mailman/listinfo/gtk-list