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

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

54 55 56 57 58 59
enum {
  MOVE_CURSOR,
  COPY_CLIPBOARD,
  POPULATE_POPUP,
  LAST_SIGNAL
};
60

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

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

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

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

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 138 139
static void gtk_label_create_window       (GtkLabel *label);
static void gtk_label_destroy_window      (GtkLabel *label);
static void gtk_label_clear_layout        (GtkLabel *label);
140
static void gtk_label_ensure_layout       (GtkLabel *label);
141 142 143 144
static void gtk_label_select_region_index (GtkLabel *label,
                                           gint      anchor_index,
                                           gint      end_index);

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

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

167 168
static GtkMiscClass *parent_class = NULL;

Elliot Lee's avatar
Elliot Lee committed
169

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

190
      label_type = g_type_register_static (GTK_TYPE_MISC, "GtkLabel", &label_info, 0);
Elliot Lee's avatar
Elliot Lee committed
191
    }
Tim Janik's avatar
Tim Janik committed
192
  
Elliot Lee's avatar
Elliot Lee committed
193 194 195
  return label_type;
}

196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
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,
				GTK_TYPE_ENUM, step,
				G_TYPE_INT, count,
                                G_TYPE_BOOLEAN, FALSE);

  /* Selection-extending version */
  gtk_binding_entry_add_signal (binding_set, keyval, modmask | GDK_SHIFT_MASK,
				"move_cursor", 3,
				GTK_TYPE_ENUM, step,
				G_TYPE_INT, count,
                                G_TYPE_BOOLEAN, TRUE);
}

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

227
  parent_class = gtk_type_class (GTK_TYPE_MISC);
Tim Janik's avatar
Tim Janik committed
228
  
229 230
  gobject_class->set_property = gtk_label_set_property;
  gobject_class->get_property = gtk_label_get_property;
231
  gobject_class->finalize = gtk_label_finalize;
232 233

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

  class->move_cursor = gtk_label_move_cursor;
  class->copy_clipboard = gtk_label_copy_clipboard;
  
  signals[MOVE_CURSOR] = 
      gtk_signal_new ("move_cursor",
                      GTK_RUN_LAST | GTK_RUN_ACTION,
                      GTK_CLASS_TYPE (object_class),
                      GTK_SIGNAL_OFFSET (GtkLabelClass, move_cursor),
260
                      _gtk_marshal_VOID__ENUM_INT_BOOLEAN,
261 262 263 264 265 266 267
                      GTK_TYPE_NONE, 3, GTK_TYPE_MOVEMENT_STEP, GTK_TYPE_INT, GTK_TYPE_BOOL);
  
  signals[COPY_CLIPBOARD] =
    gtk_signal_new ("copy_clipboard",
                    GTK_RUN_LAST | GTK_RUN_ACTION,
                    GTK_CLASS_TYPE (object_class),
                    GTK_SIGNAL_OFFSET (GtkLabelClass, copy_clipboard),
268
                    _gtk_marshal_VOID__VOID,
269 270 271 272 273 274 275
                    GTK_TYPE_NONE, 0);
  
  signals[POPULATE_POPUP] =
    gtk_signal_new ("populate_popup",
		    GTK_RUN_LAST,
		    GTK_CLASS_TYPE (object_class),
		    GTK_SIGNAL_OFFSET (GtkLabelClass, populate_popup),
276
		    _gtk_marshal_VOID__OBJECT,
277
		    GTK_TYPE_NONE, 1, GTK_TYPE_MENU);
278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303

  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"),
304
							 _("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
                                                        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
321
                                                        _("A string with _ characters in positions correspond to characters in the text to underline."),
322 323 324 325 326 327 328 329
                                                        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."),
330
                                                        FALSE,
331 332 333 334 335 336 337 338 339
                                                        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,
340 341
                                   PROP_MNEMONIC_KEYVAL,
                                   g_param_spec_uint ("mnemonic_keyval",
342
						      _("Mnemonic key"),
343
						      _("The mnemonic accelerator key for this label."),
344 345 346 347
						      0,
						      G_MAXUINT,
						      GDK_VoidSymbol,
						      G_PARAM_READABLE));
348 349 350 351 352 353 354 355
  g_object_class_install_property (gobject_class,
                                   PROP_MNEMONIC_WIDGET,
                                   g_param_spec_object ("mnemonic_widget",
							_("Mnemonic widget"),
							_("The widget to be activated when the label's mnemonic "
							  "key is pressed."),
							GTK_TYPE_WIDGET,
							G_PARAM_READWRITE));
356

