xml-sax-read.c 51.5 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 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Copyright (C) 2000 Jody Goldberg (jgoldberg@home.com)
 *
 * 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
 */

#include "config.h"
25
#include <gnome.h>
26
#include "gnumeric.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 38
#include "style-color.h"
#include "format.h"
39
#include "cell.h"
40 41
#include "position.h"
#include "expr.h"
42
#include "expr-name.h"
43
#include "print-info.h"
44 45
#include "value.h"
#include "selection.h"
46
#include "command-context.h"
47
#include "workbook-view.h"
48
#include "workbook.h"
49
#include "error-info.h"
50

51 52
#include <stdlib.h>
#include <string.h>
53 54 55 56
#include <gnome-xml/tree.h>
#include <gnome-xml/parser.h>
#include <gnome-xml/parserInternals.h>

57 58
GNUMERIC_MODULE_PLUGIN_INFO_DECL;

Chyla Zbigniew's avatar
Chyla Zbigniew committed
59 60
gboolean xml_sax_file_probe (GnumFileOpener const *fo, const gchar *file_name);
void     xml_sax_file_open (GnumFileOpener const *fo, IOContext *io_context,
61
			    WorkbookView *wb_view, char const *filename);
62

63 64 65
/*****************************************************************************/

static int
66
xml_sax_attr_double (CHAR const * const *attrs, char const *name, double * res)
67 68 69
{
	char *end;
	double tmp;
70

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
	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;

	tmp = g_strtod (attrs[1], &end);
	if (*end) {
		g_warning ("Invalid attribute '%s', expected double, received '%s'",
			   name, attrs[1]);
		return FALSE;
	}
	*res = tmp;
	return TRUE;
}
static gboolean
88
xml_sax_double (CHAR const *chars, double *res)
89
{
90 91 92
	char *end;
	*res = g_strtod (chars, &end);
	return *end == '\0';
93 94 95
}

static int
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
xml_sax_attr_bool (CHAR const * const *attrs, char const *name, int *res)
{
	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;

	*res = g_strcasecmp (attrs[1], "false") && strcmp (attrs[1], "0");

	return TRUE;
}

static int
xml_sax_attr_int (CHAR const * const *attrs, char const *name, int *res)
112 113 114
{
	char *end;
	int tmp;
115

116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	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;

	tmp = strtol (attrs[1], &end, 10);
	if (*end) {
		g_warning ("Invalid attribute '%s', expected integer, received '%s'",
			   name, attrs[1]);
		return FALSE;
	}
	*res = tmp;
	return TRUE;
}
132

133
static gboolean
134
xml_sax_int (CHAR const *chars, int *res)
135
{
136 137 138
	char *end;
	*res = strtol (chars, &end, 10);
	return *end == '\0';
139
}
140 141

