Prefered way to change the main window initial size



Dear GTK+,

In short...

It seems to me that the main window is initially displayed using the
minimum size (as reported by 'gtk_widget_get_preferred_size()'. I wished
it would use the maximum size instead, is there any straightforward way
to do that ?

Long story...

At first, my main window contained a GtkTreeView, and everything worked
fine. The default height of the window depended on the number of rows in
the GtkTreeView, so the window was created with the perfect height.

With time (and more rows), I had to put the GtkTreeView within a
GtkScrolledWindow. From this moment, the main window is created with
quite a small height, that doesn't depend on the number of rows anymore.
It means that, by default, I only see 2 or 3 rows. I wish to get back to
the previous behavior, where the window smartly scales according to the
number of rows.

I actually achieved that behavior, but I'm not sure I did it the right way.

At first, I used 'gtk_scrolled_window_set_propagate_natural_height()'.
Thanks to that, when I call 'gtk_widget_get_preferred_size()' on the
main window, the maximum size reported takes into account the number of
rows in the TreeView. This size is the one I want for my main window,
but it looks like it's not used. Instead, the minimum size is used: my
main window is still too small.

In order to use the maximum size, I hooked on the 'realize' signal of my
GtkTreeView (hooking on the 'realize' signal of the main window is too
early). At this moment, the maximum size reported by
'gtk_widget_get_preferred_size()' for the main window is correct (ie: it
takes into account the number of rows in the GtkTreeView). I then resize
my window with a call such as:

    gtk_widget_get_preferred_size(GTK_WIDGET(self), &min, &max);
    gtk_window_resize(GTK_WINDOW(self), max.width, max.height);

This gets the job done.

It's important to understand that, at construct-time, the height of the
GtkTreeView is not known. Therefore I can't use
'gtk_window_set_default_size()' at this moment. I have to wait, that's
why I hook on the 'realize' signal (not knowing if it's the right signal
to use). When the signal handler is called, it's then too late to use
'gtk_window_set_default_size()', so I use 'resize()' instead.

My questions now are:

1. Is there a better approach ?
2. Is it ok/safe to gtk_call window_resize() from a 'realize' signal
handler ?

BTW, this story happens on GTK+3.

Best regards,

Arnaud



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