gtkcellview.c 32.8 KB
Newer Older
1 2 3
/* gtkellview.c
 * Copyright (C) 2002, 2003  Kristian Rietveld <kris@gtk.org>
 *
4 5 6 7
 * 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.
8
 *
9
 * This library is distributed in the hope that it will be useful,
10 11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Library General Public License for more details.
13
 *
14 15
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
16 17 18 19
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

20
#include <config.h>
21
#include "gtkalias.h"
Kristian Rietveld's avatar
Kristian Rietveld committed
22 23 24 25 26 27
#include "gtkcellview.h"
#include "gtkcelllayout.h"
#include "gtkintl.h"
#include "gtksignal.h"
#include "gtkcellrenderertext.h"
#include "gtkcellrendererpixbuf.h"
28 29 30 31 32 33 34 35 36 37 38 39 40
#include <gobject/gmarshal.h>

typedef struct _GtkCellViewCellInfo GtkCellViewCellInfo;
struct _GtkCellViewCellInfo
{
  GtkCellRenderer *cell;

  gint requested_width;
  gint real_width;
  guint expand : 1;
  guint pack : 1;

  GSList *attributes;
41 42 43 44

  GtkCellLayoutDataFunc func;
  gpointer func_data;
  GDestroyNotify destroy;
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
};

struct _GtkCellViewPrivate
{
  GtkTreeModel *model;
  GtkTreeRowReference *displayed_row;
  GList *cell_list;
  gint spacing;

  GdkColor background;
  gboolean background_set;
};


static void        gtk_cell_view_class_init               (GtkCellViewClass *klass);
60
static void        gtk_cell_view_cell_layout_init         (GtkCellLayoutIface *iface);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
static void        gtk_cell_view_get_property             (GObject           *object,
                                                           guint             param_id,
                                                           GValue           *value,
                                                           GParamSpec       *pspec);
static void        gtk_cell_view_set_property             (GObject          *object,
                                                           guint             param_id,
                                                           const GValue     *value,
                                                           GParamSpec       *pspec);
static void        gtk_cell_view_init                     (GtkCellView      *cellview);
static void        gtk_cell_view_finalize                 (GObject          *object);
static void        gtk_cell_view_style_set                (GtkWidget        *widget,
                                                           GtkStyle         *previous_style);
static void        gtk_cell_view_size_request             (GtkWidget        *widget,
                                                           GtkRequisition   *requisition);
static void        gtk_cell_view_size_allocate            (GtkWidget        *widget,
                                                           GtkAllocation    *allocation);
static gboolean    gtk_cell_view_expose                   (GtkWidget        *widget,
                                                           GdkEventExpose   *event);
79 80 81 82
static void        gtk_cell_view_set_value                (GtkCellView     *cell_view,
                                                           GtkCellRenderer *renderer,
                                                           gchar           *property,
                                                           GValue          *value);
83 84
static GtkCellViewCellInfo *gtk_cell_view_get_cell_info   (GtkCellView      *cellview,
                                                           GtkCellRenderer  *renderer);
85
static void        gtk_cell_view_set_cell_data            (GtkCellView      *cell_view);
86 87


88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
static void        gtk_cell_view_cell_layout_pack_start        (GtkCellLayout         *layout,
                                                                GtkCellRenderer       *renderer,
                                                                gboolean               expand);
static void        gtk_cell_view_cell_layout_pack_end          (GtkCellLayout         *layout,
                                                                GtkCellRenderer       *renderer,
                                                                gboolean               expand);
static void        gtk_cell_view_cell_layout_add_attribute     (GtkCellLayout         *layout,
                                                                GtkCellRenderer       *renderer,
                                                                const gchar           *attribute,
                                                                gint                   column);
static void       gtk_cell_view_cell_layout_clear              (GtkCellLayout         *layout);
static void       gtk_cell_view_cell_layout_clear_attributes   (GtkCellLayout         *layout,
                                                                GtkCellRenderer       *renderer);
static void       gtk_cell_view_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
                                                                GtkCellRenderer       *cell,
                                                                GtkCellLayoutDataFunc  func,
                                                                gpointer               func_data,
                                                                GDestroyNotify         destroy);
Kristian Rietveld's avatar
Kristian Rietveld committed
106 107 108
static void       gtk_cell_view_cell_layout_reorder            (GtkCellLayout         *layout,
                                                                GtkCellRenderer       *cell,
                                                                gint                   position);
109 110


Owen Taylor's avatar
Owen Taylor committed
111 112
#define GTK_CELL_VIEW_GET_PRIVATE(obj)    (G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_CELL_VIEW, GtkCellViewPrivate))

113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
enum
{
  PROP_0,
  PROP_BACKGROUND,
  PROP_BACKGROUND_GDK,
  PROP_BACKGROUND_SET
};

static GtkObjectClass *parent_class = NULL;


GType
gtk_cell_view_get_type (void)
{
  static GType cell_view_type = 0;

  if (!cell_view_type)
    {
      static const GTypeInfo cell_view_info =
        {
          sizeof (GtkCellViewClass),
          NULL, /* base_init */
          NULL, /* base_finalize */
          (GClassInitFunc) gtk_cell_view_class_init,
          NULL, /* class_finalize */
          NULL, /* class_data */
          sizeof (GtkCellView),
          0,
          (GInstanceInitFunc) gtk_cell_view_init
        };

