gtklabel.c 126 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Owen Taylor's avatar
Owen Taylor committed
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Owen Taylor's avatar
Owen Taylor committed
15 16
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
18 19

/*
20
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
21 22
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
23
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/.
24 25
 */

26
#include "config.h"
27

28
#include <math.h>
Elliot Lee's avatar
Elliot Lee committed
29
#include <string.h>
30

Elliot Lee's avatar
Elliot Lee committed
31
#include "gtklabel.h"
32
#include "gtkaccellabel.h"
33
#include "gtkdnd.h"
34
#include "gtkmain.h"
35
#include "gtkmarshalers.h"
36
#include "gtkwindow.h"
37
#include "gdk/gdkkeysyms.h"
38
#include "gtkclipboard.h"
39
#include "gtkimagemenuitem.h"
40
#include "gtkintl.h"
41
#include "gtkseparatormenuitem.h"
42
#include "gtktextutil.h"
43 44
#include "gtkmenuitem.h"
#include "gtknotebook.h"
45
#include "gtkstock.h"
46
#include "gtkbindings.h"
47
#include "gtkbuildable.h"
48
#include "gtkprivate.h"
49
#include "gtkalias.h"
50

51 52 53 54
#define GTK_LABEL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_LABEL, GtkLabelPrivate))

typedef struct
{
55
  gint wrap_width;
56
  gint width_chars;
57
  gint max_width_chars;
58 59 60
}
GtkLabelPrivate;

61 62 63 64 65
struct _GtkLabelSelectionInfo
{
  GdkWindow *window;
  gint selection_anchor;
  gint selection_end;
66
  GtkWidget *popup_menu;
67 68 69 70 71
  
  gint drag_start_x;
  gint drag_start_y;

  guint in_drag : 1;
72
};
Elliot Lee's avatar
Elliot Lee committed
73

74 75 76 77 78 79
enum {
  MOVE_CURSOR,
  COPY_CLIPBOARD,
  POPULATE_POPUP,
  LAST_SIGNAL
};
80

81
enum {
82 83 84 85 86 87 88 89
  PROP_0,
  PROP_LABEL,
  PROP_ATTRIBUTES,
  PROP_USE_MARKUP,
  PROP_USE_UNDERLINE,
  PROP_JUSTIFY,
  PROP_PATTERN,
  PROP_WRAP,
90
  PROP_WRAP_MODE,
91
  PROP_SELECTABLE,
92
  PROP_MNEMONIC_KEYVAL,
93 94
  PROP_MNEMONIC_WIDGET,
  PROP_CURSOR_POSITION,
95
  PROP_SELECTION_BOUND,
96
  PROP_ELLIPSIZE,
97
  PROP_WIDTH_CHARS,
98
  PROP_SINGLE_LINE_MODE,
99 100
  PROP_ANGLE,
  PROP_MAX_WIDTH_CHARS
101 102
};

103 104
static guint signals[LAST_SIGNAL] = { 0 };

105 106 107
static void gtk_label_set_property      (GObject          *object,
					 guint             prop_id,
					 const GValue     *value,
108
					 GParamSpec       *pspec);
109 110 111
static void gtk_label_get_property      (GObject          *object,
					 guint             prop_id,
					 GValue           *value,
112
					 GParamSpec       *pspec);
113
static void gtk_label_destroy           (GtkObject        *object);
114 115 116
static void gtk_label_finalize          (GObject          *object);
static void gtk_label_size_request      (GtkWidget        *widget,
					 GtkRequisition   *requisition);
117 118
static void gtk_label_size_allocate     (GtkWidget        *widget,
                                         GtkAllocation    *allocation);
Havoc Pennington's avatar
Havoc Pennington committed
119 120
static void gtk_label_state_changed     (GtkWidget        *widget,
                                         GtkStateType      state);
121 122 123 124 125 126
static void gtk_label_style_set         (GtkWidget        *widget,
					 GtkStyle         *previous_style);
static void gtk_label_direction_changed (GtkWidget        *widget,
					 GtkTextDirection  previous_dir);
static gint gtk_label_expose            (GtkWidget        *widget,
					 GdkEventExpose   *event);
Elliot Lee's avatar
Elliot Lee committed
127

128 129 130 131
static void gtk_label_realize           (GtkWidget        *widget);
static void gtk_label_unrealize         (GtkWidget        *widget);
static void gtk_label_map               (GtkWidget        *widget);
static void gtk_label_unmap             (GtkWidget        *widget);
Federico Mena Quintero's avatar
Federico Mena Quintero committed
132 133 134 135 136 137 138

static gboolean gtk_label_button_press      (GtkWidget        *widget,
					     GdkEventButton   *event);
static gboolean gtk_label_button_release    (GtkWidget        *widget,
					     GdkEventButton   *event);
static gboolean gtk_label_motion            (GtkWidget        *widget,
					     GdkEventMotion   *event);
139
static void     gtk_label_grab_focus        (GtkWidget        *widget);
140

141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159

static void gtk_label_set_text_internal          (GtkLabel      *label,
						  gchar         *str);
static void gtk_label_set_label_internal         (GtkLabel      *label,
						  gchar         *str);
static void gtk_label_set_use_markup_internal    (GtkLabel      *label,
						  gboolean       val);
static void gtk_label_set_use_underline_internal (GtkLabel      *label,
						  gboolean       val);
static void gtk_label_set_attributes_internal    (GtkLabel      *label,
						  PangoAttrList *attrs);
static void gtk_label_set_uline_text_internal    (GtkLabel      *label,
						  const gchar   *str);
static void gtk_label_set_pattern_internal       (GtkLabel      *label,
				                  const gchar   *pattern);
static void set_markup                           (GtkLabel      *label,
						  const gchar   *str,
						  gboolean       with_uline);
static void gtk_label_recalculate                (GtkLabel      *label);
160 161
static void gtk_label_hierarchy_changed          (GtkWidget     *widget,
						  GtkWidget     *old_toplevel);
162 163
static void gtk_label_screen_changed             (GtkWidget     *widget,
						  GdkScreen     *old_screen);
164

165 166 167
static void gtk_label_create_window       (GtkLabel *label);
static void gtk_label_destroy_window      (GtkLabel *label);
static void gtk_label_clear_layout        (GtkLabel *label);
168
static void gtk_label_ensure_layout       (GtkLabel *label);
169
static void gtk_label_invalidate_wrap_width (GtkLabel *label);
170 171 172 173
static void gtk_label_select_region_index (GtkLabel *label,
                                           gint      anchor_index,
                                           gint      end_index);

