gtklabel.c 50.4 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
  PROP_MNEMONIC_KEYVAL
58 59
};

60 61
static void gtk_label_class_init        (GtkLabelClass    *klass);
static void gtk_label_init              (GtkLabel         *label);
62 63 64
static void gtk_label_set_property      (GObject          *object,
					 guint             prop_id,
					 const GValue     *value,
65
					 GParamSpec       *pspec);
66 67 68
static void gtk_label_get_property      (GObject          *object,
					 guint             prop_id,
					 GValue           *value,
69
					 GParamSpec       *pspec);
70 71 72
static void gtk_label_finalize          (GObject          *object);
static void gtk_label_size_request      (GtkWidget        *widget,
					 GtkRequisition   *requisition);
73 74
static void gtk_label_size_allocate     (GtkWidget        *widget,
                                         GtkAllocation    *allocation);
75 76 77 78 79 80
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
81

82 83 84 85 86 87 88 89 90 91 92
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);

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

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

114 115 116 117 118 119 120 121 122 123
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);

124 125 126
static gboolean gtk_label_activate_mnemonic (GtkWidget *widget,
					     gboolean   group_cycling);

127

128 129
static GtkMiscClass *parent_class = NULL;

Elliot Lee's avatar
Elliot Lee committed
130

Tim Janik's avatar
Tim Janik committed
131
GtkType
132
gtk_label_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
133
{
Tim Janik's avatar
Tim Janik committed
134 135
  static GtkType label_type = 0;
  
Elliot Lee's avatar
Elliot Lee committed
136 137
  if (!label_type)
    {
138
      static const GTypeInfo label_info =
Elliot Lee's avatar
Elliot Lee committed
139 140
      {
	sizeof (GtkLabelClass),
141 142 143 144 145 146 147 148
	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
149
      };
150

151
      label_type = g_type_register_static (GTK_TYPE_MISC, "GtkLabel", &label_info, 0);
Elliot Lee's avatar
Elliot Lee committed
152
    }
Tim Janik's avatar
Tim Janik committed
153
  
Elliot Lee's avatar
Elliot Lee committed
154 155 156
  return label_type;
}

157
static void
Elliot Lee's avatar
Elliot Lee committed
158 159
gtk_label_class_init (GtkLabelClass *class)
{
160
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
161 162
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
Tim Janik's avatar
Tim Janik committed
163
  
Elliot Lee's avatar
Elliot Lee committed
164 165
  object_class = (GtkObjectClass*) class;
  widget_class = (GtkWidgetClass*) class;
Tim Janik's avatar
Tim Janik committed
166
  
167
  parent_class = gtk_type_class (GTK_TYPE_MISC);
Tim Janik's avatar
Tim Janik committed
168
  
169 170
  gobject_class->set_property = gtk_label_set_property;
  gobject_class->get_property = gtk_label_get_property;
171
  gobject_class->finalize = gtk_label_finalize;
Tim Janik's avatar
Tim Janik committed
172
  
Elliot Lee's avatar
Elliot Lee committed
173
  widget_class->size_request = gtk_label_size_request;
174
  widget_class->size_allocate = gtk_label_size_allocate;
175
  widget_class->style_set = gtk_label_style_set;
176
  widget_class->direction_changed = gtk_label_direction_changed;
Elliot Lee's avatar
Elliot Lee committed
177
  widget_class->expose_event = gtk_label_expose;
178 179 180 181 182 183 184
  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;
185 186
  widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
  widget_class->activate_mnemonic = gtk_label_activate_mnemonic;
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212

  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"),
213
							 _("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
                                                        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
230
                                                        _("A string with _ characters in positions correspond to characters in the text to underline."),
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
                                                        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,
249 250 251 252
                                   PROP_MNEMONIC_KEYVAL,
                                   g_param_spec_uint ("mnemonic_keyval",
						      _("Mnemonic accelerator key value"),
						      _("The mnemonic accelerator key for this label."),
253 254 255 256
						      0,
						      G_MAXUINT,
						      GDK_VoidSymbol,
						      G_PARAM_READABLE));
257

Elliot Lee's avatar
Elliot Lee committed
258 259
}

