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

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

/*
 * A parsing context.
 */
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
typedef struct {
	xmlDocPtr doc;		/* Xml document */
	xmlNsPtr ns;		/* Main name space */
	xmlNodePtr parent;	/* used only for g_hash_table_foreach callbacks */
	Sheet *sheet;		/* the associated sheet */
	Workbook *wb;		/* the associated sheet */
	GHashTable *style_table;/* to generate the styles and then the links to it */
	int style_count;        /* A style number */
	xmlNodePtr style_node;  /* The node where we insert the styles */
} parse_xml_context_t;

static Sheet      *xml_sheet_read     (parse_xml_context_t *ctxt, xmlNodePtr tree);
static xmlNodePtr  xml_sheet_write    (parse_xml_context_t *ctxt, Sheet *sheet);
static Workbook   *xml_workbook_read  (parse_xml_context_t *ctxt, xmlNodePtr tree);
static xmlNodePtr  xml_workbook_write (parse_xml_context_t *ctxt, Workbook *wb);
41

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

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

56
	ret = (char *) xmlGetProp (node, name);
57
	if (ret != NULL)
Morten Welinder's avatar
Morten Welinder committed
58
		return ret;
59 60 61
	child = node->childs;

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

	return NULL;
74 75
}

76
#if 0
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
xml_get_value_string (xmlNodePtr node, const char *name)
83
{
84
	char *val;
85 86
	String *ret;

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

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

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

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

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

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

153
	ret = xml_value_get (node, name);
154
	if (ret == NULL) return(0);
155
	res = sscanf (ret, "%lf", val);
156
	free(ret);
157

158
	return (res == 1);
159 160
}

161
#if 0
162 163 164
/*
 * Get a set of coodinates for a node, carried as the content of a child.
 */
165
static int
166
xml_get_coordinate (xmlNodePtr node, const char *name, double *x, double *y)
167
{
168 169
	int res;
	char *ret;
170
	float X, Y;
171

172
	ret = xml_value_get (node, name);
173
	if (ret == NULL) return(0);
174
	res = sscanf (ret, "(%lf %lf)", x, y)
175
	free(ret);
176 177

	return (res == 2)
178
}
179
#endif
180 181 182 183 184

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

185
static int
186
xml_get_coordinates (xmlNodePtr node, const char *name,
187 188
		   double *x1, double *y1, double *x2, double *y2)
{
189 190
	int res;
	char *ret;
191

192
	ret = xml_value_get (node, name);
193
	if (ret == NULL) return(0);
194
	res = sscanf (ret, "(%lf %lf)(%lf %lf)", x1, y1, x2, y2);
195 196
	free(ret);
	
197
	if (res == 4) 
198
		return 1;
199

200
	return 0;
201 202
}

203
#if 0
204 205 206
/*
 * Get a GnomeCanvasPoints for a node, carried as the content of a child.
 */
207
static GnomeCanvasPoints *
208
xml_get_gnome_canvas_points (xmlNodePtr node, const char *name)
209
{
210
	char *val;
211 212 213 214
	GnomeCanvasPoints *ret = NULL;
	int res;
	const char *ptr;
	int index = 0, i;
215
	float coord[20];	/* TODO: must be dynamic !!!! */
216

217
	val = xml_value_get (node, name);
218
	if (val == NULL) return(NULL);
219 220 221 222 223
	ptr = val;
	do {
		while ((*ptr) && (*ptr != '('))
			ptr++;
		if (*ptr == 0)
224
			break;
225
		res = sscanf (ptr, "(%lf %lf)", &coord[index], &coord[index + 1]);
226 227 228 229 230 231 232 233 234 235 236 237 238
		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];
239
	return ret;
240
}
241
#endif
242 243 244 245 246

/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
247
static void
248
xml_set_gnome_canvas_points (xmlNodePtr node, const char *name,
249
			     GnomeCanvasPoints *val)
250 251 252 253 254 255 256 257 258
{
	xmlNodePtr child;
	char *str, *base;
	int i;

	if (val == NULL)
		return;
	if ((val->num_points < 0) || (val->num_points > 5000))
		return;
259
	base = str = g_malloc (val->num_points * 30 * sizeof (char) + 1);
260 261 262
	if (str == NULL)
		return;
	for (i = 0; i < val->num_points; i++){
263 264 265
		sprintf (str, "(%f %f)", val->coords[2 * i],
			 val->coords[2 * i + 1]);
		str += strlen (str);
266
	}
267
	*str = 0;
268 269 270 271 272 273 274 275 276 277

	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, base);
			free (base);
			return;
		}
		child = child->next;
	}
