format-template.c 31.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * format-template.c : implementation of the template handling system.
 *
 * Copyright (C) Almer. S. Tigelaar.
 * E-mail: almer1@dds.nl or almer-t@bigfoot.com
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

22 23
#include <gnumeric-config.h>
#include "gnumeric.h"
24
#include "format-template.h"
25

26
#include "mstyle.h"
27 28
#include "xml-io-autoft.h"
#include "sheet.h"
Jody Goldberg's avatar
Jody Goldberg committed
29
#include "sheet-style.h"
30
#include "style-border.h"
31 32
#include "command-context.h"
#include "ranges.h"
33 34
#include <libgnome/gnome-i18n.h>
#include <libgnome/gnome-util.h>
35

36 37 38 39 40 41 42 43 44 45 46 47
/******************************************************************************
 * Hash table related callbacks and functions
 ******************************************************************************
 *
 * These are basically wrapper function around GHashTable ment for use by
 * the FormatTemplate.
 * The hashtable will manage it's own resources, it will copy values instead
 * of using the ones passed.
 */

/**
 * hash_table_destroy_entry_cb:
48 49 50 51
 * @pkey:
 * @pvalue:
 * @data:
 *
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
 * Callback for destroying a single key/value pair from a GHashTable
 *
 * Returns : Always TRUE
 **/
static gboolean
hash_table_destroy_entry_cb (gpointer pkey, gpointer pvalue, gpointer data)
{
	g_free ((guint *) pkey);
	mstyle_unref ((MStyle *) pvalue);

	return TRUE;
}

/**
 * hash_table_destroy:
 * @table:
68
 *
69 70 71 72 73 74 75
 * Destroy the hashtable (including the keys and values)
 *
 * Returns : Always NULL
 **/
static GHashTable *
hash_table_destroy (GHashTable *table)
{
Morten Welinder's avatar
Morten Welinder committed
76
	int size, removed;
77 78 79 80 81 82 83 84 85 86

	if (table == NULL)
		return NULL;

	/*
	 * Remove all items from the table, compare the number of
	 * removed items to the number of items in the table
	 * before removal as a sanity check
	 */
	size = g_hash_table_size (table);
Morten Welinder's avatar
Morten Welinder committed
87
	removed = g_hash_table_foreach_remove (table, hash_table_destroy_entry_cb, NULL);
88

Morten Welinder's avatar
Morten Welinder committed
89
	if (removed != size)
90 91 92
		g_warning ("format-template.c: Not all items removed from hash table!");

	g_hash_table_destroy (table);
93

94 95 96 97 98
	return NULL;
}

/**
 * hash_table_create:
99
 *
100 101 102 103 104 105
 * Create a new hashtable for the format template
 *
 * Returns : A new hashtable
 **/
static GHashTable *
hash_table_create (void)
106
{
107 108 109 110 111
	return g_hash_table_new (g_int_hash, g_int_equal);
}

/**
 * hash_table_generate_key:
112 113 114
 * @row:
 * @col:
 *
115 116
 * Generate a key from row, col coordinates.
 * This key is always unique provided that row and col are
117
 * both below 65536 (2^16).
118 119 120 121 122 123 124 125 126
 *
 * Return value: The key
 **/
static guint
hash_table_generate_key (int row, int col)
{
	guint key;

	key = (row << 16) + col;
127

128 129 130 131 132
	return key;
}

/**
 * hash_table_lookup:
133 134 135 136
 * @table:
 * @row:
 * @col:
 *
137
 * Looks up a the value of a row,col pair in the hash table
138
 *
139 140 141 142 143 144 145 146 147
 * Return value: The MStyle associated with the coordinates
 **/
static MStyle *
hash_table_lookup (GHashTable *table, int row, int col)
{
	MStyle *result;
	guint key;

	key = hash_table_generate_key (row, col);
148

149 150 151 152 153 154 155
	result = g_hash_table_lookup (table, &key);

	return result;
}

/**
 * hash_table_insert:
156 157 158
 * @table:
 * @row:
 * @col:
159 160
 * @mstyle:
 * @merge_colors:
161
 *
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
 * Insert a new entry into the hashtable for row, col. Note that
 * if there is already an existing entry for those coordinates the
 * existing MStyle and the new one will be merges together to form
 * a new style
 **/
static void
hash_table_insert (GHashTable *table, int row, int col, MStyle *mstyle)
{
	MStyle *orig_value = NULL;
	guint *orig_key = NULL;
	guint key;

	g_return_if_fail (table != NULL);
	g_return_if_fail (mstyle != NULL);

	key = hash_table_generate_key (row, col);
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
	/*
	 * If an entry for this col/row combination does not
	 * yet exist then simply create a new entry in the hash table
	 */
	if (!g_hash_table_lookup_extended (table, &key, (gpointer *) &orig_key, (gpointer *) &orig_value)) {
		guint *pkey = g_new (guint, 1);

		*pkey = key;

		g_hash_table_insert (table, pkey, mstyle_copy (mstyle));
	} else {

		/*
		 * Overwrite any existing entry in the hashtable
		 * FIXME : Is this the right way to handle this?
		 */
		mstyle_unref (orig_value);
		g_hash_table_insert (table, orig_key, mstyle_copy (mstyle));
	}
}

