gtklabel.c 35.3 KB
Newer Older
1
/* GTK - The GIMP Toolkit
Elliot Lee's avatar
Elliot Lee committed
2 3 4
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Elliot Lee's avatar
Elliot Lee committed
6 7 8 9 10
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Owen Taylor's avatar
Owen Taylor committed
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
Elliot Lee's avatar
Elliot Lee committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Owen Taylor's avatar
Owen Taylor committed
15 16
 * License along with this library; if not, write to the Free
 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
Elliot Lee's avatar
Elliot Lee committed
17
 */
18 19

/*
20
 * Modified by the GTK+ Team and others 1997-2000.  See the AUTHORS
21 22 23 24 25
 * file for a list of people on the GTK+ Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GTK+ at ftp://ftp.gtk.org/pub/gtk/. 
 */

26
#include <math.h>
Elliot Lee's avatar
Elliot Lee committed
27 28
#include <string.h>
#include "gtklabel.h"
29
#include "gdk/gdkkeysyms.h"
30
#include "gtkclipboard.h"
Owen Taylor's avatar
Owen Taylor committed
31
#include "gdk/gdki18n.h"
32 33
#include <pango/pango.h>

34 35 36 37 38 39
struct _GtkLabelSelectionInfo
{
  GdkWindow *window;
  gint selection_anchor;
  gint selection_end;
};
Elliot Lee's avatar
Elliot Lee committed
40

41 42 43
enum {
  ARG_0,
  ARG_LABEL,
44
  ARG_PATTERN,
45 46
  ARG_JUSTIFY,
  ARG_WRAP
47 48
};

49 50 51 52 53 54 55 56 57 58 59
static void gtk_label_class_init        (GtkLabelClass    *klass);
static void gtk_label_init              (GtkLabel         *label);
static void gtk_label_set_arg           (GtkObject        *object,
					 GtkArg           *arg,
					 guint             arg_id);
static void gtk_label_get_arg           (GtkObject        *object,
					 GtkArg           *arg,
					 guint             arg_id);
static void gtk_label_finalize          (GObject          *object);
static void gtk_label_size_request      (GtkWidget        *widget,
					 GtkRequisition   *requisition);
60 61
static void gtk_label_size_allocate     (GtkWidget        *widget,
                                         GtkAllocation    *allocation);
62 63 64 65 66 67
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
68

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
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);

static void gtk_label_create_window       (GtkLabel *label);
static void gtk_label_destroy_window      (GtkLabel *label);
static void gtk_label_clear_layout        (GtkLabel *label);
static void gtk_label_ensure_layout       (GtkLabel *label,
                                           gint     *widthp,
                                           gint     *heightp);
static void gtk_label_select_region_index (GtkLabel *label,
                                           gint      anchor_index,
                                           gint      end_index);


GtkMiscClass *parent_class = NULL;
Elliot Lee's avatar
Elliot Lee committed
92

Tim Janik's avatar
Tim Janik committed
93
GtkType
94
gtk_label_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
95
{
Tim Janik's avatar
Tim Janik committed
96 97
  static GtkType label_type = 0;
  
Elliot Lee's avatar
Elliot Lee committed
98 99
  if (!label_type)
    {
100
      static const GTypeInfo label_info =
Elliot Lee's avatar
Elliot Lee committed
101 102
      {
	sizeof (GtkLabelClass),
103 104 105 106 107 108 109 110
	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
111
      };
112

113
      label_type = g_type_register_static (GTK_TYPE_MISC, "GtkLabel", &label_info, 0);
Elliot Lee's avatar
Elliot Lee committed
114
    }
Tim Janik's avatar
Tim Janik committed
115
  
Elliot Lee's avatar
Elliot Lee committed
116 117 118
  return label_type;
}

