xml-sax-read.c 60.8 KB
Newer Older
1
/* vim: set sw=8: */
2 3

/*
4
 * xml-sax-read.c : a test harness for the sax based xml parse routines.
5
 *
6
 * Copyright (C) 2000 Jody Goldberg (jody@gnome.org)
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
 * USA
 */

24 25
#include <gnumeric-config.h>
#include <gnumeric.h>
Jody Goldberg's avatar
Jody Goldberg committed
26
#include "xml-io-version.h"
27
#include "io-context.h"
28 29
#include "plugin.h"
#include "plugin-util.h"
30
#include "module-plugin-defs.h"
Jody Goldberg's avatar
Jody Goldberg committed
31
#include "sheet-style.h"
32
#include "sheet-merge.h"
Jody Goldberg's avatar
Jody Goldberg committed
33
#include "sheet.h"
34
#include "ranges.h"
35
#include "style.h"
36
#include "style-border.h"
37
#include "style-color.h"
38
#include "style-condition.h"
39
#include "format.h"
40
#include "cell.h"
41 42
#include "position.h"
#include "expr.h"
43
#include "expr-name.h"
44
#include "print-info.h"
45
#include "validation.h"
46 47
#include "value.h"
#include "selection.h"
48
#include "command-context.h"
49
#include "workbook-view.h"
50
#include "workbook.h"
51
#include "error-info.h"
52

53 54
#include <stdlib.h>
#include <string.h>
Jody Goldberg's avatar
Jody Goldberg committed
55
#include <gnome.h>
56 57 58
#include <libxml/tree.h>
#include <libxml/parser.h>
#include <libxml/parserInternals.h>
59

60 61
GNUMERIC_MODULE_PLUGIN_INFO_DECL;

62 63
gboolean xml_sax_file_probe (GnumFileOpener const *fo, const gchar *file_name,
                             FileProbeLevel pl);
64
void     xml_sax_file_open (GnumFileOpener const *fo, IOContext *io_context,
65
			    WorkbookView *wb_view, char const *filename);
66

67 68 69
/*****************************************************************************/

static int
70
xml_sax_attr_double (xmlChar const * const *attrs, char const *name, double * res)
71 72 73
{
	char *end;
	double tmp;
74

75 76 77 78 79 80 81
	g_return_val_if_fail (attrs != NULL, FALSE);
	g_return_val_if_fail (attrs[0] != NULL, FALSE);
	g_return_val_if_fail (attrs[1] != NULL, FALSE);

	if (strcmp (attrs[0], name))
		return FALSE;

Jody Goldberg's avatar
Jody Goldberg committed
82
	tmp = g_strtod ((gchar *)attrs[1], &end);
83 84 85 86 87 88 89 90 91
	if (*end) {
		g_warning ("Invalid attribute '%s', expected double, received '%s'",
			   name, attrs[1]);
		return FALSE;
	}
	*res = tmp;
	return TRUE;
}
static gboolean
92
xml_sax_double (xmlChar const *chars, double *res)
93
{
94
	char *end;
Jody Goldberg's avatar
Jody Goldberg committed
95
	*res = g_strtod ((gchar *)chars, &end);
96
	return *end == '\0';
97 98 99
}

static int
100
xml_sax_attr_bool (xmlChar const * const *attrs, char const *name, int *res)
101 102 103 104 105 106 107 108
{
	g_return_val_if_fail (attrs != NULL, FALSE);
	g_return_val_if_fail (attrs[0] != NULL, FALSE);
	g_return_val_if_fail (attrs[1] != NULL, FALSE);

	if (strcmp (attrs[0], name))
		return FALSE;

Jody Goldberg's avatar
Jody Goldberg committed
109
	*res = g_strcasecmp ((gchar *)attrs[1], "false") && strcmp (attrs[1], "0");
110 111 112 113 114

	return TRUE;
}

static int
115
xml_sax_attr_int (xmlChar const * const *attrs, char const *name, int *res)
116 117 118
{
	char *end;
	int tmp;
119

120 121 122 123 124 125 126
	g_return_val_if_fail (attrs != NULL, FALSE);
	g_return_val_if_fail (attrs[0] != NULL, FALSE);
	g_return_val_if_fail (attrs[1] != NULL, FALSE);

	if (strcmp (attrs[0], name))
		return FALSE;

Jody Goldberg's avatar
Jody Goldberg committed
127
	tmp = strtol ((gchar *)attrs[1], &end, 10);
128 129 130 131 132 133 134 135
	if (*end) {
		g_warning ("Invalid attribute '%s', expected integer, received '%s'",
			   name, attrs[1]);
		return FALSE;
	}
	*res = tmp;
	return TRUE;
}
136

137
static gboolean
138
xml_sax_int (xmlChar const *chars, int *res)
139
{
140
	char *end;
Jody Goldberg's avatar
Jody Goldberg committed
141
	*res = strtol ((gchar *)chars, &end, 10);
142
	return *end == '\0';
143
}
144

145
static gboolean
146
xml_sax_attr_cellpos (xmlChar const * const *attrs, char const *name, CellPos *val)
147 148 149 150 151 152 153 154 155 156
{
	CellPos tmp;

	g_return_val_if_fail (attrs != NULL, FALSE);
	g_return_val_if_fail (attrs[0] != NULL, FALSE);
	g_return_val_if_fail (attrs[1] != NULL, FALSE);

	if (strcmp (attrs[0], name))
		return FALSE;

Jody Goldberg's avatar
Jody Goldberg committed
157
	if (!parse_cell_name ((gchar *)attrs[1], &tmp.col, &tmp.row, TRUE, NULL)) {
158 159 160 161 162 163 164 165
		g_warning ("Invalid attribute '%s', expected cellpos, received '%s'",
			   name, attrs[1]);
		return FALSE;
	}
	*val = tmp;
	return TRUE;
}

166
static int
167
xml_sax_color (xmlChar const * const *attrs, char const *name, StyleColor **res)
168 169
{
	int red, green, blue;
170

171 172 173 174 175 176 177
	g_return_val_if_fail (attrs != NULL, FALSE);
	g_return_val_if_fail (attrs[0] != NULL, FALSE);
	g_return_val_if_fail (attrs[1] != NULL, FALSE);

	if (strcmp (attrs[0], name))
		return FALSE;

Jody Goldberg's avatar
Jody Goldberg committed
178
	if (sscanf ((gchar *)attrs[1], "%X:%X:%X", &red, &green, &blue) != 3){
179 180 181 182 183 184 185 186 187
		g_warning ("Invalid attribute '%s', expected colour, received '%s'",
			   name, attrs[1]);
		return FALSE;
	}
	*res = style_color_new (red, green, blue);
	return TRUE;
}