174 175 176 177
static gboolean gtk_label_mnemonic_activate (GtkWidget         *widget,
					     gboolean           group_cycling);
static void     gtk_label_setup_mnemonic    (GtkLabel          *label,
					     guint              last_key);
178 179 180 181 182 183
static void     gtk_label_drag_data_get     (GtkWidget         *widget,
					     GdkDragContext    *context,
					     GtkSelectionData  *selection_data,
					     guint              info,
					     guint              time);

184 185 186 187 188 189 190 191 192 193 194 195 196 197
static void     gtk_label_buildable_interface_init     (GtkBuildableIface *iface);
static gboolean gtk_label_buildable_custom_tag_start   (GtkBuildable     *buildable,
							GtkBuilder       *builder,
							GObject          *child,
							const gchar      *tagname,
							GMarkupParser    *parser,
							gpointer         *data);

static void     gtk_label_buildable_custom_finished    (GtkBuildable     *buildable,
							GtkBuilder       *builder,
							GObject          *child,
							const gchar      *tagname,
							gpointer          user_data);

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

/* For selectable lables: */
static void gtk_label_move_cursor        (GtkLabel        *label,
					  GtkMovementStep  step,
					  gint             count,
					  gboolean         extend_selection);
static void gtk_label_copy_clipboard     (GtkLabel        *label);
static void gtk_label_select_all         (GtkLabel        *label);
static void gtk_label_do_popup           (GtkLabel        *label,
					  GdkEventButton  *event);

static gint gtk_label_move_forward_word  (GtkLabel        *label,
					  gint             start);
static gint gtk_label_move_backward_word (GtkLabel        *label,
					  gint             start);
213

214
static GQuark quark_angle = 0;
Elliot Lee's avatar
Elliot Lee committed
215

216 217 218 219 220
static GtkBuildableIface *buildable_parent_iface = NULL;

G_DEFINE_TYPE_WITH_CODE (GtkLabel, gtk_label, GTK_TYPE_MISC,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
						gtk_label_buildable_interface_init));
Elliot Lee's avatar
Elliot Lee committed
221

222 223 224 225 226 227 228 229 230 231
static void
add_move_binding (GtkBindingSet  *binding_set,
		  guint           keyval,
		  guint           modmask,
		  GtkMovementStep step,
		  gint            count)
{
  g_return_if_fail ((modmask & GDK_SHIFT_MASK) == 0);
  
  gtk_binding_entry_add_signal (binding_set, keyval, modmask,
232
				"move-cursor", 3,
Manish Singh's avatar
Manish Singh committed
233
				G_TYPE_ENUM, step,
234
				G_TYPE_INT, count,
Manish Singh's avatar
Manish Singh committed
235
				G_TYPE_BOOLEAN, FALSE);
236 237 238

  /* Selection-extending version */
  gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
239
				"move-cursor", 3,
Manish Singh's avatar
Manish Singh committed
240
				G_TYPE_ENUM, step,
241
				G_TYPE_INT, count,
Manish Singh's avatar
Manish Singh committed
242
				G_TYPE_BOOLEAN, TRUE);
243 244
}

245
static void
Elliot Lee's avatar
Elliot Lee committed
246 247
gtk_label_class_init (GtkLabelClass *class)
{
248
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
249 250
  GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
251
  GtkBindingSet *binding_set;
252

253 254
  quark_angle = g_quark_from_static_string ("angle");

255 256
  gobject_class->set_property = gtk_label_set_property;
  gobject_class->get_property = gtk_label_get_property;
257
  gobject_class->finalize = gtk_label_finalize;
258 259

  object_class->destroy = gtk_label_destroy;
Tim Janik's avatar
Tim Janik committed
260
  
Elliot Lee's avatar
Elliot Lee committed
261
  widget_class->size_request = gtk_label_size_request;
262
  widget_class->size_allocate = gtk_label_size_allocate;
Havoc Pennington's avatar
Havoc Pennington committed
263
  widget_class->state_changed = gtk_label_state_changed;
264
  widget_class->style_set = gtk_label_style_set;
265
  widget_class->direction_changed = gtk_label_direction_changed;
Elliot Lee's avatar
Elliot Lee committed
266
  widget_class->expose_event = gtk_label_expose;
267 268 269 270 271 272 273
  widget_class->realize = gtk_label_realize;
  widget_class->unrealize = gtk_label_unrealize;
  widget_class->map = gtk_label_map;
  widget_class->unmap = gtk_label_unmap;
  widget_class->button_press_event = gtk_label_button_press;
  widget_class->button_release_event = gtk_label_button_release;
  widget_class->motion_notify_event = gtk_label_motion;
274
  widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
275
  widget_class->screen_changed = gtk_label_screen_changed;
276
  widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
277
  widget_class->drag_data_get = gtk_label_drag_data_get;
278
  widget_class->grab_focus = gtk_label_grab_focus;
279 280 281 282 283

  class->move_cursor = gtk_label_move_cursor;
  class->copy_clipboard = gtk_label_copy_clipboard;
  
  signals[MOVE_CURSOR] = 
284
    g_signal_new (I_("move-cursor"),
Manish Singh's avatar
Manish Singh committed
285 286 287 288 289 290 291 292 293
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkLabelClass, move_cursor),
		  NULL, NULL,
		  _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
		  G_TYPE_NONE, 3,
		  GTK_TYPE_MOVEMENT_STEP,
		  G_TYPE_INT,
		  G_TYPE_BOOLEAN);
294 295
  
  signals[COPY_CLIPBOARD] =
296
    g_signal_new (I_("copy-clipboard"),
Manish Singh's avatar
Manish Singh committed
297 298 299 300 301 302
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GtkLabelClass, copy_clipboard),
		  NULL, NULL,
		  _gtk_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);
303 304
  
  signals[POPULATE_POPUP] =
305
    g_signal_new (I_("populate-popup"),
Manish Singh's avatar
Manish Singh committed
306 307 308 309 310 311 312 313 314
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkLabelClass, populate_popup),
		  NULL, NULL,
		  _gtk_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1,
		  GTK_TYPE_MENU);

  g_object_class_install_property (gobject_class,
315 316
                                   PROP_LABEL,
                                   g_param_spec_string ("label",
317 318
                                                        P_("Label"),
                                                        P_("The text of the label"),
319
                                                        "",
320
                                                        GTK_PARAM_READWRITE));
