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

#include <config.h>
#include <stdio.h>
#include <gnome.h>
14
#include <locale.h>
15
#include <math.h>
16
#include "gnumeric.h"
17 18
#include "gnome-xml/tree.h"
#include "gnome-xml/parser.h"
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"
25
#include "file.h"
26 27 28 29

/*
 * A parsing context.
 */
30 31 32 33 34 35
typedef struct {
	xmlDocPtr doc;		/* Xml document */
	xmlNsPtr ns;		/* Main name space */
	xmlNodePtr parent;	/* used only for g_hash_table_foreach callbacks */
	Sheet *sheet;		/* the associated sheet */
	Workbook *wb;		/* the associated sheet */
36
	GHashTable *style_table;/* old style styles compatibility */
37 38
} parse_xml_context_t;

39 40 41
/*
 * Internal stuff: xml helper functions.
 */
42 43 44 45 46

/*
 * Get a value for a node either carried as an attibute or as
 * the content of a child.
 */
47
static char *
48
xml_value_get (xmlNodePtr node, const char *name)
49
{
50
	char *ret;
51 52
	xmlNodePtr child;

53
	ret = (char *) xmlGetProp (node, name);
54
	if (ret != NULL)
Morten Welinder's avatar
Morten Welinder committed
55
		return ret;
56 57 58
	child = node->childs;

	while (child != NULL) {
59 60 61 62 63 64 65 66
		if (!strcmp (child->name, name)) {
		        /*
			 * !!! Inefficient, but ...
			 */
			ret = xmlNodeGetContent(child);
			if (ret != NULL)
			    return (ret);
		}
67 68 69 70
		child = child->next;
	}

	return NULL;
71 72
}

73
#if 0
74 75 76 77
/*
 * Get a String value for a node either carried as an attibute or as
 * the content of a child.
 */
78
static String *
79
xml_get_value_string (xmlNodePtr node, const char *name)
80
{
81
	char *val;
82 83
	String *ret;

84
	val = xml_value_get(node, name);
85 86 87 88
	if (val == NULL) return(NULL);
        ret = string_get(val);
	free(val);
	return(ret);
89
}
90
#endif
91

92 93 94 95
/*
 * Get an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
96
static int
97
xml_get_value_int (xmlNodePtr node, const char *name, int *val)
98
{
99
	char *ret;
100
	int i;
101
	int res;
102

103
	ret = xml_value_get (node, name);
104 105 106 107 108 109
	if (ret == NULL) return(0);
	res = sscanf (ret, "%d", &i);
	free(ret);
	
	if (res == 1) {
	        *val = i;
110 111 112
		return 1;
	}
	return 0;
113 114
}

115
#if 0
116 117 118 119
/*
 * Get a float value for a node either carried as an attibute or as
 * the content of a child.
 */
120
static int
121
xml_get_value_float (xmlNodePtr node, const char *name, float *val)
122
{
123 124
	int res;
	char *ret;
125 126
	float f;

127
	ret = xml_value_get (node, name);
128 129 130 131 132 133
	if (ret == NULL) return(0);
	res = sscanf (ret, "%f", &f);
	free(ret);
	
	if (res == 1) {
	        *val = f;
134 135 136
		return 1;
	}
	return 0;
137
}
138
#endif
139 140 141 142 143

/*
 * Get a double value for a node either carried as an attibute or as
 * the content of a child.
 */
144
static int
145
xml_get_value_double (xmlNodePtr node, const char *name, double *val)
146
{
147 148
	int res;
	char *ret;
149

150
	ret = xml_value_get (node, name);
151
	if (ret == NULL) return(0);
152
	res = sscanf (ret, "%lf", val);
153
	free(ret);
154

155
	return (res == 1);
156 157
}

158
#if 0
159 160 161
/*
 * Get a set of coodinates for a node, carried as the content of a child.
 */
162
static int
163
xml_get_coordinate (xmlNodePtr node, const char *name, double *x, double *y)
164
{
165 166
	int res;
	char *ret;
167
	float X, Y;
168

169
	ret = xml_value_get (node, name);
170
	if (ret == NULL) return(0);
171
	res = sscanf (ret, "(%lf %lf)", x, y)
172
	free(ret);
173 174

	return (res == 2)
175
}
176
#endif
177 178 179 180 181

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