static gboolean
188
xml_sax_range (xmlChar const * const *attrs, Range *res)
189 190 191
{
	int flags = 0;
	for (; attrs[0] && attrs[1] ; attrs += 2)
192
		if (xml_sax_attr_int (attrs, "startCol", &res->start.col))
193
			flags |= 0x1;
194
		else if (xml_sax_attr_int (attrs, "startRow", &res->start.row))
195
			flags |= 0x2;
196
		else if (xml_sax_attr_int (attrs, "endCol", &res->end.col))
197
			flags |= 0x4;
198
		else if (xml_sax_attr_int (attrs, "endRow", &res->end.row))
199 200 201 202 203 204
			flags |= 0x8;
		else
			return FALSE;

	return flags == 0xf;
}
205 206 207 208 209 210 211

/*****************************************************************************/

#if 0
/*
 * Save a Workbook in an XML file
 */
212
void
213
xmlSax_write (Workbook *wb, char const *filename, ErrorInfo **ret_error)
214 215 216 217
{
	g_return_val_if_fail (wb != NULL, -1);
	g_return_val_if_fail (filename != NULL, -1);

218
	*ret_error = NULL;
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
}
#endif

/*****************************************************************************/

typedef enum {
STATE_START,

STATE_WB,
	STATE_WB_ATTRIBUTES,
		STATE_WB_ATTRIBUTES_ELEM,
			STATE_WB_ATTRIBUTES_ELEM_NAME,
			STATE_WB_ATTRIBUTES_ELEM_TYPE,
			STATE_WB_ATTRIBUTES_ELEM_VALUE,
	STATE_WB_SUMMARY,
		STATE_WB_SUMMARY_ITEM,
			STATE_WB_SUMMARY_ITEM_NAME,
236 237
			STATE_WB_SUMMARY_ITEM_VALUE_STR,
			STATE_WB_SUMMARY_ITEM_VALUE_INT,
238 239 240 241 242 243
	STATE_WB_SHEETNAME_INDEX,
		STATE_WB_SHEETNAME,
        STATE_NAMES,
                STATE_NAMES_NAME,
                        STATE_NAMES_NAME_NAME,
                        STATE_NAMES_NAME_VALUE,
244
                        STATE_NAMES_NAME_POSITION,
245 246 247 248 249 250 251
	STATE_WB_GEOMETRY,
	STATE_WB_SHEETS,
		STATE_SHEET,
			STATE_SHEET_NAME,	/* convert to attr */
			STATE_SHEET_MAXCOL,	/* convert to attr */
			STATE_SHEET_MAXROW,	/* convert to attr */
			STATE_SHEET_ZOOM,	/* convert to attr */
252 253 254 255
			STATE_SHEET_NAMES,
				STATE_SHEET_NAMES_NAME,
					STATE_SHEET_NAMES_NAME_NAME,
					STATE_SHEET_NAMES_NAME_VALUE,
256
					STATE_SHEET_NAMES_NAME_POSITION,
257
			STATE_SHEET_PRINTINFO,
258 259 260 261 262 263 264
                                STATE_PRINT_MARGINS,
					STATE_PRINT_MARGIN_TOP,
					STATE_PRINT_MARGIN_BOTTOM,
					STATE_PRINT_MARGIN_LEFT,
					STATE_PRINT_MARGIN_RIGHT,
					STATE_PRINT_MARGIN_HEADER,
					STATE_PRINT_MARGIN_FOOTER,
Jody Goldberg's avatar
Jody Goldberg committed
265
                                STATE_PRINT_SCALE,
266 267 268 269 270 271
				STATE_PRINT_VCENTER,
				STATE_PRINT_HCENTER,
				STATE_PRINT_GRID,
				STATE_PRINT_MONO,
				STATE_PRINT_DRAFTS,
				STATE_PRINT_TITLES,
272 273
				STATE_PRINT_REPEAT_TOP,
				STATE_PRINT_REPEAT_LEFT,
274 275 276 277 278
				STATE_PRINT_ORDER,
				STATE_PRINT_ORIENT,
				STATE_PRINT_HEADER,
				STATE_PRINT_FOOTER,
				STATE_PRINT_PAPER,
279
				STATE_PRINT_EVEN_ONLY_STYLE,
280 281 282 283 284 285 286 287 288 289 290
			STATE_SHEET_STYLES,
				STATE_STYLE_REGION,
					STATE_STYLE_STYLE,
						STATE_STYLE_FONT,
						STATE_STYLE_BORDER,
							STATE_BORDER_TOP,
							STATE_BORDER_BOTTOM,
							STATE_BORDER_LEFT,
							STATE_BORDER_RIGHT,
							STATE_BORDER_DIAG,
							STATE_BORDER_REV_DIAG,
291 292 293 294 295 296
						STATE_STYLE_VALIDATION,
							STATE_STYLE_CONDITION_CHAIN,
								STATE_STYLE_CONDITION,
									STATE_STYLE_CONDITION_EXPR,
									STATE_STYLE_CONDITION_CONSTRAINT,
									STATE_STYLE_CONDITION_FLAGS,
297 298 299 300 301 302 303 304 305
			STATE_SHEET_COLS,
				STATE_COL,
			STATE_SHEET_ROWS,
				STATE_ROW,
			STATE_SHEET_SELECTIONS,
				STATE_SELECTION,
			STATE_SHEET_CELLS,
				STATE_CELL,
					STATE_CELL_CONTENT,
306 307
			STATE_SHEET_MERGED_REGION,
				STATE_SHEET_MERGE,
308 309
			STATE_SHEET_LAYOUT,
				STATE_SHEET_FREEZEPANES,
310
			STATE_SHEET_SOLVER,
311
		STATE_SHEET_OBJECTS,
312
			STATE_OBJECT_POINTS,
313 314 315 316 317
			STATE_OBJECT_RECTANGLE,
			STATE_OBJECT_ELLIPSE,
			STATE_OBJECT_ARROW,
			STATE_OBJECT_LINE,

318 319 320
	STATE_WB_VIEW,

	STATE_UNKNOWN
321
} xmlSaxState;
322 323 324 325 326 327

/*
 * This is not complete yet.
 * I just pulled it out of a single saved gnumeric file.
 * However, with a bit of cleanup, we should be able to use it as the basis for a dtd.
 */
