xml-io.c 36.3 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
#include "sheet-object-graphic.h"
18
#include "xml-io.h"
19
#include "file.h"
20 21 22 23 24

/*
 * A parsing context.
 */
typedef struct parseXmlContext {
25 26 27 28 29 30 31
	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 */
32 33
} parseXmlContext, *parseXmlContextPtr;

Arturo Espinosa's avatar
Arturo Espinosa committed
34 35 36 37
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);
38
/* static guint       ptrHash          (gconstpointer a);
39 40 41
   static gint        ptrCompare       (gconstpointer a, gconstpointer b); */
static void nameFree                (gpointer key, gpointer value,
				     gpointer user_data);
42

43 44 45
/*
 * Internal stuff: xml helper functions.
 */
46 47 48 49 50

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

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

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

	return NULL;
75 76
}

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

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

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

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

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

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

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

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

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

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

178 179 180 181 182 183 184 185 186
	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;
187 188
	}
	return 0;
189 190 191 192 193 194
}

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

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

204 205
	ret = xmlGetValue (node, name);
	if (ret == NULL) return(0);
206
	res = sscanf (ret, "(%lf %lf)(%lf %lf)", x1, y1, x2, y2);
207 208
	free(ret);
	
209
	if (res == 4) 
210
		return 1;
211

212
	return 0;
213 214 215 216 217 218
}

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

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

230
	val = xmlGetValue (node, name);
231
	if (val == NULL) return(NULL);
232 233 234 235 236
	ptr = val;
	do {
		while ((*ptr) && (*ptr != '('))
			ptr++;
		if (*ptr == 0)
237
			break;
238 239 240 241 242 243 244 245 246 247 248 249 250 251
		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];
252
	return ret;
253 254 255 256 257 258
}

/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
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;
	}
288
	xmlNewChild (node, NULL, name, xmlEncodeEntities(node->doc, base));
289
	free (base);
290 291 292
}


293 294 295 296
/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
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);
316 317
}

318 319 320 321
/*
 * Set a String value for a node either carried as an attibute or as
 * the content of a child.
 */
322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340
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);
341 342
}

343 344 345 346
/*
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
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);
369 370 371 372 373 374
}

/*
 * Set a float value for a node either carried as an attibute or as
 * the content of a child.
 */
375 376
static void
xmlSetFloatValue (xmlNodePtr node, const char *name, float val)
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
{
	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);
397 398 399 400 401 402
}

/*
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
403 404
static void
xmlSetDoubleValue (xmlNodePtr node, const char *name, double val)
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
{
	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
static char *BorderTypes[8] =
{
	"none",
746 747 748 749 750 751 752
 	"thin",
 	"medium",
 	"dashed",
 	"dotted",
 	"thick",
 	"double",
	"hair"
753 754
};

755 756 757 758 759 760
static char *StyleSideNames[4] =
{
 	"Top",
 	"Bottom",
 	"Left",
 	"Right"
Arturo Espinosa's avatar
Arturo Espinosa committed
761
};
762

763 764 765 766 767
static xmlNodePtr
writeXmlStyleBorder (parseXmlContextPtr ctxt, StyleBorder *border)
{
	xmlNodePtr cur;
	xmlNodePtr side;
Arturo Espinosa's avatar
Arturo Espinosa committed
768
	int lp;
769
       
Arturo Espinosa's avatar
Arturo Espinosa committed
770 771 772
	for (lp = 3; lp >= 0; lp--)
		if (border->type [lp] != BORDER_NONE)
			break;
773
	if (lp<0)
Arturo Espinosa's avatar
Arturo Espinosa committed
774
		return NULL;
775

776
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
777 778 779 780 781 782 783 784 785
	
	for (lp=0;lp<4;lp++)
 	{
 		if (border->type[lp] != BORDER_NONE)
 		{
 			side = xmlNewChild (cur, ctxt->ns, StyleSideNames[lp],
 					    BorderTypes[border->type[lp]]);
 			xmlSetColorValue (side, "Color", border->color[lp]);
 		}
786 787
	}
	return cur;
788 789 790 791 792
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
793 794 795 796
static StyleBorder *
readXmlStyleBorder (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	StyleBorder *ret;
Arturo Espinosa's avatar
Arturo Espinosa committed
797 798
 	StyleBorderType style [4] = { BORDER_NONE, BORDER_NONE, BORDER_NONE, BORDER_NONE };
 	StyleColor *color [4] = { NULL, NULL, NULL, NULL };
799
	xmlNodePtr side;
Arturo Espinosa's avatar
Arturo Espinosa committed
800
	int lp;
801 802 803 804 805 806

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
			 "readXmlStyleBorder: invalid element type %s, 'StyleBorder' expected`\n",
			 tree->name);
	}
807

Arturo Espinosa's avatar
Arturo Espinosa committed
808
 	for (lp = 0; lp < 4; lp++)
809
 	{
Arturo Espinosa's avatar
Arturo Espinosa committed
810
 		if ((side = xmlSearchChild (tree, StyleSideNames [lp])) != NULL)
811 812
 		{
 			/* FIXME: need to read the proper type */