static int
142
xml_sax_color (CHAR const * const *attrs, char const *name, StyleColor **res)
143 144
{
	int red, green, blue;
145

146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	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;

	if (sscanf (attrs[1], "%X:%X:%X", &red, &green, &blue) != 3){
		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
163
xml_sax_range (CHAR const * const *attrs, Range *res)
164 165 166
{
	int flags = 0;
	for (; attrs[0] && attrs[1] ; attrs += 2)
167
		if (xml_sax_attr_int (attrs, "startCol", &res->start.col))
168
			flags |= 0x1;
169
		else if (xml_sax_attr_int (attrs, "startRow", &res->start.row))
170
			flags |= 0x2;
171
		else if (xml_sax_attr_int (attrs, "endCol", &res->end.col))
172
			flags |= 0x4;
173
		else if (xml_sax_attr_int (attrs, "endRow", &res->end.row))
174 175 176 177 178 179
			flags |= 0x8;
		else
			return FALSE;

	return flags == 0xf;
}
180 181 182 183 184 185 186

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

#if 0
/*
 * Save a Workbook in an XML file
 */
187
void
188
xmlSax_write (Workbook *wb, char const *filename, ErrorInfo **ret_error)
189 190 191 192
{
	g_return_val_if_fail (wb != NULL, -1);
	g_return_val_if_fail (filename != NULL, -1);

193
	*ret_error = NULL;
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
}
#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,
211 212
			STATE_WB_SUMMARY_ITEM_VALUE_STR,
			STATE_WB_SUMMARY_ITEM_VALUE_INT,
213 214 215 216 217 218
	STATE_WB_SHEETNAME_INDEX,
		STATE_WB_SHEETNAME,
        STATE_NAMES,
                STATE_NAMES_NAME,
                        STATE_NAMES_NAME_NAME,
                        STATE_NAMES_NAME_VALUE,
219 220 221 222 223 224 225
	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 */
226 227 228 229
			STATE_SHEET_NAMES,
				STATE_SHEET_NAMES_NAME,
					STATE_SHEET_NAMES_NAME_NAME,
					STATE_SHEET_NAMES_NAME_VALUE,
230
			STATE_SHEET_PRINTINFO,
231 232 233 234 235 236 237
                                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,
238 239 240 241 242 243
				STATE_PRINT_VCENTER,
				STATE_PRINT_HCENTER,
				STATE_PRINT_GRID,
				STATE_PRINT_MONO,
				STATE_PRINT_DRAFTS,
				STATE_PRINT_TITLES,
244 245
				STATE_PRINT_REPEAT_TOP,
				STATE_PRINT_REPEAT_LEFT,
246 247 248 249 250
				STATE_PRINT_ORDER,
				STATE_PRINT_ORIENT,
				STATE_PRINT_HEADER,
				STATE_PRINT_FOOTER,
				STATE_PRINT_PAPER,
251
				STATE_PRINT_EVEN_ONLY_STYLE,
252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
			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,
			STATE_SHEET_COLS,
				STATE_COL,
			STATE_SHEET_ROWS,
				STATE_ROW,
			STATE_SHEET_SELECTIONS,
				STATE_SELECTION,
			STATE_SHEET_CELLS,
				STATE_CELL,
					STATE_CELL_CONTENT,
272 273
			STATE_SHEET_MERGED_REGION,
				STATE_SHEET_MERGE,
274
			STATE_SHEET_SOLVER,
275
		STATE_SHEET_OBJECTS,
276
			STATE_OBJECT_POINTS,
277 278 279 280 281
			STATE_OBJECT_RECTANGLE,
			STATE_OBJECT_ELLIPSE,
			STATE_OBJECT_ARROW,
			STATE_OBJECT_LINE,

282 283 284
	STATE_WB_VIEW,

	STATE_UNKNOWN
285
} xmlSaxState;
286 287 288 289 290 291

/*
 * 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.
 */
292
static char const * const xmlSax_state_names[] =
293 294 295 296 297 298 299 300 301 302 303 304
{
"START",
"gmr:Workbook",
	"gmr:Attributes",
		"gmr:Attribute",
			"gmr:name",
			"gmr:type",
			"gmr:value",
	"gmr:Summary",
		"gmr:Item",
			"gmr:name",
			"gmr:val-string",
305
			"gmr:val-int",
306 307 308 309 310 311
	"gmr:SheetNameIndex",
		"gmr:SheetName",
        "gmr:Names",
                "gmr:Name",
                        "gmr:name",
                        "gmr:value",
312 313 314 315 316 317 318
	"gmr:Geometry",
	"gmr:Sheets",
		"gmr:Sheet",
			"gmr:Name",
			"gmr:MaxCol",
			"gmr:MaxRow",
			"gmr:Zoom",
319 320 321 322
			"gmr:Names",
				"gmr:Name",
					"gmr:name",
					"gmr:value",
323
			"gmr:PrintInformation",
324 325 326 327 328 329 330
				"gmr:Margins",
					"gmr:top",
					"gmr:bottom",
					"gmr:left",
					"gmr:right",
					"gmr:header",
					"gmr:footer",
331 332 333 334 335 336
				"gmr:vcenter",
				"gmr:hcenter",
				"gmr:grid",
				"gmr:monochrome",
				"gmr:draft",
				"gmr:titles",
337 338
				"gmr:repeat_top",
				"gmr:repeat_left",
339 340 341 342 343
				"gmr:order",
				"gmr:orientation",
				"gmr:Footer",
				"gmr:Header",
				"gmr:paper",
344
				"gmr:even_if_only_styles",
345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
			"gmr:Styles",
				"gmr:StyleRegion",
					"gmr:Style",
						"gmr:Font",
						"gmr:StyleBorder",
							"gmr:Top",
							"gmr:Bottom",
							"gmr:Left",
							"gmr:Right",
							"gmr:Diagonal",
							"gmr:Rev-Diagonal",
			"gmr:Cols",
				"gmr:ColInfo",

			"gmr:Rows",
				"gmr:RowInfo",
			"gmr:Selections",
				"gmr:Selection",
			"gmr:Cells",
				"gmr:Cell",
					"gmr:Content",
366 367
			"gmr:MergedRegions",
				"gmr:Merge",
368
			"gmr:Solver",
369
			"gmr:Objects",
370
				"gmr:Points",
371 372 373 374
				"gmr:Rectangle",
				"gmr:Ellipse",
				"gmr:Arrow",
				"gmr:Line",
375 376 377 378 379 380
	"gmr:UIData",

	"Unknown",
NULL
};

381 382 383 384 385 386 387 388 389
typedef enum
{
    GNUM_XML_UNKNOWN = 0,
    GNUM_XML_V1,
    GNUM_XML_V2,
    GNUM_XML_V3,	/* >= 0.52 */
    GNUM_XML_V4,	/* >= 0.57 */
    GNUM_XML_V5,	/* >= 0.58 */
    GNUM_XML_V6,	/* >= 0.62 */
390
    GNUM_XML_V7,        /* >= 0.66 */
391 392

    /* NOTE : Keep this up to date */
393
    GNUM_XML_LATEST = GNUM_XML_V7
394 395
} GnumericXMLVersion;
typedef struct _XMLSaxParseState
396
{
397
	xmlSaxState state;
398 399 400
	gint	  unknown_depth;	/* handle recursive unknown tags */
	GSList	 *state_stack;

401 402 403
	IOContext 	*context;	/* The IOcontext managing things */
	WorkbookView	*wb_view;	/* View for the new workbook */
	Workbook	*wb;		/* The new workbook */
404
	GnumericXMLVersion version;
405 406 407 408

	Sheet *sheet;
	double sheet_zoom;

409 410 411 412 413 414 415 416
	/* Only valid while parsing attributes */
	struct {
		char *name;
		char *value;
		int   type;
	} attribute;
	GList *attributes;

417 418 419 420 421 422
	/* Only valid when parsing wb or sheet names */
	struct {
		char *name;
		char *value;
	} name;
	
423 424 425 426
	gboolean  style_range_init;
	Range	  style_range;
	MStyle   *style;

427
	CellPos cell;
428
	int expr_id, array_rows, array_cols;
429
	int value_type;
430
	char const *value_fmt;
431 432

	GString *content;
433

434 435 436 437 438 439 440 441 442
	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;

443 444
	/* expressions with ref > 1 a map from index -> expr pointer */
	GHashTable *expr_map;
445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487
} XMLSaxParseState;

static void
xml_sax_unknown_attr (XMLSaxParseState *state, CHAR const * const *attrs, char const *name)
{
	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);
}

