1 /* GTK - The GIMP Toolkit
2 * gtklinkbutton.c - an hyperlink-enabled button
4 * Copyright (C) 2006 Emmanuele Bassi <ebassi@gmail.com>
7 * Based on gnome-href code by:
8 * James Henstridge <james@daa.com.au>
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.
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.
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.
26 * SECTION:gtklinkbutton
27 * @Title: GtkLinkButton
28 * @Short_description: Create buttons bound to a URL
29 * @See_also: #GtkButton
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.
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.
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().
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
50 #include "gtklinkbutton.h"
54 #include "gtkclipboard.h"
56 #include "gtkimagemenuitem.h"
59 #include "gtkmarshalers.h"
61 #include "gtkmenuitem.h"
62 #include "gtksizerequest.h"
65 #include "gtktooltip.h"
66 #include "gtkprivate.h"
69 #include "a11y/gtklinkbuttonaccessible.h"
71 struct _GtkLinkButtonPrivate
77 GtkWidget *popup_menu;
94 static void gtk_link_button_finalize (GObject *object);
95 static void gtk_link_button_get_property (GObject *object,
99 static void gtk_link_button_set_property (GObject *object,
103 static void gtk_link_button_add (GtkContainer *container,
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,
114 static gboolean gtk_link_button_leave_cb (GtkWidget *widget,
115 GdkEventCrossing *event,
117 static void gtk_link_button_drag_data_get_cb (GtkWidget *widget,
118 GdkDragContext *context,
119 GtkSelectionData *selection,
123 static gboolean gtk_link_button_query_tooltip_cb (GtkWidget *widget,
126 gboolean keyboard_tip,
129 static gboolean gtk_link_button_activate_link (GtkLinkButton *link_button);
131 static const GtkTargetEntry link_drop_types[] = {
132 { "text/uri-list", 0, 0 },
133 { "_NETSCAPE_URL", 0, 0 }
136 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
137 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
139 static guint link_signals[LAST_SIGNAL] = { 0, };
141 G_DEFINE_TYPE (GtkLinkButton, gtk_link_button, GTK_TYPE_BUTTON)
144 gtk_link_button_class_init (GtkLinkButtonClass *klass)
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);
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;
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;
160 container_class->add = gtk_link_button_add;
162 button_class->clicked = gtk_link_button_clicked;
164 klass->activate_link = gtk_link_button_activate_link;
169 * The URI bound to this button.
173 g_object_class_install_property (gobject_class,
175 g_param_spec_string ("uri",
177 P_("The URI bound to this button"),
181 * GtkLinkButton:visited
183 * The 'visited' state of this button. A visited link is drawn in a
188 g_object_class_install_property (gobject_class,
190 g_param_spec_boolean ("visited",
192 P_("Whether this link has been visited."),
196 g_type_class_add_private (gobject_class, sizeof (GtkLinkButtonPrivate));
199 * GtkLinkButton::activate-link:
200 * @button: the #GtkLinkButton that emitted the signal
202 * The ::activate-link signal is emitted each time the #GtkLinkButton
205 * The default handler will call gtk_show_uri() with the URI stored inside
206 * the #GtkLinkButton:uri property.
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
212 link_signals[ACTIVATE_LINK] =
213 g_signal_new (I_("activate-link"),
214 G_TYPE_FROM_CLASS (klass),
216 G_STRUCT_OFFSET (GtkLinkButtonClass, activate_link),
217 _gtk_boolean_handled_accumulator, NULL,
218 _gtk_marshal_BOOLEAN__VOID,
221 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_LINK_BUTTON_ACCESSIBLE);
225 gtk_link_button_init (GtkLinkButton *link_button)
227 link_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (link_button,
228 GTK_TYPE_LINK_BUTTON,
229 GtkLinkButtonPrivate);
231 gtk_button_set_relief (GTK_BUTTON (link_button), GTK_RELIEF_NONE);
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);
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);
244 /* enable drag source */
245 gtk_drag_source_set (GTK_WIDGET (link_button),
247 link_drop_types, G_N_ELEMENTS (link_drop_types),
252 gtk_link_button_finalize (GObject *object)
254 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
256 g_free (link_button->priv->uri);
258 G_OBJECT_CLASS (gtk_link_button_parent_class)->finalize (object);
262 gtk_link_button_get_property (GObject *object,
267 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
272 g_value_set_string (value, link_button->priv->uri);
275 g_value_set_boolean (value, link_button->priv->visited);
278 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284 gtk_link_button_set_property (GObject *object,
289 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
294 gtk_link_button_set_uri (link_button, g_value_get_string (value));
297 gtk_link_button_set_visited (link_button, g_value_get_boolean (value));
300 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
306 set_link_color (GtkLinkButton *link_button)
308 GdkColor *link_color = NULL;
312 label = gtk_bin_get_child (GTK_BIN (link_button));
313 if (!GTK_IS_LABEL (label))
316 if (link_button->priv->visited)
318 gtk_widget_style_get (GTK_WIDGET (link_button),
319 "visited-link-color", &link_color, NULL);
321 link_color = (GdkColor *) &default_visited_link_color;
325 gtk_widget_style_get (GTK_WIDGET (link_button),
326 "link-color", &link_color, NULL);
328 link_color = (GdkColor *) &default_link_color;
331 rgba.red = link_color->red / 65535.;
332 rgba.green = link_color->green / 65535.;
333 rgba.blue = link_color->blue / 65535.;
335 gtk_widget_override_color (label, GTK_STATE_FLAG_NORMAL, &rgba);
336 gtk_widget_override_color (label, GTK_STATE_FLAG_ACTIVE, &rgba);
337 gtk_widget_override_color (label, GTK_STATE_FLAG_PRELIGHT, &rgba);
338 gtk_widget_override_color (label, GTK_STATE_FLAG_SELECTED, &rgba);
340 if (link_color != &default_link_color &&
341 link_color != &default_visited_link_color)
342 gdk_color_free (link_color);
346 set_link_underline (GtkLinkButton *link_button)
350 label = gtk_bin_get_child (GTK_BIN (link_button));
351 if (GTK_IS_LABEL (label))
353 PangoAttrList *attributes;
354 PangoAttribute *uline;
356 uline = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
357 uline->start_index = 0;
358 uline->end_index = G_MAXUINT;
359 attributes = pango_attr_list_new ();
360 pango_attr_list_insert (attributes, uline);
361 gtk_label_set_attributes (GTK_LABEL (label), attributes);
362 pango_attr_list_unref (attributes);
367 gtk_link_button_add (GtkContainer *container,
370 GTK_CONTAINER_CLASS (gtk_link_button_parent_class)->add (container, widget);
372 set_link_color (GTK_LINK_BUTTON (container));
373 set_link_underline (GTK_LINK_BUTTON (container));
377 gtk_link_button_style_updated (GtkWidget *widget)
379 GTK_WIDGET_CLASS (gtk_link_button_parent_class)->style_updated (widget);
381 set_link_color (GTK_LINK_BUTTON (widget));
385 set_hand_cursor (GtkWidget *widget,
391 display = gtk_widget_get_display (widget);
395 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
397 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
398 gdk_display_flush (display);
401 g_object_unref (cursor);
405 gtk_link_button_unrealize (GtkWidget *widget)
407 set_hand_cursor (widget, FALSE);
409 GTK_WIDGET_CLASS (gtk_link_button_parent_class)->unrealize (widget);
413 popup_menu_detach (GtkWidget *attach_widget,
416 GtkLinkButton *link_button = GTK_LINK_BUTTON (attach_widget);
418 link_button->priv->popup_menu = NULL;
422 popup_position_func (GtkMenu *menu,
428 GtkLinkButton *link_button = GTK_LINK_BUTTON (user_data);
429 GtkLinkButtonPrivate *priv = link_button->priv;
430 GtkAllocation allocation;
431 GtkWidget *widget = GTK_WIDGET (link_button);
432 GdkScreen *screen = gtk_widget_get_screen (widget);
435 GdkRectangle monitor;
437 g_return_if_fail (gtk_widget_get_realized (widget));
439 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
441 gtk_widget_get_preferred_size (priv->popup_menu, &req, NULL);
443 gtk_widget_get_allocation (widget, &allocation);
444 *x += allocation.width / 2;
445 *y += allocation.height;
447 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
448 gtk_menu_set_monitor (menu, monitor_num);
449 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
451 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
452 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
458 copy_activate_cb (GtkWidget *widget,
459 GtkLinkButton *link_button)
461 GtkLinkButtonPrivate *priv = link_button->priv;
463 gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (link_button),
464 GDK_SELECTION_CLIPBOARD),
469 gtk_link_button_do_popup (GtkLinkButton *link_button,
470 GdkEventButton *event)
472 GtkLinkButtonPrivate *priv = link_button->priv;
478 button = event->button;
484 time = gtk_get_current_event_time ();
487 if (gtk_widget_get_realized (GTK_WIDGET (link_button)))
489 GtkWidget *menu_item;
491 if (priv->popup_menu)
492 gtk_widget_destroy (priv->popup_menu);
494 priv->popup_menu = gtk_menu_new ();
496 gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
497 GTK_WIDGET (link_button),
500 menu_item = gtk_image_menu_item_new_with_mnemonic (_("Copy URL"));
501 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item),
502 gtk_image_new_from_stock (GTK_STOCK_COPY,
503 GTK_ICON_SIZE_MENU));
504 g_signal_connect (menu_item, "activate",
505 G_CALLBACK (copy_activate_cb), link_button);
506 gtk_widget_show (menu_item);
507 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menu_item);
510 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
515 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
516 popup_position_func, link_button,
518 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
524 gtk_link_button_button_press (GtkWidget *widget,
525 GdkEventButton *event)
527 if (!gtk_widget_has_focus (widget))
528 gtk_widget_grab_focus (widget);
530 if (gdk_event_triggers_context_menu ((GdkEvent *) event))
532 gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), event);
537 if (GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event)
538 return GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event (widget, event);
544 gtk_link_button_activate_link (GtkLinkButton *link_button)
549 if (gtk_widget_has_screen (GTK_WIDGET (link_button)))
550 screen = gtk_widget_get_screen (GTK_WIDGET (link_button));
555 gtk_show_uri (screen, link_button->priv->uri, GDK_CURRENT_TIME, &error);
558 g_warning ("Unable to show '%s': %s",
559 link_button->priv->uri,
561 g_error_free (error);
566 gtk_link_button_set_visited (link_button, TRUE);
572 gtk_link_button_clicked (GtkButton *button)
574 gboolean retval = FALSE;
576 g_signal_emit (button, link_signals[ACTIVATE_LINK], 0, &retval);
580 gtk_link_button_popup_menu (GtkWidget *widget)
582 gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), NULL);
588 gtk_link_button_enter_cb (GtkWidget *widget,
589 GdkEventCrossing *crossing,
592 set_hand_cursor (widget, TRUE);
598 gtk_link_button_leave_cb (GtkWidget *widget,
599 GdkEventCrossing *crossing,
602 set_hand_cursor (widget, FALSE);
608 gtk_link_button_drag_data_get_cb (GtkWidget *widget,
609 GdkDragContext *context,
610 GtkSelectionData *selection,
615 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
618 uri = g_strdup_printf ("%s\r\n", link_button->priv->uri);
619 gtk_selection_data_set (selection,
620 gtk_selection_data_get_target (selection),
629 * gtk_link_button_new:
632 * Creates a new #GtkLinkButton with the URI as its text.
634 * Return value: a new link button widget.
639 gtk_link_button_new (const gchar *uri)
641 gchar *utf8_uri = NULL;
644 g_return_val_if_fail (uri != NULL, NULL);
646 if (g_utf8_validate (uri, -1, NULL))
648 utf8_uri = g_strdup (uri);
652 GError *conv_err = NULL;
654 utf8_uri = g_locale_to_utf8 (uri, -1, NULL, NULL, &conv_err);
657 g_warning ("Attempting to convert URI `%s' to UTF-8, but failed "
661 g_error_free (conv_err);
663 utf8_uri = g_strdup (_("Invalid URI"));
667 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
678 * gtk_link_button_new_with_label:
680 * @label: (allow-none): the text of the button
682 * Creates a new #GtkLinkButton containing a label.
684 * Return value: (transfer none): a new link button widget.
689 gtk_link_button_new_with_label (const gchar *uri,
694 g_return_val_if_fail (uri != NULL, NULL);
697 return gtk_link_button_new (uri);
699 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
708 gtk_link_button_query_tooltip_cb (GtkWidget *widget,
711 gboolean keyboard_tip,
715 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
716 const gchar *label, *uri;
718 label = gtk_button_get_label (GTK_BUTTON (link_button));
719 uri = link_button->priv->uri;
721 if (!gtk_widget_get_tooltip_text (widget)
722 && !gtk_widget_get_tooltip_markup (widget)
723 && label && *label != '\0' && uri && strcmp (label, uri) != 0)
725 gtk_tooltip_set_text (tooltip, uri);
734 * gtk_link_button_set_uri:
735 * @link_button: a #GtkLinkButton
738 * Sets @uri as the URI where the #GtkLinkButton points. As a side-effect
739 * this unsets the 'visited' state of the button.
744 gtk_link_button_set_uri (GtkLinkButton *link_button,
747 GtkLinkButtonPrivate *priv;
749 g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
750 g_return_if_fail (uri != NULL);
752 priv = link_button->priv;
755 priv->uri = g_strdup (uri);
757 g_object_notify (G_OBJECT (link_button), "uri");
759 gtk_link_button_set_visited (link_button, FALSE);
763 * gtk_link_button_get_uri:
764 * @link_button: a #GtkLinkButton
766 * Retrieves the URI set using gtk_link_button_set_uri().
768 * Return value: a valid URI. The returned string is owned by the link button
769 * and should not be modified or freed.
774 gtk_link_button_get_uri (GtkLinkButton *link_button)
776 g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), NULL);
778 return link_button->priv->uri;
782 * gtk_link_button_set_visited:
783 * @link_button: a #GtkLinkButton
784 * @visited: the new 'visited' state
786 * Sets the 'visited' state of the URI where the #GtkLinkButton
787 * points. See gtk_link_button_get_visited() for more details.
792 gtk_link_button_set_visited (GtkLinkButton *link_button,
795 g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
797 visited = visited != FALSE;
799 if (link_button->priv->visited != visited)
801 link_button->priv->visited = visited;
803 set_link_color (link_button);
805 g_object_notify (G_OBJECT (link_button), "visited");
810 * gtk_link_button_get_visited:
811 * @link_button: a #GtkLinkButton
813 * Retrieves the 'visited' state of the URI where the #GtkLinkButton
814 * points. The button becomes visited when it is clicked. If the URI
815 * is changed on the button, the 'visited' state is unset again.
817 * The state may also be changed using gtk_link_button_set_visited().
819 * Return value: %TRUE if the link has been visited, %FALSE otherwise
824 gtk_link_button_get_visited (GtkLinkButton *link_button)
826 g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), FALSE);
828 return link_button->priv->visited;