/******************************************************************************
 * FormatColRowInfo - Construction
 ******************************************************************************/

/**
 * format_col_row_info_make:
 * @offset: Desired offset
 * @offset_gravity: Gravity
 * @size: The size
209
 *
210 211 212 213 214 215 216 217 218 219 220 221 222 223
 * This function is simply an easy way to create a FormatColRowInfo
 * instantly
 *
 * Return value: The 3 arguments inside a FormatColRowInfo
 **/
FormatColRowInfo
format_col_row_info_make (int offset, int offset_gravity,
			  int size)
{
	FormatColRowInfo new;

	new.offset = offset;
	new.offset_gravity = offset_gravity;
	new.size = size;
224

225 226 227 228 229 230 231 232 233
	return new;
}

/******************************************************************************
 * FormatTemplateMember - Getters/setters and creation
 ******************************************************************************/

/**
 * format_template_member_new:
234
 *
235
 * Create a new TemplateMember
236
 *
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
 * Return value: the new TemplateMember
 **/
TemplateMember *
format_template_member_new (void)
{
	TemplateMember *member;

	member = g_new (TemplateMember, 1);

	format_template_member_set_row_info (member, format_col_row_info_make (0, 1, 1));
	format_template_member_set_col_info (member, format_col_row_info_make (0, 1, 1));
	member->direction = FREQ_DIRECTION_NONE;
	member->repeat    = 0;
	member->skip      = 0;
	member->edge      = 0;
	member->mstyle    = NULL;

	return member;
}

257 258
/**
 * format_template_member_clone:
259
 *
260
 * Clone a template member
261
 *
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
 * Return value: a copy of @member
 **/
TemplateMember *
format_template_member_clone (TemplateMember *member)
{
	TemplateMember *clone;

	clone = format_template_member_new ();

	clone->row = member->row;
	clone->col = member->col;
	clone->direction = member->direction;
	clone->repeat    = member->repeat;
	clone->skip      = member->skip;
	clone->edge      = member->edge;
	clone->mstyle    = member->mstyle;
	mstyle_ref (member->mstyle);

	return clone;
}

283 284 285
/**
 * format_template_member_free:
 * @member: TemplateMember
286
 *
287 288 289 290 291 292
 * Frees an existing template member
 **/
void
format_template_member_free (TemplateMember *member)
{
	g_return_if_fail (member != NULL);
293

294 295 296 297 298 299 300 301 302
	if (member->mstyle)
		mstyle_unref (member->mstyle);

	g_free (member);
}


/**
 * format_template_member_get_rect:
303
 * @member:
304
 * @r:
305
 *
306
 * Get the rectangular area covered by the TemplateMember @member in the parent
Jody Goldberg's avatar
Jody Goldberg committed
307
 * rectangle @r.
308 309 310 311 312
 * NOTE : This simply calculates the rectangle, it does not calculate repetitions
 *        or anything. That you'll have to do yourself :-)
 *
 * Return value: a Range containing the effective rectangle of @member
 **/
Jody Goldberg's avatar
Jody Goldberg committed
313
static Range
314
format_template_member_get_rect (TemplateMember *member, Range const *r)
315
{
316
	Range res;
317

318 319
	res.start.row = res.end.row = 0;
	res.start.col = res.end.col = 0;
320

321
	g_return_val_if_fail (member != NULL, res);
322

323
	/* Calculate where the top left of the rectangle will come */
324
	if (member->row.offset_gravity > 0)
325
		res.start.row = r->start.row + member->row.offset;
326
	else
327
		res.end.row = r->end.row - member->row.offset;
328

329
	if (member->col.offset_gravity > 0)
330
		res.start.col = r->start.col + member->col.offset;
331
	else
332
		res.end.col = r->end.col - member->col.offset;
333

334 335 336 337 338 339
	/*
	 * Now that we know these coordinates we'll calculate the
	 * bottom right coordinates
	 */
	if (member->row.offset_gravity > 0) {
		if (member->row.size > 0)
340
			res.end.row = res.start.row + member->row.size - 1;
341
		else
342
			res.end.row = r->end.row + member->row.size;
343 344
	} else {
		if (member->row.size > 0)
345
			res.start.row = res.end.row - member->row.size + 1;
346
		else
347
			res.start.row = r->start.row - member->row.size;
348 349 350 351
	}

	if (member->col.offset_gravity > 0) {
		if (member->col.size > 0)
352
			res.end.col = res.start.col + member->col.size - 1;
353
		else
354
			res.end.col = r->end.col + member->col.size;
355 356
	} else {
		if (member->col.size > 0)
357
			res.start.col = res.end.col - member->col.size + 1;
358
		else
359
			res.start.col = r->start.col - member->col.size;
360 361
	}

362
	return res;
363 364 365 366 367 368 369 370
}

