dialog-cell-format.c 20.9 KB
Newer Older
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
 * dialog-cell-format.c:  Implements the Cell Format dialog box.
 * (C) 1998 The Free Software Foundation
 *
 * Author:
 *  Miguel de Icaza (miguel@gnu.org)
 *
 */
#include <config.h>
#include <gnome.h>
#include "gnumeric.h"
#include "gnumeric-util.h"
13
#include "gnumeric-sheet.h"
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
14 15 16 17 18 19 20 21 22 23 24 25
#include "dialogs.h"
#include "format.h"

/* The main dialog box */
static GtkWidget *cell_format_prop_win = 0;

/* These point to the various widgets in the format/number page */
static GtkWidget *number_sample;
static GtkWidget *number_input;
static GtkWidget *number_cat_list;
static GtkWidget *number_format_list;

26 27
static GtkWidget *font_widget;

28 29 30
/* There point to the radio groups in the format/alignment page */
static GSList *hradio_list;
static GSList *vradio_list;
Arturo Espinosa's avatar
Arturo Espinosa committed
31
static GtkWidget *auto_return;
32

33 34 35 36 37
/* These points to the radio buttons of the coloring page */
static GSList *foreground_radio_list;
static GSList *background_radio_list;
static GdkPixmap *patterns [GNUMERIC_SHEET_PATTERNS];

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
38 39 40 41 42
/* Points to the first cell in the selection */
static Cell *first_cell;

/* The various formats */
static char *cell_format_numbers [] = {
43
	N_("General"),
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
	"0",
	"0.00",
	"#,##0",
	"#,##0.00",
	"#,##0_);(#,##0)",
	"#,##0_);[red](#,##0)",
	"#,##0.00_);(#,##0.00)",
	"#,##0.00_);[red](#,##0.00)",
	"0.0",
	NULL
};

static char *cell_format_accounting [] = {
	"_($*#,##0_);_($*(#,##0);_($*\"-\"_);_(@_)",
	"_(*$,$$0_);_(*(#,##0);_(*\"-\"_);_(@_)",
	"_($*#,##0.00_);_($*(#,##0.00);_($*\"-\"??_);_(@_)",
	"_(*#,##0.00_);_(*(#,##0.00);_(*\"-\"??_);_(@_)",
	NULL
};

static char *cell_format_date [] = {
	"m/d/yy",
	"d-mmm-yy",
	"d-mmm",
	"mmm-yy",
	"m/d/yy h:mm",
	NULL
};

static char *cell_format_hour [] = {
	"h:mm AM/PM",
	"h:mm:ss AM/PM",
	"h:mm",
	"h:mm:ss",
	"m/d/yy h:mm",
	"mm:ss",
	"mm:ss.0",
	"[h]:mm:ss",
	NULL
};

static char *cell_format_percent [] = {
	"0%",
	"0.00%",
	NULL,
};

static char *cell_format_fraction [] = {
	"# ?/?",
	"# ??/??",
	NULL
};

static char *cell_format_scientific [] = {
	"0.00E+00",
	"##0.0E+0",
	NULL
};

static char *cell_format_text [] = {
	"@",
	NULL,
};

static char *cell_format_money [] = {
	"$#,##0_);($#,##0)",
	"$#,##0_);[red]($#,##0)",
	"$#,##0.00_);($#,##0.00)",
	"$#,##0.00_);[red]($#,##0.00)",
	NULL,

};

static struct {
	char *name;
	char **formats;	
} cell_formats [] = {
	{ N_("Numbers"),    cell_format_numbers    },
	{ N_("Accounting"), cell_format_accounting },
	{ N_("Date"),       cell_format_date       },
	{ N_("Time"),       cell_format_hour       },
	{ N_("Percent"),    cell_format_percent    },
	{ N_("Fraction"),   cell_format_fraction   },
	{ N_("Scientific"), cell_format_scientific },
	{ N_("Text"),       cell_format_text       },
	{ N_("Money"),      cell_format_money      },
	{ NULL, NULL }
};