144 145 146 147 148 149 150
      static const GInterfaceInfo cell_layout_info =
       {
         (GInterfaceInitFunc) gtk_cell_view_cell_layout_init,
         NULL,
         NULL
       };

151 152
      cell_view_type = g_type_register_static (GTK_TYPE_WIDGET, "GtkCellView",
                                               &cell_view_info, 0);
153 154 155

      g_type_add_interface_static (cell_view_type, GTK_TYPE_CELL_LAYOUT,
                                   &cell_layout_info);
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181
    }

  return cell_view_type;
}

static void
gtk_cell_view_class_init (GtkCellViewClass *klass)
{
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  parent_class = g_type_class_peek_parent (klass);

  gobject_class->get_property = gtk_cell_view_get_property;
  gobject_class->set_property = gtk_cell_view_set_property;
  gobject_class->finalize = gtk_cell_view_finalize;

  widget_class->expose_event = gtk_cell_view_expose;
  widget_class->size_allocate = gtk_cell_view_size_allocate;
  widget_class->size_request = gtk_cell_view_size_request;
  widget_class->style_set = gtk_cell_view_style_set;

  /* properties */
  g_object_class_install_property (gobject_class,
                                   PROP_BACKGROUND,
                                   g_param_spec_string ("background",
182 183
                                                        P_("Background color name"),
                                                        P_("Background color as a string"),
184 185 186 187 188
                                                        NULL,
                                                        G_PARAM_WRITABLE));
  g_object_class_install_property (gobject_class,
                                   PROP_BACKGROUND_GDK,
                                   g_param_spec_boxed ("background_gdk",
189 190
                                                      P_("Background color"),
                                                      P_("Background color as a GdkColor"),
191 192 193 194 195 196
                                                      GDK_TYPE_COLOR,
                                                      G_PARAM_READABLE | G_PARAM_WRITABLE));

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

  ADD_SET_PROP ("background_set", PROP_BACKGROUND_SET,
197 198
                P_("Background set"),
                P_("Whether this tag affects the background color"));
199 200 201 202

  g_type_class_add_private (gobject_class, sizeof (GtkCellViewPrivate));
}

203 204 205 206 207 208 209 210 211
static void
gtk_cell_view_cell_layout_init (GtkCellLayoutIface *iface)
{
  iface->pack_start = gtk_cell_view_cell_layout_pack_start;
  iface->pack_end = gtk_cell_view_cell_layout_pack_end;
  iface->clear = gtk_cell_view_cell_layout_clear;
  iface->add_attribute = gtk_cell_view_cell_layout_add_attribute;
  iface->set_cell_data_func = gtk_cell_view_cell_layout_set_cell_data_func;
  iface->clear_attributes = gtk_cell_view_cell_layout_clear_attributes;
Kristian Rietveld's avatar
Kristian Rietveld committed
212
  iface->reorder = gtk_cell_view_cell_layout_reorder;
213 214
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 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
static void
gtk_cell_view_get_property (GObject    *object,
                            guint       param_id,
                            GValue     *value,
                            GParamSpec *pspec)
{
  GtkCellView *view = GTK_CELL_VIEW (object);

  switch (param_id)
    {
      case PROP_BACKGROUND_GDK:
        {
          GdkColor color;

          color = view->priv->background;

          g_value_set_boxed (value, &color);
        }
        break;
      case PROP_BACKGROUND_SET:
        g_value_set_boolean (value, view->priv->background_set);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    }
}

static void
gtk_cell_view_set_property (GObject      *object,
                            guint         param_id,
                            const GValue *value,
                            GParamSpec   *pspec)
{
  GtkCellView *view = GTK_CELL_VIEW (object);

  switch (param_id)
    {
      case PROP_BACKGROUND:
        {
          GdkColor color;

          if (!g_value_get_string (value))
            gtk_cell_view_set_background_color (view, NULL);
          else if (gdk_color_parse (g_value_get_string (value), &color))
            gtk_cell_view_set_background_color (view, &color);
          else
            g_warning ("Don't know color `%s'", g_value_get_string (value));

          g_object_notify (object, "background_gdk");
        }
        break;
      case PROP_BACKGROUND_GDK:
        gtk_cell_view_set_background_color (view, g_value_get_boxed (value));
        break;
      case PROP_BACKGROUND_SET:
        view->priv->background_set = g_value_get_boolean (value);
        break;
      default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, param_id, pspec);
        break;
    }
}

