dialog-stf-preview.c 27.6 KB
Newer Older
1 2 3 4 5
/*
 * dialog-stf-preview.c : by utilizing the stf-parse engine this unit can
 *                        render sheet previews on the gnomecanvas and offers
 *                        functions for making this preview more interactive.
 *
6 7
 * Almer. S. Tigelaar <almer1@dds.nl>
 *
8 9 10 11 12
 * NOTES :
 * 1) This is rather sucky, it works, reasonably fast, but it's not ideal.
 * 2) There is a distinct difference between a formatted and non-formatted preview
 *    non-formatted previews are somewhat faster.
 *
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
 * MEMORY MANAGEMENT NOTES AND "HOW IT WORKS" :
 * In fact the preview does its work trough the stf_preview_render call
 * this takes a GSList as argument which on its turns also contains GSLists
 * which hold strings, so :
 *
 * GSList (Main)
 *  |
 *  |--- GSList (Sub) --> Contains strings as GSList->Data
 *  |
 *  |--- GSList (Sub) --> Contains strings as GSList->Data
 *
 * What it comes down to is that the stf_preview_render call actually
 * merges this which an internal hash. The previewing system is thus
 * responsible for freeing all the lists _and_ the strings. Ofcourse
 * it does not have to do this immediately (and in fact it does not)
 * It first puts the Sublists in a hash and 'overwrites' them when
 * newer 'version' of these sublists come available.
 *
 * This sounds complex, but is really simple in essence, to illustrate
 * see the diagram below. It tries to tell what happens to each item in
 * GSList (Sub)
 * (Note that the NULL item has a special meaning, it means that
 *  the line has been parsed before and we just have to use
 *  the existing value in the hash)
 *
 *     |-----< GSList (Sub) Item
 *     |
 * Is this item's data NULL?
 *     |
 *     |--------------------------------------
 *     |                                     |
 *    Nope                                  Yes
 *     |                                     |
 *     |                                Look up the existing list of strings
 *     |                                in the hash and use it for display
 *     |
 * Is this item already in hash?
 *     |
 *     |--------------------------------------
 *     |                                     |
 *    Yes                                   Nope
 *     |                                     |
 * Replace our hashed list of string    Insert the new list of strings
 * with this new one and use the        into the hash and use it for
 * new one for display                  display
 * (Free both the old list and it's
 *  strings!)
 *
 * Lists from the stf_parse_cached routines are _exacly_ in this format
 * look in the stf-parse.[ch] for more details on this.
63
 */ 
64

Morten Welinder's avatar
Morten Welinder committed
65
#include <config.h>
66 67
#include "format.h"
#include "number-match.h"
68
#include "value.h"
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 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 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 144 145 146 147 148 149 150 151 152 153 154 155 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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 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

#include "dialog-stf.h"
#include "dialog-stf-preview.h"

/******************************************************************************************************************
 * BASIC DRAWING FUNCTIONS
 ******************************************************************************************************************/

/**
 * stf_preview_draw_text
 * @group : group to add a new canvas item to
 * @text : text to render
 * @font : font to use
 * @color : render color
 * @x : x position to place text
 * @y : y position to place text
 *
 * will place @text at the @x, @y coordinates
 *
 * returns : the width of the rendered text
 **/
static double
stf_preview_draw_text (GnomeCanvasGroup *group, char *text, GdkFont *font, char *color, double x, double y)
{
	GnomeCanvasText *canvastext;
	double textwidth;

	g_return_val_if_fail (group != NULL, 0);
	g_return_val_if_fail (text != NULL, 0);
		
	canvastext = GNOME_CANVAS_TEXT (gnome_canvas_item_new (group,
							       gnome_canvas_text_get_type (),
							       "text", text,
							       "font_gdk", font,
							       "fill_color", color,
							       "anchor", GTK_ANCHOR_NW,
							       "x", x,
							       "y", y,
							       NULL));
	gtk_object_get (GTK_OBJECT (canvastext),
			"text_width", &textwidth,
			NULL);

	return textwidth;
}

