xml-io.c 35.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * xml-io.c: save/read gnumeric Sheets using an XML encoding.
 *
 * Daniel Veillard <Daniel.Veillard@w3.org>
 *
 * $Id$
 */

#include <config.h>
#include <stdio.h>
#include <gnome.h>
#include "gnumeric.h"
13 14
#include "gnome-xml/tree.h"
#include "gnome-xml/parser.h"
15
#include "color.h"
16
#include "sheet-object.h"
17 18 19 20 21 22
#include "xml-io.h"

/*
 * A parsing context.
 */
typedef struct parseXmlContext {
23 24 25 26 27 28 29
	xmlDocPtr doc;		/* Xml document */
	xmlNsPtr ns;		/* Main name space */
	xmlNodePtr parent;	/* used only for g_hash_table_foreach callbacks */
	GHashTable *nameTable;	/* to reproduce multiple refs with HREFs */
	int fontIdx;		/* for Font refs names ... */
	Sheet *sheet;		/* the associated sheet */
	Workbook *wb;		/* the associated sheet */
30 31
} parseXmlContext, *parseXmlContextPtr;

Arturo Espinosa's avatar
Arturo Espinosa committed
32 33 34 35
static Sheet      *readXmlSheet     (parseXmlContextPtr ctxt, xmlNodePtr tree);
static xmlNodePtr  writeXmlSheet    (parseXmlContextPtr ctxt, Sheet *sheet);
static Workbook   *readXmlWorkbook  (parseXmlContextPtr ctxt, xmlNodePtr tree);
static xmlNodePtr  writeXmlWorkbook (parseXmlContextPtr ctxt, Workbook *wb);
36
/* static guint       ptrHash          (gconstpointer a);
37 38 39
   static gint        ptrCompare       (gconstpointer a, gconstpointer b); */
static void nameFree                (gpointer key, gpointer value,
				     gpointer user_data);
40

41 42 43
/*
 * Internal stuff: xml helper functions.
 */
44 45 46 47 48

/*
 * Get a value for a node either carried as an attibute or as
 * the content of a child.
 */
49
static char *
50 51
xmlGetValue (xmlNodePtr node, const char *name)
{
52
	char *ret;
53 54
	xmlNodePtr child;

55
	ret = (char *) xmlGetProp (node, name);
56
	if (ret != NULL)
57
		return strdup (ret);
58 59 60
	child = node->childs;

	while (child != NULL) {
61 62 63 64 65 66 67 68
		if (!strcmp (child->name, name)) {
		        /*
			 * !!! Inefficient, but ...
			 */
			ret = xmlNodeGetContent(child);
			if (ret != NULL)
			    return (ret);
		}
69 70 71 72
		child = child->next;
	}

	return NULL;
73 74
}

75 76 77 78
/*
 * Get a String value for a node either carried as an attibute or as
 * the content of a child.
 */
79 80 81
String *
xmlGetStringValue (xmlNodePtr node, const char *name)
{
82
	char *val;
83 84
	String *ret;

85 86 87 88 89
	val = xmlGetValue(node, name);
	if (val == NULL) return(NULL);
        ret = string_get(val);
	free(val);
	return(ret);
90 91
}

92 93 94 95
/*
 * Get an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
96 97 98
static int
xmlGetIntValue (xmlNodePtr node, const char *name, int *val)
{
99
	char *ret;
100
	int i;
101
	int res;
102

103 104 105 106 107 108 109
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
	res = sscanf (ret, "%d", &i);
	free(ret);
	
	if (res == 1) {
	        *val = i;
110 111 112
		return 1;
	}
	return 0;
113 114 115 116 117 118
}

/*
 * Get a float value for a node either carried as an attibute or as
 * the content of a child.
 */
119 120 121
int
xmlGetFloatValue (xmlNodePtr node, const char *name, float *val)
{
122 123
	int res;
	char *ret;
124 125 126
	xmlNodePtr child;
	float f;

127 128 129 130 131 132 133
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
	res = sscanf (ret, "%f", &f);
	free(ret);
	
	if (res == 1) {
	        *val = f;
134 135 136
		return 1;
	}
	return 0;
137 138 139 140 141 142
}

/*
 * Get a double value for a node either carried as an attibute or as
 * the content of a child.
 */
143 144 145
static int
xmlGetDoubleValue (xmlNodePtr node, const char *name, double *val)
{
146 147
	int res;
	char *ret;
148 149 150
	xmlNodePtr child;
	float f;

151 152 153 154 155 156 157
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
	res = sscanf (ret, "%f", &f);
	free(ret);
	
	if (res == 1) {
	        *val = f;
158 159 160
		return 1;
	}
	return 0;
161 162
}

163 164 165 166
/*
 * Get a set of coodinates for a node, carried as the content of a child.
 */

167 168 169 170
int
xmlGetCoordinate (xmlNodePtr node, const char *name,
		  double *x, double *y)
{
171 172
	int res;
	char *ret;
173 174 175
	xmlNodePtr child;
	float X, Y;

176 177 178 179 180 181 182 183 184
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
	res = sscanf (ret, "(%f %f)", &X, &Y);
	free(ret);
	
	if (res == 2) {
		*x = X;
		*y = Y;
		return 1;
185 186
	}
	return 0;
187 188 189 190 191 192
}

/*
 * Get a pair of coodinates for a node, carried as the content of a child.
 */

193 194 195 196
static int
xmlGetCoordinates (xmlNodePtr node, const char *name,
		   double *x1, double *y1, double *x2, double *y2)
{
197 198
	int res;
	char *ret;
199 200 201
	xmlNodePtr child;
	float X1, Y1, X2, Y2;

202 203 204 205 206 207 208 209 210 211 212
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
	res = sscanf (ret, "(%f %f)(%f %f)", &X1, &Y1, &X2, &Y2);
	free(ret);
	
	if (res == 2) {
		*x1 = X1;
		*y1 = Y1;
		*x2 = X2;
		*y2 = Y2;
		return 1;
213 214
	}
	return 0;
215 216 217 218 219 220
}