182
static int
183
xml_get_coordinates (xmlNodePtr node, const char *name,
184 185
		   double *x1, double *y1, double *x2, double *y2)
{
186 187
	int res;
	char *ret;
188

189
	ret = xml_value_get (node, name);
190
	if (ret == NULL) return(0);
191
	res = sscanf (ret, "(%lf %lf)(%lf %lf)", x1, y1, x2, y2);
192 193
	free(ret);
	
194
	if (res == 4) 
195
		return 1;
196

197
	return 0;
198 199
}

200
#if 0
201 202 203
/*
 * Get a GnomeCanvasPoints for a node, carried as the content of a child.
 */
204
static GnomeCanvasPoints *
205
xml_get_gnome_canvas_points (xmlNodePtr node, const char *name)
206
{
207
	char *val;
208 209 210 211
	GnomeCanvasPoints *ret = NULL;
	int res;
	const char *ptr;
	int index = 0, i;
212
	float coord[20];	/* TODO: must be dynamic !!!! */
213

214
	val = xml_value_get (node, name);
215
	if (val == NULL) return(NULL);
216 217 218 219 220
	ptr = val;
	do {
		while ((*ptr) && (*ptr != '('))
			ptr++;
		if (*ptr == 0)
221
			break;
222
		res = sscanf (ptr, "(%lf %lf)", &coord[index], &coord[index + 1]);
223 224 225 226 227 228 229 230 231 232 233 234 235
		if (res != 2)
			break;
		index += 2;
		ptr++;
	} while (res > 0);
	free(val);

	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];
236
	return ret;
237
}
238
#endif
239 240 241 242 243

/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
244
static void
245
xml_set_gnome_canvas_points (xmlNodePtr node, const char *name,
246
			     GnomeCanvasPoints *val)
247 248 249 250 251 252 253 254 255
{
	xmlNodePtr child;
	char *str, *base;
	int i;

	if (val == NULL)
		return;
	if ((val->num_points < 0) || (val->num_points > 5000))
		return;
256
	base = str = g_malloc (val->num_points * 30 * sizeof (char) + 1);
257 258 259
	if (str == NULL)
		return;
	for (i = 0; i < val->num_points; i++){
260 261 262
		sprintf (str, "(%f %f)", val->coords[2 * i],
			 val->coords[2 * i + 1]);
		str += strlen (str);
263
	}
264
	*str = 0;
265 266 267 268 269 270 271 272 273 274

	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, base);
			free (base);
			return;
		}
		child = child->next;
	}
275
	xmlNewChild (node, NULL, name, xmlEncodeEntities(node->doc, base));
276
	g_free (base);
277 278
}

279 280 281 282
/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
283 284
static void
xml_set_value (xmlNodePtr node, const char *name, const char *val)
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
{
	const char *ret;
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
		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);
303 304
}

305 306 307 308
/*
 * Set a String value for a node either carried as an attibute or as
 * the content of a child.
 */
309 310
static void
xml_set_value_string (xmlNodePtr node, const char *name, String *val)
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
{
	const char *ret;
	xmlNodePtr child;

	ret = xmlGetProp (node, name);
	if (ret != NULL){
		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);
329 330
}

331 332 333 334
/*
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
335
static void
336
xml_set_value_int (xmlNodePtr node, const char *name, int val)
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
{
	const char *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%d", val);
	ret = xmlGetProp (node, name);
	if (ret != NULL){
		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);
357 358
}

359
#if 0
360 361 362 363
/*
 * Set a float value for a node either carried as an attibute or as
 * the content of a child.
 */
364
static void
365
xml_set_value_float (xmlNodePtr node, const char *name, float val)
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385
{
	const char *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%f", val);
	ret = xmlGetProp (node, name);
	if (ret != NULL){
		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);
386
}
387
#endif
388 389 390 391 392