357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
  g_object_class_install_property (gobject_class,
                                   PROP_CURSOR_POSITION,
                                   g_param_spec_int ("cursor_position",
                                                     _("Cursor Position"),
                                                     _("The current position of the insertion cursor in chars."),
                                                     0,
                                                     G_MAXINT,
                                                     0,
                                                     G_PARAM_READABLE));
  
  g_object_class_install_property (gobject_class,
                                   PROP_SELECTION_BOUND,
                                   g_param_spec_int ("selection_bound",
                                                     _("Selection Bound"),
                                                     _("The position of the opposite end of the selection from the cursor in chars."),
                                                     0,
                                                     G_MAXINT,
                                                     0,
                                                     G_PARAM_READABLE));
  
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 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
  gtk_widget_class_install_style_property (widget_class,
					   g_param_spec_boxed ("cursor_color",
							       _("Cursor color"),
							       _("Color with which to draw insertion cursor"),
							       GDK_TYPE_COLOR,
							       G_PARAM_READABLE));
  
  /*
   * 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
460 461
}

462 463 464 465
static void 
gtk_label_set_property (GObject      *object,
			guint         prop_id,
			const GValue *value,
466
			GParamSpec   *pspec)
467
{
468
  GtkLabel *label;
469 470
  guint last_keyval;

471
  label = GTK_LABEL (object);
472
  last_keyval = label->mnemonic_keyval;
473
  
474
  switch (prop_id)
475
    {
476
    case PROP_LABEL:
477
      gtk_label_set_label (label, g_value_get_string (value));
478 479 480 481 482
      break;
    case PROP_ATTRIBUTES:
      gtk_label_set_attributes (label, g_value_get_boxed (value));
      break;
    case PROP_USE_MARKUP:
483
      gtk_label_set_use_markup (label, g_value_get_boolean (value));
484
      break;
485
    case PROP_USE_UNDERLINE:
486
      gtk_label_set_use_underline (label, g_value_get_boolean (value));
487
      break;
488 489
    case PROP_JUSTIFY:
      gtk_label_set_justify (label, g_value_get_enum (value));
490
      break;
491 492 493 494 495 496 497 498
    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));
499
      break;	  
500 501 502
    case PROP_MNEMONIC_WIDGET:
      gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
      break;
Tim Janik's avatar
Tim Janik committed
503
    default:
504
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
505
      break;
506 507 508
    }
}

509 510 511 512
static void 
gtk_label_get_property (GObject     *object,
			guint        prop_id,
			GValue      *value,
513
			GParamSpec  *pspec)
514
{
515
  GtkLabel *label;
516
  
517
  label = GTK_LABEL (object);
518
  
519
  switch (prop_id)
520
    {
521
    case PROP_LABEL:
Alexander Larsson's avatar
Alexander Larsson committed
522
      g_value_set_string (value, label->label);
523 524 525 526 527 528 529 530 531
      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);
532
      break;
533 534
    case PROP_JUSTIFY:
      g_value_set_enum (value, label->jtype);
535
      break;
536 537
    case PROP_WRAP:
      g_value_set_boolean (value, label->wrap);
538
      break;
539 540 541
    case PROP_SELECTABLE:
      g_value_set_boolean (value, gtk_label_get_selectable (label));
      break;
542 543
    case PROP_MNEMONIC_KEYVAL:
      g_value_set_uint (value, label->mnemonic_keyval);
544
      break;
545 546 547
    case PROP_MNEMONIC_WIDGET:
      g_value_set_object (value, (GObject*) label->mnemonic_widget);
      break;
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567
    case PROP_CURSOR_POSITION:
      if (label->select_info)
	{
	  gint offset = g_utf8_pointer_to_offset (label->label,
						  label->label + label->select_info->selection_end);
	  g_value_set_int (value, offset);
	}
      else
	g_value_set_int (value, 0);
      break;
    case PROP_SELECTION_BOUND:
      if (label->select_info)
	{
	  gint offset = g_utf8_pointer_to_offset (label->label,
						  label->label + label->select_info->selection_anchor);
	  g_value_set_int (value, offset);
	}
      else
	g_value_set_int (value, 0);
      break;
568

569
    default:
570
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
571 572 573 574
      break;
    }
}

575
static void
Elliot Lee's avatar
Elliot Lee committed
576 577 578
gtk_label_init (GtkLabel *label)
{
  GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
Tim Janik's avatar
Tim Janik committed
579
  
Elliot Lee's avatar
Elliot Lee committed
580
  label->label = NULL;
581

582
  label->jtype = GTK_JUSTIFY_LEFT;
Owen Taylor's avatar
Owen Taylor committed
583
  label->wrap = FALSE;
584 585 586

  label->use_underline = FALSE;
  label->use_markup = FALSE;
587
  
588
  label->mnemonic_keyval = GDK_VoidSymbol;
589
  label->layout = NULL;
590
  label->text = NULL;
591
  label->attrs = NULL;
592 593 594

  label->mnemonic_widget = NULL;
  label->mnemonic_window = NULL;
595
  
Owen Taylor's avatar
Owen Taylor committed
596
  gtk_label_set_text (label, "");
Elliot Lee's avatar
Elliot Lee committed
597 598
}

599 600 601 602 603
/**
 * gtk_label_new:
 * @str: The text of the label
 *
 * Creates a new #GtkLabel, containing the text in @str.
604 605
 *
 * Return value: the new #GtkLabel
606
 **/