/**
 * stf_preview_draw_line
 * @group : the gnome canvas group to add the line to
 * @color : color of the line
 * @x1 : first x coordinate
 * @y1 : first y coordinate
 * @x2 : second x coordinate
 * @y2 : second y coordinate
 *
 * Draws a line between two points
 *
 * returns : nothing
 **/
static void
stf_preview_draw_line (GnomeCanvasGroup *group, char *color, double x1, double y1, double x2, double y2)
{
	GnomeCanvasPoints *points = gnome_canvas_points_new (2);

	points->coords[0] = x1;
	points->coords[1] = y1;
	points->coords[2] = x2;
	points->coords[3] = y2;
	
	/* Render bottom and topline of the row */
	gnome_canvas_item_new (group,
			       gnome_canvas_line_get_type (),
			       "fill_color", color,
			       "points", points,
			       "width_pixels", 1,
			       NULL);

	gnome_canvas_points_unref (points);
}

/**
 * stf_preview_draw_box
 * @group : gnomecanvasgroup to add the box too
 * @color : fill color of the box
 * @x1 : left coordinate
 * @y1 : top coordinate
 * @x2 : right coordinate
 * @y2 : bottom coordinate
 *
 * Draws a box with @color as color and a transparant outline
 *
 * returns : nothing
 **/
static void
stf_preview_draw_box (GnomeCanvasGroup *group, char *color, double x1, double y1, double x2, double y2)
{

	gnome_canvas_item_new (group,
			       gnome_canvas_rect_get_type (),
			       "x1", x1, "y1", y1,
			       "x2", x2, "y2", y2,
			       "width_pixels", (int) 0,
			       "fill_color", color,
			       "outline_color", NULL,
			       NULL);	
}

/******************************************************************************************************************
 * INTERNAL HELPER FUNCTIONS
 ******************************************************************************************************************/

/**
 * stf_preview_get_table_pixel_width
 * @renderdata : struct containing rendering info
 *
 * will return the width in pixels of the table
 *
 * returns : width of table
 **/
static double
stf_preview_get_table_pixel_width (RenderData_t *renderdata)
{
	int i, tablewidth = 0;

	for (i = 0; i < renderdata->colwidths->len; i++) {
	
		tablewidth += g_array_index (renderdata->colwidths, int, i);
	}

	return (renderdata->charwidth * tablewidth) + (CELL_HPAD * renderdata->colwidths->len);
}

/**
 * stf_preview_get_table_pixel_height
 * @renderdata : struct containing rendering info
 * @rowcount : number of rows to be rendered
 *
 * will return the height in pixels of the table
 *
 * returns : height of table
 **/
static double
stf_preview_get_table_pixel_height (RenderData_t *renderdata, int rowcount)
{

	return (renderdata->charheight + CELL_VPAD) * rowcount;
}


/******************************************************************************************************************
 * ADVANCED DRAWING FUNCTIONS
 ******************************************************************************************************************/
 
/**
 * stf_preview_draw_grid
 * @renderdata : struct containing rendering info
 * @rowcount : number of rows to be rendered
 * @colcount : number of columns to be rendered
 *
 * Will draw a grid on a canvas
 *
 * returns : nothing
 **/
