gtkclist.c 95.3 KB
Newer Older
1 2
/* GTK - The GIMP Toolkit
 * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball, Josh MacDonald, 
3
 * Copyright (C) 1997-1998 Jay Painter <jpaint@serv.net><jpaint@gimp.org>  
4
 *
5 6 7 8 9 10 11 12 13 14 15
 * 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
16 17 18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
19 20
 */
#include <stdlib.h>
21
#include "../config.h"
22 23
#include "gtkclist.h"

24 25 26
/* the number rows memchunk expands at a time */
#define CLIST_OPTIMUM_SIZE 512

27
/* the width of the column resize windows */
28
#define DRAG_WIDTH  6
29 30 31 32 33 34 35 36 37 38

/* minimum allowed width of a column */
#define COLUMN_MIN_WIDTH 5

/* this defigns the base grid spacing */
#define CELL_SPACING 1

/* added the horizontal space at the beginning and end of a row*/
#define COLUMN_INSET 3

39 40
/* scrollbar spacing class macro */
#define SCROLLBAR_SPACING(w) (GTK_CLIST_CLASS (GTK_OBJECT (w)->klass)->scrollbar_spacing)
41 42 43

/* gives the top pixel of the given row in context of
 * the clist's voffset */
44 45
#define ROW_TOP_YPIXEL(clist, row) (((clist)->row_height * (row)) + \
				    (((row) + 1) * CELL_SPACING) + \
46 47 48 49
				    (clist)->voffset)

/* returns the row index from a y pixel location in the 
 * context of the clist's voffset */
50
#define ROW_FROM_YPIXEL(clist, y)  (((y) - (clist)->voffset) / \
51 52 53 54
				    ((clist)->row_height + CELL_SPACING))

/* gives the left pixel of the given column in context of
 * the clist's hoffset */
55
#define COLUMN_LEFT_XPIXEL(clist, column)  ((clist)->column[(column)].area.x + \
56 57 58 59
					    (clist)->hoffset)

/* returns the column index from a x pixel location in the 
 * context of the clist's hoffset */
60
static inline gint
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
COLUMN_FROM_XPIXEL (GtkCList * clist,
		    gint x)
{
  gint i, cx;

  for (i = 0; i < clist->columns; i++)
    {
      cx = clist->column[i].area.x + clist->hoffset;

      if (x >= (cx - (COLUMN_INSET + CELL_SPACING)) &&
	  x <= (cx + clist->column[i].area.width + COLUMN_INSET))
	return i;
    }

  /* no match */
  return -1;
}

/* returns the top pixel of the given row in the context of
 * the list height */
#define ROW_TOP(clist, row)        (((clist)->row_height + CELL_SPACING) * (row))

/* returns the left pixel of the given column in the context of
 * the list width */
#define COLUMN_LEFT(clist, column) ((clist)->column[(column)].area.x)

/* returns the total height of the list */
#define LIST_HEIGHT(clist)         (((clist)->row_height * ((clist)->rows)) + \
				    (CELL_SPACING * ((clist)->rows + 1)))

/* returns the total width of the list */
#define LIST_WIDTH(clist)          ((clist)->column[(clist)->columns - 1].area.x + \
				    (clist)->column[(clist)->columns - 1].area.width + \
				    COLUMN_INSET + CELL_SPACING)

96

97 98 99 100 101 102 103 104 105
/* Signals */
enum
{
  SELECT_ROW,
  UNSELECT_ROW,
  CLICK_COLUMN,
  LAST_SIGNAL
};

106 107 108 109 110 111 112
enum
{
  SYNC_REMOVE,
  SYNC_INSERT
};


113 114 115
typedef void (*GtkCListSignal1) (GtkObject * object,
				 gint arg1,
				 gint arg2,
116
				 GdkEventButton * arg3,
117 118 119 120 121 122 123
				 gpointer data);

typedef void (*GtkCListSignal2) (GtkObject * object,
				 gint arg1,
				 gpointer data);


124 125 126 127
static void sync_selection (GtkCList * clist,
	                    gint row,
                            gint mode);

128 129 130 131 132 133
/* GtkCList Methods */
static void gtk_clist_class_init (GtkCListClass * klass);
static void gtk_clist_init (GtkCList * clist);

/* GtkObject Methods */
static void gtk_clist_destroy (GtkObject * object);
134
static void gtk_clist_finalize (GtkObject * object);
135 136 137 138 139 140 141 142 143 144 145 146 147


/* GtkWidget Methods */
static void gtk_clist_realize (GtkWidget * widget);
static void gtk_clist_unrealize (GtkWidget * widget);
static void gtk_clist_map (GtkWidget * widget);
static void gtk_clist_unmap (GtkWidget * widget);
static void gtk_clist_draw (GtkWidget * widget,
			    GdkRectangle * area);
static gint gtk_clist_expose (GtkWidget * widget,
			      GdkEventExpose * event);
static gint gtk_clist_button_press (GtkWidget * widget,
				    GdkEventButton * event);
148 149 150 151
static gint gtk_clist_button_release (GtkWidget * widget,
				      GdkEventButton * event);
static gint gtk_clist_motion (GtkWidget * widget, 
			      GdkEventMotion * event);
152 153 154 155 156

static void gtk_clist_size_request (GtkWidget * widget,
				    GtkRequisition * requisition);
