gtklabel.c 92.5 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 <config.h>
27
#include <math.h>
Elliot Lee's avatar
Elliot Lee committed
28 29
#include <string.h>
#include "gtklabel.h"
30
#include "gtkmain.h"
31
#include "gtkmarshalers.h"
32
#include "gtkwindow.h"
33
#include "gdk/gdkkeysyms.h"
34
#include "gtkclipboard.h"
35
#include <pango/pango.h>
36
#include "gtkimagemenuitem.h"
37
#include "gtkintl.h"
38
#include "gtkseparatormenuitem.h"
39 40
#include "gtkmenuitem.h"
#include "gtknotebook.h"
41
#include "gtkstock.h"
42
#include "gtkbindings.h"
43

44 45 46 47 48
struct _GtkLabelSelectionInfo
{
  GdkWindow *window;
  gint selection_anchor;
  gint selection_end;
49
  GtkWidget *popup_menu;
50
};
Elliot Lee's avatar
Elliot Lee committed
51

52 53 54 55 56 57
enum {
  MOVE_CURSOR,
  COPY_CLIPBOARD,
  POPULATE_POPUP,
  LAST_SIGNAL
};
58

59
enum {
60 61 62 63 64 65 66 67 68
  PROP_0,
  PROP_LABEL,
  PROP_ATTRIBUTES,
  PROP_USE_MARKUP,
  PROP_USE_UNDERLINE,
  PROP_JUSTIFY,
  PROP_PATTERN,
  PROP_WRAP,
  PROP_SELECTABLE,
69
  PROP_MNEMONIC_KEYVAL,
70 71
  PROP_MNEMONIC_WIDGET,
  PROP_CURSOR_POSITION,
72 73
  PROP_SELECTION_BOUND,
  PROP_ELLIPSIZE
74 75
};

76 77
static guint signals[LAST_SIGNAL] = { 0 };

78 79
static void gtk_label_class_init        (GtkLabelClass    *klass);
static void gtk_label_init              (GtkLabel         *label);
80 81 82
static void gtk_label_set_property      (GObject          *object,
					 guint             prop_id,
					 const GValue     *value,
83
					 GParamSpec       *pspec);
84 85 86
static void gtk_label_get_property      (GObject          *object,
					 guint             prop_id,
					 GValue           *value,
87
					 GParamSpec       *pspec);
88
static void gtk_label_destroy           (GtkObject        *object);
89 90 91
static void gtk_label_finalize          (GObject          *object);
static void gtk_label_size_request      (GtkWidget        *widget,
					 GtkRequisition   *requisition);
92 93
static void gtk_label_size_allocate     (GtkWidget        *widget,
                                         GtkAllocation    *allocation);
Havoc Pennington's avatar
Havoc Pennington committed
94 95
static void gtk_label_state_changed     (GtkWidget        *widget,
                                         GtkStateType      state);
96 97 98 99 100 101
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
102

103 104 105 106
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
107 108 109 110 111 112 113

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);
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

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);
134 135
static void gtk_label_hierarchy_changed          (GtkWidget     *widget,
						  GtkWidget     *old_toplevel);
136 137
static void gtk_label_screen_changed             (GtkWidget     *widget,
						  GdkScreen     *old_screen);
138

139 140 141
static void gtk_label_create_window       (GtkLabel *label);
static void gtk_label_destroy_window      (GtkLabel *label);
static void gtk_label_clear_layout        (GtkLabel *label);
142
static void gtk_label_ensure_layout       (GtkLabel *label);
143 144 145 146
static void gtk_label_select_region_index (GtkLabel *label,
                                           gint      anchor_index,
                                           gint      end_index);

147 148 149 150
static gboolean gtk_label_mnemonic_activate (GtkWidget         *widget,
					     gboolean           group_cycling);
static void     gtk_label_setup_mnemonic    (GtkLabel          *label,
					     guint              last_key);
151 152
static gboolean gtk_label_focus             (GtkWidget         *widget,
					     GtkDirectionType   direction);
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167

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

169 170
static GtkMiscClass *parent_class = NULL;

Elliot Lee's avatar
Elliot Lee committed
171