static void
stf_preview_draw_grid (RenderData_t *renderdata, int rowcount, int colcount)
{
	double rowheight   = renderdata->charheight + CELL_VPAD;
	double tableheight = stf_preview_get_table_pixel_height (renderdata, rowcount);
	double tablewidth  = stf_preview_get_table_pixel_width (renderdata);
	double xpos        = 0;
	double ypos        = 0;
240
	char *tempcolor;
241
	int i;
242 243 244 245 246 247 248 249 250 251
	
	if (renderdata->gridgroup != NULL)
		gtk_object_destroy (GTK_OBJECT (renderdata->gridgroup));
				
	renderdata->gridgroup = GNOME_CANVAS_GROUP (gnome_canvas_item_new (gnome_canvas_root (GNOME_CANVAS (renderdata->canvas)),
									   gnome_canvas_group_get_type(),
									   "x", 0.0,
									   "y", 0.0,
									   NULL));
	
252
	/* Color the first row */
253
	stf_preview_draw_box (renderdata->gridgroup, CAPTION_COLOR, 0, 0, tablewidth, rowheight);
254 255

	/* Color the rest of the table */
256
	stf_preview_draw_box (renderdata->gridgroup, ROW_COLOR, 0, rowheight, tablewidth, tableheight);
257 258 259 260 261 262 263 264 265 266 267 268

	/* now color the active column */
	if (renderdata->activecolumn != -1) {
		double colleft = 0;
		double colright = 0;
		
		for (i = 0; i < renderdata->activecolumn; i++) {

			colleft += renderdata->charwidth * g_array_index (renderdata->colwidths, int, i) + CELL_HPAD;
		}
		colright = colleft + (renderdata->charwidth * g_array_index (renderdata->colwidths, int, renderdata->activecolumn)) + CELL_HPAD;
	
269 270
		stf_preview_draw_box (renderdata->gridgroup, CAPTION_COLOR_ACTIVE, colleft, 0, colright, rowheight);
		stf_preview_draw_box (renderdata->gridgroup, ROW_COLOR_ACTIVE, colleft, rowheight, colright, tableheight);
271 272
	}

273
	tempcolor = CAPTION_LINE_COLOR;
274 275 276 277
	/* horizontal lines */
	for (i = 0; i < rowcount + 1; i++) {
	
		if (i == 2)
278
			tempcolor = LINE_COLOR;
279
	
280
		stf_preview_draw_line (renderdata->gridgroup, tempcolor, 0, ypos, tablewidth, ypos);
281 282 283 284 285 286 287 288

		ypos += rowheight;
	}

	/* vertical lines */
	i = 0;
	while (1) {

289 290
		stf_preview_draw_line (renderdata->gridgroup, CAPTION_LINE_COLOR, xpos, 0, xpos, rowheight);
		stf_preview_draw_line (renderdata->gridgroup, LINE_COLOR, xpos, rowheight, xpos, tableheight);
291 292 293 294 295 296 297 298

		if (i == colcount + 1)
			break;
		
		xpos += (renderdata->charwidth * g_array_index (renderdata->colwidths, int, i)) + CELL_HPAD;

		i++;
	}
299 300 301 302
	stf_preview_draw_line (renderdata->gridgroup, CAPTION_LINE_COLOR, xpos, 0, xpos, rowheight);

	gnome_canvas_item_lower_to_bottom ( (GnomeCanvasItem *) renderdata->gridgroup);
				
303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
	return;
}

/**
 * stf_preview_render_row
 * @renderdata : a renderdata struct
 * @rowy : the vertical position on which to draw the top of the row
 * @row : the row data itself
 * @colcount : the number of columns that must be rendered (if necessary empty) regardless of the contents of @row
 *
 * Basically renders @row's text on the canvas 
 *
 * returns : the bottom position of the row
 **/ 
static double
stf_preview_render_row (RenderData_t *renderdata, double rowy, GSList *row, int colcount)
{
	GSList *iterator = row;
	double xpos = 0.0;
	double textwidth = 0;
	int col = 0;

	if (iterator == NULL) { /* empty row */

		return rowy + CELL_VPAD + renderdata->charheight;
	}
329

330 331 332 333 334 335
	
	for (col = 0; col <= colcount; col++)  {
		int widthwanted;

		if (iterator != NULL && iterator->data != NULL) {

336 337 338 339 340 341 342 343
			/* In case the active color differs from the inactive color
			 * this code can be activated
			 *
			 * if (col == renderdata->activecolumn && rowy != 0)
			 *	 color = text_color_active;
			 * else
			 *	 color = text_color;
			 */
344 345
				
			textwidth = stf_preview_draw_text (renderdata->group,
346
							   iterator->data,
347
							   renderdata->font,
348
							   TEXT_COLOR,
349 350 351
							   xpos + (CELL_HPAD / 2),
							   rowy + (CELL_VPAD / 2));
		}
352
		
353 354
		widthwanted = renderdata->charwidth * g_array_index (renderdata->colwidths, int, col);
		
355
		if (textwidth > widthwanted) 
356 357 358 359
			xpos += textwidth;
		else
			xpos += widthwanted;

360

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382
		xpos += CELL_HPAD;
		
		iterator = g_slist_next (iterator);
	}

	return rowy + CELL_VPAD + renderdata->charheight;
}