static void
gtk_cell_view_init (GtkCellView *cellview)
{
  GTK_WIDGET_SET_FLAGS (cellview, GTK_NO_WINDOW);

  cellview->priv = GTK_CELL_VIEW_GET_PRIVATE (cellview);
}

static void
gtk_cell_view_style_set (GtkWidget *widget,
                         GtkStyle  *previous_style)
{
  if (previous_style && GTK_WIDGET_REALIZED (widget))
    gdk_window_set_background (widget->window,
                               &widget->style->base[GTK_WIDGET_STATE (widget)]);
}

static void
gtk_cell_view_finalize (GObject *object)
{
299 300 301 302 303 304 305 306 307
  GtkCellView *cellview = GTK_CELL_VIEW (object);

  gtk_cell_view_cell_layout_clear (GTK_CELL_LAYOUT (cellview));

  if (cellview->priv->model)
     g_object_unref (cellview->priv->model);

  if (cellview->priv->displayed_row)
     gtk_tree_row_reference_free (cellview->priv->displayed_row);
308

309
  (* G_OBJECT_CLASS (parent_class)->finalize) (object);
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 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
}

static void
gtk_cell_view_size_request (GtkWidget      *widget,
                            GtkRequisition *requisition)
{
  GList *i;
  gboolean first_cell = TRUE;
  GtkCellView *cellview;

  cellview = GTK_CELL_VIEW (widget);

  requisition->width = 0;
  requisition->height = 0;

  if (cellview->priv->displayed_row)
    gtk_cell_view_set_cell_data (cellview);

  for (i = cellview->priv->cell_list; i; i = i->next)
    {
      gint width, height;
      GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)i->data;

      if (!info->cell->visible)
        continue;

      if (!first_cell)
        requisition->width += cellview->priv->spacing;

      gtk_cell_renderer_get_size (info->cell, widget, NULL, NULL, NULL,
                                  &width, &height);

      info->requested_width = width;
      requisition->width += width;
      requisition->height = MAX (requisition->height, height);

      first_cell = FALSE;
    }
}

static void
gtk_cell_view_size_allocate (GtkWidget     *widget,
                             GtkAllocation *allocation)
{
  GList *i;
  gint expand_cell_count = 0;
  gint full_requested_width = 0;
  gint extra_space;
  GtkCellView *cellview;

  widget->allocation = *allocation;

  cellview = GTK_CELL_VIEW (widget);

  /* checking how much extra space we have */
  for (i = cellview->priv->cell_list; i; i = i->next)
    {
      GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)i->data;

      if (!info->cell->visible)
        continue;

      if (info->expand)
        expand_cell_count++;

      full_requested_width += info->requested_width;
    }

  extra_space = widget->allocation.width - full_requested_width;
  if (extra_space < 0)
    extra_space = 0;
  else if (extra_space > 0 && expand_cell_count > 0)
    extra_space /= expand_cell_count;

  /* iterate list for PACK_START cells */
  for (i = cellview->priv->cell_list; i; i = i->next)
    {
      GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)i->data;

      if (info->pack == GTK_PACK_END)
        continue;

      if (!info->cell->visible)
        continue;

      info->real_width = info->requested_width + (info->expand?extra_space:0);
    }

  /* iterate list for PACK_END cells */
  for (i = cellview->priv->cell_list; i; i = i->next)
    {
      GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)i->data;

      if (info->pack == GTK_PACK_START)
        continue;

      if (!info->cell->visible)
        continue;

      info->real_width = info->requested_width + (info->expand?extra_space:0);
    }
}

