xml-io.c 76.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
 *   Jody Goldberg <jgoldberg@home.com>
8 9 10
 */
#include <config.h>
#include <gnome.h>
11
#include <locale.h>
12
#include <math.h>
13
#include <limits.h>
14
#include "gnumeric.h"
15
#include "gnome-xml/parser.h"
Daniel Veillard's avatar
Daniel Veillard committed
16
#include "gnome-xml/parserInternals.h"
17
#include "gnome-xml/xmlmemory.h"
18
#include "color.h"
19
#include "border.h"
20
#include "sheet-object.h"
21
#include "sheet-object-cell-comment.h"
22
#include "print-info.h"
23
#include "xml-io.h"
Jody Goldberg's avatar
Jody Goldberg committed
24
#include "rendered-value.h"
25
#include "file.h"
26
#include "expr.h"
27
#include "expr-name.h"
28
#include "cell.h"
29 30 31
#include "io-context.h"
#include "command-context.h"
#include "workbook-control.h"
32
#include "workbook-view.h"
33
#include "workbook.h"
34
#include "selection.h"
35
#include "clipboard.h"
Jody Goldberg's avatar
Jody Goldberg committed
36
#include "format.h"
Jody Goldberg's avatar
Jody Goldberg committed
37
#include "ranges.h"
38

39 40 41
XmlParseContext *
xml_parse_ctx_new_full (xmlDocPtr             doc,
			xmlNsPtr              ns,
Michael Meeks's avatar
Michael Meeks committed
42
			GnumericXMLVersion    version,
43 44 45 46 47 48
			XmlSheetObjectReadFn  read_fn,
			XmlSheetObjectWriteFn write_fn,
			gpointer              user_data)
{
	XmlParseContext *ctxt = g_new0 (XmlParseContext, 1);

Michael Meeks's avatar
Michael Meeks committed
49
	ctxt->version   = version;
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	ctxt->doc       = doc;
	ctxt->ns        = ns;
	ctxt->expr_map  = g_hash_table_new (g_direct_hash, g_direct_equal);

	ctxt->write_fn  = write_fn;
	ctxt->read_fn   = read_fn;
	ctxt->user_data = user_data;

	return ctxt;
}

XmlParseContext *
xml_parse_ctx_new (xmlDocPtr doc,
		   xmlNsPtr  ns)
{
Michael Meeks's avatar
Michael Meeks committed
65 66
	return xml_parse_ctx_new_full (
		doc, ns, GNUM_XML_V5, NULL, NULL, NULL);
67
}
68

69 70 71 72 73 74 75 76
void
xml_parse_ctx_destroy (XmlParseContext *ctxt)
{
	g_return_if_fail (ctxt != NULL);

	g_hash_table_destroy (ctxt->expr_map);
	g_free (ctxt);
}
77

78 79 80
/*
 * Internal stuff: xml helper functions.
 */
81

82 83 84 85
static void
xml_arg_set (GtkArg *arg, gchar *string)
{
	switch (arg->type) {
JP Rosevear's avatar
JP Rosevear committed
86 87 88 89 90 91 92
	case GTK_TYPE_CHAR:
		GTK_VALUE_CHAR (*arg) = string[0];
		break;
	case GTK_TYPE_UCHAR:
		GTK_VALUE_UCHAR (*arg) = string[0];
		break;
	case GTK_TYPE_BOOL:
Jody Goldberg's avatar
Jody Goldberg committed
93
		if (!strcmp (string, "TRUE"))
JP Rosevear's avatar
JP Rosevear committed
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
			GTK_VALUE_BOOL (*arg) = TRUE;
		else
			GTK_VALUE_BOOL (*arg) = FALSE;
		break;
	case GTK_TYPE_INT:
		GTK_VALUE_INT (*arg) = atoi (string);
		break;
	case GTK_TYPE_UINT:
		GTK_VALUE_UINT (*arg) = atoi (string);
		break;
	case GTK_TYPE_LONG:
		GTK_VALUE_LONG (*arg) = atol (string);
		break;
	case GTK_TYPE_ULONG:
		GTK_VALUE_ULONG (*arg) = atol (string);
		break;
	case GTK_TYPE_FLOAT:
		GTK_VALUE_FLOAT (*arg) = atof (string);
		break;
	case GTK_TYPE_DOUBLE:
		GTK_VALUE_DOUBLE (*arg) = atof (string);
		break;
	case GTK_TYPE_STRING:
		GTK_VALUE_STRING (*arg) = g_strdup (string);
		break;
119 120 121 122 123 124 125
	}
}