321 322 323
  g_object_class_install_property (gobject_class,
				   PROP_ATTRIBUTES,
				   g_param_spec_boxed ("attributes",
324 325
						       P_("Attributes"),
						       P_("A list of style attributes to apply to the text of the label"),
326
						       PANGO_TYPE_ATTR_LIST,
327
						       GTK_PARAM_READWRITE));
328 329
  g_object_class_install_property (gobject_class,
                                   PROP_USE_MARKUP,
Matthias Clasen's avatar
x  
Matthias Clasen committed
330
                                   g_param_spec_boolean ("use-markup",
331 332
							 P_("Use markup"),
							 P_("The text of the label includes XML markup. See pango_parse_markup()"),
333
                                                        FALSE,
334
                                                        GTK_PARAM_READWRITE));
335 336
  g_object_class_install_property (gobject_class,
                                   PROP_USE_UNDERLINE,
Matthias Clasen's avatar
x  
Matthias Clasen committed
337
                                   g_param_spec_boolean ("use-underline",
338 339
							 P_("Use underline"),
							 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
340
                                                        FALSE,
341
                                                        GTK_PARAM_READWRITE));
342 343 344 345

  g_object_class_install_property (gobject_class,
				   PROP_JUSTIFY,
                                   g_param_spec_enum ("justify",
346 347
                                                      P_("Justification"),
                                                      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"),
348 349
						      GTK_TYPE_JUSTIFICATION,
						      GTK_JUSTIFY_LEFT,
350
                                                      GTK_PARAM_READWRITE));
351 352 353 354

  g_object_class_install_property (gobject_class,
                                   PROP_PATTERN,
                                   g_param_spec_string ("pattern",
355 356
                                                        P_("Pattern"),
                                                        P_("A string with _ characters in positions correspond to characters in the text to underline"),
357
                                                        NULL,
358
                                                        GTK_PARAM_WRITABLE));
359 360 361 362

  g_object_class_install_property (gobject_class,
                                   PROP_WRAP,
                                   g_param_spec_boolean ("wrap",
363 364
                                                        P_("Line wrap"),
                                                        P_("If set, wrap lines if the text becomes too wide"),
365
                                                        FALSE,
366
                                                        GTK_PARAM_READWRITE));
367 368 369
  /**
   * GtkLabel:wrap-mode:
   *
370 371 372
   * If line wrapping is on (see the #GtkLabel:wrap property) this controls 
   * how the line wrapping is done. The default is %PANGO_WRAP_WORD, which 
   * means wrap on word boundaries.
373 374 375 376 377 378 379 380 381 382 383
   *
   * Since: 2.10
   */
  g_object_class_install_property (gobject_class,
                                   PROP_WRAP_MODE,
                                   g_param_spec_enum ("wrap-mode",
						      P_("Line wrap mode"),
						      P_("If wrap is set, controls how linewrapping is done"),
						      PANGO_TYPE_WRAP_MODE,
						      PANGO_WRAP_WORD,
						      GTK_PARAM_READWRITE));
384 385 386
  g_object_class_install_property (gobject_class,
                                   PROP_SELECTABLE,
                                   g_param_spec_boolean ("selectable",
387 388
                                                        P_("Selectable"),
                                                        P_("Whether the label text can be selected with the mouse"),
389
                                                        FALSE,
390
                                                        GTK_PARAM_READWRITE));
391
  g_object_class_install_property (gobject_class,
392
                                   PROP_MNEMONIC_KEYVAL,
Matthias Clasen's avatar
x  
Matthias Clasen committed
393
                                   g_param_spec_uint ("mnemonic-keyval",
394 395
						      P_("Mnemonic key"),
						      P_("The mnemonic accelerator key for this label"),
396 397 398
						      0,
						      G_MAXUINT,
						      GDK_VoidSymbol,
399
						      GTK_PARAM_READABLE));
400 401
  g_object_class_install_property (gobject_class,
                                   PROP_MNEMONIC_WIDGET,
Matthias Clasen's avatar
x  
Matthias Clasen committed
402
                                   g_param_spec_object ("mnemonic-widget",
403 404
							P_("Mnemonic widget"),
							P_("The widget to be activated when the label's mnemonic "
Soren Sandmann's avatar
Soren Sandmann committed
405
							  "key is pressed"),
406
							GTK_TYPE_WIDGET,
407
							GTK_PARAM_READWRITE));
408

409 410
  g_object_class_install_property (gobject_class,
                                   PROP_CURSOR_POSITION,
Matthias Clasen's avatar
x  
Matthias Clasen committed
411
                                   g_param_spec_int ("cursor-position",
412 413
                                                     P_("Cursor Position"),
                                                     P_("The current position of the insertion cursor in chars"),
414 415 416
                                                     0,
                                                     G_MAXINT,
                                                     0,
417
                                                     GTK_PARAM_READABLE));
418 419 420
  
  g_object_class_install_property (gobject_class,
                                   PROP_SELECTION_BOUND,
Matthias Clasen's avatar
x  
Matthias Clasen committed
421
                                   g_param_spec_int ("selection-bound",
422 423
                                                     P_("Selection Bound"),
                                                     P_("The position of the opposite end of the selection from the cursor in chars"),
424 425 426
                                                     0,
                                                     G_MAXINT,
                                                     0,
427
                                                     GTK_PARAM_READABLE));
428
  
429 430 431
  /**
   * GtkLabel:ellipsize:
   *
432 433 434
   * The preferred place to ellipsize the string, if the label does 
   * not have enough room to display the entire string, specified as a 
   * #PangoEllisizeMode. 
435
   *
436 437 438 439 440 441 442
   * Note that setting this property to a value other than 
   * %PANGO_ELLIPSIZE_NONE has the side-effect that the label requests 
   * only enough space to display the ellipsis "...". In particular, this 
   * means that ellipsizing labels do not work well in notebook tabs, unless 
   * the tab's #GtkNotebook:tab-expand property is set to %TRUE. Other ways
   * to set a label's width are gtk_widget_set_size_request() and
   * gtk_label_set_width_chars().
443 444 445
   *
   * Since: 2.6
   */
