gtklabel.c 10.9 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 21 22
 */
#include <string.h>
#include "gtklabel.h"


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

Elliot Lee's avatar
Elliot Lee committed
29
static void gtk_label_class_init   (GtkLabelClass  *klass);
Tim Janik's avatar
Tim Janik committed
30
static void gtk_label_init	   (GtkLabel	   *label);
31
static void gtk_label_set_arg	   (GtkObject	   *object,
Tim Janik's avatar
Tim Janik committed
32 33
				    GtkArg	   *arg,
				    guint	    arg_id);
34
static void gtk_label_get_arg	   (GtkObject      *object,
Tim Janik's avatar
Tim Janik committed
35 36 37 38
				    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
39
				    GtkRequisition *requisition);
Tim Janik's avatar
Tim Janik committed
40
static gint gtk_label_expose	   (GtkWidget	   *widget,
Elliot Lee's avatar
Elliot Lee committed
41
				    GdkEventExpose *event);
Tim Janik's avatar
Tim Janik committed
42
static void gtk_label_state_changed (GtkWidget	    *widget,
43
				     guint	     previous_state);
Tim Janik's avatar
Tim Janik committed
44 45
static void gtk_label_style_set	    (GtkWidget	    *widget,
				     GtkStyle	    *previous_style);
46

Elliot Lee's avatar
Elliot Lee committed
47 48 49 50 51


static GtkMiscClass *parent_class = NULL;


Tim Janik's avatar
Tim Janik committed
52
GtkType
53
gtk_label_get_type (void)
Elliot Lee's avatar
Elliot Lee committed
54
{
Tim Janik's avatar
Tim Janik committed
55 56
  static GtkType label_type = 0;
  
Elliot Lee's avatar
Elliot Lee committed
57 58 59 60 61 62 63 64 65
  if (!label_type)
    {
      GtkTypeInfo label_info =
      {
	"GtkLabel",
	sizeof (GtkLabel),
	sizeof (GtkLabelClass),
	(GtkClassInitFunc) gtk_label_class_init,
	(GtkObjectInitFunc) gtk_label_init,
66 67
        /* reserved_1 */ NULL,
	/* reserved_2 */ NULL,
68
	(GtkClassInitFunc) NULL,
Elliot Lee's avatar
Elliot Lee committed
69
      };
Tim Janik's avatar
Tim Janik committed
70
      
Elliot Lee's avatar
Elliot Lee committed
71
      label_type = gtk_type_unique (gtk_misc_get_type (), &label_info);
72
      gtk_type_set_chunk_alloc (label_type, 32);
Elliot Lee's avatar
Elliot Lee committed
73
    }
Tim Janik's avatar
Tim Janik committed
74
  
Elliot Lee's avatar
Elliot Lee committed
75 76 77
  return label_type;
}

78
static void
Elliot Lee's avatar
Elliot Lee committed
79 80 81 82
gtk_label_class_init (GtkLabelClass *class)
{
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
Tim Janik's avatar
Tim Janik committed
83
  
Elliot Lee's avatar
Elliot Lee committed
84 85
  object_class = (GtkObjectClass*) class;
  widget_class = (GtkWidgetClass*) class;
Tim Janik's avatar
Tim Janik committed
86
  
Elliot Lee's avatar
Elliot Lee committed
87
  parent_class = gtk_type_class (gtk_misc_get_type ());
Tim Janik's avatar
Tim Janik committed
88
  
89
  gtk_object_add_arg_type ("GtkLabel::label", GTK_TYPE_STRING, GTK_ARG_READWRITE, ARG_LABEL);
90
  gtk_object_add_arg_type ("GtkLabel::justify", GTK_TYPE_JUSTIFICATION, GTK_ARG_READWRITE, ARG_JUSTIFY);
91 92 93

  object_class->set_arg = gtk_label_set_arg;
  object_class->get_arg = gtk_label_get_arg;
94
  object_class->finalize = gtk_label_finalize;
Tim Janik's avatar
Tim Janik committed
95
  
Elliot Lee's avatar
Elliot Lee committed
96 97
  widget_class->size_request = gtk_label_size_request;
  widget_class->expose_event = gtk_label_expose;
98 99
  widget_class->style_set    = gtk_label_style_set;
  widget_class->state_changed = gtk_label_state_changed;
Elliot Lee's avatar
Elliot Lee committed
100 101
}

