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

#include <config.h>
#include <stdio.h>
#include <gnome.h>
14
#include <locale.h>
15
#include "gnumeric.h"
16 17
#include "gnome-xml/tree.h"
#include "gnome-xml/parser.h"
18
#include "color.h"
19
#include "sheet-object.h"
20
#include "sheet-object-graphic.h"
21
#include "print-info.h"
22
#include "xml-io.h"
23
#include "file.h"
24 25 26 27

/*
 * A parsing context.
 */
28 29 30 31 32 33 34 35 36 37 38 39 40
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 */
	GHashTable *style_table;/* to generate the styles and then the links to it */
	int style_count;        /* A style number */
	xmlNodePtr style_node;  /* The node where we insert the styles */
} parse_xml_context_t;

static Sheet      *xml_sheet_read     (parse_xml_context_t *ctxt, xmlNodePtr tree);
static xmlNodePtr  xml_sheet_write    (parse_xml_context_t *ctxt, Sheet *sheet);
41
static gboolean    xml_workbook_read  (Workbook *wb, parse_xml_context_t *ctxt, xmlNodePtr tree);
42
static xmlNodePtr  xml_workbook_write (parse_xml_context_t *ctxt, Workbook *wb);
43

44 45 46
/*
 * Internal stuff: xml helper functions.
 */
47 48 49 50 51

/*
 * Get a value for a node either carried as an attibute or as
 * the content of a child.
 */
52
static char *
53
xml_value_get (xmlNodePtr node, const char *name)
54
{
55
	char *ret;
56 57
	xmlNodePtr child;

58
	ret = (char *) xmlGetProp (node, name);
59
	if (ret != NULL)
Morten Welinder's avatar
Morten Welinder committed
60
		return ret;
61 62 63
	child = node->childs;

	while (child != NULL) {
64 65 66 67 68 69 70 71
		if (!strcmp (child->name, name)) {
		        /*
			 * !!! Inefficient, but ...
			 */
			ret = xmlNodeGetContent(child);
			if (ret != NULL)
			    return (ret);
		}
72 73 74 75
		child = child->next;
	}

	return NULL;
76 77
}

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

89
	val = xml_value_get(node, name);
90 91 92 93
	if (val == NULL) return(NULL);
        ret = string_get(val);
	free(val);
	return(ret);
94
}
95
#endif
96

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

108
	ret = xml_value_get (node, name);
109 110 111 112 113 114
	if (ret == NULL) return(0);
	res = sscanf (ret, "%d", &i);
	free(ret);
	
	if (res == 1) {
	        *val = i;
115 116 117
		return 1;
	}
	return 0;
118 119
}

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

132
	ret = xml_value_get (node, name);
133 134 135 136 137 138
	if (ret == NULL) return(0);
	res = sscanf (ret, "%f", &f);
	free(ret);
	
	if (res == 1) {
	        *val = f;
139 140 141
		return 1;
	}
	return 0;
142
}
143
#endif
144 145 146 147 148

/*
 * Get a double value for a node either carried as an attibute or as
 * the content of a child.
 */
149
static int
150
xml_get_value_double (xmlNodePtr node, const char *name, double *val)
151
{
152 153
	int res;
	char *ret;
154

155
	ret = xml_value_get (node, name);
156
	if (ret == NULL) return(0);
157
	res = sscanf (ret, "%lf", val);
158
	free(ret);
159

160
	return (res == 1);
161 162
}

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

174
	ret = xml_value_get (node, name);
175
	if (ret == NULL) return(0);
176
	res = sscanf (ret, "(%lf %lf)", x, y)
177
	free(ret);
178 179

	return (res == 2)
180
}
181
#endif
182 183 184 185 186

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

187
static int
188
xml_get_coordinates (xmlNodePtr node, const char *name,
189 190
		   double *x1, double *y1, double *x2, double *y2)
{
191 192
	int res;
	char *ret;
193

194
	ret = xml_value_get (node, name);
195
	if (ret == NULL) return(0);
196
	res = sscanf (ret, "(%lf %lf)(%lf %lf)", x1, y1, x2, y2);
197 198
	free(ret);
	
199
	if (res == 4) 
200
		return 1;
201

202
	return 0;
203 204
}

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