static gboolean
gtk_cell_view_expose (GtkWidget      *widget,
                      GdkEventExpose *event)
{
  GList *i;
  GtkCellView *cellview;
  GdkRectangle area;
420
  GtkCellRendererState state;
421
  gboolean rtl = (gtk_widget_get_direction(widget) == GTK_TEXT_DIR_RTL);
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446

  cellview = GTK_CELL_VIEW (widget);

  if (! GTK_WIDGET_DRAWABLE (widget))
    return FALSE;

  /* "blank" background */
  if (cellview->priv->background_set)
    {
      GdkGC *gc;

      gc = gdk_gc_new (GTK_WIDGET (cellview)->window);
      gdk_gc_set_rgb_fg_color (gc, &cellview->priv->background);

      gdk_draw_rectangle (GTK_WIDGET (cellview)->window,
                          gc,
                          TRUE,

                          /*0, 0,*/
                          widget->allocation.x,
                          widget->allocation.y,

                          widget->allocation.width,
                          widget->allocation.height);

447
      g_object_unref (gc);
448 449
    }

450 451 452 453
  /* set cell data (if available) */
  if (cellview->priv->displayed_row)
    gtk_cell_view_set_cell_data (cellview);
  else if (cellview->priv->model)
454
    return FALSE;
455 456 457 458 459

  /* render cells */
  area = widget->allocation;

  /* we draw on our very own window, initialize x and y to zero */
460
  area.x = widget->allocation.x + (rtl ? widget->allocation.width : 0); 
461 462
  area.y = widget->allocation.y;

463 464 465 466 467
  if (GTK_WIDGET_STATE (widget) == GTK_STATE_PRELIGHT)
    state = GTK_CELL_RENDERER_PRELIT;
  else
    state = 0;
      
468 469 470 471 472 473 474 475 476 477 478 479
  /* PACK_START */
  for (i = cellview->priv->cell_list; i; i = i->next)
    {
      GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)i->data;

      if (info->pack == GTK_PACK_END)
        continue;

      if (!info->cell->visible)
        continue;

      area.width = info->real_width;
480 481
      if (rtl)                                             
         area.x -= area.width;
482 483 484 485 486

      gtk_cell_renderer_render (info->cell,
                                event->window,
                                widget,
                                /* FIXME! */
487
                                &area, &area, &event->area, state);
488

489 490
      if (!rtl)                                           
         area.x += info->real_width;
491 492
    }

493 494
   area.x = rtl ? widget->allocation.x : (widget->allocation.x + widget->allocation.width);  

495 496 497 498 499 500 501 502 503 504 505 506
  /* PACK_END */
  for (i = cellview->priv->cell_list; i; i = i->next)
    {
      GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)i->data;

      if (info->pack == GTK_PACK_START)
        continue;

      if (!info->cell->visible)
        continue;

      area.width = info->real_width;
507 508
      if (!rtl)
         area.x -= area.width;   
509 510 511 512 513

      gtk_cell_renderer_render (info->cell,
                                widget->window,
                                widget,
                                /* FIXME ! */
514
                                &area, &area, &event->area, state);
515 516
      if (rtl)
         area.x += info->real_width;
517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
    }

  return FALSE;
}

static GtkCellViewCellInfo *
gtk_cell_view_get_cell_info (GtkCellView     *cellview,
                             GtkCellRenderer *renderer)
{
  GList *i;

  for (i = cellview->priv->cell_list; i; i = i->next)
    {
      GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)i->data;

      if (info->cell == renderer)
        return info;
    }

  return NULL;
}

539
static void
Matthias Clasen's avatar
Matthias Clasen committed
540
gtk_cell_view_set_cell_data (GtkCellView *cell_view)
541 542 543 544 545
{
  GList *i;
  GtkTreeIter iter;
  GtkTreePath *path;

Matthias Clasen's avatar
Matthias Clasen committed
546
  g_return_if_fail (cell_view->priv->displayed_row != NULL);
547

Matthias Clasen's avatar
Matthias Clasen committed
548
  path = gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
549 550 551
  if (!path)
    return;

Matthias Clasen's avatar
Matthias Clasen committed
552
  gtk_tree_model_get_iter (cell_view->priv->model, &iter, path);
553 554
  gtk_tree_path_free (path);

Matthias Clasen's avatar
Matthias Clasen committed
555
  for (i = cell_view->priv->cell_list; i; i = i->next)
556 557 558 559
    {
      GSList *j;
      GtkCellViewCellInfo *info = i->data;

560
      g_object_freeze_notify (G_OBJECT (info->cell));
561 562 563 564 565 566 567

      for (j = info->attributes; j && j->next; j = j->next->next)
        {
          gchar *property = j->data;
          gint column = GPOINTER_TO_INT (j->next->data);
          GValue value = {0, };

Matthias Clasen's avatar
Matthias Clasen committed
568
          gtk_tree_model_get_value (cell_view->priv->model, &iter,
569 570 571 572 573
                                    column, &value);
          g_object_set_property (G_OBJECT (info->cell),
                                 property, &value);
          g_value_unset (&value);
        }
574 575

      if (info->func)
Matthias Clasen's avatar
Matthias Clasen committed
576
	(* info->func) (GTK_CELL_LAYOUT (cell_view),
577
			info->cell,
Matthias Clasen's avatar
Matthias Clasen committed
578
			cell_view->priv->model,
579 580 581 582
			&iter,
			info->func_data);

      g_object_thaw_notify (G_OBJECT (info->cell));
583 584 585 586 587 588 589 590
    }
}

