xml-io.c 44.3 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 14
 *
 * $Id$
 */

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

/*
 * A parsing context.
 */
26 27 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);
static Workbook   *xml_workbook_read  (parse_xml_context_t *ctxt, xmlNodePtr tree);
static xmlNodePtr  xml_workbook_write (parse_xml_context_t *ctxt, Workbook *wb);
41

42 43 44
/*
 * Internal stuff: xml helper functions.
 */
45 46 47 48 49

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

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

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

	return NULL;
74 75
}

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

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

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

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

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

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

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

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

158
	return (res == 1);
159 160
}

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

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

	return (res == 2)
178
}
179
#endif
180 181 182 183 184

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

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

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

200
	return 0;
201 202
}

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

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

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

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

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


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

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

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

363
#if 0
364 365 366 367
/*
 * Set a float value for a node either carried as an attibute or as
 * the content of a child.
 */
368
static void
369
xml_set_value_float (xmlNodePtr node, const char *name, float val)
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
{
	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);
390
}
391
#endif
392 393 394 395 396

/*
 * Set a double value for a node either carried as an attibute or as
 * the content of a child.
 */
397
static void
398
xml_set_value_double (xmlNodePtr node, const char *name, double val)
399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418
{
	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);
419 420 421 422 423
}

/*
 * Search a child by name, if needed go down the tree to find it. 
 */
424 425
static xmlNodePtr
xml_search_child (xmlNodePtr node, const char *name)
426 427 428 429 430 431 432 433 434 435 436 437
{
	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){
438
		ret = xml_search_child (child, name);
439 440 441 442 443
		if (ret != NULL)
			return ret;
		child = child->next;
	}
	return NULL;
444 445 446 447 448 449 450 451 452 453
}

/*
 * 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 ...
 */
454
static int
455
xml_get_color_value (xmlNodePtr node, const char *name, StyleColor **color)
456
{
457
	char *ret;
458 459
	int red, green, blue;

460
	ret = xml_value_get (node, name);
461 462
	if (ret == NULL) return(0);
	if (sscanf (ret, "%X:%X:%X", &red, &green, &blue) == 3){
463
		*color = style_color_new (red, green, blue);
464
		free(ret);
465 466 467
		return 1;
	}
	return 0;
468 469 470 471 472 473
}

/*
 * Set a color value for a node either carried as an attibute or as
 * the content of a child.
 */
474 475
static void
xml_set_color_value (xmlNodePtr node, const char *name, StyleColor *val)
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
{
	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);
496
}
497 498 499 500 501

/**
 **
 ** Private functions : mapping between in-memory structure and XML tree
 **
502
 **/
503

504 505
static int
style_is_default_fore (StyleColor *color)
506
{
507 508
	if (!color)
		return TRUE;
509

510 511 512 513
	if (color->color.red == 0 && color->color.green == 0 && color->color.blue == 0)
		return TRUE;
	else
		return FALSE;
514 515
}

516 517
static int
style_is_default_back (StyleColor *color)
518
{
519 520
	if (!color)
		return TRUE;
521

522 523 524 525
	if (color->color.red == 0xffff && color->color.green == 0xffff && color->color.blue == 0xffff)
		return TRUE;
	else
		return FALSE;
526 527 528 529 530
}

/*
 * Create an XML subtree of doc equivalent to the given StyleBorder.
 */
531 532 533
static char *BorderTypes[8] =
{
	"none",
534 535 536 537 538 539 540
 	"thin",
 	"medium",
 	"dashed",
 	"dotted",
 	"thick",
 	"double",
	"hair"
541 542
};

543 544 545 546 547 548
static char *StyleSideNames[4] =
{
 	"Top",
 	"Bottom",
 	"Left",
 	"Right"
Arturo Espinosa's avatar
Arturo Espinosa committed
549
};
550