/**
 * stf_preview_free_row
 * @row : a GSList row
 *
 * will free @row and the char*'s it points too
 *
 * returns : nothing
 **/
static void
stf_preview_free_row (GSList *row)
{
	GSList *iterator = row;

	while (iterator) {
383
		
384 385 386 387 388 389 390
		g_free ( (char *) iterator->data);
		iterator = g_slist_next (iterator);
	}

	g_slist_free (row);
}

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 420 421 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 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 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
/**
 * stf_preview_format_recalc_colwidths
 * @renderdata : renderdata struct
 * @data : a list containing lists with strings
 * @colcount : number of items in each list in @data
 *
 * This routine will iterate trough the list and calculate the ACTUAL widths
 * the actual widths are those as seen on screen by the user.
 * The reason for doing this is that the widths that are manually set can
 * differ from the widths of the formatted strings if the the strings have
 * to be rendered formatted (@renderdata->formatted == TRUE).
 *
 * returns : nothing. (swaps renderdata->temp and renderdata->colwidths);
 **/
static void
stf_preview_format_recalc_colwidths (RenderData_t *renderdata, GSList *data, int colcount)
{
	GArray *newwidths;
	GSList *iterator;
	int i;
	int widths[colcount];

	for (i = 0; i <= colcount; i++) {

		widths[i] = g_array_index (renderdata->colwidths, int, i);
	}
	
	iterator = data;
	while (iterator) {
		GSList *subiterator = iterator->data;
		int col;

		for (col = 0; col <= colcount; col++)  {
			int width;
			
			if (!subiterator || !subiterator->data) {

				subiterator = g_slist_next (subiterator);
				continue;
			}

			/* New width calculation */
			width = gdk_string_width (renderdata->font, subiterator->data) / renderdata->charwidth;

			if (width > widths[col])
				widths[col] = width;
			
			subiterator = g_slist_next (subiterator);
		}
		
		iterator = g_slist_next (iterator);
	}

	newwidths = g_array_new (FALSE, FALSE, sizeof (int));
	
	for (i = 0; i <= colcount; i++) {
	
		g_array_append_val (newwidths, widths[i]);	
	}

	/* We were called before, free our old 'actual' columnwidths */
	if (renderdata->temp != 0)
		g_array_free (renderdata->temp, TRUE);
		
	renderdata->temp = renderdata->colwidths;
	renderdata->colwidths = newwidths;
}


/**
 * stf_preview_format_line
 * @renderdata : renderdata struct
 * @data : a list containing strings
 * @colcount : number of items in @list
 *
 * formats a single list of strings
 *
 * returns : nothing
 **/
static void
stf_preview_format_line (RenderData_t *renderdata, GSList *data, int colcount)
{
	int col;
	GSList *iterator = data;

	for (col = 0; col <= colcount; col++)  {
		Value *value;
		StyleFormat *sf;
		StyleColor *color;
		char *celltext;
			
		if (!iterator || !iterator->data) {

			iterator = g_slist_next (iterator);
			continue;
		}

		/* Formatting */
489
		if (NULL == (value = format_match (iterator->data, NULL)))
490 491 492 493
			value = value_new_string (iterator->data);
		
		sf = style_format_new (g_ptr_array_index (renderdata->colformats, col));
			
Jody Goldberg's avatar
Jody Goldberg committed
494
		celltext = format_value (sf, value, &color, iterator->data);
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
			
		style_format_unref (sf);
		value_release (value);

		/* Replacement of old data */
		g_free (iterator->data);
		iterator->data = celltext;
			
		iterator = g_slist_next (iterator);
	}
}