static void gtk_clist_size_allocate (GtkWidget * widget,
				     GtkAllocation * allocation);
157 158 159 160 161
static gint get_selection_info (GtkCList * clist,
				gint x,
				gint y,
				gint * row,
				gint * column);
162 163 164 165 166 167

/* GtkContainer Methods */
static void gtk_clist_foreach (GtkContainer * container,
			       GtkCallback callback,
			       gpointer callback_data);

168 169 170 171 172 173 174 175 176 177 178 179 180
/* Drawing */
static void draw_row (GtkCList * clist,
		      GdkRectangle * area,
		      gint row,
		      GtkCListRow * clist_row);
static void draw_rows (GtkCList * clist,
		       GdkRectangle * area);

/* Size Allocation */
static void size_allocate_title_buttons (GtkCList * clist);
static void size_allocate_columns (GtkCList * clist);

/* Selection */
181 182 183 184 185 186 187 188 189 190 191 192 193
static void toggle_row (GtkCList * clist,
			gint row,
			gint column,
			GdkEventButton * event);
static void select_row (GtkCList * clist,
			gint row,
			gint column,
			GdkEventButton * event);
static void unselect_row (GtkCList * clist,
			  gint row,
			  gint column,
			  GdkEventButton * event);

194 195 196
static void real_select_row (GtkCList * clist,
			     gint row,
			     gint column,
197
			     GdkEventButton * event);
198 199 200
static void real_unselect_row (GtkCList * clist,
			       gint row,
			       gint column,
201
			       GdkEventButton * event);
202

203
/* Resize Columns */
204 205 206
static void draw_xor_line (GtkCList * clist);
static gint new_column_width (GtkCList * clist,
			      gint column,
207 208
			      gint * x,
			      gint * visible);
209 210 211
static void resize_column (GtkCList * clist,
			   gint column,
			   gint width);
212 213

/* Buttons */
214 215 216 217
static void column_button_create (GtkCList * clist,
				  gint column);
static void column_button_clicked (GtkWidget * widget,
				   gpointer data);
218 219

/* Scrollbars */
Jay Painter's avatar
Jay Painter committed
220 221
static void create_scrollbars (GtkCList * clist);
static void adjust_scrollbars (GtkCList * clist);
222
static void check_exposures   (GtkCList * clist);
Jay Painter's avatar
Jay Painter committed
223 224 225 226 227 228 229 230
static void vadjustment_changed (GtkAdjustment * adjustment,
				 gpointer data);
static void vadjustment_value_changed (GtkAdjustment * adjustment,
				       gpointer data);
static void hadjustment_changed (GtkAdjustment * adjustment,
				 gpointer data);
static void hadjustment_value_changed (GtkAdjustment * adjustment,
				       gpointer data);
231 232 233


/* Memory Allocation/Distruction Routines */
234
static GtkCListColumn *columns_new (GtkCList * clist);
235

236 237 238 239
static void column_title_new (GtkCList * clist,
			      gint column,
			      gchar * title);
static void columns_delete (GtkCList * clist);
240

241
static GtkCListRow *row_new (GtkCList * clist);
242

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
static void row_delete (GtkCList * clist,
			GtkCListRow * clist_row);
static void cell_empty (GtkCList * clist,
			GtkCListRow * clist_row,
			gint column);
static void cell_set_text (GtkCList * clist,
			   GtkCListRow * clist_row,
			   gint column,
			   gchar * text);
static void cell_set_pixmap (GtkCList * clist,
			     GtkCListRow * clist_row,
			     gint column,
			     GdkPixmap * pixmap,
			     GdkBitmap * mask);
static void cell_set_pixtext (GtkCList * clist,
			      GtkCListRow * clist_row,
			      gint column,
			      gchar * text,
			      guint8 spacing,
			      GdkPixmap * pixmap,
			      GdkBitmap * mask);
264 265 266 267 268 269 270 271 272 273 274

/* Signals */
static void gtk_clist_marshal_signal_1 (GtkObject * object,
					GtkSignalFunc func,
					gpointer func_data,
					GtkArg * args);
static void gtk_clist_marshal_signal_2 (GtkObject * object,
					GtkSignalFunc func,
					gpointer func_data,
					GtkArg * args);

275
/* Fill in data after widget is realized and has style */
276

277 278
static void add_style_data (GtkCList * clist);

279
static GtkContainerClass *parent_class = NULL;
280
static guint clist_signals[LAST_SIGNAL] = {0};
281

282

283
GtkType
284
gtk_clist_get_type (void)
285
{
286
  static GtkType clist_type = 0;
287 288 289 290 291 292 293 294 295 296

  if (!clist_type)
    {
      GtkTypeInfo clist_info =
      {
	"GtkCList",
	sizeof (GtkCList),
	sizeof (GtkCListClass),
	(GtkClassInitFunc) gtk_clist_class_init,
	(GtkObjectInitFunc) gtk_clist_init,
297 298
	(GtkArgSetFunc) NULL,
        (GtkArgGetFunc) NULL,
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
      };

      clist_type = gtk_type_unique (gtk_container_get_type (), &clist_info);
    }

  return clist_type;
}

