Re: Tree View Cell Size Negotiation



Hi Bob,

On Tue, Sep 04, 2007 at 10:52:24PM -0700, Bob Murphy wrote:
> As part of this process, cell renderers seem to use a "width-for-height" 
> approach. It is assumed that the cell renderer will know in advance how 
> tall each cell will be (typically, all cells will be the same height), 

This is not necessarily true, when we do a get_size() call on the cell
renderer it does not know anything in advance and is free to return any
(width, height) it wants.  And typically when using rows with images and
(single-line) text the rows will all be the same height.

> I need to do something quite different: a "height-for-width" approach, 
> as follows:
> 1. There is a parent window with a fixed width and height.
> 2. Inside the parent window is a GtkTreeView (with a scroll bar) that is 
> as wide as the parent will let it get.
> 3. The cells inside the tree view contain text, drawn with a custom cell 
> renderer I've written. They need to be as wide as the tree view, and as 
> tall as is needed to display all their text.
>
> One way to do this is to trigger the size negotiation twice. The first 
> pass would set the tree view to its maximum width, and the cell sizes 
> would be ignored. The second pass would be used to determine the actual 
> cell sizes. But this seems crude.
>
> A more elegant approach would be to set the tree view to its maximum 
> width before it asks the cells what size they want to be. But I haven't 
> been able to figure out how to do this.

I don't see any other way to get this done than to measure the cells in
two "passes".  We cannot know the height of the cells until the width
the tree view will receive is known and during the initial size-request
the GtkTreeView will have to measure some cells -- there is nothing we
can change here.

With a GtkCellRendererText set up for wrapping text, something like this
will work:
 - When creating the GtkTreeView and the GtkCellRendererText, set the
   "width" property of the cell renderer to the minimum width you want
   the cells to be.   Setting this property will override the width the
   cell would return after measuring the PangoLayout.

 - Make sure you set the "wrap-width" property of the renderer to
   the available width to the cell after the GtkTreeView has finished
   a size-allocation.  Getting the full available width is currently
   tricky, you could use gtk_tree_view_get_cell_area() and subtract the
   x padding and focus-line-width (the former is a cell property, the
   latter a style property).

 - After changing the "wrap-width" property of the renderer you want to
   call gtk_tree_model_row_changed() for each row in the list.  This
   will make GtkTreeView forgot about the cached height for that row and
   remeasure the cells.  Now "wrap-width" is set the cells will return a
   correct height (note that the width is still overridden here).


The procedure outlined here is specific for GtkCellRendererText, but
should work for any cell that wants to wrap text with minor adaptions.

Hope this helps!


regards,

-kris.



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