xml-io.c 85.6 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 "gnumeric.h"
11
#include "gnome-xml/parser.h"
Daniel Veillard's avatar
Daniel Veillard committed
12
#include "gnome-xml/parserInternals.h"
13
#include "gnome-xml/xmlmemory.h"
14
#include "style-color.h"
15
#include "style-border.h"
16
#include "style.h"
Jody Goldberg's avatar
Jody Goldberg committed
17 18
#include "sheet.h"
#include "sheet-style.h"
19
#include "sheet-object.h"
20
#include "sheet-object-cell-comment.h"
21
#include "print-info.h"
22
#include "xml-io.h"
23
#include "file.h"
24
#include "expr.h"
25
#include "expr-name.h"
26
#include "cell.h"
27
#include "value.h"
28
#include "sheet-merge.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"
Chyla Zbigniew's avatar
Chyla Zbigniew committed
38
#include "file.h"
Jody Goldberg's avatar
Jody Goldberg committed
39
#include "str.h"
40
#include "plugin-util.h"
Jody Goldberg's avatar
Jody Goldberg committed
41

42 43 44
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
45
#include <errno.h>
46
#include <zlib.h>
Jody Goldberg's avatar
Jody Goldberg committed
47 48 49 50 51
#include <gal/util/e-xml-utils.h>
#include <gnome.h>
#include <locale.h>
#include <math.h>
#include <limits.h>
52

53 54 55
/* Precision to use when saving point measures. */
#define POINT_SIZE_PRECISION 3

56
/* FIXME - tune the values below */
57 58
#define XML_INPUT_BUFFER_SIZE      4096
#define N_ELEMENTS_BETWEEN_UPDATES 20
59

60 61
static GnumFileOpener *xml_opener = NULL;
static GnumFileSaver  *xml_saver = NULL;
Chyla Zbigniew's avatar
Chyla Zbigniew committed
62

63
GnumFileOpener *
64
gnumeric_xml_get_opener (void)
Chyla Zbigniew's avatar
Chyla Zbigniew committed
65
{
66
	return xml_opener;
Chyla Zbigniew's avatar
Chyla Zbigniew committed
67 68
}

69
GnumFileSaver *
70
gnumeric_xml_get_saver (void)
Chyla Zbigniew's avatar
Chyla Zbigniew committed
71
{
72
	return xml_saver;
Chyla Zbigniew's avatar
Chyla Zbigniew committed
73 74
}

75 76 77
XmlParseContext *
xml_parse_ctx_new_full (xmlDocPtr             doc,
			xmlNsPtr              ns,
Michael Meeks's avatar
Michael Meeks committed
78
			GnumericXMLVersion    version,
79 80 81 82 83 84
			XmlSheetObjectReadFn  read_fn,
			XmlSheetObjectWriteFn write_fn,
			gpointer              user_data)
{
	XmlParseContext *ctxt = g_new0 (XmlParseContext, 1);

Michael Meeks's avatar
Michael Meeks committed
85
	ctxt->version   = version;
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
	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
101
	return xml_parse_ctx_new_full (
Jody Goldberg's avatar
Jody Goldberg committed
102
		doc, ns, GNUM_XML_V6, NULL, NULL, NULL);
103
}
104

105 106 107 108 109 110 111 112
void
xml_parse_ctx_destroy (XmlParseContext *ctxt)
{
	g_return_if_fail (ctxt != NULL);

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

114 115 116
/*
 * Internal stuff: xml helper functions.
 */
117

118 119 120 121
static void
xml_arg_set (GtkArg *arg, gchar *string)
{
	switch (arg->type) {
JP Rosevear's avatar
JP Rosevear committed
122 123 124 125 126 127 128
	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
129
		if (!strcmp (string, "TRUE"))
JP Rosevear's avatar
JP Rosevear committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
			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;
155 156 157 158 159 160 161
	}
}

static char *
xml_arg_get (GtkArg *arg)
{
	switch (arg->type) {
JP Rosevear's avatar
JP Rosevear committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175
	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
176
		return g_strdup_printf("%li", GTK_VALUE_LONG (*arg));
JP Rosevear's avatar
JP Rosevear committed
177
	case GTK_TYPE_ULONG:
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
178
		return g_strdup_printf("%lu", GTK_VALUE_ULONG (*arg));
JP Rosevear's avatar
JP Rosevear committed
179 180 181 182 183 184
	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));
185 186 187 188
	}

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