551
static xmlNodePtr
552
xml_write_style_border (parse_xml_context_t *ctxt, StyleBorder *border)
553 554 555
{
	xmlNodePtr cur;
	xmlNodePtr side;
Arturo Espinosa's avatar
Arturo Espinosa committed
556
	int lp;
557
       
Arturo Espinosa's avatar
Arturo Espinosa committed
558 559 560
	for (lp = 3; lp >= 0; lp--)
		if (border->type [lp] != BORDER_NONE)
			break;
561
	if (lp < 0)
Arturo Espinosa's avatar
Arturo Espinosa committed
562
		return NULL;
563

564
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
565
	
566 567 568 569
	for (lp = 0; lp < 4; lp++){
 		if (border->type[lp] != BORDER_NONE){
 			side = xmlNewChild (cur, ctxt->ns, StyleSideNames [lp],
 					    BorderTypes [border->type [lp]]);
570
 			xml_set_color_value (side, "Color", border->color [lp]);
571
 		}
572 573
	}
	return cur;
574 575 576 577 578
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
579
static StyleBorder *
580
xml_read_style_border (parse_xml_context_t *ctxt, xmlNodePtr tree)
581 582
{
	StyleBorder *ret;
Arturo Espinosa's avatar
Arturo Espinosa committed
583
 	StyleBorderType style [4] = { BORDER_NONE, BORDER_NONE, BORDER_NONE, BORDER_NONE };
584
	StyleColor *color [4] = { NULL, NULL, NULL, NULL };
585
	xmlNodePtr side;
Arturo Espinosa's avatar
Arturo Espinosa committed
586
	int lp;
587 588 589

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
590
			 "xml_read_style_border: invalid element type %s, 'StyleBorder' expected`\n",
591 592
			 tree->name);
	}
593

Arturo Espinosa's avatar
Arturo Espinosa committed
594
 	for (lp = 0; lp < 4; lp++)
595
 	{
596
 		if ((side = xml_search_child (tree, StyleSideNames [lp])) != NULL)
597 598
 		{
 			/* FIXME: need to read the proper type */
Arturo Espinosa's avatar
Arturo Espinosa committed
599
 			style [lp] = BORDER_THICK ;
600
 			xml_get_color_value (side, "Color", &color [lp]);
601
 		}
602
	}
603
	
Arturo Espinosa's avatar
Arturo Espinosa committed
604
	ret = style_border_new (style, color);
605

606
	return NULL;
607 608 609 610 611
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
612
static xmlNodePtr
613
xml_write_style (parse_xml_context_t *ctxt, Style *style, int style_idx)
614 615 616 617 618 619 620 621
{
	xmlNodePtr cur, child;

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

622
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
623 624 625 626 627 628 629 630
	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);
631 632

	if (!style_is_default_fore (style->fore_color))
633
		xml_set_color_value (cur, "Fore", style->fore_color);
634 635

	if (!style_is_default_back (style->back_color))
636
		xml_set_color_value (cur, "Back", style->back_color);
637 638

	if (style->format != NULL){
639
		xml_set_value (cur, "Format", style->format->format);
640 641 642
	}

	if (style->font != NULL){
643 644 645 646 647 648 649
		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);

650 651 652
	}

	if (style->border != NULL){
653
		child = xml_write_style_border (ctxt, style->border);
654 655 656 657 658
		if (child)
			xmlAddChild (cur, child);
	}

	return cur;
659 660
}

661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709
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) {
710
		char *name  = NULL;
711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732
		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);
				}
733
				bits = bits->next;
734 735 736 737 738 739
			}
		}
		child = child->next;
	}
}

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 766 767 768 769 770 771 772 773 774 775 776 777 778 779
static xmlNodePtr
xml_write_summary (parse_xml_context_t *ctxt, SummaryInfo *sin)
{
	GList *items, *m;
	xmlNodePtr cur;

	if (!sin)
		return NULL;

	m = items = summary_info_as_list (sin);

	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);
780
			xmlAddChild (cur, tmp);
