application.c 19.9 KB
Newer Older
1 2 3 4
/*
 * application.c: Manage the data common to all workbooks
 *
 * Author:
5
 *     Jody Goldberg <jody@gnome.org>
6
 */
7 8
#include <gnumeric-config.h>
#include "gnumeric.h"
9
#include "application.h"
10

11
#include "clipboard.h"
12
#include "selection.h"
13
#include "workbook-control.h"
14
#include "workbook-view.h"
15 16
#include "workbook.h"
#include "sheet.h"
17
#include "sheet-private.h"
18
#include "auto-correct.h"
19

20
#include "pixmaps/menu-print-preview.xpm"
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include "pixmaps/print-preview.xpm"
#include "pixmaps/sort-descending.xpm"
#include "pixmaps/auto-sum.xpm"
#include "pixmaps/equal-sign.xpm"
#include "pixmaps/function_selector.xpm"

#include "pixmaps/label.xpm"
#include "pixmaps/frame.xpm"
#include "pixmaps/button.xpm"
#include "pixmaps/checkbutton.xpm"
#include "pixmaps/radiobutton.xpm"
#include "pixmaps/list.xpm"
#include "pixmaps/combo.xpm"

#include "pixmaps/rect.xpm"
#include "pixmaps/line.xpm"
#include "pixmaps/arrow.xpm"
#include "pixmaps/oval.xpm"

#include "pixmaps/money.xpm"
#include "pixmaps/percent.xpm"
#include "pixmaps/thousands.xpm"
#include "pixmaps/add_decimals.xpm"
#include "pixmaps/remove_decimals.xpm"

46 47
#include "pixmaps/16_save.xpm"
#include "pixmaps/16_save_as.xpm"
48
#include "pixmaps/16_search.xpm"
49 50 51 52 53 54
#include "pixmaps/16_search_and_replace.xpm"
#include "pixmaps/16_cut.xpm"
#include "pixmaps/16_copy.xpm"
#include "pixmaps/16_paste.xpm"
#include "pixmaps/16_undo.xpm"
#include "pixmaps/16_redo.xpm"
Jody Goldberg's avatar
Jody Goldberg committed
55 56 57 58 59 60 61 62 63 64 65 66
#include "pixmaps/16_add_column.xpm"
#include "pixmaps/16_add_comment.xpm"
#include "pixmaps/16_add_row.xpm"
#include "pixmaps/16_cell_height.xpm"
#include "pixmaps/16_cell_width.xpm"
#include "pixmaps/16_center_across_selection.xpm"
#include "pixmaps/16_delete_column.xpm"
#include "pixmaps/16_delete_comment.xpm"
#include "pixmaps/16_delete_row.xpm"
#include "pixmaps/16_edit_comment.xpm"
#include "pixmaps/16_hide_column.xpm"
#include "pixmaps/16_hide_row.xpm"
67 68
#include "pixmaps/16_insert_component.xpm"
#include "pixmaps/16_insert_shaped_component.xpm"
69
#include "pixmaps/16_graph_guru.xpm"
Jody Goldberg's avatar
Jody Goldberg committed
70 71
#include "pixmaps/16_merge_cells.xpm"
#include "pixmaps/16_split_cells.xpm"
72
#include "pixmaps/16_sort_ascending.xpm"
Jody Goldberg's avatar
Jody Goldberg committed
73 74
#include "pixmaps/16_unhide_column.xpm"
#include "pixmaps/16_unhide_row.xpm"
75 76 77 78
#include "pixmaps/16_group.xpm"
#include "pixmaps/16_ungroup.xpm"
#include "pixmaps/16_show_detail.xpm"
#include "pixmaps/16_hide_detail.xpm"
Jody Goldberg's avatar
Jody Goldberg committed
79

