gtklabel.c 111 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 "gtkdnd.h"
31
#include "gtkmain.h"
32
#include "gtkmarshalers.h"
33
#include "gtkwindow.h"
34
#include "gdk/gdkkeysyms.h"
35
#include "gtkclipboard.h"
36
#include <pango/pango.h>
37
#include "gtkimagemenuitem.h"
38
#include "gtkintl.h"
39
#include "gtkseparatormenuitem.h"
40
#include "gtktextutil.h"
41 42
#include "gtkmenuitem.h"
#include "gtknotebook.h"
43
#include "gtkstock.h"
44
#include "gtkbindings.h"
45
#include "gtkprivate.h"
46
#include "gtkalias.h"
47

48 49 50 51 52
#define GTK_LABEL_GET_PRIVATE(obj) (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_LABEL, GtkLabelPrivate))

typedef struct
{
  gint width_chars;
53
  gint max_width_chars;
54 55 56
}
GtkLabelPrivate;

57 58 59 60 61
struct _GtkLabelSelectionInfo
{
  GdkWindow *window;
  gint selection_anchor;
  gint selection_end;
62
  GtkWidget *popup_menu;
63 64 65 66 67
  
  gint drag_start_x;
  gint drag_start_y;

  guint in_drag : 1;
68
};
Elliot Lee's avatar
Elliot Lee committed
69

70 71 72 73 74 75
enum {
  MOVE_CURSOR,
  COPY_CLIPBOARD,
  POPULATE_POPUP,
  LAST_SIGNAL
};
76

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

98 99
static guint signals[LAST_SIGNAL] = { 0 };

100 101
static void gtk_label_class_init        (GtkLabelClass    *klass);
static void gtk_label_init              (GtkLabel         *label);
102 103 104
static void gtk_label_set_property      (GObject          *object,
					 guint             prop_id,
					 const GValue     *value,
105
					 GParamSpec       *pspec);
106 107 108
static void gtk_label_get_property      (GObject          *object,
					 guint             prop_id,
					 GValue           *value,
109
					 GParamSpec       *pspec);
110
static void gtk_label_destroy           (GtkObject        *object);
111 112 113
static void gtk_label_finalize          (GObject          *object);
static void gtk_label_size_request      (GtkWidget        *widget,
					 GtkRequisition   *requisition);
114 115
static void gtk_label_size_allocate     (GtkWidget        *widget,
                                         GtkAllocation    *allocation);
Havoc Pennington's avatar
Havoc Pennington committed
116 117
static void gtk_label_state_changed     (GtkWidget        *widget,
                                         GtkStateType      state);
118 119 120 121 122 123
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
124

125 126 127 128
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
129 130 131 132 133 134 135

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);
136
static void     gtk_label_grab_focus        (GtkWidget        *widget);
137

138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156

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);
157 158
static void gtk_label_hierarchy_changed          (GtkWidget     *widget,
						  GtkWidget     *old_toplevel);
159 160
static void gtk_label_screen_changed             (GtkWidget     *widget,
						  GdkScreen     *old_screen);
161

162 163 164
static void gtk_label_create_window       (GtkLabel *label);
static void gtk_label_destroy_window      (GtkLabel *label);
static void gtk_label_clear_layout        (GtkLabel *label);
165
static void gtk_label_ensure_layout       (GtkLabel *label);
166 167 168 169
static void gtk_label_select_region_index (GtkLabel *label,
                                           gint      anchor_index,
                                           gint      end_index);

170 171 172 173
static gboolean gtk_label_mnemonic_activate (GtkWidget         *widget,
					     gboolean           group_cycling);
static void     gtk_label_setup_mnemonic    (GtkLabel          *label,
					     guint              last_key);
174 175 176 177 178 179
static void     gtk_label_drag_data_get     (GtkWidget         *widget,
					     GdkDragContext    *context,
					     GtkSelectionData  *selection_data,
					     guint              info,
					     guint              time);

180 181 182 183 184 185 186 187 188 189 190 191 192 193 194

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

