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

#include <config.h>
#include <stdio.h>
#include <gnome.h>
12
#include <locale.h>
13
#include <math.h>
14
#include <limits.h>
15
#include "gnumeric.h"
16
#include "gnome-xml/parser.h"
Daniel Veillard's avatar
Daniel Veillard committed
17
#include "gnome-xml/parserInternals.h"
18
#include "gnome-xml/xmlmemory.h"
19
#include "color.h"
20
#include "border.h"
21
#include "sheet-object.h"
22
#include "sheet-object-graphic.h"
23
#include "print-info.h"
24
#include "xml-io.h"
Jody Goldberg's avatar
Jody Goldberg committed
25
#include "rendered-value.h"
26
#include "file.h"
27
#include "expr.h"
28
#include "expr-name.h"
29
#include "cell.h"
30
#include "cell-comment.h"
31
#include "workbook.h"
32 33
#include "workbook-view.h"
#include "selection.h"
34
#include "clipboard.h"
35
#include "command-context.h"
36 37 38 39

/*
 * A parsing context.
 */
40 41 42 43 44
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 */
Jody Goldberg's avatar
Jody Goldberg committed
45
	Workbook *wb;		/* the associated workbook */
46
	GHashTable *style_table;/* old style styles compatibility */
Jody Goldberg's avatar
Jody Goldberg committed
47 48 49 50 51
	GHashTable *expr_map;	/*
				 * Emitted expressions with ref count > 1
				 * When writing this is map from expr pointer -> index
				 * when reading this is a map from index -> expr pointer
				 */
52 53
} parse_xml_context_t;

54 55 56
/*
 * Internal stuff: xml helper functions.
 */
57

58 59 60 61
static void
xml_arg_set (GtkArg *arg, gchar *string)
{
	switch (arg->type) {
JP Rosevear's avatar
JP Rosevear committed
62 63 64 65 66 67 68
	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
69
		if (!strcmp (string, "TRUE"))
JP Rosevear's avatar
JP Rosevear committed
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
			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;
95 96 97 98 99 100 101
	}
}

static char *
xml_arg_get (GtkArg *arg)
{
	switch (arg->type) {
JP Rosevear's avatar
JP Rosevear committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115
	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
116
		return g_strdup_printf("%li", GTK_VALUE_LONG (*arg));
JP Rosevear's avatar
JP Rosevear committed
117
	case GTK_TYPE_ULONG:
Jon Kåre Hellan's avatar
Jon Kåre Hellan committed
118
		return g_strdup_printf("%lu", GTK_VALUE_ULONG (*arg));
JP Rosevear's avatar
JP Rosevear committed
119 120 121 122 123 124
	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));
125 126 127 128
	}

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

130 131 132
/*
 * Get a value for a node either carried as an attibute or as
 * the content of a child.
133 134
 *
 * Returns a g_malloc'ed string.  Caller must free.
135
 */
136
static char *
137
xml_value_get (xmlNodePtr node, const char *name)
138
{
139
	char *ret, *val;
140 141
	xmlNodePtr child;

142 143 144 145
	val = (char *) xmlGetProp (node, name);
	if (val != NULL) {
		ret = g_strdup (val);
		xmlFree (val);
Morten Welinder's avatar
Morten Welinder committed
146
		return ret;
147
	}
148 149 150
	child = node->childs;

	while (child != NULL) {
151 152 153 154
		if (!strcmp (child->name, name)) {
		        /*
			 * !!! Inefficient, but ...
			 */
155 156 157 158 159 160
			val = xmlNodeGetContent(child);
			if (val != NULL) {
				ret = g_strdup (val);
				xmlFree (val);
				return ret;
			}
161
		}
162 163 164 165
		child = child->next;
	}

	return NULL;
166 167
}

168 169 170 171
/*
 * Get a String value for a node either carried as an attibute or as
 * the content of a child.
 */
172
static String *
173
xml_get_value_string (xmlNodePtr node, const char *name)
174
{
175
	char *val;
176 177
	String *ret;

178 179 180 181 182
	val = xml_value_get (node, name);
	if (val == NULL) return NULL;
        ret = string_get (val);
	g_free (val);
	return ret;
183 184
}