219
	val = xml_value_get (node, name);
220
	if (val == NULL) return(NULL);
221 222 223 224 225
	ptr = val;
	do {
		while ((*ptr) && (*ptr != '('))
			ptr++;
		if (*ptr == 0)
226
			break;
227
		res = sscanf (ptr, "(%lf %lf)", &coord[index], &coord[index + 1]);
228 229 230 231 232 233 234 235 236 237 238 239 240
		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];
241
	return ret;
242
}
243
#endif
244 245 246 247 248

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

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

	child = node->childs;
	while (child != NULL){
		if (!strcmp (child->name, name)){
			xmlNodeSetContent (child, base);
			free (base);
			return;
		}
		child = child->next;
	}
280
	xmlNewChild (node, NULL, name, xmlEncodeEntities(node->doc, base));
281
	g_free (base);
282 283
}

284 285 286 287
/*
 * Set a string value for a node either carried as an attibute or as
 * the content of a child.
 */
288 289
static void
xml_set_value (xmlNodePtr node, const char *name, const char *val)
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
{
	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);
308 309
}

310 311 312 313
/*
 * Set a String value for a node either carried as an attibute or as
 * the content of a child.
 */
314 315
static void
xml_set_value_string (xmlNodePtr node, const char *name, String *val)
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
{
	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);
334 335
}

336 337 338 339
/*
 * Set an integer value for a node either carried as an attibute or as
 * the content of a child.
 */
340
static void
341
xml_set_value_int (xmlNodePtr node, const char *name, int val)
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
{
	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);
362 363
}

364
#if 0
365 366 367 368
/*
 * Set a float value for a node either carried as an attibute or as
 * the content of a child.
 */
369
static void
370
xml_set_value_float (xmlNodePtr node, const char *name, float val)
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
{
	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);
391
}
392
#endif
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
xml_set_value_double (xmlNodePtr node, const char *name, double val)
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
{
	const char *ret;
	xmlNodePtr child;
	char str[101];

	snprintf (str, 100, "%f", (float) 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);
420 421
}

422 423 424 425 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 475 476
static void
xml_set_print_unit (xmlNodePtr node, const char *name,
		    const PrintUnit * const pu)
{
	xmlNodePtr  child;
	char       *txt;

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

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

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

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

/*
 * Set a color value for a node either carried as an attibute or as
 * the content of a child.
 */
530 531
static void
xml_set_color_value (xmlNodePtr node, const char *name, StyleColor *val)
532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
{
	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);
552
}
553 554 555 556 557

/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
558
 **/
559

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

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

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

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

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

599 600 601 602 603 604
static char *StyleSideNames[4] =
{
 	"Top",
 	"Bottom",
 	"Left",
 	"Right"
Arturo Espinosa's avatar
Arturo Espinosa committed
605
};
606

