After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 111031 - Unable to determine what character is at a point
Unable to determine what character is at a point
Status: RESOLVED FIXED
Product: gtk+
Classification: Platform
Component: Widget: GtkTextView
2.2.x
Other Solaris
: High normal
: ---
Assigned To: gtk-bugs
gtk-bugs
AP2, API
Depends on:
Blocks:
 
 
Reported: 2003-04-17 16:33 UTC by padraig.obriain
Modified: 2011-02-04 16:17 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Test program (85.63 KB, text/plain)
2003-04-17 16:34 UTC, padraig.obriain
  Details
Test output (6.85 KB, text/plain)
2003-04-17 16:36 UTC, padraig.obriain
  Details
a patch (5.63 KB, patch)
2004-12-04 07:47 UTC, Matthias Clasen
none Details | Review
test output with the patch (9.40 KB, text/plain)
2004-12-06 06:49 UTC, Matthias Clasen
  Details
updated test program (86.45 KB, text/plain)
2004-12-07 15:52 UTC, Matthias Clasen
  Details
another update (86.46 KB, text/plain)
2004-12-08 06:22 UTC, Matthias Clasen
  Details
my test program (85.88 KB, text/plain)
2004-12-08 11:06 UTC, padraig.obriain
  Details
my test output (202.18 KB, text/plain)
2004-12-08 11:08 UTC, padraig.obriain
  Details

Description padraig.obriain 2003-04-17 16:33:34 UTC
The attached test program shows problem determin ing what character is at a
point.

For example, I get the rectangle for the character at offset 1 and check
what character is at each point. As can be seen from attached output the
last few points which should be in the character at offset 1 are reported
as being at offset 2.
Comment 1 padraig.obriain 2003-04-17 16:34:27 UTC
Created attachment 15810 [details]
Test program
Comment 2 padraig.obriain 2003-04-17 16:36:02 UTC
Created attachment 15811 [details]
Test output
Comment 3 Owen Taylor 2003-04-17 18:42:37 UTC
See extensive discussion in bug 102592. What needs to
be understand here is that 

 gtk_text_view_get_iter_at_location()

is working 100% correctly (finding the cursor position
nearest to the location), it just isn't doing what
is desired (find the character at the location).

Note that the corresponding Pango API, by returning
the character, along with the "trailing" flag allows 
for both. And also note the connection to the problem
of needing to keep track of "trailing for the previous
character" vs. "leading for the next character" for
the cursor mark.
Comment 4 Owen Taylor 2003-06-05 13:45:15 UTC
[Could be moved back to 2.4 API freeze if a proposal/patch
 appeared]
Comment 5 padraig.obriain 2003-06-09 10:53:41 UTC
The reference to bug 102592 should be 102952.
Comment 6 padraig.obriain 2003-06-09 14:13:39 UTC
I have looked at this to try and figure out what is going on and it
seems that pango_layout_xy_to_index returns a trailing value of 1 for
the second half of a character extents.

Should gtk_text_layout_get_iter_at_pixel() ignore the trailing value?
Comment 7 Owen Taylor 2003-06-09 14:35:43 UTC
If you want to determine what *character* was clicked on, you 
ignore the trailing value.

On the other hand, gtk_text_layout_get_iter_at_pixel()
is about finding the space between characters (an iter
is conceptually between characters) - like for positioning
the insertion cursor. So it *does* pay attention to trailing.
Comment 8 padraig.obriain 2003-06-09 16:08:23 UTC
Thanks. I think I understand what is happening now.

Proposal:

gint gtk_text_view_get_offset_at_location (GtkTextView *view,
                                           gint        x,
                                           gint        y);

Also

gint gtk_text_layout_get_offset_at_pixel (GtkTextLayout *layout,
                                          gint          x,
                                          gint          y);

If these functions are acceptable I can put together a patch.
Comment 9 Havoc Pennington 2003-06-09 16:15:01 UTC
It seems totally confusing to me that if you get an iter it's an 
intercharacter position and if you get an offset it's the character 
itself. gtk_text_iter_get_offset() does exist after all.
If you have get_iter_at and get_offset_at people will think the 
difference is just how the position is represented.

Maybe have a function with the "trailing" arg or have 
get_character_at_location() or something like that.
Comment 10 Owen Taylor 2003-06-09 16:22:17 UTC
Note that we're going to need a variant with the trailing 
argument to fix bug 81412.
Comment 11 padraig.obriain 2003-06-09 16:28:15 UTC
How about

void gtk_text_view_get_offset_at_location (GtkTextView *view,
                                           gint        x,
                                           gint        y,
                                           gint        *offset,
                                           gint        *trailing);
Comment 12 Havoc Pennington 2003-06-09 17:28:37 UTC
I prefer if it returns an iterator, since the rest of the API 
uses iterators.