static char *
xml_arg_get (GtkArg *arg)
{
	switch (arg->type) {
JP Rosevear's avatar
JP Rosevear committed
126 127 128 129 130 131 132 133 134 135 136 137 138 139
	case GTK_TYPE_CHAR:
		return g_strdup (&GTK_VALUE_CHAR (*arg));
	case GTK_TYPE_UCHAR:
		return g_strdup (&GTK_VALUE_UCHAR (*arg));
	case GTK_TYPE_BOOL:
		if (GTK_VALUE_BOOL (*arg))
			return g_strdup ("TRUE");
		else
			return g_strdup ("FALSE");
	case GTK_TYPE_INT:
		return g_strdup_printf("%i", GTK_VALUE_INT (*arg));
	case GTK_TYPE_UINT:
		return g_strdup_printf("%u", GTK_VALUE_UINT (*arg));
	case GTK_TYPE_LONG:
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
140
		return g_strdup_printf("%li", GTK_VALUE_LONG (*arg));
JP Rosevear's avatar
JP Rosevear committed
141
	case GTK_TYPE_ULONG:
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
142
		return g_strdup_printf("%lu", GTK_VALUE_ULONG (*arg));
JP Rosevear's avatar
JP Rosevear committed
143 144 145 146 147 148
	case GTK_TYPE_FLOAT:
		return g_strdup_printf("%f", GTK_VALUE_FLOAT (*arg));
	case GTK_TYPE_DOUBLE:
		return g_strdup_printf("%f", GTK_VALUE_DOUBLE (*arg));
	case GTK_TYPE_STRING:
		return g_strdup (GTK_VALUE_STRING (*arg));
149 150 151 152
	}

	return NULL;
}
Jody Goldberg's avatar
Jody Goldberg committed
153

154 155 156
/*
 * Get a value for a node either carried as an attibute or as
 * the content of a child.
157 158
 *
 * Returns a g_malloc'ed string.  Caller must free.
159
 */
160
static char *
161
xml_value_get (xmlNodePtr node, const char *name)
162
{
163
	char *ret, *val;
164 165
	xmlNodePtr child;

166 167 168 169
	val = (char *) xmlGetProp (node, name);
	if (val != NULL) {
		ret = g_strdup (val);
		xmlFree (val);
Morten Welinder's avatar
Morten Welinder committed
170
		return ret;
171
	}
172 173 174
	child = node->childs;

	while (child != NULL) {
175 176 177 178
		if (!strcmp (child->name, name)) {
		        /*
			 * !!! Inefficient, but ...
			 */
179 180 181 182 183 184
			val = xmlNodeGetContent(child);
			if (val != NULL) {
				ret = g_strdup (val);
				xmlFree (val);
				return ret;
			}
185
		}
186 187 188 189
		child = child->next;
	}

	return NULL;
190 191
}

192 193 194 195
/*
 * Get a String value for a node either carried as an attibute or as
 * the content of a child.
 */
196
String *
197
xml_get_value_string (xmlNodePtr node, const char *name)
198
{
199
	char *val;
200 201
	String *ret;

202 203 204 205 206
	val = xml_value_get (node, name);
	if (val == NULL) return NULL;
        ret = string_get (val);
	g_free (val);
	return ret;
207 208
}

