gtklabel.c 56.3 KB
Newer Older
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 23 24 25
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

26
#include <math.h>
Elliot Lee's avatar
Elliot Lee committed
27 28
#include <string.h>
#include "gtklabel.h"
29 30
#include "gtksignal.h"
#include "gtkwindow.h"
31
#include "gdk/gdkkeysyms.h"
32
#include "gtkclipboard.h"
Owen Taylor's avatar
Owen Taylor committed
33
#include "gdk/gdki18n.h"
34
#include <pango/pango.h>
35
#include "gtkintl.h"
36 37
#include "gtkmenuitem.h"
#include "gtknotebook.h"
38

39 40 41 42 43 44
struct _GtkLabelSelectionInfo
{
  GdkWindow *window;
  gint selection_anchor;
  gint selection_end;
};
Elliot Lee's avatar
Elliot Lee committed
45

46

47
enum {
48 49 50 51 52 53 54 55 56
  PROP_0,
  PROP_LABEL,
  PROP_ATTRIBUTES,
  PROP_USE_MARKUP,
  PROP_USE_UNDERLINE,
  PROP_JUSTIFY,
  PROP_PATTERN,
  PROP_WRAP,
  PROP_SELECTABLE,
57 58
  PROP_MNEMONIC_KEYVAL,
  PROP_MNEMONIC_WIDGET
59 60
};

61 62
static void gtk_label_class_init        (GtkLabelClass    *klass);
static void gtk_label_init              (GtkLabel         *label);
63 64 65
static void gtk_label_set_property      (GObject          *object,
					 guint             prop_id,
					 const GValue     *value,
66
					 GParamSpec       *pspec);
67 68 69
static void gtk_label_get_property      (GObject          *object,
					 guint             prop_id,
					 GValue           *value,
70
					 GParamSpec       *pspec);
71
static void gtk_label_destroy           (GtkObject        *object);
72 73 74
static void gtk_label_finalize          (GObject          *object);
static void gtk_label_size_request      (GtkWidget        *widget,
					 GtkRequisition   *requisition);
75 76
static void gtk_label_size_allocate     (GtkWidget        *widget,
                                         GtkAllocation    *allocation);
Havoc Pennington's avatar
Havoc Pennington committed
77 78
static void gtk_label_state_changed     (GtkWidget        *widget,
                                         GtkStateType      state);
79 80 81 82 83 84
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
85

86 87 88 89 90 91 92 93 94 95 96
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);
static gint gtk_label_button_press      (GtkWidget        *widget,
                                         GdkEventButton   *event);
static gint gtk_label_button_release    (GtkWidget        *widget,
                                         GdkEventButton   *event);
static gint gtk_label_motion            (GtkWidget        *widget,
                                         GdkEventMotion   *event);

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115

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);
116
static void gtk_label_hierarchy_changed          (GtkWidget     *widget);
117

118 119 120 121 122 123 124 125 126 127
static void gtk_label_create_window       (GtkLabel *label);
static void gtk_label_destroy_window      (GtkLabel *label);
static void gtk_label_clear_layout        (GtkLabel *label);
static void gtk_label_ensure_layout       (GtkLabel *label,
                                           gint     *widthp,
                                           gint     *heightp);
static void gtk_label_select_region_index (GtkLabel *label,
                                           gint      anchor_index,
                                           gint      end_index);

128
static gboolean gtk_label_mnemonic_activate (GtkWidget *widget,
129
					     gboolean   group_cycling);
130 131
static void     gtk_label_setup_mnemonic    (GtkLabel  *label,
					     guint      last_key);
132

133

134 135
static GtkMiscClass *parent_class = NULL;

Elliot Lee's avatar
Elliot Lee committed
136

Tim Janik's avatar
Tim Janik committed
137
GtkType
138
gtk_label_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
139
{
Tim Janik's avatar
Tim Janik committed
140 141
  static GtkType label_type = 0;
  
Elliot Lee's avatar
Elliot Lee committed
142 143
  if (!label_type)
    {
144
      static const GTypeInfo label_info =
Elliot Lee's avatar
Elliot Lee committed
145 146
      {
	sizeof (GtkLabelClass),
147 148 149 150 151 152 153 154
	NULL,           /* base_init */
	NULL,           /* base_finalize */
	(GClassInitFunc) gtk_label_class_init,
	NULL,           /* class_finalize */
	NULL,           /* class_data */
	sizeof (GtkLabel),
	32,             /* n_preallocs */
	(GInstanceInitFunc) gtk_label_init,
Elliot Lee's avatar
Elliot Lee committed
155
      };
156

157
      label_type = g_type_register_static (GTK_TYPE_MISC, "GtkLabel", &label_info, 0);
Elliot Lee's avatar
Elliot Lee committed
158
    }
Tim Janik's avatar
Tim Janik committed
159
  
Elliot Lee's avatar
Elliot Lee committed
160 161 162
  return label_type;
}