/*
 * Get a GnomeCanvasPoints for a node, carried as the content of a child.
 */

221 222 223
GnomeCanvasPoints *
xmlGetGnomeCanvasPoints (xmlNodePtr node, const char *name)
{
224
	char *val;
225 226 227 228 229 230 231
	GnomeCanvasPoints *ret = NULL;
	int res;
	const char *ptr;
	xmlNodePtr child;
	int index = 0, i;
	float coord[20];	/* TODO: must be dynamic !!!! */

232 233 234 235 236 237 238
	val = xmlGetValue (node, name);
	if (ret == NULL) return(NULL);
	ptr = val;
	do {
		while ((*ptr) && (*ptr != '('))
			ptr++;
		if (*ptr == 0)
239
			break;
240 241 242 243 244 245 246 247 248 249 250 251 252 253
		res = sscanf (ptr, "(%f %f)", &coord[index], &coord[index + 1]);
		if (res != 2)
			break;
		index += 2;
		ptr++;
	} while (res > 0);
	free(val);

	if (index >= 2)
		ret = gnome_canvas_points_new (index / 2);
	if (ret == NULL)
		return NULL;
	for (i = 0; i < index; i++)
		ret->coords[i] = coord[i];
254
	return ret;
255 256 257 258 259 260
}

/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
static void
xmlSetGnomeCanvasPoints (xmlNodePtr node, const char *name,
			 GnomeCanvasPoints *val)
{
	xmlNodePtr child;
	char *str, *base;
	int i;

	if (val == NULL)
		return;
	if ((val->num_points < 0) || (val->num_points > 5000))
		return;
	base = str = malloc (val->num_points * 30 * sizeof (char));
	if (str == NULL)
		return;
	for (i = 0; i < val->num_points; i++){
		str += sprintf (str, "(%f %f)", val->coords[2 * i],
				val->coords[2 * i + 1]);
	}

	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, base);
			free (base);
			return;
		}
		child = child->next;
	}
290
	xmlNewChild (node, NULL, name, xmlEncodeEntities(node->doc, base));
291
	free (base);
292 293 294
}


295 296 297 298
/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317
static void xmlSetValue (xmlNodePtr node, const char *name, const char *val)
{
	const char *ret;
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
		xmlSetProp (node, name, val);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, val);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, val);
318 319
}

320 321 322 323
/*
 * Set a String value for a node either carried as an attibute or as
 * the content of a child.
 */
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
static void xmlSetStringValue (xmlNodePtr node, const char *name, String *val)
{
	const char *ret;
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
		xmlSetProp (node, name, val->str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, val->str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, val->str);
343 344
}

345 346 347 348
/*
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
static void
xmlSetIntValue (xmlNodePtr node, const char *name, int val)
{
	const char *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%d", val);
	ret = xmlGetProp (node, name);
	if (ret != NULL){
		xmlSetProp (node, name, str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
371 372 373 374 375 376
}

/*
 * Set a float value for a node either carried as an attibute or as
 * the content of a child.
 */
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
void xmlSetFloatValue (xmlNodePtr node, const char *name, float val)
{
	const char *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%f", val);
	ret = xmlGetProp (node, name);
	if (ret != NULL){
		xmlSetProp (node, name, str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
398 399 400 401 402 403
}

/*
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
void xmlSetDoubleValue (xmlNodePtr node, const char *name, double val)
{
	const char *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%f", (float) val);
	ret = xmlGetProp (node, name);
	if (ret != NULL){
		xmlSetProp (node, name, str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
425 426 427 428 429
}

/*
 * Search a child by name, if needed go down the tree to find it. 
 */
430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
static xmlNodePtr xmlSearchChild (xmlNodePtr node, const char *name)
{
	xmlNodePtr ret;
	xmlNodePtr child;

	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name))
			return child;
		child = child->next;
	}
	child = node->childs;
	while (child != NULL){
		ret = xmlSearchChild (child, name);
		if (ret != NULL)
			return ret;
		child = child->next;
	}
	return NULL;
449 450 451 452 453 454 455 456 457 458
}

/*
 * Get a color value for a node either carried as an attibute or as
 * the content of a child.
 *
 * TODO PBM: at parse time one doesn't have yet a widget, so we have
 *           to retrieve the default colormap, but this may be a bad
 *           option ...
 */
459 460 461 462
static int
xmlGetColorValue (xmlNodePtr node, const char *name,
			     StyleColor **color)
{
463
	char *ret;
464 465 466
	xmlNodePtr child;
	int red, green, blue;

467 468 469
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
	if (sscanf (ret, "%X:%X:%X", &red, &green, &blue) == 3){
470
		*color = style_color_new (red, green, blue);
471
		free(ret);
472 473 474
		return 1;
	}
	return 0;
475 476 477 478 479 480
}

/*
 * Set a color value for a node either carried as an attibute or as
 * the content of a child.
 */
481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502
static void xmlSetColorValue (xmlNodePtr node, const char *name,
			      StyleColor *val)
{
	const char *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%X:%X:%X", val->color.red, val->color.green, val->color.blue);
	ret = xmlGetProp (node, name);
	if (ret != NULL){
		xmlSetProp (node, name, str);
		return;
	}
	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
503
}
504 505 506 507 508

/**
 **
 ** Public functions : high level read and write.
 **
509
 **/
510 511 512 513 514 515 516

/*
 * Open an XML file and read a Sheet
 * One parse the XML file, getting a tree, then analyze the tree to build
 * the actual in-memory structure.
 */

