sheet-object-graphic.c 28.5 KB
Newer Older
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2

3 4 5 6 7 8 9 10 11
/*
 * sheet-object-graphic.c: Implements the drawing object manipulation for Gnumeric
 *
 * Author:
 *   Miguel de Icaza (miguel@kernel.org)
 */
#include <config.h>
#include <gnome.h>
#include <gdk/gdkkeysyms.h>
12
#include <libgnomeprint/gnome-print.h>
13
#include <math.h>
14
#include "gnumeric.h"
15
#include "sheet-control-gui.h"
16
#include "gnumeric-canvas.h"
17
#include "str.h"
18 19
#include "gnumeric-util.h"
#include "sheet-object-graphic.h"
20 21
#include "sheet-object-impl.h"

22
#include <gal/util/e-util.h>
23 24
#include <gal/widgets/e-colors.h>
#include <gal/widgets/widget-color-combo.h>
25

26
/* These are persisted */
27
typedef enum {
28 29 30 31
	SHEET_OBJECT_LINE	= 1,
	SHEET_OBJECT_ARROW	= 2,
	SHEET_OBJECT_BOX	= 101,
	SHEET_OBJECT_OVAL	= 102,
32 33 34 35 36
} SheetObjectGraphicType;

#define SHEET_OBJECT_GRAPHIC_CLASS(k) (GTK_CHECK_CLASS_CAST ((k), SHEET_OBJECT_GRAPHIC_TYPE))

typedef struct {
37 38 39
	SheetObject  parent_object;
	GdkColor    *fill_color;
	double       width;
40
	double       a, b, c;
41 42 43 44 45 46
	SheetObjectGraphicType type;
} SheetObjectGraphic;
typedef struct {
	SheetObjectClass parent_class;
} SheetObjectGraphicClass;
static SheetObjectClass *sheet_object_graphic_parent_class;
47 48

static void
49 50
sheet_object_graphic_fill_color_set (SheetObjectGraphic *sog,
				     GdkColor *color)
51
{
52 53
	SheetObject *so = SHEET_OBJECT (sog);
	GList *l;
54

55
	sog->fill_color = color;
56 57

	for (l = so->realized_list; l; l = l->next)
58
		gnome_canvas_item_set (l->data, "fill_color_gdk", color, NULL);
59 60
}

61
static void
62
sheet_object_graphic_width_set (SheetObjectGraphic *sog, double width)
63
{
64 65
	SheetObject *so = SHEET_OBJECT (sog);
	GList *l;
66

67 68
	sog->width = width;
	for (l = so->realized_list; l; l = l->next)
69 70
		gnome_canvas_item_set (l->data, "width_units", width,
				       NULL);
71
}
72

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
static void
sheet_object_graphic_abc_set (SheetObjectGraphic *sog, double a, double b,
			      double c)
{
	SheetObject *so = SHEET_OBJECT (sog);
	GList *l;

	sog->a = a;
	sog->b = b;
	sog->c = c;
	for (l = so->realized_list; l; l = l->next)
		gnome_canvas_item_set (l->data, "arrow_shape_a", a,
						"arrow_shape_b", b,
						"arrow_shape_c", c, NULL);
}

89
SheetObject *
90
sheet_object_line_new (gboolean is_arrow)
91 92
{
	SheetObjectGraphic *sog;
93

94 95
	sog = gtk_type_new (SHEET_OBJECT_GRAPHIC_TYPE);
	sog->type = is_arrow ? SHEET_OBJECT_ARROW : SHEET_OBJECT_LINE;
96

97 98
	return SHEET_OBJECT (sog);
}
99

100 101 102
static void
sheet_object_graphic_destroy (GtkObject *object)
{
103 104 105
	SheetObjectGraphic *sog;
	
	sog = SHEET_OBJECT_GRAPHIC (object);
106

107
	GTK_OBJECT_CLASS (sheet_object_graphic_parent_class)->destroy (object);
108 109
}

110 111
static GtkObject *
sheet_object_graphic_new_view (SheetObject *so, SheetControlGUI *scg)
112
{
113
	/* FIXME : this is bogus */
114
	GnumericCanvas *gcanvas = scg_pane (scg, 0);
115 116
	SheetObjectGraphic *sog = SHEET_OBJECT_GRAPHIC (so);
	GnomeCanvasItem *item = NULL;
117

118 119
	g_return_val_if_fail (IS_SHEET_OBJECT (so), NULL);
	g_return_val_if_fail (IS_SHEET_CONTROL_GUI (scg), NULL);
120

Michael Meeks's avatar
Michael Meeks committed
121
	switch (sog->type) {
122 123
	case SHEET_OBJECT_LINE:
		item = gnome_canvas_item_new (
124
			gcanvas->object_group,
125
			gnome_canvas_line_get_type (),
126 127
			"fill_color_gdk", sog->fill_color,
			"width_units", sog->width,
128 129 130 131 132
			NULL);
		break;

	case SHEET_OBJECT_ARROW:
		item = gnome_canvas_item_new (
133
			gcanvas->object_group,
134
			gnome_canvas_line_get_type (),
135 136
			"fill_color_gdk", sog->fill_color,
			"width_units", sog->width,
137 138 139
			"arrow_shape_a", sog->a,
			"arrow_shape_b", sog->b,
			"arrow_shape_c", sog->c,
140 141 142 143 144 145 146 147
			"last_arrowhead", TRUE,
			NULL);
		break;

	default:
		g_assert_not_reached ();
	}

148 149
	scg_object_register (so, item);
	return GTK_OBJECT (item);
150 151
}