119
static void
Elliot Lee's avatar
Elliot Lee committed
120 121
gtk_label_class_init (GtkLabelClass *class)
{
122
  GObjectClass *gobject_class = G_OBJECT_CLASS (class);
Elliot Lee's avatar
Elliot Lee committed
123 124
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
Tim Janik's avatar
Tim Janik committed
125
  
Elliot Lee's avatar
Elliot Lee committed
126 127
  object_class = (GtkObjectClass*) class;
  widget_class = (GtkWidgetClass*) class;
Tim Janik's avatar
Tim Janik committed
128
  
129
  parent_class = gtk_type_class (GTK_TYPE_MISC);
Tim Janik's avatar
Tim Janik committed
130
  
131
  gtk_object_add_arg_type ("GtkLabel::label", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_LABEL);
132
  gtk_object_add_arg_type ("GtkLabel::pattern", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_PATTERN);
133
  gtk_object_add_arg_type ("GtkLabel::justify", GTK_TYPE_JUSTIFICATION, GTK_ARG_READWRITE, ARG_JUSTIFY);
134
  gtk_object_add_arg_type ("GtkLabel::wrap", GTK_TYPE_BOOL, GTK_ARG_READWRITE, ARG_WRAP);
135
  
136 137
  gobject_class->finalize = gtk_label_finalize;

138 139
  object_class->set_arg = gtk_label_set_arg;
  object_class->get_arg = gtk_label_get_arg;
Tim Janik's avatar
Tim Janik committed
140
  
Elliot Lee's avatar
Elliot Lee committed
141
  widget_class->size_request = gtk_label_size_request;
142
  widget_class->size_allocate = gtk_label_size_allocate;
143
  widget_class->style_set = gtk_label_style_set;
144
  widget_class->direction_changed = gtk_label_direction_changed;
Elliot Lee's avatar
Elliot Lee committed
145
  widget_class->expose_event = gtk_label_expose;
146 147 148 149 150 151 152
  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;
Elliot Lee's avatar
Elliot Lee committed
153 154
}

155
static void
156
gtk_label_set_arg (GtkObject	  *object,
Tim Janik's avatar
Tim Janik committed
157 158
		   GtkArg	  *arg,
		   guint	   arg_id)
159
{
160
  GtkLabel *label;
161
  
162
  label = GTK_LABEL (object);
163
  
164 165 166
  switch (arg_id)
    {
    case ARG_LABEL:
167
      gtk_label_set_text (label, GTK_VALUE_STRING (*arg));
168
      break;
169 170 171
    case ARG_PATTERN:
      gtk_label_set_pattern (label, GTK_VALUE_STRING (*arg));
      break;
172 173 174
    case ARG_JUSTIFY:
      gtk_label_set_justify (label, GTK_VALUE_ENUM (*arg));
      break;
175 176 177
    case ARG_WRAP:
      gtk_label_set_line_wrap (label, GTK_VALUE_BOOL (*arg));
      break;	  
Tim Janik's avatar
Tim Janik committed
178 179
    default:
      break;
180 181 182
    }
}

Tim Janik's avatar
Tim Janik committed
183
static void
184
gtk_label_get_arg (GtkObject	  *object,
Tim Janik's avatar
Tim Janik committed
185 186
		   GtkArg	  *arg,
		   guint	   arg_id)