As a name, how about
gtk_text_view_get_iter_at_pixel() or get_iter_at_position()
and then deprecate the get_iter_at_location() we have currently.

is "trailing" supposed to be a boolean or can it have a value other 
than 0/1?
Comment 13 padraig.obriain 2003-06-10 07:34:21 UTC
trailing is an integer judging by the comment in
pango_layout_line_x_to_index.
Comment 14 Calum Benson 2003-08-07 16:17:31 UTC
Apologies for spam... marking as GNOMEVER2.3 so it appears on the official GNOME
bug list :)
Comment 15 bill.haneman 2004-01-02 14:08:19 UTC
based on Owen's comments on 06/05, I am provisionally marking this 2.4
API Freeze.
Unfortunately this bug seems to have gotten lost for awhile, but we
(ATK team) need the functionality in order to reliably satisfy our own
atk_text_get_offset_at_point API, if I understand Padraig correctly.
Comment 16 Calum Benson 2004-10-21 16:51:56 UTC
Apologies for spam-- ensuring Sun a11y team are cc'ed on all current a11y bugs.
 Filter on "SUN A11Y SPAM" to ignore.
Comment 17 Matthias Clasen 2004-12-01 02:59:07 UTC
So it seems what is needed here is a patch which adds

void gtk_text_layout_get_iter_at_pixel (GtkTextLayout *layout,
                                        GtkTextIter   *iter,
                                        gint          *trailing,
                                        gint           x,
                                        gint           y);
void gtk_text_view_get_iter_at_pixel   (GtkTextView   *view,
                                        GtkTextIter   *iter,
                                        gint          *trailing,
                                        gint           x,
                                        gint           y);

and deprecates gtk_text_layout_get_iter_at_location(),
gtk_text_view_get_iter_at_location()
Comment 18 Matthias Clasen 2004-12-04 07:47:38 UTC
Created attachment 34478 [details] [review]
a patch

Does this patch work for you ? Note that I named the functions
at_position(), since gtk_text_layout_get_iter_at_pixel() was already taken
Comment 19 Matthias Clasen 2004-12-06 06:49:15 UTC
Hmm, I thought I had grokked the theory behind trailing, but what I get when I
replace the call to get_iter_at_location() by my get_iter_at_position() leaves
me confused.
Comment 20 Matthias Clasen 2004-12-06 06:49:57 UTC
Created attachment 34532 [details]
test output with the patch
Comment 21 padraig.obriain 2004-12-07 13:44:26 UTC
I get output very similar to yours. Results seem to be correct until we come to
the Hebrew characters at the end of the 6th line. There the offset seems to be
different for the last pixel of the character. Things seem to go seriously wrong
with the Hebrew punctuation on the next line.
Comment 22 Matthias Clasen 2004-12-07 15:51:42 UTC
Here is your example extended to track the character under the pointer (click
and drag). It seems to show that we're generally doing ok, but as you point out,
the hebrew punctuation seems problematic.
Comment 23 Matthias Clasen 2004-12-07 15:52:24 UTC
Created attachment 34594 [details]
updated test program
Comment 24 Matthias Clasen 2004-12-08 06:22:33 UTC
Created attachment 34619 [details]
another update

This version prints out trailing as well.
Using that, you can find trailing values of up to 5 in the Hebrew punctuation,
and values of up to 7 in the "Thai wrong spelling".
Comment 25 padraig.obriain 2004-12-08 11:05:51 UTC
It looks to me that if the iter is on a right-to-left character the x value
returned by gtk_text_view_get_iter_location() 1 one larger than it ought to be.
Comment 26 padraig.obriain 2004-12-08 11:06:39 UTC
Created attachment 34626 [details]
my test program
Comment 27 padraig.obriain 2004-12-08 11:08:54 UTC
Created attachment 34627 [details]
my test output

Note that the four Hebrew characters bertween 430 and 462. I think they should
be between 429 and 461, judging by the position of the left-to-rgight
characters on either side of them.
Comment 28 Matthias Clasen 2004-12-08 21:23:47 UTC
2004-12-08  Matthias Clasen  <mclasen@redhat.com>

	Fix #111031, reported by Padraig O'Briain:
	
	* gtk/gtktextlayout.h: 
	* gtk/gtktextlayout.c (gtk_text_layout_get_iter_at_position): 
	Add a variant of gtk_text_layout_get_iter_at_pixel() which
	returns the character at the position, not the closest
	cursor position.

	* gtk/gtktextview.h: 
	* gtk/gtktextview.c (gtk_text_view_get_iter_at_position): 
	Add a variant of gtk_text_view_get_iter_at_location() which
	returns the character at the position, not the closest
	cursor position.