185 186 187 188
/*
 * Get an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
189
static gboolean
190
xml_get_value_int (xmlNodePtr node, const char *name, int *val)
191
{
192
	char *ret;
193
	int i;
194
	int res;
195

196
	ret = xml_value_get (node, name);
197
	if (ret == NULL) return 0;
198
	res = sscanf (ret, "%d", &i);
199 200
	g_free (ret);

201 202
	if (res == 1) {
	        *val = i;
203
		return TRUE;
204
	}
205
	return FALSE;
206 207
}

208
#if 0
209 210 211 212
/*
 * Get a float value for a node either carried as an attibute or as
 * the content of a child.
 */
213
static int
214
xml_get_value_float (xmlNodePtr node, const char *name, float *val)
215
{
216 217
	int res;
	char *ret;
218 219
	float f;

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

225 226
	if (res == 1) {
	        *val = f;
227 228 229
		return 1;
	}
	return 0;
230
}
231
#endif
232 233 234 235 236

/*
 * Get a double value for a node either carried as an attibute or as
 * the content of a child.
 */
237
static int
238
xml_get_value_double (xmlNodePtr node, const char *name, double *val)
239
{
240 241
	int res;
	char *ret;
242

243
	ret = xml_value_get (node, name);
244
	if (ret == NULL) return 0;
245
	res = sscanf (ret, "%lf", val);
246
	g_free (ret);
247

248
	return (res == 1);
249 250
}

251
#if 0
252 253 254
/*
 * Get a set of coodinates for a node, carried as the content of a child.
 */
255
static int
256
xml_get_coordinate (xmlNodePtr node, const char *name, double *x, double *y)
257
{
258 259
	int res;
	char *ret;
260

261
	ret = xml_value_get (node, name);
262
	if (ret == NULL) return 0;
263
	res = sscanf (ret, "(%lf %lf)", x, y)
264
	g_free (ret);
265 266

	return (res == 2)
267
}
268
#endif
269 270 271 272 273

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

274
static int
275
xml_get_coordinates (xmlNodePtr node, const char *name,
276 277
		   double *x1, double *y1, double *x2, double *y2)
{
278 279
	int res;
	char *ret;
280

281
	ret = xml_value_get (node, name);
282
	if (ret == NULL) return 0;
283
	res = sscanf (ret, "(%lf %lf)(%lf %lf)", x1, y1, x2, y2);
284 285 286
	g_free (ret);

	if (res == 4)
287
		return 1;
288

289
	return 0;
290 291
}

292
#if 0
293 294 295
/*
 * Get a GnomeCanvasPoints for a node, carried as the content of a child.
 */
296
static GnomeCanvasPoints *
297
xml_get_gnome_canvas_points (xmlNodePtr node, const char *name)
298
{
299
	char *val;
300 301 302 303
	GnomeCanvasPoints *ret = NULL;
	int res;
	const char *ptr;
	int index = 0, i;
304
	float coord[20];	/* TODO: must be dynamic !!!! */
305

306
	val = xml_value_get (node, name);
307
	if (val == NULL) return NULL;
308 309 310 311 312
	ptr = val;
	do {
		while ((*ptr) && (*ptr != '('))
			ptr++;
		if (*ptr == 0)
313
			break;
314
		THIS_DOES_NOT_LOOK_RIGHT = 1;
315
		res = sscanf (ptr, "(%lf %lf)", &coord[index], &coord[index + 1]);
316 317 318 319 320
		if (res != 2)
			break;
		index += 2;
		ptr++;
	} while (res > 0);
321
	g_free (val);
322 323 324 325 326 327 328

	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];
329
	return ret;
330
}
331
#endif
332 333 334 335 336

/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
337
static void
338
xml_set_gnome_canvas_points (xmlNodePtr node, const char *name,
339
			     const GnomeCanvasPoints *val)
340 341 342
{
	xmlNodePtr child;
	char *str, *base;
343
	char *tstr;
344
	int i;
345
	int width1 = 30 + DBL_DIG;
346 347 348

	if (val == NULL)
		return;
349
	if (val->num_points < 0 || val->num_points > (INT_MAX - 1) / width1)
350
		return;
351
	base = str = g_malloc (val->num_points * width1 + 1);
352 353 354
	if (str == NULL)
		return;
	for (i = 0; i < val->num_points; i++){
355 356 357
		sprintf (str, "(%f %f)", val->coords[2 * i],
			 val->coords[2 * i + 1]);
		str += strlen (str);
358
	}
359
	*str = 0;
360 361 362 363 364

	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, base);