Elliot Lee's avatar
Elliot Lee committed
607
GtkWidget*
608
gtk_label_new (const gchar *str)
Elliot Lee's avatar
Elliot Lee committed
609 610
{
  GtkLabel *label;
611
  
612 613 614 615
  label = gtk_type_new (GTK_TYPE_LABEL);

  if (str && *str)
    gtk_label_set_text (label, str);
616
  
Elliot Lee's avatar
Elliot Lee committed
617 618 619
  return GTK_WIDGET (label);
}

620
/**
621 622 623 624 625 626
 * 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.
 *
627 628 629 630 631 632 633 634 635 636 637 638
 * If characters in @str are preceded by an underscore, they are
 * underlined indicating that they represent a keyboard accelerator
 * called a mnemonic.  The mnemonic key can be used to activate
 * another widget, chosen automatically, or explicitly using
 * gtk_label_set_mnemonic_widget().
 * 
 * If gtk_label_set_mnemonic_widget()
 * is not called, then the first activatable ancestor of the #GtkLabel
 * will be chosen as the mnemonic widget. For instance, if the
 * label is inside a button or menu item, the button or menu item will
 * automatically become the mnemonic widget and be activated by
 * the mnemonic.
639 640
 *
 * Return value: the new #GtkLabel
641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
 **/
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
656
gtk_label_mnemonic_activate (GtkWidget *widget,
657 658 659 660 661
			     gboolean   group_cycling)
{
  GtkWidget *parent;

  if (GTK_LABEL (widget)->mnemonic_widget)
662
    return gtk_widget_mnemonic_activate (GTK_LABEL (widget)->mnemonic_widget, group_cycling);
663

664 665
  /* Try to find the widget to activate by traversing the
   * widget's ancestry.
666 667 668 669 670 671
   */
  parent = widget->parent;
  while (parent)
    {
      if (GTK_WIDGET_CAN_FOCUS (parent) ||
	  (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
672
          (parent->parent && GTK_IS_NOTEBOOK (parent->parent)) ||
673
	  (GTK_IS_MENU_ITEM (parent)))
674
	return gtk_widget_mnemonic_activate (parent, group_cycling);
675 676 677
      parent = parent->parent;
    }

678
  /* barf if there was nothing to activate */
679 680 681 682 683 684 685
  g_warning ("Couldn't find a target for a mnemonic activation.");
  gdk_beep ();
  
  return FALSE;
}

static void
686 687
gtk_label_setup_mnemonic (GtkLabel *label,
			  guint     last_key)
688 689 690
{
  GtkWidget *toplevel;

691
  if (last_key != GDK_VoidSymbol && label->mnemonic_window)
692 693 694 695 696 697
    {
      gtk_window_remove_mnemonic  (label->mnemonic_window,
				   last_key,
				   GTK_WIDGET (label));
      label->mnemonic_window = NULL;
    }
698 699 700 701 702
  
  if (label->mnemonic_keyval == GDK_VoidSymbol)
    return;
  
  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (label));
703
  if (GTK_WIDGET_TOPLEVEL (toplevel))
704 705 706 707 708 709 710 711 712
    {
      gtk_window_add_mnemonic (GTK_WINDOW (toplevel),
			       label->mnemonic_keyval,
			       GTK_WIDGET (label));
      label->mnemonic_window = GTK_WINDOW (toplevel);
    }
}

static void
713 714
gtk_label_hierarchy_changed (GtkWidget *widget,
			     GtkWidget *old_toplevel)