196
static GtkMiscClass *parent_class = NULL;
197
static GQuark quark_angle = 0;
Elliot Lee's avatar
Elliot Lee committed
198

Manish Singh's avatar
Manish Singh committed
199
GType
200
gtk_label_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
201
{
Manish Singh's avatar
Manish Singh committed
202
  static GType label_type = 0;
Tim Janik's avatar
Tim Janik committed
203
  
Elliot Lee's avatar
Elliot Lee committed
204 205
  if (!label_type)
    {
206
      static const GTypeInfo label_info =
Elliot Lee's avatar
Elliot Lee committed
207 208
      {
	sizeof (GtkLabelClass),
209 210 211 212 213 214
	NULL,           /* base_init */
	NULL,           /* base_finalize */
	(GClassInitFunc) gtk_label_class_init,
	NULL,           /* class_finalize */
	NULL,           /* class_data */
	sizeof (GtkLabel),
215
	0,             /* n_preallocs */
216
	(GInstanceInitFunc) gtk_label_init,
Elliot Lee's avatar
Elliot Lee committed
217
      };
218

Matthias Clasen's avatar
Matthias Clasen committed
219
      label_type = g_type_register_static (GTK_TYPE_MISC, I_("GtkLabel"),
Manish Singh's avatar
Manish Singh committed
220
					   &label_info, 0);
Elliot Lee's avatar
Elliot Lee committed
221
    }
Tim Janik's avatar
Tim Janik committed
222
  
Elliot Lee's avatar
Elliot Lee committed
223 224 225
  return label_type;
}

226 227 228 229 230 231 232 233 234 235 236
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
237
				G_TYPE_ENUM, step,
238
				G_TYPE_INT, count,
Manish Singh's avatar
Manish Singh committed
239
				G_TYPE_BOOLEAN, FALSE);