/**
 * stf_preview_format_recalc_colwidths
 * @renderdata : renderdata struct
 * @data : a list containing lists with strings
 * @colcount : number of items in each list in @data
 *
 * Walks trough a list which has been acquired by calling parse_sheet_cached and
 * creates a new list which is a merge of items that were in the hash and items
 * that are 'new'
 *
 * returns : a new list (which the calling routine HAS to free)
 **/
static GSList *
stf_preview_merge_with_hash (RenderData_t *renderdata, GSList *list, int colcount)
{
	GSList *result = NULL;
523 524
	GSList *result_end = NULL;
	GSList *iterator = list;
525 526
	int i = 0;
	
527
	while (iterator) {
528
		GSList *data;
529
		int row = renderdata->startrow + i;
530 531

		if (iterator->data == NULL) {
532 533 534 535

			data = g_hash_table_lookup (renderdata->hashtable, &row);

			/*
536 537
			 * If the data is NULL we simply assume
			 * this means the line is empty
538
			 */
539
		} else {
540 541 542 543 544 545 546 547 548 549
			int *key;
			GSList *orig_data;

			if (g_hash_table_lookup_extended (renderdata->hashtable, &row, (gpointer*) &key, (gpointer*) &orig_data)) {

				stf_preview_free_row (orig_data);
			} else {

				key = g_new (int, 1);
				*key = row;
550 551
			}

552 553 554 555
			data = iterator->data;
			
			g_hash_table_insert (renderdata->hashtable, key, data);
			
556 557 558 559
			/* Reformat the line */
			if (renderdata->formatted && data != NULL)
				stf_preview_format_line (renderdata, data, colcount);
		}
560
		
561 562
		if (result != NULL) {
		
563
			result_end = g_slist_append (result_end, data)->next;
564 565 566
		} else {
			
			result = g_slist_append (result, data);
567
			result_end = result;
568 569 570 571 572 573 574
		}
		
		iterator = g_slist_next (iterator);
		i++;
	}

	g_slist_free (list);
575

576 577 578
	return result;
}

579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595
/**
 * stf_preview_render
 * @renderdata : a renderdata struct
 * @list : a list containing the rows of of data to display
 * @rowcount : number of rows in @list
 * @colcount : number of cols in @list
 *
 * This will render a preview on the canvas.
 *
 * returns : nothing
 **/
void
stf_preview_render (RenderData_t *renderdata, GSList *list, int rowcount, int colcount)
{
	GnomeCanvasRect *centerrect;
	GSList *iterator;
	GSList *captions;
596
	GArray *dummy;
597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
	int i;
	double ypos = 0.0;
	
	g_return_if_fail (renderdata != NULL);
	g_return_if_fail (renderdata->canvas != NULL);
	g_return_if_fail (list != NULL);
	
	if (renderdata->group != NULL)
		gtk_object_destroy (GTK_OBJECT (renderdata->group));

	if (rowcount < 1) 
		return;
				
	renderdata->group = GNOME_CANVAS_GROUP (gnome_canvas_item_new (gnome_canvas_root (GNOME_CANVAS (renderdata->canvas)),
							   gnome_canvas_group_get_type(),
							   "x", 0.0,
							   "y", 0.0,
							   NULL));
615 616 617
		
	list = stf_preview_merge_with_hash (renderdata, list, colcount);

618
	if (renderdata->formatted) 
619 620
		stf_preview_format_recalc_colwidths (renderdata, list, colcount);
		
621 622 623 624
	/* Generate column captions and prepend them */
	captions = NULL;
	for (i = 0; i <= colcount; i++) {
		char *text = g_strdup_printf (_(COLUMN_CAPTION), i);
625

626 627 628
		captions = g_slist_append (captions, text);
	}

629
	ypos = stf_preview_render_row (renderdata, ypos, captions, colcount);
630
	
631
	/* Render line by line */		   
632
	iterator = list;
633 634 635
 	while (iterator) {
	
		ypos = stf_preview_render_row (renderdata, ypos, iterator->data, colcount);
636

637
		iterator = g_slist_next (iterator);
638
	}
639 640 641

	stf_preview_draw_grid (renderdata, rowcount, colcount);
	
642 643 644 645 646 647 648 649
	centerrect = GNOME_CANVAS_RECT (gnome_canvas_item_new (renderdata->group,
							       gnome_canvas_rect_get_type (),
							       "x1", 0,	 "y1", 0,
							       "x2", 10, "y2", 10,
							       "width_pixels", (int) 0,
							       "fill_color", "white",
							       NULL));
							       
650
	stf_dialog_set_scroll_region_and_prevent_center (renderdata->canvas,
651 652 653 654 655 656 657 658 659 660 661 662
							 centerrect,
							 stf_preview_get_table_pixel_width (renderdata),
							 stf_preview_get_table_pixel_height (renderdata, rowcount));

	iterator = captions;
	while (iterator) {
	
		g_free (iterator->data);

		iterator = g_slist_next (iterator);
	}
	g_slist_free (captions);
663 664 665 666 667 668 669 670

	/* Swap the actual column widths with the set columnwidths */
	if (renderdata->formatted) {
	
		dummy                 = renderdata->temp;
		renderdata->temp      = renderdata->colwidths;
		renderdata->colwidths = dummy;
	}
671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	
	g_slist_free (list);
}