Manish Singh's avatar
Manish Singh committed
172
GType
173
gtk_label_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
174
{
Manish Singh's avatar
Manish Singh committed
175
  static GType label_type = 0;
Tim Janik's avatar
Tim Janik committed
176
  
Elliot Lee's avatar
Elliot Lee committed
177 178
  if (!label_type)
    {
179
      static const GTypeInfo label_info =
Elliot Lee's avatar
Elliot Lee committed
180 181
      {
	sizeof (GtkLabelClass),
182 183 184 185 186 187 188 189
	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
190
      };
191

Manish Singh's avatar
Manish Singh committed
192 193
      label_type = g_type_register_static (GTK_TYPE_MISC, "GtkLabel",
					   &label_info, 0);
Elliot Lee's avatar
Elliot Lee committed
194
    }
Tim Janik's avatar
Tim Janik committed
195
  
Elliot Lee's avatar
Elliot Lee committed
196 197 198
  return label_type;
}

199 200 201 202 203 204 205 206 207 208 209
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,
				"move_cursor", 3,
Manish Singh's avatar
Manish Singh committed
210
				G_TYPE_ENUM, step,
211
				G_TYPE_INT, count,
Manish Singh's avatar
Manish Singh committed
212
				G_TYPE_BOOLEAN, FALSE);
213 214 215 216

  /* Selection-extending version */
  gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
				"move_cursor", 3,
Manish Singh's avatar
Manish Singh committed
217
				G_TYPE_ENUM, step,
218
				G_TYPE_INT, count,
Manish Singh's avatar
Manish Singh committed
219
				G_TYPE_BOOLEAN, TRUE);
220 221
}

