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"
42 #include "gtklinkbutton.h"
48 struct _GtkLinkButtonPrivate
54 GtkWidget *popup_menu;
64 #define GTK_LINK_BUTTON_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_LINK_BUTTON, GtkLinkButtonPrivate))
66 static void gtk_link_button_finalize (GObject *object);
67 static void gtk_link_button_get_property (GObject *object,
71 static void gtk_link_button_set_property (GObject *object,
75 static void gtk_link_button_add (GtkContainer *container,
77 static gboolean gtk_link_button_button_press (GtkWidget *widget,
78 GdkEventButton *event);
79 static void gtk_link_button_clicked (GtkButton *button);
80 static gboolean gtk_link_button_popup_menu (GtkWidget *widget);
81 static void gtk_link_button_style_set (GtkWidget *widget,
83 static gboolean gtk_link_button_enter_cb (GtkWidget *widget,
84 GdkEventCrossing *event,
86 static gboolean gtk_link_button_leave_cb (GtkWidget *widget,
87 GdkEventCrossing *event,
89 static void gtk_link_button_drag_data_get_cb (GtkWidget *widget,
90 GdkDragContext *context,
91 GtkSelectionData *selection,
97 static const GtkTargetEntry link_drop_types[] = {
98 { "text/uri-list", 0, 0 },
99 { "_NETSCAPE_URL", 0, 0 }
102 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
103 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
105 static GtkLinkButtonUriFunc uri_func = NULL;
106 static gpointer uri_func_data = NULL;
107 static GDestroyNotify uri_func_destroy = NULL;
109 G_DEFINE_TYPE (GtkLinkButton, gtk_link_button, GTK_TYPE_BUTTON)
112 gtk_link_button_class_init (GtkLinkButtonClass *klass)
114 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
115 GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
116 GtkContainerClass *container_class = GTK_CONTAINER_CLASS (klass);
117 GtkButtonClass *button_class = GTK_BUTTON_CLASS (klass);
119 gobject_class->set_property = gtk_link_button_set_property;
120 gobject_class->get_property = gtk_link_button_get_property;
121 gobject_class->finalize = gtk_link_button_finalize;
123 widget_class->button_press_event = gtk_link_button_button_press;
124 widget_class->popup_menu = gtk_link_button_popup_menu;
125 widget_class->style_set = gtk_link_button_style_set;
127 container_class->add = gtk_link_button_add;
129 button_class->clicked = gtk_link_button_clicked;
134 * The URI bound to this button.
138 g_object_class_install_property (gobject_class,
140 g_param_spec_string ("uri",
142 _("The URI bound to this button"),
143 "http://www.gtk.org",
146 g_type_class_add_private (gobject_class, sizeof (GtkLinkButtonPrivate));
150 gtk_link_button_init (GtkLinkButton *link_button)
152 link_button->priv = GTK_LINK_BUTTON_GET_PRIVATE (link_button),
154 gtk_button_set_relief (GTK_BUTTON (link_button), GTK_RELIEF_NONE);
156 g_signal_connect (link_button, "enter-notify-event",
157 G_CALLBACK (gtk_link_button_enter_cb), NULL);
158 g_signal_connect (link_button, "leave-notify-event",
159 G_CALLBACK (gtk_link_button_leave_cb), NULL);
160 g_signal_connect (link_button, "drag-data-get",
161 G_CALLBACK (gtk_link_button_drag_data_get_cb), NULL);
163 /* enable drag source */
164 gtk_drag_source_set (GTK_WIDGET (link_button),
166 link_drop_types, G_N_ELEMENTS (link_drop_types),
171 gtk_link_button_finalize (GObject *object)
173 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
175 g_free (link_button->priv->uri);
177 G_OBJECT_CLASS (gtk_link_button_parent_class)->finalize (object);
181 gtk_link_button_get_property (GObject *object,
186 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
191 g_value_set_string (value, link_button->priv->uri);
194 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
200 gtk_link_button_set_property (GObject *object,
205 GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
210 gtk_link_button_set_uri (link_button, g_value_get_string (value));
213 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
219 set_link_color (GtkLinkButton *link_button)
221 GdkColor *link_color = NULL;
224 label = gtk_bin_get_child (GTK_BIN (link_button));
226 if (link_button->priv->visited)
228 gtk_widget_style_get (GTK_WIDGET (link_button),
229 "visited-link-color", &link_color, NULL);
231 link_color = &default_visited_link_color;
235 gtk_widget_style_get (GTK_WIDGET (link_button),
236 "link-color", &link_color, NULL);
238 link_color = &default_link_color;
241 gtk_widget_modify_fg (label, GTK_STATE_NORMAL, link_color);
242 gtk_widget_modify_fg (label, GTK_STATE_ACTIVE, link_color);
243 gtk_widget_modify_fg (label, GTK_STATE_PRELIGHT, link_color);
244 gtk_widget_modify_fg (label, GTK_STATE_SELECTED, link_color);
246 if (link_color != &default_link_color &&
247 link_color != &default_visited_link_color)
248 gdk_color_free (link_color);
252 set_link_underline (GtkLinkButton *link_button)
256 label = gtk_bin_get_child (GTK_BIN (link_button));
257 if (GTK_IS_LABEL (label))
259 PangoAttrList *attributes;
260 PangoAttribute *uline;
262 uline = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
263 uline->start_index = 0;
264 uline->end_index = G_MAXUINT;
265 attributes = pango_attr_list_new ();
266 pango_attr_list_insert (attributes, uline);
267 gtk_label_set_attributes (GTK_LABEL (label), attributes);
272 gtk_link_button_add (GtkContainer *container,
275 GTK_CONTAINER_CLASS (gtk_link_button_parent_class)->add (container, widget);
277 set_link_underline (GTK_LINK_BUTTON (container));
281 gtk_link_button_style_set (GtkWidget *widget,
284 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
286 set_link_color (link_button);
290 set_hand_cursor (GtkWidget *widget,
296 display = gtk_widget_get_display (widget);
300 cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
302 gdk_window_set_cursor (widget->window, cursor);
303 gdk_display_flush (display);
306 gdk_cursor_unref (cursor);
310 popup_menu_detach (GtkWidget *attach_widget,
313 GtkLinkButton *link_button = GTK_LINK_BUTTON (attach_widget);
315 link_button->priv->popup_menu = NULL;
319 popup_position_func (GtkMenu *menu,
325 GtkLinkButton *link_button = GTK_LINK_BUTTON (user_data);
326 GtkLinkButtonPrivate *priv = link_button->priv;
327 GtkWidget *widget = GTK_WIDGET (link_button);
328 GdkScreen *screen = gtk_widget_get_screen (widget);
331 GdkRectangle monitor;
333 g_return_if_fail (GTK_WIDGET_REALIZED (link_button));
335 gdk_window_get_origin (widget->window, x, y);
337 gtk_widget_size_request (priv->popup_menu, &req);
339 *x += widget->allocation.width / 2;
340 *y += widget->allocation.height;
342 monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
343 gtk_menu_set_monitor (menu, monitor_num);
344 gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
346 *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
347 *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
353 copy_activate_cb (GtkWidget *widget,
354 GtkLinkButton *link_button)
356 GtkLinkButtonPrivate *priv = link_button->priv;
358 gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (link_button),
359 GDK_SELECTION_CLIPBOARD),
364 gtk_link_button_do_popup (GtkLinkButton *link_button,
365 GdkEventButton *event)
367 GtkLinkButtonPrivate *priv = link_button->priv;
373 button = event->button;
379 time = gtk_get_current_event_time ();
382 if (GTK_WIDGET_REALIZED (link_button))
384 GtkWidget *menu_item;
386 if (priv->popup_menu)
387 gtk_widget_destroy (priv->popup_menu);
389 priv->popup_menu = gtk_menu_new ();
391 gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
392 GTK_WIDGET (link_button),
395 menu_item = gtk_image_menu_item_new_with_mnemonic (_("Copy URL"));
396 gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item),
397 gtk_image_new_from_stock (GTK_STOCK_COPY,
398 GTK_ICON_SIZE_MENU));
399 g_signal_connect (menu_item, "activate",
400 G_CALLBACK (copy_activate_cb), link_button);
401 gtk_widget_show (menu_item);
402 gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menu_item);
405 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
410 gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
411 popup_position_func, link_button,
413 gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
419 gtk_link_button_button_press (GtkWidget *widget,
420 GdkEventButton *event)
422 if (!GTK_WIDGET_HAS_FOCUS (widget))
423 gtk_widget_grab_focus (widget);
425 if ((event->button == 3) && (event->type == GDK_BUTTON_PRESS))
427 gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), event);
432 if (GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event)
433 return (* GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event) (widget, event);
439 gtk_link_button_clicked (GtkButton *button)
441 GtkLinkButton *link_button = GTK_LINK_BUTTON (button);
444 (* uri_func) (button, link_button->priv->uri, uri_func_data);
446 link_button->priv->visited = TRUE;
448 set_link_color (link_button);
452 gtk_link_button_popup_menu (GtkWidget *widget)
454 gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), NULL);
460 gtk_link_button_enter_cb (GtkWidget *widget,
461 GdkEventCrossing *crossing,
464 set_hand_cursor (widget, TRUE);
470 gtk_link_button_leave_cb (GtkWidget *widget,
471 GdkEventCrossing *crossing,
474 set_hand_cursor (widget, FALSE);
480 gtk_link_button_drag_data_get_cb (GtkWidget *widget,
481 GdkDragContext *context,
482 GtkSelectionData *selection,
487 GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
490 uri = g_strdup_printf ("%s\r\n", link_button->priv->uri);
491 gtk_selection_data_set (selection,
501 * gtk_link_button_new:
504 * Creates a new #GtkLinkButton with the URI as its text.
506 * Return value: a new link button widget.
511 gtk_link_button_new (const gchar *uri)
513 gchar *utf8_uri = NULL;
516 g_return_val_if_fail (uri != NULL, NULL);
518 if (g_utf8_validate (uri, -1, NULL))
520 utf8_uri = g_strdup (uri);
524 GError *conv_err = NULL;
526 utf8_uri = g_locale_to_utf8 (uri, -1, NULL, NULL, &conv_err);
529 g_warning ("Attempting to convert URI `%s' to UTF-8, but failed "
533 g_error_free (conv_err);
535 utf8_uri = g_strdup (_("Invalid URI"));
539 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
550 * gtk_link_button_new_with_label:
552 * @label: the text of the button
554 * Creates a new #GtkLinkButton containing a label.
556 * Return value: a new link button widget.
561 gtk_link_button_new_with_label (const gchar *uri,
566 g_return_val_if_fail (uri != NULL, NULL);
569 return gtk_link_button_new (uri);
571 retval = g_object_new (GTK_TYPE_LINK_BUTTON,
580 * gtk_link_button_set_uri:
581 * @link_button: a #GtkLinkButton
584 * Sets @uri as the URI where the #GtkLinkButton points.
589 gtk_link_button_set_uri (GtkLinkButton *link_button,
594 g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
595 g_return_if_fail (uri != NULL);
597 tmp = link_button->priv->uri;
598 link_button->priv->uri = g_strdup (uri);
601 link_button->priv->visited = FALSE;
603 g_object_notify (G_OBJECT (link_button), "uri");
607 * gtk_link_button_get_uri:
608 * @link_button: a #GtkLinkButton
610 * Retrieves the URI set using gtk_link_button_set_uri().
612 * Return value: a valid URI. The returned string is owned by the link button
613 * and should not be modified or freed.
617 G_CONST_RETURN gchar *
618 gtk_link_button_get_uri (GtkLinkButton *link_button)
620 g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), NULL);
622 return link_button->priv->uri;
626 * gtk_link_button_set_uri_hook:
627 * @func: a function called each time a #GtkLinkButton is clicked, or %NULL
628 * @data: user data to be passed to @func, or %NULL
629 * @destroy: a #GDestroyNotify that gets called when @data is no longer needed, or %NULL
631 * Sets @func as the function that should be invoked every time a user clicks
632 * a #GtkLinkButton. This function is called before every callback registered
633 * for the "clicked" signal.
635 * Return value: the previously set hook function.
640 gtk_link_button_set_uri_hook (GtkLinkButtonUriFunc func,
642 GDestroyNotify destroy)
644 GtkLinkButtonUriFunc old_uri_func;
646 if (uri_func_destroy)
647 (* uri_func_destroy) (uri_func_data);
649 old_uri_func = uri_func;
652 uri_func_data = data;
653 uri_func_destroy = destroy;
658 #define __GTK_LINK_BUTTON_C__
659 #include "gtkaliasdef.c"