240 241 242 243

  /* 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
244
				G_TYPE_ENUM, step,
245
				G_TYPE_INT, count,
Manish Singh's avatar
Manish Singh committed
246
				G_TYPE_BOOLEAN, TRUE);
247 248
}

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

Manish Singh's avatar
Manish Singh committed
257
  parent_class = g_type_class_peek_parent (class);
258 259 260

  quark_angle = g_quark_from_static_string ("angle");

261 262
  gobject_class->set_property = gtk_label_set_property;
  gobject_class->get_property = gtk_label_get_property;
263
  gobject_class->finalize = gtk_label_finalize;
264 265

  object_class->destroy = gtk_label_destroy;
Tim Janik's avatar
Tim Janik committed
266
  
Elliot Lee's avatar
Elliot Lee committed
267
  widget_class->size_request = gtk_label_size_request;
268
  widget_class->size_allocate = gtk_label_size_allocate;
Havoc Pennington's avatar
Havoc Pennington committed
269
  widget_class->state_changed = gtk_label_state_changed;
270
  widget_class->style_set = gtk_label_style_set;
271
  widget_class->direction_changed = gtk_label_direction_changed;
Elliot Lee's avatar
Elliot Lee committed
272
  widget_class->expose_event = gtk_label_expose;
273 274 275 276 277 278 279
  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;
280
  widget_class->hierarchy_changed = gtk_label_hierarchy_changed;
281
  widget_class->screen_changed = gtk_label_screen_changed;
282
  widget_class->mnemonic_activate = gtk_label_mnemonic_activate;
283
  widget_class->drag_data_get = gtk_label_drag_data_get;
284
  widget_class->grab_focus = gtk_label_grab_focus;
285 286 287 288 289

  class->move_cursor = gtk_label_move_cursor;
  class->copy_clipboard = gtk_label_copy_clipboard;
  
  signals[MOVE_CURSOR] = 
Matthias Clasen's avatar
Matthias Clasen committed
290
    g_signal_new (I_("move_cursor"),
Manish Singh's avatar
Manish Singh committed
291 292 293 294 295 296 297 298 299
		  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);
300 301
  
  signals[COPY_CLIPBOARD] =
Matthias Clasen's avatar
Matthias Clasen committed
302
    g_signal_new (I_("copy_clipboard"),
Manish Singh's avatar
Manish Singh committed
303 304 305 306 307 308
		  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);
309 310
  
  signals[POPULATE_POPUP] =
Matthias Clasen's avatar
Matthias Clasen committed
311
    g_signal_new (I_("populate_popup"),
Manish Singh's avatar
Manish Singh committed
312 313 314 315 316 317 318 319 320
		  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,
321 322
                                   PROP_LABEL,
                                   g_param_spec_string ("label",
323 324
                                                        P_("Label"),
                                                        P_("The text of the label"),
325
                                                        NULL,
326
                                                        GTK_PARAM_READWRITE));
327 328 329
  g_object_class_install_property (gobject_class,
				   PROP_ATTRIBUTES,
				   g_param_spec_boxed ("attributes",
330 331
						       P_("Attributes"),
						       P_("A list of style attributes to apply to the text of the label"),
332
						       PANGO_TYPE_ATTR_LIST,
333
						       GTK_PARAM_READWRITE));
334 335
  g_object_class_install_property (gobject_class,
                                   PROP_USE_MARKUP,
Matthias Clasen's avatar
x  
Matthias Clasen committed
336
                                   g_param_spec_boolean ("use-markup",
337 338
							 P_("Use markup"),
							 P_("The text of the label includes XML markup. See pango_parse_markup()"),
339
                                                        FALSE,
340
                                                        GTK_PARAM_READWRITE));
341 342
  g_object_class_install_property (gobject_class,
                                   PROP_USE_UNDERLINE,
Matthias Clasen's avatar
x  
Matthias Clasen committed
343
                                   g_param_spec_boolean ("use-underline",
344 345
							 P_("Use underline"),
							 P_("If set, an underline in the text indicates the next character should be used for the mnemonic accelerator key"),
346
                                                        FALSE,
347
                                                        GTK_PARAM_READWRITE));
348 349 350 351

  g_object_class_install_property (gobject_class,
				   PROP_JUSTIFY,
                                   g_param_spec_enum ("justify",
352 353
                                                      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"),
354 355
						      GTK_TYPE_JUSTIFICATION,
						      GTK_JUSTIFY_LEFT,
356
                                                      GTK_PARAM_READWRITE));
357 358 359 360

  g_object_class_install_property (gobject_class,
                                   PROP_PATTERN,
                                   g_param_spec_string ("pattern",
361 362
                                                        P_("Pattern"),
                                                        P_("A string with _ characters in positions correspond to characters in the text to underline"),
363
                                                        NULL,
364
                                                        GTK_PARAM_WRITABLE));
365 366 367 368

  g_object_class_install_property (gobject_class,
                                   PROP_WRAP,
                                   g_param_spec_boolean ("wrap",
369 370
                                                        P_("Line wrap"),
                                                        P_("If set, wrap lines if the text becomes too wide"),
371
                                                        FALSE,
372
                                                        GTK_PARAM_READWRITE));
373 374 375
  g_object_class_install_property (gobject_class,
                                   PROP_SELECTABLE,
                                   g_param_spec_boolean ("selectable",
376 377
                                                        P_("Selectable"),
                                                        P_("Whether the label text can be selected with the mouse"),
378
                                                        FALSE,
379
                                                        GTK_PARAM_READWRITE));
380
  g_object_class_install_property (gobject_class,
381
                                   PROP_MNEMONIC_KEYVAL,
Matthias Clasen's avatar
x  
Matthias Clasen committed
382
                                   g_param_spec_uint ("mnemonic-keyval",
383 384
						      P_("Mnemonic key"),
						      P_("The mnemonic accelerator key for this label"),
385 386 387
						      0,
						      G_MAXUINT,
						      GDK_VoidSymbol,
388
						      GTK_PARAM_READABLE));
389 390
  g_object_class_install_property (gobject_class,
                                   PROP_MNEMONIC_WIDGET,
Matthias Clasen's avatar
x  
Matthias Clasen committed
391
                                   g_param_spec_object ("mnemonic-widget",
392 393
							P_("Mnemonic widget"),
							P_("The widget to be activated when the label's mnemonic "
Soren Sandmann's avatar
Soren Sandmann committed
394
							  "key is pressed"),
395
							GTK_TYPE_WIDGET,
396
							GTK_PARAM_READWRITE));
397

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

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

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

559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
  /* select all */
  gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
				"move_cursor", 3,
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, -1,
				G_TYPE_BOOLEAN, FALSE);

  gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_CONTROL_MASK,
				"move_cursor", 3,
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, 1,
				G_TYPE_BOOLEAN, TRUE);

  gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
				"move_cursor", 3,
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, -1,
				G_TYPE_BOOLEAN, FALSE);

  gtk_binding_entry_add_signal (binding_set, GDK_slash, GDK_CONTROL_MASK,
				"move_cursor", 3,
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, 1,
				G_TYPE_BOOLEAN, TRUE);

  /* unselect all */
  gtk_binding_entry_add_signal (binding_set, GDK_a, GDK_SHIFT_MASK | GDK_CONTROL_MASK,
				"move_cursor", 3,
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, 0,
				G_TYPE_BOOLEAN, FALSE);

  gtk_binding_entry_add_signal (binding_set, GDK_backslash, GDK_CONTROL_MASK,
				"move_cursor", 3,
				G_TYPE_ENUM, GTK_MOVEMENT_PARAGRAPH_ENDS,
				G_TYPE_INT, 0,
				G_TYPE_BOOLEAN, FALSE);
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629

  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);