222
static void
Elliot Lee's avatar
Elliot Lee committed
223 224
gtk_label_class_init (GtkLabelClass *class)
{
225
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
226 227
  GtkObjectClass *object_class = GTK_OBJECT_CLASS (class);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
228
  GtkBindingSet *binding_set;
229

Manish Singh's avatar
Manish Singh committed
230
  parent_class = g_type_class_peek_parent (class);
Tim Janik's avatar
Tim Janik committed
231
  
232 233
  gobject_class->set_property = gtk_label_set_property;
  gobject_class->get_property = gtk_label_get_property;
234
  gobject_class->finalize = gtk_label_finalize;
235 236

  object_class->destroy = gtk_label_destroy;
Tim Janik's avatar
Tim Janik committed
237
  
Elliot Lee's avatar
Elliot Lee committed
238
  widget_class->size_request = gtk_label_size_request;
239
  widget_class->size_allocate = gtk_label_size_allocate;
Havoc Pennington's avatar
Havoc Pennington committed
240
  widget_class->state_changed = gtk_label_state_changed;
241
  widget_class->style_set = gtk_label_style_set;
242
  widget_class->direction_changed = gtk_label_direction_changed;
Elliot Lee's avatar
Elliot Lee committed
243
  widget_class->expose_event = gtk_label_expose;
244 245 246 247 248 249 250
  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;
251
  widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
252
  widget_class->screen_changed = gtk_label_screen_changed;
253
  widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
254
  widget_class->focus = gtk_label_focus;
255 256 257 258 259

  class->move_cursor = gtk_label_move_cursor;
  class->copy_clipboard = gtk_label_copy_clipboard;
  
  signals[MOVE_CURSOR] = 
Manish Singh's avatar
Manish Singh committed
260 261 262 263 264 265 266 267 268 269
    g_signal_new ("move_cursor",
		  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);
270 271
  
  signals[COPY_CLIPBOARD] =
Manish Singh's avatar
Manish Singh committed
272 273 274 275 276 277 278
    g_signal_new ("copy_clipboard",
		  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);
279 280
  
  signals[POPULATE_POPUP] =
Manish Singh's avatar
Manish Singh committed
281 282 283 284 285 286 287 288 289 290
    g_signal_new ("populate_popup",
		  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,
291 292
                                   PROP_LABEL,
                                   g_param_spec_string ("label",
293 294
                                                        P_("Label"),
                                                        P_("The text of the label"),
295 296 297 298 299
                                                        NULL,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
				   PROP_ATTRIBUTES,
				   g_param_spec_boxed ("attributes",
300 301
						       P_("Attributes"),
						       P_("A list of style attributes to apply to the text of the label"),
302 303 304 305 306
						       PANGO_TYPE_ATTR_LIST,
						       G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
                                   PROP_USE_MARKUP,
                                   g_param_spec_boolean ("use_markup",
307 308
							 P_("Use markup"),
							 P_("The text of the label includes XML markup. See pango_parse_markup()"),
309 310 311 312 313
                                                        FALSE,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
                                   PROP_USE_UNDERLINE,
                                   g_param_spec_boolean ("use_underline",
314 315
							 P_("Use underline"),
							 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
316 317 318 319 320 321
                                                        FALSE,
                                                        G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class,
				   PROP_JUSTIFY,
                                   g_param_spec_enum ("justify",
322 323
                                                      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"),
324 325 326 327 328 329 330
						      GTK_TYPE_JUSTIFICATION,
						      GTK_JUSTIFY_LEFT,
                                                      G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class,
                                   PROP_PATTERN,
                                   g_param_spec_string ("pattern",
331 332
                                                        P_("Pattern"),
                                                        P_("A string with _ characters in positions correspond to characters in the text to underline"),
333 334 335 336 337 338
                                                        NULL,
                                                        G_PARAM_WRITABLE));

  g_object_class_install_property (gobject_class,
                                   PROP_WRAP,
                                   g_param_spec_boolean ("wrap",
339 340
                                                        P_("Line wrap"),
                                                        P_("If set, wrap lines if the text becomes too wide"),
341
                                                        FALSE,
342 343 344 345
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
                                   PROP_SELECTABLE,
                                   g_param_spec_boolean ("selectable",
346 347
                                                        P_("Selectable"),
                                                        P_("Whether the label text can be selected with the mouse"),
348 349 350
                                                        FALSE,
                                                        G_PARAM_READWRITE));
  g_object_class_install_property (gobject_class,
351 352
                                   PROP_MNEMONIC_KEYVAL,
                                   g_param_spec_uint ("mnemonic_keyval",
353 354
						      P_("Mnemonic key"),
						      P_("The mnemonic accelerator key for this label"),
355 356 357 358
						      0,
						      G_MAXUINT,
						      GDK_VoidSymbol,
						      G_PARAM_READABLE));
359 360 361
  g_object_class_install_property (gobject_class,
                                   PROP_MNEMONIC_WIDGET,
                                   g_param_spec_object ("mnemonic_widget",
362 363
							P_("Mnemonic widget"),
							P_("The widget to be activated when the label's mnemonic "
Soren Sandmann's avatar
Soren Sandmann committed
364
							  "key is pressed"),
365 366
							GTK_TYPE_WIDGET,
							G_PARAM_READWRITE));
367

368 369 370
  g_object_class_install_property (gobject_class,
                                   PROP_CURSOR_POSITION,
                                   g_param_spec_int ("cursor_position",
371 372
                                                     P_("Cursor Position"),
                                                     P_("The current position of the insertion cursor in chars"),
373 374 375 376 377 378 379 380
                                                     0,
                                                     G_MAXINT,
                                                     0,
                                                     G_PARAM_READABLE));
  
  g_object_class_install_property (gobject_class,
                                   PROP_SELECTION_BOUND,
                                   g_param_spec_int ("selection_bound",
381 382
                                                     P_("Selection Bound"),
                                                     P_("The position of the opposite end of the selection from the cursor in chars"),
383 384 385 386 387
                                                     0,
                                                     G_MAXINT,
                                                     0,
                                                     G_PARAM_READABLE));
  
388 389 390 391 392 393 394 395 396
  g_object_class_install_property (gobject_class,
				   PROP_ELLIPSIZE,
                                   g_param_spec_enum ("ellipsize",
                                                      P_("Ellipsize"),
                                                      P_("The preferred place to ellipsize the string, if the label does not have enough room to display the entire string, if at all"),
						      PANGO_TYPE_ELLIPSIZE_MODE,
						      PANGO_ELLIPSIZE_NONE,
                                                      G_PARAM_READWRITE));
  
397 398 399 400 401 402 403 404 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
  /*
   * 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);
  
  add_move_binding (binding_set, GDK_a, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_PARAGRAPH_ENDS, -1);

  add_move_binding (binding_set, GDK_e, GDK_CONTROL_MASK,
		    GTK_MOVEMENT_PARAGRAPH_ENDS, 1);

  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,
				"copy_clipboard", 0);
Elliot Lee's avatar
Elliot Lee committed
473 474
}

475 476 477 478
static void 
gtk_label_set_property (GObject      *object,
			guint         prop_id,
			const GValue *value,
479
			GParamSpec   *pspec)
480
{
481
  GtkLabel *label;
482

483
  label = GTK_LABEL (object);
484
  
485
  switch (prop_id)
486
    {
487
    case PROP_LABEL:
488
      gtk_label_set_label (label, g_value_get_string (value));
489 490 491 492 493
      break;
    case PROP_ATTRIBUTES:
      gtk_label_set_attributes (label, g_value_get_boxed (value));
      break;
    case PROP_USE_MARKUP:
494
      gtk_label_set_use_markup (label, g_value_get_boolean (value));
495
      break;
496
    case PROP_USE_UNDERLINE:
497
      gtk_label_set_use_underline (label, g_value_get_boolean (value));
498
      break;
499 500
    case PROP_JUSTIFY:
      gtk_label_set_justify (label, g_value_get_enum (value));
501
      break;
502 503 504 505 506 507 508 509
    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));
510
      break;	  
511 512 513
    case PROP_MNEMONIC_WIDGET:
      gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
      break;
514 515 516
    case PROP_ELLIPSIZE:
      gtk_label_set_ellipsize (label, g_value_get_enum (value));
      break;
Tim Janik's avatar
Tim Janik committed
517
    default:
518
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
519
      break;
520 521 522
    }
}

523 524 525 526
static void 
gtk_label_get_property (GObject     *object,
			guint        prop_id,
			GValue      *value,
527
			GParamSpec  *pspec)
528
{
529
  GtkLabel *label;
530
  
531
  label = GTK_LABEL (object);
532
  
533
  switch (prop_id)
534
    {
535
    case PROP_LABEL:
Alexander Larsson's avatar
Alexander Larsson committed
536
      g_value_set_string (value, label->label);
537 538 539 540 541 542 543 544 545
      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);
546
      break;
547 548
    case PROP_JUSTIFY:
      g_value_set_enum (value, label->jtype);
549
      break;
550 551
    case PROP_WRAP:
      g_value_set_boolean (value, label->wrap);
552
      break;
553 554 555
    case PROP_SELECTABLE:
      g_value_set_boolean (value, gtk_label_get_selectable (label));
      break;
556 557
    case PROP_MNEMONIC_KEYVAL:
      g_value_set_uint (value, label->mnemonic_keyval);
558
      break;
559 560 561
    case PROP_MNEMONIC_WIDGET:
      g_value_set_object (value, (GObject*) label->mnemonic_widget);
      break;
562 563 564
    case PROP_CURSOR_POSITION:
      if (label->select_info)
	{
565 566
	  gint offset = g_utf8_pointer_to_offset (label->text,
						  label->text + label->select_info->selection_end);
567 568 569 570 571 572 573 574
	  g_value_set_int (value, offset);
	}
      else
	g_value_set_int (value, 0);
      break;
    case PROP_SELECTION_BOUND:
      if (label->select_info)
	{
575 576
	  gint offset = g_utf8_pointer_to_offset (label->text,
						  label->text + label->select_info->selection_anchor);
577 578 579 580 581
	  g_value_set_int (value, offset);
	}
      else
	g_value_set_int (value, 0);
      break;
582 583 584
    case PROP_ELLIPSIZE:
      g_value_set_enum (value, label->ellipsize);
      break;
585

586
    default:
587
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
588 589 590 591
      break;
    }
}

592
static void
Elliot Lee's avatar
Elliot Lee committed
593 594 595
gtk_label_init (GtkLabel *label)
{
  GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
Tim Janik's avatar
Tim Janik committed
596
  
Elliot Lee's avatar
Elliot Lee committed
597
  label->label = NULL;
598

599
  label->jtype = GTK_JUSTIFY_LEFT;
Owen Taylor's avatar
Owen Taylor committed
600
  label->wrap = FALSE;
601
  label->ellipsize = PANGO_ELLIPSIZE_NONE;
602 603 604

  label->use_underline = FALSE;
  label->use_markup = FALSE;
605
  
606
  label->mnemonic_keyval = GDK_VoidSymbol;
607
  label->layout = NULL;
608
  label->text = NULL;
609
  label->attrs = NULL;
610 611 612

  label->mnemonic_widget = NULL;
  label->mnemonic_window = NULL;
613
  
Owen Taylor's avatar
Owen Taylor committed
614
  gtk_label_set_text (label, "");
Elliot Lee's avatar
Elliot Lee committed
615 616
}

617 618 619 620
/**
 * gtk_label_new:
 * @str: The text of the label
 *
Matthias Clasen's avatar
Matthias Clasen committed
621 622
 * Creates a new label with the given text inside it. You can
 * pass %NULL to get an empty label widget.
623 624
 *
 * Return value: the new #GtkLabel
625
 **/
Elliot Lee's avatar
Elliot Lee committed
626
GtkWidget*
627
gtk_label_new (const gchar *str)
Elliot Lee's avatar
Elliot Lee committed
628 629
{
  GtkLabel *label;
630
  
Manish Singh's avatar
Manish Singh committed
631
  label = g_object_new (GTK_TYPE_LABEL, NULL);
632 633 634

  if (str && *str)
    gtk_label_set_text (label, str);
635
  
Elliot Lee's avatar
Elliot Lee committed
636 637 638
  return GTK_WIDGET (label);
}

639
/**
640 641 642 643 644 645
 * 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.
 *
646
 * If characters in @str are preceded by an underscore, they are
647 648 649 650
 * 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
651 652 653 654 655 656 657 658
 * 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.
659 660
 *
 * Return value: the new #GtkLabel
661 662 663 664 665 666
 **/
GtkWidget*
gtk_label_new_with_mnemonic (const gchar *str)
{
  GtkLabel *label;
  
Manish Singh's avatar
Manish Singh committed
667
  label = g_object_new (GTK_TYPE_LABEL, NULL);
668 669 670 671 672 673 674 675

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

static gboolean
676
gtk_label_mnemonic_activate (GtkWidget *widget,
677 678 679 680 681
			     gboolean   group_cycling)
{
  GtkWidget *parent;

  if (GTK_LABEL (widget)->mnemonic_widget)
682
    return gtk_widget_mnemonic_activate (GTK_LABEL (widget)->mnemonic_widget, group_cycling);
683

684 685
  /* Try to find the widget to activate by traversing the
   * widget's ancestry.
686 687
   */
  parent = widget->parent;
688 689

  if (parent && GTK_IS_NOTEBOOK (parent))
690
    return FALSE;
691
  
692 693 694 695
  while (parent)
    {
      if (GTK_WIDGET_CAN_FOCUS (parent) ||
	  (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
696
          (parent->parent && GTK_IS_NOTEBOOK (parent->parent)) ||
697
	  (GTK_IS_MENU_ITEM (parent)))
698
	return gtk_widget_mnemonic_activate (parent, group_cycling);
699 700 701
      parent = parent->parent;
    }

702
  /* barf if there was nothing to activate */
703
  g_warning ("Couldn't find a target for a mnemonic activation.");
704
  gdk_display_beep (gtk_widget_get_display (widget));
705 706 707 708 709
  
  return FALSE;
}

static void
710 711
gtk_label_setup_mnemonic (GtkLabel *label,
			  guint     last_key)
712 713 714
{
  GtkWidget *toplevel;

715
  if (last_key != GDK_VoidSymbol && label->mnemonic_window)
716 717 718 719 720 721
    {
      gtk_window_remove_mnemonic  (label->mnemonic_window,
				   last_key,
				   GTK_WIDGET (label));
      label->mnemonic_window = NULL;
    }
722 723 724 725 726
  
  if (label->mnemonic_keyval == GDK_VoidSymbol)
    return;
  
  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
727
  if (GTK_WIDGET_TOPLEVEL (toplevel))
728 729 730 731 732 733 734 735 736
    {
      gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
			       label->mnemonic_keyval,
			       GTK_WIDGET (label));
      label->mnemonic_window = GTK_WINDOW (toplevel);
    }
}

static void
737 738
gtk_label_hierarchy_changed (GtkWidget *widget,
			     GtkWidget *old_toplevel)
739 740
{
  GtkLabel *label = GTK_LABEL (widget);
741

742 743 744
  gtk_label_setup_mnemonic (label, label->mnemonic_keyval);
}

745 746 747 748 749 750 751
static void
gtk_label_screen_changed (GtkWidget *widget,
			  GdkScreen *old_screen)
{
  gtk_label_clear_layout (GTK_LABEL (widget));
}

752 753 754 755 756 757 758 759 760
static void
label_mnemonic_widget_weak_notify (gpointer      data,
				   GObject      *where_the_object_was)
{
  GtkLabel *label = data;

  label->mnemonic_widget = NULL;
  g_object_notify (G_OBJECT (label), "mnemonic_widget");
}
761 762 763 764 765 766

/**
 * gtk_label_set_mnemonic_widget:
 * @label: a #GtkLabel
 * @widget: the target #GtkWidget 
 *
767 768 769 770 771 772 773 774 775
 * 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.
776
 *
777
 * The target widget will be accelerated by emitting "mnemonic_activate" on it.
778 779 780 781
 * 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
782 783
gtk_label_set_mnemonic_widget (GtkLabel  *label,
			       GtkWidget *widget)
784 785
{
  g_return_if_fail (GTK_IS_LABEL (label));
786 787
  if (widget)
    g_return_if_fail (GTK_IS_WIDGET (widget));
788

789
  if (label->mnemonic_widget)
790 791 792 793 794 795
    {
      gtk_widget_remove_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
      g_object_weak_unref (G_OBJECT (label->mnemonic_widget),
			   label_mnemonic_widget_weak_notify,
			   label);
    }
796
  label->mnemonic_widget = widget;
797
  if (label->mnemonic_widget)
798 799 800 801 802 803
    {
      g_object_weak_ref (G_OBJECT (label->mnemonic_widget),
		         label_mnemonic_widget_weak_notify,
		         label);
      gtk_widget_add_mnemonic_label (label->mnemonic_widget, GTK_WIDGET (label));
    }
804 805
  
  g_object_notify (G_OBJECT (label), "mnemonic_widget");
806 807
}

808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
/**
 * gtk_label_get_mnemonic_widget:
 * @label: a #GtkLabel
 *
 * Retrieves the target of the mnemonic (keyboard shortcut) of this
 * label. See gtk_label_set_mnemonic_widget ().
 *
 * Return value: the target of the label's mnemonic, or %NULL if none
 *               has been set and the default algorithm will be used.
 **/
GtkWidget *
gtk_label_get_mnemonic_widget (GtkLabel *label)
{
  g_return_val_if_fail (GTK_IS_LABEL (label), NULL);

  return label->mnemonic_widget;
}
825 826 827

/**
 * gtk_label_get_mnemonic_keyval:
828 829
 * @label: a #GtkLabel
 *
830 831 832
 * 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
833
 *
Matthias Clasen's avatar
Matthias Clasen committed
834
 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
835 836
 **/
guint
837
gtk_label_get_mnemonic_keyval (GtkLabel *label)
838 839 840
{
  g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);

841
  return label->mnemonic_keyval;
842 843 844
}

static void
845
gtk_label_set_text_internal (GtkLabel *label,
846
			     gchar    *str)
847 848 849 850 851 852 853 854 855 856 857
{
  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
858
{
859
  g_free (label->label);
860
  
861
  label->label = str;
862

863 864
  g_object_notify (G_OBJECT (label), "label");
}
865

866 867
static void
gtk_label_set_use_markup_internal (GtkLabel *label,
868
				   gboolean  val)
869 870 871
{
  val = val != FALSE;
  if (label->use_markup != val)
872 873 874 875
    {
      g_object_notify (G_OBJECT (label), "use_markup");
      label->use_markup = val;
    }
876 877 878 879 880 881 882 883
}

static void
gtk_label_set_use_underline_internal (GtkLabel *label,
				      gboolean val)
{
  val = val != FALSE;
  if (label->use_underline != val)
884 885 886 887
    {
      g_object_notify (G_OBJECT (label), "use_underline");
      label->use_underline = val;
    }
888 889 890
}

static void
891 892
gtk_label_set_attributes_internal (GtkLabel      *label,
				   PangoAttrList *attrs)
893 894 895
{
  if (attrs)
    pango_attr_list_ref (attrs);
896
  
897 898 899
  if (label->attrs)
    pango_attr_list_unref (label->attrs);

900 901
  if (!label->use_markup && !label->use_underline)
    {
902 903
      if (attrs)
	pango_attr_list_ref (attrs);
904 905 906 907 908
      if (label->effective_attrs)
	pango_attr_list_unref (label->effective_attrs);
      label->effective_attrs = attrs;
    }

909 910 911 912 913
  label->attrs = attrs;
  g_object_notify (G_OBJECT (label), "attributes");
}


914
/* Calculates text, attrs and mnemonic_keyval from
915 916
 * label, use_underline and use_markup
 */
917 918 919 920
static void
gtk_label_recalculate (GtkLabel *label)
{
  if (label->use_markup)
921
    set_markup (label, label->label, label->use_underline);
922 923 924 925 926 927 928
  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));
929 930 931 932 933
	  if (label->attrs)
	    pango_attr_list_ref (label->attrs);
	  if (label->effective_attrs)
	    pango_attr_list_unref (label->effective_attrs);
	  label->effective_attrs = label->attrs;
934 935 936 937
	}
    }

  if (!label->use_underline)
938 939
    {
      guint keyval = label->mnemonic_keyval;
940

941 942 943
      label->mnemonic_keyval = GDK_VoidSymbol;
      gtk_label_setup_mnemonic (label, keyval);
    }
944 945

  gtk_label_clear_layout (label);  
946
  gtk_widget_queue_resize (GTK_WIDGET (label));
947 948
}

949 950
/**
 * gtk_label_set_text:
Havoc Pennington's avatar
Havoc Pennington committed
951
 * @label: a #GtkLabel
Matthias Clasen's avatar
Matthias Clasen committed
952
 * @str: The text you want to set.
953
 *
Matthias Clasen's avatar
Matthias Clasen committed
954 955
 * Sets the text within the #GtkLabel widget.  It overwrites any text that
 * was there before.  
956 957 958
 *
 * This will also clear any previously set mnemonic accelerators.
 **/
959
void
960 961
gtk_label_set_text (GtkLabel    *label,
		    const gchar *str)
962 963
{
  g_return_if_fail (GTK_IS_LABEL (label));
964
  
965 966
  g_object_freeze_notify (G_OBJECT (label));

967 968 969 970 971
  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);
972 973

  g_object_thaw_notify (G_OBJECT (label));
974 975
}

976 977 978 979 980 981
/**
 * gtk_label_set_attributes:
 * @label: a #GtkLabel
 * @attrs: a #PangoAttrList
 * 
 * Sets a #PangoAttrList; the attributes in the list are applied to the
982
 * label text. The attributes set with this function will be ignored
Soren Sandmann's avatar
Soren Sandmann committed
983 984
 * if the "use_underline" property or the "use_markup" property
 * is %TRUE.
985 986 987 988 989 990 991
 **/
void
gtk_label_set_attributes (GtkLabel         *label,
                          PangoAttrList    *attrs)
{
  g_return_if_fail (GTK_IS_LABEL (label));

992
  gtk_label_set_attributes_internal (label, attrs);
993
  
994 995
  gtk_label_clear_layout (label);  
  gtk_widget_queue_resize (GTK_WIDGET (label));
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
/**
 * gtk_label_get_attributes:
 * @label: a #GtkLabel
 *
 * Gets the attribute list that was set on the label using
 * gtk_label_set_attributes(), if any. This function does
 * not reflect attributes that come from the labels markup
 * (see gtk_label_set_markup()). If you want to get the
 * effective attributes for the label, use
 * pango_layout_get_attribute (gtk_label_get_layout (label)).
 *
 * Return value: the attribute list, or %NULL if none was set.
 **/
PangoAttrList *
gtk_label_get_attributes (GtkLabel *label)
{
  g_return_val_if_fail (GTK_IS_LABEL (label), NULL);

  return label->attrs;
}

/**
 * gtk_label_set_label:
 * @label: a #GtkLabel
 * @str: the new text to set for the label
 *
 * Sets the text of the label. The label is interpreted as
 * including embedded underlines and/or Pango markup depending
 * on the values of label->use_underline and label->use_markup.
 **/
void
gtk_label_set_label (GtkLabel    *label,
		     const gchar *str)
{
  guint last_keyval;

  g_return_if_fail (GTK_IS_LABEL (label));
  g_return_if_fail (str != NULL);

  last_keyval = label->mnemonic_keyval;

  gtk_label_set_label_internal (label, g_strdup (str));
  gtk_label_recalculate (label);
  if (last_keyval != label->mnemonic_keyval)
    gtk_label_setup_mnemonic (label, last_keyval);
}

/**
 * gtk_label_get_label:
 * @label: a #GtkLabel
 *
 * Fetches the text from a label widget including any embedded
 * underlines indicating mnemonics and Pango markup. (See
 * gtk_label_get_text ()).
 *
 * Return value: the text of the label widget. This string is
 *   owned by the widget and must not be modified or freed.
 **/
G_CONST_RETURN gchar *
gtk_label_get_label (GtkLabel *label)
{
  g_return_val_if_fail (GTK_IS_LABEL (label), NULL);

  return label->label;
}

1064
static void
1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084
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
1085
      return;
1086 1087 1088
    }

  if (text)
1089
    gtk_label_set_text_internal (label, text);
1090 1091 1092

  if (attrs)
    {
1093 1094 1095
      if (label->effective_attrs)
	pango_attr_list_unref (label->effective_attrs);
      label->effective_attrs = attrs;
1096 1097 1098
    }

  if (accel_char != 0)
1099
    label->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
1100
  else
1101
    label->mnemonic_keyval = GDK_VoidSymbol;
1102 1103 1104 1105 1106 1107 1108
}

/**
 * gtk_label_set_markup:
 * @label: a #GtkLabel
 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
 * 
Havoc Pennington's avatar
Havoc Pennington committed
1109
 * Parses @str which is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>,
1110 1111 1112 1113 1114 1115 1116 1117
 * 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));

1118 1119 1120 1121 1122
  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);
1123 1124 1125
}

/**
1126
 * gtk_label_set_markup_with_mnemonic:
1127 1128 1129
 * @label: a #GtkLabel
 * @str: a markup string (see <link linkend="PangoMarkupFormat">Pango markup format</link>)
 * 
Havoc Pennington's avatar
Havoc Pennington committed
1130
 * Parses @str which is marked up with the <link linkend="PangoMarkupFormat">Pango text markup language</link>,
1131 1132
 * 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
1133
 * indicating that they represent a keyboard accelerator called a mnemonic.
1134
 *
1135
 * The mnemonic key can be used to activate another widget, chosen automatically,
1136
 * or explicitly using gtk_label_set_mnemonic_widget().
1137
 **/
1138 1139 1140
void
gtk_label_set_markup_with_mnemonic (GtkLabel    *label,
				    const gchar *str)
1141
{
1142 1143
  guint last_keyval;
  g_return_if_fail (GTK_IS_LABEL (label));
1144

1145
  last_keyval = label->mnemonic_keyval;
1146 1147 1148 1149 1150
  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);
1151
  gtk_label_setup_mnemonic (label, last_keyval);
1152 1153
}

1154 1155 1156 1157
/**
 * gtk_label_get_text:
 * @label: a #GtkLabel
 * 
1158 1159 1160
 * Fetches the text from a label widget, as displayed on the
 * screen. This does not include any embedded underlines
 * indicating mnemonics or Pango markup. (See gtk_label_get_label())
1161
 * 
1162
 * Return value: the text in the label widget. This is the internal
1163
 *   string used by the label, and must not be modified.
1164
 **/
1165
G_CONST_RETURN gchar *
1166 1167
gtk_label_get_text (GtkLabel *label)
{
1168
  g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1169

1170 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 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220
  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);
1221 1222 1223 1224

  if (label->effective_attrs)
    pango_attr_list_unref (label->effective_attrs);
  label->effective_attrs = attrs;
1225 1226
}

