]> Pileus Git - ~andy/gtk/blob - gtk/gtklinkbutton.c
Bug 659406 - Abstract what triggers a context menu
[~andy/gtk] / gtk / gtklinkbutton.c
1 /* GTK - The GIMP Toolkit
2  * gtklinkbutton.c - an hyperlink-enabled button
3  *
4  * Copyright (C) 2006 Emmanuele Bassi <ebassi@gmail.com>
5  * All rights reserved.
6  *
7  * Based on gnome-href code by:
8  *      James Henstridge <james@daa.com.au>
9  *
10  * This library is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU Library General Public
12  * License as published by the Free Software Foundation; either
13  * version 2 of the License, or (at your option) any later version.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Library General Public License for more details.
19  *
20  * You should have received a copy of the GNU Library General Public
21  * License along with this library; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Cambridge, MA 02139, USA.
23  */
24
25 /**
26  * SECTION:gtklinkbutton
27  * @Title: GtkLinkButton
28  * @Short_description: Create buttons bound to a URL
29  * @See_also: #GtkButton
30  *
31  * A GtkLinkButton is a #GtkButton with a hyperlink, similar to the one
32  * used by web browsers, which triggers an action when clicked. It is useful
33  * to show quick links to resources.
34  *
35  * A link button is created by calling either gtk_link_button_new() or
36  * gtk_link_button_new_with_label(). If using the former, the URI you pass
37  * to the constructor is used as a label for the widget.
38  *
39  * The URI bound to a GtkLinkButton can be set specifically using
40  * gtk_link_button_set_uri(), and retrieved using gtk_link_button_get_uri().
41  *
42  * By default, GtkLinkButton calls gtk_show_uri() when the button is
43  * clicked. This behaviour can be overridden by connecting to the
44  * #GtkLinkButton::activate-link signal and returning %TRUE from the
45  * signal handler.
46  */
47
48 #include "config.h"
49
50 #include "gtklinkbutton.h"
51
52 #include <string.h>
53
54 #include "gtkclipboard.h"
55 #include "gtkdnd.h"
56 #include "gtkimagemenuitem.h"
57 #include "gtklabel.h"
58 #include "gtkmainprivate.h"
59 #include "gtkmarshalers.h"
60 #include "gtkmenu.h"
61 #include "gtkmenuitem.h"
62 #include "gtksizerequest.h"
63 #include "gtkstock.h"
64 #include "gtkshow.h"
65 #include "gtktooltip.h"
66 #include "gtkprivate.h"
67 #include "gtkintl.h"
68
69 #include "a11y/gtklinkbuttonaccessible.h"
70
71 struct _GtkLinkButtonPrivate
72 {
73   gchar *uri;
74
75   gboolean visited;
76
77   GtkWidget *popup_menu;
78 };
79
80 enum
81 {
82   PROP_0,
83   PROP_URI,
84   PROP_VISITED
85 };
86
87 enum
88 {
89   ACTIVATE_LINK,
90
91   LAST_SIGNAL
92 };
93
94 static void     gtk_link_button_finalize     (GObject          *object);
95 static void     gtk_link_button_get_property (GObject          *object,
96                                               guint             prop_id,
97                                               GValue           *value,
98                                               GParamSpec       *pspec);
99 static void     gtk_link_button_set_property (GObject          *object,
100                                               guint             prop_id,
101                                               const GValue     *value,
102                                               GParamSpec       *pspec);
103 static void     gtk_link_button_add          (GtkContainer     *container,
104                                               GtkWidget        *widget);
105 static gboolean gtk_link_button_button_press (GtkWidget        *widget,
106                                               GdkEventButton   *event);
107 static void     gtk_link_button_clicked      (GtkButton        *button);
108 static gboolean gtk_link_button_popup_menu   (GtkWidget        *widget);
109 static void     gtk_link_button_style_updated (GtkWidget        *widget);
110 static void     gtk_link_button_unrealize    (GtkWidget        *widget);
111 static gboolean gtk_link_button_enter_cb     (GtkWidget        *widget,
112                                               GdkEventCrossing *event,
113                                               gpointer          user_data);
114 static gboolean gtk_link_button_leave_cb     (GtkWidget        *widget,
115                                               GdkEventCrossing *event,
116                                               gpointer          user_data);
117 static void gtk_link_button_drag_data_get_cb (GtkWidget        *widget,
118                                               GdkDragContext   *context,
119                                               GtkSelectionData *selection,
120                                               guint             _info,
121                                               guint             _time,
122                                               gpointer          user_data);
123 static gboolean gtk_link_button_query_tooltip_cb (GtkWidget    *widget,
124                                                   gint          x,
125                                                   gint          y,
126                                                   gboolean      keyboard_tip,
127                                                   GtkTooltip   *tooltip,
128                                                   gpointer      data);
129 static gboolean gtk_link_button_activate_link (GtkLinkButton *link_button);
130
131 static const GtkTargetEntry link_drop_types[] = {
132   { "text/uri-list", 0, 0 },
133   { "_NETSCAPE_URL", 0, 0 }
134 };
135
136 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
137 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
138
139 static guint link_signals[LAST_SIGNAL] = { 0, };
140
141 G_DEFINE_TYPE (GtkLinkButton, gtk_link_button, GTK_TYPE_BUTTON)
142
143 static void
144 gtk_link_button_class_init (GtkLinkButtonClass *klass)
145 {
146   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
147   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
148   GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
149   GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
150   
151   gobject_class->set_property = gtk_link_button_set_property;
152   gobject_class->get_property = gtk_link_button_get_property;
153   gobject_class->finalize = gtk_link_button_finalize;
154   
155   widget_class->button_press_event = gtk_link_button_button_press;
156   widget_class->popup_menu = gtk_link_button_popup_menu;
157   widget_class->style_updated = gtk_link_button_style_updated;
158   widget_class->unrealize = gtk_link_button_unrealize;
159   
160   container_class->add = gtk_link_button_add;
161
162   button_class->clicked = gtk_link_button_clicked;
163
164   klass->activate_link = gtk_link_button_activate_link;
165
166   /**
167    * GtkLinkButton:uri
168    * 
169    * The URI bound to this button. 
170    *
171    * Since: 2.10
172    */
173   g_object_class_install_property (gobject_class,
174                                    PROP_URI,
175                                    g_param_spec_string ("uri",
176                                                         P_("URI"),
177                                                         P_("The URI bound to this button"),
178                                                         NULL,
179                                                         G_PARAM_READWRITE));
180   /**
181    * GtkLinkButton:visited
182    * 
183    * The 'visited' state of this button. A visited link is drawn in a
184    * different color.
185    *
186    * Since: 2.14
187    */
188   g_object_class_install_property (gobject_class,
189                                    PROP_VISITED,
190                                    g_param_spec_boolean ("visited",
191                                                          P_("Visited"),
192                                                          P_("Whether this link has been visited."),
193                                                          FALSE,
194                                                          G_PARAM_READWRITE));
195   
196   g_type_class_add_private (gobject_class, sizeof (GtkLinkButtonPrivate));
197
198   /**
199    * GtkLinkButton::activate-link:
200    * @button: the #GtkLinkButton that emitted the signal
201    *
202    * The ::activate-link signal is emitted each time the #GtkLinkButton
203    * has been clicked.
204    *
205    * The default handler will call gtk_show_uri() with the URI stored inside
206    * the #GtkLinkButton:uri property.
207    *
208    * To override the default behavior, you can connect to the ::activate-link
209    * signal and stop the propagation of the signal by returning %TRUE from
210    * your handler.
211    */
212   link_signals[ACTIVATE_LINK] =
213     g_signal_new (I_("activate-link"),
214                   G_TYPE_FROM_CLASS (klass),
215                   G_SIGNAL_RUN_LAST,
216                   G_STRUCT_OFFSET (GtkLinkButtonClass, activate_link),
217                   _gtk_boolean_handled_accumulator, NULL,
218                   _gtk_marshal_BOOLEAN__VOID,
219                   G_TYPE_BOOLEAN, 0);
220
221   gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_LINK_BUTTON_ACCESSIBLE);
222 }
223
224 static void
225 gtk_link_button_init (GtkLinkButton *link_button)
226 {
227   link_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (link_button,
228                                                    GTK_TYPE_LINK_BUTTON,
229                                                    GtkLinkButtonPrivate);
230
231   gtk_button_set_relief (GTK_BUTTON (link_button), GTK_RELIEF_NONE);
232   
233   g_signal_connect (link_button, "enter-notify-event",
234                     G_CALLBACK (gtk_link_button_enter_cb), NULL);
235   g_signal_connect (link_button, "leave-notify-event",
236                     G_CALLBACK (gtk_link_button_leave_cb), NULL);
237   g_signal_connect (link_button, "drag-data-get",
238                     G_CALLBACK (gtk_link_button_drag_data_get_cb), NULL);
239
240   g_object_set (link_button, "has-tooltip", TRUE, NULL);
241   g_signal_connect (link_button, "query-tooltip",
242                     G_CALLBACK (gtk_link_button_query_tooltip_cb), NULL);
243   
244   /* enable drag source */
245   gtk_drag_source_set (GTK_WIDGET (link_button),
246                        GDK_BUTTON1_MASK,
247                        link_drop_types, G_N_ELEMENTS (link_drop_types),
248                        GDK_ACTION_COPY);
249 }
250
251 static void
252 gtk_link_button_finalize (GObject *object)
253 {
254   GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
255   
256   g_free (link_button->priv->uri);
257   
258   G_OBJECT_CLASS (gtk_link_button_parent_class)->finalize (object);
259 }
260
261 static void
262 gtk_link_button_get_property (GObject    *object,
263                               guint       prop_id,
264                               GValue     *value,
265                               GParamSpec *pspec)
266 {
267   GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
268   
269   switch (prop_id)
270     {
271     case PROP_URI:
272       g_value_set_string (value, link_button->priv->uri);
273       break;
274     case PROP_VISITED:
275       g_value_set_boolean (value, link_button->priv->visited);
276       break;
277     default:
278       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
279       break;
280     }
281 }
282
283 static void
284 gtk_link_button_set_property (GObject      *object,
285                               guint         prop_id,
286                               const GValue *value,
287                               GParamSpec   *pspec)
288 {
289   GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
290   
291   switch (prop_id)
292     {
293     case PROP_URI:
294       gtk_link_button_set_uri (link_button, g_value_get_string (value));
295       break;
296     case PROP_VISITED:
297       gtk_link_button_set_visited (link_button, g_value_get_boolean (value));
298       break;
299     default:
300       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
301       break;
302     }
303 }
304
305 static void
306 set_link_color (GtkLinkButton *link_button)
307 {
308   GdkColor *link_color = NULL;
309   GtkWidget *label;
310
311   label = gtk_bin_get_child (GTK_BIN (link_button));
312   if (!GTK_IS_LABEL (label))
313     return;
314
315   if (link_button->priv->visited)
316     {
317       gtk_widget_style_get (GTK_WIDGET (link_button),
318                             "visited-link-color", &link_color, NULL);
319       if (!link_color)
320         link_color = (GdkColor *) &default_visited_link_color;
321     }
322   else
323     {
324       gtk_widget_style_get (GTK_WIDGET (link_button),
325                             "link-color", &link_color, NULL);
326       if (!link_color)
327         link_color = (GdkColor *) &default_link_color;
328     }
329
330   gtk_widget_modify_fg (label, GTK_STATE_NORMAL, link_color);
331   gtk_widget_modify_fg (label, GTK_STATE_ACTIVE, link_color);
332   gtk_widget_modify_fg (label, GTK_STATE_PRELIGHT, link_color);
333   gtk_widget_modify_fg (label, GTK_STATE_SELECTED, link_color);
334
335   if (link_color != &default_link_color &&
336       link_color != &default_visited_link_color)
337     gdk_color_free (link_color);
338 }
339
340 static void
341 set_link_underline (GtkLinkButton *link_button)
342 {
343   GtkWidget *label;
344   
345   label = gtk_bin_get_child (GTK_BIN (link_button));
346   if (GTK_IS_LABEL (label))
347     {
348       PangoAttrList *attributes;
349       PangoAttribute *uline;
350
351       uline = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
352       uline->start_index = 0;
353       uline->end_index = G_MAXUINT;
354       attributes = pango_attr_list_new ();
355       pango_attr_list_insert (attributes, uline); 
356       gtk_label_set_attributes (GTK_LABEL (label), attributes);
357       pango_attr_list_unref (attributes);
358     }
359 }
360
361 static void
362 gtk_link_button_add (GtkContainer *container,
363                      GtkWidget    *widget)
364 {
365   GTK_CONTAINER_CLASS (gtk_link_button_parent_class)->add (container, widget);
366
367   set_link_color (GTK_LINK_BUTTON (container));
368   set_link_underline (GTK_LINK_BUTTON (container));
369 }
370
371 static void
372 gtk_link_button_style_updated (GtkWidget *widget)
373 {
374   GTK_WIDGET_CLASS (gtk_link_button_parent_class)->style_updated (widget);
375
376   set_link_color (GTK_LINK_BUTTON (widget));
377 }
378
379 static void
380 set_hand_cursor (GtkWidget *widget,
381                  gboolean   show_hand)
382 {
383   GdkDisplay *display;
384   GdkCursor *cursor;
385
386   display = gtk_widget_get_display (widget);
387
388   cursor = NULL;
389   if (show_hand)
390     cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
391
392   gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
393   gdk_display_flush (display);
394
395   if (cursor)
396     g_object_unref (cursor);
397 }
398
399 static void
400 gtk_link_button_unrealize (GtkWidget *widget)
401 {
402   set_hand_cursor (widget, FALSE);
403
404   GTK_WIDGET_CLASS (gtk_link_button_parent_class)->unrealize (widget);
405 }
406
407 static void
408 popup_menu_detach (GtkWidget *attach_widget,
409                    GtkMenu   *menu)
410 {
411   GtkLinkButton *link_button = GTK_LINK_BUTTON (attach_widget);
412
413   link_button->priv->popup_menu = NULL;
414 }
415
416 static void
417 popup_position_func (GtkMenu  *menu,
418                      gint     *x,
419                      gint     *y,
420                      gboolean *push_in,
421                      gpointer  user_data)
422 {
423   GtkLinkButton *link_button = GTK_LINK_BUTTON (user_data);
424   GtkLinkButtonPrivate *priv = link_button->priv;
425   GtkAllocation allocation;
426   GtkWidget *widget = GTK_WIDGET (link_button);
427   GdkScreen *screen = gtk_widget_get_screen (widget);
428   GtkRequisition req;
429   gint monitor_num;
430   GdkRectangle monitor;
431   
432   g_return_if_fail (gtk_widget_get_realized (widget));
433
434   gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
435
436   gtk_widget_get_preferred_size (priv->popup_menu, &req, NULL);
437
438   gtk_widget_get_allocation (widget, &allocation);
439   *x += allocation.width / 2;
440   *y += allocation.height;
441
442   monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
443   gtk_menu_set_monitor (menu, monitor_num);
444   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
445
446   *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
447   *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
448
449   *push_in = FALSE;
450 }
451
452 static void
453 copy_activate_cb (GtkWidget     *widget,
454                   GtkLinkButton *link_button)
455 {
456   GtkLinkButtonPrivate *priv = link_button->priv;
457   
458   gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (link_button),
459                                                     GDK_SELECTION_CLIPBOARD),
460                           priv->uri, -1);
461 }
462
463 static void
464 gtk_link_button_do_popup (GtkLinkButton  *link_button,
465                           GdkEventButton *event)
466 {
467   GtkLinkButtonPrivate *priv = link_button->priv;
468   gint button;
469   guint time;
470   
471   if (event)
472     {
473       button = event->button;
474       time = event->time;
475     }
476   else
477     {
478       button = 0;
479       time = gtk_get_current_event_time ();
480     }
481
482   if (gtk_widget_get_realized (GTK_WIDGET (link_button)))
483     {
484       GtkWidget *menu_item;
485       
486       if (priv->popup_menu)
487         gtk_widget_destroy (priv->popup_menu);
488
489       priv->popup_menu = gtk_menu_new ();
490       
491       gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
492                                  GTK_WIDGET (link_button),
493                                  popup_menu_detach);
494
495       menu_item = gtk_image_menu_item_new_with_mnemonic (_("Copy URL"));
496       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item),
497                                      gtk_image_new_from_stock (GTK_STOCK_COPY,
498                                                                GTK_ICON_SIZE_MENU));
499       g_signal_connect (menu_item, "activate",
500                         G_CALLBACK (copy_activate_cb), link_button);
501       gtk_widget_show (menu_item);
502       gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menu_item);
503       
504       if (button)
505         gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
506                         NULL, NULL,
507                         button, time);
508       else
509         {
510           gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
511                           popup_position_func, link_button,
512                           button, time);
513           gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
514         }
515     }
516 }
517
518 static gboolean
519 gtk_link_button_button_press (GtkWidget      *widget,
520                               GdkEventButton *event)
521 {
522   if (!gtk_widget_has_focus (widget))
523     gtk_widget_grab_focus (widget);
524
525   if (gdk_event_triggers_context_menu ((GdkEvent *) event))
526     {
527       gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), event);
528
529       return TRUE;
530     }
531
532   if (GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event)
533     return GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event (widget, event);
534   
535   return FALSE;
536 }
537
538 static gboolean
539 gtk_link_button_activate_link (GtkLinkButton *link_button)
540 {
541   GdkScreen *screen;
542   GError *error;
543
544   if (gtk_widget_has_screen (GTK_WIDGET (link_button)))
545     screen = gtk_widget_get_screen (GTK_WIDGET (link_button));
546   else
547     screen = NULL;
548
549   error = NULL;
550   gtk_show_uri (screen, link_button->priv->uri, GDK_CURRENT_TIME, &error);
551   if (error)
552     {
553       g_warning ("Unable to show '%s': %s",
554                  link_button->priv->uri,
555                  error->message);
556       g_error_free (error);
557
558       return FALSE;
559     }
560
561   gtk_link_button_set_visited (link_button, TRUE);
562
563   return TRUE;
564 }
565
566 static void
567 gtk_link_button_clicked (GtkButton *button)
568 {
569   gboolean retval = FALSE;
570
571   g_signal_emit (button, link_signals[ACTIVATE_LINK], 0, &retval);
572 }
573
574 static gboolean
575 gtk_link_button_popup_menu (GtkWidget *widget)
576 {
577   gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), NULL);
578
579   return TRUE; 
580 }
581
582 static gboolean
583 gtk_link_button_enter_cb (GtkWidget        *widget,
584                           GdkEventCrossing *crossing,
585                           gpointer          user_data)
586 {
587   set_hand_cursor (widget, TRUE);
588   
589   return FALSE;
590 }
591
592 static gboolean
593 gtk_link_button_leave_cb (GtkWidget        *widget,
594                           GdkEventCrossing *crossing,
595                           gpointer          user_data)
596 {
597   set_hand_cursor (widget, FALSE);
598   
599   return FALSE;
600 }
601
602 static void
603 gtk_link_button_drag_data_get_cb (GtkWidget        *widget,
604                                   GdkDragContext   *context,
605                                   GtkSelectionData *selection,
606                                   guint             _info,
607                                   guint             _time,
608                                   gpointer          user_data)
609 {
610   GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
611   gchar *uri;
612   
613   uri = g_strdup_printf ("%s\r\n", link_button->priv->uri);
614   gtk_selection_data_set (selection,
615                           gtk_selection_data_get_target (selection),
616                           8,
617                           (guchar *) uri,
618                           strlen (uri));
619   
620   g_free (uri);
621 }
622
623 /**
624  * gtk_link_button_new:
625  * @uri: a valid URI
626  *
627  * Creates a new #GtkLinkButton with the URI as its text.
628  *
629  * Return value: a new link button widget.
630  *
631  * Since: 2.10
632  */
633 GtkWidget *
634 gtk_link_button_new (const gchar *uri)
635 {
636   gchar *utf8_uri = NULL;
637   GtkWidget *retval;
638   
639   g_return_val_if_fail (uri != NULL, NULL);
640   
641   if (g_utf8_validate (uri, -1, NULL))
642     {
643       utf8_uri = g_strdup (uri);
644     }
645   else
646     {
647       GError *conv_err = NULL;
648     
649       utf8_uri = g_locale_to_utf8 (uri, -1, NULL, NULL, &conv_err);
650       if (conv_err)
651         {
652           g_warning ("Attempting to convert URI `%s' to UTF-8, but failed "
653                      "with error: %s\n",
654                      uri,
655                      conv_err->message);
656           g_error_free (conv_err);
657         
658           utf8_uri = g_strdup (_("Invalid URI"));
659         }
660     }
661   
662   retval = g_object_new (GTK_TYPE_LINK_BUTTON,
663                          "label", utf8_uri,
664                          "uri", uri,
665                          NULL);
666   
667   g_free (utf8_uri);
668   
669   return retval;
670 }
671
672 /**
673  * gtk_link_button_new_with_label:
674  * @uri: a valid URI
675  * @label: (allow-none): the text of the button
676  *
677  * Creates a new #GtkLinkButton containing a label.
678  *
679  * Return value: (transfer none): a new link button widget.
680  *
681  * Since: 2.10
682  */
683 GtkWidget *
684 gtk_link_button_new_with_label (const gchar *uri,
685                                 const gchar *label)
686 {
687   GtkWidget *retval;
688   
689   g_return_val_if_fail (uri != NULL, NULL);
690   
691   if (!label)
692     return gtk_link_button_new (uri);
693
694   retval = g_object_new (GTK_TYPE_LINK_BUTTON,
695                          "label", label,
696                          "uri", uri,
697                          NULL);
698
699   return retval;
700 }
701
702 static gboolean 
703 gtk_link_button_query_tooltip_cb (GtkWidget    *widget,
704                                   gint          x,
705                                   gint          y,
706                                   gboolean      keyboard_tip,
707                                   GtkTooltip   *tooltip,
708                                   gpointer      data)
709 {
710   GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
711   const gchar *label, *uri;
712
713   label = gtk_button_get_label (GTK_BUTTON (link_button));
714   uri = link_button->priv->uri;
715
716   if (!gtk_widget_get_tooltip_text (widget)
717     && !gtk_widget_get_tooltip_markup (widget)
718     && label && *label != '\0' && uri && strcmp (label, uri) != 0)
719     {
720       gtk_tooltip_set_text (tooltip, uri);
721       return TRUE;
722     }
723
724   return FALSE;
725 }
726
727
728 /**
729  * gtk_link_button_set_uri:
730  * @link_button: a #GtkLinkButton
731  * @uri: a valid URI
732  *
733  * Sets @uri as the URI where the #GtkLinkButton points. As a side-effect
734  * this unsets the 'visited' state of the button.
735  *
736  * Since: 2.10
737  */
738 void
739 gtk_link_button_set_uri (GtkLinkButton *link_button,
740                          const gchar   *uri)
741 {
742   GtkLinkButtonPrivate *priv;
743
744   g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
745   g_return_if_fail (uri != NULL);
746
747   priv = link_button->priv;
748
749   g_free (priv->uri);
750   priv->uri = g_strdup (uri);
751
752   g_object_notify (G_OBJECT (link_button), "uri");
753
754   gtk_link_button_set_visited (link_button, FALSE);
755 }
756
757 /**
758  * gtk_link_button_get_uri:
759  * @link_button: a #GtkLinkButton
760  *
761  * Retrieves the URI set using gtk_link_button_set_uri().
762  *
763  * Return value: a valid URI.  The returned string is owned by the link button
764  *   and should not be modified or freed.
765  *
766  * Since: 2.10
767  */
768 const gchar *
769 gtk_link_button_get_uri (GtkLinkButton *link_button)
770 {
771   g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), NULL);
772   
773   return link_button->priv->uri;
774 }
775
776 /**
777  * gtk_link_button_set_visited:
778  * @link_button: a #GtkLinkButton
779  * @visited: the new 'visited' state
780  *
781  * Sets the 'visited' state of the URI where the #GtkLinkButton
782  * points.  See gtk_link_button_get_visited() for more details.
783  *
784  * Since: 2.14
785  */
786 void
787 gtk_link_button_set_visited (GtkLinkButton *link_button,
788                              gboolean       visited)
789 {
790   g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
791
792   visited = visited != FALSE;
793
794   if (link_button->priv->visited != visited)
795     {
796       link_button->priv->visited = visited;
797
798       set_link_color (link_button);
799
800       g_object_notify (G_OBJECT (link_button), "visited");
801     }
802 }
803
804 /**
805  * gtk_link_button_get_visited:
806  * @link_button: a #GtkLinkButton
807  *
808  * Retrieves the 'visited' state of the URI where the #GtkLinkButton
809  * points. The button becomes visited when it is clicked. If the URI
810  * is changed on the button, the 'visited' state is unset again.
811  *
812  * The state may also be changed using gtk_link_button_set_visited().
813  *
814  * Return value: %TRUE if the link has been visited, %FALSE otherwise
815  *
816  * Since: 2.14
817  */
818 gboolean
819 gtk_link_button_get_visited (GtkLinkButton *link_button)
820 {
821   g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), FALSE);
822   
823   return link_button->priv->visited;
824 }