163
static void
Elliot Lee's avatar
Elliot Lee committed
164 165
gtk_label_class_init (GtkLabelClass *class)
{
166
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
167 168 169
  GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);

170
  parent_class = gtk_type_class (GTK_TYPE_MISC);
Tim Janik's avatar
Tim Janik committed
171
  
172 173
  gobject_class->set_property = gtk_label_set_property;
  gobject_class->get_property = gtk_label_get_property;
174
  gobject_class->finalize = gtk_label_finalize;
175 176

  object_class->destroy = gtk_label_destroy;
Tim Janik's avatar
Tim Janik committed
177
  
Elliot Lee's avatar
Elliot Lee committed
178
  widget_class->size_request = gtk_label_size_request;
179
  widget_class->size_allocate = gtk_label_size_allocate;
Havoc Pennington's avatar
Havoc Pennington committed
180
  widget_class->state_changed = gtk_label_state_changed;
181
  widget_class->style_set = gtk_label_style_set;
182
  widget_class->direction_changed = gtk_label_direction_changed;
Elliot Lee's avatar
Elliot Lee committed
183
  widget_class->expose_event = gtk_label_expose;
184 185 186 187 188 189 190
  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;
191
  widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
192
  widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218

  g_object_class_install_property (G_OBJECT_CLASS(object_class),
                                   PROP_LABEL,
                                   g_param_spec_string ("label",
                                                        _("Label"),
                                                        _("The text of the label."),
                                                        NULL,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
				   PROP_ATTRIBUTES,
				   g_param_spec_boxed ("attributes",
						       _("Attributes"),
						       _("A list of style attributes to apply to the text of the label."),
						       PANGO_TYPE_ATTR_LIST,
						       G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
                                   PROP_USE_MARKUP,
                                   g_param_spec_boolean ("use_markup",
							 _("Use markup"),
							 _("The text of the label includes XML markup. See pango_parse_markup()."),
                                                        FALSE,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
                                   PROP_USE_UNDERLINE,
                                   g_param_spec_boolean ("use_underline",
							 _("Use underline"),
219
							 _("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
                                                        FALSE,
                                                        G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class,
				   PROP_JUSTIFY,
                                   g_param_spec_enum ("justify",
                                                      _("Justification"),
                                                      _("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."),
						      GTK_TYPE_JUSTIFICATION,
						      GTK_JUSTIFY_LEFT,
                                                      G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class,
                                   PROP_PATTERN,
                                   g_param_spec_string ("pattern",
                                                        _("Pattern"),
Hans Breuer's avatar
updated  
Hans Breuer committed
236
                                                        _("A string with _ characters in positions correspond to characters in the text to underline."),
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
                                                        NULL,
                                                        G_PARAM_WRITABLE));

  g_object_class_install_property (gobject_class,
                                   PROP_WRAP,
                                   g_param_spec_boolean ("wrap",
                                                        _("Line wrap"),
                                                        _("If set, wrap lines if the text becomes too wide."),
                                                        TRUE,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
                                   PROP_SELECTABLE,
                                   g_param_spec_boolean ("selectable",
                                                        _("Selectable"),
                                                        _("Whether the label text can be selected with the mouse."),
                                                        FALSE,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
255 256
                                   PROP_MNEMONIC_KEYVAL,
                                   g_param_spec_uint ("mnemonic_keyval",
257
						      _("Mnemonic key"),
258
						      _("The mnemonic accelerator key for this label."),
259 260 261 262
						      0,
						      G_MAXUINT,
						      GDK_VoidSymbol,
						      G_PARAM_READABLE));
263 264 265 266 267 268 269 270
  g_object_class_install_property (gobject_class,
                                   PROP_MNEMONIC_WIDGET,
                                   g_param_spec_object ("mnemonic_widget",
							_("Mnemonic widget"),
							_("The widget to be activated when the label's mnemonic "
							  "key is pressed."),
							GTK_TYPE_WIDGET,
							G_PARAM_READWRITE));
Elliot Lee's avatar
Elliot Lee committed
271 272
}

273 274 275 276
static void 
gtk_label_set_property (GObject      *object,
			guint         prop_id,
			const GValue *value,
277
			GParamSpec   *pspec)
278
{
279
  GtkLabel *label;
280 281
  guint last_keyval;

282
  label = GTK_LABEL (object);
283
  last_keyval = label->mnemonic_keyval;
284
  
285
  switch (prop_id)
286
    {
287 288 289 290
    case PROP_LABEL:
      gtk_label_set_label_internal (label,
				    g_strdup (g_value_get_string (value)));
      gtk_label_recalculate (label);
291 292
      if (last_keyval != label->mnemonic_keyval)
	gtk_label_setup_mnemonic (label, last_keyval);
293 294 295 296 297 298 299
      break;
    case PROP_ATTRIBUTES:
      gtk_label_set_attributes (label, g_value_get_boxed (value));
      break;
    case PROP_USE_MARKUP:
      gtk_label_set_use_markup_internal (label, g_value_get_boolean (value));
      gtk_label_recalculate (label);
300
      break;
301 302 303
    case PROP_USE_UNDERLINE:
      gtk_label_set_use_underline_internal (label, g_value_get_boolean (value));
      gtk_label_recalculate (label);
304 305
      if (label->use_underline)
	gtk_label_setup_mnemonic (label, last_keyval);
306
      break;
307 308
    case PROP_JUSTIFY:
      gtk_label_set_justify (label, g_value_get_enum (value));
309
      break;
310 311 312 313 314 315 316 317
    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;	  
    case PROP_SELECTABLE:
      gtk_label_set_selectable (label, g_value_get_boolean (value));
318
      break;	  
319 320 321
    case PROP_MNEMONIC_WIDGET:
      gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
      break;
Tim Janik's avatar
Tim Janik committed
322
    default:
323
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
324
      break;
325 326 327
    }
}

328 329 330 331
static void 
gtk_label_get_property (GObject     *object,
			guint        prop_id,
			GValue      *value,
332
			GParamSpec  *pspec)
333
{
334
  GtkLabel *label;
335
  
336
  label = GTK_LABEL (object);
337
  
338
  switch (prop_id)
339
    {
340
    case PROP_LABEL:
Alexander Larsson's avatar
Alexander Larsson committed
341
      g_value_set_string (value, label->label);
342 343 344 345 346 347 348 349 350
      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);
351
      break;
352 353
    case PROP_JUSTIFY:
      g_value_set_enum (value, label->jtype);
354
      break;
355 356
    case PROP_WRAP:
      g_value_set_boolean (value, label->wrap);
357
      break;
358 359 360
    case PROP_SELECTABLE:
      g_value_set_boolean (value, gtk_label_get_selectable (label));
      break;
361 362
    case PROP_MNEMONIC_KEYVAL:
      g_value_set_uint (value, label->mnemonic_keyval);
363
      break;
364 365 366
    case PROP_MNEMONIC_WIDGET:
      g_value_set_object (value, (GObject*) label->mnemonic_widget);
      break;
367

368
    default:
369
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
370 371 372 373
      break;
    }
}

374
static void
Elliot Lee's avatar
Elliot Lee committed
375 376 377
gtk_label_init (GtkLabel *label)
{
  GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
Tim Janik's avatar
Tim Janik committed
378
  
Elliot Lee's avatar
Elliot Lee committed
379
  label->label = NULL;
380

Elliot Lee's avatar
Elliot Lee committed
381
  label->jtype = GTK_JUSTIFY_CENTER;
Owen Taylor's avatar
Owen Taylor committed
382
  label->wrap = FALSE;
383 384 385

  label->use_underline = FALSE;
  label->use_markup = FALSE;
386
  
387
  label->mnemonic_keyval = GDK_VoidSymbol;
388
  label->layout = NULL;
389
  label->text = NULL;
390
  label->attrs = NULL;
391 392 393

  label->mnemonic_widget = NULL;
  label->mnemonic_window = NULL;
394
  
Owen Taylor's avatar
Owen Taylor committed
395
  gtk_label_set_text (label, "");
Elliot Lee's avatar
Elliot Lee committed
396 397
}

398 399 400 401 402 403 404
/**
 * gtk_label_new:
 * @str: The text of the label
 * @returns: a new #GtkLabel
 *
 * Creates a new #GtkLabel, containing the text in @str.
 **/
Elliot Lee's avatar
Elliot Lee committed
405
GtkWidget*
406
gtk_label_new (const gchar *str)
Elliot Lee's avatar
Elliot Lee committed
407 408
{
  GtkLabel *label;
409
  
410 411 412 413
  label = gtk_type_new (GTK_TYPE_LABEL);

  if (str && *str)
    gtk_label_set_text (label, str);
414
  
Elliot Lee's avatar
Elliot Lee committed
415 416 417
  return GTK_WIDGET (label);
}

418
/**
419 420 421 422 423 424 425
 * gtk_label_new_with_mnemonic:
 * @str: The text of the label, with an underscore in front of the
 *       mnemonic character
 * @returns: a new #GtkLabel
 *
 * Creates a new #GtkLabel, containing the text in @str.
 *
426 427 428 429 430 431 432 433 434 435 436 437
 * If characters in @str are preceded by an underscore, they are
 * underlined indicating that they represent a keyboard accelerator
 * called a mnemonic.  The mnemonic key can be used to activate
 * another widget, chosen automatically, or explicitly using
 * gtk_label_set_mnemonic_widget().
 * 
 * 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.
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
 **/
GtkWidget*
gtk_label_new_with_mnemonic (const gchar *str)
{
  GtkLabel *label;
  
  label = gtk_type_new (GTK_TYPE_LABEL);

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

static gboolean
453
gtk_label_mnemonic_activate (GtkWidget *widget,
454 455 456 457 458
			     gboolean   group_cycling)
{
  GtkWidget *parent;

  if (GTK_LABEL (widget)->mnemonic_widget)
459
    return gtk_widget_mnemonic_activate (GTK_LABEL (widget)->mnemonic_widget, group_cycling);
460

461 462
  /* Try to find the widget to activate by traversing the
   * widget's ancestry.
463 464 465 466 467 468
   */
  parent = widget->parent;
  while (parent)
    {
      if (GTK_WIDGET_CAN_FOCUS (parent) ||
	  (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
469
          (parent->parent && GTK_IS_NOTEBOOK (parent->parent)) ||
470
	  (GTK_IS_MENU_ITEM (parent)))
471
	return gtk_widget_mnemonic_activate (parent, group_cycling);
472 473 474
      parent = parent->parent;
    }

475
  /* barf if there was nothing to activate */
476 477 478 479 480 481 482
  g_warning ("Couldn't find a target for a mnemonic activation.");
  gdk_beep ();
  
  return FALSE;
}

static void
483 484
gtk_label_setup_mnemonic (GtkLabel *label,
			  guint     last_key)
485 486 487
{
  GtkWidget *toplevel;

488
  if (last_key != GDK_VoidSymbol && label->mnemonic_window)
489 490 491 492 493 494
    {
      gtk_window_remove_mnemonic  (label->mnemonic_window,
				   last_key,
				   GTK_WIDGET (label));
      label->mnemonic_window = NULL;
    }
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
  
  if (label->mnemonic_keyval == GDK_VoidSymbol)
    return;
  
  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
  
  if (GTK_IS_WINDOW (toplevel))
    {
      gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
			       label->mnemonic_keyval,
			       GTK_WIDGET (label));
      label->mnemonic_window = GTK_WINDOW (toplevel);
    }
}

static void
gtk_label_hierarchy_changed (GtkWidget *widget)
{
  GtkLabel *label = GTK_LABEL (widget);
  
  gtk_label_setup_mnemonic (label, label->mnemonic_keyval);
}


/**
 * gtk_label_set_mnemonic_widget:
 * @label: a #GtkLabel
 * @widget: the target #GtkWidget 
 *
524 525 526 527 528 529 530 531 532
 * If the label has been set so that it has an mnemonic key (using
 * i.e.  gtk_label_set_markup_with_mnemonic(),
 * gtk_label_set_text_with_mnemonic(), gtk_label_new_with_mnemonic()
 * or the "use_underline" property) the label can be associated with a
 * widget that is the target of the mnemonic. When the label is inside
 * a widget (like a #GtkButton or a #GtkNotebook tab) it is
 * automatically associated with the correct widget, but sometimes
 * (i.e. when the target is a #GtkEntry next to the label) you need to
 * set it explicitly using this function.
533
 *
534
 * The target widget will be accelerated by emitting "mnemonic_activate" on it.
535 536 537 538
 * The default handler for this signal will activate the widget if there are no
 * mnemonic collisions and toggle focus between the colliding widgets otherwise.
 **/
void
539 540
gtk_label_set_mnemonic_widget (GtkLabel  *label,
			       GtkWidget *widget)
541 542
{
  g_return_if_fail (GTK_IS_LABEL (label));
543 544
  if (widget)
    g_return_if_fail (GTK_IS_WIDGET (widget));
545

546 547
  if (label->mnemonic_widget)
    gtk_widget_unref (label->mnemonic_widget);
548
  label->mnemonic_widget = widget;
549 550
  if (label->mnemonic_widget)
    gtk_widget_ref (label->mnemonic_widget);
551 552 553 554 555
}


/**
 * gtk_label_get_mnemonic_keyval:
556 557
 * @label: a #GtkLabel
 *
558 559 560
 * If the label has been set so that it has an mnemonic key this function
 * returns the keyval used for the mnemonic accelerator. If there is no
 * mnemonic set up it returns #GDK_VoidSymbol.
Havoc Pennington's avatar
Havoc Pennington committed
561 562
 *
 * Returns: GDK keyval usable for accelerators, or GDK_VoidSymbol
563 564
 **/
guint
565
gtk_label_get_mnemonic_keyval (GtkLabel *label)
566 567 568
{
  g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);

569
  return label->mnemonic_keyval;
570 571 572
}

static void
573
gtk_label_set_text_internal (GtkLabel *label,
574
			     gchar    *str)
575 576 577 578 579 580 581 582 583 584 585
{
  g_free (label->text);
  
  label->text = str;

  gtk_label_select_region_index (label, 0, 0);
}

static void
gtk_label_set_label_internal (GtkLabel *label,
			      gchar    *str)
Elliot Lee's avatar
Elliot Lee committed
586
{
587
  g_free (label->label);
588
  
589
  label->label = str;
590

591 592
  g_object_notify (G_OBJECT (label), "label");
}
593

594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
static void
gtk_label_set_use_markup_internal (GtkLabel *label,
				   gboolean val)
{
  val = val != FALSE;
  if (label->use_markup != val)
    g_object_notify (G_OBJECT (label), "use_markup");
  label->use_markup = val;
}

static void
gtk_label_set_use_underline_internal (GtkLabel *label,
				      gboolean val)
{
  val = val != FALSE;
  if (label->use_underline != val)
    g_object_notify (G_OBJECT (label), "use_underline");
  label->use_underline = val;
}

static void
gtk_label_set_attributes_internal (GtkLabel         *label,
				   PangoAttrList    *attrs)
{
  if (attrs)
    pango_attr_list_ref (attrs);
620
  
621 622 623 624 625 626 627 628
  if (label->attrs)
    pango_attr_list_unref (label->attrs);

  label->attrs = attrs;
  g_object_notify (G_OBJECT (label), "attributes");
}


629
/* Calculates text, attrs and mnemonic_keyval from
630 631
 * label, use_underline and use_markup
 */
632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
static void
gtk_label_recalculate (GtkLabel *label)
{
  if (label->use_markup)
      set_markup (label, label->label, label->use_underline);
  else
    {
      if (label->use_underline)
	gtk_label_set_uline_text_internal (label, label->label);
      else
	{
	  gtk_label_set_text_internal (label, g_strdup (label->label));
	  gtk_label_set_attributes_internal (label, NULL);
	}
    }

  if (!label->use_underline)
649 650
    {
      guint keyval = label->mnemonic_keyval;
651

652 653 654
      label->mnemonic_keyval = GDK_VoidSymbol;
      gtk_label_setup_mnemonic (label, keyval);
    }
655 656

  gtk_label_clear_layout (label);  
657
  gtk_widget_queue_resize (GTK_WIDGET (label));
658 659
}

660 661
/**
 * gtk_label_set_text:
Havoc Pennington's avatar
Havoc Pennington committed
662 663
 * @label: a #GtkLabel
 * @str: a string
664 665 666 667 668
 *
 * Sets the text of the label to @str.
 *
 * This will also clear any previously set mnemonic accelerators.
 **/
669
void
670 671
gtk_label_set_text (GtkLabel    *label,
		    const gchar *str)
672 673
{
  g_return_if_fail (GTK_IS_LABEL (label));
674
  
675 676 677 678 679
  gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
  gtk_label_set_use_markup_internal (label, FALSE);
  gtk_label_set_use_underline_internal (label, FALSE);
  
  gtk_label_recalculate (label);
680 681
}

682 683 684 685 686 687 688 689 690 691 692 693 694 695
/**
 * gtk_label_set_attributes:
 * @label: a #GtkLabel
 * @attrs: a #PangoAttrList
 * 
 * Sets a #PangoAttrList; the attributes in the list are applied to the
 * label text.
 **/
void
gtk_label_set_attributes (GtkLabel         *label,
                          PangoAttrList    *attrs)
{
  g_return_if_fail (GTK_IS_LABEL (label));

696
  gtk_label_set_attributes_internal (label, attrs);
697
  
698 699
  gtk_label_clear_layout (label);  
  gtk_widget_queue_resize (GTK_WIDGET (label));
700 701
}

702
static void
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722
set_markup (GtkLabel    *label,
            const gchar *str,
            gboolean     with_uline)
{
  gchar *text = NULL;
  GError *error = NULL;
  PangoAttrList *attrs = NULL;
  gunichar accel_char = 0;

  if (!pango_parse_markup (str,
                           -1,
                           with_uline ? '_' : 0,
                           &attrs,
                           &text,
                           with_uline ? &accel_char : NULL,
                           &error))
    {
      g_warning ("Failed to set label from markup due to error parsing markup: %s",
                 error->message);
      g_error_free (error);
Alexander Larsson's avatar
Alexander Larsson committed
723
      return;
724 725 726
    }

  if (text)
727
    gtk_label_set_text_internal (label, text);
728 729 730

  if (attrs)
    {
731
      gtk_label_set_attributes_internal (label, attrs);
732 733 734 735
      pango_attr_list_unref (attrs);
    }

  if (accel_char != 0)
736
    label->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
737
  else
738
    label->mnemonic_keyval = GDK_VoidSymbol;
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754
}

/**
 * gtk_label_set_markup:
 * @label: a #GtkLabel
 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
 * 
 * Parses @str which is marked up with the Pango text markup language,
 * setting the label's text and attribute list based on the parse results.
 **/
void
gtk_label_set_markup (GtkLabel    *label,
                      const gchar *str)
{  
  g_return_if_fail (GTK_IS_LABEL (label));

755 756 757 758 759
  gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
  gtk_label_set_use_markup_internal (label, TRUE);
  gtk_label_set_use_underline_internal (label, FALSE);
  
  gtk_label_recalculate (label);
760 761 762
}

/**
763
 * gtk_label_set_markup_with_mnemonic:
764 765 766 767 768 769
 * @label: a #GtkLabel
 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
 * 
 * Parses @str which is marked up with the Pango text markup language,
 * setting the label's text and attribute list based on the parse results.
 * If characters in @str are preceded by an underscore, they are underlined
770
 * indicating that they represent a keyboard accelerator called a mnemonic.
771
 *
772
 * The mnemonic key can be used to activate another widget, chosen automatically,
773
 * or explicitly using gtk_label_set_mnemonic_widget().
774
 **/
775 776 777
void
gtk_label_set_markup_with_mnemonic (GtkLabel    *label,
				    const gchar *str)
778
{
779 780
  guint last_keyval;
  g_return_if_fail (GTK_IS_LABEL (label));
781

782
  last_keyval = label->mnemonic_keyval;
783 784 785 786 787
  gtk_label_set_label_internal (label, g_strdup (str ? str : ""));
  gtk_label_set_use_markup_internal (label, TRUE);
  gtk_label_set_use_underline_internal (label, TRUE);
  
  gtk_label_recalculate (label);
788
  gtk_label_setup_mnemonic (label, last_keyval);
789 790
}

791 792 793 794 795 796
/**
 * gtk_label_get_text:
 * @label: a #GtkLabel
 * 
 * Fetches the text from a label widget
 * 
797 798
 * Return value: the text in the label widget. This is the internal
 * string used by the label, and must not be modified.
799
 **/
800
G_CONST_RETURN gchar *
801 802
gtk_label_get_text (GtkLabel *label)
{
803 804
  g_return_val_if_fail (label != NULL, NULL);
  g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
805

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858
  return label->text;
}

static PangoAttrList *
gtk_label_pattern_to_attrs (GtkLabel      *label,
			    const gchar   *pattern)
{
  const char *start;
  const char *p = label->text;
  const char *q = pattern;
  PangoAttrList *attrs;

  attrs = pango_attr_list_new ();

  while (1)
    {
      while (*p && *q && *q != '_')
	{
	  p = g_utf8_next_char (p);
	  q++;
	}
      start = p;
      while (*p && *q && *q == '_')
	{
	  p = g_utf8_next_char (p);
	  q++;
	}
      
      if (p > start)
	{
	  PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
	  attr->start_index = start - label->text;
	  attr->end_index = p - label->text;
	  
	  pango_attr_list_insert (attrs, attr);
	}
      else
	break;
    }

  return attrs;
}

static void
gtk_label_set_pattern_internal (GtkLabel    *label,
				const gchar *pattern)
{
  PangoAttrList *attrs;
  g_return_if_fail (GTK_IS_LABEL (label));
  
  attrs = gtk_label_pattern_to_attrs (label, pattern);

  gtk_label_set_attributes_internal (label, attrs);
859 860
}

861 862 863 864 865 866
void
gtk_label_set_pattern (GtkLabel	   *label,
		       const gchar *pattern)
{
  g_return_if_fail (GTK_IS_LABEL (label));
  
867
  gtk_label_set_pattern_internal (label, pattern);
868

869
  gtk_label_clear_layout (label);  
870
  gtk_widget_queue_resize (GTK_WIDGET (label));
Elliot Lee's avatar
Elliot Lee committed
871 872
}

873

Elliot Lee's avatar
Elliot Lee committed
874
void
875 876
gtk_label_set_justify (GtkLabel        *label,
		       GtkJustification jtype)
Elliot Lee's avatar
Elliot Lee committed
877 878
{
  g_return_if_fail (GTK_IS_LABEL (label));
879
  g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
880
  
Elliot Lee's avatar
Elliot Lee committed
881 882 883
  if ((GtkJustification) label->jtype != jtype)
    {
      label->jtype = jtype;
884

885 886
      /* No real need to be this drastic, but easier than duplicating the code */
      gtk_label_clear_layout (label);
887
      
888
      g_object_notify (G_OBJECT (label), "justify");
889
      gtk_widget_queue_resize (GTK_WIDGET (label));
Elliot Lee's avatar
Elliot Lee committed
890 891 892 893
    }
}

void
894 895
gtk_label_set_line_wrap (GtkLabel *label,
			 gboolean  wrap)
Owen Taylor's avatar
Owen Taylor committed
896 897
{
  g_return_if_fail (GTK_IS_LABEL (label));
898
  
899 900 901 902 903
  wrap = wrap != FALSE;
  
  if (label->wrap != wrap)
    {
      label->wrap = wrap;
904 905
      g_object_notify (G_OBJECT (label), "wrap");
      
906 907
      gtk_widget_queue_resize (GTK_WIDGET (label));
    }
Owen Taylor's avatar
Owen Taylor committed
908 909 910
}

void
911 912
gtk_label_get (GtkLabel *label,
	       gchar   **str)
Elliot Lee's avatar
Elliot Lee committed
913 914 915 916
{
  g_return_if_fail (label != NULL);
  g_return_if_fail (GTK_IS_LABEL (label));
  g_return_if_fail (str != NULL);
917
  
918
  *str = label->text;
Elliot Lee's avatar
Elliot Lee committed
919 920
}

921 922 923 924 925 926 927 928 929 930
static void
gtk_label_destroy (GtkObject *object)
{
  GtkLabel *label = GTK_LABEL (object);

  gtk_label_set_mnemonic_widget (label, NULL);

  GTK_OBJECT_CLASS (parent_class)->destroy (object);
}

Elliot Lee's avatar
Elliot Lee committed
931
static void
932
gtk_label_finalize (GObject *object)
Elliot Lee's avatar
Elliot Lee committed
933 934
{
  GtkLabel *label;
935
  
Elliot Lee's avatar
Elliot Lee committed
936
  g_return_if_fail (GTK_IS_LABEL (object));
937
  
Elliot Lee's avatar
Elliot Lee committed
938
  label = GTK_LABEL (object);
939
  
Elliot Lee's avatar
Elliot Lee committed
940
  g_free (label->label);
941
  g_free (label->text);
942

943
  if (label->layout)
944
    g_object_unref (G_OBJECT (label->layout));
945

946 947
  if (label->attrs)
    pango_attr_list_unref (label->attrs);
948 949

  g_free (label->select_info);
950

951
  G_OBJECT_CLASS (parent_class)->finalize (object);
Elliot Lee's avatar
Elliot Lee committed
952 953 954
}

static void
955
gtk_label_clear_layout (GtkLabel *label)
Elliot Lee's avatar
Elliot Lee committed
956
{
957 958 959 960 961 962 963 964 965 966 967 968 969
  if (label->layout)
    {
      g_object_unref (G_OBJECT (label->layout));
      label->layout = NULL;
    }
}

static void
gtk_label_ensure_layout (GtkLabel *label,
                         gint     *widthp,
                         gint     *heightp)
{
  GtkWidget *widget;
970
  PangoRectangle logical_rect;
971
  gint rwidth, rheight;
972 973

  widget = GTK_WIDGET (label);
974

Owen Taylor's avatar
Owen Taylor committed
975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994
  /*
   * There are a number of conditions which will necessitate re-filling
   * our text:
   *
   *     1. text changed.
   *     2. justification changed either from to to GTK_JUSTIFY_FILL.
   *     3. font changed.
   *
   * These have been detected elsewhere, and label->words will be zero,
   * if one of the above has occured.
   *
   * Additionally, though, if GTK_JUSTIFY_FILL, we need to re-fill if:
   *
   *     4. gtk_widget_set_usize has changed the requested width.
   *     5. gtk_misc_set_padding has changed xpad.
   *     6.  maybe others?...
   *
   * Too much of a pain to detect all these case, so always re-fill.  I
   * don't think it's really that slow.
   */
995

996 997
  rwidth = label->misc.xpad * 2;
  rheight = label->misc.ypad * 2;
998 999 1000 1001 1002

  if (!label->layout)
    {
      PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */

1003
      label->layout = gtk_widget_create_pango_layout (widget, label->text);
1004

1005
      if (label->attrs)
1006
	pango_layout_set_attributes (label->layout, label->attrs);
1007
      
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
      switch (label->jtype)
	{
	case GTK_JUSTIFY_LEFT:
	  align = PANGO_ALIGN_LEFT;
	  break;
	case GTK_JUSTIFY_RIGHT:
	  align = PANGO_ALIGN_RIGHT;
	  break;
	case GTK_JUSTIFY_CENTER:
	  align = PANGO_ALIGN_LEFT;
	  break;
	case GTK_JUSTIFY_FILL:
	  /* FIXME: This just doesn't work to do this */
	  align = PANGO_ALIGN_LEFT;
	  pango_layout_set_justify (label->layout, TRUE);
	  break;
	default:
	  g_assert_not_reached();
	}

      pango_layout_set_alignment (label->layout, align);
    }

Owen Taylor's avatar
Owen Taylor committed
1031
  if (label->wrap)
Elliot Lee's avatar
Elliot Lee committed
1032
    {
Owen Taylor's avatar
Owen Taylor committed
1033 1034
      GtkWidgetAuxInfo *aux_info;
      gint longest_paragraph;
1035 1036 1037
      gint width, height;
      gint real_width;

1038
      aux_info = _gtk_widget_get_aux_info (widget, FALSE);
Owen Taylor's avatar
Owen Taylor committed
1039 1040
      if (aux_info && aux_info->width > 0)
	{
1041 1042 1043
	  pango_layout_set_width (label->layout, aux_info->width * PANGO_SCALE);
	  pango_layout_get_extents (label->layout, NULL, &logical_rect);

1044 1045
	  rwidth += aux_info->width;
	  rheight += PANGO_PIXELS (logical_rect.height);
Owen Taylor's avatar
Owen Taylor committed
1046 1047 1048
	}
      else
	{
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
	  pango_layout_set_width (label->layout, -1);
	  pango_layout_get_extents (label->layout, NULL, &logical_rect);
      
	  width = logical_rect.width;
	  height = logical_rect.height;
	  
	  /* Try to guess a reasonable maximum width
	   */
	  longest_paragraph = width;

	  width = MIN (width,
		       PANGO_SCALE * gdk_string_width (GTK_WIDGET (label)->style->font,
						"This long string gives a good enough length for any line to have."));
	  width = MIN (width,
		       PANGO_SCALE * (gdk_screen_width () + 1) / 2);

	  pango_layout_set_width (label->layout, width);
	  pango_layout_get_extents (label->layout, NULL, &logical_rect);
	  real_width = logical_rect.width;
	  height = logical_rect.height;
	  
	  /* Unfortunately, the above may leave us with a very unbalanced looking paragraph,
	   * so we try short search for a narrower width that leaves us with the same height
	   */
Owen Taylor's avatar
Owen Taylor committed
1073 1074 1075
	  if (longest_paragraph > 0)
	    {
	      gint nlines, perfect_width;
1076 1077

	      nlines = pango_layout_get_line_count (label->layout);
Owen Taylor's avatar
Owen Taylor committed
1078
	      perfect_width = (longest_paragraph + nlines - 1) / nlines;
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
	      
	      if (perfect_width < width)
		{
		  pango_layout_set_width (label->layout, perfect_width);
		  pango_layout_get_extents (label->layout, NULL, &logical_rect);
		  
		  if (logical_rect.height <= height)
		    {
		      width = perfect_width;
		      real_width = logical_rect.width;
		      height = logical_rect.height;
		    }
		  else
		    {
		      gint mid_width = (perfect_width + width) / 2;

		      if (mid_width > perfect_width)
			{
			  pango_layout_set_width (label->layout, mid_width);
			  pango_layout_get_extents (label->layout, NULL, &logical_rect);

			  if (logical_rect.height <= height)
			    {
			      width = mid_width;
			      real_width = logical_rect.width;
			      height = logical_rect.height;
			    }
			}
		    }
		}
Owen Taylor's avatar
Owen Taylor committed
1109
	    }
1110 1111
	  pango_layout_set_width (label->layout, width);

1112 1113
	  rwidth += PANGO_PIXELS (real_width);
	  rheight += PANGO_PIXELS (height);
Owen Taylor's avatar
Owen Taylor committed
1114 1115
	}
    }
1116
  else				/* !label->wrap */
Owen Taylor's avatar
Owen Taylor committed
1117
    {
1118 1119 1120
      pango_layout_set_width (label->layout, -1);
      pango_layout_get_extents (label->layout, NULL, &logical_rect);

1121 1122
      rwidth += PANGO_PIXELS (logical_rect.width);
      rheight += PANGO_PIXELS (logical_rect.height);
1123 1124 1125
    }

  if (widthp)
1126
    *widthp = rwidth;
1127 1128

  if (heightp)
1129
    *heightp = rheight;
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 1160 1161 1162 1163 1164 1165 1166
}

static void
gtk_label_size_request (GtkWidget      *widget,
			GtkRequisition *requisition)
{
  GtkLabel *label;
  gint width, height;
  
  g_return_if_fail (GTK_IS_LABEL (widget));
  g_return_if_fail (requisition != NULL);
  
  label = GTK_LABEL (widget);

  gtk_label_ensure_layout (label, &width, &height);

  requisition->width = width;
  requisition->height = height;
}

static void
gtk_label_size_allocate (GtkWidget     *widget,
                         GtkAllocation *allocation)
{
  GtkLabel *label;

  label = GTK_LABEL (widget);
  
  (* GTK_WIDGET_CLASS (parent_class)->size_allocate) (widget, allocation);

  if (label->select_info && label->select_info->window)
    {
      gdk_window_move_resize (label->select_info->window,
                              allocation->x,
                              allocation->y,
                              allocation->width,
                              allocation->height);
Owen Taylor's avatar
Owen Taylor committed
1167 1168
    }
}
1169

Havoc Pennington's avatar