gtkcellrenderertext.c 42.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* gtkcellrenderertext.c
 * Copyright (C) 2000  Red Hat, Inc.,  Jonathan Blandford <jrb@redhat.com>
 *
 * 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
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include <stdlib.h>
#include "gtkcellrenderertext.h"
22
#include "gtkintl.h"
23 24 25

static void gtk_cell_renderer_text_init       (GtkCellRendererText      *celltext);
static void gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class);
Havoc Pennington's avatar
Havoc Pennington committed
26
static void gtk_cell_renderer_text_finalize   (GObject                  *object);
27

28 29 30
static void gtk_cell_renderer_text_get_property  (GObject                  *object,
						  guint                     param_id,
						  GValue                   *value,
Tim Janik's avatar
Tim Janik committed
31
						  GParamSpec               *pspec);
32 33 34
static void gtk_cell_renderer_text_set_property  (GObject                  *object,
						  guint                     param_id,
						  const GValue             *value,
Tim Janik's avatar
Tim Janik committed
35
						  GParamSpec               *pspec);
36 37
static void gtk_cell_renderer_text_get_size   (GtkCellRenderer          *cell,
					       GtkWidget                *widget,
38 39 40
					       GdkRectangle             *cell_area,
					       gint                     *x_offset,
					       gint                     *y_offset,
41 42 43 44 45 46 47 48 49 50 51
					       gint                     *width,
					       gint                     *height);
static void gtk_cell_renderer_text_render     (GtkCellRenderer          *cell,
					       GdkWindow                *window,
					       GtkWidget                *widget,
					       GdkRectangle             *background_area,
					       GdkRectangle             *cell_area,
					       GdkRectangle             *expose_area,
					       guint                     flags);


Havoc Pennington's avatar
Havoc Pennington committed
52

53
enum {
Havoc Pennington's avatar
Havoc Pennington committed
54 55
  PROP_0,

56
  PROP_TEXT,
Alexander Larsson's avatar
Alexander Larsson committed
57
  PROP_MARKUP,
58
  PROP_ATTRIBUTES,
Havoc Pennington's avatar
Havoc Pennington committed
59 60
  
  /* Style args */
61 62
  PROP_BACKGROUND,
  PROP_FOREGROUND,
Havoc Pennington's avatar
Havoc Pennington committed
63
  PROP_BACKGROUND_GDK,
64
  PROP_FOREGROUND_GDK,
Havoc Pennington's avatar
Havoc Pennington committed
65 66 67 68 69 70 71 72 73
  PROP_FONT,
  PROP_FONT_DESC,
  PROP_FAMILY,
  PROP_STYLE,
  PROP_VARIANT,
  PROP_WEIGHT,
  PROP_STRETCH,
  PROP_SIZE,
  PROP_SIZE_POINTS,
Havoc Pennington's avatar
Havoc Pennington committed
74
  PROP_SCALE,
Havoc Pennington's avatar
Havoc Pennington committed
75
  PROP_EDITABLE,
76 77
  PROP_STRIKETHROUGH,
  PROP_UNDERLINE,
Havoc Pennington's avatar
Havoc Pennington committed
78 79 80 81 82 83 84 85 86 87 88
  PROP_RISE,
  
  /* Whether-a-style-arg-is-set args */
  PROP_BACKGROUND_SET,
  PROP_FOREGROUND_SET,
  PROP_FAMILY_SET,
  PROP_STYLE_SET,
  PROP_VARIANT_SET,
  PROP_WEIGHT_SET,
  PROP_STRETCH_SET,
  PROP_SIZE_SET,
Havoc Pennington's avatar
Havoc Pennington committed
89
  PROP_SCALE_SET,
Havoc Pennington's avatar
Havoc Pennington committed
90 91 92 93
  PROP_EDITABLE_SET,
  PROP_STRIKETHROUGH_SET,
  PROP_UNDERLINE_SET,
  PROP_RISE_SET
94 95
};

Havoc Pennington's avatar
Havoc Pennington committed
96 97
static gpointer parent_class;

98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
GtkType
gtk_cell_renderer_text_get_type (void)
{
  static GtkType cell_text_type = 0;

  if (!cell_text_type)
    {
      static const GTypeInfo cell_text_info =
      {
        sizeof (GtkCellRendererTextClass),
	NULL,		/* base_init */
	NULL,		/* base_finalize */
        (GClassInitFunc) gtk_cell_renderer_text_class_init,
	NULL,		/* class_finalize */
	NULL,		/* class_data */
        sizeof (GtkCellRendererText),
	0,              /* n_preallocs */
        (GInstanceInitFunc) gtk_cell_renderer_text_init,
      };

118
      cell_text_type = g_type_register_static (GTK_TYPE_CELL_RENDERER, "GtkCellRendererText", &cell_text_info, 0);
119 120 121 122 123 124 125 126 127 128 129 130
    }

  return cell_text_type;
}

static void
gtk_cell_renderer_text_init (GtkCellRendererText *celltext)
{
  GTK_CELL_RENDERER (celltext)->xalign = 0.0;
  GTK_CELL_RENDERER (celltext)->yalign = 0.5;
  GTK_CELL_RENDERER (celltext)->xpad = 2;
  GTK_CELL_RENDERER (celltext)->ypad = 2;
131 132

  celltext->fixed_height_rows = -1;
133 134 135 136 137 138 139 140
}

static void
gtk_cell_renderer_text_class_init (GtkCellRendererTextClass *class)
{
  GObjectClass *object_class = G_OBJECT_CLASS (class);
  GtkCellRendererClass *cell_class = GTK_CELL_RENDERER_CLASS (class);

Havoc Pennington's avatar
Havoc Pennington committed
141 142 143 144
  parent_class = g_type_class_peek_parent (class);
  
  object_class->finalize = gtk_cell_renderer_text_finalize;
  
145 146
  object_class->get_property = gtk_cell_renderer_text_get_property;
  object_class->set_property = gtk_cell_renderer_text_set_property;
147 148 149

  cell_class->get_size = gtk_cell_renderer_text_get_size;
  cell_class->render = gtk_cell_renderer_text_render;
Havoc Pennington's avatar
Havoc Pennington committed
150

151
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
152 153 154 155 156 157
                                   PROP_TEXT,
                                   g_param_spec_string ("text",
                                                        _("Text"),
                                                        _("Text to render"),
                                                        NULL,
                                                        G_PARAM_READWRITE));
