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.
27 #include "gtklinkbutton.h"
31 #include "gtkclipboard.h"
33 #include "gtkimagemenuitem.h"
37 #include "gtkmenuitem.h"
38 #include "gtksizerequest.h"
41 #include "gtktooltip.h"
46 struct _GtkLinkButtonPrivate
52 GtkWidget *popup_menu;
63 static void gtk_link_button_finalize (GObject *object);
64 static void gtk_link_button_get_property (GObject *object,
68 static void gtk_link_button_set_property (GObject *object,
72 static void gtk_link_button_add (GtkContainer *container,
74 static gboolean gtk_link_button_button_press (GtkWidget *widget,
75 GdkEventButton *event);
76 static void gtk_link_button_clicked (GtkButton *button);
77 static gboolean gtk_link_button_popup_menu (GtkWidget *widget);
78 static void gtk_link_button_style_set (GtkWidget *widget,
80 static gboolean gtk_link_button_enter_cb (GtkWidget *widget,
81 GdkEventCrossing *event,
83 static gboolean gtk_link_button_leave_cb (GtkWidget *widget,
84 GdkEventCrossing *event,
86 static void gtk_link_button_drag_data_get_cb (GtkWidget *widget,
87 GdkDragContext *context,
88 GtkSelectionData *selection,
92 static gboolean gtk_link_button_query_tooltip_cb (GtkWidget *widget,
95 gboolean keyboard_tip,
100 static const GtkTargetEntry link_drop_types[] = {
101 { "text/uri-list", 0, 0 },
102 { "_NETSCAPE_URL", 0, 0 }
105 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
106 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
108 G_DEFINE_TYPE (GtkLinkButton, gtk_link_button, GTK_TYPE_BUTTON)
111 gtk_link_button_class_init (GtkLinkButtonClass *klass)
113 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
114 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
115 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
116 GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
118 gobject_class->set_property = gtk_link_button_set_property;
119 gobject_class->get_property = gtk_link_button_get_property;
120 gobject_class->finalize = gtk_link_button_finalize;
122 widget_class->button_press_event = gtk_link_button_button_press;
123 widget_class->popup_menu = gtk_link_button_popup_menu;
124 widget_class->style_set = gtk_link_button_style_set;
126 container_class->add = gtk_link_button_add;
128 button_class->clicked = gtk_link_button_clicked;
133 * The URI bound to this button.
137 g_object_class_install_property (gobject_class,
139 g_param_spec_string ("uri",
141 P_("The URI bound to this button"),
145 * GtkLinkButton:visited
147 * The 'visited' state of this button. A visited link is drawn in a
152 g_object_class_install_property (gobject_class,
154 g_param_spec_boolean ("visited",
156 P_("Whether this link has been visited."),
160 g_type_class_add_private (gobject_class, sizeof (GtkLinkButtonPrivate));
164 gtk_link_button_init (GtkLinkButton *link_button)
166 link_button->priv = G_TYPE_INSTANCE_GET_PRIVATE (link_button,
167 GTK_TYPE_LINK_BUTTON,
168 GtkLinkButtonPrivate);
170 gtk_button_set_relief (GTK_BUTTON (link_button), GTK_RELIEF_NONE);
172 g_signal_connect (link_button, "enter-notify-event",
173 G_CALLBACK (gtk_link_button_enter_cb), NULL);
174 g_signal_connect (link_button, "leave-notify-event",
175 G_CALLBACK (gtk_link_button_leave_cb), NULL);
176 g_signal_connect (link_button, "drag-data-get",
177 G_CALLBACK (gtk_link_button_drag_data_get_cb), NULL);
179 g_object_set (link_button, "has-tooltip", TRUE, NULL);
180 g_signal_connect (link_button, "query-tooltip",
181 G_CALLBACK (gtk_link_button_query_tooltip_cb), NULL);
183 /* enable drag source */
184 gtk_drag_source_set (GTK_WIDGET (link_button),
186 link_drop_types, G_N_ELEMENTS (link_drop_types),
191 gtk_link_button_finalize (GObject *object)
193 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
195 g_free (link_button->priv->uri);
197 G_OBJECT_CLASS (gtk_link_button_parent_class)->finalize (object);
201 gtk_link_button_get_property (GObject *object,
206 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
211 g_value_set_string (value, link_button->priv->uri);
214 g_value_set_boolean (value, link_button->priv->visited);
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
223 gtk_link_button_set_property (GObject *object,
228 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
233 gtk_link_button_set_uri (link_button, g_value_get_string (value));
236 gtk_link_button_set_visited (link_button, g_value_get_boolean (value));
239 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
245 set_link_color (GtkLinkButton *link_button)
247 GdkColor *link_color = NULL;
250 label = gtk_bin_get_child (GTK_BIN (link_button));
251 if (!GTK_IS_LABEL (label))
254 if (link_button->priv->visited)
256 gtk_widget_style_get (GTK_WIDGET (link_button),
257 "visited-link-color", &link_color, NULL);
259 link_color = (GdkColor *) &default_visited_link_color;
263 gtk_widget_style_get (GTK_WIDGET (link_button),
264 "link-color", &link_color, NULL);
266 link_color = (GdkColor *) &default_link_color;
269 gtk_widget_modify_fg (label, GTK_STATE_NORMAL, link_color);
270 gtk_widget_modify_fg (label, GTK_STATE_ACTIVE, link_color);
271 gtk_widget_modify_fg (label, GTK_STATE_PRELIGHT, link_color);
272 gtk_widget_modify_fg (label, GTK_STATE_SELECTED, link_color);
274 if (link_color != &default_link_color &&
275 link_color != &default_visited_link_color)
276 gdk_color_free (link_color);
280 set_link_underline (GtkLinkButton *link_button)
284 label = gtk_bin_get_child (GTK_BIN (link_button));
285 if (GTK_IS_LABEL (label))
287 PangoAttrList *attributes;
288 PangoAttribute *uline;
290 uline = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
291 uline->start_index = 0;
292 uline->end_index = G_MAXUINT;
293 attributes = pango_attr_list_new ();
294 pango_attr_list_insert (attributes, uline);
295 gtk_label_set_attributes (GTK_LABEL (label), attributes);
296 pango_attr_list_unref (attributes);
301 gtk_link_button_add (GtkContainer *container,
304 GTK_CONTAINER_CLASS (gtk_link_button_parent_class)->add (container, widget);
306 set_link_color (GTK_LINK_BUTTON (container));
307 set_link_underline (GTK_LINK_BUTTON (container));
311 gtk_link_button_style_set (GtkWidget *widget,
314 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
316 set_link_color (link_button);
320 set_hand_cursor (GtkWidget *widget,
326 display = gtk_widget_get_display (widget);
330 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
332 gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
333 gdk_display_flush (display);
336 gdk_cursor_unref (cursor);
340 popup_menu_detach (GtkWidget *attach_widget,
343 GtkLinkButton *link_button = GTK_LINK_BUTTON (attach_widget);
345 link_button->priv->popup_menu = NULL;
349 popup_position_func (GtkMenu *menu,
355 GtkLinkButton *link_button = GTK_LINK_BUTTON (user_data);
356 GtkLinkButtonPrivate *priv = link_button->priv;
357 GtkAllocation allocation;
358 GtkWidget *widget = GTK_WIDGET (link_button);
359 GdkScreen *screen = gtk_widget_get_screen (widget);
362 GdkRectangle monitor;
364 g_return_if_fail (gtk_widget_get_realized (widget));
366 gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
368 gtk_size_request_get_size (GTK_SIZE_REQUEST (priv->popup_menu),
371 gtk_widget_get_allocation (widget, &allocation);
372 *x += allocation.width / 2;
373 *y += allocation.height;
375 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
376 gtk_menu_set_monitor (menu, monitor_num);
377 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
379 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
380 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
386 copy_activate_cb (GtkWidget *widget,
387 GtkLinkButton *link_button)
389 GtkLinkButtonPrivate *priv = link_button->priv;
391 gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (link_button),
392 GDK_SELECTION_CLIPBOARD),
397 gtk_link_button_do_popup (GtkLinkButton *link_button,
398 GdkEventButton *event)
400 GtkLinkButtonPrivate *priv = link_button->priv;
406 button = event->button;
412 time = gtk_get_current_event_time ();
415 if (gtk_widget_get_realized (GTK_WIDGET (link_button)))
417 GtkWidget *menu_item;
419 if (priv->popup_menu)
420 gtk_widget_destroy (priv->popup_menu);
422 priv->popup_menu = gtk_menu_new ();
424 gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
425 GTK_WIDGET (link_button),
428 menu_item = gtk_image_menu_item_new_with_mnemonic (_("Copy URL"));
429 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item),
430 gtk_image_new_from_stock (GTK_STOCK_COPY,
431 GTK_ICON_SIZE_MENU));
432 g_signal_connect (menu_item, "activate",
433 G_CALLBACK (copy_activate_cb), link_button);
434 gtk_widget_show (menu_item);
435 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menu_item);
438 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
443 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
444 popup_position_func, link_button,
446 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
452 gtk_link_button_button_press (GtkWidget *widget,
453 GdkEventButton *event)
455 if (!gtk_widget_has_focus (widget))
456 gtk_widget_grab_focus (widget);
458 if ((event->button == 3) && (event->type == GDK_BUTTON_PRESS))
460 gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), event);
465 if (GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event)
466 return GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event (widget, event);
472 gtk_link_button_clicked (GtkButton *button)
474 GtkLinkButton *link_button = GTK_LINK_BUTTON (button);
478 if (gtk_widget_has_screen (GTK_WIDGET (button)))
479 screen = gtk_widget_get_screen (GTK_WIDGET (button));
484 gtk_show_uri (screen, link_button->priv->uri, GDK_CURRENT_TIME, &error);
487 g_warning ("Unable to show '%s': %s",
488 link_button->priv->uri,
490 g_error_free (error);
493 gtk_link_button_set_visited (link_button, TRUE);
497 gtk_link_button_popup_menu (GtkWidget *widget)
499 gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), NULL);
505 gtk_link_button_enter_cb (GtkWidget *widget,
506 GdkEventCrossing *crossing,
509 set_hand_cursor (widget, TRUE);
515 gtk_link_button_leave_cb (GtkWidget *widget,
516 GdkEventCrossing *crossing,
519 set_hand_cursor (widget, FALSE);
525 gtk_link_button_drag_data_get_cb (GtkWidget *widget,
526 GdkDragContext *context,
527 GtkSelectionData *selection,
532 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
535 uri = g_strdup_printf ("%s\r\n", link_button->priv->uri);
536 gtk_selection_data_set (selection,
546 * gtk_link_button_new:
549 * Creates a new #GtkLinkButton with the URI as its text.
551 * Return value: a new link button widget.
556 gtk_link_button_new (const gchar *uri)
558 gchar *utf8_uri = NULL;
561 g_return_val_if_fail (uri != NULL, NULL);
563 if (g_utf8_validate (uri, -1, NULL))
565 utf8_uri = g_strdup (uri);
569 GError *conv_err = NULL;
571 utf8_uri = g_locale_to_utf8 (uri, -1, NULL, NULL, &conv_err);
574 g_warning ("Attempting to convert URI `%s' to UTF-8, but failed "
578 g_error_free (conv_err);
580 utf8_uri = g_strdup (_("Invalid URI"));
584 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
595 * gtk_link_button_new_with_label:
597 * @label: (allow-none): the text of the button
599 * Creates a new #GtkLinkButton containing a label.
601 * Return value: (transfer none): a new link button widget.
606 gtk_link_button_new_with_label (const gchar *uri,
611 g_return_val_if_fail (uri != NULL, NULL);
614 return gtk_link_button_new (uri);
616 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
625 gtk_link_button_query_tooltip_cb (GtkWidget *widget,
628 gboolean keyboard_tip,
632 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
633 const gchar *label, *uri;
635 label = gtk_button_get_label (GTK_BUTTON (link_button));
636 uri = link_button->priv->uri;
638 if (!gtk_widget_get_tooltip_text (widget)
639 && !gtk_widget_get_tooltip_markup (widget)
640 && label && *label != '\0' && uri && strcmp (label, uri) != 0)
642 gtk_tooltip_set_text (tooltip, uri);
651 * gtk_link_button_set_uri:
652 * @link_button: a #GtkLinkButton
655 * Sets @uri as the URI where the #GtkLinkButton points. As a side-effect
656 * this unsets the 'visited' state of the button.
661 gtk_link_button_set_uri (GtkLinkButton *link_button,
664 GtkLinkButtonPrivate *priv;
666 g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
667 g_return_if_fail (uri != NULL);
669 priv = link_button->priv;
672 priv->uri = g_strdup (uri);
674 g_object_notify (G_OBJECT (link_button), "uri");
676 gtk_link_button_set_visited (link_button, FALSE);
680 * gtk_link_button_get_uri:
681 * @link_button: a #GtkLinkButton
683 * Retrieves the URI set using gtk_link_button_set_uri().
685 * Return value: a valid URI. The returned string is owned by the link button
686 * and should not be modified or freed.
690 G_CONST_RETURN gchar *
691 gtk_link_button_get_uri (GtkLinkButton *link_button)
693 g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), NULL);
695 return link_button->priv->uri;
699 * gtk_link_button_set_visited:
700 * @link_button: a #GtkLinkButton
701 * @visited: the new 'visited' state
703 * Sets the 'visited' state of the URI where the #GtkLinkButton
704 * points. See gtk_link_button_get_visited() for more details.
709 gtk_link_button_set_visited (GtkLinkButton *link_button,
712 g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
714 visited = visited != FALSE;
716 if (link_button->priv->visited != visited)
718 link_button->priv->visited = visited;
720 set_link_color (link_button);
722 g_object_notify (G_OBJECT (link_button), "visited");
727 * gtk_link_button_get_visited:
728 * @link_button: a #GtkLinkButton
730 * Retrieves the 'visited' state of the URI where the #GtkLinkButton
731 * points. The button becomes visited when it is clicked. If the URI
732 * is changed on the button, the 'visited' state is unset again.
734 * The state may also be changed using gtk_link_button_set_visited().
736 * Return value: %TRUE if the link has been visited, %FALSE otherwise
741 gtk_link_button_get_visited (GtkLinkButton *link_button)
743 g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), FALSE);
745 return link_button->priv->visited;