446 447 448 449
  g_object_class_install_property (gobject_class,
				   PROP_ELLIPSIZE,
                                   g_param_spec_enum ("ellipsize",
                                                      P_("Ellipsize"),
450
                                                      P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string"),
451 452
						      PANGO_TYPE_ELLIPSIZE_MODE,
						      PANGO_ELLIPSIZE_NONE,
453
                                                      GTK_PARAM_READWRITE));
454 455 456 457 458

  /**
   * GtkLabel:width-chars:
   * 
   * The desired width of the label, in characters. If this property is set to
459
   * -1, the width will be calculated automatically, otherwise the label will
460
   * request either 3 characters or the property value, whichever is greater.
461 462
   * If the "width-chars" property is set to a positive value, then the 
   * #GtkLabel:max-width-chars property is ignored. 
463
   *
464 465 466 467
   * Since: 2.6
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_WIDTH_CHARS,
Matthias Clasen's avatar
x  
Matthias Clasen committed
468
                                   g_param_spec_int ("width-chars",
Matthias Clasen's avatar
Matthias Clasen committed
469
                                                     P_("Width In Characters"),
470 471 472 473
                                                     P_("The desired width of the label, in characters"),
                                                     -1,
                                                     G_MAXINT,
                                                     -1,
474
                                                     GTK_PARAM_READWRITE));
475
  
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
  /**
   * GtkLabel:single-line-mode:
   * 
   * Whether the label is in single line mode. In single line mode,
   * the height of the label does not depend on the actual text, it
   * is always set to ascent + descent of the font. This can be an
   * advantage in situations where resizing the label because of text 
   * changes would be distracting, e.g. in a statusbar.
   *
   * Since: 2.6
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_SINGLE_LINE_MODE,
                                   g_param_spec_boolean ("single-line-mode",
                                                        P_("Single Line Mode"),
                                                        P_("Whether the label is in single line mode"),
                                                        FALSE,
493
                                                        GTK_PARAM_READWRITE));
494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

  /**
   * GtkLabel:angle:
   * 
   * The angle that the baseline of the label makes with the horizontal,
   * in degrees, measured counterclockwise. An angle of 90 reads from
   * from bottom to top, an angle of 270, from top to bottom. Ignored
   * if the label is selectable, wrapped, or ellipsized.
   *
   * Since: 2.6
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_ANGLE,
                                   g_param_spec_double ("angle",
							P_("Angle"),
							P_("Angle at which the label is rotated"),
							0.0,
							360.0,
							0.0, 
513
							GTK_PARAM_READWRITE));
514
  
515 516 517
  /**
   * GtkLabel:max-width-chars:
   * 
518
   * The desired maximum width of the label, in characters. If this property 
519 520
   * is set to -1, the width will be calculated automatically, otherwise the 
   * label will request space for no more than the requested number of 
521 522
   * characters. If the #GtkLabel:width-chars property is set to a positive 
   * value, then the "max-width-chars" property is ignored.
523 524 525 526 527
   * 
   * Since: 2.6
   **/
  g_object_class_install_property (gobject_class,
                                   PROP_MAX_WIDTH_CHARS,
Matthias Clasen's avatar
x  
Matthias Clasen committed
528
                                   g_param_spec_int ("max-width-chars",
529 530
                                                     P_("Maximum Width In Characters"),
                                                     P_("The desired maximum width of the label, in characters"),
531 532 533
                                                     -1,
                                                     G_MAXINT,
                                                     -1,
534
                                                     GTK_PARAM_READWRITE));
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571
  /*
   * Key bindings
   */

  binding_set = gtk_binding_set_by_class (class);

  /* Moving the insertion point */
  add_move_binding (binding_set, GDK_Right, 0,
		    GTK_MOVEMENT_VISUAL_POSITIONS, 1);
  
  add_move_binding (binding_set, GDK_Left, 0,
		    GTK_MOVEMENT_VISUAL_POSITIONS, -1);

  add_move_binding (binding_set, GDK_KP_Right, 0,
		    GTK_MOVEMENT_VISUAL_POSITIONS, 1);
  
  add_move_binding (binding_set, GDK_KP_Left, 0,
		    GTK_MOVEMENT_VISUAL_POSITIONS, -1);
  
  add_move_binding (binding_set, GDK_f, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_LOGICAL_POSITIONS, 1);
  
  add_move_binding (binding_set, GDK_b, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_LOGICAL_POSITIONS, -1);
  
  add_move_binding (binding_set, GDK_Right, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_WORDS, 1);

  add_move_binding (binding_set, GDK_Left, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_WORDS, -1);

  add_move_binding (binding_set, GDK_KP_Right, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_WORDS, 1);

  add_move_binding (binding_set, GDK_KP_Left, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_WORDS, -1);

572 573
  /* select all */
  gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
574
				"move-cursor", 3,
575 576 577 578 579
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, -1,
				G_TYPE_BOOLEAN, FALSE);

  gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
580
				"move-cursor", 3,
581 582 583 584 585
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, 1,
				G_TYPE_BOOLEAN, TRUE);

  gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
586
				"move-cursor", 3,
587 588 589 590 591
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, -1,
				G_TYPE_BOOLEAN, FALSE);

  gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
592
				"move-cursor", 3,
593 594 595 596 597 598
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, 1,
				G_TYPE_BOOLEAN, TRUE);

  /* unselect all */
  gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
599
				"move-cursor", 3,
600 601 602 603 604
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, 0,
				G_TYPE_BOOLEAN, FALSE);

  gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
605
				"move-cursor", 3,
606 607 608
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, 0,
				G_TYPE_BOOLEAN, FALSE);
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641

  add_move_binding (binding_set, GDK_f, GDK_MOD1_MASK,
		    GTK_MOVEMENT_WORDS, 1);

  add_move_binding (binding_set, GDK_b, GDK_MOD1_MASK,
		    GTK_MOVEMENT_WORDS, -1);

  add_move_binding (binding_set, GDK_Home, 0,
		    GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);

  add_move_binding (binding_set, GDK_End, 0,
		    GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);

  add_move_binding (binding_set, GDK_KP_Home, 0,
		    GTK_MOVEMENT_DISPLAY_LINE_ENDS, -1);

  add_move_binding (binding_set, GDK_KP_End, 0,
		    GTK_MOVEMENT_DISPLAY_LINE_ENDS, 1);
  
  add_move_binding (binding_set, GDK_Home, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_BUFFER_ENDS, -1);

  add_move_binding (binding_set, GDK_End, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_BUFFER_ENDS, 1);

  add_move_binding (binding_set, GDK_KP_Home, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_BUFFER_ENDS, -1);

  add_move_binding (binding_set, GDK_KP_End, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_BUFFER_ENDS, 1);

  /* copy */
  gtk_binding_entry_add_signal (binding_set, GDK_c, GDK_CONTROL_MASK,
642
				"copy-clipboard", 0);