158
  
Alexander Larsson's avatar
Alexander Larsson committed
159 160 161 162 163 164 165
  g_object_class_install_property (object_class,
                                   PROP_MARKUP,
                                   g_param_spec_string ("markup",
                                                        _("Markup"),
                                                        _("Marked up text to render"),
                                                        NULL,
                                                        G_PARAM_WRITABLE));
166

167
  g_object_class_install_property (object_class,
168 169 170 171 172 173
				   PROP_ATTRIBUTES,
				   g_param_spec_boxed ("attributes",
						       _("Attributes"),
						       _("A list of style attributes to apply to the text of the renderer."),
						       PANGO_TYPE_ATTR_LIST,
						       G_PARAM_READWRITE));
Alexander Larsson's avatar
Alexander Larsson committed
174
  
175
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
176 177 178 179 180 181 182 183 184 185 186 187
                                   PROP_BACKGROUND,
                                   g_param_spec_string ("background",
                                                        _("Background color name"),
                                                        _("Background color as a string"),
                                                        NULL,
                                                        G_PARAM_WRITABLE));

  g_object_class_install_property (object_class,
                                   PROP_BACKGROUND_GDK,
                                   g_param_spec_boxed ("background_gdk",
                                                       _("Background color"),
                                                       _("Background color as a GdkColor"),
188
                                                       GDK_TYPE_COLOR,
Havoc Pennington's avatar
Havoc Pennington committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));  

  g_object_class_install_property (object_class,
                                   PROP_FOREGROUND,
                                   g_param_spec_string ("foreground",
                                                        _("Foreground color name"),
                                                        _("Foreground color as a string"),
                                                        NULL,
                                                        G_PARAM_WRITABLE));

  g_object_class_install_property (object_class,
                                   PROP_FOREGROUND_GDK,
                                   g_param_spec_boxed ("foreground_gdk",
                                                       _("Foreground color"),
                                                       _("Foreground color as a GdkColor"),
204
                                                       GDK_TYPE_COLOR,
Havoc Pennington's avatar
Havoc Pennington committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));


  g_object_class_install_property (object_class,
                                   PROP_EDITABLE,
                                   g_param_spec_boolean ("editable",
                                                         _("Editable"),
                                                         _("Whether the text can be modified by the user"),
                                                         TRUE,
                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));

  g_object_class_install_property (object_class,
                                   PROP_FONT,
                                   g_param_spec_string ("font",
                                                        _("Font"),
                                                        _("Font description as a string"),
                                                        NULL,
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));

224
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
225 226 227 228
                                   PROP_FONT_DESC,
                                   g_param_spec_boxed ("font_desc",
                                                       _("Font"),
                                                       _("Font description as a PangoFontDescription struct"),
229
                                                       PANGO_TYPE_FONT_DESCRIPTION,
Havoc Pennington's avatar
Havoc Pennington committed
230 231
                                                       G_PARAM_READABLE | G_PARAM_WRITABLE));

232 233
  
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
                                   PROP_FAMILY,
                                   g_param_spec_string ("family",
                                                        _("Font family"),
                                                        _("Name of the font family, e.g. Sans, Helvetica, Times, Monospace"),
                                                        NULL,
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));

  g_object_class_install_property (object_class,
                                   PROP_STYLE,
                                   g_param_spec_enum ("style",
                                                      _("Font style"),
                                                      _("Font style"),
                                                      PANGO_TYPE_STYLE,
                                                      PANGO_STYLE_NORMAL,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));

  g_object_class_install_property (object_class,
                                   PROP_VARIANT,
                                   g_param_spec_enum ("variant",
                                                     _("Font variant"),
                                                     _("Font variant"),
                                                      PANGO_TYPE_VARIANT,
                                                      PANGO_VARIANT_NORMAL,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
258 259
  
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
260 261 262 263 264 265 266 267
                                   PROP_WEIGHT,
                                   g_param_spec_int ("weight",
                                                     _("Font weight"),
                                                     _("Font weight"),
                                                     0,
                                                     G_MAXINT,
                                                     PANGO_WEIGHT_NORMAL,
                                                     G_PARAM_READABLE | G_PARAM_WRITABLE));
268
  
Havoc Pennington's avatar
Havoc Pennington committed
269

270
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
271 272 273 274 275 276 277
                                   PROP_STRETCH,
                                   g_param_spec_enum ("stretch",
                                                      _("Font stretch"),
                                                      _("Font stretch"),
                                                      PANGO_TYPE_STRETCH,
                                                      PANGO_STRETCH_NORMAL,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));
278 279
  
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
                                   PROP_SIZE,
                                   g_param_spec_int ("size",
                                                     _("Font size"),
                                                     _("Font size"),
                                                     0,
                                                     G_MAXINT,
                                                     0,
                                                     G_PARAM_READABLE | G_PARAM_WRITABLE));

  g_object_class_install_property (object_class,
                                   PROP_SIZE_POINTS,
                                   g_param_spec_double ("size_points",
                                                        _("Font points"),
                                                        _("Font size in points"),
                                                        0.0,
                                                        G_MAXDOUBLE,
                                                        0.0,
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));  
Havoc Pennington's avatar
Havoc Pennington committed
298 299 300 301 302 303 304 305 306 307

  g_object_class_install_property (object_class,
                                   PROP_SCALE,
                                   g_param_spec_double ("scale",
                                                        _("Font scale"),
                                                        _("Font scaling factor"),
                                                        0.0,
                                                        G_MAXDOUBLE,
                                                        1.0,
                                                        G_PARAM_READABLE | G_PARAM_WRITABLE));
