xml-io.c 52.7 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
static char *BorderTypes[] =
586 587
{
	"none",
588 589 590 591 592 593
 	"thin",
 	"medium",
 	"dashed",
 	"dotted",
 	"thick",
 	"double",
594 595 596 597 598 599 600
	"hair",
	"medium_dash",
	"dash_dot",
	"medium_dash_dot",
	"dash_dot_dot",
	"medium_dash_dot_dot",
	"slanted_dash_dot"
601 602
};

Michael Meeks's avatar
Michael Meeks committed
603
static char *StyleSideNames[6] =
604 605 606 607
{
 	"Top",
 	"Bottom",
 	"Left",
Michael Meeks's avatar
Michael Meeks committed
608 609 610
 	"Right",
	"Diagonal",
	"Rev-Diagonal"
Arturo Espinosa's avatar
Arturo Espinosa committed
611
};
612

613
static xmlNodePtr
614 615
xml_write_style_border (parse_xml_context_t *ctxt,
			const MStyle *style)
616 617 618
{
	xmlNodePtr cur;
	xmlNodePtr side;
619
	int        i;
620
       
621 622
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_RIGHT; i++) {
		if (mstyle_is_element_set (style, i))
Arturo Espinosa's avatar
Arturo Espinosa committed
623
			break;
624 625
	}
	if (i > MSTYLE_BORDER_RIGHT)
Arturo Espinosa's avatar
Arturo Espinosa committed
626
		return NULL;
627

628
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleBorder", NULL);
629
	
Michael Meeks's avatar
Michael Meeks committed
630
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
Jody Goldberg's avatar
Jody Goldberg committed
631 632 633 634 635
		MStyleBorder const *border;
		if (mstyle_is_element_set (style, i) &&
		    NULL != (border = mstyle_get_border (style, i))) {
			StyleBorderType t = border->line_type;
			StyleColor *col   = border->color;
636 637 638 639
 			side = xmlNewChild (cur, ctxt->ns,
					    StyleSideNames [i - MSTYLE_BORDER_TOP],
 					    BorderTypes [t]);
 			xml_set_color_value (side, "Color", col);
640
 		}
641 642
	}
	return cur;
643 644 645 646 647
}

/*
 * Create a StyleBorder equivalent to the XML subtree of doc.
 */
648
static void
649
xml_read_style_border (parse_xml_context_t *ctxt, xmlNodePtr tree, MStyle *mstyle)
650 651
{
	xmlNodePtr side;
652
	int        i;
653 654 655

	if (strcmp (tree->name, "StyleBorder")){
		fprintf (stderr,
656
			 "xml_read_style_border: invalid element type %s, 'StyleBorder' expected`\n",
657 658
			 tree->name);
	}
659

Michael Meeks's avatar
Michael Meeks committed
660
	for (i = MSTYLE_BORDER_TOP; i <= MSTYLE_BORDER_REV_DIAGONAL; i++) {
661 662
 		if ((side = xml_search_child (tree,
					      StyleSideNames [i - MSTYLE_BORDER_TOP])) != NULL) {
663 664
			StyleColor   *color = NULL;
			MStyleBorder *border;
665
 			xml_get_color_value (side, "Color", &color);
Michael Meeks's avatar
Michael Meeks committed
666
			border = style_border_fetch (STYLE_BORDER_NONE, color, 
Michael Meeks's avatar
Michael Meeks committed
667
						     style_border_get_orientation (i));
668 669
			if (border)
				mstyle_set_border (mstyle, i, border);
670
 		}
671
	}
672 673 674 675 676
}

/*
 * Create an XML subtree of doc equivalent to the given Style.
 */
677
static xmlNodePtr
678 679
xml_write_style (parse_xml_context_t *ctxt,
		 MStyle *style)
680 681 682
{
	xmlNodePtr cur, child;

683
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "Style", NULL);
684
	
685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
	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)) {
701
		if (!style_is_default_back (mstyle_get_color (style, MSTYLE_COLOR_BACK)))
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
			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";
717

718
		child = xmlNewChild (cur, ctxt->ns, "Font", 
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
				     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);
734 735

	return cur;
736 737
}

738
static xmlNodePtr
739
xml_write_names (parse_xml_context_t *ctxt, GList *names)
740
{
741
	xmlNodePtr  cur;
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

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

768 769 770 771
	return cur;
}