/******************************************************************************************************************
 * STRUCTURE MANIPULATION FUNCTIONS
 ******************************************************************************************************************/

/**
 * stf_preview_new
 * @canvas : a gnomecanvas
 * @formatted : if set the values will be rendered formatted
 *
 * returns : a new renderdata struct
 **/
RenderData_t*
stf_preview_new (GnomeCanvas *canvas, gboolean formatted)
{
	RenderData_t* renderdata;

	g_return_val_if_fail (canvas != NULL, NULL);
	
	renderdata = g_new (RenderData_t, 1);

	renderdata->canvas       = canvas;
	renderdata->formatted    = formatted;
	renderdata->startrow     = 1;
	renderdata->colwidths    = g_array_new (FALSE, FALSE, sizeof (int));
	renderdata->actualwidths = g_array_new (FALSE, FALSE, sizeof (int));
	renderdata->colformats   = g_ptr_array_new ();
701
	renderdata->temp         = NULL;
702
	renderdata->group        = NULL;
703
	renderdata->gridgroup    = NULL;
704
	renderdata->hashtable    = g_hash_table_new (g_int_hash, g_int_equal);
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 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

	renderdata->font       = gdk_font_load ("fixed");
	renderdata->charwidth  = gdk_string_width (renderdata->font, "W");
	renderdata->charheight = gdk_string_height (renderdata->font, "W");

	renderdata->activecolumn = -1;
	
	return renderdata;
}

/**
 * stf_preview_hash_item_remove
 * @key : the key
 * @value : a value
 * @user_data : some misc data
 *
 * This will free an item in the cache, both the int* key and the GSList* value will be
 * freed
 *
 * returns : always TRUE
 **/
static gboolean
stf_preview_hash_item_remove (gpointer key, gpointer value, gpointer user_data)
{
	g_free (key);
	stf_preview_free_row ( (GSList *) value);

	return TRUE;
}

/**
 * stf_preview_free
 * @renderdata : a renderdata struct
 *
 * This will free the @renderdata and if it's hash is not shared
 * across multiple previews it will also free the hash
 *
 * returns : nothing
 **/
void
stf_preview_free (RenderData_t *renderdata)
{
	g_return_if_fail (renderdata != NULL);

	if (renderdata->group != NULL)
		gtk_object_destroy (GTK_OBJECT (renderdata->group));
751 752 753

	if (renderdata->formatted && renderdata->temp != NULL)
		g_array_free (renderdata->temp, TRUE);
754 755 756 757 758
		
	g_array_free (renderdata->colwidths, TRUE);
	g_array_free (renderdata->actualwidths, TRUE);
	g_ptr_array_free (renderdata->colformats, TRUE);
	
759 760 761 762
	g_hash_table_foreach_remove (renderdata->hashtable,
				     stf_preview_hash_item_remove,
				     NULL);
	g_hash_table_destroy (renderdata->hashtable);
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 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883

	gdk_font_unref (renderdata->font);
	
	g_free (renderdata);
}