308 309
  
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
                                   PROP_RISE,
                                   g_param_spec_int ("rise",
                                                     _("Rise"),
                                                     _("Offset of text above the baseline (below the baseline if rise is negative)"),
                                                     -G_MAXINT,
                                                     G_MAXINT,
                                                     0,
                                                     G_PARAM_READABLE | G_PARAM_WRITABLE));


  g_object_class_install_property (object_class,
                                   PROP_STRIKETHROUGH,
                                   g_param_spec_boolean ("strikethrough",
                                                         _("Strikethrough"),
                                                         _("Whether to strike through the text"),
                                                         FALSE,
                                                         G_PARAM_READABLE | G_PARAM_WRITABLE));
327 328
  
  g_object_class_install_property (object_class,
Havoc Pennington's avatar
Havoc Pennington committed
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 366 367 368 369 370 371 372 373 374 375 376
                                   PROP_UNDERLINE,
                                   g_param_spec_enum ("underline",
                                                      _("Underline"),
                                                      _("Style of underline for this text"),
                                                      PANGO_TYPE_UNDERLINE,
                                                      PANGO_UNDERLINE_NONE,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));

  /* Style props are set or not */

#define ADD_SET_PROP(propname, propval, nick, blurb) g_object_class_install_property (object_class, propval, g_param_spec_boolean (propname, nick, blurb, FALSE, G_PARAM_READABLE | G_PARAM_WRITABLE))

  ADD_SET_PROP ("background_set", PROP_BACKGROUND_SET,
                _("Background set"),
                _("Whether this tag affects the background color"));

  ADD_SET_PROP ("foreground_set", PROP_FOREGROUND_SET,
                _("Foreground set"),
                _("Whether this tag affects the foreground color"));
  
  ADD_SET_PROP ("editable_set", PROP_EDITABLE_SET,
                _("Editability set"),
                _("Whether this tag affects text editability"));

  ADD_SET_PROP ("family_set", PROP_FAMILY_SET,
                _("Font family set"),
                _("Whether this tag affects the font family"));  

  ADD_SET_PROP ("style_set", PROP_STYLE_SET,
                _("Font style set"),
                _("Whether this tag affects the font style"));

  ADD_SET_PROP ("variant_set", PROP_VARIANT_SET,
                _("Font variant set"),
                _("Whether this tag affects the font variant"));

  ADD_SET_PROP ("weight_set", PROP_WEIGHT_SET,
                _("Font weight set"),
                _("Whether this tag affects the font weight"));

  ADD_SET_PROP ("stretch_set", PROP_STRETCH_SET,
                _("Font stretch set"),
                _("Whether this tag affects the font stretch"));

  ADD_SET_PROP ("size_set", PROP_SIZE_SET,
                _("Font size set"),
                _("Whether this tag affects the font size"));

Havoc Pennington's avatar
Havoc Pennington committed
377 378 379 380
  ADD_SET_PROP ("scale_set", PROP_SCALE_SET,
                _("Font scale set"),
                _("Whether this tag scales the font size by a factor"));
  
Havoc Pennington's avatar
Havoc Pennington committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
  ADD_SET_PROP ("rise_set", PROP_RISE_SET,
                _("Rise set"),
                _("Whether this tag affects the rise"));

  ADD_SET_PROP ("strikethrough_set", PROP_STRIKETHROUGH_SET,
                _("Strikethrough set"),
                _("Whether this tag affects strikethrough"));

  ADD_SET_PROP ("underline_set", PROP_UNDERLINE_SET,
                _("Underline set"),
                _("Whether this tag affects underlining"));
}

static void
gtk_cell_renderer_text_finalize (GObject *object)
{
  GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);

  if (celltext->font.family_name)
    g_free (celltext->font.family_name);

  if (celltext->text)
    g_free (celltext->text);
Alexander Larsson's avatar
Alexander Larsson committed
404 405 406 407

  if (celltext->extra_attrs)
    pango_attr_list_unref (celltext->extra_attrs);

Havoc Pennington's avatar
Havoc Pennington committed
408
  (* G_OBJECT_CLASS (parent_class)->finalize) (object);
409 410 411
}

static void
412 413 414
gtk_cell_renderer_text_get_property (GObject        *object,
				     guint           param_id,
				     GValue         *value,
Tim Janik's avatar
Tim Janik committed
415
				     GParamSpec     *pspec)
