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, see <http://www.gnu.org/licenses/>.
25 * SECTION:gtklinkbutton
26 * @Title: GtkLinkButton
27 * @Short_description: Create buttons bound to a URL
28 * @See_also: #GtkButton
30 * A GtkLinkButton is a #GtkButton with a hyperlink, similar to the one
31 * used by web browsers, which triggers an action when clicked. It is useful
32 * to show quick links to resources.
34 * A link button is created by calling either gtk_link_button_new() or
35 * gtk_link_button_new_with_label(). If using the former, the URI you pass
36 * to the constructor is used as a label for the widget.
38 * The URI bound to a GtkLinkButton can be set specifically using
39 * gtk_link_button_set_uri(), and retrieved using gtk_link_button_get_uri().
41 * By default, GtkLinkButton calls gtk_show_uri() when the button is
42 * clicked. This behaviour can be overridden by connecting to the
43 * #GtkLinkButton::activate-link signal and returning %TRUE from the
49 #include "gtklinkbutton.h"
53 #include "gtkclipboard.h"
55 #include "gtkimagemenuitem.h"
58 #include "gtkmarshalers.h"
60 #include "gtkmenuitem.h"
61 #include "gtksizerequest.h"
64 #include "gtktooltip.h"
65 #include "gtkprivate.h"
68 #include "a11y/gtklinkbuttonaccessible.h"
70 struct _GtkLinkButtonPrivate
76 GtkWidget *popup_menu;
93 static void gtk_link_button_finalize (GObject *object);
94 static void gtk_link_button_get_property (GObject *object,
98 static void gtk_link_button_set_property (GObject *object,
102 static void gtk_link_button_add (GtkContainer *container,
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,
113 static gboolean gtk_link_button_leave_cb (GtkWidget *widget,
114 GdkEventCrossing *event,
116 static void gtk_link_button_drag_data_get_cb (GtkWidget *widget,
117 GdkDragContext *context,
118 GtkSelectionData *selection,
122 static gboolean gtk_link_button_query_tooltip_cb (GtkWidget *widget,
125 gboolean keyboard_tip,
128 static gboolean gtk_link_button_activate_link (GtkLinkButton *link_button);
130 static const GtkTargetEntry link_drop_types[] = {
131 { "text/uri-list", 0, 0 },
132 { "_NETSCAPE_URL", 0, 0 }
135 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
136 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
138 static guint link_signals[LAST_SIGNAL] = { 0, };
140 G_DEFINE_TYPE (GtkLinkButton, gtk_link_button, GTK_TYPE_BUTTON)
143 gtk_link_button_class_init (GtkLinkButtonClass *klass)
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);
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;
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;
159 container_class->add = gtk_link_button_add;
161 button_class->clicked = gtk_link_button_clicked;
163 klass->activate_link = gtk_link_button_activate_link;
168 * The URI bound to this button.
172 g_object_class_install_property (gobject_class,
174 g_param_spec_string ("uri",
176 P_("The URI bound to this button"),
180 * GtkLinkButton:visited:
182 * The 'visited' state of this button. A visited link is drawn in a
187 g_object_class_install_property (gobject_class,
189 g_param_spec_boolean ("visited",
191 P_("Whether this link has been visited."),
195 g_type_class_add_private (gobject_class, sizeof (GtkLinkButtonPrivate));
198 * GtkLinkButton::activate-link:
199 * @button: the #GtkLinkButton that emitted the signal
201 * The ::activate-link signal is emitted each time the #GtkLinkButton
204 * The default handler will call gtk_show_uri() with the URI stored inside
205 * the #GtkLinkButton:uri property.
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
211 link_signals[ACTIVATE_LINK] =
212 g_signal_new (I_("activate-link"),
213 G_TYPE_FROM_CLASS (klass),
215 G_STRUCT_OFFSET (GtkLinkButtonClass, activate_link),
216 _gtk_boolean_handled_accumulator, NULL,
217 _gtk_marshal_BOOLEAN__VOID,
220 gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_LINK_BUTTON_ACCESSIBLE);
224 gtk_link_button_init (GtkLinkButton *link_button)
226 link_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (link_button,
227 GTK_TYPE_LINK_BUTTON,
228 GtkLinkButtonPrivate);
230 gtk_button_set_relief (GTK_BUTTON (link_button), GTK_RELIEF_NONE);
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);
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);
243 /* enable drag source */
244 gtk_drag_source_set (GTK_WIDGET (link_button),
246 link_drop_types, G_N_ELEMENTS (link_drop_types),
251 gtk_link_button_finalize (GObject *object)
253 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
255 g_free (link_button->priv->uri);
257 G_OBJECT_CLASS (gtk_link_button_parent_class)->finalize (object);
261 gtk_link_button_get_property (GObject *object,
266 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
271 g_value_set_string (value, link_button->priv->uri);
274 g_value_set_boolean (value, link_button->priv->visited);
277 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
283 gtk_link_button_set_property (GObject *object,
288 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
293 gtk_link_button_set_uri (link_button, g_value_get_string (value));
296 gtk_link_button_set_visited (link_button, g_value_get_boolean (value));
299 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
305 set_link_color (GtkLinkButton *link_button)
307 GdkColor *link_color = NULL;
311 label = gtk_bin_get_child (GTK_BIN (link_button));
312 if (!GTK_IS_LABEL (label))
315 if (link_button->priv->visited)
317 gtk_widget_style_get (GTK_WIDGET (link_button),
318 "visited-link-color", &link_color, NULL);
320 link_color = (GdkColor *) &default_visited_link_color;
324 gtk_widget_style_get (GTK_WIDGET (link_button),
325 "link-color", &link_color, NULL);
327 link_color = (GdkColor *) &default_link_color;
330 rgba.red = link_color->red / 65535.;
331 rgba.green = link_color->green / 65535.;
332 rgba.blue = link_color->blue / 65535.;
334 gtk_widget_override_color (label, GTK_STATE_FLAG_NORMAL, &rgba);
335 gtk_widget_override_color (label, GTK_STATE_FLAG_ACTIVE, &rgba);
336 gtk_widget_override_color (label, GTK_STATE_FLAG_PRELIGHT, &rgba);
337 gtk_widget_override_color (label, GTK_STATE_FLAG_SELECTED, &rgba);
339 if (link_color != &default_link_color &&
340 link_color != &default_visited_link_color)
341 gdk_color_free (link_color);
345 set_link_underline (GtkLinkButton *link_button)
349 label = gtk_bin_get_child (GTK_BIN (link_button));
350 if (GTK_IS_LABEL (label))
352 PangoAttrList *attributes;
353 PangoAttribute *uline;
355 uline = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
356 uline->start_index = 0;
357 uline->end_index = G_MAXUINT;
358 attributes = pango_attr_list_new ();
359 pango_attr_list_insert (attributes, uline);
360 gtk_label_set_attributes (GTK_LABEL (label), attributes);
361 pango_attr_list_unref (attributes);
366 gtk_link_button_add (GtkContainer *container,
369 GTK_CONTAINER_CLASS (gtk_link_button_parent_class)->add (container, widget);
371 set_link_color (GTK_LINK_BUTTON (container));
372 set_link_underline (GTK_LINK_BUTTON (container));
376 gtk_link_button_style_updated (GtkWidget *widget)
378 GTK_WIDGET_CLASS (gtk_link_button_parent_class)->style_updated (widget);
380 set_link_color (GTK_LINK_BUTTON (widget));
384 set_hand_cursor (GtkWidget *widget,
390 display = gtk_widget_get_display (widget);
394 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
396 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
397 gdk_display_flush (display);
400 g_object_unref (cursor);
404 gtk_link_button_unrealize (GtkWidget *widget)
406 set_hand_cursor (widget, FALSE);
408 GTK_WIDGET_CLASS (gtk_link_button_parent_class)->unrealize (widget);
412 popup_menu_detach (GtkWidget *attach_widget,
415 GtkLinkButton *link_button = GTK_LINK_BUTTON (attach_widget);
417 link_button->priv->popup_menu = NULL;
421 popup_position_func (GtkMenu *menu,
427 GtkLinkButton *link_button = GTK_LINK_BUTTON (user_data);
428 GtkLinkButtonPrivate *priv = link_button->priv;
429 GtkAllocation allocation;
430 GtkWidget *widget = GTK_WIDGET (link_button);
431 GdkScreen *screen = gtk_widget_get_screen (widget);
434 GdkRectangle monitor;
436 g_return_if_fail (gtk_widget_get_realized (widget));
438 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
440 gtk_widget_get_preferred_size (priv->popup_menu, &req, NULL);
442 gtk_widget_get_allocation (widget, &allocation);
443 *x += allocation.width / 2;
444 *y += allocation.height;
446 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
447 gtk_menu_set_monitor (menu, monitor_num);
448 gdk_screen_get_monitor_workarea (screen, monitor_num, &monitor);
450 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
451 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
457 copy_activate_cb (GtkWidget *widget,
458 GtkLinkButton *link_button)
460 GtkLinkButtonPrivate *priv = link_button->priv;
462 gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (link_button),
463 GDK_SELECTION_CLIPBOARD),
468 gtk_link_button_do_popup (GtkLinkButton *link_button,
469 GdkEventButton *event)
471 GtkLinkButtonPrivate *priv = link_button->priv;
477 button = event->button;
483 time = gtk_get_current_event_time ();
486 if (gtk_widget_get_realized (GTK_WIDGET (link_button)))
488 GtkWidget *menu_item;
490 if (priv->popup_menu)
491 gtk_widget_destroy (priv->popup_menu);
493 priv->popup_menu = gtk_menu_new ();
495 gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
496 GTK_WIDGET (link_button),
499 menu_item = gtk_image_menu_item_new_with_mnemonic (_("Copy URL"));
500 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item),
501 gtk_image_new_from_stock (GTK_STOCK_COPY,
502 GTK_ICON_SIZE_MENU));
503 g_signal_connect (menu_item, "activate",
504 G_CALLBACK (copy_activate_cb), link_button);
505 gtk_widget_show (menu_item);
506 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menu_item);
509 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
514 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
515 popup_position_func, link_button,
517 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
523 gtk_link_button_button_press (GtkWidget *widget,
524 GdkEventButton *event)
526 if (!gtk_widget_has_focus (widget))
527 gtk_widget_grab_focus (widget);
529 /* Don't popup the menu if there's no URI set,
530 * otherwise the menu item will trigger a warning */
531 if (gdk_event_triggers_context_menu ((GdkEvent *) event) &&
532 GTK_LINK_BUTTON (widget)->priv->uri != NULL)
534 gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), event);
539 if (GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event)
540 return GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event (widget, event);
546 gtk_link_button_activate_link (GtkLinkButton *link_button)
551 if (gtk_widget_has_screen (GTK_WIDGET (link_button)))
552 screen = gtk_widget_get_screen (GTK_WIDGET (link_button));
557 gtk_show_uri (screen, link_button->priv->uri, GDK_CURRENT_TIME, &error);
560 g_warning ("Unable to show '%s': %s",
561 link_button->priv->uri,
563 g_error_free (error);
568 gtk_link_button_set_visited (link_button, TRUE);
574 gtk_link_button_clicked (GtkButton *button)
576 gboolean retval = FALSE;
578 g_signal_emit (button, link_signals[ACTIVATE_LINK], 0, &retval);
582 gtk_link_button_popup_menu (GtkWidget *widget)
584 gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), NULL);
590 gtk_link_button_enter_cb (GtkWidget *widget,
591 GdkEventCrossing *crossing,
594 set_hand_cursor (widget, TRUE);
600 gtk_link_button_leave_cb (GtkWidget *widget,
601 GdkEventCrossing *crossing,
604 set_hand_cursor (widget, FALSE);
610 gtk_link_button_drag_data_get_cb (GtkWidget *widget,
611 GdkDragContext *context,
612 GtkSelectionData *selection,
617 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
620 uri = g_strdup_printf ("%s\r\n", link_button->priv->uri);
621 gtk_selection_data_set (selection,
622 gtk_selection_data_get_target (selection),
631 * gtk_link_button_new:
634 * Creates a new #GtkLinkButton with the URI as its text.
636 * Return value: a new link button widget.
641 gtk_link_button_new (const gchar *uri)
643 gchar *utf8_uri = NULL;
646 g_return_val_if_fail (uri != NULL, NULL);
648 if (g_utf8_validate (uri, -1, NULL))
650 utf8_uri = g_strdup (uri);
654 GError *conv_err = NULL;
656 utf8_uri = g_locale_to_utf8 (uri, -1, NULL, NULL, &conv_err);
659 g_warning ("Attempting to convert URI `%s' to UTF-8, but failed "
663 g_error_free (conv_err);
665 utf8_uri = g_strdup (_("Invalid URI"));
669 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
680 * gtk_link_button_new_with_label:
682 * @label: (allow-none): the text of the button
684 * Creates a new #GtkLinkButton containing a label.
686 * Return value: (transfer none): a new link button widget.
691 gtk_link_button_new_with_label (const gchar *uri,
696 g_return_val_if_fail (uri != NULL, NULL);
699 return gtk_link_button_new (uri);
701 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
710 gtk_link_button_query_tooltip_cb (GtkWidget *widget,
713 gboolean keyboard_tip,
717 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
718 const gchar *label, *uri;
720 label = gtk_button_get_label (GTK_BUTTON (link_button));
721 uri = link_button->priv->uri;
723 if (!gtk_widget_get_tooltip_text (widget)
724 && !gtk_widget_get_tooltip_markup (widget)
725 && label && *label != '\0' && uri && strcmp (label, uri) != 0)
727 gtk_tooltip_set_text (tooltip, uri);
736 * gtk_link_button_set_uri:
737 * @link_button: a #GtkLinkButton
740 * Sets @uri as the URI where the #GtkLinkButton points. As a side-effect
741 * this unsets the 'visited' state of the button.
746 gtk_link_button_set_uri (GtkLinkButton *link_button,
749 GtkLinkButtonPrivate *priv;
751 g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
752 g_return_if_fail (uri != NULL);
754 priv = link_button->priv;
757 priv->uri = g_strdup (uri);
759 g_object_notify (G_OBJECT (link_button), "uri");
761 gtk_link_button_set_visited (link_button, FALSE);
765 * gtk_link_button_get_uri:
766 * @link_button: a #GtkLinkButton
768 * Retrieves the URI set using gtk_link_button_set_uri().
770 * Return value: a valid URI. The returned string is owned by the link button
771 * and should not be modified or freed.
776 gtk_link_button_get_uri (GtkLinkButton *link_button)
778 g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), NULL);
780 return link_button->priv->uri;
784 * gtk_link_button_set_visited:
785 * @link_button: a #GtkLinkButton
786 * @visited: the new 'visited' state
788 * Sets the 'visited' state of the URI where the #GtkLinkButton
789 * points. See gtk_link_button_get_visited() for more details.
794 gtk_link_button_set_visited (GtkLinkButton *link_button,
797 g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
799 visited = visited != FALSE;
801 if (link_button->priv->visited != visited)
803 link_button->priv->visited = visited;
805 set_link_color (link_button);
807 g_object_notify (G_OBJECT (link_button), "visited");
812 * gtk_link_button_get_visited:
813 * @link_button: a #GtkLinkButton
815 * Retrieves the 'visited' state of the URI where the #GtkLinkButton
816 * points. The button becomes visited when it is clicked. If the URI
817 * is changed on the button, the 'visited' state is unset again.
819 * The state may also be changed using gtk_link_button_set_visited().
821 * Return value: %TRUE if the link has been visited, %FALSE otherwise
826 gtk_link_button_get_visited (GtkLinkButton *link_button)
828 g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), FALSE);
830 return link_button->priv->visited;