static void
772 773
xml_read_names (parse_xml_context_t *ctxt, xmlNodePtr tree, Workbook *wb,
		Sheet *sheet)
774 775 776 777 778 779 780 781
{
	xmlNodePtr child;

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

	child = tree->childs;
	while (child) {
782
		char *name  = NULL;
783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
		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"));

800
					if (!expr_name_create (wb, sheet, name, txt, &error))
801 802 803 804
						g_warning (error);

					g_free (txt);
				}
805
				bits = bits->next;
806 807 808 809 810 811
			}
		}
		child = child->next;
	}
}

812
static xmlNodePtr
813
xml_write_summary (parse_xml_context_t *ctxt, SummaryInfo *summary_info)
814 815 816 817
{
	GList *items, *m;
	xmlNodePtr cur;

818
	if (!summary_info)
819 820
		return NULL;

821
	m = items = summary_info_as_list (summary_info);
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851

	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);
852
			xmlAddChild (cur, tmp);
853 854 855 856 857 858 859 860
		}
		items = g_list_next (items);
	}
	g_list_free (m);
	return cur;
}

static void
861
xml_read_summary (parse_xml_context_t *ctxt, xmlNodePtr tree, SummaryInfo *summary_info)
862
{
863
	xmlNodePtr child;
864

865 866 867
	g_return_if_fail (ctxt != NULL);
	g_return_if_fail (tree != NULL);
	g_return_if_fail (summary_info != NULL);
868 869 870

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

873 874 875 876 877 878 879 880
		if (child->name && !strcmp (child->name, "Item")) {
			xmlNodePtr bits;

			bits = child->childs;
			while (bits) {
				SummaryItem *sit = NULL;
				
				if (!strcmp (bits->name, "name")) {
881
					name = xmlNodeGetContent (bits);
882 883 884 885 886
				} else {
					char *txt;
					g_return_if_fail (name);

					txt = xmlNodeGetContent (bits);
887 888 889 890
					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
891
							sit = summary_item_new_int (name, atoi (txt));
892 893
						
						if (sit)
894
							summary_info_add (summary_info, sit);
895 896
						g_free (txt);
					}
897
				}
898
				bits = bits->next;
899 900
			}
		}
Miguel de Icaza's avatar
Miguel de Icaza committed
901 902 903 904
		if (name){
			free (name);
			name = NULL;
		}
905 906 907 908
		child = child->next;
	}
}

Michael Meeks's avatar
Michael Meeks committed
909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927
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
928
xml_get_print_hf (xmlNodePtr node, PrintHF *const hf)
Michael Meeks's avatar
Michael Meeks committed
929 930 931 932 933 934 935
{
	char *txt;
	
	g_return_if_fail (hf != NULL);
	g_return_if_fail (node != NULL);

	txt = xml_value_get (node, "Left");
936
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
937 938 939
		if (hf->left_format)
			g_free (hf->left_format);
		
Michael Meeks's avatar
Michael Meeks committed
940
		hf->left_format = g_strdup (txt);
Miguel de Icaza's avatar
Miguel de Icaza committed
941
		free (txt);
Arturo Espinosa's avatar
Arturo Espinosa committed
942 943
	}
	
Michael Meeks's avatar
Michael Meeks committed
944
	txt = xml_value_get (node, "Middle");
945
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
946 947 948
		if (hf->middle_format)
			g_free (hf->middle_format);
		
Michael Meeks's avatar
Michael Meeks committed
949
		hf->middle_format = g_strdup (txt);
Miguel de Icaza's avatar
Miguel de Icaza committed
950
		free (txt);
Arturo Espinosa's avatar
Arturo Espinosa committed
951
	}
Michael Meeks's avatar
Michael Meeks committed
952 953

	txt = xml_value_get (node, "Right");
954
	if (txt) {
Arturo Espinosa's avatar
Arturo Espinosa committed
955 956 957
		if (hf->right_format)
			g_free (hf->right_format);
		
Michael Meeks's avatar
Michael Meeks committed
958
		hf->right_format = g_strdup (txt);
Miguel de Icaza's avatar
Miguel de Icaza committed
959
		free (txt);
Arturo Espinosa's avatar
Arturo Espinosa committed
960
	}
Michael Meeks's avatar
Michael Meeks committed
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 992 993 994 995 996 997 998 999
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
1000 1001 1002 1003
	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");
1004 1005
	xmlAddChild (cur, child);