152
static void
153 154
sheet_object_graphic_update_bounds (SheetObject *so, GtkObject *view,
				    SheetControlGUI *scg)
155
{
156
	GnomeCanvasPoints *points = gnome_canvas_points_new (2);
157

158 159 160 161 162 163
	scg_object_view_position (scg, so, points->coords);
	gnome_canvas_item_set (GNOME_CANVAS_ITEM (view),
			       "points", points,
			       NULL);
	gnome_canvas_points_free (points);
}
164

165
static gboolean
166 167
sheet_object_graphic_read_xml (SheetObject *so,
			       XmlParseContext const *ctxt, xmlNodePtr tree)
168
{
169
	SheetObjectGraphic *sog;
170
	double width, a, b, c;
171 172 173 174 175 176 177 178
	int tmp = 0;
	GdkColor *color = NULL;

	g_return_val_if_fail (IS_SHEET_OBJECT_GRAPHIC (so), TRUE);
	sog = SHEET_OBJECT_GRAPHIC (so);

	color = xml_get_value_color (tree, "FillColor");
	sheet_object_graphic_fill_color_set (sog, color);
179

180 181
	if (xml_get_value_int (tree, "Type", &tmp))
		sog->type = tmp;
182 183 184

	xml_get_value_double (tree, "Width", &width);
	sheet_object_graphic_width_set (sog, width);
185

186 187 188 189 190
	if (xml_get_value_double (tree, "ArrowShapeA", &a) &&
	    xml_get_value_double (tree, "ArrowShapeB", &b) &&
	    xml_get_value_double (tree, "ArrowShapeC", &c))
		sheet_object_graphic_abc_set (sog, a, b, c);

191 192 193 194 195 196 197
	return FALSE;
}

static gboolean
sheet_object_graphic_write_xml (SheetObject const *so,
				XmlParseContext const *ctxt, xmlNodePtr tree)
{
198 199 200 201
	SheetObjectGraphic *sog;
	
	g_return_val_if_fail (IS_SHEET_OBJECT_GRAPHIC (so), TRUE);
	sog = SHEET_OBJECT_GRAPHIC (so);
202

203
	xml_set_value_color (tree, "FillColor", sog->fill_color);
204
	xml_set_value_int (tree, "Type", sog->type);
205
	xml_set_value_double (tree, "Width", sog->width, -1);
206

207 208 209 210 211 212
	if (sog->type == SHEET_OBJECT_ARROW) {
		xml_set_value_double (tree, "ArrowShapeA", sog->a, -1);
		xml_set_value_double (tree, "ArrowShapeB", sog->b, -1);
		xml_set_value_double (tree, "ArrowShapeC", sog->c, -1);
	}

213
	return FALSE;
214 215
}

216 217 218
static SheetObject *
sheet_object_graphic_clone (SheetObject const *so, Sheet *sheet)
{
219
	SheetObjectGraphic *sog;
220 221
	SheetObjectGraphic *new_sog;

222 223 224
	g_return_val_if_fail (IS_SHEET_OBJECT_GRAPHIC (so), NULL);
	sog = SHEET_OBJECT_GRAPHIC (so);

225 226 227 228
	new_sog = SHEET_OBJECT_GRAPHIC (gtk_type_new (GTK_OBJECT_TYPE (sog)));

	new_sog->type  = sog->type;
	new_sog->width = sog->width;
229
	new_sog->fill_color = sog->fill_color;
230 231 232
	new_sog->a = sog->a;
	new_sog->b = sog->b;
	new_sog->c = sog->c;
233 234 235 236

	return SHEET_OBJECT (new_sog);
}

237
static void
238 239
sheet_object_graphic_print (SheetObject const *so,
			    SheetObjectPrintInfo const *pi)
240
{
241 242
#ifdef ENABLE_BONOBO
	SheetObjectGraphic *sog;
243
	double coords [4];
244 245 246 247 248 249
	double x1 = 0.0, y1 = 0.0, x2 = 0.0, y2 = 0.0;
	GnomePrintMeta *meta;
	GnomePrintContext *ctx;

	g_return_if_fail (IS_SHEET_OBJECT_GRAPHIC (so));
	sog = SHEET_OBJECT_GRAPHIC (so);
250 251 252 253 254

	sheet_object_position_pts (so, coords);

	if (so->type == SHEET_OBJECT_ARROW) {
		static gboolean warned = FALSE;
255 256
		if (!warned)
			g_warning ("FIXME: I print arrows as lines");
257 258 259
		warned = TRUE;
	}

260 261 262
	meta = bonobo_print_data_get_meta (pi->pd);
	ctx = GNOME_PRINT_CONTEXT (meta);
	
263 264
	gnome_print_gsave (ctx);

265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	if (sog->fill_color) {
		switch (so->direction) {
		case SO_DIR_UP_RIGHT:
		case SO_DIR_DOWN_RIGHT:
			x1 = coords [0];
			x2 = coords [2];
			break;
		case SO_DIR_UP_LEFT:
		case SO_DIR_DOWN_LEFT:
			x1 = coords [2];
			x2 = coords [0];
			break;
		default:
			g_warning ("Cannot guess direction!");
			gnome_print_grestore (ctx);
			return;
		}

		switch (so->direction) {
		case SO_DIR_UP_LEFT:
		case SO_DIR_UP_RIGHT:
			y1 = coords [3];
			y2 = coords [1];
			break;
		case SO_DIR_DOWN_LEFT:
		case SO_DIR_DOWN_RIGHT:
			y1 = coords [1];
			y2 = coords [3];
			break;
		default:
			g_warning ("Cannot guess direction!");
			gnome_print_grestore (ctx);
			return;
		}

		gnome_print_setrgbcolor (ctx, (double) sog->fill_color->red,
					      (double) sog->fill_color->green,
					      (double) sog->fill_color->blue);
		gnome_print_setlinewidth (ctx, sog->width);
		gnome_print_newpath (ctx);
		gnome_print_moveto (ctx, x1, y1);
		gnome_print_lineto (ctx, x2, y2);
		gnome_print_stroke (ctx);
	}
	
310
	gnome_print_grestore (ctx);
311 312 313
#else
	g_warning (PACKAGE " has been compiled without bonobo support. "
		   "Objects will not be printed.");
314
#endif
315 316
}

317 318 319
typedef struct
{
	SheetObjectGraphic *sog;
320 321
	GtkWidget *canvas;
	GnomeCanvasItem *arrow;
322
	GtkWidget *fill_color_combo;
323 324
	GtkObject *adj_width;
	GtkObject *adj_a, *adj_b, *adj_c;
325 326
	GdkColor *fill_color;
	double width;
327
	double a, b, c;
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
} DialogGraphicData;

static gboolean
cb_dialog_graphic_close (GnomeDialog *dialog, DialogGraphicData *data)
{
	g_free (data);

	return (FALSE);
}

static void
cb_dialog_graphic_clicked (GnomeDialog *dialog, int button,
			   DialogGraphicData *data)
{
	GdkColor *color;

	switch (button) {
	case 0: /* Ok */
	case 1: /* Apply */
		color = color_combo_get_color (
				COLOR_COMBO (data->fill_color_combo));
		sheet_object_graphic_fill_color_set (data->sog, color);
		sheet_object_graphic_width_set (data->sog,
351 352 353 354 355
				GTK_ADJUSTMENT (data->adj_width)->value);
		sheet_object_graphic_abc_set (data->sog, 
				GTK_ADJUSTMENT (data->adj_a)->value,
				GTK_ADJUSTMENT (data->adj_b)->value,
				GTK_ADJUSTMENT (data->adj_c)->value);
356 357 358 359 360 361 362
		if (button == 0)
			gnome_dialog_close (dialog);
		break;
	case 2: /* Cancel */
		sheet_object_graphic_fill_color_set (data->sog,
						     data->fill_color);
		sheet_object_graphic_width_set (data->sog, data->width);
363 364
		sheet_object_graphic_abc_set (data->sog, data->a, data->b,
					      data->c);
365 366 367 368 369 370 371 372
		gnome_dialog_close (dialog);
		break;
	default:
		g_warning ("Unhandled button %i.", button);
		break;
	}
}

373 374 375 376 377 378 379 380 381 382 383
static void
cb_adjustment_value_changed (GtkAdjustment *adj, DialogGraphicData *data)
{
	gnome_canvas_item_set (data->arrow,
		"arrow_shape_a", (double) GTK_ADJUSTMENT (data->adj_a)->value,
		"arrow_shape_b", (double) GTK_ADJUSTMENT (data->adj_b)->value,
		"arrow_shape_c", (double) GTK_ADJUSTMENT (data->adj_c)->value,
		"width_units", (double) GTK_ADJUSTMENT (data->adj_width)->value,
		NULL);
}

384 385 386
static void
sheet_object_graphic_user_config (SheetObject *so, SheetControlGUI *scg)
{
387 388 389
	GtkWidget *dialog, *table, *label, *spin, *spin_a, *spin_b, *spin_c; 
	GtkTooltips *tooltips;
	WorkbookControlGUI *wbcg;
390 391
	SheetObjectGraphic *sog;
	DialogGraphicData *data;
392
	GnomeCanvasPoints *points;
393 394 395 396 397

	g_return_if_fail (IS_SHEET_OBJECT_GRAPHIC (so));
	g_return_if_fail (IS_SHEET_CONTROL_GUI (scg));

	sog = SHEET_OBJECT_GRAPHIC (so);
398
	wbcg = scg_get_wbcg (scg);
399 400 401 402 403 404

	dialog = gnome_dialog_new (_("Configure line or arrow"),
				   GNOME_STOCK_BUTTON_OK,
				   GNOME_STOCK_BUTTON_APPLY,
				   GNOME_STOCK_BUTTON_CANCEL, NULL);
	gnome_dialog_set_close (GNOME_DIALOG (dialog), FALSE);
405 406 407
	gnome_dialog_set_parent (GNOME_DIALOG (dialog),
				 wb_control_gui_toplevel (wbcg));
	tooltips = gtk_tooltips_new ();
408

409
	table = gtk_table_new (3, 5, FALSE);
410 411 412 413
	gtk_table_set_col_spacings (GTK_TABLE (table), 10);
	gtk_table_set_row_spacings (GTK_TABLE (table), 10);
	gtk_widget_show (table);
	gtk_container_add (GTK_CONTAINER (GNOME_DIALOG (dialog)->vbox), table);
414

415 416 417 418 419
	label = gtk_label_new (_("Color"));
	gtk_widget_show (label);
	gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 0, 1);
	label = gtk_label_new (_("Border width"));
	gtk_widget_show (label);
420 421 422 423 424 425 426 427 428 429 430
	gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 1, 2);
	label = gtk_label_new (_("Arrow shape a"));
	gtk_widget_show (label);
	gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 2, 3);
	label = gtk_label_new (_("Arrow shape b"));
	gtk_widget_show (label);
	gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 3, 4);
	label = gtk_label_new (_("Arrow shape c"));
	gtk_widget_show (label);
	gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 4, 5);

431 432 433
	data = g_new0 (DialogGraphicData, 1);
	data->sog = sog;

434 435 436 437
	data->canvas = gnome_canvas_new ();
	gtk_widget_show (data->canvas);
	gtk_table_attach_defaults (GTK_TABLE (table), data->canvas, 2, 3, 2, 5);

438 439 440 441 442 443
	data->fill_color_combo = color_combo_new (NULL, _("Transparent"), NULL,
					color_group_fetch ("color", so));
	color_combo_set_color (COLOR_COMBO (data->fill_color_combo), 
			       sog->fill_color);
	data->fill_color = sog->fill_color;
	
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465
	data->adj_width = gtk_adjustment_new ((float) sog->width, 1.0,
					      100.0, 1.0, 5.0, 1.0);
	spin = gtk_spin_button_new (GTK_ADJUSTMENT (data->adj_width), 1, 0);
	data->width = sog->width;

	data->adj_a = gtk_adjustment_new (sog->a, 1.0, 100.0, 1.0, 5.0, 1.0);
	data->adj_b = gtk_adjustment_new (sog->b, 1.0, 100.0, 1.0, 5.0, 1.0);
	data->adj_c = gtk_adjustment_new (sog->c, 1.0, 100.0, 1.0, 5.0, 1.0);
	spin_a = gtk_spin_button_new (GTK_ADJUSTMENT (data->adj_a), 1, 0);
	spin_b = gtk_spin_button_new (GTK_ADJUSTMENT (data->adj_b), 1, 0);
	spin_c = gtk_spin_button_new (GTK_ADJUSTMENT (data->adj_c), 1, 0);
	data->a = sog->a;
	data->b = sog->b;
	data->c = sog->c;
	gtk_tooltips_set_tip (tooltips, spin_a, _("Distance from tip of "
			      "arrowhead to the center point"), NULL);
	gtk_tooltips_set_tip (tooltips, spin_b, _("Distance from tip of "
			      "arrowhead to trailing point, measured along "
			      "shaft"), NULL);
	gtk_tooltips_set_tip (tooltips, spin_c, _("Distance of trailing point "
			      "from outside edge of shaft"), NULL);

466
	gtk_widget_show (data->fill_color_combo);
467 468 469 470 471
	gtk_widget_show (spin);
	gtk_widget_show (spin_a);
	gtk_widget_show (spin_b);
	gtk_widget_show (spin_c);

472
	gtk_table_attach_defaults (GTK_TABLE (table),
473 474 475 476 477
				   data->fill_color_combo, 1, 3, 0, 1);
	gtk_table_attach_defaults (GTK_TABLE (table), spin, 1, 3, 1, 2);
	gtk_table_attach_defaults (GTK_TABLE (table), spin_a, 1, 2, 2, 3);
	gtk_table_attach_defaults (GTK_TABLE (table), spin_b, 1, 2, 3, 4);
	gtk_table_attach_defaults (GTK_TABLE (table), spin_c, 1, 2, 4, 5);
478

479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
	gtk_widget_show (dialog);

	points = gnome_canvas_points_new (2);
	points->coords [0] = data->canvas->allocation.width / 2.0;
	points->coords [1] = 0.0;
	points->coords [2] = points->coords [0];
	points->coords [3] = data->canvas->allocation.height;
	data->arrow = gnome_canvas_item_new (
			gnome_canvas_root (GNOME_CANVAS (data->canvas)),
			GNOME_TYPE_CANVAS_LINE, "points", points, 
			"first_arrowhead", TRUE, NULL);
	gnome_canvas_points_free (points);
	gnome_canvas_set_scroll_region (GNOME_CANVAS (data->canvas), 0.0, 0.0, 
					data->canvas->allocation.width,
					data->canvas->allocation.height);
	cb_adjustment_value_changed (NULL, data);

	gtk_signal_connect (GTK_OBJECT (data->adj_width), "value_changed",
			    GTK_SIGNAL_FUNC (cb_adjustment_value_changed),
			    data);
	gtk_signal_connect (GTK_OBJECT (data->adj_a), "value_changed",
			    GTK_SIGNAL_FUNC (cb_adjustment_value_changed),
			    data);
	gtk_signal_connect (GTK_OBJECT (data->adj_b), "value_changed",
			    GTK_SIGNAL_FUNC (cb_adjustment_value_changed),
			    data);
	gtk_signal_connect (GTK_OBJECT (data->adj_c), "value_changed",
			    GTK_SIGNAL_FUNC (cb_adjustment_value_changed),
			    data);
508 509 510 511 512 513
	gtk_signal_connect (GTK_OBJECT (dialog), "clicked",
			    GTK_SIGNAL_FUNC (cb_dialog_graphic_clicked), data);
	gtk_signal_connect (GTK_OBJECT (dialog), "close",
			    GTK_SIGNAL_FUNC (cb_dialog_graphic_close), data);
}

514 515 516
static void
sheet_object_graphic_class_init (GtkObjectClass *object_class)
{
517
	SheetObjectClass *sheet_object_class;
518

519
	sheet_object_graphic_parent_class = gtk_type_class (SHEET_OBJECT_TYPE);
520

521
	/* Object class method overrides */
522
	object_class->destroy = sheet_object_graphic_destroy;
523 524

	/* SheetObject class method overrides */
525
	sheet_object_class = SHEET_OBJECT_CLASS (object_class);
526 527 528
	sheet_object_class->update_bounds = sheet_object_graphic_update_bounds;
	sheet_object_class->new_view	  = sheet_object_graphic_new_view;
	sheet_object_class->read_xml	  = sheet_object_graphic_read_xml;
529
	sheet_object_class->write_xml	  = sheet_object_graphic_write_xml;
530
	sheet_object_class->print         = sheet_object_graphic_print;
531
	sheet_object_class->clone         = sheet_object_graphic_clone;
532
	sheet_object_class->user_config   = sheet_object_graphic_user_config;
533
	sheet_object_class->rubber_band_directly = TRUE;
534 535
}

536 537 538
static void
sheet_object_graphic_init (GtkObject *obj)
{
539 540
	SheetObjectGraphic *sog;
	SheetObject *so;
541
	
542 543 544 545
	sog = SHEET_OBJECT_GRAPHIC (obj);
	sog->fill_color = g_new0 (GdkColor, 1);
	e_color_alloc_gdk (sog->fill_color);
	sog->width = 1.0;
546 547 548
	sog->a = 8.0;
	sog->b = 10.0;
	sog->c = 3.0;
549 550

	so = SHEET_OBJECT (obj);
551
	so->direction = SO_DIR_NONE_MASK;
552 553
}

554 555 556
E_MAKE_TYPE (sheet_object_graphic, "SheetObjectGraphic", SheetObjectGraphic,
	     sheet_object_graphic_class_init, sheet_object_graphic_init,
	     SHEET_OBJECT_TYPE);
557

558
/************************************************************************/
559 560

/*
561
 * SheetObjectFilled
562
 *
563
 * Derivative of SheetObjectGraphic, with filled parameter
564
 */
565
#define IS_SHEET_OBJECT_FILLED_CLASS(k) (GTK_CHECK_CLASS_CAST ((k), SHEET_OBJECT_FILLED_TYPE, SheetObjectFilledClass))
566 567 568 569

typedef struct {
	SheetObjectGraphic parent_object;

570
	GdkColor *outline_color;
571 572 573 574 575 576 577 578 579
} SheetObjectFilled;

typedef struct {
	SheetObjectGraphicClass parent_class;
} SheetObjectFilledClass;

static SheetObjectGraphicClass *sheet_object_filled_parent_class;

static void
580
sheet_object_filled_outline_color_set (SheetObjectFilled *sof, GdkColor *color)
581 582 583 584
{
	SheetObject *so = SHEET_OBJECT (sof);
	GList *l;

585
	sof->outline_color = color;
586
	for (l = so->realized_list; l; l = l->next)
587 588
		gnome_canvas_item_set (l->data, "outline_color_gdk", color,
				       NULL);
589 590
}

591
SheetObject *
592
sheet_object_box_new (gboolean is_oval)
593
{
594
	SheetObjectFilled *sof;
595 596
	SheetObjectGraphic *sog;

597
	sof = gtk_type_new (SHEET_OBJECT_FILLED_TYPE);
598

599 600
	sog = SHEET_OBJECT_GRAPHIC (sof);
	sog->type = is_oval ? SHEET_OBJECT_OVAL : SHEET_OBJECT_BOX;
601

602
	return SHEET_OBJECT (sof);
603 604 605 606 607
}

static void
sheet_object_filled_destroy (GtkObject *object)
{
608 609 610
	SheetObjectFilled *sof;
	
	sof = SHEET_OBJECT_FILLED (object);
611

612 613 614
	GTK_OBJECT_CLASS (sheet_object_filled_parent_class)->destroy (object);
}

615
static void
616 617
sheet_object_filled_update_bounds (SheetObject *so, GtkObject *view,
				   SheetControlGUI *scg)
618
{
619
	double coords [4];
620

621
	scg_object_view_position (scg, so, coords);
622
	
623
	gnome_canvas_item_set (GNOME_CANVAS_ITEM (view),
624 625 626 627
		"x1", MIN (coords [0], coords [2]),
		"x2", MAX (coords [0], coords [2]),
		"y1", MIN (coords [1], coords [3]),
		"y2", MAX (coords [1], coords [3]),
628
		NULL);
629 630
}

631 632
static GtkObject *
sheet_object_filled_new_view (SheetObject *so, SheetControlGUI *scg)
633
{
634
	/* FIXME : this is bogus */
635
	GnumericCanvas *gcanvas = scg_pane (scg, 0);
636 637 638
	SheetObjectGraphic *sog;
	SheetObjectFilled  *sof;
	GnomeCanvasItem *item;
639

640
	g_return_val_if_fail (IS_SHEET_OBJECT_FILLED (so), NULL);
641
	g_return_val_if_fail (IS_SHEET_CONTROL_GUI (scg), NULL);
642 643
	sof = SHEET_OBJECT_FILLED (so);
	sog = SHEET_OBJECT_GRAPHIC (so);
644

645
	item = gnome_canvas_item_new (
646 647 648 649 650 651 652
		gcanvas->object_group,
		(sog->type == SHEET_OBJECT_OVAL) ?
					gnome_canvas_ellipse_get_type () : 
					gnome_canvas_rect_get_type (),
		"fill_color_gdk", sog->fill_color,
		"outline_color_gdk", sof->outline_color,
		"width_units", sog->width, NULL);
653

654 655 656 657 658
	scg_object_register (so, item);
	return GTK_OBJECT (item);
}

static gboolean
659 660
sheet_object_filled_read_xml (SheetObject *so,
			      XmlParseContext const *ctxt, xmlNodePtr tree)
661
{
662 663
	SheetObjectFilled *sof;
	GdkColor *color = NULL;
664

665 666
	g_return_val_if_fail (IS_SHEET_OBJECT_FILLED (so), TRUE);
	sof = SHEET_OBJECT_FILLED (so);
667

668 669
	color = xml_get_value_color (tree, "OutlineColor");
	sheet_object_filled_outline_color_set (sof, color);
670

671 672 673 674 675 676 677
	return sheet_object_graphic_read_xml (so, ctxt, tree);
}

static gboolean
sheet_object_filled_write_xml (SheetObject const *so, 
			       XmlParseContext const *ctxt, xmlNodePtr tree)
{
678 679 680 681 682 683
	SheetObjectFilled *sof;
	
	g_return_val_if_fail (IS_SHEET_OBJECT_FILLED (so), TRUE);
	sof = SHEET_OBJECT_FILLED (so);

	xml_set_value_color (tree, "OutlineColor", sof->outline_color);
684 685

	return sheet_object_graphic_write_xml (so, ctxt, tree);
686 687
}

688 689 690
static SheetObject *
sheet_object_filled_clone (SheetObject const *so, Sheet *sheet)
{
691
	SheetObjectFilled *sof;
692 693 694
	SheetObjectFilled *new_sof;
	SheetObject *new_so;

695 696 697
	g_return_val_if_fail (IS_SHEET_OBJECT_FILLED (so), NULL);
	sof = SHEET_OBJECT_FILLED (so);

698 699 700
	new_so = sheet_object_graphic_clone (so, sheet);
	new_sof = SHEET_OBJECT_FILLED (new_so);

701
	new_sof->outline_color = sof->outline_color;
702 703 704 705

	return SHEET_OBJECT (new_sof);
}

706 707 708 709 710
typedef struct 
{
	SheetObjectFilled *sof;
	GtkWidget *fill_color_combo;
	GtkWidget *outline_color_combo;
711
	GtkObject *adj_width;
712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
	GdkColor *outline_color;
	GdkColor *fill_color;
	double width;
} DialogFilledData;

static gboolean
cb_dialog_filled_close (GnomeDialog *dialog, DialogFilledData *data)
{
	g_free (data);

	return (FALSE);
}

static void
cb_dialog_filled_clicked (GnomeDialog *dialog, int button,
			  DialogFilledData *data)
{
	SheetObjectGraphic *sog = SHEET_OBJECT_GRAPHIC (data->sof);
	GdkColor *color;

	switch (button) {
	case 0: /* Ok */
	case 1: /* Apply */
		color = color_combo_get_color (
				COLOR_COMBO (data->outline_color_combo));
		sheet_object_filled_outline_color_set (data->sof, color);
		color = color_combo_get_color (
				COLOR_COMBO (data->fill_color_combo));
		sheet_object_graphic_fill_color_set (sog, color);
		sheet_object_graphic_width_set (sog, 
742
				GTK_ADJUSTMENT (data->adj_width)->value);
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808
		if (button == 0)
			gnome_dialog_close (dialog);
		break;
	case 2: /* Cancel */
		sheet_object_graphic_fill_color_set (sog, data->fill_color);
		sheet_object_graphic_width_set (sog, data->width);
		sheet_object_filled_outline_color_set (data->sof, 
						       data->outline_color);
		gnome_dialog_close (dialog);
		break;
	default:
		g_warning ("Unhandled button %i.", button);
		break;
	}
}

static void
sheet_object_filled_user_config (SheetObject *so, SheetControlGUI *scg)
{
	GtkWidget *dialog, *table, *label, *spin; 
	SheetObjectFilled *sof;
	SheetObjectGraphic *sog;
	DialogFilledData *data;

	g_return_if_fail (IS_SHEET_OBJECT_FILLED (so));
	g_return_if_fail (IS_SHEET_CONTROL_GUI (scg));

	sof = SHEET_OBJECT_FILLED (so);
	sog = SHEET_OBJECT_GRAPHIC (so);

	dialog = gnome_dialog_new (_("Configure filled object"),
				   GNOME_STOCK_BUTTON_OK,
				   GNOME_STOCK_BUTTON_APPLY,
				   GNOME_STOCK_BUTTON_CANCEL, NULL);
	gnome_dialog_set_close (GNOME_DIALOG (dialog), FALSE);

	table = gtk_table_new (2, 3, FALSE);
	gtk_table_set_col_spacings (GTK_TABLE (table), 10);
	gtk_table_set_row_spacings (GTK_TABLE (table), 10);
	gtk_widget_show (table);
	gtk_container_add (GTK_CONTAINER (GNOME_DIALOG (dialog)->vbox), table);
	label = gtk_label_new (_("Outline color"));
	gtk_widget_show (label);
	gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 0, 1);
	label = gtk_label_new (_("Fill color"));
	gtk_widget_show (label);
	gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 1, 2);
	label = gtk_label_new (_("Border width"));
	gtk_widget_show (label);
	gtk_table_attach_defaults (GTK_TABLE (table), label, 0, 1, 2, 3);

	data = g_new0 (DialogFilledData, 1);
	data->sof = sof;

	data->outline_color_combo = color_combo_new (NULL, _("Transparent"),
				NULL, color_group_fetch ("outline_color", so));
	color_combo_set_color (COLOR_COMBO (data->outline_color_combo),
			       sof->outline_color);
	data->outline_color = sof->outline_color;

	data->fill_color_combo = color_combo_new (NULL, _("Transparent"),
				NULL, color_group_fetch ("fill_color", so));
	color_combo_set_color (COLOR_COMBO (data->fill_color_combo), 
			       sog->fill_color);
	data->fill_color = sog->fill_color;
	
809 810 811
	data->adj_width = gtk_adjustment_new ((float) sog->width, 1.0,
					      100.0, 1.0, 5.0, 1.0);
	spin = gtk_spin_button_new (GTK_ADJUSTMENT (data->adj_width), 1, 0);
812 813 814 815 816 817 818 819 820 821 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 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
	data->width = sog->width;

	gtk_widget_show (data->fill_color_combo);
	gtk_widget_show (data->outline_color_combo);
	gtk_widget_show (spin);

	gtk_table_attach_defaults (GTK_TABLE (table),
				   data->outline_color_combo, 1, 2, 0, 1);
	gtk_table_attach_defaults (GTK_TABLE (table),
				   data->fill_color_combo, 1, 2, 1, 2);
	gtk_table_attach_defaults (GTK_TABLE (table), spin, 1, 2, 2, 3);

	gtk_signal_connect (GTK_OBJECT (dialog), "clicked",
			    GTK_SIGNAL_FUNC (cb_dialog_filled_clicked), data);
	gtk_signal_connect (GTK_OBJECT (dialog), "close",
			    GTK_SIGNAL_FUNC (cb_dialog_filled_close), data);

	gtk_widget_show (dialog);
}

#ifdef ENABLE_BONOBO
static void
make_rect (GnomePrintContext *ctx, double x1, double x2, double y1, double y2)
{
	gnome_print_moveto (ctx, x1, y1);
	gnome_print_lineto (ctx, x2, y1);
	gnome_print_lineto (ctx, x2, y2);
	gnome_print_lineto (ctx, x1, y2);
	gnome_print_lineto (ctx, x1, y1);
}

/*
 * The following lines are copy and paste from dia. The ellipse logic has been 
 * slightly adapted. I have _no_ idea what's going on...
 */

/* This constant is equal to sqrt(2)/3*(8*cos(pi/8) - 4 - 1/sqrt(2)) - 1.
 * Its use should be quite obvious.
 */
#define ELLIPSE_CTRL1 0.26521648984
/* this constant is equal to 1/sqrt(2)*(1-ELLIPSE_CTRL1).
 * Its use should also be quite obvious.
 */
#define ELLIPSE_CTRL2 0.519570402739
#define ELLIPSE_CTRL3 M_SQRT1_2
/* this constant is equal to 1/sqrt(2)*(1+ELLIPSE_CTRL1).
 * Its use should also be quite obvious.
 */
#define ELLIPSE_CTRL4 0.894643159635