781 782 783 784 785 786 787 788 789 790
		}
		items = g_list_next (items);
	}
	g_list_free (m);
	return cur;
}

static void
xml_read_summary (parse_xml_context_t *ctxt, xmlNodePtr tree, SummaryInfo *sin)
{
791
	xmlNodePtr child;
792 793 794 795 796 797 798

	g_return_if_fail (sin);
	g_return_if_fail (ctxt);
	g_return_if_fail (tree);

	child = tree->childs;
	while (child) {
799
		char *name = NULL;
800 801 802 803 804 805 806 807
		if (child->name && !strcmp (child->name, "Item")) {
			xmlNodePtr bits;

			bits = child->childs;
			while (bits) {
				SummaryItem *sit = NULL;
				
				if (!strcmp (bits->name, "name")) {
808
					name = xmlNodeGetContent (bits);
809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824
				} else {
					char *txt;
					g_return_if_fail (name);

					txt = xmlNodeGetContent (bits);
					g_return_if_fail (txt);

					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)
						summary_info_add (sin, sit);
					g_free (txt);
				}
825
				bits = bits->next;
826 827 828 829 830 831
			}
		}
		child = child->next;
	}
}

832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
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;
}

889 890 891
/*
 * Create a Style equivalent to the XML subtree of doc.
 */
892
static Style *
893
xml_read_style (parse_xml_context_t *ctxt, xmlNodePtr tree, Style * ret)
894 895
{
	xmlNodePtr child;
Morten Welinder's avatar
Morten Welinder committed
896
	char *prop;
897 898 899 900 901
	int val;
	StyleColor *c;

	if (strcmp (tree->name, "Style")){
		fprintf (stderr,
902
			 "xml_read_style: invalid element type %s, 'Style' expected\n",
903 904 905 906 907 908 909 910
			 tree->name);
	}
	if (ret == NULL)
		ret = style_new_empty ();
	
	if (ret == NULL)
		return NULL;

911
	if (xml_get_value_int (tree, "HAlign", &val)){
912 913 914
		ret->halign = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
915
	if (xml_get_value_int (tree, "Fit", &val)){
916 917 918
		ret->fit_in_cell = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
919
	if (xml_get_value_int (tree, "VAlign", &val)){
920 921 922
		ret->valign = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
923
	if (xml_get_value_int (tree, "Orient", &val)){
924 925 926
		ret->orientation = val;
		ret->valid_flags |= STYLE_ALIGN;
	}
927
	if (xml_get_value_int (tree, "Shade", &val)){
928 929 930
		ret->pattern = val;
		ret->valid_flags |= STYLE_PATTERN;
	}
931
	if (xml_get_color_value (tree, "Fore", &c)){
932 933 934
		ret->fore_color = c;
		ret->valid_flags |= STYLE_FORE_COLOR;
	}
935
	if (xml_get_color_value (tree, "Back", &c)){
936 937 938
		ret->back_color = c;
		ret->valid_flags |= STYLE_BACK_COLOR;
	}
939
	prop = xml_value_get (tree, "Format");
940 941
	if (prop != NULL){
		if (ret->format == NULL){
Morten Welinder's avatar
Morten Welinder committed
942
			ret->format = style_format_new ((const char *) prop);
943
			ret->valid_flags |= STYLE_FORMAT;
944
		}
Morten Welinder's avatar
Morten Welinder committed
945
		free (prop);
946
	}
947

948 949 950
	child = tree->childs;
	while (child != NULL){
		if (!strcmp (child->name, "Font")){
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
			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;
978 979 980 981
			}
		} else if (!strcmp (child->name, "StyleBorder")){
			StyleBorder *sb;

982
			sb = xml_read_style_border (ctxt, child);
983
		} else {
984
			fprintf (stderr, "xml_read_style: unknown type '%s'\n",
985 986 987 988 989 990 991
				 child->name);
		}
		child = child->next;
	}

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

994
#if 0
995 996 997
/*
 * Create an XML subtree of doc equivalent to the given StyleRegion.
 */
998
static xmlNodePtr
999
xml_write_style_region (parse_xml_context_t *ctxt, StyleRegion *region)
1000 1001 1002
{
	xmlNodePtr cur, child;

1003
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleRegion", NULL);
1004 1005 1006 1007
	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);
1008 1009

	if (region->style != NULL){
1010
		child = xml_write_style (ctxt, region->style);
1011 1012 1013 1014
		if (child)
			xmlAddChild (cur, child);
	}
	return cur;
1015
}
1016
#endif
1017 1018 1019 1020

/*
 * Create a StyleRegion equivalent to the XML subtree of doc.
 */
1021
static void
1022
xml_read_style_region (parse_xml_context_t *ctxt, xmlNodePtr tree)
1023 1024 1025 1026 1027 1028 1029
{
	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,
1030
			 "xml_read_style_region: invalid element type %s, 'StyleRegion' expected`\n",
1031 1032 1033
			 tree->name);
		return;
	}
1034 1035 1036 1037
	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);