607
static xmlNodePtr
608
xml_write_style_border (parse_xml_context_t *ctxt, StyleBorder *border)
609 610 611
{
	xmlNodePtr cur;
	xmlNodePtr side;
Arturo Espinosa's avatar
Arturo Espinosa committed
612
	int lp;
613
       
Arturo Espinosa's avatar
Arturo Espinosa committed
614 615 616
	for (lp = 3; lp >= 0; lp--)
		if (border->type [lp] != BORDER_NONE)
			break;
617
	if (lp < 0)
Arturo Espinosa's avatar
Arturo Espinosa committed
618
		return NULL;
619

620
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
621
	
622 623 624 625
	for (lp = 0; lp < 4; lp++){
 		if (border->type[lp] != BORDER_NONE){
 			side = xmlNewChild (cur, ctxt->ns, StyleSideNames [lp],
 					    BorderTypes [border->type [lp]]);
626
 			xml_set_color_value (side, "Color", border->color [lp]);
627
 		}
628 629
	}
	return cur;
630 631 632 633 634
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
635
static StyleBorder *
636
xml_read_style_border (parse_xml_context_t *ctxt, xmlNodePtr tree)
637 638
{
	StyleBorder *ret;
Arturo Espinosa's avatar
Arturo Espinosa committed
639
 	StyleBorderType style [4] = { BORDER_NONE, BORDER_NONE, BORDER_NONE, BORDER_NONE };
640
	StyleColor *color [4] = { NULL, NULL, NULL, NULL };
641
	xmlNodePtr side;
Arturo Espinosa's avatar
Arturo Espinosa committed
642
	int lp;
643 644 645

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

Arturo Espinosa's avatar
Arturo Espinosa committed
650
 	for (lp = 0; lp < 4; lp++)
651
 	{
652
 		if ((side = xml_search_child (tree, StyleSideNames [lp])) != NULL)
653 654
 		{
 			/* FIXME: need to read the proper type */
Arturo Espinosa's avatar
Arturo Espinosa committed
655
 			style [lp] = BORDER_THICK ;
656
 			xml_get_color_value (side, "Color", &color [lp]);
657
 		}
658
	}
659
	
Arturo Espinosa's avatar
Arturo Espinosa committed
660
	ret = style_border_new (style, color);
661

662
	return NULL;
663 664 665 666 667
}

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

	if ((style->halign == 0) && (style->valign == 0) &&
	    (style->orientation == 0) && (style->format == NULL) &&
	    (style->font == NULL) && (style->border == NULL))
		return NULL;

678
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
679 680 681 682 683 684 685 686
	if (style_idx != -1)
		xml_set_value_int (cur, "No", style_idx);
	
	xml_set_value_int (cur, "HAlign", style->halign);
	xml_set_value_int (cur, "VAlign", style->valign);
	xml_set_value_int (cur, "Fit", style->fit_in_cell);
	xml_set_value_int (cur, "Orient", style->orientation);
	xml_set_value_int (cur, "Shade", style->pattern);
687 688

	if (!style_is_default_fore (style->fore_color))
689
		xml_set_color_value (cur, "Fore", style->fore_color);
690 691

	if (!style_is_default_back (style->back_color))
692
		xml_set_color_value (cur, "Back", style->back_color);
693 694

	if (style->format != NULL){
695
		xml_set_value (cur, "Format", style->format->format);
696 697 698
	}

	if (style->font != NULL){
699 700 701 702 703 704 705
		child = xmlNewChild (cur, ctxt->ns, "Font", 
				     xmlEncodeEntities(ctxt->doc,
						       style->font->font_name));
		xml_set_value_double (child, "Unit", style->font->size);
		xml_set_value_int (child, "Bold", style->font->is_bold);
		xml_set_value_int (child, "Italic", style->font->is_italic);

706 707 708
	}

	if (style->border != NULL){
709
		child = xml_write_style_border (ctxt, style->border);
710 711 712 713 714
		if (child)
			xmlAddChild (cur, child);
	}

	return cur;
715 716
}

717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 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 759 760 761 762 763 764 765
static xmlNodePtr
xml_write_names (parse_xml_context_t *ctxt, Workbook *wb)
{
	GList *names, *m;
	xmlNodePtr cur;

	g_return_val_if_fail (wb != NULL, NULL);

	m = names = expr_name_list (wb, FALSE);

	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);
	}
	g_list_free (m);
	return cur;
}

static void
xml_read_names (parse_xml_context_t *ctxt, xmlNodePtr tree, Workbook *wb)
{
	xmlNodePtr child;

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

	child = tree->childs;
	while (child) {
766
		char *name  = NULL;
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
		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"));

					if (!expr_name_create (wb, name, txt, &error))
						g_warning (error);

					g_free (txt);
				}
789
				bits = bits->next;
790 791 792 793 794 795
			}
		}
		child = child->next;
	}
}

796
static xmlNodePtr
797
xml_write_summary (parse_xml_context_t *ctxt, SummaryInfo *summary_info)
798 799 800 801
{
	GList *items, *m;
	xmlNodePtr cur;

802
	if (!summary_info)
803 804
		return NULL;

805
	m = items = summary_info_as_list (summary_info);
806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835

	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);
836
			xmlAddChild (cur, tmp);
837 838 839 840 841 842 843 844
		}
		items = g_list_next (items);
	}
	g_list_free (m);
	return cur;
}