416 417 418 419 420 421 422 423
{
  GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);

  switch (param_id)
    {
    case PROP_TEXT:
      g_value_set_string (value, celltext->text);
      break;
424 425 426 427 428

    case PROP_ATTRIBUTES:
      g_value_set_boxed (value, celltext->extra_attrs);
      break;

Havoc Pennington's avatar
Havoc Pennington committed
429 430 431 432 433 434 435 436 437 438
    case PROP_BACKGROUND_GDK:
      {
        GdkColor color;
        
        color.red = celltext->background.red;
        color.green = celltext->background.green;
        color.blue = celltext->background.blue;
        
        g_value_set_boxed (value, &color);
      }
439
      break;
Havoc Pennington's avatar
Havoc Pennington committed
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461

    case PROP_FOREGROUND_GDK:
      {
        GdkColor color;
        
        color.red = celltext->foreground.red;
        color.green = celltext->foreground.green;
        color.blue = celltext->foreground.blue;
        
        g_value_set_boxed (value, &color);
      }
      break;

    case PROP_FONT:
      {
        /* FIXME GValue imposes a totally gratuitous string copy
         * here, we could just hand off string ownership
         */
        gchar *str = pango_font_description_to_string (&celltext->font);
        g_value_set_string (value, str);
        g_free (str);
      }
462
      break;
Havoc Pennington's avatar
Havoc Pennington committed
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
      
    case PROP_FONT_DESC:
      g_value_set_boxed (value, &celltext->font);
      break;

    case PROP_FAMILY:
      g_value_set_string (value, celltext->font.family_name);
      break;

    case PROP_STYLE:
      g_value_set_enum (value, celltext->font.style);
      break;

    case PROP_VARIANT:
      g_value_set_enum (value, celltext->font.variant);
      break;

    case PROP_WEIGHT:
      g_value_set_int (value, celltext->font.weight);
      break;

    case PROP_STRETCH:
      g_value_set_enum (value, celltext->font.stretch);
      break;

    case PROP_SIZE:
      g_value_set_int (value, celltext->font.size);
      break;

    case PROP_SIZE_POINTS:
      g_value_set_double (value, ((double)celltext->font.size) / (double)PANGO_SCALE);
      break;

Havoc Pennington's avatar
Havoc Pennington committed
496 497 498 499
    case PROP_SCALE:
      g_value_set_double (value, celltext->font_scale);
      break;
      
500 501 502
    case PROP_EDITABLE:
      g_value_set_boolean (value, celltext->editable);
      break;
Havoc Pennington's avatar
Havoc Pennington committed
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

    case PROP_STRIKETHROUGH:
      g_value_set_boolean (value, celltext->strikethrough);
      break;

    case PROP_UNDERLINE:
      g_value_set_enum (value, celltext->underline_style);
      break;

    case PROP_RISE:
      g_value_set_int (value, celltext->rise);
      break;  

    case PROP_BACKGROUND_SET:
      g_value_set_boolean (value, celltext->background_set);
      break;

    case PROP_FOREGROUND_SET:
      g_value_set_boolean (value, celltext->foreground_set);
      break;

    case PROP_FAMILY_SET:
      g_value_set_boolean (value, celltext->family_set);
      break;

    case PROP_STYLE_SET:
      g_value_set_boolean (value, celltext->style_set);
      break;

    case PROP_VARIANT_SET:
      g_value_set_boolean (value, celltext->variant_set);
      break;

    case PROP_WEIGHT_SET:
      g_value_set_boolean (value, celltext->weight_set);
      break;

    case PROP_STRETCH_SET:
      g_value_set_boolean (value, celltext->stretch_set);
      break;

    case PROP_SIZE_SET:
      g_value_set_boolean (value, celltext->size_set);
      break;

Havoc Pennington's avatar
Havoc Pennington committed
548 549 550 551
    case PROP_SCALE_SET:
      g_value_set_boolean (value, celltext->scale_set);
      break;
      
Havoc Pennington's avatar
Havoc Pennington committed
552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569
    case PROP_EDITABLE_SET:
      g_value_set_boolean (value, celltext->editable_set);
      break;

    case PROP_STRIKETHROUGH_SET:
      g_value_set_boolean (value, celltext->strikethrough_set);
      break;

    case PROP_UNDERLINE_SET:
      g_value_set_boolean (value, celltext->underline_set);
      break;

    case  PROP_RISE_SET:
      g_value_set_boolean (value, celltext->rise_set);
      break;
      
    case PROP_BACKGROUND:
    case PROP_FOREGROUND:
Alexander Larsson's avatar
Alexander Larsson committed
570
    case PROP_MARKUP:
571
    default:
572
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
573 574 575 576 577
      break;
    }
}


Havoc Pennington's avatar
Havoc Pennington committed
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 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 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
static void
set_bg_color (GtkCellRendererText *celltext,
              GdkColor            *color)
{
  if (color)
    {
      if (!celltext->background_set)
        {
          celltext->background_set = TRUE;
          g_object_notify (G_OBJECT (celltext), "background_set");
        }
      
      celltext->background.red = color->red;
      celltext->background.green = color->green;
      celltext->background.blue = color->blue;
    }
  else
    {
      if (celltext->background_set)
        {
          celltext->background_set = FALSE;
          g_object_notify (G_OBJECT (celltext), "background_set");
        }
    }
}


static void
set_fg_color (GtkCellRendererText *celltext,
              GdkColor            *color)
{
  if (color)
    {
      if (!celltext->foreground_set)
        {
          celltext->foreground_set = TRUE;
          g_object_notify (G_OBJECT (celltext), "foreground_set");
        }
      
      celltext->foreground.red = color->red;
      celltext->foreground.green = color->green;
      celltext->foreground.blue = color->blue;
    }
  else
    {
      if (celltext->foreground_set)
        {
          celltext->foreground_set = FALSE;
          g_object_notify (G_OBJECT (celltext), "foreground_set");
        }
    }
}

static void
set_font_description (GtkCellRendererText  *celltext,
                      PangoFontDescription *font_desc)
{
  if (font_desc != NULL)
    {
      /* pango_font_description_from_string() will sometimes return
       * a NULL family or -1 size, so handle those cases.
       */
      
      if (font_desc->family_name)
        g_object_set (G_OBJECT (celltext),
                      "family", font_desc->family_name,
                      NULL);
      
      if (font_desc->size >= 0)
        g_object_set (G_OBJECT (celltext),
                      "size", font_desc->size,
                      NULL);
        
      g_object_set (G_OBJECT (celltext),
                    "style", font_desc->style,
                    "variant", font_desc->variant,
                    "weight", font_desc->weight,
                    "stretch", font_desc->stretch,
                    NULL);
    }
  else
    {
Hans Breuer's avatar
Hans Breuer committed
660
      g_object_freeze_notify (G_OBJECT (celltext));
Havoc Pennington's avatar
Havoc Pennington committed
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
      if (celltext->family_set)
        {
          celltext->family_set = FALSE;
          g_object_notify (G_OBJECT (celltext), "family_set");
        }
      if (celltext->style_set)
        {
          celltext->style_set = FALSE;
          g_object_notify (G_OBJECT (celltext), "style_set");
        }
      if (celltext->variant_set)
        {
          celltext->variant_set = FALSE;
          g_object_notify (G_OBJECT (celltext), "variant_set");
        }
      if (celltext->weight_set)
        {
          celltext->weight_set = FALSE;
          g_object_notify (G_OBJECT (celltext), "weight_set");
        }
      if (celltext->stretch_set)
        {
          celltext->stretch_set = FALSE;
          g_object_notify (G_OBJECT (celltext), "stretch_set");
        }
      if (celltext->size_set)
        {
          celltext->size_set = FALSE;
          g_object_notify (G_OBJECT (celltext), "size_set");
        }
Hans Breuer's avatar
Hans Breuer committed
691
      g_object_thaw_notify (G_OBJECT (celltext));
Havoc Pennington's avatar
Havoc Pennington committed
692 693 694
    }
}

