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