1038 1039
	child = tree->childs;
	if (child != NULL)
1040
		style = xml_read_style (ctxt, child, NULL);
1041 1042 1043
	if (style != NULL)
		sheet_style_attach (ctxt->sheet, start_col, start_row, end_col,
				    end_row, style);
1044 1045 1046

}

1047 1048 1049
/*
 * Create an XML subtree of doc equivalent to the given ColRowInfo.
 */
1050
static xmlNodePtr
1051
xml_write_colrow_info (parse_xml_context_t *ctxt, ColRowInfo *info, int col)
1052 1053 1054 1055
{
	xmlNodePtr cur;

	if (col)
1056
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "ColInfo", NULL);
1057
	else
1058
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "RowInfo", NULL);
1059

1060 1061 1062 1063 1064
	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);
1065 1066

	return cur;
1067 1068 1069 1070 1071
}

/*
 * Create a ColRowInfo equivalent to the XML subtree of doc.
 */
1072
static ColRowInfo *
1073
xml_read_colrow_info (parse_xml_context_t *ctxt, xmlNodePtr tree, ColRowInfo *ret)
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
{
	int col = 0;
	int val;

	if (!strcmp (tree->name, "ColInfo")){
		col = 1;
	} else if (!strcmp (tree->name, "RowInfo")){
		col = 0;
	} else {
		fprintf (stderr,
1084
			 "xml_read_colrow_info: invalid element type %s, 'ColInfo/RowInfo' expected`\n",
1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
			 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;
1096

1097 1098 1099 1100 1101
	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))
1102
		ret->hard_size = val;
1103

1104
	return ret;
1105 1106
}

1107 1108 1109
/*
 * Create an XML subtree of doc equivalent to the given Object.
 */
1110
static xmlNodePtr
1111
xml_write_sheet_object (parse_xml_context_t *ctxt, SheetObject *object)
1112
{
1113
	SheetObjectGraphic *sog = SHEET_OBJECT_GRAPHIC (object);
1114 1115
	xmlNodePtr cur = NULL;

1116
	switch (sog->type){
1117
	case SHEET_OBJECT_RECTANGLE:{
1118
			SheetObjectFilled *sof = SHEET_OBJECT_FILLED (object);
1119

1120
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Rectangle", NULL);
1121
			if (sof->fill_color != NULL)
1122 1123
				xml_set_value_string (cur, "FillColor", sof->fill_color);
			xml_set_value_int (cur, "Pattern", sof->pattern);
1124 1125
			break;
		}
1126

1127
	case SHEET_OBJECT_ELLIPSE:{
1128
			SheetObjectFilled *sof = SHEET_OBJECT_FILLED (object);
1129

1130
			cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Ellipse", NULL);
1131
			if (sof->fill_color != NULL)
1132 1133
				xml_set_value_string (cur, "FillColor", sof->fill_color);
			xml_set_value_int (cur, "Pattern", sof->pattern);
1134 1135
			break;
		}
1136

1137
	case SHEET_OBJECT_ARROW:
1138
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Arrow", NULL);
1139
		break;
1140

1141
	case SHEET_OBJECT_LINE:
1142
		cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Line", NULL);
1143 1144 1145 1146 1147
		break;
	}
	if (cur == NULL)
		return NULL;
	
1148 1149 1150
	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);