328
static char const * const xmlSax_state_names[] =
329 330 331 332 333 334 335 336 337 338 339 340
{
"START",
"gmr:Workbook",
	"gmr:Attributes",
		"gmr:Attribute",
			"gmr:name",
			"gmr:type",
			"gmr:value",
	"gmr:Summary",
		"gmr:Item",
			"gmr:name",
			"gmr:val-string",
341
			"gmr:val-int",
342 343 344 345 346 347
	"gmr:SheetNameIndex",
		"gmr:SheetName",
        "gmr:Names",
                "gmr:Name",
                        "gmr:name",
                        "gmr:value",
348
                        "gmr:position",
349 350 351 352 353 354 355
	"gmr:Geometry",
	"gmr:Sheets",
		"gmr:Sheet",
			"gmr:Name",
			"gmr:MaxCol",
			"gmr:MaxRow",
			"gmr:Zoom",
356 357 358 359
			"gmr:Names",
				"gmr:Name",
					"gmr:name",
					"gmr:value",
360
					"gmr:position",
361
			"gmr:PrintInformation",
362 363 364 365 366 367 368
				"gmr:Margins",
					"gmr:top",
					"gmr:bottom",
					"gmr:left",
					"gmr:right",
					"gmr:header",
					"gmr:footer",
Jody Goldberg's avatar
Jody Goldberg committed
369
				"gmr:Scale",
370 371 372 373 374 375
				"gmr:vcenter",
				"gmr:hcenter",
				"gmr:grid",
				"gmr:monochrome",
				"gmr:draft",
				"gmr:titles",
376 377
				"gmr:repeat_top",
				"gmr:repeat_left",
378 379 380 381 382
				"gmr:order",
				"gmr:orientation",
				"gmr:Footer",
				"gmr:Header",
				"gmr:paper",
383
				"gmr:even_if_only_styles",
384 385 386 387 388 389 390 391 392 393 394
			"gmr:Styles",
				"gmr:StyleRegion",
					"gmr:Style",
						"gmr:Font",
						"gmr:StyleBorder",
							"gmr:Top",
							"gmr:Bottom",
							"gmr:Left",
							"gmr:Right",
							"gmr:Diagonal",
							"gmr:Rev-Diagonal",
395 396 397 398 399 400 401
						"gmr:Validation",
							"gmr:StyleConditionChain",
								"gmr:StyleCondition",
									"gmr:Expr",
									"gmr:Constraint",
									"gmr:Flags",

402 403 404 405 406 407 408 409 410 411
			"gmr:Cols",
				"gmr:ColInfo",

			"gmr:Rows",
				"gmr:RowInfo",
			"gmr:Selections",
				"gmr:Selection",
			"gmr:Cells",
				"gmr:Cell",
					"gmr:Content",
412 413
			"gmr:MergedRegions",
				"gmr:Merge",
414 415
			"gmr:SheetLayout",
				"gmr:FreezePanes",
416
			"gmr:Solver",
417
			"gmr:Objects",
418
				"gmr:Points",
419 420 421 422
				"gmr:Rectangle",
				"gmr:Ellipse",
				"gmr:Arrow",
				"gmr:Line",
423 424 425 426 427 428
	"gmr:UIData",

	"Unknown",
NULL
};

429
typedef struct _XMLSaxParseState
430
{
431
	xmlSaxState state;
432 433 434
	gint	  unknown_depth;	/* handle recursive unknown tags */
	GSList	 *state_stack;

435 436 437
	IOContext 	*context;	/* The IOcontext managing things */
	WorkbookView	*wb_view;	/* View for the new workbook */
	Workbook	*wb;		/* The new workbook */
438
	GnumericXMLVersion version;
439 440 441 442

	Sheet *sheet;
	double sheet_zoom;

443 444 445 446 447 448 449 450
	/* Only valid while parsing attributes */
	struct {
		char *name;
		char *value;
		int   type;
	} attribute;
	GList *attributes;

451 452 453 454
	/* Only valid when parsing wb or sheet names */
	struct {
		char *name;
		char *value;
455
		char *position;
456
	} name;
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471

	struct {
		ValidationStyle  vs;
		char            *title;
		char            *msg;
	} validation;

	struct {
		StyleConditionType type;
		StyleConditionBool next_op;
		StyleConditionBool prev_next_op;

		StyleCondition *scf;
		StyleCondition *scl;			
	} condition;
472
	
473 474 475 476
	gboolean  style_range_init;
	Range	  style_range;
	MStyle   *style;

477
	CellPos cell;
478
	int expr_id, array_rows, array_cols;
479
	int value_type;
Jody Goldberg's avatar
Jody Goldberg committed
480
	unsigned char const *value_fmt;
481 482

	GString *content;
483

484 485 486 487 488 489 490 491 492
	int display_formulas;
	int hide_zero;
	int hide_grid;
	int hide_col_header;
	int hide_row_header;
	int display_outlines;
	int outline_symbols_below;
	int outline_symbols_right;

493 494
	/* expressions with ref > 1 a map from index -> expr pointer */
	GHashTable *expr_map;
495 496 497
} XMLSaxParseState;

static void
498
xml_sax_unknown_attr (XMLSaxParseState *state, xmlChar const * const *attrs, char const *name)
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537
{
	g_return_if_fail (attrs != NULL);

	/* FIXME : Use IOContext to get these messages back to the user. */
	if (state->version == GNUM_XML_LATEST)
		g_warning ("Unexpected attribute '%s'='%s' for element of type %s.",
			   name, attrs[0], attrs[1]);
}

static void
xml_sax_warning (XMLSaxParseState *state, const char *msg, ...)
{
	va_list args;

	va_start (args, msg);
	g_logv ("XML", G_LOG_LEVEL_WARNING, msg, args);
	va_end (args);
}

static void
xml_sax_error (XMLSaxParseState *state, const char *msg, ...)
{
	va_list args;

	va_start (args, msg);
	g_logv ("XML", G_LOG_LEVEL_CRITICAL, msg, args);
	va_end (args);
}

static void
xml_sax_fatal_error (XMLSaxParseState *state, const char *msg, ...)
{
	va_list args;

	va_start (args, msg);
	g_logv ("XML", G_LOG_LEVEL_ERROR, msg, args);
	va_end (args);
}

538 539 540

/****************************************************************************/

541
static void
542
xml_sax_wb (XMLSaxParseState *state, xmlChar const **attrs)
543 544 545 546 547 548 549
{
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
		if (strcmp (attrs[0], "xmlns:gmr") == 0) {
			static const struct {
				char const * const id;
				GnumericXMLVersion const version;
			} GnumericVersions [] = {
550
				{ "http://www.gnumeric.org/v8.dtd", GNUM_XML_V8 },	/* 0.71 */
551
				{ "http://www.gnome.org/gnumeric/v7", GNUM_XML_V7 },	/* 0.66 */
552 553 554 555 556 557 558 559 560 561
				{ "http://www.gnome.org/gnumeric/v6", GNUM_XML_V6 },	/* 0.62 */
				{ "http://www.gnome.org/gnumeric/v5", GNUM_XML_V5 },
				{ "http://www.gnome.org/gnumeric/v4", GNUM_XML_V4 },
				{ "http://www.gnome.org/gnumeric/v3", GNUM_XML_V3 },
				{ "http://www.gnome.org/gnumeric/v2", GNUM_XML_V2 },
				{ "http://www.gnome.org/gnumeric/", GNUM_XML_V1 },
				{ NULL }
			};
			int i;
			for (i = 0 ; GnumericVersions [i].id != NULL ; ++i )
562
				if (strcmp (attrs[1], GnumericVersions [i].id) == 0) {
563 564 565
					if (state->version != GNUM_XML_UNKNOWN)
						xml_sax_warning (state, "Multiple version specifications.  Assuming %d",
								state->version);
566
					else {
567
						state->version = GnumericVersions [i].version;
568 569
						break;
					}
570 571 572 573 574
				}
		} else
			xml_sax_unknown_attr (state, attrs, "Workbook");
}