/******************************************************************************
 * Getters and setters for FormatTemplateMember
 *
 * NOTE : MStyle are taken care of internally, there is no
 *        need to unref or ref mstyle's manually.
 */
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 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
FormatColRowInfo
format_template_member_get_row_info (TemplateMember *member)
{
	return member->row;
}

FormatColRowInfo
format_template_member_get_col_info (TemplateMember *member)
{
	return member->col;
}

FreqDirection
format_template_member_get_direction (TemplateMember *member)
{
	return member->direction;
}

int
format_template_member_get_repeat (TemplateMember *member)
{
	return member->repeat;
}

int
format_template_member_get_skip (TemplateMember *member)
{
	return member->skip;
}

int
format_template_member_get_edge (TemplateMember *member)
{
	return member->edge;
}

MStyle *
format_template_member_get_style (TemplateMember *member)
{
	return member->mstyle;
}

void
format_template_member_set_row_info (TemplateMember *member, FormatColRowInfo row_info)
{
	g_return_if_fail (row_info.offset >= 0);
	g_return_if_fail (row_info.offset_gravity == -1 || row_info.offset_gravity == +1);

	member->row = row_info;
}

void
format_template_member_set_col_info (TemplateMember *member, FormatColRowInfo col_info)
{
	g_return_if_fail (col_info.offset >= 0);
	g_return_if_fail (col_info.offset_gravity == -1 || col_info.offset_gravity == +1);

	member->col = col_info;
}

void
format_template_member_set_direction (TemplateMember *member, FreqDirection direction)
{
	g_return_if_fail (direction == FREQ_DIRECTION_NONE || direction == FREQ_DIRECTION_HORIZONTAL ||
			  direction == FREQ_DIRECTION_VERTICAL);

	member->direction = direction;
}

void
format_template_member_set_repeat (TemplateMember *member, int repeat)
{
	g_return_if_fail (repeat >= -1);

	member->repeat = repeat;
}

void
format_template_member_set_skip (TemplateMember *member, int skip)
{
	g_return_if_fail (skip >= 0);

	member->skip = skip;
}

void
format_template_member_set_edge (TemplateMember *member, int edge)
{
	g_return_if_fail (edge >= 0);

	member->edge = edge;
}

void
format_template_member_set_style (TemplateMember *member, MStyle *mstyle)
{
468 469
	MStyle *mstyle_default;
	
470 471 472 473 474
	g_return_if_fail (mstyle != NULL);

	if (member->mstyle)
		mstyle_unref (member->mstyle);

475 476 477 478 479 480 481 482 483
	/*
	 * We need to do some magic here. The problem is that the new
	 * mstyle might not have _all_ elements set and we _do_ need it
	 * to have all elements set. We therefore merge with the default
	 * mstyle.
	 */
	mstyle_default = mstyle_new_default ();
	member->mstyle = mstyle_copy_merge (mstyle_default, mstyle);
	mstyle_unref (mstyle_default);
484 485 486 487 488
}

/******************************************************************************
 * FormatTemplate - Creation/Destruction
 ******************************************************************************/
489

490 491
/**
 * format_template_new:
492
 * @context: a WorkbookControl
493
 *
494
 * Create a new 'empty' FormatTemplate
495
 *
496 497 498
 * Return value: the new FormatTemplate
 **/
FormatTemplate *
499
format_template_new (WorkbookControl *context)
500 501 502 503
{
	FormatTemplate *ft;

	g_return_val_if_fail (context != NULL, NULL);
504

505 506 507
	ft = g_new0 (FormatTemplate, 1);

	ft->filename    = g_string_new ("");
508

509
	ft->author      = g_string_new (g_get_real_name ());
510
	ft->name        = g_string_new (_("Name"));
511
	ft->description = g_string_new ("");
512 513

	ft->category = NULL;
514

515 516
	ft->members = NULL;
	ft->context = context;
517

518 519 520 521 522 523
	ft->number    = TRUE;
	ft->border    = TRUE;
	ft->font      = TRUE;
	ft->patterns  = TRUE;
	ft->alignment = TRUE;

524
	ft->table     = hash_table_create ();
525 526
	ft->invalidate_hash = TRUE;

527
	range_init (&ft->dimension, 0,0,0,0);
528

529 530 531 532 533 534
	return ft;
}

/**
 * format_template_free:
 * @ft: FormatTemplate
535
 *
536
 * Free @ft
537
 *
538 539 540 541 542 543 544 545 546
 **/
void
format_template_free (FormatTemplate *ft)
{
	GSList *iterator;

	g_return_if_fail (ft != NULL);

	g_string_free (ft->filename, TRUE);
547

548 549 550 551
	g_string_free (ft->author, TRUE);
	g_string_free (ft->name, TRUE);
	g_string_free (ft->description, TRUE);

552
	iterator = ft->members;
553 554 555 556
	while (iterator) {
		TemplateMember *member = iterator->data;

		format_template_member_free (member);
557

558 559 560 561 562
		iterator = g_slist_next (iterator);
	}
	g_slist_free (ft->members);

	ft->table = hash_table_destroy (ft->table);
563

564 565 566
	g_free (ft);
}

