]> Pileus Git - ~andy/gtk/blob - gtk/gtklinkbutton.c
Practically everything changed.
[~andy/gtk] / gtk / gtklinkbutton.c
1 /* GTK - The GTK+ Toolkit
2  * gtklinkbutton.c - an hyperlink-enabled button
3  * 
4  * Copyright (C) 2006 Emmanuele Bassi <ebassi@gmail.com>
5  * All rights reserved.
6  *
7  * Based on gnome-href code by:
8  *      James Henstridge <james@daa.com.au>
9  * 
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.
14  *
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.
19  *
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.
23  */
24
25 #include "config.h"
26
27 #include <string.h>
28
29 #include "gtkclipboard.h"
30 #include "gtkdnd.h"
31 #include "gtkimagemenuitem.h"
32 #include "gtklabel.h"
33 #include "gtkmain.h"
34 #include "gtkmenu.h"
35 #include "gtkmenuitem.h"
36 #include "gtkstock.h"
37 #include "gtktooltip.h"
38
39 #include "gtklinkbutton.h"
40
41 #include "gtkintl.h"
42 #include "gtkalias.h"
43
44
45 struct _GtkLinkButtonPrivate
46 {
47   gchar *uri;
48
49   gboolean visited;
50
51   GtkWidget *popup_menu;
52 };
53
54 enum
55 {
56   PROP_0,
57
58   PROP_URI
59 };
60
61 #define GTK_LINK_BUTTON_GET_PRIVATE(obj)        (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_LINK_BUTTON, GtkLinkButtonPrivate))
62
63 static void     gtk_link_button_finalize     (GObject          *object);
64 static void     gtk_link_button_get_property (GObject          *object,
65                                               guint             prop_id,
66                                               GValue           *value,
67                                               GParamSpec       *pspec);
68 static void     gtk_link_button_set_property (GObject          *object,
69                                               guint             prop_id,
70                                               const GValue     *value,
71                                               GParamSpec       *pspec);
72 static void     gtk_link_button_add          (GtkContainer     *container,
73                                               GtkWidget        *widget);
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,
79                                               GtkStyle         *old_style);
80 static gboolean gtk_link_button_enter_cb     (GtkWidget        *widget,
81                                               GdkEventCrossing *event,
82                                               gpointer          user_data);
83 static gboolean gtk_link_button_leave_cb     (GtkWidget        *widget,
84                                               GdkEventCrossing *event,
85                                               gpointer          user_data);
86 static void gtk_link_button_drag_data_get_cb (GtkWidget        *widget,
87                                               GdkDragContext   *context,
88                                               GtkSelectionData *selection,
89                                               guint             _info,
90                                               guint             _time,
91                                               gpointer          user_data);
92 static gboolean gtk_link_button_query_tooltip_cb (GtkWidget    *widget,
93                                                   gint          x,
94                                                   gint          y,
95                                                   gboolean      keyboard_tip,
96                                                   GtkTooltip   *tooltip,
97                                                   gpointer      data);
98
99
100 static const GtkTargetEntry link_drop_types[] = {
101   { "text/uri-list", 0, 0 },
102   { "_NETSCAPE_URL", 0, 0 }
103 };
104
105 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
106 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
107
108 static GtkLinkButtonUriFunc uri_func = NULL;
109 static gpointer uri_func_data = NULL;
110 static GDestroyNotify uri_func_destroy = NULL;
111
112 G_DEFINE_TYPE (GtkLinkButton, gtk_link_button, GTK_TYPE_BUTTON)
113
114 static void
115 gtk_link_button_class_init (GtkLinkButtonClass *klass)
116 {
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);
121   
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;
125   
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;
129   
130   container_class->add = gtk_link_button_add;
131
132   button_class->clicked = gtk_link_button_clicked;
133
134   /**
135    * GtkLinkButton:uri
136    * 
137    * The URI bound to this button. 
138    *
139    * Since: 2.10
140    */
141   g_object_class_install_property (gobject_class,
142                                    PROP_URI,
143                                    g_param_spec_string ("uri",
144                                                         _("URI"),
145                                                         _("The URI bound to this button"),
146                                                         NULL,
147                                                         G_PARAM_READWRITE));
148   
149   g_type_class_add_private (gobject_class, sizeof (GtkLinkButtonPrivate));
150 }
151
152 static void
153 gtk_link_button_init (GtkLinkButton *link_button)
154 {
155   link_button->priv = GTK_LINK_BUTTON_GET_PRIVATE (link_button),
156   
157   gtk_button_set_relief (GTK_BUTTON (link_button), GTK_RELIEF_NONE);
158   
159   g_signal_connect (link_button, "enter_notify_event",
160                     G_CALLBACK (gtk_link_button_enter_cb), NULL);
161   g_signal_connect (link_button, "leave_notify_event",
162                     G_CALLBACK (gtk_link_button_leave_cb), NULL);
163   g_signal_connect (link_button, "drag_data_get",
164                     G_CALLBACK (gtk_link_button_drag_data_get_cb), NULL);
165   g_object_set (link_button, "has-tooltip", TRUE, NULL);
166   g_signal_connect (link_button, "query-tooltip",
167                     G_CALLBACK (gtk_link_button_query_tooltip_cb), NULL);
168   
169   /* enable drag source */
170   gtk_drag_source_set (GTK_WIDGET (link_button),
171                        GDK_BUTTON1_MASK,
172                        link_drop_types, G_N_ELEMENTS (link_drop_types),
173                        GDK_ACTION_COPY);
174 }
175
176 static void
177 gtk_link_button_finalize (GObject *object)
178 {
179   GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
180   
181   g_free (link_button->priv->uri);
182   
183   G_OBJECT_CLASS (gtk_link_button_parent_class)->finalize (object);
184 }
185
186 static void
187 gtk_link_button_get_property (GObject    *object,
188                               guint       prop_id,
189                               GValue     *value,
190                               GParamSpec *pspec)
191 {
192   GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
193   
194   switch (prop_id)
195     {
196     case PROP_URI:
197       g_value_set_string (value, link_button->priv->uri);
198       break;
199     default:
200       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
201       break;
202     }
203 }
204
205 static void
206 gtk_link_button_set_property (GObject      *object,
207                               guint         prop_id,
208                               const GValue *value,
209                               GParamSpec   *pspec)
210 {
211   GtkLinkButton *link_button = GTK_LINK_BUTTON (object);
212   
213   switch (prop_id)
214     {
215     case PROP_URI:
216       gtk_link_button_set_uri (link_button, g_value_get_string (value));
217       break;
218     default:
219       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220       break;
221     }
222 }
223
224 static void
225 set_link_color (GtkLinkButton *link_button)
226 {
227   GdkColor *link_color = NULL;
228   GtkWidget *label;
229
230   label = gtk_bin_get_child (GTK_BIN (link_button));
231
232   if (link_button->priv->visited)
233     {
234       gtk_widget_style_get (GTK_WIDGET (link_button),
235                             "visited-link-color", &link_color, NULL);
236       if (!link_color)
237         link_color = (GdkColor *) &default_visited_link_color;
238     }
239   else
240     {
241       gtk_widget_style_get (GTK_WIDGET (link_button),
242                             "link-color", &link_color, NULL);
243       if (!link_color)
244         link_color = (GdkColor *) &default_link_color;
245     }
246
247   gtk_widget_modify_fg (label, GTK_STATE_NORMAL, link_color);
248   gtk_widget_modify_fg (label, GTK_STATE_ACTIVE, link_color);
249   gtk_widget_modify_fg (label, GTK_STATE_PRELIGHT, link_color);
250   gtk_widget_modify_fg (label, GTK_STATE_SELECTED, link_color);
251
252   if (link_color != &default_link_color &&
253       link_color != &default_visited_link_color)
254     gdk_color_free (link_color);
255 }
256
257 static void
258 set_link_underline (GtkLinkButton *link_button)
259 {
260   GtkWidget *label;
261   
262   label = gtk_bin_get_child (GTK_BIN (link_button));
263   if (GTK_IS_LABEL (label))
264     {
265       PangoAttrList *attributes;
266       PangoAttribute *uline;
267
268       uline = pango_attr_underline_new (PANGO_UNDERLINE_SINGLE);
269       uline->start_index = 0;
270       uline->end_index = G_MAXUINT;
271       attributes = pango_attr_list_new ();
272       pango_attr_list_insert (attributes, uline); 
273       gtk_label_set_attributes (GTK_LABEL (label), attributes);
274       pango_attr_list_unref (attributes);
275     }
276 }
277
278 static void
279 gtk_link_button_add (GtkContainer *container,
280                      GtkWidget    *widget)
281 {
282   GTK_CONTAINER_CLASS (gtk_link_button_parent_class)->add (container, widget);
283
284   set_link_underline (GTK_LINK_BUTTON (container));
285 }
286
287 static void
288 gtk_link_button_style_set (GtkWidget *widget,
289                            GtkStyle  *old_style)
290 {
291   GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
292
293   set_link_color (link_button);
294 }
295
296 static void
297 set_hand_cursor (GtkWidget *widget,
298                  gboolean   show_hand)
299 {
300   GdkDisplay *display;
301   GdkCursor *cursor;
302
303   display = gtk_widget_get_display (widget);
304
305   cursor = NULL;
306   if (show_hand)
307     cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
308
309   gdk_window_set_cursor (widget->window, cursor);
310   gdk_display_flush (display);
311
312   if (cursor)
313     gdk_cursor_unref (cursor);
314 }
315
316 static void
317 popup_menu_detach (GtkWidget *attach_widget,
318                    GtkMenu   *menu)
319 {
320   GtkLinkButton *link_button = GTK_LINK_BUTTON (attach_widget);
321
322   link_button->priv->popup_menu = NULL;
323 }
324
325 static void
326 popup_position_func (GtkMenu  *menu,
327                      gint     *x,
328                      gint     *y,
329                      gboolean *push_in,
330                      gpointer  user_data)
331 {
332   GtkLinkButton *link_button = GTK_LINK_BUTTON (user_data);
333   GtkLinkButtonPrivate *priv = link_button->priv;
334   GtkWidget *widget = GTK_WIDGET (link_button);
335   GdkScreen *screen = gtk_widget_get_screen (widget);
336   GtkRequisition req;
337   gint monitor_num;
338   GdkRectangle monitor;
339   
340   g_return_if_fail (GTK_WIDGET_REALIZED (link_button));
341
342   gdk_window_get_origin (widget->window, x, y);
343
344   gtk_widget_size_request (priv->popup_menu, &req);
345
346   *x += widget->allocation.width / 2;
347   *y += widget->allocation.height;
348
349   monitor_num = gdk_screen_get_monitor_at_point (screen, *x, *y);
350   gtk_menu_set_monitor (menu, monitor_num);
351   gdk_screen_get_monitor_geometry (screen, monitor_num, &monitor);
352
353   *x = CLAMP (*x, monitor.x, monitor.x + MAX (0, monitor.width - req.width));
354   *y = CLAMP (*y, monitor.y, monitor.y + MAX (0, monitor.height - req.height));
355
356   *push_in = FALSE;
357 }
358
359 static void
360 copy_activate_cb (GtkWidget     *widget,
361                   GtkLinkButton *link_button)
362 {
363   GtkLinkButtonPrivate *priv = link_button->priv;
364   
365   gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (link_button),
366                                                     GDK_SELECTION_CLIPBOARD),
367                           priv->uri, -1);
368 }
369
370 static void
371 gtk_link_button_do_popup (GtkLinkButton  *link_button,
372                           GdkEventButton *event)
373 {
374   GtkLinkButtonPrivate *priv = link_button->priv;
375   gint button;
376   guint time;
377   
378   if (event)
379     {
380       button = event->button;
381       time = event->time;
382     }
383   else
384     {
385       button = 0;
386       time = gtk_get_current_event_time ();
387     }
388
389   if (GTK_WIDGET_REALIZED (link_button))
390     {
391       GtkWidget *menu_item;
392       
393       if (priv->popup_menu)
394         gtk_widget_destroy (priv->popup_menu);
395
396       priv->popup_menu = gtk_menu_new ();
397       
398       gtk_menu_attach_to_widget (GTK_MENU (priv->popup_menu),
399                                  GTK_WIDGET (link_button),
400                                  popup_menu_detach);
401
402       menu_item = gtk_image_menu_item_new_with_mnemonic (_("Copy URL"));
403       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menu_item),
404                                      gtk_image_new_from_stock (GTK_STOCK_COPY,
405                                                                GTK_ICON_SIZE_MENU));
406       g_signal_connect (menu_item, "activate",
407                         G_CALLBACK (copy_activate_cb), link_button);
408       gtk_widget_show (menu_item);
409       gtk_menu_shell_append (GTK_MENU_SHELL (priv->popup_menu), menu_item);
410       
411       if (button)
412         gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
413                         NULL, NULL,
414                         button, time);
415       else
416         {
417           gtk_menu_popup (GTK_MENU (priv->popup_menu), NULL, NULL,
418                           popup_position_func, link_button,
419                           button, time);
420           gtk_menu_shell_select_first (GTK_MENU_SHELL (priv->popup_menu), FALSE);
421         }
422     }
423 }
424
425 static gboolean
426 gtk_link_button_button_press (GtkWidget      *widget,
427                               GdkEventButton *event)
428 {
429   if (!GTK_WIDGET_HAS_FOCUS (widget))
430     gtk_widget_grab_focus (widget);
431
432   if ((event->button == 3) && (event->type == GDK_BUTTON_PRESS))
433     {
434       gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), event);
435       
436       return TRUE;
437     }
438
439   if (GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event)
440     return (* GTK_WIDGET_CLASS (gtk_link_button_parent_class)->button_press_event) (widget, event);
441   
442   return FALSE;
443 }
444
445 static void
446 gtk_link_button_clicked (GtkButton *button)
447 {
448   GtkLinkButton *link_button = GTK_LINK_BUTTON (button);
449
450   if (uri_func)
451     (* uri_func) (link_button, link_button->priv->uri, uri_func_data);
452
453   link_button->priv->visited = TRUE;
454
455   set_link_color (link_button);
456 }
457
458 static gboolean
459 gtk_link_button_popup_menu (GtkWidget *widget)
460 {
461   gtk_link_button_do_popup (GTK_LINK_BUTTON (widget), NULL);
462
463   return TRUE; 
464 }
465
466 static gboolean
467 gtk_link_button_enter_cb (GtkWidget        *widget,
468                           GdkEventCrossing *crossing,
469                           gpointer          user_data)
470 {
471   set_hand_cursor (widget, TRUE);
472   
473   return FALSE;
474 }
475
476 static gboolean
477 gtk_link_button_leave_cb (GtkWidget        *widget,
478                           GdkEventCrossing *crossing,
479                           gpointer          user_data)
480 {
481   set_hand_cursor (widget, FALSE);
482   
483   return FALSE;
484 }
485
486 static void
487 gtk_link_button_drag_data_get_cb (GtkWidget        *widget,
488                                   GdkDragContext   *context,
489                                   GtkSelectionData *selection,
490                                   guint             _info,
491                                   guint             _time,
492                                   gpointer          user_data)
493 {
494   GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
495   gchar *uri;
496   
497   uri = g_strdup_printf ("%s\r\n", link_button->priv->uri);
498   gtk_selection_data_set (selection,
499                           selection->target,
500                           8,
501                           (guchar *) uri,
502                           strlen (uri));
503   
504   g_free (uri);
505 }
506
507 /**
508  * gtk_link_button_new:
509  * @uri: a valid URI
510  *
511  * Creates a new #GtkLinkButton with the URI as its text.
512  *
513  * Return value: a new link button widget.
514  *
515  * Since: 2.10
516  */
517 GtkWidget *
518 gtk_link_button_new (const gchar *uri)
519 {
520   gchar *utf8_uri = NULL;
521   GtkWidget *retval;
522   
523   g_return_val_if_fail (uri != NULL, NULL);
524   
525   if (g_utf8_validate (uri, -1, NULL))
526     {
527       utf8_uri = g_strdup (uri);
528     }
529   else
530     {
531       GError *conv_err = NULL;
532     
533       utf8_uri = g_locale_to_utf8 (uri, -1, NULL, NULL, &conv_err);
534       if (conv_err)
535         {
536           g_warning ("Attempting to convert URI `%s' to UTF-8, but failed "
537                      "with error: %s\n",
538                      uri,
539                      conv_err->message);
540           g_error_free (conv_err);
541         
542           utf8_uri = g_strdup (_("Invalid URI"));
543         }
544     }
545   
546   retval = g_object_new (GTK_TYPE_LINK_BUTTON,
547                          "label", utf8_uri,
548                          "uri", uri,
549                          NULL);
550   
551   g_free (utf8_uri);
552   
553   return retval;
554 }
555
556 /**
557  * gtk_link_button_new_with_label:
558  * @uri: a valid URI
559  * @label: the text of the button
560  *
561  * Creates a new #GtkLinkButton containing a label.
562  *
563  * Return value: a new link button widget.
564  *
565  * Since: 2.10
566  */
567 GtkWidget *
568 gtk_link_button_new_with_label (const gchar *uri,
569                                 const gchar *label)
570 {
571   GtkWidget *retval;
572   
573   g_return_val_if_fail (uri != NULL, NULL);
574   
575   if (!label)
576     return gtk_link_button_new (uri);
577
578   retval = g_object_new (GTK_TYPE_LINK_BUTTON,
579                          "label", label,
580                          "uri", uri,
581                          NULL);
582
583   return retval;
584 }
585
586 static gboolean 
587 gtk_link_button_query_tooltip_cb (GtkWidget    *widget,
588                                   gint          x,
589                                   gint          y,
590                                   gboolean      keyboard_tip,
591                                   GtkTooltip   *tooltip,
592                                   gpointer      data)
593 {
594   GtkLinkButton *link_button = GTK_LINK_BUTTON (widget);
595   const gchar *label, *uri;
596
597   label = gtk_button_get_label (GTK_BUTTON (link_button));
598   uri = link_button->priv->uri;
599
600   if (label && *label != '\0' && uri && strcmp (label, uri) != 0)
601     {
602       gtk_tooltip_set_text (tooltip, uri);
603       return TRUE;
604     }
605
606   return FALSE;
607 }
608
609
610 /**
611  * gtk_link_button_set_uri:
612  * @link_button: a #GtkLinkButton
613  * @uri: a valid URI
614  *
615  * Sets @uri as the URI where the #GtkLinkButton points.
616  *
617  * Since: 2.10
618  */
619 void
620 gtk_link_button_set_uri (GtkLinkButton *link_button,
621                          const gchar   *uri)
622 {
623   GtkLinkButtonPrivate *priv;
624
625   g_return_if_fail (GTK_IS_LINK_BUTTON (link_button));
626   g_return_if_fail (uri != NULL);
627
628   priv = link_button->priv;
629
630   g_free (priv->uri);
631   priv->uri = g_strdup (uri);
632
633   priv->visited = FALSE;
634
635   g_object_notify (G_OBJECT (link_button), "uri");
636 }
637
638 /**
639  * gtk_link_button_get_uri:
640  * @link_button: a #GtkLinkButton
641  *
642  * Retrieves the URI set using gtk_link_button_set_uri().
643  *
644  * Return value: a valid URI.  The returned string is owned by the link button
645  *   and should not be modified or freed.
646  *
647  * Since: 2.10
648  */
649 G_CONST_RETURN gchar *
650 gtk_link_button_get_uri (GtkLinkButton *link_button)
651 {
652   g_return_val_if_fail (GTK_IS_LINK_BUTTON (link_button), NULL);
653   
654   return link_button->priv->uri;
655 }
656
657 /**
658  * gtk_link_button_set_uri_hook:
659  * @func: a function called each time a #GtkLinkButton is clicked, or %NULL
660  * @data: user data to be passed to @func, or %NULL
661  * @destroy: a #GDestroyNotify that gets called when @data is no longer needed, or %NULL
662  *
663  * Sets @func as the function that should be invoked every time a user clicks
664  * a #GtkLinkButton. This function is called before every callback registered
665  * for the "clicked" signal.
666  *
667  * Return value: the previously set hook function.
668  *
669  * Since: 2.10
670  */
671 GtkLinkButtonUriFunc
672 gtk_link_button_set_uri_hook (GtkLinkButtonUriFunc func,
673                               gpointer             data,
674                               GDestroyNotify       destroy)
675 {
676   GtkLinkButtonUriFunc old_uri_func;
677
678   if (uri_func_destroy)
679     (* uri_func_destroy) (uri_func_data);
680
681   old_uri_func = uri_func;
682
683   uri_func = func;
684   uri_func_data = data;
685   uri_func_destroy = destroy;
686
687   return old_uri_func;
688 }
689
690 #define __GTK_LINK_BUTTON_C__
691 #include "gtkaliasdef.c"