80
#include "pixmaps/24_save.xpm"
81 82 83 84 85
#include "pixmaps/24_cut.xpm"
#include "pixmaps/24_copy.xpm"
#include "pixmaps/24_paste.xpm"
#include "pixmaps/24_undo.xpm"
#include "pixmaps/24_redo.xpm"
Jody Goldberg's avatar
Jody Goldberg committed
86 87 88 89 90 91 92 93 94
#include "pixmaps/24_add_column.xpm"
#include "pixmaps/24_add_row.xpm"
#include "pixmaps/24_cell_height.xpm"
#include "pixmaps/24_cell_width.xpm"
#include "pixmaps/24_center_across_selection.xpm"
#include "pixmaps/24_delete_column.xpm"
#include "pixmaps/24_delete_row.xpm"
#include "pixmaps/24_hide_column.xpm"
#include "pixmaps/24_hide_row.xpm"
95 96
#include "pixmaps/24_insert_component.xpm"
#include "pixmaps/24_insert_shaped_component.xpm"
97
#include "pixmaps/24_graph_guru.xpm"
Jody Goldberg's avatar
Jody Goldberg committed
98 99
#include "pixmaps/24_merge_cells.xpm"
#include "pixmaps/24_split_cells.xpm"
100
#include "pixmaps/24_sort_ascending.xpm"
Jody Goldberg's avatar
Jody Goldberg committed
101 102
#include "pixmaps/24_unhide_column.xpm"
#include "pixmaps/24_unhide_row.xpm"
103 104 105 106
#include "pixmaps/24_group.xpm"
#include "pixmaps/24_ungroup.xpm"
#include "pixmaps/24_show_detail.xpm"
#include "pixmaps/24_hide_detail.xpm"
Jody Goldberg's avatar
Jody Goldberg committed
107

108 109 110 111
#include <gtk/gtk.h>
#include <libgnome/gnome-config.h>
#include <libgnomeui/gnome-stock.h>

112 113 114
typedef struct
{
	/* Clipboard */
115 116 117
	Sheet		*clipboard_sheet;
	CellRegion	*clipboard_copied_contents;
	Range		 clipboard_cut_range;
118 119

	/* Display resolution */
120
	double           horizontal_dpi, vertical_dpi;
121 122 123

	/* History for file menu */
	GList           *history_list;
Miguel de Icaza's avatar
Miguel de Icaza committed
124 125

	gboolean         edit_auto_complete;
126
	gboolean         live_scrolling;
127
	int         	 auto_expr_recalc_lag;
128 129 130 131 132 133 134 135
} GnumericApplication;

static GnumericApplication app;

/**
 * application_init:
 *
 * Initialize the application specific data structures.
136
 * and register some extra stock icons.
137 138
 */