/* GtkCellLayout implementation */
static void
gtk_cell_view_cell_layout_pack_start (GtkCellLayout   *layout,
                                      GtkCellRenderer *renderer,
                                      gboolean         expand)
591 592
{
  GtkCellViewCellInfo *info;
593
  GtkCellView *cellview = GTK_CELL_VIEW (layout);
594 595 596 597 598

  g_return_if_fail (GTK_IS_CELL_VIEW (cellview));
  g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
  g_return_if_fail (!gtk_cell_view_get_cell_info (cellview, renderer));

599
  g_object_ref (renderer);
600 601 602 603 604 605 606 607 608 609
  gtk_object_sink (GTK_OBJECT (renderer));

  info = g_new0 (GtkCellViewCellInfo, 1);
  info->cell = renderer;
  info->expand = expand ? TRUE : FALSE;
  info->pack = GTK_PACK_START;

  cellview->priv->cell_list = g_list_append (cellview->priv->cell_list, info);
}

610 611 612 613
static void
gtk_cell_view_cell_layout_pack_end (GtkCellLayout   *layout,
                                    GtkCellRenderer *renderer,
                                    gboolean         expand)
614 615
{
  GtkCellViewCellInfo *info;
616
  GtkCellView *cellview = GTK_CELL_VIEW (layout);
617 618 619 620 621

  g_return_if_fail (GTK_IS_CELL_VIEW (cellview));
  g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));
  g_return_if_fail (!gtk_cell_view_get_cell_info (cellview, renderer));

622
  g_object_ref (renderer);
623 624 625 626 627 628 629 630 631 632
  gtk_object_sink (GTK_OBJECT (renderer));

  info = g_new0 (GtkCellViewCellInfo, 1);
  info->cell = renderer;
  info->expand = expand ? TRUE : FALSE;
  info->pack = GTK_PACK_END;

  cellview->priv->cell_list = g_list_append (cellview->priv->cell_list, info);
}

633 634 635 636 637
static void
gtk_cell_view_cell_layout_add_attribute (GtkCellLayout   *layout,
                                         GtkCellRenderer *renderer,
                                         const gchar     *attribute,
                                         gint             column)
638 639
{
  GtkCellViewCellInfo *info;
640
  GtkCellView *cellview = GTK_CELL_VIEW (layout);
641 642 643 644 645 646 647 648 649 650 651

  g_return_if_fail (GTK_IS_CELL_VIEW (cellview));
  info = gtk_cell_view_get_cell_info (cellview, renderer);
  g_return_if_fail (info != NULL);

  info->attributes = g_slist_prepend (info->attributes,
                                      GINT_TO_POINTER (column));
  info->attributes = g_slist_prepend (info->attributes,
                                      g_strdup (attribute));
}

652 653
static void
gtk_cell_view_cell_layout_clear (GtkCellLayout *layout)
654
{
655 656 657
  GtkCellView *cellview = GTK_CELL_VIEW (layout);

  g_return_if_fail (GTK_IS_CELL_VIEW (cellview));
658

659
  while (cellview->priv->cell_list)
660
    {
661
      GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)cellview->priv->cell_list->data;
662

663
      gtk_cell_view_cell_layout_clear_attributes (layout, info->cell);
664
      g_object_unref (info->cell);
665
      g_free (info);
666 667
      cellview->priv->cell_list = g_list_delete_link (cellview->priv->cell_list, 
						      cellview->priv->cell_list);
668 669 670
    }
}

671 672 673 674 675 676
static void
gtk_cell_view_cell_layout_set_cell_data_func (GtkCellLayout         *layout,
                                              GtkCellRenderer       *cell,
                                              GtkCellLayoutDataFunc  func,
                                              gpointer               func_data,
                                              GDestroyNotify         destroy)
677
{
678
  GtkCellView *cellview = GTK_CELL_VIEW (layout);
679
  GtkCellViewCellInfo *info;
680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704

  g_return_if_fail (GTK_IS_CELL_VIEW (cellview));

  info = gtk_cell_view_get_cell_info (cellview, cell);
  g_return_if_fail (info != NULL);

  if (info->destroy)
    {
      GDestroyNotify d = info->destroy;

      info->destroy = NULL;
      d (info->func_data);
    }

  info->func = func;
  info->func_data = func_data;
  info->destroy = destroy;
}

static void
gtk_cell_view_cell_layout_clear_attributes (GtkCellLayout   *layout,
                                            GtkCellRenderer *renderer)
{
  GtkCellViewCellInfo *info;
  GtkCellView *cellview = GTK_CELL_VIEW (layout);
705
  GSList *list;
706 707 708 709 710

  g_return_if_fail (GTK_IS_CELL_VIEW (cellview));
  g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));

  info = gtk_cell_view_get_cell_info (cellview, renderer);
