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.
29 #include "gtkclipboard.h"
31 #include "gtkimagemenuitem.h"
35 #include "gtkmenuitem.h"
38 #include "gtktooltip.h"
40 #include "gtklinkbutton.h"
46 struct _GtkLinkButtonPrivate
52 GtkWidget *popup_menu;
62 #define GTK_LINK_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_LINK_BUTTON, GtkLinkButtonPrivate))
64 static void gtk_link_button_finalize (GObject *object);
65 static void gtk_link_button_get_property (GObject *object,
69 static void gtk_link_button_set_property (GObject *object,
73 static void gtk_link_button_add (GtkContainer *container,
75 static gboolean gtk_link_button_button_press (GtkWidget *widget,
76 GdkEventButton *event);
77 static void gtk_link_button_clicked (GtkButton *button);
78 static gboolean gtk_link_button_popup_menu (GtkWidget *widget);
79 static void gtk_link_button_style_set (GtkWidget *widget,
81 static gboolean gtk_link_button_enter_cb (GtkWidget *widget,
82 GdkEventCrossing *event,
84 static gboolean gtk_link_button_leave_cb (GtkWidget *widget,
85 GdkEventCrossing *event,
87 static void gtk_link_button_drag_data_get_cb (GtkWidget *widget,
88 GdkDragContext *context,
89 GtkSelectionData *selection,
93 static gboolean gtk_link_button_query_tooltip_cb (GtkWidget *widget,
96 gboolean keyboard_tip,
101 static const GtkTargetEntry link_drop_types[] = {
102 { "text/uri-list", 0, 0 },
103 { "_NETSCAPE_URL", 0, 0 }
106 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
107 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
109 static GtkLinkButtonUriFunc uri_func = NULL;
110 static gpointer uri_func_data = NULL;
111 static GDestroyNotify uri_func_destroy = NULL;
113 G_DEFINE_TYPE (GtkLinkButton, gtk_link_button, GTK_TYPE_BUTTON)
116 gtk_link_button_class_init (GtkLinkButtonClass *klass)
118 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
119 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
120 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
121 GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
123 gobject_class->set_property = gtk_link_button_set_property;
124 gobject_class->get_property = gtk_link_button_get_property;
125 gobject_class->finalize = gtk_link_button_finalize;
127 widget_class->button_press_event = gtk_link_button_button_press;
128 widget_class->popup_menu = gtk_link_button_popup_menu;
129 widget_class->style_set = gtk_link_button_style_set;
131 container_class->add = gtk_link_button_add;
133 button_class->clicked = gtk_link_button_clicked;
138 * The URI bound to this button.
142 g_object_class_install_property (gobject_class,
144 g_param_spec_string ("uri",
146 P_("The URI bound to this button"),
150 * GtkLinkButton:visited
152 * The 'visited' state of this button. A visited link is drawn in a
157 g_object_class_install_property (gobject_class,
159 g_param_spec_boolean ("visited",
161 P_("Whether this link has been visited."),
165 g_type_class_add_private (gobject_class, sizeof (GtkLinkButtonPrivate));
169 gtk_link_button_init (GtkLinkButton *link_button)
171 link_button->priv = GTK_LINK_BUTTON_GET_PRIVATE (link_button),
173 gtk_button_set_relief (GTK_BUTTON (link_button), GTK_RELIEF_NONE);
175 g_signal_connect (link_button, "enter-notify-event",
176 G_CALLBACK (gtk_link_button_enter_cb), NULL);
177 g_signal_connect (link_button, "leave-notify-event",
178 G_CALLBACK (gtk_link_button_leave_cb), NULL);
179 g_signal_connect (link_button, "drag-data-get",
180 G_CALLBACK (gtk_link_button_drag_data_get_cb), NULL);
182 g_object_set (link_button, "has-tooltip", TRUE, NULL);
183 g_signal_connect (link_button, "query-tooltip",
184 G_CALLBACK (gtk_link_button_query_tooltip_cb), NULL);
186 /* enable drag source */
187 gtk_drag_source_set (GTK_WIDGET (link_button),
189 link_drop_types, G_N_ELEMENTS (link_drop_types),
194 gtk_link_button_finalize (GObject *object)
196 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
198 g_free (link_button->priv->uri);
200 G_OBJECT_CLASS (gtk_link_button_parent_class)->finalize (object);
204 gtk_link_button_get_property (GObject *object,
209 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
214 g_value_set_string (value, link_button->priv->uri);
217 g_value_set_boolean (value, link_button->priv->visited);
220 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
226 gtk_link_button_set_property (GObject *object,
231 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
236 gtk_link_button_set_uri (link_button, g_value_get_string (value));
239 gtk_link_button_set_visited (link_button, g_value_get_boolean (value));
242 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
248 set_link_color (GtkLinkButton *link_button)
250 GdkColor *link_color = NULL;
253 label = gtk_bin_get_child (GTK_BIN (link_button));
254 if (!GTK_IS_LABEL (label))
257 if (link_button->priv->visited)
259 gtk_widget_style_get (GTK_WIDGET (link_button),
260 "visited-link-color", &link_color, NULL);
262 link_color = (GdkColor *) &default_visited_link_color;
266 gtk_widget_style_get (GTK_WIDGET (link_button),
267 "link-color", &link_color, NULL);
269 link_color = (GdkColor *) &default_link_color;
272 gtk_widget_modify_fg (label, GTK_STATE_NORMAL, link_color);
273 gtk_widget_modify_fg (label, GTK_STATE_ACTIVE, link_color);
274 gtk_widget_modify_fg (label, GTK_STATE_PRELIGHT, link_color);
275 gtk_widget_modify_fg (label, GTK_STATE_SELECTED, link_color);
277 if (link_color != &default_link_color &&
278 link_color != &default_visited_link_color)
279 gdk_color_free (link_color);
283 set_link_underline (GtkLinkButton *link_button)
287 label = gtk_bin_get_child (GTK_BIN (link_button));
288 if (GTK_IS_LABEL (label))
290 PangoAttrList *attributes;
291 PangoAttribute *uline;
293 uline = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
294 uline->start_index = 0;
295 uline->end_index = G_MAXUINT;
296 attributes = pango_attr_list_new ();
297 pango_attr_list_insert (attributes, uline);
298 gtk_label_set_attributes (GTK_LABEL (label), attributes);
299 pango_attr_list_unref (attributes);
304 gtk_link_button_add (GtkContainer *container,
307 GTK_CONTAINER_CLASS (gtk_link_button_parent_class)->add (container, widget);
309 set_link_color (GTK_LINK_BUTTON (container));
310 set_link_underline (GTK_LINK_BUTTON (container));
314 gtk_link_button_style_set (GtkWidget *widget,
317 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
319 set_link_color (link_button);
323 set_hand_cursor (GtkWidget *widget,
329 display = gtk_widget_get_display (widget);
333 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
335 gdk_window_set_cursor (widget->window, cursor);
336 gdk_display_flush (display);
339 gdk_cursor_unref (cursor);
343 popup_menu_detach (GtkWidget *attach_widget,
346 GtkLinkButton *link_button = GTK_LINK_BUTTON (attach_widget);
348 link_button->priv->popup_menu = NULL;
352 popup_position_func (GtkMenu *menu,
358 GtkLinkButton *link_button = GTK_LINK_BUTTON (user_data);
359 GtkLinkButtonPrivate *priv = link_button->priv;
360 GtkWidget *widget = GTK_WIDGET (link_button);
361 GdkScreen *screen = gtk_widget_get_screen (widget);
364 GdkRectangle monitor;
366 g_return_if_fail (GTK_WIDGET_REALIZED (link_button));
368 gdk_window_get_origin (widget->window, x, y);
370 gtk_widget_size_request (priv->popup_menu, &req);
372 *x += widget->allocation.width / 2;
373 *y += widget->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_REALIZED (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);
477 (* uri_func) (link_button, link_button->priv->uri, uri_func_data);
483 if (gtk_widget_has_screen (GTK_WIDGET (button)))
484 screen = gtk_widget_get_screen (GTK_WIDGET (button));
489 gtk_show_uri (screen, link_button->priv->uri, GDK_CURRENT_TIME, &error);
492 g_warning ("Unable to show '%s': %s",
493 link_button->priv->uri,
495 g_error_free (error);
499 gtk_link_button_set_visited (link_button, TRUE);
503 gtk_link_button_popup_menu (GtkWidget *widget)
505 gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), NULL);
511 gtk_link_button_enter_cb (GtkWidget *widget,
512 GdkEventCrossing *crossing,
515 set_hand_cursor (widget, TRUE);
521 gtk_link_button_leave_cb (GtkWidget *widget,
522 GdkEventCrossing *crossing,
525 set_hand_cursor (widget, FALSE);
531 gtk_link_button_drag_data_get_cb (GtkWidget *widget,
532 GdkDragContext *context,
533 GtkSelectionData *selection,
538 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
541 uri = g_strdup_printf ("%s\r\n", link_button->priv->uri);
542 gtk_selection_data_set (selection,
552 * gtk_link_button_new:
555 * Creates a new #GtkLinkButton with the URI as its text.
557 * Return value: a new link button widget.
562 gtk_link_button_new (const gchar *uri)
564 gchar *utf8_uri = NULL;
567 g_return_val_if_fail (uri != NULL, NULL);
569 if (g_utf8_validate (uri, -1, NULL))
571 utf8_uri = g_strdup (uri);
575 GError *conv_err = NULL;
577 utf8_uri = g_locale_to_utf8 (uri, -1, NULL, NULL, &conv_err);
580 g_warning ("Attempting to convert URI `%s' to UTF-8, but failed "
584 g_error_free (conv_err);
586 utf8_uri = g_strdup (_("Invalid URI"));
590 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
601 * gtk_link_button_new_with_label:
603 * @label: (allow-none): the text of the button
605 * Creates a new #GtkLinkButton containing a label.
607 * Return value: (transfer none): a new link button widget.
612 gtk_link_button_new_with_label (const gchar *uri,
617 g_return_val_if_fail (uri != NULL, NULL);
620 return gtk_link_button_new (uri);
622 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
631 gtk_link_button_query_tooltip_cb (GtkWidget *widget,
634 gboolean keyboard_tip,
638 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
639 const gchar *label, *uri;
641 label = gtk_button_get_label (GTK_BUTTON (link_button));
642 uri = link_button->priv->uri;
644 if (!gtk_widget_get_tooltip_text (widget)
645 && !gtk_widget_get_tooltip_markup (widget)
646 && label && *label != '\0' && uri && strcmp (label, uri) != 0)
648 gtk_tooltip_set_text (tooltip, uri);
657 * gtk_link_button_set_uri:
658 * @link_button: a #GtkLinkButton
661 * Sets @uri as the URI where the #GtkLinkButton points. As a side-effect
662 * this unsets the 'visited' state of the button.
667 gtk_link_button_set_uri (GtkLinkButton *link_button,
670 GtkLinkButtonPrivate *priv;
672 g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
673 g_return_if_fail (uri != NULL);
675 priv = link_button->priv;
678 priv->uri = g_strdup (uri);
680 g_object_notify (G_OBJECT (link_button), "uri");
682 gtk_link_button_set_visited (link_button, FALSE);
686 * gtk_link_button_get_uri:
687 * @link_button: a #GtkLinkButton
689 * Retrieves the URI set using gtk_link_button_set_uri().
691 * Return value: a valid URI. The returned string is owned by the link button
692 * and should not be modified or freed.
696 G_CONST_RETURN gchar *
697 gtk_link_button_get_uri (GtkLinkButton *link_button)
699 g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), NULL);
701 return link_button->priv->uri;
705 * gtk_link_button_set_uri_hook:
706 * @func: (allow-none): a function called each time a #GtkLinkButton is clicked, or %NULL
707 * @data: (allow-none): user data to be passed to @func, or %NULL
708 * @destroy: (allow-none): a #GDestroyNotify that gets called when @data is no longer needed, or %NULL
710 * Sets @func as the function that should be invoked every time a user clicks
711 * a #GtkLinkButton. This function is called before every callback registered
712 * for the "clicked" signal.
714 * If no uri hook has been set, GTK+ defaults to calling gtk_show_uri().
716 * Return value: the previously set hook function.
721 gtk_link_button_set_uri_hook (GtkLinkButtonUriFunc func,
723 GDestroyNotify destroy)
725 GtkLinkButtonUriFunc old_uri_func;
727 if (uri_func_destroy)
728 (* uri_func_destroy) (uri_func_data);
730 old_uri_func = uri_func;
733 uri_func_data = data;
734 uri_func_destroy = destroy;
740 * gtk_link_button_set_visited:
741 * @link_button: a #GtkLinkButton
742 * @visited: the new 'visited' state
744 * Sets the 'visited' state of the URI where the #GtkLinkButton
745 * points. See gtk_link_button_get_visited() for more details.
750 gtk_link_button_set_visited (GtkLinkButton *link_button,
753 g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
755 visited = visited != FALSE;
757 if (link_button->priv->visited != visited)
759 link_button->priv->visited = visited;
761 set_link_color (link_button);
763 g_object_notify (G_OBJECT (link_button), "visited");
768 * gtk_link_button_get_visited:
769 * @link_button: a #GtkLinkButton
771 * Retrieves the 'visited' state of the URI where the #GtkLinkButton
772 * points. The button becomes visited when it is clicked. If the URI
773 * is changed on the button, the 'visited' state is unset again.
775 * The state may also be changed using gtk_link_button_set_visited().
777 * Return value: %TRUE if the link has been visited, %FALSE otherwise
782 gtk_link_button_get_visited (GtkLinkButton *link_button)
784 g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), FALSE);
786 return link_button->priv->visited;
790 #define __GTK_LINK_BUTTON_C__
791 #include "gtkaliasdef.c"