static void
gtk_clist_class_init (GtkCListClass * klass)
{
  GtkObjectClass *object_class;
  GtkWidgetClass *widget_class;
  GtkContainerClass *container_class;

  object_class = (GtkObjectClass *) klass;
  widget_class = (GtkWidgetClass *) klass;
  container_class = (GtkContainerClass *) klass;

  parent_class = gtk_type_class (gtk_container_get_type ());
319

320 321
  clist_signals[SELECT_ROW] =
    gtk_signal_new ("select_row",
322
		    GTK_RUN_FIRST,
323 324 325
		    object_class->type,
		    GTK_SIGNAL_OFFSET (GtkCListClass, select_row),
		    gtk_clist_marshal_signal_1,
326
	    GTK_TYPE_NONE, 3, GTK_TYPE_INT, GTK_TYPE_INT, GTK_TYPE_POINTER);
327 328
  clist_signals[UNSELECT_ROW] =
    gtk_signal_new ("unselect_row",
329
		    GTK_RUN_FIRST,
330 331 332
		    object_class->type,
		    GTK_SIGNAL_OFFSET (GtkCListClass, unselect_row),
		    gtk_clist_marshal_signal_1,
333
	    GTK_TYPE_NONE, 3, GTK_TYPE_INT, GTK_TYPE_INT, GTK_TYPE_POINTER);
334 335
  clist_signals[CLICK_COLUMN] =
    gtk_signal_new ("click_column",
336
		    GTK_RUN_FIRST,
337 338 339 340 341 342 343 344
		    object_class->type,
		    GTK_SIGNAL_OFFSET (GtkCListClass, click_column),
		    gtk_clist_marshal_signal_2,
		    GTK_TYPE_NONE, 1, GTK_TYPE_INT);

  gtk_object_class_add_signals (object_class, clist_signals, LAST_SIGNAL);

  object_class->destroy = gtk_clist_destroy;
345
  object_class->finalize = gtk_clist_finalize;
346 347 348 349 350 351 352

  widget_class->realize = gtk_clist_realize;
  widget_class->unrealize = gtk_clist_unrealize;
  widget_class->map = gtk_clist_map;
  widget_class->unmap = gtk_clist_unmap;
  widget_class->draw = gtk_clist_draw;
  widget_class->button_press_event = gtk_clist_button_press;
353 354
  widget_class->button_release_event = gtk_clist_button_release;
  widget_class->motion_notify_event = gtk_clist_motion;
355 356 357 358
  widget_class->expose_event = gtk_clist_expose;
  widget_class->size_request = gtk_clist_size_request;
  widget_class->size_allocate = gtk_clist_size_allocate;

359 360
  /* container_class->add = NULL; use the default GtkContainerClass warning */
  /* container_class->remove = NULL; use the default GtkContainerClass warning */
361 362
  container_class->foreach = gtk_clist_foreach;

363 364
  klass->select_row = real_select_row;
  klass->unselect_row = real_unselect_row;
365
  klass->click_column = NULL;
366

367 368
  klass->draw_row = draw_row;

369
  klass->scrollbar_spacing = 5;
370 371 372 373 374 375 376 377 378 379 380 381
}

static void
gtk_clist_marshal_signal_1 (GtkObject * object,
			    GtkSignalFunc func,
			    gpointer func_data,
			    GtkArg * args)
{
  GtkCListSignal1 rfunc;

  rfunc = (GtkCListSignal1) func;

382
  (*rfunc) (object, GTK_VALUE_INT (args[0]),
383
	    GTK_VALUE_INT (args[1]),
384
	    GTK_VALUE_POINTER (args[2]),
385 386 387 388 389 390 391 392 393 394 395 396 397
	    func_data);
}

static void
gtk_clist_marshal_signal_2 (GtkObject * object,
			    GtkSignalFunc func,
			    gpointer func_data,
			    GtkArg * args)
{
  GtkCListSignal2 rfunc;

  rfunc = (GtkCListSignal2) func;

398
  (*rfunc) (object, GTK_VALUE_INT (args[0]),
399 400 401 402 403 404 405 406 407
	    func_data);
}

static void
gtk_clist_init (GtkCList * clist)
{
  clist->flags = 0;

  GTK_WIDGET_UNSET_FLAGS (clist, GTK_NO_WINDOW);
408
  GTK_CLIST_SET_FLAG (clist, CLIST_FROZEN);
409

Tim Janik's avatar
Tim Janik committed
410 411 412
  clist->row_mem_chunk = NULL;
  clist->cell_mem_chunk = NULL;

413 414 415 416 417 418 419 420
  clist->rows = 0;
  clist->row_center_offset = 0;
  clist->row_height = 0;
  clist->row_list = NULL;
  clist->row_list_end = NULL;

  clist->columns = 0;

421
  clist->title_window = NULL;
422 423
  clist->column_title_area.x = 0;
  clist->column_title_area.y = 0;
Owen Taylor's avatar
Owen Taylor committed
424 425
  clist->column_title_area.width = 1;
  clist->column_title_area.height = 1;
426 427

  clist->clist_window = NULL;
Owen Taylor's avatar
Owen Taylor committed
428 429
  clist->clist_window_width = 1;
  clist->clist_window_height = 1;
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444

  clist->hoffset = 0;
  clist->voffset = 0;

  clist->shadow_type = GTK_SHADOW_IN;
  clist->hscrollbar_policy = GTK_POLICY_ALWAYS;
  clist->vscrollbar_policy = GTK_POLICY_ALWAYS;

  clist->cursor_drag = NULL;
  clist->xor_gc = NULL;
  clist->fg_gc = NULL;
  clist->bg_gc = NULL;
  clist->x_drag = 0;

  clist->selection_mode = GTK_SELECTION_SINGLE;
445
  clist->selection = NULL;
446 447
}