102
static void
103
gtk_label_set_arg (GtkObject	  *object,
Tim Janik's avatar
Tim Janik committed
104 105
		   GtkArg	  *arg,
		   guint	   arg_id)
106
{
107 108 109 110
  GtkLabel *label;

  label = GTK_LABEL (object);

111 112 113
  switch (arg_id)
    {
    case ARG_LABEL:
114
      gtk_label_set (label, GTK_VALUE_STRING (*arg) ? GTK_VALUE_STRING (*arg) : "");
115 116 117 118
      break;
    case ARG_JUSTIFY:
      gtk_label_set_justify (label, GTK_VALUE_ENUM (*arg));
      break;
Tim Janik's avatar
Tim Janik committed
119 120
    default:
      break;
121 122 123
    }
}

Tim Janik's avatar
Tim Janik committed
124
static void
125
gtk_label_get_arg (GtkObject	  *object,
Tim Janik's avatar
Tim Janik committed
126 127
		   GtkArg	  *arg,
		   guint	   arg_id)
128
{
129 130 131 132
  GtkLabel *label;

  label = GTK_LABEL (object);

133 134 135 136 137 138 139 140 141 142 143 144 145 146
  switch (arg_id)
    {
    case ARG_LABEL:
      GTK_VALUE_STRING (*arg) = g_strdup (label->label);
      break;
    case ARG_JUSTIFY:
      GTK_VALUE_ENUM (*arg) = label->jtype;
      break;
    default:
      arg->type = GTK_TYPE_INVALID;
      break;
    }
}

147
static void
Elliot Lee's avatar
Elliot Lee committed
148 149 150
gtk_label_init (GtkLabel *label)
{
  GTK_WIDGET_SET_FLAGS (label, GTK_NO_WINDOW);
Tim Janik's avatar
Tim Janik committed
151
  
Elliot Lee's avatar
Elliot Lee committed
152 153
  label->label = NULL;
  label->row = NULL;
Tim Janik's avatar
Tim Janik committed
154
  label->max_width = 0;
Elliot Lee's avatar
Elliot Lee committed
155
  label->jtype = GTK_JUSTIFY_CENTER;
Tim Janik's avatar
Tim Janik committed
156 157
  label->needs_clear = FALSE;
  
158
  gtk_label_set (label, "");
Elliot Lee's avatar
Elliot Lee committed
159 160 161
}

GtkWidget*
Tim Janik's avatar
Tim Janik committed
162
gtk_label_new (const gchar *str)
Elliot Lee's avatar
Elliot Lee committed
163 164
{
  GtkLabel *label;
Tim Janik's avatar
Tim Janik committed
165
  
Elliot Lee's avatar
Elliot Lee committed
166
  g_return_val_if_fail (str != NULL, NULL);
Tim Janik's avatar
Tim Janik committed
167
  
Elliot Lee's avatar
Elliot Lee committed
168
  label = gtk_type_new (gtk_label_get_type ());
Tim Janik's avatar
Tim Janik committed
169
  
Elliot Lee's avatar
Elliot Lee committed
170
  gtk_label_set (label, str);
Tim Janik's avatar
Tim Janik committed
171
  
Elliot Lee's avatar
Elliot Lee committed
172 173 174 175
  return GTK_WIDGET (label);
}

void
Tim Janik's avatar
Tim Janik committed
176 177
gtk_label_set (GtkLabel	   *label,
	       const gchar *str)