711
  if (info != NULL)
712
    {
713 714 715 716 717 718 719 720 721
      list = info->attributes;
      while (list && list->next)
	{
	  g_free (list->data);
	  list = list->next->next;
	}
      
      g_slist_free (info->attributes);
      info->attributes = NULL;
722
    }
723 724
}

Kristian Rietveld's avatar
Kristian Rietveld committed
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
static void
gtk_cell_view_cell_layout_reorder (GtkCellLayout   *layout,
                                   GtkCellRenderer *cell,
                                   gint             position)
{
  GList *link;
  GtkCellViewCellInfo *info;
  GtkCellView *cellview = GTK_CELL_VIEW (layout);

  g_return_if_fail (GTK_IS_CELL_VIEW (cellview));
  g_return_if_fail (GTK_IS_CELL_RENDERER (cell));

  info = gtk_cell_view_get_cell_info (cellview, cell);

  g_return_if_fail (info != NULL);
  g_return_if_fail (position >= 0);

  link = g_list_find (cellview->priv->cell_list, info);

  g_return_if_fail (link != NULL);

  cellview->priv->cell_list = g_list_remove_link (cellview->priv->cell_list,
                                                  link);
  cellview->priv->cell_list = g_list_insert (cellview->priv->cell_list,
                                             info, position);

  gtk_widget_queue_draw (GTK_WIDGET (cellview));
}
753

Matthias Clasen's avatar
Matthias Clasen committed
754 755 756 757 758 759 760 761 762
/**
 * gtk_cell_view_new:
 *
 * Creates a new #GtkCellView widget.
 *
 * Return value: A newly created #GtkCellView widget.
 *
 * Since: 2.6
 */
763 764
GtkWidget *
gtk_cell_view_new (void)
765
{
766
  GtkCellView *cellview;
767

768
  cellview = g_object_new (gtk_cell_view_get_type (), NULL);
769

770 771
  return GTK_WIDGET (cellview);
}
772

Matthias Clasen's avatar
Matthias Clasen committed
773 774 775 776 777 778 779 780 781 782 783
/**
 * gtk_cell_view_new_with_text:
 * @text: the text to display in the cell view
 *
 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText 
 * to it, and makes its show @text.
 *
 * Return value: A newly created #GtkCellView widget.
 *
 * Since: 2.6
 */
784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
GtkWidget *
gtk_cell_view_new_with_text (const gchar *text)
{
  GtkCellView *cellview;
  GtkCellRenderer *renderer;
  GValue value = {0, };

  cellview = GTK_CELL_VIEW (gtk_cell_view_new ());

  renderer = gtk_cell_renderer_text_new ();
  gtk_cell_view_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
                                        renderer, TRUE);

  g_value_init (&value, G_TYPE_STRING);
  g_value_set_string (&value, text);
799
  gtk_cell_view_set_value (cellview, renderer, "text", &value);
800 801 802
  g_value_unset (&value);

  return GTK_WIDGET (cellview);
803 804
}

Matthias Clasen's avatar
Matthias Clasen committed
805 806 807 808 809 810 811 812 813 814 815 816 817
/**
 * gtk_cell_view_new_with_markup:
 * @markup: the text to display in the cell view
 *
 * Creates a new #GtkCellView widget, adds a #GtkCellRendererText 
 * to it, and makes its show @markup. The text can text can be
 * marked up with the <link linkend="PangoMarkupFormat">Pango text 
 * markup language</link>.
 *
 * Return value: A newly created #GtkCellView widget.
 *
 * Since: 2.6
 */
818 819
GtkWidget *
gtk_cell_view_new_with_markup (const gchar *markup)
820
{
821 822 823
  GtkCellView *cellview;
  GtkCellRenderer *renderer;
  GValue value = {0, };
824

825
  cellview = GTK_CELL_VIEW (gtk_cell_view_new ());
826

827 828 829 830 831 832
  renderer = gtk_cell_renderer_text_new ();
  gtk_cell_view_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
                                        renderer, TRUE);

  g_value_init (&value, G_TYPE_STRING);
  g_value_set_string (&value, markup);
833
  gtk_cell_view_set_value (cellview, renderer, "markup", &value);
834 835 836 837 838
  g_value_unset (&value);

  return GTK_WIDGET (cellview);
}

Matthias Clasen's avatar
Matthias Clasen committed
839 840 841 842 843 844 845 846 847 848 849
/**
 * gtk_cell_view_new_with_pixbuf:
 * @pixbuf: the image to display in the cell view
 *
 * Creates a new #GtkCellView widget, adds a #GtkCellRendererPixbuf 
 * to it, and makes its show @pixbuf. 
 *
 * Return value: A newly created #GtkCellView widget.
 *
 * Since: 2.6
 */
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864
GtkWidget *
gtk_cell_view_new_with_pixbuf (GdkPixbuf *pixbuf)
{
  GtkCellView *cellview;
  GtkCellRenderer *renderer;
  GValue value = {0, };

  cellview = GTK_CELL_VIEW (gtk_cell_view_new ());

  renderer = gtk_cell_renderer_pixbuf_new ();
  gtk_cell_view_cell_layout_pack_start (GTK_CELL_LAYOUT (cellview),
                                        renderer, TRUE);

  g_value_init (&value, GDK_TYPE_PIXBUF);
  g_value_set_object (&value, pixbuf);
865
  gtk_cell_view_set_value (cellview, renderer, "pixbuf", &value);
866 867 868
  g_value_unset (&value);

  return GTK_WIDGET (cellview);
869 870
}

Matthias Clasen's avatar
Matthias Clasen committed
871 872 873 874 875 876 877 878 879 880 881 882
/**
 * gtk_cell_view_set_value:
 * @cell_view: a #GtkCellView widget
 * @renderer: one of the renderers of @cell_view
 * @property: the name of the property of @renderer to set
 * @value: the new value to set the property to
 * 
 * Sets a property of a cell renderer of @cell_view, and
 * makes sure the display of @cell_view is updated.
 *
 * Since: 2.6
 */
883
static void
884
gtk_cell_view_set_value (GtkCellView     *cell_view,
885 886 887 888
                         GtkCellRenderer *renderer,
                         gchar           *property,
                         GValue          *value)
{
889
  g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
890 891 892 893
  g_return_if_fail (GTK_IS_CELL_RENDERER (renderer));

  g_object_set_property (G_OBJECT (renderer), property, value);

894 895 896
  /* force resize and redraw */
  gtk_widget_queue_resize (GTK_WIDGET (cell_view));
  gtk_widget_queue_draw (GTK_WIDGET (cell_view));
897 898
}

Matthias Clasen's avatar
Matthias Clasen committed
899 900 901 902 903 904 905 906 907 908 909
/**
 * gtk_cell_view_set_model:
 * @cell_view: a #GtkCellView
 * @model: a #GtkTreeModel
 *
 * Sets the model for @cell_view.  If @cell_view already has a model
 * set, it will remove it before setting the new model.  If @model is 
 * %NULL, then it will unset the old model.
 *
 * Since: 2.6
 */
910
void
911
gtk_cell_view_set_model (GtkCellView  *cell_view,
912 913
                         GtkTreeModel *model)
{
914
  g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
915 916
  g_return_if_fail (GTK_IS_TREE_MODEL (model));

917
  if (cell_view->priv->model)
918
    {
919 920 921
      if (cell_view->priv->displayed_row)
        gtk_tree_row_reference_free (cell_view->priv->displayed_row);
      cell_view->priv->displayed_row = NULL;
922

923
      g_object_unref (cell_view->priv->model);
924
      cell_view->priv->model = NULL;
925 926
    }

927
  cell_view->priv->model = model;
928

929
  if (cell_view->priv->model)
930
    g_object_ref (cell_view->priv->model);
931 932
}

933 934 935 936 937 938 939 940 941 942 943
/**
 * gtk_cell_view_set_displayed_row:
 * @cell_view: a #GtkCellView
 * @path: a #GtkTreePath or %NULL to unset.
 * 
 * Sets the row of the model that is currently displayed
 * by the #GtkCellView. If the path is unset, then the
 * contents of the cellview "stick" at their last value;
 * this is not normally a desired result, but may be
 * a needed intermediate state if say, the model for
 * the #GtkCellView becomes temporarily empty.
Matthias Clasen's avatar
Matthias Clasen committed
944 945
 *
 * Since: 2.6
946
 **/
947
void
948
gtk_cell_view_set_displayed_row (GtkCellView *cell_view,
949 950
                                 GtkTreePath *path)
{
951 952
  g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
  g_return_if_fail (GTK_IS_TREE_MODEL (cell_view->priv->model));
953

954 955
  if (cell_view->priv->displayed_row)
    gtk_tree_row_reference_free (cell_view->priv->displayed_row);
956

957 958 959 960 961 962 963
  if (path)
    {
      cell_view->priv->displayed_row =
	gtk_tree_row_reference_new (cell_view->priv->model, path);
    }
  else
    cell_view->priv->displayed_row = NULL;
964

965 966 967
  /* force resize and redraw */
  gtk_widget_queue_resize (GTK_WIDGET (cell_view));
  gtk_widget_queue_draw (GTK_WIDGET (cell_view));
968 969 970
}