575 576 577 578 579 580 581 582
static void
xml_sax_wb_sheetname (XMLSaxParseState *state)
{
	char const * content = state->content->str;
	Sheet *sheet = sheet_new (state->wb, content);
	workbook_sheet_attach (state->wb, sheet, NULL);
}

583
static void
584
xml_sax_wb_view (XMLSaxParseState *state, xmlChar const **attrs)
585 586 587 588
{
	int sheet_index;
	int width = -1, height = -1;

589
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
590
		if (xml_sax_attr_int (attrs, "SelectedTab", &sheet_index))
591
			wb_view_sheet_focus (state->wb_view,
592
				workbook_sheet_by_index (state->wb, sheet_index));
593 594
		else if (xml_sax_attr_int (attrs, "Width", &width)) ;
		else if (xml_sax_attr_int (attrs, "Height", &height)) ;
595
		else
596
			xml_sax_unknown_attr (state, attrs, "WorkbookView");
597 598

	if (width > 0 && height > 0)
599
		wb_view_preferred_size (state->wb_view, width, height);
600 601
}

602
static void
603
xml_sax_arg_set (GtkArg *arg, gchar *string)
604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642
{
	switch (arg->type) {
	case GTK_TYPE_CHAR:
		GTK_VALUE_CHAR (*arg) = string[0];
		break;
	case GTK_TYPE_UCHAR:
		GTK_VALUE_UCHAR (*arg) = string[0];
		break;
	case GTK_TYPE_BOOL:
		if (!strcmp (string, "TRUE"))
			GTK_VALUE_BOOL (*arg) = TRUE;
		else
			GTK_VALUE_BOOL (*arg) = FALSE;
		break;
	case GTK_TYPE_INT:
		GTK_VALUE_INT (*arg) = atoi (string);
		break;
	case GTK_TYPE_UINT:
		GTK_VALUE_UINT (*arg) = atoi (string);
		break;
	case GTK_TYPE_LONG:
		GTK_VALUE_LONG (*arg) = atol (string);
		break;
	case GTK_TYPE_ULONG:
		GTK_VALUE_ULONG (*arg) = atol (string);
		break;
	case GTK_TYPE_FLOAT:
		GTK_VALUE_FLOAT (*arg) = atof (string);
		break;
	case GTK_TYPE_DOUBLE:
		GTK_VALUE_DOUBLE (*arg) = atof (string);
		break;
	case GTK_TYPE_STRING:
		GTK_VALUE_STRING (*arg) = g_strdup (string);
		break;
	}
}

static void
643
xml_sax_finish_parse_wb_attr (XMLSaxParseState *state)
644 645 646 647 648 649 650 651 652
{
	GtkArg *arg;

	g_return_if_fail (state->attribute.name != NULL);
	g_return_if_fail (state->attribute.value != NULL);
	g_return_if_fail (state->attribute.type >= 0);

	arg = gtk_arg_new (state->attribute.type);
	arg->name = state->attribute.name;
653
	xml_sax_arg_set (arg, state->attribute.value);
654 655 656 657 658 659 660 661 662
	state->attributes = g_list_prepend (state->attributes, arg);

	state->attribute.type = -1;
	g_free (state->attribute.value);
	state->attribute.value = NULL;
	state->attribute.name = NULL;
}

static void
663
xml_sax_free_arg_list (GList *start)
664 665 666 667 668 669 670 671 672 673 674 675 676 677
{
	GList *list = start;
	while (list) {
		GtkArg *arg = list->data;
		if (arg) {
			g_free (arg->name);
			gtk_arg_free (arg, FALSE);
		}
		list = list->next;
	}
	g_list_free (start);
}

static void
678
xml_sax_attr_elem (XMLSaxParseState *state)
679
{
680 681 682
	char const * content = state->content->str;
	int const len = state->content->len;

683 684 685
	switch (state->state) {
	case STATE_WB_ATTRIBUTES_ELEM_NAME :
		g_return_if_fail (state->attribute.name == NULL);
686
		state->attribute.name = g_strndup (content, len);
687 688 689 690
		break;

	case STATE_WB_ATTRIBUTES_ELEM_VALUE :
		g_return_if_fail (state->attribute.value == NULL);
691
		state->attribute.value = g_strndup (content, len);
692 693 694 695 696
		break;

	case STATE_WB_ATTRIBUTES_ELEM_TYPE :
	{
		int type;
Jody Goldberg's avatar
Jody Goldberg committed
697
		if (xml_sax_int ((xmlChar *)content, &type))
698 699 700 701 702 703 704 705 706
			state->attribute.type = type;
		break;
	}

	default :
		g_assert_not_reached ();
	};
}

707
static void
708
xml_sax_sheet_start (XMLSaxParseState *state, xmlChar const **attrs)
709 710 711
{
	int tmp;

712 713 714 715 716 717
	state->hide_col_header = state->hide_row_header =
	state->display_formulas = state->hide_zero =
	state->hide_grid = state->display_outlines =
	state->outline_symbols_below = state->outline_symbols_right = -1;
	state->sheet_zoom = 1.; /* default */

718
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734
		if (xml_sax_attr_bool (attrs, "DisplayFormulas", &tmp))
			state->display_formulas = tmp;
		else if (xml_sax_attr_bool (attrs, "HideZero", &tmp))
			state->hide_zero = tmp;
		else if (xml_sax_attr_bool (attrs, "HideGrid", &tmp))
			state->hide_grid = tmp;
		else if (xml_sax_attr_bool (attrs, "HideColHeader", &tmp))
			state->hide_col_header = tmp;
		else if (xml_sax_attr_bool (attrs, "HideRowHeader", &tmp))
			state->hide_row_header = tmp;
		else if (xml_sax_attr_bool (attrs, "DisplayOutlines", &tmp))
			state->display_outlines = tmp;
		else if (xml_sax_attr_bool (attrs, "OutlineSymbolsBelow", &tmp))
			state->outline_symbols_below = tmp;
		else if (xml_sax_attr_bool (attrs, "OutlineSymbolsRight", &tmp))
			state->outline_symbols_right = tmp;
735
		else
736
			xml_sax_unknown_attr (state, attrs, "Sheet");
737 738
}