448 449 450 451 452 453 454 455
/* Constructors */
void
gtk_clist_construct (GtkCList * clist,
		     gint columns,
		     gchar *titles[])
{
  int i;

Tim Janik's avatar
Tim Janik committed
456 457
  g_return_if_fail (clist != NULL);
  g_return_if_fail (GTK_IS_CLIST (clist));
458
  g_return_if_fail (GTK_CLIST_CONSTRUCTED (clist) == FALSE);
Tim Janik's avatar
Tim Janik committed
459

460
  GTK_CLIST_SET_FLAG (clist, CLIST_CONSTRUCTED);
461

462 463 464 465 466 467 468 469 470 471 472 473 474 475
  /* initalize memory chunks, if this has not been done by any
   * possibly derived widget
   */
  if (!clist->row_mem_chunk)
    clist->row_mem_chunk = g_mem_chunk_new ("clist row mem chunk",
					    sizeof (GtkCListRow),
					    sizeof (GtkCListRow) * CLIST_OPTIMUM_SIZE, 
					    G_ALLOC_AND_FREE);

  if (!clist->cell_mem_chunk)
    clist->cell_mem_chunk = g_mem_chunk_new ("clist cell mem chunk",
					     sizeof (GtkCell) * columns,
					     sizeof (GtkCell) * columns * CLIST_OPTIMUM_SIZE, 
					     G_ALLOC_AND_FREE);
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490

  /* set number of columns, allocate memory */
  clist->columns = columns;
  clist->column = columns_new (clist);

  /* there needs to be at least one column button 
   * because there is alot of code that will break if it
   * isn't there*/
  column_button_create (clist, 0);

  /* create scrollbars */
  create_scrollbars (clist);

  if (titles)
    {
491
      GTK_CLIST_SET_FLAG (clist, CLIST_SHOW_TITLES);
492 493 494 495 496
      for (i = 0; i < columns; i++)
	gtk_clist_set_column_title (clist, i, titles[i]);
    }
  else
    {
497
      GTK_CLIST_UNSET_FLAG (clist, CLIST_SHOW_TITLES);
498 499 500
    }
}

501 502 503 504 505
/*
 * GTKCLIST PUBLIC INTERFACE
 *   gtk_clist_new_with_titles
 *   gtk_clist_new
 */
506
GtkWidget *
507
gtk_clist_new_with_titles (gint columns,
508
			   gchar * titles[])
509
{
510
  GtkWidget *widget;
511

512 513 514 515 516
  g_return_val_if_fail (titles != NULL, NULL);
  
  widget = gtk_type_new (gtk_clist_get_type ());
  
  gtk_clist_construct (GTK_CLIST (widget), columns, titles);
517 518 519 520 521

  return widget;
}

GtkWidget *
522
gtk_clist_new (gint columns)
523
{
524 525
  GtkCList *clist;

526
  if (columns < 1)
527 528 529
    return NULL;

  clist = gtk_type_new (gtk_clist_get_type ());
530
  gtk_clist_construct (clist, columns, NULL);
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
  return GTK_WIDGET (clist);
}

void
gtk_clist_set_border (GtkCList * clist,
		      GtkShadowType border)
{
  g_return_if_fail (clist != NULL);

  clist->shadow_type = border;

  if (GTK_WIDGET_VISIBLE (clist))
    gtk_widget_queue_resize (GTK_WIDGET (clist));
}

void
gtk_clist_set_selection_mode (GtkCList * clist,
			      GtkSelectionMode mode)
{
  g_return_if_fail (clist != NULL);

  clist->selection_mode = mode;
}

void
gtk_clist_freeze (GtkCList * clist)
{
  g_return_if_fail (clist != NULL);

560
  GTK_CLIST_SET_FLAG (clist, CLIST_FROZEN);
561 562 563 564 565 566 567
}

void
gtk_clist_thaw (GtkCList * clist)
{
  g_return_if_fail (clist != NULL);

568
  GTK_CLIST_UNSET_FLAG (clist, CLIST_FROZEN);
569

Jay Painter's avatar
Jay Painter committed
570
  adjust_scrollbars (clist);
571
  draw_rows (clist, NULL);
572 573
}

574 575 576 577 578 579 580
void
gtk_clist_column_titles_show (GtkCList * clist)
{
  g_return_if_fail (clist != NULL);

  if (!GTK_CLIST_SHOW_TITLES (clist))
    {
581
      GTK_CLIST_SET_FLAG (clist, CLIST_SHOW_TITLES);
582 583
      if (clist->title_window)
	      gdk_window_show (clist->title_window);
584 585 586 587 588 589 590
      gtk_widget_queue_resize (GTK_WIDGET (clist));
    }
}