Elliot Lee's avatar
Elliot Lee committed
178 179
{
  char* p;
Tim Janik's avatar
Tim Janik committed
180
  
Elliot Lee's avatar
Elliot Lee committed
181 182 183
  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
184
  
Elliot Lee's avatar
Elliot Lee committed
185 186 187
  if (label->label)
    g_free (label->label);
  label->label = g_strdup (str);
Tim Janik's avatar
Tim Janik committed
188
  
Elliot Lee's avatar
Elliot Lee committed
189 190 191 192 193 194 195
  if (label->row)
    g_slist_free (label->row);
  label->row = NULL;
  label->row = g_slist_append (label->row, label->label);
  p = label->label;
  while ((p = strchr(p, '\n')))
    label->row = g_slist_append (label->row, ++p);
Tim Janik's avatar
Tim Janik committed
196
  
Elliot Lee's avatar
Elliot Lee committed
197 198 199 200 201 202 203 204
  if (GTK_WIDGET_VISIBLE (label))
    {
      if (GTK_WIDGET_MAPPED (label))
	gdk_window_clear_area (GTK_WIDGET (label)->window,
			       GTK_WIDGET (label)->allocation.x,
			       GTK_WIDGET (label)->allocation.y,
			       GTK_WIDGET (label)->allocation.width,
			       GTK_WIDGET (label)->allocation.height);
Tim Janik's avatar
Tim Janik committed
205
      
Elliot Lee's avatar
Elliot Lee committed
206 207 208 209 210
      gtk_widget_queue_resize (GTK_WIDGET (label));
    }
}

void
Tim Janik's avatar
Tim Janik committed
211 212
gtk_label_set_justify (GtkLabel	       *label,
		       GtkJustification jtype)
Elliot Lee's avatar
Elliot Lee committed
213 214 215
{
  g_return_if_fail (label != NULL);
  g_return_if_fail (GTK_IS_LABEL (label));
Tim Janik's avatar
Tim Janik committed
216
  
Elliot Lee's avatar
Elliot Lee committed
217 218 219 220 221
  if ((GtkJustification) label->jtype != jtype)
    {
      label->jtype = jtype;
      
      if (GTK_WIDGET_VISIBLE (label))
Tim Janik's avatar
Tim Janik committed
222 223 224 225 226 227 228 229 230 231
	{
	  if (GTK_WIDGET_MAPPED (label))
	    gdk_window_clear_area (GTK_WIDGET (label)->window,
				   GTK_WIDGET (label)->allocation.x,
				   GTK_WIDGET (label)->allocation.y,
				   GTK_WIDGET (label)->allocation.width,
				   GTK_WIDGET (label)->allocation.height);
	  
	  gtk_widget_queue_resize (GTK_WIDGET (label));
	}
Elliot Lee's avatar
Elliot Lee committed
232 233 234 235
    }
}

void
Tim Janik's avatar
Tim Janik committed
236 237
gtk_label_get (GtkLabel	 *label,
	       gchar	**str)
Elliot Lee's avatar
Elliot Lee committed
238 239 240 241
{
  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
242
  
Elliot Lee's avatar
Elliot Lee committed
243 244 245 246 247
  *str = label->label;
}


static void
248
gtk_label_finalize (GtkObject *object)
Elliot Lee's avatar
Elliot Lee committed
249 250
{
  GtkLabel *label;
251
  
Elliot Lee's avatar
Elliot Lee committed
252 253
  g_return_if_fail (object != NULL);
  g_return_if_fail (GTK_IS_LABEL (object));
254
  
Elliot Lee's avatar
Elliot Lee committed
255
  label = GTK_LABEL (object);
256
  
Elliot Lee's avatar
Elliot Lee committed
257 258
  g_free (label->label);
  g_slist_free (label->row);
259 260
  
  (* GTK_OBJECT_CLASS (parent_class)->finalize) (object);
Elliot Lee's avatar
Elliot Lee committed
261 262 263 264 265 266 267 268 269
}

