Re: gtk_widget_set_size_request delayed until next allocation change




On 04/27/13 02:35, Sandro Mani wrote:
Hello,

I have a GtkDrawingArea inside a GtkViewport, and I have a callback
connected to the "size-allocate" signal of the GtkViewport which resizes
the GtkDrawingArea by calling gtk_widget_set_size_request on the latter,
see sample source code below.

I however notice that the GtkDrawingArea allocates the requested size
only the _next_ time the allocation of the GtkViewport changes, and
hence the allocation of the GtkDrawingArea is the one requested for the
previous allocation of the GtkViewport.

The issue is best visible when maximizing and unmaximizing the window:
when maximizing, the size of the GtkDrawingArea is still the one for the
unmaximized state, and when unmaximizing again, the size of the
GtkDrawingArea is the one for the maximized state.

Question: Is this a mistake in my code or a Gtk bug? I'm using
gtk3-3.8.1-1.fc20.x86_64

Seems you could fix it by setting GTK_RESIZE_IMMEDIATE as resize mode on
the viewport, although it seems to be deprecated.

I'm not sure what's the proper/recommended way to go it instead, but
instead of calling gtk_widget_queue_resize() and the
gtk_main_iteration() loop in resize_drawarea() you could call
gtk_container_resize_children() (on the viewport).
Although, since it triggers a new size-allocate, it might be a good idea
to block your handler to avoid it getting triggered again.

-j

Thanks for any inputs.

Sandro

------ Sample code: ------
// gcc alloc.c -o alloc $(pkg-config --cflags --libs gtk+-3.0)
#include <gtk/gtk.h>
#include <stdio.h>

void resize_drawarea (GtkWidget* viewport, GdkRectangle* rect, gpointer
data)
{
  gtk_widget_set_size_request (GTK_WIDGET (data), rect->width / 2,
rect->height / 2);
  gtk_widget_queue_resize (GTK_WIDGET (data));
  printf("set_size_request(%d, %d)\n", rect->width /2, rect->height / 2);
  // Wait for size_request
  while (gtk_events_pending ()) {
    gtk_main_iteration ();
  }
  int w = gtk_widget_get_allocated_width (GTK_WIDGET (data));
  int h = gtk_widget_get_allocated_height (GTK_WIDGET (data));
  printf("allocation = %d x %d\n\n", w, h);
}

int main(int argc, char* argv[])
{
  gtk_init (&argc, &argv);
  GtkWidget* window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  GtkWidget* viewport = gtk_viewport_new (NULL, NULL);
  GtkWidget* drawarea = gtk_drawing_area_new ();

  gtk_container_add (GTK_CONTAINER (window), viewport);
  gtk_container_add (GTK_CONTAINER (viewport), drawarea);

  gtk_widget_set_halign (drawarea, GTK_ALIGN_CENTER);
  gtk_widget_set_valign (drawarea, GTK_ALIGN_CENTER);
  GdkRGBA black = {0., 0., 0., 1.};
  gtk_widget_override_background_color (drawarea, GTK_STATE_FLAG_NORMAL,
&black);
  gtk_window_set_default_size (GTK_WINDOW (window), 640, 480);

  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  g_signal_connect (viewport, "size-allocate", G_CALLBACK
(resize_drawarea), drawarea);

  gtk_widget_show_all (window);
  gtk_main ();
  return 0;
}

_______________________________________________
gtk-app-devel-list mailing list
gtk-app-devel-list gnome org
https://mail.gnome.org/mailman/listinfo/gtk-app-devel-list


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