278
	xmlNewChild (node, NULL, name, xmlEncodeEntities(node->doc, base));
279
	g_free (base);
280 281 282
}


283 284 285 286
/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
287 288
static void
xml_set_value (xmlNodePtr node, const char *name, const char *val)
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
{
	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);
307 308
}

309 310 311 312
/*
 * Set a String value for a node either carried as an attibute or as
 * the content of a child.
 */
313 314
static void
xml_set_value_string (xmlNodePtr node, const char *name, String *val)
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
{
	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);
333 334
}

335 336 337 338
/*
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
339
static void
340
xml_set_value_int (xmlNodePtr node, const char *name, int val)
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
{
	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);
361 362
}

363
#if 0
364 365 366 367
/*
 * Set a float value for a node either carried as an attibute or as
 * the content of a child.
 */
368
static void
369
xml_set_value_float (xmlNodePtr node, const char *name, float val)
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
{
	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);
390
}
391
#endif
392 393 394 395 396

/*
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
397
static void
398
xml_set_value_double (xmlNodePtr node, const char *name, double val)
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
{
	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);
419 420 421 422 423
}

/*
 * Search a child by name, if needed go down the tree to find it. 
 */
424 425
static xmlNodePtr
xml_search_child (xmlNodePtr node, const char *name)
426 427 428 429 430 431 432 433 434 435 436 437
{
	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){
438
		ret = xml_search_child (child, name);
439 440 441 442 443
		if (ret != NULL)
			return ret;
		child = child->next;
	}
	return NULL;
444 445 446 447 448 449 450 451 452 453
}

/*
 * 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 ...
 */
454
static int
455
xml_get_color_value (xmlNodePtr node, const char *name, StyleColor **color)
456
{
457
	char *ret;
458 459
	int red, green, blue;

460
	ret = xml_value_get (node, name);
461 462
	if (ret == NULL) return(0);
	if (sscanf (ret, "%X:%X:%X", &red, &green, &blue) == 3){
463
		*color = style_color_new (red, green, blue);
464
		free(ret);
465 466 467
		return 1;
	}
	return 0;
468 469 470 471 472 473
}

/*
 * Set a color value for a node either carried as an attibute or as
 * the content of a child.
 */
474 475
static void
xml_set_color_value (xmlNodePtr node, const char *name, StyleColor *val)
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
{
	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);
496
}
497 498 499 500 501

/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
502
 **/
503

504 505
static int
style_is_default_fore (StyleColor *color)
506
{
507 508
	if (!color)
		return TRUE;
509

510 511 512 513
	if (color->color.red == 0 && color->color.green == 0 && color->color.blue == 0)
		return TRUE;
	else
		return FALSE;
514 515
}

516 517
static int
style_is_default_back (StyleColor *color)
518
{
519 520
	if (!color)
		return TRUE;
521

522 523 524 525
	if (color->color.red == 0xffff && color->color.green == 0xffff && color->color.blue == 0xffff)
		return TRUE;
	else
		return FALSE;
526 527 528 529 530
}

/*
 * Create an XML subtree of doc equivalent to the given StyleBorder.
 */
531 532 533
static char *BorderTypes[8] =
{
	"none",
534 535 536 537 538 539 540
 	"thin",
 	"medium",
 	"dashed",
 	"dotted",
 	"thick",
 	"double",
	"hair"
541 542
};

543 544 545 546 547 548
static char *StyleSideNames[4] =
{
 	"Top",
 	"Bottom",
 	"Left",
 	"Right"
Arturo Espinosa's avatar
Arturo Espinosa committed
549
};
550

