[prev in list] [next in list] [prev in thread] [next in thread] 

List:       gtk
Subject:    Re: How to bring up tooltip text for certain words in a GTK textview?
From:       Ken Resander <kresander () yahoo ! com>
Date:       2010-05-20 14:19:54
Message-ID: 975174.61652.qm () web51003 ! mail ! re2 ! yahoo ! com
[Download RAW message or body]

[Attachment #2 (multipart/alternative)]


With the callback below the tooltip removes itself when the mouse leaves a word and \
pops up when I move to a different word (what I wanted) . It didn't with the previous \
bare-bone version. Any idea why?

However, the callback is still called when the mouse moves. In this version
tracing also goes to the window title bar. 

static gboolean querytooltipcb (GtkWidget * w,
                                                               gint x,   gint y,
                                                               gboolean       \
                keyboard_mode,
                                                               GtkTooltip *tooltip,
                                                               gpointer data )
     {
   printf ( "querytooltip(%d): x=%d y=%d kbm=%d   tip=%d\n" ,
                     counter , x , y , keyboard_mode ? 1 : 0, (int)tooltip ) ;
     char text [ 2000 ] ;
     int x1 ;
     int y1 ;
     GtkTextIter iter ;
     gtk_text_view_window_to_buffer_coords((GtkTextView *)w, GTK_TEXT_WINDOW_WIDGET,
                                                                                 x , \
                y , &x1 , &y1 );
     gtk_text_view_get_iter_at_location ( (GtkTextView *)w , &iter , x1 , y1 ) ;
     GtkTextIter startiter = iter ;
     GtkTextIter enditer = iter ;
     if ( gtk_text_iter_starts_word ( &iter ) )
           {
           gtk_text_iter_forward_word_end ( &enditer ) ;
           }
     else if ( gtk_text_iter_inside_word ( &iter ) )
           {
           gtk_text_iter_forward_word_end ( &enditer ) ;
           gtk_text_iter_backward_word_start ( &startiter ) ;
           }
     else if ( gtk_text_iter_ends_word ( &iter ) )
           {
           gtk_text_iter_backward_word_start ( &startiter ) ;
           }
     else
           {
           sprintf ( text , "Not in word, Hi #%d (%d,%d)\n" ,
                               counter , x , y ) ;
           gtk_window_set_title (GTK_WINDOW (window), text );
           counter++;
           return false ;
           }
     GtkTextBuffer * buf = (GtkTextBuffer *)data ;
     TCHAR * word = gtk_text_buffer_get_text ( buf , &startiter , &enditer ,
                                                                                      \
false ) ;  gtk_tooltip_set_text ( tooltip, word );
     sprintf ( text , "Word=%s Hello #%d (%d,%d)\n" ,
                         word , counter , x , y ) ;
     gtk_window_set_title (GTK_WINDOW (window), text );
   counter++;
     return TRUE ;
     }

Ken


--- On Thu, 20/5/10, Tadej Borovšak <tadeboro@gmail.com> wrote:

From: Tadej Borovšak <tadeboro@gmail.com>
Subject: Re: How to bring up tooltip text for certain words in a GTK textview?
To: "richard boaz" <ivor.boaz@gmail.com>
Cc: "Ken Resander" <kresander@yahoo.com>, gtk-list@gnome.org
Date: Thursday, 20 May, 2010, 2:23 AM

Hi.

General flow is correct, but GTK+ does offer some functionality that
may come handy in your case. (I'm commenting in a rather strange
succession because Richard top-posted his reply).

> unless gtk is now providing direct access to tooltip style pop-ups, you must create \
> it yourself

GTK+ does offer access to tooltips in various ways: you can simply set
text or markup; you can insert custom content into tooltip; or you can
create your own window and present it as tooltip.

> meaning that you must create and manage your own borderless top-level window

This is not needed anymore.

> whose location must be explicitly specified to be placed onto the screen
> calculation of the window location coordinates must access the root-window where to \
> the tooltip is to be located on top of, and since you don't have access to the size \
> of the WM's borders on the root window, a little "guessing" as to the WM's size \
> must be taken into account

If you connect to GtkWidget::query-tooltip signal, coordinates are
already provided, so I think most of this stuff is not needed now.

> > A textview shows help text with many 'technical' words that users may not know or \
> > remember. I would like a tooltip text with a short explanation to pop up when a \
> > user hovers the cursor over a technical phrase. There is a lookup table from \
> > technical phrases to explanations. I am thinking about using the mouse move event \
> > to get x,y then getting the technical phrase from x,y, then looking up the \
> > explanation and outputting the tooltip text.

Don't use movement events to monitor your position. Simply set text
view's "has-tooltip" property to "TRUE" and connect handler to
"query-tooltip" signal. Use coordinates provided by callback to find
the word that cursor hovers over and then do the lookup. I think
things should be relatively simple.

Tadej


--
Tadej Borovšak
tadeboro.blogspot.com
tadeboro@gmail.com
tadej.borovsak@gmail.com


[Attachment #5 (text/html)]

<table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="font: \
inherit;">With the callback below the tooltip removes itself when the mouse leaves a \
word and pops up when I move to a different word (what I wanted) . It didn't with the \
previous bare-bone version.<br>Any idea why?<br><br>However, the callback is still \
called when the mouse moves. In this version<br>tracing also goes to the window title \
bar. <br><br>static gboolean querytooltipcb (GtkWidget * \
w,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n \
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
gint x,&nbsp; gint y,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& \
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
gboolean&nbsp;&nbsp;&nbsp;  \
keyboard_mode,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n \
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
GtkTooltip *tooltip,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&n \
bsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
gpointer data )<br>&nbsp;&nbsp; {<br>&nbsp; printf ( "querytooltip(%d): x=%d y=%d \
kbm=%d&nbsp; tip=%d\n" \
,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; counter , x , y , \
keyboard_mode ? 1 : 0, (int)tooltip ) ;<br>&nbsp;&nbsp; char text [ 2000 ] \
;<br>&nbsp;&nbsp; int x1 ;<br>&nbsp;&nbsp; int y1 ;<br>&nbsp;&nbsp; GtkTextIter iter \
;<br>&nbsp;&nbsp; gtk_text_view_window_to_buffer_coords((GtkTextView *)w,  \
GTK_TEXT_WINDOW_WIDGET,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp \
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;& \
nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
x , y , &amp;x1 , &amp;y1 );<br>&nbsp;&nbsp; gtk_text_view_get_iter_at_location ( \
(GtkTextView *)w , &amp;iter , x1 , y1 ) ;<br>&nbsp;&nbsp; GtkTextIter startiter = \
iter ;<br>&nbsp;&nbsp; GtkTextIter enditer = iter ;<br>&nbsp;&nbsp; if ( \
gtk_text_iter_starts_word ( &amp;iter ) )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gtk_text_iter_forward_word_end ( &amp;enditer ) \
;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp; else if ( \
gtk_text_iter_inside_word ( &amp;iter ) )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gtk_text_iter_forward_word_end ( &amp;enditer ) \
;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gtk_text_iter_backward_word_start (  \
&amp;startiter ) ;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp; else if ( \
gtk_text_iter_ends_word ( &amp;iter ) )<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
{<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gtk_text_iter_backward_word_start ( \
&amp;startiter ) ;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp; \
else<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; sprintf ( \
text , "Not in word, Hi #%d (%d,%d)\n" \
,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
counter , x , y ) ;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; gtk_window_set_title \
(GTK_WINDOW (window), text );<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
counter++;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return false \
;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<br>&nbsp;&nbsp; GtkTextBuffer * buf = \
(GtkTextBuffer *)data ;<br>&nbsp;&nbsp; TCHAR * word = gtk_text_buffer_get_text ( buf \
, &amp;startiter , &amp;enditer  \
,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nb \
sp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp \
;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; \
false ) ;<br>&nbsp;&nbsp; gtk_tooltip_set_text ( tooltip, word );<br>&nbsp;&nbsp; \
sprintf ( text , "Word=%s Hello #%d (%d,%d)\n" \
,<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; word , \
counter , x , y ) ;<br>&nbsp;&nbsp; gtk_window_set_title (GTK_WINDOW (window), text \
);<br>&nbsp; counter++;<br>&nbsp;&nbsp; return TRUE ;<br>&nbsp;&nbsp; \
}<br><br>Ken<br><br><br>--- On <b>Thu, 20/5/10, Tadej Borovšak \
<i>&lt;tadeboro@gmail.com&gt;</i></b> wrote:<br><blockquote style="border-left: 2px \
solid rgb(16, 16, 255); margin-left: 5px; padding-left: 5px;"><br>From: Tadej \
Borovšak &lt;tadeboro@gmail.com&gt;<br>Subject: Re: How to bring up tooltip text for \
certain words in  a GTK textview?<br>To: "richard boaz" \
&lt;ivor.boaz@gmail.com&gt;<br>Cc: "Ken Resander" &lt;kresander@yahoo.com&gt;, \
gtk-list@gnome.org<br>Date: Thursday, 20 May, 2010, 2:23 AM<br><br><div \
class="plainMail">Hi.<br><br>General flow is correct, but GTK+ does offer some \
functionality that<br>may come handy in your case. (I'm commenting in a rather \
strange<br>succession because Richard top-posted his reply).<br><br>&gt; unless gtk \
is now providing direct access to tooltip style pop-ups, you must create it \
yourself<br><br>GTK+ does offer access to tooltips in various ways: you can simply \
set<br>text or markup; you can insert custom content into tooltip; or you \
can<br>create your own window and present it as tooltip.<br><br>&gt; meaning that you \
must create and manage your own borderless top-level window<br><br>This is not needed \
anymore.<br><br>&gt; whose location must be explicitly specified to be placed onto \
the screen<br>&gt; calculation of the window  location coordinates must access the \
root-window where to the tooltip is to be located on top of, and<br>&gt; since you \
don't have access to the size of the WM's borders on the root window, a little \
"guessing" as to the WM's size must be taken into account<br><br>If you connect to \
GtkWidget::query-tooltip signal, coordinates are<br>already provided, so I think most \
of this stuff is not needed now.<br><br>&gt;&gt; A textview shows help text with many \
'technical' words that users may not know or remember. I would like a tooltip text \
with a short explanation to pop up when a user hovers the cursor over a technical \
phrase. There is a lookup table from technical phrases to explanations. I am thinking \
about using the mouse move event to get x,y then getting the technical phrase from \
x,y, then looking up the explanation and outputting the tooltip text.<br><br>Don't \
use movement events to monitor your position. Simply set text<br>view's "has-tooltip" \
property  to "TRUE" and connect handler to<br>"query-tooltip" signal. Use coordinates \
provided by callback to find<br>the word that cursor hovers over and then do the \
lookup. I think<br>things should be relatively \
simple.<br><br>Tadej<br><br><br>--<br>Tadej Borovšak<br>tadeboro.blogspot.com<br><a \
ymailto="mailto:tadeboro@gmail.com" \
href="/mc/compose?to=tadeboro@gmail.com">tadeboro@gmail.com</a><br><a \
ymailto="mailto:tadej.borovsak@gmail.com" \
href="/mc/compose?to=tadej.borovsak@gmail.com">tadej.borovsak@gmail.com</a><br></div></blockquote></td></tr></table><br>




_______________________________________________
gtk-list mailing list
gtk-list@gnome.org
http://mail.gnome.org/mailman/listinfo/gtk-list


[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic