Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re: Auto-adjust row height to line wrap in Tk-Tablematrix

by BrowserUk (Patriarch)
on Feb 09, 2013 at 14:51 UTC ( [id://1017967]=note: print w/replies, xml ) Need Help??


in reply to Auto-adjust row height to line wrap in Tk-Tablematrix

Making no claim to understanding tcl; what the code appears to be doing is:

  1. for each row in the table;
  2. query the text for each column within that row;
  3. split that text on newlines;
  4. find the maximum number of lines in any column within that row;
  5. set the row height for that row accordingly.

SOmething roughly equivalent in Perl terms to:

my $maxRows = $tableMatrix->cget( -rows ); my $maxCols = $tableMatrix->cget( -cols ); for my $r ( 1 .. $maxRows -1 ) { set $maxHeight = 1; for my $col ( 0 .. $maxCols - 1 ) { my $text = $tableMatrix->getText( -row => $row, -col => $col ) +; my $nLines = scalar split "\n", $text; if( $nLines > 1 and $maxHeight < $nLines ) { $maxHeight = $nLines; } } $tableMatrix->cset( row => $row, height => $maxHeight ); }

You'll have to work out the appropriate methods to call for what I've shown as getText() & cset().


With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^2: Auto-adjust row height to line wrap in Tk-Tablematrix
by elef (Friar) on Feb 09, 2013 at 15:22 UTC
    This seems to assume that there are \n characters in the strings. I am using -wrap => 1, to get automatic line wraps, and I don't think that it inserts \n in the string. It just displays it wrapped without altering the string itself, so I don't think my $nLines = scalar split "\n", $text; would work. But I've been wrong before...
    Of course I could perhaps query the width of each column, use a monospace font and do the line wraps myself with \n, in which case of course I would know how many lines each cell is wrapped into, and I could set the height accordingly. But that would essentially mean that I'm rewriting the functionality of -wrap from scratch. There should be a better way - to be honest, I don't really see the usefulness of -wrap if there is no height auto-adjustment functionality. Instead of hanging off the cell to the right, text just ends up hanging off the top and bottom.
      This seems to assume that there are \n characters in the strings. I am using -wrap => 1, to get automatic line wraps, and I don't think that it inserts \n in the string.

      Hm. I could be wrong, but I'd bet my bottom dollar that wrap is implemented by inserting newlines into the text.

      This part of the tcl code:if {[set n [llength [split [.table get $r,$c] \n]]]>1 && $maxh<$n} { breaks down to:

      1. Get the text from cell $r, $c: [.table get $r,$c]
      2. Split that text on newlines: [split $text \n]
      3. Calculate the list length of the result from split: [llength <list> ]
      4. Set n to that value: [set n $length ]
      5. If the number of lines is greater than one; and greater than the current value of maxh(eight), then set maxh to n;

        (Note: that first test is redundant.)

      You would be wasting your time doing font calculations yourself, cos the tablematrix must already be doing them in order to perform the wrapping.


      With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I did some testing, \n not found.
        use Tk; use Tk::Dialog; use Tk::TableMatrix; my $mw = MainWindow->new; $mw->minsize(600, 300); # minimum size of main window in pixels + my $textfont = '-*-Helvetica-Medium-R-Normal--*-140-*-*-*-*-*-*'; my $arrayVar = {}; while (<DATA>) { my $i = $. - 1; /^([^\t]*)\t([^\t]*)/; $arrayVar->{"$i,0"} = "$1"; $arrayVar->{"$i,1"} = "$2"; } my $t = $mw->Scrolled('TableMatrix', -scrollbars => "osoe", -font => $textfont, -resizeborders => 'none', -rows => 3, -cols => 2, -wrap => 1, -rowheight => 4, -variable => $arrayVar, -colstretchmode => 'all', ); my $buttexit = $mw->Button( -text => "Exit", -command => sub{ $mw->des +troy}); my $buttinfo = $mw->Button( -text => "View cellinfo", -command => sub{ my $cursorpos = $t->icursor(); my $activecont = $t->get(active); my $activeindex = $t->index('active'); my $linebreak; if ($activecont =~ /\n/) {$linebreak = "found"} else {$linebreak = + "not found"}; $mw->Dialog( -title => 'Info', -text => "current cell contains:\n\n$a +ctivecont\n\nindex of active cell: $activeindex\nCursor position with +in cell: $cursorpos\n\nLine break $linebreak", -default_button => 'OK', -buttons => ['OK'] )->Show( ); }); $t->pack(-expand => 1, -fill => 'both'); $buttexit->pack(-expand => 1, -fill => 'both'); $buttinfo->pack(-expand => 1, -fill => 'both'); Tk::MainLoop; __DATA__ this is a relatively long string that will probably end up displayed o +n two lines if the window isn't wide enough blabla foo bar tralla lalala


        When I click into the cell with the long text and click the View cell info button, the dialog shows the text without line breaks and reports that \n is "not found".
        Am I doing something wrong or is this approach a dead end?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1017967]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (4)
As of 2024-04-19 03:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found