187
{
188
  GtkLabel *label;
189
  
190
  label = GTK_LABEL (object);
191
  
192 193 194 195 196
  switch (arg_id)
    {
    case ARG_LABEL:
      GTK_VALUE_STRING (*arg) = g_strdup (label->label);
      break;
197 198 199
    case ARG_PATTERN:
      GTK_VALUE_STRING (*arg) = g_strdup (label->pattern);
      break;
200 201 202
    case ARG_JUSTIFY:
      GTK_VALUE_ENUM (*arg) = label->jtype;
      break;
203 204 205
    case ARG_WRAP:
      GTK_VALUE_BOOL (*arg) = label->wrap;
      break;
206 207 208 209 210 211
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

212
static void
Elliot Lee's avatar
Elliot Lee committed
213 214 215
gtk_label_init (GtkLabel *label)
{
  GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
Tim Janik's avatar
Tim Janik committed
216
  
Elliot Lee's avatar
Elliot Lee committed
217
  label->label = NULL;
218 219
  label->pattern = NULL;

Elliot Lee's avatar
Elliot Lee committed
220
  label->jtype = GTK_JUSTIFY_CENTER;
Owen Taylor's avatar
Owen Taylor committed
221
  label->wrap = FALSE;
222
  
223
  label->layout = NULL;
224
  label->attrs = NULL;
225
  
Owen Taylor's avatar
Owen Taylor committed
226
  gtk_label_set_text (label, "");
Elliot Lee's avatar
Elliot Lee committed
227 228 229
}

GtkWidget*
230
gtk_label_new (const gchar *str)
Elliot Lee's avatar
Elliot Lee committed
231 232
{
  GtkLabel *label;
233
  
234 235 236 237
  label = gtk_type_new (GTK_TYPE_LABEL);

  if (str && *str)
    gtk_label_set_text (label, str);
238
  
Elliot Lee's avatar
Elliot Lee committed
239 240 241
  return GTK_WIDGET (label);
}

242
static inline void
243
gtk_label_set_text_internal (GtkLabel *label,
244
			     gchar    *str)
Elliot Lee's avatar
Elliot Lee committed
245
{
246
  g_free (label->label);
247
  
248
  label->label = str;
249 250 251 252

  gtk_label_clear_layout (label);  

  gtk_label_select_region_index (label, 0, 0);
253
  
254
  gtk_widget_queue_resize (GTK_WIDGET (label));
255 256
}

257
void
258 259
gtk_label_set_text (GtkLabel    *label,
		    const gchar *str)
260 261
{
  g_return_if_fail (GTK_IS_LABEL (label));
262
  
263 264 265
  gtk_label_set_text_internal (label, g_strdup (str ? str : ""));
}

266 267 268 269 270 271 272 273 274 275 276 277 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 304 305 306 307 308 309 310 311 312 313 314 315 316
/**
 * gtk_label_set_attributes:
 * @label: a #GtkLabel
 * @attrs: a #PangoAttrList
 * 
 * Sets a #PangoAttrList; the attributes in the list are applied to the
 * label text.
 **/
void
gtk_label_set_attributes (GtkLabel         *label,
                          PangoAttrList    *attrs)
{
  g_return_if_fail (GTK_IS_LABEL (label));

  if (attrs)
    pango_attr_list_ref (attrs);
  
  if (label->attrs)
    pango_attr_list_unref (label->attrs);

  label->attrs = attrs;
}

static guint
set_markup (GtkLabel    *label,
            const gchar *str,
            gboolean     with_uline)
{
  gchar *text = NULL;
  GError *error = NULL;
  PangoAttrList *attrs = NULL;
  gunichar accel_char = 0;

  if (str == NULL)
    str = "";
  
  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);
      return 0;
    }

  if (text)
317
    gtk_label_set_text_internal (label, text);
318 319 320 321 322 323 324 325

  if (attrs)
    {
      gtk_label_set_attributes (label, attrs);
      pango_attr_list_unref (attrs);
    }

  if (accel_char != 0)
326
    return gdk_keyval_to_lower (gdk_unicode_to_keyval (accel_char));
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348
  else
    return GDK_VoidSymbol;
}

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

  set_markup (label, str, FALSE);
}

/**
349
 * gtk_label_set_markup_with_accel:
350 351 352 353 354 355 356 357 358
 * @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
 * indicating that they represent a keyboard accelerator, and the GDK
 * keyval for the first underlined accelerator is returned. If there are
 * no underlines in the text, GDK_VoidSymbol will be returned.
359 360
 *
 * Return value: GDK keyval for accelerator
361 362 363 364 365
 **/
guint
gtk_label_set_markup_with_accel (GtkLabel    *label,
                                 const gchar *str)
{
Tor Lillqvist's avatar
Tor Lillqvist committed
366
  g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
367 368 369 370

  return set_markup (label, str, TRUE);
}

371 372 373 374 375 376 377 378 379
/**
 * gtk_label_get_text:
 * @label: a #GtkLabel
 * 
 * Fetches the text from a label widget
 * 
 * Return value: the text in the label widget. This value must
 * be freed with g_free().
 **/
380
G_CONST_RETURN gchar *
381 382
gtk_label_get_text (GtkLabel *label)
{
383 384
  g_return_val_if_fail (label != NULL, NULL);
  g_return_val_if_fail (GTK_IS_LABEL (label), NULL);
385

386
  return label->label;
387 388
}

389 390 391 392 393 394
void
gtk_label_set_pattern (GtkLabel	   *label,
		       const gchar *pattern)
{
  g_return_if_fail (GTK_IS_LABEL (label));
  
395 396 397 398
  g_free (label->pattern);
  label->pattern = g_strdup (pattern);

  gtk_widget_queue_resize (GTK_WIDGET (label));
Elliot Lee's avatar
Elliot Lee committed
399 400 401
}

void
402 403
gtk_label_set_justify (GtkLabel        *label,
		       GtkJustification jtype)