715 716 717 718 719 720 721 722 723 724 725 726
{
  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 
 *
727 728 729 730 731 732 733 734 735
 * 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.
736
 *
737
 * The target widget will be accelerated by emitting "mnemonic_activate" on it.
738 739 740 741
 * 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
742 743
gtk_label_set_mnemonic_widget (GtkLabel  *label,
			       GtkWidget *widget)
744 745
{
  g_return_if_fail (GTK_IS_LABEL (label));
746 747
  if (widget)
    g_return_if_fail (GTK_IS_WIDGET (widget));
748

749 750
  if (label->mnemonic_widget)
    gtk_widget_unref (label->mnemonic_widget);
751
  label->mnemonic_widget = widget;
752 753
  if (label->mnemonic_widget)
    gtk_widget_ref (label->mnemonic_widget);
754 755
}

756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772
/**
 * 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;
}
773 774 775

/**
 * gtk_label_get_mnemonic_keyval:
776 777
 * @label: a #GtkLabel
 *
778 779 780
 * 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
781
 *
Matthias Clasen's avatar
Matthias Clasen committed
782
 * Returns: GDK keyval usable for accelerators, or #GDK_VoidSymbol
783 784
 **/
guint
785
gtk_label_get_mnemonic_keyval (GtkLabel *label)
786 787 788
{
  g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);

789
  return label->mnemonic_keyval;
790 791 792
}

static void
793
gtk_label_set_text_internal (GtkLabel *label,
794
			     gchar    *str)
795 796 797 798 799 800 801 802 803 804 805
{
  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
806
{
807
  g_free (label->label);
808
  
809
  label->label = str;
810

811 812
  g_object_notify (G_OBJECT (label), "label");
}
813

814 815
static void
gtk_label_set_use_markup_internal (GtkLabel *label,
816
				   gboolean  val)
817 818 819
{
  val = val != FALSE;
  if (label->use_markup != val)
820 821 822 823
    {
      g_object_notify (G_OBJECT (label), "use_markup");
      label->use_markup = val;
    }
824 825 826 827 828 829 830 831
}

static void
gtk_label_set_use_underline_internal (GtkLabel *label,
				      gboolean val)
{
  val = val != FALSE;
  if (label->use_underline != val)
832 833 834 835
    {
      g_object_notify (G_OBJECT (label), "use_underline");
      label->use_underline = val;
    }
836 837 838
}

static void
839 840
gtk_label_set_attributes_internal (GtkLabel      *label,
				   PangoAttrList *attrs)
841 842 843
{
  if (attrs)
    pango_attr_list_ref (attrs);
844
  
845 846 847
  if (label->attrs)
    pango_attr_list_unref (label->attrs);

848 849 850 851 852 853 854 855
  if (!label->use_markup && !label->use_underline)
    {
      pango_attr_list_ref (attrs);
      if (label->effective_attrs)
	pango_attr_list_unref (label->effective_attrs);
      label->effective_attrs = attrs;
    }

856 857 858 859 860
  label->attrs = attrs;
  g_object_notify (G_OBJECT (label), "attributes");
}


861
/* Calculates text, attrs and mnemonic_keyval from
862 863
 * label, use_underline and use_markup
 */
864 865 866 867
static void
gtk_label_recalculate (GtkLabel *label)
{
  if (label->use_markup)
868
    set_markup (label, label->label, label->use_underline);
869 870 871 872 873 874 875
  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));
876 877 878 879 880
	  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;
881 882 883 884
	}
    }

  if (!label->use_underline)
885 886
    {
      guint keyval = label->mnemonic_keyval;
887

888 889 890
      label->mnemonic_keyval = GDK_VoidSymbol;
      gtk_label_setup_mnemonic (label, keyval);
    }
891 892

  gtk_label_clear_layout (label);  
893
  gtk_widget_queue_resize (GTK_WIDGET (label));
894 895
}

896 897
/**
 * gtk_label_set_text:
Havoc Pennington's avatar
Havoc Pennington committed
898 899
 * @label: a #GtkLabel
 * @str: a string
900 901 902 903 904
 *
 * Sets the text of the label to @str.
 *
 * This will also clear any previously set mnemonic accelerators.
 **/
905
void
906 907
gtk_label_set_text (GtkLabel    *label,
		    const gchar *str)
908 909
{
  g_return_if_fail (GTK_IS_LABEL (label));
910
  
911 912 913 914 915
  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);
916 917
}

918 919 920 921 922 923
/**
 * gtk_label_set_attributes:
 * @label: a #GtkLabel
 * @attrs: a #PangoAttrList
 * 
 * Sets a #PangoAttrList; the attributes in the list are applied to the
924 925
 * label text. The attributes set with this function will be ignored
 * if label->use_underline or label->use_markup is %TRUE.
926 927 928 929 930 931 932
 **/