Arturo Espinosa's avatar
Arturo Espinosa committed
813 814
 			style [lp] = BORDER_THICK ;
 			xmlGetColorValue (side, "Color", &color [lp]);
815
 		}
816
	}
817
	
Arturo Espinosa's avatar
Arturo Espinosa committed
818
	ret = style_border_new (style, color);
819

820
	return NULL;
821 822 823 824 825
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
826 827 828 829 830 831 832 833 834 835 836 837
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;

838
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
	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 {
862 863 864
			child = xmlNewChild (cur, ctxt->ns, "Font", 
			           xmlEncodeEntities(ctxt->doc,
				                     style->font->font_name));
865 866 867 868 869 870 871 872 873 874 875 876 877 878 879
			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;
880 881 882 883 884
}

/*
 * Create a Style equivalent to the XML subtree of doc.
 */
885 886 887 888 889 890 891 892 893 894
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,
895
			 "readXmlStyle: invalid element type %s, 'Style' expected\n",
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 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936
			 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;
937
		}
938
	}
939

940 941 942 943 944 945 946 947
	child = tree->childs;
	while (child != NULL){
		if (!strcmp (child->name, "Font")){
			const char *v;

			v = xmlGetValue (child, "NAME");
			if (v){
				int units = 14;
948
				char *font;
949 950

				xmlGetIntValue (child, "Unit", &units);
951 952 953 954 955
				font = xmlNodeGetContent(child);
				if (font != NULL) {
					ret->font = style_font_new (font, units);
					free(font);
				}
956 957 958 959 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
				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;
986 987
}

988 989 990
/*
 * Create an XML subtree of doc equivalent to the given StyleRegion.
 */
991
static xmlNodePtr
992 993 994 995
writeXmlStyleRegion (parseXmlContextPtr ctxt, StyleRegion *region)
{
	xmlNodePtr cur, child;

996
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleRegion", NULL);
997 998 999 1000 1001 1002 1003 1004 1005 1006 1007
	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;
1008 1009 1010 1011 1012
}

/*
 * Create a StyleRegion equivalent to the XML subtree of doc.
 */
1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
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);
1036 1037 1038

}

1039 1040 1041
/*
 * Create an XML subtree of doc equivalent to the given ColRowInfo.
 */
1042 1043 1044 1045 1046 1047
static xmlNodePtr
writeXmlColRowInfo (parseXmlContextPtr ctxt, ColRowInfo *info, int col)
{
	xmlNodePtr cur;

	if (col)
1048
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "ColInfo", NULL);
1049
	else
1050
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "RowInfo", NULL);
1051 1052 1053 1054 1055 1056 1057 1058

	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;
1059 1060 1061 1062 1063
}

/*
 * Create a ColRowInfo equivalent to the XML subtree of doc.
 */
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088
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;
1089

1090 1091 1092 1093 1094 1095
	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;
1096

1097
	return ret;
1098 1099
}

1100 1101 1102
/*
 * Create an XML subtree of doc equivalent to the given Object.
 */
