]> Pileus Git - ~andy/gtk/blob - gtk/gtklabel.c
Trivial doc fix
[~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 #include <math.h>
28 #include <string.h>
29 #include "gtklabel.h"
30 #include "gtkaccellabel.h"
31 #include "gtkdnd.h"
32 #include "gtkmain.h"
33 #include "gtkmarshalers.h"
34 #include "gtkwindow.h"
35 #include "gdk/gdkkeysyms.h"
36 #include "gtkclipboard.h"
37 #include <pango/pango.h>
38 #include "gtkimagemenuitem.h"
39 #include "gtkintl.h"
40 #include "gtkseparatormenuitem.h"
41 #include "gtktextutil.h"
42 #include "gtkmenuitem.h"
43 #include "gtknotebook.h"
44 #include "gtkstock.h"
45 #include "gtkbindings.h"
46 #include "gtkbuildable.h"
47 #include "gtkprivate.h"
48 #include "gtkalias.h"
49
50 #define GTK_LABEL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_LABEL, GtkLabelPrivate))
51
52 typedef struct
53 {
54   gint wrap_width;
55   gint width_chars;
56   gint max_width_chars;
57 }
58 GtkLabelPrivate;
59
60 struct _GtkLabelSelectionInfo
61 {
62   GdkWindow *window;
63   gint selection_anchor;
64   gint selection_end;
65   GtkWidget *popup_menu;
66   
67   gint drag_start_x;
68   gint drag_start_y;
69
70   guint in_drag : 1;
71 };
72
73 enum {
74   MOVE_CURSOR,
75   COPY_CLIPBOARD,
76   POPULATE_POPUP,
77   LAST_SIGNAL
78 };
79
80 enum {
81   PROP_0,
82   PROP_LABEL,
83   PROP_ATTRIBUTES,
84   PROP_USE_MARKUP,
85   PROP_USE_UNDERLINE,
86   PROP_JUSTIFY,
87   PROP_PATTERN,
88   PROP_WRAP,
89   PROP_WRAP_MODE,
90   PROP_SELECTABLE,
91   PROP_MNEMONIC_KEYVAL,
92   PROP_MNEMONIC_WIDGET,
93   PROP_CURSOR_POSITION,
94   PROP_SELECTION_BOUND,
95   PROP_ELLIPSIZE,
96   PROP_WIDTH_CHARS,
97   PROP_SINGLE_LINE_MODE,
98   PROP_ANGLE,
99   PROP_MAX_WIDTH_CHARS
100 };
101
102 static guint signals[LAST_SIGNAL] = { 0 };
103
104 static void gtk_label_set_property      (GObject          *object,
105                                          guint             prop_id,
106                                          const GValue     *value,
107                                          GParamSpec       *pspec);
108 static void gtk_label_get_property      (GObject          *object,
109                                          guint             prop_id,
110                                          GValue           *value,
111                                          GParamSpec       *pspec);
112 static void gtk_label_destroy           (GtkObject        *object);
113 static void gtk_label_finalize          (GObject          *object);
114 static void gtk_label_size_request      (GtkWidget        *widget,
115                                          GtkRequisition   *requisition);
116 static void gtk_label_size_allocate     (GtkWidget        *widget,
117                                          GtkAllocation    *allocation);
118 static void gtk_label_state_changed     (GtkWidget        *widget,
119                                          GtkStateType      state);
120 static void gtk_label_style_set         (GtkWidget        *widget,
121                                          GtkStyle         *previous_style);
122 static void gtk_label_direction_changed (GtkWidget        *widget,
123                                          GtkTextDirection  previous_dir);
124 static gint gtk_label_expose            (GtkWidget        *widget,
125                                          GdkEventExpose   *event);
126
127 static void gtk_label_realize           (GtkWidget        *widget);
128 static void gtk_label_unrealize         (GtkWidget        *widget);
129 static void gtk_label_map               (GtkWidget        *widget);
130 static void gtk_label_unmap             (GtkWidget        *widget);
131
132 static gboolean gtk_label_button_press      (GtkWidget        *widget,
133                                              GdkEventButton   *event);
134 static gboolean gtk_label_button_release    (GtkWidget        *widget,
135                                              GdkEventButton   *event);
136 static gboolean gtk_label_motion            (GtkWidget        *widget,
137                                              GdkEventMotion   *event);
138 static void     gtk_label_grab_focus        (GtkWidget        *widget);
139
140
141 static void gtk_label_set_text_internal          (GtkLabel      *label,
142                                                   gchar         *str);
143 static void gtk_label_set_label_internal         (GtkLabel      *label,
144                                                   gchar         *str);
145 static void gtk_label_set_use_markup_internal    (GtkLabel      *label,
146                                                   gboolean       val);
147 static void gtk_label_set_use_underline_internal (GtkLabel      *label,
148                                                   gboolean       val);
149 static void gtk_label_set_attributes_internal    (GtkLabel      *label,
150                                                   PangoAttrList *attrs);
151 static void gtk_label_set_uline_text_internal    (GtkLabel      *label,
152                                                   const gchar   *str);
153 static void gtk_label_set_pattern_internal       (GtkLabel      *label,
154                                                   const gchar   *pattern);
155 static void set_markup                           (GtkLabel      *label,
156                                                   const gchar   *str,
157                                                   gboolean       with_uline);
158 static void gtk_label_recalculate                (GtkLabel      *label);
159 static void gtk_label_hierarchy_changed          (GtkWidget     *widget,
160                                                   GtkWidget     *old_toplevel);
161 static void gtk_label_screen_changed             (GtkWidget     *widget,
162                                                   GdkScreen     *old_screen);
163
164 static void gtk_label_create_window       (GtkLabel *label);
165 static void gtk_label_destroy_window      (GtkLabel *label);
166 static void gtk_label_clear_layout        (GtkLabel *label);
167 static void gtk_label_ensure_layout       (GtkLabel *label);
168 static void gtk_label_invalidate_wrap_width (GtkLabel *label);
169 static void gtk_label_select_region_index (GtkLabel *label,
170                                            gint      anchor_index,
171                                            gint      end_index);
172
173 static gboolean gtk_label_mnemonic_activate (GtkWidget         *widget,
174                                              gboolean           group_cycling);
175 static void     gtk_label_setup_mnemonic    (GtkLabel          *label,
176                                              guint              last_key);
177 static void     gtk_label_drag_data_get     (GtkWidget         *widget,
178                                              GdkDragContext    *context,
179                                              GtkSelectionData  *selection_data,
180                                              guint              info,
181                                              guint              time);
182
183 static void     gtk_label_buildable_interface_init     (GtkBuildableIface *iface);
184 static gboolean gtk_label_buildable_custom_tag_start   (GtkBuildable     *buildable,
185                                                         GtkBuilder       *builder,
186                                                         GObject          *child,
187                                                         const gchar      *tagname,
188                                                         GMarkupParser    *parser,
189                                                         gpointer         *data);
190
191 static void     gtk_label_buildable_custom_finished    (GtkBuildable     *buildable,
192                                                         GtkBuilder       *builder,
193                                                         GObject          *child,
194                                                         const gchar      *tagname,
195                                                         gpointer          user_data);
196
197
198 /* For selectable lables: */
199 static void gtk_label_move_cursor        (GtkLabel        *label,
200                                           GtkMovementStep  step,
201                                           gint             count,
202                                           gboolean         extend_selection);
203 static void gtk_label_copy_clipboard     (GtkLabel        *label);
204 static void gtk_label_select_all         (GtkLabel        *label);
205 static void gtk_label_do_popup           (GtkLabel        *label,
206                                           GdkEventButton  *event);
207
208 static gint gtk_label_move_forward_word  (GtkLabel        *label,
209                                           gint             start);
210 static gint gtk_label_move_backward_word (GtkLabel        *label,
211                                           gint             start);
212
213 static GQuark quark_angle = 0;
214
215 static GtkBuildableIface *buildable_parent_iface = NULL;
216
217 G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
218                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
219                                                 gtk_label_buildable_interface_init));
220
221 static void
222 add_move_binding (GtkBindingSet  *binding_set,
223                   guint           keyval,
224                   guint           modmask,
225                   GtkMovementStep step,
226                   gint            count)
227 {
228   g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
229   
230   gtk_binding_entry_add_signal (binding_set, keyval, modmask,
231                                 "move_cursor", 3,
232                                 G_TYPE_ENUM, step,
233                                 G_TYPE_INT, count,
234                                 G_TYPE_BOOLEAN, FALSE);
235
236   /* Selection-extending version */
237   gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
238                                 "move_cursor", 3,
239                                 G_TYPE_ENUM, step,
240                                 G_TYPE_INT, count,
241                                 G_TYPE_BOOLEAN, TRUE);
242 }
243
244 static void
245 gtk_label_class_init (GtkLabelClass *class)
246 {
247   GObjectClass *gobject_class = G_OBJECT_CLASS (class);
248   GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
249   GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
250   GtkBindingSet *binding_set;
251
252   quark_angle = g_quark_from_static_string ("angle");
253
254   gobject_class->set_property = gtk_label_set_property;
255   gobject_class->get_property = gtk_label_get_property;
256   gobject_class->finalize = gtk_label_finalize;
257
258   object_class->destroy = gtk_label_destroy;
259   
260   widget_class->size_request = gtk_label_size_request;
261   widget_class->size_allocate = gtk_label_size_allocate;
262   widget_class->state_changed = gtk_label_state_changed;
263   widget_class->style_set = gtk_label_style_set;
264   widget_class->direction_changed = gtk_label_direction_changed;
265   widget_class->expose_event = gtk_label_expose;
266   widget_class->realize = gtk_label_realize;
267   widget_class->unrealize = gtk_label_unrealize;
268   widget_class->map = gtk_label_map;
269   widget_class->unmap = gtk_label_unmap;
270   widget_class->button_press_event = gtk_label_button_press;
271   widget_class->button_release_event = gtk_label_button_release;
272   widget_class->motion_notify_event = gtk_label_motion;
273   widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
274   widget_class->screen_changed = gtk_label_screen_changed;
275   widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
276   widget_class->drag_data_get = gtk_label_drag_data_get;
277   widget_class->grab_focus = gtk_label_grab_focus;
278
279   class->move_cursor = gtk_label_move_cursor;
280   class->copy_clipboard = gtk_label_copy_clipboard;
281   
282   signals[MOVE_CURSOR] = 
283     g_signal_new (I_("move_cursor"),
284                   G_OBJECT_CLASS_TYPE (gobject_class),
285                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
286                   G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
287                   NULL, NULL,
288                   _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
289                   G_TYPE_NONE, 3,
290                   GTK_TYPE_MOVEMENT_STEP,
291                   G_TYPE_INT,
292                   G_TYPE_BOOLEAN);
293   
294   signals[COPY_CLIPBOARD] =
295     g_signal_new (I_("copy_clipboard"),
296                   G_OBJECT_CLASS_TYPE (gobject_class),
297                   G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
298                   G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
299                   NULL, NULL,
300                   _gtk_marshal_VOID__VOID,
301                   G_TYPE_NONE, 0);
302   
303   signals[POPULATE_POPUP] =
304     g_signal_new (I_("populate_popup"),
305                   G_OBJECT_CLASS_TYPE (gobject_class),
306                   G_SIGNAL_RUN_LAST,
307                   G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
308                   NULL, NULL,
309                   _gtk_marshal_VOID__OBJECT,
310                   G_TYPE_NONE, 1,
311                   GTK_TYPE_MENU);
312
313   g_object_class_install_property (gobject_class,
314                                    PROP_LABEL,
315                                    g_param_spec_string ("label",
316                                                         P_("Label"),
317                                                         P_("The text of the label"),
318                                                         "",
319                                                         GTK_PARAM_READWRITE));
320   g_object_class_install_property (gobject_class,
321                                    PROP_ATTRIBUTES,
322                                    g_param_spec_boxed ("attributes",
323                                                        P_("Attributes"),
324                                                        P_("A list of style attributes to apply to the text of the label"),
325                                                        PANGO_TYPE_ATTR_LIST,
326                                                        GTK_PARAM_READWRITE));
327   g_object_class_install_property (gobject_class,
328                                    PROP_USE_MARKUP,
329                                    g_param_spec_boolean ("use-markup",
330                                                          P_("Use markup"),
331                                                          P_("The text of the label includes XML markup. See pango_parse_markup()"),
332                                                         FALSE,
333                                                         GTK_PARAM_READWRITE));
334   g_object_class_install_property (gobject_class,
335                                    PROP_USE_UNDERLINE,
336                                    g_param_spec_boolean ("use-underline",
337                                                          P_("Use underline"),
338                                                          P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
339                                                         FALSE,
340                                                         GTK_PARAM_READWRITE));
341
342   g_object_class_install_property (gobject_class,
343                                    PROP_JUSTIFY,
344                                    g_param_spec_enum ("justify",
345                                                       P_("Justification"),
346                                                       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"),
347                                                       GTK_TYPE_JUSTIFICATION,
348                                                       GTK_JUSTIFY_LEFT,
349                                                       GTK_PARAM_READWRITE));
350
351   g_object_class_install_property (gobject_class,
352                                    PROP_PATTERN,
353                                    g_param_spec_string ("pattern",
354                                                         P_("Pattern"),
355                                                         P_("A string with _ characters in positions correspond to characters in the text to underline"),
356                                                         NULL,
357                                                         GTK_PARAM_WRITABLE));
358
359   g_object_class_install_property (gobject_class,
360                                    PROP_WRAP,
361                                    g_param_spec_boolean ("wrap",
362                                                         P_("Line wrap"),
363                                                         P_("If set, wrap lines if the text becomes too wide"),
364                                                         FALSE,
365                                                         GTK_PARAM_READWRITE));
366   /**
367    * GtkLabel:wrap-mode:
368    *
369    * If line wrapping is on (see the #GtkLabel:wrap property) this controls 
370    * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which 
371    * means wrap on word boundaries.
372    *
373    * Since: 2.10
374    */
375   g_object_class_install_property (gobject_class,
376                                    PROP_WRAP_MODE,
377                                    g_param_spec_enum ("wrap-mode",
378                                                       P_("Line wrap mode"),
379                                                       P_("If wrap is set, controls how linewrapping is done"),
380                                                       PANGO_TYPE_WRAP_MODE,
381                                                       PANGO_WRAP_WORD,
382                                                       GTK_PARAM_READWRITE));
383   g_object_class_install_property (gobject_class,
384                                    PROP_SELECTABLE,
385                                    g_param_spec_boolean ("selectable",
386                                                         P_("Selectable"),
387                                                         P_("Whether the label text can be selected with the mouse"),
388                                                         FALSE,
389                                                         GTK_PARAM_READWRITE));
390   g_object_class_install_property (gobject_class,
391                                    PROP_MNEMONIC_KEYVAL,
392                                    g_param_spec_uint ("mnemonic-keyval",
393                                                       P_("Mnemonic key"),
394                                                       P_("The mnemonic accelerator key for this label"),
395                                                       0,
396                                                       G_MAXUINT,
397                                                       GDK_VoidSymbol,
398                                                       GTK_PARAM_READABLE));
399   g_object_class_install_property (gobject_class,
400                                    PROP_MNEMONIC_WIDGET,
401                                    g_param_spec_object ("mnemonic-widget",
402                                                         P_("Mnemonic widget"),
403                                                         P_("The widget to be activated when the label's mnemonic "
404                                                           "key is pressed"),
405                                                         GTK_TYPE_WIDGET,
406                                                         GTK_PARAM_READWRITE));
407
408   g_object_class_install_property (gobject_class,
409                                    PROP_CURSOR_POSITION,
410                                    g_param_spec_int ("cursor-position",
411                                                      P_("Cursor Position"),
412                                                      P_("The current position of the insertion cursor in chars"),
413                                                      0,
414                                                      G_MAXINT,
415                                                      0,
416                                                      GTK_PARAM_READABLE));
417   
418   g_object_class_install_property (gobject_class,
419                                    PROP_SELECTION_BOUND,
420                                    g_param_spec_int ("selection-bound",
421                                                      P_("Selection Bound"),
422                                                      P_("The position of the opposite end of the selection from the cursor in chars"),
423                                                      0,
424                                                      G_MAXINT,
425                                                      0,
426                                                      GTK_PARAM_READABLE));
427   
428   /**
429    * GtkLabel:ellipsize:
430    *
431    * The preferred place to ellipsize the string, if the label does 
432    * not have enough room to display the entire string, specified as a 
433    * #PangoEllisizeMode. 
434    *
435    * Note that setting this property to a value other than 
436    * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests 
437    * only enough space to display the ellipsis "...". In particular, this 
438    * means that ellipsizing labels do not work well in notebook tabs, unless 
439    * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
440    * to set a label's width are gtk_widget_set_size_request() and
441    * gtk_label_set_width_chars().
442    *
443    * Since: 2.6
444    */
445   g_object_class_install_property (gobject_class,
446                                    PROP_ELLIPSIZE,
447                                    g_param_spec_enum ("ellipsize",
448                                                       P_("Ellipsize"),
449                                                       P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
450                                                       PANGO_TYPE_ELLIPSIZE_MODE,
451                                                       PANGO_ELLIPSIZE_NONE,
452                                                       GTK_PARAM_READWRITE));
453
454   /**
455    * GtkLabel:width-chars:
456    * 
457    * The desired width of the label, in characters. If this property is set to
458    * -1, the width will be calculated automatically, otherwise the label will
459    * request either 3 characters or the property value, whichever is greater.
460    * If the "width-chars" property is set to a positive value, then the 
461    * #GtkLabel:max-width-chars property is ignored. 
462    *
463    * Since: 2.6
464    **/
465   g_object_class_install_property (gobject_class,
466                                    PROP_WIDTH_CHARS,
467                                    g_param_spec_int ("width-chars",
468                                                      P_("Width In Characters"),
469                                                      P_("The desired width of the label, in characters"),
470                                                      -1,
471                                                      G_MAXINT,
472                                                      -1,
473                                                      GTK_PARAM_READWRITE));
474   
475   /**
476    * GtkLabel:single-line-mode:
477    * 
478    * Whether the label is in single line mode. In single line mode,
479    * the height of the label does not depend on the actual text, it
480    * is always set to ascent + descent of the font. This can be an
481    * advantage in situations where resizing the label because of text 
482    * changes would be distracting, e.g. in a statusbar.
483    *
484    * Since: 2.6
485    **/
486   g_object_class_install_property (gobject_class,
487                                    PROP_SINGLE_LINE_MODE,
488                                    g_param_spec_boolean ("single-line-mode",
489                                                         P_("Single Line Mode"),
490                                                         P_("Whether the label is in single line mode"),
491                                                         FALSE,
492                                                         GTK_PARAM_READWRITE));
493
494   /**
495    * GtkLabel:angle:
496    * 
497    * The angle that the baseline of the label makes with the horizontal,
498    * in degrees, measured counterclockwise. An angle of 90 reads from
499    * from bottom to top, an angle of 270, from top to bottom. Ignored
500    * if the label is selectable, wrapped, or ellipsized.
501    *
502    * Since: 2.6
503    **/
504   g_object_class_install_property (gobject_class,
505                                    PROP_ANGLE,
506                                    g_param_spec_double ("angle",
507                                                         P_("Angle"),
508                                                         P_("Angle at which the label is rotated"),
509                                                         0.0,
510                                                         360.0,
511                                                         0.0, 
512                                                         GTK_PARAM_READWRITE));
513   
514   /**
515    * GtkLabel:max-width-chars:
516    * 
517    * The desired maximum width of the label, in characters. If this property 
518    * is set to -1, the width will be calculated automatically, otherwise the 
519    * label will request space for no more than the requested number of 
520    * characters. If the #GtkLabel:width-chars property is set to a positive 
521    * value, then the "max-width-chars" property is ignored.
522    * 
523    * Since: 2.6
524    **/
525   g_object_class_install_property (gobject_class,
526                                    PROP_MAX_WIDTH_CHARS,
527                                    g_param_spec_int ("max-width-chars",
528                                                      P_("Maximum Width In Characters"),
529                                                      P_("The desired maximum width of the label, in characters"),
530                                                      -1,
531                                                      G_MAXINT,
532                                                      -1,
533                                                      GTK_PARAM_READWRITE));
534   /*
535    * Key bindings
536    */
537
538   binding_set = gtk_binding_set_by_class (class);
539
540   /* Moving the insertion point */
541   add_move_binding (binding_set, GDK_Right, 0,
542                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
543   
544   add_move_binding (binding_set, GDK_Left, 0,
545                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
546
547   add_move_binding (binding_set, GDK_KP_Right, 0,
548                     GTK_MOVEMENT_VISUAL_POSITIONS, 1);
549   
550   add_move_binding (binding_set, GDK_KP_Left, 0,
551                     GTK_MOVEMENT_VISUAL_POSITIONS, -1);
552   
553   add_move_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
554                     GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
555   
556   add_move_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
557                     GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
558   
559   add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
560                     GTK_MOVEMENT_WORDS, 1);
561
562   add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
563                     GTK_MOVEMENT_WORDS, -1);
564
565   add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
566                     GTK_MOVEMENT_WORDS, 1);
567
568   add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
569                     GTK_MOVEMENT_WORDS, -1);
570
571   /* select all */
572   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
573                                 "move_cursor", 3,
574                                 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
575                                 G_TYPE_INT, -1,
576                                 G_TYPE_BOOLEAN, FALSE);
577
578   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
579                                 "move_cursor", 3,
580                                 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
581                                 G_TYPE_INT, 1,
582                                 G_TYPE_BOOLEAN, TRUE);
583
584   gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
585                                 "move_cursor", 3,
586                                 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
587                                 G_TYPE_INT, -1,
588                                 G_TYPE_BOOLEAN, FALSE);
589
590   gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
591                                 "move_cursor", 3,
592                                 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
593                                 G_TYPE_INT, 1,
594                                 G_TYPE_BOOLEAN, TRUE);
595
596   /* unselect all */
597   gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
598                                 "move_cursor", 3,
599                                 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
600                                 G_TYPE_INT, 0,
601                                 G_TYPE_BOOLEAN, FALSE);
602
603   gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
604                                 "move_cursor", 3,
605                                 G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
606                                 G_TYPE_INT, 0,
607                                 G_TYPE_BOOLEAN, FALSE);
608
609   add_move_binding (binding_set, GDK_f, GDK_MOD1_MASK,
610                     GTK_MOVEMENT_WORDS, 1);
611
612   add_move_binding (binding_set, GDK_b, GDK_MOD1_MASK,
613                     GTK_MOVEMENT_WORDS, -1);
614
615   add_move_binding (binding_set, GDK_Home, 0,
616                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
617
618   add_move_binding (binding_set, GDK_End, 0,
619                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
620
621   add_move_binding (binding_set, GDK_KP_Home, 0,
622                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);
623
624   add_move_binding (binding_set, GDK_KP_End, 0,
625                     GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
626   
627   add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
628                     GTK_MOVEMENT_BUFFER_ENDS, -1);
629
630   add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
631                     GTK_MOVEMENT_BUFFER_ENDS, 1);
632
633   add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
634                     GTK_MOVEMENT_BUFFER_ENDS, -1);
635
636   add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
637                     GTK_MOVEMENT_BUFFER_ENDS, 1);
638
639   /* copy */
640   gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
641                                 "copy_clipboard", 0);
642
643   gtk_settings_install_property (g_param_spec_boolean ("gtk-label-select-on-focus",
644                                                        P_("Select on focus"),
645                                                        P_("Whether to select the contents of a selectable label when it is focused"),
646                                                        TRUE,
647                                                        GTK_PARAM_READWRITE));
648
649                                 
650   g_type_class_add_private (class, sizeof (GtkLabelPrivate));
651 }
652
653 static void 
654 gtk_label_set_property (GObject      *object,
655                         guint         prop_id,
656                         const GValue *value,
657                         GParamSpec   *pspec)
658 {
659   GtkLabel *label;
660
661   label = GTK_LABEL (object);
662   
663   switch (prop_id)
664     {
665     case PROP_LABEL:
666       gtk_label_set_label (label, g_value_get_string (value));
667       break;
668     case PROP_ATTRIBUTES:
669       gtk_label_set_attributes (label, g_value_get_boxed (value));
670       break;
671     case PROP_USE_MARKUP:
672       gtk_label_set_use_markup (label, g_value_get_boolean (value));
673       break;
674     case PROP_USE_UNDERLINE:
675       gtk_label_set_use_underline (label, g_value_get_boolean (value));
676       break;
677     case PROP_JUSTIFY:
678       gtk_label_set_justify (label, g_value_get_enum (value));
679       break;
680     case PROP_PATTERN:
681       gtk_label_set_pattern (label, g_value_get_string (value));
682       break;
683     case PROP_WRAP:
684       gtk_label_set_line_wrap (label, g_value_get_boolean (value));
685       break;      
686     case PROP_WRAP_MODE:
687       gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
688       break;      
689     case PROP_SELECTABLE:
690       gtk_label_set_selectable (label, g_value_get_boolean (value));
691       break;      
692     case PROP_MNEMONIC_WIDGET:
693       gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
694       break;
695     case PROP_ELLIPSIZE:
696       gtk_label_set_ellipsize (label, g_value_get_enum (value));
697       break;
698     case PROP_WIDTH_CHARS:
699       gtk_label_set_width_chars (label, g_value_get_int (value));
700       break;
701     case PROP_SINGLE_LINE_MODE:
702       gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
703       break;      
704     case PROP_ANGLE:
705       gtk_label_set_angle (label, g_value_get_double (value));
706       break;
707     case PROP_MAX_WIDTH_CHARS:
708       gtk_label_set_max_width_chars (label, g_value_get_int (value));
709       break;
710     default:
711       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
712       break;
713     }
714 }
715
716 static void 
717 gtk_label_get_property (GObject     *object,
718                         guint        prop_id,
719                         GValue      *value,
720                         GParamSpec  *pspec)
721 {
722   GtkLabel *label;
723   
724   label = GTK_LABEL (object);
725   
726   switch (prop_id)
727     {
728     case PROP_LABEL:
729       g_value_set_string (value, label->label);
730       break;
731     case PROP_ATTRIBUTES:
732       g_value_set_boxed (value, label->attrs);
733       break;
734     case PROP_USE_MARKUP:
735       g_value_set_boolean (value, label->use_markup);
736       break;
737     case PROP_USE_UNDERLINE:
738       g_value_set_boolean (value, label->use_underline);
739       break;
740     case PROP_JUSTIFY:
741       g_value_set_enum (value, label->jtype);
742       break;
743     case PROP_WRAP:
744       g_value_set_boolean (value, label->wrap);
745       break;
746     case PROP_WRAP_MODE:
747       g_value_set_enum (value, label->wrap_mode);
748       break;
749     case PROP_SELECTABLE:
750       g_value_set_boolean (value, gtk_label_get_selectable (label));
751       break;
752     case PROP_MNEMONIC_KEYVAL:
753       g_value_set_uint (value, label->mnemonic_keyval);
754       break;
755     case PROP_MNEMONIC_WIDGET:
756       g_value_set_object (value, (GObject*) label->mnemonic_widget);
757       break;
758     case PROP_CURSOR_POSITION:
759       if (label->select_info)
760         {
761           gint offset = g_utf8_pointer_to_offset (label->text,
762                                                   label->text + label->select_info->selection_end);
763           g_value_set_int (value, offset);
764         }
765       else
766         g_value_set_int (value, 0);
767       break;
768     case PROP_SELECTION_BOUND:
769       if (label->select_info)
770         {
771           gint offset = g_utf8_pointer_to_offset (label->text,
772                                                   label->text + label->select_info->selection_anchor);
773           g_value_set_int (value, offset);
774         }
775       else
776         g_value_set_int (value, 0);
777       break;
778     case PROP_ELLIPSIZE:
779       g_value_set_enum (value, label->ellipsize);
780       break;
781     case PROP_WIDTH_CHARS:
782       g_value_set_int (value, gtk_label_get_width_chars (label));
783       break;
784     case PROP_SINGLE_LINE_MODE:
785       g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
786       break;
787     case PROP_ANGLE:
788       g_value_set_double (value, gtk_label_get_angle (label));
789       break;
790     case PROP_MAX_WIDTH_CHARS:
791       g_value_set_int (value, gtk_label_get_max_width_chars (label));
792       break;
793
794     default:
795       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
796       break;
797     }
798 }
799
800 static void
801 gtk_label_init (GtkLabel *label)
802 {
803   GtkLabelPrivate *priv;
804
805   GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
806
807   priv = GTK_LABEL_GET_PRIVATE (label);
808   priv->width_chars = -1;
809   priv->max_width_chars = -1;
810   priv->wrap_width = -1;
811   label->label = NULL;
812
813   label->jtype = GTK_JUSTIFY_LEFT;
814   label->wrap = FALSE;
815   label->wrap_mode = PANGO_WRAP_WORD;
816   label->ellipsize = PANGO_ELLIPSIZE_NONE;
817
818   label->use_underline = FALSE;
819   label->use_markup = FALSE;
820   label->pattern_set = FALSE;
821   
822   label->mnemonic_keyval = GDK_VoidSymbol;
823   label->layout = NULL;
824   label->text = NULL;
825   label->attrs = NULL;
826
827   label->mnemonic_widget = NULL;
828   label->mnemonic_window = NULL;
829   
830   gtk_label_set_text (label, "");
831 }
832
833
834 static void
835 gtk_label_buildable_interface_init (GtkBuildableIface *iface)
836 {
837   buildable_parent_iface = g_type_interface_peek_parent (iface);
838
839   iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
840   iface->custom_finished = gtk_label_buildable_custom_finished;
841 }
842
843 typedef struct {
844   GtkBuilder    *builder;
845   GObject       *object;
846   PangoAttrList *attrs;
847 } PangoParserData;
848
849 static PangoAttribute *
850 attribute_from_text (GtkBuilder   *builder,
851                      const gchar  *name, 
852                      const gchar  *value,
853                      GError      **error)
854 {
855   PangoAttribute *attribute = NULL;
856   PangoAttrType   type;
857   PangoLanguage  *language;
858   PangoFontDescription *font_desc;
859   GdkColor       *color;
860   GValue          val = { 0, };
861
862   if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
863     return NULL;
864
865   type = g_value_get_enum (&val);
866   g_value_unset (&val);
867
868   switch (type)
869     {
870       /* PangoAttrLanguage */
871     case PANGO_ATTR_LANGUAGE:
872       if ((language = pango_language_from_string (value)))
873         {
874           attribute = pango_attr_language_new (language);
875           g_value_init (&val, G_TYPE_INT);
876         }
877       break;
878       /* PangoAttrInt */
879     case PANGO_ATTR_STYLE:
880       if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
881         attribute = pango_attr_style_new (g_value_get_enum (&val));
882       break;
883     case PANGO_ATTR_WEIGHT:
884       if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
885         attribute = pango_attr_weight_new (g_value_get_enum (&val));
886       break;
887     case PANGO_ATTR_VARIANT:
888       if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
889         attribute = pango_attr_variant_new (g_value_get_enum (&val));
890       break;
891     case PANGO_ATTR_STRETCH:
892       if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
893         attribute = pango_attr_stretch_new (g_value_get_enum (&val));
894       break;
895     case PANGO_ATTR_UNDERLINE:
896       if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
897         attribute = pango_attr_underline_new (g_value_get_boolean (&val));
898       break;
899     case PANGO_ATTR_STRIKETHROUGH:      
900       if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
901         attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
902       break;
903     case PANGO_ATTR_GRAVITY:
904       if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
905         attribute = pango_attr_gravity_new (g_value_get_enum (&val));
906       break;
907     case PANGO_ATTR_GRAVITY_HINT:
908       if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT, 
909                                               value, &val, error))
910         attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
911       break;
912
913       /* PangoAttrString */       
914     case PANGO_ATTR_FAMILY:
915       attribute = pango_attr_family_new (value);
916       g_value_init (&val, G_TYPE_INT);
917       break;
918
919       /* PangoAttrSize */         
920     case PANGO_ATTR_SIZE:
921       if (gtk_builder_value_from_string_type (builder, G_TYPE_INT, 
922                                               value, &val, error))
923         attribute = pango_attr_size_new (g_value_get_int (&val));
924       break;
925     case PANGO_ATTR_ABSOLUTE_SIZE:
926       if (gtk_builder_value_from_string_type (builder, G_TYPE_INT, 
927                                               value, &val, error))
928         attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
929       break;
930     
931       /* PangoAttrFontDesc */
932     case PANGO_ATTR_FONT_DESC:
933       if ((font_desc = pango_font_description_from_string (value)))
934         {
935           attribute = pango_attr_font_desc_new (font_desc);
936           pango_font_description_free (font_desc);
937           g_value_init (&val, G_TYPE_INT);
938         }
939       break;
940
941       /* PangoAttrColor */
942     case PANGO_ATTR_FOREGROUND:
943       if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR, 
944                                               value, &val, error))
945         {
946           color = g_value_get_boxed (&val);
947           attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
948         }
949       break;
950     case PANGO_ATTR_BACKGROUND: 
951       if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR, 
952                                               value, &val, error))
953         {
954           color = g_value_get_boxed (&val);
955           attribute = pango_attr_background_new (color->red, color->green, color->blue);
956         }
957       break;
958     case PANGO_ATTR_UNDERLINE_COLOR:
959       if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR, 
960                                               value, &val, error))
961         {
962           color = g_value_get_boxed (&val);
963           attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
964         }
965       break;
966     case PANGO_ATTR_STRIKETHROUGH_COLOR:
967       if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR, 
968                                               value, &val, error))
969         {
970           color = g_value_get_boxed (&val);
971           attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
972         }
973       break;
974       
975       /* PangoAttrShape */
976     case PANGO_ATTR_SHAPE:
977       /* Unsupported for now */
978       break;
979       /* PangoAttrFloat */
980     case PANGO_ATTR_SCALE:
981       if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE, 
982                                               value, &val, error))
983         attribute = pango_attr_scale_new (g_value_get_double (&val));
984       break;
985
986     case PANGO_ATTR_INVALID:
987     case PANGO_ATTR_LETTER_SPACING:
988     case PANGO_ATTR_RISE:
989     case PANGO_ATTR_FALLBACK:
990     default:
991       break;
992     }
993
994   g_value_unset (&val);
995
996   return attribute;
997 }
998
999
1000 static void
1001 pango_start_element (GMarkupParseContext *context,
1002                      const gchar         *element_name,
1003                      const gchar        **names,
1004                      const gchar        **values,
1005                      gpointer             user_data,
1006                      GError             **error)
1007 {
1008   PangoParserData *data = (PangoParserData*)user_data;
1009   GValue val = { 0, };
1010   guint i;
1011   gint line_number, char_number;
1012
1013   if (strcmp (element_name, "attribute") == 0)
1014     {
1015       PangoAttribute *attr = NULL;
1016       const gchar *name = NULL;
1017       const gchar *value = NULL;
1018       const gchar *start = NULL;
1019       const gchar *end = NULL;
1020       guint start_val = 0;
1021       guint end_val   = G_MAXUINT;
1022
1023       for (i = 0; names[i]; i++)
1024         {
1025           if (strcmp (names[i], "name") == 0)
1026             name = values[i];
1027           else if (strcmp (names[i], "value") == 0)
1028             value = values[i];
1029           else if (strcmp (names[i], "start") == 0)
1030             start = values[i];
1031           else if (strcmp (names[i], "end") == 0)
1032             end = values[i];
1033           else
1034             {
1035               g_markup_parse_context_get_position (context,
1036                                                    &line_number,
1037                                                    &char_number);
1038               g_set_error (error,
1039                            GTK_BUILDER_ERROR,
1040                            GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
1041                            "%s:%d:%d '%s' is not a valid attribute of <%s>",
1042                            "<input>",
1043                            line_number, char_number, names[i], "attribute");
1044               return;
1045             }
1046         }
1047
1048       if (!name || !value)
1049         {
1050           g_markup_parse_context_get_position (context,
1051                                                &line_number,
1052                                                &char_number);
1053           g_set_error (error,
1054                        GTK_BUILDER_ERROR,
1055                        GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
1056                        "%s:%d:%d <%s> requires attribute \"%s\"",
1057                        "<input>",
1058                        line_number, char_number, "attribute",
1059                        name ? "value" : "name");
1060           return;
1061         }
1062
1063       if (start)
1064         {
1065           if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT, 
1066                                                    start, &val, error))
1067             return;
1068           start_val = g_value_get_uint (&val);
1069           g_value_unset (&val);
1070         }
1071
1072       if (end)
1073         {
1074           if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT, 
1075                                                    end, &val, error))
1076             return;
1077           end_val = g_value_get_uint (&val);
1078           g_value_unset (&val);
1079         }
1080
1081       attr = attribute_from_text (data->builder, name, value, error);
1082       attr->start_index = start_val;
1083       attr->end_index   = end_val;
1084
1085       if (attr)
1086         {
1087           if (!data->attrs)
1088             data->attrs = pango_attr_list_new ();
1089
1090           pango_attr_list_insert (data->attrs, attr);
1091         }
1092     }
1093   else if (strcmp (element_name, "attributes") == 0)
1094     ;
1095   else
1096     g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
1097 }
1098
1099 static const GMarkupParser pango_parser =
1100   {
1101     pango_start_element,
1102   };
1103
1104 static gboolean
1105 gtk_label_buildable_custom_tag_start (GtkBuildable     *buildable,
1106                                       GtkBuilder       *builder,
1107                                       GObject          *child,
1108                                       const gchar      *tagname,
1109                                       GMarkupParser    *parser,
1110                                       gpointer         *data)
1111 {
1112   if (buildable_parent_iface->custom_tag_start (buildable, builder, child, 
1113                                                 tagname, parser, data))
1114     return TRUE;
1115
1116   if (strcmp (tagname, "attributes") == 0)
1117     {
1118       PangoParserData *parser_data;
1119
1120       parser_data = g_slice_new0 (PangoParserData);
1121       parser_data->builder = g_object_ref (builder);
1122       parser_data->object = g_object_ref (buildable);
1123       *parser = pango_parser;
1124       *data = parser_data;
1125       return TRUE;
1126     }
1127   return FALSE;
1128 }
1129
1130 static void
1131 gtk_label_buildable_custom_finished (GtkBuildable *buildable,
1132                                      GtkBuilder   *builder,
1133                                      GObject      *child,
1134                                      const gchar  *tagname,
1135                                      gpointer      user_data)
1136 {
1137   PangoParserData *data;
1138
1139   buildable_parent_iface->custom_finished (buildable, builder, child, 
1140                                            tagname, user_data);
1141
1142   if (strcmp (tagname, "attributes") == 0)
1143     {
1144       data = (PangoParserData*)user_data;
1145
1146       if (data->attrs)
1147         {
1148           gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
1149           pango_attr_list_unref (data->attrs);
1150         }
1151
1152       g_object_unref (data->object);
1153       g_object_unref (data->builder);
1154       g_slice_free (PangoParserData, data);
1155     }
1156 }
1157
1158
1159 /**
1160  * gtk_label_new:
1161  * @str: The text of the label
1162  *
1163  * Creates a new label with the given text inside it. You can
1164  * pass %NULL to get an empty label widget.
1165  *
1166  * Return value: the new #GtkLabel
1167  **/
1168 GtkWidget*
1169 gtk_label_new (const gchar *str)
1170 {
1171   GtkLabel *label;
1172   
1173   label = g_object_new (GTK_TYPE_LABEL, NULL);
1174
1175   if (str && *str)
1176     gtk_label_set_text (label, str);
1177   
1178   return GTK_WIDGET (label);
1179 }
1180
1181 /**
1182  * gtk_label_new_with_mnemonic:
1183  * @str: The text of the label, with an underscore in front of the
1184  *       mnemonic character
1185  *
1186  * Creates a new #GtkLabel, containing the text in @str.
1187  *
1188  * If characters in @str are preceded by an underscore, they are
1189  * underlined. If you need a literal underscore character in a label, use
1190  * '__' (two underscores). The first underlined character represents a 
1191  * keyboard accelerator called a mnemonic. The mnemonic key can be used 
1192  * to activate another widget, chosen automatically, or explicitly using
1193  * gtk_label_set_mnemonic_widget().
1194  * 
1195  * If gtk_label_set_mnemonic_widget() is not called, then the first 
1196  * activatable ancestor of the #GtkLabel will be chosen as the mnemonic 
1197  * widget. For instance, if the label is inside a button or menu item, 
1198  * the button or menu item will automatically become the mnemonic widget 
1199  * and be activated by the mnemonic.
1200  *
1201  * Return value: the new #GtkLabel
1202  **/
1203 GtkWidget*
1204 gtk_label_new_with_mnemonic (const gchar *str)
1205 {
1206   GtkLabel *label;
1207   
1208   label = g_object_new (GTK_TYPE_LABEL, NULL);
1209
1210   if (str && *str)
1211     gtk_label_set_text_with_mnemonic (label, str);
1212   
1213   return GTK_WIDGET (label);
1214 }
1215
1216 static gboolean
1217 gtk_label_mnemonic_activate (GtkWidget *widget,
1218                              gboolean   group_cycling)
1219 {
1220   GtkWidget *parent;
1221
1222   if (GTK_LABEL (widget)->mnemonic_widget)
1223     return gtk_widget_mnemonic_activate (GTK_LABEL (widget)->mnemonic_widget, group_cycling);
1224
1225   /* Try to find the widget to activate by traversing the
1226    * widget's ancestry.
1227    */
1228   parent = widget->parent;
1229
1230   if (GTK_IS_NOTEBOOK (parent))
1231     return FALSE;
1232   
1233   while (parent)
1234     {
1235       if (GTK_WIDGET_CAN_FOCUS (parent) ||
1236           (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1237           GTK_IS_NOTEBOOK (parent->parent) ||
1238           GTK_IS_MENU_ITEM (parent))
1239         return gtk_widget_mnemonic_activate (parent, group_cycling);
1240       parent = parent->parent;
1241     }
1242
1243   /* barf if there was nothing to activate */
1244   g_warning ("Couldn't find a target for a mnemonic activation.");
1245   gtk_widget_error_bell (widget);
1246
1247   return FALSE;
1248 }
1249
1250 static void
1251 gtk_label_setup_mnemonic (GtkLabel *label,
1252                           guint     last_key)
1253 {
1254   GtkWidget *widget = GTK_WIDGET (label);
1255   GtkWidget *toplevel;
1256   GtkWidget *mnemonic_menu;
1257   
1258   mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
1259   
1260   if (last_key != GDK_VoidSymbol)
1261     {
1262       if (label->mnemonic_window)
1263         {
1264           gtk_window_remove_mnemonic  (label->mnemonic_window,
1265                                        last_key,
1266                                        widget);
1267           label->mnemonic_window = NULL;
1268         }
1269       if (mnemonic_menu)
1270         {
1271           _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
1272                                            last_key,
1273                                            widget);
1274           mnemonic_menu = NULL;
1275         }
1276     }
1277   
1278   if (label->mnemonic_keyval == GDK_VoidSymbol)
1279     goto done;
1280
1281   toplevel = gtk_widget_get_toplevel (widget);
1282   if (GTK_WIDGET_TOPLEVEL (toplevel))
1283     {
1284       GtkWidget *menu_shell;
1285       
1286       menu_shell = gtk_widget_get_ancestor (widget,
1287                                             GTK_TYPE_MENU_SHELL);
1288
1289       if (menu_shell)
1290         {
1291           _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
1292                                         label->mnemonic_keyval,
1293                                         widget);
1294           mnemonic_menu = menu_shell;
1295         }
1296       
1297       if (!GTK_IS_MENU (menu_shell))
1298         {
1299           gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
1300                                    label->mnemonic_keyval,
1301                                    widget);
1302           label->mnemonic_window = GTK_WINDOW (toplevel);
1303         }
1304     }
1305   
1306  done:
1307   g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1308 }
1309
1310 static void
1311 gtk_label_hierarchy_changed (GtkWidget *widget,
1312                              GtkWidget *old_toplevel)
1313 {
1314   GtkLabel *label = GTK_LABEL (widget);
1315
1316   gtk_label_setup_mnemonic (label, label->mnemonic_keyval);
1317 }
1318
1319 static void
1320 label_shortcut_setting_apply (GtkLabel *label)
1321 {
1322   gtk_label_recalculate (label);
1323   if (GTK_IS_ACCEL_LABEL (label))
1324     gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
1325 }
1326
1327 static void
1328 label_shortcut_setting_traverse_container (GtkWidget *widget,
1329                                            gpointer   data)
1330 {
1331   if (GTK_IS_LABEL (widget))
1332     label_shortcut_setting_apply (GTK_LABEL (widget));
1333   else if (GTK_IS_CONTAINER (widget))
1334     gtk_container_forall (GTK_CONTAINER (widget),
1335                           label_shortcut_setting_traverse_container, data);
1336 }
1337
1338 static void
1339 label_shortcut_setting_changed (GtkSettings *settings)
1340 {
1341   GList *list, *l;
1342
1343   list = gtk_window_list_toplevels ();
1344
1345   for (l = list; l ; l = l->next)
1346     {
1347       GtkWidget *widget = l->data;
1348
1349       if (gtk_widget_get_settings (widget) == settings)
1350         gtk_container_forall (GTK_CONTAINER (widget),
1351                               label_shortcut_setting_traverse_container, NULL);
1352     }
1353
1354   g_list_free (list);
1355 }
1356
1357 static void
1358 gtk_label_screen_changed (GtkWidget *widget,
1359                           GdkScreen *old_screen)
1360 {
1361   GtkSettings *settings;
1362   gboolean shortcuts_connected;
1363
1364   if (!gtk_widget_has_screen (widget))
1365     return;
1366
1367   settings = gtk_widget_get_settings (widget);
1368
1369   shortcuts_connected =
1370     GPOINTER_TO_INT (g_object_get_data (G_OBJECT (settings),
1371                                         "gtk-label-shortcuts-connected"));
1372
1373   if (! shortcuts_connected)
1374     {
1375       g_signal_connect (settings, "notify::gtk-enable-mnemonics",
1376                         G_CALLBACK (label_shortcut_setting_changed),
1377                         NULL);
1378       g_signal_connect (settings, "notify::gtk-enable-accels",
1379                         G_CALLBACK (label_shortcut_setting_changed),
1380                         NULL);
1381
1382       g_object_set_data (G_OBJECT (settings), "gtk-label-shortcuts-connected",
1383                          GINT_TO_POINTER (TRUE));
1384     }
1385
1386   label_shortcut_setting_apply (GTK_LABEL (widget));
1387 }
1388
1389
1390 static void
1391 label_mnemonic_widget_weak_notify (gpointer      data,
1392                                    GObject      *where_the_object_was)
1393 {
1394   GtkLabel *label = data;
1395
1396   label->mnemonic_widget = NULL;
1397   g_object_notify (G_OBJECT (label), "mnemonic-widget");
1398 }
1399
1400 /**
1401  * gtk_label_set_mnemonic_widget:
1402  * @label: a #GtkLabel
1403  * @widget: the target #GtkWidget 
1404  *
1405  * If the label has been set so that it has an mnemonic key (using
1406  * i.e. gtk_label_set_markup_with_mnemonic(),
1407  * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
1408  * or the "use_underline" property) the label can be associated with a
1409  * widget that is the target of the mnemonic. When the label is inside
1410  * a widget (like a #GtkButton or a #GtkNotebook tab) it is
1411  * automatically associated with the correct widget, but sometimes
1412  * (i.e. when the target is a #GtkEntry next to the label) you need to
1413  * set it explicitly using this function.
1414  *
1415  * The target widget will be accelerated by emitting the 
1416  * GtkWidget::mnemonic-activate signal on it. The default handler for 
1417  * this signal will activate the widget if there are no mnemonic collisions 
1418  * and toggle focus between the colliding widgets otherwise.
1419  **/
1420 void
1421 gtk_label_set_mnemonic_widget (GtkLabel  *label,
1422                                GtkWidget *widget)
1423 {
1424   g_return_if_fail (GTK_IS_LABEL (label));
1425   if (widget)
1426     g_return_if_fail (GTK_IS_WIDGET (widget));
1427
1428   if (label->mnemonic_widget)
1429     {
1430       gtk_widget_remove_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
1431       g_object_weak_unref (G_OBJECT (label->mnemonic_widget),
1432                            label_mnemonic_widget_weak_notify,
1433                            label);
1434     }
1435   label->mnemonic_widget = widget;
1436   if (label->mnemonic_widget)
1437     {
1438       g_object_weak_ref (G_OBJECT (label->mnemonic_widget),
1439                          label_mnemonic_widget_weak_notify,
1440                          label);
1441       gtk_widget_add_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
1442     }
1443   
1444   g_object_notify (G_OBJECT (label), "mnemonic-widget");
1445 }
1446
1447 /**
1448  * gtk_label_get_mnemonic_widget:
1449  * @label: a #GtkLabel
1450  *
1451  * Retrieves the target of the mnemonic (keyboard shortcut) of this
1452  * label. See gtk_label_set_mnemonic_widget().
1453  *
1454  * Return value: the target of the label's mnemonic, or %NULL if none
1455  *               has been set and the default algorithm will be used.
1456  **/
1457 GtkWidget *
1458 gtk_label_get_mnemonic_widget (GtkLabel *label)
1459 {
1460   g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1461
1462   return label->mnemonic_widget;
1463 }
1464
1465 /**
1466  * gtk_label_get_mnemonic_keyval:
1467  * @label: a #GtkLabel
1468  *
1469  * If the label has been set so that it has an mnemonic key this function
1470  * returns the keyval used for the mnemonic accelerator. If there is no
1471  * mnemonic set up it returns #GDK_VoidSymbol.
1472  *
1473  * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
1474  **/
1475 guint
1476 gtk_label_get_mnemonic_keyval (GtkLabel *label)
1477 {
1478   g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
1479
1480   return label->mnemonic_keyval;
1481 }
1482
1483 static void
1484 gtk_label_set_text_internal (GtkLabel *label,
1485                              gchar    *str)
1486 {
1487   g_free (label->text);
1488   
1489   label->text = str;
1490
1491   gtk_label_select_region_index (label, 0, 0);
1492 }
1493
1494 static void
1495 gtk_label_set_label_internal (GtkLabel *label,
1496                               gchar    *str)
1497 {
1498   g_free (label->label);
1499   
1500   label->label = str;
1501
1502   g_object_notify (G_OBJECT (label), "label");
1503 }
1504
1505 static void
1506 gtk_label_set_use_markup_internal (GtkLabel *label,
1507                                    gboolean  val)
1508 {
1509   val = val != FALSE;
1510   if (label->use_markup != val)
1511     {
1512       label->use_markup = val;
1513
1514       g_object_notify (G_OBJECT (label), "use-markup");
1515     }
1516 }
1517
1518 static void
1519 gtk_label_set_use_underline_internal (GtkLabel *label,
1520                                       gboolean val)
1521 {
1522   val = val != FALSE;
1523   if (label->use_underline != val)
1524     {
1525       label->use_underline = val;
1526
1527       g_object_notify (G_OBJECT (label), "use-underline");
1528     }
1529 }
1530
1531 static void
1532 gtk_label_set_attributes_internal (GtkLabel      *label,
1533                                    PangoAttrList *attrs)
1534 {
1535   if (attrs)
1536     pango_attr_list_ref (attrs);
1537   
1538   if (label->attrs)
1539     pango_attr_list_unref (label->attrs);
1540
1541   if (!label->use_markup && !label->use_underline)
1542     {
1543       if (attrs)
1544         pango_attr_list_ref (attrs);
1545       if (label->effective_attrs)
1546         pango_attr_list_unref (label->effective_attrs);
1547       label->effective_attrs = attrs;
1548     }
1549
1550   label->attrs = attrs;
1551   g_object_notify (G_OBJECT (label), "attributes");
1552 }
1553
1554
1555 /* Calculates text, attrs and mnemonic_keyval from
1556  * label, use_underline and use_markup
1557  */
1558 static void
1559 gtk_label_recalculate (GtkLabel *label)
1560 {
1561   guint keyval = label->mnemonic_keyval;
1562
1563   if (label->use_markup)
1564     set_markup (label, label->label, label->use_underline);
1565   else
1566     {
1567       if (label->use_underline)
1568         gtk_label_set_uline_text_internal (label, label->label);
1569       else
1570         {
1571           gtk_label_set_text_internal (label, g_strdup (label->label));
1572           if (label->attrs)
1573             pango_attr_list_ref (label->attrs);
1574           if (label->effective_attrs)
1575             pango_attr_list_unref (label->effective_attrs);
1576           label->effective_attrs = label->attrs;
1577         }
1578     }
1579
1580   if (!label->use_underline)
1581     label->mnemonic_keyval = GDK_VoidSymbol;
1582
1583   if (keyval != label->mnemonic_keyval)
1584     {
1585       gtk_label_setup_mnemonic (label, keyval);
1586       g_object_notify (G_OBJECT (label), "mnemonic-keyval");
1587     }
1588
1589   gtk_label_clear_layout (label);  
1590   gtk_widget_queue_resize (GTK_WIDGET (label));
1591 }
1592
1593 /**
1594  * gtk_label_set_text:
1595  * @label: a #GtkLabel
1596  * @str: The text you want to set
1597  *
1598  * Sets the text within the #GtkLabel widget. It overwrites any text that
1599  * was there before.  
1600  *
1601  * This will also clear any previously set mnemonic accelerators.
1602  **/
1603 void
1604 gtk_label_set_text (GtkLabel    *label,
1605                     const gchar *str)
1606 {
1607   g_return_if_fail (GTK_IS_LABEL (label));
1608   
1609   g_object_freeze_notify (G_OBJECT (label));
1610
1611   gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1612   gtk_label_set_use_markup_internal (label, FALSE);
1613   gtk_label_set_use_underline_internal (label, FALSE);
1614   
1615   gtk_label_recalculate (label);
1616
1617   g_object_thaw_notify (G_OBJECT (label));
1618 }
1619
1620 /**
1621  * gtk_label_set_attributes:
1622  * @label: a #GtkLabel
1623  * @attrs: a #PangoAttrList
1624  * 
1625  * Sets a #PangoAttrList; the attributes in the list are applied to the
1626  * label text. The attributes set with this function will be ignored
1627  * if the #GtkLabel:use-underline" or #GtkLabel:use-markup properties
1628  * are set to %TRUE.
1629  **/
1630 void
1631 gtk_label_set_attributes (GtkLabel         *label,
1632                           PangoAttrList    *attrs)
1633 {
1634   g_return_if_fail (GTK_IS_LABEL (label));
1635
1636   gtk_label_set_attributes_internal (label, attrs);
1637   
1638   gtk_label_clear_layout (label);  
1639   gtk_widget_queue_resize (GTK_WIDGET (label));
1640 }
1641
1642 /**
1643  * gtk_label_get_attributes:
1644  * @label: a #GtkLabel
1645  *
1646  * Gets the attribute list that was set on the label using
1647  * gtk_label_set_attributes(), if any. This function does
1648  * not reflect attributes that come from the labels markup
1649  * (see gtk_label_set_markup()). If you want to get the
1650  * effective attributes for the label, use
1651  * pango_layout_get_attribute (gtk_label_get_layout (label)).
1652  *
1653  * Return value: the attribute list, or %NULL if none was set.
1654  **/
1655 PangoAttrList *
1656 gtk_label_get_attributes (GtkLabel *label)
1657 {
1658   g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1659
1660   return label->attrs;
1661 }
1662
1663 /**
1664  * gtk_label_set_label:
1665  * @label: a #GtkLabel
1666  * @str: the new text to set for the label
1667  *
1668  * Sets the text of the label. The label is interpreted as
1669  * including embedded underlines and/or Pango markup depending
1670  * on the values of the #GtkLabel:use-underline" and
1671  * #GtkLabel:use-markup properties.
1672  **/
1673 void
1674 gtk_label_set_label (GtkLabel    *label,
1675                      const gchar *str)
1676 {
1677   g_return_if_fail (GTK_IS_LABEL (label));
1678   g_return_if_fail (str != NULL);
1679
1680   g_object_freeze_notify (G_OBJECT (label));
1681
1682   gtk_label_set_label_internal (label, g_strdup (str));
1683   gtk_label_recalculate (label);
1684
1685   g_object_thaw_notify (G_OBJECT (label));
1686 }
1687
1688 /**
1689  * gtk_label_get_label:
1690  * @label: a #GtkLabel
1691  *
1692  * Fetches the text from a label widget including any embedded
1693  * underlines indicating mnemonics and Pango markup. (See
1694  * gtk_label_get_text()).
1695  *
1696  * Return value: the text of the label widget. This string is
1697  *   owned by the widget and must not be modified or freed.
1698  **/
1699 G_CONST_RETURN gchar *
1700 gtk_label_get_label (GtkLabel *label)
1701 {
1702   g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1703
1704   return label->label;
1705 }
1706
1707 static void
1708 set_markup (GtkLabel    *label,
1709             const gchar *str,
1710             gboolean     with_uline)
1711 {
1712   gchar *text = NULL;
1713   GError *error = NULL;
1714   PangoAttrList *attrs = NULL;
1715   gunichar accel_char = 0;
1716
1717   if (!pango_parse_markup (str,
1718                            -1,
1719                            with_uline ? '_' : 0,
1720                            &attrs,
1721                            &text,
1722                            with_uline ? &accel_char : NULL,
1723                            &error))
1724     {
1725       g_warning ("Failed to set text from markup due to error parsing markup: %s",
1726                  error->message);
1727       g_error_free (error);
1728       return;
1729     }
1730
1731   if (text)
1732     gtk_label_set_text_internal (label, text);
1733
1734   if (attrs)
1735     {
1736       if (label->effective_attrs)
1737         pango_attr_list_unref (label->effective_attrs);
1738       label->effective_attrs = attrs;
1739     }
1740
1741   if (accel_char != 0)
1742     label->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
1743   else
1744     label->mnemonic_keyval = GDK_VoidSymbol;
1745 }
1746
1747 /**
1748  * gtk_label_set_markup:
1749  * @label: a #GtkLabel
1750  * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
1751  * 
1752  * Parses @str which is marked up with the <link
1753  * linkend="PangoMarkupFormat">Pango text markup language</link>, setting the
1754  * label's text and attribute list based on the parse results. If the @str is
1755  * external data, you may need to escape it with g_markup_escape_text() or
1756  * g_markup_printf_escaped()<!-- -->:
1757  * |[
1758  * char *markup;
1759  *   
1760  * markup = g_markup_printf_escaped ("&lt;span style=\"italic\"&gt;&percnt;s&lt;/span&gt;", str);
1761  * gtk_label_set_markup (GTK_LABEL (label), markup);
1762  * g_free (markup);
1763  * ]|
1764  **/
1765 void
1766 gtk_label_set_markup (GtkLabel    *label,
1767                       const gchar *str)
1768 {  
1769   g_return_if_fail (GTK_IS_LABEL (label));
1770
1771   g_object_freeze_notify (G_OBJECT (label));
1772
1773   gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1774   gtk_label_set_use_markup_internal (label, TRUE);
1775   gtk_label_set_use_underline_internal (label, FALSE);
1776   
1777   gtk_label_recalculate (label);
1778
1779   g_object_thaw_notify (G_OBJECT (label));
1780 }
1781
1782 /**
1783  * gtk_label_set_markup_with_mnemonic:
1784  * @label: a #GtkLabel
1785  * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
1786  * 
1787  * Parses @str which is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>,
1788  * setting the label's text and attribute list based on the parse results.
1789  * If characters in @str are preceded by an underscore, they are underlined
1790  * indicating that they represent a keyboard accelerator called a mnemonic.
1791  *
1792  * The mnemonic key can be used to activate another widget, chosen 
1793  * automatically, or explicitly using gtk_label_set_mnemonic_widget().
1794  **/
1795 void
1796 gtk_label_set_markup_with_mnemonic (GtkLabel    *label,
1797                                     const gchar *str)
1798 {
1799   g_return_if_fail (GTK_IS_LABEL (label));
1800
1801   g_object_freeze_notify (G_OBJECT (label));
1802
1803   gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
1804   gtk_label_set_use_markup_internal (label, TRUE);
1805   gtk_label_set_use_underline_internal (label, TRUE);
1806   
1807   gtk_label_recalculate (label);
1808
1809   g_object_thaw_notify (G_OBJECT (label));
1810 }
1811
1812 /**
1813  * gtk_label_get_text:
1814  * @label: a #GtkLabel
1815  * 
1816  * Fetches the text from a label widget, as displayed on the
1817  * screen. This does not include any embedded underlines
1818  * indicating mnemonics or Pango markup. (See gtk_label_get_label())
1819  * 
1820  * Return value: the text in the label widget. This is the internal
1821  *   string used by the label, and must not be modified.
1822  **/
1823 G_CONST_RETURN gchar *
1824 gtk_label_get_text (GtkLabel *label)
1825 {
1826   g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1827
1828   return label->text;
1829 }
1830
1831 static PangoAttrList *
1832 gtk_label_pattern_to_attrs (GtkLabel      *label,
1833                             const gchar   *pattern)
1834 {
1835   const char *start;
1836   const char *p = label->text;
1837   const char *q = pattern;
1838   PangoAttrList *attrs;
1839
1840   attrs = pango_attr_list_new ();
1841
1842   while (1)
1843     {
1844       while (*p && *q && *q != '_')
1845         {
1846           p = g_utf8_next_char (p);
1847           q++;
1848         }
1849       start = p;
1850       while (*p && *q && *q == '_')
1851         {
1852           p = g_utf8_next_char (p);
1853           q++;
1854         }
1855       
1856       if (p > start)
1857         {
1858           PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
1859           attr->start_index = start - label->text;
1860           attr->end_index = p - label->text;
1861           
1862           pango_attr_list_insert (attrs, attr);
1863         }
1864       else
1865         break;
1866     }
1867
1868   return attrs;
1869 }
1870
1871 static void
1872 gtk_label_set_pattern_internal (GtkLabel    *label,
1873                                 const gchar *pattern)
1874 {
1875   PangoAttrList *attrs;
1876   gboolean enable_mnemonics;
1877
1878   g_return_if_fail (GTK_IS_LABEL (label));
1879
1880   if (label->pattern_set)
1881     return;
1882
1883   g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
1884                 "gtk-enable-mnemonics", &enable_mnemonics,
1885                 NULL);
1886
1887   if (enable_mnemonics && pattern)
1888     attrs = gtk_label_pattern_to_attrs (label, pattern);
1889   else
1890     attrs = NULL;
1891
1892   if (label->effective_attrs)
1893     pango_attr_list_unref (label->effective_attrs);
1894   label->effective_attrs = attrs;
1895 }
1896
1897 void
1898 gtk_label_set_pattern (GtkLabel    *label,
1899                        const gchar *pattern)
1900 {
1901   g_return_if_fail (GTK_IS_LABEL (label));
1902   
1903   label->pattern_set = FALSE;
1904
1905   if (pattern)
1906     {
1907       gtk_label_set_pattern_internal (label, pattern);
1908       label->pattern_set = TRUE;
1909     }
1910   else
1911     gtk_label_recalculate (label);
1912
1913   gtk_label_clear_layout (label);  
1914   gtk_widget_queue_resize (GTK_WIDGET (label));
1915 }
1916
1917
1918 /**
1919  * gtk_label_set_justify:
1920  * @label: a #GtkLabel
1921  * @jtype: a #GtkJustification
1922  *
1923  * Sets the alignment of the lines in the text of the label relative to
1924  * each other. %GTK_JUSTIFY_LEFT is the default value when the
1925  * widget is first created with gtk_label_new(). If you instead want
1926  * to set the alignment of the label as a whole, use
1927  * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
1928  * effect on labels containing only a single line.
1929  **/
1930 void
1931 gtk_label_set_justify (GtkLabel        *label,
1932                        GtkJustification jtype)
1933 {
1934   g_return_if_fail (GTK_IS_LABEL (label));
1935   g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
1936   
1937   if ((GtkJustification) label->jtype != jtype)
1938     {
1939       label->jtype = jtype;
1940
1941       /* No real need to be this drastic, but easier than duplicating the code */
1942       gtk_label_clear_layout (label);
1943       
1944       g_object_notify (G_OBJECT (label), "justify");
1945       gtk_widget_queue_resize (GTK_WIDGET (label));
1946     }
1947 }
1948
1949 /**
1950  * gtk_label_get_justify:
1951  * @label: a #GtkLabel
1952  *
1953  * Returns the justification of the label. See gtk_label_set_justify().
1954  *
1955  * Return value: #GtkJustification
1956  **/
1957 GtkJustification
1958 gtk_label_get_justify (GtkLabel *label)
1959 {
1960   g_return_val_if_fail (GTK_IS_LABEL (label), 0);
1961
1962   return label->jtype;
1963 }
1964
1965 /**
1966  * gtk_label_set_ellipsize:
1967  * @label: a #GtkLabel
1968  * @mode: a #PangoEllipsizeMode
1969  *
1970  * Sets the mode used to ellipsize (add an ellipsis: "...") to the text 
1971  * if there is not enough space to render the entire string.
1972  *
1973  * Since: 2.6
1974  **/
1975 void
1976 gtk_label_set_ellipsize (GtkLabel          *label,
1977                          PangoEllipsizeMode mode)
1978 {
1979   g_return_if_fail (GTK_IS_LABEL (label));
1980   g_return_if_fail (mode >= PANGO_ELLIPSIZE_NONE && mode <= PANGO_ELLIPSIZE_END);
1981   
1982   if ((PangoEllipsizeMode) label->ellipsize != mode)
1983     {
1984       label->ellipsize = mode;
1985
1986       /* No real need to be this drastic, but easier than duplicating the code */
1987       gtk_label_clear_layout (label);
1988       
1989       g_object_notify (G_OBJECT (label), "ellipsize");
1990       gtk_widget_queue_resize (GTK_WIDGET (label));
1991     }
1992 }
1993
1994 /**
1995  * gtk_label_get_ellipsize:
1996  * @label: a #GtkLabel
1997  *
1998  * Returns the ellipsizing position of the label. See gtk_label_set_ellipsize().
1999  *
2000  * Return value: #PangoEllipsizeMode
2001  *
2002  * Since: 2.6
2003  **/
2004 PangoEllipsizeMode
2005 gtk_label_get_ellipsize (GtkLabel *label)
2006 {
2007   g_return_val_if_fail (GTK_IS_LABEL (label), PANGO_ELLIPSIZE_NONE);
2008
2009   return label->ellipsize;
2010 }
2011
2012 /**
2013  * gtk_label_set_width_chars:
2014  * @label: a #GtkLabel
2015  * @n_chars: the new desired width, in characters.
2016  * 
2017  * Sets the desired width in characters of @label to @n_chars.
2018  * 
2019  * Since: 2.6
2020  **/
2021 void
2022 gtk_label_set_width_chars (GtkLabel *label,
2023                            gint      n_chars)
2024 {
2025   GtkLabelPrivate *priv;
2026
2027   g_return_if_fail (GTK_IS_LABEL (label));
2028
2029   priv = GTK_LABEL_GET_PRIVATE (label);
2030
2031   if (priv->width_chars != n_chars)
2032     {
2033       priv->width_chars = n_chars;
2034       g_object_notify (G_OBJECT (label), "width-chars");
2035       gtk_label_invalidate_wrap_width (label);
2036       gtk_widget_queue_resize (GTK_WIDGET (label));
2037     }
2038 }
2039
2040 /**
2041  * gtk_label_get_width_chars:
2042  * @label: a #GtkLabel
2043  * 
2044  * Retrieves the desired width of @label, in characters. See
2045  * gtk_label_set_width_chars().
2046  * 
2047  * Return value: the width of the label in characters.
2048  * 
2049  * Since: 2.6
2050  **/
2051 gint
2052 gtk_label_get_width_chars (GtkLabel *label)
2053 {
2054   g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2055
2056   return GTK_LABEL_GET_PRIVATE (label)->width_chars;
2057 }
2058
2059 /**
2060  * gtk_label_set_max_width_chars:
2061  * @label: a #GtkLabel
2062  * @n_chars: the new desired maximum width, in characters.
2063  * 
2064  * Sets the desired maximum width in characters of @label to @n_chars.
2065  * 
2066  * Since: 2.6
2067  **/
2068 void
2069 gtk_label_set_max_width_chars (GtkLabel *label,
2070                                gint      n_chars)
2071 {
2072   GtkLabelPrivate *priv;
2073
2074   g_return_if_fail (GTK_IS_LABEL (label));
2075
2076   priv = GTK_LABEL_GET_PRIVATE (label);
2077
2078   if (priv->max_width_chars != n_chars)
2079     {
2080       priv->max_width_chars = n_chars;
2081
2082       g_object_notify (G_OBJECT (label), "max-width-chars");
2083       gtk_label_invalidate_wrap_width (label);
2084       gtk_widget_queue_resize (GTK_WIDGET (label));
2085     }
2086 }
2087
2088 /**
2089  * gtk_label_get_max_width_chars:
2090  * @label: a #GtkLabel
2091  * 
2092  * Retrieves the desired maximum width of @label, in characters. See
2093  * gtk_label_set_width_chars().
2094  * 
2095  * Return value: the maximum width of the label in characters.
2096  * 
2097  * Since: 2.6
2098  **/
2099 gint
2100 gtk_label_get_max_width_chars (GtkLabel *label)
2101 {
2102   g_return_val_if_fail (GTK_IS_LABEL (label), -1);
2103
2104   return GTK_LABEL_GET_PRIVATE (label)->max_width_chars;
2105 }
2106
2107 /**
2108  * gtk_label_set_line_wrap:
2109  * @label: a #GtkLabel
2110  * @wrap: the setting
2111  *
2112  * Toggles line wrapping within the #GtkLabel widget. %TRUE makes it break
2113  * lines if text exceeds the widget's size. %FALSE lets the text get cut off
2114  * by the edge of the widget if it exceeds the widget size.
2115  *
2116  * Note that setting line wrapping to %TRUE does not make the label
2117  * wrap at its parent container's width, because GTK+ widgets
2118  * conceptually can't make their requisition depend on the parent
2119  * container's size. For a label that wraps at a specific position,
2120  * set the label's width using gtk_widget_set_size_request().
2121  **/
2122 void
2123 gtk_label_set_line_wrap (GtkLabel *label,
2124                          gboolean  wrap)
2125 {
2126   g_return_if_fail (GTK_IS_LABEL (label));
2127   
2128   wrap = wrap != FALSE;
2129   
2130   if (label->wrap != wrap)
2131     {
2132       label->wrap = wrap;
2133
2134       gtk_label_clear_layout (label);
2135       gtk_widget_queue_resize (GTK_WIDGET (label));
2136       g_object_notify (G_OBJECT (label), "wrap");
2137     }
2138 }
2139
2140 /**
2141  * gtk_label_get_line_wrap:
2142  * @label: a #GtkLabel
2143  *
2144  * Returns whether lines in the label are automatically wrapped. 
2145  * See gtk_label_set_line_wrap().
2146  *
2147  * Return value: %TRUE if the lines of the label are automatically wrapped.
2148  */
2149 gboolean
2150 gtk_label_get_line_wrap (GtkLabel *label)
2151 {
2152   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2153
2154   return label->wrap;
2155 }
2156
2157 /**
2158  * gtk_label_set_line_wrap_mode:
2159  * @label: a #GtkLabel
2160  * @wrap_mode: the line wrapping mode
2161  *
2162  * If line wrapping is on (see gtk_label_set_line_wrap()) this controls how
2163  * the line wrapping is done. The default is %PANGO_WRAP_WORD which means
2164  * wrap on word boundaries.
2165  *
2166  * Since: 2.10
2167  **/
2168 void
2169 gtk_label_set_line_wrap_mode (GtkLabel *label,
2170                               PangoWrapMode wrap_mode)
2171 {
2172   g_return_if_fail (GTK_IS_LABEL (label));
2173   
2174   if (label->wrap_mode != wrap_mode)
2175     {
2176       label->wrap_mode = wrap_mode;
2177       g_object_notify (G_OBJECT (label), "wrap-mode");
2178       
2179       gtk_widget_queue_resize (GTK_WIDGET (label));
2180     }
2181 }
2182
2183 /**
2184  * gtk_label_get_line_wrap_mode:
2185  * @label: a #GtkLabel
2186  *
2187  * Returns line wrap mode used by the label. See gtk_label_set_line_wrap_mode().
2188  *
2189  * Return value: %TRUE if the lines of the label are automatically wrapped.
2190  *
2191  * Since: 2.10
2192  */
2193 PangoWrapMode
2194 gtk_label_get_line_wrap_mode (GtkLabel *label)
2195 {
2196   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
2197
2198   return label->wrap_mode;
2199 }
2200
2201
2202 void
2203 gtk_label_get (GtkLabel *label,
2204                gchar   **str)
2205 {
2206   g_return_if_fail (GTK_IS_LABEL (label));
2207   g_return_if_fail (str != NULL);
2208   
2209   *str = label->text;
2210 }
2211
2212 static void
2213 gtk_label_destroy (GtkObject *object)
2214 {
2215   GtkLabel *label = GTK_LABEL (object);
2216
2217   gtk_label_set_mnemonic_widget (label, NULL);
2218
2219   GTK_OBJECT_CLASS (gtk_label_parent_class)->destroy (object);
2220 }
2221
2222 static void
2223 gtk_label_finalize (GObject *object)
2224 {
2225   GtkLabel *label = GTK_LABEL (object);
2226
2227   g_free (label->label);
2228   g_free (label->text);
2229
2230   if (label->layout)
2231     g_object_unref (label->layout);
2232
2233   if (label->attrs)
2234     pango_attr_list_unref (label->attrs);
2235
2236   if (label->effective_attrs)
2237     pango_attr_list_unref (label->effective_attrs);
2238
2239   g_free (label->select_info);
2240
2241   G_OBJECT_CLASS (gtk_label_parent_class)->finalize (object);
2242 }
2243
2244 static void
2245 gtk_label_clear_layout (GtkLabel *label)
2246 {
2247   if (label->layout)
2248     {
2249       g_object_unref (label->layout);
2250       label->layout = NULL;
2251     }
2252 }
2253
2254 static gint
2255 get_label_char_width (GtkLabel *label)
2256 {
2257   GtkLabelPrivate *priv;
2258   PangoContext *context;
2259   PangoFontMetrics *metrics;
2260   gint char_width, digit_width, char_pixels, w;
2261   
2262   priv = GTK_LABEL_GET_PRIVATE (label);
2263   
2264   context = pango_layout_get_context (label->layout);
2265   metrics = pango_context_get_metrics (context, GTK_WIDGET (label)->style->font_desc, 
2266                                        pango_context_get_language (context));
2267   
2268   char_width = pango_font_metrics_get_approximate_char_width (metrics);
2269   digit_width = pango_font_metrics_get_approximate_digit_width (metrics);
2270   char_pixels = MAX (char_width, digit_width);
2271   pango_font_metrics_unref (metrics);
2272   
2273   if (priv->width_chars < 0)
2274     {
2275       PangoRectangle rect;
2276       
2277       pango_layout_set_width (label->layout, -1);
2278       pango_layout_get_extents (label->layout, NULL, &rect);
2279       
2280       w = char_pixels * MAX (priv->max_width_chars, 3);
2281       w = MIN (rect.width, w);
2282     }
2283   else
2284     {
2285       /* enforce minimum width for ellipsized labels at ~3 chars */
2286       w = char_pixels * MAX (priv->width_chars, 3);
2287     }
2288   
2289   return w;
2290 }
2291
2292 static void
2293 gtk_label_invalidate_wrap_width (GtkLabel *label)
2294 {
2295   GtkLabelPrivate *priv;
2296
2297   priv = GTK_LABEL_GET_PRIVATE (label);
2298
2299   priv->wrap_width = -1;
2300 }
2301
2302 static gint
2303 get_label_wrap_width (GtkLabel *label)
2304 {
2305   GtkLabelPrivate *priv;
2306
2307   priv = GTK_LABEL_GET_PRIVATE (label);
2308   
2309   if (priv->wrap_width < 0)
2310     {
2311       if (priv->width_chars > 0 || priv->max_width_chars > 0)
2312         priv->wrap_width = get_label_char_width (label);
2313       else
2314         {
2315           PangoLayout *layout;
2316   
2317           layout = gtk_widget_create_pango_layout (GTK_WIDGET (label), 
2318                                                    "This long string gives a good enough length for any line to have.");
2319           pango_layout_get_size (layout, &priv->wrap_width, NULL);
2320           g_object_unref (layout);
2321         }
2322     }
2323
2324   return priv->wrap_width;
2325 }
2326
2327 static void
2328 gtk_label_ensure_layout (GtkLabel *label)
2329 {
2330   GtkWidget *widget;
2331   PangoRectangle logical_rect;
2332   gboolean rtl;
2333
2334   widget = GTK_WIDGET (label);
2335
2336   rtl = gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL;
2337
2338   if (!label->layout)
2339     {
2340       PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
2341       gdouble angle = gtk_label_get_angle (label);
2342
2343       if (angle != 0.0 && !label->wrap && !label->ellipsize && !label->select_info)
2344         {
2345           /* We rotate the standard singleton PangoContext for the widget,
2346            * depending on the fact that it's meant pretty much exclusively
2347            * for our use.
2348            */
2349           PangoMatrix matrix = PANGO_MATRIX_INIT;
2350           
2351           pango_matrix_rotate (&matrix, angle);
2352
2353           pango_context_set_matrix (gtk_widget_get_pango_context (widget), &matrix);
2354           
2355           label->have_transform = TRUE;
2356         }
2357       else 
2358         {
2359           if (label->have_transform)
2360             pango_context_set_matrix (gtk_widget_get_pango_context (widget), NULL);
2361
2362           label->have_transform = FALSE;
2363         }
2364
2365       label->layout = gtk_widget_create_pango_layout (widget, label->text);
2366
2367       if (label->effective_attrs)
2368         pango_layout_set_attributes (label->layout, label->effective_attrs);
2369       
2370       switch (label->jtype)
2371         {
2372         case GTK_JUSTIFY_LEFT:
2373           align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
2374           break;
2375         case GTK_JUSTIFY_RIGHT:
2376           align = rtl ? PANGO_ALIGN_LEFT : PANGO_ALIGN_RIGHT;
2377           break;
2378         case GTK_JUSTIFY_CENTER:
2379           align = PANGO_ALIGN_CENTER;
2380           break;
2381         case GTK_JUSTIFY_FILL:
2382           align = rtl ? PANGO_ALIGN_RIGHT : PANGO_ALIGN_LEFT;
2383           pango_layout_set_justify (label->layout, TRUE);
2384           break;
2385         default:
2386           g_assert_not_reached();
2387         }
2388
2389       pango_layout_set_alignment (label->layout, align);
2390       pango_layout_set_ellipsize (label->layout, label->ellipsize);
2391       pango_layout_set_single_paragraph_mode (label->layout, label->single_line_mode);
2392
2393       if (label->ellipsize)
2394         pango_layout_set_width (label->layout, 
2395                                 widget->allocation.width * PANGO_SCALE);
2396       else if (label->wrap)
2397         {
2398           GtkWidgetAuxInfo *aux_info;
2399           gint longest_paragraph;
2400           gint width, height;
2401
2402           pango_layout_set_wrap (label->layout, label->wrap_mode);
2403           
2404           aux_info = _gtk_widget_get_aux_info (widget, FALSE);
2405           if (aux_info && aux_info->width > 0)
2406             pango_layout_set_width (label->layout, aux_info->width * PANGO_SCALE);
2407           else
2408             {
2409               GdkScreen *screen = gtk_widget_get_screen (GTK_WIDGET (label));
2410               gint wrap_width;
2411               
2412               pango_layout_set_width (label->layout, -1);
2413               pango_layout_get_extents (label->layout, NULL, &logical_rect);
2414
2415               width = logical_rect.width;
2416               
2417               /* Try to guess a reasonable maximum width */
2418               longest_paragraph = width;
2419
2420               wrap_width = get_label_wrap_width (label);
2421               width = MIN (width, wrap_width);
2422               width = MIN (width,
2423                            PANGO_SCALE * (gdk_screen_get_width (screen) + 1) / 2);
2424               
2425               pango_layout_set_width (label->layout, width);
2426               pango_layout_get_extents (label->layout, NULL, &logical_rect);
2427               width = logical_rect.width;
2428               height = logical_rect.height;
2429               
2430               /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
2431                * so we try short search for a narrower width that leaves us with the same height
2432                */
2433               if (longest_paragraph > 0)
2434                 {
2435                   gint nlines, perfect_width;
2436                   
2437                   nlines = pango_layout_get_line_count (label->layout);
2438                   perfect_width = (longest_paragraph + nlines - 1) / nlines;
2439                   
2440                   if (perfect_width < width)
2441                     {
2442                       pango_layout_set_width (label->layout, perfect_width);
2443                       pango_layout_get_extents (label->layout, NULL, &logical_rect);
2444                       
2445                       if (logical_rect.height <= height)
2446                         width = logical_rect.width;
2447                       else
2448                         {
2449                           gint mid_width = (perfect_width + width) / 2;
2450                           
2451                           if (mid_width > perfect_width)
2452                             {
2453                               pango_layout_set_width (label->layout, mid_width);
2454                               pango_layout_get_extents (label->layout, NULL, &logical_rect);
2455                               
2456                               if (logical_rect.height <= height)
2457                                 width = logical_rect.width;
2458                             }
2459                         }
2460                     }
2461                 }
2462               pango_layout_set_width (label->layout, width);
2463             }
2464         }
2465       else /* !label->wrap */
2466         pango_layout_set_width (label->layout, -1);
2467     }
2468 }
2469
2470 static void
2471 gtk_label_size_request (GtkWidget      *widget,
2472                         GtkRequisition *requisition)
2473 {
2474   GtkLabel *label = GTK_LABEL (widget);
2475   GtkLabelPrivate *priv;
2476   gint width, height;
2477   PangoRectangle logical_rect;
2478   GtkWidgetAuxInfo *aux_info;
2479
2480   priv = GTK_LABEL_GET_PRIVATE (widget);
2481
2482   /*  
2483    * If word wrapping is on, then the height requisition can depend
2484    * on:
2485    *
2486    *   - Any width set on the widget via gtk_widget_set_size_request().
2487    *   - The padding of the widget (xpad, set by gtk_misc_set_padding)
2488    *
2489    * Instead of trying to detect changes to these quantities, if we
2490    * are wrapping, we just rewrap for each size request. Since
2491    * size requisitions are cached by the GTK+ core, this is not
2492    * expensive.
2493    */
2494
2495   if (label->wrap)
2496     gtk_label_clear_layout (label);
2497
2498   gtk_label_ensure_layout (label);
2499
2500   width = label->misc.xpad * 2;
2501   height = label->misc.ypad * 2;
2502
2503   aux_info = _gtk_widget_get_aux_info (widget, FALSE);
2504
2505   if (label->have_transform)
2506     {
2507       PangoRectangle rect;
2508       PangoContext *context = pango_layout_get_context (label->layout);
2509       const PangoMatrix *matrix = pango_context_get_matrix (context);
2510
2511       pango_layout_get_extents (label->layout, NULL, &rect);
2512       pango_matrix_transform_rectangle (matrix, &rect);
2513       pango_extents_to_pixels (&rect, NULL);
2514       
2515       requisition->width = width + rect.width;
2516       requisition->height = height + rect.height;
2517
2518       return;
2519     }
2520   else
2521     pango_layout_get_extents (label->layout, NULL, &logical_rect);
2522
2523   if ((label->wrap || label->ellipsize || 
2524        priv->width_chars > 0 || priv->max_width_chars > 0) && 
2525       aux_info && aux_info->width > 0)
2526     width += aux_info->width;
2527   else if (label->ellipsize || priv->width_chars > 0 || priv->max_width_chars > 0)
2528     {
2529       width += PANGO_PIXELS (get_label_char_width (label));
2530     }
2531   else
2532     width += PANGO_PIXELS (logical_rect.width);
2533
2534   if (label->single_line_mode)
2535     {
2536       PangoContext *context;
2537       PangoFontMetrics *metrics;
2538       gint ascent, descent;
2539
2540       context = pango_layout_get_context (label->layout);
2541       metrics = pango_context_get_metrics (context, widget->style->font_desc,
2542                                            pango_context_get_language (context));
2543
2544       ascent = pango_font_metrics_get_ascent (metrics);
2545       descent = pango_font_metrics_get_descent (metrics);
2546       pango_font_metrics_unref (metrics);
2547     
2548       height += PANGO_PIXELS (ascent + descent);
2549     }
2550   else
2551     height += PANGO_PIXELS (logical_rect.height);
2552
2553   requisition->width = width;
2554   requisition->height = height;
2555 }
2556
2557 static void
2558 gtk_label_size_allocate (GtkWidget     *widget,
2559                          GtkAllocation *allocation)
2560 {
2561   GtkLabel *label;
2562
2563   label = GTK_LABEL (widget);
2564
2565   (* GTK_WIDGET_CLASS (gtk_label_parent_class)->size_allocate) (widget, allocation);
2566
2567   if (label->ellipsize)
2568     {
2569       if (label->layout)
2570         {
2571           gint width;
2572           PangoRectangle logical;
2573
2574           width = (allocation->width - label->misc.xpad * 2) * PANGO_SCALE;
2575
2576           pango_layout_set_width (label->layout, -1);
2577           pango_layout_get_extents (label->layout, NULL, &logical);
2578
2579           if (logical.width > width)
2580             pango_layout_set_width (label->layout, width);
2581         }
2582     }
2583
2584   if (label->select_info && label->select_info->window)
2585     {
2586       gdk_window_move_resize (label->select_info->window,
2587                               allocation->x,
2588                               allocation->y,
2589                               allocation->width,
2590                               allocation->height);
2591     }
2592 }
2593
2594 static void
2595 gtk_label_state_changed (GtkWidget   *widget,
2596                          GtkStateType prev_state)
2597 {
2598   GtkLabel *label;
2599   GdkCursor *cursor;
2600   
2601   label = GTK_LABEL (widget);
2602
2603   if (label->select_info)
2604     {
2605       gtk_label_select_region (label, 0, 0);
2606
2607       if (GTK_WIDGET_REALIZED (widget))
2608         {
2609           if (GTK_WIDGET_IS_SENSITIVE (widget))
2610             cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget), GDK_XTERM);
2611           else
2612             cursor = NULL;
2613
2614           gdk_window_set_cursor (label->select_info->window, cursor);
2615
2616           if (cursor)
2617             gdk_cursor_unref (cursor);
2618         }
2619     }
2620
2621   if (GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed)
2622     GTK_WIDGET_CLASS (gtk_label_parent_class)->state_changed (widget, prev_state);
2623 }
2624
2625 static void 
2626 gtk_label_style_set (GtkWidget *widget,
2627                      GtkStyle  *previous_style)
2628 {
2629   GtkLabel *label = GTK_LABEL (widget);
2630
2631   /* We have to clear the layout, fonts etc. may have changed */
2632   gtk_label_clear_layout (label);
2633   gtk_label_invalidate_wrap_width (label);
2634 }
2635
2636 static void 
2637 gtk_label_direction_changed (GtkWidget        *widget,
2638                              GtkTextDirection previous_dir)
2639 {
2640   GtkLabel *label = GTK_LABEL (widget);
2641
2642   if (label->layout)
2643     pango_layout_context_changed (label->layout);
2644
2645   GTK_WIDGET_CLASS (gtk_label_parent_class)->direction_changed (widget, previous_dir);
2646 }
2647
2648 static void
2649 get_layout_location (GtkLabel  *label,
2650                      gint      *xp,
2651                      gint      *yp)
2652 {
2653   GtkMisc *misc;
2654   GtkWidget *widget; 
2655   GtkLabelPrivate *priv;
2656   gfloat xalign;
2657   gint req_width, x, y;
2658   PangoRectangle logical;
2659   
2660   misc = GTK_MISC (label);
2661   widget = GTK_WIDGET (label);
2662   priv = GTK_LABEL_GET_PRIVATE (label);
2663
2664   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2665     xalign = misc->xalign;
2666   else
2667     xalign = 1.0 - misc->xalign;
2668
2669   pango_layout_get_pixel_extents (label->layout, NULL, &logical);
2670
2671   if (label->ellipsize || priv->width_chars > 0)
2672     {
2673       int width;
2674
2675       width = pango_layout_get_width (label->layout);
2676
2677       req_width = logical.width;
2678       if (width != -1)
2679         req_width = MIN(PANGO_PIXELS (width), req_width);
2680       req_width += 2 * misc->xpad;
2681     }
2682   else
2683     req_width = widget->requisition.width;
2684
2685   x = floor (widget->allocation.x + (gint)misc->xpad +
2686               xalign * (widget->allocation.width - req_width));
2687
2688   if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
2689     x = MAX (x, widget->allocation.x + misc->xpad);
2690   else
2691     x = MIN (x, widget->allocation.x + widget->allocation.width - misc->xpad);
2692   x -= logical.x;
2693
2694   y = floor (widget->allocation.y + (gint)misc->ypad 
2695              + MAX (((widget->allocation.height - widget->requisition.height) * misc->yalign),
2696              0));
2697
2698   if (xp)
2699     *xp = x;
2700
2701   if (yp)
2702     *yp = y;
2703 }
2704
2705 static void
2706 draw_insertion_cursor (GtkLabel      *label,
2707                        GdkRectangle  *cursor_location,
2708                        gboolean       is_primary,
2709                        PangoDirection direction,
2710                        gboolean       draw_arrow)
2711 {
2712   GtkWidget *widget = GTK_WIDGET (label);
2713   GtkTextDirection text_dir;
2714
2715   if (direction == PANGO_DIRECTION_LTR)
2716     text_dir = GTK_TEXT_DIR_LTR;
2717   else
2718     text_dir = GTK_TEXT_DIR_RTL;
2719
2720   gtk_draw_insertion_cursor (widget, widget->window, &(widget->allocation),
2721                              cursor_location,
2722                              is_primary, text_dir, draw_arrow);
2723 }
2724
2725 static PangoDirection
2726 get_cursor_direction (GtkLabel *label)
2727 {
2728   GSList *l;
2729
2730   g_assert (label->select_info);
2731
2732   gtk_label_ensure_layout (label);
2733
2734   for (l = pango_layout_get_lines_readonly (label->layout); l; l = l->next)
2735     {
2736       PangoLayoutLine *line = l->data;
2737
2738       /* If label->select_info->selection_end is at the very end of
2739        * the line, we don't know if the cursor is on this line or
2740        * the next without looking ahead at the next line. (End
2741        * of paragraph is different from line break.) But it's
2742        * definitely in this paragraph, which is good enough
2743        * to figure out the resolved direction.
2744        */
2745        if (line->start_index + line->length >= label->select_info->selection_end)
2746         return line->resolved_dir;
2747     }
2748
2749   return PANGO_DIRECTION_LTR;
2750 }
2751
2752 static void
2753 gtk_label_draw_cursor (GtkLabel  *label, gint xoffset, gint yoffset)
2754 {
2755   if (label->select_info == NULL)
2756     return;
2757   
2758   if (GTK_WIDGET_DRAWABLE (label))
2759     {
2760       GtkWidget *widget = GTK_WIDGET (label);
2761
2762       PangoDirection keymap_direction;
2763       PangoDirection cursor_direction;
2764       PangoRectangle strong_pos, weak_pos;
2765       gboolean split_cursor;
2766       PangoRectangle *cursor1 = NULL;
2767       PangoRectangle *cursor2 = NULL;
2768       GdkRectangle cursor_location;
2769       PangoDirection dir1 = PANGO_DIRECTION_NEUTRAL;
2770       PangoDirection dir2 = PANGO_DIRECTION_NEUTRAL;
2771
2772       keymap_direction = gdk_keymap_get_direction (gdk_keymap_get_for_display (gtk_widget_get_display (widget)));
2773       cursor_direction = get_cursor_direction (label);
2774
2775       gtk_label_ensure_layout (label);
2776       
2777       pango_layout_get_cursor_pos (label->layout, label->select_info->selection_end,
2778                                    &strong_pos, &weak_pos);
2779
2780       g_object_get (gtk_widget_get_settings (widget),
2781                     "gtk-split-cursor", &split_cursor,
2782                     NULL);
2783
2784       dir1 = cursor_direction;
2785       
2786       if (split_cursor)
2787         {
2788           cursor1 = &strong_pos;
2789
2790           if (strong_pos.x != weak_pos.x ||
2791               strong_pos.y != weak_pos.y)
2792             {
2793               dir2 = (cursor_direction == PANGO_DIRECTION_LTR) ? PANGO_DIRECTION_RTL : PANGO_DIRECTION_LTR;
2794               cursor2 = &weak_pos;
2795             }
2796         }
2797       else
2798         {
2799           if (keymap_direction == cursor_direction)
2800             cursor1 = &strong_pos;
2801           else
2802             cursor1 = &weak_pos;
2803         }
2804       
2805       cursor_location.x = xoffset + PANGO_PIXELS (cursor1->x);
2806       cursor_location.y = yoffset + PANGO_PIXELS (cursor1->y);
2807       cursor_location.width = 0;
2808       cursor_location.height = PANGO_PIXELS (cursor1->height);
2809
2810       draw_insertion_cursor (label,
2811                              &cursor_location, TRUE, dir1,
2812                              dir2 != PANGO_DIRECTION_NEUTRAL);
2813       
2814       if (dir2 != PANGO_DIRECTION_NEUTRAL)
2815         {
2816           cursor_location.x = xoffset + PANGO_PIXELS (cursor2->x);
2817           cursor_location.y = yoffset + PANGO_PIXELS (cursor2->y);
2818           cursor_location.width = 0;
2819           cursor_location.height = PANGO_PIXELS (cursor2->height);
2820
2821           draw_insertion_cursor (label,
2822                                  &cursor_location, FALSE, dir2,
2823                                  TRUE);
2824         }
2825     }
2826 }
2827
2828
2829 static gint
2830 gtk_label_expose (GtkWidget      *widget,
2831                   GdkEventExpose *event)
2832 {
2833   GtkLabel *label;
2834   gint x, y;
2835   
2836   g_return_val_if_fail (GTK_IS_LABEL (widget), FALSE);
2837   g_return_val_if_fail (event != NULL, FALSE);
2838   
2839   label = GTK_LABEL (widget);
2840
2841   gtk_label_ensure_layout (label);
2842   
2843   if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
2844       label->text && (*label->text != '\0'))
2845     {
2846       get_layout_location (label, &x, &y);
2847
2848       gtk_paint_layout (widget->style,
2849                         widget->window,
2850                         GTK_WIDGET_STATE (widget),
2851                         FALSE,
2852                         &event->area,
2853                         widget,
2854                         "label",
2855                         x, y,
2856                         label->layout);
2857       
2858       if (label->select_info &&
2859           (label->select_info->selection_anchor !=
2860            label->select_info->selection_end))
2861         {
2862           gint range[2];
2863           GdkRegion *clip;
2864           GtkStateType state;
2865           
2866           range[0] = label->select_info->selection_anchor;
2867           range[1] = label->select_info->selection_end;
2868
2869           if (range[0] > range[1])
2870             {
2871               gint tmp = range[0];
2872               range[0] = range[1];
2873               range[1] = tmp;
2874             }
2875
2876           clip = gdk_pango_layout_get_clip_region (label->layout,
2877                                                    x, y,
2878                                                    range,
2879                                                    1);
2880           gdk_region_intersect (clip, event->region);
2881  
2882          /* FIXME should use gtk_paint, but it can't use a clip
2883            * region
2884            */
2885
2886           gdk_gc_set_clip_region (widget->style->black_gc, clip);
2887
2888
2889           state = GTK_STATE_SELECTED;
2890           if (!GTK_WIDGET_HAS_FOCUS (widget))
2891             state = GTK_STATE_ACTIVE;
2892               
2893           gdk_draw_layout_with_colors (widget->window,
2894                                        widget->style->black_gc,
2895                                        x, y,
2896                                        label->layout,
2897                                        &widget->style->text[state],
2898                                        &widget->style->base[state]);
2899
2900           gdk_gc_set_clip_region (widget->style->black_gc, NULL);
2901           gdk_region_destroy (clip);
2902         }
2903       else if (label->select_info && GTK_WIDGET_HAS_FOCUS (widget))
2904         gtk_label_draw_cursor (label, x, y);
2905     }
2906
2907   return FALSE;
2908 }
2909
2910 static void
2911 gtk_label_set_uline_text_internal (GtkLabel    *label,
2912                                    const gchar *str)
2913 {
2914   guint accel_key = GDK_VoidSymbol;
2915
2916   gchar *new_str;
2917   gchar *pattern;
2918   const gchar *src;
2919   gchar *dest, *pattern_dest;
2920   gboolean underscore;
2921       
2922   g_return_if_fail (GTK_IS_LABEL (label));
2923   g_return_if_fail (str != NULL);
2924
2925   /* Split text into the base text and a separate pattern
2926    * of underscores.
2927    */
2928   
2929   new_str = g_new (gchar, strlen (str) + 1);
2930   pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
2931   
2932   underscore = FALSE;
2933
2934   if (str == NULL)
2935     str = "";
2936   
2937   src = str;
2938   dest = new_str;
2939   pattern_dest = pattern;
2940   
2941   while (*src)
2942     {
2943       gunichar c;
2944       gchar *next_src;
2945
2946       c = g_utf8_get_char (src);
2947       if (c == (gunichar)-1)
2948         {
2949           g_warning ("Invalid input string");
2950           g_free (new_str);
2951           g_free (pattern);
2952           return;
2953         }
2954       next_src = g_utf8_next_char (src);
2955       
2956       if (underscore)
2957         {
2958           if (c == '_')
2959             *pattern_dest++ = ' ';
2960           else
2961             {
2962               *pattern_dest++ = '_';
2963               if (accel_key == GDK_VoidSymbol)
2964                 accel_key = gdk_keyval_to_lower (gdk_unicode_to_keyval (c));
2965             }
2966
2967           while (src < next_src)
2968             *dest++ = *src++;
2969           
2970           underscore = FALSE;
2971         }
2972       else
2973         {
2974           if (c == '_')
2975             {
2976               underscore = TRUE;
2977               src = next_src;
2978             }
2979           else
2980             {
2981               while (src < next_src)
2982                 *dest++ = *src++;
2983           
2984               *pattern_dest++ = ' ';
2985             }
2986         }
2987     }
2988   *dest = 0;
2989   *pattern_dest = 0;
2990   
2991   gtk_label_set_text_internal (label, new_str);
2992   gtk_label_set_pattern_internal (label, pattern);
2993   
2994   g_free (pattern);
2995
2996   label->mnemonic_keyval = accel_key;
2997 }
2998
2999 guint      
3000 gtk_label_parse_uline (GtkLabel    *label,
3001                        const gchar *str)
3002 {
3003   guint keyval;
3004   
3005   g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
3006   g_return_val_if_fail (str != NULL, GDK_VoidSymbol);
3007
3008   g_object_freeze_notify (G_OBJECT (label));
3009   
3010   gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3011   gtk_label_set_use_markup_internal (label, FALSE);
3012   gtk_label_set_use_underline_internal (label, TRUE);
3013   
3014   gtk_label_recalculate (label);
3015
3016   keyval = label->mnemonic_keyval;
3017   if (keyval != GDK_VoidSymbol)
3018     {
3019       label->mnemonic_keyval = GDK_VoidSymbol;
3020       gtk_label_setup_mnemonic (label, keyval);
3021       g_object_notify (G_OBJECT (label), "mnemonic-keyval");
3022     }
3023   
3024   g_object_thaw_notify (G_OBJECT (label));
3025
3026   return keyval;
3027 }
3028
3029 /**
3030  * gtk_label_set_text_with_mnemonic:
3031  * @label: a #GtkLabel
3032  * @str: a string
3033  * 
3034  * Sets the label's text from the string @str.
3035  * If characters in @str are preceded by an underscore, they are underlined
3036  * indicating that they represent a keyboard accelerator called a mnemonic.
3037  * The mnemonic key can be used to activate another widget, chosen 
3038  * automatically, or explicitly using gtk_label_set_mnemonic_widget().
3039  **/
3040 void
3041 gtk_label_set_text_with_mnemonic (GtkLabel    *label,
3042                                   const gchar *str)
3043 {
3044   g_return_if_fail (GTK_IS_LABEL (label));
3045   g_return_if_fail (str != NULL);
3046
3047   g_object_freeze_notify (G_OBJECT (label));
3048
3049   gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
3050   gtk_label_set_use_markup_internal (label, FALSE);
3051   gtk_label_set_use_underline_internal (label, TRUE);
3052   
3053   gtk_label_recalculate (label);
3054
3055   g_object_thaw_notify (G_OBJECT (label));
3056 }
3057
3058 static void
3059 gtk_label_realize (GtkWidget *widget)
3060 {
3061   GtkLabel *label;
3062
3063   label = GTK_LABEL (widget);
3064   
3065   (* GTK_WIDGET_CLASS (gtk_label_parent_class)->realize) (widget);
3066
3067   if (label->select_info)
3068     gtk_label_create_window (label);
3069 }
3070
3071 static void
3072 gtk_label_unrealize (GtkWidget *widget)
3073 {
3074   GtkLabel *label;
3075
3076   label = GTK_LABEL (widget);
3077
3078   if (label->select_info)
3079     gtk_label_destroy_window (label);
3080   
3081   (* GTK_WIDGET_CLASS (gtk_label_parent_class)->unrealize) (widget);
3082 }
3083
3084 static void
3085 gtk_label_map (GtkWidget *widget)
3086 {
3087   GtkLabel *label;
3088
3089   label = GTK_LABEL (widget);
3090   
3091   (* GTK_WIDGET_CLASS (gtk_label_parent_class)->map) (widget);
3092   
3093   if (label->select_info)
3094     gdk_window_show (label->select_info->window);
3095 }
3096
3097 static void
3098 gtk_label_unmap (GtkWidget *widget)
3099 {
3100   GtkLabel *label;
3101
3102   label = GTK_LABEL (widget);
3103
3104   if (label->select_info)
3105     gdk_window_hide (label->select_info->window);
3106   
3107   (* GTK_WIDGET_CLASS (gtk_label_parent_class)->unmap) (widget);
3108 }
3109
3110 static void
3111 window_to_layout_coords (GtkLabel *label,
3112                          gint     *x,
3113                          gint     *y)
3114 {
3115   gint lx, ly;
3116   GtkWidget *widget;
3117
3118   widget = GTK_WIDGET (label);
3119   
3120   /* get layout location in widget->window coords */
3121   get_layout_location (label, &lx, &ly);
3122   
3123   if (x)
3124     {
3125       *x += widget->allocation.x; /* go to widget->window */
3126       *x -= lx;                   /* go to layout */
3127     }
3128
3129   if (y)
3130     {
3131       *y += widget->allocation.y; /* go to widget->window */
3132       *y -= ly;                   /* go to layout */
3133     }
3134 }
3135
3136 #if 0
3137 static void
3138 layout_to_window_coords (GtkLabel *label,
3139                          gint     *x,
3140                          gint     *y)
3141 {
3142   gint lx, ly;
3143   GtkWidget *widget;
3144
3145   widget = GTK_WIDGET (label);
3146   
3147   /* get layout location in widget->window coords */
3148   get_layout_location (label, &lx, &ly);
3149   
3150   if (x)
3151     {
3152       *x += lx;                   /* go to widget->window */
3153       *x -= widget->allocation.x; /* go to selection window */
3154     }
3155
3156   if (y)
3157     {
3158       *y += ly;                   /* go to widget->window */
3159       *y -= widget->allocation.y; /* go to selection window */
3160     }
3161 }
3162 #endif
3163
3164 static void
3165 get_layout_index (GtkLabel *label,
3166                   gint      x,
3167                   gint      y,
3168                   gint     *index)
3169 {
3170   gint trailing = 0;
3171   const gchar *cluster;
3172   const gchar *cluster_end;
3173
3174   *index = 0;
3175   
3176   gtk_label_ensure_layout (label);
3177   
3178   window_to_layout_coords (label, &x, &y);
3179
3180   x *= PANGO_SCALE;
3181   y *= PANGO_SCALE;
3182   
3183   pango_layout_xy_to_index (label->layout,
3184                             x, y,
3185                             index, &trailing);
3186
3187   
3188   cluster = label->text + *index;
3189   cluster_end = cluster;
3190   while (trailing)
3191     {
3192       cluster_end = g_utf8_next_char (cluster_end);
3193       --trailing;
3194     }
3195
3196   *index += (cluster_end - cluster);
3197 }
3198
3199 static void
3200 gtk_label_select_word (GtkLabel *label)
3201 {
3202   gint min, max;
3203   
3204   gint start_index = gtk_label_move_backward_word (label, label->select_info->selection_end);
3205   gint end_index = gtk_label_move_forward_word (label, label->select_info->selection_end);
3206
3207   min = MIN (label->select_info->selection_anchor,
3208              label->select_info->selection_end);
3209   max = MAX (label->select_info->selection_anchor,
3210              label->select_info->selection_end);
3211
3212   min = MIN (min, start_index);
3213   max = MAX (max, end_index);
3214
3215   gtk_label_select_region_index (label, min, max);
3216 }
3217
3218 static void
3219 gtk_label_grab_focus (GtkWidget *widget)
3220 {
3221   GtkLabel *label;
3222   gboolean select_on_focus;
3223   
3224   label = GTK_LABEL (widget);
3225
3226   if (label->select_info == NULL)
3227     return;
3228
3229   GTK_WIDGET_CLASS (gtk_label_parent_class)->grab_focus (widget);
3230
3231   g_object_get (gtk_widget_get_settings (widget),
3232                 "gtk-label-select-on-focus",
3233                 &select_on_focus,
3234                 NULL);
3235   
3236   if (select_on_focus && !label->in_click)
3237     gtk_label_select_region (label, 0, -1);
3238 }
3239  
3240 static gboolean
3241 gtk_label_button_press (GtkWidget      *widget,
3242                         GdkEventButton *event)
3243 {
3244   GtkLabel *label;
3245   gint index = 0;
3246   gint min, max;  
3247   
3248   label = GTK_LABEL (widget);
3249
3250   if (label->select_info == NULL)
3251     return FALSE;
3252
3253   label->select_info->in_drag = FALSE;
3254   if (event->button == 1)
3255     {
3256       if (!GTK_WIDGET_HAS_FOCUS (widget)) 
3257         {
3258           label->in_click = TRUE;
3259           gtk_widget_grab_focus (widget);
3260           label->in_click = FALSE;
3261         }
3262
3263       if (event->type == GDK_3BUTTON_PRESS)
3264         {
3265           gtk_label_select_region_index (label, 0, strlen (label->text));
3266           return TRUE;
3267         }
3268       
3269       if (event->type == GDK_2BUTTON_PRESS)
3270         {
3271           gtk_label_select_word (label);
3272           return TRUE;
3273         }
3274       
3275       get_layout_index (label, event->x, event->y, &index);
3276       
3277       min = MIN (label->select_info->selection_anchor,
3278                  label->select_info->selection_end);
3279       max = MAX (label->select_info->selection_anchor,
3280                  label->select_info->selection_end);
3281           
3282       if ((label->select_info->selection_anchor !=
3283            label->select_info->selection_end) &&
3284           (event->state & GDK_SHIFT_MASK))
3285         {
3286           /* extend (same as motion) */
3287           min = MIN (min, index);
3288           max = MAX (max, index);
3289           
3290           /* ensure the anchor is opposite index */
3291           if (index == min)
3292             {
3293               gint tmp = min;
3294               min = max;
3295               max = tmp;
3296             }
3297           
3298           gtk_label_select_region_index (label, min, max);
3299         }
3300       else 
3301         {
3302           if (event->type == GDK_3BUTTON_PRESS)
3303             gtk_label_select_region_index (label, 0, strlen (label->text));
3304           else if (event->type == GDK_2BUTTON_PRESS)
3305             gtk_label_select_word (label);
3306           else if (min < max && min <= index && index <= max)
3307             {
3308               label->select_info->in_drag = TRUE;
3309               label->select_info->drag_start_x = event->x;
3310               label->select_info->drag_start_y = event->y;
3311             }
3312           else
3313             /* start a replacement */
3314             gtk_label_select_region_index (label, index, index);
3315         }
3316   
3317       return TRUE;
3318     }
3319   else if (event->button == 3 && event->type == GDK_BUTTON_PRESS)
3320     {
3321       gtk_label_do_popup (label, event);
3322
3323       return TRUE;
3324       
3325     }
3326   return FALSE;
3327 }
3328
3329 static gboolean
3330 gtk_label_button_release (GtkWidget      *widget,
3331                           GdkEventButton *event)
3332
3333 {
3334   GtkLabel *label = GTK_LABEL (widget);
3335   gint index;
3336   
3337   if (label->select_info == NULL)
3338     return FALSE;
3339   
3340   if (label->select_info->in_drag)
3341     {
3342       label->select_info->in_drag = 0;
3343
3344       get_layout_index (label, event->x, event->y, &index);
3345       gtk_label_select_region_index (label, index, index);
3346       
3347       return FALSE;
3348     }
3349
3350   if (event->button != 1)
3351     return FALSE;
3352   
3353   /* The goal here is to return TRUE iff we ate the
3354    * button press to start selecting.
3355    */
3356   
3357   return TRUE;
3358 }
3359
3360 static void
3361 drag_begin_cb (GtkWidget      *widget,
3362                GdkDragContext *context,
3363                gpointer        data)
3364 {
3365   GtkLabel *label;
3366   GdkPixmap *pixmap = NULL;
3367
3368   g_signal_handlers_disconnect_by_func (widget, drag_begin_cb, NULL);
3369
3370   label = GTK_LABEL (widget);
3371
3372   if ((label->select_info->selection_anchor !=
3373        label->select_info->selection_end) &&
3374       label->text)
3375     {
3376       gint start, end;
3377       gint len;
3378       
3379       start = MIN (label->select_info->selection_anchor,
3380                    label->select_info->selection_end);
3381       end = MAX (label->select_info->selection_anchor,
3382                  label->select_info->selection_end);
3383       
3384       len = strlen (label->text);
3385       
3386       if (end > len)
3387         end = len;
3388       
3389       if (start > len)
3390         start = len;
3391       
3392       pixmap = _gtk_text_util_create_drag_icon (widget, 
3393                                                 label->text + start,
3394                                                 end - start);
3395     }
3396
3397   if (pixmap)
3398     gtk_drag_set_icon_pixmap (context,
3399                               gdk_drawable_get_colormap (pixmap),
3400                               pixmap,
3401                               NULL,
3402                               -2, -2);
3403   else
3404     gtk_drag_set_icon_default (context);
3405   
3406   if (pixmap)
3407     g_object_unref (pixmap);
3408 }
3409
3410 static gboolean
3411 gtk_label_motion (GtkWidget      *widget,
3412                   GdkEventMotion *event)
3413 {
3414   GtkLabel *label;
3415   gint index;
3416   gint x, y;
3417   
3418   label = GTK_LABEL (widget);
3419   
3420   if (label->select_info == NULL)
3421     return FALSE;  
3422
3423
3424   if ((event->state & GDK_BUTTON1_MASK) == 0)
3425     return FALSE;
3426
3427   gdk_window_get_pointer (label->select_info->window,
3428                           &x, &y, NULL);
3429   
3430   if (label->select_info->in_drag)
3431     {
3432       if (gtk_drag_check_threshold (widget,
3433                                     label->select_info->drag_start_x, 
3434                                     label->select_info->drag_start_y,
3435                                     event->x, event->y))
3436         {
3437           GtkTargetList *target_list = gtk_target_list_new (NULL, 0);
3438
3439           gtk_target_list_add_text_targets (target_list, 0);
3440           
3441           g_signal_connect (widget, "drag_begin", 
3442                             G_CALLBACK (drag_begin_cb), NULL);
3443           gtk_drag_begin (widget, target_list, 
3444                           GDK_ACTION_COPY,
3445                           1, (GdkEvent *)event);
3446           
3447           label->select_info->in_drag = FALSE;
3448           
3449           gtk_target_list_unref (target_list);
3450         }
3451     }
3452   else
3453     {
3454       get_layout_index (label, x, y, &index);
3455       
3456       gtk_label_select_region_index (label,
3457                                      label->select_info->selection_anchor,
3458                                      index);
3459     }
3460
3461   return TRUE;
3462 }
3463
3464 static void
3465 gtk_label_create_window (GtkLabel *label)
3466 {
3467   GtkWidget *widget;
3468   GdkWindowAttr attributes;
3469   gint attributes_mask;
3470   
3471   g_assert (label->select_info);
3472   g_assert (GTK_WIDGET_REALIZED (label));
3473   
3474   if (label->select_info->window)
3475     return;
3476   
3477   widget = GTK_WIDGET (label);
3478
3479   attributes.x = widget->allocation.x;
3480   attributes.y = widget->allocation.y;
3481   attributes.width = widget->allocation.width;
3482   attributes.height = widget->allocation.height;
3483   attributes.window_type = GDK_WINDOW_CHILD;
3484   attributes.wclass = GDK_INPUT_ONLY;
3485   attributes.override_redirect = TRUE;
3486   attributes.event_mask = gtk_widget_get_events (widget) |
3487     GDK_BUTTON_PRESS_MASK        |
3488     GDK_BUTTON_RELEASE_MASK      |
3489     GDK_BUTTON_MOTION_MASK;
3490   attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_NOREDIR; 
3491   if (GTK_WIDGET_IS_SENSITIVE (widget))
3492     {
3493       attributes.cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
3494                                                       GDK_XTERM);
3495       attributes_mask |= GDK_WA_CURSOR;
3496     }
3497
3498
3499   label->select_info->window = gdk_window_new (widget->window,
3500                                                &attributes, attributes_mask);
3501   gdk_window_set_user_data (label->select_info->window, widget);
3502
3503   if (attributes_mask & GDK_WA_CURSOR)
3504     gdk_cursor_unref (attributes.cursor);
3505 }
3506
3507 static void
3508 gtk_label_destroy_window (GtkLabel *label)
3509 {
3510   g_assert (label->select_info);
3511
3512   if (label->select_info->window == NULL)
3513     return;
3514   
3515   gdk_window_set_user_data (label->select_info->window, NULL);
3516   gdk_window_destroy (label->select_info->window);
3517   label->select_info->window = NULL;
3518 }
3519
3520 /**
3521  * gtk_label_set_selectable:
3522  * @label: a #GtkLabel
3523  * @setting: %TRUE to allow selecting text in the label
3524  *
3525  * Selectable labels allow the user to select text from the label, for
3526  * copy-and-paste.
3527  **/
3528 void
3529 gtk_label_set_selectable (GtkLabel *label,
3530                           gboolean  setting)
3531 {
3532   gboolean old_setting;
3533   
3534   g_return_if_fail (GTK_IS_LABEL (label));
3535   
3536   setting = setting != FALSE;
3537   old_setting = label->select_info != NULL;
3538   
3539   if (setting)
3540     {
3541       if (label->select_info == NULL)
3542         {
3543           label->select_info = g_new0 (GtkLabelSelectionInfo, 1);
3544
3545           GTK_WIDGET_SET_FLAGS (label, GTK_CAN_FOCUS);
3546       
3547           if (GTK_WIDGET_REALIZED (label))
3548             gtk_label_create_window (label);
3549
3550           if (GTK_WIDGET_MAPPED (label))
3551             gdk_window_show (label->select_info->window);
3552         }
3553     }
3554   else
3555     {
3556       if (label->select_info)
3557         {
3558           /* unselect, to give up the selection */
3559           gtk_label_select_region (label, 0, 0);
3560           
3561           if (label->select_info->window)
3562             {
3563               gtk_label_destroy_window (label);
3564             }
3565
3566           g_free (label->select_info);
3567
3568           label->select_info = NULL;
3569
3570           GTK_WIDGET_UNSET_FLAGS (label, GTK_CAN_FOCUS);
3571         }
3572     }
3573   if (setting != old_setting)
3574     {
3575       g_object_freeze_notify (G_OBJECT (label));
3576       g_object_notify (G_OBJECT (label), "selectable");
3577       g_object_notify (G_OBJECT (label), "cursor-position");
3578       g_object_notify (G_OBJECT (label), "selection-bound");
3579       g_object_thaw_notify (G_OBJECT (label));
3580       gtk_widget_queue_draw (GTK_WIDGET (label));
3581     }
3582 }
3583
3584 /**
3585  * gtk_label_get_selectable:
3586  * @label: a #GtkLabel
3587  * 
3588  * Gets the value set by gtk_label_set_selectable().
3589  * 
3590  * Return value: %TRUE if the user can copy text from the label
3591  **/
3592 gboolean
3593 gtk_label_get_selectable (GtkLabel *label)
3594 {
3595   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3596
3597   return label->select_info != NULL;
3598 }
3599
3600 static void
3601 free_angle (gpointer angle)
3602 {
3603   g_slice_free (gdouble, angle);
3604 }
3605
3606 /**
3607  * gtk_label_set_angle:
3608  * @label: a #GtkLabel
3609  * @angle: the angle that the baseline of the label makes with
3610  *   the horizontal, in degrees, measured counterclockwise
3611  * 
3612  * Sets the angle of rotation for the label. An angle of 90 reads from
3613  * from bottom to top, an angle of 270, from top to bottom. The angle
3614  * setting for the label is ignored if the label is selectable,
3615  * wrapped, or ellipsized.
3616  *
3617  * Since: 2.6
3618  **/
3619 void
3620 gtk_label_set_angle (GtkLabel *label,
3621                      gdouble   angle)
3622 {
3623   gdouble *label_angle;
3624
3625   g_return_if_fail (GTK_IS_LABEL (label));
3626
3627   label_angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
3628
3629   if (!label_angle)
3630     {
3631       label_angle = g_slice_new (gdouble);
3632       *label_angle = 0.0;
3633       g_object_set_qdata_full (G_OBJECT (label), quark_angle, 
3634                                label_angle, free_angle);
3635     }
3636   
3637   /* Canonicalize to [0,360]. We don't canonicalize 360 to 0, because
3638    * double property ranges are inclusive, and changing 360 to 0 would
3639    * make a property editor behave strangely.
3640    */
3641   if (angle < 0 || angle > 360.0)
3642     angle = angle - 360. * floor (angle / 360.);
3643
3644   if (*label_angle != angle)
3645     {
3646       *label_angle = angle;
3647       
3648       gtk_label_clear_layout (label);
3649       gtk_widget_queue_resize (GTK_WIDGET (label));
3650
3651       g_object_notify (G_OBJECT (label), "angle");
3652     }
3653 }
3654
3655 /**
3656  * gtk_label_get_angle:
3657  * @label: a #GtkLabel
3658  * 
3659  * Gets the angle of rotation for the label. See
3660  * gtk_label_set_angle().
3661  * 
3662  * Return value: the angle of rotation for the label
3663  *
3664  * Since: 2.6
3665  **/
3666 gdouble
3667 gtk_label_get_angle  (GtkLabel *label)
3668 {
3669   gdouble *angle;
3670
3671   g_return_val_if_fail (GTK_IS_LABEL (label), 0.0);
3672   
3673   angle = (gdouble *)g_object_get_qdata (G_OBJECT (label), quark_angle);
3674
3675   if (angle)
3676     return *angle;
3677   else
3678     return 0.0;
3679 }
3680
3681 static void
3682 gtk_label_set_selection_text (GtkLabel         *label,
3683                               GtkSelectionData *selection_data)
3684 {
3685   if ((label->select_info->selection_anchor !=
3686        label->select_info->selection_end) &&
3687       label->text)
3688     {
3689       gint start, end;
3690       gint len;
3691       
3692       start = MIN (label->select_info->selection_anchor,
3693                    label->select_info->selection_end);
3694       end = MAX (label->select_info->selection_anchor,
3695                  label->select_info->selection_end);
3696       
3697       len = strlen (label->text);
3698       
3699       if (end > len)
3700         end = len;
3701       
3702       if (start > len)
3703         start = len;
3704       
3705       gtk_selection_data_set_text (selection_data,
3706                                    label->text + start,
3707                                    end - start);
3708     }
3709 }
3710
3711 static void
3712 gtk_label_drag_data_get (GtkWidget        *widget,
3713                          GdkDragContext   *context,
3714                          GtkSelectionData *selection_data,
3715                          guint             info,
3716                          guint             time)
3717 {
3718   gtk_label_set_selection_text (GTK_LABEL (widget), selection_data);
3719 }
3720
3721 static void
3722 get_text_callback (GtkClipboard     *clipboard,
3723                    GtkSelectionData *selection_data,
3724                    guint             info,
3725                    gpointer          user_data_or_owner)
3726 {
3727   gtk_label_set_selection_text (GTK_LABEL (user_data_or_owner), selection_data);
3728 }
3729
3730 static void
3731 clear_text_callback (GtkClipboard     *clipboard,
3732                      gpointer          user_data_or_owner)
3733 {
3734   GtkLabel *label;
3735
3736   label = GTK_LABEL (user_data_or_owner);
3737
3738   if (label->select_info)
3739     {
3740       label->select_info->selection_anchor = label->select_info->selection_end;
3741       
3742       gtk_widget_queue_draw (GTK_WIDGET (label));
3743     }
3744 }
3745
3746 static void
3747 gtk_label_select_region_index (GtkLabel *label,
3748                                gint      anchor_index,
3749                                gint      end_index)
3750 {
3751   g_return_if_fail (GTK_IS_LABEL (label));
3752   
3753   if (label->select_info)
3754     {
3755       GtkClipboard *clipboard;
3756
3757       if (label->select_info->selection_anchor == anchor_index &&
3758           label->select_info->selection_end == end_index)
3759         return;
3760
3761       label->select_info->selection_anchor = anchor_index;
3762       label->select_info->selection_end = end_index;
3763
3764       clipboard = gtk_widget_get_clipboard (GTK_WIDGET (label),
3765                                             GDK_SELECTION_PRIMARY);      
3766       
3767       if (anchor_index != end_index)
3768         {
3769           GtkTargetList *list;
3770           GtkTargetEntry *targets;
3771           gint n_targets;
3772
3773           list = gtk_target_list_new (NULL, 0);
3774           gtk_target_list_add_text_targets (list, 0);
3775           targets = gtk_target_table_new_from_list (list, &n_targets);
3776
3777           gtk_clipboard_set_with_owner (clipboard,
3778                                         targets, n_targets,
3779                                         get_text_callback,
3780                                         clear_text_callback,
3781                                         G_OBJECT (label));
3782
3783           gtk_target_table_free (targets, n_targets);
3784           gtk_target_list_unref (list);
3785         }
3786       else
3787         {
3788           if (gtk_clipboard_get_owner (clipboard) == G_OBJECT (label))
3789             gtk_clipboard_clear (clipboard);
3790         }
3791
3792       gtk_widget_queue_draw (GTK_WIDGET (label));
3793
3794       g_object_freeze_notify (G_OBJECT (label));
3795       g_object_notify (G_OBJECT (label), "cursor-position");
3796       g_object_notify (G_OBJECT (label), "selection-bound");
3797       g_object_thaw_notify (G_OBJECT (label));
3798     }
3799 }
3800
3801 /**
3802  * gtk_label_select_region:
3803  * @label: a #GtkLabel
3804  * @start_offset: start offset (in characters not bytes)
3805  * @end_offset: end offset (in characters not bytes)
3806  *
3807  * Selects a range of characters in the label, if the label is selectable.
3808  * See gtk_label_set_selectable(). If the label is not selectable,
3809  * this function has no effect. If @start_offset or
3810  * @end_offset are -1, then the end of the label will be substituted.
3811  **/
3812 void
3813 gtk_label_select_region  (GtkLabel *label,
3814                           gint      start_offset,
3815                           gint      end_offset)
3816 {
3817   g_return_if_fail (GTK_IS_LABEL (label));
3818   
3819   if (label->text && label->select_info)
3820     {
3821       if (start_offset < 0)
3822         start_offset = g_utf8_strlen (label->text, -1);
3823       
3824       if (end_offset < 0)
3825         end_offset = g_utf8_strlen (label->text, -1);
3826       
3827       gtk_label_select_region_index (label,
3828                                      g_utf8_offset_to_pointer (label->text, start_offset) - label->text,
3829                                      g_utf8_offset_to_pointer (label->text, end_offset) - label->text);
3830     }
3831 }
3832
3833 /**
3834  * gtk_label_get_selection_bounds:
3835  * @label: a #GtkLabel
3836  * @start: return location for start of selection, as a character offset
3837  * @end: return location for end of selection, as a character offset
3838  * 
3839  * Gets the selected range of characters in the label, returning %TRUE
3840  * if there's a selection.
3841  * 
3842  * Return value: %TRUE if selection is non-empty
3843  **/
3844 gboolean
3845 gtk_label_get_selection_bounds (GtkLabel  *label,
3846                                 gint      *start,
3847                                 gint      *end)
3848 {
3849   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3850
3851   if (label->select_info == NULL)
3852     {
3853       /* not a selectable label */
3854       if (start)
3855         *start = 0;
3856       if (end)
3857         *end = 0;
3858
3859       return FALSE;
3860     }
3861   else
3862     {
3863       gint start_index, end_index;
3864       gint start_offset, end_offset;
3865       gint len;
3866       
3867       start_index = MIN (label->select_info->selection_anchor,
3868                    label->select_info->selection_end);
3869       end_index = MAX (label->select_info->selection_anchor,
3870                  label->select_info->selection_end);
3871
3872       len = strlen (label->text);
3873
3874       if (end_index > len)
3875         end_index = len;
3876
3877       if (start_index > len)
3878         start_index = len;
3879       
3880       start_offset = g_utf8_strlen (label->text, start_index);
3881       end_offset = g_utf8_strlen (label->text, end_index);
3882
3883       if (start_offset > end_offset)
3884         {
3885           gint tmp = start_offset;
3886           start_offset = end_offset;
3887           end_offset = tmp;
3888         }
3889       
3890       if (start)
3891         *start = start_offset;
3892
3893       if (end)
3894         *end = end_offset;
3895
3896       return start_offset != end_offset;
3897     }
3898 }
3899
3900
3901 /**
3902  * gtk_label_get_layout:
3903  * @label: a #GtkLabel
3904  * 
3905  * Gets the #PangoLayout used to display the label.
3906  * The layout is useful to e.g. convert text positions to
3907  * pixel positions, in combination with gtk_label_get_layout_offsets().
3908  * The returned layout is owned by the label so need not be
3909  * freed by the caller.
3910  * 
3911  * Return value: the #PangoLayout for this label
3912  **/
3913 PangoLayout*
3914 gtk_label_get_layout (GtkLabel *label)
3915 {
3916   g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
3917
3918   gtk_label_ensure_layout (label);
3919
3920   return label->layout;
3921 }
3922
3923 /**
3924  * gtk_label_get_layout_offsets:
3925  * @label: a #GtkLabel
3926  * @x: location to store X offset of layout, or %NULL
3927  * @y: location to store Y offset of layout, or %NULL
3928  *
3929  * Obtains the coordinates where the label will draw the #PangoLayout
3930  * representing the text in the label; useful to convert mouse events
3931  * into coordinates inside the #PangoLayout, e.g. to take some action
3932  * if some part of the label is clicked. Of course you will need to
3933  * create a #GtkEventBox to receive the events, and pack the label
3934  * inside it, since labels are a #GTK_NO_WINDOW widget. Remember
3935  * when using the #PangoLayout functions you need to convert to
3936  * and from pixels using PANGO_PIXELS() or #PANGO_SCALE.
3937  **/
3938 void
3939 gtk_label_get_layout_offsets (GtkLabel *label,
3940                               gint     *x,
3941                               gint     *y)
3942 {
3943   g_return_if_fail (GTK_IS_LABEL (label));
3944
3945   gtk_label_ensure_layout (label);
3946
3947   get_layout_location (label, x, y);
3948 }
3949
3950 /**
3951  * gtk_label_set_use_markup:
3952  * @label: a #GtkLabel
3953  * @setting: %TRUE if the label's text should be parsed for markup.
3954  *
3955  * Sets whether the text of the label contains markup in <link
3956  * linkend="PangoMarkupFormat">Pango's text markup
3957  * language</link>. See gtk_label_set_markup().
3958  **/
3959 void
3960 gtk_label_set_use_markup (GtkLabel *label,
3961                           gboolean  setting)
3962 {
3963   g_return_if_fail (GTK_IS_LABEL (label));
3964
3965   gtk_label_set_use_markup_internal (label, setting);
3966   gtk_label_recalculate (label);
3967 }
3968
3969 /**
3970  * gtk_label_get_use_markup:
3971  * @label: a #GtkLabel
3972  *
3973  * Returns whether the label's text is interpreted as marked up with
3974  * the <link linkend="PangoMarkupFormat">Pango text markup
3975  * language</link>. See gtk_label_set_use_markup ().
3976  *
3977  * Return value: %TRUE if the label's text will be parsed for markup.
3978  **/
3979 gboolean
3980 gtk_label_get_use_markup (GtkLabel *label)
3981 {
3982   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
3983   
3984   return label->use_markup;
3985 }
3986
3987 /**
3988  * gtk_label_set_use_underline:
3989  * @label: a #GtkLabel
3990  * @setting: %TRUE if underlines in the text indicate mnemonics
3991  *
3992  * If true, an underline in the text indicates the next character should be
3993  * used for the mnemonic accelerator key.
3994  */
3995 void
3996 gtk_label_set_use_underline (GtkLabel *label,
3997                              gboolean  setting)
3998 {
3999   g_return_if_fail (GTK_IS_LABEL (label));
4000
4001   gtk_label_set_use_underline_internal (label, setting);
4002   gtk_label_recalculate (label);
4003 }
4004
4005 /**
4006  * gtk_label_get_use_underline:
4007  * @label: a #GtkLabel
4008  *
4009  * Returns whether an embedded underline in the label indicates a
4010  * mnemonic. See gtk_label_set_use_underline().
4011  *
4012  * Return value: %TRUE whether an embedded underline in the label indicates
4013  *               the mnemonic accelerator keys.
4014  **/
4015 gboolean
4016 gtk_label_get_use_underline (GtkLabel *label)
4017 {
4018   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4019   
4020   return label->use_underline;
4021 }
4022
4023 /**
4024  * gtk_label_set_single_line_mode:
4025  * @label: a #GtkLabel
4026  * @single_line_mode: %TRUE if the label should be in single line mode
4027  *
4028  * Sets whether the label is in single line mode.
4029  *
4030  * Since: 2.6
4031  */
4032 void
4033 gtk_label_set_single_line_mode (GtkLabel *label,
4034                                 gboolean single_line_mode)
4035 {
4036   g_return_if_fail (GTK_IS_LABEL (label));
4037
4038   single_line_mode = single_line_mode != FALSE;
4039
4040   if (label->single_line_mode != single_line_mode)
4041     {
4042       label->single_line_mode = single_line_mode;
4043
4044       gtk_label_clear_layout (label);
4045       gtk_widget_queue_resize (GTK_WIDGET (label));
4046
4047       g_object_notify (G_OBJECT (label), "single-line-mode");
4048     }
4049 }
4050
4051 /**
4052  * gtk_label_get_single_line_mode:
4053  * @label: a #GtkLabel
4054  *
4055  * Returns whether the label is in single line mode.
4056  *
4057  * Return value: %TRUE when the label is in single line mode.
4058  *
4059  * Since: 2.6
4060  **/
4061 gboolean
4062 gtk_label_get_single_line_mode  (GtkLabel *label)
4063 {
4064   g_return_val_if_fail (GTK_IS_LABEL (label), FALSE);
4065
4066   return label->single_line_mode;
4067 }
4068
4069 /* Compute the X position for an offset that corresponds to the "more important
4070  * cursor position for that offset. We use this when trying to guess to which
4071  * end of the selection we should go to when the user hits the left or
4072  * right arrow key.
4073  */
4074 static void
4075 get_better_cursor (GtkLabel *label,
4076                    gint      index,
4077                    gint      *x,
4078                    gint      *y)
4079 {
4080   GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
4081   PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
4082   PangoDirection cursor_direction = get_cursor_direction (label);
4083   gboolean split_cursor;
4084   PangoRectangle strong_pos, weak_pos;
4085   
4086   g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
4087                 "gtk-split-cursor", &split_cursor,
4088                 NULL);
4089
4090   gtk_label_ensure_layout (label);
4091   
4092   pango_layout_get_cursor_pos (label->layout, index,
4093                                &strong_pos, &weak_pos);
4094
4095   if (split_cursor)
4096     {
4097       *x = strong_pos.x / PANGO_SCALE;
4098       *y = strong_pos.y / PANGO_SCALE;
4099     }
4100   else
4101     {
4102       if (keymap_direction == cursor_direction)
4103         {
4104           *x = strong_pos.x / PANGO_SCALE;
4105           *y = strong_pos.y / PANGO_SCALE;
4106         }
4107       else
4108         {
4109           *x = weak_pos.x / PANGO_SCALE;
4110           *y = weak_pos.y / PANGO_SCALE;
4111         }
4112     }
4113 }
4114
4115
4116 static gint
4117 gtk_label_move_logically (GtkLabel *label,
4118                           gint      start,
4119                           gint      count)
4120 {
4121   gint offset = g_utf8_pointer_to_offset (label->text,
4122                                           label->text + start);
4123
4124   if (label->text)
4125     {
4126       PangoLogAttr *log_attrs;
4127       gint n_attrs;
4128       gint length;
4129
4130       gtk_label_ensure_layout (label);
4131       
4132       length = g_utf8_strlen (label->text, -1);
4133
4134       pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
4135
4136       while (count > 0 && offset < length)
4137         {
4138           do
4139             offset++;
4140           while (offset < length && !log_attrs[offset].is_cursor_position);
4141           
4142           count--;
4143         }
4144       while (count < 0 && offset > 0)
4145         {
4146           do
4147             offset--;
4148           while (offset > 0 && !log_attrs[offset].is_cursor_position);
4149           
4150           count++;
4151         }
4152       
4153       g_free (log_attrs);
4154     }
4155
4156   return g_utf8_offset_to_pointer (label->text, offset) - label->text;
4157 }
4158
4159 static gint
4160 gtk_label_move_visually (GtkLabel *label,
4161                          gint      start,
4162                          gint      count)
4163 {
4164   gint index;
4165
4166   index = start;
4167   
4168   while (count != 0)
4169     {
4170       int new_index, new_trailing;
4171       gboolean split_cursor;
4172       gboolean strong;
4173
4174       gtk_label_ensure_layout (label);
4175
4176       g_object_get (gtk_widget_get_settings (GTK_WIDGET (label)),
4177                     "gtk-split-cursor", &split_cursor,
4178                     NULL);
4179
4180       if (split_cursor)
4181         strong = TRUE;
4182       else
4183         {
4184           GdkKeymap *keymap = gdk_keymap_get_for_display (gtk_widget_get_display (GTK_WIDGET (label)));
4185           PangoDirection keymap_direction = gdk_keymap_get_direction (keymap);
4186
4187           strong = keymap_direction == get_cursor_direction (label);
4188         }
4189       
4190       if (count > 0)
4191         {
4192           pango_layout_move_cursor_visually (label->layout, strong, index, 0, 1, &new_index, &new_trailing);
4193           count--;
4194         }
4195       else
4196         {
4197           pango_layout_move_cursor_visually (label->layout, strong, index, 0, -1, &new_index, &new_trailing);
4198           count++;
4199         }
4200
4201       if (new_index < 0 || new_index == G_MAXINT)
4202         break;
4203
4204       index = new_index;
4205       
4206       while (new_trailing--)
4207         index = g_utf8_next_char (label->text + new_index) - label->text;
4208     }
4209   
4210   return index;
4211 }
4212
4213 static gint
4214 gtk_label_move_forward_word (GtkLabel *label,
4215                              gint      start)
4216 {
4217   gint new_pos = g_utf8_pointer_to_offset (label->text,
4218                                            label->text + start);
4219   gint length;
4220
4221   length = g_utf8_strlen (label->text, -1);
4222   if (new_pos < length)
4223     {
4224       PangoLogAttr *log_attrs;
4225       gint n_attrs;
4226
4227       gtk_label_ensure_layout (label);
4228       
4229       pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
4230       
4231       /* Find the next word end */
4232       new_pos++;
4233       while (new_pos < n_attrs && !log_attrs[new_pos].is_word_end)
4234         new_pos++;
4235
4236       g_free (log_attrs);
4237     }
4238
4239   return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
4240 }
4241
4242
4243 static gint
4244 gtk_label_move_backward_word (GtkLabel *label,
4245                               gint      start)
4246 {
4247   gint new_pos = g_utf8_pointer_to_offset (label->text,
4248                                            label->text + start);
4249
4250   if (new_pos > 0)
4251     {
4252       PangoLogAttr *log_attrs;
4253       gint n_attrs;
4254
4255       gtk_label_ensure_layout (label);
4256       
4257       pango_layout_get_log_attrs (label->layout, &log_attrs, &n_attrs);
4258       
4259       new_pos -= 1;
4260
4261       /* Find the previous word beginning */
4262       while (new_pos > 0 && !log_attrs[new_pos].is_word_start)
4263         new_pos--;
4264
4265       g_free (log_attrs);
4266     }
4267
4268   return g_utf8_offset_to_pointer (label->text, new_pos) - label->text;
4269 }
4270
4271 static void
4272 gtk_label_move_cursor (GtkLabel       *label,
4273                        GtkMovementStep step,
4274                        gint            count,
4275                        gboolean        extend_selection)
4276 {
4277   gint old_pos;
4278   gint new_pos;
4279   
4280   if (label->select_info == NULL)
4281     return;
4282
4283   old_pos = new_pos = label->select_info->selection_end;
4284
4285   if (label->select_info->selection_end != label->select_info->selection_anchor &&
4286       !extend_selection)
4287     {
4288       /* If we have a current selection and aren't extending it, move to the
4289        * start/or end of the selection as appropriate
4290        */
4291       switch (step)
4292         {
4293         case GTK_MOVEMENT_VISUAL_POSITIONS:
4294           {
4295             gint end_x, end_y;
4296             gint anchor_x, anchor_y;
4297             gboolean end_is_left;
4298             
4299             get_better_cursor (label, label->select_info->selection_end, &end_x, &end_y);
4300             get_better_cursor (label, label->select_info->selection_anchor, &anchor_x, &anchor_y);
4301
4302             end_is_left = (end_y < anchor_y) || (end_y == anchor_y && end_x < anchor_x);
4303             
4304             if (count < 0)
4305               new_pos = end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
4306             else
4307               new_pos = !end_is_left ? label->select_info->selection_end : label->select_info->selection_anchor;
4308             break;
4309           }
4310         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4311         case GTK_MOVEMENT_WORDS:
4312           if (count < 0)
4313             new_pos = MIN (label->select_info->selection_end, label->select_info->selection_anchor);
4314           else
4315             new_pos = MAX (label->select_info->selection_end, label->select_info->selection_anchor);
4316           break;
4317         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4318         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4319         case GTK_MOVEMENT_BUFFER_ENDS:
4320           /* FIXME: Can do better here */
4321           new_pos = count < 0 ? 0 : strlen (label->text);
4322           break;
4323         case GTK_MOVEMENT_DISPLAY_LINES:
4324         case GTK_MOVEMENT_PARAGRAPHS:
4325         case GTK_MOVEMENT_PAGES:
4326         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4327           break;
4328         }
4329     }
4330   else
4331     {
4332       switch (step)
4333         {
4334         case GTK_MOVEMENT_LOGICAL_POSITIONS:
4335           new_pos = gtk_label_move_logically (label, new_pos, count);
4336           break;
4337         case GTK_MOVEMENT_VISUAL_POSITIONS:
4338           new_pos = gtk_label_move_visually (label, new_pos, count);
4339           if (new_pos == old_pos)
4340             {
4341               if (!extend_selection)
4342                 {
4343                   if (!gtk_widget_keynav_failed (GTK_WIDGET (label),
4344                                                  count > 0 ?
4345                                                  GTK_DIR_RIGHT : GTK_DIR_LEFT))
4346                     {
4347                       GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
4348
4349                       if (toplevel)
4350                         gtk_widget_child_focus (toplevel,
4351                                                 count > 0 ?
4352                                                 GTK_DIR_RIGHT : GTK_DIR_LEFT);
4353                     }
4354                 }
4355               else
4356                 {
4357                   gtk_widget_error_bell (GTK_WIDGET (label));
4358                 }
4359             }
4360           break;
4361         case GTK_MOVEMENT_WORDS:
4362           while (count > 0)
4363             {
4364               new_pos = gtk_label_move_forward_word (label, new_pos);
4365               count--;
4366             }
4367           while (count < 0)
4368             {
4369               new_pos = gtk_label_move_backward_word (label, new_pos);
4370               count++;
4371             }
4372           if (new_pos == old_pos)
4373             gtk_widget_error_bell (GTK_WIDGET (label));
4374           break;
4375         case GTK_MOVEMENT_DISPLAY_LINE_ENDS:
4376         case GTK_MOVEMENT_PARAGRAPH_ENDS:
4377         case GTK_MOVEMENT_BUFFER_ENDS:
4378           /* FIXME: Can do better here */
4379           new_pos = count < 0 ? 0 : strlen (label->text);
4380           if (new_pos == old_pos)
4381             gtk_widget_error_bell (GTK_WIDGET (label));
4382           break;
4383         case GTK_MOVEMENT_DISPLAY_LINES:
4384         case GTK_MOVEMENT_PARAGRAPHS:
4385         case GTK_MOVEMENT_PAGES:
4386         case GTK_MOVEMENT_HORIZONTAL_PAGES:
4387           break;
4388         }
4389     }
4390
4391   if (extend_selection)
4392     gtk_label_select_region_index (label,
4393                                    label->select_info->selection_anchor,
4394                                    new_pos);
4395   else
4396     gtk_label_select_region_index (label, new_pos, new_pos);
4397 }
4398
4399 static void
4400 gtk_label_copy_clipboard (GtkLabel *label)
4401 {
4402   if (label->text && label->select_info)
4403     {
4404       gint start, end;
4405       gint len;
4406       
4407       start = MIN (label->select_info->selection_anchor,
4408                    label->select_info->selection_end);
4409       end = MAX (label->select_info->selection_anchor,
4410                  label->select_info->selection_end);
4411
4412       len = strlen (label->text);
4413
4414       if (end > len)
4415         end = len;
4416
4417       if (start > len)
4418         start = len;
4419
4420       if (start != end)
4421         gtk_clipboard_set_text (gtk_widget_get_clipboard (GTK_WIDGET (label),
4422                                                           GDK_SELECTION_CLIPBOARD),
4423                                 label->text + start, end - start);
4424     }
4425 }
4426
4427 static void
4428 gtk_label_select_all (GtkLabel *label)
4429 {
4430   gtk_label_select_region_index (label, 0, strlen (label->text));
4431 }
4432
4433 /* Quick hack of a popup menu
4434  */
4435 static void
4436 activate_cb (GtkWidget *menuitem,
4437              GtkLabel  *label)
4438 {
4439   const gchar *signal = g_object_get_data (G_OBJECT (menuitem), "gtk-signal");
4440   g_signal_emit_by_name (label, signal);
4441 }
4442
4443 static void
4444 append_action_signal (GtkLabel     *label,
4445                       GtkWidget    *menu,
4446                       const gchar  *stock_id,
4447                       const gchar  *signal,
4448                       gboolean      sensitive)
4449 {
4450   GtkWidget *menuitem = gtk_image_menu_item_new_from_stock (stock_id, NULL);
4451
4452   g_object_set_data (G_OBJECT (menuitem), I_("gtk-signal"), (char *)signal);
4453   g_signal_connect (menuitem, "activate",
4454                     G_CALLBACK (activate_cb), label);
4455
4456   gtk_widget_set_sensitive (menuitem, sensitive);
4457   
4458   gtk_widget_show (menuitem);
4459   gtk_menu_shell_append (GTK_MENU_SHELL (menu), menuitem);
4460 }
4461
4462 static void
4463 popup_menu_detach (GtkWidget *attach_widget,
4464                    GtkMenu   *menu)
4465 {
4466   GtkLabel *label;
4467   label = GTK_LABEL (attach_widget);
4468
4469   if (label->select_info)
4470     label->select_info->popup_menu = NULL;
4471 }
4472
4473 static void
4474 popup_position_func (GtkMenu   *menu,
4475                      gint      *x,
4476                      gint      *y,
4477                      gboolean  *push_in,
4478                      gpointer   user_data)
4479 {
4480   GtkLabel *label;
4481   GtkWidget *widget;
4482   GtkRequisition req;
4483   GdkScreen *screen;
4484   
4485   label = GTK_LABEL (user_data);  
4486   widget = GTK_WIDGET (label);
4487
4488   if (label->select_info == NULL)
4489     return;
4490   
4491   g_return_if_fail (GTK_WIDGET_REALIZED (label));
4492   
4493   screen = gtk_widget_get_screen (widget);
4494   gdk_window_get_origin (widget->window, x, y);      
4495
4496   gtk_widget_size_request (label->select_info->popup_menu, &req);
4497   
4498   *x += widget->allocation.width / 2;
4499   *y += widget->allocation.height;
4500
4501   *x = CLAMP (*x, 0, MAX (0, gdk_screen_get_width (screen) - req.width));
4502   *y = CLAMP (*y, 0, MAX (0, gdk_screen_get_height (screen) - req.height));
4503 }
4504
4505
4506 static void
4507 gtk_label_do_popup (GtkLabel       *label,
4508                     GdkEventButton *event)
4509 {
4510   GtkWidget *menuitem;
4511   gboolean have_selection;
4512
4513   if (label->select_info == NULL)
4514     return;
4515     
4516   if (label->select_info->popup_menu)
4517     gtk_widget_destroy (label->select_info->popup_menu);
4518   
4519   label->select_info->popup_menu = gtk_menu_new ();
4520
4521   gtk_menu_attach_to_widget (GTK_MENU (label->select_info->popup_menu),
4522                              GTK_WIDGET (label),
4523                              popup_menu_detach);
4524
4525   have_selection =
4526     label->select_info->selection_anchor != label->select_info->selection_end;
4527
4528
4529   append_action_signal (label, label->select_info->popup_menu, GTK_STOCK_CUT, "cut_clipboard",
4530                         FALSE);
4531   append_action_signal (label, label->select_info->popup_menu, GTK_STOCK_COPY, "copy_clipboard",
4532                         have_selection);
4533   append_action_signal (label, label->select_info->popup_menu, GTK_STOCK_PASTE, "paste_clipboard",
4534                         FALSE);
4535   
4536   menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_DELETE, NULL);
4537   gtk_widget_set_sensitive (menuitem, FALSE);
4538   gtk_widget_show (menuitem);
4539   gtk_menu_shell_append (GTK_MENU_SHELL (label->select_info->popup_menu), menuitem);
4540
4541   menuitem = gtk_separator_menu_item_new ();
4542   gtk_widget_show (menuitem);
4543   gtk_menu_shell_append (GTK_MENU_SHELL (label->select_info->popup_menu), menuitem);
4544       
4545   menuitem = gtk_image_menu_item_new_from_stock (GTK_STOCK_SELECT_ALL, NULL);
4546   g_signal_connect_swapped (menuitem, "activate",
4547                             G_CALLBACK (gtk_label_select_all), label);
4548   gtk_widget_show (menuitem);
4549   gtk_menu_shell_append (GTK_MENU_SHELL (label->select_info->popup_menu), menuitem);
4550
4551   g_signal_emit (label,
4552                  signals[POPULATE_POPUP],
4553                  0,
4554                  label->select_info->popup_menu);
4555   
4556   if (event)
4557     gtk_menu_popup (GTK_MENU (label->select_info->popup_menu), NULL, NULL,
4558                     NULL, NULL,
4559                     event->button, event->time);
4560   else
4561     gtk_menu_popup (GTK_MENU (label->select_info->popup_menu), NULL, NULL,
4562                     popup_position_func, label,
4563                     0, gtk_get_current_event_time ());
4564 }
4565
4566 #define __GTK_LABEL_C__
4567 #include "gtkaliasdef.c"