Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re^4: Query the height of Tk::Text widget with word wrap

by Anonymous Monk
on Dec 01, 2013 at 13:04 UTC ( [id://1065145]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Query the height of Tk::Text widget with word wrap
in thread Query the height of Tk::Text widget with word wrap

This part response to Re^2: Query the height of Tk::Text widget with word wrap

Well, click once, resize window so there is wrapping, click again, compare the numbers

dlineinfo/bbox apparently mostly works for stuff that is visible , that isn't scrolled out of the view

Wow, that's written in a complicated way.

Sure its not :D subs are the fundamental units of code, the essence of good programming, they are testable, if used with argument passing they provide scoping, argument passing eliminates circular references and leaks with closures ... why is my-Tk-prog taking so much memory? It isn't, oh thank you mr non-circular-non-leaky:)

sub foo { my( $mw, $t ) ... } sub bar { my( $mw, $t ) ... } sub Main { GoTk(...); GoTk(...); } sub GoTk { ... [\&foo, $mw, $t] ... [\&bar, $mw, $t] ... MainLoop }

more on this tk closures people, Re: TK Submenus (Tk::Menu , global variables/ spirit of strict), tk scoping function argument passing, can't share Tk mainwondow between threads

This part response to Re^3: Query the height of Tk::Text widget with word wrap

I still can't get dlineinfo to work.

Remember how I (my program) had a button? Meaning you only (it only) call dlineinfo after MainLoop starts and draws everything on screen?

If the widgets (text ...) aren't drawn, dlineinfo can't return coordinates ... so if you want it to work in this program you need to draw the widget, you need $tw->update; before dlineinfo stuff

I used $tw->update; print "now its drawn, now it will be visible\n"; sleep 1;

The window can be located off-the-monitor while the initial drawing takes place :)  $mw->geometry('-1000-1000');

Replies are listed 'Best First'.
Re^5: Query the height of Tk::Text widget with word wrap
by elef (Friar) on Dec 02, 2013 at 11:15 UTC
    Thanks, ->update fixes it.
    I now have a simple script that automatically sets the correct height on rows based on the pixel values from dlineinfo. However, it only works on the cells that are in the screen; it fails on the cells that are off the bottom.
    I could cycle through all the cells, putting the contents of each one into cell 0,0 (first row), storing the required height for each one. Then I could draw the final table with those values. But I would probably get a lot of flickering as the cell is refreshed. I can't just move the $mw off the screen because it's visible already when this process starts. Maybe I could try creating a new toplevel and withdrawing it right away... but of course that only works if dlineinfo returns values from a withdrawn window. Perhaps create a new toplevel, move it off the screen and try to hide the icon in the taskbar somehow? Perhaps use a Dialog instead of a toplevel window so I don't get a taskbar icon?
    I'll be trying these things but I'd be grateful for tips in case they don't work or there is a better option.
      well, you only need one single appropriately sized text widget to determine how the wraps wrap before adjusting the matrix ... and yes it can be off-screen as long as its being drawn ...
        Here's what I have. It works, but it puts an icon in the system tray while it's doing the calculations. Any idea about how to get rid of that? My googling turns up nothing. I tried a Dialog instead of a toplevel window but dialogs stop everything and require the user to click a button to proceed. It looks like that feature can't be disabled.
        It's also pretty slow. It takes about 3 or 4 seconds for 500 rows. I'm thinking about how to speed it up but I'm not sure.

        use strict; use warnings; use Tk; use Tk::TableMatrix; require Tk::Dialog; my $mw = Tk::MainWindow->new; $mw->geometry('1000x800'); # TableMatrix my $t = $mw->Scrolled('TableMatrix', -rows => 500, -cols => 1, -colwidth => -500, -scrollbars => 'e' )->pack(-expand => 1, -fill => 'both');; my $width = $t->colWidth(0); # print "\ncolumn width: $width\n"; $width = $width * -1; my $heightwin = $mw->Toplevel(-title => 'Height set window'); $heightwin->geometry("${width}x1000-2000-2000"); my $twtest = $heightwin->Text(-wrap=>'word', -width=> $width)->pack(-e +xpand=> 1, -fill=>'both'); $mw -> focus; my @tw; while (<DATA>) { my $i = $. - 1; chomp; # $tw[$i] = $heightwin->Text(-wrap=>'word', -width=> $width)->pack +(-expand=> 1, -fill=>'both'); $twtest->delete('0.0','end'); $twtest->insert('end', $_); my $endidx = $twtest->index('end - 1 chars'); # print "end index: $endidx\n"; $twtest->update; my @dline = $twtest->dlineinfo($endidx); my $high = -1 * ($dline[1] + 17); #do customize '+ 17' based on + font size # print "\nSetting height of row $i to $high\n"; $t->rowHeight($i, $high); $tw[$i] = $mw->Text(-wrap=>'word', )->pack(qw/ -expand 1 -fill bot +h /); $tw[$i]->insert('end', "$i - $_"); $t->windowConfigure("$i,0", -window => $tw[$i]); # -sticky => 's' +, #put the Text widget in the tablematrix table } $heightwin->destroy; MainLoop; __DATA__ The general budget 1. The Head of the Agency shall provide the Steering Board by 30 June +each year with an overall estimate of the draft general budget for th +e following year, fully respecting the limits set down in the financi +al framework. 2. The Head of the Agency shall propose the draft general budget to th +e Steering Board by 30 September each year. The draft shall include: (a) the appropriations deemed necessary: (i) to cover the Agency's running, staffing and meeting costs; (ii) for procuring external advice, notably operational analysis, esse +ntial for the Agency to discharge its tasks, and for specific researc +h and technology activities for the common benefit of all participati +ng Member States, notably technical case-studies and pre-feasibility +studies; (b) a forecast of the revenue needed to cover expenditure. 3. The Steering Board shall aim to ensure that the appropriations refe +rred to in paragraph 2(a)(ii) shall represent a significant share of +the total appropriations referred to in paragraph 2. These appropriations shall reflect actual needs and shall allow for an + operational role for the Agency. 4. The draft general budget shall be accompanied by a detailed staff e +stablishment plan and detailed justifications. 5. The Steering Board, acting by unanimity, may decide that the draft +general budget shall furthermore cover a particular project or progra +mme where this is clearly for the common benefit of all participating + Member States. 6. The appropriations shall be classified in titles and chapters group +ing expenditure together by type or purpose, subdivided as necessary +into articles. 7. Each title may include a chapter entitled «provisional appropriatio +ns» . These appropriations shall be entered where there is uncertainty, base +d on serious grounds, about the amount of appropriations needed or th +e scope for implementing the appropriations entered.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 06:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found