/*
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
393
static void
394
xml_set_value_double (xmlNodePtr node, const char *name, double val)
395 396 397
{
	const char *ret;
	xmlNodePtr child;
398
	char str[101 + DBL_DIG];
399

400 401 402 403
	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);
404 405 406 407 408 409 410 411 412 413 414 415 416 417
	ret = xmlGetProp (node, name);
	if (ret != NULL){
		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);
418 419
}

420 421 422 423 424
static void
xml_set_print_unit (xmlNodePtr node, const char *name,
		    const PrintUnit * const pu)
{
	xmlNodePtr  child;
Jody Goldberg's avatar
Jody Goldberg committed
425
	char       *txt = "points";
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474

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

	child = xmlNewChild (node, NULL, "PrintUnit",
			     xmlEncodeEntities(node->doc, name));
	xml_set_value_double (child, "Points", pu->points);
	xml_set_value (child, "PrefUnit",
		       xmlEncodeEntities (node->doc, txt));
}

static void
xml_get_print_unit (xmlNodePtr node, PrintUnit * const pu)
{
	char       *txt;
	
	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;
	}
}

475 476 477
/*
 * Search a child by name, if needed go down the tree to find it. 
 */
478 479
static xmlNodePtr
xml_search_child (xmlNodePtr node, const char *name)
480 481 482 483 484 485 486 487 488 489 490 491
{
	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){
492
		ret = xml_search_child (child, name);
493 494 495 496 497
		if (ret != NULL)
			return ret;
		child = child->next;
	}
	return NULL;
498 499 500 501 502 503 504 505 506 507
}

/*
 * 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 ...
 */
508
static int
509
xml_get_color_value (xmlNodePtr node, const char *name, StyleColor **color)
510
{
511
	char *ret;
512 513
	int red, green, blue;

514
	ret = xml_value_get (node, name);
515 516
	if (ret == NULL) return(0);
	if (sscanf (ret, "%X:%X:%X", &red, &green, &blue) == 3){
517
		*color = style_color_new (red, green, blue);
518
		free(ret);
519 520 521
		return 1;
	}
	return 0;
522 523 524 525 526 527
}

/*
 * Set a color value for a node either carried as an attibute or as
 * the content of a child.
 */
528 529
static void
xml_set_color_value (xmlNodePtr node, const char *name, StyleColor *val)
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549
{
	const char *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%X:%X:%X", val->color.red, val->color.green, val->color.blue);
	ret = xmlGetProp (node, name);
	if (ret != NULL){
		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);
550
}
551 552 553 554 555

/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
556
 **/
557

558 559
static int
style_is_default_fore (StyleColor *color)
560
{
561 562
	if (!color)
		return TRUE;
563

564 565 566 567
	if (color->color.red == 0 && color->color.green == 0 && color->color.blue == 0)
		return TRUE;
	else
		return FALSE;
568 569
}

570 571
static int
style_is_default_back (StyleColor *color)
572
{
573 574
	if (!color)
		return TRUE;
575

576 577 578 579
	if (color->color.red == 0xffff && color->color.green == 0xffff && color->color.blue == 0xffff)
		return TRUE;
	else
		return FALSE;
580 581 582 583 584
}

/*
 * Create an XML subtree of doc equivalent to the given StyleBorder.
 */
585 586 587
static char *BorderTypes[8] =
{
	"none",
588 589 590 591 592 593 594
 	"thin",
 	"medium",
 	"dashed",
 	"dotted",
 	"thick",
 	"double",
	"hair"
595 596
};

Michael Meeks's avatar
Michael Meeks committed
597
static char *StyleSideNames[6] =
598 599 600 601
{
 	"Top",
 	"Bottom",
 	"Left",
Michael Meeks's avatar
Michael Meeks committed
602 603 604
 	"Right",
	"Diagonal",
	"Rev-Diagonal"
Arturo Espinosa's avatar
Arturo Espinosa committed
605
};
606

607
static xmlNodePtr
608 609
xml_write_style_border (parse_xml_context_t *ctxt,
			const MStyle *style)
610 611 612
{
	xmlNodePtr cur;
	xmlNodePtr side;
613
	int        i;
614
       
615 616
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_RIGHT; i++) {
		if (mstyle_is_element_set (style, i))
Arturo Espinosa's avatar
Arturo Espinosa committed
617
			break;
618 619
	}
	if (i > MSTYLE_BORDER_RIGHT)