695
static void
696 697 698
gtk_cell_renderer_text_set_property (GObject      *object,
				     guint         param_id,
				     const GValue *value,
Tim Janik's avatar
Tim Janik committed
699
				     GParamSpec   *pspec)
700 701 702 703 704 705
{
  GtkCellRendererText *celltext = GTK_CELL_RENDERER_TEXT (object);

  switch (param_id)
    {
    case PROP_TEXT:
Havoc Pennington's avatar
Havoc Pennington committed
706 707 708
      if (celltext->text)
        g_free (celltext->text);
      celltext->text = g_strdup (g_value_get_string (value));
709
      g_object_notify(G_OBJECT(object), "text");
710
      break;
711 712 713 714 715 716

    case PROP_ATTRIBUTES:
      if (celltext->extra_attrs)
	pango_attr_list_unref (celltext->extra_attrs);

      celltext->extra_attrs = g_value_get_boxed (value);
717 718
      if (celltext->extra_attrs)
        pango_attr_list_ref (celltext->extra_attrs);
719
      break;
Alexander Larsson's avatar
Alexander Larsson committed
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752
    case PROP_MARKUP:
      {
	const gchar *str;
	gchar *text = NULL;
	GError *error = NULL;
	PangoAttrList *attrs = NULL;
	
	if (celltext->text)
	  g_free (celltext->text);

	if (celltext->extra_attrs)
	  pango_attr_list_unref (celltext->extra_attrs);

	str = g_value_get_string (value);
	if (str && !pango_parse_markup (str,
					-1,
					0,
					&attrs,
					&text,
					NULL,
					&error))
	  {
	    g_warning ("Failed to set cell text from markup due to error parsing markup: %s",
		       error->message);
	    g_error_free (error);
	    return;
	  }
	
	celltext->text = text;
	celltext->extra_attrs = attrs;
      }
      break;
      
753
    case PROP_BACKGROUND:
Havoc Pennington's avatar
Havoc Pennington committed
754 755 756
      {
        GdkColor color;

757 758 759
        if (!g_value_get_string (value))
          set_bg_color (celltext, NULL);       /* reset to backgrounmd_set to FALSE */
        else if (gdk_color_parse (g_value_get_string (value), &color))
Havoc Pennington's avatar
Havoc Pennington committed
760 761 762 763 764 765
          set_bg_color (celltext, &color);
        else
          g_warning ("Don't know color `%s'", g_value_get_string (value));

        g_object_notify (G_OBJECT (celltext), "background_gdk");
      }
766
      break;
Havoc Pennington's avatar
Havoc Pennington committed
767
      
768
    case PROP_FOREGROUND:
Havoc Pennington's avatar
Havoc Pennington committed
769 770 771
      {
        GdkColor color;

772 773 774 775
        if (!g_value_get_string (value))
          set_fg_color (celltext, NULL);       /* reset to foreground_set to FALSE */
        else if (gdk_color_parse (g_value_get_string (value), &color))
          set_fg_color (celltext, &color);
Havoc Pennington's avatar
Havoc Pennington committed
776 777 778 779 780 781 782 783
        else
          g_warning ("Don't know color `%s'", g_value_get_string (value));

        g_object_notify (G_OBJECT (celltext), "foreground_gdk");
      }
      break;

    case PROP_BACKGROUND_GDK:
784
      /* This notifies the GObject itself. */
Havoc Pennington's avatar
Havoc Pennington committed
785
      set_bg_color (celltext, g_value_get_boxed (value));
786
      break;
Havoc Pennington's avatar
Havoc Pennington committed
787

788
    case PROP_FOREGROUND_GDK:
789
      /* This notifies the GObject itself. */
Havoc Pennington's avatar
Havoc Pennington committed
790 791 792 793 794 795 796 797 798 799 800 801 802
      set_fg_color (celltext, g_value_get_boxed (value));
      break;

    case PROP_FONT:
      {
        PangoFontDescription *font_desc = NULL;
        const gchar *name;

        name = g_value_get_string (value);

        if (name)
          font_desc = pango_font_description_from_string (name);

803 804 805 806
	/* This function notifies the relevant GObjects itself.
	 * I'm not sure how useful the notify() on 'font'
	 * actually is here, but it's here for consistency.
	 */
Havoc Pennington's avatar
Havoc Pennington committed
807
        set_font_description (celltext, font_desc);
808 809
	g_object_notify(G_OBJECT(object), "font_desc");
	g_object_notify(G_OBJECT(object), "font");
Havoc Pennington's avatar
Havoc Pennington committed
810 811 812
        
        if (font_desc)
          pango_font_description_free (font_desc);
813 814
	if (celltext->fixed_height_rows != -1)
	  celltext->calc_fixed_height = TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
815 816 817 818 819
      }
      break;

    case PROP_FONT_DESC:
      set_font_description (celltext, g_value_get_boxed (value));
820 821
      if (celltext->fixed_height_rows != -1)
	celltext->calc_fixed_height = TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
822 823 824 825 826 827 828 829 830 831 832
      break;

    case PROP_FAMILY:
      if (celltext->font.family_name)
        g_free (celltext->font.family_name);
      celltext->font.family_name = g_strdup (g_value_get_string (value));

      celltext->family_set = TRUE;
      g_object_notify (G_OBJECT (celltext), "family_set");
      g_object_notify (G_OBJECT (celltext), "font_desc");
      g_object_notify (G_OBJECT (celltext), "font");
833 834
      if (celltext->fixed_height_rows != -1)
	celltext->calc_fixed_height = TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
835 836 837 838 839 840 841 842 843
      break;

    case PROP_STYLE:
      celltext->font.style = g_value_get_enum (value);

      celltext->style_set = TRUE;
      g_object_notify (G_OBJECT (celltext), "style_set");
      g_object_notify (G_OBJECT (celltext), "font_desc");
      g_object_notify (G_OBJECT (celltext), "font");
844 845
      if (celltext->fixed_height_rows != -1)
	celltext->calc_fixed_height = TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
846 847 848 849 850 851 852 853 854
      break;

    case PROP_VARIANT:
      celltext->font.variant = g_value_get_enum (value);

      celltext->variant_set = TRUE;
      g_object_notify (G_OBJECT (celltext), "variant_set");
      g_object_notify (G_OBJECT (celltext), "font_desc");
      g_object_notify (G_OBJECT (celltext), "font");
855 856
      if (celltext->fixed_height_rows != -1)
	celltext->calc_fixed_height = TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
857 858 859 860 861 862 863 864 865
      break;

    case PROP_WEIGHT:
      celltext->font.weight = g_value_get_int (value);
      
      celltext->weight_set = TRUE;
      g_object_notify (G_OBJECT (celltext), "weight_set");
      g_object_notify (G_OBJECT (celltext), "font_desc");
      g_object_notify (G_OBJECT (celltext), "font");
866 867
      if (celltext->fixed_height_rows != -1)
	celltext->calc_fixed_height = TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
868 869 870 871 872 873 874 875 876
      break;

    case PROP_STRETCH:
      celltext->font.stretch = g_value_get_enum (value);

      celltext->stretch_set = TRUE;
      g_object_notify (G_OBJECT (celltext), "stretch_set");
      g_object_notify (G_OBJECT (celltext), "font_desc");
      g_object_notify (G_OBJECT (celltext), "font");
877 878
      if (celltext->fixed_height_rows != -1)
	celltext->calc_fixed_height = TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
879 880 881 882 883 884 885 886 887
      break;

    case PROP_SIZE:
      celltext->font.size = g_value_get_int (value);

      celltext->size_set = TRUE;
      g_object_notify (G_OBJECT (celltext), "size_set");
      g_object_notify (G_OBJECT (celltext), "font_desc");
      g_object_notify (G_OBJECT (celltext), "font");
888 889
      if (celltext->fixed_height_rows != -1)
	celltext->calc_fixed_height = TRUE;
890
      break;
Havoc Pennington's avatar
Havoc Pennington committed
891

Havoc Pennington's avatar
Havoc Pennington committed
892 893 894
    case PROP_SCALE:
      celltext->font_scale = g_value_get_double (value);
      celltext->scale_set = TRUE;
895 896
      if (celltext->fixed_height_rows != -1)
	celltext->calc_fixed_height = TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
897 898
      break;
      
Havoc Pennington's avatar
Havoc Pennington committed
899 900 901 902 903 904 905
    case PROP_SIZE_POINTS:
      celltext->font.size = g_value_get_double (value) * PANGO_SCALE;

      celltext->size_set = TRUE;
      g_object_notify (G_OBJECT (celltext), "size_set");
      g_object_notify (G_OBJECT (celltext), "font_desc");
      g_object_notify (G_OBJECT (celltext), "font");
906 907
      if (celltext->fixed_height_rows != -1)
	celltext->calc_fixed_height = TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
908 909 910 911 912 913 914 915
      break;

    case PROP_EDITABLE:
      celltext->editable = g_value_get_boolean (value);
      celltext->editable_set = TRUE;
      g_object_notify (G_OBJECT (celltext), "editable_set");
      break;

916
    case PROP_STRIKETHROUGH:
Havoc Pennington's avatar
Havoc Pennington committed
917 918 919
      celltext->strikethrough = g_value_get_boolean (value);
      celltext->strikethrough_set = TRUE;
      g_object_notify (G_OBJECT (celltext), "strikethrough_set");
920
      break;
Havoc Pennington's avatar
Havoc Pennington committed
921

922
    case PROP_UNDERLINE:
Havoc Pennington's avatar
Havoc Pennington committed
923 924 925
      celltext->underline_style = g_value_get_enum (value);
      celltext->underline_set = TRUE;
      g_object_notify (G_OBJECT (celltext), "underline_set");
Havoc Pennington's avatar
Havoc Pennington committed
926
            
927
      break;
Havoc Pennington's avatar
Havoc Pennington committed
928 929 930 931 932

    case PROP_RISE:
      celltext->rise = g_value_get_int (value);
      celltext->rise_set = TRUE;
      g_object_notify (G_OBJECT (celltext), "rise_set");
933 934
      if (celltext->fixed_height_rows != -1)
	celltext->calc_fixed_height = TRUE;
Havoc Pennington's avatar
Havoc Pennington committed
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
      break;  

    case PROP_BACKGROUND_SET:
      celltext->background_set = g_value_get_boolean (value);
      break;

    case PROP_FOREGROUND_SET:
      celltext->foreground_set = g_value_get_boolean (value);
      break;

    case PROP_FAMILY_SET:
      celltext->family_set = g_value_get_boolean (value);
      break;

    case PROP_STYLE_SET:
      celltext->style_set = g_value_get_boolean (value);
951
      break;
Havoc Pennington's avatar
Havoc Pennington committed
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968

    case PROP_VARIANT_SET:
      celltext->variant_set = g_value_get_boolean (value);
      break;

    case PROP_WEIGHT_SET:
      celltext->weight_set = g_value_get_boolean (value);
      break;

    case PROP_STRETCH_SET:
      celltext->stretch_set = g_value_get_boolean (value);
      break;

    case PROP_SIZE_SET:
      celltext->size_set = g_value_get_boolean (value);
      break;

Havoc Pennington's avatar
Havoc Pennington committed
969 970 971 972
    case PROP_SCALE_SET:
      celltext->scale_set = g_value_get_boolean (value);
      break;
      
Havoc Pennington's avatar
Havoc Pennington committed
973 974
    case PROP_EDITABLE_SET:
      celltext->editable_set = g_value_get_boolean (value);
975
      break;
Havoc Pennington's avatar
Havoc Pennington committed
976 977 978 979 980 981 982 983 984

    case PROP_STRIKETHROUGH_SET:
      celltext->strikethrough_set = g_value_get_boolean (value);
      break;

    case PROP_UNDERLINE_SET:
      celltext->underline_set = g_value_get_boolean (value);
      break;

985
    case PROP_RISE_SET:
Havoc Pennington's avatar
Havoc Pennington committed
986 987 988
      celltext->rise_set = g_value_get_boolean (value);
      break;

989
    default:
990
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
991 992 993 994
      break;
    }
}