void
Morten Welinder's avatar
Morten Welinder committed
139
application_init (void)
140
{
141 142 143 144 145
	static struct GnumericStockPixmap{
		int width, height;
		char const * const name;
		gchar **xpm_data;
	} const entry_names [] = {
146 147
		{ 16, 16, "Menu_Gnumeric_Save", i16_save_xpm },
		{ 16, 16, "Menu_Gnumeric_SaveAs", i16_save_as_xpm },
148
		{ 16, 16, "Menu_Gnumeric_Search", i16_search },
149
		{ 16, 16, "Menu_Gnumeric_SearchAndReplace", i16_search_and_replace },
150
		{ 16, 16, "Menu_Gnumeric_PrintPreview", menu_print_preview_xpm },
151 152
		{ 16, 16, "Menu_Gnumeric_InsertComponent", i16_insert_component_xpm },
		{ 16, 16, "Menu_Gnumeric_InsertShapedComponent", i16_insert_shaped_component_xpm },
153
		{ 16, 16, "Menu_Gnumeric_GraphGuru", i16_graph_guru_xpm },
Jody Goldberg's avatar
Jody Goldberg committed
154

155 156 157 158 159
		{ 16, 16, "Menu_Gnumeric_Cut", i16_cut_xpm },
		{ 16, 16, "Menu_Gnumeric_Copy", i16_copy_xpm },
		{ 16, 16, "Menu_Gnumeric_Paste", i16_paste_xpm },
		{ 16, 16, "Menu_Gnumeric_Undo", i16_undo_xpm },
		{ 16, 16, "Menu_Gnumeric_Redo", i16_redo_xpm },
Jody Goldberg's avatar
Jody Goldberg committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
		{ 16, 16, "Menu_Gnumeric_CommentAdd", i16_add_comment_xpm },
		{ 16, 16, "Menu_Gnumeric_CommentDelete", i16_delete_comment_xpm },
		{ 16, 16, "Menu_Gnumeric_CommentEdit", i16_edit_comment_xpm },
		{ 16, 16, "Menu_Gnumeric_ColumnAdd", i16_add_column_xpm },
		{ 16, 16, "Menu_Gnumeric_ColumnDelete", i16_delete_column_xpm },
		{ 16, 16, "Menu_Gnumeric_ColumnSize", i16_cell_width_xpm },
		{ 16, 16, "Menu_Gnumeric_ColumnHide", i16_hide_column_xpm },
		{ 16, 16, "Menu_Gnumeric_ColumnUnhide", i16_unhide_column_xpm },
		{ 16, 16, "Menu_Gnumeric_RowAdd", i16_add_row_xpm },
		{ 16, 16, "Menu_Gnumeric_RowDelete", i16_delete_row_xpm },
		{ 16, 16, "Menu_Gnumeric_RowSize", i16_cell_height_xpm },
		{ 16, 16, "Menu_Gnumeric_RowHide", i16_hide_row_xpm },
		{ 16, 16, "Menu_Gnumeric_RowUnhide", i16_unhide_row_xpm },
		{ 16, 16, "Menu_Gnumeric_MergeCells", i16_merge_cells_xpm },
		{ 16, 16, "Menu_Gnumeric_SplitCells", i16_split_cells_xpm },
175
		{ 16, 16, "Menu_Gnumeric_SortAscending", i16_sort_ascending_xpm },
Jody Goldberg's avatar
Jody Goldberg committed
176
		{ 16, 16, "Menu_Gnumeric_CenterAcrossSelection", i16_center_across_selection_xpm },
177 178 179 180
		{ 16, 16, "Menu_Gnumeric_ShowDetail", i16_show_detail_xpm },
		{ 16, 16, "Menu_Gnumeric_HideDetail", i16_hide_detail_xpm },
		{ 16, 16, "Menu_Gnumeric_Group", i16_group_xpm },
		{ 16, 16, "Menu_Gnumeric_Ungroup", i16_ungroup_xpm },
Jody Goldberg's avatar
Jody Goldberg committed
181

182
		{ 24, 24, "Gnumeric_PrintPreview", print_preview_xpm },
183 184
		{ 24, 24, "Gnumeric_InsertComponent", i24_insert_component_xpm },
		{ 24, 24, "Gnumeric_InsertShapedComponent", i24_insert_shaped_component_xpm },
185
		{ 24, 24, "Gnumeric_GraphGuru", i24_graph_guru_xpm },
Jody Goldberg's avatar
Jody Goldberg committed
186 187 188 189 190
#if 0
		{ 24, 24, "Gnumeric_CommentAdd", i24_add_comment_xpm },
		{ 24, 24, "Gnumeric_CommentDelete", i24_delete_comment_xpm },
		{ 24, 24, "Gnumeric_CommentEdit", i24_edit_comment_xpm },
#endif
191
		{ 24, 24, "Gnumeric_Save", i24_save_xpm },
192 193 194 195 196
		{ 24, 24, "Gnumeric_Cut", i24_cut_xpm },
		{ 24, 24, "Gnumeric_Copy", i24_copy_xpm },
		{ 24, 24, "Gnumeric_Paste", i24_paste_xpm },
		{ 24, 24, "Gnumeric_Undo", i24_undo_xpm },
		{ 24, 24, "Gnumeric_Redo", i24_redo_xpm },
Jody Goldberg's avatar
Jody Goldberg committed
197 198 199 200 201 202 203 204 205 206 207 208
		{ 24, 24, "Gnumeric_ColumnAdd", i24_add_column_xpm },
		{ 24, 24, "Gnumeric_ColumnDelete", i24_delete_column_xpm },
		{ 24, 24, "Gnumeric_ColumnSize", i24_cell_width_xpm },
		{ 24, 24, "Gnumeric_ColumnHide", i24_hide_column_xpm },
		{ 24, 24, "Gnumeric_ColumnUnhide", i24_unhide_column_xpm },
		{ 24, 24, "Gnumeric_RowAdd", i24_add_row_xpm },
		{ 24, 24, "Gnumeric_RowDelete", i24_delete_row_xpm },
		{ 24, 24, "Gnumeric_RowSize", i24_cell_height_xpm },
		{ 24, 24, "Gnumeric_RowHide", i24_hide_row_xpm },
		{ 24, 24, "Gnumeric_RowUnhide", i24_unhide_row_xpm },
		{ 24, 24, "Gnumeric_MergeCells", i24_merge_cells_xpm },
		{ 24, 24, "Gnumeric_SplitCells", i24_split_cells_xpm },
209
		{ 24, 24, "Gnumeric_SortAscending", i24_sort_ascending_xpm },
Jody Goldberg's avatar
Jody Goldberg committed
210
		{ 24, 24, "Gnumeric_CenterAcrossSelection", i24_center_across_selection_xpm },
211 212 213 214
		{ 24, 24, "Gnumeric_ShowDetail", i24_show_detail_xpm },
		{ 24, 24, "Gnumeric_HideDetail", i24_hide_detail_xpm },
		{ 24, 24, "Gnumeric_Group", i24_group_xpm },
		{ 24, 24, "Gnumeric_Ungroup", i24_ungroup_xpm },
Jody Goldberg's avatar
Jody Goldberg committed
215

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
		{ 24, 24, "Gnumeric_SortDescending", sort_descending_xpm },
		{ 24, 24, "Gnumeric_AutoSum", auto_sum_xpm },
		{ 24, 24, "Gnumeric_EqualSign", equal_sign_xpm },
		{ 24, 24, "Gnumeric_FormulaGuru", formula_guru_xpm },

		{ 24, 21, "Gnumeric_FormatAsMoney", money_xpm },
		{ 24, 21, "Gnumeric_FormatAsPercent", percent_xpm },
		{ 24, 21, "Gnumeric_FormatThousandSeperator", thousands_xpm },
		{ 24, 21, "Gnumeric_FormatAddPrecision", add_decimals_xpm },
		{ 24, 21, "Gnumeric_FormatRemovePrecision", remove_decimals_xpm },

		{ 21, 21, "Gnumeric_Label", label_xpm },
		{ 21, 21, "Gnumeric_Frame", frame_xpm },
		{ 21, 21, "Gnumeric_Button", button_xpm },
		{ 21, 21, "Gnumeric_Checkbutton", checkbutton_xpm },
		{ 21, 21, "Gnumeric_Radiobutton", radiobutton_xpm },
		{ 21, 21, "Gnumeric_List", list_xpm },
		{ 21, 21, "Gnumeric_Combo", combo_xpm },
		{ 21, 21, "Gnumeric_Line", line_xpm },
		{ 21, 21, "Gnumeric_Arrow", arrow_xpm },
		{ 21, 21, "Gnumeric_Rectangle", rect_xpm },
		{ 21, 21, "Gnumeric_Oval", oval_xpm },
Jody Goldberg's avatar
Jody Goldberg committed
238

239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
		{ 0, 0, NULL, NULL}
	};
	static GnomeStockPixmapEntry entry[sizeof(entry_names)/sizeof(struct GnumericStockPixmap)-1];

	int i = 0;

	for (i = 0; entry_names[i].name != NULL ; i++) {
		entry[i].data.type = GNOME_STOCK_PIXMAP_TYPE_DATA;
		entry[i].data.width = entry_names[i].width;
		entry[i].data.height = entry_names[i].height;
		entry[i].data.xpm_data = entry_names[i].xpm_data;
		gnome_stock_pixmap_register (entry_names[i].name,
					     GNOME_STOCK_PIXMAP_REGULAR, entry + i);
	}

254 255
	app.clipboard_copied_contents = NULL;
	app.clipboard_sheet = NULL;
256

257
	/* FIXME : 96 as the default will scale Helvetica-9 to Helvetica-12
258 259
	 * which is not too ugly.  Ideally we could get the correct values here but,
	 *
260
	 * XFree86-3) lies.  It defaults to 75x75 dpi and only allows the user to
261 262 263 264
	 *            specify one resolution, which it uses for both axis.
	 * XFree86-4) Makes a better guess, but still seems to use the same
	 *            resolution for both directions.
	 *
265
	 * XL seems to assume that everything is 96 dpi.
266 267 268 269
	 *
	 * I'll leave it as is for now, and revisit the solution when we shake
	 * out the flaws in the display code.
	 */
270
	gnome_config_push_prefix ("Gnumeric/Screen_Resolution/");
271 272
	app.horizontal_dpi = gnome_config_get_float ("Horizontal_dpi=96");
	app.vertical_dpi = gnome_config_get_float ("Vertical_dpi=96");
273
	gnome_config_pop_prefix ();
Miguel de Icaza's avatar
Miguel de Icaza committed
274

275
	gnome_config_push_prefix ("Gnumeric/Editing/");
276 277
	app.edit_auto_complete	= gnome_config_get_bool ("AutoComplete=true");
	app.live_scrolling	= gnome_config_get_bool ("LiveScrolling=true");
278 279 280 281 282 283 284 285

	/* If positive auto expressions are recalculated within <lag>
	 * millesecond after a change.
	 * if negative they are recalculated with <lag> milleseconds after the
	 * last change where 'last' is defined as a periond > <lag> after a
	 * change with no changes.
	 */
	app.auto_expr_recalc_lag = gnome_config_get_int ("AutoExprRecalcLag=200");
Miguel de Icaza's avatar
Miguel de Icaza committed
286
	gnome_config_pop_prefix ();
287 288

	autocorrect_init ();
289 290
}