void
gtk_label_set_attributes (GtkLabel         *label,
                          PangoAttrList    *attrs)
{
  g_return_if_fail (GTK_IS_LABEL (label));

933
  gtk_label_set_attributes_internal (label, attrs);
934
  
935 936
  gtk_label_clear_layout (label);  
  gtk_widget_queue_resize (GTK_WIDGET (label));
937 938
}

939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004
/**
 * 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;
}

1005
static void
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
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
1026
      return;
1027 1028 1029
    }

  if (text)
1030
    gtk_label_set_text_internal (label, text);
1031 1032 1033

  if (attrs)
    {
1034 1035 1036
      if (label->effective_attrs)
	pango_attr_list_unref (label->effective_attrs);
      label->effective_attrs = attrs;
1037 1038 1039
    }

  if (accel_char != 0)
1040
    label->mnemonic_keyval = gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
1041
  else
1042
    label->mnemonic_keyval = GDK_VoidSymbol;
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058
}

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

1059 1060 1061 1062 1063
  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);
1064 1065 1066
}

/**
1067
 * gtk_label_set_markup_with_mnemonic:
1068 1069 1070 1071 1072 1073
 * @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
1074
 * indicating that they represent a keyboard accelerator called a mnemonic.
1075
 *
1076
 * The mnemonic key can be used to activate another widget, chosen automatically,
1077
 * or explicitly using gtk_label_set_mnemonic_widget().
1078
 **/
1079 1080 1081
void
gtk_label_set_markup_with_mnemonic (GtkLabel    *label,
				    const gchar *str)
1082
{
1083 1084
  guint last_keyval;
  g_return_if_fail (GTK_IS_LABEL (label));
1085

1086
  last_keyval = label->mnemonic_keyval;
1087 1088 1089 1090 1091
  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);
1092
  gtk_label_setup_mnemonic (label, last_keyval);
1093 1094
}

1095 1096 1097 1098
/**
 * gtk_label_get_text:
 * @label: a #GtkLabel
 * 
1099 1100 1101
 * 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())
1102
 * 
1103
 * Return value: the text in the label widget. This is the internal
1104
 *   string used by the label, and must not be modified.
1105
 **/
1106
G_CONST_RETURN gchar *
1107 1108
gtk_label_get_text (GtkLabel *label)
{
1109
  g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
1110

1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161
  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);
1162 1163 1164 1165

  if (label->effective_attrs)
    pango_attr_list_unref (label->effective_attrs);
  label->effective_attrs = attrs;
1166 1167
}

1168 1169 1170 1171 1172 1173
void
gtk_label_set_pattern (GtkLabel	   *label,
		       const gchar *pattern)
{
  g_return_if_fail (GTK_IS_LABEL (label));
  
1174
  gtk_label_set_pattern_internal (label, pattern);
1175

1176
  gtk_label_clear_layout (label);  
1177
  gtk_widget_queue_resize (GTK_WIDGET (label));
Elliot Lee's avatar
Elliot Lee committed
1178 1179
}

1180

1181 1182 1183 1184 1185 1186 1187 1188
/**
 * gtk_label_set_justify:
 * @label: a #GtkLabel
 * @jtype: a #GtkJustification
 *
 * Sets the alignment of the lines in the text of the label relative to
 * each other.
 **/
Elliot Lee's avatar
Elliot Lee committed
1189
void
1190 1191
gtk_label_set_justify (GtkLabel        *label,
		       GtkJustification jtype)
Elliot Lee's avatar
Elliot Lee committed
1192 1193
{
  g_return_if_fail (GTK_IS_LABEL (label));
1194
  g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
1195
  
Elliot Lee's avatar
Elliot Lee committed
1196 1197 1198
  if ((GtkJustification) label->jtype != jtype)
    {
      label->jtype = jtype;
1199

1200 1201
      /* No real need to be this drastic, but easier than duplicating the code */
      gtk_label_clear_layout (label);
1202
      
1203
      g_object_notify (G_OBJECT (label), "justify");
1204
      gtk_widget_queue_resize (GTK_WIDGET (label));
Elliot Lee's avatar
Elliot Lee committed
1205 1206 1207
    }
}

1208 1209 1210 1211
/**
 * gtk_label_get_justify:
 * @label: a #GtkLabel
 *
Matthias Clasen's avatar
Matthias Clasen committed
1212
 * Returns the justification of the label. See gtk_label_set_justify ().
1213
 *
Matthias Clasen's avatar
Matthias Clasen committed