void 
gtk_clist_column_titles_hide (GtkCList * clist)
{
Jay Painter's avatar
Jay Painter committed
591 592
  g_return_if_fail (clist != NULL);

593 594
  if (GTK_CLIST_SHOW_TITLES (clist))
    {
595
      GTK_CLIST_UNSET_FLAG (clist, CLIST_SHOW_TITLES);
596 597
      if (clist->title_window)
	      gdk_window_hide (clist->title_window);
598 599 600
      gtk_widget_queue_resize (GTK_WIDGET (clist));
    }
}
601

Jay Painter's avatar
Jay Painter committed
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 660 661
void
gtk_clist_column_title_active (GtkCList * clist,
			       gint column)
{
  g_return_if_fail (clist != NULL);

  if (column < 0 || column >= clist->columns)
    return;

  if (!GTK_WIDGET_SENSITIVE (clist->column[column].button) ||
      !GTK_WIDGET_CAN_FOCUS (clist->column[column].button))
    {
      GTK_WIDGET_SET_FLAGS (clist->column[column].button, GTK_SENSITIVE | GTK_CAN_FOCUS);
      if (GTK_WIDGET_VISIBLE (clist))
	gtk_widget_queue_draw (clist->column[column].button);
    }
}

void
gtk_clist_column_title_passive (GtkCList * clist,
				gint column)
{
  g_return_if_fail (clist != NULL);

  if (column < 0 || column >= clist->columns)
    return;

  if (GTK_WIDGET_SENSITIVE (clist->column[column].button) ||
      GTK_WIDGET_CAN_FOCUS (clist->column[column].button))
    {
      GTK_WIDGET_UNSET_FLAGS (clist->column[column].button, GTK_SENSITIVE | GTK_CAN_FOCUS);
      if (GTK_WIDGET_VISIBLE (clist))
	gtk_widget_queue_draw (clist->column[column].button);
    }
}

void
gtk_clist_column_titles_active (GtkCList * clist)
{
  gint i;

  g_return_if_fail (clist != NULL);

  for (i = 0; i < clist->columns; i++)
    if (clist->column[i].button)
      gtk_clist_column_title_active (clist, i);
}

void
gtk_clist_column_titles_passive (GtkCList * clist)
{
  gint i;

  g_return_if_fail (clist != NULL);

  for (i = 0; i < clist->columns; i++)
    if (clist->column[i].button)
      gtk_clist_column_title_passive (clist, i);
}

662 663 664 665 666
void
gtk_clist_set_column_title (GtkCList * clist,
			    gint column,
			    gchar * title)
{
667
  gint new_button = 0;
668
  GtkWidget *old_widget;
669
  GtkWidget *alignment = NULL;
670 671 672 673 674 675 676
  GtkWidget *label;

  g_return_if_fail (clist != NULL);

  if (column < 0 || column >= clist->columns)
    return;

677
  /* if the column button doesn't currently exist,
678 679 680 681 682 683 684
   * it has to be created first */
  if (!clist->column[column].button)
    {
      column_button_create (clist, column);
      new_button = 1;
    }

685
  column_title_new (clist, column, title);
686 687 688 689

  /* remove and destroy the old widget */
  old_widget = GTK_BUTTON (clist->column[column].button)->child;
  if (old_widget)
690
    gtk_container_remove (GTK_CONTAINER (clist->column[column].button), old_widget);
691

692
  /* create new alignment based no column justification */
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711
  switch (clist->column[column].justification)
    {
    case GTK_JUSTIFY_LEFT:
      alignment = gtk_alignment_new (0.0, 0.5, 0.0, 0.0);
      break;

    case GTK_JUSTIFY_RIGHT:
      alignment = gtk_alignment_new (1.0, 0.5, 0.0, 0.0);
      break;

    case GTK_JUSTIFY_CENTER:
      alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
      break;

    case GTK_JUSTIFY_FILL:
      alignment = gtk_alignment_new (0.5, 0.5, 0.0, 0.0);
      break;
    }

712 713 714 715 716 717
  label = gtk_label_new (clist->column[column].title);
  gtk_container_add (GTK_CONTAINER (alignment), label);
  gtk_container_add (GTK_CONTAINER (clist->column[column].button), alignment);
  gtk_widget_show (label);
  gtk_widget_show (alignment);

718
  /* if this button didn't previously exist, then the
719 720
   * column button positions have to be re-computed */
  if (GTK_WIDGET_VISIBLE (clist) && new_button)
Jay Painter's avatar
Jay Painter committed
721
    size_allocate_title_buttons (clist);
722 723 724 725 726 727 728
}

void
gtk_clist_set_column_widget (GtkCList * clist,
			     gint column,
			     GtkWidget * widget)
{
729
  gint new_button = 0;
730 731 732 733 734 735 736
  GtkWidget *old_widget;

  g_return_if_fail (clist != NULL);

  if (column < 0 || column >= clist->columns)
    return;

737
  /* if the column button doesn't currently exist,
738 739 740 741 742 743 744
   * it has to be created first */
  if (!clist->column[column].button)
    {
      column_button_create (clist, column);
      new_button = 1;
    }

745
  column_title_new (clist, column, NULL);
746 747 748 749

  /* remove and destroy the old widget */
  old_widget = GTK_BUTTON (clist->column[column].button)->child;
  if (old_widget)
750
    gtk_container_remove (GTK_CONTAINER (clist->column[column].button), old_widget);
751 752 753 754 755 756 757

  /* add and show the widget */
  if (widget)
    {
      gtk_container_add (GTK_CONTAINER (clist->column[column].button), widget);
      gtk_widget_show (widget);
    }
758

759
  /* if this button didn't previously exist, then the
760 761
   * column button positions have to be re-computed */
  if (GTK_WIDGET_VISIBLE (clist) && new_button)
Jay Painter's avatar
Jay Painter committed
762
    size_allocate_title_buttons (clist);
763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
}