260 261 262 263
static void 
gtk_label_set_property (GObject      *object,
			guint         prop_id,
			const GValue *value,
264
			GParamSpec   *pspec)
265
{
266
  GtkLabel *label;
267
  
268
  label = GTK_LABEL (object);
269
  
270
  switch (prop_id)
271
    {
272 273 274 275 276 277 278 279 280 281 282
    case PROP_LABEL:
      gtk_label_set_label_internal (label,
				    g_strdup (g_value_get_string (value)));
      gtk_label_recalculate (label);
      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);
283
      break;
284 285 286
    case PROP_USE_UNDERLINE:
      gtk_label_set_use_underline_internal (label, g_value_get_boolean (value));
      gtk_label_recalculate (label);
287
      break;
288 289
    case PROP_JUSTIFY:
      gtk_label_set_justify (label, g_value_get_enum (value));
290
      break;
291 292 293 294 295 296 297 298
    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));
299
      break;	  
Tim Janik's avatar
Tim Janik committed
300
    default:
301
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
302
      break;
303 304 305
    }
}

306 307 308 309
static void 
gtk_label_get_property (GObject     *object,
			guint        prop_id,
			GValue      *value,
310
			GParamSpec  *pspec)
311
{
312
  GtkLabel *label;
313
  
314
  label = GTK_LABEL (object);
315
  
316
  switch (prop_id)
317
    {
318 319 320 321 322 323 324 325 326 327 328
    case PROP_LABEL:
      g_value_set_string (value, g_strdup (label->label));
      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);
329
      break;
330 331
    case PROP_JUSTIFY:
      g_value_set_enum (value, label->jtype);
332
      break;
333 334
    case PROP_WRAP:
      g_value_set_boolean (value, label->wrap);
335
      break;
336 337 338
    case PROP_SELECTABLE:
      g_value_set_boolean (value, gtk_label_get_selectable (label));
      break;
339 340
    case PROP_MNEMONIC_KEYVAL:
      g_value_set_uint (value, label->mnemonic_keyval);
341
      break;
342

343
    default:
344
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
345 346 347 348
      break;
    }
}

349
static void
Elliot Lee's avatar
Elliot Lee committed
350 351 352
gtk_label_init (GtkLabel *label)
{
  GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
Tim Janik's avatar
Tim Janik committed
353
  
Elliot Lee's avatar
Elliot Lee committed
354
  label->label = NULL;
355

Elliot Lee's avatar
Elliot Lee committed
356
  label->jtype = GTK_JUSTIFY_CENTER;
Owen Taylor's avatar
Owen Taylor committed
357
  label->wrap = FALSE;
358 359 360

  label->use_underline = FALSE;
  label->use_markup = FALSE;
361
  
362
  label->mnemonic_keyval = GDK_VoidSymbol;
363
  label->layout = NULL;
364
  label->text = NULL;
365
  label->attrs = NULL;
366 367 368

  label->mnemonic_widget = NULL;
  label->mnemonic_window = NULL;
369
  
Owen Taylor's avatar
Owen Taylor committed
370
  gtk_label_set_text (label, "");
Elliot Lee's avatar
Elliot Lee committed
371 372
}

