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 <gdk/gdkcolor.h>
30 #include <gdk/gdkcursor.h>
31 #include <gdk/gdkdisplay.h>
33 #include "gtkclipboard.h"
35 #include "gtkimagemenuitem.h"
39 #include "gtkmenuitem.h"
41 #include "gtktooltip.h"
43 #include "gtklinkbutton.h"
49 struct _GtkLinkButtonPrivate
55 GtkWidget *popup_menu;
65 #define GTK_LINK_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_LINK_BUTTON, GtkLinkButtonPrivate))
67 static void gtk_link_button_finalize (GObject *object);
68 static void gtk_link_button_get_property (GObject *object,
72 static void gtk_link_button_set_property (GObject *object,
76 static void gtk_link_button_add (GtkContainer *container,
78 static gboolean gtk_link_button_button_press (GtkWidget *widget,
79 GdkEventButton *event);
80 static void gtk_link_button_clicked (GtkButton *button);
81 static gboolean gtk_link_button_popup_menu (GtkWidget *widget);
82 static void gtk_link_button_style_set (GtkWidget *widget,
84 static gboolean gtk_link_button_enter_cb (GtkWidget *widget,
85 GdkEventCrossing *event,
87 static gboolean gtk_link_button_leave_cb (GtkWidget *widget,
88 GdkEventCrossing *event,
90 static void gtk_link_button_drag_data_get_cb (GtkWidget *widget,
91 GdkDragContext *context,
92 GtkSelectionData *selection,
96 static gboolean gtk_link_button_query_tooltip_cb (GtkWidget *widget,
99 gboolean keyboard_tip,
104 static const GtkTargetEntry link_drop_types[] = {
105 { "text/uri-list", 0, 0 },
106 { "_NETSCAPE_URL", 0, 0 }
109 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
110 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
112 static GtkLinkButtonUriFunc uri_func = NULL;
113 static gpointer uri_func_data = NULL;
114 static GDestroyNotify uri_func_destroy = NULL;
116 G_DEFINE_TYPE (GtkLinkButton, gtk_link_button, GTK_TYPE_BUTTON)
119 gtk_link_button_class_init (GtkLinkButtonClass *klass)
121 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
122 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
123 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
124 GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
126 gobject_class->set_property = gtk_link_button_set_property;
127 gobject_class->get_property = gtk_link_button_get_property;
128 gobject_class->finalize = gtk_link_button_finalize;
130 widget_class->button_press_event = gtk_link_button_button_press;
131 widget_class->popup_menu = gtk_link_button_popup_menu;
132 widget_class->style_set = gtk_link_button_style_set;
134 container_class->add = gtk_link_button_add;
136 button_class->clicked = gtk_link_button_clicked;
141 * The URI bound to this button.
145 g_object_class_install_property (gobject_class,
147 g_param_spec_string ("uri",
149 _("The URI bound to this button"),
153 g_type_class_add_private (gobject_class, sizeof (GtkLinkButtonPrivate));
157 gtk_link_button_init (GtkLinkButton *link_button)
159 link_button->priv = GTK_LINK_BUTTON_GET_PRIVATE (link_button),
161 gtk_button_set_relief (GTK_BUTTON (link_button), GTK_RELIEF_NONE);
163 g_signal_connect (link_button, "enter_notify_event",
164 G_CALLBACK (gtk_link_button_enter_cb), NULL);
165 g_signal_connect (link_button, "leave_notify_event",
166 G_CALLBACK (gtk_link_button_leave_cb), NULL);
167 g_signal_connect (link_button, "drag_data_get",
168 G_CALLBACK (gtk_link_button_drag_data_get_cb), NULL);
169 g_object_set (link_button, "has-tooltip", TRUE, NULL);
170 g_signal_connect (link_button, "query-tooltip",
171 G_CALLBACK (gtk_link_button_query_tooltip_cb), NULL);
173 /* enable drag source */
174 gtk_drag_source_set (GTK_WIDGET (link_button),
176 link_drop_types, G_N_ELEMENTS (link_drop_types),
181 gtk_link_button_finalize (GObject *object)
183 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
185 g_free (link_button->priv->uri);
187 G_OBJECT_CLASS (gtk_link_button_parent_class)->finalize (object);
191 gtk_link_button_get_property (GObject *object,
196 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
201 g_value_set_string (value, link_button->priv->uri);
204 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
210 gtk_link_button_set_property (GObject *object,
215 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
220 gtk_link_button_set_uri (link_button, g_value_get_string (value));
223 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
229 set_link_color (GtkLinkButton *link_button)
231 GdkColor *link_color = NULL;
234 label = gtk_bin_get_child (GTK_BIN (link_button));
236 if (link_button->priv->visited)
238 gtk_widget_style_get (GTK_WIDGET (link_button),
239 "visited-link-color", &link_color, NULL);
241 link_color = (GdkColor *) &default_visited_link_color;
245 gtk_widget_style_get (GTK_WIDGET (link_button),
246 "link-color", &link_color, NULL);
248 link_color = (GdkColor *) &default_link_color;
251 gtk_widget_modify_fg (label, GTK_STATE_NORMAL, link_color);
252 gtk_widget_modify_fg (label, GTK_STATE_ACTIVE, link_color);
253 gtk_widget_modify_fg (label, GTK_STATE_PRELIGHT, link_color);
254 gtk_widget_modify_fg (label, GTK_STATE_SELECTED, link_color);
256 if (link_color != &default_link_color &&
257 link_color != &default_visited_link_color)
258 gdk_color_free (link_color);
262 set_link_underline (GtkLinkButton *link_button)
266 label = gtk_bin_get_child (GTK_BIN (link_button));
267 if (GTK_IS_LABEL (label))
269 PangoAttrList *attributes;
270 PangoAttribute *uline;
272 uline = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
273 uline->start_index = 0;
274 uline->end_index = G_MAXUINT;
275 attributes = pango_attr_list_new ();
276 pango_attr_list_insert (attributes, uline);
277 gtk_label_set_attributes (GTK_LABEL (label), attributes);
278 pango_attr_list_unref (attributes);
283 gtk_link_button_add (GtkContainer *container,
286 GTK_CONTAINER_CLASS (gtk_link_button_parent_class)->add (container, widget);
288 set_link_underline (GTK_LINK_BUTTON (container));
292 gtk_link_button_style_set (GtkWidget *widget,
295 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
297 set_link_color (link_button);
301 set_hand_cursor (GtkWidget *widget,
307 display = gtk_widget_get_display (widget);
311 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
313 gdk_window_set_cursor (widget->window, cursor);
314 gdk_display_flush (display);
317 gdk_cursor_unref (cursor);
321 popup_menu_detach (GtkWidget *attach_widget,
324 GtkLinkButton *link_button = GTK_LINK_BUTTON (attach_widget);
326 link_button->priv->popup_menu = NULL;
330 popup_position_func (GtkMenu *menu,
336 GtkLinkButton *link_button = GTK_LINK_BUTTON (user_data);
337 GtkLinkButtonPrivate *priv = link_button->priv;
338 GtkWidget *widget = GTK_WIDGET (link_button);
339 GdkScreen *screen = gtk_widget_get_screen (widget);
342 GdkRectangle monitor;
344 g_return_if_fail (GTK_WIDGET_REALIZED (link_button));
346 gdk_window_get_origin (widget->window, x, y);
348 gtk_widget_size_request (priv->popup_menu, &req);
350 *x += widget->allocation.width / 2;
351 *y += widget->allocation.height;
353 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
354 gtk_menu_set_monitor (menu, monitor_num);
355 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
357 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
358 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
364 copy_activate_cb (GtkWidget *widget,
365 GtkLinkButton *link_button)
367 GtkLinkButtonPrivate *priv = link_button->priv;
369 gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (link_button),
370 GDK_SELECTION_CLIPBOARD),
375 gtk_link_button_do_popup (GtkLinkButton *link_button,
376 GdkEventButton *event)
378 GtkLinkButtonPrivate *priv = link_button->priv;
384 button = event->button;
390 time = gtk_get_current_event_time ();
393 if (GTK_WIDGET_REALIZED (link_button))
395 GtkWidget *menu_item;
397 if (priv->popup_menu)
398 gtk_widget_destroy (priv->popup_menu);
400 priv->popup_menu = gtk_menu_new ();
402 gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
403 GTK_WIDGET (link_button),
406 menu_item = gtk_image_menu_item_new_with_mnemonic (_("Copy URL"));
407 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item),
408 gtk_image_new_from_stock (GTK_STOCK_COPY,
409 GTK_ICON_SIZE_MENU));
410 g_signal_connect (menu_item, "activate",
411 G_CALLBACK (copy_activate_cb), link_button);
412 gtk_widget_show (menu_item);
413 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menu_item);
416 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
421 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
422 popup_position_func, link_button,
424 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
430 gtk_link_button_button_press (GtkWidget *widget,
431 GdkEventButton *event)
433 if (!GTK_WIDGET_HAS_FOCUS (widget))
434 gtk_widget_grab_focus (widget);
436 if ((event->button == 3) && (event->type == GDK_BUTTON_PRESS))
438 gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), event);
443 if (GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event)
444 return (* GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event) (widget, event);
450 gtk_link_button_clicked (GtkButton *button)
452 GtkLinkButton *link_button = GTK_LINK_BUTTON (button);
455 (* uri_func) (link_button, link_button->priv->uri, uri_func_data);
457 link_button->priv->visited = TRUE;
459 set_link_color (link_button);
463 gtk_link_button_popup_menu (GtkWidget *widget)
465 gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), NULL);
471 gtk_link_button_enter_cb (GtkWidget *widget,
472 GdkEventCrossing *crossing,
475 set_hand_cursor (widget, TRUE);
481 gtk_link_button_leave_cb (GtkWidget *widget,
482 GdkEventCrossing *crossing,
485 set_hand_cursor (widget, FALSE);
491 gtk_link_button_drag_data_get_cb (GtkWidget *widget,
492 GdkDragContext *context,
493 GtkSelectionData *selection,
498 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
501 uri = g_strdup_printf ("%s\r\n", link_button->priv->uri);
502 gtk_selection_data_set (selection,
512 * gtk_link_button_new:
515 * Creates a new #GtkLinkButton with the URI as its text.
517 * Return value: a new link button widget.
522 gtk_link_button_new (const gchar *uri)
524 gchar *utf8_uri = NULL;
527 g_return_val_if_fail (uri != NULL, NULL);
529 if (g_utf8_validate (uri, -1, NULL))
531 utf8_uri = g_strdup (uri);
535 GError *conv_err = NULL;
537 utf8_uri = g_locale_to_utf8 (uri, -1, NULL, NULL, &conv_err);
540 g_warning ("Attempting to convert URI `%s' to UTF-8, but failed "
544 g_error_free (conv_err);
546 utf8_uri = g_strdup (_("Invalid URI"));
550 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
561 * gtk_link_button_new_with_label:
563 * @label: the text of the button
565 * Creates a new #GtkLinkButton containing a label.
567 * Return value: a new link button widget.
572 gtk_link_button_new_with_label (const gchar *uri,
577 g_return_val_if_fail (uri != NULL, NULL);
580 return gtk_link_button_new (uri);
582 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
591 gtk_link_button_query_tooltip_cb (GtkWidget *widget,
594 gboolean keyboard_tip,
598 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
599 const gchar *label, *uri;
601 label = gtk_button_get_label (GTK_BUTTON (link_button));
602 uri = link_button->priv->uri;
604 if (label && *label != '\0' && uri && strcmp (label, uri) != 0)
606 gtk_tooltip_set_text (tooltip, uri);
615 * gtk_link_button_set_uri:
616 * @link_button: a #GtkLinkButton
619 * Sets @uri as the URI where the #GtkLinkButton points.
624 gtk_link_button_set_uri (GtkLinkButton *link_button,
627 GtkLinkButtonPrivate *priv;
629 g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
630 g_return_if_fail (uri != NULL);
632 priv = link_button->priv;
635 priv->uri = g_strdup (uri);
637 priv->visited = FALSE;
639 g_object_notify (G_OBJECT (link_button), "uri");
643 * gtk_link_button_get_uri:
644 * @link_button: a #GtkLinkButton
646 * Retrieves the URI set using gtk_link_button_set_uri().
648 * Return value: a valid URI. The returned string is owned by the link button
649 * and should not be modified or freed.
653 G_CONST_RETURN gchar *
654 gtk_link_button_get_uri (GtkLinkButton *link_button)
656 g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), NULL);
658 return link_button->priv->uri;
662 * gtk_link_button_set_uri_hook:
663 * @func: a function called each time a #GtkLinkButton is clicked, or %NULL
664 * @data: user data to be passed to @func, or %NULL
665 * @destroy: a #GDestroyNotify that gets called when @data is no longer needed, or %NULL
667 * Sets @func as the function that should be invoked every time a user clicks
668 * a #GtkLinkButton. This function is called before every callback registered
669 * for the "clicked" signal.
671 * Return value: the previously set hook function.
676 gtk_link_button_set_uri_hook (GtkLinkButtonUriFunc func,
678 GDestroyNotify destroy)
680 GtkLinkButtonUriFunc old_uri_func;
682 if (uri_func_destroy)
683 (* uri_func_destroy) (uri_func_data);
685 old_uri_func = uri_func;
688 uri_func_data = data;
689 uri_func_destroy = destroy;
694 #define __GTK_LINK_BUTTON_C__
695 #include "gtkaliasdef.c"