567 568 569
/**
 * format_template_clone:
 * @ft: FormatTemplate
570
 *
571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588
 * Make a copy of @ft.
 *
 * Returns : a copy of @ft
 **/
FormatTemplate *
format_template_clone (FormatTemplate *ft)
{
	FormatTemplate *clone;
	GSList *iterator = NULL;

	g_return_val_if_fail (ft != NULL, NULL);

	clone = format_template_new (ft->context);

	clone->filename    = g_string_new (ft->filename->str);
	clone->author      = g_string_new (ft->author->str);
	clone->name        = g_string_new (ft->name->str);
	clone->description = g_string_new (ft->description->str);
589 590

	clone->category    = ft->category;
591

592
	iterator = ft->members;
593 594 595 596 597 598 599 600 601 602 603 604 605
	while (iterator) {
		TemplateMember *member = format_template_member_clone ((TemplateMember *) iterator->data);

		format_template_attach_member (clone, member);
		iterator = g_slist_next (iterator);
	}

	clone->number    = ft->number;
	clone->border    = ft->border;
	clone->font      = ft->font;
	clone->patterns  = ft->patterns;
	clone->alignment = ft->alignment;

606
	clone->dimension = ft->dimension;
607 608

	clone->invalidate_hash = TRUE;
609

610 611 612
	return clone;
}

613 614
/**
 * format_template_new_from_file:
615
 * @context: a WorkbookControl
616
 * @filename: The filename to load from
617
 *
618 619
 * Create a new FormatTemplate and load a template file
 * into it.
620
 *
621 622 623
 * Return value: a new FormatTemplate (or NULL on error)
 **/
FormatTemplate *
624
format_template_new_from_file (WorkbookControl *context, const char *filename)
625 626 627 628 629 630 631 632
{
	FormatTemplate *ft;

	g_return_val_if_fail (context != NULL, NULL);
	g_return_val_if_fail (filename != NULL, NULL);

	if (!g_file_exists (filename))
		return NULL;
633

634 635 636 637
	ft = format_template_new (context);

	g_string_free (ft->filename, TRUE);
	ft->filename = g_string_new (filename);
638

639
	if (gnumeric_xml_read_format_template (ft->context, ft, filename) != 0) {
640

641 642 643 644 645 646 647 648 649 650
		format_template_free (ft);
		return NULL;
	}

	return ft;
}

/**
 * format_template_save_to_file:
 * @ft: a FormatTemplate
651
 *
652
 * Saves template @ft to a filename set with format_template_set_filename
653
 *
654 655 656 657 658 659 660 661 662 663 664 665 666 667
 * Return value: 0 on success, or -1 on error.
 **/
int
format_template_save (FormatTemplate *ft)
{
	g_return_val_if_fail (ft != NULL, -1);

	return gnumeric_xml_write_format_template (ft->context, ft, ft->filename->str);
}

/**
 * format_template_attach_member:
 * @ft: FormatTemplate
 * @member: the new member to attach
668
 *
669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688
 * Attaches @member to template @ft
 **/
void
format_template_attach_member (FormatTemplate *ft, TemplateMember *member)
{
	g_return_if_fail (ft != NULL);
	g_return_if_fail (member != NULL);

	/*
	 * NOTE : Append is slower, but that's not really an issue
	 *        here, because a FormatTemplate will most likely
	 *        not have 'that many' members anyway
	 */
	ft->members = g_slist_append (ft->members, member);
}

/**
 * format_template_detach_member:
 * @ft: FormatTemplate
 * @member: a TemplateMember
689
 *
690 691 692 693 694 695 696 697 698 699 700
 * Detaches @member from template @ft
 **/
void
format_template_detach_member (FormatTemplate *ft, TemplateMember *member)
{
	g_return_if_fail (ft != NULL);
	g_return_if_fail (member != NULL);

	ft->members = g_slist_remove (ft->members, member);
}

701 702 703 704 705 706 707 708 709 710 711 712 713 714 715
/**
 * format_template_compare_name:
 * @ft_a: First FormatTemplate
 * @ft_b: Second FormatTemplate
 *
 **/
gint
format_template_compare_name (gconstpointer a, gconstpointer b)
{
	FormatTemplate *ft_a = (FormatTemplate *) a,
	               *ft_b = (FormatTemplate *) b;

	return strcmp (ft_a->name->str, ft_b->name->str);
}

716 717 718 719 720 721
/******************************************************************************
 * FormatTemplate - Actual implementation (Filtering and calculating)
 ******************************************************************************/