643 644 645 646 647 648 649

  gtk_settings_install_property (g_param_spec_boolean ("gtk-label-select-on-focus",
						       P_("Select on focus"),
						       P_("Whether to select the contents of a selectable label when it is focused"),
						       TRUE,
						       GTK_PARAM_READWRITE));

650 651
				
  g_type_class_add_private (class, sizeof (GtkLabelPrivate));
Elliot Lee's avatar
Elliot Lee committed
652 653
}

654 655 656 657
static void 
gtk_label_set_property (GObject      *object,
			guint         prop_id,
			const GValue *value,
658
			GParamSpec   *pspec)
659
{
660
  GtkLabel *label;
661

662
  label = GTK_LABEL (object);
663
  
664
  switch (prop_id)
665
    {
666
    case PROP_LABEL:
667
      gtk_label_set_label (label, g_value_get_string (value));
668 669 670 671 672
      break;
    case PROP_ATTRIBUTES:
      gtk_label_set_attributes (label, g_value_get_boxed (value));
      break;
    case PROP_USE_MARKUP:
673
      gtk_label_set_use_markup (label, g_value_get_boolean (value));
674
      break;
675
    case PROP_USE_UNDERLINE:
676
      gtk_label_set_use_underline (label, g_value_get_boolean (value));
677
      break;
678 679
    case PROP_JUSTIFY:
      gtk_label_set_justify (label, g_value_get_enum (value));
680
      break;
681 682 683 684 685 686
    case PROP_PATTERN:
      gtk_label_set_pattern (label, g_value_get_string (value));
      break;
    case PROP_WRAP:
      gtk_label_set_line_wrap (label, g_value_get_boolean (value));
      break;	  
687 688 689
    case PROP_WRAP_MODE:
      gtk_label_set_line_wrap_mode (label, g_value_get_enum (value));
      break;	  
690 691
    case PROP_SELECTABLE:
      gtk_label_set_selectable (label, g_value_get_boolean (value));
692
      break;	  
693 694 695
    case PROP_MNEMONIC_WIDGET:
      gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
      break;
696 697 698
    case PROP_ELLIPSIZE:
      gtk_label_set_ellipsize (label, g_value_get_enum (value));
      break;
699 700 701
    case PROP_WIDTH_CHARS:
      gtk_label_set_width_chars (label, g_value_get_int (value));
      break;
702 703 704
    case PROP_SINGLE_LINE_MODE:
      gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
      break;	  
705 706
    case PROP_ANGLE:
      gtk_label_set_angle (label, g_value_get_double (value));
707 708 709 710
      break;
    case PROP_MAX_WIDTH_CHARS:
      gtk_label_set_max_width_chars (label, g_value_get_int (value));
      break;
Tim Janik's avatar
Tim Janik committed
711
    default:
712
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
713
      break;
714 715 716
    }
}

717 718 719 720
static void 
gtk_label_get_property (GObject     *object,
			guint        prop_id,
			GValue      *value,
721
			GParamSpec  *pspec)
722
{
723
  GtkLabel *label;
724
  
725
  label = GTK_LABEL (object);
726
  
727
  switch (prop_id)
728
    {
729
    case PROP_LABEL:
Alexander Larsson's avatar
Alexander Larsson committed
730
      g_value_set_string (value, label->label);
731 732 733 734 735 736 737 738 739
      break;
    case PROP_ATTRIBUTES:
      g_value_set_boxed (value, label->attrs);
      break;
    case PROP_USE_MARKUP:
      g_value_set_boolean (value, label->use_markup);
      break;
    case PROP_USE_UNDERLINE:
      g_value_set_boolean (value, label->use_underline);
740
      break;
741 742
    case PROP_JUSTIFY:
      g_value_set_enum (value, label->jtype);
743
      break;
744 745
    case PROP_WRAP:
      g_value_set_boolean (value, label->wrap);
746
      break;
747 748 749
    case PROP_WRAP_MODE:
      g_value_set_enum (value, label->wrap_mode);
      break;
750 751 752
    case PROP_SELECTABLE:
      g_value_set_boolean (value, gtk_label_get_selectable (label));
      break;
753 754
    case PROP_MNEMONIC_KEYVAL:
      g_value_set_uint (value, label->mnemonic_keyval);
755
      break;
756 757 758
    case PROP_MNEMONIC_WIDGET:
      g_value_set_object (value, (GObject*) label->mnemonic_widget);
      break;
759 760 761
    case PROP_CURSOR_POSITION:
      if (label->select_info)
	{
762 763
	  gint offset = g_utf8_pointer_to_offset (label->text,
						  label->text + label->select_info->selection_end);
764 765 766 767 768 769 770 771
	  g_value_set_int (value, offset);
	}
      else
	g_value_set_int (value, 0);
      break;
    case PROP_SELECTION_BOUND:
      if (label->select_info)
	{
772 773
	  gint offset = g_utf8_pointer_to_offset (label->text,
						  label->text + label->select_info->selection_anchor);
774 775 776 777 778
	  g_value_set_int (value, offset);
	}
      else
	g_value_set_int (value, 0);
      break;
779 780 781
    case PROP_ELLIPSIZE:
      g_value_set_enum (value, label->ellipsize);
      break;
782
    case PROP_WIDTH_CHARS:
783
      g_value_set_int (value, gtk_label_get_width_chars (label));
784
      break;
785 786 787
    case PROP_SINGLE_LINE_MODE:
      g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
      break;
788 789 790
    case PROP_ANGLE:
      g_value_set_double (value, gtk_label_get_angle (label));
      break;
791 792 793
    case PROP_MAX_WIDTH_CHARS:
      g_value_set_int (value, gtk_label_get_max_width_chars (label));
      break;
794

795
    default:
796
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
797 798 799 800
      break;
    }
}