1227 1228 1229 1230 1231 1232
void
gtk_label_set_pattern (GtkLabel	   *label,
		       const gchar *pattern)
{
  g_return_if_fail (GTK_IS_LABEL (label));
  
1233
  gtk_label_set_pattern_internal (label, pattern);
1234

1235
  gtk_label_clear_layout (label);  
1236
  gtk_widget_queue_resize (GTK_WIDGET (label));
Elliot Lee's avatar
Elliot Lee committed
1237 1238
}

1239

1240 1241 1242 1243 1244 1245
/**
 * gtk_label_set_justify:
 * @label: a #GtkLabel
 * @jtype: a #GtkJustification
 *
 * Sets the alignment of the lines in the text of the label relative to
1246
 * each other.  %GTK_JUSTIFY_LEFT is the default value when the
Havoc Pennington's avatar
Havoc Pennington committed
1247 1248 1249 1250
 * widget is first created with gtk_label_new(). If you instead want
 * to set the alignment of the label as a whole, use
 * gtk_misc_set_alignment() instead. gtk_label_set_justify() has no
 * effect on labels containing only a single line.
1251
 **/
Elliot Lee's avatar
Elliot Lee committed
1252
void
1253 1254
gtk_label_set_justify (GtkLabel        *label,
		       GtkJustification jtype)
Elliot Lee's avatar
Elliot Lee committed
1255 1256
{
  g_return_if_fail (GTK_IS_LABEL (label));
1257
  g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
1258
  
Elliot Lee's avatar
Elliot Lee committed
1259 1260 1261
  if ((GtkJustification) label->jtype != jtype)
    {
      label->jtype = jtype;
1262

1263 1264
      /* No real need to be this drastic, but easier than duplicating the code */
      gtk_label_clear_layout (label);
1265
      
1266
      g_object_notify (G_OBJECT (label), "justify");
1267
      gtk_widget_queue_resize (GTK_WIDGET (label));
Elliot Lee's avatar
Elliot Lee committed
1268 1269 1270
    }
}

1271 1272 1273 1274
/**
 * gtk_label_get_justify:
 * @label: a #GtkLabel
 *
Matthias Clasen's avatar
Matthias Clasen committed
1275
 * Returns the justification of the label. See gtk_label_set_justify ().
1276
 *
Matthias Clasen's avatar
Matthias Clasen committed
1277
 * Return value: #GtkJustification
1278 1279 1280 1281 1282 1283 1284 1285 1286