/**
 * format_template_filter_style:
722
 * @ft:
723 724
 * @mstyle:
 * @fill_defaults: If set fill in the gaps with the "default" mstyle.
725
 *
726 727
 * Filter an mstyle and strip and replace certain elements
 * based on what the user wants to apply.
728 729 730
 * Basically you should pass FALSE as @fill_defaults, unless you want to have
 * a completely filled style to be returned. If you set @fill_default to TRUE
 * the returned mstyle might have some of it's elements 'not set'
731
 *
732 733 734
 * Return value: The same mstyle as @mstyle with most likely some modifications
 **/
static MStyle *
735
format_template_filter_style (FormatTemplate *ft, MStyle *mstyle, gboolean fill_defaults)
736 737 738
{
	g_return_val_if_fail (ft != NULL, NULL);
	g_return_val_if_fail (mstyle != NULL, NULL);
739

740
	/*
741 742 743
	 * Don't fill with defaults, this is perfect for when the
	 * mstyles are going to be 'merged' with other mstyles which
	 * have all their elements set
744
	 */
745
	if (!fill_defaults) {
746

747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764
		if (!ft->number) {
			mstyle_unset_element (mstyle, MSTYLE_FORMAT);
		}
		if (!ft->border) {
			mstyle_unset_element (mstyle, MSTYLE_BORDER_TOP);
			mstyle_unset_element (mstyle, MSTYLE_BORDER_BOTTOM);
			mstyle_unset_element (mstyle, MSTYLE_BORDER_LEFT);
			mstyle_unset_element (mstyle, MSTYLE_BORDER_RIGHT);
			mstyle_unset_element (mstyle, MSTYLE_BORDER_DIAGONAL);
			mstyle_unset_element (mstyle, MSTYLE_BORDER_REV_DIAGONAL);
		}
		if (!ft->font) {
			mstyle_unset_element (mstyle, MSTYLE_FONT_NAME);
			mstyle_unset_element (mstyle, MSTYLE_FONT_BOLD);
			mstyle_unset_element (mstyle, MSTYLE_FONT_ITALIC);
			mstyle_unset_element (mstyle, MSTYLE_FONT_UNDERLINE);
			mstyle_unset_element (mstyle, MSTYLE_FONT_STRIKETHROUGH);
			mstyle_unset_element (mstyle, MSTYLE_FONT_SIZE);
765

766 767 768 769 770 771 772 773 774 775 776 777 778
			mstyle_unset_element (mstyle, MSTYLE_COLOR_FORE);
		}
		if (!ft->patterns) {
			mstyle_unset_element (mstyle, MSTYLE_COLOR_BACK);
			mstyle_unset_element (mstyle, MSTYLE_COLOR_PATTERN);
			mstyle_unset_element (mstyle, MSTYLE_PATTERN);
		}
		if (!ft->alignment) {
			mstyle_unset_element (mstyle, MSTYLE_ALIGN_V);
			mstyle_unset_element (mstyle, MSTYLE_ALIGN_H);
		}
	} else {
		MStyle *mstyle_default = mstyle_new_default ();
779

780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
		/*
		 * We fill in the gaps with the default mstyle
		 */

		 if (!ft->number) {
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_FORMAT);
		 }
		 if (!ft->border) {
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_BORDER_TOP);
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_BORDER_BOTTOM);
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_BORDER_LEFT);
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_BORDER_RIGHT);
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_BORDER_DIAGONAL);
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_BORDER_REV_DIAGONAL);
		 }
		 if (!ft->font) {
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_FONT_NAME);
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_FONT_BOLD);
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_FONT_ITALIC);
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_FONT_UNDERLINE);
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_FONT_STRIKETHROUGH);
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_FONT_SIZE);
802

803 804 805 806 807 808 809 810 811 812 813 814 815
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_COLOR_FORE);
		 }
		 if (!ft->patterns) {
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_COLOR_BACK);
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_COLOR_PATTERN);
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_PATTERN);
		 }
		 if (!ft->alignment) {
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_ALIGN_V);
			 mstyle_replace_element (mstyle_default, mstyle, MSTYLE_ALIGN_H);
		 }

		 mstyle_unref (mstyle_default);
816
	}
817

818 819 820 821 822 823 824 825
	return mstyle;
}

/*
 * Callback used for calculating the styles
 */
typedef void (* PCalcCallback) (FormatTemplate *ft, Range *r, MStyle *mstyle, gpointer data);

826 827 828
/**
 * format_template_range_check:
 * @ft: Format template
829 830
 * @r: Target range
 * @display_error: If TRUE will display an error message if @r is not appropriate for @ft.
831
 *
832
 * Check whether range @r is big enough to apply format template @ft to it.
833
 * If this is not the case an error message WILL be displayed if @display_error is TRUE
834
 *
835 836 837
 * Return value: TRUE if @s is big enough, FALSE if not.
 **/
