Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

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

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


in reply to Re^2: Auto-adjust row height to line wrap in Tk-Tablematrix
in thread Auto-adjust row height to line wrap in Tk-Tablematrix

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.

Replies are listed 'Best First'.
Re^4: Auto-adjust row height to line wrap in Tk-Tablematrix
by elef (Friar) on Feb 09, 2013 at 18:15 UTC
    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?
      Am I doing something wrong or is this approach a dead end?

      My guess is that the TableMatrix widget keeps two copies of the text. One that reflects the applications view of that text; and one that gets manipulated for the display purposes. And what you are querying is the former rather than the latter.

      Maybe if you could work out the appropriate incantations for using the windowCget() method to obtain the text from the cells directly rather than via the TableMatrix, it might return the wrapped text.

      But, I'm guessing. I pretty much gave up trying to do anything clever with Tk because the (perl/tk) docs are so bad; and half of what you see in the tcl samples doesn't translate. Sorry.


      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.
        My guess is that the TableMatrix widget keeps two copies of the text. One that reflects the applications view of that text; and one that gets manipulated for the display purposes. And what you are querying is the former rather than the latter.
        That's what it looks like.

        Maybe if you could work out the appropriate incantations for using the windowCget() method to obtain the text from the cells directly rather than via the TableMatrix, it might return the wrapped text.
        That could work but I don't know how to figure out said incantations. I might email the TableMatrix author to see if he has some easy solution up his sleeve but the module hasn't been updated in 5 years so hopes are slim.


        I also found this:
        It looks like that TableMatrix does not support auto-resize. But it won't be too difficult for you to calculate the number of lines that the text of a cell occupies. To simplify the matter: 1) don't auto-resize the width (use ColWidth function to get the current width of a cell), and only auto-resize the height; 2) Measure height and width in characters, not pixels. When you calculate the number of lines, don't forget to handle special characters like \n. Call RowHeight to resize the cell (height only). - http://qs1969.pair.com/~perl2/index.pl?node_id=882420

        The trouble is that I would need to abandon width auto-resizing (it seems that colWidth doesn't report correct values if a column is auto-resized to fit the window. On top of that, if a column is resized by dragging the border, it reports width in pixels, not characters.)

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-03-29 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found