/**
 * stf_preview_set_startrow
 * @renderdata : struct containing rendering information
 * @startrow : the new row to start rendering at
 *
 * This will set a new row to start rendering.
 *
 * returns : nothing
 **/
void
stf_preview_set_startrow (RenderData_t *renderdata, int startrow)
{
	g_return_if_fail (renderdata != NULL);
	g_return_if_fail (startrow >= 0);
	
	renderdata->startrow = startrow;
}

/**
 * stf_preview_set_activecolumn
 * @renderdata : struct containing rendering information
 * @column : column to set as active column
 *
 * This will set the active column, if you pass -1 as @column no column
 * will be drawn in 'active' state
 *
 * returns : nothing
 **/
void
stf_preview_set_activecolumn (RenderData_t *renderdata, int column)
{
	g_return_if_fail (renderdata != NULL);
	g_return_if_fail (column >= -1);

	renderdata->activecolumn = column;
}

/**
 * stf_preview_colwidths_clear
 * @renderdata : a struct containing rendering information
 *
 * This will clear the @renderdata->colwidths array which contains the minimum width of
 * each column.
 *
 * returns : nothing
 **/
void
stf_preview_colwidths_clear (RenderData_t *renderdata)
{
	g_return_if_fail (renderdata != NULL);

	g_array_free (renderdata->colwidths, TRUE);
	renderdata->colwidths = g_array_new (FALSE, FALSE, sizeof (int));

	g_array_free (renderdata->actualwidths, TRUE);
	renderdata->actualwidths = g_array_new (FALSE, FALSE, sizeof (int));
}

/**
 * stf_preview_colwidths_add
 * @renderdata : a struct containing rendering information
 * @width : the width of the next column
 *
 * This will add an entry to the @renderdata->colwidths array.
 *
 * returns : nothing
 **/
void
stf_preview_colwidths_add (RenderData_t *renderdata, int width)
{
	char *caption;
	int captionwidth;

	g_return_if_fail (renderdata != NULL);
	
	if (width < 0)
		width = 0;

	caption = g_strdup_printf (_(COLUMN_CAPTION), renderdata->colwidths->len);
	captionwidth = gdk_string_width (renderdata->font, caption) / renderdata->charwidth;

	if (captionwidth > width)
		g_array_append_val (renderdata->colwidths, captionwidth);
	else
		g_array_append_val (renderdata->colwidths, width);

	g_array_append_val (renderdata->actualwidths, width);
		
	g_free (caption);
}

/**
 * stf_preview_colformats_clear
 * @renderdata : a struct containing rendering information
 *
 * This will clear the @renderdata->colformats array which contains the format of
 * each column.
 *
 * returns : nothing
 **/
void
stf_preview_colformats_clear (RenderData_t *renderdata)
{
	g_return_if_fail (renderdata != NULL);

	g_ptr_array_free (renderdata->colformats, TRUE);
	renderdata->colformats = g_ptr_array_new ();
}

/**
 * stf_preview_colformats_add
 * @renderdata : a struct containing rendering information
 * @format : the format of the column
 *
 * This will add an entry to the @renderdata->colformats array.
884 885
 * The widths of the columns will be set to at least have the width of
 * the @format.
886 887 888 889 890 891
 *
 * returns : nothing
 **/
void
stf_preview_colformats_add (RenderData_t *renderdata, char *format)
{
892

893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
	g_return_if_fail (renderdata != NULL);
	g_return_if_fail (format != NULL);

	g_ptr_array_add (renderdata->colformats, format);
}

/******************************************************************************************************************
 * PUBLIC UTILITY FUNCTIONS 
 ******************************************************************************************************************/

/**
 * stf_preview_get_displayed_rowcount
 * @renderdata : a struct containing rendering information
 *
 * returns : number of rows that can be displayed on the @renderdata->canvas
 **/
int
stf_preview_get_displayed_rowcount (RenderData_t *renderdata)
{
	int canvasheight, rowcount = 0;

	g_return_val_if_fail (renderdata != NULL, 0);
	g_return_val_if_fail (renderdata->canvas != NULL, 0);
	
	
	gtk_object_get (GTK_OBJECT (renderdata->canvas),
			"height", &canvasheight,
			NULL);
	
922
	rowcount = (canvasheight / (gdk_string_height (renderdata->font, "Test"))) + 1;
923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939

	return rowcount;
}