373 374 375 376 377 378 379
/**
 * 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
380
GtkWidget*
381
gtk_label_new (const gchar *str)
Elliot Lee's avatar
Elliot Lee committed
382 383
{
  GtkLabel *label;
384
  
385 386 387 388
  label = gtk_type_new (GTK_TYPE_LABEL);

  if (str && *str)
    gtk_label_set_text (label, str);
389
  
Elliot Lee's avatar
Elliot Lee committed
390 391 392
  return GTK_WIDGET (label);
}

393
/**
394 395 396 397 398 399 400 401 402 403
 * 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.
 *
 * 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,
404
 * or explicitly using gtk_label_set_mnemonic_widget().
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
 **/
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
gtk_label_activate_mnemonic (GtkWidget *widget,
			     gboolean   group_cycling)
{
  GtkWidget *parent;

  if (GTK_LABEL (widget)->mnemonic_widget)
    return gtk_widget_activate_mnemonic (GTK_LABEL (widget)->mnemonic_widget, group_cycling);

  /* Try to find the widget to activate by traversing the widget
   * hierarachy.
   */
  
  parent = widget->parent;
  while (parent)
    {
      if (GTK_WIDGET_CAN_FOCUS (parent) ||
	  (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
	  (parent->parent && GTK_IS_NOTEBOOK (parent->parent)) ||
	  (GTK_IS_MENU_ITEM (parent)))
	return gtk_widget_activate_mnemonic (parent, group_cycling);
      parent = parent->parent;
    }

  g_warning ("Couldn't find a target for a mnemonic activation.");
  gdk_beep ();
  
  return FALSE;
}

static void
gtk_label_setup_mnemonic (GtkLabel *label, guint last_key)
{
  GtkWidget *toplevel;

  if ((last_key != GDK_VoidSymbol) && label->mnemonic_window)
    gtk_window_remove_mnemonic  (label->mnemonic_window,
				 last_key,
				 GTK_WIDGET (label));
  
  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 
 *
487 488 489 490 491 492 493 494 495
 * 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.
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513
 *
 * The target widget will be accelerated by emitting "activate_mnemonic" on it.
 * 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
gtk_label_set_mnemonic_widget (GtkLabel         *label,
			       GtkWidget        *widget)
{
  g_return_if_fail (GTK_IS_LABEL (label));
  g_return_if_fail (GTK_IS_WIDGET (widget));

  label->mnemonic_widget = widget;
}


/**
 * gtk_label_get_mnemonic_keyval:
514 515 516
 * @label: a #GtkLabel
 * @Returns: GDK keyval usable for accelerators, or GDK_VoidSymbol
 *
517 518 519
 * 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.
520 521
 **/
guint
522
gtk_label_get_mnemonic_keyval (GtkLabel *label)
523 524 525
{
  g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);

526
  return label->mnemonic_keyval;
527 528 529
}

static void
530
gtk_label_set_text_internal (GtkLabel *label,
531
			     gchar    *str)
532 533 534 535 536 537 538 539 540 541 542
{
  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
543
{
544
  g_free (label->label);
545
  
546
  label->label = str;
547

548 549
  g_object_notify (G_OBJECT (label), "label");
}
550

551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576
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);
577
  
578 579 580 581 582 583 584 585
  if (label->attrs)
    pango_attr_list_unref (label->attrs);

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


586
/* Calculates text, attrs and mnemonic_keyval from
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
 * label, use_underline and use_markup */
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)
605 606 607 608 609
    {
      guint keyval = label->mnemonic_keyval;
      label->mnemonic_keyval = GDK_VoidSymbol;
      gtk_label_setup_mnemonic (label, keyval);
    }
610 611

  gtk_label_clear_layout (label);  
612
  gtk_widget_queue_resize (GTK_WIDGET (label));
613 614
}

615 616 617 618 619 620 621 622 623
/**
 * gtk_label_set_text:
 * label: a #GtkLabel
 * str: a string
 *
 * Sets the text of the label to @str.
 *
 * This will also clear any previously set mnemonic accelerators.
 **/
624
void
625 626
gtk_label_set_text (GtkLabel    *label,
		    const gchar *str)
627 628
{
  g_return_if_fail (GTK_IS_LABEL (label));
629
  
630 631 632 633 634
  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);
635 636
}