739
static void
740 741 742 743 744 745 746 747 748 749 750 751 752
xml_sax_sheet_end (XMLSaxParseState *state)
{
	g_return_if_fail (state->sheet != NULL);

	/* Init ColRowInfo's size_pixels and force a full respan */
	sheet_flag_recompute_spans (state->sheet);
	sheet_set_zoom_factor (state->sheet, state->sheet_zoom,
			       FALSE, FALSE);
	state->sheet = NULL;
}

static void
xml_sax_sheet_name (XMLSaxParseState *state)
753
{
754
	char const * content = state->content->str;
755 756
	g_return_if_fail (state->sheet == NULL);

757 758 759 760 761 762
	/*
	 * FIXME: Pull this out at some point, so we don't
	 * have to support < GNUM_XML_V7 anymore
	 */
	if (state->version >= GNUM_XML_V7) {
		state->sheet = workbook_sheet_by_name (state->wb, content);
763

764 765 766 767 768 769 770
		if (!state->sheet)
			xml_sax_fatal_error (state, "SheetNameIndex reading failed");
	} else {
		state->sheet = sheet_new (state->wb, content);
		workbook_sheet_attach (state->wb, state->sheet, NULL);
	}
	
771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786
	if (state->display_formulas >= 0)
		state->sheet->display_formulas = state->display_formulas;
	if (state->hide_zero >= 0)
		state->sheet->hide_zero = state->hide_zero;
	if (state->hide_grid >= 0)
		state->sheet->hide_grid = state->hide_grid;
	if (state->hide_col_header >= 0)
		state->sheet->hide_col_header = state->hide_col_header;
	if (state->hide_row_header >= 0)
		state->sheet->hide_row_header = state->hide_row_header;
	if (state->display_outlines >= 0)
		state->sheet->display_outlines = state->display_outlines;
	if (state->outline_symbols_below >= 0)
		state->sheet->outline_symbols_below = state->outline_symbols_below;
	if (state->outline_symbols_right >= 0)
		state->sheet->outline_symbols_right = state->outline_symbols_right;
787 788 789
}

static void
790
xml_sax_sheet_zoom (XMLSaxParseState *state)
791
{
792
	char const * content = state->content->str;
793 794 795 796
	double zoom;

	g_return_if_fail (state->sheet != NULL);

Jody Goldberg's avatar
Jody Goldberg committed
797
	if (xml_sax_double ((xmlChar *)content, &zoom))
798
		state->sheet_zoom = zoom;
799 800
}

801
static void
802
xml_sax_print_margins (XMLSaxParseState *state, xmlChar const **attrs)
803 804 805 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
{
	PrintInformation *pi;
	PrintUnit *pu;
	double points;

	g_return_if_fail (state->sheet != NULL);
	g_return_if_fail (state->sheet->print_info != NULL);

	pi = state->sheet->print_info;
	switch (state->state) {
	case STATE_PRINT_MARGIN_TOP:
		pu = &pi->margins.top;
		break;
	case STATE_PRINT_MARGIN_BOTTOM:
		pu = &pi->margins.bottom;
		break;
	case STATE_PRINT_MARGIN_LEFT:
		pu = &pi->margins.left;
		break;
	case STATE_PRINT_MARGIN_RIGHT:
		pu = &pi->margins.right;
		break;
	case STATE_PRINT_MARGIN_HEADER:
		pu = &pi->margins.header;
		break;
	case STATE_PRINT_MARGIN_FOOTER:
		pu = &pi->margins.footer;
		break;
	default:
		return;
	}

835
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
836
		if (xml_sax_attr_double (attrs, "Points", &points))
837 838
			pu->points = points;
		else if (!strcmp (attrs[0], "PrefUnit")) {
Jody Goldberg's avatar
Jody Goldberg committed
839
			if (!strcmp (attrs[1], "points"))
840
				pu->desired_display = UNIT_POINTS;
Jody Goldberg's avatar
Jody Goldberg committed
841
			else if (!strcmp (attrs[1], "mm"))
842
				pu->desired_display = UNIT_MILLIMETER;
Jody Goldberg's avatar
Jody Goldberg committed
843
			else if (!strcmp (attrs[1], "cm"))
844
				pu->desired_display = UNIT_CENTIMETER;
Jody Goldberg's avatar
Jody Goldberg committed
845
			else if (!strcmp (attrs[1], "in"))
846 847
				pu->desired_display = UNIT_INCH;
		} else
848
			xml_sax_unknown_attr (state, attrs, "Margin");
849 850 851
	}
}

Jody Goldberg's avatar
Jody Goldberg committed
852
static void
853
xml_sax_print_scale (XMLSaxParseState *state, xmlChar const **attrs)
Jody Goldberg's avatar
Jody Goldberg committed
854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875
{
	PrintInformation *pi;
	double percentage;
	int cols, rows;

	g_return_if_fail (state->sheet != NULL);
	g_return_if_fail (state->sheet->print_info != NULL);

	pi = state->sheet->print_info;
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
		if (!strcmp (attrs[0], "type"))
			pi->scaling.type = strcmp (attrs[1], "percentage")
				? SIZE_FIT : PERCENTAGE;
		else if (xml_sax_attr_double (attrs, "percentage", &percentage))
			pi->scaling.percentage = percentage;
		else if (xml_sax_attr_int (attrs, "cols", &cols))
			pi->scaling.dim.cols = cols;
		else if (xml_sax_attr_int (attrs, "rows", &rows))
			pi->scaling.dim.rows = rows;
	}
}

876
static void
877
xml_sax_selection_range (XMLSaxParseState *state, xmlChar const **attrs)
878 879
{
	Range r;
880
	if (xml_sax_range (attrs, &r))
881 882 883 884 885 886 887
		sheet_selection_add_range (state->sheet,
					   r.start.col, r.start.row,
					   r.start.col, r.start.row,
					   r.end.col, r.end.row);
}

static void
888
xml_sax_selection (XMLSaxParseState *state, xmlChar const **attrs)
889 890 891
{
	int col = -1, row = -1;

892
	sheet_selection_reset (state->sheet);
893

894
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
895 896
		if (xml_sax_attr_int (attrs, "CursorCol", &col)) ;
		else if (xml_sax_attr_int (attrs, "CursorRow", &row)) ;
897
		else
898
			xml_sax_unknown_attr (state, attrs, "Selection");
899 900 901

	g_return_if_fail (col >= 0);
	g_return_if_fail (row >= 0);
902 903 904 905
	g_return_if_fail (state->cell.col < 0);
	g_return_if_fail (state->cell.row < 0);
	state->cell.col = col;
	state->cell.row = row;
906 907 908
}