209 210 211 212
/*
 * Get an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
213
gboolean
214
xml_get_value_int (xmlNodePtr node, const char *name, int *val)
215
{
216
	char *ret;
217
	int i;
218
	int res;
219

220
	ret = xml_value_get (node, name);
221
	if (ret == NULL) return 0;
222
	res = sscanf (ret, "%d", &i);
223 224
	g_free (ret);

225 226
	if (res == 1) {
	        *val = i;
227
		return TRUE;
228
	}
229
	return FALSE;
230 231
}

232
#if 0
233 234 235 236
/*
 * Get a float value for a node either carried as an attibute or as
 * the content of a child.
 */
237
static int
238
xml_get_value_float (xmlNodePtr node, const char *name, float *val)
239
{
240 241
	int res;
	char *ret;
242 243
	float f;

244
	ret = xml_value_get (node, name);
245
	if (ret == NULL) return 0;
246
	res = sscanf (ret, "%f", &f);
247 248
	g_free (ret);

249 250
	if (res == 1) {
	        *val = f;
251 252 253
		return 1;
	}
	return 0;
254
}
255
#endif
256 257 258 259 260

/*
 * Get a double value for a node either carried as an attibute or as
 * the content of a child.
 */
261
static int
262
xml_get_value_double (xmlNodePtr node, const char *name, double *val)
263
{
264 265
	int res;
	char *ret;
266

267
	ret = xml_value_get (node, name);
268
	if (ret == NULL) return 0;
269
	res = sscanf (ret, "%lf", val);
270
	g_free (ret);
271

272
	return (res == 1);
273 274 275 276 277 278
}

/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
279 280
void
xml_set_value_cstr (xmlNodePtr node, const char *name, const char *val)
281
{
282
	char *ret;
283 284 285 286
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
287
		xmlFree (ret);
288 289 290 291 292 293 294 295 296 297 298 299
		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);
300 301
}

302 303 304 305
/*
 * Set a String value for a node either carried as an attibute or as
 * the content of a child.
 */
306
void
307
xml_set_value_string (xmlNodePtr node, const char *name, const String *val)
308
{
309
	char *ret;
310 311 312
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
313
	if (ret != NULL) {
314
		xmlFree (ret);
315 316 317 318 319 320 321 322 323 324 325 326
		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);
327 328
}

329 330 331 332
/*
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
333
void
334
xml_set_value_int (xmlNodePtr node, const char *name, int val)
335
{
336
	char *ret;
337
	xmlNodePtr child;
338
	char str[4 * sizeof (int)];
339

340
	sprintf (str, "%d", val);
341 342
	ret = xmlGetProp (node, name);
	if (ret != NULL){
343
		xmlFree (ret);
344 345 346 347 348 349 350 351 352 353 354 355
		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);
356 357 358 359 360 361
}

/*
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
362
static void
363
xml_set_value_double (xmlNodePtr node, const char *name, double val)
364
{
365
	char *ret;
366
	xmlNodePtr child;
367
	char str[101 + DBL_DIG];
368

369 370 371 372
	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);
373

374 375
	ret = xmlGetProp (node, name);
	if (ret != NULL){
376
		xmlFree (ret);
377 378 379 380 381 382 383 384 385 386 387 388
		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);
389 390
}

391 392 393 394 395
static void
xml_set_print_unit (xmlNodePtr node, const char *name,
		    const PrintUnit * const pu)
{
	xmlNodePtr  child;
Jody Goldberg's avatar
Jody Goldberg committed
396
	char       *txt = "points";
397
	char       *tstr;
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416

	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;
	}

417 418 419 420
	tstr = xmlEncodeEntitiesReentrant (node->doc, name);
	child = xmlNewChild (node, NULL, "PrintUnit", tstr);
	if (tstr) xmlFree (tstr);

421
	xml_set_value_double (child, "Points", pu->points);
422 423

	tstr = xmlEncodeEntitiesReentrant (node->doc, txt);
424
	xml_set_value_cstr (child, "PrefUnit", tstr);
425
	if (tstr) xmlFree (tstr);
426 427 428 429 430 431
}

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

433 434 435 436 437 438 439 440 441 442 443 444 445 446 447
	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;
448
		g_free (txt);
449 450 451
	}
}

452
/*
453
 * Search a child by name, if needed go down the tree to find it.
454
 */