void
gtk_clist_set_column_justification (GtkCList * clist,
				    gint column,
				    GtkJustification justification)
{
  GtkWidget *alignment;

  g_return_if_fail (clist != NULL);

  if (column < 0 || column >= clist->columns)
    return;

  clist->column[column].justification = justification;

  /* change the alinment of the button title if it's not a
   * custom widget */
  if (clist->column[column].title)
    {
      alignment = GTK_BUTTON (clist->column[column].button)->child;

      switch (clist->column[column].justification)
	{
	case GTK_JUSTIFY_LEFT:
	  gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.0, 0.5, 0.0, 0.0);
	  break;

	case GTK_JUSTIFY_RIGHT:
	  gtk_alignment_set (GTK_ALIGNMENT (alignment), 1.0, 0.5, 0.0, 0.0);
	  break;

	case GTK_JUSTIFY_CENTER:
	  gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.5, 0.5, 0.0, 0.0);
	  break;

	case GTK_JUSTIFY_FILL:
	  gtk_alignment_set (GTK_ALIGNMENT (alignment), 0.5, 0.5, 0.0, 0.0);
	  break;

	default:
	  break;
	}
    }

  if (!GTK_CLIST_FROZEN (clist))
809
    draw_rows (clist, NULL);
810 811 812 813 814 815 816 817 818 819 820 821 822
}

void
gtk_clist_set_column_width (GtkCList * clist,
			    gint column,
			    gint width)
{
  g_return_if_fail (clist != NULL);

  if (column < 0 || column >= clist->columns)
    return;

  clist->column[column].width = width;
823
  clist->column[column].width_set = TRUE;
824

825 826 827 828
  /* FIXME: this is quite expensive to do if the widget hasn't
   *        been size_allocated yet, and pointless. Should
   *        a flag be kept
   */
Jay Painter's avatar
Jay Painter committed
829 830
  size_allocate_columns (clist);
  size_allocate_title_buttons (clist);
831 832 833

  if (!GTK_CLIST_FROZEN (clist))
    {
Jay Painter's avatar
Jay Painter committed
834
      adjust_scrollbars (clist);
835
      draw_rows (clist, NULL);
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851
    }
}

void
gtk_clist_set_row_height (GtkCList * clist,
			  gint height)
{
  gint text_height;

  g_return_if_fail (clist != NULL);

  if (height > 0)
    clist->row_height = height;
  else
    return;

852
  GTK_CLIST_SET_FLAG (clist, CLIST_ROW_HEIGHT_SET);
853 854 855 856
  
  if (GTK_WIDGET_REALIZED (clist))
    {
      text_height = height - (GTK_WIDGET (clist)->style->font->ascent +
857
			      GTK_WIDGET (clist) ->style->font->descent + 1);
858 859 860
      clist->row_center_offset = (text_height / 2) + GTK_WIDGET (clist)->style->font->ascent + 1.5;
    }
      
861 862
  if (!GTK_CLIST_FROZEN (clist))
    {
Jay Painter's avatar
Jay Painter committed
863
      adjust_scrollbars (clist);
864
      draw_rows (clist, NULL);
865 866 867 868 869 870 871 872 873 874 875 876 877 878
    }
}

void
gtk_clist_moveto (GtkCList * clist,
		  gint row,
		  gint column,
		  gfloat row_align,
		  gfloat col_align)
{
  gint x, y;

  g_return_if_fail (clist != NULL);

879
  if (row < -1 || row >= clist->rows)
880
    return;
881
  if (column < -1 || column >= clist->columns)
882 883 884
    return;

  /* adjust vertical scrollbar */
885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901
  if (row >= 0)
    {
      x = ROW_TOP (clist, row) - (row_align * (clist->clist_window_height - 
					       (clist->row_height + 2 * CELL_SPACING)));
      
      if (x < 0)
	GTK_RANGE (clist->vscrollbar)->adjustment->value = 0.0;
      else if (x > LIST_HEIGHT (clist) - clist->clist_window_height)
	GTK_RANGE (clist->vscrollbar)->adjustment->value = LIST_HEIGHT (clist) - 
	  clist->clist_window_height;
      else
	GTK_RANGE (clist->vscrollbar)->adjustment->value = x;
      
      gtk_signal_emit_by_name (GTK_OBJECT (GTK_RANGE (clist->vscrollbar)->adjustment), 
			       "value_changed");
    } 
     
902
  /* adjust horizontal scrollbar */
903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
  if (column >= 0)
    {
      y = COLUMN_LEFT (clist, column) - (col_align * (clist->clist_window_width - 
						      clist->column[column].area.width + 
						      2 * (CELL_SPACING + COLUMN_INSET)));
      
      if (y < 0)
	GTK_RANGE (clist->hscrollbar)->adjustment->value = 0.0;
      else if (y > LIST_WIDTH (clist) - clist->clist_window_width)
	GTK_RANGE (clist->hscrollbar)->adjustment->value = LIST_WIDTH (clist) - 
	  clist->clist_window_width;
      else
	GTK_RANGE (clist->hscrollbar)->adjustment->value = y;
      
      gtk_signal_emit_by_name (GTK_OBJECT (GTK_RANGE (clist->hscrollbar)->adjustment), 
			       "value_changed");
    }
920 921
}