365
			g_free (base);
366 367 368 369
			return;
		}
		child = child->next;
	}
370 371 372 373

	tstr = xmlEncodeEntitiesReentrant (node->doc, base);
	xmlNewChild (node, NULL, name, tstr);
	if (tstr) xmlFree (tstr);
374
	g_free (base);
375 376
}

377 378 379 380
/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
381 382
static void
xml_set_value (xmlNodePtr node, const char *name, const char *val)
383
{
384
	char *ret;
385 386 387 388
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
389
		xmlFree (ret);
390 391 392 393 394 395 396 397 398 399 400 401
		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);
402 403
}

404 405 406 407
/*
 * Set a String value for a node either carried as an attibute or as
 * the content of a child.
 */
408
static void
409
xml_set_value_string (xmlNodePtr node, const char *name, const String *val)
410
{
411
	char *ret;
412 413 414 415
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
416
		xmlFree (ret);
417 418 419 420 421 422 423 424 425 426 427 428
		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);
429 430
}

431 432 433 434
/*
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
435
static void
436
xml_set_value_int (xmlNodePtr node, const char *name, int val)
437
{
438
	char *ret;
439
	xmlNodePtr child;
440
	char str[4 * sizeof (int)];
441

442
	sprintf (str, "%d", val);
443 444
	ret = xmlGetProp (node, name);
	if (ret != NULL){
445
		xmlFree (ret);
446 447 448 449 450 451 452 453 454 455 456 457
		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);
458 459 460 461 462 463
}

/*
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
464
static void
465
xml_set_value_double (xmlNodePtr node, const char *name, double val)
466
{
467
	char *ret;
468
	xmlNodePtr child;
469
	char str[101 + DBL_DIG];
470

471 472 473 474
	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);
475

476 477
	ret = xmlGetProp (node, name);
	if (ret != NULL){
478
		xmlFree (ret);
479 480 481 482 483 484 485 486 487 488 489 490
		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);
491 492
}

493 494 495 496 497
static void
xml_set_print_unit (xmlNodePtr node, const char *name,
		    const PrintUnit * const pu)
{
	xmlNodePtr  child;
Jody Goldberg's avatar
Jody Goldberg committed
498
	char       *txt = "points";
499
	char       *tstr;
500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518

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

519 520 521 522
	tstr = xmlEncodeEntitiesReentrant (node->doc, name);
	child = xmlNewChild (node, NULL, "PrintUnit", tstr);
	if (tstr) xmlFree (tstr);

523
	xml_set_value_double (child, "Points", pu->points);
524 525 526 527

	tstr = xmlEncodeEntitiesReentrant (node->doc, txt);
	xml_set_value (child, "PrefUnit", tstr);
	if (tstr) xmlFree (tstr);
528 529 530 531 532 533
}

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

535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
	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;
550
		g_free (txt);
551 552 553
	}
}

554
/*
555
 * Search a child by name, if needed go down the tree to find it.
556
 */
557 558
static xmlNodePtr
xml_search_child (xmlNodePtr node, const char *name)
559 560 561 562 563 564 565 566 567 568 569 570
{
	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){
571
		ret = xml_search_child (child, name);
572 573 574 575 576
		if (ret != NULL)
			return ret;
		child = child->next;
	}
	return NULL;
577 578
}

579 580 581
static gboolean
xml_read_range (xmlNodePtr tree, Range *res)
{
Jody Goldberg's avatar
Jody Goldberg committed
582
	return
583 584 585 586 587 588 589
	    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
590
xml_write_range (xmlNodePtr tree, const Range *value)
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
{
	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))
610 611 612 613
			sheet_selection_add_range (sheet,
						   r.start.col, r.start.row,
						   r.start.col, r.start.row,
						   r.end.col, r.end.row);
614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636
	}

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

637 638
	xml_set_value_int (tree, "CursorCol", sheet->cursor.edit_pos.col);
	xml_set_value_int (tree, "CursorRow", sheet->cursor.edit_pos.row);