static gboolean
838
format_template_range_check (FormatTemplate *ft, Range const *r, gboolean display_error)
839 840 841 842 843 844 845
{
	GSList *iterator;
	int diff_col_high = -1;
	int diff_row_high = -1;
	gboolean invalid_range_seen = FALSE;

	g_return_val_if_fail (ft != NULL, FALSE);
846

847 848 849
	iterator = ft->members;
	while (iterator) {
		TemplateMember *member = iterator->data;
850
		Range range = format_template_member_get_rect (member, r);
851

852 853 854
		if (!range_valid (&range)) {
			int diff_col = (range.start.col - range.end.col);
			int diff_row = (range.start.row - range.end.row);
855 856 857 858 859 860

			if (diff_col > diff_col_high)
				diff_col_high = diff_col;

			if (diff_row > diff_row_high)
				diff_row_high = diff_row;
861

862 863
			invalid_range_seen = TRUE;
		}
864

865 866 867 868
		iterator = g_slist_next (iterator);
	}

	if (invalid_range_seen && display_error) {
869 870
		int diff_row_high_ft = diff_row_high + range_height (r);
		int diff_col_high_ft = diff_col_high + range_width (r);
871 872 873
		char *errmsg;

		if (diff_col_high > 0 && diff_row_high > 0) {
Jody Goldberg's avatar
Jody Goldberg committed
874
			errmsg = g_strdup_printf (_("The target region is too small.  It should be at least %d rows by %d columns"),
875 876
							  diff_row_high_ft, diff_col_high_ft);
		} else if (diff_col_high > 0) {
Jody Goldberg's avatar
Jody Goldberg committed
877
			errmsg = g_strdup_printf (_("The target region is too small.  It should be at least %d columns wide"),
878 879
							  diff_col_high_ft);
		} else if (diff_row_high > 0) {
Jody Goldberg's avatar
Jody Goldberg committed
880
			errmsg = g_strdup_printf (_("The target region is too small.  It should be at least %d rows high"),
881 882 883 884 885 886 887 888 889 890 891 892
							  diff_row_high_ft);
		} else {
			errmsg = NULL;

			g_warning ("Internal error while verifying ranges! (this should not happen!)");
		}

		if (errmsg) {
			gnumeric_error_system (COMMAND_CONTEXT (ft->context), errmsg);
		}

		g_free (errmsg);
893

894 895 896 897 898 899 900 901 902
		return FALSE;
	} else if (invalid_range_seen) {

		return FALSE;
	}

	return TRUE;
}

903 904 905 906 907 908
/**
 * format_template_calculate:
 * @ft: FormatTemplate
 * @s: Target range
 * @pc: Callback function
 * @cb_data: Data to pass to the callback function
909
 *
910 911 912
 * Calculate all styles for a range of @s. This routine will invoke the callback function
 * and pass all styles and ranges for those styles to the callback function.
 * The callback function should UNREF the mstyle passed!
913
 *
914 915
 **/
static void
916
format_template_calculate (FormatTemplate *ft, Range const *r, PCalcCallback pc, gpointer cb_data)
917 918 919
{
	GSList *iterator;

920
	g_return_if_fail (ft != NULL);
921

922 923 924
	/*
	 * Apply all styles
	 */
925
	iterator = ft->members;
926 927 928
	while (iterator) {
		TemplateMember *member = iterator->data;
		MStyle *mstyle = format_template_member_get_style (member);
929
		Range range = format_template_member_get_rect (member, r);
930

931 932
		if (member->direction == FREQ_DIRECTION_NONE)
			pc (ft, &range, mstyle_copy (mstyle), cb_data);
933

934
		else if (member->direction == FREQ_DIRECTION_HORIZONTAL) {
935
			int col_repeat = member->repeat;
936
			Range hr = range;
937 938 939

			while (col_repeat != 0) {
				pc (ft, &hr, mstyle_copy (mstyle), cb_data);
940

941 942 943 944 945 946
				hr.start.col += member->skip + member->col.size;
				hr.end.col   += member->skip + member->col.size;

				if (member->repeat != -1)
					col_repeat--;
				else {
947
					if (hr.start.row > r->end.row)
948 949 950
						break;
				}

951
				if (hr.start.row > r->end.row - member->edge)
952 953 954 955
					break;
			}
		} else if (member->direction == FREQ_DIRECTION_VERTICAL) {
			int row_repeat = member->repeat;
956
			Range vr = range;
957 958 959

			while (row_repeat != 0) {
				pc (ft, &vr, mstyle_copy (mstyle), cb_data);
960

961 962 963 964 965 966
				vr.start.row += member->skip + member->row.size;
				vr.end.row   += member->skip + member->row.size;

				if (member->repeat != -1)
					row_repeat--;
				else {
967
					if (vr.start.row > r->end.row)
968 969
						break;
				}
970

971
				if (vr.start.row > r->end.row - member->edge)
972 973 974 975
					break;
			}
		}

976

977 978 979 980 981 982 983
		iterator = g_slist_next (iterator);
	}
}

/******************************************************************************
 * FormatTemplate - Application for the hashtable (previews)
 ******************************************************************************/
984