995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
/**
 * gtk_cell_renderer_text_new:
 * 
 * Creates a new #GtkCellRendererText. Adjust how text is drawn using
 * object properties. Object properties can be
 * set globally (with g_object_set()). Also, with #GtkTreeViewColumn,
 * you can bind a property to a value in a #GtkTreeModel. For example,
 * you can bind the "text" property on the cell renderer to a string
 * value in the model, thus rendering a different string in each row
 * of the #GtkTreeView
 * 
 * Return value: the new cell renderer
 **/
1008 1009 1010
GtkCellRenderer *
gtk_cell_renderer_text_new (void)
{
Havoc Pennington's avatar
Havoc Pennington committed
1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034
  return GTK_CELL_RENDERER (g_object_new (gtk_cell_renderer_text_get_type (), NULL));
}

static void
add_attr (PangoAttrList  *attr_list,
          PangoAttribute *attr)
{
  attr->start_index = 0;
  attr->end_index = G_MAXINT;
  
  pango_attr_list_insert (attr_list, attr);
}

static PangoLayout*
get_layout (GtkCellRendererText *celltext,
            GtkWidget           *widget,
            gboolean             will_render,
            GtkCellRendererState flags)
{
  PangoAttrList *attr_list;
  PangoLayout *layout;
  PangoUnderline uline;
  
  layout = gtk_widget_create_pango_layout (widget, celltext->text);
Alexander Larsson's avatar
Alexander Larsson committed
1035 1036 1037 1038 1039

  if (celltext->extra_attrs)
    attr_list = pango_attr_list_copy (celltext->extra_attrs);
  else
    attr_list = pango_attr_list_new ();
Havoc Pennington's avatar
Havoc Pennington committed
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

  if (will_render)
    {
      /* Add options that affect appearance but not size */
      
      /* note that background doesn't go here, since it affects
       * background_area not the PangoLayout area
       */
      
      if (celltext->foreground_set)
        {
          PangoColor color;

          color = celltext->foreground;
          
          add_attr (attr_list,
                    pango_attr_foreground_new (color.red, color.green, color.blue));
        }

      if (celltext->strikethrough_set)
        add_attr (attr_list,
                  pango_attr_strikethrough_new (celltext->strikethrough));
    }

  if (celltext->family_set &&
      celltext->font.family_name)
    add_attr (attr_list, pango_attr_family_new (celltext->font.family_name));
  
  if (celltext->style_set)
    add_attr (attr_list, pango_attr_style_new (celltext->font.style));

  if (celltext->variant_set)
    add_attr (attr_list, pango_attr_variant_new (celltext->font.variant));

  if (celltext->weight_set)
    add_attr (attr_list, pango_attr_weight_new (celltext->font.weight));

  if (celltext->stretch_set)
    add_attr (attr_list, pango_attr_stretch_new (celltext->font.stretch));

  if (celltext->size_set &&
      celltext->font.size >= 0)
    add_attr (attr_list, pango_attr_size_new (celltext->font.size));

Havoc Pennington's avatar
Havoc Pennington committed
1084 1085 1086 1087
  if (celltext->scale_set &&
      celltext->font_scale != 1.0)
    add_attr (attr_list, pango_attr_scale_new (celltext->font_scale));
  
Havoc Pennington's avatar
Havoc Pennington committed
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
  if (celltext->underline_set)
    uline = celltext->underline_style;
  else
    uline = PANGO_UNDERLINE_NONE;
  
  if ((flags & GTK_CELL_RENDERER_PRELIT) == GTK_CELL_RENDERER_PRELIT)
    {
      switch (uline)
        {
        case PANGO_UNDERLINE_NONE:
          uline = PANGO_UNDERLINE_SINGLE;
          break;

        case PANGO_UNDERLINE_SINGLE:
          uline = PANGO_UNDERLINE_DOUBLE;
          break;

        default:
          break;
        }
    }

  if (uline != PANGO_UNDERLINE_NONE)
    add_attr (attr_list, pango_attr_underline_new (celltext->underline_style));

  if (celltext->rise_set)
    add_attr (attr_list, pango_attr_rise_new (celltext->rise));
  
  pango_layout_set_attributes (layout, attr_list);
  pango_layout_set_width (layout, -1);

1119 1120
  pango_attr_list_unref (attr_list);
  
Havoc Pennington's avatar
Havoc Pennington committed
1121
  return layout;
1122 1123 1124 1125 1126
}