static void
909
xml_sax_selection_end (XMLSaxParseState *state)
910
{
911
	CellPos const pos = state->cell;
912

913
	state->cell.col = state->cell.row = -1;
914

915 916
	g_return_if_fail (pos.col >= 0);
	g_return_if_fail (pos.row >= 0);
917

918
	sheet_set_edit_pos (state->sheet, pos.col, pos.row);
919 920
}

921
static void
922
xml_sax_sheet_layout (XMLSaxParseState *state, xmlChar const **attrs)
923 924 925 926 927 928 929 930 931 932 933
{
	CellPos tmp;

	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
		if (xml_sax_attr_cellpos (attrs, "TopLeft", &tmp))
			sheet_set_initial_top_left (state->sheet, tmp.col, tmp.row);
		else
			xml_sax_unknown_attr (state, attrs, "SheetLayout");
}

static void
934
xml_sax_sheet_freezepanes (XMLSaxParseState *state, xmlChar const **attrs)
935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
{
	CellPos frozen_tl, unfrozen_tl;
	int flags = 0;

	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
		if (xml_sax_attr_cellpos (attrs, "FrozenTopLeft", &frozen_tl))
			flags |= 1;
		else if (xml_sax_attr_cellpos (attrs, "UnfrozenTopLeft", &unfrozen_tl))
			flags |= 2;
		else
			xml_sax_unknown_attr (state, attrs, "SheetLayout");

	if (flags == 3)
		sheet_freeze_panes (state->sheet, &frozen_tl, &unfrozen_tl);
}

951
static void
952
xml_sax_cols_rows (XMLSaxParseState *state, xmlChar const **attrs, gboolean is_col)
953 954 955 956 957 958 959 960 961 962 963 964 965 966 967
{
	double def_size;

	g_return_if_fail (state->sheet != NULL);

	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
		if (xml_sax_attr_double (attrs, "DefaultSizePts", &def_size)) {
			if (is_col)
				sheet_col_set_default_size_pts (state->sheet, def_size);
			else
				sheet_row_set_default_size_pts (state->sheet, def_size);
		}
}

static void
968
xml_sax_colrow (XMLSaxParseState *state, xmlChar const **attrs, gboolean is_col)
969 970 971 972
{
	ColRowInfo *cri = NULL;
	double size = -1.;
	int dummy;
973
	int count = 1;
974 975 976

	g_return_if_fail (state->sheet != NULL);

977
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
978
		if (xml_sax_attr_int (attrs, "No", &dummy)) {
979 980 981 982 983 984 985 986
			g_return_if_fail (cri == NULL);

			cri = is_col
				? sheet_col_fetch (state->sheet, dummy)
				: sheet_row_fetch (state->sheet, dummy);
		} else {
			g_return_if_fail (cri != NULL);

987 988 989
			if (xml_sax_attr_double (attrs, "Unit", &size)) ;
			else if (xml_sax_attr_int (attrs, "Count", &count)) ;
			else if (xml_sax_attr_int (attrs, "MarginA", &dummy))
990
				cri->margin_a = dummy;
991
			else if (xml_sax_attr_int (attrs, "MarginB", &dummy))
992
				cri->margin_b = dummy;
993
			else if (xml_sax_attr_int (attrs, "HardSize", &dummy))
994
				cri->hard_size = dummy;
995
			else if (xml_sax_attr_int (attrs, "Hidden", &dummy))
996
				cri->visible = !dummy;
997
			else if (xml_sax_attr_int (attrs, "Collapsed", &dummy))
998
				cri->is_collapsed = dummy;
999
			else if (xml_sax_attr_int (attrs, "OutlineLevel", &dummy))
1000
				cri->outline_level = dummy;
1001
			else
1002
				xml_sax_unknown_attr (state, attrs, "ColRow");
1003 1004 1005 1006 1007
		}
	}

	g_return_if_fail (cri != NULL && size > -1.);

1008 1009 1010 1011 1012
	if (is_col) {
		int pos = cri->pos;
		sheet_col_set_size_pts (state->sheet, pos, size, cri->hard_size);
		/* resize flags are already set only need to copy the sizes */
		while (--count > 0)
1013
			colrow_copy (sheet_col_fetch (state->sheet, ++pos), cri);
1014 1015
	} else {
		int pos = cri->pos;
1016
		sheet_row_set_size_pts (state->sheet, cri->pos, size, cri->hard_size);
1017 1018
		/* resize flags are already set only need to copy the sizes */
		while (--count > 0)
1019
			colrow_copy (sheet_row_fetch (state->sheet, ++pos), cri);
1020
	}
1021 1022
}

1023
static void
1024
xml_sax_style_region_start (XMLSaxParseState *state, xmlChar const **attrs)
1025 1026 1027 1028 1029 1030
{
	g_return_if_fail (state->style_range_init == FALSE);
	g_return_if_fail (state->style == NULL);

	state->style = mstyle_new ();
	state->style_range_init =
1031
		xml_sax_range (attrs, &state->style_range);
1032 1033 1034
}

static void
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
xml_sax_style_region_end (XMLSaxParseState *state)
{
	g_return_if_fail (state->style_range_init);
	g_return_if_fail (state->style != NULL);
	g_return_if_fail (state->sheet != NULL);

	sheet_style_set_range (state->sheet, &state->style_range, state->style);

	state->style_range_init = FALSE;
	state->style = NULL;
}