637 638 639 640 641 642 643 644 645 646 647 648 649 650
/**
 * 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));

651
  gtk_label_set_attributes_internal (label, attrs);
652
  
653 654
  gtk_label_clear_layout (label);  
  gtk_widget_queue_resize (GTK_WIDGET (label));
655 656
}

657
static void
658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
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
678
      return;
679 680 681
    }

  if (text)
682
    gtk_label_set_text_internal (label, text);
683 684 685

  if (attrs)
    {
686
      gtk_label_set_attributes_internal (label, attrs);
687 688 689 690
      pango_attr_list_unref (attrs);
    }

  if (accel_char != 0)
691
    label->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
692
  else
693
    label->mnemonic_keyval = GDK_VoidSymbol;
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
}

/**
 * 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));

710 711 712 713 714
  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);
715 716 717
}

/**
718
 * gtk_label_set_markup_with_mnemonic:
719 720 721 722 723 724
 * @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
725
 * indicating that they represent a keyboard accelerator called a mnemonic.
726
 *
727
 * The mnemonic key can be used to activate another widget, chosen automatically,
728
 * or explicitly using gtk_label_set_mnemonic_widget().
729
 **/
730 731 732
void
gtk_label_set_markup_with_mnemonic (GtkLabel    *label,
				    const gchar *str)
733
{
734 735
  guint last_keyval;
  g_return_if_fail (GTK_IS_LABEL (label));
736

737
  last_keyval = label->mnemonic_keyval;
738 739 740 741 742
  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);
743
  gtk_label_setup_mnemonic (label, last_keyval);
744 745
}

746 747 748 749 750 751
/**
 * gtk_label_get_text:
 * @label: a #GtkLabel
 * 
 * Fetches the text from a label widget
 * 
752 753
 * Return value: the text in the label widget. This is the internal
 * string used by the label, and must not be modified.
754
 **/
755
G_CONST_RETURN gchar *
756 757
gtk_label_get_text (GtkLabel *label)
{
758 759
  g_return_val_if_fail (label != NULL, NULL);
  g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
760

761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813
  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);
814 815
}

816 817 818 819 820 821
void
gtk_label_set_pattern (GtkLabel	   *label,
		       const gchar *pattern)
{
  g_return_if_fail (GTK_IS_LABEL (label));
  
822
  gtk_label_set_pattern_internal (label, pattern);
823

824
  gtk_label_clear_layout (label);  
825
  gtk_widget_queue_resize (GTK_WIDGET (label));
Elliot Lee's avatar
Elliot Lee committed
826 827
}

828

Elliot Lee's avatar
Elliot Lee committed
829
void
830 831
gtk_label_set_justify (GtkLabel        *label,
		       GtkJustification jtype)
Elliot Lee's avatar
Elliot Lee committed
832 833
{
  g_return_if_fail (GTK_IS_LABEL (label));
834
  g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
835
  
Elliot Lee's avatar
Elliot Lee committed
836 837 838
  if ((GtkJustification) label->jtype != jtype)
    {
      label->jtype = jtype;
839

840 841
      /* No real need to be this drastic, but easier than duplicating the code */
      gtk_label_clear_layout (label);
842
      
843
      g_object_notify (G_OBJECT (label), "justify");
844
      gtk_widget_queue_resize (GTK_WIDGET (label));
Elliot Lee's avatar
Elliot Lee committed
845 846 847 848
    }
}

void
849 850
gtk_label_set_line_wrap (GtkLabel *label,
			 gboolean  wrap)
Owen Taylor's avatar
Owen Taylor committed
851 852
{
  g_return_if_fail (GTK_IS_LABEL (label));
853
  
854 855 856 857 858
  wrap = wrap != FALSE;
  
  if (label->wrap != wrap)
    {
      label->wrap = wrap;
859 860
      g_object_notify (G_OBJECT (label), "wrap");
      
861 862
      gtk_widget_queue_resize (GTK_WIDGET (label));
    }
Owen Taylor's avatar
Owen Taylor committed
863 864 865
}

void
866 867
gtk_label_get (GtkLabel *label,
	       gchar   **str)
Elliot Lee's avatar
Elliot Lee committed
868 869 870 871
{
  g_return_if_fail (label != NULL);
  g_return_if_fail (GTK_IS_LABEL (label));
  g_return_if_fail (str != NULL);
872
  
873
  *str = label->text;
Elliot Lee's avatar
Elliot Lee committed
874 875 876
}