Arturo Espinosa's avatar
Arturo Espinosa committed
620
		return NULL;
621

622
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
623
	
Michael Meeks's avatar
Michael Meeks committed
624
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
625 626 627 628 629 630 631
		if (mstyle_is_element_set (style, i)) {
			StyleBorderType t = mstyle_get_border (style, i)->line_type;
			StyleColor *col   = mstyle_get_border (style, i)->color;
 			side = xmlNewChild (cur, ctxt->ns,
					    StyleSideNames [i - MSTYLE_BORDER_TOP],
 					    BorderTypes [t]);
 			xml_set_color_value (side, "Color", col);
632
 		}
633 634
	}
	return cur;
635 636 637 638 639
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
640
static void
641
xml_read_style_border (parse_xml_context_t *ctxt, xmlNodePtr tree, MStyle *mstyle)
642 643
{
	xmlNodePtr side;
644
	int        i;
645 646 647

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
648
			 "xml_read_style_border: invalid element type %s, 'StyleBorder' expected`\n",
649 650
			 tree->name);
	}
651

Michael Meeks's avatar
Michael Meeks committed
652
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
653 654
 		if ((side = xml_search_child (tree,
					      StyleSideNames [i - MSTYLE_BORDER_TOP])) != NULL) {
655 656
			StyleColor   *color = NULL;
			MStyleBorder *border;
657
 			xml_get_color_value (side, "Color", &color);
Michael Meeks's avatar
Michael Meeks committed
658 659
			border = border_fetch (BORDER_NONE, color, 
					       border_get_orientation (i));
660 661
			if (border)
				mstyle_set_border (mstyle, i, border);
662
 		}
663
	}
664 665 666 667 668
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
669
static xmlNodePtr
670 671
xml_write_style (parse_xml_context_t *ctxt,
		 MStyle *style)
672 673 674
{
	xmlNodePtr cur, child;

675
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
676
	
677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
	if (mstyle_is_element_set (style, MSTYLE_ALIGN_H))
		xml_set_value_int (cur, "HAlign", mstyle_get_align_h (style));
	if (mstyle_is_element_set (style, MSTYLE_ALIGN_V))
		xml_set_value_int (cur, "VAlign", mstyle_get_align_v (style));
	if (mstyle_is_element_set (style, MSTYLE_FIT_IN_CELL))
		xml_set_value_int (cur, "Fit", mstyle_get_fit_in_cell (style));
	if (mstyle_is_element_set (style, MSTYLE_ORIENTATION))
		xml_set_value_int (cur, "Orient", mstyle_get_orientation (style));
	if (mstyle_is_element_set (style, MSTYLE_PATTERN))
		xml_set_value_int (cur, "Shade", mstyle_get_pattern (style));

	if (mstyle_is_element_set (style, MSTYLE_COLOR_FORE)) {
		if (!style_is_default_fore (mstyle_get_color (style, MSTYLE_COLOR_FORE)))
			xml_set_color_value (cur, "Fore", mstyle_get_color (style, MSTYLE_COLOR_FORE));
	}
	if (mstyle_is_element_set (style, MSTYLE_COLOR_BACK)) {
693
		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_BACK)))
694 695 696 697 698 699 700 701 702 703 704 705 706 707 708
			xml_set_color_value (cur, "Back", mstyle_get_color (style, MSTYLE_COLOR_BACK));
	}
	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";
709

710
		child = xmlNewChild (cur, ctxt->ns, "Font", 
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
				     xmlEncodeEntities(ctxt->doc, fontname));
		if (mstyle_is_element_set (style, MSTYLE_FONT_SIZE))
			xml_set_value_double (child, "Unit",
					      mstyle_get_font_size (style));
		if (mstyle_is_element_set (style, MSTYLE_FONT_BOLD))
			xml_set_value_int (child, "Bold",
					   mstyle_get_font_bold (style));
		if (mstyle_is_element_set (style, MSTYLE_FONT_ITALIC))
			xml_set_value_int (child, "Italic",
					   mstyle_get_font_italic (style));
	}

	child = xml_write_style_border (ctxt, style);
	if (child)
		xmlAddChild (cur, child);
