gtklabel.c 12.7 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * 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
Tim Janik's avatar
Tim Janik committed
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
Elliot Lee's avatar
Elliot Lee committed
12 13 14
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
15 16 17
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
Elliot Lee's avatar
Elliot Lee committed
18 19 20
 */
#include <string.h>
#include "gtklabel.h"
21
#include "gdk/gdkkeysyms.h"
Elliot Lee's avatar
Elliot Lee committed
22

23 24 25
enum {
  ARG_0,
  ARG_LABEL,
26
  ARG_PATTERN,
27 28 29
  ARG_JUSTIFY
};

30 31 32 33 34 35 36 37 38 39 40
typedef struct _GtkLabelRow GtkLabelRow;

struct _GtkLabelRow {
  gint index;
  gint width;
  gint height;
  gint len;
};

GMemChunk *row_mem_chunk = NULL;

Elliot Lee's avatar
Elliot Lee committed
41
static void gtk_label_class_init   (GtkLabelClass  *klass);
Tim Janik's avatar
Tim Janik committed
42
static void gtk_label_init	   (GtkLabel	   *label);
43
static void gtk_label_set_arg	   (GtkObject	   *object,
Tim Janik's avatar
Tim Janik committed
44 45
				    GtkArg	   *arg,
				    guint	    arg_id);
46
static void gtk_label_get_arg	   (GtkObject      *object,
Tim Janik's avatar
Tim Janik committed
47 48 49 50
				    GtkArg	   *arg,
				    guint	    arg_id);
static void gtk_label_finalize	   (GtkObject	   *object);
static void gtk_label_size_request (GtkWidget	   *widget,
Elliot Lee's avatar
Elliot Lee committed
51
				    GtkRequisition *requisition);
Tim Janik's avatar
Tim Janik committed
52
static gint gtk_label_expose	   (GtkWidget	   *widget,
Elliot Lee's avatar
Elliot Lee committed
53
				    GdkEventExpose *event);
54
static void gtk_label_free_rows    (GtkLabel       *label);
Elliot Lee's avatar
Elliot Lee committed
55 56 57 58 59


static GtkMiscClass *parent_class = NULL;