1103 1104 1105
static xmlNodePtr
writeXmlObject (parseXmlContextPtr ctxt, SheetObject *object)
{
1106
	SheetObjectGraphic *sog = SHEET_OBJECT_GRAPHIC (object);
1107 1108
	xmlNodePtr cur = NULL;

1109
	switch (sog->type){
1110
	case SHEET_OBJECT_RECTANGLE:{
1111
			SheetObjectFilled *sof = SHEET_OBJECT_FILLED (object);
1112

1113
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Rectangle", NULL);
1114 1115 1116
			if (sof->fill_color != NULL)
				xmlSetStringValue (cur, "FillColor", sof->fill_color);
			xmlSetIntValue (cur, "Pattern", sof->pattern);
1117 1118
			break;
		}
1119

1120
	case SHEET_OBJECT_ELLIPSE:{
1121
			SheetObjectFilled *sof = SHEET_OBJECT_FILLED (object);
1122

1123
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Ellipse", NULL);
1124 1125 1126
			if (sof->fill_color != NULL)
				xmlSetStringValue (cur, "FillColor", sof->fill_color);
			xmlSetIntValue (cur, "Pattern", sof->pattern);
1127 1128
			break;
		}
1129

1130
	case SHEET_OBJECT_ARROW:
1131
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Arrow", NULL);
1132
		break;
1133

1134
	case SHEET_OBJECT_LINE:
1135
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Line", NULL);
1136 1137 1138 1139 1140
		break;
	}
	if (cur == NULL)
		return NULL;
	
1141
	xmlSetGnomeCanvasPoints (cur, "Points", object->bbox_points);
1142 1143 1144
	xmlSetIntValue (cur, "Width", sog->width);
	xmlSetStringValue (cur, "Color", sog->color);

1145
	return cur;
1146 1147 1148 1149 1150
}

/*
 * Create a Object equivalent to the XML subtree of doc.
 */
1151 1152 1153 1154
static SheetObject *
readXmlObject (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	SheetObject *ret;
1155
	SheetObjectFilled *sof;
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
	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);
1185 1186 1187
		ret = sheet_object_create_filled (
			ctxt->sheet, type,
			x1, y1, x2, y2, fill_color, color, width);
1188
		if (ret != NULL){
1189 1190
			sof = SHEET_OBJECT_FILLED (ret);
			sof->pattern = pattern;
1191 1192
		}
	} else {
1193 1194 1195
		ret = sheet_object_create_line (
			ctxt->sheet, type,
			x1, y1, x2, y2, color, width);
1196
	}
1197
	sheet_object_realize (ret);
1198
	return ret;
1199 1200
}

1201 1202 1203
/*
 * Create an XML subtree of doc equivalent to the given Cell.
 */
1204 1205 1206 1207
static xmlNodePtr
writeXmlCell (parseXmlContextPtr ctxt, Cell *cell)
{
	xmlNodePtr cur, child;
1208 1209
	char *text;
	
1210
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Cell", NULL);
1211 1212 1213 1214 1215 1216
	xmlSetIntValue (cur, "Col", cell->col->pos);
	xmlSetIntValue (cur, "Row", cell->row->pos);
	child = writeXmlStyle (ctxt, cell->style);

	if (child)
		xmlAddChild (cur, child);
1217
	text = cell_get_content (cell);
1218 1219
	xmlNewChild(cur, ctxt->ns, "Content",
	            xmlEncodeEntities(ctxt->doc, text));
1220
	g_free (text);
1221 1222

	return cur;
1223 1224 1225 1226 1227
}

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

Michael Meeks's avatar
Michael Meeks committed
1237 1238
	cell_deep_freeze_redraws ();

1239 1240
	if (strcmp (tree->name, "Cell")){
		fprintf (stderr,
1241
		 "readXmlCell: invalid element type %s, 'Cell' expected`\n",
1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253
			 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;

1254 1255 1256 1257 1258 1259 1260 1261 1262 1263
	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;
			}
		}
1264 1265
	        if (!strcmp (childs->name, "Content"))
		        content = xmlNodeGetContent(childs);
1266 1267
		childs = childs->next;
	}
1268 1269
	if (content == NULL)
		content = xmlNodeGetContent(tree);
1270 1271
	if (content != NULL) {
		char *p = content + strlen (content);
1272

1273
		while (p > content){
1274 1275 1276 1277 1278 1279
			p--;
			if (*p != ' ' && *p != '\n')
				break;
			*p = 0;
		}

1280 1281
		cell_set_text_simple (ret, content);
		free (content);
1282 1283 1284
	} else
		cell_set_text_simple (ret, "");