static void
845
xml_read_summary (parse_xml_context_t *ctxt, xmlNodePtr tree, SummaryInfo *summary_info)
846
{
847
	xmlNodePtr child;
848

849 850 851
	g_return_if_fail (ctxt != NULL);
	g_return_if_fail (tree != NULL);
	g_return_if_fail (summary_info != NULL);
852 853 854

	child = tree->childs;
	while (child) {
855
		char *name = NULL;
856 857 858 859 860 861 862 863
		if (child->name && !strcmp (child->name, "Item")) {
			xmlNodePtr bits;

			bits = child->childs;
			while (bits) {
				SummaryItem *sit = NULL;
				
				if (!strcmp (bits->name, "name")) {
864
					name = xmlNodeGetContent (bits);
865 866 867 868 869
				} else {
					char *txt;
					g_return_if_fail (name);

					txt = xmlNodeGetContent (bits);
870 871 872 873 874 875 876
					if (txt != NULL){
						if (!strcmp (bits->name, "val-string"))
							sit = summary_item_new_string (name, txt);
						else if (!strcmp (bits->name, "val-int"))
							sit = summary_item_new_int    (name, atoi (txt));
						
						if (sit)
877
							summary_info_add (summary_info, sit);
878 879
						g_free (txt);
					}
880
				}
881
				bits = bits->next;
882 883 884 885 886 887
			}
		}
		child = child->next;
	}
}

888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 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
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);

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

983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 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
static const char *
font_component (const char *fontname, int idx)
{
	int i = 0;
	const char *p = fontname;
	
	for (; *p && i < idx; p++){
		if (*p == '-')
			i++;
	}
	if (*p == '-')
		p++;

	return p;
}

/**
 * style_font_new_from_x11:
 * @fontname: an X11-like font name.
 * @scale: scale desired
 *
 * Tries to guess the fontname, the weight and italization parameters
 * to invoke style_font_new
 *
 * Returns: A valid style font.
 */
static StyleFont *
style_font_new_from_x11 (const char *fontname, double units, double scale)
{
	StyleFont *sf;
	char *typeface = "Helvetica";
	int is_bold = 0;
	int is_italic = 0;
	const char *c;

	/*
	 * FIXME: we should do something about the typeface instead
	 * of hardcoding it to helvetica.
	 */
	
	c = font_component (fontname, 2);
	if (strncmp (c, "bold", 4) == 0)
		is_bold = 1;

	c = font_component (fontname, 3);
	if (strncmp (c, "o", 1) == 0)
		is_italic = 1;
	if (strncmp (c, "i", 1) == 0)
		is_italic = 1;

	sf = style_font_new (typeface, units, scale, is_bold, is_italic);
	if (sf == NULL)
		sf = style_font_new_from (gnumeric_default_font, scale);

	return sf;
}

1040 1041 1042
/*
 * Create a Style equivalent to the XML subtree of doc.
 */