517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
Sheet *gnumericReadXmlSheet (const char *filename)
{
	Sheet *sheet;
	xmlDocPtr res;
	xmlNsPtr gmr;
	parseXmlContext ctxt;

	g_return_val_if_fail (filename != NULL, NULL);

	/*
	 * Load the file into an XML tree.
	 */
	res = xmlParseFile (filename);
	if (res == NULL)
		return NULL;
	if (res->root == NULL){
		fprintf (stderr, "gnumericReadXmlSheet %s: tree is empty\n", filename);
		xmlFreeDoc (res);
		return NULL;
	}
	/*
	 * Do a bit of checking, get the namespaces, and check the top elem.
	 */
	gmr = xmlSearchNsByHref (res, res->root, "http://www.gnome.org/gnumeric/");
	if (strcmp (res->root->name, "Sheet") || (gmr == NULL)){
		fprintf (stderr, "gnumericReadXmlSheet %s: not a Sheet file\n",
			 filename);
		xmlFreeDoc (res);
		return NULL;
	}
	ctxt.doc = res;
	ctxt.ns = gmr;
	ctxt.nameTable = g_hash_table_new (g_str_hash, g_str_equal);
	ctxt.fontIdx = 1;
	sheet = readXmlSheet (&ctxt, res->root);
	g_hash_table_foreach (ctxt.nameTable, nameFree, NULL);
	g_hash_table_destroy (ctxt.nameTable);

	xmlFreeDoc (res);
556 557

	sheet->modified = FALSE;
558
	return sheet;
559 560 561 562 563 564 565 566
}

/*
 * Save a Sheet in an XML file
 * One build an in-memory XML tree and save it to a file.
 * returns 0 in case of success, -1 otherwise.
 */

567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600
int
gnumericWriteXmlSheet (Sheet * sheet, const char *filename)
{
	xmlDocPtr xml;
	xmlNsPtr gmr;
	parseXmlContext ctxt;
	int ret;

	g_return_val_if_fail (sheet != NULL, -1);
	g_return_val_if_fail (IS_SHEET (sheet), -1);
	g_return_val_if_fail (filename != NULL, -1);

	/*
	 * Create the tree
	 */
	xml = xmlNewDoc ("1.0");
	if (xml == NULL){
		return -1;
	}
	gmr = xmlNewGlobalNs (xml, "http://www.gnome.org/gnumeric/", "gmr");
	ctxt.doc = xml;
	ctxt.ns = gmr;
	ctxt.nameTable = g_hash_table_new (g_str_hash, g_str_equal);
	ctxt.fontIdx = 1;
	xml->root = writeXmlSheet (&ctxt, sheet);
	g_hash_table_foreach (ctxt.nameTable, nameFree, NULL);
	g_hash_table_destroy (ctxt.nameTable);

	/*
	 * Dump it.
	 */
	xmlSetDocCompressMode (xml, 9);
	ret = xmlSaveFile (filename, xml);
	xmlFreeDoc (xml);
601
	sheet->modified = FALSE;
602 603 604
	if (ret < 0)
		return -1;
	return 0;
605 606 607 608 609 610 611 612
}

/*
 * Open an XML file and read a Workbook
 * One parse the XML file, getting a tree, then analyze the tree to build
 * the actual in-memory structure.
 */

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
Workbook *gnumericReadXmlWorkbook (const char *filename)
{
	Workbook *wb;
	xmlDocPtr res;
	xmlNsPtr gmr;
	parseXmlContext ctxt;

	g_return_val_if_fail (filename != NULL, NULL);

	/*
	 * Load the file into an XML tree.
	 */
	res = xmlParseFile (filename);
	if (res == NULL)
		return NULL;
	if (res->root == NULL){
		fprintf (stderr, "gnumericReadXmlWorkbook %s: tree is empty\n", filename);
		xmlFreeDoc (res);
		return NULL;
	}
	/*
	 * Do a bit of checking, get the namespaces, and chech the top elem.
	 */
	gmr = xmlSearchNsByHref (res, res->root, "http://www.gnome.org/gnumeric/");
	if (strcmp (res->root->name, "Workbook") || (gmr == NULL)){
		fprintf (stderr, "gnumericReadXmlWorkbook %s: not an Workbook file\n",
			 filename);
		xmlFreeDoc (res);
		return NULL;
	}
	ctxt.doc = res;
	ctxt.ns = gmr;
	ctxt.nameTable = g_hash_table_new (g_str_hash, g_str_equal);
	ctxt.fontIdx = 1;
	wb = readXmlWorkbook (&ctxt, res->root);
648
	workbook_set_filename (wb, (char *) filename);
649 650 651 652 653 654
	workbook_recalc_all (wb);
	g_hash_table_foreach (ctxt.nameTable, nameFree, NULL);
	g_hash_table_destroy (ctxt.nameTable);

	xmlFreeDoc (res);
	return wb;
655 656 657 658 659 660 661 662
}

/*
 * Save a Workbook in an XML file
 * One build an in-memory XML tree and save it to a file.
 * returns 0 in case of success, -1 otherwise.
 */

663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698
int
gnumericWriteXmlWorkbook (Workbook *wb, const char *filename)
{
	xmlDocPtr xml;
	xmlNsPtr gmr;
	parseXmlContext ctxt;
	int ret;

	g_return_val_if_fail (wb != NULL, -1);
	g_return_val_if_fail (filename != NULL, -1);

	/*
	 * Create the tree
	 */
	xml = xmlNewDoc ("1.0");
	if (xml == NULL){
		return -1;
	}
	gmr = xmlNewGlobalNs (xml, "http://www.gnome.org/gnumeric/", "gmr");
	ctxt.doc = xml;
	ctxt.ns = gmr;
	ctxt.nameTable = g_hash_table_new (g_str_hash, g_str_equal);
	ctxt.fontIdx = 1;
	xml->root = writeXmlWorkbook (&ctxt, wb);
	g_hash_table_foreach (ctxt.nameTable, nameFree, NULL);
	g_hash_table_destroy (ctxt.nameTable);

	/*
	 * Dump it.
	 */
	xmlSetDocCompressMode (xml, 9);
	ret = xmlSaveFile (filename, xml);
	xmlFreeDoc (xml);
	if (ret < 0)
		return -1;
	return 0;
699 700 701 702 703 704
}