Michael Meeks's avatar
Michael Meeks committed
1285 1286
	cell_deep_thaw_redraws ();

1287
	return ret;
1288 1289 1290 1291 1292
}

/*
 * Create an XML subtree equivalent to the given cell and add it to the parent
 */
1293 1294 1295 1296 1297
static void
writeXmlCellTo (gpointer key, gpointer value, gpointer data)
{
	parseXmlContextPtr ctxt = (parseXmlContextPtr) data;
	xmlNodePtr cur;
1298

1299 1300
	cur = writeXmlCell (ctxt, (Cell *) value);
	xmlAddChild (ctxt->parent, cur);
1301 1302 1303 1304 1305
}

/*
 * Create an XML subtree of doc equivalent to the given Sheet.
 */
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
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.
	 */
1321
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Sheet", NULL);
1322 1323
	if (cur == NULL)
		return NULL;
1324 1325
	xmlNewChild (cur, ctxt->ns, "Name", 
	             xmlEncodeEntities(ctxt->doc, sheet->name));
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 1378 1379 1380 1381 1382 1383 1384 1385 1386
	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);
1387
	sheet->modified = 0;
1388
	return cur;
1389 1390 1391 1392 1393
}

/*
 * Create a Sheet equivalent to the XML subtree of doc.
 */
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
static Sheet *
readXmlSheet (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	xmlNodePtr child;
	xmlNodePtr rows;
	xmlNodePtr cols;
	xmlNodePtr regions;
	/* xmlNodePtr styles; */
	xmlNodePtr cells;
	xmlNodePtr objects;
1404
	Sheet *ret = NULL;
1405 1406 1407 1408
	const char *val;

	if (strcmp (tree->name, "Sheet")){
		fprintf (stderr,
1409
			 "readXmlSheet: invalid element type %s, 'Sheet' expected\n",
1410 1411 1412
			 tree->name);
	}
	child = tree->childs;
1413

1414
	/*
1415 1416 1417
	 * Get the name of the sheet.  If it does exist, use the existing
	 * name, otherwise create a sheet (ie, for the case of only reading
	 * a new sheet).
1418 1419 1420
	 */
	val = xmlGetValue (tree, "Name");
	if (val != NULL){
1421 1422 1423 1424
		ret = workbook_sheet_lookup (ctxt->wb, (const char *) val);
		if (ret == NULL)
			ret = sheet_new (ctxt->wb, (const char *) val);
	} 
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 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484

	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;
1485 1486 1487
}

/*
1488 1489
 * Create an XML subtree equivalent to the given sheet
 * and add it to the parent
1490
 */
1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501
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);
1502 1503 1504 1505 1506
}

/*
 * Create an XML subtree of doc equivalent to the given Workbook.
 */
1507 1508 1509 1510 1511 1512 1513 1514 1515
static xmlNodePtr
writeXmlWorkbook (parseXmlContextPtr ctxt, Workbook *wb)
{
	xmlNodePtr cur;
	xmlNodePtr child;

	/*
	 * General informations about the Sheet.
	 */
1516
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Workbook", NULL);	/* the Workbook name !!! */
1517 1518 1519 1520 1521 1522 1523
	if (cur == NULL)
		return NULL;

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

1524
	child = xmlNewDocNode (ctxt->doc, ctxt->ns, "Geometry", NULL);
1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
	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;
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
static void
createXmlSheet (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	const char *val;
	xmlNodePtr child;
	
	if (strcmp (tree->name, "Sheet")){
		fprintf (stderr,
			 "createXmlSheet: invalid element type %s, 'Sheet' expected\n",
			 tree->name);
		return;
	}
	child = tree->childs;
	val = xmlGetValue (tree, "Name");
	if (val != NULL){
		Sheet *sheet;
		
		sheet = sheet_new (ctxt->wb, (char *) val);
		workbook_attach_sheet (ctxt->wb, sheet);
	}
	return;
}

1562 1563 1564
/*
 * Create a Workbook equivalent to the XML subtree of doc.
 */
1565 1566 1567 1568 1569
static Workbook *
readXmlWorkbook (parseXmlContextPtr ctxt, xmlNodePtr tree)
{
	Workbook *ret;
	Sheet *sheet;
1570
	xmlNodePtr child, c;
1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595

	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;
1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606