1151

1152
	return cur;
1153 1154 1155 1156 1157
}

/*
 * Create a Object equivalent to the XML subtree of doc.
 */
1158
static SheetObject *
1159
xml_read_sheet_object (parse_xml_context_t *ctxt, xmlNodePtr tree)
1160 1161
{
	SheetObject *ret;
1162
	SheetObjectFilled *sof;
1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
	char *color;
	char *fill_color;
	int type;
	double x1, y1, x2, y2;
	int width = 1;
	int pattern;

	if (!strcmp (tree->name, "Rectangle")){
		type = SHEET_OBJECT_RECTANGLE;
	} else if (!strcmp (tree->name, "Ellipse")){
		type = SHEET_OBJECT_ELLIPSE;
	} else if (!strcmp (tree->name, "Arrow")){
		type = SHEET_OBJECT_ARROW;
	} else if (!strcmp (tree->name, "Line")){
		type = SHEET_OBJECT_LINE;
	} else {
		fprintf (stderr,
1180
			 "xml_read_sheet_object: invalid element type %s, 'Rectangle/Ellipse ...' expected`\n",
1181 1182 1183 1184
			 tree->name);
		return NULL;
	}
	
1185 1186 1187
	color = (char *) xml_value_get (tree, "Color");
	xml_get_coordinates (tree, "Points", &x1, &y1, &x2, &y2);
	xml_get_value_int (tree, "Width", &width);
1188 1189
	if ((type == SHEET_OBJECT_RECTANGLE) ||
	    (type == SHEET_OBJECT_ELLIPSE)){
1190 1191
		fill_color = (char *) xml_value_get (tree, "FillColor");
		xml_get_value_int (tree, "Pattern", &pattern);
1192 1193 1194
		ret = sheet_object_create_filled (
			ctxt->sheet, type,
			x1, y1, x2, y2, fill_color, color, width);
1195
		if (ret != NULL){
1196 1197
			sof = SHEET_OBJECT_FILLED (ret);
			sof->pattern = pattern;
1198 1199
		}
	} else {
1200 1201 1202
		ret = sheet_object_create_line (
			ctxt->sheet, type,
			x1, y1, x2, y2, color, width);
1203
	}
1204
	sheet_object_realize (ret);
1205
	return ret;
1206 1207
}

1208 1209 1210
/*
 * Create an XML subtree of doc equivalent to the given Cell.
 */
1211
static xmlNodePtr
1212
xml_write_cell (parse_xml_context_t *ctxt, Cell *cell)
1213
{
1214
	xmlNodePtr cur;
1215
	char *text;
1216 1217 1218 1219
	int style_id;

	style_id = GPOINTER_TO_INT (g_hash_table_lookup (ctxt->style_table, cell->style));
	g_assert (style_id != 0);
1220
	
1221
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Cell", NULL);
1222 1223 1224
	xml_set_value_int (cur, "Col", cell->col->pos);
	xml_set_value_int (cur, "Row", cell->row->pos);
	xml_set_value_int (cur, "Style", style_id);
1225

1226
	text = cell_get_content (cell);
1227 1228
	xmlNewChild (cur, ctxt->ns, "Content",
		     xmlEncodeEntities(ctxt->doc, text));
1229
	g_free (text);
1230

1231 1232 1233 1234 1235 1236 1237 1238
 	text = cell_get_comment(cell);
 	if (text) {
		xmlNewChild(cur, ctxt->ns, "Comment", 
			    xmlEncodeEntities(ctxt->doc, text));
		g_free(text);
 	}
	

1239
	return cur;