/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
705
 **/
706 707 708 709

/*
 * Free a name when cleaning up the name Hash table.
 */
710 711
static void
nameFree (gpointer key, gpointer value, gpointer user_data)
712
{
713
	g_free (key);
714 715
}

716 717
static int
style_is_default_fore (StyleColor *color)
718
{
719 720
	if (!color)
		return TRUE;
721

722 723 724 725
	if (color->color.red == 0 && color->color.green == 0 && color->color.blue == 0)
		return TRUE;
	else
		return FALSE;
726 727
}

728 729
static int
style_is_default_back (StyleColor *color)
730
{
731 732
	if (!color)
		return TRUE;
733

734 735 736 737
	if (color->color.red == 0xffff && color->color.green == 0xffff && color->color.blue == 0xffff)
		return TRUE;
	else
		return FALSE;
738 739 740 741 742
}

/*
 * Create an XML subtree of doc equivalent to the given StyleBorder.
 */
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
static char *BorderTypes[8] =
{
	"none",
	"solid",
	"unknown",
	"unknown",
	"unknown",
	"unknown",
	"unknown",
	"unknown"
};

static xmlNodePtr
writeXmlStyleBorder (parseXmlContextPtr ctxt, StyleBorder *border)
{
	xmlNodePtr cur;
	xmlNodePtr side;

	if ((border->left == BORDER_NONE) && (border->right == BORDER_NONE) &&
	 (border->left == BORDER_NONE) && (border->right == BORDER_NONE))
		return NULL;
764
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
	if (border->left != BORDER_NONE){
		side = xmlNewChild (cur, ctxt->ns, "Left", BorderTypes[border->left]);
		xmlSetColorValue (side, "Color", border->left_color);
	}
	if (border->right != BORDER_NONE){
		side = xmlNewChild (cur, ctxt->ns, "Right", BorderTypes[border->right]);
		xmlSetColorValue (side, "Color", border->right_color);
	}
	if (border->top != BORDER_NONE){
		side = xmlNewChild (cur, ctxt->ns, "Top", BorderTypes[border->top]);
		xmlSetColorValue (side, "Color", border->top_color);
	}
	if (border->bottom != BORDER_NONE){
		side = xmlNewChild (cur, ctxt->ns, "Bottom", BorderTypes[border->bottom]);
		xmlSetColorValue (side, "Color", border->bottom_color);
	}
	return cur;
782 783 784 785 786
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
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
static StyleBorder *
readXmlStyleBorder (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	StyleBorder *ret;
	StyleBorderType left = BORDER_NONE;
	StyleBorderType right = BORDER_NONE;
	StyleBorderType top = BORDER_NONE;
	StyleBorderType bottom = BORDER_NONE;
	StyleColor *left_color = NULL;
	StyleColor *right_color = NULL;
	StyleColor *top_color = NULL;
	StyleColor *bottom_color = NULL;
	xmlNodePtr side;

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
			 "readXmlStyleBorder: invalid element type %s, 'StyleBorder' expected`\n",
			 tree->name);
	}
	if ((side = xmlSearchChild (tree, "Left")) != NULL){
		left = BORDER_SOLID;
		xmlGetColorValue (side, "Color", &left_color);
	}
	if ((side = xmlSearchChild (tree, "Right")) != NULL){
		right = BORDER_SOLID;
		xmlGetColorValue (side, "Color", &right_color);
	}
	if ((side = xmlSearchChild (tree, "Top")) != NULL){
		top = BORDER_SOLID;
		xmlGetColorValue (side, "Color", &top_color);
	}
	if ((side = xmlSearchChild (tree, "Bottom")) != NULL){
		bottom = BORDER_SOLID;
		xmlGetColorValue (side, "Color", &bottom_color);
	}
	ret = style_border_new (left, right, top, bottom,
		       left_color, right_color, top_color, bottom_color);
	return NULL;
825 826 827 828 829
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
830 831 832 833 834 835 836 837 838 839 840 841
static xmlNodePtr
writeXmlStyle (parseXmlContextPtr ctxt, Style * style)
{
	xmlNodePtr cur, child;
	char str[50];
	char *name;

	if ((style->halign == 0) && (style->valign == 0) &&
	    (style->orientation == 0) && (style->format == NULL) &&
	    (style->font == NULL) && (style->border == NULL))
		return NULL;

842
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865
	xmlSetIntValue (cur, "HAlign", style->halign);
	xmlSetIntValue (cur, "VAlign", style->valign);
	xmlSetIntValue (cur, "Fit", style->fit_in_cell);
	xmlSetIntValue (cur, "Orient", style->orientation);
	xmlSetIntValue (cur, "Shade", style->pattern);

	if (!style_is_default_fore (style->fore_color))
		xmlSetColorValue (cur, "Fore", style->fore_color);

	if (!style_is_default_back (style->back_color))
		xmlSetColorValue (cur, "Back", style->back_color);

	if (style->format != NULL){
		xmlSetValue (cur, "Format", style->format->format);
	}

	if (style->font != NULL){
		if ((name = (char *)
		     g_hash_table_lookup (ctxt->nameTable, style->font)) != NULL){
			child = xmlNewChild (cur, ctxt->ns, "Font", NULL);
			sprintf (str, "#%s", name);
			xmlNewProp (child, "HREF", str);
		} else {
866 867 868
			child = xmlNewChild (cur, ctxt->ns, "Font", 
			           xmlEncodeEntities(ctxt->doc,
				                     style->font->font_name));
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
			xmlSetIntValue (child, "Unit", style->font->units);
			sprintf (str, "FontDef%d", ctxt->fontIdx++);
			xmlNewProp (child, "NAME", str);
			g_hash_table_insert (ctxt->nameTable, g_strdup (str), style->font);
		}

	}

	if (style->border != NULL){
		child = writeXmlStyleBorder (ctxt, style->border);
		if (child)
			xmlAddChild (cur, child);
	}

	return cur;
884 885 886 887 888
}