static void
1048
xml_sax_styleregion_start (XMLSaxParseState *state, xmlChar const **attrs)
1049 1050 1051 1052 1053 1054
{
	int val;
	StyleColor *colour;

	g_return_if_fail (state->style != NULL);

1055
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
1056
		if (xml_sax_attr_int (attrs, "HAlign", &val))
1057
			mstyle_set_align_h (state->style, val);
1058
		else if (xml_sax_attr_int (attrs, "VAlign", &val))
1059
			mstyle_set_align_v (state->style, val);
1060 1061

		/* Pre version V6 */
1062
		else if (xml_sax_attr_int (attrs, "Fit", &val))
1063 1064
			mstyle_set_wrap_text (state->style, val);

1065
		else if (xml_sax_attr_int (attrs, "WrapText", &val))
1066
			mstyle_set_wrap_text (state->style, val);
1067
		else if (xml_sax_attr_int (attrs, "Orient", &val))
1068
			mstyle_set_orientation (state->style, val);
1069
		else if (xml_sax_attr_int (attrs, "Shade", &val))
1070
			mstyle_set_pattern (state->style, val);
1071
		else if (xml_sax_attr_int (attrs, "Indent", &val))
Jody Goldberg's avatar
Jody Goldberg committed
1072
			mstyle_set_indent (state->style, val);
1073
		else if (xml_sax_color (attrs, "Fore", &colour))
1074
			mstyle_set_color (state->style, MSTYLE_COLOR_FORE, colour);
1075
		else if (xml_sax_color (attrs, "Back", &colour))
1076
			mstyle_set_color (state->style, MSTYLE_COLOR_BACK, colour);
1077
		else if (xml_sax_color (attrs, "PatternColor", &colour))
1078 1079
			mstyle_set_color (state->style, MSTYLE_COLOR_PATTERN, colour);
		else if (!strcmp (attrs[0], "Format"))
Jody Goldberg's avatar
Jody Goldberg committed
1080
			mstyle_set_format_text (state->style, (char *)attrs[1]);
1081 1082 1083 1084
		else if (xml_sax_attr_int (attrs, "Hidden", &val))
			mstyle_set_content_hidden (state->style, val);
		else if (xml_sax_attr_int (attrs, "Locked", &val))
			mstyle_set_content_locked (state->style, val);
1085
		else
1086
			xml_sax_unknown_attr (state, attrs, "StyleRegion");
1087 1088 1089 1090
	}
}

static void
1091
xml_sax_styleregion_font (XMLSaxParseState *state, xmlChar const **attrs)
1092 1093 1094 1095 1096 1097
{
	double size_pts = 10.;
	int val;

	g_return_if_fail (state->style != NULL);

1098
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
1099
		if (xml_sax_attr_double (attrs, "Unit", &size_pts))
1100
			mstyle_set_font_size (state->style, size_pts);
1101
		else if (xml_sax_attr_int (attrs, "Bold", &val))
1102
			mstyle_set_font_bold (state->style, val);
1103
		else if (xml_sax_attr_int (attrs, "Italic", &val))
1104
			mstyle_set_font_italic (state->style, val);
1105
		else if (xml_sax_attr_int (attrs, "Underline", &val))
1106
			mstyle_set_font_uline (state->style, (StyleUnderlineType)val);
1107
		else if (xml_sax_attr_int (attrs, "StrikeThrough", &val))
1108 1109
			mstyle_set_font_strike (state->style, val ? TRUE : FALSE);
		else
1110
			xml_sax_unknown_attr (state, attrs, "StyleFont");
1111
	}
1112
}
1113

1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162
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_read_from_x11:
 * @mstyle: the style to setup to this font.
 * @fontname: an X11-like font name.
 *
 * Tries to guess the fontname, the weight and italization parameters
 * and setup mstyle
 *
 * Returns: A valid style font.
 */
static void
style_font_read_from_x11 (MStyle *mstyle, const char *fontname)
{
	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)
		mstyle_set_font_bold (mstyle, TRUE);

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

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

static void
1163
xml_sax_styleregion_font_end (XMLSaxParseState *state)
1164 1165 1166 1167 1168
{
	if (state->content->len > 0) {
		char const * content = state->content->str;
		if (*content == '-')
			style_font_read_from_x11 (state->style, content);
1169
		else
1170
			mstyle_set_font_name (state->style, content);
1171 1172 1173
	}
}

1174
static void
1175
xml_sax_validation (XMLSaxParseState *state, xmlChar const **attrs)
1176 1177 1178 1179
{
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
		if (xml_sax_attr_int (attrs, "Style", (int *) &state->validation.vs)) {
		} else if (!strcmp (attrs[0], "Title")) {
Jody Goldberg's avatar
Jody Goldberg committed
1180
			state->validation.title = g_strdup ((gchar *)attrs[1]);
1181
		} else if (!strcmp (attrs[0], "Message")) {
Jody Goldberg's avatar
Jody Goldberg committed
1182
			state->validation.msg = g_strdup ((gchar *)attrs[1]);			
1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213
		} else
			xml_sax_unknown_attr (state, attrs, "StyleCondition");
	}
	state->condition.scf = NULL;
	state->condition.scl = NULL;
}

static void
xml_sax_validation_end (XMLSaxParseState *state)
{
	Validation *v;

	g_return_if_fail (state->style != NULL);

	v = validation_new (state->validation.vs, state->validation.title,
			    state->validation.msg, state->condition.scf);
	mstyle_set_validation (state->style, v);

	if (state->validation.title) {
		g_free (state->validation.title);
		state->validation.title = NULL;
	}
	if (state->validation.msg) {
		g_free (state->validation.msg);
		state->validation.msg = NULL;
	}
	state->condition.scf = NULL;
	state->condition.scl = NULL;
}

static void
1214
xml_sax_style_condition (XMLSaxParseState *state, xmlChar const **attrs)
1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
{
	state->condition.prev_next_op = state->condition.next_op;
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
		if (xml_sax_attr_int (attrs, "Type", (int *) &state->condition.type));
		else if (xml_sax_attr_int (attrs, "NextOperator", (int *) &state->condition.next_op));
		else
			xml_sax_unknown_attr (state, attrs, "StyleCondition");
	}
}

static void
xml_sax_style_condition_chain (XMLSaxParseState *state, StyleCondition *sc)
{
	if (state->condition.scl) {
		style_condition_chain (state->condition.scl, state->condition.prev_next_op, sc);
		state->condition.scl = sc;
	} else {
		state->condition.scl = sc;
		state->condition.scf = sc;
	}
}

static void
1238
xml_sax_style_condition_expr (XMLSaxParseState *state, xmlChar const **attrs)
1239 1240 1241
{
	StyleConditionOperator op = -1;
	StyleCondition *sc;
Jody Goldberg's avatar
Jody Goldberg committed
1242
	unsigned char const *cexpr = NULL;
1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256
	ExprTree *expr = NULL;
	
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
		if (xml_sax_attr_int (attrs, "Operator", (int *) &op));
		else if (!strcmp (attrs[0], "Expression"))
			 cexpr = attrs[1];
		else
			xml_sax_unknown_attr (state, attrs, "StyleConditionExpr");
	}

	if (cexpr) {
		ParsePos pos, *pp;
		
		pp = parse_pos_init (&pos, state->wb, state->sheet, 0, 0);
Jody Goldberg's avatar
Jody Goldberg committed
1257
		if ((expr = expr_parse_str_simple ((char *)cexpr, pp)) == NULL)
1258 1259 1260 1261
			g_warning ("XML-IO: empty/missing expression in StyleCondition");
	} else
		g_warning ("StyleConditionExpression without Expression!");

1262
	sc = style_condition_new_expr (op, expr);
1263 1264 1265 1266
	xml_sax_style_condition_chain (state, sc);
}

