xml-io.c 55 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
 * $Id$
9 10 11 12 13
 */

#include <config.h>
#include <stdio.h>
#include <gnome.h>
14
#include <locale.h>
15
#include <math.h>
16
#include "gnumeric.h"
17 18
#include "gnome-xml/tree.h"
#include "gnome-xml/parser.h"
Daniel Veillard's avatar
Daniel Veillard committed
19
#include "gnome-xml/parserInternals.h"
20
#include "gnome-xml/xmlmemory.h"
21
#include "color.h"
22
#include "border.h"
23
#include "sheet-object.h"
24
#include "sheet-object-graphic.h"
25
#include "print-info.h"
26
#include "xml-io.h"
27
#include "file.h"
28
#include "workbook.h"
29 30
#include "workbook-view.h"
#include "selection.h"
31
#include "command-context.h"
32 33 34 35

/*
 * A parsing context.
 */
36 37 38 39 40 41
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 */
42
	GHashTable *style_table;/* old style styles compatibility */
43 44
} parse_xml_context_t;

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

/*
 * Get a value for a node either carried as an attibute or as
 * the content of a child.
52 53
 *
 * Returns a g_malloc'ed string.  Caller must free.
54
 */
55
static char *
56
xml_value_get (xmlNodePtr node, const char *name)
57
{
58
	char *ret, *val;
59 60
	xmlNodePtr child;

61 62 63 64
	val = (char *) xmlGetProp (node, name);
	if (val != NULL) {
		ret = g_strdup (val);
		xmlFree (val);
Morten Welinder's avatar
Morten Welinder committed
65
		return ret;
66
	}
67 68 69
	child = node->childs;

	while (child != NULL) {
70 71 72 73
		if (!strcmp (child->name, name)) {
		        /*
			 * !!! Inefficient, but ...
			 */
74 75 76 77 78 79
			val = xmlNodeGetContent(child);
			if (val != NULL) {
				ret = g_strdup (val);
				xmlFree (val);
				return ret;
			}
80
		}
81 82 83 84
		child = child->next;
	}

	return NULL;
85 86
}

87
#if 0
88 89 90 91
/*
 * Get a String value for a node either carried as an attibute or as
 * the content of a child.
 */
92
static String *
93
xml_get_value_string (xmlNodePtr node, const char *name)
94
{
95
	char *val;
96 97
	String *ret;

98 99 100 101 102
	val = xml_value_get (node, name);
	if (val == NULL) return NULL;
        ret = string_get (val);
	g_free (val);
	return ret;
103
}
104
#endif
105

106 107 108 109
/*
 * Get an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
110
static int
111
xml_get_value_int (xmlNodePtr node, const char *name, int *val)
112
{
113
	char *ret;
114
	int i;
115
	int res;
116

117
	ret = xml_value_get (node, name);
118
	if (ret == NULL) return 0;
119
	res = sscanf (ret, "%d", &i);
120 121
	g_free (ret);

122 123
	if (res == 1) {
	        *val = i;
124 125 126
		return 1;
	}
	return 0;
127 128
}

129
#if 0
130 131 132 133
/*
 * Get a float value for a node either carried as an attibute or as
 * the content of a child.
 */
134
static int
135
xml_get_value_float (xmlNodePtr node, const char *name, float *val)
136
{
137 138
	int res;
	char *ret;
139 140
	float f;

141
	ret = xml_value_get (node, name);
142
	if (ret == NULL) return 0;
143
	res = sscanf (ret, "%f", &f);
144 145
	g_free (ret);

146 147
	if (res == 1) {
	        *val = f;
148 149 150
		return 1;
	}
	return 0;
151
}
152
#endif
153 154 155 156 157

/*
 * Get a double value for a node either carried as an attibute or as
 * the content of a child.
 */
158
static int
159
xml_get_value_double (xmlNodePtr node, const char *name, double *val)
160
{
161 162
	int res;
	char *ret;
163

164
	ret = xml_value_get (node, name);
165
	if (ret == NULL) return 0;
166
	res = sscanf (ret, "%lf", val);
167
	g_free (ret);
168

169
	return (res == 1);
170 171
}

172
#if 0
173 174 175
/*
 * Get a set of coodinates for a node, carried as the content of a child.
 */