static void
gtk_label_size_request (GtkWidget      *widget,
			GtkRequisition *requisition)
{
  GtkLabel *label;
  GSList *row;
  gint width;
Tim Janik's avatar
Tim Janik committed
270
  
Elliot Lee's avatar
Elliot Lee committed
271 272 273
  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
274
  
Elliot Lee's avatar
Elliot Lee committed
275
  label = GTK_LABEL (widget);
Tim Janik's avatar
Tim Janik committed
276
  
Elliot Lee's avatar
Elliot Lee committed
277 278 279 280 281
  row = label->row;
  width = 0;
  while (row)
    {
      if (row->next)
Tim Janik's avatar
Tim Janik committed
282 283 284 285
	width = MAX (width,
		     gdk_text_width (GTK_WIDGET (label)->style->font,
				     row->data,
				     (gchar*) row->next->data - (gchar*) row->data - 1));
Elliot Lee's avatar
Elliot Lee committed
286
      else
Tim Janik's avatar
Tim Janik committed
287
	width = MAX (width, gdk_string_width (GTK_WIDGET (label)->style->font, row->data));
Elliot Lee's avatar
Elliot Lee committed
288 289
      row = row->next;
    }
Tim Janik's avatar
Tim Janik committed
290 291

  label->max_width = width;
Elliot Lee's avatar
Elliot Lee committed
292 293
  requisition->width = width + label->misc.xpad * 2;
  requisition->height = ((GTK_WIDGET (label)->style->font->ascent +
Tim Janik's avatar
Tim Janik committed
294 295 296
			  GTK_WIDGET (label)->style->font->descent + 2) *
			 g_slist_length(label->row) +
			 label->misc.ypad * 2);
Elliot Lee's avatar
Elliot Lee committed
297 298 299
}