static void
gtk_cell_renderer_text_get_size (GtkCellRenderer *cell,
				 GtkWidget       *widget,
1127 1128 1129
				 GdkRectangle    *cell_area,
				 gint            *x_offset,
				 gint            *y_offset,
1130 1131 1132
				 gint            *width,
				 gint            *height)
{
1133
  GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
1134 1135 1136
  PangoRectangle rect;
  PangoLayout *layout;

1137
  if (celltext->calc_fixed_height)
1138
    {
1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149
      PangoContext *context;
      PangoFontMetrics metrics;
      PangoFontDescription font_desc;

      font_desc = (* widget->style->font_desc);

      if (celltext->family_set &&
	  celltext->font.family_name)
	font_desc.family_name = celltext->font.family_name;
      if (celltext->style_set)
	font_desc.style = celltext->font.style;
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
      if (celltext->variant_set)
	font_desc.variant = celltext->font.variant;

      if (celltext->weight_set)
	font_desc.weight = celltext->font.weight;

      if (celltext->stretch_set)
	font_desc.stretch = celltext->font.stretch;

      if (celltext->size_set &&
	  celltext->font.size >= 0)
	font_desc.size = celltext->font.size;

      context = gtk_widget_get_pango_context (widget);
      pango_context_get_metrics (context,
				 &font_desc,
				 pango_context_get_language (context),
				 &metrics);
      gtk_cell_renderer_set_fixed_size (cell,
					cell->width, 2*cell->ypad +
					celltext->fixed_height_rows*(metrics.ascent + metrics.descent)/PANGO_SCALE);
      if (height)
	{
	  *height = cell->height;
	  height = NULL;
	}
      celltext->calc_fixed_height = FALSE;
      if (width == NULL)
	return;
1180
    }
Havoc Pennington's avatar
Havoc Pennington committed
1181
  layout = get_layout (celltext, widget, FALSE, 0);
1182 1183 1184 1185 1186 1187 1188 1189
  pango_layout_get_pixel_extents (layout, NULL, &rect);

  if (width)
    *width = GTK_CELL_RENDERER (celltext)->xpad * 2 + rect.width;

  if (height)
    *height = GTK_CELL_RENDERER (celltext)->ypad * 2 + rect.height;

1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203
  if (cell_area)
    {
      if (x_offset)
	{
	  *x_offset = cell->xalign * (cell_area->width - rect.width - (2 * cell->xpad));
	  *x_offset = MAX (*x_offset, 0) + cell->xpad;
	}
      if (y_offset)
	{
	  *y_offset = cell->yalign * (cell_area->height - rect.height - (2 * cell->ypad));
	  *y_offset = MAX (*y_offset, 0) + cell->ypad;
	}
    }

1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
  g_object_unref (G_OBJECT (layout));
}