176
static int
177
xml_get_coordinate (xmlNodePtr node, const char *name, double *x, double *y)
178
{
179 180
	int res;
	char *ret;
181
	float X, Y;
182

183
	ret = xml_value_get (node, name);
184
	if (ret == NULL) return 0;
185
	res = sscanf (ret, "(%lf %lf)", x, y)
186
	g_free (ret);
187 188

	return (res == 2)
189
}
190
#endif
191 192 193 194 195

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

196
static int
197
xml_get_coordinates (xmlNodePtr node, const char *name,
198 199
		   double *x1, double *y1, double *x2, double *y2)
{
200 201
	int res;
	char *ret;
202

203
	ret = xml_value_get (node, name);
204
	if (ret == NULL) return 0;
205
	res = sscanf (ret, "(%lf %lf)(%lf %lf)", x1, y1, x2, y2);
206 207 208
	g_free (ret);

	if (res == 4)
209
		return 1;
210

211
	return 0;
212 213
}

214
#if 0
215 216 217
/*
 * Get a GnomeCanvasPoints for a node, carried as the content of a child.
 */
218
static GnomeCanvasPoints *
219
xml_get_gnome_canvas_points (xmlNodePtr node, const char *name)
220
{
221
	char *val;
222 223 224 225
	GnomeCanvasPoints *ret = NULL;
	int res;
	const char *ptr;
	int index = 0, i;
226
	float coord[20];	/* TODO: must be dynamic !!!! */
227

228
	val = xml_value_get (node, name);
229
	if (val == NULL) return NULL;
230 231 232 233 234
	ptr = val;
	do {
		while ((*ptr) && (*ptr != '('))
			ptr++;
		if (*ptr == 0)
235
			break;
236
		res = sscanf (ptr, "(%lf %lf)", &coord[index], &coord[index + 1]);
237 238 239 240 241
		if (res != 2)
			break;
		index += 2;
		ptr++;
	} while (res > 0);
242
	g_free (val);
243 244 245 246 247 248 249

	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];
250
	return ret;
251
}
252
#endif
253 254 255 256 257

/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
258
static void
259
xml_set_gnome_canvas_points (xmlNodePtr node, const char *name,
260
			     GnomeCanvasPoints *val)
261 262 263
{
	xmlNodePtr child;
	char *str, *base;
264
	char *tstr;
265 266 267 268 269 270
	int i;

	if (val == NULL)
		return;
	if ((val->num_points < 0) || (val->num_points > 5000))
		return;
271
	base = str = g_malloc (val->num_points * 30 * sizeof (char) + 1);
272 273 274
	if (str == NULL)
		return;
	for (i = 0; i < val->num_points; i++){
275 276 277
		sprintf (str, "(%f %f)", val->coords[2 * i],
			 val->coords[2 * i + 1]);
		str += strlen (str);
278
	}
279
	*str = 0;
280 281 282 283 284

	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, base);
285
			g_free (base);
286 287 288 289
			return;
		}
		child = child->next;
	}
290 291 292 293

	tstr = xmlEncodeEntitiesReentrant (node->doc, base);
	xmlNewChild (node, NULL, name, tstr);
	if (tstr) xmlFree (tstr);
294
	g_free (base);
295 296
}

297 298 299 300
/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
301 302
static void
xml_set_value (xmlNodePtr node, const char *name, const char *val)
303
{
304
	char *ret;
305 306 307 308
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
309
		xmlFree (ret);
310 311 312 313 314 315 316 317 318 319 320 321
		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);
322 323
}

324 325 326 327
/*
 * Set a String value for a node either carried as an attibute or as
 * the content of a child.
 */
328 329
static void
xml_set_value_string (xmlNodePtr node, const char *name, String *val)
330
{
331
	char *ret;
332 333 334 335
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
336
		xmlFree (ret);
337 338 339 340 341 342 343 344 345 346 347 348
		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);
349 350
}