/*
 * Create a Style equivalent to the XML subtree of doc.
 */
889 890 891 892 893 894 895 896 897 898
static Style *
readXmlStyle (parseXmlContextPtr ctxt, xmlNodePtr tree, Style * ret)
{
	xmlNodePtr child;
	const char *prop;
	int val;
	StyleColor *c;

	if (strcmp (tree->name, "Style")){
		fprintf (stderr,
899
			 "readXmlStyle: invalid element type %s, 'Style' expected\n",
900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
			 tree->name);
	}
	if (ret == NULL)
		ret = style_new_empty ();
	
	if (ret == NULL)
		return NULL;

	if (xmlGetIntValue (tree, "HAlign", &val)){
		ret->halign = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
	if (xmlGetIntValue (tree, "Fit", &val)){
		ret->fit_in_cell = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
	if (xmlGetIntValue (tree, "VAlign", &val)){
		ret->valign = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
	if (xmlGetIntValue (tree, "Orient", &val)){
		ret->orientation = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
	if (xmlGetIntValue (tree, "Shade", &val)){
		ret->pattern = val;
		ret->valid_flags |= STYLE_PATTERN;
	}
	if (xmlGetColorValue (tree, "Fore", &c)){
		ret->fore_color = c;
		ret->valid_flags |= STYLE_FORE_COLOR;
	}
	if (xmlGetColorValue (tree, "Back", &c)){
		ret->back_color = c;
		ret->valid_flags |= STYLE_BACK_COLOR;
	}
	prop = xmlGetValue (tree, "Format");
	if (prop != NULL){
		if (ret->format == NULL){
			ret->format = style_format_new ((char *) prop);
			ret->valid_flags |= STYLE_FORMAT;
941
		}
942
	}
943

944 945 946 947 948 949 950 951
	child = tree->childs;
	while (child != NULL){
		if (!strcmp (child->name, "Font")){
			const char *v;

			v = xmlGetValue (child, "NAME");
			if (v){
				int units = 14;
952
				char *font;
953 954

				xmlGetIntValue (child, "Unit", &units);
955 956 957 958 959
				font = xmlNodeGetContent(child);
				if (font != NULL) {
					ret->font = style_font_new (font, units);
					free(font);
				}
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989
				if (ret->font){
					g_hash_table_insert (ctxt->nameTable, g_strdup (v), ret->font);
					ret->valid_flags |= STYLE_FONT;
				}
			} else {
				StyleFont *font;

				v = xmlGetValue (child, "HREF");
				if (v){
					font = g_hash_table_lookup (ctxt->nameTable, v + 1);
					if (font){
						ret->font = font;
						style_font_ref (font);
						ret->valid_flags |= STYLE_FONT;
					}
				}
			}
		} else if (!strcmp (child->name, "StyleBorder")){
			StyleBorder *sb;

			sb = readXmlStyleBorder (ctxt, child);
		} else {
			fprintf (stderr, "readXmlStyle: unknown type '%s'\n",
				 child->name);
		}
		child = child->next;
	}

	/* Now add defaults to any style that was not loaded */
	return ret;
990 991
}

992 993 994
/*
 * Create an XML subtree of doc equivalent to the given StyleRegion.
 */
995 996 997 998 999 1000
/* static !!! */
xmlNodePtr
writeXmlStyleRegion (parseXmlContextPtr ctxt, StyleRegion *region)
{
	xmlNodePtr cur, child;

1001
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleRegion", NULL);
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
	xmlSetIntValue (cur, "startCol", region->range.start_col);
	xmlSetIntValue (cur, "endCol", region->range.end_col);
	xmlSetIntValue (cur, "startRow", region->range.start_row);
	xmlSetIntValue (cur, "endRow", region->range.end_row);

	if (region->style != NULL){
		child = writeXmlStyle (ctxt, region->style);
		if (child)
			xmlAddChild (cur, child);
	}
	return cur;
1013 1014 1015 1016 1017
}

/*
 * Create a StyleRegion equivalent to the XML subtree of doc.
 */
1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040
static void
readXmlStyleRegion (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	xmlNodePtr child;
	Style *style = NULL;
	int start_col = 0, start_row = 0, end_col = 0, end_row = 0;

	if (strcmp (tree->name, "StyleRegion")){
		fprintf (stderr,
			 "readXmlStyleRegion: invalid element type %s, 'StyleRegion' expected`\n",
			 tree->name);
		return;
	}
	xmlGetIntValue (tree, "startCol", &start_col);
	xmlGetIntValue (tree, "startRow", &start_row);
	xmlGetIntValue (tree, "endCol", &end_col);
	xmlGetIntValue (tree, "endRow", &end_row);
	child = tree->childs;
	if (child != NULL)
		style = readXmlStyle (ctxt, child, NULL);
	if (style != NULL)
		sheet_style_attach (ctxt->sheet, start_col, start_row, end_col,
				    end_row, style);
1041 1042 1043

}

1044 1045 1046
/*
 * Create an XML subtree of doc equivalent to the given ColRowInfo.
 */
1047 1048 1049 1050 1051 1052
static xmlNodePtr
writeXmlColRowInfo (parseXmlContextPtr ctxt, ColRowInfo *info, int col)
{
	xmlNodePtr cur;

	if (col)
1053
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "ColInfo", NULL);
1054
	else
1055
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "RowInfo", NULL);
1056 1057 1058 1059 1060 1061 1062 1063

	xmlSetIntValue (cur, "No", info->pos);
	xmlSetIntValue (cur, "Unit", info->units);
	xmlSetIntValue (cur, "MarginA", info->margin_a);
	xmlSetIntValue (cur, "MarginB", info->margin_b);
	xmlSetIntValue (cur, "HardSize", info->hard_size);

	return cur;
1064 1065 1066 1067 1068
}

/*
 * Create a ColRowInfo equivalent to the XML subtree of doc.
 */
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
static ColRowInfo *
readXmlColRowInfo (parseXmlContextPtr ctxt,
		   xmlNodePtr tree, ColRowInfo *ret)
{
	int col = 0;
	int val;

	if (!strcmp (tree->name, "ColInfo")){
		col = 1;
	} else if (!strcmp (tree->name, "RowInfo")){
		col = 0;
	} else {
		fprintf (stderr,
			 "readXmlColRowInfo: invalid element type %s, 'ColInfo/RowInfo' expected`\n",
			 tree->name);
		return NULL;
	}
	if (ret == NULL){
		if (col)
			ret = sheet_col_new (ctxt->sheet);
		else
			ret = sheet_row_new (ctxt->sheet);
	}
	if (ret == NULL)
		return NULL;
1094

1095 1096 1097 1098 1099 1100
	xmlGetIntValue (tree, "No", &ret->pos);
	xmlGetIntValue (tree, "Unit", &ret->units);
	xmlGetIntValue (tree, "MarginA", &ret->margin_a);
	xmlGetIntValue (tree, "MarginB", &ret->margin_b);
	if (xmlGetIntValue (tree, "HardSize", &val))
		ret->hard_size = val;
1101

1102
	return ret;
1103 1104
}

1105 1106 1107
/*
 * Create an XML subtree of doc equivalent to the given Object.
 */
1108 1109 1110 1111 1112 1113 1114 1115 1116
static xmlNodePtr
writeXmlObject (parseXmlContextPtr ctxt, SheetObject *object)
{
	xmlNodePtr cur = NULL;

	switch (object->type){
	case SHEET_OBJECT_RECTANGLE:{
			SheetFilledObject *fo = (SheetFilledObject *) object;

1117
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Rectangle", NULL);
1118 1119 1120 1121 1122 1123 1124 1125
			if (fo->fill_color != NULL)
				xmlSetStringValue (cur, "FillColor", fo->fill_color);
			xmlSetIntValue (cur, "Pattern", fo->pattern);
			break;
		}
	case SHEET_OBJECT_ELLIPSE:{
			SheetFilledObject *fo = (SheetFilledObject *) object;

1126
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Ellipse", NULL);
1127 1128 1129 1130 1131
			if (fo->fill_color != NULL)
				xmlSetStringValue (cur, "FillColor", fo->fill_color);
			xmlSetIntValue (cur, "Pattern", fo->pattern);
			break;
		}
1132
	case SHEET_OBJECT_ARROW:
1133
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Arrow", NULL);
1134
		break;
1135
	case SHEET_OBJECT_LINE:
1136
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Line", NULL);
1137 1138 1139 1140 1141 1142 1143 1144 1145
		break;
	}
	if (cur == NULL)
		return NULL;
	
	xmlSetGnomeCanvasPoints (cur, "Points", object->points);
	xmlSetIntValue (cur, "Width", object->width);
	xmlSetStringValue (cur, "Color", object->color);
	return cur;
1146 1147 1148 1149 1150
}

/*
 * Create a Object equivalent to the XML subtree of doc.
 */
1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196
static SheetObject *
readXmlObject (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	SheetObject *ret;
	SheetFilledObject *fo;
	char *color;
	char *fill_color;
	int type;
	double x1, y1, x2, y2;
	int width = 1;
	int pattern;

	if (!strcmp (tree->name, "Rectangle")){
		type = SHEET_OBJECT_RECTANGLE;
	} else if (!strcmp (tree->name, "Ellipse")){
		type = SHEET_OBJECT_ELLIPSE;
	} else if (!strcmp (tree->name, "Arrow")){
		type = SHEET_OBJECT_ARROW;
	} else if (!strcmp (tree->name, "Line")){
		type = SHEET_OBJECT_LINE;
	} else {
		fprintf (stderr,
			 "readXmlObject: invalid element type %s, 'Rectangle/Ellipse ...' expected`\n",
			 tree->name);
		return NULL;
	}
	
	color = (char *) xmlGetValue (tree, "Color");
	xmlGetCoordinates (tree, "Points", &x1, &y1, &x2, &y2);
	xmlGetIntValue (tree, "Width", &width);
	if ((type == SHEET_OBJECT_RECTANGLE) ||
	    (type == SHEET_OBJECT_ELLIPSE)){
		fill_color = (char *) xmlGetValue (tree, "FillColor");
		xmlGetIntValue (tree, "Pattern", &pattern);
		ret = sheet_object_create_filled (ctxt->sheet, type,
			       x1, y1, x2, y2, fill_color, color, width);
		if (ret != NULL){
			fo = (SheetFilledObject *) ret;
			fo->pattern = pattern;
		}
	} else {
		ret = sheet_object_create_line (ctxt->sheet, type,
					   x1, y1, x2, y2, color, width);
	}
	sheet_object_realize (ctxt->sheet, ret);
	return ret;
1197 1198
}

1199 1200 1201
/*
 * Create an XML subtree of doc equivalent to the given Cell.
 */
1202 1203 1204 1205 1206
static xmlNodePtr
writeXmlCell (parseXmlContextPtr ctxt, Cell *cell)
{
	xmlNodePtr cur, child;

1207
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Cell", NULL);
1208 1209 1210 1211 1212 1213
	xmlSetIntValue (cur, "Col", cell->col->pos);
	xmlSetIntValue (cur, "Row", cell->row->pos);
	child = writeXmlStyle (ctxt, cell->style);

	if (child)
		xmlAddChild (cur, child);
1214 1215
	xmlNewChild(cur, ctxt->ns, "Content", 
	            xmlEncodeEntities(ctxt->doc, cell->entered_text->str));
1216 1217

	return cur;
1218 1219 1220 1221 1222
}

/*
 * Create a Cell equivalent to the XML subtree of doc.
 */
1223 1224 1225 1226 1227
static Cell *
readXmlCell (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	Style *style;
	Cell *ret;
1228
	xmlNodePtr childs;
1229
	int row = 0, col = 0;
1230
	char *content = NULL;
1231 1232 1233

	if (strcmp (tree->name, "Cell")){
		fprintf (stderr,
1234
		 "readXmlCell: invalid element type %s, 'Cell' expected`\n",
1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
			 tree->name);
		return NULL;
	}
	xmlGetIntValue (tree, "Col", &col);
	xmlGetIntValue (tree, "Row", &row);

	ret = sheet_cell_get (ctxt->sheet, col, row);
	if (ret == NULL)
		ret = sheet_cell_new (ctxt->sheet, col, row);
	if (ret == NULL)
		return NULL;

1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
	childs = tree->childs;
	while (childs != NULL) {
	        if (!strcmp (childs->name, "Style")) {
		        style = readXmlStyle (ctxt, childs, NULL);
			if (style){
				style_merge_to (style, ret->style);
				style_destroy (ret->style);
				ret->style = style;
			}
		}
1257 1258
	        if (!strcmp (childs->name, "Content"))
		        content = xmlNodeGetContent(childs);
1259 1260
		childs = childs->next;
	}
1261 1262
	if (content == NULL)
		content = xmlNodeGetContent(tree);
1263 1264
	if (content != NULL) {
		char *p = content + strlen (content);
1265

1266
		while (p > content){
1267 1268 1269 1270 1271 1272
			p--;
			if (*p != ' ' && *p != '\n')
				break;
			*p = 0;
		}

1273 1274
		cell_set_text_simple (ret, content);
		free (content);
1275 1276 1277 1278
	} else
		cell_set_text_simple (ret, "");

	return ret;
1279 1280 1281 1282 1283
}

/*
 * Create an XML subtree equivalent to the given cell and add it to the parent
 */
1284 1285 1286 1287 1288
static void
writeXmlCellTo (gpointer key, gpointer value, gpointer data)
{
	parseXmlContextPtr ctxt = (parseXmlContextPtr) data;
	xmlNodePtr cur;
1289

1290 1291
	cur = writeXmlCell (ctxt, (Cell *) value);
	xmlAddChild (ctxt->parent, cur);
1292 1293 1294 1295 1296
}

/*
 * Create an XML subtree of doc equivalent to the given Sheet.
 */
1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
static xmlNodePtr
writeXmlSheet (parseXmlContextPtr ctxt, Sheet *sheet)
{
	xmlNodePtr cur;
	xmlNodePtr child;
	xmlNodePtr rows;
	xmlNodePtr cols;
	xmlNodePtr cells;
	xmlNodePtr objects;
	GList *l;
	char str[50];

	/*
	 * General informations about the Sheet.
	 */
1312
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Sheet", NULL);
1313 1314
	if (cur == NULL)
		return NULL;
1315 1316
	xmlNewChild (cur, ctxt->ns, "Name", 
	             xmlEncodeEntities(ctxt->doc, sheet->name));
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377
	sprintf (str, "%d", sheet->max_col_used);
	xmlNewChild (cur, ctxt->ns, "MaxCol", str);
	sprintf (str, "%d", sheet->max_row_used);
	xmlNewChild (cur, ctxt->ns, "MaxRow", str);
	sprintf (str, "%f", sheet->last_zoom_factor_used);
	xmlNewChild (cur, ctxt->ns, "Zoom", str);

	/*
	 * Cols informations.
	 */
	cols = xmlNewChild (cur, ctxt->ns, "Cols", NULL);
	l = sheet->cols_info;
	while (l){
		child = writeXmlColRowInfo (ctxt, l->data, 1);
		if (child)
			xmlAddChild (cols, child);
		l = l->next;
	}

	/*
	 * Rows informations.
	 */
	rows = xmlNewChild (cur, ctxt->ns, "Rows", NULL);
	l = sheet->rows_info;
	while (l){
		child = writeXmlColRowInfo (ctxt, l->data, 0);
		if (child)
			xmlAddChild (rows, child);
		l = l->next;
	}

	/*
	 * Style : TODO ...
	 */

	/*
	 * Objects
	 * NOTE: seems that objects == NULL while current_object != NULL
	 * is possible
	 */
	if (sheet->objects != NULL){
		objects = xmlNewChild (cur, ctxt->ns, "Objects", NULL);
		l = sheet->objects;
		while (l){
			child = writeXmlObject (ctxt, l->data);
			if (child)
				xmlAddChild (objects, child);
			l = l->next;
		}
	} else if (sheet->current_object != NULL){
		objects = xmlNewChild (cur, ctxt->ns, "Objects", NULL);
		child = writeXmlObject (ctxt, sheet->current_object);
		if (child)
			xmlAddChild (objects, child);
	}
	/*
	 * Cells informations
	 */
	cells = xmlNewChild (cur, ctxt->ns, "Cells", NULL);
	ctxt->parent = cells;
	g_hash_table_foreach (sheet->cell_hash, writeXmlCellTo, ctxt);
1378
	sheet->modified = 0;
1379
	return cur;
1380 1381 1382 1383 1384
}

/*
 * Create a Sheet equivalent to the XML subtree of doc.
 */
1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473
static Sheet *
readXmlSheet (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	xmlNodePtr child;
	xmlNodePtr rows;
	xmlNodePtr cols;
	xmlNodePtr regions;
	/* xmlNodePtr styles; */
	xmlNodePtr cells;
	xmlNodePtr objects;
	Sheet *ret;
	const char *val;

	if (strcmp (tree->name, "Sheet")){
		fprintf (stderr,
			 "readXmlSheet: invalid element type %s, 'Sheet' expected`\n",
			 tree->name);
	}
	child = tree->childs;
	/*
	 * Get the name to create the sheet
	 */
	val = xmlGetValue (tree, "Name");
	if (val != NULL){
		ret = sheet_new (ctxt->wb, (char *) val);
	} else {
		fprintf (stderr, "readXmlSheet: Sheet has no name\n");
		ret = sheet_new (ctxt->wb, _ ("NoName"));
	}

	if (ret == NULL)
		return NULL;

	ctxt->sheet = ret;

	xmlGetIntValue (tree, "MaxCol", &ret->max_col_used);
	xmlGetIntValue (tree, "MaxRow", &ret->max_row_used);
	xmlGetDoubleValue (tree, "Zoom", &ret->last_zoom_factor_used);
	child = xmlSearchChild (tree, "Styles");
	if (child != NULL){
		regions = child->childs;
		while (regions != NULL){
			readXmlStyleRegion (ctxt, regions);
			regions = regions->next;
		}
	}
	child = xmlSearchChild (tree, "Cols");
	if (child != NULL){
		ColRowInfo *info;

		cols = child->childs;
		while (cols != NULL){
			info = readXmlColRowInfo (ctxt, cols, NULL);
			if (info != NULL)
				sheet_col_add (ret, info);
			cols = cols->next;
		}
	}
	child = xmlSearchChild (tree, "Rows");
	if (child != NULL){
		ColRowInfo *info;

		rows = child->childs;
		while (rows != NULL){
			info = readXmlColRowInfo (ctxt, rows, NULL);
			if (info != NULL)
				sheet_row_add (ret, info);
			rows = rows->next;
		}
	}
	child = xmlSearchChild (tree, "Objects");
	if (child != NULL){
		objects = child->childs;
		while (objects != NULL){
			readXmlObject (ctxt, objects);
			objects = objects->next;
		}
	}
	child = xmlSearchChild (tree, "Cells");
	if (child != NULL){
		cells = child->childs;
		while (cells != NULL){
			readXmlCell (ctxt, cells);
			cells = cells->next;
		}
	}
	/* Initialize the ColRowInfo's ->pixels data */
	sheet_set_zoom_factor (ret, ret->last_zoom_factor_used);
	return ret;
1474 1475 1476
}

/*
1477 1478
 * Create an XML subtree equivalent to the given sheet
 * and add it to the parent
1479
 */
1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490
static void
writeXmlSheetTo (gpointer key, gpointer value, gpointer data)
{
	parseXmlContextPtr ctxt = (parseXmlContextPtr) data;
	xmlNodePtr cur, parent;
	Sheet *sheet = (Sheet *) value;

	parent = ctxt->parent;
	cur = writeXmlSheet (ctxt, sheet);
	ctxt->parent = parent;
	xmlAddChild (parent, cur);
1491 1492 1493 1494 1495
}

/*
 * Create an XML subtree of doc equivalent to the given Workbook.
 */
1496 1497 1498 1499 1500 1501 1502 1503 1504
static xmlNodePtr
writeXmlWorkbook (parseXmlContextPtr ctxt, Workbook *wb)
{
	xmlNodePtr cur;
	xmlNodePtr child;

	/*
	 * General informations about the Sheet.
	 */
1505
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Workbook", NULL);	/* the Workbook name !!! */
1506 1507 1508 1509 1510 1511 1512
	if (cur == NULL)
		return NULL;

	child = writeXmlStyle (ctxt, &wb->style);
	if (child)
		xmlAddChild (cur, child);

1513
	child = xmlNewDocNode (ctxt->doc, ctxt->ns, "Geometry", NULL);
1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525
	xmlSetIntValue (child, "Width", wb->toplevel->allocation.width);
	xmlSetIntValue (child, "Height", wb->toplevel->allocation.height);
	xmlAddChild (cur, child);

	/*
	 * Cells informations
	 */
	child = xmlNewChild (cur, ctxt->ns, "Sheets", NULL);
	ctxt->parent = child;
	g_hash_table_foreach (wb->sheets, writeXmlSheetTo, ctxt);
	
	return cur;
1526 1527 1528 1529 1530
}

/*
 * Create a Workbook equivalent to the XML subtree of doc.
 */
1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570
static Workbook *
readXmlWorkbook (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	Workbook *ret;
	Sheet *sheet;
	xmlNodePtr child;

	if (strcmp (tree->name, "Workbook")){
		fprintf (stderr,
			 "readXmlWorkbook: invalid element type %s, 'Workbook' expected`\n",
			 tree->name);
		return NULL;
	}
	ret = workbook_new ();
	ctxt->wb = ret;

	child = xmlSearchChild (tree, "Geometry");
	if (child){
		int width, height;

		xmlGetIntValue (child, "Width", &width);
		xmlGetIntValue (child, "Height", &height);
/*      gtk_widget_set_usize(ret->toplevel, width, height); */
	}
	child = xmlSearchChild (tree, "Style");
	if (child != NULL)
		readXmlStyle (ctxt, child, &ret->style);

	child = xmlSearchChild (tree, "Sheets");
	if (child == NULL)
		return ret;
	child = child->childs;
	while (child != NULL){
		sheet = readXmlSheet (ctxt, child);
		if (sheet != NULL)
			workbook_attach_sheet (ret, sheet);
		child = child->next;
	}
	
	return ret;
1571
}