Tim Janik's avatar
Tim Janik committed
60
GtkType
61
gtk_label_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
62
{
Tim Janik's avatar
Tim Janik committed
63 64
  static GtkType label_type = 0;
  
Elliot Lee's avatar
Elliot Lee committed
65 66
  if (!label_type)
    {
67
      static const GtkTypeInfo label_info =
Elliot Lee's avatar
Elliot Lee committed
68 69 70 71 72 73
      {
	"GtkLabel",
	sizeof (GtkLabel),
	sizeof (GtkLabelClass),
	(GtkClassInitFunc) gtk_label_class_init,
	(GtkObjectInitFunc) gtk_label_init,
74 75
        /* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
76
	(GtkClassInitFunc) NULL,
Elliot Lee's avatar
Elliot Lee committed
77
      };
Tim Janik's avatar
Tim Janik committed
78
      
Elliot Lee's avatar
Elliot Lee committed
79
      label_type = gtk_type_unique (gtk_misc_get_type (), &label_info);
80
      gtk_type_set_chunk_alloc (label_type, 32);
Elliot Lee's avatar
Elliot Lee committed
81
    }
Tim Janik's avatar
Tim Janik committed
82
  
Elliot Lee's avatar
Elliot Lee committed
83 84 85
  return label_type;
}

86
static void
Elliot Lee's avatar
Elliot Lee committed
87 88 89 90
gtk_label_class_init (GtkLabelClass *class)
{
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
Tim Janik's avatar
Tim Janik committed
91
  
Elliot Lee's avatar
Elliot Lee committed
92 93
  object_class = (GtkObjectClass*) class;
  widget_class = (GtkWidgetClass*) class;
Tim Janik's avatar
Tim Janik committed
94
  
Elliot Lee's avatar
Elliot Lee committed
95
  parent_class = gtk_type_class (gtk_misc_get_type ());
Tim Janik's avatar
Tim Janik committed
96
  
97
  gtk_object_add_arg_type ("GtkLabel::label", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_LABEL);
98
  gtk_object_add_arg_type ("GtkLabel::pattern", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_PATTERN);
99
  gtk_object_add_arg_type ("GtkLabel::justify", GTK_TYPE_JUSTIFICATION, GTK_ARG_READWRITE, ARG_JUSTIFY);
100 101 102

  object_class->set_arg = gtk_label_set_arg;
  object_class->get_arg = gtk_label_get_arg;
103
  object_class->finalize = gtk_label_finalize;
Tim Janik's avatar
Tim Janik committed
104
  
Elliot Lee's avatar
Elliot Lee committed
105 106 107 108
  widget_class->size_request = gtk_label_size_request;
  widget_class->expose_event = gtk_label_expose;
}

109
static void
110
gtk_label_set_arg (GtkObject	  *object,
Tim Janik's avatar
Tim Janik committed
111 112
		   GtkArg	  *arg,
		   guint	   arg_id)
113
{
114 115 116 117
  GtkLabel *label;

  label = GTK_LABEL (object);

118 119 120
  switch (arg_id)
    {
    case ARG_LABEL:
121
      gtk_label_set (label, GTK_VALUE_STRING (*arg) ? GTK_VALUE_STRING (*arg) : "");
122
      break;
123 124 125
    case ARG_PATTERN:
      gtk_label_set_pattern (label, GTK_VALUE_STRING (*arg));
      break;
126 127 128
    case ARG_JUSTIFY:
      gtk_label_set_justify (label, GTK_VALUE_ENUM (*arg));
      break;
Tim Janik's avatar
Tim Janik committed
129 130
    default:
      break;
131 132 133
    }
}

Tim Janik's avatar
Tim Janik committed
134
static void
135
gtk_label_get_arg (GtkObject	  *object,
Tim Janik's avatar
Tim Janik committed
136 137
		   GtkArg	  *arg,
		   guint	   arg_id)
138
{
139 140 141 142
  GtkLabel *label;

  label = GTK_LABEL (object);

143 144 145 146 147
  switch (arg_id)
    {
    case ARG_LABEL:
      GTK_VALUE_STRING (*arg) = g_strdup (label->label);
      break;
148 149 150
    case ARG_PATTERN:
      GTK_VALUE_STRING (*arg) = g_strdup (label->pattern);
      break;
151 152 153 154 155 156 157 158 159
    case ARG_JUSTIFY:
      GTK_VALUE_ENUM (*arg) = label->jtype;
      break;
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

160
static void
Elliot Lee's avatar
Elliot Lee committed
161 162 163
gtk_label_init (GtkLabel *label)
{
  GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
Tim Janik's avatar
Tim Janik committed
164
  
Elliot Lee's avatar
Elliot Lee committed
165 166
  label->label = NULL;
  label->row = NULL;
Tim Janik's avatar
Tim Janik committed
167
  label->max_width = 0;
Elliot Lee's avatar
Elliot Lee committed
168
  label->jtype = GTK_JUSTIFY_CENTER;
169
  label->pattern = NULL;
Tim Janik's avatar
Tim Janik committed
170
  
171
  gtk_label_set (label, "");
Elliot Lee's avatar
Elliot Lee committed
172 173 174
}

GtkWidget*
Tim Janik's avatar
Tim Janik committed
175
gtk_label_new (const gchar *str)
Elliot Lee's avatar
Elliot Lee committed
176 177
{
  GtkLabel *label;
Tim Janik's avatar
Tim Janik committed
178
  
Elliot Lee's avatar
Elliot Lee committed
179
  g_return_val_if_fail (str != NULL, NULL);
Tim Janik's avatar
Tim Janik committed
180
  
Elliot Lee's avatar
Elliot Lee committed
181
  label = gtk_type_new (gtk_label_get_type ());
Tim Janik's avatar
Tim Janik committed
182
  
Elliot Lee's avatar
Elliot Lee committed
183
  gtk_label_set (label, str);
Tim Janik's avatar
Tim Janik committed
184
  
Elliot Lee's avatar
Elliot Lee committed
185 186 187 188
  return GTK_WIDGET (label);
}

void
Tim Janik's avatar
Tim Janik committed
189 190
gtk_label_set (GtkLabel	   *label,
	       const gchar *str)
Elliot Lee's avatar
Elliot Lee committed
191 192
{
  char* p;
Tim Janik's avatar
Tim Janik committed
193
  
Elliot Lee's avatar
Elliot Lee committed
194 195 196
  g_return_if_fail (label != NULL);
  g_return_if_fail (GTK_IS_LABEL (label));
  g_return_if_fail (str != NULL);
197 198 199

  if (!row_mem_chunk)
    row_mem_chunk = g_mem_chunk_create (GtkLabelRow, 64, G_ALLOC_AND_FREE);
Tim Janik's avatar
Tim Janik committed
200
  
Elliot Lee's avatar
Elliot Lee committed
201 202 203
  if (label->label)
    g_free (label->label);
  label->label = g_strdup (str);
Tim Janik's avatar
Tim Janik committed
204
  
Elliot Lee's avatar
Elliot Lee committed
205
  if (label->row)
206 207
    gtk_label_free_rows (label);

Elliot Lee's avatar
Elliot Lee committed
208
  p = label->label;
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
  do {
    GtkLabelRow *row = g_chunk_new (GtkLabelRow, row_mem_chunk);
    label->row = g_slist_append (label->row, row);

    row->index = p - label->label;
    
    p = strchr(p, '\n');
    if (p)
      {
	p++;
	row->len = (p - label->label) - row->index;
      }
    else
      row->len = strlen (label->label) - row->index;
  } while (p);

  if (GTK_WIDGET_VISIBLE (label))
    {
      if (GTK_WIDGET_MAPPED (label))
228 229
	gtk_widget_queue_clear (GTK_WIDGET (label));

230 231 232 233 234 235 236 237 238 239 240 241 242 243
      gtk_widget_queue_resize (GTK_WIDGET (label));
    }
}

void
gtk_label_set_pattern (GtkLabel	   *label,
		       const gchar *pattern)
{
  g_return_if_fail (label != NULL);
  g_return_if_fail (GTK_IS_LABEL (label));
  
  if (label->pattern)
    g_free (label->pattern);
  label->pattern = g_strdup (pattern);
Tim Janik's avatar
Tim Janik committed
244
  
Elliot Lee's avatar
Elliot Lee committed
245 246 247
  if (GTK_WIDGET_VISIBLE (label))
    {
      if (GTK_WIDGET_MAPPED (label))
248
	gtk_widget_queue_clear (GTK_WIDGET (label));
Tim Janik's avatar
Tim Janik committed
249
      
Elliot Lee's avatar
Elliot Lee committed
250 251 252 253 254
      gtk_widget_queue_resize (GTK_WIDGET (label));
    }
}

void
Tim Janik's avatar
Tim Janik committed
255 256
gtk_label_set_justify (GtkLabel	       *label,
		       GtkJustification jtype)
Elliot Lee's avatar
Elliot Lee committed
257 258 259
{
  g_return_if_fail (label != NULL);
  g_return_if_fail (GTK_IS_LABEL (label));
Tim Janik's avatar
Tim Janik committed
260
  
Elliot Lee's avatar
Elliot Lee committed
261 262 263 264 265
  if ((GtkJustification) label->jtype != jtype)
    {
      label->jtype = jtype;
      
      if (GTK_WIDGET_VISIBLE (label))
Tim Janik's avatar
Tim Janik committed
266 267
	{
	  if (GTK_WIDGET_MAPPED (label))
268 269
	    gtk_widget_queue_clear (GTK_WIDGET (label));

Tim Janik's avatar
Tim Janik committed
270 271
	  gtk_widget_queue_resize (GTK_WIDGET (label));
	}
Elliot Lee's avatar
Elliot Lee committed
272 273 274 275
    }
}

void
Tim Janik's avatar
Tim Janik committed
276 277
gtk_label_get (GtkLabel	 *label,
	       gchar	**str)
Elliot Lee's avatar
Elliot Lee committed
278 279 280 281
{
  g_return_if_fail (label != NULL);
  g_return_if_fail (GTK_IS_LABEL (label));
  g_return_if_fail (str != NULL);
Tim Janik's avatar
Tim Janik committed
282
  
Elliot Lee's avatar
Elliot Lee committed
283 284 285 286 287
  *str = label->label;
}


static void
288
gtk_label_finalize (GtkObject *object)
Elliot Lee's avatar
Elliot Lee committed
289 290
{
  GtkLabel *label;
291
  
Elliot Lee's avatar
Elliot Lee committed
292 293
  g_return_if_fail (object != NULL);
  g_return_if_fail (GTK_IS_LABEL (object));
294
  
Elliot Lee's avatar
Elliot Lee committed
295
  label = GTK_LABEL (object);
296
  
Elliot Lee's avatar
Elliot Lee committed
297
  g_free (label->label);
298
  if (label->pattern) g_free (label->pattern);
299
  gtk_label_free_rows (label);
300 301
  
  (* GTK_OBJECT_CLASS (parent_class)->finalize) (object);
Elliot Lee's avatar
Elliot Lee committed
302 303
}

304
static gint
305 306 307 308 309
gtk_label_process_row (GtkLabel     *label,
		       GtkLabelRow  *row,
		       gint          x, gint y,
		       gboolean      draw,
		       GdkRectangle *area)
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
{
  GtkWidget *widget = GTK_WIDGET (label);
  
  char lastc;
  gint i, j;
  gint offset;
  gint height;
  gint pattern_length;

  if (label->pattern) 
    pattern_length = strlen (label->pattern);
  else
    pattern_length = 0;

  offset = 0;
  height = widget->style->font->ascent + widget->style->font->descent;
  
  if (draw)
    {
      if (label->jtype == GTK_JUSTIFY_CENTER)
	offset = (label->max_width - row->width) / 2;
      else if (label->jtype == GTK_JUSTIFY_RIGHT)
	offset = (label->max_width - row->width);
    }
      
  if (label->pattern && (row->index < pattern_length))
    lastc = label->pattern[0];
  else
    lastc = ' ';
  
  j = 0;
  for (i=1; i<=row->len; i++)
    {
      char c;

      if (label->pattern && (row->index + i < pattern_length))
	c = label->pattern[row->index+i];
      else
	c = ' ';

      if ((i == row->len) || (lastc != c))
	{
	  gint width = 0;

	  if (lastc == '_')
	    {
	      gint descent;
	      gint rbearing;
	      gint lbearing;

	      gdk_text_extents (widget->style->font,
				&label->label[row->index+j], i - j,
				&lbearing, &rbearing, &width, NULL, &descent);

	      if (draw)
		{
366 367 368 369 370 371
		  gtk_paint_hline (widget->style, widget->window, 
				   widget->state, area,
				   widget, "label", 
				   offset + x + lbearing - 1, 
				   offset + x + rbearing, 
				   y + descent + 2);
372 373 374 375 376 377 378 379 380 381 382 383 384 385
		}

	      height = MAX (height, 
			    widget->style->font->ascent + descent + 2);
	    }
	  else if (i != row->len)
	    {
	      width = gdk_text_width (widget->style->font, 
				      &label->label[row->index+j], 
				      i - j);
	    }

	  if (draw)
	    {
386 387 388 389 390 391
	      char save = label->label[row->index + i];
	      label->label[row->index + i] = '\0';
	      gtk_paint_string(widget->style, widget->window, widget->state,
			       area, widget, "label", offset + x, y, 
			       &label->label[row->index+j]);
	      label->label[row->index + i] = save;
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
	    }

		  
	  offset += width;

	  if (i != row->len)
	    {
	      lastc = c;
	      j = i;
	    }
	}
    }

  return height;
}

Elliot Lee's avatar
Elliot Lee committed
408 409 410 411 412
static void
gtk_label_size_request (GtkWidget      *widget,
			GtkRequisition *requisition)
{
  GtkLabel *label;
413
  GSList *tmp_list;
Elliot Lee's avatar
Elliot Lee committed
414
  gint width;
Tim Janik's avatar
Tim Janik committed
415
  
Elliot Lee's avatar
Elliot Lee committed
416 417 418
  g_return_if_fail (widget != NULL);
  g_return_if_fail (GTK_IS_LABEL (widget));
  g_return_if_fail (requisition != NULL);
Tim Janik's avatar
Tim Janik committed
419
  
Elliot Lee's avatar
Elliot Lee committed
420
  label = GTK_LABEL (widget);
Tim Janik's avatar
Tim Janik committed
421
  
422 423 424
  requisition->height = label->misc.ypad * 2;

  tmp_list = label->row;
Elliot Lee's avatar
Elliot Lee committed
425
  width = 0;
426
  while (tmp_list)
Elliot Lee's avatar
Elliot Lee committed
427
    {
428 429 430 431 432 433 434
      GtkLabelRow *row = tmp_list->data;

      row->width = gdk_text_width (GTK_WIDGET (label)->style->font,
				   &label->label [row->index],
				   row->len);
      width = MAX (width, row->width);

435
      requisition->height += gtk_label_process_row (label, row, 0, 0, FALSE, NULL) + 2;
436 437

      tmp_list = tmp_list->next;
Elliot Lee's avatar
Elliot Lee committed
438
    }
Tim Janik's avatar
Tim Janik committed
439 440

  label->max_width = width;
Elliot Lee's avatar
Elliot Lee committed
441 442
  requisition->width = width + label->misc.xpad * 2;
}
443
  
Elliot Lee's avatar
Elliot Lee committed
444
static gint
Tim Janik's avatar
Tim Janik committed
445
gtk_label_expose (GtkWidget	 *widget,
Elliot Lee's avatar
Elliot Lee committed
446 447 448 449
		  GdkEventExpose *event)
{
  GtkLabel *label;
  GtkMisc *misc;
450
  GSList *tmp_list;
Elliot Lee's avatar
Elliot Lee committed
451
  gint x, y;
Tim Janik's avatar
Tim Janik committed
452
  
Elliot Lee's avatar
Elliot Lee committed
453 454 455
  g_return_val_if_fail (widget != NULL, FALSE);
  g_return_val_if_fail (GTK_IS_LABEL (widget), FALSE);
  g_return_val_if_fail (event != NULL, FALSE);
Tim Janik's avatar
Tim Janik committed
456
  
Elliot Lee's avatar
Elliot Lee committed
457 458 459 460
  if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
    {
      label = GTK_LABEL (widget);
      misc = GTK_MISC (widget);
Tim Janik's avatar
Tim Janik committed
461
      
462 463
      /*
       * GC Clipping
Elliot Lee's avatar
Elliot Lee committed
464
       */
465
      gdk_gc_set_clip_rectangle (widget->style->white_gc, &event->area);
466
      gdk_gc_set_clip_rectangle (widget->style->fg_gc[widget->state], &event->area);
467

468
      x = widget->allocation.x + misc->xpad +
Tim Janik's avatar
Tim Janik committed
469
	(widget->allocation.width - (label->max_width + label->misc.xpad * 2))
470
	* misc->xalign + 0.5;
Tim Janik's avatar
Tim Janik committed
471
      
472 473 474 475
      y = (widget->allocation.y * (1.0 - misc->yalign) +
	   (widget->allocation.y + widget->allocation.height -
	    (widget->requisition.height - misc->ypad * 2)) *
	   misc->yalign + widget->style->font->ascent) + 1.5;
476

477 478 479 480 481
      /* 
       * COMMENT: we can avoid gdk_text_width() calls here storing in label->row
       * the widths of the rows calculated in gtk_label_set.
       * Once we have a wrapping interface we can support GTK_JUSTIFY_FILL.
       */
482 483 484 485

      tmp_list = label->row;
      while (tmp_list)
	{
486
	  y += gtk_label_process_row (label, tmp_list->data, x, y, TRUE, &event->area) + 2;
487 488
	  tmp_list = tmp_list->next;
	}
Tim Janik's avatar
Tim Janik committed
489
      
490
      gdk_gc_set_clip_mask (widget->style->white_gc, NULL);
491
      gdk_gc_set_clip_mask (widget->style->fg_gc[widget->state], NULL);
492
    }
Elliot Lee's avatar
Elliot Lee committed
493 494 495
  return TRUE;
}

496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 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 559 560 561 562
static void
gtk_label_free_rows (GtkLabel *label)
{
  GSList *tmp_list;

  tmp_list = label->row;
  while (tmp_list)
    {
      g_mem_chunk_free (row_mem_chunk, tmp_list->data);
      tmp_list = tmp_list->next;
    }
  g_slist_free (label->row);
  label->row = NULL;
}

guint      
gtk_label_parse_uline (GtkLabel         *label,
		       const gchar      *string)
{
  guint accel_key = GDK_VoidSymbol;
  const gchar *p;
  gchar *q, *r;
  gchar *name, *pattern;

  gint length;
  gboolean underscore;

  length = strlen (string);
  
  name = g_new (gchar, length+1);
  pattern = g_new (gchar, length+1);

  underscore = FALSE;

  p = string;
  q = name;
  r = pattern;

  while (*p)
    {
      if (underscore)
	{
	  if (*p == '_')
	    *r++ = ' ';
	  else
	    {
	      *r++ = '_';
	      if (accel_key == GDK_VoidSymbol)
		accel_key = gdk_keyval_to_lower (*p);
	    }

	  *q++ = *p;
	  underscore = FALSE;
	}
      else
	{
	  if (*p == '_')
	    underscore = TRUE;
	  else
	    {
	      *q++ = *p;
	      *r++ = ' ';
	    }
	}
      p++;
    }
  *q = 0;
563 564
  *r = 0;
 
565 566 567 568 569 570 571 572
  gtk_label_set (label, name);
  gtk_label_set_pattern (label, pattern);
  
  g_free (name);
  g_free (pattern);

  return accel_key;
}