351 352 353 354
/*
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
355
static void
356
xml_set_value_int (xmlNodePtr node, const char *name, int val)
357
{
358
	char *ret;
359 360 361 362 363 364
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%d", val);
	ret = xmlGetProp (node, name);
	if (ret != NULL){
365
		xmlFree (ret);
366 367 368 369 370 371 372 373 374 375 376 377
		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);
378 379
}

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

	snprintf (str, 100, "%f", val);
	ret = xmlGetProp (node, name);
	if (ret != NULL){
395
		xmlFree (ret);
396 397 398 399 400 401 402 403 404 405 406 407
		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);
408
}
409
#endif
410 411 412 413 414

/*
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
415
static void
416
xml_set_value_double (xmlNodePtr node, const char *name, double val)
417
{
418
	char *ret;
419
	xmlNodePtr child;
420
	char str[101 + DBL_DIG];
421

422 423 424 425
	if (fabs (val) < 1e9 && fabs (val) > 1e-5)
		snprintf (str, 100 + DBL_DIG, "%.*g", DBL_DIG, val);
	else
		snprintf (str, 100 + DBL_DIG, "%f", val);
426

427 428
	ret = xmlGetProp (node, name);
	if (ret != NULL){
429
		xmlFree (ret);
430 431 432 433 434 435 436 437 438 439 440 441
		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);
442 443
}

444 445 446 447 448
static void
xml_set_print_unit (xmlNodePtr node, const char *name,
		    const PrintUnit * const pu)
{
	xmlNodePtr  child;
Jody Goldberg's avatar
Jody Goldberg committed
449
	char       *txt = "points";
450
	char       *tstr;
451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469

	if (pu == NULL || name == NULL)
		return;

	switch (pu->desired_display) {
	case UNIT_POINTS:
		txt = "points";
		break;
	case UNIT_MILLIMETER:
		txt = "mm";
		break;
	case UNIT_CENTIMETER:
		txt = "cm";
		break;
	case UNIT_INCH:
		txt = "in";
		break;
	}

470 471 472 473
	tstr = xmlEncodeEntitiesReentrant (node->doc, name);
	child = xmlNewChild (node, NULL, "PrintUnit", tstr);
	if (tstr) xmlFree (tstr);

474
	xml_set_value_double (child, "Points", pu->points);
475 476 477 478

	tstr = xmlEncodeEntitiesReentrant (node->doc, txt);
	xml_set_value (child, "PrefUnit", tstr);
	if (tstr) xmlFree (tstr);
479 480 481 482 483 484
}

static void
xml_get_print_unit (xmlNodePtr node, PrintUnit * const pu)
{
	char       *txt;
485

486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
	g_return_if_fail (pu != NULL);
	g_return_if_fail (node != NULL);
	g_return_if_fail (node->childs != NULL);

	xml_get_value_double (node, "Points", &pu->points);
	txt = xml_value_get  (node, "PrefUnit");
	if (txt) {
		if (!g_strcasecmp (txt, "points"))
			pu->desired_display = UNIT_POINTS;
		else if (!g_strcasecmp (txt, "mm"))
			pu->desired_display = UNIT_MILLIMETER;
		else if (!g_strcasecmp (txt, "cm"))
			pu->desired_display = UNIT_CENTIMETER;
		else if (!g_strcasecmp (txt, "in"))
			pu->desired_display = UNIT_INCH;
501
		g_free (txt);
502 503 504
	}
}

505
/*
506
 * Search a child by name, if needed go down the tree to find it.
507
 */
508 509
static xmlNodePtr
xml_search_child (xmlNodePtr node, const char *name)
510 511 512 513 514 515 516 517 518 519 520 521
{
	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){
522
		ret = xml_search_child (child, name);
523 524 525 526 527
		if (ret != NULL)
			return ret;
		child = child->next;
	}
	return NULL;
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 556 557 558 559 560 561 562 563 564 565 566 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
static gboolean
xml_read_range (xmlNodePtr tree, Range *res)
{
	return 
	    xml_get_value_int (tree, "startCol", &res->start.col) &&
	    xml_get_value_int (tree, "startRow", &res->start.row) &&
	    xml_get_value_int (tree, "endCol",   &res->end.col) &&
	    xml_get_value_int (tree, "endRow",   &res->end.row);
}

static void
xml_write_range (xmlNodePtr tree, Range *value)
{
	xml_set_value_int (tree, "startCol", value->start.col);
	xml_set_value_int (tree, "startRow", value->start.row);
	xml_set_value_int (tree, "endCol",   value->end.col);
	xml_set_value_int (tree, "endRow",   value->end.row);
}

static void
xml_read_selection_info (parse_xml_context_t *ctxt, Sheet *sheet, xmlNodePtr tree)
{
	Range r;
	int row, col;
	xmlNodePtr sel, selections = xml_search_child (tree, "Selections");
	if (selections == NULL)
		return;

	sheet_selection_reset_only (sheet);
	for (sel = selections->childs; sel; sel = sel->next) {
		if (xml_read_range (sel, &r))
			sheet_selection_append_range (sheet,
						      r.start.col, r.start.row,
						      r.start.col, r.start.row,
						      r.end.col, r.end.row);
	}

	if (xml_get_value_int (tree, "CursorCol", &col) &&
	    xml_get_value_int (tree, "CursorRow", &row))
		sheet_cursor_set (sheet, col, row, col, row, col, row);
}