Michael Meeks's avatar
Michael Meeks committed
1006 1007 1008 1009
	if (pi->orientation == PRINT_ORIENT_VERTICAL)
		child = xmlNewDocNode (ctxt->doc, ctxt->ns, "orientation", "portrait");
	else
		child = xmlNewDocNode (ctxt->doc, ctxt->ns, "orientation", "landscape");
1010 1011
	xmlAddChild (cur, child);

Michael Meeks's avatar
Michael Meeks committed
1012 1013 1014
	xml_set_print_hf (cur, "Header", pi->header);
	xml_set_print_hf (cur, "Footer", pi->footer);

1015 1016
	child = xmlNewDocNode (ctxt->doc, ctxt->ns, "paper", gnome_paper_name (pi->paper));
	xmlAddChild (cur, child);
Michael Meeks's avatar
Michael Meeks committed
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 1066 1067 1068 1069 1070 1071 1072 1073
	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
1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095
	
	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));
1096 1097
}

1098 1099 1100 1101 1102
static const char *
font_component (const char *fontname, int idx)
{
	int i = 0;
	const char *p = fontname;
1103
      
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114
	for (; *p && i < idx; p++){
		if (*p == '-')
			i++;
	}
	if (*p == '-')
		p++;

	return p;
}

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

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

1142 1143
	if (strncmp (c, "i", 1) == 0)
		mstyle_set_font_italic (mstyle, TRUE);
1144 1145
}

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

1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186
	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);

1187
	prop = xml_value_get (tree, "Format");
1188 1189
	if (prop != NULL) {
		mstyle_set_format (mstyle, prop);
Morten Welinder's avatar
Morten Welinder committed
1190
		free (prop);
1191
	}
1192

1193
	child = tree->childs;
1194 1195
	while (child != NULL) {
		if (!strcmp (child->name, "Font")) {
1196 1197 1198 1199 1200 1201 1202
			char *font;
			double units = 14;
			int t;
				
			xml_get_value_double (child, "Unit", &units);

			if (xml_get_value_int (child, "Bold", &t))
1203 1204
				mstyle_set_font_bold (mstyle, t);

1205
			if (xml_get_value_int (child, "Italic", &t))
1206
				mstyle_set_font_italic (mstyle, t);
1207
			
1208 1209 1210 1211 1212 1213
			font = xmlNodeGetContent (child);
			if (font) {
				if (*font == '-')
					style_font_read_from_x11 (mstyle, font);
				else
					mstyle_set_font_name (mstyle, font);
1214
			}
1215 1216 1217
			
		} else if (!strcmp (child->name, "StyleBorder")) {
			xml_read_style_border (ctxt, child, mstyle);
1218
		} else {
1219
			fprintf (stderr, "xml_read_style: unknown type '%s'\n",
1220 1221 1222 1223 1224
				 child->name);
		}
		child = child->next;
	}

1225
	return mstyle;
1226 1227
}

1228 1229 1230
/*
 * Create an XML subtree of doc equivalent to the given StyleRegion.
 */
1231
static xmlNodePtr
1232
xml_write_style_region (parse_xml_context_t *ctxt, StyleRegion *region)
1233 1234 1235
{
	xmlNodePtr cur, child;

1236
	cur = xmlNewDocNode (ctxt->doc, ctxt->ns, "StyleRegion", NULL);
1237 1238 1239 1240
	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);
1241

1242
	if (region->style != NULL) {
1243
		child = xml_write_style (ctxt, region->style);
1244 1245 1246 1247
		if (child)
			xmlAddChild (cur, child);
	}
	return cur;
1248 1249 1250 1251 1252
}

/*
 * Create a StyleRegion equivalent to the XML subtree of doc.
 */
1253
static void
1254
xml_read_style_region (parse_xml_context_t *ctxt, xmlNodePtr tree)
1255 1256
{
	xmlNodePtr child;
1257 1258
	MStyle    *style = NULL;
	Range      range;
1259 1260 1261

	if (strcmp (tree->name, "StyleRegion")){
		fprintf (stderr,
1262
			 "xml_read_style_region: invalid element type %s, 'StyleRegion' expected`\n",
1263 1264 1265
			 tree->name);
		return;
	}
1266 1267 1268 1269
	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);
1270
	child = tree->childs;
1271

1272 1273 1274 1275 1276
	if (child)
		style = xml_read_style (ctxt, child);

	if (style)
		sheet_style_attach (ctxt->sheet, range, style);
1277