291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
static GList *workbook_list = NULL;
void
application_workbook_list_add (Workbook *wb)
{
	workbook_list = g_list_prepend (workbook_list, wb);
}
void
application_workbook_list_remove (Workbook *wb)
{
	workbook_list = g_list_remove (workbook_list, wb);
}
GList *
application_workbook_list (void)
{
	return workbook_list;
}

308 309 310 311 312 313 314
/**
 * application_clipboard_clear:
 *
 * Clear and free the contents of the clipboard if it is
 * not empty.
 */
void
315
application_clipboard_clear (gboolean drop_selection)
316 317
{
	if (app.clipboard_copied_contents) {
318
		cellregion_free (app.clipboard_copied_contents);
319 320
		app.clipboard_copied_contents = NULL;
	}
321 322
	if (app.clipboard_sheet != NULL) {
		Sheet *sheet = app.clipboard_sheet;
323

324
		sheet_unant (sheet);
325 326
		
		sheet->priv->enable_paste_special = FALSE;
327
		WORKBOOK_FOREACH_CONTROL (sheet->workbook, view, control,
328
			wb_control_menu_state_update (control, sheet, MS_PASTE_SPECIAL););
329
			
330
		app.clipboard_sheet = NULL;
Jody Goldberg's avatar
Jody Goldberg committed
331 332

		/* Release the selection */
333 334 335 336
		if (drop_selection)
			gtk_selection_owner_set (NULL,
						 GDK_SELECTION_PRIMARY,
						 GDK_CURRENT_TIME);
337
	}
338 339
}