630 631 632 633 634 635 636

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

637 638
				
  g_type_class_add_private (class, sizeof (GtkLabelPrivate));
Elliot Lee's avatar
Elliot Lee committed
639 640
}

641 642 643 644
static void 
gtk_label_set_property (GObject      *object,
			guint         prop_id,
			const GValue *value,
645
			GParamSpec   *pspec)
646
{
647
  GtkLabel *label;
648

649
  label = GTK_LABEL (object);
650
  
651
  switch (prop_id)
652
    {
653
    case PROP_LABEL:
654
      gtk_label_set_label (label, g_value_get_string (value));
655 656 657 658 659
      break;
    case PROP_ATTRIBUTES:
      gtk_label_set_attributes (label, g_value_get_boxed (value));
      break;
    case PROP_USE_MARKUP:
660
      gtk_label_set_use_markup (label, g_value_get_boolean (value));
661
      break;
662
    case PROP_USE_UNDERLINE:
663
      gtk_label_set_use_underline (label, g_value_get_boolean (value));
664
      break;
665 666
    case PROP_JUSTIFY:
      gtk_label_set_justify (label, g_value_get_enum (value));
667
      break;
668 669 670 671 672 673 674 675
    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));
676
      break;	  
677 678 679
    case PROP_MNEMONIC_WIDGET:
      gtk_label_set_mnemonic_widget (label, (GtkWidget*) g_value_get_object (value));
      break;
680 681 682
    case PROP_ELLIPSIZE:
      gtk_label_set_ellipsize (label, g_value_get_enum (value));
      break;
683 684 685
    case PROP_WIDTH_CHARS:
      gtk_label_set_width_chars (label, g_value_get_int (value));
      break;
686 687 688
    case PROP_SINGLE_LINE_MODE:
      gtk_label_set_single_line_mode (label, g_value_get_boolean (value));
      break;	  
689 690
    case PROP_ANGLE:
      gtk_label_set_angle (label, g_value_get_double (value));
691 692 693 694
      break;
    case PROP_MAX_WIDTH_CHARS:
      gtk_label_set_max_width_chars (label, g_value_get_int (value));
      break;
Tim Janik's avatar
Tim Janik committed
695
    default:
696
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
Tim Janik's avatar
Tim Janik committed
697
      break;
698 699 700
    }
}