static void
1267
xml_sax_style_condition_constraint (XMLSaxParseState *state, xmlChar const **attrs)
1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
{
	StyleCondition *sc;
	StyleConditionConstraint constraint = -1;
	
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
		if (xml_sax_attr_int (attrs, "Value", (int *) &constraint));
		else
			xml_sax_unknown_attr (state, attrs, "StyleConditionConstraint");
	}

	sc = style_condition_new_constraint (constraint);
	xml_sax_style_condition_chain (state, sc);
}

static void
1283
xml_sax_style_condition_flags (XMLSaxParseState *state, xmlChar const **attrs)
1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
{
	StyleCondition *sc;
	StyleConditionFlags flags;

	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
		if (xml_sax_attr_int (attrs, "Value", (int *) &flags));
		else
			xml_sax_unknown_attr (state, attrs, "StyleConditionFlags");
	}

	sc = style_condition_new_flags (flags);
	xml_sax_style_condition_chain (state, sc);	
}

1298
static void
1299
xml_sax_style_region_borders (XMLSaxParseState *state, xmlChar const **attrs)
1300
{
1301 1302 1303
	int pattern = -1;
	StyleColor *colour = NULL;

1304 1305
	g_return_if_fail (state->style != NULL);

1306
	/* Colour is optional */
1307
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
1308 1309
		if (xml_sax_color (attrs, "Color", &colour)) ;
		else if (xml_sax_attr_int (attrs, "Style", &pattern)) ;
1310
		else
1311
			xml_sax_unknown_attr (state, attrs, "StyleBorder");
1312
	}
1313

1314
	if (pattern >= STYLE_BORDER_NONE) {
1315 1316
		MStyleElementType const type = MSTYLE_BORDER_TOP +
			state->state - STATE_BORDER_TOP;
Jody Goldberg's avatar
Jody Goldberg committed
1317
		StyleBorder *border =
1318 1319
			style_border_fetch ((StyleBorderType)pattern, colour,
					    style_border_get_orientation (type));
1320
		mstyle_set_border (state->style, type, border);
1321
	}
1322 1323 1324
}

static void
1325
xml_sax_cell (XMLSaxParseState *state, xmlChar const **attrs)
1326 1327
{
	int row = -1, col = -1;
1328
	int rows = -1, cols = -1;
1329
	int value_type = -1;
Jody Goldberg's avatar
Jody Goldberg committed
1330
	unsigned char const *value_fmt = NULL;
1331
	int expr_id = -1;
1332

1333 1334
	g_return_if_fail (state->cell.row == -1);
	g_return_if_fail (state->cell.col == -1);
1335 1336 1337
	g_return_if_fail (state->array_rows == -1);
	g_return_if_fail (state->array_cols == -1);
	g_return_if_fail (state->expr_id == -1);
1338
	g_return_if_fail (state->value_type == -1);
1339

1340
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
1341 1342 1343 1344 1345 1346
		if (xml_sax_attr_int (attrs, "Col", &col)) ;
		else if (xml_sax_attr_int (attrs, "Row", &row)) ;
		else if (xml_sax_attr_int (attrs, "Cols", &cols)) ;
		else if (xml_sax_attr_int (attrs, "Rows", &rows)) ;
		else if (xml_sax_attr_int (attrs, "ExprID", &expr_id)) ;
		else if (xml_sax_attr_int (attrs, "ValueType", &value_type)) ;
1347 1348 1349
		else if (!strcmp (attrs[0], "ValueFormat"))
			value_fmt = attrs[1];
		else
1350
			xml_sax_unknown_attr (state, attrs, "Cell");
1351 1352 1353 1354 1355
	}

	g_return_if_fail (col >= 0);
	g_return_if_fail (row >= 0);

1356 1357 1358 1359 1360 1361 1362 1363 1364
	if (cols > 0 || rows > 0) {
		/* Both must be valid */
		g_return_if_fail (cols <= 0);
		g_return_if_fail (rows <= 0);

		state->array_cols = cols;
		state->array_rows = rows;
	}

1365 1366
	state->cell.row = row;
	state->cell.col = col;
1367
	state->expr_id = expr_id;
1368 1369
	state->value_type = value_type;
	state->value_fmt = value_fmt;
1370 1371
}

1372 1373 1374 1375 1376 1377 1378 1379 1380
/**
 * xml_cell_set_array_expr : Utility routine to parse an expression
 *     and store it as an array.
 *
 * @cell : The upper left hand corner of the array.
 * @text : The text to parse.
 * @rows : The number of rows.
 * @cols : The number of columns.
 */
1381
static void
1382 1383
xml_cell_set_array_expr (Cell *cell, char const *text,
			 int const cols, int const rows)
1384
{
1385
	ParsePos pp;
1386
	ExprTree *expr;
1387

1388
	expr = expr_parse_str_simple (text, parse_pos_init_cell (&pp, cell));
1389 1390

	g_return_if_fail (expr != NULL);
1391
	cell_set_array_formula (cell->base.sheet,
1392 1393 1394
				cell->pos.col, cell->pos.row,
				cell->pos.col + cols-1, cell->pos.row + rows-1,
				expr);
1395
}
1396

1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407
/**
 * xml_not_used_old_array_spec : See if the string corresponds to
 *     a pre-0.53 style array expression.
 *     If it is the upper left corner	 - assign it.
 *     If it is a member of the an array - ignore it the corner will assign it.
 *     If it is not a member of an array return TRUE.
 */
static gboolean
xml_not_used_old_array_spec (Cell *cell, char const *content)
{
	int rows, cols, row, col;
1408

1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425
#if 0
	/* This is the syntax we are trying to parse */
	g_string_sprintfa (str, "{%s}(%d,%d)[%d][%d]", expr_text,
			   array.rows, array.cols, array.y, array.x);
#endif
	char *end, *expr_end, *ptr;

	if (content[0] != '=' || content[1] != '{')
		return TRUE;

	expr_end = strrchr (content, '}');
	if (expr_end == NULL || expr_end[1] != '(')
		return TRUE;

	rows = strtol (ptr = expr_end + 2, &end, 10);
	if (end == ptr || *end != ',')
		return TRUE;
Jody Goldberg's avatar
Jody Goldberg committed
1426
	cols = strtol (ptr = end + 1, &end, 10);
1427 1428
	if (end == ptr || end[0] != ')' || end[1] != '[')
		return TRUE;
Jody Goldberg's avatar
Jody Goldberg committed
1429
	row = strtol (ptr = end + 2, &end, 10);
1430 1431
	if (end == ptr || end[0] != ']' || end[1] != '[')
		return TRUE;
Jody Goldberg's avatar
Jody Goldberg committed
1432
	col = strtol (ptr = end + 2, &end, 10);
1433 1434