985 986 987 988 989 990 991 992
static void
cb_format_hash_style (FormatTemplate *ft, Range *r, MStyle *mstyle, GHashTable *table)
{
	int row, col;

	/*
	 * Filter out undesired elements
	 */
993
	mstyle = format_template_filter_style (ft, mstyle, TRUE);
994

995 996
	for (row = r->start.row; row <= r->end.row; row++) {
		for (col = r->start.col; col <= r->end.col; col++) {
997

998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
			hash_table_insert (table, row, col, mstyle);
		}
	}

	/*
	 * Unref here, the hashtable will take care of it's own
	 * resources
	 */
	mstyle_unref (mstyle);
}

/**
 * format_template_recalc_hash:
 * @ft: FormatTemplate
1012
 *
1013 1014 1015 1016 1017
 * Refills the hashtable based on new dimensions
 **/
static void
format_template_recalc_hash (FormatTemplate *ft)
{
1018
	Range r;
1019 1020

	g_return_if_fail (ft != NULL);
1021

1022 1023 1024 1025 1026
	ft->table = hash_table_destroy (ft->table);
	ft->table = hash_table_create ();

	g_hash_table_freeze (ft->table);

1027
	r = ft->dimension;
1028 1029 1030 1031 1032

	/*
	 * If the range check fails then the template it simply too *huge*
	 * so we don't display an error dialog.
	 */
1033
	if (!format_template_range_check (ft, &r, FALSE)) {
1034 1035 1036 1037 1038 1039

		g_warning ("Template %s is too large, hash can't be calculated", ft->name->str);
		g_hash_table_thaw (ft->table);

		return;
	}
1040

1041
	format_template_calculate (ft, &r, (PCalcCallback) cb_format_hash_style, ft->table);
1042 1043 1044 1045 1046 1047

	g_hash_table_thaw (ft->table);
}

/**
 * format_template_get_style:
1048 1049 1050 1051
 * @ft:
 * @row:
 * @col:
 *
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
 * Returns the MStyle associated with coordinates row, col.
 * This routine uses the hash to do this.
 * NOTE : You MAY NOT free the result of this operation,
 *        you may also NOT MODIFY the MStyle returned.
 *        (make a copy first)
 *
 * Return value: an MStyle
 **/
MStyle *
format_template_get_style (FormatTemplate *ft, int row, int col)
{
	MStyle *mstyle;
1064

1065 1066 1067 1068 1069 1070 1071 1072
	g_return_val_if_fail (ft != NULL, NULL);
	g_return_val_if_fail (ft->table != NULL, NULL);

	/*
	 * If the hash isn't filled (as result of resizing) or whatever,
	 * then refill it
	 */
	if (ft->invalidate_hash) {
1073

1074 1075 1076
		ft->invalidate_hash = FALSE;
		format_template_recalc_hash (ft);
	}
1077

1078 1079 1080 1081 1082 1083
	mstyle = hash_table_lookup (ft->table, row, col);

	return mstyle;
}


1084

1085 1086 1087
/******************************************************************************
 * FormatTemplate - Application to Sheet
 ******************************************************************************/
1088

1089 1090 1091
static void
cb_format_sheet_style (FormatTemplate *ft, Range *r, MStyle *mstyle, Sheet *sheet)
{
1092
	g_return_if_fail (ft != NULL);
1093 1094
	g_return_if_fail (r != NULL);
	g_return_if_fail (mstyle != NULL);
1095

1096 1097
	mstyle = format_template_filter_style (ft, mstyle, FALSE);

1098 1099 1100 1101
	/*
	 * We need not unref the mstyle, sheet will
	 * take care of the mstyle
	 */
1102
	sheet_apply_style (sheet, r, mstyle);
1103 1104
}

1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
/**
 * format_template_check_valid :
 * @ft :
 * @regions :
 *
 * check to see if the @regions are able to contain the support template @ft.
 */
gboolean
format_template_check_valid (FormatTemplate *ft, GSList *regions)
{
	for (; regions != NULL ; regions = regions->next)
		if (!format_template_range_check (ft, regions->data, TRUE))
			return FALSE;

	return TRUE;
}

1122
/**
1123
 * format_template_apply_to_sheet_regions:
1124 1125
 * @ft: FormatTemplate
 * @sheet: the Target sheet
1126 1127
 * @regions: Region list
 *
1128 1129 1130
 * Apply the template to all selected regions in @sheet.
 **/
void
1131
format_template_apply_to_sheet_regions (FormatTemplate *ft, Sheet *sheet, GSList *regions)
1132
{
1133 1134
	for (; regions != NULL ; regions = regions->next)
		format_template_calculate (ft, regions->data,
1135
			(PCalcCallback) cb_format_sheet_style, sheet);
1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148
}

/******************************************************************************
 * Getters and setters for FormatTemplate
 *
 * NOTE : The caller is always responsible for freeing
 *        return variables and variables passed to the functions.
 */

char *
format_template_get_filename (FormatTemplate *ft)
{
	g_return_val_if_fail (ft != NULL, NULL);
1149

1150 1151 1152 1153 1154 1155 1156 1157 1158 1159
	if (ft->filename)
		return g_strdup (ft->filename->str);
	else
		return NULL;
}