340 341 342 343
void
application_clipboard_unant (void)
{
	if (app.clipboard_sheet != NULL)
344
		sheet_unant (app.clipboard_sheet);
345 346
}

347
static gboolean
348
application_set_selected_sheet (WorkbookControl *wbc, Sheet *sheet)
349
{
350
	g_return_val_if_fail (IS_SHEET (sheet), FALSE);
351

352
	application_clipboard_clear (FALSE);
353

354
	/* FIXME : how to do this on a per display basis ? */
355
	if (wb_control_claim_selection (wbc)) {
356 357 358 359 360 361 362 363 364
		app.clipboard_sheet = sheet;
		return TRUE;
	}

	g_warning ("Unable to set selection ?");

	return FALSE;
}

365
/**
366
 * application_clipboard_cut_copy:
367
 *
368
 * @wbc   : the workbook control that requested the operation.
369
 * @is_cut: is this a cut or a copy.
370 371
 * @sheet : The source sheet for the copy.
 * @area  : A single rectangular range to be copied.
372
 * @animate_cursor : Do we want ot add an animated cursor around things.
373
 *
374 375 376 377
 * When Cutting we
 *   Clear and free the contents of the clipboard and save the sheet and area
 *   to be cut.  DO NOT ACTUALLY CUT!  Paste will move the region if this was a
 *   cut operation.
378
 *
379 380 381
 * When Copying we
 *   Clear and free the contents of the clipboard and COPY the designated region
 *   into the clipboard.
382 383 384
 *
 */
void
385 386 387
application_clipboard_cut_copy (WorkbookControl *wbc, gboolean is_cut,
				Sheet *sheet, Range const *area,
				gboolean animate_cursor)