455
xmlNodePtr
456
xml_search_child (xmlNodePtr node, const char *name)
457 458 459 460 461 462 463 464 465 466 467 468
{
	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){
469
		ret = xml_search_child (child, name);
470 471 472 473 474
		if (ret != NULL)
			return ret;
		child = child->next;
	}
	return NULL;
475 476
}

477
static gboolean
478
xml_read_range (xmlNodePtr tree, Range *r)
479
{
480 481 482 483 484 485 486 487 488 489 490 491
	gboolean res =
	    xml_get_value_int (tree, "startCol", &r->start.col) &&
	    xml_get_value_int (tree, "startRow", &r->start.row) &&
	    xml_get_value_int (tree, "endCol",   &r->end.col) &&
	    xml_get_value_int (tree, "endRow",   &r->end.row);

	/* Add some silent sanity checking to cleanup problems
	 * with older versions of gnumeric that had some boundary problems
	 */
	range_check_sanity (r);

	return res;
492 493 494
}

static void
495
xml_write_range (xmlNodePtr tree, const Range *value)
496 497 498 499 500 501 502 503
{
	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
504
xml_read_selection_info (XmlParseContext *ctxt, Sheet *sheet, xmlNodePtr tree)
505 506 507 508 509 510 511 512 513 514
{
	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))
515 516 517 518
			sheet_selection_add_range (sheet,
						   r.start.col, r.start.row,
						   r.start.col, r.start.row,
						   r.end.col, r.end.row);
519 520 521 522 523 524 525 526
	}

	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
527
xml_write_selection_info (XmlParseContext *ctxt, Sheet *sheet, xmlNodePtr tree)
528 529 530 531 532 533 534 535 536 537 538 539 540 541
{
	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);

542 543
	xml_set_value_int (tree, "CursorCol", sheet->cursor.edit_pos.col);
	xml_set_value_int (tree, "CursorRow", sheet->cursor.edit_pos.row);
544 545
}

546 547 548 549 550 551 552 553
/*
 * 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 ...
 */
554
static int
555
xml_get_color_value (xmlNodePtr node, const char *name, StyleColor **color)
556
{
557
	char *ret;
558 559
	int red, green, blue;

560
	ret = xml_value_get (node, name);
561
	if (ret == NULL) return 0;
562
	if (sscanf (ret, "%X:%X:%X", &red, &green, &blue) == 3){
563
		*color = style_color_new (red, green, blue);
564
		g_free (ret);
565 566
		return 1;
	}
567
	g_free (ret);
568
	return 0;
569 570 571 572 573 574
}

/*
 * Set a color value for a node either carried as an attibute or as
 * the content of a child.
 */
575 576
static void
xml_set_color_value (xmlNodePtr node, const char *name, StyleColor *val)
577
{
578
	char *ret;
579
	xmlNodePtr child;
580
	char str[4 * sizeof (val->color)];
581

582
	sprintf (str, "%X:%X:%X", val->color.red, val->color.green, val->color.blue);
583 584
	ret = xmlGetProp (node, name);
	if (ret != NULL){
585
		xmlFree (ret);
586 587 588 589 590 591 592 593 594 595 596 597
		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);
598
}
599 600 601 602 603

/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
604
 **/
605
#if 0
606 607
static int
style_is_default_fore (StyleColor *color)
608
{
609 610
	if (!color)
		return TRUE;
611

612 613 614 615
	if (color->color.red == 0 && color->color.green == 0 && color->color.blue == 0)
		return TRUE;
	else
		return FALSE;
616 617
}