488 489 490

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

491
static void
492
xml_sax_wb (XMLSaxParseState *state, CHAR const **attrs)
493 494 495 496 497 498 499
{
	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 [] = {
500
				{ "http://www.gnome.org/gnumeric/v7", GNUM_XML_V7 },	/* 0.66 */
501 502 503 504 505 506 507 508 509 510
				{ "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 )
511
				if (strcmp (attrs[1], GnumericVersions [i].id) == 0) {
512 513 514
					if (state->version != GNUM_XML_UNKNOWN)
						xml_sax_warning (state, "Multiple version specifications.  Assuming %d",
								state->version);
515
					else {
516
						state->version = GnumericVersions [i].version;
517 518
						break;
					}
519 520 521 522 523
				}
		} else
			xml_sax_unknown_attr (state, attrs, "Workbook");
}

524 525 526 527 528 529 530 531
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);
}

532
static void
533
xml_sax_wb_view (XMLSaxParseState *state, CHAR const **attrs)
534 535 536 537
{
	int sheet_index;
	int width = -1, height = -1;

538
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
539
		if (xml_sax_attr_int (attrs, "SelectedTab", &sheet_index))
540
			wb_view_sheet_focus (state->wb_view,
541
				workbook_sheet_by_index (state->wb, sheet_index));
542 543
		else if (xml_sax_attr_int (attrs, "Width", &width)) ;
		else if (xml_sax_attr_int (attrs, "Height", &height)) ;
544
		else
545
			xml_sax_unknown_attr (state, attrs, "WorkbookView");
546 547

	if (width > 0 && height > 0)
548
		wb_view_preferred_size (state->wb_view, width, height);
549 550
}