190 191 192
/*
 * Get a value for a node either carried as an attibute or as
 * the content of a child.
193 194
 *
 * Returns a g_malloc'ed string.  Caller must free.
195
 */
196
static char *
197
xml_value_get (xmlNodePtr node, const char *name)
198
{
199
	char *ret, *val;
200 201
	xmlNodePtr child;

202 203 204 205
	val = (char *) xmlGetProp (node, name);
	if (val != NULL) {
		ret = g_strdup (val);
		xmlFree (val);
Morten Welinder's avatar
Morten Welinder committed
206
		return ret;
207
	}
208
	child = node->xmlChildrenNode;
209 210

	while (child != NULL) {
211 212 213 214
		if (!strcmp (child->name, name)) {
		        /*
			 * !!! Inefficient, but ...
			 */
215 216 217 218 219 220
			val = xmlNodeGetContent(child);
			if (val != NULL) {
				ret = g_strdup (val);
				xmlFree (val);
				return ret;
			}
221
		}
222 223 224 225
		child = child->next;
	}

	return NULL;
226 227
}

228 229 230 231
/*
 * Get a String value for a node either carried as an attibute or as
 * the content of a child.
 */
232
String *
233
xml_get_value_string (xmlNodePtr node, const char *name)
234
{
235
	char *val;
236 237
	String *ret;

238 239 240 241 242
	val = xml_value_get (node, name);
	if (val == NULL) return NULL;
        ret = string_get (val);
	g_free (val);
	return ret;
243 244
}

245 246 247 248
/*
 * Get an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
249
gboolean
250
xml_get_value_int (xmlNodePtr node, const char *name, int *val)
251
{
252
	char *ret;
253
	int i;
254
	int res;
255

256
	ret = xml_value_get (node, name);
257
	if (ret == NULL) return 0;
258
	res = sscanf (ret, "%d", &i);
259 260
	g_free (ret);

261 262
	if (res == 1) {
	        *val = i;
263
		return TRUE;
264
	}
265
	return FALSE;
266 267
}

268
#if 0
269 270 271 272
/*
 * Get a float value for a node either carried as an attibute or as
 * the content of a child.
 */
273
static int
274
xml_get_value_float (xmlNodePtr node, const char *name, float *val)
275
{
276 277
	int res;
	char *ret;
278 279
	float f;

280
	ret = xml_value_get (node, name);
281
	if (ret == NULL) return 0;
282
	res = sscanf (ret, "%f", &f);
283 284
	g_free (ret);

285 286
	if (res == 1) {
	        *val = f;
287 288 289
		return 1;
	}
	return 0;
290
}
291
#endif
292 293 294 295 296

/*
 * Get a double value for a node either carried as an attibute or as
 * the content of a child.
 */
297
static int
298
xml_get_value_double (xmlNodePtr node, const char *name, double *val)
299
{
300 301
	int res;
	char *ret;
302

303
	ret = xml_value_get (node, name);
304
	if (ret == NULL) return 0;
305
	res = sscanf (ret, "%lf", val);
306
	g_free (ret);
307

308
	return (res == 1);
309 310 311 312 313 314
}

/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
315 316
void
xml_set_value_cstr (xmlNodePtr node, const char *name, const char *val)
317
{
318
	char *ret;
319 320 321 322
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
323
		xmlFree (ret);
324 325 326
		xmlSetProp (node, name, val);
		return;
	}
327
	child = node->xmlChildrenNode;
328 329 330 331 332 333 334 335
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, val);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, val);
336 337
}

338 339 340 341
/*
 * Set a String value for a node either carried as an attibute or as
 * the content of a child.
 */
342
void
343
xml_set_value_string (xmlNodePtr node, const char *name, const String *val)
344
{
345
	char *ret;
346 347 348
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
349
	if (ret != NULL) {
350
		xmlFree (ret);
351 352 353
		xmlSetProp (node, name, val->str);
		return;
	}
354
	child = node->xmlChildrenNode;
355 356 357 358 359 360 361 362
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, val->str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, val->str);
363 364
}