618 619
static int
style_is_default_back (StyleColor *color)
620
{
621 622
	if (!color)
		return TRUE;
623

624 625 626 627
	if (color->color.red == 0xffff && color->color.green == 0xffff && color->color.blue == 0xffff)
		return TRUE;
	else
		return FALSE;
628
}
629
#endif
630 631 632 633

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

Michael Meeks's avatar
Michael Meeks committed
635
static char *StyleSideNames[6] =
636 637 638 639
{
 	"Top",
 	"Bottom",
 	"Left",
Michael Meeks's avatar
Michael Meeks committed
640 641 642
 	"Right",
	"Diagonal",
	"Rev-Diagonal"
Arturo Espinosa's avatar
Arturo Espinosa committed
643
};
644

645
static xmlNodePtr
646
xml_write_style_border (XmlParseContext *ctxt,
647
			const MStyle *style)
648 649 650
{
	xmlNodePtr cur;
	xmlNodePtr side;
651
	int        i;
652

653 654 655
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
		MStyleBorder const *border;
		if (mstyle_is_element_set (style, i) &&
656
		    NULL != (border = mstyle_get_border (style, i))) {
Arturo Espinosa's avatar
Arturo Espinosa committed
657
			break;
658
		}
659
	}
660
	if (i > MSTYLE_BORDER_REV_DIAGONAL)
Arturo Espinosa's avatar
Arturo Espinosa committed
661
		return NULL;
662

663
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
664

Michael Meeks's avatar
Michael Meeks committed
665
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
Jody Goldberg's avatar
Jody Goldberg committed
666 667
		MStyleBorder const *border;
		if (mstyle_is_element_set (style, i) &&
668
		    NULL != (border = mstyle_get_border (style, i))) {
Jody Goldberg's avatar
Jody Goldberg committed
669 670
			StyleBorderType t = border->line_type;
			StyleColor *col   = border->color;
671 672
 			side = xmlNewChild (cur, ctxt->ns,
					    StyleSideNames [i - MSTYLE_BORDER_TOP],
673 674
 					    NULL);
			xml_set_value_int (side, "Style", t);
675 676
			if (t != STYLE_BORDER_NONE)
				xml_set_color_value (side, "Color", col);
677
 		}
678 679
	}
	return cur;
680 681 682 683 684
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
685
static void
686
xml_read_style_border (XmlParseContext *ctxt, xmlNodePtr tree, MStyle *mstyle)
687 688
{
	xmlNodePtr side;
689
	int        i;
690 691 692

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
693
			 "xml_read_style_border: invalid element type %s, 'StyleBorder' expected`\n",
694 695
			 tree->name);
	}
696

Michael Meeks's avatar
Michael Meeks committed
697
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
698 699
 		if ((side = xml_search_child (tree,
					      StyleSideNames [i - MSTYLE_BORDER_TOP])) != NULL) {
700 701 702 703
			int		 t;
			StyleColor      *color = NULL;
			MStyleBorder    *border;
			xml_get_value_int (side, "Style", &t);
704 705
			if (t != STYLE_BORDER_NONE)
				xml_get_color_value (side, "Color", &color);
706
			border = style_border_fetch ((StyleBorderType)t, color,
Michael Meeks's avatar
Michael Meeks committed
707
						     style_border_get_orientation (i));
708
			mstyle_set_border (mstyle, i, border);
709
 		}
710
	}
711 712 713 714 715
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
716
xmlNodePtr
717
xml_write_style (XmlParseContext *ctxt,
718
		 MStyle *style)
719
{
720
	xmlNodePtr  cur, child;
721
	char       *tstr;
722

723
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
724

725 726 727 728 729 730 731 732 733 734 735 736
	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)) {
737
/*		if (!style_is_default_fore (mstyle_get_color (style, MSTYLE_COLOR_FORE)))*/
738 739 740
			xml_set_color_value (cur, "Fore", mstyle_get_color (style, MSTYLE_COLOR_FORE));
	}
	if (mstyle_is_element_set (style, MSTYLE_COLOR_BACK)) {
741
/*		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_BACK)))*/
742 743
			xml_set_color_value (cur, "Back", mstyle_get_color (style, MSTYLE_COLOR_BACK));
	}