static void
format_list_fill (int n)
{
	GtkCList *cl = GTK_CLIST (number_format_list);
	char **texts;
	int i;

	g_return_if_fail (n >= 0);
	g_return_if_fail (cell_formats [n].name != NULL);

	texts = cell_formats [n].formats;
	
	gtk_clist_freeze (cl);
	gtk_clist_clear (cl);

	for (i = 0; texts [i]; i++){
		char *t [1] = { texts [i] };
		
		gtk_clist_append (cl, t);
	}
	gtk_clist_thaw (cl);
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
/*
 * This routine is just used at startup to find the current
 * format that applies to this cell
 */
static int
format_find (char *format)
{
	int i, row;
	char **p;
	
	for (i = 0; cell_formats [i].name; i++){
		p = cell_formats [i].formats;

		for (row = 0; *p; p++, row++){
			if (strcmp (format, *p) == 0){
				format_list_fill (i);
				gtk_clist_select_row (GTK_CLIST (number_format_list), row, 0);
				return 1;
			}
		}
	}
	return 0;
}


Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
/*
 * Invoked when the user has selected a new format category
 */
static void
format_number_select_row (GtkCList *clist, gint row, gint col, GdkEvent *event, GtkWidget *prop_win)
{
	format_list_fill (row);
	gtk_clist_select_row (GTK_CLIST (number_format_list), 0, 0);

	gnome_property_box_changed (GNOME_PROPERTY_BOX (prop_win));
}

static void
render_formated_version (char *format)
{
	if (!first_cell)
		gtk_label_set (GTK_LABEL (number_sample), "");
	else {
		StyleFormat *style_format;
		Value *v = first_cell->value;
		char *str;

		style_format = style_format_new (format);
204
		str = format_value (style_format, v, NULL);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 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

		gtk_label_set (GTK_LABEL (number_sample), str);
		g_free (str);
		style_format_unref (style_format);
	}
}

/*
 * Invoked when a specific format has been selected
 */
static void
format_selected (GtkCList *clist, gint row, gint col, GdkEvent *event, GnomePropertyBox *prop_win)
{
	char *format;

	gtk_clist_get_text (clist, row, col, &format);

	/* Set the input line to reflect the selected format */
	gtk_entry_set_text (GTK_ENTRY (number_input), format);

	/* Notify gnome-property-box that a change has happened */
	if (cell_format_prop_win)
		gnome_property_box_changed (prop_win);
		
}

/*
 * cells_get_format:
 *
 * Checks if all the cells in the list share the same format string
 */
static StyleFormat *
cells_get_format (CellList *cells)
{
	StyleFormat *last_format;
	
	for (last_format = NULL; cells; cells = cells->next){
		Cell *cell = cells->data;

		if (!last_format){
			last_format = cell->style->format;
			continue;
		}

		if (cell->style->format != last_format)
			return NULL;
	}

	return last_format;
}

/*
 * Creates the lists for the number display with our defaults
 */
static GtkWidget *
my_clist_new (void)
{
	GtkCList *cl;
	GdkFont *font;
	
	cl = GTK_CLIST (gtk_clist_new (1));
	gtk_clist_column_titles_hide (cl);
	gtk_clist_set_policy (cl, GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_clist_set_selection_mode (cl,GTK_SELECTION_SINGLE);
				      
	/* Configure the size */
	font = GTK_WIDGET (cl)->style->font;
	gtk_widget_set_usize (GTK_WIDGET (cl), 0, 11 * (font->ascent + font->descent));

	return GTK_WIDGET (cl);
}

static void
format_code_changed (GtkEntry *entry, GnomePropertyBox *prop_win)
{
	render_formated_version (gtk_entry_get_text (entry));
	if (cell_format_prop_win)
		gnome_property_box_changed (prop_win);
}

/*
 * Creates the widget that represents the number format configuration page
 */
static GtkWidget *
create_number_format_page (GtkWidget *prop_win, CellList *cells)
{
	StyleFormat *format;
	GtkWidget *l;
	GtkTable *t, *tt;
	int i;
	
	enum {
		BOXES_LINE  = 1,
		INPUT_LINE  = 3,
	};

301
	t = (GtkTable *) gtk_table_new (0, 0, 0);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
302 303 304 305 306

	/* try to select the current format the user is using */
	format = cells_get_format (cells);

	/* 1. Categories */
307 308 309
	gtk_table_attach (t, l = gtk_label_new (_("Categories")),
			  0, 1, BOXES_LINE, BOXES_LINE+1,
			  GTK_FILL | GTK_EXPAND, 0, 0, 0);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
310 311 312 313
	gtk_misc_set_alignment (GTK_MISC (l), 0.0, 0.5);

	number_cat_list = my_clist_new ();
	gtk_table_attach (t, number_cat_list, 0, 1, BOXES_LINE+1, BOXES_LINE+2,
314
			  GTK_FILL | GTK_EXPAND, GTK_FILL | GTK_EXPAND, 4, 0);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337

	/* 1.1 Connect our signal handler */
	gtk_signal_connect (GTK_OBJECT (number_cat_list), "select_row",
			    GTK_SIGNAL_FUNC (format_number_select_row), prop_win);

	/* 1.2 Fill the category list */
	gtk_clist_freeze (GTK_CLIST (number_cat_list));
	for (i = 0; cell_formats [i].name; i++){
		char *text [1] = { cell_formats [i].name };
		
		gtk_clist_append (GTK_CLIST (number_cat_list), text);
	}
	gtk_clist_thaw (GTK_CLIST (number_cat_list));
	
	/* 2. Code input and sample display */
	tt = GTK_TABLE (gtk_table_new (0, 0, 0));
	
	/* 2.1 Input line */
	gtk_table_attach (tt, gtk_label_new (_("Code:")), 0, 1, 0, 1, 0, 0, 2, 0);
	
	number_input = gtk_entry_new ();
	gtk_signal_connect (GTK_OBJECT (number_input), "changed",
			    GTK_SIGNAL_FUNC (format_code_changed), prop_win);
338

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
339 340 341 342 343 344 345 346 347 348 349 350 351
	gtk_table_attach (tt, number_input, 1, 2, 0, 1, GTK_FILL | GTK_EXPAND, 0, 0, 2);
	
	/* 2.2 Sample */
	gtk_table_attach (tt, gtk_label_new (_("Sample:")), 0, 1, 1, 2, 0, 0, 2, 0);
	number_sample = gtk_label_new ("X");
	gtk_misc_set_alignment (GTK_MISC (number_sample), 0.0, 0.5);
	gtk_table_attach (tt, number_sample, 1, 2, 1, 2, GTK_FILL | GTK_EXPAND, 0, 0, 2);

	gtk_table_attach (t, GTK_WIDGET (tt), 0, 2, INPUT_LINE, INPUT_LINE+1,
			  GTK_FILL | GTK_EXPAND, 0, 0, 0);

	/* 3. Format codes */
	number_format_list = my_clist_new ();
352 353 354
	gtk_table_attach (t, l = gtk_label_new (_("Format codes")),
			  1, 2, BOXES_LINE, BOXES_LINE+1,
			  GTK_FILL | GTK_EXPAND, 0, 0, 0);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
355 356 357 358 359 360 361 362 363 364
	gtk_misc_set_alignment (GTK_MISC (l), 0.0, 0.5);
	gtk_table_attach_defaults (t, number_format_list, 1, 2, BOXES_LINE + 1, BOXES_LINE + 2);
	format_list_fill (0);

	/* 3.1 connect the signal handled for row selected */
	gtk_signal_connect (GTK_OBJECT (number_format_list), "select_row",
			    GTK_SIGNAL_FUNC (format_selected), prop_win);


	/* 3.2: Invoke the current style for the cell if possible */
365 366 367 368 369
	if (format){
		if (!format_find (format->format))
		    gtk_entry_set_text (GTK_ENTRY (number_input), format->format);
	} else 
		gtk_clist_select_row (GTK_CLIST (number_format_list), 0, 0);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
370 371 372 373 374 375 376 377 378


	/* 4. finish */
	gtk_widget_show_all (GTK_WIDGET (t));
	
	return GTK_WIDGET (t);
}

static void
Miguel de Icaza's avatar
Miguel de Icaza committed
379
apply_number_formats (Style *style, Sheet *sheet, CellList *list)
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
380 381 382 383 384 385 386 387 388
{
	char *str = gtk_entry_get_text (GTK_ENTRY (number_input));
	
	for (;list; list = list->next){
		Cell *cell = list->data;

		cell_set_format (cell, str);
	}

Miguel de Icaza's avatar
Miguel de Icaza committed
389 390
	style->valid_flags |= STYLE_FORMAT;
	style->format = style_format_new (str);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
391 392
}

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
typedef struct {
	char *name;
	int  flag;
} align_def_t;

static align_def_t horizontal_aligns [] = {
	{ N_("General"),   HALIGN_GENERAL },
	{ N_("Left"),      HALIGN_LEFT    },
	{ N_("Center"),    HALIGN_CENTER  },
	{ N_("Right"),     HALIGN_RIGHT   },
	{ N_("Fill"),      HALIGN_FILL    },
	{ N_("Justify"),   HALIGN_JUSTIFY },
	{ NULL, 0 }
};

static align_def_t vertical_aligns [] = {
	{ N_("Top"),       VALIGN_TOP     },
	{ N_("Center"),    VALIGN_CENTER  },
	{ N_("Bottom"),    VALIGN_BOTTOM  },
	{ N_("Justify"),   VALIGN_JUSTIFY },
	{ NULL, 0 }
};

Arturo Espinosa's avatar
Arturo Espinosa committed
416 417 418 419 420 421 422 423 424 425 426 427 428
static void
do_disable (GtkWidget *widget, GtkWidget *op)
{
	int v;
	
	if (GTK_TOGGLE_BUTTON (widget)->active)
		v = FALSE;
	else
		v = TRUE;
	
	gtk_widget_set_sensitive (op, v);
}

429
static GtkWidget *
Arturo Espinosa's avatar
Arturo Espinosa committed
430
make_radio_selection (GtkWidget *prop_win, char *title, align_def_t *array, GSList **dest_list, GtkWidget *disable)
431 432 433 434 435 436 437 438 439 440 441 442 443 444
{
	GtkWidget *frame, *vbox;
	GSList *group;

	frame = gtk_frame_new (title);
	vbox = gtk_vbox_new (0, 0);
	gtk_container_add (GTK_CONTAINER (frame), vbox);

	for (group = NULL;array->name; array++){
		GtkWidget *item;

		item = gtk_radio_button_new_with_label (group, _(array->name));
		group = gtk_radio_button_group (GTK_RADIO_BUTTON (item));
		gtk_box_pack_start_defaults (GTK_BOX (vbox), item);
Arturo Espinosa's avatar
Arturo Espinosa committed
445 446 447 448 449

		if (disable && strcmp (array->name, "Fill") == 0){
			gtk_signal_connect (GTK_OBJECT (item), "toggled",
					    GTK_SIGNAL_FUNC (do_disable), disable);
		}
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466
	}
	
	*dest_list = group;
	return frame;
}

static void
prop_modified (GtkWidget *widge, GnomePropertyBox *box)
{
	gnome_property_box_changed (box);
}

static GtkWidget *
create_align_page (GtkWidget *prop_win, CellList *cells)
{
	GtkTable *t;
	GtkWidget *w;
Arturo Espinosa's avatar
Arturo Espinosa committed
467
	int ha, va, autor, ok = 0;
468 469 470 471 472 473
	GList *l;
	GSList *sl;
	
	t = (GtkTable *) gtk_table_new (0, 0, 0);

	/* Vertical alignment */
Arturo Espinosa's avatar
Arturo Espinosa committed
474
	w = make_radio_selection (prop_win, _("Vertical"), vertical_aligns, &vradio_list, NULL);
475 476
	gtk_table_attach (t, w, 1, 2, 0, 1, 0, GTK_FILL, 4, 0);

Arturo Espinosa's avatar
Arturo Espinosa committed
477 478 479 480
	/* Horizontal alignment */
	w = make_radio_selection (prop_win, _("Horizontal"), horizontal_aligns, &hradio_list, w);
	gtk_table_attach (t, w, 0, 1, 0, 2, 0, GTK_FILL, 4, 0);

481
	auto_return = gtk_check_button_new_with_label (_("Auto return"));
Arturo Espinosa's avatar
Arturo Espinosa committed
482 483
	gtk_table_attach (t, auto_return, 0, 3, 2, 3, 0, 0, 0, 0);
	
484
	/* Check if all cells have the same properties */
485 486 487 488 489
	/*
	 * FIXME: This should check the cells *AND* the
	 * style regions to figure out what to check and what
	 * not, right now this is broken in that regard
	 */
490
	if (cells){
Arturo Espinosa's avatar
Arturo Espinosa committed
491 492 493 494
		ha    = ((Cell *) (cells->data))->style->halign;
		va    = ((Cell *) (cells->data))->style->valign;
		autor = ((Cell *) (cells->data))->style->fit_in_cell;
		
Arturo Espinosa's avatar
Arturo Espinosa committed
495
		for (ok = 1, l = cells; l; l = l->next){
496 497
			Cell *cell = l->data;
			
Arturo Espinosa's avatar
Arturo Espinosa committed
498 499 500
			if (cell->style->halign != ha ||
			    cell->style->valign != va ||
			    cell->style->fit_in_cell != autor){
501 502 503 504 505 506 507 508
				ok = 0;
				break;
			}
		}

		/* If all the cells share the same alignment, select that on the radio boxes */
		if (ok){
			int n;
Arturo Espinosa's avatar
Arturo Espinosa committed
509

510 511 512 513 514 515 516 517 518 519 520
			for (n = 0; horizontal_aligns [n].name; n++)
				if (horizontal_aligns [n].flag == ha){
					gtk_radio_button_select (hradio_list, n);
					break;
				}
			
			for (n = 0; vertical_aligns [n].name; n++)
				if (vertical_aligns [n].flag == va){
					gtk_radio_button_select (vradio_list, n);
					break;
				}
Arturo Espinosa's avatar
Arturo Espinosa committed
521 522

			if (autor)
523
				gtk_toggle_button_set_state (GTK_TOGGLE_BUTTON (auto_return), 1);
524 525 526
		}
	}

Miguel de Icaza's avatar
Miguel de Icaza committed
527
	/* Now after we *potentially* toggled the radio button above, we
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
	 * connect the signals to activate the propertybox
	 */
	for (sl = hradio_list; sl; sl = sl->next){
		gtk_signal_connect (GTK_OBJECT (sl->data), "toggled",
				    GTK_SIGNAL_FUNC (prop_modified), prop_win);
	}

	for (sl = vradio_list; sl; sl = sl->next){
		gtk_signal_connect (GTK_OBJECT (sl->data), "toggled",
				    GTK_SIGNAL_FUNC (prop_modified), prop_win);
	}
	
	gtk_widget_show_all (GTK_WIDGET (t));

	return GTK_WIDGET (t);
}

static void
Miguel de Icaza's avatar
Miguel de Icaza committed
546
apply_align_format (Style *style, Sheet *sheet, CellList *cells)
547
{
Miguel de Icaza's avatar
Miguel de Icaza committed
548
	int i;
Arturo Espinosa's avatar
Arturo Espinosa committed
549
	int halign, valign, autor;
Miguel de Icaza's avatar
Miguel de Icaza committed
550 551 552 553 554

	i = gtk_radio_group_get_selected (hradio_list);
	halign = horizontal_aligns [i].flag;
	i = gtk_radio_group_get_selected (vradio_list);
	valign = vertical_aligns [i].flag;
Arturo Espinosa's avatar
Arturo Espinosa committed
555 556
	autor = GTK_TOGGLE_BUTTON (auto_return)->active;

Miguel de Icaza's avatar
Miguel de Icaza committed
557 558 559
	for (; cells; cells = cells->next){
		Cell *cell = cells->data;
		
Arturo Espinosa's avatar
Arturo Espinosa committed
560
		cell_set_alignment (cell, halign, valign, ORIENT_HORIZ, autor);
Miguel de Icaza's avatar
Miguel de Icaza committed
561
	}
562 563 564
	style->halign = halign;
	style->valign = valign;
	style->orientation = ORIENT_HORIZ;
565
	style->fit_in_cell = autor;
Miguel de Icaza's avatar
Miguel de Icaza committed
566
	style->valid_flags |= STYLE_ALIGN;
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 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627
static void
font_changed (GtkWidget *widget, GtkStyle *previous_style, GnomePropertyBox *prop_win)
{
	gnome_property_box_changed (prop_win);
}

static GtkWidget *
create_font_page (GtkWidget *prop_win, CellList *cells)
{
	font_widget = gtk_font_selection_new ();
	gtk_widget_show (font_widget);

	gtk_signal_connect (GTK_OBJECT (GTK_FONT_SELECTION (font_widget)->preview_entry),
			    "style_set",
			    GTK_SIGNAL_FUNC (font_changed), prop_win);
	
	return font_widget;
}

static void
apply_font_format (Style *style, Sheet *sheet, CellList *cells)
{
	GtkFontSelection *font_sel = GTK_FONT_SELECTION (font_widget);
	GdkFont   *gdk_font;
	GList *l;
	char *font_name;
	int  height;
	
	font_name = gtk_font_selection_get_font_name (font_sel);

	if (!font_name)
		return;

	gdk_font = gtk_font_selection_get_font (font_sel);
	height = gdk_font->ascent + gdk_font->descent;

	/* Apply the new font to all of the cell rows */
	for (; cells; cells = cells->next){
		Cell *cell = cells->data;
		
		cell_set_font (cell, font_name);
	}

	/* Now apply it to every row in the selection */
	for (l = sheet->selections; l; l = l->next){
		SheetSelection *ss = l->data;
		int i;

		for (i = ss->start_row; i <= ss->end_row; i++){
			ColRowInfo *ri;

			ri = sheet_row_get (sheet, i);
			sheet_row_set_internal_height (sheet, ri, height);
		}
	}
	style->valid_flags |= STYLE_FONT;
	style->font = style_font_new (font_name, 10); 
}

628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 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 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
static GtkWidget *
create_foreground_radio (GtkWidget *prop_win)
{
	GtkWidget *frame, *table, *r1, *r2;
	int e = GTK_FILL | GTK_EXPAND;
	
	frame = gtk_frame_new (_("Text color"));
        table = gtk_table_new (2, 2, 0);
	gtk_container_add (GTK_CONTAINER (frame), table);

	r1 = gtk_radio_button_new_with_label (NULL, _("None"));
	foreground_radio_list = GTK_RADIO_BUTTON (r1)->group;
	r2 = gtk_radio_button_new_with_label (foreground_radio_list,
					      _("Use this color"));

	gtk_table_attach (GTK_TABLE (table), r1, 0, 1, 0, 1, e, 0, 0, 0);
	gtk_table_attach (GTK_TABLE (table), r2, 0, 1, 1, 2, e, 0, 0, 0);
/*	gtk_table_attach (GTK_TABLE (table), cs, 1, 2, 1, 2, 0, 0, 0, 0); */

	return frame;
}

static void
create_stipples (GnomeCanvas *canvas)
{
	GnomeCanvasGroup *group;
	GdkWindow *window;
	int i;

	group = GNOME_CANVAS_GROUP (gnome_canvas_root (canvas));
	window = GTK_WIDGET (canvas)->window;
	
	for (i = 0; i < GNUMERIC_SHEET_PATTERNS; i++){
		GnomeCanvasRE *item;
		int x, y;

		x = i % 7;
		y = i / 7;
		
		item = GNOME_CANVAS_RE (gnome_canvas_item_new (
			group,
			gnome_canvas_rect_get_type (),
			"x1",           (double) x * 1.0 + 0.1,
			"y1",           (double) y * 1.0 + 0.1,
			"x2",           (double) x * 1.0 + 1.2,
			"y2",           (double) y * 1.0 + 1.2,
			"fill_color",   "black",
			"width_pixels", (int) 1,
			NULL));
#if 0
		patterns [i] = gdk_bitmap_create_from_data (
			window, gnumeric_sheet_patterns [i].pattern, 8, 8);

		gdk_gc_set_stipple (item->fill_gc, patterns [i]);
		gdk_gc_set_fill (item->fill_gc, GDK_STIPPLED);
#endif
	}
}

static GtkWidget *
create_pattern_preview (GtkWidget *prop_win)
{
	GnomeCanvas *canvas;

	canvas = (GnomeCanvas *) gnome_canvas_new ();
	
	gnome_canvas_set_scroll_region (canvas, 0.0, 0.0, 14.0, 4.0);
	gnome_canvas_set_size (canvas, 112, 112);
	gtk_signal_connect_after (
		GTK_OBJECT (canvas), "realize",
		GTK_SIGNAL_FUNC (create_stipples), NULL);

	return GTK_WIDGET (canvas);
}

static GtkWidget *
create_background_radio (GtkWidget *prop_win)
{
	GtkWidget *frame, *table, *r1, *r2, *r3, *cs1, *cs2, *p;
	int e = GTK_FILL | GTK_EXPAND;
	
	frame = gtk_frame_new (_("Background configuration"));
        table = gtk_table_new (2, 2, 0);
	gtk_container_add (GTK_CONTAINER (frame), table);

	/* The radio buttons */
	r1 = gtk_radio_button_new_with_label (NULL, _("None"));
	background_radio_list = GTK_RADIO_BUTTON (r1)->group;
	r2 = gtk_radio_button_new_with_label (background_radio_list,
					      _("Use solid color"));
	r3 = gtk_radio_button_new_with_label (background_radio_list,
					      _("Use a pattern"));

	/* The color selectors */
	cs1 = gtk_label_new ("color selector goes here");
	cs2 = gtk_label_new ("color selector goes here");

	/* Create the pattern preview */
	p = create_pattern_preview (prop_win);
	
	gtk_table_attach (GTK_TABLE (table), r1, 0, 1, 0, 1, e, 0, 0, 0);
	gtk_table_attach (GTK_TABLE (table), r2, 0, 1, 1, 2, e, 0, 0, 0);
	gtk_table_attach (GTK_TABLE (table), r3, 0, 1, 2, 3, e, 0, 0, 0);

	gtk_table_attach (GTK_TABLE (table), cs1, 1, 2, 1, 2, 0, 0, 0, 0);
	gtk_table_attach (GTK_TABLE (table), cs2, 1, 2, 2, 3, 0, 0, 0, 0);
	gtk_table_attach (GTK_TABLE (table), p, 0, 2, 3, 4, 0, 0, 0, 0);
	return frame;
}

static GtkWidget *
create_coloring_page (GtkWidget *prop_win, CellList *cells)
{
	GtkTable *t;
	GtkWidget *fore, *back;
	int e = GTK_FILL | GTK_EXPAND;
	
	t = (GtkTable *) gtk_table_new (0, 0, 0);
	fore = create_foreground_radio (prop_win);
	back = create_background_radio (prop_win);

	gtk_table_attach (t, fore, 0, 1, 0, 1, e, 0, 4, 4);
	gtk_table_attach (t, back, 0, 1, 1, 2, e, 0, 4, 4);
	gtk_widget_show_all (GTK_WIDGET (t));
	return GTK_WIDGET (t);
}

static void
apply_coloring_format (Style *style, Sheet *sheet, CellList *cells)
{
}

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
760 761 762
static struct {
	char      *title;
	GtkWidget *(*create_page)(GtkWidget *prop_win, CellList *cells);
Miguel de Icaza's avatar
Miguel de Icaza committed
763
	void      (*apply_page)(Style *style, Sheet *sheet, CellList *cells);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
764
} cell_format_pages [] = {
765 766 767 768
	{ N_("Number"),    create_number_format_page,  apply_number_formats  },
	{ N_("Alignment"), create_align_page,          apply_align_format    },
	{ N_("Font"),      create_font_page,           apply_font_format     },
	{ N_("Coloring"),  create_coloring_page,       apply_coloring_format },
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
769 770 771 772 773 774 775
	{ NULL, NULL }
};

static void
cell_properties_apply (GtkObject *w, int page, CellList *cells)
{
	Sheet *sheet;
Miguel de Icaza's avatar
Miguel de Icaza committed
776 777
	Style *style;
	GList *l;
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
778 779 780 781 782 783 784
	int i;
	
	if (page != -1)
		return;

	sheet = (Sheet *) gtk_object_get_data (w, "Sheet");

Miguel de Icaza's avatar
Miguel de Icaza committed
785 786 787 788
	/* Now, let each property page apply their style */
	style = style_new_empty ();
	style->valid_flags = 0;
	
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
789
	for (i = 0; cell_format_pages [i].title; i++)
Miguel de Icaza's avatar
Miguel de Icaza committed
790 791 792 793 794 795 796 797 798 799 800 801
		(*cell_format_pages [i].apply_page)(style, sheet, cells);

	/* Attach this style to all of the selections */
	for (l = sheet->selections; l; l = l->next){
		SheetSelection *ss = l->data;
		
		sheet_style_attach (
			sheet,
			ss->start_col, ss->start_row,
			ss->end_col,   ss->end_row,
			style);
	}
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
802 803 804 805 806 807
}

static void
cell_properties_close (void)
{
	gtk_main_quit ();
808
	gtk_widget_destroy (cell_format_prop_win);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
809 810
	cell_format_prop_win = 0;
}
811 812 813 814

/*
 * Main entry point for the Cell Format dialog box
 */
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
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
void
dialog_cell_format (Sheet *sheet)
{
	GtkWidget *prop_win;
	CellList  *cells;
	int i;

	g_return_if_fail (sheet != NULL);
	g_return_if_fail (IS_SHEET (sheet));
	g_assert (cell_format_prop_win == NULL);

	cells = sheet_selection_to_list (sheet);
	
	prop_win = gnome_property_box_new ();
	if (cells)
		first_cell = cells->data;
	else
		first_cell = NULL;
	
	for (i = 0; cell_format_pages [i].title; i++){
		GtkWidget *page;

		page = (*cell_format_pages [i].create_page)(prop_win, cells);
		gnome_property_box_append_page (
			GNOME_PROPERTY_BOX (prop_win), page,
			gtk_label_new (_(cell_format_pages [i].title)));

	}

	gtk_signal_connect (GTK_OBJECT (prop_win), "apply",
			    GTK_SIGNAL_FUNC (cell_properties_apply), cells);
	gtk_object_set_data (GTK_OBJECT (prop_win), "Sheet", sheet);
	
	gtk_signal_connect (GTK_OBJECT (prop_win), "destroy",
			    GTK_SIGNAL_FUNC (cell_properties_close), NULL);
	
	gtk_widget_show (prop_win);
	gtk_grab_add (prop_win);
Arturo Espinosa's avatar
Arturo Espinosa committed
853

Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
854 855 856
	cell_format_prop_win = prop_win;
	gtk_main ();
	cell_format_prop_win = NULL;
Arturo Espinosa's avatar
Arturo Espinosa committed
857 858

	g_list_free (cells);
Miguel de Icaza's avatar
Today:  
Miguel de Icaza committed
859
}
Arturo Espinosa's avatar
Arturo Espinosa committed
860