388
{
389
	g_return_if_fail (IS_SHEET (sheet));
390
	g_return_if_fail (area != NULL);
391

392
	if (application_set_selected_sheet (wbc, sheet) ) {
393
		app.clipboard_cut_range = *area;
394

395 396 397 398 399
		sheet->priv->enable_paste_special = !is_cut;
		if (!is_cut)
			app.clipboard_copied_contents =
				clipboard_copy_range (sheet, area);

400
		WORKBOOK_FOREACH_CONTROL (sheet->workbook, view, control,
401
			wb_control_menu_state_update (control, sheet, MS_PASTE_SPECIAL););
402

403 404 405 406 407 408 409 410
		if (animate_cursor) {
			/* * The 'area' and the list itself will be copied
			 * entirely. We ant the copied range on the sheet.
			 */
			GList *l = g_list_append (NULL, (Range *) area);
			sheet_ant (sheet, l);
			g_list_free (l);
		}
411
	}
412 413 414
}

gboolean
Morten Welinder's avatar
Morten Welinder committed
415
application_clipboard_is_empty (void)
416 417 418 419
{
	return app.clipboard_sheet == NULL;
}

420 421 422 423 424 425 426 427
gboolean
application_clipboard_is_cut (void)
{
	if (app.clipboard_sheet != NULL)
		return app.clipboard_copied_contents ? FALSE : TRUE;
	return FALSE;
}

428
Sheet *
Morten Welinder's avatar
Morten Welinder committed
429
application_clipboard_sheet_get (void)
430 431 432 433 434
{
	return app.clipboard_sheet;
}

CellRegion *
Morten Welinder's avatar
Morten Welinder committed
435
application_clipboard_contents_get (void)
436 437 438 439 440
{
	return app.clipboard_copied_contents;
}

Range const *
Morten Welinder's avatar
Morten Welinder committed
441
application_clipboard_area_get (void)
442 443 444 445 446 447 448
{
	/*
	 * Only return the range if the sheet has been set.
	 * The range will still contain data even after
	 * the clipboard has been cleared so we need to be extra
	 * safe and only return a range if there is a valid selection
	 */
449
	if (app.clipboard_sheet != NULL)
450 451 452
		return &app.clipboard_cut_range;
	return NULL;
}
Jody Goldberg's avatar
Jody Goldberg committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475

struct wb_name_closure
{
	Workbook *wb;
	char const * name;
};
static gboolean
cb_workbook_name (Workbook * wb, gpointer closure)
{
	struct wb_name_closure *dat = closure;
	if (0 == strcmp (wb->filename, dat->name)) {
		dat->wb = wb;
		return FALSE;
	}
	return TRUE;
}

Workbook *
application_workbook_get_by_name (char const * const name)
{
	struct wb_name_closure close;
	close.wb = NULL;
	close.name = name;
476
	application_workbook_foreach (&cb_workbook_name, &close);
Jody Goldberg's avatar
Jody Goldberg committed
477 478 479 480

	return close.wb;
}

481 482 483 484 485 486 487 488 489 490 491 492 493 494
gboolean
application_workbook_foreach (WorkbookCallback cback, gpointer data)
{
	GList *l;

	for (l = workbook_list; l; l = l->next){
		Workbook *wb = l->data;

		if (!(*cback)(wb, data))
			return FALSE;
	}
	return TRUE;
}

Jody Goldberg's avatar
Jody Goldberg committed
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
struct wb_index_closure
{
	Workbook *wb;
	int index;	/* 1 based */
};
static gboolean
cb_workbook_index (Workbook * wb, gpointer closure)
{
	struct wb_index_closure *dat = closure;
	return (--(dat->index) != 0);
}

Workbook *
application_workbook_get_by_index (int i)
{
	struct wb_index_closure close;
	close.wb = NULL;
	close.index = i;
513
	application_workbook_foreach (&cb_workbook_index, &close);
Jody Goldberg's avatar
Jody Goldberg committed
514 515 516

	return close.wb;
}
517

518
double
519
application_display_dpi_get (gboolean horizontal)
520
{
521
	return horizontal ? app.horizontal_dpi : app.vertical_dpi;
522 523 524
}

void
525
application_display_dpi_set (gboolean horizontal, double val)
526
{
527 528 529 530 531 532
	if (horizontal)
		app.horizontal_dpi  = val;
	else
		app.vertical_dpi = val;
}

533
double
534
application_dpi_to_pixels (void)
535 536 537
{
	return MIN (application_display_dpi_get (FALSE),
		    application_display_dpi_get (TRUE)) / 72.;
538 539
}