551
static xmlNodePtr
552
xml_write_style_border (parse_xml_context_t *ctxt, StyleBorder *border)
553 554 555
{
	xmlNodePtr cur;
	xmlNodePtr side;
Arturo Espinosa's avatar
Arturo Espinosa committed
556
	int lp;
557
       
Arturo Espinosa's avatar
Arturo Espinosa committed
558 559 560
	for (lp = 3; lp >= 0; lp--)
		if (border->type [lp] != BORDER_NONE)
			break;
561
	if (lp < 0)
Arturo Espinosa's avatar
Arturo Espinosa committed
562
		return NULL;
563

564
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
565
	
566 567 568 569
	for (lp = 0; lp < 4; lp++){
 		if (border->type[lp] != BORDER_NONE){
 			side = xmlNewChild (cur, ctxt->ns, StyleSideNames [lp],
 					    BorderTypes [border->type [lp]]);
570
 			xml_set_color_value (side, "Color", border->color [lp]);
571
 		}
572 573
	}
	return cur;
574 575 576 577 578
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
579
static StyleBorder *
580
xml_read_style_border (parse_xml_context_t *ctxt, xmlNodePtr tree)
581 582
{
	StyleBorder *ret;
Arturo Espinosa's avatar
Arturo Espinosa committed
583
 	StyleBorderType style [4] = { BORDER_NONE, BORDER_NONE, BORDER_NONE, BORDER_NONE };
584
	StyleColor *color [4] = { NULL, NULL, NULL, NULL };
585
	xmlNodePtr side;
Arturo Espinosa's avatar
Arturo Espinosa committed
586
	int lp;
587 588 589

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
590
			 "xml_read_style_border: invalid element type %s, 'StyleBorder' expected`\n",
591 592
			 tree->name);
	}
593

Arturo Espinosa's avatar
Arturo Espinosa committed
594
 	for (lp = 0; lp < 4; lp++)
595
 	{
596
 		if ((side = xml_search_child (tree, StyleSideNames [lp])) != NULL)
597 598
 		{
 			/* FIXME: need to read the proper type */
Arturo Espinosa's avatar
Arturo Espinosa committed
599
 			style [lp] = BORDER_THICK ;
600
 			xml_get_color_value (side, "Color", &color [lp]);
601
 		}
602
	}
603
	
Arturo Espinosa's avatar
Arturo Espinosa committed
604
	ret = style_border_new (style, color);
605

606
	return NULL;
607 608 609 610 611
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
612
static xmlNodePtr
613
xml_write_style (parse_xml_context_t *ctxt, Style *style, int style_idx)
614 615 616 617 618 619 620 621
{
	xmlNodePtr cur, child;

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

622
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
623 624 625 626 627 628 629 630
	if (style_idx != -1)
		xml_set_value_int (cur, "No", style_idx);
	
	xml_set_value_int (cur, "HAlign", style->halign);
	xml_set_value_int (cur, "VAlign", style->valign);
	xml_set_value_int (cur, "Fit", style->fit_in_cell);
	xml_set_value_int (cur, "Orient", style->orientation);
	xml_set_value_int (cur, "Shade", style->pattern);
631 632

	if (!style_is_default_fore (style->fore_color))
633
		xml_set_color_value (cur, "Fore", style->fore_color);
634 635

	if (!style_is_default_back (style->back_color))
636
		xml_set_color_value (cur, "Back", style->back_color);
637 638

	if (style->format != NULL){
639
		xml_set_value (cur, "Format", style->format->format);
640 641 642
	}

	if (style->font != NULL){
643 644 645 646 647 648 649
		child = xmlNewChild (cur, ctxt->ns, "Font", 
				     xmlEncodeEntities(ctxt->doc,
						       style->font->font_name));
		xml_set_value_double (child, "Unit", style->font->size);
		xml_set_value_int (child, "Bold", style->font->is_bold);
		xml_set_value_int (child, "Italic", style->font->is_italic);

650 651 652
	}

	if (style->border != NULL){
653
		child = xml_write_style_border (ctxt, style->border);
654 655 656 657 658
		if (child)
			xmlAddChild (cur, child);
	}

	return cur;
659 660
}

661 662 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 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
static xmlNodePtr
xml_write_summary (parse_xml_context_t *ctxt, SummaryInfo *sin)
{
	GList *items, *m;
	xmlNodePtr cur;

	if (!sin)
		return NULL;

	m = items = summary_info_as_list (sin);

	if (!items)
		return NULL;

	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Summary", NULL);

	while (items) {
		xmlNodePtr   tmp;
		SummaryItem *sit = items->data;
		if (sit) {
			char *text;

			tmp = xmlNewDocNode (ctxt->doc, ctxt->ns, "Item", NULL);
			xmlNewChild (tmp, ctxt->ns, "name",
				     xmlEncodeEntities (ctxt->doc, sit->name));

			if (sit->type == SUMMARY_INT) {

				text = g_strdup_printf ("%d", sit->v.i);
				xmlNewChild (tmp, ctxt->ns, "val-int",
					     xmlEncodeEntities (ctxt->doc, text));

			} else {

				text = summary_item_as_text (sit);
				xmlNewChild (tmp, ctxt->ns, "val-string",
					     xmlEncodeEntities (ctxt->doc, text));

			}
			g_free (text);
		}
		xmlAddChild (cur, tmp);
		items = g_list_next (items);
	}
	g_list_free (m);
	return cur;
}

static void
xml_read_summary (parse_xml_context_t *ctxt, xmlNodePtr tree, SummaryInfo *sin)
{
	xmlNodePtr child, tmp;

	g_return_if_fail (sin);
	g_return_if_fail (ctxt);
	g_return_if_fail (tree);

	child = tree->childs;
	while (child) {
		if (child->name && !strcmp (child->name, "Item")) {
			xmlNodePtr bits;

			bits = child->childs;
			while (bits) {
				SummaryItem *sit = NULL;
				char *name = NULL;
				
				if (!strcmp (bits->name, "name")) {
					name = xmlNodeGetContent(bits);
				} else {
					char *txt;
					g_return_if_fail (name);

					txt = xmlNodeGetContent (bits);
					g_return_if_fail (txt);

					if (!strcmp (bits->name, "val-string"))
						sit = summary_item_new_string (name, txt);
					else if (!strcmp (bits->name, "val-int"))
						sit = summary_item_new_int    (name, atoi (txt));

					if (sit)
						summary_info_add (sin, sit);
					g_free (txt);
				}
			}
		}
		child = child->next;
	}
}

752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
static const char *
font_component (const char *fontname, int idx)
{
	int i = 0;
	const char *p = fontname;
	
	for (; *p && i < idx; p++){
		if (*p == '-')
			i++;
	}
	if (*p == '-')
		p++;

	return p;
}

/**
 * style_font_new_from_x11:
 * @fontname: an X11-like font name.
 * @scale: scale desired
 *
 * Tries to guess the fontname, the weight and italization parameters
 * to invoke style_font_new
 *
 * Returns: A valid style font.
 */
static StyleFont *
style_font_new_from_x11 (const char *fontname, double units, double scale)
{
	StyleFont *sf;
	char *typeface = "Helvetica";
	int is_bold = 0;
	int is_italic = 0;
	const char *c;

	/*
	 * FIXME: we should do something about the typeface instead
	 * of hardcoding it to helvetica.
	 */
	
	c = font_component (fontname, 2);
	if (strncmp (c, "bold", 4) == 0)
		is_bold = 1;

	c = font_component (fontname, 3);
	if (strncmp (c, "o", 1) == 0)
		is_italic = 1;
	if (strncmp (c, "i", 1) == 0)
		is_italic = 1;

	sf = style_font_new (typeface, units, scale, is_bold, is_italic);
	if (sf == NULL)
		sf = style_font_new_from (gnumeric_default_font, scale);

	return sf;
}

809 810 811
/*
 * Create a Style equivalent to the XML subtree of doc.
 */
812
static Style *
813
xml_read_style (parse_xml_context_t *ctxt, xmlNodePtr tree, Style * ret)
814 815
{
	xmlNodePtr child;
Morten Welinder's avatar
Morten Welinder committed
816
	char *prop;
817 818 819 820 821
	int val;
	StyleColor *c;

	if (strcmp (tree->name, "Style")){
		fprintf (stderr,
822
			 "xml_read_style: invalid element type %s, 'Style' expected\n",
823 824 825 826 827 828 829 830
			 tree->name);
	}
	if (ret == NULL)
		ret = style_new_empty ();
	
	if (ret == NULL)
		return NULL;

831
	if (xml_get_value_int (tree, "HAlign", &val)){
832 833 834
		ret->halign = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
835
	if (xml_get_value_int (tree, "Fit", &val)){
836 837 838
		ret->fit_in_cell = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
839
	if (xml_get_value_int (tree, "VAlign", &val)){
840 841 842
		ret->valign = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
843
	if (xml_get_value_int (tree, "Orient", &val)){
844 845 846
		ret->orientation = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
847
	if (xml_get_value_int (tree, "Shade", &val)){
848 849 850
		ret->pattern = val;
		ret->valid_flags |= STYLE_PATTERN;
	}
851
	if (xml_get_color_value (tree, "Fore", &c)){
852 853 854
		ret->fore_color = c;
		ret->valid_flags |= STYLE_FORE_COLOR;
	}
855
	if (xml_get_color_value (tree, "Back", &c)){
856 857 858
		ret->back_color = c;
		ret->valid_flags |= STYLE_BACK_COLOR;
	}
859
	prop = xml_value_get (tree, "Format");
860 861
	if (prop != NULL){
		if (ret->format == NULL){
Morten Welinder's avatar
Morten Welinder committed
862
			ret->format = style_format_new ((const char *) prop);
863
			ret->valid_flags |= STYLE_FORMAT;
864
		}
Morten Welinder's avatar
Morten Welinder committed
865
		free (prop);
866
	}
867

868 869 870
	child = tree->childs;
	while (child != NULL){
		if (!strcmp (child->name, "Font")){
871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897
			char *font;
			double units = 14;
			int is_bold = 0;
			int is_italic = 0;
			int t;
				
			xml_get_value_double (child, "Unit", &units);

			if (xml_get_value_int (child, "Bold", &t))
				is_bold = t;
			if (xml_get_value_int (child, "Italic", &t))
				is_italic = t;
			
			font = xmlNodeGetContent(child);
			if (font != NULL) {
				StyleFont *sf;

				if (*font == '-'){
					sf = style_font_new_from_x11 (font, units, 1.0);
				} else
					sf = style_font_new (font, units, 1.0, is_bold, is_italic);

				ret->font = sf;
				free(font);
			}
			if (ret->font){
				ret->valid_flags |= STYLE_FONT;
898 899 900 901
			}
		} else if (!strcmp (child->name, "StyleBorder")){
			StyleBorder *sb;

902
			sb = xml_read_style_border (ctxt, child);
903
		} else {
904
			fprintf (stderr, "xml_read_style: unknown type '%s'\n",
905 906 907 908 909 910 911
				 child->name);
		}
		child = child->next;
	}

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

914
#if 0
915 916 917
/*
 * Create an XML subtree of doc equivalent to the given StyleRegion.
 */
918
static xmlNodePtr
919
xml_write_style_region (parse_xml_context_t *ctxt, StyleRegion *region)
920 921 922
{
	xmlNodePtr cur, child;

923
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleRegion", NULL);
924 925 926 927
	xml_set_value_int (cur, "startCol", region->range.start_col);
	xml_set_value_int (cur, "endCol", region->range.end_col);
	xml_set_value_int (cur, "startRow", region->range.start_row);
	xml_set_value_int (cur, "endRow", region->range.end_row);
928 929

	if (region->style != NULL){
930
		child = xml_write_style (ctxt, region->style);
931 932 933 934
		if (child)
			xmlAddChild (cur, child);
	}
	return cur;
935
}
936
#endif
937 938 939 940

/*
 * Create a StyleRegion equivalent to the XML subtree of doc.
 */
941
static void
942
xml_read_style_region (parse_xml_context_t *ctxt, xmlNodePtr tree)
943 944 945 946 947 948 949
{
	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,
950
			 "xml_read_style_region: invalid element type %s, 'StyleRegion' expected`\n",
951 952 953
			 tree->name);
		return;
	}
954 955 956 957
	xml_get_value_int (tree, "startCol", &start_col);
	xml_get_value_int (tree, "startRow", &start_row);
	xml_get_value_int (tree, "endCol", &end_col);
	xml_get_value_int (tree, "endRow", &end_row);
958 959
	child = tree->childs;
	if (child != NULL)
960
		style = xml_read_style (ctxt, child, NULL);
961 962 963
	if (style != NULL)
		sheet_style_attach (ctxt->sheet, start_col, start_row, end_col,
				    end_row, style);
964 965 966

}

967 968 969
/*
 * Create an XML subtree of doc equivalent to the given ColRowInfo.
 */
970
static xmlNodePtr
971
xml_write_colrow_info (parse_xml_context_t *ctxt, ColRowInfo *info, int col)
972 973 974 975
{
	xmlNodePtr cur;

	if (col)
976
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "ColInfo", NULL);
977
	else
978
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "RowInfo", NULL);
979

980 981 982 983 984
	xml_set_value_int (cur, "No", info->pos);
	xml_set_value_double (cur, "Unit", info->units);
	xml_set_value_double (cur, "MarginA", info->margin_a_pt);
	xml_set_value_double (cur, "MarginB", info->margin_b);
	xml_set_value_int (cur, "HardSize", info->hard_size);
985 986

	return cur;
987 988 989 990 991
}

/*
 * Create a ColRowInfo equivalent to the XML subtree of doc.
 */
992
static ColRowInfo *
993
xml_read_colrow_info (parse_xml_context_t *ctxt, xmlNodePtr tree, ColRowInfo *ret)
994 995 996 997 998 999 1000 1001 1002 1003
{
	int col = 0;
	int val;

	if (!strcmp (tree->name, "ColInfo")){
		col = 1;
	} else if (!strcmp (tree->name, "RowInfo")){
		col = 0;
	} else {
		fprintf (stderr,
1004
			 "xml_read_colrow_info: invalid element type %s, 'ColInfo/RowInfo' expected`\n",
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015
			 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;
1016

1017 1018 1019 1020 1021
	xml_get_value_int (tree, "No", &ret->pos);
	xml_get_value_double (tree, "Unit", &ret->units);
	xml_get_value_double (tree, "MarginA", &ret->margin_a_pt);
	xml_get_value_double (tree, "MarginB", &ret->margin_b_pt);
	if (xml_get_value_int (tree, "HardSize", &val))
1022
		ret->hard_size = val;
1023

1024
	return ret;
1025 1026
}

1027 1028 1029
/*
 * Create an XML subtree of doc equivalent to the given Object.
 */
1030
static xmlNodePtr
1031
xml_write_sheet_object (parse_xml_context_t *ctxt, SheetObject *object)
1032
{
1033
	SheetObjectGraphic *sog = SHEET_OBJECT_GRAPHIC (object);
1034 1035
	xmlNodePtr cur = NULL;

1036
	switch (sog->type){
1037
	case SHEET_OBJECT_RECTANGLE:{
1038
			SheetObjectFilled *sof = SHEET_OBJECT_FILLED (object);
1039

1040
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Rectangle", NULL);
1041
			if (sof->fill_color != NULL)
1042 1043
				xml_set_value_string (cur, "FillColor", sof->fill_color);
			xml_set_value_int (cur, "Pattern", sof->pattern);
1044 1045
			break;
		}
1046

1047
	case SHEET_OBJECT_ELLIPSE:{
1048
			SheetObjectFilled *sof = SHEET_OBJECT_FILLED (object);
1049

1050
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Ellipse", NULL);
1051
			if (sof->fill_color != NULL)
1052 1053
				xml_set_value_string (cur, "FillColor", sof->fill_color);
			xml_set_value_int (cur, "Pattern", sof->pattern);
1054 1055
			break;
		}
1056

1057
	case SHEET_OBJECT_ARROW:
1058
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Arrow", NULL);
1059
		break;
1060

1061
	case SHEET_OBJECT_LINE:
1062
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Line", NULL);
1063 1064 1065 1066 1067
		break;
	}
	if (cur == NULL)
		return NULL;
	
1068 1069 1070
	xml_set_gnome_canvas_points (cur, "Points", object->bbox_points);
	xml_set_value_int (cur, "Width", sog->width);
	xml_set_value_string (cur, "Color", sog->color);
1071

1072
	return cur;
1073 1074 1075 1076 1077
}

/*
 * Create a Object equivalent to the XML subtree of doc.
 */
1078
static SheetObject *
1079
xml_read_sheet_object (parse_xml_context_t *ctxt, xmlNodePtr tree)
1080 1081
{
	SheetObject *ret;
1082
	SheetObjectFilled *sof;
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099
	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,
1100
			 "xml_read_sheet_object: invalid element type %s, 'Rectangle/Ellipse ...' expected`\n",
1101 1102 1103 1104
			 tree->name);
		return NULL;
	}
	
1105 1106 1107
	color = (char *) xml_value_get (tree, "Color");
	xml_get_coordinates (tree, "Points", &x1, &y1, &x2, &y2);
	xml_get_value_int (tree, "Width", &width);
1108 1109
	if ((type == SHEET_OBJECT_RECTANGLE) ||
	    (type == SHEET_OBJECT_ELLIPSE)){
1110 1111
		fill_color = (char *) xml_value_get (tree, "FillColor");
		xml_get_value_int (tree, "Pattern", &pattern);
1112 1113 1114
		ret = sheet_object_create_filled (
			ctxt->sheet, type,
			x1, y1, x2, y2, fill_color, color, width);
1115
		if (ret != NULL){
1116 1117
			sof = SHEET_OBJECT_FILLED (ret);
			sof->pattern = pattern;
1118 1119
		}
	} else {
1120 1121 1122
		ret = sheet_object_create_line (
			ctxt->sheet, type,
			x1, y1, x2, y2, color, width);
1123
	}
1124
	sheet_object_realize (ret);
1125
	return ret;
1126 1127
}

1128 1129 1130
/*
 * Create an XML subtree of doc equivalent to the given Cell.
 */
1131
static xmlNodePtr
1132
xml_write_cell (parse_xml_context_t *ctxt, Cell *cell)
1133
{
1134
	xmlNodePtr cur;
1135
	char *text;
1136 1137 1138 1139
	int style_id;

	style_id = GPOINTER_TO_INT (g_hash_table_lookup (ctxt->style_table, cell->style));
	g_assert (style_id != 0);
1140
	
1141
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Cell", NULL);
1142 1143 1144
	xml_set_value_int (cur, "Col", cell->col->pos);
	xml_set_value_int (cur, "Row", cell->row->pos);
	xml_set_value_int (cur, "Style", style_id);
1145

1146
	text = cell_get_content (cell);
1147 1148
	xmlNewChild (cur, ctxt->ns, "Content",
		     xmlEncodeEntities(ctxt->doc, text));
1149
	g_free (text);
1150

1151 1152 1153 1154 1155 1156 1157 1158
 	text = cell_get_comment(cell);
 	if (text) {
		xmlNewChild(cur, ctxt->ns, "Comment", 
			    xmlEncodeEntities(ctxt->doc, text));
		g_free(text);
 	}
	

1159
	return cur;
1160 1161 1162 1163 1164
}

/*
 * Create a Cell equivalent to the XML subtree of doc.
 */
1165
static Cell *
1166
xml_read_cell (parse_xml_context_t *ctxt, xmlNodePtr tree)
1167 1168 1169
{
	Style *style;
	Cell *ret;
1170
	xmlNodePtr childs;
1171
	int row = 0, col = 0;
1172
	char *content = NULL;
1173
	char *comment = NULL;
1174 1175 1176
	gboolean style_read = FALSE;
	int style_idx;
	
1177 1178
	if (strcmp (tree->name, "Cell")){
		fprintf (stderr,
1179
		 "xml_read_cell: invalid element type %s, 'Cell' expected`\n",
1180 1181 1182
			 tree->name);
		return NULL;
	}
1183 1184
	xml_get_value_int (tree, "Col", &col);
	xml_get_value_int (tree, "Row", &row);
1185

Morten Welinder's avatar
Morten Welinder committed
1186 1187
	cell_deep_freeze_redraws ();

1188 1189 1190 1191 1192 1193
	ret = sheet_cell_get (ctxt->sheet, col, row);
	if (ret == NULL)
		ret = sheet_cell_new (ctxt->sheet, col, row);
	if (ret == NULL)
		return NULL;

1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222
	/*
	 * New file format includes an index pointer to the Style
	 * Old format includes the Style online
	 */
	if (xml_get_value_int (tree, "Style", &style_idx)){
		Style *s;
		
		style_read = TRUE;
		s = g_hash_table_lookup (ctxt->style_table, GINT_TO_POINTER (style_idx));
		if (s){
			Style *copy;

			/*
			 * The main style is the style we read, but this
			 * style might be incomplete (ie, older formats might
			 * not have full styles.
			 *
			 * so we merge the missing bits from the current cell
			 * style
			 */
			copy = style_duplicate (s);
			style_merge_to (copy, ret->style);
			style_destroy (ret->style);
			ret->style = copy;
		} else {
			printf ("Error: could not find style %d\n", style_idx);
		}
	}
	
1223 1224
	childs = tree->childs;
	while (childs != NULL) {
1225 1226