365 366 367 368
/*
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
369
void
370
xml_set_value_int (xmlNodePtr node, const char *name, int val)
371
{
372
	char *ret;
373
	xmlNodePtr child;
374
	char str[4 * sizeof (int)];
375

376
	sprintf (str, "%d", val);
377 378
	ret = xmlGetProp (node, name);
	if (ret != NULL){
379
		xmlFree (ret);
380 381 382
		xmlSetProp (node, name, str);
		return;
	}
383
	child = node->xmlChildrenNode;
384 385 386 387 388 389 390 391
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
392 393 394 395 396 397
}

/*
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
398
static void
399 400
xml_set_value_double (xmlNodePtr node, const char *name, double val,
		      int precision)
401
{
402
	char *ret;
403
	xmlNodePtr child;
404
	char str[101 + DBL_DIG];
405

406 407
	if (precision < 0 || precision > DBL_DIG)
		precision = DBL_DIG;
408

409
	if (fabs (val) < 1e9 && fabs (val) > 1e-5)
410
		snprintf (str, 100 + DBL_DIG, "%.*g", precision, val);
411 412
	else
		snprintf (str, 100 + DBL_DIG, "%f", val);
413

414 415
	ret = xmlGetProp (node, name);
	if (ret != NULL){
416
		xmlFree (ret);
417 418 419
		xmlSetProp (node, name, str);
		return;
	}
420
	child = node->xmlChildrenNode;
421 422 423 424 425 426 427 428
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
429 430
}

431 432 433 434 435 436 437 438 439
/*
 * Set a double value for a node with POINT_SIZE_PRECISION digits precision.
 */
static void
xml_set_value_points (xmlNodePtr node, const char *name, double val)
{
	xml_set_value_double (node, name, val, POINT_SIZE_PRECISION);
}

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

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

466
	child = xmlNewChild (node, NULL, name, NULL);
467

468
	xml_set_value_points (child, "Points", pu->points);
469 470

	tstr = xmlEncodeEntitiesReentrant (node->doc, txt);
471
	xml_set_value_cstr (child, "PrefUnit", tstr);
472
	if (tstr) xmlFree (tstr);
473 474 475 476 477 478
}

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

480 481 482 483 484 485 486 487
	g_return_if_fail (pu != NULL);
	g_return_if_fail (node != 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;
Jody Goldberg's avatar
Doh!  
Jody Goldberg committed
488
		else if (!strcmp (txt, "mm"))
489
			pu->desired_display = UNIT_MILLIMETER;
Jody Goldberg's avatar
Doh!  
Jody Goldberg committed
490
		else if (!strcmp (txt, "cm"))
491
			pu->desired_display = UNIT_CENTIMETER;
Jody Goldberg's avatar
Doh!  
Jody Goldberg committed
492
		else if (!strcmp (txt, "in"))
493
			pu->desired_display = UNIT_INCH;
494
		g_free (txt);
495 496 497
	}
}

498
static gboolean
499
xml_read_range (xmlNodePtr tree, Range *r)
500
{
501 502 503 504 505 506
	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);

507 508
	/* Older versions of gnumeric had some boundary problems */
	range_ensure_sanity (r);
509 510

	return res;
511 512 513
}

static void
514
xml_write_range (xmlNodePtr tree, const Range *value)
515 516 517 518 519 520 521 522
{
	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
523
xml_read_selection_info (XmlParseContext *ctxt, Sheet *sheet, xmlNodePtr tree)
524 525 526
{
	Range r;
	int row, col;
527
	xmlNodePtr sel, selections = e_xml_get_child_by_name (tree, "Selections");
528 529 530
	if (selections == NULL)
		return;

531
	sheet_selection_reset (sheet);
532
	for (sel = selections->xmlChildrenNode; sel; sel = sel->next)
533
		if (xml_read_range (sel, &r))
534 535 536 537
			sheet_selection_add_range (sheet,
						   r.start.col, r.start.row,
						   r.start.col, r.start.row,
						   r.end.col, r.end.row);
538

539 540
	if (xml_get_value_int (selections, "CursorCol", &col) &&
	    xml_get_value_int (selections, "CursorRow", &row))
541
		sheet_set_edit_pos (sheet, col, row);
542 543 544
}

static void
545
xml_write_selection_info (XmlParseContext *ctxt, Sheet const *sheet, xmlNodePtr tree)
546 547 548 549 550 551 552 553
{
	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) {
554
		Range const *r = ptr->data;
555
		xmlNodePtr child = xmlNewChild (tree, ctxt->ns, "Selection", NULL);
556
		xml_write_range (child, r);
557 558 559
	}
	g_list_free (copy);

560 561
	xml_set_value_int (tree, "CursorCol", sheet->edit_pos_real.col);
	xml_set_value_int (tree, "CursorRow", sheet->edit_pos_real.row);
562 563
}

564 565 566 567 568 569 570 571
/*
 * 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 ...
 */
