diff options
-rw-r--r-- | Xwt.Gtk/Xwt.GtkBackend/LabelBackend.cs | 42 |
1 files changed, 30 insertions, 12 deletions
diff --git a/Xwt.Gtk/Xwt.GtkBackend/LabelBackend.cs b/Xwt.Gtk/Xwt.GtkBackend/LabelBackend.cs index f05d8309..ca01723b 100644 --- a/Xwt.Gtk/Xwt.GtkBackend/LabelBackend.cs +++ b/Xwt.Gtk/Xwt.GtkBackend/LabelBackend.cs @@ -40,6 +40,7 @@ namespace Xwt.GtkBackend Color? bgColor, textColor; int wrapHeight, wrapWidth; List<LabelLink> links; + TextIndexer indexer; public LabelBackend () { @@ -133,19 +134,27 @@ namespace Xwt.GtkBackend LabelLink FindLink (double px, double py) { - var x = px * Pango.Scale.PangoScale; - var y = py * Pango.Scale.PangoScale; + if (links == null) + return null; + + var alloc = Label.Allocation; + + int offsetX, offsetY; + Label.GetLayoutOffsets (out offsetX, out offsetY); - int index, trailing; - if (!Label.Layout.XyToIndex ((int)x, (int)y, out index, out trailing)) + var x = (px - offsetX + alloc.X) * Pango.Scale.PangoScale; + var y = (py - offsetY + alloc.Y) * Pango.Scale.PangoScale; + + int byteIndex, trailing; + if (!Label.Layout.XyToIndex ((int)x, (int)y, out byteIndex, out trailing)) return null; - if (links != null) { - foreach (var li in links) { - if (index >= li.StartIndex && index <= li.EndIndex) - return li; - } - } + int index = indexer.ByteIndexToIndex (byteIndex); + + foreach (var li in links) + if (byteIndex >= li.StartIndex && byteIndex <= li.EndIndex) + return li; + return null; } @@ -189,14 +198,18 @@ namespace Xwt.GtkBackend public virtual string Text { get { return Label.Text; } - set { Label.Text = value; } + set { + links = null; + indexer = null; + Label.Text = value; + } } public void SetFormattedText (FormattedText text) { Label.Text = text.Text; var list = new FastPangoAttrList (); - TextIndexer indexer = new TextIndexer (text.Text); + indexer = new TextIndexer (text.Text); list.AddAttributes (indexer, text.Attributes); gtk_label_set_attributes (Label.Handle, list.Handle); @@ -215,6 +228,11 @@ namespace Xwt.GtkBackend } links.Add (ll); } + + if (links == null || links.Count == 0) { + links = null; + indexer = null; + } } [DllImport (GtkInterop.LIBGTK, CallingConvention=CallingConvention.Cdecl)] |