639 640
}

641 642 643 644 645 646 647 648
/*
 * 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 ...
 */
649
static int
650
xml_get_color_value (xmlNodePtr node, const char *name, StyleColor **color)
651
{
652
	char *ret;
653 654
	int red, green, blue;

655
	ret = xml_value_get (node, name);
656
	if (ret == NULL) return 0;
657
	if (sscanf (ret, "%X:%X:%X", &red, &green, &blue) == 3){
658
		*color = style_color_new (red, green, blue);
659
		g_free (ret);
660 661
		return 1;
	}
662
	g_free (ret);
663
	return 0;
664 665 666 667 668 669
}

/*
 * Set a color value for a node either carried as an attibute or as
 * the content of a child.
 */
670 671
static void
xml_set_color_value (xmlNodePtr node, const char *name, StyleColor *val)
672
{
673
	char *ret;
674
	xmlNodePtr child;
675
	char str[4 * sizeof (val->color)];
676

677
	sprintf (str, "%X:%X:%X", val->color.red, val->color.green, val->color.blue);
678 679
	ret = xmlGetProp (node, name);
	if (ret != NULL){
680
		xmlFree (ret);
681 682 683 684 685 686 687 688 689 690 691 692
		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);
693
}
694 695 696 697 698

/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
699
 **/
700
#if 0
701 702
static int
style_is_default_fore (StyleColor *color)
703
{
704 705
	if (!color)
		return TRUE;
706

707 708 709 710
	if (color->color.red == 0 && color->color.green == 0 && color->color.blue == 0)
		return TRUE;
	else
		return FALSE;
711 712
}

713 714
static int
style_is_default_back (StyleColor *color)
715
{
716 717
	if (!color)
		return TRUE;
718

719 720 721 722
	if (color->color.red == 0xffff && color->color.green == 0xffff && color->color.blue == 0xffff)
		return TRUE;
	else
		return FALSE;
723
}
724
#endif
725 726 727 728

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

Michael Meeks's avatar
Michael Meeks committed
730
static char *StyleSideNames[6] =
731 732 733 734
{
 	"Top",
 	"Bottom",
 	"Left",
Michael Meeks's avatar
Michael Meeks committed
735 736 737
 	"Right",
	"Diagonal",
	"Rev-Diagonal"
Arturo Espinosa's avatar
Arturo Espinosa committed
738
};
739

740
static xmlNodePtr
741 742
xml_write_style_border (parse_xml_context_t *ctxt,
			const MStyle *style)
743 744 745
{
	xmlNodePtr cur;
	xmlNodePtr side;
746
	int        i;
747

748 749 750
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
		MStyleBorder const *border;
		if (mstyle_is_element_set (style, i) &&
751
		    NULL != (border = mstyle_get_border (style, i))) {
Arturo Espinosa's avatar
Arturo Espinosa committed
752
			break;
753
		}
754
	}
755
	if (i > MSTYLE_BORDER_REV_DIAGONAL)
Arturo Espinosa's avatar
Arturo Espinosa committed
756
		return NULL;
757

758
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
759

Michael Meeks's avatar
Michael Meeks committed
760
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
Jody Goldberg's avatar
Jody Goldberg committed
761 762
		MStyleBorder const *border;
		if (mstyle_is_element_set (style, i) &&
763
		    NULL != (border = mstyle_get_border (style, i))) {
Jody Goldberg's avatar
Jody Goldberg committed
764 765
			StyleBorderType t = border->line_type;
			StyleColor *col   = border->color;
766 767
 			side = xmlNewChild (cur, ctxt->ns,
					    StyleSideNames [i - MSTYLE_BORDER_TOP],
768
 					    NULL);
769
 			xml_set_color_value (side, "Color", col);
770
			xml_set_value_int (side, "Style", t);
771
 		}
772 773
	}
	return cur;
774 775 776 777 778
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
779
static void
780
xml_read_style_border (parse_xml_context_t *ctxt, xmlNodePtr tree, MStyle *mstyle)
781 782
{
	xmlNodePtr side;
783
	int        i;
784 785 786

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
787
			 "xml_read_style_border: invalid element type %s, 'StyleBorder' expected`\n",
788 789
			 tree->name);
	}
790