/**
 * stf_preview_get_column_at_x
 * @renderdata : a struct containing rendering information
 * @x : x coordinate
 *
 * Given the @x coordinate this function will determine what column is located at the @x coordinate
 *
 * returns : the column number the user clicked on or -1 if the user did not click on a column at all
 **/
int
stf_preview_get_column_at_x (RenderData_t *renderdata, double x)
{
	int i, xpos = 0;
940
	GArray *sourcearray;
941 942

	g_return_val_if_fail (renderdata != NULL, -1);
943 944 945 946 947

	if (renderdata->formatted)
		sourcearray = renderdata->temp;
	else
		sourcearray = renderdata->colwidths;
948 949 950
	
	for (i = 0; i < renderdata->colwidths->len; i++) {
	
951
		xpos += (renderdata->charwidth * g_array_index (sourcearray, int, i)) + CELL_HPAD;
952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970

		if (xpos > x)
			break;
	}

	if (i > renderdata->colwidths->len - 1)
		return -1;
	else
		return i;
}

/**
 * stf_preview_get_column_border_at_x
 * @renderdata : a struct containing rendering information
 * @x : x coordinate
 *
 * Given the @x coordinate this function will determine weather @x is
 * over a column border. (the right side of a column)
 *
971 972
 * NOTE : Can't use this on a formatted preview
 *
973 974 975 976 977 978 979 980 981
 * returns : the number of the column @x is at or -1 if it is not positioned over a column border
 **/
int
stf_preview_get_column_border_at_x (RenderData_t *renderdata, double x)
{
	int i, xpos = 0;
	gboolean broken = FALSE;

	g_return_val_if_fail (renderdata != NULL, -1);
982
	g_return_val_if_fail (renderdata->formatted == FALSE, -1);
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020

	for (i = 0; i < renderdata->colwidths->len; i++) {
	
		xpos += (renderdata->charwidth * g_array_index (renderdata->colwidths, int, i)) + CELL_HPAD;

		if ( (x >= xpos - MOUSE_SENSITIVITY) && (x <= xpos + MOUSE_SENSITIVITY)) {

			broken = TRUE;
			break;
		}
	}
	
	if (broken) {
	
		if (i >= renderdata->colwidths->len - 1)
			return -1;
		else
			return i;
	}
	else {
	
		return -1;
	}
}

/**
 * stf_preview_get_char_at_x
 * @renderdata : a struct containing rendering information
 * @x : x coordinate
 *
 * Given the @x coordinate this function will the index of the character @x is at, e.g.
 *
 * Column 0 | Column 1 | Column 2
 * a          b          cd
 *
 * if the mouse is over 'b' this function will return 1 because it is the second character,
 * and if it is over d it will return 3.
 *
1021 1022
 * NOTE : Can't use this on a formatted preview
 *
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
 * returns : the character index of the character below @x or -1 if none.
 **/
int
stf_preview_get_char_at_x (RenderData_t *renderdata, double x)
{
	int i;
	double xpos = 0, subxpos = 0, tpos = 0;
	int charindex = 0;

	g_return_val_if_fail (renderdata != NULL, -1);
1033 1034
	g_return_val_if_fail (renderdata->formatted == FALSE, -1);
	
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057
	for (i = 0; i < renderdata->actualwidths->len; i++) {
			
		tpos = xpos + (renderdata->charwidth * g_array_index (renderdata->actualwidths, int, i));
		
		while (subxpos < tpos) {

			if (x > subxpos && x < (subxpos + renderdata->charwidth + 1))
				return charindex;
		
			subxpos += renderdata->charwidth;
			charindex++;

		}
		
		xpos += (renderdata->charwidth * g_array_index (renderdata->colwidths, int, i)) + CELL_HPAD;
		subxpos = xpos;
	}

	return -1;
}



1058 1059 1060 1061 1062





1063 1064