701 702 703 704
static void 
gtk_label_get_property (GObject     *object,
			guint        prop_id,
			GValue      *value,
705
			GParamSpec  *pspec)
706
{
707
  GtkLabel *label;
708
  
709
  label = GTK_LABEL (object);
710
  
711
  switch (prop_id)
712
    {
713
    case PROP_LABEL:
Alexander Larsson's avatar
Alexander Larsson committed
714
      g_value_set_string (value, label->label);
715 716 717 718 719 720 721 722 723
      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);
724
      break;
725 726
    case PROP_JUSTIFY:
      g_value_set_enum (value, label->jtype);
727
      break;
728 729
    case PROP_WRAP:
      g_value_set_boolean (value, label->wrap);
730
      break;
731 732 733
    case PROP_SELECTABLE:
      g_value_set_boolean (value, gtk_label_get_selectable (label));
      break;
734 735
    case PROP_MNEMONIC_KEYVAL:
      g_value_set_uint (value, label->mnemonic_keyval);
736
      break;
737 738 739
    case PROP_MNEMONIC_WIDGET:
      g_value_set_object (value, (GObject*) label->mnemonic_widget);
      break;
740 741 742
    case PROP_CURSOR_POSITION:
      if (label->select_info)
	{
743 744
	  gint offset = g_utf8_pointer_to_offset (label->text,
						  label->text + label->select_info->selection_end);
745 746 747 748 749 750 751 752
	  g_value_set_int (value, offset);
	}
      else
	g_value_set_int (value, 0);
      break;
    case PROP_SELECTION_BOUND:
      if (label->select_info)
	{
753 754
	  gint offset = g_utf8_pointer_to_offset (label->text,
						  label->text + label->select_info->selection_anchor);
755 756 757 758 759
	  g_value_set_int (value, offset);
	}
      else
	g_value_set_int (value, 0);
      break;
760 761 762
    case PROP_ELLIPSIZE:
      g_value_set_enum (value, label->ellipsize);
      break;
763
    case PROP_WIDTH_CHARS:
764
      g_value_set_int (value, gtk_label_get_width_chars (label));
765
      break;
766 767 768
    case PROP_SINGLE_LINE_MODE:
      g_value_set_boolean (value, gtk_label_get_single_line_mode (label));
      break;
769 770 771
    case PROP_ANGLE:
      g_value_set_double (value, gtk_label_get_angle (label));
      break;
772 773 774
    case PROP_MAX_WIDTH_CHARS:
      g_value_set_int (value, gtk_label_get_max_width_chars (label));
      break;
775

776
    default:
777
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
778 779 780 781
      break;
    }
}

782
static void
Elliot Lee's avatar
Elliot Lee committed
783 784
gtk_label_init (GtkLabel *label)
{
785 786
  GtkLabelPrivate *priv;

Elliot Lee's avatar
Elliot Lee committed
787
  GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
788 789 790

  priv = GTK_LABEL_GET_PRIVATE (label);
  priv->width_chars = -1;
791
  priv->max_width_chars = -1;
Elliot Lee's avatar
Elliot Lee committed
792
  label->label = NULL;
793

794
  label->jtype = GTK_JUSTIFY_LEFT;
Owen Taylor's avatar
Owen Taylor committed
795
  label->wrap = FALSE;
796
  label->ellipsize = PANGO_ELLIPSIZE_NONE;
797 798 799

  label->use_underline = FALSE;
  label->use_markup = FALSE;
800
  
801
  label->mnemonic_keyval = GDK_VoidSymbol;
802
  label->layout = NULL;
803
  label->text = NULL;
804
  label->attrs = NULL;
805 806 807

  label->mnemonic_widget = NULL;
  label->mnemonic_window = NULL;
808
  
Owen Taylor's avatar
Owen Taylor committed
809
  gtk_label_set_text (label, "");
Elliot Lee's avatar
Elliot Lee committed
810 811
}