572
static int
573
xml_get_color_value (xmlNodePtr node, const char *name, StyleColor **color)
574
{
575
	char *ret;
576 577
	int red, green, blue;

578
	ret = xml_value_get (node, name);
579
	if (ret == NULL) return 0;
580
	if (sscanf (ret, "%X:%X:%X", &red, &green, &blue) == 3){
581
		*color = style_color_new (red, green, blue);
582
		g_free (ret);
583 584
		return 1;
	}
585
	g_free (ret);
586
	return 0;
587 588 589 590 591 592
}

/*
 * Set a color value for a node either carried as an attibute or as
 * the content of a child.
 */
593 594
static void
xml_set_color_value (xmlNodePtr node, const char *name, StyleColor *val)
595
{
596
	char *ret;
597
	xmlNodePtr child;
598
	char str[4 * sizeof (val->color)];
599

600
	sprintf (str, "%X:%X:%X", val->color.red, val->color.green, val->color.blue);
601 602
	ret = xmlGetProp (node, name);
	if (ret != NULL){
603
		xmlFree (ret);
604 605 606
		xmlSetProp (node, name, str);
		return;
	}
607
	child = node->xmlChildrenNode;
608 609 610 611 612 613 614 615
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, str);
			return;
		}
		child = child->next;
	}
	xmlSetProp (node, name, str);
616
}
617 618 619 620 621

/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
622
 **/
623
#if 0
624 625
static int
style_is_default_fore (StyleColor *color)
626
{
627 628
	if (!color)
		return TRUE;
629

630 631 632 633
	if (color->color.red == 0 && color->color.green == 0 && color->color.blue == 0)
		return TRUE;
	else
		return FALSE;
634 635
}

636 637
static int
style_is_default_back (StyleColor *color)
638
{
639 640
	if (!color)
		return TRUE;
641

642 643 644 645
	if (color->color.red == 0xffff && color->color.green == 0xffff && color->color.blue == 0xffff)
		return TRUE;
	else
		return FALSE;
646
}
647
#endif
648 649 650 651

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

Michael Meeks's avatar
Michael Meeks committed
653
static char *StyleSideNames[6] =
654 655 656 657
{
 	"Top",
 	"Bottom",
 	"Left",
Michael Meeks's avatar
Michael Meeks committed
658 659 660
 	"Right",
	"Diagonal",
	"Rev-Diagonal"
Arturo Espinosa's avatar
Arturo Espinosa committed
661
};
662

663
static xmlNodePtr
664
xml_write_style_border (XmlParseContext *ctxt,
665
			const MStyle *style)
666 667 668
{
	xmlNodePtr cur;
	xmlNodePtr side;
669
	int        i;
670

671
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_DIAGONAL; i++) {
Jody Goldberg's avatar
Jody Goldberg committed
672
		StyleBorder const *border;
673
		if (mstyle_is_element_set (style, i) &&
674
		    NULL != (border = mstyle_get_border (style, i))) {
Arturo Espinosa's avatar
Arturo Espinosa committed
675
			break;
676
		}
677
	}
678
	if (i > MSTYLE_BORDER_DIAGONAL)
Arturo Espinosa's avatar
Arturo Espinosa committed
679
		return NULL;
680

681
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
682

683
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_DIAGONAL; i++) {
Jody Goldberg's avatar
Jody Goldberg committed
684
		StyleBorder const *border;
Jody Goldberg's avatar
Jody Goldberg committed
685
		if (mstyle_is_element_set (style, i) &&
686
		    NULL != (border = mstyle_get_border (style, i))) {
Jody Goldberg's avatar
Jody Goldberg committed
687 688
			StyleBorderType t = border->line_type;
			StyleColor *col   = border->color;
689 690
 			side = xmlNewChild (cur, ctxt->ns,
					    StyleSideNames [i - MSTYLE_BORDER_TOP],
691 692
 					    NULL);
			xml_set_value_int (side, "Style", t);
693 694
			if (t != STYLE_BORDER_NONE)
				xml_set_color_value (side, "Color", col);
695
 		}
696 697
	}
	return cur;
698 699 700 701 702
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
703
static void
704
xml_read_style_border (XmlParseContext *ctxt, xmlNodePtr tree, MStyle *mstyle)
705 706
{
	xmlNodePtr side;
707
	int        i;
708 709 710

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
711
			 "xml_read_style_border: invalid element type %s, 'StyleBorder' expected`\n",
712 713
			 tree->name);
	}
714