726 727

	return cur;
728 729
}

730
static xmlNodePtr
731
xml_write_names (parse_xml_context_t *ctxt, GList *names)
732
{
733
	xmlNodePtr  cur;
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758

	if (!names)
		return NULL;

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

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

		g_return_val_if_fail (expr_name != NULL, NULL);

		tmp = xmlNewDocNode (ctxt->doc, ctxt->ns, "Name", NULL);
		xmlNewChild (tmp, ctxt->ns, "name",
			     xmlEncodeEntities (ctxt->doc, expr_name->name->str));

		text = expr_name_value (expr_name);
		xmlNewChild (tmp, ctxt->ns, "value",
			     xmlEncodeEntities (ctxt->doc, text));
		g_free (text);

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

760 761 762 763
	return cur;
}

static void
764 765
xml_read_names (parse_xml_context_t *ctxt, xmlNodePtr tree, Workbook *wb,
		Sheet *sheet)
766 767 768 769 770 771 772 773
{
	xmlNodePtr child;

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

	child = tree->childs;
	while (child) {
774
		char *name  = NULL;
775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
		if (child->name && !strcmp (child->name, "Name")) {
			xmlNodePtr bits;

			bits = child->childs;
			while (bits) {
				
				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"));

792
					if (!expr_name_create (wb, sheet, name, txt, &error))
793 794 795 796
						g_warning (error);

					g_free (txt);
				}
797
				bits = bits->next;
798 799 800 801 802 803
			}
		}
		child = child->next;
	}
}

804
static xmlNodePtr
805
xml_write_summary (parse_xml_context_t *ctxt, SummaryInfo *summary_info)
806 807 808 809
{
	GList *items, *m;
	xmlNodePtr cur;

810
	if (!summary_info)
811 812
		return NULL;

813
	m = items = summary_info_as_list (summary_info);
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843

	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);
			xmlNewChild (tmp, ctxt->ns, "name",
				     xmlEncodeEntities (ctxt->doc, sit->name));

			if (sit->type == SUMMARY_INT) {

				text = g_strdup_printf ("%d", sit->v.i);
				xmlNewChild (tmp, ctxt->ns, "val-int",
					     xmlEncodeEntities (ctxt->doc, text));

			} else {

				text = summary_item_as_text (sit);
				xmlNewChild (tmp, ctxt->ns, "val-string",
					     xmlEncodeEntities (ctxt->doc, text));

			}
			g_free (text);
844
			xmlAddChild (cur, tmp);
845 846 847 848 849 850 851 852
		}
		items = g_list_next (items);
	}
	g_list_free (m);
	return cur;
}

static void
853
xml_read_summary (parse_xml_context_t *ctxt, xmlNodePtr tree, SummaryInfo *summary_info)
854
{
855
	xmlNodePtr child;
856

857 858 859
	g_return_if_fail (ctxt != NULL);
	g_return_if_fail (tree != NULL);
	g_return_if_fail (summary_info != NULL);
860 861 862

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

865 866 867 868 869 870 871 872
		if (child->name && !strcmp (child->name, "Item")) {
			xmlNodePtr bits;

			bits = child->childs;
			while (bits) {
				SummaryItem *sit = NULL;
				
				if (!strcmp (bits->name, "name")) {
873
					name = xmlNodeGetContent (bits);
874 875 876 877 878
				} else {
					char *txt;
					g_return_if_fail (name);

					txt = xmlNodeGetContent (bits);
879 880 881 882
					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
883
							sit = summary_item_new_int (name, atoi (txt));
884 885
						
						if (sit)
886
							summary_info_add (summary_info, sit);
887 888
						g_free (txt);
					}
889
				}
890
				bits = bits->next;
891 892
			}
		}
Miguel de Icaza's avatar
Miguel de Icaza committed
893 894 895 896
		if (name){
			free (name);
			name = NULL;
		}
897 898 899 900
		child = child->next;
	}
}