static gint
Tim Janik's avatar
Tim Janik committed
300
gtk_label_expose (GtkWidget	 *widget,
Elliot Lee's avatar
Elliot Lee committed
301 302 303 304 305 306 307 308 309
		  GdkEventExpose *event)
{
  GtkLabel *label;
  GtkMisc *misc;
  GSList *row;
  gint state;
  gint offset;
  gint len;
  gint x, y;
Tim Janik's avatar
Tim Janik committed
310
  
Elliot Lee's avatar
Elliot Lee committed
311 312 313
  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
314
  
Elliot Lee's avatar
Elliot Lee committed
315 316 317 318
  if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget))
    {
      label = GTK_LABEL (widget);
      misc = GTK_MISC (widget);
Tim Janik's avatar
Tim Janik committed
319
      
Elliot Lee's avatar
Elliot Lee committed
320
      state = widget->state;
Tim Janik's avatar
Tim Janik committed
321
      
322 323
      /*
       * GC Clipping
Elliot Lee's avatar
Elliot Lee committed
324
       */
325 326
      gdk_gc_set_clip_rectangle (widget->style->white_gc, &event->area);
      gdk_gc_set_clip_rectangle (widget->style->fg_gc[state], &event->area);
Tim Janik's avatar
Tim Janik committed
327
      
328 329 330 331 332 333 334 335 336 337 338 339 340
      /* We clear the whole allocation here so that if a partial
       * expose is triggered we don't just clear part and mess up
       * when the queued redraw comes along. (There will always
       * be a complete queued redraw when the needs_clear flag
       * is set.)
       */
      if (label->needs_clear)
	{
	  gdk_window_clear_area (widget->window,
				 widget->allocation.x,
				 widget->allocation.y,
				 widget->allocation.width,
				 widget->allocation.height);
Tim Janik's avatar
Tim Janik committed
341
	  
342 343
	  label->needs_clear = FALSE;
	}
Tim Janik's avatar
Tim Janik committed
344
      
345
      x = widget->allocation.x + misc->xpad +
Tim Janik's avatar
Tim Janik committed
346
	(widget->allocation.width - (label->max_width + label->misc.xpad * 2))
347
	* misc->xalign + 0.5;
Tim Janik's avatar
Tim Janik committed
348
      
349 350 351 352
      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;
Tim Janik's avatar
Tim Janik committed
353
      
354 355
      row = label->row;
      while (row && row->next)
Elliot Lee's avatar
Elliot Lee committed
356
	{
357
	  len = (gchar*) row->next->data - (gchar*) row->data - 1;
358
	  offset = 0;
Tim Janik's avatar
Tim Janik committed
359
	  
360
	  if (label->jtype == GTK_JUSTIFY_CENTER)
Tim Janik's avatar
Tim Janik committed
361 362
	    offset = (label->max_width - gdk_text_width (widget->style->font, row->data, len)) / 2;
	  
363
	  else if (label->jtype == GTK_JUSTIFY_RIGHT)
Tim Janik's avatar
Tim Janik committed
364 365
	    offset = (label->max_width - gdk_text_width (widget->style->font, row->data, len));
	  
366 367 368 369 370 371 372 373 374 375
	  if (state == GTK_STATE_INSENSITIVE)
	    gdk_draw_text (widget->window, widget->style->font,
			   widget->style->white_gc,
			   offset + x + 1, y + 1, row->data, len);
	  
	  gdk_draw_text (widget->window, widget->style->font,
			 widget->style->fg_gc[state],
			 offset + x, y, row->data, len);
	  row = row->next;
	  y += widget->style->font->ascent + widget->style->font->descent + 2;
Elliot Lee's avatar
Elliot Lee committed
376
	}
377 378 379 380 381 382 383
      
      /* 
       * 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.
       */
      offset = 0;
Tim Janik's avatar
Tim Janik committed
384
      
385
      if (label->jtype == GTK_JUSTIFY_CENTER)
Tim Janik's avatar
Tim Janik committed
386 387
	offset = (label->max_width - gdk_string_width (widget->style->font, row->data)) / 2;
      
388
      else if (label->jtype == GTK_JUSTIFY_RIGHT)
Tim Janik's avatar
Tim Janik committed
389 390
	offset = (label->max_width - gdk_string_width (widget->style->font, row->data));
      
391 392 393 394 395 396 397 398
      if (state == GTK_STATE_INSENSITIVE)
	gdk_draw_string (widget->window, widget->style->font,
			 widget->style->white_gc,
			 offset + x + 1, y + 1, row->data);
      
      gdk_draw_string (widget->window, widget->style->font,
		       widget->style->fg_gc[state],
		       offset + x, y, row->data);
Tim Janik's avatar
Tim Janik committed
399
      
400 401
      gdk_gc_set_clip_mask (widget->style->white_gc, NULL);
      gdk_gc_set_clip_mask (widget->style->fg_gc[state], NULL);
Tim Janik's avatar
Tim Janik committed
402
      
403
    }
Elliot Lee's avatar
Elliot Lee committed
404 405 406
  return TRUE;
}

407
static void 
Tim Janik's avatar
Tim Janik committed
408 409
gtk_label_state_changed (GtkWidget	*widget,
			 guint		 previous_state)
410
{
Owen Taylor's avatar
Owen Taylor committed
411
  if (GTK_WIDGET_DRAWABLE (widget))
Tim Janik's avatar
Tim Janik committed
412
    GTK_LABEL (widget)->needs_clear = TRUE;
413
}
Elliot Lee's avatar
Elliot Lee committed
414

415
static void 
Tim Janik's avatar
Tim Janik committed
416 417
gtk_label_style_set (GtkWidget	*widget,
		     GtkStyle	*previous_style)
418
{
Owen Taylor's avatar
Owen Taylor committed
419
  if (GTK_WIDGET_DRAWABLE (widget))
Tim Janik's avatar
Tim Janik committed
420
    GTK_LABEL (widget)->needs_clear = TRUE;
421
}
Elliot Lee's avatar
Elliot Lee committed
422