static void
gtk_cell_renderer_text_render (GtkCellRenderer    *cell,
			       GdkWindow          *window,
			       GtkWidget          *widget,
			       GdkRectangle       *background_area,
			       GdkRectangle       *cell_area,
			       GdkRectangle       *expose_area,
			       guint               flags)

{
  GtkCellRendererText *celltext = (GtkCellRendererText *) cell;
  PangoLayout *layout;
1219
  GtkStateType state;
1220 1221
  gint x_offset;
  gint y_offset;
1222

Havoc Pennington's avatar
Havoc Pennington committed
1223
  layout = get_layout (celltext, widget, TRUE, flags);
1224

1225
  gtk_cell_renderer_text_get_size (cell, widget, cell_area, &x_offset, &y_offset, NULL, NULL);
1226

1227
  if ((flags & GTK_CELL_RENDERER_SELECTED) == GTK_CELL_RENDERER_SELECTED)
1228 1229 1230 1231 1232 1233
    {
      if (GTK_WIDGET_HAS_FOCUS (widget))
	state = GTK_STATE_SELECTED;
      else
	state = GTK_STATE_ACTIVE;
    }
1234
  else
1235 1236 1237
    {
      state = GTK_STATE_NORMAL;
    }
Havoc Pennington's avatar
Havoc Pennington committed
1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261

  if (celltext->background_set && state != GTK_STATE_SELECTED)
    {
      GdkColor color;
      GdkGC *gc;
      
      color.red = celltext->background.red;
      color.green = celltext->background.green;
      color.blue = celltext->background.blue;

      gc = gdk_gc_new (window);

      gdk_gc_set_rgb_fg_color (gc, &color);
      
      gdk_draw_rectangle (window,
                          gc,
                          TRUE,
                          background_area->x,
                          background_area->y,
                          background_area->width,
                          background_area->height);

      g_object_unref (G_OBJECT (gc));
    }
1262 1263 1264 1265
  
  gtk_paint_layout (widget->style,
                    window,
                    state,
1266
		    TRUE,
1267 1268 1269
                    cell_area,
                    widget,
                    "cellrenderertext",
1270 1271
                    cell_area->x + x_offset,
                    cell_area->y + y_offset,
1272
                    layout);
1273 1274 1275

  g_object_unref (G_OBJECT (layout));
}
1276 1277

/**
1278
 * gtk_cell_renderer_text_set_fixed_height_from_font:
1279
 * @renderer: A #GtkCellRendererText
1280
 * @number_of_rows: Number of rows of text each cell renderer is allocated, or -1
1281
 * 
1282 1283 1284 1285 1286 1287
 * Sets the height of a renderer to explicitly be determined by the "font" and
 * "y_pad" property set on it.  Further changes in these properties do not
 * affect the height, so they must be accompanied by a subsequent call to this
 * function.  Using this function is unflexible, and should really only be used
 * if calculating the size of a cell is too slow.  If @no_rows is -1, then the
 * fixed height is unset, and the height is determined by the properties again.
1288 1289
 **/
void
1290 1291
gtk_cell_renderer_text_set_fixed_height_from_font (GtkCellRendererText *renderer,
						   gint                 number_of_rows)
1292 1293
{
  g_return_if_fail (GTK_IS_CELL_RENDERER_TEXT (renderer));
1294
  g_return_if_fail (number_of_rows == -1 || number_of_rows > 0);
1295

1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306
  if (number_of_rows == -1)
    {
      gtk_cell_renderer_set_fixed_size (GTK_CELL_RENDERER (renderer),
					GTK_CELL_RENDERER (renderer)->width,
					-1);
    }
  else
    {
      renderer->fixed_height_rows = number_of_rows;
      renderer->calc_fixed_height = TRUE;
    }
1307
}