Michael Meeks's avatar
Michael Meeks committed
901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919
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);
	xml_set_value (child, "Left",
		       xmlEncodeEntities (node->doc, hf->left_format));
	xml_set_value (child, "Middle",
		       xmlEncodeEntities (node->doc, hf->middle_format));
	xml_set_value (child, "Right",
		       xmlEncodeEntities (node->doc, hf->right_format));
}

static void
Arturo Espinosa's avatar
Arturo Espinosa committed
920
xml_get_print_hf (xmlNodePtr node, PrintHF *const hf)
Michael Meeks's avatar
Michael Meeks committed
921 922 923 924 925 926 927
{
	char *txt;
	
	g_return_if_fail (hf != NULL);
	g_return_if_fail (node != NULL);

	txt = xml_value_get (node, "Left");
928
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
929 930 931
		if (hf->left_format)
			g_free (hf->left_format);
		
Michael Meeks's avatar
Michael Meeks committed
932
		hf->left_format = g_strdup (txt);
Miguel de Icaza's avatar
Miguel de Icaza committed
933
		free (txt);
Arturo Espinosa's avatar
Arturo Espinosa committed
934 935
	}
	
Michael Meeks's avatar
Michael Meeks committed
936
	txt = xml_value_get (node, "Middle");
937
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
938 939 940
		if (hf->middle_format)
			g_free (hf->middle_format);
		
Michael Meeks's avatar
Michael Meeks committed
941
		hf->middle_format = g_strdup (txt);
Miguel de Icaza's avatar
Miguel de Icaza committed
942
		free (txt);
Arturo Espinosa's avatar
Arturo Espinosa committed
943
	}
Michael Meeks's avatar
Michael Meeks committed
944 945

	txt = xml_value_get (node, "Right");
946
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
947 948 949
		if (hf->right_format)
			g_free (hf->right_format);
		
Michael Meeks's avatar
Michael Meeks committed
950
		hf->right_format = g_strdup (txt);
Miguel de Icaza's avatar
Miguel de Icaza committed
951
		free (txt);
Arturo Espinosa's avatar
Arturo Espinosa committed
952
	}
Michael Meeks's avatar
Michael Meeks committed
953 954
}

955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
static xmlNodePtr
xml_write_print_info (parse_xml_context_t *ctxt, PrintInformation *pi)
{
	xmlNodePtr cur, child;

	g_return_val_if_fail (pi != NULL, NULL);

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

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


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

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

Michael Meeks's avatar
Michael Meeks committed
992 993 994 995
	if (pi->print_order == PRINT_ORDER_DOWN_THEN_RIGHT)
		child = xmlNewDocNode (ctxt->doc, ctxt->ns, "order", "d_then_r");
	else
		child = xmlNewDocNode (ctxt->doc, ctxt->ns, "order", "r_then_d");
996 997
	xmlAddChild (cur, child);

Michael Meeks's avatar
Michael Meeks committed
998 999 1000 1001
	if (pi->orientation == PRINT_ORIENT_VERTICAL)
		child = xmlNewDocNode (ctxt->doc, ctxt->ns, "orientation", "portrait");
	else
		child = xmlNewDocNode (ctxt->doc, ctxt->ns, "orientation", "landscape");
1002 1003
	xmlAddChild (cur, child);

Michael Meeks's avatar
Michael Meeks committed
1004 1005 1006
	xml_set_print_hf (cur, "Header", pi->header);
	xml_set_print_hf (cur, "Footer", pi->footer);

1007 1008
	child = xmlNewDocNode (ctxt->doc, ctxt->ns, "paper", gnome_paper_name (pi->paper));
	xmlAddChild (cur, child);
Michael Meeks's avatar
Michael Meeks committed
1009
	
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
	return cur;
}

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

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

	pi = ctxt->sheet->print_info;
	
	g_return_if_fail (pi != NULL);

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

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

	if ((child = xml_search_child (tree, "grid"))) {
		xml_get_value_int  (child, "value",    &b);
		pi->print_line_divisions  = (b == 1);
	}
	if ((child = xml_search_child (tree, "monochrome"))) {
		xml_get_value_int  (child, "value", &b);
		pi->print_black_and_white = (b == 1);
	}
	if ((child = xml_search_child (tree, "draft"))) {
		xml_get_value_int  (child, "value",   &b);
		pi->print_as_draft        = (b == 1);
	}
	if ((child = xml_search_child (tree, "titles"))) {
		xml_get_value_int  (child, "value",  &b);
		pi->print_titles          = (b == 1);
	}
