Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Get x,y position from text widget + highlight 1

Status
Not open for further replies.

fabien

Technical User
Sep 25, 2001
299
AU
Hi!

Following-up onto my previous post RE a Format builder app can someone with the following:
from a text widget, how can I
1) get the x,y position of when I first click with MB1 the mouse and when I release it?
2) how can I hightlight some text given x y coordinates? For instance when I select a word only for the first line, I would like to highlight the entire column.

Thanks,
 
1/ get the x, y coords inside the text widget:
Code:
  text .t ...
  bind .t <ButtonPress-1> {keep start %x %y}
  bind .t <ButtonRelease-1> {keep end %x %y}
  proc keep {var x y} { set ::$var [.t index @$x,$y] }
2/ highlight from start to end:
Code:
  .t tag config highlight -bg navy -fg white
  proc highlight {start end}   { 
    .t tag remove highlight 1.0 end 
    .t tag add highlight $start $end 
  }

More on text:
HTH

ulis
 
Bonjour Ulis,

Thanks for your reply. However, I am having problems with the code you wrote, could you please explain the part between {} of keep proc.

Also I think what this does is to highlight the selected word(s) from one line only, what I want is to highlight the entire column based on the start and end values.

Thanks for your help.

 
The
Code:
keep
proc is a straightforward use of the index operation of the text widget.
From the Tk Manual:
pathName index index
Returns the position corresponding to index in the form line.char where line is the line number and char is the character number. Index may have any of the forms described under INDICES above.
@x,y
Indicates the character that covers the pixel whose x and y coordinates within the text's window are x and y.

So
Code:
[.t index @$x,$y]
translates the x,y coords in row,col coords and returns the line and char positions of the char under the mouse cursor.

My code save these coords in global variables and let you highlight all the chars from the char where the mouse button was pressed to the char where the mouse button was released.
I understand that its not what you wanted.

If you want to higlight the column of chars between the column of the starting char and the column of the ending char you need to do some work:
Scan all lines of the text widget and highlight the desired range in the line.

For more on the text widget: You can get a french translation here: Forums of interest:
(french)

HTH

ulis
 
Hi ulis,

I tried your code and it does not work, it does not even highlight the first line I select.

I know how to get the number of lines from the input text file. Say we have 5 lines what commands should I use then?

Thanks,

Fabien
 
My mistake it works fine I just did not all your function properly.

Thanks again
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top