Michael Meeks's avatar
Michael Meeks committed
791
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
792 793
 		if ((side = xml_search_child (tree,
					      StyleSideNames [i - MSTYLE_BORDER_TOP])) != NULL) {
794 795 796 797 798
			int		 t;
			StyleColor      *color = NULL;
			MStyleBorder    *border;
			xml_get_value_int (side, "Style", &t);
			xml_get_color_value (side, "Color", &color);
799
			border = style_border_fetch ((StyleBorderType)t, color,
Michael Meeks's avatar
Michael Meeks committed
800
						     style_border_get_orientation (i));
801 802
			if (border)
				mstyle_set_border (mstyle, i, border);
803
 		}
804
	}
805 806 807 808 809
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
810
static xmlNodePtr
811 812
xml_write_style (parse_xml_context_t *ctxt,
		 MStyle *style)
813
{
814
	xmlNodePtr  cur, child;
815
	char       *tstr;
816

817
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
818

819 820 821 822 823 824 825 826 827 828 829 830
	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)) {
831
/*		if (!style_is_default_fore (mstyle_get_color (style, MSTYLE_COLOR_FORE)))*/
832 833 834
			xml_set_color_value (cur, "Fore", mstyle_get_color (style, MSTYLE_COLOR_FORE));
	}
	if (mstyle_is_element_set (style, MSTYLE_COLOR_BACK)) {
835
/*		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_BACK)))*/
836 837
			xml_set_color_value (cur, "Back", mstyle_get_color (style, MSTYLE_COLOR_BACK));
	}
838
	if (mstyle_is_element_set (style, MSTYLE_COLOR_PATTERN)) {
839
/*		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_PATTERN)))*/
840 841
			xml_set_color_value (cur, "PatternColor", mstyle_get_color (style, MSTYLE_COLOR_PATTERN));
	}
842 843 844 845 846 847 848 849 850 851 852 853 854
	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";
855

856 857 858 859
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, fontname);
		child = xmlNewChild (cur, ctxt->ns, "Font", tstr);
		if (tstr) xmlFree (tstr);

860 861 862 863 864 865 866 867 868
		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
869 870 871
		if (mstyle_is_element_set (style, MSTYLE_FONT_UNDERLINE))
			xml_set_value_int (child, "Underline",
					   (int)mstyle_get_font_uline (style));
872 873 874
		if (mstyle_is_element_set (style, MSTYLE_FONT_STRIKETHROUGH))
			xml_set_value_int (child, "StrikeThrough",
					   mstyle_get_font_strike (style));
875 876 877 878 879
	}

	child = xml_write_style_border (ctxt, style);
	if (child)
		xmlAddChild (cur, child);
880 881

	return cur;
882 883
}

884
static xmlNodePtr
885
xml_write_names (parse_xml_context_t *ctxt, GList *names)
886
{
887
	xmlNodePtr  cur;
888
	char       *tstr;
889 890 891 892 893 894 895 896

	if (!names)
		return NULL;

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

	while (names) {
		xmlNodePtr   tmp;
897
		NamedExpression    *expr_name = names->data;
898 899 900 901 902
		char        *text;

		g_return_val_if_fail (expr_name != NULL, NULL);

		tmp = xmlNewDocNode (ctxt->doc, ctxt->ns, "Name", NULL);
903 904 905
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, expr_name->name->str);
		xmlNewChild (tmp, ctxt->ns, "name", tstr);
		if (tstr) xmlFree (tstr);
906 907

		text = expr_name_value (expr_name);
908 909 910
		tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
		xmlNewChild (tmp, ctxt->ns, "value", tstr);
		if (tstr) xmlFree (tstr);
911 912 913 914 915
		g_free (text);

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

917 918 919 920
	return cur;
}

static void
921 922
xml_read_names (parse_xml_context_t *ctxt, xmlNodePtr tree, Workbook *wb,
		Sheet *sheet)
923 924 925 926 927 928 929 930
{
	xmlNodePtr child;

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

	child = tree->childs;
	while (child) {
931
		char *name  = NULL;
932 933 934 935 936
		if (child->name && !strcmp (child->name, "Name")) {
			xmlNodePtr bits;

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

938 939 940 941 942 943 944 945 946 947 948
				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"));

949
					if (!expr_name_create (wb, sheet, name, txt, &error))
950 951
						g_warning (error);

Daniel Veillard's avatar
Daniel Veillard committed
952
					xmlFree (txt);
953
				}
954
				bits = bits->next;
955 956 957 958 959 960
			}
		}
		child = child->next;
	}
}