Michael Meeks's avatar
Michael Meeks committed
1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087
	
	if ((child = xml_search_child (tree, "order"))) {
		if (!strcmp (xmlNodeGetContent(child), "d_then_r"))
			pi->print_order = PRINT_ORDER_DOWN_THEN_RIGHT;
		else
			pi->print_order = PRINT_ORDER_RIGHT_THEN_DOWN;
	}

	if ((child = xml_search_child (tree, "orientation"))) {
		if (!strcmp (xmlNodeGetContent(child), "portrait"))
			pi->orientation = PRINT_ORIENT_VERTICAL;
		else
			pi->orientation = PRINT_ORIENT_HORIZONTAL;
	}
	
	if ((child = xml_search_child (tree, "Header")))
		xml_get_print_hf (child, pi->header);
	if ((child = xml_search_child (tree, "Footer")))
		xml_get_print_hf (child, pi->header);

	if ((child = xml_search_child (tree, "paper")))
		pi->paper = gnome_paper_with_name (xmlNodeGetContent (child));
1088 1089
}

1090 1091 1092 1093 1094
static const char *
font_component (const char *fontname, int idx)
{
	int i = 0;
	const char *p = fontname;
1095
      
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
	for (; *p && i < idx; p++){
		if (*p == '-')
			i++;
	}
	if (*p == '-')
		p++;

	return p;
}

/**
1107 1108
 * style_font_read_from_x11:
 * @mstyle: the style to setup to this font.
1109 1110 1111
 * @fontname: an X11-like font name.
 *
 * Tries to guess the fontname, the weight and italization parameters
1112
 * and setup mstyle
1113 1114 1115
 *
 * Returns: A valid style font.
 */
1116 1117
static void
style_font_read_from_x11 (MStyle *mstyle, const char *fontname)
1118 1119
{
	const char *c;
1120
	
1121 1122 1123 1124
	/*
	 * FIXME: we should do something about the typeface instead
	 * of hardcoding it to helvetica.
	 */
1125
      
1126 1127
	c = font_component (fontname, 2);
	if (strncmp (c, "bold", 4) == 0)
1128
		mstyle_set_font_bold (mstyle, TRUE);
1129 1130 1131

	c = font_component (fontname, 3);
	if (strncmp (c, "o", 1) == 0)
1132
		mstyle_set_font_italic (mstyle, TRUE);
1133

1134 1135
	if (strncmp (c, "i", 1) == 0)
		mstyle_set_font_italic (mstyle, TRUE);
1136 1137
}

1138 1139 1140
/*
 * Create a Style equivalent to the XML subtree of doc.
 */