Elliot Lee's avatar
Elliot Lee committed
404 405
{
  g_return_if_fail (GTK_IS_LABEL (label));
406
  g_return_if_fail (jtype >= GTK_JUSTIFY_LEFT && jtype <= GTK_JUSTIFY_FILL);
407
  
Elliot Lee's avatar
Elliot Lee committed
408 409 410
  if ((GtkJustification) label->jtype != jtype)
    {
      label->jtype = jtype;
411

412 413 414
      if (label->layout)
	{
	  /* No real need to be this drastic, but easier than duplicating the code */
415
          g_object_unref (G_OBJECT (label->layout));
416 417 418
	  label->layout = NULL;
	}
      
419
      gtk_widget_queue_resize (GTK_WIDGET (label));
Elliot Lee's avatar
Elliot Lee committed
420 421 422 423
    }
}

void
424 425
gtk_label_set_line_wrap (GtkLabel *label,
			 gboolean  wrap)
Owen Taylor's avatar
Owen Taylor committed
426 427
{
  g_return_if_fail (GTK_IS_LABEL (label));
428
  
429 430 431 432 433 434 435 436
  wrap = wrap != FALSE;
  
  if (label->wrap != wrap)
    {
      label->wrap = wrap;

      gtk_widget_queue_resize (GTK_WIDGET (label));
    }
Owen Taylor's avatar
Owen Taylor committed
437 438 439
}

void
440 441
gtk_label_get (GtkLabel *label,
	       gchar   **str)
Elliot Lee's avatar
Elliot Lee committed
442 443 444 445
{
  g_return_if_fail (label != NULL);
  g_return_if_fail (GTK_IS_LABEL (label));
  g_return_if_fail (str != NULL);
446
  
Elliot Lee's avatar
Elliot Lee committed
447 448 449 450
  *str = label->label;
}

static void
451
gtk_label_finalize (GObject *object)
Elliot Lee's avatar
Elliot Lee committed
452 453
{
  GtkLabel *label;
454
  
Elliot Lee's avatar
Elliot Lee committed
455
  g_return_if_fail (GTK_IS_LABEL (object));
456
  
Elliot Lee's avatar
Elliot Lee committed
457
  label = GTK_LABEL (object);
458
  
Elliot Lee's avatar
Elliot Lee committed
459
  g_free (label->label);
460 461
  g_free (label->pattern);

462
  if (label->layout)
463
    g_object_unref (G_OBJECT (label->layout));
464

465 466
  if (label->attrs)
    pango_attr_list_unref (label->attrs);
467 468

  g_free (label->select_info);
469
  
470
  G_OBJECT_CLASS (parent_class)->finalize (object);
Elliot Lee's avatar
Elliot Lee committed
471 472
}

473 474 475
static void
gtk_label_pattern_to_attrs (GtkLabel      *label,
                            PangoAttrList *attrs)
476
{
477
  if (label->pattern)
Owen Taylor's avatar
Owen Taylor committed
478
    {
479 480 481
      const char *start;
      const char *p = label->label;
      const char *q = label->pattern;
482

483
      while (1)
Owen Taylor's avatar
Owen Taylor committed
484
	{
485
	  while (*p && *q && *q != '_')
Owen Taylor's avatar
Owen Taylor committed
486
	    {
487
	      p = g_utf8_next_char (p);
488
	      q++;
Owen Taylor's avatar
Owen Taylor committed
489
	    }
490 491
	  start = p;
	  while (*p && *q && *q == '_')
492
	    {
493
	      p = g_utf8_next_char (p);
494
	      q++;
495
	    }
Owen Taylor's avatar
Owen Taylor committed
496

497
	  if (p > start)
498
	    {
499 500 501 502 503
	      PangoAttribute *attr = pango_attr_underline_new (PANGO_UNDERLINE_LOW);
	      attr->start_index = start - label->label;
	      attr->end_index = p - label->label;

	      pango_attr_list_insert (attrs, attr);
504
	    }
Owen Taylor's avatar
Owen Taylor committed
505
	  else
506
	    break;
507 508 509
	}
    }
}
510

Elliot Lee's avatar
Elliot Lee committed
511
static void
512
gtk_label_clear_layout (GtkLabel *label)
Elliot Lee's avatar
Elliot Lee committed
513
{
514 515 516 517 518 519 520 521 522 523 524 525 526
  if (label->layout)
    {
      g_object_unref (G_OBJECT (label->layout));
      label->layout = NULL;
    }
}