812 813 814 815
/**
 * gtk_label_new:
 * @str: The text of the label
 *
Matthias Clasen's avatar
Matthias Clasen committed
816 817
 * Creates a new label with the given text inside it. You can
 * pass %NULL to get an empty label widget.
818 819
 *
 * Return value: the new #GtkLabel
820
 **/
Elliot Lee's avatar
Elliot Lee committed
821
GtkWidget*
822
gtk_label_new (const gchar *str)
Elliot Lee's avatar
Elliot Lee committed
823 824
{
  GtkLabel *label;
825
  
Manish Singh's avatar
Manish Singh committed
826
  label = g_object_new (GTK_TYPE_LABEL, NULL);
827 828 829

  if (str && *str)
    gtk_label_set_text (label, str);
830
  
Elliot Lee's avatar
Elliot Lee committed
831 832 833
  return GTK_WIDGET (label);
}

834
/**
835 836 837 838 839 840
 * 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.
 *
841
 * If characters in @str are preceded by an underscore, they are
842 843 844 845
 * 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
846 847 848 849 850 851 852 853
 * 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.
854 855
 *
 * Return value: the new #GtkLabel
856 857 858 859 860 861
 **/
GtkWidget*
gtk_label_new_with_mnemonic (const gchar *str)
{
  GtkLabel *label;
  
Manish Singh's avatar
Manish Singh committed
862
  label = g_object_new (GTK_TYPE_LABEL, NULL);
863 864 865 866 867 868 869 870

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

static gboolean
871
gtk_label_mnemonic_activate (GtkWidget *widget,
872 873 874 875 876
			     gboolean   group_cycling)
{
  GtkWidget *parent;

  if (GTK_LABEL (widget)->mnemonic_widget)
877
    return gtk_widget_mnemonic_activate (GTK_LABEL (widget)->mnemonic_widget, group_cycling);
878

879 880
  /* Try to find the widget to activate by traversing the
   * widget's ancestry.
881 882
   */
  parent = widget->parent;
883 884

  if (parent && GTK_IS_NOTEBOOK (parent))
885
    return FALSE;
886
  
887 888 889 890
  while (parent)
    {
      if (GTK_WIDGET_CAN_FOCUS (parent) ||
	  (!group_cycling && GTK_WIDGET_GET_CLASS (parent)->activate_signal) ||
891
          (parent->parent && GTK_IS_NOTEBOOK (parent->parent)) ||
892
	  (GTK_IS_MENU_ITEM (parent)))
893
	return gtk_widget_mnemonic_activate (parent, group_cycling);
894 895 896
      parent = parent->parent;
    }

897
  /* barf if there was nothing to activate */
898
  g_warning ("Couldn't find a target for a mnemonic activation.");
899
  gdk_display_beep (gtk_widget_get_display (widget));
900 901 902 903 904
  
  return FALSE;
}

static void
905 906
gtk_label_setup_mnemonic (GtkLabel *label,
			  guint     last_key)
907
{
908
  GtkWidget *widget = GTK_WIDGET (label);
909
  GtkWidget *toplevel;
910 911 912 913 914
  GtkWidget *mnemonic_menu;
  
  mnemonic_menu = g_object_get_data (G_OBJECT (label), "gtk-mnemonic-menu");
  
  if (last_key != GDK_VoidSymbol)
915
    {
916 917 918 919 920 921 922 923 924 925 926 927
      if (label->mnemonic_window)
	{
	  gtk_window_remove_mnemonic  (label->mnemonic_window,
				       last_key,
				       widget);
	  label->mnemonic_window = NULL;
	}
      if (mnemonic_menu)
	{
	  _gtk_menu_shell_remove_mnemonic (GTK_MENU_SHELL (mnemonic_menu),
					   last_key,
					   widget);
928
	  mnemonic_menu = NULL;
929
	}
930
    }
931 932
  
  if (label->mnemonic_keyval == GDK_VoidSymbol)
933
    goto done;
934 935

  toplevel = gtk_widget_get_toplevel (widget);
936
  if (GTK_WIDGET_TOPLEVEL (toplevel))