]> Pileus Git - ~andy/gtk/blob - gtk/gtklabel.c
e0c6c5b771d17dadd6c534dcd630305c5be71741
[~andy/gtk] / gtk / gtklabel.c
1 /* GTK - The GIMP Toolkit
2  * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free
16  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  */
18
19 /*
20  * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
21  * file for a list of people on the GTK+ Team.  See the ChangeLog
22  * files for a list of changes.  These files are distributed with
23  * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
24  */
25
26 #include "config.h"
27
28 #include <math.h>
29 #include <string.h>
30
31 #include "gtklabel.h"
32 #include "gtkaccellabel.h"
33 #include "gtkdnd.h"
34 #include "gtkmainprivate.h"
35 #include "gtkmarshalers.h"
36 #include "gtkpango.h"
37 #include "gtkwindow.h"
38 #include "gtkclipboard.h"
39 #include "gtkimagemenuitem.h"
40 #include "gtkintl.h"
41 #include "gtkseparatormenuitem.h"
42 #include "gtktextutil.h"
43 #include "gtkmenuitem.h"
44 #include "gtkmenushellprivate.h"
45 #include "gtknotebook.h"
46 #include "gtkstock.h"
47 #include "gtkbindings.h"
48 #include "gtkbuildable.h"
49 #include "gtkimage.h"
50 #include "gtkshow.h"
51 #include "gtktooltip.h"
52 #include "gtkprivate.h"
53 #include "gtktypebuiltins.h"
54
55 /*rint() is only available in GCC and/or C99*/
56 #if (__STDC_VERSION__ < 199901L && !defined __GNUC__)
57 double rint(double x)
58 {
59         if (ceil(x+0.5) == floor(x+0.5))
60         {
61                 int a = (int)ceil(x);
62                 if (a%2 == 0)
63                         return ceil(x);
64                 else
65                         return floor(x);
66         }
67         else
68                 return floor(x+0.5);
69 }
70 #endif
71
72
73
74 struct _GtkLabelPrivate
75 {
76   GtkLabelSelectionInfo *select_info;
77   GtkWidget *mnemonic_widget;
78   GtkWindow *mnemonic_window;
79
80   PangoAttrList *attrs;
81   PangoAttrList *effective_attrs;
82   PangoLayout   *layout;
83
84   gboolean mnemonics_visible;
85
86   gchar   *label;
87   gchar   *text;
88
89   guint    jtype            : 2;
90   guint    wrap             : 1;
91   guint    use_underline    : 1;
92   guint    use_markup       : 1;
93   guint    ellipsize        : 3;
94   guint    single_line_mode : 1;
95   guint    have_transform   : 1;
96   guint    in_click         : 1;
97   guint    wrap_mode        : 3;
98   guint    pattern_set      : 1;
99   guint    track_links      : 1;
100
101   guint    mnemonic_keyval;
102
103   gint     wrap_width;
104   gint     width_chars;
105   gint     max_width_chars;
106
107   gdouble  angle;
108 };
109
110 /* Notes about the handling of links:
111  *
112  * Links share the GtkLabelSelectionInfo struct with selectable labels.
113  * There are some new fields for links. The links field contains the list
114  * of GtkLabelLink structs that describe the links which are embedded in
115  * the label. The active_link field points to the link under the mouse
116  * pointer. For keyboard navigation, the 'focus' link is determined by
117  * finding the link which contains the selection_anchor position.
118  * The link_clicked field is used with button press and release events
119  * to ensure that pressing inside a link and releasing outside of it
120  * does not activate the link.
121  *
122  * Links are rendered with the link-color/visited-link-color colors
123  * that are determined by the style and with an underline. When the mouse
124  * pointer is over a link, the pointer is changed to indicate the link,
125  * and the background behind the link is rendered with the base[PRELIGHT]
126  * color. While a button is pressed over a link, the background is rendered
127  * with the base[ACTIVE] color.
128  *
129  * Labels with links accept keyboard focus, and it is possible to move
130  * the focus between the embedded links using Tab/Shift-Tab. The focus
131  * is indicated by a focus rectangle that is drawn around the link text.
132  * Pressing Enter activates the focussed link, and there is a suitable
133  * context menu for links that can be opened with the Menu key. Pressing
134  * Control-C copies the link URI to the clipboard.
135  *
136  * In selectable labels with links, link functionality is only available
137  * when the selection is empty.
138  */
139 typedef struct
140 {
141   gchar *uri;
142   gchar *title;     /* the title attribute, used as tooltip */
143   gboolean visited; /* get set when the link is activated; this flag
144                      * gets preserved over later set_markup() calls
145                      */
146   gint start;       /* position of the link in the PangoLayout */
147   gint end;
148 } GtkLabelLink;
149
150 struct _GtkLabelSelectionInfo
151 {
152   GdkWindow *window;
153   gint selection_anchor;
154   gint selection_end;
155   GtkWidget *popup_menu;
156
157   GList *links;
158   GtkLabelLink *active_link;
159
160   gint drag_start_x;
161   gint drag_start_y;
162
163   guint in_drag      : 1;
164   guint select_words : 1;
165   guint selectable   : 1;
166   guint link_clicked : 1;
167 };
168
169 enum {
170   MOVE_CURSOR,
171   COPY_CLIPBOARD,
172   POPULATE_POPUP,
173   ACTIVATE_LINK,
174   ACTIVATE_CURRENT_LINK,
175   LAST_SIGNAL
176 };
177
178 enum {
179   PROP_0,
180   PROP_LABEL,
181   PROP_ATTRIBUTES,
182   PROP_USE_MARKUP,
183   PROP_USE_UNDERLINE,
184   PROP_JUSTIFY,
185   PROP_PATTERN,
186   PROP_WRAP,
187   PROP_WRAP_MODE,
188   PROP_SELECTABLE,
189   PROP_MNEMONIC_KEYVAL,
190   PROP_MNEMONIC_WIDGET,
191   PROP_CURSOR_POSITION,
192   PROP_SELECTION_BOUND,
193   PROP_ELLIPSIZE,
194   PROP_WIDTH_CHARS,
195   PROP_SINGLE_LINE_MODE,
196   PROP_ANGLE,
197   PROP_MAX_WIDTH_CHARS,
198   PROP_TRACK_VISITED_LINKS
199 };
200
201 /* When rotating ellipsizable text we want the natural size to request 
202  * more to ensure the label wont ever ellipsize in an allocation of full natural size.
203  * */
204 #define ROTATION_ELLIPSIZE_PADDING 2
205
206 static guint signals[LAST_SIGNAL] = { 0 };
207
208 static const GdkColor default_link_color = { 0, 0, 0, 0xeeee };
209 static const GdkColor default_visited_link_color = { 0, 0x5555, 0x1a1a, 0x8b8b };
210
211 static void gtk_label_set_property      (GObject          *object,
212                                          guint             prop_id,
213                                          const GValue     *value,
214                                          GParamSpec       *pspec);
215 static void gtk_label_get_property      (GObject          *object,
216                                          guint             prop_id,
217                                          GValue           *value,
218                                          GParamSpec       *pspec);
219 static void gtk_label_finalize          (GObject          *object);
220 static void gtk_label_destroy           (GtkWidget        *widget);
221 static void gtk_label_size_allocate     (GtkWidget        *widget,
222                                          GtkAllocation    *allocation);
223 static void gtk_label_state_changed     (GtkWidget        *widget,
224                                          GtkStateType      state);
225 static void gtk_label_style_updated     (GtkWidget        *widget);
226 static void gtk_label_direction_changed (GtkWidget        *widget,
227                                          GtkTextDirection  previous_dir);
228 static gint gtk_label_draw              (GtkWidget        *widget,
229                                          cairo_t          *cr);
230 static gboolean gtk_label_focus         (GtkWidget         *widget,
231                                          GtkDirectionType   direction);
232
233 static void gtk_label_realize           (GtkWidget        *widget);
234 static void gtk_label_unrealize         (GtkWidget        *widget);
235 static void gtk_label_map               (GtkWidget        *widget);
236 static void gtk_label_unmap             (GtkWidget        *widget);
237
238 static gboolean gtk_label_button_press      (GtkWidget        *widget,
239                                              GdkEventButton   *event);
240 static gboolean gtk_label_button_release    (GtkWidget        *widget,
241                                              GdkEventButton   *event);
242 static gboolean gtk_label_motion            (GtkWidget        *widget,
243                                              GdkEventMotion   *event);
244 static gboolean gtk_label_leave_notify      (GtkWidget        *widget,
245                                              GdkEventCrossing *event);
246
247 static void     gtk_label_grab_focus        (GtkWidget        *widget);
248
249 static gboolean gtk_label_query_tooltip     (GtkWidget        *widget,
250                                              gint              x,
251                                              gint              y,
252                                              gboolean          keyboard_tip,
253                                              GtkTooltip       *tooltip);
254
255 static void gtk_label_set_text_internal          (GtkLabel      *label,
256                                                   gchar         *str);
257 static void gtk_label_set_label_internal         (GtkLabel      *label,
258                                                   gchar         *str);
259 static void gtk_label_set_use_markup_internal    (GtkLabel      *label,
260                                                   gboolean       val);
261 static void gtk_label_set_use_underline_internal (GtkLabel      *label,
262                                                   gboolean       val);
263 static void gtk_label_set_attributes_internal    (GtkLabel      *label,
264                                                   PangoAttrList *attrs);
265 static void gtk_label_set_uline_text_internal    (GtkLabel      *label,
266                                                   const gchar   *str);
267 static void gtk_label_set_pattern_internal       (GtkLabel      *label,
268                                                   const gchar   *pattern,
269                                                   gboolean       is_mnemonic);
270 static void gtk_label_set_markup_internal        (GtkLabel      *label,
271                                                   const gchar   *str,
272                                                   gboolean       with_uline);
273 static void gtk_label_recalculate                (GtkLabel      *label);
274 static void gtk_label_hierarchy_changed          (GtkWidget     *widget,
275                                                   GtkWidget     *old_toplevel);
276 static void gtk_label_screen_changed             (GtkWidget     *widget,
277                                                   GdkScreen     *old_screen);
278 static gboolean gtk_label_popup_menu             (GtkWidget     *widget);
279
280 static void gtk_label_create_window       (GtkLabel *label);
281 static void gtk_label_destroy_window      (GtkLabel *label);
282 static void gtk_label_ensure_select_info  (GtkLabel *label);
283 static void gtk_label_clear_select_info   (GtkLabel *label);
284 static void gtk_label_update_cursor       (GtkLabel *label);
285 static void gtk_label_clear_layout        (GtkLabel *label);
286 static void gtk_label_ensure_layout       (GtkLabel *label);
287 static void gtk_label_invalidate_wrap_width (GtkLabel *label);
288 static void gtk_label_select_region_index (GtkLabel *label,
289                                            gint      anchor_index,
290                                            gint      end_index);
291
292 static gboolean gtk_label_mnemonic_activate (GtkWidget         *widget,
293                                              gboolean           group_cycling);
294 static void     gtk_label_setup_mnemonic    (GtkLabel          *label,
295                                              guint              last_key);
296 static void     gtk_label_drag_data_get     (GtkWidget         *widget,
297                                              GdkDragContext    *context,
298                                              GtkSelectionData  *selection_data,
299                                              guint              info,
300                                              guint              time);
301
302 static void     gtk_label_buildable_interface_init     (GtkBuildableIface *iface);
303 static gboolean gtk_label_buildable_custom_tag_start   (GtkBuildable     *buildable,
304                                                         GtkBuilder       *builder,
305                                                         GObject          *child,
306                                                         const gchar      *tagname,
307                                                         GMarkupParser    *parser,
308                                                         gpointer         *data);
309
310 static void     gtk_label_buildable_custom_finished    (GtkBuildable     *buildable,
311                                                         GtkBuilder       *builder,
312                                                         GObject          *child,
313                                                         const gchar      *tagname,
314                                                         gpointer          user_data);
315
316
317 static void connect_mnemonics_visible_notify    (GtkLabel   *label);
318 static gboolean      separate_uline_pattern     (const gchar  *str,
319                                                  guint        *accel_key,
320                                                  gchar       **new_str,
321                                                  gchar       **pattern);
322
323
324 /* For selectable labels: */
325 static void gtk_label_move_cursor        (GtkLabel        *label,
326                                           GtkMovementStep  step,
327                                           gint             count,
328                                           gboolean         extend_selection);
329 static void gtk_label_copy_clipboard     (GtkLabel        *label);
330 static void gtk_label_select_all         (GtkLabel        *label);
331 static void gtk_label_do_popup           (GtkLabel        *label,
332                                           GdkEventButton  *event);
333 static gint gtk_label_move_forward_word  (GtkLabel        *label,
334                                           gint             start);
335 static gint gtk_label_move_backward_word (GtkLabel        *label,
336                                           gint             start);
337
338 /* For links: */
339 static void          gtk_label_rescan_links     (GtkLabel  *label);
340 static void          gtk_label_clear_links      (GtkLabel  *label);
341 static gboolean      gtk_label_activate_link    (GtkLabel    *label,
342                                                  const gchar *uri);
343 static void          gtk_label_activate_current_link (GtkLabel *label);
344 static GtkLabelLink *gtk_label_get_current_link (GtkLabel  *label);
345 static void          gtk_label_get_link_colors  (GtkWidget  *widget,
346                                                  GdkColor  **link_color,
347                                                  GdkColor  **visited_link_color);
348 static void          emit_activate_link         (GtkLabel     *label,
349                                                  GtkLabelLink *link);
350
351 static GtkSizeRequestMode gtk_label_get_request_mode                (GtkWidget           *widget);
352 static void               gtk_label_get_preferred_width             (GtkWidget           *widget,
353                                                                      gint                *minimum_size,
354                                                                      gint                *natural_size);
355 static void               gtk_label_get_preferred_height            (GtkWidget           *widget,
356                                                                      gint                *minimum_size,
357                                                                      gint                *natural_size);
358 static void               gtk_label_get_preferred_width_for_height  (GtkWidget           *widget,
359                                                                      gint                 height,
360                                                                      gint                *minimum_width,
361                                                                      gint                *natural_width);
362 static void               gtk_label_get_preferred_height_for_width  (GtkWidget           *widget,
363                                                                      gint                 width,
364                                                                      gint                *minimum_height,
365                                                                      gint                *natural_height);
366
367 static GtkBuildableIface *buildable_parent_iface = NULL;
368
369 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
370                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
371                                                 gtk_label_buildable_interface_init))
372
373 static void
374 add_move_binding (GtkBindingSet  *binding_set,
375                   guint           keyval,
376                   guint           modmask,
377                   GtkMovementStep step,
378                   gint            count)
379 {
380   g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
381   
382   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
383                                 "move-cursor", 3,
384                                 G_TYPE_ENUM, step,
385                                 G_TYPE_INT, count,
386                                 G_TYPE_BOOLEAN, FALSE);
387
388   /* Selection-extending version */
389   gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
390                                 "move-cursor", 3,
391                                 G_TYPE_ENUM, step,
392                                 G_TYPE_INT, count,
393                                 G_TYPE_BOOLEAN, TRUE);
394 }
395
396 static void
397 gtk_label_class_init (GtkLabelClass *class)
398 {
399   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
400   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
401   GtkBindingSet *binding_set;
402
403   gobject_class->set_property = gtk_label_set_property;
404   gobject_class->get_property = gtk_label_get_property;
405   gobject_class->finalize = gtk_label_finalize;
406
407   widget_class->destroy = gtk_label_destroy;
408   widget_class->size_allocate = gtk_label_size_allocate;
409   widget_class->state_changed = gtk_label_state_changed;
410   widget_class->style_updated = gtk_label_style_updated;
411   widget_class->query_tooltip = gtk_label_query_tooltip;
412   widget_class->direction_changed = gtk_label_direction_changed;
413   widget_class->draw = gtk_label_draw;
414   widget_class->realize = gtk_label_realize;
415   widget_class->unrealize = gtk_label_unrealize;
416   widget_class->map = gtk_label_map;
417   widget_class->unmap = gtk_label_unmap;
418   widget_class->button_press_event = gtk_label_button_press;
419   widget_class->button_release_event = gtk_label_button_release;
420   widget_class->motion_notify_event = gtk_label_motion;
421   widget_class->leave_notify_event = gtk_label_leave_notify;
422   widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
423   widget_class->screen_changed = gtk_label_screen_changed;
424   widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
425   widget_class->drag_data_get = gtk_label_drag_data_get;
426   widget_class->grab_focus = gtk_label_grab_focus;
427   widget_class->popup_menu = gtk_label_popup_menu;
428   widget_class->focus = gtk_label_focus;
429   widget_class->get_request_mode = gtk_label_get_request_mode;
430   widget_class->get_preferred_width = gtk_label_get_preferred_width;
431   widget_class->get_preferred_height = gtk_label_get_preferred_height;
432   widget_class->get_preferred_width_for_height = gtk_label_get_preferred_width_for_height;
433   widget_class->get_preferred_height_for_width = gtk_label_get_preferred_height_for_width;
434
435   class->move_cursor = gtk_label_move_cursor;
436   class->copy_clipboard = gtk_label_copy_clipboard;
437   class->activate_link = gtk_label_activate_link;
438
439   /**
440    * GtkLabel::move-cursor:
441    * @entry: the object which received the signal
442    * @step: the granularity of the move, as a #GtkMovementStep
443    * @count: the number of @step units to move
444    * @extend_selection: %TRUE if the move should extend the selection
445    *
446    * The ::move-cursor signal is a
447    * <link linkend="keybinding-signals">keybinding signal</link>
448    * which gets emitted when the user initiates a cursor movement.
449    * If the cursor is not visible in @entry, this signal causes
450    * the viewport to be moved instead.
451    *
452    * Applications should not connect to it, but may emit it with
453    * g_signal_emit_by_name() if they need to control the cursor
454    * programmatically.
455    *
456    * The default bindings for this signal come in two variants,
457    * the variant with the Shift modifier extends the selection,
458    * the variant without the Shift modifer does not.
459    * There are too many key combinations to list them all here.
460    * <itemizedlist>
461    * <listitem>Arrow keys move by individual characters/lines</listitem>
462    * <listitem>Ctrl-arrow key combinations move by words/paragraphs</listitem>
463    * <listitem>Home/End keys move to the ends of the buffer</listitem>
464    * </itemizedlist>
465    */
466   signals[MOVE_CURSOR] = 
467     g_signal_new (I_("move-cursor"),
468                   G_OBJECT_CLASS_TYPE (gobject_class),
469                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
470                   G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
471                   NULL, NULL,
472                   _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
473                   G_TYPE_NONE, 3,
474                   GTK_TYPE_MOVEMENT_STEP,
475                   G_TYPE_INT,
476                   G_TYPE_BOOLEAN);
477
478    /**
479    * GtkLabel::copy-clipboard:
480    * @label: the object which received the signal
481    *
482    * The ::copy-clipboard signal is a
483    * <link linkend="keybinding-signals">keybinding signal</link>
484    * which gets emitted to copy the selection to the clipboard.
485    *
486    * The default binding for this signal is Ctrl-c.
487    */ 
488   signals[COPY_CLIPBOARD] =
489     g_signal_new (I_("copy-clipboard"),
490                   G_OBJECT_CLASS_TYPE (gobject_class),
491                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
492                   G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
493                   NULL, NULL,
494                   _gtk_marshal_VOID__VOID,
495                   G_TYPE_NONE, 0);
496   
497   /**
498    * GtkLabel::populate-popup:
499    * @label: The label on which the signal is emitted
500    * @menu: the menu that is being populated
501    *
502    * The ::populate-popup signal gets emitted before showing the
503    * context menu of the label. Note that only selectable labels
504    * have context menus.
505    *
506    * If you need to add items to the context menu, connect
507    * to this signal and append your menuitems to the @menu.
508    */
509   signals[POPULATE_POPUP] =
510     g_signal_new (I_("populate-popup"),
511                   G_OBJECT_CLASS_TYPE (gobject_class),
512                   G_SIGNAL_RUN_LAST,
513                   G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
514                   NULL, NULL,
515                   _gtk_marshal_VOID__OBJECT,
516                   G_TYPE_NONE, 1,
517                   GTK_TYPE_MENU);
518
519     /**
520      * GtkLabel::activate-current-link:
521      * @label: The label on which the signal was emitted
522      *
523      * A <link linkend="keybinding-signals">keybinding signal</link>
524      * which gets emitted when the user activates a link in the label.
525      *
526      * Applications may also emit the signal with g_signal_emit_by_name()
527      * if they need to control activation of URIs programmatically.
528      *
529      * The default bindings for this signal are all forms of the Enter key.
530      *
531      * Since: 2.18
532      */
533     signals[ACTIVATE_CURRENT_LINK] =
534       g_signal_new_class_handler ("activate-current-link",
535                                   G_TYPE_FROM_CLASS (gobject_class),
536                                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
537                                   G_CALLBACK (gtk_label_activate_current_link),
538                                   NULL, NULL,
539                                   _gtk_marshal_VOID__VOID,
540                                   G_TYPE_NONE, 0);
541
542     /**
543      * GtkLabel::activate-link:
544      * @label: The label on which the signal was emitted
545      * @uri: the URI that is activated
546      *
547      * The signal which gets emitted to activate a URI.
548      * Applications may connect to it to override the default behaviour,
549      * which is to call gtk_show_uri().
550      *
551      * Returns: %TRUE if the link has been activated
552      *
553      * Since: 2.18
554      */
555     signals[ACTIVATE_LINK] =
556       g_signal_new ("activate-link",
557                     G_TYPE_FROM_CLASS (gobject_class),
558                     G_SIGNAL_RUN_LAST,
559                     G_STRUCT_OFFSET (GtkLabelClass, activate_link),
560                     _gtk_boolean_handled_accumulator, NULL,
561                     _gtk_marshal_BOOLEAN__STRING,
562                     G_TYPE_BOOLEAN, 1, G_TYPE_STRING);
563
564   g_object_class_install_property (gobject_class,
565                                    PROP_LABEL,
566                                    g_param_spec_string ("label",
567                                                         P_("Label"),
568                                                         P_("The text of the label"),
569                                                         "",
570                                                         GTK_PARAM_READWRITE));
571   g_object_class_install_property (gobject_class,
572                                    PROP_ATTRIBUTES,
573                                    g_param_spec_boxed ("attributes",
574                                                        P_("Attributes"),
575                                                        P_("A list of style attributes to apply to the text of the label"),
576                                                        PANGO_TYPE_ATTR_LIST,
577                                                        GTK_PARAM_READWRITE));
578   g_object_class_install_property (gobject_class,
579                                    PROP_USE_MARKUP,
580                                    g_param_spec_boolean ("use-markup",
581                                                          P_("Use markup"),
582                                                          P_("The text of the label includes XML markup. See pango_parse_markup()"),
583                                                         FALSE,
584                                                         GTK_PARAM_READWRITE));
585   g_object_class_install_property (gobject_class,
586                                    PROP_USE_UNDERLINE,
587                                    g_param_spec_boolean ("use-underline",
588                                                          P_("Use underline"),
589                                                          P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
590                                                         FALSE,
591                                                         GTK_PARAM_READWRITE));
592
593   g_object_class_install_property (gobject_class,
594                                    PROP_JUSTIFY,
595                                    g_param_spec_enum ("justify",
596                                                       P_("Justification"),
597                                                       P_("The alignment of the lines in the text of the label relative to each other. This does NOT affect the alignment of the label within its allocation. See GtkMisc::xalign for that"),
598                                                       GTK_TYPE_JUSTIFICATION,
599                                                       GTK_JUSTIFY_LEFT,
600                                                       GTK_PARAM_READWRITE));
601
602   g_object_class_install_property (gobject_class,
603                                    PROP_PATTERN,
604                                    g_param_spec_string ("pattern",
605                                                         P_("Pattern"),
606                                                         P_("A string with _ characters in positions correspond to characters in the text to underline"),
607                                                         NULL,
608                                                         GTK_PARAM_WRITABLE));
609
610   g_object_class_install_property (gobject_class,
611                                    PROP_WRAP,
612                                    g_param_spec_boolean ("wrap",
613                                                         P_("Line wrap"),
614                                                         P_("If set, wrap lines if the text becomes too wide"),
615                                                         FALSE,
616                                                         GTK_PARAM_READWRITE));
617   /**
618    * GtkLabel:wrap-mode:
619    *
620    * If line wrapping is on (see the #GtkLabel:wrap property) this controls 
621    * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which 
622    * means wrap on word boundaries.
623    *
624    * Since: 2.10
625    */
626   g_object_class_install_property (gobject_class,
627                                    PROP_WRAP_MODE,
628                                    g_param_spec_enum ("wrap-mode",
629                                                       P_("Line wrap mode"),
630                                                       P_("If wrap is set, controls how linewrapping is done"),
631                                                       PANGO_TYPE_WRAP_MODE,
632                                                       PANGO_WRAP_WORD,
633                                                       GTK_PARAM_READWRITE));
634   g_object_class_install_property (gobject_class,
635                                    PROP_SELECTABLE,
636                                    g_param_spec_boolean ("selectable",
637                                                         P_("Selectable"),
638                                                         P_("Whether the label text can be selected with the mouse"),
639                                                         FALSE,
640                                                         GTK_PARAM_READWRITE));
641   g_object_class_install_property (gobject_class,
642                                    PROP_MNEMONIC_KEYVAL,
643                                    g_param_spec_uint ("mnemonic-keyval",
644                                                       P_("Mnemonic key"),
645                                                       P_("The mnemonic accelerator key for this label"),
646                                                       0,
647                                                       G_MAXUINT,
648                                                       GDK_KEY_VoidSymbol,
649                                                       GTK_PARAM_READABLE));
650   g_object_class_install_property (gobject_class,
651                                    PROP_MNEMONIC_WIDGET,
652                                    g_param_spec_object ("mnemonic-widget",
653                                                         P_("Mnemonic widget"),
654                                                         P_("The widget to be activated when the label's mnemonic "
655                                                           "key is pressed"),
656                                                         GTK_TYPE_WIDGET,
657                                                         GTK_PARAM_READWRITE));
658
659   g_object_class_install_property (gobject_class,
660                                    PROP_CURSOR_POSITION,
661                                    g_param_spec_int ("cursor-position",
662                                                      P_("Cursor Position"),
663                                                      P_("The current position of the insertion cursor in chars"),
664                                                      0,
665                                                      G_MAXINT,
666                                                      0,
667                                                      GTK_PARAM_READABLE));
668   
669   g_object_class_install_property (gobject_class,
670                                    PROP_SELECTION_BOUND,
671                                    g_param_spec_int ("selection-bound",
672                                                      P_("Selection Bound"),
673                                                      P_("The position of the opposite end of the selection from the cursor in chars"),
674                                                      0,
675                                                      G_MAXINT,
676                                                      0,
677                                                      GTK_PARAM_READABLE));
678   
679   /**
680    * GtkLabel:ellipsize:
681    *
682    * The preferred place to ellipsize the string, if the label does 
683    * not have enough room to display the entire string, specified as a 
684    * #PangoEllisizeMode. 
685    *
686    * Note that setting this property to a value other than 
687    * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests 
688    * only enough space to display the ellipsis "...". In particular, this 
689    * means that ellipsizing labels do not work well in notebook tabs, unless 
690    * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
691    * to set a label's width are gtk_widget_set_size_request() and
692    * gtk_label_set_width_chars().
693    *
694    * Since: 2.6
695    */
696   g_object_class_install_property (gobject_class,
697                                    PROP_ELLIPSIZE,
698                                    g_param_spec_enum ("ellipsize",
699                                                       P_("Ellipsize"),
700                                                       P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
701                                                       PANGO_TYPE_ELLIPSIZE_MODE,
702                                                       PANGO_ELLIPSIZE_NONE,
703                                                       GTK_PARAM_READWRITE));
704
705   /**
706    * GtkLabel:width-chars:
707    *
708    * The desired width of the label, in characters. If this property is set to
709    * -1, the width will be calculated automatically.
710    *
711    * See the section on <link linkend="label-text-layout">text layout</link>
712    * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
713    * determine the width of ellipsized and wrapped labels.
714    *
715    * Since: 2.6
716    **/
717   g_object_class_install_property (gobject_class,
718                                    PROP_WIDTH_CHARS,
719                                    g_param_spec_int ("width-chars",
720                                                      P_("Width In Characters"),
721                                                      P_("The desired width of the label, in characters"),
722                                                      -1,
723                                                      G_MAXINT,
724                                                      -1,
725                                                      GTK_PARAM_READWRITE));
726   
727   /**
728    * GtkLabel:single-line-mode:
729    * 
730    * Whether the label is in single line mode. In single line mode,
731    * the height of the label does not depend on the actual text, it
732    * is always set to ascent + descent of the font. This can be an
733    * advantage in situations where resizing the label because of text 
734    * changes would be distracting, e.g. in a statusbar.
735    *
736    * Since: 2.6
737    **/
738   g_object_class_install_property (gobject_class,
739                                    PROP_SINGLE_LINE_MODE,
740                                    g_param_spec_boolean ("single-line-mode",
741                                                         P_("Single Line Mode"),
742                                                         P_("Whether the label is in single line mode"),
743                                                         FALSE,
744                                                         GTK_PARAM_READWRITE));
745
746   /**
747    * GtkLabel:angle:
748    * 
749    * The angle that the baseline of the label makes with the horizontal,
750    * in degrees, measured counterclockwise. An angle of 90 reads from
751    * from bottom to top, an angle of 270, from top to bottom. Ignored
752    * if the label is selectable, wrapped, or ellipsized.
753    *
754    * Since: 2.6
755    **/
756   g_object_class_install_property (gobject_class,
757                                    PROP_ANGLE,
758                                    g_param_spec_double ("angle",
759                                                         P_("Angle"),
760                                                         P_("Angle at which the label is rotated"),
761                                                         0.0,
762                                                         360.0,
763                                                         0.0, 
764                                                         GTK_PARAM_READWRITE));
765   
766   /**
767    * GtkLabel:max-width-chars:
768    * 
769    * The desired maximum width of the label, in characters. If this property 
770    * is set to -1, the width will be calculated automatically.
771    *
772    * See the section on <link linkend="label-text-layout">text layout</link>
773    * for details of how #GtkLabel:width-chars and #GtkLabel:max-width-chars
774    * determine the width of ellipsized and wrapped labels.
775    *
776    * Since: 2.6
777    **/
778   g_object_class_install_property (gobject_class,
779                                    PROP_MAX_WIDTH_CHARS,
780                                    g_param_spec_int ("max-width-chars",
781                                                      P_("Maximum Width In Characters"),
782                                                      P_("The desired maximum width of the label, in characters"),
783                                                      -1,
784                                                      G_MAXINT,
785                                                      -1,
786                                                      GTK_PARAM_READWRITE));
787
788   /**
789    * GtkLabel:track-visited-links:
790    *
791    * Set this property to %TRUE to make the label track which links
792    * have been clicked. It will then apply the ::visited-link-color
793    * color, instead of ::link-color.
794    *
795    * Since: 2.18
796    */
797   g_object_class_install_property (gobject_class,
798                                    PROP_TRACK_VISITED_LINKS,
799                                    g_param_spec_boolean ("track-visited-links",
800                                                          P_("Track visited links"),
801                                                          P_("Whether visited links should be tracked"),
802                                                          TRUE,
803                                                          GTK_PARAM_READWRITE));
804   /*
805    * Key bindings
806    */
807
808   binding_set = gtk_binding_set_by_class (class);
809
810   /* Moving the insertion point */
811   add_move_binding (binding_set, GDK_KEY_Right, 0,
812                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
813   
814   add_move_binding (binding_set, GDK_KEY_Left, 0,
815                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
816
817   add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
818                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
819   
820   add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
821                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
822   
823   add_move_binding (binding_set, GDK_KEY_f, GDK_CONTROL_MASK,
824                     GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
825   
826   add_move_binding (binding_set, GDK_KEY_b, GDK_CONTROL_MASK,
827                     GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
828   
829   add_move_binding (binding_set, GDK_KEY_Right, GDK_CONTROL_MASK,
830                     GTK_MOVEMENT_WORDS, 1);
831
832   add_move_binding (binding_set, GDK_KEY_Left, GDK_CONTROL_MASK,
833                     GTK_MOVEMENT_WORDS, -1);
834
835   add_move_binding (binding_set, GDK_KEY_KP_Right, GDK_CONTROL_MASK,
836                     GTK_MOVEMENT_WORDS, 1);
837
838   add_move_binding (binding_set, GDK_KEY_KP_Left, GDK_CONTROL_MASK,
839                     GTK_MOVEMENT_WORDS, -1);
840
841   /* select all */
842   gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
843                                 "move-cursor", 3,
844                                 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
845                                 G_TYPE_INT, -1,
846                                 G_TYPE_BOOLEAN, FALSE);
847
848   gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
849                                 "move-cursor", 3,
850                                 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
851                                 G_TYPE_INT, 1,
852                                 G_TYPE_BOOLEAN, TRUE);
853
854   gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
855                                 "move-cursor", 3,
856                                 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
857                                 G_TYPE_INT, -1,
858                                 G_TYPE_BOOLEAN, FALSE);
859
860   gtk_binding_entry_add_signal (binding_set, GDK_KEY_slash, GDK_CONTROL_MASK,
861                                 "move-cursor", 3,
862                                 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
863                                 G_TYPE_INT, 1,
864                                 G_TYPE_BOOLEAN, TRUE);
865
866   /* unselect all */
867   gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
868                                 "move-cursor", 3,
869                                 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
870                                 G_TYPE_INT, 0,
871                                 G_TYPE_BOOLEAN, FALSE);
872
873   gtk_binding_entry_add_signal (binding_set, GDK_KEY_backslash, GDK_CONTROL_MASK,
874                                 "move-cursor", 3,
875                                 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
876                                 G_TYPE_INT, 0,
877                                 G_TYPE_BOOLEAN, FALSE);
878
879   add_move_binding (binding_set, GDK_KEY_f, GDK_MOD1_MASK,
880                     GTK_MOVEMENT_WORDS, 1);
881
882   add_move_binding (binding_set, GDK_KEY_b, GDK_MOD1_MASK,
883                     GTK_MOVEMENT_WORDS, -1);
884
885   add_move_binding (binding_set, GDK_KEY_Home, 0,
886                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
887
888   add_move_binding (binding_set, GDK_KEY_End, 0,
889                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
890
891   add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
892                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
893
894   add_move_binding (binding_set, GDK_KEY_KP_End, 0,
895                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
896   
897   add_move_binding (binding_set, GDK_KEY_Home, GDK_CONTROL_MASK,
898                     GTK_MOVEMENT_BUFFER_ENDS, -1);
899
900   add_move_binding (binding_set, GDK_KEY_End, GDK_CONTROL_MASK,
901                     GTK_MOVEMENT_BUFFER_ENDS, 1);
902
903   add_move_binding (binding_set, GDK_KEY_KP_Home, GDK_CONTROL_MASK,
904                     GTK_MOVEMENT_BUFFER_ENDS, -1);
905
906   add_move_binding (binding_set, GDK_KEY_KP_End, GDK_CONTROL_MASK,
907                     GTK_MOVEMENT_BUFFER_ENDS, 1);
908
909   /* copy */
910   gtk_binding_entry_add_signal (binding_set, GDK_KEY_c, GDK_CONTROL_MASK,
911                                 "copy-clipboard", 0);
912
913   gtk_binding_entry_add_signal (binding_set, GDK_KEY_Return, 0,
914                                 "activate-current-link", 0);
915   gtk_binding_entry_add_signal (binding_set, GDK_KEY_ISO_Enter, 0,
916                                 "activate-current-link", 0);
917   gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Enter, 0,
918                                 "activate-current-link", 0);
919
920   g_type_class_add_private (class, sizeof (GtkLabelPrivate));
921 }
922
923 static void 
924 gtk_label_set_property (GObject      *object,
925                         guint         prop_id,
926                         const GValue *value,
927                         GParamSpec   *pspec)
928 {
929   GtkLabel *label = GTK_LABEL (object);
930
931   switch (prop_id)
932     {
933     case PROP_LABEL:
934       gtk_label_set_label (label, g_value_get_string (value));
935       break;
936     case PROP_ATTRIBUTES:
937       gtk_label_set_attributes (label, g_value_get_boxed (value));
938       break;
939     case PROP_USE_MARKUP:
940       gtk_label_set_use_markup (label, g_value_get_boolean (value));
941       break;
942     case PROP_USE_UNDERLINE:
943       gtk_label_set_use_underline (label, g_value_get_boolean (value));
944       break;
945     case PROP_JUSTIFY:
946       gtk_label_set_justify (label, g_value_get_enum (value));
947       break;
948     case PROP_PATTERN:
949       gtk_label_set_pattern (label, g_value_get_string (value));
950       break;
951     case PROP_WRAP:
952       gtk_label_set_line_wrap (label, g_value_get_boolean (value));
953       break;      
954     case PROP_WRAP_MODE:
955       gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
956       break;      
957     case PROP_SELECTABLE:
958       gtk_label_set_selectable (label, g_value_get_boolean (value));
959       break;      
960     case PROP_MNEMONIC_WIDGET:
961       gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
962       break;
963     case PROP_ELLIPSIZE:
964       gtk_label_set_ellipsize (label, g_value_get_enum (value));
965       break;
966     case PROP_WIDTH_CHARS:
967       gtk_label_set_width_chars (label, g_value_get_int (value));
968       break;
969     case PROP_SINGLE_LINE_MODE:
970       gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
971       break;      
972     case PROP_ANGLE:
973       gtk_label_set_angle (label, g_value_get_double (value));
974       break;
975     case PROP_MAX_WIDTH_CHARS:
976       gtk_label_set_max_width_chars (label, g_value_get_int (value));
977       break;
978     case PROP_TRACK_VISITED_LINKS:
979       gtk_label_set_track_visited_links (label, g_value_get_boolean (value));
980       break;
981     default:
982       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
983       break;
984     }
985 }
986
987 static void 
988 gtk_label_get_property (GObject     *object,
989                         guint        prop_id,
990                         GValue      *value,
991                         GParamSpec  *pspec)
992 {
993   GtkLabel *label = GTK_LABEL (object);
994   GtkLabelPrivate *priv = label->priv;
995
996   switch (prop_id)
997     {
998     case PROP_LABEL:
999       g_value_set_string (value, priv->label);
1000       break;
1001     case PROP_ATTRIBUTES:
1002       g_value_set_boxed (value, priv->attrs);
1003       break;
1004     case PROP_USE_MARKUP:
1005       g_value_set_boolean (value, priv->use_markup);
1006       break;
1007     case PROP_USE_UNDERLINE:
1008       g_value_set_boolean (value, priv->use_underline);
1009       break;
1010     case PROP_JUSTIFY:
1011       g_value_set_enum (value, priv->jtype);
1012       break;
1013     case PROP_WRAP:
1014       g_value_set_boolean (value, priv->wrap);
1015       break;
1016     case PROP_WRAP_MODE:
1017       g_value_set_enum (value, priv->wrap_mode);
1018       break;
1019     case PROP_SELECTABLE:
1020       g_value_set_boolean (value, gtk_label_get_selectable (label));
1021       break;
1022     case PROP_MNEMONIC_KEYVAL:
1023       g_value_set_uint (value, priv->mnemonic_keyval);
1024       break;
1025     case PROP_MNEMONIC_WIDGET:
1026       g_value_set_object (value, (GObject*) priv->mnemonic_widget);
1027       break;
1028     case PROP_CURSOR_POSITION:
1029       if (priv->select_info && priv->select_info->selectable)
1030         {
1031           gint offset = g_utf8_pointer_to_offset (priv->text,
1032                                                   priv->text + priv->select_info->selection_end);
1033           g_value_set_int (value, offset);
1034         }
1035       else
1036         g_value_set_int (value, 0);
1037       break;
1038     case PROP_SELECTION_BOUND:
1039       if (priv->select_info && priv->select_info->selectable)
1040         {
1041           gint offset = g_utf8_pointer_to_offset (priv->text,
1042                                                   priv->text + priv->select_info->selection_anchor);
1043           g_value_set_int (value, offset);
1044         }
1045       else
1046         g_value_set_int (value, 0);
1047       break;
1048     case PROP_ELLIPSIZE:
1049       g_value_set_enum (value, priv->ellipsize);
1050       break;
1051     case PROP_WIDTH_CHARS:
1052       g_value_set_int (value, gtk_label_get_width_chars (label));
1053       break;
1054     case PROP_SINGLE_LINE_MODE:
1055       g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
1056       break;
1057     case PROP_ANGLE:
1058       g_value_set_double (value, gtk_label_get_angle (label));
1059       break;
1060     case PROP_MAX_WIDTH_CHARS:
1061       g_value_set_int (value, gtk_label_get_max_width_chars (label));
1062       break;
1063     case PROP_TRACK_VISITED_LINKS:
1064       g_value_set_boolean (value, gtk_label_get_track_visited_links (label));
1065       break;
1066     default:
1067       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
1068       break;
1069     }
1070 }
1071
1072 static void
1073 gtk_label_init (GtkLabel *label)
1074 {
1075   GtkLabelPrivate *priv;
1076
1077   label->priv = G_TYPE_INSTANCE_GET_PRIVATE (label,
1078                                              GTK_TYPE_LABEL,
1079                                              GtkLabelPrivate);
1080   priv = label->priv;
1081
1082   gtk_widget_set_has_window (GTK_WIDGET (label), FALSE);
1083
1084   priv->width_chars = -1;
1085   priv->max_width_chars = -1;
1086   priv->wrap_width = -1;
1087   priv->label = NULL;
1088
1089   priv->jtype = GTK_JUSTIFY_LEFT;
1090   priv->wrap = FALSE;
1091   priv->wrap_mode = PANGO_WRAP_WORD;
1092   priv->ellipsize = PANGO_ELLIPSIZE_NONE;
1093
1094   priv->use_underline = FALSE;
1095   priv->use_markup = FALSE;
1096   priv->pattern_set = FALSE;
1097   priv->track_links = TRUE;
1098
1099   priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1100   priv->layout = NULL;
1101   priv->text = NULL;
1102   priv->attrs = NULL;
1103
1104   priv->mnemonic_widget = NULL;
1105   priv->mnemonic_window = NULL;
1106
1107   priv->mnemonics_visible = TRUE;
1108
1109   gtk_label_set_text (label, "");
1110 }
1111
1112
1113 static void
1114 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
1115 {
1116   buildable_parent_iface = g_type_interface_peek_parent (iface);
1117
1118   iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
1119   iface->custom_finished = gtk_label_buildable_custom_finished;
1120 }
1121
1122 typedef struct {
1123   GtkBuilder    *builder;
1124   GObject       *object;
1125   PangoAttrList *attrs;
1126 } PangoParserData;
1127
1128 static PangoAttribute *
1129 attribute_from_text (GtkBuilder   *builder,
1130                      const gchar  *name, 
1131                      const gchar  *value,
1132                      GError      **error)
1133 {
1134   PangoAttribute *attribute = NULL;
1135   PangoAttrType   type;
1136   PangoLanguage  *language;
1137   PangoFontDescription *font_desc;
1138   GdkColor       *color;
1139   GValue          val = { 0, };
1140
1141   if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
1142     return NULL;
1143
1144   type = g_value_get_enum (&val);
1145   g_value_unset (&val);
1146
1147   switch (type)
1148     {
1149       /* PangoAttrLanguage */
1150     case PANGO_ATTR_LANGUAGE:
1151       if ((language = pango_language_from_string (value)))
1152         {
1153           attribute = pango_attr_language_new (language);
1154           g_value_init (&val, G_TYPE_INT);
1155         }
1156       break;
1157       /* PangoAttrInt */
1158     case PANGO_ATTR_STYLE:
1159       if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
1160         attribute = pango_attr_style_new (g_value_get_enum (&val));
1161       break;
1162     case PANGO_ATTR_WEIGHT:
1163       if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
1164         attribute = pango_attr_weight_new (g_value_get_enum (&val));
1165       break;
1166     case PANGO_ATTR_VARIANT:
1167       if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
1168         attribute = pango_attr_variant_new (g_value_get_enum (&val));
1169       break;
1170     case PANGO_ATTR_STRETCH:
1171       if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
1172         attribute = pango_attr_stretch_new (g_value_get_enum (&val));
1173       break;
1174     case PANGO_ATTR_UNDERLINE:
1175       if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1176         attribute = pango_attr_underline_new (g_value_get_boolean (&val));
1177       break;
1178     case PANGO_ATTR_STRIKETHROUGH:      
1179       if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
1180         attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
1181       break;
1182     case PANGO_ATTR_GRAVITY:
1183       if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
1184         attribute = pango_attr_gravity_new (g_value_get_enum (&val));
1185       break;
1186     case PANGO_ATTR_GRAVITY_HINT:
1187       if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT, 
1188                                               value, &val, error))
1189         attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
1190       break;
1191       /* PangoAttrString */       
1192     case PANGO_ATTR_FAMILY:
1193       attribute = pango_attr_family_new (value);
1194       g_value_init (&val, G_TYPE_INT);
1195       break;
1196
1197       /* PangoAttrSize */         
1198     case PANGO_ATTR_SIZE:
1199       if (gtk_builder_value_from_string_type (builder, G_TYPE_INT, 
1200                                               value, &val, error))
1201         attribute = pango_attr_size_new (g_value_get_int (&val));
1202       break;
1203     case PANGO_ATTR_ABSOLUTE_SIZE:
1204       if (gtk_builder_value_from_string_type (builder, G_TYPE_INT, 
1205                                               value, &val, error))
1206         attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
1207       break;
1208     
1209       /* PangoAttrFontDesc */
1210     case PANGO_ATTR_FONT_DESC:
1211       if ((font_desc = pango_font_description_from_string (value)))
1212         {
1213           attribute = pango_attr_font_desc_new (font_desc);
1214           pango_font_description_free (font_desc);
1215           g_value_init (&val, G_TYPE_INT);
1216         }
1217       break;
1218
1219       /* PangoAttrColor */
1220     case PANGO_ATTR_FOREGROUND:
1221       if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR, 
1222                                               value, &val, error))
1223         {
1224           color = g_value_get_boxed (&val);
1225           attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
1226         }
1227       break;
1228     case PANGO_ATTR_BACKGROUND: 
1229       if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR, 
1230                                               value, &val, error))
1231         {
1232           color = g_value_get_boxed (&val);
1233           attribute = pango_attr_background_new (color->red, color->green, color->blue);
1234         }
1235       break;
1236     case PANGO_ATTR_UNDERLINE_COLOR:
1237       if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR, 
1238                                               value, &val, error))
1239         {
1240           color = g_value_get_boxed (&val);
1241           attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
1242         }
1243       break;
1244     case PANGO_ATTR_STRIKETHROUGH_COLOR:
1245       if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR, 
1246                                               value, &val, error))
1247         {
1248           color = g_value_get_boxed (&val);
1249           attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
1250         }
1251       break;
1252       
1253       /* PangoAttrShape */
1254     case PANGO_ATTR_SHAPE:
1255       /* Unsupported for now */
1256       break;
1257       /* PangoAttrFloat */
1258     case PANGO_ATTR_SCALE:
1259       if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE, 
1260                                               value, &val, error))
1261         attribute = pango_attr_scale_new (g_value_get_double (&val));
1262       break;
1263
1264     case PANGO_ATTR_INVALID:
1265     case PANGO_ATTR_LETTER_SPACING:
1266     case PANGO_ATTR_RISE:
1267     case PANGO_ATTR_FALLBACK:
1268     default:
1269       break;
1270     }
1271
1272   g_value_unset (&val);
1273
1274   return attribute;
1275 }
1276
1277
1278 static void
1279 pango_start_element (GMarkupParseContext *context,
1280                      const gchar         *element_name,
1281                      const gchar        **names,
1282                      const gchar        **values,
1283                      gpointer             user_data,
1284                      GError             **error)
1285 {
1286   PangoParserData *data = (PangoParserData*)user_data;
1287   GValue val = { 0, };
1288   guint i;
1289   gint line_number, char_number;
1290
1291   if (strcmp (element_name, "attribute") == 0)
1292     {
1293       PangoAttribute *attr = NULL;
1294       const gchar *name = NULL;
1295       const gchar *value = NULL;
1296       const gchar *start = NULL;
1297       const gchar *end = NULL;
1298       guint start_val = 0;
1299       guint end_val   = G_MAXUINT;
1300
1301       for (i = 0; names[i]; i++)
1302         {
1303           if (strcmp (names[i], "name") == 0)
1304             name = values[i];
1305           else if (strcmp (names[i], "value") == 0)
1306             value = values[i];
1307           else if (strcmp (names[i], "start") == 0)
1308             start = values[i];
1309           else if (strcmp (names[i], "end") == 0)
1310             end = values[i];
1311           else
1312             {
1313               g_markup_parse_context_get_position (context,
1314                                                    &line_number,
1315                                                    &char_number);
1316               g_set_error (error,
1317                            GTK_BUILDER_ERROR,
1318                            GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1319                            "%s:%d:%d '%s' is not a valid attribute of <%s>",
1320                            "<input>",
1321                            line_number, char_number, names[i], "attribute");
1322               return;
1323             }
1324         }
1325
1326       if (!name || !value)
1327         {
1328           g_markup_parse_context_get_position (context,
1329                                                &line_number,
1330                                                &char_number);
1331           g_set_error (error,
1332                        GTK_BUILDER_ERROR,
1333                        GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1334                        "%s:%d:%d <%s> requires attribute \"%s\"",
1335                        "<input>",
1336                        line_number, char_number, "attribute",
1337                        name ? "value" : "name");
1338           return;
1339         }
1340
1341       if (start)
1342         {
1343           if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT, 
1344                                                    start, &val, error))
1345             return;
1346           start_val = g_value_get_uint (&val);
1347           g_value_unset (&val);
1348         }
1349
1350       if (end)
1351         {
1352           if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT, 
1353                                                    end, &val, error))
1354             return;
1355           end_val = g_value_get_uint (&val);
1356           g_value_unset (&val);
1357         }
1358
1359       attr = attribute_from_text (data->builder, name, value, error);
1360       attr->start_index = start_val;
1361       attr->end_index   = end_val;
1362
1363       if (attr)
1364         {
1365           if (!data->attrs)
1366             data->attrs = pango_attr_list_new ();
1367
1368           pango_attr_list_insert (data->attrs, attr);
1369         }
1370     }
1371   else if (strcmp (element_name, "attributes") == 0)
1372     ;
1373   else
1374     g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1375 }
1376
1377 static const GMarkupParser pango_parser =
1378   {
1379     pango_start_element,
1380   };
1381
1382 static gboolean
1383 gtk_label_buildable_custom_tag_start (GtkBuildable     *buildable,
1384                                       GtkBuilder       *builder,
1385                                       GObject          *child,
1386                                       const gchar      *tagname,
1387                                       GMarkupParser    *parser,
1388                                       gpointer         *data)
1389 {
1390   if (buildable_parent_iface->custom_tag_start (buildable, builder, child, 
1391                                                 tagname, parser, data))
1392     return TRUE;
1393
1394   if (strcmp (tagname, "attributes") == 0)
1395     {
1396       PangoParserData *parser_data;
1397
1398       parser_data = g_slice_new0 (PangoParserData);
1399       parser_data->builder = g_object_ref (builder);
1400       parser_data->object = g_object_ref (buildable);
1401       *parser = pango_parser;
1402       *data = parser_data;
1403       return TRUE;
1404     }
1405   return FALSE;
1406 }
1407
1408 static void
1409 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1410                                      GtkBuilder   *builder,
1411                                      GObject      *child,
1412                                      const gchar  *tagname,
1413                                      gpointer      user_data)
1414 {
1415   PangoParserData *data;
1416
1417   buildable_parent_iface->custom_finished (buildable, builder, child, 
1418                                            tagname, user_data);
1419
1420   if (strcmp (tagname, "attributes") == 0)
1421     {
1422       data = (PangoParserData*)user_data;
1423
1424       if (data->attrs)
1425         {
1426           gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1427           pango_attr_list_unref (data->attrs);
1428         }
1429
1430       g_object_unref (data->object);
1431       g_object_unref (data->builder);
1432       g_slice_free (PangoParserData, data);
1433     }
1434 }
1435
1436
1437 /**
1438  * gtk_label_new:
1439  * @str: The text of the label
1440  *
1441  * Creates a new label with the given text inside it. You can
1442  * pass %NULL to get an empty label widget.
1443  *
1444  * Return value: the new #GtkLabel
1445  **/
1446 GtkWidget*
1447 gtk_label_new (const gchar *str)
1448 {
1449   GtkLabel *label;
1450   
1451   label = g_object_new (GTK_TYPE_LABEL, NULL);
1452
1453   if (str && *str)
1454     gtk_label_set_text (label, str);
1455   
1456   return GTK_WIDGET (label);
1457 }
1458
1459 /**
1460  * gtk_label_new_with_mnemonic:
1461  * @str: The text of the label, with an underscore in front of the
1462  *       mnemonic character
1463  *
1464  * Creates a new #GtkLabel, containing the text in @str.
1465  *
1466  * If characters in @str are preceded by an underscore, they are
1467  * underlined. If you need a literal underscore character in a label, use
1468  * '__' (two underscores). The first underlined character represents a 
1469  * keyboard accelerator called a mnemonic. The mnemonic key can be used 
1470  * to activate another widget, chosen automatically, or explicitly using
1471  * gtk_label_set_mnemonic_widget().
1472  * 
1473  * If gtk_label_set_mnemonic_widget() is not called, then the first 
1474  * activatable ancestor of the #GtkLabel will be chosen as the mnemonic 
1475  * widget. For instance, if the label is inside a button or menu item, 
1476  * the button or menu item will automatically become the mnemonic widget 
1477  * and be activated by the mnemonic.
1478  *
1479  * Return value: the new #GtkLabel
1480  **/
1481 GtkWidget*
1482 gtk_label_new_with_mnemonic (const gchar *str)
1483 {
1484   GtkLabel *label;
1485   
1486   label = g_object_new (GTK_TYPE_LABEL, NULL);
1487
1488   if (str && *str)
1489     gtk_label_set_text_with_mnemonic (label, str);
1490   
1491   return GTK_WIDGET (label);
1492 }
1493
1494 static gboolean
1495 gtk_label_mnemonic_activate (GtkWidget *widget,
1496                              gboolean   group_cycling)
1497 {
1498   GtkLabel *label = GTK_LABEL (widget);
1499   GtkLabelPrivate *priv = label->priv;
1500   GtkWidget *parent;
1501
1502   if (priv->mnemonic_widget)
1503     return gtk_widget_mnemonic_activate (priv->mnemonic_widget, group_cycling);
1504
1505   /* Try to find the widget to activate by traversing the
1506    * widget's ancestry.
1507    */
1508   parent = gtk_widget_get_parent (widget);
1509
1510   if (GTK_IS_NOTEBOOK (parent))
1511     return FALSE;
1512   
1513   while (parent)
1514     {
1515       if (gtk_widget_get_can_focus (parent) ||
1516           (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1517           GTK_IS_NOTEBOOK (gtk_widget_get_parent (parent)) ||
1518           GTK_IS_MENU_ITEM (parent))
1519         return gtk_widget_mnemonic_activate (parent, group_cycling);
1520       parent = gtk_widget_get_parent (parent);
1521     }
1522
1523   /* barf if there was nothing to activate */
1524   g_warning ("Couldn't find a target for a mnemonic activation.");
1525   gtk_widget_error_bell (widget);
1526
1527   return FALSE;
1528 }
1529
1530 static void
1531 gtk_label_setup_mnemonic (GtkLabel *label,
1532                           guint     last_key)
1533 {
1534   GtkLabelPrivate *priv = label->priv;
1535   GtkWidget *widget = GTK_WIDGET (label);
1536   GtkWidget *toplevel;
1537   GtkWidget *mnemonic_menu;
1538   
1539   mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1540   
1541   if (last_key != GDK_KEY_VoidSymbol)
1542     {
1543       if (priv->mnemonic_window)
1544         {
1545           gtk_window_remove_mnemonic  (priv->mnemonic_window,
1546                                        last_key,
1547                                        widget);
1548           priv->mnemonic_window = NULL;
1549         }
1550       if (mnemonic_menu)
1551         {
1552           _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1553                                            last_key,
1554                                            widget);
1555           mnemonic_menu = NULL;
1556         }
1557     }
1558   
1559   if (priv->mnemonic_keyval == GDK_KEY_VoidSymbol)
1560       goto done;
1561
1562   connect_mnemonics_visible_notify (GTK_LABEL (widget));
1563
1564   toplevel = gtk_widget_get_toplevel (widget);
1565   if (gtk_widget_is_toplevel (toplevel))
1566     {
1567       GtkWidget *menu_shell;
1568       
1569       menu_shell = gtk_widget_get_ancestor (widget,
1570                                             GTK_TYPE_MENU_SHELL);
1571
1572       if (menu_shell)
1573         {
1574           _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1575                                         priv->mnemonic_keyval,
1576                                         widget);
1577           mnemonic_menu = menu_shell;
1578         }
1579       
1580       if (!GTK_IS_MENU (menu_shell))
1581         {
1582           gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1583                                    priv->mnemonic_keyval,
1584                                    widget);
1585           priv->mnemonic_window = GTK_WINDOW (toplevel);
1586         }
1587     }
1588   
1589  done:
1590   g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1591 }
1592
1593 static void
1594 gtk_label_hierarchy_changed (GtkWidget *widget,
1595                              GtkWidget *old_toplevel)
1596 {
1597   GtkLabel *label = GTK_LABEL (widget);
1598   GtkLabelPrivate *priv = label->priv;
1599
1600   gtk_label_setup_mnemonic (label, priv->mnemonic_keyval);
1601 }
1602
1603 static void
1604 label_shortcut_setting_apply (GtkLabel *label)
1605 {
1606   gtk_label_recalculate (label);
1607   if (GTK_IS_ACCEL_LABEL (label))
1608     gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1609 }
1610
1611 static void
1612 label_shortcut_setting_traverse_container (GtkWidget *widget,
1613                                            gpointer   data)
1614 {
1615   if (GTK_IS_LABEL (widget))
1616     label_shortcut_setting_apply (GTK_LABEL (widget));
1617   else if (GTK_IS_CONTAINER (widget))
1618     gtk_container_forall (GTK_CONTAINER (widget),
1619                           label_shortcut_setting_traverse_container, data);
1620 }
1621
1622 static void
1623 label_shortcut_setting_changed (GtkSettings *settings)
1624 {
1625   GList *list, *l;
1626
1627   list = gtk_window_list_toplevels ();
1628
1629   for (l = list; l ; l = l->next)
1630     {
1631       GtkWidget *widget = l->data;
1632
1633       if (gtk_widget_get_settings (widget) == settings)
1634         gtk_container_forall (GTK_CONTAINER (widget),
1635                               label_shortcut_setting_traverse_container, NULL);
1636     }
1637
1638   g_list_free (list);
1639 }
1640
1641 static void
1642 mnemonics_visible_apply (GtkWidget *widget,
1643                          gboolean   mnemonics_visible)
1644 {
1645   GtkLabel *label = GTK_LABEL (widget);
1646   GtkLabelPrivate *priv = label->priv;
1647
1648   mnemonics_visible = mnemonics_visible != FALSE;
1649
1650   if (priv->mnemonics_visible != mnemonics_visible)
1651     {
1652       priv->mnemonics_visible = mnemonics_visible;
1653
1654       gtk_label_recalculate (label);
1655     }
1656 }
1657
1658 static void
1659 label_mnemonics_visible_traverse_container (GtkWidget *widget,
1660                                             gpointer   data)
1661 {
1662   gboolean mnemonics_visible = GPOINTER_TO_INT (data);
1663
1664   _gtk_label_mnemonics_visible_apply_recursively (widget, mnemonics_visible);
1665 }
1666
1667 void
1668 _gtk_label_mnemonics_visible_apply_recursively (GtkWidget *widget,
1669                                                 gboolean   mnemonics_visible)
1670 {
1671   if (GTK_IS_LABEL (widget))
1672     mnemonics_visible_apply (widget, mnemonics_visible);
1673   else if (GTK_IS_CONTAINER (widget))
1674     gtk_container_forall (GTK_CONTAINER (widget),
1675                           label_mnemonics_visible_traverse_container,
1676                           GINT_TO_POINTER (mnemonics_visible));
1677 }
1678
1679 static void
1680 label_mnemonics_visible_changed (GtkWindow  *window,
1681                                  GParamSpec *pspec,
1682                                  gpointer    data)
1683 {
1684   gboolean mnemonics_visible;
1685
1686   g_object_get (window, "mnemonics-visible", &mnemonics_visible, NULL);
1687
1688   gtk_container_forall (GTK_CONTAINER (window),
1689                         label_mnemonics_visible_traverse_container,
1690                         GINT_TO_POINTER (mnemonics_visible));
1691 }
1692
1693 static void
1694 gtk_label_screen_changed (GtkWidget *widget,
1695                           GdkScreen *old_screen)
1696 {
1697   GtkSettings *settings;
1698   gboolean shortcuts_connected;
1699
1700   if (!gtk_widget_has_screen (widget))
1701     return;
1702
1703   settings = gtk_widget_get_settings (widget);
1704
1705   shortcuts_connected =
1706     GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1707                                         "gtk-label-shortcuts-connected"));
1708
1709   if (! shortcuts_connected)
1710     {
1711       g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1712                         G_CALLBACK (label_shortcut_setting_changed),
1713                         NULL);
1714       g_signal_connect (settings, "notify::gtk-enable-accels",
1715                         G_CALLBACK (label_shortcut_setting_changed),
1716                         NULL);
1717
1718       g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1719                          GINT_TO_POINTER (TRUE));
1720     }
1721
1722   label_shortcut_setting_apply (GTK_LABEL (widget));
1723 }
1724
1725
1726 static void
1727 label_mnemonic_widget_weak_notify (gpointer      data,
1728                                    GObject      *where_the_object_was)
1729 {
1730   GtkLabel *label = data;
1731   GtkLabelPrivate *priv = label->priv;
1732
1733   priv->mnemonic_widget = NULL;
1734   g_object_notify (G_OBJECT (label), "mnemonic-widget");
1735 }
1736
1737 /**
1738  * gtk_label_set_mnemonic_widget:
1739  * @label: a #GtkLabel
1740  * @widget: (allow-none): the target #GtkWidget
1741  *
1742  * If the label has been set so that it has an mnemonic key (using
1743  * i.e. gtk_label_set_markup_with_mnemonic(),
1744  * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1745  * or the "use_underline" property) the label can be associated with a
1746  * widget that is the target of the mnemonic. When the label is inside
1747  * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1748  * automatically associated with the correct widget, but sometimes
1749  * (i.e. when the target is a #GtkEntry next to the label) you need to
1750  * set it explicitly using this function.
1751  *
1752  * The target widget will be accelerated by emitting the 
1753  * GtkWidget::mnemonic-activate signal on it. The default handler for 
1754  * this signal will activate the widget if there are no mnemonic collisions 
1755  * and toggle focus between the colliding widgets otherwise.
1756  **/
1757 void
1758 gtk_label_set_mnemonic_widget (GtkLabel  *label,
1759                                GtkWidget *widget)
1760 {
1761   GtkLabelPrivate *priv;
1762
1763   g_return_if_fail (GTK_IS_LABEL (label));
1764
1765   priv = label->priv;
1766
1767   if (widget)
1768     g_return_if_fail (GTK_IS_WIDGET (widget));
1769
1770   if (priv->mnemonic_widget)
1771     {
1772       gtk_widget_remove_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1773       g_object_weak_unref (G_OBJECT (priv->mnemonic_widget),
1774                            label_mnemonic_widget_weak_notify,
1775                            label);
1776     }
1777   priv->mnemonic_widget = widget;
1778   if (priv->mnemonic_widget)
1779     {
1780       g_object_weak_ref (G_OBJECT (priv->mnemonic_widget),
1781                          label_mnemonic_widget_weak_notify,
1782                          label);
1783       gtk_widget_add_mnemonic_label (priv->mnemonic_widget, GTK_WIDGET (label));
1784     }
1785   
1786   g_object_notify (G_OBJECT (label), "mnemonic-widget");
1787 }
1788
1789 /**
1790  * gtk_label_get_mnemonic_widget:
1791  * @label: a #GtkLabel
1792  *
1793  * Retrieves the target of the mnemonic (keyboard shortcut) of this
1794  * label. See gtk_label_set_mnemonic_widget().
1795  *
1796  * Return value: (transfer none): the target of the label's mnemonic,
1797  *     or %NULL if none has been set and the default algorithm will be used.
1798  **/
1799 GtkWidget *
1800 gtk_label_get_mnemonic_widget (GtkLabel *label)
1801 {
1802   g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1803
1804   return label->priv->mnemonic_widget;
1805 }
1806
1807 /**
1808  * gtk_label_get_mnemonic_keyval:
1809  * @label: a #GtkLabel
1810  *
1811  * If the label has been set so that it has an mnemonic key this function
1812  * returns the keyval used for the mnemonic accelerator. If there is no
1813  * mnemonic set up it returns #GDK_VoidSymbol.
1814  *
1815  * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1816  **/
1817 guint
1818 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1819 {
1820   g_return_val_if_fail (GTK_IS_LABEL (label), GDK_KEY_VoidSymbol);
1821
1822   return label->priv->mnemonic_keyval;
1823 }
1824
1825 static void
1826 gtk_label_set_text_internal (GtkLabel *label,
1827                              gchar    *str)
1828 {
1829   GtkLabelPrivate *priv = label->priv;
1830
1831   g_free (priv->text);
1832
1833   priv->text = str;
1834
1835   gtk_label_select_region_index (label, 0, 0);
1836 }
1837
1838 static void
1839 gtk_label_set_label_internal (GtkLabel *label,
1840                               gchar    *str)
1841 {
1842   GtkLabelPrivate *priv = label->priv;
1843
1844   g_free (priv->label);
1845
1846   priv->label = str;
1847
1848   g_object_notify (G_OBJECT (label), "label");
1849 }
1850
1851 static void
1852 gtk_label_set_use_markup_internal (GtkLabel *label,
1853                                    gboolean  val)
1854 {
1855   GtkLabelPrivate *priv = label->priv;
1856
1857   val = val != FALSE;
1858   if (priv->use_markup != val)
1859     {
1860       priv->use_markup = val;
1861
1862       g_object_notify (G_OBJECT (label), "use-markup");
1863     }
1864 }
1865
1866 static void
1867 gtk_label_set_use_underline_internal (GtkLabel *label,
1868                                       gboolean val)
1869 {
1870   GtkLabelPrivate *priv = label->priv;
1871
1872   val = val != FALSE;
1873   if (priv->use_underline != val)
1874     {
1875       priv->use_underline = val;
1876
1877       g_object_notify (G_OBJECT (label), "use-underline");
1878     }
1879 }
1880
1881 static void
1882 gtk_label_compose_effective_attrs (GtkLabel *label)
1883 {
1884   GtkLabelPrivate      *priv = label->priv;
1885   PangoAttrIterator *iter;
1886   PangoAttribute    *attr;
1887   GSList            *iter_attrs, *l;
1888
1889   if (priv->attrs)
1890     {
1891       if (priv->effective_attrs)
1892         {
1893           if ((iter = pango_attr_list_get_iterator (priv->attrs)))
1894             {
1895               do
1896                 {
1897                   iter_attrs = pango_attr_iterator_get_attrs (iter);
1898                   for (l = iter_attrs; l; l = l->next)
1899                     {
1900                       attr = l->data;
1901                       pango_attr_list_insert (priv->effective_attrs, attr);
1902                     }
1903                   g_slist_free (iter_attrs);
1904                 }
1905               while (pango_attr_iterator_next (iter));
1906               pango_attr_iterator_destroy (iter);
1907             }
1908         }
1909       else
1910         priv->effective_attrs =
1911           pango_attr_list_ref (priv->attrs);
1912     }
1913 }
1914
1915 static void
1916 gtk_label_set_attributes_internal (GtkLabel      *label,
1917                                    PangoAttrList *attrs)
1918 {
1919   GtkLabelPrivate *priv = label->priv;
1920
1921   if (attrs)
1922     pango_attr_list_ref (attrs);
1923
1924   if (priv->attrs)
1925     pango_attr_list_unref (priv->attrs);
1926   priv->attrs = attrs;
1927
1928   g_object_notify (G_OBJECT (label), "attributes");
1929 }
1930
1931
1932 /* Calculates text, attrs and mnemonic_keyval from
1933  * label, use_underline and use_markup
1934  */
1935 static void
1936 gtk_label_recalculate (GtkLabel *label)
1937 {
1938   GtkLabelPrivate *priv = label->priv;
1939   guint keyval = priv->mnemonic_keyval;
1940
1941   if (priv->use_markup)
1942     gtk_label_set_markup_internal (label, priv->label, priv->use_underline);
1943   else
1944     {
1945       if (priv->use_underline)
1946         gtk_label_set_uline_text_internal (label, priv->label);
1947       else
1948         {
1949           if (priv->effective_attrs)
1950             pango_attr_list_unref (priv->effective_attrs);
1951           priv->effective_attrs = NULL;
1952           gtk_label_set_text_internal (label, g_strdup (priv->label));
1953         }
1954     }
1955
1956   gtk_label_compose_effective_attrs (label);
1957
1958   if (!priv->use_underline)
1959     priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
1960
1961   if (keyval != priv->mnemonic_keyval)
1962     {
1963       gtk_label_setup_mnemonic (label, keyval);
1964       g_object_notify (G_OBJECT (label), "mnemonic-keyval");
1965     }
1966
1967   gtk_label_clear_layout (label);
1968   gtk_label_clear_select_info (label);
1969   gtk_widget_queue_resize (GTK_WIDGET (label));
1970 }
1971
1972 /**
1973  * gtk_label_set_text:
1974  * @label: a #GtkLabel
1975  * @str: The text you want to set
1976  *
1977  * Sets the text within the #GtkLabel widget. It overwrites any text that
1978  * was there before.  
1979  *
1980  * This will also clear any previously set mnemonic accelerators.
1981  **/
1982 void
1983 gtk_label_set_text (GtkLabel    *label,
1984                     const gchar *str)
1985 {
1986   g_return_if_fail (GTK_IS_LABEL (label));
1987   
1988   g_object_freeze_notify (G_OBJECT (label));
1989
1990   gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1991   gtk_label_set_use_markup_internal (label, FALSE);
1992   gtk_label_set_use_underline_internal (label, FALSE);
1993   
1994   gtk_label_recalculate (label);
1995
1996   g_object_thaw_notify (G_OBJECT (label));
1997 }
1998
1999 /**
2000  * gtk_label_set_attributes:
2001  * @label: a #GtkLabel
2002  * @attrs: a #PangoAttrList
2003  * 
2004  * Sets a #PangoAttrList; the attributes in the list are applied to the
2005  * label text. 
2006  *
2007  * <note><para>The attributes set with this function will be applied
2008  * and merged with any other attributes previously effected by way
2009  * of the #GtkLabel:use-underline or #GtkLabel:use-markup properties.
2010  * While it is not recommended to mix markup strings with manually set
2011  * attributes, if you must; know that the attributes will be applied
2012  * to the label after the markup string is parsed.</para></note>
2013  **/
2014 void
2015 gtk_label_set_attributes (GtkLabel         *label,
2016                           PangoAttrList    *attrs)
2017 {
2018   g_return_if_fail (GTK_IS_LABEL (label));
2019
2020   gtk_label_set_attributes_internal (label, attrs);
2021
2022   gtk_label_recalculate (label);
2023
2024   gtk_label_clear_layout (label);
2025   gtk_widget_queue_resize (GTK_WIDGET (label));
2026 }
2027
2028 /**
2029  * gtk_label_get_attributes:
2030  * @label: a #GtkLabel
2031  *
2032  * Gets the attribute list that was set on the label using
2033  * gtk_label_set_attributes(), if any. This function does
2034  * not reflect attributes that come from the labels markup
2035  * (see gtk_label_set_markup()). If you want to get the
2036  * effective attributes for the label, use
2037  * pango_layout_get_attribute (gtk_label_get_layout (label)).
2038  *
2039  * Return value: (transfer none): the attribute list, or %NULL
2040  *     if none was set.
2041  **/
2042 PangoAttrList *
2043 gtk_label_get_attributes (GtkLabel *label)
2044 {
2045   g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2046
2047   return label->priv->attrs;
2048 }
2049
2050 /**
2051  * gtk_label_set_label:
2052  * @label: a #GtkLabel
2053  * @str: the new text to set for the label
2054  *
2055  * Sets the text of the label. The label is interpreted as
2056  * including embedded underlines and/or Pango markup depending
2057  * on the values of the #GtkLabel:use-underline" and
2058  * #GtkLabel:use-markup properties.
2059  **/
2060 void
2061 gtk_label_set_label (GtkLabel    *label,
2062                      const gchar *str)
2063 {
2064   g_return_if_fail (GTK_IS_LABEL (label));
2065
2066   g_object_freeze_notify (G_OBJECT (label));
2067
2068   gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2069   gtk_label_recalculate (label);
2070
2071   g_object_thaw_notify (G_OBJECT (label));
2072 }
2073
2074 /**
2075  * gtk_label_get_label:
2076  * @label: a #GtkLabel
2077  *
2078  * Fetches the text from a label widget including any embedded
2079  * underlines indicating mnemonics and Pango markup. (See
2080  * gtk_label_get_text()).
2081  *
2082  * Return value: the text of the label widget. This string is
2083  *   owned by the widget and must not be modified or freed.
2084  **/
2085 G_CONST_RETURN gchar *
2086 gtk_label_get_label (GtkLabel *label)
2087 {
2088   g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2089
2090   return label->priv->label;
2091 }
2092
2093 typedef struct
2094 {
2095   GtkLabel *label;
2096   GList *links;
2097   GString *new_str;
2098   GdkColor *link_color;
2099   GdkColor *visited_link_color;
2100 } UriParserData;
2101
2102 static void
2103 start_element_handler (GMarkupParseContext  *context,
2104                        const gchar          *element_name,
2105                        const gchar         **attribute_names,
2106                        const gchar         **attribute_values,
2107                        gpointer              user_data,
2108                        GError              **error)
2109 {
2110   GtkLabelPrivate *priv;
2111   UriParserData *pdata = user_data;
2112
2113   if (strcmp (element_name, "a") == 0)
2114     {
2115       GtkLabelLink *link;
2116       const gchar *uri = NULL;
2117       const gchar *title = NULL;
2118       gboolean visited = FALSE;
2119       gint line_number;
2120       gint char_number;
2121       gint i;
2122       GdkColor *color = NULL;
2123
2124       g_markup_parse_context_get_position (context, &line_number, &char_number);
2125
2126       for (i = 0; attribute_names[i] != NULL; i++)
2127         {
2128           const gchar *attr = attribute_names[i];
2129
2130           if (strcmp (attr, "href") == 0)
2131             uri = attribute_values[i];
2132           else if (strcmp (attr, "title") == 0)
2133             title = attribute_values[i];
2134           else
2135             {
2136               g_set_error (error,
2137                            G_MARKUP_ERROR,
2138                            G_MARKUP_ERROR_UNKNOWN_ATTRIBUTE,
2139                            "Attribute '%s' is not allowed on the <a> tag "
2140                            "on line %d char %d",
2141                             attr, line_number, char_number);
2142               return;
2143             }
2144         }
2145
2146       if (uri == NULL)
2147         {
2148           g_set_error (error,
2149                        G_MARKUP_ERROR,
2150                        G_MARKUP_ERROR_INVALID_CONTENT,
2151                        "Attribute 'href' was missing on the <a> tag "
2152                        "on line %d char %d",
2153                        line_number, char_number);
2154           return;
2155         }
2156
2157       visited = FALSE;
2158       priv = pdata->label->priv;
2159       if (priv->track_links && priv->select_info)
2160         {
2161           GList *l;
2162           for (l = priv->select_info->links; l; l = l->next)
2163             {
2164               link = l->data;
2165               if (strcmp (uri, link->uri) == 0)
2166                 {
2167                   visited = link->visited;
2168                   break;
2169                 }
2170             }
2171         }
2172
2173       if (visited)
2174         color = pdata->visited_link_color;
2175       else
2176         color = pdata->link_color;
2177
2178       g_string_append_printf (pdata->new_str,
2179                               "<span color=\"#%04x%04x%04x\" underline=\"single\">",
2180                               color->red,
2181                               color->green,
2182                               color->blue);
2183
2184       link = g_new0 (GtkLabelLink, 1);
2185       link->uri = g_strdup (uri);
2186       link->title = g_strdup (title);
2187       link->visited = visited;
2188       pdata->links = g_list_append (pdata->links, link);
2189     }
2190   else
2191     {
2192       gint i;
2193
2194       g_string_append_c (pdata->new_str, '<');
2195       g_string_append (pdata->new_str, element_name);
2196
2197       for (i = 0; attribute_names[i] != NULL; i++)
2198         {
2199           const gchar *attr  = attribute_names[i];
2200           const gchar *value = attribute_values[i];
2201           gchar *newvalue;
2202
2203           newvalue = g_markup_escape_text (value, -1);
2204
2205           g_string_append_c (pdata->new_str, ' ');
2206           g_string_append (pdata->new_str, attr);
2207           g_string_append (pdata->new_str, "=\"");
2208           g_string_append (pdata->new_str, newvalue);
2209           g_string_append_c (pdata->new_str, '\"');
2210
2211           g_free (newvalue);
2212         }
2213       g_string_append_c (pdata->new_str, '>');
2214     }
2215 }
2216
2217 static void
2218 end_element_handler (GMarkupParseContext  *context,
2219                      const gchar          *element_name,
2220                      gpointer              user_data,
2221                      GError              **error)
2222 {
2223   UriParserData *pdata = user_data;
2224
2225   if (!strcmp (element_name, "a"))
2226     g_string_append (pdata->new_str, "</span>");
2227   else
2228     {
2229       g_string_append (pdata->new_str, "</");
2230       g_string_append (pdata->new_str, element_name);
2231       g_string_append_c (pdata->new_str, '>');
2232     }
2233 }
2234
2235 static void
2236 text_handler (GMarkupParseContext  *context,
2237               const gchar          *text,
2238               gsize                 text_len,
2239               gpointer              user_data,
2240               GError              **error)
2241 {
2242   UriParserData *pdata = user_data;
2243   gchar *newtext;
2244
2245   newtext = g_markup_escape_text (text, text_len);
2246   g_string_append (pdata->new_str, newtext);
2247   g_free (newtext);
2248 }
2249
2250 static const GMarkupParser markup_parser =
2251 {
2252   start_element_handler,
2253   end_element_handler,
2254   text_handler,
2255   NULL,
2256   NULL
2257 };
2258
2259 static gboolean
2260 xml_isspace (gchar c)
2261 {
2262   return (c == ' ' || c == '\t' || c == '\n' || c == '\r');
2263 }
2264
2265 static void
2266 link_free (GtkLabelLink *link)
2267 {
2268   g_free (link->uri);
2269   g_free (link->title);
2270   g_free (link);
2271 }
2272
2273 static void
2274 gtk_label_get_link_colors (GtkWidget  *widget,
2275                            GdkColor  **link_color,
2276                            GdkColor  **visited_link_color)
2277 {
2278   GtkStyleContext *context;
2279
2280   context = gtk_widget_get_style_context (widget);
2281   gtk_style_context_get_style (context,
2282                                "link-color", link_color,
2283                                "visited-link-color", visited_link_color,
2284                                 NULL);
2285   if (!*link_color)
2286     *link_color = gdk_color_copy (&default_link_color);
2287   if (!*visited_link_color)
2288     *visited_link_color = gdk_color_copy (&default_visited_link_color);
2289 }
2290
2291 static gboolean
2292 parse_uri_markup (GtkLabel     *label,
2293                   const gchar  *str,
2294                   gchar       **new_str,
2295                   GList       **links,
2296                   GError      **error)
2297 {
2298   GMarkupParseContext *context = NULL;
2299   const gchar *p, *end;
2300   gboolean needs_root = TRUE;
2301   gsize length;
2302   UriParserData pdata;
2303
2304   length = strlen (str);
2305   p = str;
2306   end = str + length;
2307
2308   pdata.label = label;
2309   pdata.links = NULL;
2310   pdata.new_str = g_string_sized_new (length);
2311
2312   gtk_label_get_link_colors (GTK_WIDGET (label), &pdata.link_color, &pdata.visited_link_color);
2313
2314   while (p != end && xml_isspace (*p))
2315     p++;
2316
2317   if (end - p >= 8 && strncmp (p, "<markup>", 8) == 0)
2318     needs_root = FALSE;
2319
2320   context = g_markup_parse_context_new (&markup_parser, 0, &pdata, NULL);
2321
2322   if (needs_root)
2323     {
2324       if (!g_markup_parse_context_parse (context, "<markup>", -1, error))
2325         goto failed;
2326     }
2327
2328   if (!g_markup_parse_context_parse (context, str, length, error))
2329     goto failed;
2330
2331   if (needs_root)
2332     {
2333       if (!g_markup_parse_context_parse (context, "</markup>", -1, error))
2334         goto failed;
2335     }
2336
2337   if (!g_markup_parse_context_end_parse (context, error))
2338     goto failed;
2339
2340   g_markup_parse_context_free (context);
2341
2342   *new_str = g_string_free (pdata.new_str, FALSE);
2343   *links = pdata.links;
2344
2345   gdk_color_free (pdata.link_color);
2346   gdk_color_free (pdata.visited_link_color);
2347
2348   return TRUE;
2349
2350 failed:
2351   g_markup_parse_context_free (context);
2352   g_string_free (pdata.new_str, TRUE);
2353   g_list_foreach (pdata.links, (GFunc)link_free, NULL);
2354   g_list_free (pdata.links);
2355   gdk_color_free (pdata.link_color);
2356   gdk_color_free (pdata.visited_link_color);
2357
2358   return FALSE;
2359 }
2360
2361 static void
2362 gtk_label_ensure_has_tooltip (GtkLabel *label)
2363 {
2364   GtkLabelPrivate *priv = label->priv;
2365   GList *l;
2366   gboolean has_tooltip = FALSE;
2367
2368   for (l = priv->select_info->links; l; l = l->next)
2369     {
2370       GtkLabelLink *link = l->data;
2371       if (link->title)
2372         {
2373           has_tooltip = TRUE;
2374           break;
2375         }
2376     }
2377
2378   gtk_widget_set_has_tooltip (GTK_WIDGET (label), has_tooltip);
2379 }
2380
2381 static void
2382 gtk_label_set_markup_internal (GtkLabel    *label,
2383                                const gchar *str,
2384                                gboolean     with_uline)
2385 {
2386   GtkLabelPrivate *priv = label->priv;
2387   gchar *text = NULL;
2388   GError *error = NULL;
2389   PangoAttrList *attrs = NULL;
2390   gunichar accel_char = 0;
2391   gchar *new_str;
2392   GList *links = NULL;
2393
2394   if (!parse_uri_markup (label, str, &new_str, &links, &error))
2395     {
2396       g_warning ("Failed to set text from markup due to error parsing markup: %s",
2397                  error->message);
2398       g_error_free (error);
2399       return;
2400     }
2401
2402   gtk_label_clear_links (label);
2403   if (links)
2404     {
2405       gtk_label_ensure_select_info (label);
2406       priv->select_info->links = links;
2407       gtk_label_ensure_has_tooltip (label);
2408     }
2409
2410   if (with_uline)
2411     {
2412       gboolean enable_mnemonics;
2413       gboolean auto_mnemonics;
2414
2415       g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2416                     "gtk-enable-mnemonics", &enable_mnemonics,
2417                     "gtk-auto-mnemonics", &auto_mnemonics,
2418                     NULL);
2419
2420       if (!(enable_mnemonics && priv->mnemonics_visible &&
2421             (!auto_mnemonics ||
2422              (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2423               (!priv->mnemonic_widget ||
2424                gtk_widget_is_sensitive (priv->mnemonic_widget))))))
2425         {
2426           gchar *tmp;
2427           gchar *pattern;
2428           guint key;
2429
2430           if (separate_uline_pattern (new_str, &key, &tmp, &pattern))
2431             {
2432               g_free (new_str);
2433               new_str = tmp;
2434               g_free (pattern);
2435             }
2436         }
2437     }
2438
2439   if (!pango_parse_markup (new_str,
2440                            -1,
2441                            with_uline ? '_' : 0,
2442                            &attrs,
2443                            &text,
2444                            with_uline ? &accel_char : NULL,
2445                            &error))
2446     {
2447       g_warning ("Failed to set text from markup due to error parsing markup: %s",
2448                  error->message);
2449       g_free (new_str);
2450       g_error_free (error);
2451       return;
2452     }
2453
2454   g_free (new_str);
2455
2456   if (text)
2457     gtk_label_set_text_internal (label, text);
2458
2459   if (attrs)
2460     {
2461       if (priv->effective_attrs)
2462         pango_attr_list_unref (priv->effective_attrs);
2463       priv->effective_attrs = attrs;
2464     }
2465
2466   if (accel_char != 0)
2467     priv->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
2468   else
2469     priv->mnemonic_keyval = GDK_KEY_VoidSymbol;
2470 }
2471
2472 /**
2473  * gtk_label_set_markup:
2474  * @label: a #GtkLabel
2475  * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
2476  * 
2477  * Parses @str which is marked up with the <link
2478  * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
2479  * label's text and attribute list based on the parse results. If the @str is
2480  * external data, you may need to escape it with g_markup_escape_text() or
2481  * g_markup_printf_escaped()<!-- -->:
2482  * |[
2483  * char *markup;
2484  *
2485  * markup = g_markup_printf_escaped ("&lt;span style=\"italic\"&gt;&percnt;s&lt;/span&gt;", str);
2486  * gtk_label_set_markup (GTK_LABEL (label), markup);
2487  * g_free (markup);
2488  * ]|
2489  **/
2490 void
2491 gtk_label_set_markup (GtkLabel    *label,
2492                       const gchar *str)
2493 {
2494   g_return_if_fail (GTK_IS_LABEL (label));
2495
2496   g_object_freeze_notify (G_OBJECT (label));
2497
2498   gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2499   gtk_label_set_use_markup_internal (label, TRUE);
2500   gtk_label_set_use_underline_internal (label, FALSE);
2501
2502   gtk_label_recalculate (label);
2503
2504   g_object_thaw_notify (G_OBJECT (label));
2505 }
2506
2507 /**
2508  * gtk_label_set_markup_with_mnemonic:
2509  * @label: a #GtkLabel
2510  * @str: a markup string (see
2511  *     <link linkend="PangoMarkupFormat">Pango markup format</link>)
2512  *
2513  * Parses @str which is marked up with the
2514  * <link linkend="PangoMarkupFormat">Pango text markup language</link>,
2515  * setting the label's text and attribute list based on the parse results.
2516  * If characters in @str are preceded by an underscore, they are underlined
2517  * indicating that they represent a keyboard accelerator called a mnemonic.
2518  *
2519  * The mnemonic key can be used to activate another widget, chosen
2520  * automatically, or explicitly using gtk_label_set_mnemonic_widget().
2521  */
2522 void
2523 gtk_label_set_markup_with_mnemonic (GtkLabel    *label,
2524                                     const gchar *str)
2525 {
2526   g_return_if_fail (GTK_IS_LABEL (label));
2527
2528   g_object_freeze_notify (G_OBJECT (label));
2529
2530   gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
2531   gtk_label_set_use_markup_internal (label, TRUE);
2532   gtk_label_set_use_underline_internal (label, TRUE);
2533
2534   gtk_label_recalculate (label);
2535
2536   g_object_thaw_notify (G_OBJECT (label));
2537 }
2538
2539 /**
2540  * gtk_label_get_text:
2541  * @label: a #GtkLabel
2542  * 
2543  * Fetches the text from a label widget, as displayed on the
2544  * screen. This does not include any embedded underlines
2545  * indicating mnemonics or Pango markup. (See gtk_label_get_label())
2546  * 
2547  * Return value: the text in the label widget. This is the internal
2548  *   string used by the label, and must not be modified.
2549  **/
2550 G_CONST_RETURN gchar *
2551 gtk_label_get_text (GtkLabel *label)
2552 {
2553   g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
2554
2555   return label->priv->text;
2556 }
2557
2558 static PangoAttrList *
2559 gtk_label_pattern_to_attrs (GtkLabel      *label,
2560                             const gchar   *pattern)
2561 {
2562   GtkLabelPrivate *priv = label->priv;
2563   const char *start;
2564   const char *p = priv->text;
2565   const char *q = pattern;
2566   PangoAttrList *attrs;
2567
2568   attrs = pango_attr_list_new ();
2569
2570   while (1)
2571     {
2572       while (*p && *q && *q != '_')
2573         {
2574           p = g_utf8_next_char (p);
2575           q++;
2576         }
2577       start = p;
2578       while (*p && *q && *q == '_')
2579         {
2580           p = g_utf8_next_char (p);
2581           q++;
2582         }
2583       
2584       if (p > start)
2585         {
2586           PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
2587           attr->start_index = start - priv->text;
2588           attr->end_index = p - priv->text;
2589           
2590           pango_attr_list_insert (attrs, attr);
2591         }
2592       else
2593         break;
2594     }
2595
2596   return attrs;
2597 }
2598
2599 static void
2600 gtk_label_set_pattern_internal (GtkLabel    *label,
2601                                 const gchar *pattern,
2602                                 gboolean     is_mnemonic)
2603 {
2604   GtkLabelPrivate *priv = label->priv;
2605   PangoAttrList *attrs;
2606   gboolean enable_mnemonics;
2607   gboolean auto_mnemonics;
2608
2609   if (priv->pattern_set)
2610     return;
2611
2612   if (is_mnemonic)
2613     {
2614       g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
2615                     "gtk-enable-mnemonics", &enable_mnemonics,
2616                     "gtk-auto-mnemonics", &auto_mnemonics,
2617                     NULL);
2618
2619       if (enable_mnemonics && priv->mnemonics_visible && pattern &&
2620           (!auto_mnemonics ||
2621            (gtk_widget_is_sensitive (GTK_WIDGET (label)) &&
2622             (!priv->mnemonic_widget ||
2623              gtk_widget_is_sensitive (priv->mnemonic_widget)))))
2624         attrs = gtk_label_pattern_to_attrs (label, pattern);
2625       else
2626         attrs = NULL;
2627     }
2628   else
2629     attrs = gtk_label_pattern_to_attrs (label, pattern);
2630
2631   if (priv->effective_attrs)
2632     pango_attr_list_unref (priv->effective_attrs);
2633   priv->effective_attrs = attrs;
2634 }
2635
2636 void
2637 gtk_label_set_pattern (GtkLabel    *label,
2638                        const gchar *pattern)
2639 {
2640   GtkLabelPrivate *priv = label->priv;
2641
2642   g_return_if_fail (GTK_IS_LABEL (label));
2643
2644   priv = label->priv;
2645
2646   priv->pattern_set = FALSE;
2647
2648   if (pattern)
2649     {
2650       gtk_label_set_pattern_internal (label, pattern, FALSE);
2651       priv->pattern_set = TRUE;
2652     }
2653   else
2654     gtk_label_recalculate (label);
2655
2656   gtk_label_clear_layout (label);
2657   gtk_widget_queue_resize (GTK_WIDGET (label));
2658 }
2659
2660
2661 /**
2662  * gtk_label_set_justify:
2663  * @label: a #GtkLabel
2664  * @jtype: a #GtkJustification
2665  *
2666  * Sets the alignment of the lines in the text of the label relative to
2667  * each other. %GTK_JUSTIFY_LEFT is the default value when the
2668  * widget is first created with gtk_label_new(). If you instead want
2669  * to set the alignment of the label as a whole, use
2670  * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
2671  * effect on labels containing only a single line.
2672  **/
2673 void
2674 gtk_label_set_justify (GtkLabel        *label,
2675                        GtkJustification jtype)
2676 {
2677   GtkLabelPrivate *priv;
2678
2679   g_return_if_fail (GTK_IS_LABEL (label));
2680   g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
2681
2682   priv = label->priv;
2683
2684   if ((GtkJustification) priv->jtype != jtype)
2685     {
2686       priv->jtype = jtype;
2687
2688       /* No real need to be this drastic, but easier than duplicating the code */
2689       gtk_label_clear_layout (label);
2690       
2691       g_object_notify (G_OBJECT (label), "justify");
2692       gtk_widget_queue_resize (GTK_WIDGET (label));
2693     }
2694 }
2695
2696 /**
2697  * gtk_label_get_justify:
2698  * @label: a #GtkLabel
2699  *
2700  * Returns the justification of the label. See gtk_label_set_justify().
2701  *
2702  * Return value: #GtkJustification
2703  **/
2704 GtkJustification
2705 gtk_label_get_justify (GtkLabel *label)
2706 {
2707   g_return_val_if_fail (GTK_IS_LABEL (label), 0);
2708
2709   return label->priv->jtype;
2710 }
2711
2712 /**
2713  * gtk_label_set_ellipsize:
2714  * @label: a #GtkLabel
2715  * @mode: a #PangoEllipsizeMode
2716  *
2717  * Sets the mode used to ellipsize (add an ellipsis: "...") to the text 
2718  * if there is not enough space to render the entire string.
2719  *
2720  * Since: 2.6
2721  **/
2722 void
2723 gtk_label_set_ellipsize (GtkLabel          *label,
2724                          PangoEllipsizeMode mode)
2725 {
2726   GtkLabelPrivate *priv;
2727
2728   g_return_if_fail (GTK_IS_LABEL (label));
2729   g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
2730
2731   priv = label->priv;
2732
2733   if ((PangoEllipsizeMode) priv->ellipsize != mode)
2734     {
2735       priv->ellipsize = mode;
2736
2737       /* No real need to be this drastic, but easier than duplicating the code */
2738       gtk_label_clear_layout (label);
2739       
2740       g_object_notify (G_OBJECT (label), "ellipsize");
2741       gtk_widget_queue_resize (GTK_WIDGET (label));
2742     }
2743 }
2744
2745 /**
2746  * gtk_label_get_ellipsize:
2747  * @label: a #GtkLabel
2748  *
2749  * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
2750  *
2751  * Return value: #PangoEllipsizeMode
2752  *
2753  * Since: 2.6
2754  **/
2755 PangoEllipsizeMode
2756 gtk_label_get_ellipsize (GtkLabel *label)
2757 {
2758   g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2759
2760   return label->priv->ellipsize;
2761 }
2762
2763 /**
2764  * gtk_label_set_width_chars:
2765  * @label: a #GtkLabel
2766  * @n_chars: the new desired width, in characters.
2767  * 
2768  * Sets the desired width in characters of @label to @n_chars.
2769  * 
2770  * Since: 2.6
2771  **/
2772 void
2773 gtk_label_set_width_chars (GtkLabel *label,
2774                            gint      n_chars)
2775 {
2776   GtkLabelPrivate *priv;
2777
2778   g_return_if_fail (GTK_IS_LABEL (label));
2779
2780   priv = label->priv;
2781
2782   if (priv->width_chars != n_chars)
2783     {
2784       priv->width_chars = n_chars;
2785       g_object_notify (G_OBJECT (label), "width-chars");
2786       gtk_label_invalidate_wrap_width (label);
2787       gtk_widget_queue_resize (GTK_WIDGET (label));
2788     }
2789 }
2790
2791 /**
2792  * gtk_label_get_width_chars:
2793  * @label: a #GtkLabel
2794  * 
2795  * Retrieves the desired width of @label, in characters. See
2796  * gtk_label_set_width_chars().
2797  * 
2798  * Return value: the width of the label in characters.
2799  * 
2800  * Since: 2.6
2801  **/
2802 gint
2803 gtk_label_get_width_chars (GtkLabel *label)
2804 {
2805   g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2806
2807   return label->priv->width_chars;
2808 }
2809
2810 /**
2811  * gtk_label_set_max_width_chars:
2812  * @label: a #GtkLabel
2813  * @n_chars: the new desired maximum width, in characters.
2814  * 
2815  * Sets the desired maximum width in characters of @label to @n_chars.
2816  * 
2817  * Since: 2.6
2818  **/
2819 void
2820 gtk_label_set_max_width_chars (GtkLabel *label,
2821                                gint      n_chars)
2822 {
2823   GtkLabelPrivate *priv;
2824
2825   g_return_if_fail (GTK_IS_LABEL (label));
2826
2827   priv = label->priv;
2828
2829   if (priv->max_width_chars != n_chars)
2830     {
2831       priv->max_width_chars = n_chars;
2832
2833       g_object_notify (G_OBJECT (label), "max-width-chars");
2834       gtk_label_invalidate_wrap_width (label);
2835       gtk_widget_queue_resize (GTK_WIDGET (label));
2836     }
2837 }
2838
2839 /**
2840  * gtk_label_get_max_width_chars:
2841  * @label: a #GtkLabel
2842  * 
2843  * Retrieves the desired maximum width of @label, in characters. See
2844  * gtk_label_set_width_chars().
2845  * 
2846  * Return value: the maximum width of the label in characters.
2847  * 
2848  * Since: 2.6
2849  **/
2850 gint
2851 gtk_label_get_max_width_chars (GtkLabel *label)
2852 {
2853   g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2854
2855   return label->priv->max_width_chars;
2856 }
2857
2858 /**
2859  * gtk_label_set_line_wrap:
2860  * @label: a #GtkLabel
2861  * @wrap: the setting
2862  *
2863  * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2864  * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2865  * by the edge of the widget if it exceeds the widget size.
2866  *
2867  * Note that setting line wrapping to %TRUE does not make the label
2868  * wrap at its parent container's width, because GTK+ widgets
2869  * conceptually can't make their requisition depend on the parent
2870  * container's size. For a label that wraps at a specific position,
2871  * set the label's width using gtk_widget_set_size_request().
2872  **/
2873 void
2874 gtk_label_set_line_wrap (GtkLabel *label,
2875                          gboolean  wrap)
2876 {
2877   GtkLabelPrivate *priv;
2878
2879   g_return_if_fail (GTK_IS_LABEL (label));
2880
2881   priv = label->priv;
2882
2883   wrap = wrap != FALSE;
2884
2885   if (priv->wrap != wrap)
2886     {
2887       priv->wrap = wrap;
2888
2889       gtk_label_clear_layout (label);
2890       gtk_widget_queue_resize (GTK_WIDGET (label));
2891       g_object_notify (G_OBJECT (label), "wrap");
2892     }
2893 }
2894
2895 /**
2896  * gtk_label_get_line_wrap:
2897  * @label: a #GtkLabel
2898  *
2899  * Returns whether lines in the label are automatically wrapped. 
2900  * See gtk_label_set_line_wrap().
2901  *
2902  * Return value: %TRUE if the lines of the label are automatically wrapped.
2903  */
2904 gboolean
2905 gtk_label_get_line_wrap (GtkLabel *label)
2906 {
2907   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2908
2909   return label->priv->wrap;
2910 }
2911
2912 /**
2913  * gtk_label_set_line_wrap_mode:
2914  * @label: a #GtkLabel
2915  * @wrap_mode: the line wrapping mode
2916  *
2917  * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
2918  * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
2919  * wrap on word boundaries.
2920  *
2921  * Since: 2.10
2922  **/
2923 void
2924 gtk_label_set_line_wrap_mode (GtkLabel *label,
2925                               PangoWrapMode wrap_mode)
2926 {
2927   GtkLabelPrivate *priv;
2928
2929   g_return_if_fail (GTK_IS_LABEL (label));
2930
2931   priv = label->priv;
2932
2933   if (priv->wrap_mode != wrap_mode)
2934     {
2935       priv->wrap_mode = wrap_mode;
2936       g_object_notify (G_OBJECT (label), "wrap-mode");
2937       
2938       gtk_widget_queue_resize (GTK_WIDGET (label));
2939     }
2940 }
2941
2942 /**
2943  * gtk_label_get_line_wrap_mode:
2944  * @label: a #GtkLabel
2945  *
2946  * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
2947  *
2948  * Return value: %TRUE if the lines of the label are automatically wrapped.
2949  *
2950  * Since: 2.10
2951  */
2952 PangoWrapMode
2953 gtk_label_get_line_wrap_mode (GtkLabel *label)
2954 {
2955   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2956
2957   return label->priv->wrap_mode;
2958 }
2959
2960 static void
2961 gtk_label_destroy (GtkWidget *widget)
2962 {
2963   GtkLabel *label = GTK_LABEL (widget);
2964
2965   gtk_label_set_mnemonic_widget (label, NULL);
2966
2967   GTK_WIDGET_CLASS (gtk_label_parent_class)->destroy (widget);
2968 }
2969
2970 static void
2971 gtk_label_finalize (GObject *object)
2972 {
2973   GtkLabel *label = GTK_LABEL (object);
2974   GtkLabelPrivate *priv = label->priv;
2975
2976   g_free (priv->label);
2977   g_free (priv->text);
2978
2979   if (priv->layout)
2980     g_object_unref (priv->layout);
2981
2982   if (priv->attrs)
2983     pango_attr_list_unref (priv->attrs);
2984
2985   if (priv->effective_attrs)
2986     pango_attr_list_unref (priv->effective_attrs);
2987
2988   gtk_label_clear_links (label);
2989   g_free (priv->select_info);
2990
2991   G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
2992 }
2993
2994 static void
2995 gtk_label_clear_layout (GtkLabel *label)
2996 {
2997   GtkLabelPrivate *priv = label->priv;
2998
2999   if (priv->layout)
3000     {
3001       g_object_unref (priv->layout);
3002       priv->layout = NULL;
3003
3004       //gtk_label_clear_links (label);
3005     }
3006 }
3007
3008 static PangoFontMetrics *
3009 get_font_metrics (PangoContext *context, GtkWidget *widget)
3010 {
3011   GtkStyleContext *style_context;
3012   PangoFontDescription *font;
3013   PangoFontMetrics *retval;
3014
3015   style_context = gtk_widget_get_style_context (widget);
3016   gtk_style_context_get (style_context, 0, "font", &font, NULL);
3017
3018   retval = pango_context_get_metrics (context,
3019                                       font,
3020                                       pango_context_get_language (context));
3021
3022   if (font != NULL)
3023     pango_font_description_free (font);
3024
3025   return retval;
3026 }
3027
3028 /**
3029  * gtk_label_get_measuring_layout:
3030  * @label: the label
3031  * @existing_layout: %NULL or an existing layout already in use.
3032  * @width: the width to measure with in pango units, or -1 for infinite
3033  *
3034  * Gets a layout that can be used for measuring sizes. The returned
3035  * layout will be identical to the label's layout except for the
3036  * layout's width, which will be set to @width. Do not modify the returned
3037  * layout.
3038  *
3039  * Returns: a new reference to a pango layout
3040  **/
3041 static PangoLayout *
3042 gtk_label_get_measuring_layout (GtkLabel *   label,
3043                                 PangoLayout *existing_layout,
3044                                 int          width)
3045 {
3046   GtkLabelPrivate *priv = label->priv;
3047   PangoRectangle rect;
3048   PangoLayout *copy;
3049
3050   if (existing_layout != NULL)
3051     {
3052       if (existing_layout != priv->layout)
3053         {
3054           pango_layout_set_width (existing_layout, width);
3055           return existing_layout;
3056         }
3057
3058       g_object_unref (existing_layout);
3059     }
3060
3061   gtk_label_ensure_layout (label);
3062
3063   if (pango_layout_get_width (priv->layout) == width)
3064     {
3065       g_object_ref (priv->layout);
3066       return priv->layout;
3067     }
3068
3069   /* oftentimes we want to measure a width that is far wider than the current width,
3070    * even though the layout is not wrapped. In that case, we can just return the
3071    * current layout, because for measuring purposes, it will be identical.
3072    */
3073   pango_layout_get_extents (priv->layout, NULL, &rect);
3074   if ((width == -1 || rect.width <= width) &&
3075       !pango_layout_is_wrapped (priv->layout))
3076     {
3077       g_object_ref (priv->layout);
3078       return priv->layout;
3079     }
3080
3081   copy = pango_layout_copy (priv->layout);
3082   pango_layout_set_width (copy, width);
3083   return copy;
3084 }
3085
3086 static void
3087 get_label_width (GtkLabel    *label,
3088                  PangoLayout *guess_layout,
3089                  gint        *minimum,
3090                  gint        *natural)
3091 {
3092   GtkLabelPrivate     *priv;
3093   PangoLayout      *layout;
3094   PangoRectangle    rect;
3095   gint              text_width, ellipsize_chars, guess_width;
3096
3097   priv     = label->priv;
3098
3099   layout  = gtk_label_get_measuring_layout (label, NULL, -1);
3100       
3101   /* Fetch the length of the complete unwrapped text */
3102   pango_layout_get_extents (layout, NULL, &rect);
3103   text_width = rect.width;
3104
3105   /* Fetch the width that was guessed */
3106   pango_layout_get_extents (guess_layout, NULL, &rect);
3107   guess_width = rect.width;
3108
3109   /* enforce minimum width for ellipsized labels at ~3 chars */
3110   if (priv->ellipsize)
3111     ellipsize_chars = 3;
3112   else
3113     ellipsize_chars = 0;
3114
3115   /* "width-chars" Hard-coded minimum width: 
3116    *    - minimum size should be MAX (width-chars, strlen ("..."));
3117    *    - natural size should be MAX (width-chars, strlen (priv->text));
3118    *
3119    * "max-width-chars" User specified maximum size requisition
3120    *    - minimum size should be MAX (width-chars, 0)
3121    *    - natural size should be MIN (max-width-chars, strlen (priv->text))
3122    *
3123    *    For ellipsizing labels; if max-width-chars is specified: either it is used as 
3124    *    a minimum size or the label text as a minimum size (natural size still overflows).
3125    *
3126    *    For wrapping labels; A reasonable minimum size is useful to naturally layout
3127    *    interfaces automatically. In this case if no "width-chars" is specified, the minimum
3128    *    width will default to the wrap guess that gtk_label_ensure_layout() does.
3129    *
3130    *    In *any* case the minimum width is completely overridden if an explicit width 
3131    *    request was provided.
3132    */
3133
3134   if (priv->ellipsize || priv->wrap)
3135     {
3136       PangoContext     *context;
3137       PangoFontMetrics *metrics;
3138       gint              char_width, digit_width, char_pixels;
3139
3140       context = pango_layout_get_context (layout);
3141       metrics = get_font_metrics (context, GTK_WIDGET (label));
3142       char_width = pango_font_metrics_get_approximate_char_width (metrics);
3143       digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3144       char_pixels = MAX (char_width, digit_width);
3145       pango_font_metrics_unref (metrics);
3146
3147       *minimum = char_pixels * MAX (priv->width_chars, ellipsize_chars);
3148
3149       /* Default to the minimum width regularly guessed by GTK+ if no minimum
3150        * width was specified, only allow unwrapping of these labels.
3151        *
3152        * Note that when specifying a small width_chars for a long text;
3153        * an accordingly large size will be required for the label height.
3154        */
3155       if (priv->wrap && priv->width_chars <= 0)
3156         *minimum = guess_width;
3157
3158       if (priv->max_width_chars < 0)
3159         {
3160           *natural = MAX (*minimum, text_width);
3161         }
3162       else
3163         {
3164           gint max_char_width = char_pixels * priv->max_width_chars;
3165           gint max_width      = MIN (text_width, max_char_width);
3166
3167           /* With max-char-width specified, we let the minimum widths of 
3168            * ellipsized text crawl up to the max-char-width
3169            * (note that we dont want to limit the minimum width for wrapping text).
3170            */
3171           if (priv->ellipsize)
3172             *minimum = MIN (text_width, max_width);
3173
3174           *natural = MAX (*minimum, max_width);
3175         }
3176     }
3177   else
3178     {
3179       *minimum = text_width;
3180       *natural = *minimum;
3181     }
3182
3183   /* if a width-request is set, use that as the requested label width */
3184   if (priv->wrap || priv->ellipsize || priv->width_chars > 0 || priv->max_width_chars > 0)
3185     {
3186       GtkWidgetAuxInfo *aux_info;
3187
3188       aux_info = _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3189       if (aux_info && aux_info->width > 0)
3190         {
3191           *minimum = aux_info->width * PANGO_SCALE;
3192           *natural = MAX (*natural, *minimum);
3193         }
3194     }
3195
3196   g_object_unref (layout);
3197 }
3198
3199 static void
3200 gtk_label_invalidate_wrap_width (GtkLabel *label)
3201 {
3202   GtkLabelPrivate *priv = label->priv;
3203
3204   priv->wrap_width = -1;
3205 }
3206
3207 static gint
3208 get_label_wrap_width (GtkLabel *label)
3209 {
3210   GtkLabelPrivate *priv = label->priv;
3211
3212   if (priv->wrap_width < 0)
3213     {
3214       if (priv->width_chars > 0)
3215         {
3216           PangoLayout      *layout;
3217           PangoContext     *context;
3218           PangoFontMetrics *metrics;
3219           PangoRectangle    rect;
3220           gint              char_width, digit_width, char_pixels, text_width;
3221
3222           layout  = gtk_label_get_measuring_layout (label, NULL, -1);
3223           context = pango_layout_get_context (layout);
3224           metrics = get_font_metrics (context, GTK_WIDGET (label));
3225           char_width = pango_font_metrics_get_approximate_char_width (metrics);
3226           digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
3227           char_pixels = MAX (char_width, digit_width);
3228           pango_font_metrics_unref (metrics);
3229           
3230           pango_layout_get_extents (layout, NULL, &rect);
3231           g_object_unref (layout);
3232
3233           text_width = rect.width;
3234
3235           priv->wrap_width = PANGO_PIXELS (MAX (text_width, char_pixels * priv->width_chars));
3236         }
3237       else
3238         {
3239           PangoLayout *layout;
3240   
3241           layout = gtk_widget_create_pango_layout (GTK_WIDGET (label), 
3242                                                    "This string is just about long enough.");
3243           pango_layout_get_size (layout, &priv->wrap_width, NULL);
3244           g_object_unref (layout);
3245         }
3246     }
3247
3248   return priv->wrap_width;
3249 }
3250
3251 static PangoLayout *
3252 gtk_label_get_layout_with_guessed_wrap_width (GtkLabel *label)
3253 {
3254   GdkScreen *screen;
3255   PangoRectangle logical_rect;
3256   gint wrap_width, width, height, longest_paragraph;
3257   PangoLayout *layout;
3258
3259   if (!label->priv->wrap)
3260     return gtk_label_get_measuring_layout (label, NULL, -1);
3261
3262   screen = gtk_widget_get_screen (GTK_WIDGET (label));
3263
3264   layout = gtk_label_get_measuring_layout (label, NULL, -1);
3265   pango_layout_get_extents (layout, NULL, &logical_rect);
3266
3267   width = logical_rect.width;
3268   /* Try to guess a reasonable maximum width */
3269   longest_paragraph = width;
3270
3271   wrap_width = get_label_wrap_width (label);
3272   width = MIN (width, wrap_width);
3273   width = MIN (width,
3274                PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
3275
3276   layout = gtk_label_get_measuring_layout (label, layout, width);
3277
3278   pango_layout_get_extents (layout, NULL, &logical_rect);
3279   width = logical_rect.width;
3280   height = logical_rect.height;
3281
3282   /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
3283    * so we try short search for a narrower width that leaves us with the same height
3284    */
3285   if (longest_paragraph > 0)
3286     {
3287       gint nlines, perfect_width;
3288
3289       nlines = pango_layout_get_line_count (layout);
3290       perfect_width = (longest_paragraph + nlines - 1) / nlines;
3291       
3292       if (perfect_width < width)
3293         {
3294           layout = gtk_label_get_measuring_layout (label, layout, perfect_width);
3295           pango_layout_get_extents (layout, NULL, &logical_rect);
3296
3297           if (logical_rect.height <= height)
3298             width = logical_rect.width;
3299           else
3300             {
3301               gint mid_width = (perfect_width + width) / 2;
3302               
3303               if (mid_width > perfect_width)
3304                 {
3305                   layout = gtk_label_get_measuring_layout (label, layout, mid_width);
3306                   pango_layout_get_extents (layout, NULL, &logical_rect);
3307
3308                   if (logical_rect.height <= height)
3309                     width = logical_rect.width;
3310                 }
3311             }
3312         }
3313     }
3314
3315   return gtk_label_get_measuring_layout (label, layout, width);
3316 }
3317
3318 static void
3319 gtk_label_ensure_layout (GtkLabel *label)
3320 {
3321   GtkLabelPrivate *priv = label->priv;
3322   GtkWidget *widget;
3323   gboolean rtl;
3324
3325   widget = GTK_WIDGET (label);
3326
3327   rtl = gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL;
3328
3329   if (!priv->layout)
3330     {
3331       GtkAllocation allocation;
3332       PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
3333       gdouble angle = gtk_label_get_angle (label);
3334
3335       if (angle != 0.0 && !priv->select_info)
3336         {
3337           PangoMatrix matrix = PANGO_MATRIX_INIT;
3338
3339           /* We rotate the standard singleton PangoContext for the widget,
3340            * depending on the fact that it's meant pretty much exclusively
3341            * for our use.
3342            */
3343           pango_matrix_rotate (&matrix, angle);
3344
3345           pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
3346
3347           priv->have_transform = TRUE;
3348         }
3349       else
3350         {
3351           if (priv->have_transform)
3352             pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
3353
3354           priv->have_transform = FALSE;
3355         }
3356
3357       priv->layout = gtk_widget_create_pango_layout (widget, priv->text);
3358
3359       if (priv->effective_attrs)
3360         pango_layout_set_attributes (priv->layout, priv->effective_attrs);
3361
3362       gtk_label_rescan_links (label);
3363
3364       switch (priv->jtype)
3365         {
3366         case GTK_JUSTIFY_LEFT:
3367           align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3368           break;
3369         case GTK_JUSTIFY_RIGHT:
3370           align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
3371           break;
3372         case GTK_JUSTIFY_CENTER:
3373           align = PANGO_ALIGN_CENTER;
3374           break;
3375         case GTK_JUSTIFY_FILL:
3376           align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
3377           pango_layout_set_justify (priv->layout, TRUE);
3378           break;
3379         default:
3380           g_assert_not_reached();
3381         }
3382
3383       pango_layout_set_alignment (priv->layout, align);
3384       pango_layout_set_ellipsize (priv->layout, priv->ellipsize);
3385       pango_layout_set_single_paragraph_mode (priv->layout, priv->single_line_mode);
3386
3387       gtk_widget_get_allocation (widget, &allocation);
3388
3389       if (priv->ellipsize)
3390         pango_layout_set_width (priv->layout, allocation.width * PANGO_SCALE);
3391       else if (priv->wrap)
3392         {
3393           GtkWidgetAuxInfo *aux_info = _gtk_widget_get_aux_info (widget, FALSE);
3394           gint width;
3395           gint aux_width = 0;
3396
3397           if ((angle == 90 || angle == 270) && aux_info && aux_info->height > 0)
3398             aux_width = aux_info->height;
3399           else if (aux_info && aux_info->width > 0)
3400             aux_width = aux_info->width;
3401
3402           if (aux_width > 0)
3403             pango_layout_set_width (priv->layout, aux_width * PANGO_SCALE);
3404           else
3405             {
3406               PangoRectangle rect;
3407               gint xpad, ypad, natural_width;
3408               gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3409
3410               if (angle == 90 || angle == 270)
3411                 width = allocation.height - ypad * 2;
3412               else
3413                 width = allocation.width  - xpad * 2;
3414
3415               /* dont set a wrap width wider than the label's natural width
3416                * incase we're allocated more space than needed */
3417               pango_layout_get_extents (priv->layout, NULL, &rect);
3418               natural_width = PANGO_PIXELS (rect.width);
3419               width = MIN (natural_width, width);
3420
3421               pango_layout_set_wrap (priv->layout, priv->wrap_mode);
3422               pango_layout_set_width (priv->layout, MAX (width, 1) * PANGO_SCALE);
3423             }
3424         }
3425       else /* !priv->wrap */
3426         pango_layout_set_width (priv->layout, -1);
3427     }
3428 }
3429
3430 static gint
3431 get_single_line_height (GtkWidget   *widget,
3432                         PangoLayout *layout)
3433 {
3434   PangoContext *context;
3435   PangoFontMetrics *metrics;
3436   gint ascent, descent;
3437
3438   context = pango_layout_get_context (layout);
3439   metrics = get_font_metrics (context, widget);
3440   ascent = pango_font_metrics_get_ascent (metrics);
3441   descent = pango_font_metrics_get_descent (metrics);
3442   pango_font_metrics_unref (metrics);
3443
3444   return ascent + descent;
3445 }
3446
3447 static GtkSizeRequestMode
3448 gtk_label_get_request_mode (GtkWidget *widget)
3449 {
3450   GtkLabel *label = GTK_LABEL (widget);
3451   gdouble   angle = gtk_label_get_angle (label);
3452
3453   if (label->priv->wrap)
3454     return (angle == 90 || angle == 270) ?
3455       GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT : 
3456       GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
3457
3458     return GTK_SIZE_REQUEST_CONSTANT_SIZE;
3459 }
3460
3461 static void
3462 get_size_for_allocation (GtkLabel        *label,
3463                          GtkOrientation   orientation,
3464                          gint             allocation,
3465                          gint            *minimum_size,
3466                          gint            *natural_size)
3467 {
3468   PangoLayout *layout;
3469   GtkWidgetAuxInfo *aux_info =
3470     _gtk_widget_get_aux_info (GTK_WIDGET (label), FALSE);
3471   gint aux_size;
3472   gint text_height;
3473
3474   if (aux_info)
3475     {
3476       if (orientation == GTK_ORIENTATION_HORIZONTAL)
3477         aux_size = aux_info->width;
3478       else
3479         aux_size = aux_info->height;
3480     }
3481   else
3482     aux_size = 0;
3483
3484   if (aux_size > 0)
3485     layout = gtk_label_get_measuring_layout (label, NULL, aux_size * PANGO_SCALE);
3486   else
3487     layout = gtk_label_get_measuring_layout (label, NULL, allocation * PANGO_SCALE);
3488
3489   pango_layout_get_pixel_size (layout, NULL, &text_height);
3490
3491   if (minimum_size)
3492     *minimum_size = text_height;
3493
3494   if (natural_size)
3495     *natural_size = text_height;
3496
3497   g_object_unref (layout);
3498 }
3499
3500 static void
3501 gtk_label_get_preferred_size (GtkWidget      *widget,
3502                               GtkOrientation  orientation,
3503                               gint           *minimum_size,
3504                               gint           *natural_size)
3505 {
3506   GtkLabel      *label = GTK_LABEL (widget);
3507   GtkLabelPrivate  *priv = label->priv;
3508   PangoRectangle required_rect;
3509   PangoRectangle natural_rect;
3510   gint           xpad, ypad;
3511   PangoLayout *layout;
3512
3513   /* "width-chars" Hard-coded minimum width:
3514    *    - minimum size should be MAX (width-chars, strlen ("..."));
3515    *    - natural size should be MAX (width-chars, strlen (priv->text));
3516    *
3517    * "max-width-chars" User specified maximum size requisition
3518    *    - minimum size should be MAX (width-chars, 0)
3519    *    - natural size should be MIN (max-width-chars, strlen (priv->text))
3520    *
3521    */
3522
3523   /* When calculating ->wrap sometimes we need to invent a size; Ideally we should be doing
3524    * that stuff here instead of inside gtk_label_ensure_layout() */
3525   layout = gtk_label_get_layout_with_guessed_wrap_width (label);
3526
3527   /* Start off with the pixel extents of the rendered layout */
3528   pango_layout_get_extents (layout, NULL, &required_rect);
3529   required_rect.x = required_rect.y = 0;
3530
3531   if (priv->single_line_mode || priv->wrap)
3532     required_rect.height = get_single_line_height (GTK_WIDGET (label), layout);
3533
3534   natural_rect = required_rect;
3535
3536   /* Calculate text width itself based on GtkLabel property rules */
3537   get_label_width (label, layout, &required_rect.width, &natural_rect.width);
3538
3539   /* Now that we have minimum and natural sizes in pango extents, apply a possible transform */
3540   if (priv->have_transform)
3541     {
3542       PangoLayout *copy;
3543       PangoContext *context;
3544       const PangoMatrix *matrix;
3545
3546       copy = pango_layout_copy (priv->layout);
3547       context = pango_layout_get_context (copy);
3548       matrix = pango_context_get_matrix (context);
3549
3550       pango_layout_set_width (copy, -1);
3551       pango_layout_set_ellipsize (copy, PANGO_ELLIPSIZE_NONE);
3552
3553       pango_layout_get_extents (copy, NULL, &natural_rect);
3554       g_object_unref (copy);
3555
3556       pango_matrix_transform_rectangle (matrix, &required_rect);
3557       pango_matrix_transform_rectangle (matrix, &natural_rect);
3558
3559       /* Bump the natural size in case of ellipsize to ensure pango has
3560        * enough space in the angles (note, we could alternatively set the
3561        * layout to not ellipsize when we know we have been allocated our
3562        * full natural size, or it may be that pango needs a fix here).
3563        */
3564       if (priv->ellipsize && priv->angle != 0 && priv->angle != 90 && 
3565           priv->angle != 180 && priv->angle != 270 && priv->angle != 360)
3566         {
3567           /* For some reason we only need this at about 110 degrees, and only
3568            * when gaining in height
3569            */
3570           natural_rect.height += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3571           natural_rect.width  += ROTATION_ELLIPSIZE_PADDING * 2 * PANGO_SCALE;
3572         }
3573     }
3574
3575   required_rect.width  = PANGO_PIXELS_CEIL (required_rect.width);
3576   required_rect.height = PANGO_PIXELS_CEIL (required_rect.height);
3577
3578   natural_rect.width  = PANGO_PIXELS_CEIL (natural_rect.width);
3579   natural_rect.height = PANGO_PIXELS_CEIL (natural_rect.height);
3580
3581   gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3582
3583   if (orientation == GTK_ORIENTATION_HORIZONTAL)
3584     {
3585       /* Note, we cant use get_size_for_allocation() when rotating
3586        * ellipsized labels.
3587        */
3588       if (!(priv->ellipsize && priv->have_transform) &&
3589           (priv->angle == 90 || priv->angle == 270))
3590         {
3591           /* Doing a h4w request on a rotated label here, return the
3592            * required width for the minimum height.
3593            */
3594           get_size_for_allocation (label,
3595                                    GTK_ORIENTATION_VERTICAL,
3596                                    required_rect.height,
3597                                    minimum_size, natural_size);
3598
3599         }
3600       else
3601         {
3602           /* Normal desired width */
3603           *minimum_size = required_rect.width;
3604           *natural_size = natural_rect.width;
3605         }
3606
3607       *minimum_size += xpad * 2;
3608       *natural_size += xpad * 2;
3609     }
3610   else /* GTK_ORIENTATION_VERTICAL */
3611     {
3612       /* Note, we cant use get_size_for_allocation() when rotating
3613        * ellipsized labels.
3614        */
3615       if (!(priv->ellipsize && priv->have_transform) &&
3616           (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3617         {
3618           /* Doing a w4h request on a label here, return the required
3619            * height for the minimum width.
3620            */
3621           get_size_for_allocation (label,
3622                                    GTK_ORIENTATION_HORIZONTAL,
3623                                    required_rect.width,
3624                                    minimum_size, natural_size);
3625         }
3626       else
3627         {
3628           /* A vertically rotated label does w4h, so return the base
3629            * desired height (text length)
3630            */
3631           *minimum_size = required_rect.height;
3632           *natural_size = natural_rect.height;
3633         }
3634
3635       *minimum_size += ypad * 2;
3636       *natural_size += ypad * 2;
3637     }
3638
3639   g_object_unref (layout);
3640 }
3641
3642
3643 static void
3644 gtk_label_get_preferred_width (GtkWidget *widget,
3645                                gint      *minimum_size,
3646                                gint      *natural_size)
3647 {
3648   gtk_label_get_preferred_size (widget, GTK_ORIENTATION_HORIZONTAL, minimum_size, natural_size);
3649 }
3650
3651 static void
3652 gtk_label_get_preferred_height (GtkWidget *widget,
3653                                 gint      *minimum_size,
3654                                 gint      *natural_size)
3655 {
3656   gtk_label_get_preferred_size (widget, GTK_ORIENTATION_VERTICAL, minimum_size, natural_size);
3657 }
3658
3659 static void
3660 gtk_label_get_preferred_width_for_height (GtkWidget *widget,
3661                                           gint       height,
3662                                           gint      *minimum_width,
3663                                           gint      *natural_width)
3664 {
3665   GtkLabel *label = GTK_LABEL (widget);
3666   GtkLabelPrivate *priv = label->priv;
3667
3668   if (priv->wrap && (priv->angle == 90 || priv->angle == 270))
3669     {
3670       gint xpad, ypad;
3671
3672       gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3673
3674       if (priv->wrap)
3675         gtk_label_clear_layout (label);
3676
3677       get_size_for_allocation (label, GTK_ORIENTATION_VERTICAL,
3678                                MAX (1, height - (ypad * 2)),
3679                                minimum_width, natural_width);
3680
3681       if (minimum_width)
3682         *minimum_width += xpad * 2;
3683
3684       if (natural_width)
3685         *natural_width += xpad * 2;
3686     }
3687   else
3688     GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, minimum_width, natural_width);
3689 }
3690
3691 static void
3692 gtk_label_get_preferred_height_for_width (GtkWidget *widget,
3693                                           gint       width,
3694                                           gint      *minimum_height,
3695                                           gint      *natural_height)
3696 {
3697   GtkLabel *label = GTK_LABEL (widget);
3698   GtkLabelPrivate *priv = label->priv;
3699
3700   if (priv->wrap && (priv->angle == 0 || priv->angle == 180 || priv->angle == 360))
3701     {
3702       gint xpad, ypad;
3703
3704       gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3705
3706       if (priv->wrap)
3707         gtk_label_clear_layout (label);
3708
3709       get_size_for_allocation (label, GTK_ORIENTATION_HORIZONTAL,
3710                                MAX (1, width - xpad * 2),
3711                                minimum_height, natural_height);
3712
3713       if (minimum_height)
3714         *minimum_height += ypad * 2;
3715
3716       if (natural_height)
3717         *natural_height += ypad * 2;
3718     }
3719   else
3720     GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, minimum_height, natural_height);
3721 }
3722
3723 static void
3724 gtk_label_size_allocate (GtkWidget     *widget,
3725                          GtkAllocation *allocation)
3726 {
3727   GtkLabel *label = GTK_LABEL (widget);
3728   GtkLabelPrivate *priv = label->priv;
3729
3730   GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate (widget, allocation);
3731
3732   /* The layout may have been recently cleared in get_size_for_orientation(),
3733    * but the width at that point may not be the same as the allocated width
3734    */
3735   if (priv->wrap)
3736     gtk_label_clear_layout (label);
3737
3738   gtk_label_ensure_layout (label);
3739
3740   if (priv->ellipsize)
3741     {
3742       if (priv->layout)
3743         {
3744           PangoRectangle logical;
3745           PangoRectangle bounds;
3746           gint xpad, ypad;
3747
3748           gtk_misc_get_padding (GTK_MISC (label), &xpad, &ypad);
3749
3750           bounds.x = bounds.y = 0;
3751           bounds.width = allocation->width - xpad * 2;
3752           bounds.height = allocation->height - ypad * 2;
3753
3754           pango_layout_set_width (priv->layout, -1);
3755           pango_layout_get_pixel_extents (priv->layout, NULL, &logical);
3756
3757           if (priv->have_transform)
3758             {
3759               PangoContext *context = gtk_widget_get_pango_context (widget);
3760               const PangoMatrix *matrix = pango_context_get_matrix (context);
3761
3762               const gdouble dx = matrix->xx; /* cos (M_PI * angle / 180) */
3763               const gdouble dy = matrix->xy; /* sin (M_PI * angle / 180) */
3764               if (fabs (dy) < 0.01)
3765                 {
3766                   if (logical.width > bounds.width)
3767                     pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3768                 }
3769               else if (fabs (dx) < 0.01)
3770                 {
3771                   if (logical.width > bounds.height)
3772                     pango_layout_set_width (priv->layout, bounds.height * PANGO_SCALE);
3773                 }
3774               else
3775                 {
3776                   gdouble x0, y0, x1, y1, length;
3777                   gboolean vertical;
3778                   gint cy;
3779
3780                   x0 = bounds.width / 2;
3781                   y0 = dx ? x0 * dy / dx : G_MAXDOUBLE;
3782                   vertical = fabs (y0) > bounds.height / 2;
3783
3784                   if (vertical)
3785                     {
3786                       y0 = bounds.height/2;
3787                       x0 = dy ? y0 * dx / dy : G_MAXDOUBLE;
3788                     }
3789
3790                   length = 2 * sqrt (x0 * x0 + y0 * y0);
3791                   pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3792                   pango_layout_get_pixel_size (priv->layout, NULL, &cy);
3793
3794                   x1 = +dy * cy/2;
3795                   y1 = -dx * cy/2;
3796
3797                   if (vertical)
3798                     {
3799                       y0 = bounds.height/2 + y1 - y0;
3800                       x0 = -y0 * dx/dy;
3801                     }
3802                   else
3803                     {
3804                       x0 = bounds.width/2 + x1 - x0;
3805                       y0 = -x0 * dy/dx;
3806                     }
3807  
3808                   length = length - sqrt (x0 * x0 + y0 * y0) * 2;
3809                   pango_layout_set_width (priv->layout, rint (length * PANGO_SCALE));
3810                 }
3811             }
3812           else if (logical.width > bounds.width)
3813             pango_layout_set_width (priv->layout, bounds.width * PANGO_SCALE);
3814         }
3815     }
3816
3817   if (priv->select_info && priv->select_info->window)
3818     {
3819       gdk_window_move_resize (priv->select_info->window,
3820                               allocation->x,
3821                               allocation->y,
3822                               allocation->width,
3823                               allocation->height);
3824     }
3825 }
3826
3827 static void
3828 gtk_label_update_cursor (GtkLabel *label)
3829 {
3830   GtkLabelPrivate *priv = label->priv;
3831   GtkWidget *widget;
3832
3833   if (!priv->select_info)
3834     return;
3835
3836   widget = GTK_WIDGET (label);
3837
3838   if (gtk_widget_get_realized (widget))
3839     {
3840       GdkDisplay *display;
3841       GdkCursor *cursor;
3842
3843       if (gtk_widget_is_sensitive (widget))
3844         {
3845           display = gtk_widget_get_display (widget);
3846
3847           if (priv->select_info->active_link)
3848             cursor = gdk_cursor_new_for_display (display, GDK_HAND2);
3849           else if (priv->select_info->selectable)
3850             cursor = gdk_cursor_new_for_display (display, GDK_XTERM);
3851           else
3852             cursor = NULL;
3853         }
3854       else
3855         cursor = NULL;
3856
3857       gdk_window_set_cursor (priv->select_info->window, cursor);
3858
3859       if (cursor)
3860         g_object_unref (cursor);
3861     }
3862 }
3863
3864 static void
3865 gtk_label_state_changed (GtkWidget   *widget,
3866                          GtkStateType prev_state)
3867 {
3868   GtkLabel *label = GTK_LABEL (widget);
3869   GtkLabelPrivate *priv = label->priv;
3870
3871   if (priv->select_info)
3872     {
3873       if (!gtk_widget_is_sensitive (widget))
3874         gtk_label_select_region (label, 0, 0);
3875
3876       gtk_label_update_cursor (label);
3877     }
3878
3879   if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
3880     GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
3881 }
3882
3883 static void
3884 gtk_label_style_updated (GtkWidget *widget)
3885 {
3886   GtkLabel *label = GTK_LABEL (widget);
3887
3888   GTK_WIDGET_CLASS (gtk_label_parent_class)->style_updated (widget);
3889
3890   /* We have to clear the layout, fonts etc. may have changed */
3891   gtk_label_clear_layout (label);
3892   gtk_label_invalidate_wrap_width (label);
3893 }
3894
3895 static void 
3896 gtk_label_direction_changed (GtkWidget        *widget,
3897                              GtkTextDirection previous_dir)
3898 {
3899   GtkLabel *label = GTK_LABEL (widget);
3900   GtkLabelPrivate *priv = label->priv;
3901
3902   if (priv->layout)
3903     pango_layout_context_changed (priv->layout);
3904
3905   GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
3906 }
3907
3908 static void
3909 get_layout_location (GtkLabel  *label,
3910                      gint      *xp,
3911                      gint      *yp)
3912 {
3913   GtkAllocation allocation;
3914   GtkMisc *misc;
3915   GtkWidget *widget;
3916   GtkLabelPrivate *priv;
3917   gint req_width, x, y;
3918   gint req_height;
3919   gint xpad, ypad;
3920   gfloat xalign, yalign;
3921   PangoRectangle logical;
3922
3923   misc   = GTK_MISC (label);
3924   widget = GTK_WIDGET (label);
3925   priv   = label->priv;
3926
3927   gtk_misc_get_alignment (misc, &xalign, &yalign);
3928   gtk_misc_get_padding (misc, &xpad, &ypad);
3929
3930   if (gtk_widget_get_direction (widget) != GTK_TEXT_DIR_LTR)
3931     xalign = 1.0 - xalign;
3932
3933   pango_layout_get_extents (priv->layout, NULL, &logical);
3934
3935   if (priv->have_transform)
3936     {
3937       PangoContext *context = gtk_widget_get_pango_context (widget);
3938       const PangoMatrix *matrix = pango_context_get_matrix (context);
3939       pango_matrix_transform_rectangle (matrix, &logical);
3940     }
3941
3942   pango_extents_to_pixels (&logical, NULL);
3943
3944   req_width  = logical.width;
3945   req_height = logical.height;
3946
3947   req_width  += 2 * xpad;
3948   req_height += 2 * ypad;
3949
3950   gtk_widget_get_allocation (widget, &allocation);
3951
3952   x = floor (allocation.x + xpad + xalign * (allocation.width - req_width));
3953
3954   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
3955     x = MAX (x, allocation.x + xpad);
3956   else
3957     x = MIN (x, allocation.x + allocation.width - xpad);
3958
3959
3960   /* bgo#315462 - For single-line labels, *do* align the requisition with
3961    * respect to the allocation, even if we are under-allocated.  For multi-line
3962    * labels, always show the top of the text when they are under-allocated.  The
3963    * rationale is this:
3964    *
3965    * - Single-line labels appear in GtkButtons, and it is very easy to get them
3966    *   to be smaller than their requisition.  The button may clip the label, but
3967    *   the label will still be able to show most of itself and the focus
3968    *   rectangle.  Also, it is fairly easy to read a single line of clipped text.
3969    *
3970    * - Multi-line labels should not be clipped to showing "something in the
3971    *   middle".  You want to read the first line, at least, to get some context.
3972    */
3973   if (pango_layout_get_line_count (priv->layout) == 1)
3974     y = floor (allocation.y + ypad + (allocation.height - req_height) * yalign);
3975   else
3976     y = floor (allocation.y + ypad + MAX ((allocation.height - req_height) * yalign, 0));
3977
3978   if (xp)
3979     *xp = x;
3980
3981   if (yp)
3982     *yp = y;
3983 }
3984
3985 static void
3986 draw_insertion_cursor (GtkLabel      *label,
3987                        cairo_t       *cr,
3988                        GdkRectangle  *cursor_location,
3989                        gboolean       is_primary,
3990                        PangoDirection direction,
3991                        gboolean       draw_arrow)
3992 {
3993   GtkWidget *widget = GTK_WIDGET (label);
3994   GtkTextDirection text_dir;
3995
3996   if (direction == PANGO_DIRECTION_LTR)
3997     text_dir = GTK_TEXT_DIR_LTR;
3998   else
3999     text_dir = GTK_TEXT_DIR_RTL;
4000
4001   gtk_draw_insertion_cursor (widget, cr, cursor_location,
4002                              is_primary, text_dir, draw_arrow);
4003 }
4004
4005 static PangoDirection
4006 get_cursor_direction (GtkLabel *label)
4007 {
4008   GtkLabelPrivate *priv = label->priv;
4009   GSList *l;
4010
4011   g_assert (priv->select_info);
4012
4013   gtk_label_ensure_layout (label);
4014
4015   for (l = pango_layout_get_lines_readonly (priv->layout); l; l = l->next)
4016     {
4017       PangoLayoutLine *line = l->data;
4018
4019       /* If priv->select_info->selection_end is at the very end of
4020        * the line, we don't know if the cursor is on this line or
4021        * the next without looking ahead at the next line. (End
4022        * of paragraph is different from line break.) But it's
4023        * definitely in this paragraph, which is good enough
4024        * to figure out the resolved direction.
4025        */
4026        if (line->start_index + line->length >= priv->select_info->selection_end)
4027         return line->resolved_dir;
4028     }
4029
4030   return PANGO_DIRECTION_LTR;
4031 }
4032
4033 static void
4034 gtk_label_draw_cursor (GtkLabel  *label, cairo_t *cr, gint xoffset, gint yoffset)
4035 {
4036   GtkLabelPrivate *priv = label->priv;
4037   GtkWidget *widget;
4038
4039   if (priv->select_info == NULL)
4040     return;
4041
4042   widget = GTK_WIDGET (label);
4043   
4044   if (gtk_widget_is_drawable (widget))
4045     {
4046       PangoDirection keymap_direction;
4047       PangoDirection cursor_direction;
4048       PangoRectangle strong_pos, weak_pos;
4049       gboolean split_cursor;
4050       PangoRectangle *cursor1 = NULL;
4051       PangoRectangle *cursor2 = NULL;
4052       GdkRectangle cursor_location;
4053       PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
4054       PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
4055
4056       keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
4057       cursor_direction = get_cursor_direction (label);
4058
4059       gtk_label_ensure_layout (label);
4060       
4061       pango_layout_get_cursor_pos (priv->layout, priv->select_info->selection_end,
4062                                    &strong_pos, &weak_pos);
4063
4064       g_object_get (gtk_widget_get_settings (widget),
4065                     "gtk-split-cursor", &split_cursor,
4066                     NULL);
4067
4068       dir1 = cursor_direction;
4069       
4070       if (split_cursor)
4071         {
4072           cursor1 = &strong_pos;
4073
4074           if (strong_pos.x != weak_pos.x ||
4075               strong_pos.y != weak_pos.y)
4076             {
4077               dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
4078               cursor2 = &weak_pos;
4079             }
4080         }
4081       else
4082         {
4083           if (keymap_direction == cursor_direction)
4084             cursor1 = &strong_pos;
4085           else
4086             cursor1 = &weak_pos;
4087         }
4088       
4089       cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
4090       cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
4091       cursor_location.width = 0;
4092       cursor_location.height = PANGO_PIXELS (cursor1->height);
4093
4094       draw_insertion_cursor (label, cr,
4095                              &cursor_location, TRUE, dir1,
4096                              dir2 != PANGO_DIRECTION_NEUTRAL);
4097       
4098       if (dir2 != PANGO_DIRECTION_NEUTRAL)
4099         {
4100           cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
4101           cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
4102           cursor_location.width = 0;
4103           cursor_location.height = PANGO_PIXELS (cursor2->height);
4104
4105           draw_insertion_cursor (label, cr,
4106                                  &cursor_location, FALSE, dir2,
4107                                  TRUE);
4108         }
4109     }
4110 }
4111
4112 static GtkLabelLink *
4113 gtk_label_get_focus_link (GtkLabel *label)
4114 {
4115   GtkLabelPrivate *priv = label->priv;
4116   GtkLabelSelectionInfo *info = priv->select_info;
4117   GList *l;
4118
4119   if (!info)
4120     return NULL;
4121
4122   if (info->selection_anchor != info->selection_end)
4123     return NULL;
4124
4125   for (l = info->links; l; l = l->next)
4126     {
4127       GtkLabelLink *link = l->data;
4128       if (link->start <= info->selection_anchor &&
4129           info->selection_anchor <= link->end)
4130         return link;
4131     }
4132
4133   return NULL;
4134 }
4135
4136 static gint
4137 gtk_label_draw (GtkWidget *widget,
4138                 cairo_t   *cr)
4139 {
4140   GtkLabel *label = GTK_LABEL (widget);
4141   GtkLabelPrivate *priv = label->priv;
4142   GtkLabelSelectionInfo *info = priv->select_info;
4143   GtkAllocation allocation;
4144   GtkStyleContext *context;
4145   GtkStateFlags state;
4146   gint x, y;
4147
4148   gtk_label_ensure_layout (label);
4149
4150   if (priv->text && (*priv->text != '\0'))
4151     {
4152       GdkRGBA *bg_color, *fg_color;
4153
4154       get_layout_location (label, &x, &y);
4155
4156       context = gtk_widget_get_style_context (widget);
4157       gtk_widget_get_allocation (widget, &allocation);
4158
4159       cairo_translate (cr, -allocation.x, -allocation.y);
4160
4161       state = gtk_widget_get_state_flags (widget);
4162       gtk_style_context_set_state (context, state);
4163
4164       gtk_render_layout (context, cr,
4165                          x, y,
4166                          priv->layout);
4167
4168       if (info &&
4169           (info->selection_anchor != info->selection_end))
4170         {
4171           gint range[2];
4172           cairo_region_t *clip;
4173
4174           range[0] = info->selection_anchor;
4175           range[1] = info->selection_end;
4176
4177           if (range[0] > range[1])
4178             {
4179               gint tmp = range[0];
4180               range[0] = range[1];
4181               range[1] = tmp;
4182             }
4183
4184           clip = gdk_pango_layout_get_clip_region (priv->layout,
4185                                                    x, y,
4186                                                    range,
4187                                                    1);
4188
4189          /* FIXME should use gtk_paint, but it can't use a clip region */
4190           cairo_save (cr);
4191
4192           gdk_cairo_region (cr, clip);
4193           cairo_clip (cr);
4194
4195           state = GTK_STATE_FLAG_SELECTED;
4196
4197           if (gtk_widget_has_focus (widget))
4198             state |= GTK_STATE_FLAG_FOCUSED;
4199
4200           gtk_style_context_get (context, state,
4201                                  "background-color", &bg_color,
4202                                  "color", &fg_color,
4203                                  NULL);
4204
4205           gdk_cairo_set_source_rgba (cr, bg_color);
4206           cairo_paint (cr);
4207
4208           gdk_cairo_set_source_rgba (cr, fg_color);
4209           cairo_move_to (cr, x, y);
4210           _gtk_pango_fill_layout (cr, priv->layout);
4211
4212           cairo_restore (cr);
4213           cairo_region_destroy (clip);
4214
4215           gdk_rgba_free (bg_color);
4216           gdk_rgba_free (fg_color);
4217         }
4218       else if (info)
4219         {
4220           GtkLabelLink *focus_link;
4221           GtkLabelLink *active_link;
4222           gint range[2];
4223           cairo_region_t *clip;
4224           GdkRectangle rect;
4225           GdkColor *text_color;
4226           GdkColor *link_color;
4227           GdkColor *visited_link_color;
4228
4229           if (info->selectable && gtk_widget_has_focus (widget))
4230             gtk_label_draw_cursor (label, cr, x, y);
4231
4232           focus_link = gtk_label_get_focus_link (label);
4233           active_link = info->active_link;
4234
4235
4236           if (active_link)
4237             {
4238               GdkRGBA *bg_color;
4239
4240               range[0] = active_link->start;
4241               range[1] = active_link->end;
4242
4243               cairo_save (cr);
4244
4245               clip = gdk_pango_layout_get_clip_region (priv->layout,
4246                                                        x, y,
4247                                                        range,
4248                                                        1);
4249               gdk_cairo_region (cr, clip);
4250               cairo_clip (cr);
4251               cairo_region_destroy (clip);
4252
4253               gtk_label_get_link_colors (widget, &link_color, &visited_link_color);
4254               if (active_link->visited)
4255                 text_color = visited_link_color;
4256               else
4257                 text_color = link_color;
4258
4259               if (info->link_clicked)
4260                 state = GTK_STATE_FLAG_ACTIVE;
4261               else
4262                 state = GTK_STATE_FLAG_PRELIGHT;
4263
4264               gtk_style_context_get (context, state,
4265                                      "background-color", &bg_color,
4266                                      NULL);
4267
4268               gdk_cairo_set_source_rgba (cr, bg_color);
4269               cairo_paint (cr);
4270
4271               gdk_cairo_set_source_color (cr, text_color);
4272               cairo_move_to (cr, x, y);
4273               _gtk_pango_fill_layout (cr, priv->layout);
4274
4275               gdk_color_free (link_color);
4276               gdk_color_free (visited_link_color);
4277               gdk_rgba_free (bg_color);
4278
4279               cairo_restore (cr);
4280             }
4281
4282           if (focus_link && gtk_widget_has_focus (widget))
4283             {
4284               range[0] = focus_link->start;
4285               range[1] = focus_link->end;
4286
4287               clip = gdk_pango_layout_get_clip_region (priv->layout,
4288                                                        x, y,
4289                                                        range,
4290                                                        1);
4291               cairo_region_get_extents (clip, &rect);
4292
4293               state = gtk_widget_get_state_flags (widget);
4294               gtk_style_context_set_state (context, state);
4295
4296               gtk_render_focus (context, cr,
4297                                 rect.x, rect.y,
4298                                 rect.width, rect.height);
4299
4300               cairo_region_destroy (clip);
4301             }
4302         }
4303     }
4304
4305   return FALSE;
4306 }
4307
4308 static gboolean
4309 separate_uline_pattern (const gchar  *str,
4310                         guint        *accel_key,
4311                         gchar       **new_str,
4312                         gchar       **pattern)
4313 {
4314   gboolean underscore;
4315   const gchar *src;
4316   gchar *dest;
4317   gchar *pattern_dest;
4318
4319   *accel_key = GDK_KEY_VoidSymbol;
4320   *new_str = g_new (gchar, strlen (str) + 1);
4321   *pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
4322
4323   underscore = FALSE;
4324
4325   src = str;
4326   dest = *new_str;
4327   pattern_dest = *pattern;
4328
4329   while (*src)
4330     {
4331       gunichar c;
4332       const gchar *next_src;
4333
4334       c = g_utf8_get_char (src);
4335       if (c == (gunichar)-1)
4336         {
4337           g_warning ("Invalid input string");
4338           g_free (*new_str);
4339           g_free (*pattern);
4340
4341           return FALSE;
4342         }
4343       next_src = g_utf8_next_char (src);
4344
4345       if (underscore)
4346         {
4347           if (c == '_')
4348             *pattern_dest++ = ' ';
4349           else
4350             {
4351               *pattern_dest++ = '_';
4352               if (*accel_key == GDK_KEY_VoidSymbol)
4353                 *accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
4354             }
4355
4356           while (src < next_src)
4357             *dest++ = *src++;
4358
4359           underscore = FALSE;
4360         }
4361       else
4362         {
4363           if (c == '_')
4364             {
4365               underscore = TRUE;
4366               src = next_src;
4367             }
4368           else
4369             {
4370               while (src < next_src)
4371                 *dest++ = *src++;
4372
4373               *pattern_dest++ = ' ';
4374             }
4375         }
4376     }
4377
4378   *dest = 0;
4379   *pattern_dest = 0;
4380
4381   return TRUE;
4382 }
4383
4384 static void
4385 gtk_label_set_uline_text_internal (GtkLabel    *label,
4386                                    const gchar *str)
4387 {
4388   GtkLabelPrivate *priv = label->priv;
4389   guint accel_key = GDK_KEY_VoidSymbol;
4390   gchar *new_str;
4391   gchar *pattern;
4392
4393   g_return_if_fail (GTK_IS_LABEL (label));
4394   g_return_if_fail (str != NULL);
4395
4396   /* Split text into the base text and a separate pattern
4397    * of underscores.
4398    */
4399   if (!separate_uline_pattern (str, &accel_key, &new_str, &pattern))
4400     return;
4401
4402   gtk_label_set_text_internal (label, new_str);
4403   gtk_label_set_pattern_internal (label, pattern, TRUE);
4404   priv->mnemonic_keyval = accel_key;
4405
4406   g_free (pattern);
4407 }
4408
4409 /**
4410  * gtk_label_set_text_with_mnemonic:
4411  * @label: a #GtkLabel
4412  * @str: a string
4413  * 
4414  * Sets the label's text from the string @str.
4415  * If characters in @str are preceded by an underscore, they are underlined
4416  * indicating that they represent a keyboard accelerator called a mnemonic.
4417  * The mnemonic key can be used to activate another widget, chosen 
4418  * automatically, or explicitly using gtk_label_set_mnemonic_widget().
4419  **/
4420 void
4421 gtk_label_set_text_with_mnemonic (GtkLabel    *label,
4422                                   const gchar *str)
4423 {
4424   g_return_if_fail (GTK_IS_LABEL (label));
4425   g_return_if_fail (str != NULL);
4426
4427   g_object_freeze_notify (G_OBJECT (label));
4428
4429   gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
4430   gtk_label_set_use_markup_internal (label, FALSE);
4431   gtk_label_set_use_underline_internal (label, TRUE);
4432   
4433   gtk_label_recalculate (label);
4434
4435   g_object_thaw_notify (G_OBJECT (label));
4436 }
4437
4438 static void
4439 gtk_label_realize (GtkWidget *widget)
4440 {
4441   GtkLabel *label = GTK_LABEL (widget);
4442   GtkLabelPrivate *priv = label->priv;
4443
4444   GTK_WIDGET_CLASS (gtk_label_parent_class)->realize (widget);
4445
4446   if (priv->select_info)
4447     gtk_label_create_window (label);
4448 }
4449
4450 static void
4451 gtk_label_unrealize (GtkWidget *widget)
4452 {
4453   GtkLabel *label = GTK_LABEL (widget);
4454   GtkLabelPrivate *priv = label->priv;
4455
4456   if (priv->select_info)
4457     gtk_label_destroy_window (label);
4458
4459   GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize (widget);
4460 }
4461
4462 static void
4463 gtk_label_map (GtkWidget *widget)
4464 {
4465   GtkLabel *label = GTK_LABEL (widget);
4466   GtkLabelPrivate *priv = label->priv;
4467
4468   GTK_WIDGET_CLASS (gtk_label_parent_class)->map (widget);
4469
4470   if (priv->select_info)
4471     gdk_window_show (priv->select_info->window);
4472 }
4473
4474 static void
4475 gtk_label_unmap (GtkWidget *widget)
4476 {
4477   GtkLabel *label = GTK_LABEL (widget);
4478   GtkLabelPrivate *priv = label->priv;
4479
4480   if (priv->select_info)
4481     gdk_window_hide (priv->select_info->window);
4482
4483   GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap (widget);
4484 }
4485
4486 static void
4487 window_to_layout_coords (GtkLabel *label,
4488                          gint     *x,
4489                          gint     *y)
4490 {
4491   GtkAllocation allocation;
4492   gint lx, ly;
4493   GtkWidget *widget;
4494
4495   widget = GTK_WIDGET (label);
4496   
4497   /* get layout location in widget->window coords */
4498   get_layout_location (label, &lx, &ly);
4499
4500   gtk_widget_get_allocation (widget, &allocation);
4501
4502   if (x)
4503     {
4504       *x += allocation.x; /* go to widget->window */
4505       *x -= lx;                   /* go to layout */
4506     }
4507
4508   if (y)
4509     {
4510       *y += allocation.y; /* go to widget->window */
4511       *y -= ly;                   /* go to layout */
4512     }
4513 }
4514
4515 #if 0
4516 static void
4517 layout_to_window_coords (GtkLabel *label,
4518                          gint     *x,
4519                          gint     *y)
4520 {
4521   gint lx, ly;
4522   GtkWidget *widget;
4523
4524   widget = GTK_WIDGET (label);
4525   
4526   /* get layout location in widget->window coords */
4527   get_layout_location (label, &lx, &ly);
4528   
4529   if (x)
4530     {
4531       *x += lx;                   /* go to widget->window */
4532       *x -= widget->allocation.x; /* go to selection window */
4533     }
4534
4535   if (y)
4536     {
4537       *y += ly;                   /* go to widget->window */
4538       *y -= widget->allocation.y; /* go to selection window */
4539     }
4540 }
4541 #endif
4542
4543 static gboolean
4544 get_layout_index (GtkLabel *label,
4545                   gint      x,
4546                   gint      y,
4547                   gint     *index)
4548 {
4549   GtkLabelPrivate *priv = label->priv;
4550   gint trailing = 0;
4551   const gchar *cluster;
4552   const gchar *cluster_end;
4553   gboolean inside;
4554
4555   *index = 0;
4556
4557   gtk_label_ensure_layout (label);
4558
4559   window_to_layout_coords (label, &x, &y);
4560
4561   x *= PANGO_SCALE;
4562   y *= PANGO_SCALE;
4563
4564   inside = pango_layout_xy_to_index (priv->layout,
4565                                      x, y,
4566                                      index, &trailing);
4567
4568   cluster = priv->text + *index;
4569   cluster_end = cluster;
4570   while (trailing)
4571     {
4572       cluster_end = g_utf8_next_char (cluster_end);
4573       --trailing;
4574     }
4575
4576   *index += (cluster_end - cluster);
4577
4578   return inside;
4579 }
4580
4581 static void
4582 gtk_label_select_word (GtkLabel *label)
4583 {
4584   GtkLabelPrivate *priv = label->priv;
4585   gint min, max;
4586
4587   gint start_index = gtk_label_move_backward_word (label, priv->select_info->selection_end);
4588   gint end_index = gtk_label_move_forward_word (label, priv->select_info->selection_end);
4589
4590   min = MIN (priv->select_info->selection_anchor,
4591              priv->select_info->selection_end);
4592   max = MAX (priv->select_info->selection_anchor,
4593              priv->select_info->selection_end);
4594
4595   min = MIN (min, start_index);
4596   max = MAX (max, end_index);
4597
4598   gtk_label_select_region_index (label, min, max);
4599 }
4600
4601 static void
4602 gtk_label_grab_focus (GtkWidget *widget)
4603 {
4604   GtkLabel *label = GTK_LABEL (widget);
4605   GtkLabelPrivate *priv = label->priv;
4606   gboolean select_on_focus;
4607   GtkLabelLink *link;
4608
4609   if (priv->select_info == NULL)
4610     return;
4611
4612   GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
4613
4614   if (priv->select_info->selectable)
4615     {
4616       g_object_get (gtk_widget_get_settings (widget),
4617                     "gtk-label-select-on-focus",
4618                     &select_on_focus,
4619                     NULL);
4620
4621       if (select_on_focus && !priv->in_click)
4622         gtk_label_select_region (label, 0, -1);
4623     }
4624   else
4625     {
4626       if (priv->select_info->links && !priv->in_click)
4627         {
4628           link = priv->select_info->links->data;
4629           priv->select_info->selection_anchor = link->start;
4630           priv->select_info->selection_end = link->start;
4631         }
4632     }
4633 }
4634
4635 static gboolean
4636 gtk_label_focus (GtkWidget        *widget,
4637                  GtkDirectionType  direction)
4638 {
4639   GtkLabel *label = GTK_LABEL (widget);
4640   GtkLabelPrivate *priv = label->priv;
4641   GtkLabelSelectionInfo *info = priv->select_info;
4642   GtkLabelLink *focus_link;
4643   GList *l;
4644
4645   if (!gtk_widget_is_focus (widget))
4646     {
4647       gtk_widget_grab_focus (widget);
4648       if (info)
4649         {
4650           focus_link = gtk_label_get_focus_link (label);
4651           if (focus_link && direction == GTK_DIR_TAB_BACKWARD)
4652             {
4653               l = g_list_last (info->links);
4654               focus_link = l->data;
4655               info->selection_anchor = focus_link->start;
4656               info->selection_end = focus_link->start;
4657             }
4658         }
4659
4660       return TRUE;
4661     }
4662
4663   if (!info)
4664     return FALSE;
4665
4666   if (info->selectable)
4667     {
4668       gint index;
4669
4670       if (info->selection_anchor != info->selection_end)
4671         goto out;
4672
4673       index = info->selection_anchor;
4674
4675       if (direction == GTK_DIR_TAB_FORWARD)
4676         for (l = info->links; l; l = l->next)
4677           {
4678             GtkLabelLink *link = l->data;
4679
4680             if (link->start > index)
4681               {
4682                 gtk_label_select_region_index (label, link->start, link->start);
4683                 return TRUE;
4684               }
4685           }
4686       else if (direction == GTK_DIR_TAB_BACKWARD)
4687         for (l = g_list_last (info->links); l; l = l->prev)
4688           {
4689             GtkLabelLink *link = l->data;
4690
4691             if (link->end < index)
4692               {
4693                 gtk_label_select_region_index (label, link->start, link->start);
4694                 return TRUE;
4695               }
4696           }
4697
4698       goto out;
4699     }
4700   else
4701     {
4702       focus_link = gtk_label_get_focus_link (label);
4703       switch (direction)
4704         {
4705         case GTK_DIR_TAB_FORWARD:
4706           if (focus_link)
4707             {
4708               l = g_list_find (info->links, focus_link);
4709               l = l->next;
4710             }
4711           else
4712             l = info->links;
4713           break;
4714
4715         case GTK_DIR_TAB_BACKWARD:
4716           if (focus_link)
4717             {
4718               l = g_list_find (info->links, focus_link);
4719               l = l->prev;
4720             }
4721           else
4722             l = g_list_last (info->links);
4723           break;
4724
4725         default:
4726           goto out;
4727         }
4728
4729       if (l)
4730         {
4731           focus_link = l->data;
4732           info->selection_anchor = focus_link->start;
4733           info->selection_end = focus_link->start;
4734           gtk_widget_queue_draw (widget);
4735
4736           return TRUE;
4737         }
4738     }
4739
4740 out:
4741
4742   return FALSE;
4743 }
4744
4745 static gboolean
4746 gtk_label_button_press (GtkWidget      *widget,
4747                         GdkEventButton *event)
4748 {
4749   GtkLabel *label = GTK_LABEL (widget);
4750   GtkLabelPrivate *priv = label->priv;
4751   GtkLabelSelectionInfo *info = priv->select_info;
4752   gint index = 0;
4753   gint min, max;
4754
4755   if (info == NULL)
4756     return FALSE;
4757
4758   if (info->active_link)
4759     {
4760       if (event->button == 1)
4761         {
4762           info->link_clicked = 1;
4763           gtk_widget_queue_draw (widget);
4764         }
4765       else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4766         {
4767           info->link_clicked = 1;
4768           gtk_label_do_popup (label, event);
4769           return TRUE;
4770         }
4771     }
4772
4773   if (!info->selectable)
4774     return FALSE;
4775
4776   info->in_drag = FALSE;
4777   info->select_words = FALSE;
4778
4779   if (event->button == 1)
4780     {
4781       if (!gtk_widget_has_focus (widget))
4782         {
4783           priv->in_click = TRUE;
4784           gtk_widget_grab_focus (widget);
4785           priv->in_click = FALSE;
4786         }
4787
4788       if (event->type == GDK_3BUTTON_PRESS)
4789         {
4790           gtk_label_select_region_index (label, 0, strlen (priv->text));
4791           return TRUE;
4792         }
4793
4794       if (event->type == GDK_2BUTTON_PRESS)
4795         {
4796           info->select_words = TRUE;
4797           gtk_label_select_word (label);
4798           return TRUE;
4799         }
4800
4801       get_layout_index (label, event->x, event->y, &index);
4802
4803       min = MIN (info->selection_anchor, info->selection_end);
4804       max = MAX (info->selection_anchor, info->selection_end);
4805
4806       if ((info->selection_anchor != info->selection_end) &&
4807           (event->state & GDK_SHIFT_MASK))
4808         {
4809           /* extend (same as motion) */
4810           min = MIN (min, index);
4811           max = MAX (max, index);
4812
4813           /* ensure the anchor is opposite index */
4814           if (index == min)
4815             {
4816               gint tmp = min;
4817               min = max;
4818               max = tmp;
4819             }
4820
4821           gtk_label_select_region_index (label, min, max);
4822         }
4823       else
4824         {
4825           if (event->type == GDK_3BUTTON_PRESS)
4826             gtk_label_select_region_index (label, 0, strlen (priv->text));
4827           else if (event->type == GDK_2BUTTON_PRESS)
4828             gtk_label_select_word (label);
4829           else if (min < max && min <= index && index <= max)
4830             {
4831               info->in_drag = TRUE;
4832               info->drag_start_x = event->x;
4833               info->drag_start_y = event->y;
4834             }
4835           else
4836             /* start a replacement */
4837             gtk_label_select_region_index (label, index, index);
4838         }
4839
4840       return TRUE;
4841     }
4842   else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
4843     {
4844       gtk_label_do_popup (label, event);
4845
4846       return TRUE;
4847     }
4848   return FALSE;
4849 }
4850
4851 static gboolean
4852 gtk_label_button_release (GtkWidget      *widget,
4853                           GdkEventButton *event)
4854
4855 {
4856   GtkLabel *label = GTK_LABEL (widget);
4857   GtkLabelPrivate *priv = label->priv;
4858   GtkLabelSelectionInfo *info = priv->select_info;
4859   gint index;
4860
4861   if (info == NULL)
4862     return FALSE;
4863
4864   if (info->in_drag)
4865     {
4866       info->in_drag = 0;
4867
4868       get_layout_index (label, event->x, event->y, &index);
4869       gtk_label_select_region_index (label, index, index);
4870
4871       return FALSE;
4872     }
4873
4874   if (event->button != 1)
4875     return FALSE;
4876
4877   if (info->active_link &&
4878       info->selection_anchor == info->selection_end &&
4879       info->link_clicked)
4880     {
4881       emit_activate_link (label, info->active_link);
4882       info->link_clicked = 0;
4883
4884       return TRUE;
4885     }
4886
4887   /* The goal here is to return TRUE iff we ate the
4888    * button press to start selecting.
4889    */
4890   return TRUE;
4891 }
4892
4893 static void
4894 connect_mnemonics_visible_notify (GtkLabel *label)
4895 {
4896   GtkLabelPrivate *priv = label->priv;
4897   GtkWidget *toplevel;
4898   gboolean connected;
4899
4900   toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4901
4902   if (!GTK_IS_WINDOW (toplevel))
4903     return;
4904
4905   /* always set up this widgets initial value */
4906   priv->mnemonics_visible =
4907     gtk_window_get_mnemonics_visible (GTK_WINDOW (toplevel));
4908
4909   connected =
4910     GPOINTER_TO_INT (g_object_get_data (G_OBJECT (toplevel),
4911                                         "gtk-label-mnemonics-visible-connected"));
4912
4913   if (!connected)
4914     {
4915       g_signal_connect (toplevel,
4916                         "notify::mnemonics-visible",
4917                         G_CALLBACK (label_mnemonics_visible_changed),
4918                         label);
4919       g_object_set_data (G_OBJECT (toplevel),
4920                          "gtk-label-mnemonics-visible-connected",
4921                          GINT_TO_POINTER (1));
4922     }
4923 }
4924
4925 static void
4926 drag_begin_cb (GtkWidget      *widget,
4927                GdkDragContext *context,
4928                gpointer        data)
4929 {
4930   GtkLabel *label = GTK_LABEL (widget);
4931   GtkLabelPrivate *priv = label->priv;
4932   cairo_surface_t *surface = NULL;
4933
4934   g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
4935
4936   if ((priv->select_info->selection_anchor !=
4937        priv->select_info->selection_end) &&
4938       priv->text)
4939     {
4940       gint start, end;
4941       gint len;
4942
4943       start = MIN (priv->select_info->selection_anchor,
4944                    priv->select_info->selection_end);
4945       end = MAX (priv->select_info->selection_anchor,
4946                  priv->select_info->selection_end);
4947
4948       len = strlen (priv->text);
4949
4950       if (end > len)
4951         end = len;
4952
4953       if (start > len)
4954         start = len;
4955
4956       surface = _gtk_text_util_create_drag_icon (widget,
4957                                                  priv->text + start,
4958                                                  end - start);
4959     }
4960
4961   if (surface)
4962     {
4963       gtk_drag_set_icon_surface (context, surface);
4964       cairo_surface_destroy (surface);
4965     }
4966   else
4967     {
4968       gtk_drag_set_icon_default (context);
4969     }
4970 }
4971
4972 static gboolean
4973 gtk_label_motion (GtkWidget      *widget,
4974                   GdkEventMotion *event)
4975 {
4976   GtkLabel *label = GTK_LABEL (widget);
4977   GtkLabelPrivate *priv = label->priv;
4978   GtkLabelSelectionInfo *info = priv->select_info;
4979   gint index;
4980
4981   if (info == NULL)
4982     return FALSE;
4983
4984   if (info->links && !info->in_drag)
4985     {
4986       GList *l;
4987       GtkLabelLink *link;
4988       gboolean found = FALSE;
4989
4990       if (info->selection_anchor == info->selection_end)
4991         {
4992           if (get_layout_index (label, event->x, event->y, &index))
4993             {
4994               for (l = info->links; l != NULL; l = l->next)
4995                 {
4996                   link = l->data;
4997                   if (index >= link->start && index <= link->end)
4998                     {
4999                       found = TRUE;
5000                       break;
5001                     }
5002                 }
5003             }
5004         }
5005
5006       if (found)
5007         {
5008           if (info->active_link != link)
5009             {
5010               info->link_clicked = 0;
5011               info->active_link = link;
5012               gtk_label_update_cursor (label);
5013               gtk_widget_queue_draw (widget);
5014             }
5015         }
5016       else
5017         {
5018           if (info->active_link != NULL)
5019             {
5020               info->link_clicked = 0;
5021               info->active_link = NULL;
5022               gtk_label_update_cursor (label);
5023               gtk_widget_queue_draw (widget);
5024             }
5025         }
5026     }
5027
5028   if (!info->selectable)
5029     return FALSE;
5030
5031   if ((event->state & GDK_BUTTON1_MASK) == 0)
5032     return FALSE;
5033
5034   if (info->in_drag)
5035     {
5036       if (gtk_drag_check_threshold (widget,
5037                                     info->drag_start_x,
5038                                     info->drag_start_y,
5039                                     event->x, event->y))
5040         {
5041           GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
5042
5043           gtk_target_list_add_text_targets (target_list, 0);
5044
5045           g_signal_connect (widget, "drag-begin",
5046                             G_CALLBACK (drag_begin_cb), NULL);
5047           gtk_drag_begin (widget, target_list,
5048                           GDK_ACTION_COPY,
5049                           1, (GdkEvent *)event);
5050
5051           info->in_drag = FALSE;
5052
5053           gtk_target_list_unref (target_list);
5054         }
5055     }
5056   else
5057     {
5058       gint x, y;
5059
5060       gdk_window_get_device_position (info->window, event->device, &x, &y, NULL);
5061       get_layout_index (label, x, y, &index);
5062
5063       if (info->select_words)
5064         {
5065           gint min, max;
5066           gint old_min, old_max;
5067           gint anchor, end;
5068
5069           min = gtk_label_move_backward_word (label, index);
5070           max = gtk_label_move_forward_word (label, index);
5071
5072           anchor = info->selection_anchor;
5073           end = info->selection_end;
5074
5075           old_min = MIN (anchor, end);
5076           old_max = MAX (anchor, end);
5077
5078           if (min < old_min)
5079             {
5080               anchor = min;
5081               end = old_max;
5082             }
5083           else if (old_max < max)
5084             {
5085               anchor = max;
5086               end = old_min;
5087             }
5088           else if (anchor == old_min)
5089             {
5090               if (anchor != min)
5091                 anchor = max;
5092             }
5093           else
5094             {
5095               if (anchor != max)
5096                 anchor = min;
5097             }
5098
5099           gtk_label_select_region_index (label, anchor, end);
5100         }
5101       else
5102         gtk_label_select_region_index (label, info->selection_anchor, index);
5103     }
5104
5105   return TRUE;
5106 }
5107
5108 static gboolean
5109 gtk_label_leave_notify (GtkWidget        *widget,
5110                         GdkEventCrossing *event)
5111 {
5112   GtkLabel *label = GTK_LABEL (widget);
5113   GtkLabelPrivate *priv = label->priv;
5114
5115   if (priv->select_info)
5116     {
5117       priv->select_info->active_link = NULL;
5118       gtk_label_update_cursor (label);
5119       gtk_widget_queue_draw (widget);
5120     }
5121
5122   if (GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event)
5123     return GTK_WIDGET_CLASS (gtk_label_parent_class)->leave_notify_event (widget, event);
5124
5125  return FALSE;
5126 }
5127
5128 static void
5129 gtk_label_create_window (GtkLabel *label)
5130 {
5131   GtkLabelPrivate *priv = label->priv;
5132   GtkAllocation allocation;
5133   GtkWidget *widget;
5134   GdkWindowAttr attributes;
5135   gint attributes_mask;
5136
5137   g_assert (priv->select_info);
5138   widget = GTK_WIDGET (label);
5139   g_assert (gtk_widget_get_realized (widget));
5140
5141   if (priv->select_info->window)
5142     return;
5143
5144   gtk_widget_get_allocation (widget, &allocation);
5145
5146   attributes.x = allocation.x;
5147   attributes.y = allocation.y;
5148   attributes.width = allocation.width;
5149   attributes.height = allocation.height;
5150   attributes.window_type = GDK_WINDOW_CHILD;
5151   attributes.wclass = GDK_INPUT_ONLY;
5152   attributes.override_redirect = TRUE;
5153   attributes.event_mask = gtk_widget_get_events (widget) |
5154     GDK_BUTTON_PRESS_MASK        |
5155     GDK_BUTTON_RELEASE_MASK      |
5156     GDK_LEAVE_NOTIFY_MASK        |
5157     GDK_BUTTON_MOTION_MASK       |
5158     GDK_POINTER_MOTION_MASK      |
5159     GDK_POINTER_MOTION_HINT_MASK;
5160   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR;
5161   if (gtk_widget_is_sensitive (widget))
5162     {
5163       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
5164                                                       GDK_XTERM);
5165       attributes_mask |= GDK_WA_CURSOR;
5166     }
5167
5168
5169   priv->select_info->window = gdk_window_new (gtk_widget_get_window (widget),
5170                                                &attributes, attributes_mask);
5171   gdk_window_set_user_data (priv->select_info->window, widget);
5172
5173   if (attributes_mask & GDK_WA_CURSOR)
5174     g_object_unref (attributes.cursor);
5175 }
5176
5177 static void
5178 gtk_label_destroy_window (GtkLabel *label)
5179 {
5180   GtkLabelPrivate *priv = label->priv;
5181
5182   g_assert (priv->select_info);
5183
5184   if (priv->select_info->window == NULL)
5185     return;
5186
5187   gdk_window_set_user_data (priv->select_info->window, NULL);
5188   gdk_window_destroy (priv->select_info->window);
5189   priv->select_info->window = NULL;
5190 }
5191
5192 static void
5193 gtk_label_ensure_select_info (GtkLabel *label)
5194 {
5195   GtkLabelPrivate *priv = label->priv;
5196
5197   if (priv->select_info == NULL)
5198     {
5199       priv->select_info = g_new0 (GtkLabelSelectionInfo, 1);
5200
5201       gtk_widget_set_can_focus (GTK_WIDGET (label), TRUE);
5202
5203       if (gtk_widget_get_realized (GTK_WIDGET (label)))
5204         gtk_label_create_window (label);
5205
5206       if (gtk_widget_get_mapped (GTK_WIDGET (label)))
5207         gdk_window_show (priv->select_info->window);
5208     }
5209 }
5210
5211 static void
5212 gtk_label_clear_select_info (GtkLabel *label)
5213 {
5214   GtkLabelPrivate *priv = label->priv;
5215
5216   if (priv->select_info == NULL)
5217     return;
5218
5219   if (!priv->select_info->selectable && !priv->select_info->links)
5220     {
5221       gtk_label_destroy_window (label);
5222
5223       g_free (priv->select_info);
5224       priv->select_info = NULL;
5225
5226       gtk_widget_set_can_focus (GTK_WIDGET (label), FALSE);
5227     }
5228 }
5229
5230 /**
5231  * gtk_label_set_selectable:
5232  * @label: a #GtkLabel
5233  * @setting: %TRUE to allow selecting text in the label
5234  *
5235  * Selectable labels allow the user to select text from the label, for
5236  * copy-and-paste.
5237  **/
5238 void
5239 gtk_label_set_selectable (GtkLabel *label,
5240                           gboolean  setting)
5241 {
5242   GtkLabelPrivate *priv;
5243   gboolean old_setting;
5244
5245   g_return_if_fail (GTK_IS_LABEL (label));
5246
5247   priv = label->priv;
5248
5249   setting = setting != FALSE;
5250   old_setting = priv->select_info && priv->select_info->selectable;
5251
5252   if (setting)
5253     {
5254       gtk_label_ensure_select_info (label);
5255       priv->select_info->selectable = TRUE;
5256       gtk_label_update_cursor (label);
5257     }
5258   else
5259     {
5260       if (old_setting)
5261         {
5262           /* unselect, to give up the selection */
5263           gtk_label_select_region (label, 0, 0);
5264
5265           priv->select_info->selectable = FALSE;
5266           gtk_label_clear_select_info (label);
5267           gtk_label_update_cursor (label);
5268         }
5269     }
5270   if (setting != old_setting)
5271     {
5272       g_object_freeze_notify (G_OBJECT (label));
5273       g_object_notify (G_OBJECT (label), "selectable");
5274       g_object_notify (G_OBJECT (label), "cursor-position");
5275       g_object_notify (G_OBJECT (label), "selection-bound");
5276       g_object_thaw_notify (G_OBJECT (label));
5277       gtk_widget_queue_draw (GTK_WIDGET (label));
5278     }
5279 }
5280
5281 /**
5282  * gtk_label_get_selectable:
5283  * @label: a #GtkLabel
5284  * 
5285  * Gets the value set by gtk_label_set_selectable().
5286  * 
5287  * Return value: %TRUE if the user can copy text from the label
5288  **/
5289 gboolean
5290 gtk_label_get_selectable (GtkLabel *label)
5291 {
5292   GtkLabelPrivate *priv;
5293
5294   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5295
5296   priv = label->priv;
5297
5298   return priv->select_info && priv->select_info->selectable;
5299 }
5300
5301 /**
5302  * gtk_label_set_angle:
5303  * @label: a #GtkLabel
5304  * @angle: the angle that the baseline of the label makes with
5305  *   the horizontal, in degrees, measured counterclockwise
5306  * 
5307  * Sets the angle of rotation for the label. An angle of 90 reads from
5308  * from bottom to top, an angle of 270, from top to bottom. The angle
5309  * setting for the label is ignored if the label is selectable,
5310  * wrapped, or ellipsized.
5311  *
5312  * Since: 2.6
5313  **/
5314 void
5315 gtk_label_set_angle (GtkLabel *label,
5316                      gdouble   angle)
5317 {
5318   GtkLabelPrivate *priv;
5319
5320   g_return_if_fail (GTK_IS_LABEL (label));
5321
5322   priv = label->priv;
5323
5324   /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
5325    * double property ranges are inclusive, and changing 360 to 0 would
5326    * make a property editor behave strangely.
5327    */
5328   if (angle < 0 || angle > 360.0)
5329     angle = angle - 360. * floor (angle / 360.);
5330
5331   if (priv->angle != angle)
5332     {
5333       priv->angle = angle;
5334       
5335       gtk_label_clear_layout (label);
5336       gtk_widget_queue_resize (GTK_WIDGET (label));
5337
5338       g_object_notify (G_OBJECT (label), "angle");
5339     }
5340 }
5341
5342 /**
5343  * gtk_label_get_angle:
5344  * @label: a #GtkLabel
5345  * 
5346  * Gets the angle of rotation for the label. See
5347  * gtk_label_set_angle().
5348  * 
5349  * Return value: the angle of rotation for the label
5350  *
5351  * Since: 2.6
5352  **/
5353 gdouble
5354 gtk_label_get_angle  (GtkLabel *label)
5355 {
5356   g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
5357   
5358   return label->priv->angle;
5359 }
5360
5361 static void
5362 gtk_label_set_selection_text (GtkLabel         *label,
5363                               GtkSelectionData *selection_data)
5364 {
5365   GtkLabelPrivate *priv = label->priv;
5366
5367   if ((priv->select_info->selection_anchor !=
5368        priv->select_info->selection_end) &&
5369       priv->text)
5370     {
5371       gint start, end;
5372       gint len;
5373
5374       start = MIN (priv->select_info->selection_anchor,
5375                    priv->select_info->selection_end);
5376       end = MAX (priv->select_info->selection_anchor,
5377                  priv->select_info->selection_end);
5378
5379       len = strlen (priv->text);
5380
5381       if (end > len)
5382         end = len;
5383
5384       if (start > len)
5385         start = len;
5386
5387       gtk_selection_data_set_text (selection_data,
5388                                    priv->text + start,
5389                                    end - start);
5390     }
5391 }
5392
5393 static void
5394 gtk_label_drag_data_get (GtkWidget        *widget,
5395                          GdkDragContext   *context,
5396                          GtkSelectionData *selection_data,
5397                          guint             info,
5398                          guint             time)
5399 {
5400   gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
5401 }
5402
5403 static void
5404 get_text_callback (GtkClipboard     *clipboard,
5405                    GtkSelectionData *selection_data,
5406                    guint             info,
5407                    gpointer          user_data_or_owner)
5408 {
5409   gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
5410 }
5411
5412 static void
5413 clear_text_callback (GtkClipboard     *clipboard,
5414                      gpointer          user_data_or_owner)
5415 {
5416   GtkLabel *label;
5417   GtkLabelPrivate *priv;
5418
5419   label = GTK_LABEL (user_data_or_owner);
5420   priv = label->priv;
5421
5422   if (priv->select_info)
5423     {
5424       priv->select_info->selection_anchor = priv->select_info->selection_end;
5425
5426       gtk_widget_queue_draw (GTK_WIDGET (label));
5427     }
5428 }
5429
5430 static void
5431 gtk_label_select_region_index (GtkLabel *label,
5432                                gint      anchor_index,
5433                                gint      end_index)
5434 {
5435   GtkLabelPrivate *priv;
5436
5437   g_return_if_fail (GTK_IS_LABEL (label));
5438
5439   priv = label->priv;
5440
5441   if (priv->select_info && priv->select_info->selectable)
5442     {
5443       GtkClipboard *clipboard;
5444
5445       if (priv->select_info->selection_anchor == anchor_index &&
5446           priv->select_info->selection_end == end_index)
5447         return;
5448
5449       priv->select_info->selection_anchor = anchor_index;
5450       priv->select_info->selection_end = end_index;
5451
5452       clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
5453                                             GDK_SELECTION_PRIMARY);
5454
5455       if (anchor_index != end_index)
5456         {
5457           GtkTargetList *list;
5458           GtkTargetEntry *targets;
5459           gint n_targets;
5460
5461           list = gtk_target_list_new (NULL, 0);
5462           gtk_target_list_add_text_targets (list, 0);
5463           targets = gtk_target_table_new_from_list (list, &n_targets);
5464
5465           gtk_clipboard_set_with_owner (clipboard,
5466                                         targets, n_targets,
5467                                         get_text_callback,
5468                                         clear_text_callback,
5469                                         G_OBJECT (label));
5470
5471           gtk_target_table_free (targets, n_targets);
5472           gtk_target_list_unref (list);
5473         }
5474       else
5475         {
5476           if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
5477             gtk_clipboard_clear (clipboard);
5478         }
5479
5480       gtk_widget_queue_draw (GTK_WIDGET (label));
5481
5482       g_object_freeze_notify (G_OBJECT (label));
5483       g_object_notify (G_OBJECT (label), "cursor-position");
5484       g_object_notify (G_OBJECT (label), "selection-bound");
5485       g_object_thaw_notify (G_OBJECT (label));
5486     }
5487 }
5488
5489 /**
5490  * gtk_label_select_region:
5491  * @label: a #GtkLabel
5492  * @start_offset: start offset (in characters not bytes)
5493  * @end_offset: end offset (in characters not bytes)
5494  *
5495  * Selects a range of characters in the label, if the label is selectable.
5496  * See gtk_label_set_selectable(). If the label is not selectable,
5497  * this function has no effect. If @start_offset or
5498  * @end_offset are -1, then the end of the label will be substituted.
5499  **/
5500 void
5501 gtk_label_select_region  (GtkLabel *label,
5502                           gint      start_offset,
5503                           gint      end_offset)
5504 {
5505   GtkLabelPrivate *priv;
5506
5507   g_return_if_fail (GTK_IS_LABEL (label));
5508
5509   priv = label->priv;
5510
5511   if (priv->text && priv->select_info)
5512     {
5513       if (start_offset < 0)
5514         start_offset = g_utf8_strlen (priv->text, -1);
5515       
5516       if (end_offset < 0)
5517         end_offset = g_utf8_strlen (priv->text, -1);
5518       
5519       gtk_label_select_region_index (label,
5520                                      g_utf8_offset_to_pointer (priv->text, start_offset) - priv->text,
5521                                      g_utf8_offset_to_pointer (priv->text, end_offset) - priv->text);
5522     }
5523 }
5524
5525 /**
5526  * gtk_label_get_selection_bounds:
5527  * @label: a #GtkLabel
5528  * @start: (out): return location for start of selection, as a character offset
5529  * @end: (out): return location for end of selection, as a character offset
5530  * 
5531  * Gets the selected range of characters in the label, returning %TRUE
5532  * if there's a selection.
5533  * 
5534  * Return value: %TRUE if selection is non-empty
5535  **/
5536 gboolean
5537 gtk_label_get_selection_bounds (GtkLabel  *label,
5538                                 gint      *start,
5539                                 gint      *end)
5540 {
5541   GtkLabelPrivate *priv;
5542
5543   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5544
5545   priv = label->priv;
5546
5547   if (priv->select_info == NULL)
5548     {
5549       /* not a selectable label */
5550       if (start)
5551         *start = 0;
5552       if (end)
5553         *end = 0;
5554
5555       return FALSE;
5556     }
5557   else
5558     {
5559       gint start_index, end_index;
5560       gint start_offset, end_offset;
5561       gint len;
5562       
5563       start_index = MIN (priv->select_info->selection_anchor,
5564                    priv->select_info->selection_end);
5565       end_index = MAX (priv->select_info->selection_anchor,
5566                  priv->select_info->selection_end);
5567
5568       len = strlen (priv->text);
5569
5570       if (end_index > len)
5571         end_index = len;
5572
5573       if (start_index > len)
5574         start_index = len;
5575       
5576       start_offset = g_utf8_strlen (priv->text, start_index);
5577       end_offset = g_utf8_strlen (priv->text, end_index);
5578
5579       if (start_offset > end_offset)
5580         {
5581           gint tmp = start_offset;
5582           start_offset = end_offset;
5583           end_offset = tmp;
5584         }
5585       
5586       if (start)
5587         *start = start_offset;
5588
5589       if (end)
5590         *end = end_offset;
5591
5592       return start_offset != end_offset;
5593     }
5594 }
5595
5596
5597 /**
5598  * gtk_label_get_layout:
5599  * @label: a #GtkLabel
5600  * 
5601  * Gets the #PangoLayout used to display the label.
5602  * The layout is useful to e.g. convert text positions to
5603  * pixel positions, in combination with gtk_label_get_layout_offsets().
5604  * The returned layout is owned by the @label so need not be
5605  * freed by the caller. The @label is free to recreate its layout at
5606  * any time, so it should be considered read-only.
5607  *
5608  * Return value: (transfer none): the #PangoLayout for this label
5609  **/
5610 PangoLayout*
5611 gtk_label_get_layout (GtkLabel *label)
5612 {
5613   GtkLabelPrivate *priv;
5614
5615   g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
5616
5617   priv = label->priv;
5618
5619   gtk_label_ensure_layout (label);
5620
5621   return priv->layout;
5622 }
5623
5624 /**
5625  * gtk_label_get_layout_offsets:
5626  * @label: a #GtkLabel
5627  * @x: (out) (allow-none): location to store X offset of layout, or %NULL
5628  * @y: (out) (allow-none): location to store Y offset of layout, or %NULL
5629  *
5630  * Obtains the coordinates where the label will draw the #PangoLayout
5631  * representing the text in the label; useful to convert mouse events
5632  * into coordinates inside the #PangoLayout, e.g. to take some action
5633  * if some part of the label is clicked. Of course you will need to
5634  * create a #GtkEventBox to receive the events, and pack the label
5635  * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
5636  * when using the #PangoLayout functions you need to convert to
5637  * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
5638  **/
5639 void
5640 gtk_label_get_layout_offsets (GtkLabel *label,
5641                               gint     *x,
5642                               gint     *y)
5643 {
5644   g_return_if_fail (GTK_IS_LABEL (label));
5645
5646   gtk_label_ensure_layout (label);
5647
5648   get_layout_location (label, x, y);
5649 }
5650
5651 /**
5652  * gtk_label_set_use_markup:
5653  * @label: a #GtkLabel
5654  * @setting: %TRUE if the label's text should be parsed for markup.
5655  *
5656  * Sets whether the text of the label contains markup in <link
5657  * linkend="PangoMarkupFormat">Pango's text markup
5658  * language</link>. See gtk_label_set_markup().
5659  **/
5660 void
5661 gtk_label_set_use_markup (GtkLabel *label,
5662                           gboolean  setting)
5663 {
5664   g_return_if_fail (GTK_IS_LABEL (label));
5665
5666   gtk_label_set_use_markup_internal (label, setting);
5667   gtk_label_recalculate (label);
5668 }
5669
5670 /**
5671  * gtk_label_get_use_markup:
5672  * @label: a #GtkLabel
5673  *
5674  * Returns whether the label's text is interpreted as marked up with
5675  * the <link linkend="PangoMarkupFormat">Pango text markup
5676  * language</link>. See gtk_label_set_use_markup ().
5677  *
5678  * Return value: %TRUE if the label's text will be parsed for markup.
5679  **/
5680 gboolean
5681 gtk_label_get_use_markup (GtkLabel *label)
5682 {
5683   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5684
5685   return label->priv->use_markup;
5686 }
5687
5688 /**
5689  * gtk_label_set_use_underline:
5690  * @label: a #GtkLabel
5691  * @setting: %TRUE if underlines in the text indicate mnemonics
5692  *
5693  * If true, an underline in the text indicates the next character should be
5694  * used for the mnemonic accelerator key.
5695  */
5696 void
5697 gtk_label_set_use_underline (GtkLabel *label,
5698                              gboolean  setting)
5699 {
5700   g_return_if_fail (GTK_IS_LABEL (label));
5701
5702   gtk_label_set_use_underline_internal (label, setting);
5703   gtk_label_recalculate (label);
5704 }
5705
5706 /**
5707  * gtk_label_get_use_underline:
5708  * @label: a #GtkLabel
5709  *
5710  * Returns whether an embedded underline in the label indicates a
5711  * mnemonic. See gtk_label_set_use_underline().
5712  *
5713  * Return value: %TRUE whether an embedded underline in the label indicates
5714  *               the mnemonic accelerator keys.
5715  **/
5716 gboolean
5717 gtk_label_get_use_underline (GtkLabel *label)
5718 {
5719   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5720
5721   return label->priv->use_underline;
5722 }
5723
5724 /**
5725  * gtk_label_set_single_line_mode:
5726  * @label: a #GtkLabel
5727  * @single_line_mode: %TRUE if the label should be in single line mode
5728  *
5729  * Sets whether the label is in single line mode.
5730  *
5731  * Since: 2.6
5732  */
5733 void
5734 gtk_label_set_single_line_mode (GtkLabel *label,
5735                                 gboolean single_line_mode)
5736 {
5737   GtkLabelPrivate *priv;
5738
5739   g_return_if_fail (GTK_IS_LABEL (label));
5740
5741   priv = label->priv;
5742
5743   single_line_mode = single_line_mode != FALSE;
5744
5745   if (priv->single_line_mode != single_line_mode)
5746     {
5747       priv->single_line_mode = single_line_mode;
5748
5749       gtk_label_clear_layout (label);
5750       gtk_widget_queue_resize (GTK_WIDGET (label));
5751
5752       g_object_notify (G_OBJECT (label), "single-line-mode");
5753     }
5754 }
5755
5756 /**
5757  * gtk_label_get_single_line_mode:
5758  * @label: a #GtkLabel
5759  *
5760  * Returns whether the label is in single line mode.
5761  *
5762  * Return value: %TRUE when the label is in single line mode.
5763  *
5764  * Since: 2.6
5765  **/
5766 gboolean
5767 gtk_label_get_single_line_mode  (GtkLabel *label)
5768 {
5769   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
5770
5771   return label->priv->single_line_mode;
5772 }
5773
5774 /* Compute the X position for an offset that corresponds to the "more important
5775  * cursor position for that offset. We use this when trying to guess to which
5776  * end of the selection we should go to when the user hits the left or
5777  * right arrow key.
5778  */
5779 static void
5780 get_better_cursor (GtkLabel *label,
5781                    gint      index,
5782                    gint      *x,
5783                    gint      *y)
5784 {
5785   GtkLabelPrivate *priv = label->priv;
5786   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5787   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5788   PangoDirection cursor_direction = get_cursor_direction (label);
5789   gboolean split_cursor;
5790   PangoRectangle strong_pos, weak_pos;
5791   
5792   g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5793                 "gtk-split-cursor", &split_cursor,
5794                 NULL);
5795
5796   gtk_label_ensure_layout (label);
5797   
5798   pango_layout_get_cursor_pos (priv->layout, index,
5799                                &strong_pos, &weak_pos);
5800
5801   if (split_cursor)
5802     {
5803       *x = strong_pos.x / PANGO_SCALE;
5804       *y = strong_pos.y / PANGO_SCALE;
5805     }
5806   else
5807     {
5808       if (keymap_direction == cursor_direction)
5809         {
5810           *x = strong_pos.x / PANGO_SCALE;
5811           *y = strong_pos.y / PANGO_SCALE;
5812         }
5813       else
5814         {
5815           *x = weak_pos.x / PANGO_SCALE;
5816           *y = weak_pos.y / PANGO_SCALE;
5817         }
5818     }
5819 }
5820
5821
5822 static gint
5823 gtk_label_move_logically (GtkLabel *label,
5824                           gint      start,
5825                           gint      count)
5826 {
5827   GtkLabelPrivate *priv = label->priv;
5828   gint offset = g_utf8_pointer_to_offset (priv->text,
5829                                           priv->text + start);
5830
5831   if (priv->text)
5832     {
5833       PangoLogAttr *log_attrs;
5834       gint n_attrs;
5835       gint length;
5836
5837       gtk_label_ensure_layout (label);
5838       
5839       length = g_utf8_strlen (priv->text, -1);
5840
5841       pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5842
5843       while (count > 0 && offset < length)
5844         {
5845           do
5846             offset++;
5847           while (offset < length && !log_attrs[offset].is_cursor_position);
5848           
5849           count--;
5850         }
5851       while (count < 0 && offset > 0)
5852         {
5853           do
5854             offset--;
5855           while (offset > 0 && !log_attrs[offset].is_cursor_position);
5856           
5857           count++;
5858         }
5859       
5860       g_free (log_attrs);
5861     }
5862
5863   return g_utf8_offset_to_pointer (priv->text, offset) - priv->text;
5864 }
5865
5866 static gint
5867 gtk_label_move_visually (GtkLabel *label,
5868                          gint      start,
5869                          gint      count)
5870 {
5871   GtkLabelPrivate *priv = label->priv;
5872   gint index;
5873
5874   index = start;
5875   
5876   while (count != 0)
5877     {
5878       int new_index, new_trailing;
5879       gboolean split_cursor;
5880       gboolean strong;
5881
5882       gtk_label_ensure_layout (label);
5883
5884       g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
5885                     "gtk-split-cursor", &split_cursor,
5886                     NULL);
5887
5888       if (split_cursor)
5889         strong = TRUE;
5890       else
5891         {
5892           GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
5893           PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
5894
5895           strong = keymap_direction == get_cursor_direction (label);
5896         }
5897       
5898       if (count > 0)
5899         {
5900           pango_layout_move_cursor_visually (priv->layout, strong, index, 0, 1, &new_index, &new_trailing);
5901           count--;
5902         }
5903       else
5904         {
5905           pango_layout_move_cursor_visually (priv->layout, strong, index, 0, -1, &new_index, &new_trailing);
5906           count++;
5907         }
5908
5909       if (new_index < 0 || new_index == G_MAXINT)
5910         break;
5911
5912       index = new_index;
5913       
5914       while (new_trailing--)
5915         index = g_utf8_next_char (priv->text + new_index) - priv->text;
5916     }
5917   
5918   return index;
5919 }
5920
5921 static gint
5922 gtk_label_move_forward_word (GtkLabel *label,
5923                              gint      start)
5924 {
5925   GtkLabelPrivate *priv = label->priv;
5926   gint new_pos = g_utf8_pointer_to_offset (priv->text,
5927                                            priv->text + start);
5928   gint length;
5929
5930   length = g_utf8_strlen (priv->text, -1);
5931   if (new_pos < length)
5932     {
5933       PangoLogAttr *log_attrs;
5934       gint n_attrs;
5935
5936       gtk_label_ensure_layout (label);
5937
5938       pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5939
5940       /* Find the next word end */
5941       new_pos++;
5942       while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
5943         new_pos++;
5944
5945       g_free (log_attrs);
5946     }
5947
5948   return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5949 }
5950
5951
5952 static gint
5953 gtk_label_move_backward_word (GtkLabel *label,
5954                               gint      start)
5955 {
5956   GtkLabelPrivate *priv = label->priv;
5957   gint new_pos = g_utf8_pointer_to_offset (priv->text,
5958                                            priv->text + start);
5959
5960   if (new_pos > 0)
5961     {
5962       PangoLogAttr *log_attrs;
5963       gint n_attrs;
5964
5965       gtk_label_ensure_layout (label);
5966
5967       pango_layout_get_log_attrs (priv->layout, &log_attrs, &n_attrs);
5968
5969       new_pos -= 1;
5970
5971       /* Find the previous word beginning */
5972       while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
5973         new_pos--;
5974
5975       g_free (log_attrs);
5976     }
5977
5978   return g_utf8_offset_to_pointer (priv->text, new_pos) - priv->text;
5979 }
5980
5981 static void
5982 gtk_label_move_cursor (GtkLabel       *label,
5983                        GtkMovementStep step,
5984                        gint            count,
5985                        gboolean        extend_selection)
5986 {
5987   GtkLabelPrivate *priv = label->priv;
5988   gint old_pos;
5989   gint new_pos;
5990
5991   if (priv->select_info == NULL)
5992     return;
5993
5994   old_pos = new_pos = priv->select_info->selection_end;
5995
5996   if (priv->select_info->selection_end != priv->select_info->selection_anchor &&
5997       !extend_selection)
5998     {
5999       /* If we have a current selection and aren't extending it, move to the
6000        * start/or end of the selection as appropriate
6001        */
6002       switch (step)
6003         {
6004         case GTK_MOVEMENT_VISUAL_POSITIONS:
6005           {
6006             gint end_x, end_y;
6007             gint anchor_x, anchor_y;
6008             gboolean end_is_left;
6009
6010             get_better_cursor (label, priv->select_info->selection_end, &end_x, &end_y);
6011             get_better_cursor (label, priv->select_info->selection_anchor, &anchor_x, &anchor_y);
6012
6013             end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
6014             
6015             if (count < 0)
6016               new_pos = end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6017             else
6018               new_pos = !end_is_left ? priv->select_info->selection_end : priv->select_info->selection_anchor;
6019             break;
6020           }
6021         case GTK_MOVEMENT_LOGICAL_POSITIONS:
6022         case GTK_MOVEMENT_WORDS:
6023           if (count < 0)
6024             new_pos = MIN (priv->select_info->selection_end, priv->select_info->selection_anchor);
6025           else
6026             new_pos = MAX (priv->select_info->selection_end, priv->select_info->selection_anchor);
6027           break;
6028         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6029         case GTK_MOVEMENT_PARAGRAPH_ENDS:
6030         case GTK_MOVEMENT_BUFFER_ENDS:
6031           /* FIXME: Can do better here */
6032           new_pos = count < 0 ? 0 : strlen (priv->text);
6033           break;
6034         case GTK_MOVEMENT_DISPLAY_LINES:
6035         case GTK_MOVEMENT_PARAGRAPHS:
6036         case GTK_MOVEMENT_PAGES:
6037         case GTK_MOVEMENT_HORIZONTAL_PAGES:
6038           break;
6039         }
6040     }
6041   else
6042     {
6043       switch (step)
6044         {
6045         case GTK_MOVEMENT_LOGICAL_POSITIONS:
6046           new_pos = gtk_label_move_logically (label, new_pos, count);
6047           break;
6048         case GTK_MOVEMENT_VISUAL_POSITIONS:
6049           new_pos = gtk_label_move_visually (label, new_pos, count);
6050           if (new_pos == old_pos)
6051             {
6052               if (!extend_selection)
6053                 {
6054                   if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
6055                                                  count > 0 ?
6056                                                  GTK_DIR_RIGHT : GTK_DIR_LEFT))
6057                     {
6058                       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
6059
6060                       if (toplevel)
6061                         gtk_widget_child_focus (toplevel,
6062                                                 count > 0 ?
6063                                                 GTK_DIR_RIGHT : GTK_DIR_LEFT);
6064                     }
6065                 }
6066               else
6067                 {
6068                   gtk_widget_error_bell (GTK_WIDGET (label));
6069                 }
6070             }
6071           break;
6072         case GTK_MOVEMENT_WORDS:
6073           while (count > 0)
6074             {
6075               new_pos = gtk_label_move_forward_word (label, new_pos);
6076               count--;
6077             }
6078           while (count < 0)
6079             {
6080               new_pos = gtk_label_move_backward_word (label, new_pos);
6081               count++;
6082             }
6083           if (new_pos == old_pos)
6084             gtk_widget_error_bell (GTK_WIDGET (label));
6085           break;
6086         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
6087         case GTK_MOVEMENT_PARAGRAPH_ENDS:
6088         case GTK_MOVEMENT_BUFFER_ENDS:
6089           /* FIXME: Can do better here */
6090           new_pos = count < 0 ? 0 : strlen (priv->text);
6091           if (new_pos == old_pos)
6092             gtk_widget_error_bell (GTK_WIDGET (label));
6093           break;
6094         case GTK_MOVEMENT_DISPLAY_LINES:
6095         case GTK_MOVEMENT_PARAGRAPHS:
6096         case GTK_MOVEMENT_PAGES:
6097         case GTK_MOVEMENT_HORIZONTAL_PAGES:
6098           break;
6099         }
6100     }
6101
6102   if (extend_selection)
6103     gtk_label_select_region_index (label,
6104                                    priv->select_info->selection_anchor,
6105                                    new_pos);
6106   else
6107     gtk_label_select_region_index (label, new_pos, new_pos);
6108 }
6109
6110 static void
6111 gtk_label_copy_clipboard (GtkLabel *label)
6112 {
6113   GtkLabelPrivate *priv = label->priv;
6114
6115   if (priv->text && priv->select_info)
6116     {
6117       gint start, end;
6118       gint len;
6119       GtkClipboard *clipboard;
6120
6121       start = MIN (priv->select_info->selection_anchor,
6122                    priv->select_info->selection_end);
6123       end = MAX (priv->select_info->selection_anchor,
6124                  priv->select_info->selection_end);
6125
6126       len = strlen (priv->text);
6127
6128       if (end > len)
6129         end = len;
6130
6131       if (start > len)
6132         start = len;
6133
6134       clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6135
6136       if (start != end)
6137         gtk_clipboard_set_text (clipboard, priv->text + start, end - start);
6138       else
6139         {
6140           GtkLabelLink *link;
6141
6142           link = gtk_label_get_focus_link (label);
6143           if (link)
6144             gtk_clipboard_set_text (clipboard, link->uri, -1);
6145         }
6146     }
6147 }
6148
6149 static void
6150 gtk_label_select_all (GtkLabel *label)
6151 {
6152   GtkLabelPrivate *priv = label->priv;
6153
6154   gtk_label_select_region_index (label, 0, strlen (priv->text));
6155 }
6156
6157 /* Quick hack of a popup menu
6158  */
6159 static void
6160 activate_cb (GtkWidget *menuitem,
6161              GtkLabel  *label)
6162 {
6163   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
6164   g_signal_emit_by_name (label, signal);
6165 }
6166
6167 static void
6168 append_action_signal (GtkLabel     *label,
6169                       GtkWidget    *menu,
6170                       const gchar  *stock_id,
6171                       const gchar  *signal,
6172                       gboolean      sensitive)
6173 {
6174   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
6175
6176   g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
6177   g_signal_connect (menuitem, "activate",
6178                     G_CALLBACK (activate_cb), label);
6179
6180   gtk_widget_set_sensitive (menuitem, sensitive);
6181   
6182   gtk_widget_show (menuitem);
6183   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6184 }
6185
6186 static void
6187 popup_menu_detach (GtkWidget *attach_widget,
6188                    GtkMenu   *menu)
6189 {
6190   GtkLabel *label = GTK_LABEL (attach_widget);
6191   GtkLabelPrivate *priv = label->priv;
6192
6193   if (priv->select_info)
6194     priv->select_info->popup_menu = NULL;
6195 }
6196
6197 static void
6198 popup_position_func (GtkMenu   *menu,
6199                      gint      *x,
6200                      gint      *y,
6201                      gboolean  *push_in,
6202                      gpointer   user_data)
6203 {
6204   GtkLabel *label;
6205   GtkWidget *widget;
6206   GtkAllocation allocation;
6207   GtkRequisition req;
6208   GdkScreen *screen;
6209
6210   label = GTK_LABEL (user_data);
6211   widget = GTK_WIDGET (label);
6212
6213   g_return_if_fail (gtk_widget_get_realized (widget));
6214
6215   screen = gtk_widget_get_screen (widget);
6216   gdk_window_get_origin (gtk_widget_get_window (widget), x, y);
6217
6218   gtk_widget_get_allocation (widget, &allocation);
6219
6220   *x += allocation.x;
6221   *y += allocation.y;
6222
6223   gtk_widget_get_preferred_size (GTK_WIDGET (menu),
6224                                  &req, NULL);
6225
6226   gtk_widget_get_allocation (widget, &allocation);
6227
6228   *x += allocation.width / 2;
6229   *y += allocation.height;
6230
6231   *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
6232   *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
6233 }
6234
6235 static void
6236 open_link_activate_cb (GtkMenuItem *menu_item,
6237                        GtkLabel    *label)
6238 {
6239   GtkLabelLink *link;
6240
6241   link = gtk_label_get_current_link (label);
6242
6243   if (link)
6244     emit_activate_link (label, link);
6245 }
6246
6247 static void
6248 copy_link_activate_cb (GtkMenuItem *menu_item,
6249                        GtkLabel    *label)
6250 {
6251   GtkClipboard *clipboard;
6252   const gchar *uri;
6253
6254   uri = gtk_label_get_current_uri (label);
6255   if (uri)
6256     {
6257       clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label), GDK_SELECTION_CLIPBOARD);
6258       gtk_clipboard_set_text (clipboard, uri, -1);
6259     }
6260 }
6261
6262 static gboolean
6263 gtk_label_popup_menu (GtkWidget *widget)
6264 {
6265   gtk_label_do_popup (GTK_LABEL (widget), NULL);
6266
6267   return TRUE;
6268 }
6269
6270 static void
6271 gtk_label_do_popup (GtkLabel       *label,
6272                     GdkEventButton *event)
6273 {
6274   GtkLabelPrivate *priv = label->priv;
6275   GtkWidget *menuitem;
6276   GtkWidget *menu;
6277   GtkWidget *image;
6278   gboolean have_selection;
6279   GtkLabelLink *link;
6280
6281   if (!priv->select_info)
6282     return;
6283
6284   if (priv->select_info->popup_menu)
6285     gtk_widget_destroy (priv->select_info->popup_menu);
6286
6287   priv->select_info->popup_menu = menu = gtk_menu_new ();
6288
6289   gtk_menu_attach_to_widget (GTK_MENU (menu), GTK_WIDGET (label), popup_menu_detach);
6290
6291   have_selection =
6292     priv->select_info->selection_anchor != priv->select_info->selection_end;
6293
6294   if (event)
6295     {
6296       if (priv->select_info->link_clicked)
6297         link = priv->select_info->active_link;
6298       else
6299         link = NULL;
6300     }
6301   else
6302     link = gtk_label_get_focus_link (label);
6303
6304   if (!have_selection && link)
6305     {
6306       /* Open Link */
6307       menuitem = gtk_image_menu_item_new_with_mnemonic (_("_Open Link"));
6308       gtk_widget_show (menuitem);
6309       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6310
6311       g_signal_connect (G_OBJECT (menuitem), "activate",
6312                         G_CALLBACK (open_link_activate_cb), label);
6313
6314       image = gtk_image_new_from_stock (GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_MENU);
6315       gtk_widget_show (image);
6316       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6317
6318       /* Copy Link Address */
6319       menuitem = gtk_image_menu_item_new_with_mnemonic (_("Copy _Link Address"));
6320       gtk_widget_show (menuitem);
6321       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6322
6323       g_signal_connect (G_OBJECT (menuitem), "activate",
6324                         G_CALLBACK (copy_link_activate_cb), label);
6325
6326       image = gtk_image_new_from_stock (GTK_STOCK_COPY, GTK_ICON_SIZE_MENU);
6327       gtk_widget_show (image);
6328       gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem), image);
6329     }
6330   else
6331     {
6332       append_action_signal (label, menu, GTK_STOCK_CUT, "cut-clipboard", FALSE);
6333       append_action_signal (label, menu, GTK_STOCK_COPY, "copy-clipboard", have_selection);
6334       append_action_signal (label, menu, GTK_STOCK_PASTE, "paste-clipboard", FALSE);
6335   
6336       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
6337       gtk_widget_set_sensitive (menuitem, FALSE);
6338       gtk_widget_show (menuitem);
6339       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6340
6341       menuitem = gtk_separator_menu_item_new ();
6342       gtk_widget_show (menuitem);
6343       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6344
6345       menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
6346       g_signal_connect_swapped (menuitem, "activate",
6347                                 G_CALLBACK (gtk_label_select_all), label);
6348       gtk_widget_show (menuitem);
6349       gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
6350     }
6351
6352   g_signal_emit (label, signals[POPULATE_POPUP], 0, menu);
6353
6354   if (event)
6355     gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6356                     NULL, NULL,
6357                     event->button, event->time);
6358   else
6359     {
6360       gtk_menu_popup (GTK_MENU (menu), NULL, NULL,
6361                       popup_position_func, label,
6362                       0, gtk_get_current_event_time ());
6363       gtk_menu_shell_select_first (GTK_MENU_SHELL (menu), FALSE);
6364     }
6365 }
6366
6367 static void
6368 gtk_label_clear_links (GtkLabel *label)
6369 {
6370   GtkLabelPrivate *priv = label->priv;
6371
6372   if (!priv->select_info)
6373     return;
6374
6375   g_list_foreach (priv->select_info->links, (GFunc)link_free, NULL);
6376   g_list_free (priv->select_info->links);
6377   priv->select_info->links = NULL;
6378   priv->select_info->active_link = NULL;
6379 }
6380
6381 static void
6382 gtk_label_rescan_links (GtkLabel *label)
6383 {
6384   GtkLabelPrivate *priv = label->priv;
6385   PangoLayout *layout = priv->layout;
6386   PangoAttrList *attlist;
6387   PangoAttrIterator *iter;
6388   GList *links;
6389
6390   if (!priv->select_info || !priv->select_info->links)
6391     return;
6392
6393   attlist = pango_layout_get_attributes (layout);
6394
6395   if (attlist == NULL)
6396     return;
6397
6398   iter = pango_attr_list_get_iterator (attlist);
6399
6400   links = priv->select_info->links;
6401
6402   do
6403     {
6404       PangoAttribute *underline;
6405       PangoAttribute *color;
6406
6407       underline = pango_attr_iterator_get (iter, PANGO_ATTR_UNDERLINE);
6408       color = pango_attr_iterator_get (iter, PANGO_ATTR_FOREGROUND);
6409
6410       if (underline != NULL && color != NULL)
6411         {
6412           gint start, end;
6413           PangoRectangle start_pos;
6414           PangoRectangle end_pos;
6415           GtkLabelLink *link;
6416
6417           pango_attr_iterator_range (iter, &start, &end);
6418           pango_layout_index_to_pos (layout, start, &start_pos);
6419           pango_layout_index_to_pos (layout, end, &end_pos);
6420
6421           if (links == NULL)
6422             {
6423               g_warning ("Ran out of links");
6424               break;
6425             }
6426           link = links->data;
6427           links = links->next;
6428           link->start = start;
6429           link->end = end;
6430         }
6431       } while (pango_attr_iterator_next (iter));
6432
6433     pango_attr_iterator_destroy (iter);
6434 }
6435
6436 static gboolean
6437 gtk_label_activate_link (GtkLabel    *label,
6438                          const gchar *uri)
6439 {
6440   GtkWidget *widget = GTK_WIDGET (label);
6441   GError *error = NULL;
6442
6443   if (!gtk_show_uri (gtk_widget_get_screen (widget),
6444                      uri, gtk_get_current_event_time (), &error))
6445     {
6446       g_warning ("Unable to show '%s': %s", uri, error->message);
6447       g_error_free (error);
6448     }
6449
6450   return TRUE;
6451 }
6452
6453 static void
6454 emit_activate_link (GtkLabel     *label,
6455                     GtkLabelLink *link)
6456 {
6457   GtkLabelPrivate *priv = label->priv;
6458   gboolean handled;
6459
6460   g_signal_emit (label, signals[ACTIVATE_LINK], 0, link->uri, &handled);
6461   if (handled && priv->track_links && !link->visited)
6462     {
6463       link->visited = TRUE;
6464       /* FIXME: shouldn't have to redo everything here */
6465       gtk_label_recalculate (label);
6466     }
6467 }
6468
6469 static void
6470 gtk_label_activate_current_link (GtkLabel *label)
6471 {
6472   GtkLabelLink *link;
6473   GtkWidget *widget = GTK_WIDGET (label);
6474
6475   link = gtk_label_get_focus_link (label);
6476
6477   if (link)
6478     {
6479       emit_activate_link (label, link);
6480     }
6481   else
6482     {
6483       GtkWidget *toplevel;
6484       GtkWindow *window;
6485       GtkWidget *default_widget, *focus_widget;
6486
6487       toplevel = gtk_widget_get_toplevel (widget);
6488       if (GTK_IS_WINDOW (toplevel))
6489         {
6490           window = GTK_WINDOW (toplevel);
6491
6492           if (window)
6493             {
6494               default_widget = gtk_window_get_default_widget (window);
6495               focus_widget = gtk_window_get_focus (window);
6496
6497               if (default_widget != widget &&
6498                   !(widget == focus_widget && (!default_widget || !gtk_widget_is_sensitive (default_widget))))
6499                 gtk_window_activate_default (window);
6500             }
6501         }
6502     }
6503 }
6504
6505 static GtkLabelLink *
6506 gtk_label_get_current_link (GtkLabel *label)
6507 {
6508   GtkLabelPrivate *priv = label->priv;
6509   GtkLabelLink *link;
6510
6511   if (!priv->select_info)
6512     return NULL;
6513
6514   if (priv->select_info->link_clicked)
6515     link = priv->select_info->active_link;
6516   else
6517     link = gtk_label_get_focus_link (label);
6518
6519   return link;
6520 }
6521
6522 /**
6523  * gtk_label_get_current_uri:
6524  * @label: a #GtkLabel
6525  *
6526  * Returns the URI for the currently active link in the label.
6527  * The active link is the one under the mouse pointer or, in a
6528  * selectable label, the link in which the text cursor is currently
6529  * positioned.
6530  *
6531  * This function is intended for use in a #GtkLabel::activate-link handler
6532  * or for use in a #GtkWidget::query-tooltip handler.
6533  *
6534  * Returns: the currently active URI. The string is owned by GTK+ and must
6535  *   not be freed or modified.
6536  *
6537  * Since: 2.18
6538  */
6539 G_CONST_RETURN gchar *
6540 gtk_label_get_current_uri (GtkLabel *label)
6541 {
6542   GtkLabelLink *link;
6543
6544   g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
6545
6546   link = gtk_label_get_current_link (label);
6547
6548   if (link)
6549     return link->uri;
6550
6551   return NULL;
6552 }
6553
6554 /**
6555  * gtk_label_set_track_visited_links:
6556  * @label: a #GtkLabel
6557  * @track_links: %TRUE to track visited links
6558  *
6559  * Sets whether the label should keep track of clicked
6560  * links (and use a different color for them).
6561  *
6562  * Since: 2.18
6563  */
6564 void
6565 gtk_label_set_track_visited_links (GtkLabel *label,
6566                                    gboolean  track_links)
6567 {
6568   GtkLabelPrivate *priv;
6569
6570   g_return_if_fail (GTK_IS_LABEL (label));
6571
6572   priv = label->priv;
6573
6574   track_links = track_links != FALSE;
6575
6576   if (priv->track_links != track_links)
6577     {
6578       priv->track_links = track_links;
6579
6580       /* FIXME: shouldn't have to redo everything here */
6581       gtk_label_recalculate (label);
6582
6583       g_object_notify (G_OBJECT (label), "track-visited-links");
6584     }
6585 }
6586
6587 /**
6588  * gtk_label_get_track_visited_links:
6589  * @label: a #GtkLabel
6590  *
6591  * Returns whether the label is currently keeping track
6592  * of clicked links.
6593  *
6594  * Returns: %TRUE if clicked links are remembered
6595  *
6596  * Since: 2.18
6597  */
6598 gboolean
6599 gtk_label_get_track_visited_links (GtkLabel *label)
6600 {
6601   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
6602
6603   return label->priv->track_links;
6604 }
6605
6606 static gboolean
6607 gtk_label_query_tooltip (GtkWidget  *widget,
6608                          gint        x,
6609                          gint        y,
6610                          gboolean    keyboard_tip,
6611                          GtkTooltip *tooltip)
6612 {
6613   GtkLabel *label = GTK_LABEL (widget);
6614   GtkLabelPrivate *priv = label->priv;
6615   GtkLabelSelectionInfo *info = priv->select_info;
6616   gint index = -1;
6617   GList *l;
6618
6619   if (info && info->links)
6620     {
6621       if (keyboard_tip)
6622         {
6623           if (info->selection_anchor == info->selection_end)
6624             index = info->selection_anchor;
6625         }
6626       else
6627         {
6628           if (!get_layout_index (label, x, y, &index))
6629             index = -1;
6630         }
6631
6632       if (index != -1)
6633         {
6634           for (l = info->links; l != NULL; l = l->next)
6635             {
6636               GtkLabelLink *link = l->data;
6637               if (index >= link->start && index <= link->end)
6638                 {
6639                   if (link->title)
6640                     {
6641                       gtk_tooltip_set_markup (tooltip, link->title);
6642                       return TRUE;
6643                     }
6644                   break;
6645                 }
6646             }
6647         }
6648     }
6649
6650   return GTK_WIDGET_CLASS (gtk_label_parent_class)->query_tooltip (widget,
6651                                                                    x, y,
6652                                                                    keyboard_tip,
6653                                                                    tooltip);
6654 }