From jfox at mcmaster.ca  Mon Feb 28 17:25:39 2005
From: jfox at mcmaster.ca (John Fox)
Date: Mon Feb 28 17:25:49 2005
Subject: [Rd] Getting width of Tk text widget via tcltk
In-Reply-To: <x2oee4hexd.fsf@biostat.ku.dk>
Message-ID: <20050228162539.RTUM1899.tomts13-srv.bellnexxia.net@JohnDesktop8300>

Dear Peter,

(as.numeric(tkwinfo("width", .output$ID))
    - 2*as.numeric(tkcget(.output, borderwidth=NULL)) - 2)/
    as.numeric(tkfont.measure(tkcget(.output, font=NULL), "0"))

(for the text widget .output) appears to do the trick (note, subtracting 2
rather than 4).

Thank you,
 John

--------------------------------
John Fox
Department of Sociology
McMaster University
Hamilton, Ontario
Canada L8S 4M4
905-525-9140x23604
http://socserv.mcmaster.ca/jfox 
-------------------------------- 

> -----Original Message-----
> From: r-devel-bounces@stat.math.ethz.ch 
> [mailto:r-devel-bounces@stat.math.ethz.ch] On Behalf Of Peter Dalgaard
> Sent: Monday, February 28, 2005 10:39 AM
> To: Prof Brian Ripley
> Cc: John Fox; r-devel@stat.math.ethz.ch
> Subject: Re: [Rd] Getting width of Tk text widget via tcltk
> 
> Prof Brian Ripley <ripley@stats.ox.ac.uk> writes:
> 
> 
> > > <Tcl> 743
> > >
> > > I could convert pixels to characters, but wonder whether 
> I can get 
> > > the latter directly.
> > 
> > I suspect you may have some difficulty with the latter, even in a 
> > monospace font.  Notice that 486 is not a multiple of 80, 
> and if that 
> > is actually 81, 743 is not a multiple of 6.
> > 
> > I was trying to do this with heights for a listbox a few 
> days ago, and 
> > it seems that the line spacing is actually 1 pixel greater than is 
> > reported. Since you can resize to a non-integer number of lines I 
> > don't believe (and I tried to read the source code) that 
> Tcl/Tk works 
> > internally with characters.
> 
> I think that's actually an issue that involves the window manager too.
> I don't think all WMs know how to deal in integer number of 
> characters. 
> 
> Anyways, the following piece of Tcl seems to do the trick:
> 
> % expr ([winfo width .a] - 2 * [.a cget -borderwidth] - 
> 4)/[font measure [.a cget -font] 0]
> 27
> 
> Converting to R is left as an exercise...
> 
> If I got it right then the point is that at either side of 
> the window you have by 1 pixel, n border pixels, and 1 spacer 
> pixel before the first character.
> 
> -- 
>    O__  ---- Peter Dalgaard             Blegdamsvej 3  
>   c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
>  (*) \(*) -- University of Copenhagen   Denmark      Ph: 
> (+45) 35327918
> ~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: 
> (+45) 35327907
> 
> ______________________________________________
> R-devel@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel


[Rd] Getting width of Tk text widget via tcltk
John Fox