1141 1142
static MStyle *
xml_read_style (parse_xml_context_t *ctxt, xmlNodePtr tree)
1143 1144
{
	xmlNodePtr child;
Morten Welinder's avatar
Morten Welinder committed
1145
	char *prop;
1146 1147
	int val;
	StyleColor *c;
1148 1149 1150 1151 1152
	MStyle     *mstyle;
	
	mstyle = mstyle_new ();
	
	if (strcmp (tree->name, "Style")) {
1153
		fprintf (stderr,
1154
			 "xml_read_style: invalid element type %s, 'Style' expected\n",
1155 1156 1157
			 tree->name);
	}

1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
	if (xml_get_value_int (tree, "HAlign", &val))
		mstyle_set_align_h (mstyle, val);

	if (xml_get_value_int (tree, "Fit", &val))
		mstyle_set_fit_in_cell (mstyle, val);

	if (xml_get_value_int (tree, "VAlign", &val))
		mstyle_set_align_v (mstyle, val);

	if (xml_get_value_int (tree, "Orient", &val))
		mstyle_set_orientation (mstyle, val);

	if (xml_get_value_int (tree, "Shade", &val))
		mstyle_set_pattern (mstyle, val);

	if (xml_get_color_value (tree, "Fore", &c))
		mstyle_set_color (mstyle, MSTYLE_COLOR_FORE, c);

	if (xml_get_color_value (tree, "Back", &c))
		mstyle_set_color (mstyle, MSTYLE_COLOR_BACK, c);

1179
	prop = xml_value_get (tree, "Format");
1180 1181
	if (prop != NULL) {
		mstyle_set_format (mstyle, prop);
Morten Welinder's avatar
Morten Welinder committed
1182
		free (prop);
1183
	}
1184

1185
	child = tree->childs;
1186 1187
	while (child != NULL) {
		if (!strcmp (child->name, "Font")) {
1188 1189 1190 1191 1192 1193 1194
			char *font;
			double units = 14;
			int t;
				
			xml_get_value_double (child, "Unit", &units);

			if (xml_get_value_int (child, "Bold", &t))
1195 1196
				mstyle_set_font_bold (mstyle, t);

1197
			if (xml_get_value_int (child, "Italic", &t))
1198
				mstyle_set_font_italic (mstyle, t);
1199
			
1200 1201 1202 1203 1204 1205
			font = xmlNodeGetContent (child);
			if (font) {
				if (*font == '-')
					style_font_read_from_x11 (mstyle, font);
				else
					mstyle_set_font_name (mstyle, font);
1206
			}
1207 1208 1209
			
		} else if (!strcmp (child->name, "StyleBorder")) {
			xml_read_style_border (ctxt, child, mstyle);
1210
		} else {
1211
			fprintf (stderr, "xml_read_style: unknown type '%s'\n",
1212 1213 1214 1215 1216
				 child->name);
		}
		child = child->next;
	}

1217
	return mstyle;
1218 1219
}

1220 1221 1222
/*
 * Create an XML subtree of doc equivalent to the given StyleRegion.
 */
1223
static xmlNodePtr
1224
xml_write_style_region (parse_xml_context_t *ctxt, StyleRegion *region)
1225 1226 1227
{
	xmlNodePtr cur, child;

1228
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleRegion", NULL);
1229 1230 1231 1232
	xml_set_value_int (cur, "startCol", region->range.start.col);
	xml_set_value_int (cur, "startRow", region->range.start.row);
	xml_set_value_int (cur, "endCol",   region->range.end.col);
	xml_set_value_int (cur, "endRow",   region->range.end.row);
1233

1234
	if (region->style != NULL) {
1235
		child = xml_write_style (ctxt, region->style);
1236 1237 1238 1239
		if (child)
			xmlAddChild (cur, child);
	}
	return cur;
1240 1241 1242 1243 1244
}

/*
 * Create a StyleRegion equivalent to the XML subtree of doc.
 */
1245
static void
1246
xml_read_style_region (parse_xml_context_t *ctxt, xmlNodePtr tree)
1247 1248
{
	xmlNodePtr child;
1249 1250
	MStyle    *style = NULL;
	Range      range;
1251 1252 1253

	if (strcmp (tree->name, "StyleRegion")){
		fprintf (stderr,
1254
			 "xml_read_style_region: invalid element type %s, 'StyleRegion' expected`\n",
1255 1256 1257
			 tree->name);
		return;
	}
1258 1259 1260 1261
	xml_get_value_int (tree, "startCol", &range.start.col);
	xml_get_value_int (tree, "startRow", &range.start.row);
	xml_get_value_int (tree, "endCol",   &range.end.col);
	xml_get_value_int (tree, "endRow",   &range.end.row);
1262
	child = tree->childs;
1263

1264 1265 1266 1267 1268
	if (child)
		style = xml_read_style (ctxt, child);

	if (style)
		sheet_style_attach (ctxt->sheet, range, style);
1269 1270
}

1271 1272 1273
/*
 * Create an XML subtree of doc equivalent to the given ColRowInfo.
 */
1274 1275 1276 1277 1278 1279 1280 1281 1282
typedef struct
{
	gboolean is_column;
	xmlNodePtr container;
	parse_xml_context_t *ctxt;
} closure_write_colrow;

static gboolean
xml_write_colrow_info (Sheet *sheet, ColRowInfo *info, void *user_data)