715
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_DIAGONAL; i++) {
716
 		if ((side = e_xml_get_child_by_name (tree,
717
					      StyleSideNames [i - MSTYLE_BORDER_TOP])) != NULL) {
718 719
			int		 t;
			StyleColor      *color = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
720
			StyleBorder    *border;
721
			xml_get_value_int (side, "Style", &t);
722 723
			if (t != STYLE_BORDER_NONE)
				xml_get_color_value (side, "Color", &color);
724
			border = style_border_fetch ((StyleBorderType)t, color,
Michael Meeks's avatar
Michael Meeks committed
725
						     style_border_get_orientation (i));
726
			mstyle_set_border (mstyle, i, border);
727
 		}
728
	}
729 730 731 732 733
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
734
xmlNodePtr
735
xml_write_style (XmlParseContext *ctxt,
736
		 MStyle *style)
737
{
738
	xmlNodePtr  cur, child;
739
	char       *tstr;
740

741
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
742

743 744 745 746
	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));
747 748
	if (mstyle_is_element_set (style, MSTYLE_WRAP_TEXT))
		xml_set_value_int (cur, "WrapText", mstyle_get_wrap_text (style));
749 750 751 752
	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));
Jody Goldberg's avatar
Jody Goldberg committed
753 754
	if (mstyle_is_element_set (style, MSTYLE_INDENT))
		xml_set_value_int (cur, "Indent", mstyle_get_indent (style));
755 756

	if (mstyle_is_element_set (style, MSTYLE_COLOR_FORE)) {
757
/*		if (!style_is_default_fore (mstyle_get_color (style, MSTYLE_COLOR_FORE)))*/
758 759 760
			xml_set_color_value (cur, "Fore", mstyle_get_color (style, MSTYLE_COLOR_FORE));
	}
	if (mstyle_is_element_set (style, MSTYLE_COLOR_BACK)) {
761
/*		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_BACK)))*/
762 763
			xml_set_color_value (cur, "Back", mstyle_get_color (style, MSTYLE_COLOR_BACK));
	}
764
	if (mstyle_is_element_set (style, MSTYLE_COLOR_PATTERN)) {
765
/*		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_PATTERN)))*/
766 767
			xml_set_color_value (cur, "PatternColor", mstyle_get_color (style, MSTYLE_COLOR_PATTERN));
	}
Jody Goldberg's avatar
Jody Goldberg committed
768 769
	if (mstyle_is_element_set (style, MSTYLE_FORMAT)) {
		char *fmt = style_format_as_XL (mstyle_get_format (style), FALSE);
770
		xml_set_value_cstr (cur, "Format", fmt);
Jody Goldberg's avatar
Jody Goldberg committed
771 772
		g_free (fmt);
	}
773 774

	if (mstyle_is_element_set (style, MSTYLE_FONT_NAME) ||
775
	    mstyle_is_element_set (style, MSTYLE_FONT_SIZE) ||
776 777
	    mstyle_is_element_set (style, MSTYLE_FONT_BOLD) ||
	    mstyle_is_element_set (style, MSTYLE_FONT_ITALIC) ||
778 779
	    mstyle_is_element_set (style, MSTYLE_FONT_UNDERLINE) ||
	    mstyle_is_element_set (style, MSTYLE_FONT_STRIKETHROUGH)) {
780 781 782 783 784 785
		const char *fontname;

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

787 788 789 790
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, fontname);
		child = xmlNewChild (cur, ctxt->ns, "Font", tstr);
		if (tstr) xmlFree (tstr);

791
		if (mstyle_is_element_set (style, MSTYLE_FONT_SIZE))
792
			xml_set_value_points (child, "Unit",
793 794 795 796 797 798 799
					      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
800 801 802
		if (mstyle_is_element_set (style, MSTYLE_FONT_UNDERLINE))
			xml_set_value_int (child, "Underline",
					   (int)mstyle_get_font_uline (style));
803 804 805
		if (mstyle_is_element_set (style, MSTYLE_FONT_STRIKETHROUGH))
			xml_set_value_int (child, "StrikeThrough",
					   mstyle_get_font_strike (style));
806 807 808 809 810
	}

	child = xml_write_style_border (ctxt, style);
	if (child)
		xmlAddChild (cur, child);
811 812

	return cur;
813 814
}

815
static xmlNodePtr
816
xml_write_names (XmlParseContext *ctxt, GList *names)
817
{
818
	xmlNodePtr  cur;
819
	char       *tstr;
820

821 822 823 824
#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
	   */
825 826
	if (!names)
		return NULL;
827
#endif
828 829 830 831 832

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

	while (names) {
		xmlNodePtr   tmp;
Jody Goldberg's avatar