744
	if (mstyle_is_element_set (style, MSTYLE_COLOR_PATTERN)) {
745
/*		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_PATTERN)))*/
746 747
			xml_set_color_value (cur, "PatternColor", mstyle_get_color (style, MSTYLE_COLOR_PATTERN));
	}
Jody Goldberg's avatar
Jody Goldberg committed
748 749
	if (mstyle_is_element_set (style, MSTYLE_FORMAT)) {
		char *fmt = style_format_as_XL (mstyle_get_format (style), FALSE);
750
		xml_set_value_cstr (cur, "Format", fmt);
Jody Goldberg's avatar
Jody Goldberg committed
751 752
		g_free (fmt);
	}
753 754

	if (mstyle_is_element_set (style, MSTYLE_FONT_NAME) ||
755
	    mstyle_is_element_set (style, MSTYLE_FONT_SIZE) ||
756 757
	    mstyle_is_element_set (style, MSTYLE_FONT_BOLD) ||
	    mstyle_is_element_set (style, MSTYLE_FONT_ITALIC) ||
758 759
	    mstyle_is_element_set (style, MSTYLE_FONT_UNDERLINE) ||
	    mstyle_is_element_set (style, MSTYLE_FONT_STRIKETHROUGH)) {
760 761 762 763 764 765
		const char *fontname;

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

767 768 769 770
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, fontname);
		child = xmlNewChild (cur, ctxt->ns, "Font", tstr);
		if (tstr) xmlFree (tstr);

771 772 773 774 775 776 777 778 779
		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));
Jody Goldberg's avatar
Jody Goldberg committed
780 781 782
		if (mstyle_is_element_set (style, MSTYLE_FONT_UNDERLINE))
			xml_set_value_int (child, "Underline",
					   (int)mstyle_get_font_uline (style));
783 784 785
		if (mstyle_is_element_set (style, MSTYLE_FONT_STRIKETHROUGH))
			xml_set_value_int (child, "StrikeThrough",
					   mstyle_get_font_strike (style));
786 787 788 789 790
	}

	child = xml_write_style_border (ctxt, style);
	if (child)
		xmlAddChild (cur, child);
791 792

	return cur;
793 794
}

795
static xmlNodePtr
796
xml_write_names (XmlParseContext *ctxt, GList *names)
797
{
798
	xmlNodePtr  cur;
799
	char       *tstr;
800 801 802 803 804 805 806 807

	if (!names)
		return NULL;

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

	while (names) {
		xmlNodePtr   tmp;
808
		NamedExpression    *expr_name = names->data;
809 810 811 812 813
		char        *text;

		g_return_val_if_fail (expr_name != NULL, NULL);

		tmp = xmlNewDocNode (ctxt->doc, ctxt->ns, "Name", NULL);
814 815 816
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, expr_name->name->str);
		xmlNewChild (tmp, ctxt->ns, "name", tstr);
		if (tstr) xmlFree (tstr);
817 818

		text = expr_name_value (expr_name);
819 820 821
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
		xmlNewChild (tmp, ctxt->ns, "value", tstr);
		if (tstr) xmlFree (tstr);
822 823 824 825 826
		g_free (text);

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

828 829 830 831
	return cur;
}

static void
832
xml_read_names (XmlParseContext *ctxt, xmlNodePtr tree, Workbook *wb,
833
		Sheet *sheet)
834 835 836 837 838 839 840 841
{
	xmlNodePtr child;

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

	child = tree->childs;
	while (child) {
842
		char *name  = NULL;
843 844 845 846 847
		if (child->name && !strcmp (child->name, "Name")) {
			xmlNodePtr bits;

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

849 850 851 852 853 854 855 856 857 858 859
				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"));

860
					if (!expr_name_create (wb, sheet, name, txt, &error))
861 862
						g_warning (error);

Daniel Veillard's avatar
Daniel Veillard committed
863
					xmlFree (txt);
864
				}
865
				bits = bits->next;
866 867 868 869 870 871
			}
		}
		child = child->next;
	}
}

