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"
37 #include "gtktooltip.h"
39 #include "gtklinkbutton.h"
45 struct _GtkLinkButtonPrivate
51 GtkWidget *popup_menu;
61 #define GTK_LINK_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_LINK_BUTTON, GtkLinkButtonPrivate))
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 static GtkLinkButtonUriFunc uri_func = NULL;
109 static gpointer uri_func_data = NULL;
110 static GDestroyNotify uri_func_destroy = NULL;
112 G_DEFINE_TYPE (GtkLinkButton, gtk_link_button, GTK_TYPE_BUTTON)
115 gtk_link_button_class_init (GtkLinkButtonClass *klass)
117 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
118 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
119 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
120 GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
122 gobject_class->set_property = gtk_link_button_set_property;
123 gobject_class->get_property = gtk_link_button_get_property;
124 gobject_class->finalize = gtk_link_button_finalize;
126 widget_class->button_press_event = gtk_link_button_button_press;
127 widget_class->popup_menu = gtk_link_button_popup_menu;
128 widget_class->style_set = gtk_link_button_style_set;
130 container_class->add = gtk_link_button_add;
132 button_class->clicked = gtk_link_button_clicked;
137 * The URI bound to this button.
141 g_object_class_install_property (gobject_class,
143 g_param_spec_string ("uri",
145 P_("The URI bound to this button"),
149 * GtkLinkButton:visited
151 * The 'visited' state of this button. A visited link is drawn in a
156 g_object_class_install_property (gobject_class,
158 g_param_spec_boolean ("visited",
160 P_("Whether this link has been visited."),
164 g_type_class_add_private (gobject_class, sizeof (GtkLinkButtonPrivate));
168 gtk_link_button_init (GtkLinkButton *link_button)
170 link_button->priv = GTK_LINK_BUTTON_GET_PRIVATE (link_button),
172 gtk_button_set_relief (GTK_BUTTON (link_button), GTK_RELIEF_NONE);
174 g_signal_connect (link_button, "enter-notify-event",
175 G_CALLBACK (gtk_link_button_enter_cb), NULL);
176 g_signal_connect (link_button, "leave-notify-event",
177 G_CALLBACK (gtk_link_button_leave_cb), NULL);
178 g_signal_connect (link_button, "drag-data-get",
179 G_CALLBACK (gtk_link_button_drag_data_get_cb), NULL);
181 g_object_set (link_button, "has-tooltip", TRUE, NULL);
182 g_signal_connect (link_button, "query-tooltip",
183 G_CALLBACK (gtk_link_button_query_tooltip_cb), NULL);
185 /* enable drag source */
186 gtk_drag_source_set (GTK_WIDGET (link_button),
188 link_drop_types, G_N_ELEMENTS (link_drop_types),
193 gtk_link_button_finalize (GObject *object)
195 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
197 g_free (link_button->priv->uri);
199 G_OBJECT_CLASS (gtk_link_button_parent_class)->finalize (object);
203 gtk_link_button_get_property (GObject *object,
208 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
213 g_value_set_string (value, link_button->priv->uri);
216 g_value_set_boolean (value, link_button->priv->visited);
219 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
225 gtk_link_button_set_property (GObject *object,
230 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
235 gtk_link_button_set_uri (link_button, g_value_get_string (value));
238 gtk_link_button_set_visited (link_button, g_value_get_boolean (value));
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 set_link_color (GtkLinkButton *link_button)
249 GdkColor *link_color = NULL;
252 label = gtk_bin_get_child (GTK_BIN (link_button));
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_underline (GTK_LINK_BUTTON (container));
310 gtk_link_button_style_set (GtkWidget *widget,
313 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
315 set_link_color (link_button);
319 set_hand_cursor (GtkWidget *widget,
325 display = gtk_widget_get_display (widget);
329 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
331 gdk_window_set_cursor (widget->window, cursor);
332 gdk_display_flush (display);
335 gdk_cursor_unref (cursor);
339 popup_menu_detach (GtkWidget *attach_widget,
342 GtkLinkButton *link_button = GTK_LINK_BUTTON (attach_widget);
344 link_button->priv->popup_menu = NULL;
348 popup_position_func (GtkMenu *menu,
354 GtkLinkButton *link_button = GTK_LINK_BUTTON (user_data);
355 GtkLinkButtonPrivate *priv = link_button->priv;
356 GtkWidget *widget = GTK_WIDGET (link_button);
357 GdkScreen *screen = gtk_widget_get_screen (widget);
360 GdkRectangle monitor;
362 g_return_if_fail (GTK_WIDGET_REALIZED (link_button));
364 gdk_window_get_origin (widget->window, x, y);
366 gtk_widget_size_request (priv->popup_menu, &req);
368 *x += widget->allocation.width / 2;
369 *y += widget->allocation.height;
371 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
372 gtk_menu_set_monitor (menu, monitor_num);
373 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
375 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
376 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
382 copy_activate_cb (GtkWidget *widget,
383 GtkLinkButton *link_button)
385 GtkLinkButtonPrivate *priv = link_button->priv;
387 gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (link_button),
388 GDK_SELECTION_CLIPBOARD),
393 gtk_link_button_do_popup (GtkLinkButton *link_button,
394 GdkEventButton *event)
396 GtkLinkButtonPrivate *priv = link_button->priv;
402 button = event->button;
408 time = gtk_get_current_event_time ();
411 if (GTK_WIDGET_REALIZED (link_button))
413 GtkWidget *menu_item;
415 if (priv->popup_menu)
416 gtk_widget_destroy (priv->popup_menu);
418 priv->popup_menu = gtk_menu_new ();
420 gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
421 GTK_WIDGET (link_button),
424 menu_item = gtk_image_menu_item_new_with_mnemonic (_("Copy URL"));
425 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item),
426 gtk_image_new_from_stock (GTK_STOCK_COPY,
427 GTK_ICON_SIZE_MENU));
428 g_signal_connect (menu_item, "activate",
429 G_CALLBACK (copy_activate_cb), link_button);
430 gtk_widget_show (menu_item);
431 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menu_item);
434 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
439 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
440 popup_position_func, link_button,
442 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
448 gtk_link_button_button_press (GtkWidget *widget,
449 GdkEventButton *event)
451 if (!GTK_WIDGET_HAS_FOCUS (widget))
452 gtk_widget_grab_focus (widget);
454 if ((event->button == 3) && (event->type == GDK_BUTTON_PRESS))
456 gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), event);
461 if (GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event)
462 return (* GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event) (widget, event);
468 gtk_link_button_clicked (GtkButton *button)
470 GtkLinkButton *link_button = GTK_LINK_BUTTON (button);
473 (* uri_func) (link_button, link_button->priv->uri, uri_func_data);
475 gtk_link_button_set_visited (link_button, TRUE);
479 gtk_link_button_popup_menu (GtkWidget *widget)
481 gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), NULL);
487 gtk_link_button_enter_cb (GtkWidget *widget,
488 GdkEventCrossing *crossing,
491 set_hand_cursor (widget, TRUE);
497 gtk_link_button_leave_cb (GtkWidget *widget,
498 GdkEventCrossing *crossing,
501 set_hand_cursor (widget, FALSE);
507 gtk_link_button_drag_data_get_cb (GtkWidget *widget,
508 GdkDragContext *context,
509 GtkSelectionData *selection,
514 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
517 uri = g_strdup_printf ("%s\r\n", link_button->priv->uri);
518 gtk_selection_data_set (selection,
528 * gtk_link_button_new:
531 * Creates a new #GtkLinkButton with the URI as its text.
533 * Return value: a new link button widget.
538 gtk_link_button_new (const gchar *uri)
540 gchar *utf8_uri = NULL;
543 g_return_val_if_fail (uri != NULL, NULL);
545 if (g_utf8_validate (uri, -1, NULL))
547 utf8_uri = g_strdup (uri);
551 GError *conv_err = NULL;
553 utf8_uri = g_locale_to_utf8 (uri, -1, NULL, NULL, &conv_err);
556 g_warning ("Attempting to convert URI `%s' to UTF-8, but failed "
560 g_error_free (conv_err);
562 utf8_uri = g_strdup (_("Invalid URI"));
566 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
577 * gtk_link_button_new_with_label:
579 * @label: the text of the button
581 * Creates a new #GtkLinkButton containing a label.
583 * Return value: a new link button widget.
588 gtk_link_button_new_with_label (const gchar *uri,
593 g_return_val_if_fail (uri != NULL, NULL);
596 return gtk_link_button_new (uri);
598 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
607 gtk_link_button_query_tooltip_cb (GtkWidget *widget,
610 gboolean keyboard_tip,
614 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
615 const gchar *label, *uri;
617 label = gtk_button_get_label (GTK_BUTTON (link_button));
618 uri = link_button->priv->uri;
620 if (label && *label != '\0' && uri && strcmp (label, uri) != 0)
622 gtk_tooltip_set_text (tooltip, uri);
631 * gtk_link_button_set_uri:
632 * @link_button: a #GtkLinkButton
635 * Sets @uri as the URI where the #GtkLinkButton points. As a side-effect
636 * this unsets the 'visited' state of the button.
641 gtk_link_button_set_uri (GtkLinkButton *link_button,
644 GtkLinkButtonPrivate *priv;
646 g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
647 g_return_if_fail (uri != NULL);
649 priv = link_button->priv;
652 priv->uri = g_strdup (uri);
654 g_object_notify (G_OBJECT (link_button), "uri");
656 gtk_link_button_set_visited (link_button, FALSE);
660 * gtk_link_button_get_uri:
661 * @link_button: a #GtkLinkButton
663 * Retrieves the URI set using gtk_link_button_set_uri().
665 * Return value: a valid URI. The returned string is owned by the link button
666 * and should not be modified or freed.
670 G_CONST_RETURN gchar *
671 gtk_link_button_get_uri (GtkLinkButton *link_button)
673 g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), NULL);
675 return link_button->priv->uri;
679 * gtk_link_button_set_uri_hook:
680 * @func: a function called each time a #GtkLinkButton is clicked, or %NULL
681 * @data: user data to be passed to @func, or %NULL
682 * @destroy: a #GDestroyNotify that gets called when @data is no longer needed, or %NULL
684 * Sets @func as the function that should be invoked every time a user clicks
685 * a #GtkLinkButton. This function is called before every callback registered
686 * for the "clicked" signal.
688 * Return value: the previously set hook function.
693 gtk_link_button_set_uri_hook (GtkLinkButtonUriFunc func,
695 GDestroyNotify destroy)
697 GtkLinkButtonUriFunc old_uri_func;
699 if (uri_func_destroy)
700 (* uri_func_destroy) (uri_func_data);
702 old_uri_func = uri_func;
705 uri_func_data = data;
706 uri_func_destroy = destroy;
712 * gtk_link_button_set_visited:
713 * @link_button: a #GtkLinkButton
714 * @visited: the new 'visited' state
716 * Sets the 'visited' state of the URI where the #GtkLinkButton
717 * points. See gtk_link_button_get_visited() for more details.
722 gtk_link_button_set_visited (GtkLinkButton *link_button,
725 g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
727 visited = visited != FALSE;
729 if (link_button->priv->visited != visited)
731 link_button->priv->visited = visited;
733 set_link_color (link_button);
735 g_object_notify (G_OBJECT (link_button), "visited");
740 * gtk_link_button_get_visited:
741 * @link_button: a #GtkLinkButton
743 * Retrieves the 'visited' state of the URI where the #GtkLinkButton
744 * points. The button becomes visited when it is clicked. If the URI
745 * is changed on the button, the 'visited' state is unset again.
747 * The state may also be changed using gtk_link_button_set_visited().
749 * Return value: %TRUE if the link has been visited, %FALSE otherwise
754 gtk_link_button_get_visited (GtkLinkButton *link_button)
756 g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), FALSE);
758 return link_button->priv->visited;
762 #define __GTK_LINK_BUTTON_C__
763 #include "gtkaliasdef.c"