Re: clist column autosizing issues



On Wed, 2002-03-13 at 04:15, Pavel Rousnak wrote:
When I issue a $clist->columns_autosize; the columns of $clist are sized
to appropriate sizes for the content, but not the titles.  For example:

my $clist = Gtk::CList(qw(tabname, owner, partnum, tabid rowsize));

my @data = (
    [qw(systables,  informix, 1048676, 1, 104)],
    [qw(syscolumns, informix, 1048677, 2, 36)],
    [qw(sysindexes, informix, 1048678, 3, 78)],
    [qw(systabauth, informix, 1048679, 4, 28)],
);

foreach (@data) {
    $clist->append(@$_);
}

$clist->columns_autosize;

When I run code like this the rowsize column gets set to a width smaller
than the Gtk::Label.  I have tried to fix this by replacing the call to
columns_autosize with code that looks like this:

for (my $i = 0; $i < $clist->columns; $i++) {
    my $widget    = $clist->get_column_widget($i);
    my $width     = (@{$widget->allocation})[2];
    my $opt_width = $clist->optimal_column_width($i);
    $clist->set_column_width($i, $opt_width) if $width < $opt_width;
}

but $width is equal to 1 for every row.  Any ideas about what I am doing
wrong?


I've written a test from your example (attached). Everything works fine.
Columns are resized as expected.
Could you send a complete test reflecting that strange behavior?


----


#!/usr/bin/perl

use Gtk 0.7006;
use strict;

Gtk->init();

my $window = new Gtk::Window('toplevel');
$window->signal_connect('delete_event', sub { Gtk->main_quit(); return 0; });

my $clist = new_with_titles Gtk::CList(qw(tabname owner partnum tabid rowsize));

my @data = (
      [qw(systables  informix 1048676 1 104)],
      [qw(syscolumns informix 1048677 2 36)],
      [qw(sysindexes informix 1048678 3 78)],
      [qw(systabauth informix 1048679 4 28)],
);

foreach (@data) {
      $clist->append(@$_);
}
$window->add($clist);
$window->show_all();
print_allocations($clist, "Before autosize");
$clist->columns_autosize;
print_allocations($clist, "After autosize");

Gtk->main();

#------------------------------------------------------------
sub print_allocations {
  my($clist, $msg) = @_;
  print "$msg\n" if $msg;
  for (my $i = 0; $i < $clist->columns; $i++) {
    my $widget    = $clist->get_column_widget($i);
    my $alloc = $widget->allocation();
    print "$i: ", join(" ",@$alloc), "\n";
  }
}
----


Before autosize
0: 3 3 38 17
1: 3 3 28 17
2: 3 3 37 17
3: 3 3 22 17
4: 3 3 35 17
After autosize
0: 3 3 55 17
1: 3 3 37 17
2: 3 3 44 17
3: 3 3 29 17
4: 3 3 42 17
----


Best regards
Pavel.

Well, I don't know what to say.  I ran the tcl1.pl on my machine and it
produced the following results (very similar to yours):

Before autosize
0: 3 3 47 16
1: 3 3 34 16
2: 3 3 44 16
3: 3 3 27 16
4: 3 3 43 16
After autosize
0: 3 3 66 16
1: 3 3 43 16
2: 3 3 51 16
3: 3 3 34 16
4: 3 3 50 16

But my real code still doesn't work.  The only difference I can think of
is that the Gtk::CList is not visible (it is on an unseen tab) when I
issue the columns_autosize for the list.  I am going to focus on the new
tab and see if that fixes the problem.  Here is the relevant function. 
Unfortunately you need an Informix DB to run the script (it is an SQL
editor ala Informix SQL Editor).  If any one has postgresql installed
and wants to take a stab at the problem I can hack together a reduced
functionality version that connects to it instead.

sub runsql {
    our $dbh;

    unless ($dbh) {
        error('No database selected');
        return;
    }

    my  ($text, $nb, $stat) = @_; 
    #this needs to be much more intelligent, but it solves 90% 
    #of the use cases (single query or batch of queries with no 
    # {} comments
    my  @statements = split /;/, $text->get_chars(0, -1);
    our $sql_count;

    foreach my $sql (@statements) {
        next unless $sql =~ /\S/;
        $stat->set_status('Preparing...');
        my $sth = $dbh->prepare($sql);
        if ($dbh->err) {
            error($dbh->errstr);
            last;
        }
        $stat->set_status('Running...');
        $sth->execute;
        if ($dbh->err) {
            error($dbh->errstr);
            last;
        }
        my $cols = $sth->{NUM_OF_FIELDS};
        unless ($cols) {
            my $rows = $sth->rows; 
            $stat->set_status("$rows were affected");
            next;
        }
        my @cols = @{$sth->{NAME}};

        my $sw = Gtk::ScrolledWindow->new;
        my $list = Gtk::CList->new_with_titles(@cols);

        $sw->set_policy('automatic','automatic');

        $nb->append_page($sw, Gtk::Label->new('results '.++$sql_count));
        $sw->add($list);

        $nb->show_all;

        while (my $ref = $sth->fetch) {
            no warnings;
            $list->append(@$ref);
            use warnings;
        }

        $stat->set_status($sth->rows . ' selected');
        $sth = undef;

        $list->signal_connect('click_column', \&sort_column);

        print "pre autosize\n";
        for (my $i = 0; $i < $list->columns; $i++) { 
            print "@{$list->get_column_widget($i)->allocation}\n";
        }

        $list->columns_autosize;

        print "post autosize\n";
        for (my $i = 0; $i < $list->columns; $i++) { 
            print "@{$list->get_column_widget($i)->allocation}\n";
        }

        $nb->show_all;
    }
}

-- 
Today is Boomtime the 72nd day of Chaos in the YOLD 3168
Kallisti!

Missile Address: 33:48:3.521N  84:23:34.786W




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