801
static void
Elliot Lee's avatar
Elliot Lee committed
802 803
gtk_label_init (GtkLabel *label)
{
804 805
  GtkLabelPrivate *priv;

Elliot Lee's avatar
Elliot Lee committed
806
  GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
807 808 809

  priv = GTK_LABEL_GET_PRIVATE (label);
  priv->width_chars = -1;
810
  priv->max_width_chars = -1;
811
  priv->wrap_width = -1;
Elliot Lee's avatar
Elliot Lee committed
812
  label->label = NULL;
813

814
  label->jtype = GTK_JUSTIFY_LEFT;
Owen Taylor's avatar
Owen Taylor committed
815
  label->wrap = FALSE;
816
  label->wrap_mode = PANGO_WRAP_WORD;
817
  label->ellipsize = PANGO_ELLIPSIZE_NONE;
818 819 820

  label->use_underline = FALSE;
  label->use_markup = FALSE;
821
  label->pattern_set = FALSE;
822
  
823
  label->mnemonic_keyval = GDK_VoidSymbol;
824
  label->layout = NULL;
825
  label->text = NULL;
826
  label->attrs = NULL;
827 828 829

  label->mnemonic_widget = NULL;
  label->mnemonic_window = NULL;
830
  
Owen Taylor's avatar
Owen Taylor committed
831
  gtk_label_set_text (label, "");
Elliot Lee's avatar
Elliot Lee committed
832 833
}

834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849

static void
gtk_label_buildable_interface_init (GtkBuildableIface *iface)
{
  buildable_parent_iface = g_type_interface_peek_parent (iface);

  iface->custom_tag_start = gtk_label_buildable_custom_tag_start;
  iface->custom_finished = gtk_label_buildable_custom_finished;
}

typedef struct {
  GtkBuilder    *builder;
  GObject       *object;
  PangoAttrList *attrs;
} PangoParserData;

Matthias Clasen's avatar
Matthias Clasen committed
850
static PangoAttribute *
851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
attribute_from_text (GtkBuilder   *builder,
		     const gchar  *name, 
		     const gchar  *value,
		     GError      **error)
{
  PangoAttribute *attribute = NULL;
  PangoAttrType   type;
  PangoLanguage  *language;
  PangoFontDescription *font_desc;
  GdkColor       *color;
  GValue          val = { 0, };

  if (!gtk_builder_value_from_string_type (builder, PANGO_TYPE_ATTR_TYPE, name, &val, error))
    return NULL;

  type = g_value_get_enum (&val);
  g_value_unset (&val);

  switch (type)
    {
      /* PangoAttrLanguage */
    case PANGO_ATTR_LANGUAGE:
      if ((language = pango_language_from_string (value)))
	{
	  attribute = pango_attr_language_new (language);
	  g_value_init (&val, G_TYPE_INT);
	}
      break;
      /* PangoAttrInt */
    case PANGO_ATTR_STYLE:
      if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STYLE, value, &val, error))
	attribute = pango_attr_style_new (g_value_get_enum (&val));
      break;
    case PANGO_ATTR_WEIGHT:
      if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_WEIGHT, value, &val, error))
	attribute = pango_attr_weight_new (g_value_get_enum (&val));
      break;
    case PANGO_ATTR_VARIANT:
      if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_VARIANT, value, &val, error))
	attribute = pango_attr_variant_new (g_value_get_enum (&val));
      break;
    case PANGO_ATTR_STRETCH:
      if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_STRETCH, value, &val, error))
	attribute = pango_attr_stretch_new (g_value_get_enum (&val));
      break;
    case PANGO_ATTR_UNDERLINE:
      if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
	attribute = pango_attr_underline_new (g_value_get_boolean (&val));
      break;
    case PANGO_ATTR_STRIKETHROUGH:	
      if (gtk_builder_value_from_string_type (builder, G_TYPE_BOOLEAN, value, &val, error))
	attribute = pango_attr_strikethrough_new (g_value_get_boolean (&val));
      break;
    case PANGO_ATTR_GRAVITY:
      if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY, value, &val, error))
	attribute = pango_attr_gravity_new (g_value_get_enum (&val));
      break;
    case PANGO_ATTR_GRAVITY_HINT:
      if (gtk_builder_value_from_string_type (builder, PANGO_TYPE_GRAVITY_HINT, 
					      value, &val, error))
	attribute = pango_attr_gravity_hint_new (g_value_get_enum (&val));
      break;

      /* PangoAttrString */	  
    case PANGO_ATTR_FAMILY:
      attribute = pango_attr_family_new (value);
      g_value_init (&val, G_TYPE_INT);
      break;

      /* PangoAttrSize */	  
    case PANGO_ATTR_SIZE:
      if (gtk_builder_value_from_string_type (builder, G_TYPE_INT, 
					      value, &val, error))
	attribute = pango_attr_size_new (g_value_get_int (&val));
      break;
    case PANGO_ATTR_ABSOLUTE_SIZE:
      if (gtk_builder_value_from_string_type (builder, G_TYPE_INT, 
					      value, &val, error))
	attribute = pango_attr_size_new_absolute (g_value_get_int (&val));
      break;
    
      /* PangoAttrFontDesc */
    case PANGO_ATTR_FONT_DESC:
      if ((font_desc = pango_font_description_from_string (value)))
	{
	  attribute = pango_attr_font_desc_new (font_desc);
	  pango_font_description_free (font_desc);
	  g_value_init (&val, G_TYPE_INT);
	}
      break;

      /* PangoAttrColor */
    case PANGO_ATTR_FOREGROUND:
      if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR, 
					      value, &val, error))
	{
	  color = g_value_get_boxed (&val);
	  attribute = pango_attr_foreground_new (color->red, color->green, color->blue);
	}
      break;
    case PANGO_ATTR_BACKGROUND: 
      if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR, 
					      value, &val, error))
	{
	  color = g_value_get_boxed (&val);
	  attribute = pango_attr_background_new (color->red, color->green, color->blue);
	}
      break;
    case PANGO_ATTR_UNDERLINE_COLOR:
      if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR, 
					      value, &val, error))
	{
	  color = g_value_get_boxed (&val);
	  attribute = pango_attr_underline_color_new (color->red, color->green, color->blue);
	}
      break;
    case PANGO_ATTR_STRIKETHROUGH_COLOR:
      if (gtk_builder_value_from_string_type (builder, GDK_TYPE_COLOR, 
					      value, &val, error))
	{
	  color = g_value_get_boxed (&val);
	  attribute = pango_attr_strikethrough_color_new (color->red, color->green, color->blue);
	}
      break;
      
      /* PangoAttrShape */
    case PANGO_ATTR_SHAPE:
      /* Unsupported for now */
      break;
      /* PangoAttrFloat */
    case PANGO_ATTR_SCALE:
      if (gtk_builder_value_from_string_type (builder, G_TYPE_DOUBLE, 
					      value, &val, error))
	attribute = pango_attr_scale_new (g_value_get_double (&val));
      break;

    case PANGO_ATTR_INVALID:
    case PANGO_ATTR_LETTER_SPACING:
    case PANGO_ATTR_RISE:
    case PANGO_ATTR_FALLBACK:
    default:
      break;
    }

  g_value_unset (&val);

  return attribute;
}