1043
static Style *
1044
xml_read_style (parse_xml_context_t *ctxt, xmlNodePtr tree, Style * ret)
1045 1046
{
	xmlNodePtr child;
Morten Welinder's avatar
Morten Welinder committed
1047
	char *prop;
1048 1049 1050 1051 1052
	int val;
	StyleColor *c;

	if (strcmp (tree->name, "Style")){
		fprintf (stderr,
1053
			 "xml_read_style: invalid element type %s, 'Style' expected\n",
1054 1055 1056 1057 1058 1059 1060 1061
			 tree->name);
	}
	if (ret == NULL)
		ret = style_new_empty ();
	
	if (ret == NULL)
		return NULL;

1062
	if (xml_get_value_int (tree, "HAlign", &val)){
1063 1064 1065
		ret->halign = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
1066
	if (xml_get_value_int (tree, "Fit", &val)){
1067 1068 1069
		ret->fit_in_cell = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
1070
	if (xml_get_value_int (tree, "VAlign", &val)){
1071 1072 1073
		ret->valign = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
1074
	if (xml_get_value_int (tree, "Orient", &val)){
1075 1076 1077
		ret->orientation = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
1078
	if (xml_get_value_int (tree, "Shade", &val)){
1079 1080 1081
		ret->pattern = val;
		ret->valid_flags |= STYLE_PATTERN;
	}
1082
	if (xml_get_color_value (tree, "Fore", &c)){
1083 1084 1085
		ret->fore_color = c;
		ret->valid_flags |= STYLE_FORE_COLOR;
	}
1086
	if (xml_get_color_value (tree, "Back", &c)){
1087 1088 1089
		ret->back_color = c;
		ret->valid_flags |= STYLE_BACK_COLOR;
	}
1090
	prop = xml_value_get (tree, "Format");
1091 1092
	if (prop != NULL){
		if (ret->format == NULL){
Morten Welinder's avatar
Morten Welinder committed
1093
			ret->format = style_format_new ((const char *) prop);
1094
			ret->valid_flags |= STYLE_FORMAT;
1095
		}
Morten Welinder's avatar
Morten Welinder committed
1096
		free (prop);
1097
	}
1098

1099 1100 1101
	child = tree->childs;
	while (child != NULL){
		if (!strcmp (child->name, "Font")){
1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128
			char *font;
			double units = 14;
			int is_bold = 0;
			int is_italic = 0;
			int t;
				
			xml_get_value_double (child, "Unit", &units);

			if (xml_get_value_int (child, "Bold", &t))
				is_bold = t;
			if (xml_get_value_int (child, "Italic", &t))
				is_italic = t;
			
			font = xmlNodeGetContent(child);
			if (font != NULL) {
				StyleFont *sf;

				if (*font == '-'){
					sf = style_font_new_from_x11 (font, units, 1.0);
				} else
					sf = style_font_new (font, units, 1.0, is_bold, is_italic);

				ret->font = sf;
				free(font);
			}
			if (ret->font){
				ret->valid_flags |= STYLE_FONT;
1129 1130 1131 1132
			}
		} else if (!strcmp (child->name, "StyleBorder")){
			StyleBorder *sb;

1133
			sb = xml_read_style_border (ctxt, child);
1134
		} else {
1135
			fprintf (stderr, "xml_read_style: unknown type '%s'\n",
1136 1137 1138 1139 1140 1141 1142
				 child->name);
		}
		child = child->next;
	}

	/* Now add defaults to any style that was not loaded */
	return ret;
1143 1144
}

1145
#if 0
1146 1147 1148
/*
 * Create an XML subtree of doc equivalent to the given StyleRegion.
 */
1149
static xmlNodePtr
1150
xml_write_style_region (parse_xml_context_t *ctxt, StyleRegion *region)
1151 1152 1153
{
	xmlNodePtr cur, child;

1154
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleRegion", NULL);
1155 1156 1157 1158
	xml_set_value_int (cur, "startCol", region->range.start_col);
	xml_set_value_int (cur, "endCol", region->range.end_col);
	xml_set_value_int (cur, "startRow", region->range.start_row);
	xml_set_value_int (cur, "endRow", region->range.end_row);
1159 1160

	if (region->style != NULL){
1161
		child = xml_write_style (ctxt, region->style);
1162 1163 1164 1165
		if (child)
			xmlAddChild (cur, child);
	}
	return cur;
1166
}
1167
#endif
1168 1169 1170 1171

/*
 * Create a StyleRegion equivalent to the XML subtree of doc.
 */
1172
static void
1173
xml_read_style_region (parse_xml_context_t *ctxt, xmlNodePtr tree)
1174 1175 1176 1177 1178 1179 1180
{
	xmlNodePtr child;
	Style *style = NULL;
	int start_col = 0, start_row = 0, end_col = 0, end_row = 0;

	if (strcmp (tree->name, "StyleRegion")){
		fprintf (stderr,
1181
			 "xml_read_style_region: invalid element type %s, 'StyleRegion' expected`\n",
1182 1183 1184
			 tree->name);
		return;
	}
1185 1186 1187 1188
	xml_get_value_int (tree, "startCol", &start_col);
	xml_get_value_int (tree, "startRow", &start_row);
	xml_get_value_int (tree, "endCol", &end_col);
	xml_get_value_int (tree, "endRow", &end_row);
1189 1190
	child = tree->childs;
	if (child != NULL)
1191
		style = xml_read_style (ctxt, child, NULL);
1192 1193 1194
	if (style != NULL)
		sheet_style_attach (ctxt->sheet, start_col, start_row, end_col,
				    end_row, style);
1195 1196 1197

}

1198 1199 1200
/*
 * Create an XML subtree of doc equivalent to the given ColRowInfo.
 */
1201
static xmlNodePtr
1202
xml_write_colrow_info (parse_xml_context_t *ctxt, ColRowInfo *info, int col)
1203 1204 1205 1206
{
	xmlNodePtr cur;

	if (col)
1207
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "ColInfo", NULL);
1208
	else
1209
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "RowInfo", NULL);
1210

1211 1212 1213 1214 1215
	xml_set_value_int (cur, "No", info->pos);
	xml_set_value_double (cur, "Unit", info->units);
	xml_set_value_double (cur, "MarginA", info->margin_a_pt);
	xml_set_value_double (cur, "MarginB", info->margin_b);
	xml_set_value_int (cur, "HardSize", info->hard_size);
1216 1217

	return cur;
1218 1219 1220 1221 1222
}

/*
 * Create a ColRowInfo equivalent to the XML subtree of doc.
 */
1223
static ColRowInfo *
1224
xml_read_colrow_info (parse_xml_context_t *ctxt, xmlNodePtr tree, ColRowInfo *ret)
1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
{
	int col = 0;
	int val;

	if (!strcmp (tree->name, "ColInfo")){
		col = 1;
	} else if (!strcmp (tree->name, "RowInfo")){
		col = 0;
	} else {
		fprintf (stderr,
1235
			 "xml_read_colrow_info: invalid element type %s, 'ColInfo/RowInfo' expected`\n",
1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246
			 tree->name);
		return NULL;
	}
	if (ret == NULL){
		if (col)
			ret = sheet_col_new (ctxt->sheet);
		else
			ret = sheet_row_new (ctxt->sheet);
	}
	if (ret == NULL)
		return NULL;
1247

1248 1249 1250 1251 1252
	xml_get_value_int (tree, "No", &ret->pos);
	xml_get_value_double (tree, "Unit", &ret->units);
	xml_get_value_double (tree, "MarginA", &ret->margin_a_pt);
	xml_get_value_double (tree, "MarginB", &ret->margin_b_pt);
	if (xml_get_value_int (tree, "HardSize", &val))
1253
		ret->hard_size = val;
1254

1255
	return ret;
1256 1257
}

1258 1259 1260
/*
 * Create an XML subtree of doc equivalent to the given Object.
 */
1261
static xmlNodePtr
1262
xml_write_sheet_object (parse_xml_context_t *ctxt, SheetObject *object)
1263
{
1264
	SheetObjectGraphic *sog = SHEET_OBJECT_GRAPHIC (object);
1265 1266
	xmlNodePtr cur = NULL;

1267
	switch (sog->type){
1268
	case SHEET_OBJECT_RECTANGLE:{
1269
			SheetObjectFilled *sof = SHEET_OBJECT_FILLED (object);
1270

1271
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Rectangle", NULL);
1272
			if (sof->fill_color != NULL)
1273 1274
				xml_set_value_string (cur, "FillColor", sof->fill_color);
			xml_set_value_int (cur, "Pattern", sof->pattern);
1275 1276
			break;
		}
1277

1278
	case SHEET_OBJECT_ELLIPSE:{
1279
			SheetObjectFilled *sof = SHEET_OBJECT_FILLED (object);
1280

1281
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Ellipse", NULL);
1282
			if (sof->fill_color != NULL)
1283 1284
				xml_set_value_string (cur, "FillColor", sof->fill_color);
			xml_set_value_int (cur, "Pattern", sof->pattern);
1285 1286
			break;
		}
1287

1288
	case SHEET_OBJECT_ARROW:
1289
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Arrow", NULL);
1290
		break;
1291

1292
	case SHEET_OBJECT_LINE:
1293
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Line", NULL);
1294 1295 1296 1297 1298
		break;
	}
	if (cur == NULL)
		return NULL;
	
1299 1300 1301
	xml_set_gnome_canvas_points (cur, "Points", object->bbox_points);
	xml_set_value_int (cur, "Width", sog->width);
	xml_set_value_string (cur, "Color", sog->color);