922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
GtkCellType 
gtk_clist_get_cell_type (GtkCList * clist,
			 gint row,
			 gint column)
{
  GtkCListRow *clist_row;

  g_return_val_if_fail (clist != NULL, -1);

  if (row < 0 || row >= clist->rows)
    return -1;
  if (column < 0 || column >= clist->columns)
    return -1;

  clist_row = (g_list_nth (clist->row_list, row))->data;

  return clist_row->cell[column].type;
}

941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
void
gtk_clist_set_text (GtkCList * clist,
		    gint row,
		    gint column,
		    gchar * text)
{
  GtkCListRow *clist_row;

  g_return_if_fail (clist != NULL);

  if (row < 0 || row >= clist->rows)
    return;
  if (column < 0 || column >= clist->columns)
    return;

  clist_row = (g_list_nth (clist->row_list, row))->data;

  /* if text is null, then the cell is empty */
  if (text)
960
    cell_set_text (clist, clist_row, column, text);
961
  else
962
    cell_empty (clist, clist_row, column);
963 964 965 966

  /* redraw the list if it's not frozen */
  if (!GTK_CLIST_FROZEN (clist))
    {
967
      if (gtk_clist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
968 969
	(GTK_CLIST_CLASS (GTK_OBJECT (clist)->klass)->draw_row)
	  (clist, NULL, row, clist_row);
970 971 972
    }
}

973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998
gint
gtk_clist_get_text (GtkCList * clist,
		    gint row,
		    gint column,
		    gchar ** text)
{
  GtkCListRow *clist_row;

  g_return_val_if_fail (clist != NULL, 0);

  if (row < 0 || row >= clist->rows)
    return 0;
  if (column < 0 || column >= clist->columns)
    return 0;

  clist_row = (g_list_nth (clist->row_list, row))->data;

  if (clist_row->cell[column].type != GTK_CELL_TEXT)
    return 0;

  if (text)
    *text = GTK_CELL_TEXT (clist_row->cell[column])->text;

  return 1;
}

999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017
void
gtk_clist_set_pixmap (GtkCList * clist,
		      gint row,
		      gint column,
		      GdkPixmap * pixmap,
		      GdkBitmap * mask)
{
  GtkCListRow *clist_row;

  g_return_if_fail (clist != NULL);

  if (row < 0 || row >= clist->rows)
    return;
  if (column < 0 || column >= clist->columns)
    return;

  clist_row = (g_list_nth (clist->row_list, row))->data;
  
  gdk_pixmap_ref (pixmap);
1018 1019 1020
  
  if (mask) gdk_pixmap_ref (mask);
  
1021
  cell_set_pixmap (clist, clist_row, column, pixmap, mask);
1022 1023 1024 1025

  /* redraw the list if it's not frozen */
  if (!GTK_CLIST_FROZEN (clist))
    {
1026
      if (gtk_clist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
1027 1028
	(GTK_CLIST_CLASS (GTK_OBJECT (clist)->klass)->draw_row)
	  (clist, NULL, row, clist_row);
1029 1030 1031
    }
}

1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
gint
gtk_clist_get_pixmap (GtkCList * clist,
		      gint row,
		      gint column,
		      GdkPixmap ** pixmap,
		      GdkBitmap ** mask)
{
  GtkCListRow *clist_row;

  g_return_val_if_fail (clist != NULL, 0);

  if (row < 0 || row >= clist->rows)
    return 0;
  if (column < 0 || column >= clist->columns)
    return 0;

  clist_row = (g_list_nth (clist->row_list, row))->data;

  if (clist_row->cell[column].type != GTK_CELL_PIXMAP)
    return 0;

  if (pixmap)
1054
  {
1055
    *pixmap = GTK_CELL_PIXMAP (clist_row->cell[column])->pixmap;
Owen Taylor's avatar
Owen Taylor committed
1056
    /* mask can be NULL */
1057
    *mask = GTK_CELL_PIXMAP (clist_row->cell[column])->mask;
1058
  }
1059 1060 1061 1062

  return 1;
}

1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
void
gtk_clist_set_pixtext (GtkCList * clist,
		       gint row,
		       gint column,
		       gchar * text,
		       guint8 spacing,
		       GdkPixmap * pixmap,
		       GdkBitmap * mask)
{
  GtkCListRow *clist_row;

  g_return_if_fail (clist != NULL);

  if (row < 0 || row >= clist->rows)
    return;
  if (column < 0 || column >= clist->columns)
    return;

  clist_row = (g_list_nth (clist->row_list, row))->data;
  
  gdk_pixmap_ref (pixmap);
1084
  if (mask) gdk_pixmap_ref (mask);
1085
  cell_set_pixtext (clist, clist_row, column, text, spacing, pixmap, mask);
1086 1087 1088 1089

  /* redraw the list if it's not frozen */
  if (!GTK_CLIST_FROZEN (clist))
    {
1090
      if (gtk_clist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE)
1091 1092
	(GTK_CLIST_CLASS (GTK_OBJECT (clist)->klass)->draw_row) 
	  (clist, NULL, row, clist_row);
1093 1094 1095
    }
}

1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124
gint
gtk_clist_get_pixtext (GtkCList * clist,
		       gint row,
		       gint column,
		       gchar ** text,
		       guint8 * spacing,
		       GdkPixmap ** pixmap,
		       GdkBitmap ** mask)
{
  GtkCListRow *clist_row;

  g_return_val_if_fail (clist != NULL, 0);

  if (row < 0 || row >= clist->rows)
    return 0;
  if (column < 0 || column >= clist->columns)
    return 0;

  clist_row = (g_list_nth (clist->row_list, row))->data;

  if (clist_row->cell[column].type != GTK_CELL_PIXTEXT)
    return 0;

  if (text)
    *text = GTK_CELL_PIXTEXT (clist_row->cell[column])->text;
  if (spacing)
    *spacing = GTK_CELL_PIXTEXT (clist_row->cell[column])->spacing;
  if (pixmap)
    *pixmap = GTK_CELL_PIXTEXT (clist_row->cell[column])->pixmap;
1125

Owen Taylor's avatar
Owen Taylor committed
1126
  /* mask can be NULL */
1127
  *mask = GTK_CELL_PIXTEXT (clist_row->cell[column])->mask;
1128 1129 1130 1131

  return 1;
}

1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
void
gtk_clist_set_foreground (GtkCList * clist,
			  gint row,
			  GdkColor * color)
{
  GtkCListRow *clist_row;

  g_return_if_fail (clist != NULL);

  if (row < 0 || row >= clist->rows)
    return;

  clist_row = (g_list_nth (clist->row_list, row))->data;
1145 1146 1147 1148 1149 1150 1151 1152

  if (color)
    {
      clist_row->foreground = *color;
      clist_row->fg_set = TRUE;
    }
  else
    clist_row->fg_set = FALSE;
1153

1154 1155
  if (!GTK_CLIST_FROZEN (clist)
      && (gtk_clist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE))
1156 1157
    (GTK_CLIST_CLASS (GTK_OBJECT (clist)->klass)->draw_row)
      (clist, NULL, row, clist_row);
1158 1159
}

1160 1161 1162 1163
void
gtk_clist_set_background (GtkCList * clist,
			  gint row,
			  GdkColor * color)
1164 1165 1166 1167 1168 1169 1170 1171 1172
{
  GtkCListRow *clist_row;

  g_return_if_fail (clist != NULL);

  if (row < 0 || row >= clist->rows)
    return;

  clist_row = (g_list_nth (clist->row_list, row))->data;
1173 1174 1175 1176 1177 1178 1179 1180

  if (color)
    {
      clist_row->background = *color;
      clist_row->bg_set = TRUE;
    }
  else
    clist_row->bg_set = FALSE;
1181

1182 1183
  if (!GTK_CLIST_FROZEN (clist)
      && (gtk_clist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE))
1184 1185
    (GTK_CLIST_CLASS (GTK_OBJECT (clist)->klass)->draw_row)
      (clist, NULL, row, clist_row);
1186 1187 1188 1189 1190 1191
}

void
gtk_clist_set_shift (GtkCList * clist,
		     gint row,
		     gint column,
1192
		     gint vertical,
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205
		     gint horizontal)
{
  GtkCListRow *clist_row;

  g_return_if_fail (clist != NULL);

  if (row < 0 || row >= clist->rows)
    return;
  if (column < 0 || column >= clist->columns)
    return;

  clist_row = (g_list_nth (clist->row_list, row))->data;

1206
  clist_row->cell[column].vertical = vertical;
1207 1208
  clist_row->cell[column].horizontal = horizontal;

1209 1210
  if (!GTK_CLIST_FROZEN (clist)
      && (gtk_clist_row_is_visible (clist, row) != GTK_VISIBILITY_NONE))
1211 1212
    (GTK_CLIST_CLASS (GTK_OBJECT (clist)->klass)->draw_row) 
      (clist, NULL, row, clist_row);
1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
}

gint
gtk_clist_append (GtkCList * clist,
		  gchar * text[])
{
  gint i;
  GtkCListRow *clist_row;

  g_return_val_if_fail (clist != NULL, -1);

1224
  clist_row = row_new (clist);
1225 1226
  clist->rows++;

1227 1228 1229 1230 1231 1232
  /* set the text in the row's columns */
  if (text)
    for (i = 0; i < clist->columns; i++)
      if (text[i])
        cell_set_text (clist, clist_row, i, text[i]);

1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
  /* keeps track of the end of the list so the list 
   * doesn't have to be traversed every time a item is added */
  if (!clist->row_list)
    {
      clist->row_list = g_list_append (clist->row_list, clist_row);
      clist->row_list_end = clist->row_list;

      /* check the selection mode to see if we should select
       * the first row automaticly */
      switch (clist->selection_mode)
	{
	case GTK_SELECTION_BROWSE:
	  gtk_clist_select_row (clist, 0, -1);
	  break;

	default:
	  break;
	}
    }
  else
    clist->row_list_end = (g_list_append (clist->row_list_end, clist_row))->next;