872
static xmlNodePtr
873
xml_write_summary (XmlParseContext *ctxt, SummaryInfo *summary_info)
874 875
{
	GList *items, *m;
876
	char *tstr;
877 878
	xmlNodePtr cur;

879
	if (!summary_info)
880 881
		return NULL;

882
	m = items = summary_info_as_list (summary_info);
883 884 885 886 887 888 889 890 891 892 893 894 895

	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);
896 897 898
			tstr = xmlEncodeEntitiesReentrant (ctxt->doc, sit->name);
			xmlNewChild (tmp, ctxt->ns, "name", tstr);
			if (tstr) xmlFree (tstr);
899 900 901

			if (sit->type == SUMMARY_INT) {
				text = g_strdup_printf ("%d", sit->v.i);
902 903 904
				tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
				xmlNewChild (tmp, ctxt->ns, "val-int", tstr);
				if (tstr) xmlFree (tstr);
905 906
			} else {
				text = summary_item_as_text (sit);
907 908 909
				tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
				xmlNewChild (tmp, ctxt->ns, "val-string", tstr);
				if (tstr) xmlFree (tstr);
910 911
			}
			g_free (text);
912
			xmlAddChild (cur, tmp);
913 914 915 916 917 918 919 920
		}
		items = g_list_next (items);
	}
	g_list_free (m);
	return cur;
}

static void
921
xml_read_summary (XmlParseContext *ctxt, xmlNodePtr tree, SummaryInfo *summary_info)
922
{
923
	xmlNodePtr child;
924

925 926 927
	g_return_if_fail (ctxt != NULL);
	g_return_if_fail (tree != NULL);
	g_return_if_fail (summary_info != NULL);
928 929 930

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

933 934 935 936 937 938
		if (child->name && !strcmp (child->name, "Item")) {
			xmlNodePtr bits;

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

940
				if (!strcmp (bits->name, "name")) {
941
					name = xmlNodeGetContent (bits);
942 943 944 945 946
				} else {
					char *txt;
					g_return_if_fail (name);

					txt = xmlNodeGetContent (bits);
947 948 949 950
					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
951
							sit = summary_item_new_int (name, atoi (txt));
952

953
						if (sit)
954
							summary_info_add (summary_info, sit);
Daniel Veillard's avatar
Daniel Veillard committed
955
						xmlFree (txt);
956
					}
957
				}
958
				bits = bits->next;
959 960
			}
		}
Miguel de Icaza's avatar
Miguel de Icaza committed
961
		if (name){
962
			xmlFree (name);
Miguel de Icaza's avatar
Miguel de Icaza committed
963 964
			name = NULL;
		}
965 966 967 968
		child = child->next;
	}
}

Michael Meeks's avatar
Michael Meeks committed
969 970 971 972 973 974 975 976 977 978
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);
979 980 981
	xml_set_value_cstr (child, "Left", hf->left_format);
	xml_set_value_cstr (child, "Middle", hf->middle_format);
	xml_set_value_cstr (child, "Right", hf->right_format);
Michael Meeks's avatar
Michael Meeks committed
982 983 984
}

static void
Arturo Espinosa's avatar
Arturo Espinosa committed
985
xml_get_print_hf (xmlNodePtr node, PrintHF *const hf)
Michael Meeks's avatar
Michael Meeks committed
986 987
{
	char *txt;
988

Michael Meeks's avatar
Michael Meeks committed
989 990 991 992
	g_return_if_fail (hf != NULL);
	g_return_if_fail (node != NULL);

	txt = xml_value_get (node, "Left");
993
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
994 995
		if (hf->left_format)
			g_free (hf->left_format);
996
		hf->left_format = txt;
Arturo Espinosa's avatar
Arturo Espinosa committed
997
	}