char *
format_template_get_name (FormatTemplate *ft)
{
	g_return_val_if_fail (ft != NULL, NULL);
1160

1161 1162 1163 1164 1165 1166 1167 1168 1169 1170
	if (ft->name)
		return g_strdup (ft->name->str);
	else
		return NULL;
}

char *
format_template_get_author (FormatTemplate *ft)
{
	g_return_val_if_fail (ft != NULL, NULL);
1171

1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
	if (ft->author)
		return g_strdup (ft->author->str);
	else
		return NULL;
}

char *
format_template_get_description (FormatTemplate *ft)
{
	g_return_val_if_fail (ft != NULL, NULL);
1182

1183 1184 1185 1186 1187 1188
	if (ft->description)
		return g_strdup (ft->description->str);
	else
		return NULL;
}

1189 1190
GSList *
format_template_get_members (FormatTemplate *ft)
1191 1192
{
	g_return_val_if_fail (ft != NULL, NULL);
1193

1194
	return ft->members;
1195 1196
}

1197 1198
FormatTemplateCategory *
format_template_get_category (FormatTemplate *ft)
1199 1200 1201
{
	g_return_val_if_fail (ft != NULL, NULL);

1202
	return ft->category;
1203 1204 1205 1206 1207 1208 1209
}

void
format_template_set_filename (FormatTemplate *ft, const char *filename)
{
	g_return_if_fail (ft != NULL);
	g_return_if_fail (filename != NULL);
1210

1211 1212
	if (ft->filename)
		g_string_free (ft->filename, TRUE);
1213

1214 1215 1216 1217 1218 1219 1220 1221
	ft->filename = g_string_new (filename);
}

void
format_template_set_name (FormatTemplate *ft, const char *name)
{
	g_return_if_fail (ft != NULL);
	g_return_if_fail (name != NULL);
1222

1223 1224
	if (ft->name)
		g_string_free (ft->name, TRUE);
1225

1226 1227 1228 1229 1230 1231 1232 1233
	ft->name = g_string_new (name);
}

void
format_template_set_author (FormatTemplate *ft, const char *author)
{
	g_return_if_fail (ft != NULL);
	g_return_if_fail (author != NULL);
1234

1235 1236
	if (ft->author)
		g_string_free (ft->author, TRUE);
1237

1238 1239 1240 1241 1242 1243 1244 1245
	ft->author = g_string_new (author);
}

void
format_template_set_description (FormatTemplate *ft, const char *description)
{
	g_return_if_fail (ft != NULL);
	g_return_if_fail (description != NULL);
1246

1247 1248
	if (ft->description)
		g_string_free (ft->description, TRUE);
1249

1250 1251 1252 1253
	ft->description = g_string_new (description);
}

void
1254
format_template_set_category (FormatTemplate *ft, FormatTemplateCategory *category)
1255 1256 1257
{
	g_return_if_fail (ft != NULL);
	g_return_if_fail (category != NULL);
1258

1259
	ft->category = category;
1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270
}


/**
 * format_template_set_filter:
 * @ft: a FormatTemplate
 * @number: Apply number settings?
 * @border: Apply borders?
 * @font: Apply font?
 * @patterns: Apply patterns?
 * @alignment: Apply alignment?
1271
 *
1272 1273 1274 1275 1276 1277 1278 1279
 * Sets the types of elements to filter out of mstyles
 * returned by format_template_calculate_style and applied
 * when using format_template_apply_to_sheet_selection.
 * Every gboolean which is FALSE will be filtered out!
 **/
void
format_template_set_filter (FormatTemplate *ft,
			    gboolean number, gboolean border,
1280
			    gboolean font,   gboolean patterns,
1281 1282 1283
			    gboolean alignment)
{
	g_return_if_fail (ft != NULL);
1284

1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295
	ft->number    = number;
	ft->border    = border;
	ft->font      = font;
	ft->patterns  = patterns;
	ft->alignment = alignment;

	ft->invalidate_hash = TRUE;
}

/**
 * format_template_set_size:
1296 1297 1298 1299 1300 1301
 * @ft:
 * @x1:
 * @y1:
 * @x2:
 * @y2:
 *
1302 1303 1304 1305 1306 1307 1308 1309 1310
 * This will set the size of the application area.
 * BIG FAT NOTE : You will need to pass the COORDINATES of top, left and
 * bottom, right. Not the dimensions.
 * So if you want to calculate for an area of 5 x 5, you pass :
 * x1 = 0, y1 = 0, x2 = 4, y2 = 4;
 **/
void
format_template_set_size (FormatTemplate *ft, int x1, int y1, int x2, int y2)
{
1311 1312 1313 1314
	ft->dimension.start.col = x1;
	ft->dimension.start.row = y1;
	ft->dimension.end.col = x2;
	ft->dimension.end.row = y2;
1315 1316 1317

	ft->invalidate_hash = TRUE;
}