540 541
/**
 * application_history_get_list:
542
 *
543 544
 *  This function returns a pointer to the history list,
 * creating it if neccessary.
545
 *
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560
 * Return value: the list./
 **/
GList*
application_history_get_list (void)
{
        gchar *filename, *key;
        gint max_entries, i;
	gboolean do_set = FALSE;

	/* If the list is already populated, return it. */
	if (app.history_list)
		return app.history_list;

        gnome_config_push_prefix ("/Gnumeric/History/");

561
        /* Get maximum number of history entries.  Write default value to
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
	 * config file if no entry exists. */
        max_entries = gnome_config_get_int_with_default ("MaxFiles=4", &do_set);
	if (do_set)
		gnome_config_set_int ("MaxFiles", 4);

	/* Read the history filenames from the config file */
        for (i = 0; i < max_entries; i++) {
		key = g_strdup_printf ("File%d", i);
 	        filename = gnome_config_get_string (key);
 	        if (filename == NULL) {
                       /* Ran out of filenames. */
                       g_free (key);
                       break;
		}
		app.history_list = g_list_append (app.history_list, filename);
               	g_free (key);
       	}
       	gnome_config_pop_prefix ();

	return app.history_list;
}

/**
 * application_history_update_list:
586 587 588
 * @filename:
 *
 * This function updates the history list.  The return value is a
589 590
 * pointer to the filename that was removed, if the list was already full
 * or NULL if no items were removed.
591 592
 *
 * Return value:
593 594
 **/
gchar *
595
application_history_update_list (const gchar *filename)
596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615
{
	gchar *name, *old_name = NULL;
	GList *l = NULL;
	GList *new_list = NULL;
	gint max_entries, count = 0;
	gboolean do_set = FALSE;
	gboolean found = FALSE;

	g_return_val_if_fail (filename != NULL, NULL);

	/* Get maximum list length from config */
	gnome_config_push_prefix ("Gnumeric/History/");
	max_entries = gnome_config_get_int_with_default ("MaxFiles=4", &do_set);
	if (do_set)
		gnome_config_set_int ("MaxFiles", max_entries);
	gnome_config_pop_prefix ();

	/* Check if this filename already exists in the list */
	for (l = app.history_list; l && (count < max_entries); l = l->next) {

616
		if (!found && (!strcmp ((gchar *)l->data, filename) ||
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657
			       (count == max_entries - 1))) {
			/* This is either the last item in the list, or a
			 * duplicate of the requested entry. */
			old_name = (gchar *)l->data;
			found = TRUE;
		} else  /* Add this item to the new list */
			new_list = g_list_append (new_list, l->data);

		count++;
	}

	/* Insert the new filename to the new list and free up the old list */
	name = g_strdup (filename);
	new_list = g_list_prepend (new_list, name);
	g_list_free (app.history_list);
	app.history_list = new_list;

	return old_name;
}

/* Remove the last item from the history list and return it. */
gchar *
application_history_list_shrink (void)
{
	gchar *name;
	GList *l;

	if (app.history_list == NULL)
		return NULL;

	l = g_list_last (app.history_list);
	name = (gchar *)l->data;
	app.history_list = g_list_remove (app.history_list, name);

	return name;
}

/* Write contents of the history list to the configuration file. */
void
application_history_write_config (void)
{
658
	gchar *key;
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
	GList *l;
	gint max_entries, i = 0;

	if (app.history_list == NULL) return;

	max_entries = gnome_config_get_int ("Gnumeric/History/MaxFiles=4");
	gnome_config_clean_section ("Gnumeric/History");
	gnome_config_push_prefix("Gnumeric/History/");
	gnome_config_set_int ("MaxFiles", max_entries);

	for (l = app.history_list; l; l = l->next) {
		key = g_strdup_printf ("File%d", i++);
		gnome_config_set_string (key, (gchar *)l->data);
		g_free (l->data);
		g_free (key);
	}

	gnome_config_sync ();
	gnome_config_pop_prefix ();
	g_list_free (app.history_list);
	app.history_list = NULL;
}

682 683 684
gboolean application_use_auto_complete	  (void) { return app.edit_auto_complete; }
gboolean application_live_scrolling	  (void) { return app.live_scrolling; }
int	 application_auto_expr_recalc_lag (void) { return app.auto_expr_recalc_lag; }