998

Michael Meeks's avatar
Michael Meeks committed
999
	txt = xml_value_get (node, "Middle");
1000
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
1001 1002
		if (hf->middle_format)
			g_free (hf->middle_format);
1003
		hf->middle_format = txt;
Arturo Espinosa's avatar
Arturo Espinosa committed
1004
	}
Michael Meeks's avatar
Michael Meeks committed
1005 1006

	txt = xml_value_get (node, "Right");
1007
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
1008 1009
		if (hf->right_format)
			g_free (hf->right_format);
1010
		hf->right_format = txt;
Arturo Espinosa's avatar
Arturo Espinosa committed
1011
	}
Michael Meeks's avatar
Michael Meeks committed
1012 1013
}

1014
static void
1015
xml_write_attribute (XmlParseContext *ctxt, xmlNodePtr attr, GtkArg *arg)
1016 1017 1018
{
	xmlChar *tstr;
	gchar *str;
Jody Goldberg's avatar
Jody Goldberg committed
1019

1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
	switch (arg->type) {
	case GTK_TYPE_CHAR:
	case GTK_TYPE_UCHAR:
	case GTK_TYPE_BOOL:
	case GTK_TYPE_INT:
	case GTK_TYPE_UINT:
	case GTK_TYPE_LONG:
	case GTK_TYPE_ULONG:
	case GTK_TYPE_FLOAT:
	case GTK_TYPE_DOUBLE:
	case GTK_TYPE_STRING:
Jody Goldberg's avatar
Jody Goldberg committed
1031 1032
		str = xml_arg_get (arg);
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, str);
1033 1034 1035 1036 1037 1038
		xmlNewChild (attr, ctxt->ns, "value", tstr);
		if (tstr) {
			xmlFree (tstr);
		}
		g_free (str);
		break;
Jody Goldberg's avatar
Jody Goldberg committed
1039
	}
1040 1041
}

1042
static xmlNodePtr
1043
xml_write_attributes (XmlParseContext *ctxt, guint n_args, GtkArg *args)
1044 1045 1046
{
	xmlNodePtr cur;
	gint i;
Jody Goldberg's avatar
Jody Goldberg committed
1047

1048 1049 1050
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Attributes", NULL);

	for (i=0; i < n_args; args++, i++) {
Jody Goldberg's avatar
Jody Goldberg committed
1051 1052
		xmlNodePtr tmp = xmlNewDocNode (ctxt->doc, ctxt->ns, "Attribute", NULL);
		xmlChar *tstr = xmlEncodeEntitiesReentrant (ctxt->doc, args->name);
1053 1054

		xmlNewChild (tmp, ctxt->ns, "name", tstr);
Jody Goldberg's avatar
Jody Goldberg committed
1055
		if (tstr)
1056
			xmlFree (tstr);
Jody Goldberg's avatar
Jody Goldberg committed
1057

Jody Goldberg's avatar
Jody Goldberg committed
1058
		xmlNewChild (tmp, ctxt->ns, "type", NULL);
1059
		xml_set_value_int (tmp, "type", args->type);
1060 1061

		xml_write_attribute (ctxt, tmp, args);
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072

		xmlAddChild (cur, tmp);
	}

	return cur;
}

static void
xml_free_arg_list (GList *list)
{
	while (list) {
Morten Welinder's avatar
Morten Welinder committed
1073 1074 1075 1076
		GtkArg *arg = list->data;
		if (arg) {
			g_free (arg->name);
			gtk_arg_free (arg, FALSE);
1077 1078 1079 1080 1081
		}
		list = list->next;
	}
}

1082
static void
1083
xml_read_attribute (XmlParseContext *ctxt, xmlNodePtr attr, GtkArg *arg)
1084
{
1085
	xmlNodePtr val;
1086
	char *value;
Jody Goldberg's avatar
Jody Goldberg committed
1087

1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102
	switch (arg->type) {
	case GTK_TYPE_CHAR: