TreeView column widths



Hello!

I'm developing a (small) application centered around one big table. I
use PyGTK.

<Side remarks>

I have broad experience developing GUI applications with Delphi. Some
things seemed very unnatural to me at first. In Delphi, you design GUI
"outside-in": the widgets get their size from parent containers via
"anchors", so the size of the windows basically determines the layout
of the widgets. From my much smaller experience with GTK, I see that
GTK dialogs are designed "inside-out": the widgets ask their parent
containers for needed size, and the resulting size of the window is
actually a function of it's contents.

Actually, I like the GTK way (much) more -- it's more "semantic", you
don't need to explicitly calculate and set the bounds of parent
controls. And even if I did, I couldn't do it more accurately.
However, in some cases I would prefer more outside-in approach -- see
below.

</Side remarks>

I'm having problems with implementing desired table behavior. I wish
the following:

1) if text in a cell occupies more then one line, it should be
word-wrapped

2) by default, columns should be assigned "sensible" widths (equal
widths is enough sensible)

3) columns are resizeable by user

Item 2) suggests that I should set expand property to True, for all
columns. In this case however, I get really inacceptable behavior if
the user resizes a column. Usually user would like to stretch column
to see more data -- but with expand set to True all other columns will
get less size, word-wrapping will trigger, and user will actually see
less:

| Column 1   | Column 2   | Column 3   |
----------------------------------------
| some text  | this line  | some text  |
|____________|_is_long____|____________|
|_some_text__|_short_line_|_some_text__|
|_some_text__|_short_line_|_some_text__|

will become

| Column| Column 2             | Column|
| 1     |                      | 3     |
----------------------------------------
| some  | this line is long    | some  |
|_text__|______________________|_test__|
| some  | short line           | some  |

Note that only 2 lines are now visible instead of 3!

What is desired is that no other columns change their size. That would
require a scrollbar, but in this situation it's Ok:

| Column 1   | Column 2             | C|
----------------------------------------
|_some_text__|_this_line_is_long____|_s|
|_some_text__|_short_line___________|_s|
|_some_text__|_short_line___________|_s|
|_some_text__|_another_line_ _______|_s|
[<][________________________]________[>]

So now it looks like I should set expand to False. However, it's not
obvious how to setup sensible "default" column widths then. The
problem is that the window is not fully initialized at the moment I
construct my TreeView. I did the following:

def create_column(self, name, index):
    renderer = gtk.CellRendererText()
    column = gtk.TreeViewColumn(name, renderer, text = index)
    column.set_min_width(1)
    column.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
    column.set_expand(True)

    def adjust_width(column, width):
        if column.get_fixed_width() != column.get_width() or column.get_expand():
            column.set_fixed_width(column.get_width())
            column.set_expand(False)

    column.connect('notify::width', adjust_width)

# after all columns added to TreeView
for column in self.tableview.get_columns():
    column.set_fixed_width(column.get_min_width())

Basically, I let expand be True until the window is initialized, and
then remember automatically calculated widths. But it doesn't always
work: the window manager may reset the size of the window according to
it's config (i.e. maximize the window), and column width won't get
updated.

I believe it could be solved if GTK had a kind of "manual column
resize" signal, but it doesn't seem to :-(

Another problem I had with TreeView is that you cannot resize the
rightmost column. Often it's not needed as the rightmost column
already takes all available space. But it could be very useful for
TreeView-in-a-ScrollWindow -- you would like to be able to shrink the
last column like any other one. I use the following ugly hack:

def add_dummy_column(treeview):

    def put_dummy_last(treeview, dummy):
        columns = treeview.get_columns()
        last = next(reversed(columns), None)
        if not last or last == dummy:
            return
        if dummy in columns:
            treeview.move_column_after(dummy, last)
        else:
            treeview.append_column(dummy)

    dummy = gtk.TreeViewColumn()
    dummy.set_min_width(1)
    dummy.set_fixed_width(1)
    dummy.set_sizing(gtk.TREE_VIEW_COLUMN_FIXED)
    dummy.set_resizable(False)
    dummy.set_expand(False)
    treeview.connect('columns-changed', put_dummy_last, dummy)
    return dummy

All-in-all, it looks like GTK is not really designed for such an
"outside-in" layout, which table with word-wrapped cells is. But I
admit I may be missing something. Do you think it's possible to
implement the desired functionality more clearly?

Best wishes,
Andrey Paramonov



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