xml-io.c 76.4 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
#if 0  /* Don't return, We need to have a names node in the worksheet
	   * all the time becasue xml_search_child looks for a node down the
	   * tree and it will find the first "Names" node in the sheet #1
	   */
805 806
	if (!names)
		return NULL;
807
#endif	
808 809 810 811 812

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

	while (names) {
		xmlNodePtr   tmp;
813
		NamedExpression    *expr_name = names->data;
814 815 816 817 818
		char        *text;

		g_return_val_if_fail (expr_name != NULL, NULL);

		tmp = xmlNewDocNode (ctxt->doc, ctxt->ns, "Name", NULL);
819 820 821
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, expr_name->name->str);
		xmlNewChild (tmp, ctxt->ns, "name", tstr);
		if (tstr) xmlFree (tstr);
822 823

		text = expr_name_value (expr_name);
824 825 826
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
		xmlNewChild (tmp, ctxt->ns, "value", tstr);
		if (tstr) xmlFree (tstr);
827 828 829 830 831
		g_free (text);

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

833 834 835 836
	return cur;
}

static void
837
xml_read_names (XmlParseContext *ctxt, xmlNodePtr tree, Workbook *wb,
838
		Sheet *sheet)
839 840 841 842 843 844 845 846
{
	xmlNodePtr child;

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

	child = tree->childs;
	while (child) {
847
		char *name  = NULL;
848 849 850 851 852
		if (child->name && !strcmp (child->name, "Name")) {
			xmlNodePtr bits;

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

854 855 856 857 858 859 860 861 862 863 864
				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"));

865
					if (!expr_name_create (wb, sheet, name, txt, &error))
866 867
						g_warning (error);

Daniel Veillard's avatar
Daniel Veillard committed
868
					xmlFree (txt);
869
				}
870
				bits = bits->next;
871 872 873 874 875 876
			}
		}
		child = child->next;
	}
}

877
static xmlNodePtr
878
xml_write_summary (XmlParseContext *ctxt, SummaryInfo *summary_info)
879 880
{
	GList *items, *m;
881
	char *tstr;
882 883
	xmlNodePtr cur;

884
	if (!summary_info)
885 886
		return NULL;

887
	m = items = summary_info_as_list (summary_info);
888 889 890 891 892 893 894 895 896 897 898 899 900

	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);
901 902 903
			tstr = xmlEncodeEntitiesReentrant (ctxt->doc, sit->name);
			xmlNewChild (tmp, ctxt->ns, "name", tstr);
			if (tstr) xmlFree (tstr);
904 905 906

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

static void
926
xml_read_summary (XmlParseContext *ctxt, xmlNodePtr tree, SummaryInfo *summary_info)
927
{
928
	xmlNodePtr child;
929

930 931 932
	g_return_if_fail (ctxt != NULL);
	g_return_if_fail (tree != NULL);
	g_return_if_fail (summary_info != NULL);
933 934 935

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

938 939 940 941 942 943
		if (child->name && !strcmp (child->name, "Item")) {
			xmlNodePtr bits;

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

945
				if (!strcmp (bits->name, "name")) {
946
					name = xmlNodeGetContent (bits);
947 948 949 950 951
				} else {
					char *txt;
					g_return_if_fail (name);

					txt = xmlNodeGetContent (bits);
952 953 954 955
					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
956
							sit = summary_item_new_int (name, atoi (txt));
957

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

Michael Meeks's avatar
Michael Meeks committed
974 975 976 977 978 979 980 981 982 983
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);
984 985 986
	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
987 988 989
}

static void
Arturo Espinosa's avatar
Arturo Espinosa committed
990
xml_get_print_hf (xmlNodePtr node, PrintHF *const hf)
Michael Meeks's avatar
Michael Meeks committed
991 992
{
	char *txt;
993

Michael Meeks's avatar
Michael Meeks committed
994 995 996 997
	g_return_if_fail (hf != NULL);
	g_return_if_fail (node != NULL);

	txt = xml_value_get (node, "Left");
998
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
999 1000
		if (hf->left_format)
			g_free (hf->left_format);
1001
		hf->left_format = txt;
Arturo Espinosa's avatar
Arturo Espinosa committed
1002
	}
1003

Michael Meeks's avatar
Michael Meeks committed
1004
	txt = xml_value_get (node, "Middle");
1005
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
1006 1007
		if (hf->middle_format)
			g_free (hf->middle_format);
1008
		hf->middle_format = txt;
Arturo Espinosa's avatar
Arturo Espinosa committed
1009
	}
Michael Meeks's avatar
Michael Meeks committed
1010 1011

	txt = xml_value_get (node, "Right");
1012
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
1013 1014
		if (hf->right_format)
			g_free (hf->right_format);
1015
		hf->right_format = txt;
Arturo Espinosa's avatar
Arturo Espinosa committed
1016
	}
Michael Meeks's avatar
Michael Meeks committed
1017 1018
}

1019
static void
1020
xml_write_attribute (XmlParseContext *ctxt, xmlNodePtr attr, GtkArg *arg)
1021 1022 1023
{
	xmlChar *tstr;
	gchar *str;
Jody Goldberg's avatar
Jody Goldberg committed
1024

1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035
	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
1036 1037
		str = xml_arg_get (arg);
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, str);
1038 1039 1040 1041 1042 1043
		xmlNewChild (attr, ctxt->ns, "value", tstr);
		if (tstr) {
			xmlFree (tstr);
		}
		g_free (str);
		break;
Jody Goldberg's avatar
Jody Goldberg committed
1044
	}
1045 1046
}

1047
static xmlNodePtr
1048
xml_write_attributes (XmlParseContext *ctxt, guint n_args, GtkArg *args)
1049 1050 1051
{
	xmlNodePtr cur;
	gint i;
Jody Goldberg's avatar
Jody Goldberg committed
1052

1053 1054 1055
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Attributes", NULL);

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

		xmlNewChild (tmp, ctxt->ns, "name", tstr);
Jody Goldberg's avatar
Jody Goldberg committed
1060
		if (tstr)
1061
			xmlFree (tstr);
Jody Goldberg's avatar
Jody Goldberg committed
1062

Jody Goldberg's avatar
Jody Goldberg committed
1063
		xmlNewChild (tmp, ctxt->ns, "type", NULL);
1064
		xml_set_value_int (tmp, "type", args->type);
1065 1066

		xml_write_attribute (ctxt, tmp, args);
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077

		xmlAddChild (cur, tmp);
	}

	return cur;
}

static void
xml_free_arg_list (GList *list)
{
	while (list) {
Morten Welinder's avatar
Morten Welinder committed
1078 1079 1080 1081
		GtkArg *arg = list->data;
		if (arg) {
			g_free (arg->name);
			gtk_arg_free (arg, FALSE);
1082 1083 1084 1085 1086
		}
		list = list->next;
	}
}

1087
static void
1088
xml_read_attribute (XmlParseContext *ctxt, xmlNodePtr attr, GtkArg *arg)
1089
{
1090
	xmlNodePtr val;