static void
xml_write_selection_info (parse_xml_context_t *ctxt, Sheet *sheet, xmlNodePtr tree)
{
	GList *ptr, *copy;
	tree = xmlNewChild (tree, ctxt->ns, "Selections", NULL);

	/* Insert the selections in REVERSE order */
	copy = g_list_copy (sheet->selections);
	ptr = g_list_reverse (copy);
	for (; ptr != NULL ; ptr = ptr->next) {
		SheetSelection *sel = ptr->data;
		xmlNodePtr child = xmlNewChild (tree, ctxt->ns, "Selection", NULL);
		xml_write_range (child, &sel->user);
	}
	g_list_free (copy);

	xml_set_value_int (tree, "CursorCol", sheet->cursor_col);
	xml_set_value_int (tree, "CursorRow", sheet->cursor_row);
}

592 593 594 595 596 597 598 599
/*
 * 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 ...
 */
600
static int
601
xml_get_color_value (xmlNodePtr node, const char *name, StyleColor **color)
602
{
603
	char *ret;
604 605
	int red, green, blue;

606
	ret = xml_value_get (node, name);
607
	if (ret == NULL) return 0;
608
	if (sscanf (ret, "%X:%X:%X", &red, &green, &blue) == 3){
609
		*color = style_color_new (red, green, blue);
610
		g_free (ret);
611 612
		return 1;
	}
613
	g_free (ret);
614
	return 0;
615 616 617 618 619 620
}

/*
 * Set a color value for a node either carried as an attibute or as
 * the content of a child.
 */
621 622
static void
xml_set_color_value (xmlNodePtr node, const char *name, StyleColor *val)
623
{
624
	char *ret;
625 626 627 628 629 630
	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){
631
		xmlFree (ret);
632 633 634 635 636 637 638 639 640 641 642 643
		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);
644
}
645 646 647 648 649

/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
650
 **/
651

652 653
static int
style_is_default_fore (StyleColor *color)
654
{
655 656
	if (!color)
		return TRUE;
657

658 659 660 661
	if (color->color.red == 0 && color->color.green == 0 && color->color.blue == 0)
		return TRUE;
	else
		return FALSE;
662 663
}

664 665
static int
style_is_default_back (StyleColor *color)
666
{
667 668
	if (!color)
		return TRUE;
669

670 671 672 673
	if (color->color.red == 0xffff && color->color.green == 0xffff && color->color.blue == 0xffff)
		return TRUE;
	else
		return FALSE;
674 675 676 677 678
}

/*
 * Create an XML subtree of doc equivalent to the given StyleBorder.
 */
679

Michael Meeks's avatar
Michael Meeks committed
680
static char *StyleSideNames[6] =
681 682 683 684
{
 	"Top",
 	"Bottom",
 	"Left",
Michael Meeks's avatar
Michael Meeks committed
685 686 687
 	"Right",
	"Diagonal",
	"Rev-Diagonal"
Arturo Espinosa's avatar
Arturo Espinosa committed
688
};
689

690
static xmlNodePtr
691 692
xml_write_style_border (parse_xml_context_t *ctxt,
			const MStyle *style)
693 694 695
{
	xmlNodePtr cur;
	xmlNodePtr side;
696
	int        i;
697

698 699 700
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
		MStyleBorder const *border;
		if (mstyle_is_element_set (style, i) &&
701
		    NULL != (border = mstyle_get_border (style, i))) {
Arturo Espinosa's avatar
Arturo Espinosa committed
702
			break;
703
		}
704
	}
705
	if (i > MSTYLE_BORDER_REV_DIAGONAL)
Arturo Espinosa's avatar
Arturo Espinosa committed
706
		return NULL;
707

708
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
709

Michael Meeks's avatar
Michael Meeks committed
710
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
Jody Goldberg's avatar
Jody Goldberg committed
711 712
		MStyleBorder const *border;
		if (mstyle_is_element_set (style, i) &&
713
		    NULL != (border = mstyle_get_border (style, i))) {
Jody Goldberg's avatar
Jody Goldberg committed
714 715
			StyleBorderType t = border->line_type;
			StyleColor *col   = border->color;
716 717
 			side = xmlNewChild (cur, ctxt->ns,
					    StyleSideNames [i - MSTYLE_BORDER_TOP],
718
 					    NULL);
719
 			xml_set_color_value (side, "Color", col);
720
			xml_set_value_int (side, "Style", t);
721
 		}
722 723
	}
	return cur;
724 725 726 727 728
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
729
static void
730
xml_read_style_border (parse_xml_context_t *ctxt, xmlNodePtr tree, MStyle *mstyle)
731 732
{
	xmlNodePtr side;
733
	int        i;
734 735 736

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
737
			 "xml_read_style_border: invalid element type %s, 'StyleBorder' expected`\n",
738 739
			 tree->name);
	}
740

Michael Meeks's avatar
Michael Meeks committed
741
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
742 743
 		if ((side = xml_search_child (tree,
					      StyleSideNames [i - MSTYLE_BORDER_TOP])) != NULL) {
744 745 746 747 748
			int		 t;
			StyleColor      *color = NULL;
			MStyleBorder    *border;
			xml_get_value_int (side, "Style", &t);
			xml_get_color_value (side, "Color", &color);
749
			border = style_border_fetch ((StyleBorderType)t, color,
Michael Meeks's avatar
Michael Meeks committed
750
						     style_border_get_orientation (i));
751 752
			if (border)
				mstyle_set_border (mstyle, i, border);
753
 		}
754
	}
755 756 757 758 759
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
760
static xmlNodePtr
761 762
xml_write_style (parse_xml_context_t *ctxt,
		 MStyle *style)
763
{
764
	xmlNodePtr  cur, child;
765
	char       *tstr;
766

767
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
768

769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784
	if (mstyle_is_element_set (style, MSTYLE_ALIGN_H))
		xml_set_value_int (cur, "HAlign", mstyle_get_align_h (style));
	if (mstyle_is_element_set (style, MSTYLE_ALIGN_V))
		xml_set_value_int (cur, "VAlign", mstyle_get_align_v (style));
	if (mstyle_is_element_set (style, MSTYLE_FIT_IN_CELL))
		xml_set_value_int (cur, "Fit", mstyle_get_fit_in_cell (style));
	if (mstyle_is_element_set (style, MSTYLE_ORIENTATION))
		xml_set_value_int (cur, "Orient", mstyle_get_orientation (style));
	if (mstyle_is_element_set (style, MSTYLE_PATTERN))
		xml_set_value_int (cur, "Shade", mstyle_get_pattern (style));

	if (mstyle_is_element_set (style, MSTYLE_COLOR_FORE)) {
		if (!style_is_default_fore (mstyle_get_color (style, MSTYLE_COLOR_FORE)))
			xml_set_color_value (cur, "Fore", mstyle_get_color (style, MSTYLE_COLOR_FORE));
	}
	if (mstyle_is_element_set (style, MSTYLE_COLOR_BACK)) {
785
		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_BACK)))
786 787
			xml_set_color_value (cur, "Back", mstyle_get_color (style, MSTYLE_COLOR_BACK));
	}
788 789 790 791
	if (mstyle_is_element_set (style, MSTYLE_COLOR_PATTERN)) {
		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_PATTERN)))
			xml_set_color_value (cur, "PatternColor", mstyle_get_color (style, MSTYLE_COLOR_PATTERN));
	}
792 793 794 795 796 797 798 799 800 801 802 803 804
	if (mstyle_is_element_set (style, MSTYLE_FORMAT))
		xml_set_value (cur, "Format", mstyle_get_format (style)->format);

	if (mstyle_is_element_set (style, MSTYLE_FONT_NAME) ||
	    mstyle_is_element_set (style, MSTYLE_FONT_BOLD) ||
	    mstyle_is_element_set (style, MSTYLE_FONT_ITALIC) ||
	    mstyle_is_element_set (style, MSTYLE_FONT_SIZE)) {
		const char *fontname;

		if (mstyle_is_element_set (style, MSTYLE_FONT_NAME))
			fontname = mstyle_get_font_name (style);
		else /* backwards compatibility */
			fontname = "Helvetica";
805

806 807 808 809
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, fontname);
		child = xmlNewChild (cur, ctxt->ns, "Font", tstr);
		if (tstr) xmlFree (tstr);

810 811 812 813 814 815 816 817 818 819 820 821 822 823
		if (mstyle_is_element_set (style, MSTYLE_FONT_SIZE))
			xml_set_value_double (child, "Unit",
					      mstyle_get_font_size (style));
		if (mstyle_is_element_set (style, MSTYLE_FONT_BOLD))
			xml_set_value_int (child, "Bold",
					   mstyle_get_font_bold (style));
		if (mstyle_is_element_set (style, MSTYLE_FONT_ITALIC))
			xml_set_value_int (child, "Italic",
					   mstyle_get_font_italic (style));
	}

	child = xml_write_style_border (ctxt, style);
	if (child)
		xmlAddChild (cur, child);
824 825

	return cur;
826 827
}

828
static xmlNodePtr
829
xml_write_names (parse_xml_context_t *ctxt, GList *names)
830
{
831
	xmlNodePtr  cur;
832
	char       *tstr;
833 834 835 836 837 838 839 840 841 842 843 844 845 846

	if (!names)
		return NULL;

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

	while (names) {
		xmlNodePtr   tmp;
		ExprName    *expr_name = names->data;
		char        *text;

		g_return_val_if_fail (expr_name != NULL, NULL);

		tmp = xmlNewDocNode (ctxt->doc, ctxt->ns, "Name", NULL);
847 848 849
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, expr_name->name->str);
		xmlNewChild (tmp, ctxt->ns, "name", tstr);
		if (tstr) xmlFree (tstr);
850 851

		text = expr_name_value (expr_name);
852 853 854
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
		xmlNewChild (tmp, ctxt->ns, "value", tstr);
		if (tstr) xmlFree (tstr);
855 856 857 858 859
		g_free (text);

		xmlAddChild (cur, tmp);
		names = g_list_next (names);
	}
860

861 862 863 864
	return cur;
}

static void
865 866
xml_read_names (parse_xml_context_t *ctxt, xmlNodePtr tree, Workbook *wb,
		Sheet *sheet)
867 868 869 870 871 872 873 874
{
	xmlNodePtr child;

	g_return_if_fail (ctxt != NULL);
	g_return_if_fail (tree != NULL);

	child = tree->childs;
	while (child) {
875
		char *name  = NULL;
876 877 878 879 880
		if (child->name && !strcmp (child->name, "Name")) {
			xmlNodePtr bits;

			bits = child->childs;
			while (bits) {
881

882 883 884 885 886 887 888 889 890 891 892
				if (!strcmp (bits->name, "name")) {
					name = xmlNodeGetContent (bits);
				} else {
					char     *txt;
					char     *error;
					g_return_if_fail (name != NULL);

					txt = xmlNodeGetContent (bits);
					g_return_if_fail (txt != NULL);
					g_return_if_fail (!strcmp (bits->name, "value"));

893
					if (!expr_name_create (wb, sheet, name, txt, &error))
894 895
						g_warning (error);

Daniel Veillard's avatar
Daniel Veillard committed
896
					xmlFree (txt);
897
				}
898
				bits = bits->next;
899 900 901 902 903 904
			}
		}
		child = child->next;
	}
}

905
static xmlNodePtr
906
xml_write_summary (parse_xml_context_t *ctxt, SummaryInfo *summary_info)
907 908
{
	GList *items, *m;
909
	char *tstr;
910 911
	xmlNodePtr cur;

912
	if (!summary_info)
913 914
		return NULL;

915
	m = items = summary_info_as_list (summary_info);
916 917 918 919 920 921 922 923 924 925 926 927 928

	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);
929 930 931
			tstr = xmlEncodeEntitiesReentrant (ctxt->doc, sit->name);
			xmlNewChild (tmp, ctxt->ns, "name", tstr);
			if (tstr) xmlFree (tstr);
932 933 934

			if (sit->type == SUMMARY_INT) {
				text = g_strdup_printf ("%d", sit->v.i);
935 936 937
				tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
				xmlNewChild (tmp, ctxt->ns, "val-int", tstr);
				if (tstr) xmlFree (tstr);
938 939
			} else {
				text = summary_item_as_text (sit);
940 941 942
				tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
				xmlNewChild (tmp, ctxt->ns, "val-string", tstr);
				if (tstr) xmlFree (tstr);
943 944
			}
			g_free (text);
945
			xmlAddChild (cur, tmp);
946 947 948 949 950 951 952 953
		}
		items = g_list_next (items);
	}
	g_list_free (m);
	return cur;
}

static void
954
xml_read_summary (parse_xml_context_t *ctxt, xmlNodePtr tree, SummaryInfo *summary_info)
955
{
956
	xmlNodePtr child;
957

958 959 960
	g_return_if_fail (ctxt != NULL);
	g_return_if_fail (tree != NULL);
	g_return_if_fail (summary_info != NULL);
961 962 963

	child = tree->childs;
	while (child) {
964
		char *name = NULL;
Miguel de Icaza's avatar
Miguel de Icaza committed
965

966 967 968 969 970 971
		if (child->name && !strcmp (child->name, "Item")) {
			xmlNodePtr bits;

			bits = child->childs;
			while (bits) {
				SummaryItem *sit = NULL;
972

973
				if (!strcmp (bits->name, "name")) {
974
					name = xmlNodeGetContent (bits);
975 976 977 978 979
				} else {
					char *txt;
					g_return_if_fail (name);

					txt = xmlNodeGetContent (bits);
980 981 982 983
					if (txt != NULL){
						if (!strcmp (bits->name, "val-string"))
							sit = summary_item_new_string (name, txt);
						else if (!strcmp (bits->name, "val-int"))
Miguel de Icaza's avatar
Miguel de Icaza committed
984
							sit = summary_item_new_int (name, atoi (txt));
985

986
						if (sit)
987
							summary_info_add (summary_info, sit);
Daniel Veillard's avatar
Daniel Veillard committed
988
						xmlFree (txt);
989
					}
990
				}
991
				bits = bits->next;
992 993
			}
		}
Miguel de Icaza's avatar
Miguel de Icaza committed
994
		if (name){
995
			xmlFree (name);
Miguel de Icaza's avatar
Miguel de Icaza committed
996 997
			name = NULL;
		}
998 999 1000 1001
		child = child->next;
	}
}

Michael Meeks's avatar
Michael Meeks committed
1002 1003 1004 1005 1006 1007 1008 1009 1010 1011
static void
xml_set_print_hf (xmlNodePtr node, const char *name,
		  const PrintHF * const hf)
{
	xmlNodePtr  child;

	if (hf == NULL || name == NULL)
		return;

	child = xmlNewChild (node, NULL, name, NULL);
1012 1013 1014
	xml_set_value (child, "Left", hf->left_format);
	xml_set_value (child, "Middle", hf->middle_format);
	xml_set_value (child, "Right", hf->right_format);
Michael Meeks's avatar
Michael Meeks committed
1015 1016 1017
}

static void
Arturo Espinosa's avatar
Arturo Espinosa committed
1018
xml_get_print_hf (xmlNodePtr node, PrintHF *const hf)
Michael Meeks's avatar
Michael Meeks committed
1019 1020
{
	char *txt;
1021

Michael Meeks's avatar
Michael Meeks committed
1022 1023 1024 1025
	g_return_if_fail (hf != NULL);
	g_return_if_fail (node != NULL);

	txt = xml_value_get (node, "Left");
1026
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
1027 1028
		if (hf->left_format)
			g_free (hf->left_format);
1029
		hf->left_format = txt;
Arturo Espinosa's avatar
Arturo Espinosa committed
1030
	}
1031

Michael Meeks's avatar
Michael Meeks committed
1032
	txt = xml_value_get (node, "Middle");
1033
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
1034 1035
		if (hf->middle_format)
			g_free (hf->middle_format);
1036
		hf->middle_format = txt;
Arturo Espinosa's avatar
Arturo Espinosa committed
1037
	}
Michael Meeks's avatar
Michael Meeks committed
1038 1039

	txt = xml_value_get (node, "Right");
1040
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
1041 1042
		if (hf->right_format)
			g_free (hf->right_format);
1043
		hf->right_format = txt;
Arturo Espinosa's avatar
Arturo Espinosa committed
1044
	}
Michael Meeks's avatar
Michael Meeks committed
1045 1046
}