static void
gtk_label_ensure_layout (GtkLabel *label,
                         gint     *widthp,
                         gint     *heightp)
{
  GtkWidget *widget;
527
  PangoRectangle logical_rect;
528
  gint rwidth, rheight;
529 530

  widget = GTK_WIDGET (label);
531

Owen Taylor's avatar
Owen Taylor committed
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
  /*
   * There are a number of conditions which will necessitate re-filling
   * our text:
   *
   *     1. text changed.
   *     2. justification changed either from to to GTK_JUSTIFY_FILL.
   *     3. font changed.
   *
   * These have been detected elsewhere, and label->words will be zero,
   * if one of the above has occured.
   *
   * Additionally, though, if GTK_JUSTIFY_FILL, we need to re-fill if:
   *
   *     4. gtk_widget_set_usize has changed the requested width.
   *     5. gtk_misc_set_padding has changed xpad.
   *     6.  maybe others?...
   *
   * Too much of a pain to detect all these case, so always re-fill.  I
   * don't think it's really that slow.
   */
552

553 554
  rwidth = label->misc.xpad * 2;
  rheight = label->misc.ypad * 2;
555 556 557 558

  if (!label->layout)
    {
      PangoAlignment align = PANGO_ALIGN_LEFT; /* Quiet gcc */
559
      PangoAttrList *attrs = NULL;
560

561 562
      label->layout = gtk_widget_create_pango_layout (widget, label->label);

563
      /* FIXME move to a model where the pattern isn't stored
564
       * permanently, and just modifies or creates the AttrList
565 566
       */
      if (label->attrs)
567 568
	attrs = pango_attr_list_copy (label->attrs);
      else if (label->pattern)
569
        attrs = pango_attr_list_new ();
570 571 572

      if (label->pattern)
	gtk_label_pattern_to_attrs (label, attrs);
573
      
574 575 576 577 578
      if (attrs)
	{
	  pango_layout_set_attributes (label->layout, attrs);
	  pango_attr_list_unref (attrs);
	}
579
      
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602
      switch (label->jtype)
	{
	case GTK_JUSTIFY_LEFT:
	  align = PANGO_ALIGN_LEFT;
	  break;
	case GTK_JUSTIFY_RIGHT:
	  align = PANGO_ALIGN_RIGHT;
	  break;
	case GTK_JUSTIFY_CENTER:
	  align = PANGO_ALIGN_LEFT;
	  break;
	case GTK_JUSTIFY_FILL:
	  /* FIXME: This just doesn't work to do this */
	  align = PANGO_ALIGN_LEFT;
	  pango_layout_set_justify (label->layout, TRUE);
	  break;
	default:
	  g_assert_not_reached();
	}

      pango_layout_set_alignment (label->layout, align);
    }

Owen Taylor's avatar
Owen Taylor committed
603
  if (label->wrap)
Elliot Lee's avatar
Elliot Lee committed
604
    {
Owen Taylor's avatar
Owen Taylor committed
605 606
      GtkWidgetAuxInfo *aux_info;
      gint longest_paragraph;
607 608 609
      gint width, height;
      gint real_width;

Owen Taylor's avatar
Owen Taylor committed
610 611 612
      aux_info = gtk_object_get_data (GTK_OBJECT (widget), "gtk-aux-info");
      if (aux_info && aux_info->width > 0)
	{
613 614 615
	  pango_layout_set_width (label->layout, aux_info->width * PANGO_SCALE);
	  pango_layout_get_extents (label->layout, NULL, &logical_rect);

616 617
	  rwidth += aux_info->width;
	  rheight += PANGO_PIXELS (logical_rect.height);
Owen Taylor's avatar
Owen Taylor committed
618 619 620
	}
      else
	{
621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644
	  pango_layout_set_width (label->layout, -1);
	  pango_layout_get_extents (label->layout, NULL, &logical_rect);
      
	  width = logical_rect.width;
	  height = logical_rect.height;
	  
	  /* Try to guess a reasonable maximum width
	   */
	  longest_paragraph = width;

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

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

	      nlines = pango_layout_get_line_count (label->layout);
Owen Taylor's avatar
Owen Taylor committed
650
	      perfect_width = (longest_paragraph + nlines - 1) / nlines;
651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680
	      
	      if (perfect_width < width)
		{
		  pango_layout_set_width (label->layout, perfect_width);
		  pango_layout_get_extents (label->layout, NULL, &logical_rect);
		  
		  if (logical_rect.height <= height)
		    {
		      width = perfect_width;
		      real_width = logical_rect.width;
		      height = logical_rect.height;
		    }
		  else
		    {
		      gint mid_width = (perfect_width + width) / 2;

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

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

684 685
	  rwidth += PANGO_PIXELS (real_width);
	  rheight += PANGO_PIXELS (height);
Owen Taylor's avatar
Owen Taylor committed
686 687
	}
    }
688
  else				/* !label->wrap */
Owen Taylor's avatar
Owen Taylor committed
689
    {
690 691 692
      pango_layout_set_width (label->layout, -1);
      pango_layout_get_extents (label->layout, NULL, &logical_rect);

693 694
      rwidth += PANGO_PIXELS (logical_rect.width);
      rheight += PANGO_PIXELS (logical_rect.height);
695 696 697
    }

  if (widthp)
698
    *widthp = rwidth;
699 700

  if (heightp)
701
    *heightp = rheight;
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738
}

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

  gtk_label_ensure_layout (label, &width, &height);

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

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

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

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

742
static void 
743 744
gtk_label_style_set (GtkWidget *widget,
		     GtkStyle  *previous_style)
745 746
{
  GtkLabel *label;
747
  
748
  g_return_if_fail (GTK_IS_LABEL (widget));
749
  
750
  label = GTK_LABEL (widget);
751

752 753
  /* We have to clear the layout, fonts etc. may have changed */
  gtk_label_clear_layout (label);
754 755
}

756 757 758 759 760 761 762
static void 
gtk_label_direction_changed (GtkWidget        *widget,
			     GtkTextDirection previous_dir)
{
  GtkLabel *label = GTK_LABEL (widget);

  if (label->layout)
763
    pango_layout_context_changed (label->layout);
764 765 766 767

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

768
#if 0
Owen Taylor's avatar
Owen Taylor committed
769
static void
770 771 772
gtk_label_paint_word (GtkLabel     *label,
		      gint          x,
		      gint          y,
Owen Taylor's avatar
Owen Taylor committed
773 774 775 776 777 778
		      GtkLabelWord *word,
		      GdkRectangle *area)
{
  GtkWidget *widget = GTK_WIDGET (label);
  GtkLabelULine *uline;
  gchar *tmp_str;
779
  
Owen Taylor's avatar
Owen Taylor committed
780
  tmp_str = gdk_wcstombs (word->beginning);
781 782 783 784 785 786 787 788 789
  if (tmp_str)
    {
      gtk_paint_string (widget->style, widget->window, widget->state,
			area, widget, "label", 
			x + word->x,
			y + word->y,
			tmp_str);
      g_free (tmp_str);
    }
Owen Taylor's avatar
Owen Taylor committed
790
  
791
  for (uline = word->uline; uline; uline = uline->next)
Owen Taylor's avatar
Owen Taylor committed
792 793 794
    gtk_paint_hline (widget->style, widget->window, 
		     widget->state, area,
		     widget, "label", 
795
		     x + uline->x1, x + uline->x2, y + uline->y);
Elliot Lee's avatar
Elliot Lee committed
796
}
797
#endif
798

799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832
static void
get_layout_location (GtkLabel  *label,
                     gint      *xp,
                     gint      *yp)
{
  GtkMisc *misc;
  GtkWidget *widget;
  gfloat xalign;
  gint x, y;
  
  misc = GTK_MISC (label);
  widget = GTK_WIDGET (label);
  
  if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_LTR)
    xalign = misc->xalign;
  else
    xalign = 1.0 - misc->xalign;
  
  x = floor (widget->allocation.x + (gint)misc->xpad
             + ((widget->allocation.width - widget->requisition.width) * xalign)
             + 0.5);
  
  y = floor (widget->allocation.y + (gint)misc->ypad 
             + ((widget->allocation.height - widget->requisition.height) * misc->yalign)
             + 0.5);
  

  if (xp)
    *xp = x;

  if (yp)
    *yp = y;
}

Elliot Lee's avatar
Elliot Lee committed
833
static gint
Owen Taylor's avatar
Owen Taylor committed
834
gtk_label_expose (GtkWidget      *widget,
Elliot Lee's avatar
Elliot Lee committed
835 836 837 838
		  GdkEventExpose *event)
{
  GtkLabel *label;
  gint x, y;
839
  
Elliot Lee's avatar
Elliot Lee committed
840 841
  g_return_val_if_fail (GTK_IS_LABEL (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
842
  
Arturo Espinosa's avatar
Arturo Espinosa committed
843
  label = GTK_LABEL (widget);
844

845 846
  gtk_label_ensure_layout (label, NULL, NULL);
  
847
  if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget) &&
848
      label->label && (*label->label != '\0'))
Elliot Lee's avatar
Elliot Lee committed
849
    {
850
      get_layout_location (label, &x, &y);
851
      
852 853 854 855 856 857 858 859
      gtk_paint_layout (widget->style,
                        widget->window,
                        GTK_WIDGET_STATE (widget),
                        &event->area,
                        widget,
                        "label",
                        x, y,
                        label->layout);
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898
      
      if (label->select_info &&
          (label->select_info->selection_anchor !=
           label->select_info->selection_end))
        {
          gint range[2];
          GdkRegion *clip;
          
          range[0] = label->select_info->selection_anchor;
          range[1] = label->select_info->selection_end;

          if (range[0] > range[1])
            {
              gint tmp = range[0];
              range[0] = range[1];
              range[1] = tmp;
            }

          clip = gdk_pango_layout_get_clip_region (label->layout,
                                                   x, y,
                                                   range,
                                                   1);

          /* FIXME should use gtk_paint, but it can't use a clip
           * region
           */

          gdk_gc_set_clip_region (widget->style->white_gc, clip);
          
          gdk_draw_layout_with_colors (widget->window,
                                       widget->style->white_gc,
                                       x, y,
                                       label->layout,
                                       &widget->style->fg[GTK_STATE_SELECTED],
                                       &widget->style->bg[GTK_STATE_SELECTED]);

          gdk_gc_set_clip_region (widget->style->white_gc, NULL);
          gdk_region_destroy (clip);
        }
899
    }
900

Elliot Lee's avatar
Elliot Lee committed
901 902
  return TRUE;
}
903

904
guint      
905
gtk_label_parse_uline (GtkLabel    *label,
906
		       const gchar *str)
907 908
{
  guint accel_key = GDK_VoidSymbol;
909 910

  gchar *new_str;
Owen Taylor's avatar
Owen Taylor committed
911
  gchar *pattern;
912 913
  const gchar *src;
  gchar *dest, *pattern_dest;
914
  gboolean underscore;
915
      
916
  g_return_val_if_fail (GTK_IS_LABEL (label), GDK_VoidSymbol);
917
  g_return_val_if_fail (str != NULL, GDK_VoidSymbol);
918

919
  /* Convert text to wide characters */
920

921
  new_str = g_new (gchar, strlen (str) + 1);
922
  pattern = g_new (gchar, g_utf8_strlen (str, -1) + 1);
923
  
924
  underscore = FALSE;
925 926 927 928

  src = str;
  dest = new_str;
  pattern_dest = pattern;
929
  
930
  while (*src)
931
    {
932
      gunichar c;
933
      gchar *next_src;
934 935 936

      c = g_utf8_get_char (src);
      if (c == (gunichar)-1)
937 938 939 940 941 942
	{
	  g_warning ("Invalid input string");
	  g_free (new_str);
	  g_free (pattern);
	  return GDK_VoidSymbol;
	}
943
      next_src = g_utf8_next_char (src);
944
      
945 946
      if (underscore)
	{
947 948
	  if (c == '_')
	    *pattern_dest++ = ' ';
949 950
	  else
	    {
951
	      *pattern_dest++ = '_';
952
	      if (accel_key == GDK_VoidSymbol)
953
		accel_key = gdk_keyval_to_lower (c);
954
	    }
955 956 957

	  while (src < next_src)
	    *dest++ = *src++;
958
	  
959 960 961 962
	  underscore = FALSE;
	}
      else
	{
963 964 965 966 967
	  if (c == '_')
	    {
	      underscore = TRUE;
	      src = next_src;
	    }
968 969
	  else
	    {
970 971 972 973
	      while (src < next_src)
		*dest++ = *src++;
	  
	      *pattern_dest++ = ' ';
974 975 976
	    }
	}
    }
977 978
  *dest = 0;
  *pattern_dest = 0;
979
  
980
  gtk_label_set_text_internal (label, new_str);
981 982 983
  gtk_label_set_pattern (label, pattern);
  
  g_free (pattern);
984
  
985 986
  return accel_key;
}
987 988 989 990 991 992 993 994 995 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 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 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 1162 1163 1164 1165 1166 1167 1168 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 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275

static void
gtk_label_realize (GtkWidget *widget)
{
  GtkLabel *label;

  label = GTK_LABEL (widget);
  
  (* GTK_WIDGET_CLASS (parent_class)->realize) (widget);

  if (label->select_info)
    gtk_label_create_window (label);
}

static void
gtk_label_unrealize (GtkWidget *widget)
{
  GtkLabel *label;

  label = GTK_LABEL (widget);

  if (label->select_info)
    gtk_label_destroy_window (label);
  
  (* GTK_WIDGET_CLASS (parent_class)->unrealize) (widget);
}

static void
gtk_label_map (GtkWidget *widget)
{
  GtkLabel *label;

  label = GTK_LABEL (widget);
  
  (* GTK_WIDGET_CLASS (parent_class)->map) (widget);
  
  if (label->select_info)
    gdk_window_show (label->select_info->window);
}

static void
gtk_label_unmap (GtkWidget *widget)
{
  GtkLabel *label;

  label = GTK_LABEL (widget);

  if (label->select_info)
    gdk_window_hide (label->select_info->window);
  
  (* GTK_WIDGET_CLASS (parent_class)->unmap) (widget);
}

static void
window_to_layout_coords (GtkLabel *label,
                         gint     *x,
                         gint     *y)
{
  gint lx, ly;
  GtkWidget *widget;

  widget = GTK_WIDGET (label);
  
  /* get layout location in widget->window coords */
  get_layout_location (label, &lx, &ly);
  
  if (x)
    {
      *x += widget->allocation.x; /* go to widget->window */
      *x -= lx;                   /* go to layout */
    }

  if (y)
    {
      *y += widget->allocation.y; /* go to widget->window */
      *y -= ly;                   /* go to layout */
    }
}

static void
layout_to_window_coords (GtkLabel *label,
                         gint     *x,
                         gint     *y)
{
  gint lx, ly;
  GtkWidget *widget;

  widget = GTK_WIDGET (label);
  
  /* get layout location in widget->window coords */
  get_layout_location (label, &lx, &ly);
  
  if (x)
    {
      *x += lx;                   /* go to widget->window */
      *x -= widget->allocation.x; /* go to selection window */
    }

  if (y)
    {
      *y += ly;                   /* go to widget->window */
      *y -= widget->allocation.y; /* go to selection window */
    }
}

static void
get_layout_index (GtkLabel *label,
                  gint      x,
                  gint      y,
                  gint     *index)
{
  gint trailing = 0;
  const gchar *cluster;
  const gchar *cluster_end;

  *index = 0;
  
  gtk_label_ensure_layout (label, NULL, NULL);
  
  window_to_layout_coords (label, &x, &y);

  x *= PANGO_SCALE;
  y *= PANGO_SCALE;
  
  pango_layout_xy_to_index (label->layout,
                            x, y,
                            index, &trailing);

  
  cluster = label->label + *index;
  cluster_end = cluster;
  while (trailing)
    {
      cluster_end = g_utf8_next_char (cluster_end);
      --trailing;
    }

  *index += (cluster_end - cluster);
}

static gint
gtk_label_button_press (GtkWidget      *widget,
                        GdkEventButton *event)
{
  GtkLabel *label;
  gint index = 0;
  
  label = GTK_LABEL (widget);

  if (label->select_info == NULL)
    return FALSE;

  if (event->button != 1)
    return FALSE;

  get_layout_index (label, event->x, event->y, &index);
  
  if ((label->select_info->selection_anchor !=
       label->select_info->selection_end) &&
      (event->state & GDK_SHIFT_MASK))
    {
      /* extend (same as motion) */
      if (index < label->select_info->selection_end)
        gtk_label_select_region_index (label,
                                       index,
                                       label->select_info->selection_end);
      else
        gtk_label_select_region_index (label,
                                       label->select_info->selection_anchor,
                                       index);

      /* ensure the anchor is opposite index */
      if (index == label->select_info->selection_anchor)
        {
          gint tmp = label->select_info->selection_end;
          label->select_info->selection_end = label->select_info->selection_anchor;
          label->select_info->selection_anchor = tmp;
        }
    }
  else
    {
      /* start a replacement */
      gtk_label_select_region_index (label, index, index);
    }
  
  return TRUE;
}

static gint
gtk_label_button_release (GtkWidget      *widget,
                          GdkEventButton *event)

{
  GtkLabel *label;

  label = GTK_LABEL (widget);
  
  if (label->select_info == NULL)
    return FALSE;
  
  if (event->button != 1)
    return FALSE;
  
  /* The goal here is to return TRUE iff we ate the
   * button press to start selecting.
   */
  
  return TRUE;
}

static gint
gtk_label_motion (GtkWidget      *widget,
                  GdkEventMotion *event)