961
static xmlNodePtr
962
xml_write_summary (parse_xml_context_t *ctxt, SummaryInfo *summary_info)
963 964
{
	GList *items, *m;
965
	char *tstr;
966 967
	xmlNodePtr cur;

968
	if (!summary_info)
969 970
		return NULL;

971
	m = items = summary_info_as_list (summary_info);
972 973 974 975 976 977 978 979 980 981 982 983 984

	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);
985 986 987
			tstr = xmlEncodeEntitiesReentrant (ctxt->doc, sit->name);
			xmlNewChild (tmp, ctxt->ns, "name", tstr);
			if (tstr) xmlFree (tstr);
988 989 990

			if (sit->type == SUMMARY_INT) {
				text = g_strdup_printf ("%d", sit->v.i);
991 992 993
				tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
				xmlNewChild (tmp, ctxt->ns, "val-int", tstr);
				if (tstr) xmlFree (tstr);
994 995
			} else {
				text = summary_item_as_text (sit);
996 997 998
				tstr = xmlEncodeEntitiesReentrant (ctxt->doc, text);
				xmlNewChild (tmp, ctxt->ns, "val-string", tstr);
				if (tstr) xmlFree (tstr);
999 1000
			}
			g_free (text);
1001
			xmlAddChild (cur, tmp);
1002 1003 1004 1005 1006 1007 1008 1009
		}
		items = g_list_next (items);
	}
	g_list_free (m);
	return cur;
}

static void
1010
xml_read_summary (parse_xml_context_t *ctxt, xmlNodePtr tree, SummaryInfo *summary_info)
1011
{
1012
	xmlNodePtr child;
1013

1014 1015 1016
	g_return_if_fail (ctxt != NULL);
	g_return_if_fail (tree != NULL);
	g_return_if_fail (summary_info != NULL);
1017 1018 1019

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

1022 1023 1024 1025 1026 1027
		if (child->name && !strcmp (child->name, "Item")) {
			xmlNodePtr bits;

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

1029
				if (!strcmp (bits->name, "name")) {
1030
					name = xmlNodeGetContent (bits);
1031 1032 1033 1034 1035
				} else {
					char *txt;
					g_return_if_fail (name);

					txt = xmlNodeGetContent (bits);
1036 1037 1038 1039
					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
1040
							sit = summary_item_new_int (name, atoi (txt));
1041

1042
						if (sit)
1043
							summary_info_add (summary_info, sit);
Daniel Veillard's avatar
Daniel Veillard committed
1044
						xmlFree (txt);
1045
					}
1046
				}
1047
				bits = bits->next;
1048 1049
			}
		}
Miguel de Icaza's avatar
Miguel de Icaza committed
1050
		if (name){
1051
			xmlFree (name);
Miguel de Icaza's avatar
Miguel de Icaza committed
1052 1053
			name = NULL;
		}
1054 1055 1056 1057
		child = child->next;
	}
}

Michael Meeks's avatar
Michael Meeks committed
1058 1059 1060 1061 1062 1063 1064 1065 1066 1067
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);
1068 1069 1070
	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
1071 1072 1073
}

static void
Arturo Espinosa's avatar
Arturo Espinosa committed
1074
xml_get_print_hf (xmlNodePtr node, PrintHF *const hf)
Michael Meeks's avatar
Michael Meeks committed
1075 1076
{
	char *txt;
1077

Michael Meeks's avatar
Michael Meeks committed
1078 1079 1080 1081
	g_return_if_fail (hf != NULL);
	g_return_if_fail (node != NULL);

	txt = xml_value_get (node, "Left");
1082
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
1083 1084
		if (hf->left_format)
			g_free (hf->left_format);
1085
		hf->left_format = txt;
Arturo Espinosa's avatar
Arturo Espinosa committed
1086
	}
1087

Michael Meeks's avatar
Michael Meeks committed
1088
	txt = xml_value_get (node, "Middle");
1089
	if (txt) {