static void
877
gtk_label_finalize (GObject *object)
Elliot Lee's avatar
Elliot Lee committed
878 879
{
  GtkLabel *label;
880
  
Elliot Lee's avatar
Elliot Lee committed
881
  g_return_if_fail (GTK_IS_LABEL (object));
882
  
Elliot Lee's avatar
Elliot Lee committed
883
  label = GTK_LABEL (object);
884
  
Elliot Lee's avatar
Elliot Lee committed
885
  g_free (label->label);
886
  g_free (label->text);
887

888
  if (label->layout)
889
    g_object_unref (G_OBJECT (label->layout));
890

891 892
  if (label->attrs)
    pango_attr_list_unref (label->attrs);
893 894

  g_free (label->select_info);
895

896
  G_OBJECT_CLASS (parent_class)->finalize (object);
Elliot Lee's avatar
Elliot Lee committed
897 898 899
}

static void
900
gtk_label_clear_layout (GtkLabel *label)
Elliot Lee's avatar
Elliot Lee committed
901
{
902 903 904 905 906 907 908 909 910 911 912 913 914
  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;
915
  PangoRectangle logical_rect;
916
  gint rwidth, rheight;
917 918

  widget = GTK_WIDGET (label);
919

Owen Taylor's avatar
Owen Taylor committed
920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939
  /*
   * 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.
   */
940

941 942
  rwidth = label->misc.xpad * 2;
  rheight = label->misc.ypad * 2;
943 944 945 946 947

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

948
      label->layout = gtk_widget_create_pango_layout (widget, label->text);
949

950
      if (label->attrs)
951
	pango_layout_set_attributes (label->layout, label->attrs);
952
      
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975
      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
976
  if (label->wrap)
Elliot Lee's avatar
Elliot Lee committed
977
    {
Owen Taylor's avatar
Owen Taylor committed
978 979
      GtkWidgetAuxInfo *aux_info;
      gint longest_paragraph;
980 981 982
      gint width, height;
      gint real_width;

Owen Taylor's avatar
Owen Taylor committed
983 984 985
      aux_info = gtk_object_get_data (GTK_OBJECT (widget), "gtk-aux-info");
      if (aux_info && aux_info->width > 0)
	{
986 987 988
	  pango_layout_set_width (label->layout, aux_info->width * PANGO_SCALE);
	  pango_layout_get_extents (label->layout, NULL, &logical_rect);

989 990
	  rwidth += aux_info->width;
	  rheight += PANGO_PIXELS (logical_rect.height);
Owen Taylor's avatar
Owen Taylor committed
991 992 993
	}
      else
	{
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
	  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
1018 1019 1020
	  if (longest_paragraph > 0)
	    {
	      gint nlines, perfect_width;
1021 1022

	      nlines = pango_layout_get_line_count (label->layout);
Owen Taylor's avatar
Owen Taylor committed
1023
	      perfect_width = (longest_paragraph + nlines - 1) / nlines;
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
	      
	      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
1054
	    }
1055 1056
	  pango_layout_set_width (label->layout, width);

1057 1058
	  rwidth += PANGO_PIXELS (real_width);
	  rheight += PANGO_PIXELS (height);
Owen Taylor's avatar
Owen Taylor committed
1059 1060
	}
    }
1061
  else				/* !label->wrap */
Owen Taylor's avatar
Owen Taylor committed
1062
    {
1063 1064 1065
      pango_layout_set_width (label->layout, -1);
      pango_layout_get_extents (label->layout, NULL, &logical_rect);

1066 1067
      rwidth += PANGO_PIXELS (logical_rect.width);
      rheight += PANGO_PIXELS (logical_rect.height);
1068 1069 1070
    }

  if (widthp)
1071
    *widthp = rwidth;
1072 1073

  if (heightp)
1074
    *heightp = rheight;
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
}

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
1112 1113
    }
}
1114

1115
static void 
1116 1117
gtk_label_style_set (GtkWidget *widget,
		     GtkStyle  *previous_style)
1118 1119
{
  GtkLabel *label;
1120
  
1121
  g_return_if_fail (GTK_IS_LABEL (widget));
1122
  
1123
  label = GTK_LABEL (widget);
1124

1125 1126
  /* We have to clear the layout, fonts etc. may have changed */
  gtk_label_clear_layout (label);
1127 1128
}

1129 1130 1131 1132 1133 1134 1135
static void 
gtk_label_direction_changed (GtkWidget        *widget,
			     GtkTextDirection previous_dir)
{
  GtkLabel *label = GTK_LABEL (widget);

  if (label->layout)
1136
    pango_layout_context_changed (label->layout);
1137 1138 1139 1140

  GTK_WIDGET_CLASS (parent_class)->direction_changed (widget, previous_dir);
}

1141
#if 0
Owen Taylor's avatar
Owen Taylor committed
1142
static void
1143 1144 1145
gtk_label_paint_word (GtkLabel     *label,
		      gint          x,
		      gint          y,
Owen Taylor's avatar
Owen Taylor committed
1146 1147 1148 1149 1150 1151
		      GtkLabelWord *word,
		      GdkRectangle *area)
{
  GtkWidget *widget = GTK_WIDGET (label);
  GtkLabelULine *uline;
  gchar *tmp_str;
1152
  
Owen Taylor's avatar
Owen Taylor committed
1153
  tmp_str = gdk_wcstombs (word->beginning);
1154 1155 1156 1157 1158 1159 1160 1161 1162
  if (tmp_str)
    {
      gtk_paint_string (widget->style, widget->window, widget->state,
			area, widget, "label", 
			x + word->x,
			y + word->y,
			tmp_str);
      g_free (tmp_str);
    }
Owen Taylor's avatar
Owen Taylor committed
1163
  
1164
  for (uline = word->uline; uline; uline = uline->next)
Owen Taylor's avatar
Owen Taylor committed
1165 1166 1167
    gtk_paint_hline (widget->style, widget->window, 
		     widget->state, area,
		     widget, "label", 
1168
		     x + uline->x1, x + uline->x2, y + uline->y);
Elliot Lee's avatar
Elliot Lee committed
1169
}
1170
#endif
1171

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
static void
get_layout_location (GtkLabel  *label,
                     gint      *xp,
                     gint      *yp)
{
  GtkMisc *misc;
  GtkWidget *widget;
  gfloat xalign;
  gint x, y;
  
  misc = GTK_MISC (label);
  widget = GTK_WIDGET (label);
  
  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
    xalign = misc->xalign;
  else
    xalign = 1.0 - misc->xalign;
  
  x = floor (widget->allocation.x + (gint)misc->xpad
             + ((widget->allocation.width - widget->requisition.width) * xalign)
             + 0.5);
  
  y = floor (widget->allocation.y + (gint)misc->ypad 
             + ((widget->allocation.height - widget->requisition.height) * misc->yalign)
             + 0.5);
  

  if (xp)
    *xp = x;

  if (yp)
    *yp = y;
}

Elliot Lee's avatar
Elliot Lee committed
1206
static gint
Owen Taylor's avatar
Owen Taylor committed
1207
gtk_label_expose (GtkWidget      *widget,
Elliot Lee's avatar
Elliot Lee committed
1208 1209 1210 1211
		  GdkEventExpose *event)
{
  GtkLabel *label;
  gint x, y;
1212
  
Elliot Lee's avatar
Elliot Lee committed
1213 1214
  g_return_val_if_fail (GTK_IS_LABEL (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
1215
  
Arturo Espinosa's avatar
Arturo Espinosa committed
1216
  label = GTK_LABEL (widget);
1217

1218 1219
  gtk_label_ensure_layout (label, NULL, NULL);
  
1220
  if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
1221
      label->text && (*label->text != '\0'))
Elliot Lee's avatar
Elliot Lee committed
1222
    {