GtkTreePath *
971
gtk_cell_view_get_displayed_row (GtkCellView *cell_view)
972
{
973
  g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), NULL);
974

975
  if (!cell_view->priv->displayed_row)
976 977
    return NULL;

978 979 980
  return gtk_tree_row_reference_get_path (cell_view->priv->displayed_row);
}

Matthias Clasen's avatar
Matthias Clasen committed
981 982 983 984 985 986 987 988 989 990 991 992 993
/**
 * gtk_cell_view_get_size_of_row:
 * @cell_view: a #GtkCellView
 * @path: a #GtkTreePath 
 * @requisition: return location for the size 
 *
 * Sets @requisition to the size needed by @cell_view to display 
 * the model row pointed to by @path.
 * 
 * Return value: %TRUE
 *
 * Since: 2.6
 */
994 995 996 997 998 999
gboolean
gtk_cell_view_get_size_of_row (GtkCellView    *cell_view,
                               GtkTreePath    *path,
                               GtkRequisition *requisition)
{
  GtkTreeRowReference *tmp;
1000
  GtkRequisition req;
1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014

  g_return_val_if_fail (GTK_IS_CELL_VIEW (cell_view), FALSE);
  g_return_val_if_fail (path != NULL, FALSE);
  g_return_val_if_fail (requisition != NULL, FALSE);

  tmp = cell_view->priv->displayed_row;
  cell_view->priv->displayed_row =
    gtk_tree_row_reference_new (cell_view->priv->model, path);

  gtk_cell_view_size_request (GTK_WIDGET (cell_view), requisition);

  gtk_tree_row_reference_free (cell_view->priv->displayed_row);
  cell_view->priv->displayed_row = tmp;

1015 1016 1017
  /* restore actual size info */
  gtk_cell_view_size_request (GTK_WIDGET (cell_view), &req);

1018
  return TRUE;
1019 1020
}

Matthias Clasen's avatar
Matthias Clasen committed
1021 1022
/**
 * gtk_cell_view_set_background_color:
Matthias Clasen's avatar
Matthias Clasen committed
1023
 * @cell_view: a #GtkCellView
Matthias Clasen's avatar
Matthias Clasen committed
1024 1025 1026 1027 1028 1029
 * @color: the new background color
 *
 * Sets the background color of @view.
 *
 * Since: 2.6
 */
1030
void
Matthias Clasen's avatar
Matthias Clasen committed
1031
gtk_cell_view_set_background_color (GtkCellView    *cell_view,
1032
                                    const GdkColor *color)
1033
{
Matthias Clasen's avatar
Matthias Clasen committed
1034
  g_return_if_fail (GTK_IS_CELL_VIEW (cell_view));
1035 1036 1037

  if (color)
    {
Matthias Clasen's avatar
Matthias Clasen committed
1038
      if (!cell_view->priv->background_set)
1039
        {
Matthias Clasen's avatar
Matthias Clasen committed
1040 1041
          cell_view->priv->background_set = TRUE;
          g_object_notify (G_OBJECT (cell_view), "background_set");
1042 1043
        }

Matthias Clasen's avatar
Matthias Clasen committed
1044
      cell_view->priv->background = *color;
1045 1046 1047
    }
  else
    {
Matthias Clasen's avatar
Matthias Clasen committed
1048
      if (cell_view->priv->background_set)
1049
        {
Matthias Clasen's avatar
Matthias Clasen committed
1050 1051
          cell_view->priv->background_set = FALSE;
          g_object_notify (G_OBJECT (cell_view), "background_set");
1052 1053
        }
    }
1054

1055
  gtk_widget_queue_draw (GTK_WIDGET (cell_view));
1056
}
1057

Matthias Clasen's avatar
Matthias Clasen committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069
/**
 * gtk_cell_view_get_cell_renderers:
 * @cell_view: a #GtkCellView
 * 
 * Returns the cell renderers which have been added to @cell_view.
 *
 * Return value: a list of cell renderers. The list, but not the
 *   renderers has been newly allocated and should be freed with
 *   g_list_free() when no longer needed.
 * 
 * Since: 2.6
 */
1070 1071 1072 1073 1074 1075 1076
GList *
gtk_cell_view_get_cell_renderers (GtkCellView *cell_view)
{
  GList *retval = NULL, *list;

  g_return_val_if_fail (cell_view != NULL, NULL);

1077 1078
  gtk_cell_view_set_cell_data (cell_view);

1079 1080 1081 1082 1083 1084 1085 1086 1087
  for (list = cell_view->priv->cell_list; list; list = list->next)
    {
      GtkCellViewCellInfo *info = (GtkCellViewCellInfo *)list->data;

      retval = g_list_prepend (retval, info->cell);
    }

  return g_list_reverse (retval);
}