551
static void
552
xml_sax_arg_set (GtkArg *arg, gchar *string)
553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
{
	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
592
xml_sax_finish_parse_wb_attr (XMLSaxParseState *state)
593 594 595 596 597 598 599 600 601
{
	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;
602
	xml_sax_arg_set (arg, state->attribute.value);
603 604 605 606 607 608 609 610 611
	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
612
xml_sax_free_arg_list (GList *start)
613 614 615 616 617 618 619 620 621 622 623 624 625 626
{
	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
627
xml_sax_attr_elem (XMLSaxParseState *state)
628
{
629 630 631
	char const * content = state->content->str;
	int const len = state->content->len;

632 633 634
	switch (state->state) {
	case STATE_WB_ATTRIBUTES_ELEM_NAME :
		g_return_if_fail (state->attribute.name == NULL);
635
		state->attribute.name = g_strndup (content, len);
636 637 638 639
		break;

	case STATE_WB_ATTRIBUTES_ELEM_VALUE :
		g_return_if_fail (state->attribute.value == NULL);
640
		state->attribute.value = g_strndup (content, len);
641 642 643 644 645
		break;

	case STATE_WB_ATTRIBUTES_ELEM_TYPE :
	{
		int type;
646
		if (xml_sax_int (content, &type))
647 648 649 650 651 652 653 654 655
			state->attribute.type = type;
		break;
	}

	default :
		g_assert_not_reached ();
	};
}

656
static void
657
xml_sax_sheet_start (XMLSaxParseState *state, CHAR const **attrs)
658 659 660
{
	int tmp;

661 662 663 664 665 666
	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 */

667
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683
		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;
684
		else
685
			xml_sax_unknown_attr (state, attrs, "Sheet");
686 687
}

688
static void
689 690 691 692 693 694 695 696 697 698 699 700 701
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)
702
{
703
	char const * content = state->content->str;
704 705
	g_return_if_fail (state->sheet == NULL);

706 707 708 709 710 711
	/*
	 * 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);
712

713 714 715 716 717 718 719
		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);
	}
	
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735
	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;
736 737 738
}

static void
739
xml_sax_sheet_zoom (XMLSaxParseState *state)
740
{
741
	char const * content = state->content->str;
742 743 744 745
	double zoom;

	g_return_if_fail (state->sheet != NULL);

746 747
	if (xml_sax_double (content, &zoom))
		state->sheet_zoom = zoom;
748 749
}

750
static void
751
xml_sax_print_margins (XMLSaxParseState *state, CHAR const **attrs)
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 780 781 782 783
{
	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;
	}

784
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
785
		if (xml_sax_attr_double (attrs, "Points", &points))
786 787
			pu->points = points;
		else if (!strcmp (attrs[0], "PrefUnit")) {
Jody Goldberg's avatar
Doh!  
Jody Goldberg committed
788
			if (!strcmp (attrs[1], "points"))
789
				pu->desired_display = UNIT_POINTS;
Jody Goldberg's avatar
Doh!  
Jody Goldberg committed
790
			else if (!strcmp (attrs[1], "mm"))
791
				pu->desired_display = UNIT_MILLIMETER;
Jody Goldberg's avatar
Doh!  
Jody Goldberg committed
792
			else if (!strcmp (attrs[1], "cm"))
793
				pu->desired_display = UNIT_CENTIMETER;
Jody Goldberg's avatar
Doh!  
Jody Goldberg committed
794
			else if (!strcmp (attrs[1], "in"))
795 796
				pu->desired_display = UNIT_INCH;
		} else
797
			xml_sax_unknown_attr (state, attrs, "Margin");
798 799 800
	}
}

801
static void
802
xml_sax_selection_range (XMLSaxParseState *state, CHAR const **attrs)
803 804
{
	Range r;
805
	if (xml_sax_range (attrs, &r))
806 807 808 809 810 811 812
		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
813
xml_sax_selection (XMLSaxParseState *state, CHAR const **attrs)
814 815 816
{
	int col = -1, row = -1;

817
	sheet_selection_reset (state->sheet);
818

819
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2)
820 821
		if (xml_sax_attr_int (attrs, "CursorCol", &col)) ;
		else if (xml_sax_attr_int (attrs, "CursorRow", &row)) ;
822
		else
823
			xml_sax_unknown_attr (state, attrs, "Selection");
824 825 826

	g_return_if_fail (col >= 0);
	g_return_if_fail (row >= 0);
827 828 829 830
	g_return_if_fail (state->cell.col < 0);
	g_return_if_fail (state->cell.row < 0);
	state->cell.col = col;
	state->cell.row = row;
831 832 833
}

static void
834
xml_sax_selection_end (XMLSaxParseState *state)
835
{
836
	CellPos const pos = state->cell;
837

838
	state->cell.col = state->cell.row = -1;
839

840 841
	g_return_if_fail (pos.col >= 0);
	g_return_if_fail (pos.row >= 0);
842

843
	sheet_set_edit_pos (state->sheet, pos.col, pos.row);
844 845
}

846
static void
847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863
xml_sax_cols_rows (XMLSaxParseState *state, CHAR const **attrs, gboolean is_col)
{
	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
xml_sax_colrow (XMLSaxParseState *state, CHAR const **attrs, gboolean is_col)
864 865 866 867
{
	ColRowInfo *cri = NULL;
	double size = -1.;
	int dummy;
868
	int count = 1;
869 870 871

	g_return_if_fail (state->sheet != NULL);

872
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
873
		if (xml_sax_attr_int (attrs, "No", &dummy)) {
874 875 876 877 878 879 880 881
			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);

882 883 884
			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))
885
				cri->margin_a = dummy;
886
			else if (xml_sax_attr_int (attrs, "MarginB", &dummy))
887
				cri->margin_b = dummy;
888
			else if (xml_sax_attr_int (attrs, "HardSize", &dummy))
889
				cri->hard_size = dummy;
890
			else if (xml_sax_attr_int (attrs, "Hidden", &dummy))
891
				cri->visible = !dummy;
892
			else if (xml_sax_attr_int (attrs, "Collapsed", &dummy))
893
				cri->is_collapsed = dummy;
894
			else if (xml_sax_attr_int (attrs, "OutlineLevel", &dummy))
895
				cri->outline_level = dummy;
896
			else
897
				xml_sax_unknown_attr (state, attrs, "ColRow");
898 899 900 901 902
		}
	}

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

903 904 905 906 907
	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)
908
			colrow_copy (sheet_col_fetch (state->sheet, ++pos), cri);
909 910
	} else {
		int pos = cri->pos;
911
		sheet_row_set_size_pts (state->sheet, cri->pos, size, cri->hard_size);
912 913
		/* resize flags are already set only need to copy the sizes */
		while (--count > 0)
914
			colrow_copy (sheet_row_fetch (state->sheet, ++pos), cri);
915
	}
916 917
}

918
static void
919
xml_sax_style_region_start (XMLSaxParseState *state, CHAR const **attrs)
920 921 922 923 924 925
{
	g_return_if_fail (state->style_range_init == FALSE);
	g_return_if_fail (state->style == NULL);

	state->style = mstyle_new ();
	state->style_range_init =
926
		xml_sax_range (attrs, &state->style_range);
927 928 929
}

static void
930 931 932 933 934 935 936 937 938 939 940 941 942 943
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
xml_sax_styleregion_start (XMLSaxParseState *state, CHAR const **attrs)
944 945 946 947 948 949
{
	int val;
	StyleColor *colour;

	g_return_if_fail (state->style != NULL);

950
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
951
		if (xml_sax_attr_int (attrs, "HAlign", &val))
952
			mstyle_set_align_h (state->style, val);
953
		else if (xml_sax_attr_int (attrs, "VAlign", &val))
954
			mstyle_set_align_v (state->style, val);
955 956

		/* Pre version V6 */
957
		else if (xml_sax_attr_int (attrs, "Fit", &val))
958 959
			mstyle_set_wrap_text (state->style, val);

960
		else if (xml_sax_attr_int (attrs, "WrapText", &val))
961
			mstyle_set_wrap_text (state->style, val);
962
		else if (xml_sax_attr_int (attrs, "Orient", &val))
963
			mstyle_set_orientation (state->style, val);
964
		else if (xml_sax_attr_int (attrs, "Shade", &val))
965
			mstyle_set_pattern (state->style, val);
966
		else if (xml_sax_attr_int (attrs, "Indent", &val))
Jody Goldberg's avatar
Jody Goldberg committed
967
			mstyle_set_indent (state->style, val);
968
		else if (xml_sax_color (attrs, "Fore", &colour))
969
			mstyle_set_color (state->style, MSTYLE_COLOR_FORE, colour);
970
		else if (xml_sax_color (attrs, "Back", &colour))
971
			mstyle_set_color (state->style, MSTYLE_COLOR_BACK, colour);
972
		else if (xml_sax_color (attrs, "PatternColor", &colour))
973 974
			mstyle_set_color (state->style, MSTYLE_COLOR_PATTERN, colour);
		else if (!strcmp (attrs[0], "Format"))
975
			mstyle_set_format_text (state->style, attrs[1]);
976
		else
977
			xml_sax_unknown_attr (state, attrs, "StyleRegion");
978 979 980 981
	}
}

static void
982
xml_sax_styleregion_font (XMLSaxParseState *state, CHAR const **attrs)
983 984 985 986 987 988
{
	double size_pts = 10.;
	int val;

	g_return_if_fail (state->style != NULL);

989
	for (; attrs != NULL && attrs[0] && attrs[1] ; attrs += 2) {
990
		if (xml_sax_attr_double (attrs, "Unit", &size_pts))
991
			mstyle_set_font_size (state->style, size_pts);
992
		else if (xml_sax_attr_int (attrs, "Bold", &val))
993
			mstyle_set_font_bold (state->style, val);
994
		else if (xml_sax_attr_int (attrs, "Italic", &val))
995
			mstyle_set_font_italic (state->style, val);
996
		else if (xml_sax_attr_int (attrs, "Underline", &val))
997
			mstyle_set_font_uline (state->style, (StyleUnderlineType)val);
998
		else if (xml_sax_attr_int (attrs, "StrikeThrough", &val))
999 1000
			mstyle_set_font_strike (state->style, val ? TRUE : FALSE);
		else
1001
			xml_sax_unknown_attr (state, attrs, "StyleFont");
1002
	}
1003
}
1004

1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053
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