static void
pango_start_element (GMarkupParseContext *context,
		     const gchar         *element_name,
		     const gchar        **names,
		     const gchar        **values,
		     gpointer             user_data,
		     GError             **error)
{
  PangoParserData *data = (PangoParserData*)user_data;
  GValue val = { 0, };
  guint i;
  gint line_number, char_number;

  if (strcmp (element_name, "attribute") == 0)
    {
      PangoAttribute *attr = NULL;
      const gchar *name = NULL;
      const gchar *value = NULL;
      const gchar *start = NULL;
      const gchar *end = NULL;
      guint start_val = 0;
      guint end_val   = G_MAXUINT;

      for (i = 0; names[i]; i++)
	{
	  if (strcmp (names[i], "name") == 0)
	    name = values[i];
	  else if (strcmp (names[i], "value") == 0)
	    value = values[i];
	  else if (strcmp (names[i], "start") == 0)
	    start = values[i];
	  else if (strcmp (names[i], "end") == 0)
	    end = values[i];
	  else
	    {
	      g_markup_parse_context_get_position (context,
						   &line_number,
						   &char_number);
	      g_set_error (error,
			   GTK_BUILDER_ERROR,
			   GTK_BUILDER_ERROR_INVALID_ATTRIBUTE,
			   "%s:%d:%d '%s' is not a valid attribute of <%s>",
			   "<input>",
			   line_number, char_number, names[i], "attribute");
	      return;
	    }
	}

      if (!name || !value)
	{
	  g_markup_parse_context_get_position (context,
					       &line_number,
					       &char_number);
	  g_set_error (error,
		       GTK_BUILDER_ERROR,
		       GTK_BUILDER_ERROR_MISSING_ATTRIBUTE,
		       "%s:%d:%d <%s> requires attribute \"%s\"",
		       "<input>",
		       line_number, char_number, "attribute",
		       name ? "value" : "name");
	  return;
	}

      if (start)
	{
	  if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT, 
						   start, &val, error))
	    return;
	  start_val = g_value_get_uint (&val);
	  g_value_unset (&val);
	}

      if (end)
	{
	  if (!gtk_builder_value_from_string_type (data->builder, G_TYPE_UINT, 
						   end, &val, error))
	    return;
	  end_val = g_value_get_uint (&val);
	  g_value_unset (&val);
	}

      attr = attribute_from_text (data->builder, name, value, error);
      attr->start_index = start_val;
      attr->end_index   = end_val;

      if (attr)
	{
	  if (!data->attrs)
	    data->attrs = pango_attr_list_new ();

	  pango_attr_list_insert (data->attrs, attr);
	}
    }
  else if (strcmp (element_name, "attributes") == 0)
    ;
  else
    g_warning ("Unsupported tag for GtkLabel: %s\n", element_name);
}

static const GMarkupParser pango_parser =
  {
    pango_start_element,
  };

static gboolean
gtk_label_buildable_custom_tag_start (GtkBuildable     *buildable,
				      GtkBuilder       *builder,
				      GObject          *child,
				      const gchar      *tagname,
				      GMarkupParser    *parser,
				      gpointer         *data)
{
  if (buildable_parent_iface->custom_tag_start (buildable, builder, child, 
						tagname, parser, data))
    return TRUE;

  if (strcmp (tagname, "attributes") == 0)
    {
      PangoParserData *parser_data;

      parser_data = g_slice_new0 (PangoParserData);
      parser_data->builder = g_object_ref (builder);
      parser_data->object = g_object_ref (buildable);
      *parser = pango_parser;
      *data = parser_data;
      return TRUE;
    }
  return FALSE;
}

static void
gtk_label_buildable_custom_finished (GtkBuildable *buildable,
				     GtkBuilder   *builder,
				     GObject      *child,
				     const gchar  *tagname,
				     gpointer      user_data)
{
  PangoParserData *data;

  buildable_parent_iface->custom_finished (buildable, builder, child, 
					   tagname, user_data);

  if (strcmp (tagname, "attributes") == 0)
    {
      data = (PangoParserData*)user_data;

      if (data->attrs)
	{
	  gtk_label_set_attributes (GTK_LABEL (buildable), data->attrs);
	  pango_attr_list_unref (data->attrs);
	}

      g_object_unref (data->object);
      g_object_unref (data->builder);
      g_slice_free (PangoParserData, data);
    }
}


1160 1161 1162 1163
/**
 * gtk_label_new:
 * @str: The text of the label
 *
Matthias Clasen's avatar
Matthias Clasen committed
1164 1165
 * Creates a new label with the given text inside it. You can
 * pass %NULL to get an empty label widget.
1166 1167
 *
 * Return value: the new #GtkLabel
1168
 **/
Elliot Lee's avatar
Elliot Lee committed
1169
GtkWidget*
1170
gtk_label_new (const gchar *str)
Elliot Lee's avatar
Elliot Lee committed
1171 1172
{
  GtkLabel *label;
1173
  
Manish Singh's avatar
Manish Singh committed
1174
  label = g_object_new (GTK_TYPE_LABEL, NULL);
1175 1176 1177

  if (str && *str)
    gtk_label_set_text (label, str);
1178
  
Elliot Lee's avatar
Elliot Lee committed
1179 1180 1181
  return GTK_WIDGET (label);
}