static void
make_ellipse (GnomePrintContext *ctx,
	      double x1, double x2, double y1, double y2)
{
	double width  = x2 - x1;
	double height = y2 - y1;
	double center_x = x1 + width / 2.0;
	double center_y = y1 + height / 2.0;
	double cw1 = ELLIPSE_CTRL1 * width / 2.0;
	double cw2 = ELLIPSE_CTRL2 * width / 2.0;
	double cw3 = ELLIPSE_CTRL3 * width / 2.0;
	double cw4 = ELLIPSE_CTRL4 * width / 2.0;
	double ch1 = ELLIPSE_CTRL1 * height / 2.0;
	double ch2 = ELLIPSE_CTRL2 * height / 2.0;
	double ch3 = ELLIPSE_CTRL3 * height / 2.0;
	double ch4 = ELLIPSE_CTRL4 * height / 2.0;

	gnome_print_moveto (ctx, x1, center_y);
	gnome_print_curveto (ctx,
			     x1,             center_y - ch1,
			     center_x - cw4, center_y - ch2,
			     center_x - cw3, center_y - ch3);
	gnome_print_curveto (ctx,
			     center_x - cw2, center_y - ch4,
			     center_x - cw1, y1,
			     center_x,       y1);
	gnome_print_curveto (ctx,
			     center_x + cw1, y1,
			     center_x + cw2, center_y - ch4,
			     center_x + cw3, center_y - ch3);
	gnome_print_curveto (ctx,
			     center_x + cw4, center_y - ch2,
			     x2,             center_y - ch1,
			     x2,             center_y);
	gnome_print_curveto (ctx,
			     x2,             center_y + ch1,
			     center_x + cw4, center_y + ch2,
			     center_x + cw3, center_y + ch3);
	gnome_print_curveto (ctx,
			     center_x + cw2, center_y + ch4,
			     center_x + cw1, y2,
			     center_x,       y2);
	gnome_print_curveto (ctx,
			     center_x - cw1, y2,
			     center_x - cw2, center_y + ch4,
			     center_x - cw3, center_y + ch3);
	gnome_print_curveto (ctx,
			     center_x - cw4, center_y + ch2,
			     x1,             center_y + ch1,
			     x1,             center_y); 
}
#endif

static void
sheet_object_filled_print (SheetObject const *so,
			   SheetObjectPrintInfo const *pi)
{
#ifdef ENABLE_BONOBO
	SheetObjectFilled *sof;
	SheetObjectGraphic *sog;
	double coords [4];
	double width, height;
	GnomePrintMeta *meta;
	GnomePrintContext *ctx;

	g_return_if_fail (IS_SHEET_OBJECT_FILLED (so));
	sof = SHEET_OBJECT_FILLED (so);
	sog = SHEET_OBJECT_GRAPHIC (so);

	sheet_object_position_pts (so, coords);
	width  = coords [2] - coords [0];
	height = coords [3] - coords [1];

	meta = bonobo_print_data_get_meta (pi->pd);
	ctx = GNOME_PRINT_CONTEXT (meta);

	gnome_print_gsave (ctx);

	if (sof->outline_color) {
		gnome_print_setlinewidth (ctx, sog->width);
		gnome_print_setrgbcolor (ctx,
					 (double) sof->outline_color->red,
					 (double) sof->outline_color->green,
					 (double) sof->outline_color->blue); 
		gnome_print_newpath (ctx);
		if (sog->type == SHEET_OBJECT_OVAL)
			make_ellipse (ctx, 0.0, width, 0.0, height);
		else
			make_rect (ctx, 0.0, width, 0.0, height);
		gnome_print_closepath (ctx);
		gnome_print_stroke (ctx);
	}

	if (sog->fill_color) {
		gnome_print_setrgbcolor (ctx,
					 (double) sog->fill_color->red,
					 (double) sog->fill_color->green,
					 (double) sog->fill_color->blue);
		gnome_print_newpath (ctx);
		if (sog->type == SHEET_OBJECT_OVAL)
			make_ellipse (ctx, 0.0, width, 0.0, height);
		else
			make_rect (ctx, 0.0, width, 0.0, height);
		gnome_print_fill (ctx);
	}

	gnome_print_grestore (ctx);
#else
	g_warning (PACKAGE " has been compiled without bonobo support. "
		   "Objects will not be printed.");
#endif
}

975 976 977
static void
sheet_object_filled_class_init (GtkObjectClass *object_class)
{
978 979 980
	SheetObjectClass *sheet_object_class;

	sheet_object_filled_parent_class = gtk_type_class (SHEET_OBJECT_GRAPHIC_TYPE);
981

982
	object_class->destroy		  = sheet_object_filled_destroy;
983 984

	sheet_object_class = SHEET_OBJECT_CLASS (object_class);
985
	sheet_object_class->new_view	  = sheet_object_filled_new_view;
986
	sheet_object_class->update_bounds = sheet_object_filled_update_bounds;
987
	sheet_object_class->read_xml	  = sheet_object_filled_read_xml;
988
	sheet_object_class->write_xml	  = sheet_object_filled_write_xml;
989
	sheet_object_class->clone         = sheet_object_filled_clone;
990 991
	sheet_object_class->user_config   = sheet_object_filled_user_config;
	sheet_object_class->print         = sheet_object_filled_print;
992
	sheet_object_class->rubber_band_directly = TRUE;
993 994
}

995 996 997
static void
sheet_object_filled_init (GtkObject *obj)
{
998 999 1000 1001 1002
	SheetObjectFilled *sof;
	
	sof = SHEET_OBJECT_FILLED (obj);
	sof->outline_color = g_new0 (GdkColor, 1);
	e_color_alloc_gdk (sof->outline_color);
1003
}
1004

1005 1006 1007
E_MAKE_TYPE (sheet_object_filled, "SheetObjectFilled", SheetObjectFilled,
	     sheet_object_filled_class_init, sheet_object_filled_init,
	     SHEET_OBJECT_GRAPHIC_TYPE);