1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
static xmlNodePtr
xml_write_print_info (parse_xml_context_t *ctxt, PrintInformation *pi)
{
	xmlNodePtr cur, child;

	g_return_val_if_fail (pi != NULL, NULL);

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

	xml_set_print_unit (cur, "top",    &pi->margins.top);
	xml_set_print_unit (cur, "bottom", &pi->margins.bottom);
	xml_set_print_unit (cur, "left",   &pi->margins.left);
	xml_set_print_unit (cur, "right",  &pi->margins.right);
	xml_set_print_unit (cur, "header", &pi->margins.header);
	xml_set_print_unit (cur, "footer", &pi->margins.footer);


	child = xmlNewDocNode (ctxt->doc, ctxt->ns, "vcenter", NULL);
	xml_set_value_int  (child, "value", pi->center_vertically);
	xmlAddChild (cur, child);
	child = xmlNewDocNode (ctxt->doc, ctxt->ns, "hcenter", NULL);
	xml_set_value_int  (child, "value", pi->center_horizontally);
	xmlAddChild (cur, child);

	child = xmlNewDocNode (ctxt->doc, ctxt->ns, "grid", NULL);
	xml_set_value_int  (child, "value",    pi->print_line_divisions);
	xmlAddChild (cur, child);
	child = xmlNewDocNode (ctxt->doc, ctxt->ns, "monochrome", NULL);
	xml_set_value_int  (child, "value",    pi->print_black_and_white);
	xmlAddChild (cur, child);
	child = xmlNewDocNode (ctxt->doc, ctxt->ns, "draft", NULL);
	xml_set_value_int  (child, "value",    pi->print_as_draft);
	xmlAddChild (cur, child);
	child = xmlNewDocNode (ctxt->doc, ctxt->ns, "titles", NULL);
	xml_set_value_int  (child, "value",    pi->print_titles);
	xmlAddChild (cur, child);

Michael Meeks's avatar
Michael Meeks committed
1084 1085 1086 1087
	if (pi->print_order == PRINT_ORDER_DOWN_THEN_RIGHT)
		child = xmlNewDocNode (ctxt->doc, ctxt->ns, "order", "d_then_r");
	else
		child = xmlNewDocNode (ctxt->doc, ctxt->ns, "order", "r_then_d");
1088 1089
	xmlAddChild (cur, child);

Michael Meeks's avatar
Michael Meeks committed
1090 1091 1092 1093
	if (pi->orientation == PRINT_ORIENT_VERTICAL)
		child = xmlNewDocNode (ctxt->doc, ctxt->ns, "orientation", "portrait");
	else
		child = xmlNewDocNode (ctxt->doc, ctxt->ns, "orientation", "landscape");
1094 1095
	xmlAddChild (cur, child);

Michael Meeks's avatar
Michael Meeks committed
1096 1097 1098
	xml_set_print_hf (cur, "Header", pi->header);
	xml_set_print_hf (cur, "Footer", pi->footer);

1099 1100
	child = xmlNewDocNode (ctxt->doc, ctxt->ns, "paper", gnome_paper_name (pi->paper));
	xmlAddChild (cur, child);
1101

1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116
	return cur;
}

static void
xml_read_print_info (parse_xml_context_t *ctxt, xmlNodePtr tree)
{
	xmlNodePtr child;
	PrintInformation *pi;
	int b;

	g_return_if_fail (ctxt != NULL);
	g_return_if_fail (tree != NULL);
	g_return_if_fail (ctxt->sheet != NULL);

	pi = ctxt->sheet->print_info;
1117

1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157
	g_return_if_fail (pi != NULL);

	if ((child = xml_search_child (tree, "top")))
		xml_get_print_unit (child, &pi->margins.top);
	if ((child = xml_search_child (tree, "bottom")))
		xml_get_print_unit (child, &pi->margins.bottom);
	if ((child = xml_search_child (tree, "left")))
		xml_get_print_unit (child, &pi->margins.left);
	if ((child = xml_search_child (tree, "right")))
		xml_get_print_unit (child, &pi->margins.right);
	if ((child = xml_search_child (tree, "header")))
		xml_get_print_unit (child, &pi->margins.header);
	if ((child = xml_search_child (tree, "footer")))
		xml_get_print_unit (child, &pi->margins.footer);

	if ((child = xml_search_child (tree, "vcenter"))) {
		xml_get_value_int  (child, "value", &b);
		pi->center_vertically   = (b == 1);
	}
	if ((child = xml_search_child (tree, "hcenter"))) {
		xml_get_value_int  (child, "value", &b);
		pi->center_horizontally = (b == 1);
	}

	if ((child = xml_search_child (tree, "grid"))) {
		xml_get_value_int  (child, "value",    &b);
		pi->print_line_divisions  = (b == 1);
	}
	if ((child = xml_search_child (tree, "monochrome"))) {
		xml_get_value_int  (child, "value", &b);
		pi->print_black_and_white = (b == 1);
	}
	if ((child = xml_search_child (tree, "draft"))) {
		xml_get_value_int  (child, "value",   &b);
		pi->print_as_draft        = (b == 1);
	}
	if ((child = xml_search_child (tree, "titles"))) {
		xml_get_value_int  (child, "value",  &b);
		pi->print_titles          = (b == 1);
	}
Morten Welinder's avatar