1182
/**
1183 1184 1185 1186 1187 1188
 * gtk_label_new_with_mnemonic:
 * @str: The text of the label, with an underscore in front of the
 *       mnemonic character
 *
 * Creates a new #GtkLabel, containing the text in @str.
 *
1189
 * If characters in @str are preceded by an underscore, they are
1190 1191 1192 1193
 * underlined. If you need a literal underscore character in a label, use
 * '__' (two underscores). The first underlined character represents a 
 * keyboard accelerator called a mnemonic. The mnemonic key can be used 
 * to activate another widget, chosen automatically, or explicitly using
1194 1195
 * gtk_label_set_mnemonic_widget().
 * 
1196 1197 1198 1199 1200
 * If gtk_label_set_mnemonic_widget() is not called, then the first 
 * activatable ancestor of the #GtkLabel will be chosen as the mnemonic 
 * widget. For instance, if the label is inside a button or menu item, 
 * the button or menu item will automatically become the mnemonic widget 
 * and be activated by the mnemonic.
1201 1202
 *
 * Return value: the new #GtkLabel
1203 1204 1205 1206 1207 1208
 **/
GtkWidget*
gtk_label_new_with_mnemonic (const gchar *str)
{
  GtkLabel *label;
  
Manish Singh's avatar
Manish Singh committed
1209
  label = g_object_new (GTK_TYPE_LABEL, NULL);
1210 1211 1212 1213 1214 1215 1216 1217

  if (str && *str)
    gtk_label_set_text_with_mnemonic (label, str);
  
  return GTK_WIDGET (label);
}

static gboolean
1218
gtk_label_mnemonic_activate (GtkWidget *widget,
1219 1220 1221 1222 1223
			     gboolean   group_cycling)
{
  GtkWidget *parent;

  if (GTK_LABEL (widget)->mnemonic_widget)
1224
    return gtk_widget_mnemonic_activate (GTK_LABEL (widget)->mnemonic_widget, group_cycling);
1225

1226 1227
  /* Try to find the widget to activate by traversing the
   * widget's ancestry.
1228 1229
   */
  parent = widget->parent;
1230

1231
  if (GTK_IS_NOTEBOOK (parent))
1232
    return FALSE;
1233
  
1234 1235 1236 1237
  while (parent)
    {
      if (GTK_WIDGET_CAN_FOCUS (parent) ||
	  (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
1238 1239
          GTK_IS_NOTEBOOK (parent->parent) ||
	  GTK_IS_MENU_ITEM (parent))
1240
	return gtk_widget_mnemonic_activate (parent, group_cycling);
1241 1242 1243
      parent = parent->parent;
    }

1244
  /* barf if there was nothing to activate */
1245
  g_warning ("Couldn't find a target for a mnemonic activation.");
1246 1247
  gtk_widget_error_bell (widget);

1248 1249 1250 1251
  return FALSE;
}

static void
1252 1253
gtk_label_setup_mnemonic (GtkLabel *label,
			  guint     last_key)
1254
{
1255
  GtkWidget *widget = GTK_WIDGET (label);
1256
  GtkWidget *toplevel;
1257 1258 1259 1260 1261
  GtkWidget *mnemonic_menu;
  
  mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
  
  if (last_key != GDK_VoidSymbol)
1262
    {
1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274
      if (label->mnemonic_window)
	{
	  gtk_window_remove_mnemonic  (label->mnemonic_window,
				       last_key,
				       widget);
	  label->mnemonic_window = NULL;
	}
      if (mnemonic_menu)
	{
	  _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
					   last_key,
					   widget);
1275
	  mnemonic_menu = NULL;
1276
	}
1277
    }
1278 1279
  
  if (label->mnemonic_keyval == GDK_VoidSymbol)
1280
    goto done;
1281 1282

  toplevel = gtk_widget_get_toplevel (widget);
1283
  if (GTK_WIDGET_TOPLEVEL (toplevel))
1284
    {
1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
      GtkWidget *menu_shell;
      
      menu_shell = gtk_widget_get_ancestor (widget,
					    GTK_TYPE_MENU_SHELL);

      if (menu_shell)
	{
	  _gtk_menu_shell_add_mnemonic (GTK_MENU_SHELL (menu_shell),
					label->mnemonic_keyval,
					widget);
	  mnemonic_menu = menu_shell;
	}
      
1298
      if (!GTK_IS_MENU (menu_shell))
1299 1300 1301 1302 1303 1304
	{
	  gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
				   label->mnemonic_keyval,
				   widget);
	  label->mnemonic_window = GTK_WINDOW (toplevel);
	}
1305
    }
1306
  
1307
 done:
Matthias Clasen's avatar
Matthias Clasen committed
1308
  g_object_set_data (G_OBJECT (label), I_("gtk-mnemonic-menu"), mnemonic_menu);
1309 1310 1311
}

static void
1312 1313
gtk_label_hierarchy_changed (GtkWidget *widget,
			     GtkWidget *old_toplevel)
1314 1315
{
  GtkLabel *label = GTK_LABEL (widget);
1316

1317 1318 1319
  gtk_label_setup_mnemonic (label, label->mnemonic_keyval);
}

1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
static void
label_shortcut_setting_apply (GtkLabel *label)
{
  gtk_label_recalculate (label);
  if (GTK_IS_ACCEL_LABEL (label))
    gtk_accel_label_refetch (GTK_ACCEL_LABEL (label));
}

static void
label_shortcut_setting_traverse_container (GtkWidget *widget,
                                           gpointer   data)
{
  if (GTK_IS_LABEL (widget))
    label_shortcut_setting_apply (GTK_LABEL (widget));
  else if (GTK_IS_CONTAINER (widget))
    gtk_container_forall (GTK_CONTAINER (widget),
                          label_shortcut_setting_traverse_container, data);
}

static void
label_shortcut_setting_changed (GtkSettings *settings)
{
  GList *list, *l;

  list = gtk_window_list_toplevels ();

  for (l = list; l ; l = l->next)
    {
      GtkWidget *widget = l->data;

      if (gtk_widget_get_settings (widget) == settings)
        gtk_container_forall (GTK_CONTAINER (widget),
                              label_shortcut_setting_traverse_container, NULL);
    }

  g_list_free (list);
}

1358 1359 1360 1361
static void
gtk_label_screen_changed (GtkWidget *widget,
			  GdkScreen *old_screen)
{