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

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Here's the code I came up with for auto-adjusting row height in case anyone needs it. It sets the height of each row to accommodate the tallest cell in the row, based on the width of each column and the content of each cell. Of course it's an approximation so it's occasionally off by one line. You can tune it by changing the -8 value. It's reasonably fast, it reflows 5000 rows in about 2 sec.
On another note, anyone know how I can automatically call the setheight sub when the width of a column is changed (by drag and drop)? If I could detect the change of the width I wouldn't need a separate button for this.

#!/usr/bin/perl use strict; use warnings; use Tk; use Tk::Dialog; use Tk::TableMatrix; use utf8; sub setheight; my $mw = MainWindow->new; $mw->minsize(800, 500); # minimum size of main window in pixels + my $textfont = '-*-Courier-Medium-R-Normal--*-140-*-*-*-*-*-*'; my $arrayVar = {}; while (<DATA>) { my $i = $. - 1; /^([^\t]*)\t([^\t]*)/; $arrayVar->{"$i,0"} = "$1"; $arrayVar->{"$i,1"} = "$2"; } my $rows = $.; # print "\nrows: $rows\n"; my $cols = 2; # number of columns my $t = $mw->Scrolled('TableMatrix', -scrollbars => "osoe", -font => $textfont, -resizeborders => 'col', # none, col, row -bordercursor => "sb_h_double_arrow", -rows => $rows, -cols => $cols, -wrap => 1, -justify => 'left', # this only affects mul +tiline cells -anchor => 'nw', # this aligns text to +the top left # -rowheight => 1, -colwidth =>-400, -variable => $arrayVar, # -colstretchmode => 'all', )->pack(-expand => 1, -fill => 'both'); setheight; # initial height adjustment; height will be readjusted l +ater when necessary by calling the same sub my $buttexit = $mw->Button( -text => "Exit", -command => sub{ $mw->des +troy}); my $buttheight = $mw->Button( -text => "Set height", -command => sub { +setheight;}); $buttheight->pack(); $buttexit->pack(); $buttexit->focus( -force ); # my $cursorpos = $t->icursor(); # my $activeindex = $t->index('active'); # my $activecont = $t->get('active'); Tk::MainLoop; sub setheight { for my $i (0 .. $rows - 1) { # print "\n\nrow $i"; for my $col (0 .. $cols - 1) { $t->activate("$i,$col"); my $activecont = $t->get('active'); # print "\n$i,$col"; my $charcount = length($activecont); # print " length: $charcount"; my $width = $t->colWidth($col,) / -8; # approx width in ch +aracters my $reqheight = int(length($activecont) / $width) + 1; # print ", required height: $reqheight"; my $currheight = $t->rowHeight($i); $t->rowHeight($i, $reqheight) if ( ($col == 0) or ($reqhei +ght > $currheight)); # set row height for the height of the tallest c +ell in the row } } } __DATA__ this is a relatively long string that will probably end up displaye +d on two lines if the window isn't wide enough this is a relatively l +ong string that will probably end up displayed on two lines if the wi +ndow isn't wide enough this is not that short, eitherrr lalala)(=/%!+"'§΄&#729;`&#731;°^&# +711;~ well, this cell contains quite a lot of text so I'm expecting it will +be wrapped lalalaια&#369;ϊ&#337;νσόΦ)(=/%!+"'§΄&#729;`&#731;°^&#71 +1;~

In reply to Re: Auto-adjust row height to line wrap in Tk-Tablematrix by elef
in thread Auto-adjust row height to line wrap in Tk-Tablematrix by elef

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    NodeReaper adds to his collection

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (4)
    As of 2018-05-24 18:23 GMT
    Find Nodes?
      Voting Booth?