1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
4 * workbook.c: workbook model and manipulation utilities
7 * Miguel de Icaza (miguel@gnu.org).
8 * Jody Goldberg (jody@gnome.org)
10 * (C) 1998, 1999, 2000 Miguel de Icaza
11 * (C) 2000-2001 Ximian, Inc.
12 * (C) 2002-2007 Jody Goldberg
13 * Copyright (C) 1999-2009 Morten Welinder (terra@gnome.org)
15 #include <gnumeric-config.h>
17 #include "workbook-priv.h"
18 #include "compilation.h"
20 #include "workbook-view.h"
21 #include "workbook-control.h"
22 #include "command-context.h"
23 #include "application.h"
24 #include "gnumeric-conf.h"
26 #include "sheet-view.h"
27 #include "sheet-control.h"
30 #include "expr-name.h"
31 #include "dependent.h"
36 #include "libgnumeric.h"
38 #include "gnm-marshalers.h"
39 #include "style-color.h"
40 #include "sheet-style.h"
41 #include "sheet-object-graph.h"
43 #include <goffice/goffice.h>
45 #include <gsf/gsf-doc-meta-data.h>
46 #include <gsf/gsf-impl-utils.h>
47 #include <gsf/gsf-meta-names.h>
54 * @wb_views: (element-type WorkbookView):
67 static guint signals
[LAST_SIGNAL
] = { 0 };
68 static GObjectClass
*workbook_parent_class
;
71 cb_saver_finalize (Workbook
*wb
, GOFileSaver
*saver
)
73 g_return_if_fail (GO_IS_FILE_SAVER (saver
));
74 g_return_if_fail (GNM_IS_WORKBOOK (wb
));
75 g_return_if_fail (wb
->file_saver
== saver
);
76 wb
->file_saver
= NULL
;
79 cb_exporter_finalize (Workbook
*wb
, GOFileSaver
*saver
)
81 g_return_if_fail (GO_IS_FILE_SAVER (saver
));
82 g_return_if_fail (GNM_IS_WORKBOOK (wb
));
83 g_return_if_fail (wb
->file_exporter
== saver
);
84 workbook_set_file_exporter (wb
, NULL
);
88 workbook_update_history (Workbook
*wb
, GnmFileSaveAsStyle type
)
90 g_return_if_fail (GNM_IS_WORKBOOK (wb
));
93 case GNM_FILE_SAVE_AS_STYLE_SAVE
:
94 if (wb
->doc
.uri
&& wb
->file_format_level
>= GO_FILE_FL_MANUAL_REMEMBER
) {
95 const char *mimetype
= wb
->file_saver
96 ? go_file_saver_get_mime_type (wb
->file_saver
)
98 gnm_app_history_add (wb
->doc
.uri
, mimetype
);
101 case GNM_FILE_SAVE_AS_STYLE_EXPORT
:
103 if (wb
->last_export_uri
&&
104 wb
->file_export_format_level
>= GO_FILE_FL_MANUAL_REMEMBER
) {
105 const char *mimetype
= wb
->file_exporter
106 ? go_file_saver_get_mime_type (wb
->file_exporter
)
108 gnm_app_history_add (wb
->last_export_uri
, mimetype
);
115 workbook_update_graphs (Workbook
*wb
)
117 WORKBOOK_FOREACH_SHEET (wb
, sheet
, ({
118 GSList
*l
, *graphs
= sheet_objects_get (sheet
, NULL
, GNM_SO_GRAPH_TYPE
);
119 for (l
= graphs
; l
; l
= l
->next
) {
120 SheetObject
*sog
= l
->data
;
121 gog_graph_force_update (sheet_object_graph_get_gog (sog
));
123 g_slist_free (graphs
);
129 workbook_dispose (GObject
*wb_object
)
131 Workbook
*wb
= WORKBOOK (wb_object
);
132 GSList
*sheets
, *ptr
;
133 GSList
*controls
= NULL
;
135 wb
->during_destruction
= TRUE
;
138 workbook_set_saveinfo (wb
, GO_FILE_FL_AUTO
, NULL
);
139 if (wb
->file_exporter
)
140 workbook_set_saveinfo (wb
, GO_FILE_FL_WRITE_ONLY
, NULL
);
141 workbook_set_last_export_uri (wb
, NULL
);
143 // Remove all the sheet controls to avoid displaying while we exit
144 // However, hold on to a ref for each -- dialogs like to refer
145 // to ->wbcg during destruction
146 WORKBOOK_FOREACH_CONTROL (wb
, view
, control
,
147 controls
= g_slist_prepend (controls
, g_object_ref (control
));
148 wb_control_sheet_remove_all (control
););
150 /* Get rid of all the views */
151 WORKBOOK_FOREACH_VIEW (wb
, wbv
, {
152 wb_view_detach_from_workbook (wbv
);
153 g_object_unref (wbv
);
155 if (wb
->wb_views
!= NULL
)
156 g_warning ("Unexpected left over views");
158 command_list_release (wb
->undo_commands
);
159 wb
->undo_commands
= NULL
;
160 command_list_release (wb
->redo_commands
);
161 wb
->redo_commands
= NULL
;
163 dependents_workbook_destroy (wb
);
165 /* Copy the set of sheets, the list changes under us. */
166 sheets
= workbook_sheets (wb
);
168 /* Remove all contents while all sheets still exist */
169 for (ptr
= sheets
; ptr
!= NULL
; ptr
= ptr
->next
) {
170 Sheet
*sheet
= ptr
->data
;
173 sheet_destroy_contents (sheet
);
174 range_init_full_sheet (&r
, sheet
);
175 sheet_style_set_range (sheet
, &r
, sheet_style_default (sheet
));
178 /* Now remove the sheets themselves */
179 for (ptr
= sheets
; ptr
!= NULL
; ptr
= ptr
->next
) {
180 Sheet
*sheet
= ptr
->data
;
181 workbook_sheet_delete (sheet
);
183 g_slist_free (sheets
);
185 // Now get rid of the control refs
186 g_slist_free_full (controls
, g_object_unref
);
188 workbook_parent_class
->dispose (wb_object
);
192 workbook_finalize (GObject
*obj
)
194 Workbook
*wb
= WORKBOOK (obj
);
196 /* Remove ourselves from the list of workbooks. */
197 gnm_app_workbook_list_remove (wb
);
199 if (wb
->sheet_local_functions
) {
200 g_hash_table_destroy (wb
->sheet_local_functions
);
201 wb
->sheet_local_functions
= NULL
;
204 /* Now do deletions that will put this workbook into a weird
205 state. Careful here. */
206 g_hash_table_destroy (wb
->sheet_hash_private
);
207 wb
->sheet_hash_private
= NULL
;
209 g_ptr_array_free (wb
->sheets
, TRUE
);
212 workbook_parent_class
->finalize (obj
);
216 workbook_init (GObject
*object
)
218 Workbook
*wb
= WORKBOOK (object
);
220 wb
->is_placeholder
= FALSE
;
222 wb
->sheets
= g_ptr_array_new ();
223 wb
->sheet_hash_private
= g_hash_table_new (g_str_hash
, g_str_equal
);
224 wb
->sheet_order_dependents
= NULL
;
225 wb
->sheet_local_functions
= NULL
;
226 wb
->names
= gnm_named_expr_collection_new ();
228 /* Nothing to undo or redo */
229 wb
->undo_commands
= wb
->redo_commands
= NULL
;
231 /* default to no iteration */
232 wb
->iteration
.enabled
= TRUE
;
233 wb
->iteration
.max_number
= 100;
234 wb
->iteration
.tolerance
= .001;
235 wb
->recalc_auto
= TRUE
;
237 workbook_set_1904 (wb
, FALSE
);
239 wb
->file_format_level
= GO_FILE_FL_NEW
;
240 wb
->file_export_format_level
= GO_FILE_FL_NEW
;
241 wb
->file_saver
= NULL
;
242 wb
->file_exporter
= NULL
;
243 wb
->last_export_uri
= NULL
;
245 wb
->during_destruction
= FALSE
;
246 wb
->being_reordered
= FALSE
;
247 wb
->recursive_dirty_enabled
= TRUE
;
249 gnm_app_workbook_list_add (wb
);
253 workbook_get_property (GObject
*object
, guint property_id
,
254 GValue
*value
, GParamSpec
*pspec
)
256 Workbook
*wb
= (Workbook
*)object
;
258 switch (property_id
) {
260 g_value_set_boolean (value
, wb
->recalc_auto
);
263 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
269 workbook_set_property (GObject
*object
, guint property_id
,
270 const GValue
*value
, GParamSpec
*pspec
)
272 Workbook
*wb
= (Workbook
*)object
;
274 switch (property_id
) {
276 workbook_set_recalcmode (wb
, g_value_get_boolean (value
));
279 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
285 workbook_class_init (GObjectClass
*gobject_class
)
287 workbook_parent_class
= g_type_class_peek_parent (gobject_class
);
289 gobject_class
->set_property
= workbook_set_property
;
290 gobject_class
->get_property
= workbook_get_property
;
291 gobject_class
->finalize
= workbook_finalize
;
292 gobject_class
->dispose
= workbook_dispose
;
294 g_object_class_install_property (gobject_class
, RECALC_MODE
,
295 g_param_spec_boolean ("recalc-mode",
297 P_("Enable automatic recalculation."),
302 signals
[SHEET_ORDER_CHANGED
] = g_signal_new ("sheet_order_changed",
305 G_STRUCT_OFFSET (WorkbookClass
, sheet_order_changed
),
307 g_cclosure_marshal_VOID__VOID
,
311 signals
[SHEET_ADDED
] = g_signal_new ("sheet_added",
314 G_STRUCT_OFFSET (WorkbookClass
, sheet_added
),
316 g_cclosure_marshal_VOID__VOID
,
320 signals
[SHEET_DELETED
] = g_signal_new ("sheet_deleted",
323 G_STRUCT_OFFSET (WorkbookClass
, sheet_deleted
),
325 g_cclosure_marshal_VOID__VOID
,
333 * Creates a new empty Workbook
334 * and assigns a unique name.
339 static int count
= 0;
342 GOFileSaver
*def_save
= go_file_saver_get_default ();
343 char const *extension
= NULL
;
345 if (def_save
!= NULL
)
346 extension
= go_file_saver_get_extension (def_save
);
347 if (extension
== NULL
)
348 extension
= "gnumeric";
350 wb
= g_object_new (GNM_WORKBOOK_TYPE
, NULL
);
352 /* Assign a default name */
354 char *name
, *nameutf8
, *uri
;
357 nameutf8
= g_strdup_printf (_("Book%d.%s"), count
, extension
);
358 name
= g_filename_from_utf8 (nameutf8
, -1, NULL
, NULL
, NULL
);
360 name
= g_strdup_printf ("Book%d.%s", count
, extension
);
362 uri
= go_filename_to_uri (name
);
364 is_unique
= go_doc_set_uri (GO_DOC (wb
), uri
);
369 } while (!is_unique
);
370 gnm_insert_meta_date (GO_DOC (wb
), GSF_META_NAME_DATE_CREATED
);
375 * workbook_sheet_name_strip_number:
376 * @name: name to strip number from
377 * @number: returns the number stripped off, or 1 if no number.
379 * Gets a name in the form of "Sheet (10)", "Stuff" or "Dummy ((((,"
380 * and returns the real name of the sheet "Sheet ", "Stuff", "Dummy ((((,"
381 * without the copy number.
384 workbook_sheet_name_strip_number (char *name
, unsigned int *number
)
386 char *end
, *p
, *pend
;
390 g_return_if_fail (*name
!= 0);
392 end
= name
+ strlen (name
) - 1;
396 for (p
= end
; p
> name
; p
--)
397 if (!g_ascii_isdigit (p
[-1]))
400 if (p
== name
|| p
[-1] != '(')
404 ul
= strtoul (p
, &pend
, 10);
405 if (pend
!= end
|| ul
!= (unsigned int)ul
|| errno
== ERANGE
)
408 *number
= (unsigned)ul
;
413 * workbook_new_with_sheets:
414 * @sheet_count: initial number of sheets to create.
416 * Returns a Workbook with @sheet_count allocated
420 workbook_new_with_sheets (int sheet_count
)
422 Workbook
*wb
= workbook_new ();
423 int cols
= gnm_conf_get_core_workbook_n_cols ();
424 int rows
= gnm_conf_get_core_workbook_n_rows ();
425 if (!gnm_sheet_valid_size (cols
, rows
))
426 gnm_sheet_suggest_size (&cols
, &rows
);
427 while (sheet_count
-- > 0)
428 workbook_sheet_add (wb
, -1, cols
, rows
);
429 go_doc_set_dirty (GO_DOC (wb
), FALSE
);
430 GO_DOC (wb
)->pristine
= TRUE
;
435 * workbook_set_saveinfo:
436 * @wb: the workbook to modify
437 * @lev: the file format level
438 * @saver: the file saver.
440 * If level is sufficiently advanced assign the info.
442 * Returns: TRUE if save info was set and history may require updating
444 * FIXME : Add a check to ensure the name is unique.
447 workbook_set_saveinfo (Workbook
*wb
, GOFileFormatLevel level
, GOFileSaver
*fs
)
449 g_return_val_if_fail (wb
!= NULL
, FALSE
);
450 g_return_val_if_fail (level
> GO_FILE_FL_NONE
&& level
< GO_FILE_FL_LAST
,
453 if (level
!= GO_FILE_FL_AUTO
) {
454 if (wb
->file_exporter
!= NULL
)
455 g_object_weak_unref (G_OBJECT (wb
->file_exporter
),
456 (GWeakNotify
) cb_exporter_finalize
, wb
);
457 workbook_set_file_exporter (wb
, fs
);
459 g_object_weak_ref (G_OBJECT (fs
),
460 (GWeakNotify
) cb_exporter_finalize
, wb
);
462 if (wb
->file_saver
!= NULL
)
463 g_object_weak_unref (G_OBJECT (wb
->file_saver
),
464 (GWeakNotify
) cb_saver_finalize
, wb
);
468 g_object_weak_ref (G_OBJECT (fs
),
469 (GWeakNotify
) cb_saver_finalize
, wb
);
472 if (level
!= GO_FILE_FL_AUTO
) {
473 wb
->file_export_format_level
= level
;
476 wb
->file_format_level
= level
;
481 * workbook_get_file_saver:
484 * Returns: (transfer none): the saver for the Workbook.
487 workbook_get_file_saver (Workbook
*wb
)
489 g_return_val_if_fail (GNM_IS_WORKBOOK (wb
), NULL
);
491 return wb
->file_saver
;
495 * workbook_get_file_exporter:
498 * Returns: (transfer none): the exporter for the Workbook.
501 workbook_get_file_exporter (Workbook
*wb
)
503 g_return_val_if_fail (GNM_IS_WORKBOOK (wb
), NULL
);
505 return wb
->file_exporter
;
509 * workbook_get_last_export_uri:
512 * Returns: (transfer none): the URI for export.
515 workbook_get_last_export_uri (Workbook
*wb
)
517 g_return_val_if_fail (GNM_IS_WORKBOOK (wb
), NULL
);
519 return wb
->last_export_uri
;
523 workbook_set_file_exporter (Workbook
*wb
, GOFileSaver
*fs
)
525 wb
->file_exporter
= fs
;
526 WORKBOOK_FOREACH_CONTROL (wb
, wbv
, wbc
,
527 wb_control_menu_state_update (wbc
, MS_FILE_EXPORT_IMPORT
););
531 workbook_set_last_export_uri (Workbook
*wb
, const gchar
*uri
)
533 char *s
= g_strdup (uri
);
534 g_free (wb
->last_export_uri
);
535 wb
->last_export_uri
= s
;
536 WORKBOOK_FOREACH_CONTROL (wb
, wbv
, wbc
,
537 wb_control_menu_state_update (wbc
, MS_FILE_EXPORT_IMPORT
););
542 * workbook_foreach_cell_in_range:
543 * @pos: The position the range is relative to.
544 * @cell_range: A value containing a range;
545 * @flags: if TRUE only existing cells are sent to the handler.
546 * @handler: (scope call): The operator to apply to each cell.
547 * @closure: User data.
549 * The supplied value must be a cellrange.
550 * The range bounds are calculated relative to the eval position
552 * For each existing cell in the range specified, invoke the
553 * callback routine. If the only_existing flag is TRUE, then
554 * callbacks are only invoked for existing cells.
556 * Note: this function does not honour the CELL_ITER_IGNORE_SUBTOTAL flag.
559 * non-NULL on error, or VALUE_TERMINATE if some invoked routine requested
560 * to stop (by returning non-NULL).
563 workbook_foreach_cell_in_range (GnmEvalPos
const *pos
,
564 GnmValue
const *cell_range
,
566 CellIterFunc handler
,
570 Sheet
*start_sheet
, *end_sheet
;
572 g_return_val_if_fail (pos
!= NULL
, NULL
);
573 g_return_val_if_fail (cell_range
!= NULL
, NULL
);
574 g_return_val_if_fail (VALUE_IS_CELLRANGE (cell_range
), NULL
);
576 gnm_rangeref_normalize (&cell_range
->v_range
.cell
, pos
,
577 &start_sheet
, &end_sheet
, &r
);
579 if (start_sheet
!= end_sheet
) {
581 Workbook
const *wb
= start_sheet
->workbook
;
582 int i
= start_sheet
->index_in_wb
;
583 int stop
= end_sheet
->index_in_wb
;
584 if (i
> stop
) { int tmp
= i
; i
= stop
; stop
= tmp
; }
586 g_return_val_if_fail (end_sheet
->workbook
== wb
, VALUE_TERMINATE
);
588 for (; i
<= stop
; i
++) {
589 res
= sheet_foreach_cell_in_range (
590 g_ptr_array_index (wb
->sheets
, i
), flags
,
591 r
.start
.col
, r
.start
.row
, r
.end
.col
, r
.end
.row
,
599 return sheet_foreach_cell_in_range (start_sheet
, flags
,
600 r
.start
.col
, r
.start
.row
, r
.end
.col
, r
.end
.row
,
606 * @wb: The workbook to find cells in.
607 * @comments: If true, include cells with only comments also.
608 * @vis: How visible a sheet needs to be in order to be considered.
610 * Collects a GPtrArray of GnmEvalPos pointers for all cells in a workbook.
611 * No particular order should be assumed.
613 * Returns: (element-type GnmEvalPos) (transfer full): the cells array
616 workbook_cells (Workbook
*wb
, gboolean comments
, GnmSheetVisibility vis
)
618 GPtrArray
*cells
= g_ptr_array_new ();
620 g_return_val_if_fail (wb
!= NULL
, cells
);
622 WORKBOOK_FOREACH_SHEET (wb
, sheet
, {
623 int oldlen
= cells
->len
;
626 if (sheet
->visibility
> vis
)
629 scells
= sheet_cell_positions (sheet
, comments
);
630 g_ptr_array_set_size (cells
, oldlen
+ scells
->len
);
631 memcpy (&g_ptr_array_index (cells
, oldlen
),
632 &g_ptr_array_index (scells
, 0),
633 scells
->len
* sizeof (GnmEvalPos
*));
635 g_ptr_array_free (scells
, TRUE
);
642 workbook_share_expressions (Workbook
*wb
, gboolean freeit
)
644 GnmExprSharer
*es
= gnm_expr_sharer_new ();
646 WORKBOOK_FOREACH_DEPENDENT (wb
, dep
, {
647 if (dependent_is_cell (dep
)) {
648 /* Hopefully safe, even when linked. */
649 dep
->texpr
= gnm_expr_sharer_share (es
, dep
->texpr
);
651 /* Not sure we want to touch this here. */
655 if (gnm_debug_flag ("expr-sharer")) {
656 g_printerr ("Sharing report for %s\n", go_doc_get_uri (GO_DOC (wb
)));
657 gnm_expr_sharer_report (es
);
661 gnm_expr_sharer_destroy (es
);
669 workbook_optimize_style (Workbook
*wb
)
671 WORKBOOK_FOREACH_SHEET (wb
, sheet
, {
672 sheet_style_optimize (sheet
);
677 * workbook_foreach_name:
679 * @globals_only: whether to apply only to global names.
680 * @func: (scope call): The operator to apply to each cell.
685 workbook_foreach_name (Workbook
const *wb
, gboolean globals_only
,
686 GHFunc func
, gpointer data
)
688 g_return_if_fail (GNM_IS_WORKBOOK (wb
));
691 gnm_named_expr_collection_foreach (wb
->names
, func
, data
);
694 WORKBOOK_FOREACH_SHEET (wb
, sheet
, {
695 gnm_sheet_foreach_name (sheet
, func
, data
);
702 workbook_enable_recursive_dirty (Workbook
*wb
, gboolean enable
)
706 g_return_val_if_fail (GNM_IS_WORKBOOK (wb
), FALSE
);
708 old
= wb
->recursive_dirty_enabled
;
709 wb
->recursive_dirty_enabled
= enable
;
714 workbook_set_recalcmode (Workbook
*wb
, gboolean is_auto
)
716 g_return_if_fail (GNM_IS_WORKBOOK (wb
));
717 wb
->recalc_auto
= is_auto
;
721 workbook_get_recalcmode (Workbook
const *wb
)
723 g_return_val_if_fail (GNM_IS_WORKBOOK (wb
), FALSE
);
724 return wb
->recalc_auto
;
728 workbook_iteration_enabled (Workbook
*wb
, gboolean enable
)
730 g_return_if_fail (GNM_IS_WORKBOOK (wb
));
731 wb
->iteration
.enabled
= enable
;
735 workbook_iteration_max_number (Workbook
*wb
, int max_number
)
737 g_return_if_fail (GNM_IS_WORKBOOK (wb
));
738 g_return_if_fail (max_number
>= 0);
739 wb
->iteration
.max_number
= max_number
;
743 workbook_iteration_tolerance (Workbook
*wb
, double tolerance
)
745 g_return_if_fail (GNM_IS_WORKBOOK (wb
));
746 g_return_if_fail (tolerance
>= 0);
748 wb
->iteration
.tolerance
= tolerance
;
752 workbook_attach_view (WorkbookView
*wbv
)
756 g_return_if_fail (GNM_IS_WORKBOOK_VIEW (wbv
));
758 wb
= wb_view_get_workbook (wbv
);
759 g_return_if_fail (GNM_IS_WORKBOOK (wb
));
761 if (wb
->wb_views
== NULL
)
762 wb
->wb_views
= g_ptr_array_new ();
763 g_ptr_array_add (wb
->wb_views
, wbv
);
767 workbook_detach_view (WorkbookView
*wbv
)
769 g_return_if_fail (GNM_IS_WORKBOOK_VIEW (wbv
));
770 g_return_if_fail (GNM_IS_WORKBOOK (wbv
->wb
));
772 WORKBOOK_FOREACH_SHEET (wbv
->wb
, sheet
, {
773 SheetView
*sv
= sheet_get_view (sheet
, wbv
);
777 g_ptr_array_remove (wbv
->wb
->wb_views
, wbv
);
778 if (wbv
->wb
->wb_views
->len
== 0) {
779 g_ptr_array_free (wbv
->wb
->wb_views
, TRUE
);
780 wbv
->wb
->wb_views
= NULL
;
784 /*****************************************************************************/
790 * Get an ordered list of the sheets in the workbook
791 * The caller is required to free the list.
792 * Returns: (element-type Sheet) (transfer container): the sheets list.
795 workbook_sheets (Workbook
const *wb
)
799 g_return_val_if_fail (GNM_IS_WORKBOOK (wb
), NULL
);
802 int i
= wb
->sheets
->len
;
804 list
= g_slist_prepend (list
,
805 g_ptr_array_index (wb
->sheets
, i
));
812 workbook_sheet_count (Workbook
const *wb
)
814 g_return_val_if_fail (GNM_IS_WORKBOOK (wb
), 0);
816 return wb
->sheets
? wb
->sheets
->len
: 0;
820 pre_sheet_index_change (Workbook
*wb
)
822 g_return_if_fail (!wb
->being_reordered
);
824 wb
->being_reordered
= TRUE
;
826 if (wb
->sheet_order_dependents
!= NULL
)
827 g_hash_table_foreach (wb
->sheet_order_dependents
,
828 (GHFunc
)dependent_unlink
,
833 post_sheet_index_change (Workbook
*wb
)
835 g_return_if_fail (wb
->being_reordered
);
837 if (wb
->sheet_order_dependents
!= NULL
)
838 g_hash_table_foreach (wb
->sheet_order_dependents
,
839 (GHFunc
)dependent_link
,
842 wb
->being_reordered
= FALSE
;
844 if (wb
->during_destruction
)
847 g_signal_emit (G_OBJECT (wb
), signals
[SHEET_ORDER_CHANGED
], 0);
851 workbook_sheet_index_update (Workbook
*wb
, int start
)
855 for (i
= wb
->sheets
->len
; i
-- > start
; ) {
856 Sheet
*sheet
= g_ptr_array_index (wb
->sheets
, i
);
857 sheet
->index_in_wb
= i
;
862 * workbook_sheet_by_index:
863 * @wb: workbook to lookup the sheet on
864 * @i: the sheet index we are looking for.
866 * Return value: (transfer none): a pointer to a Sheet or NULL if the sheet
870 workbook_sheet_by_index (Workbook
const *wb
, int i
)
872 g_return_val_if_fail (GNM_IS_WORKBOOK (wb
), NULL
);
873 g_return_val_if_fail (i
< (int)wb
->sheets
->len
, NULL
);
874 g_return_val_if_fail (i
>= -1, NULL
);
876 /* i = -1 is special, return NULL */
881 return g_ptr_array_index (wb
->sheets
, i
);
885 * workbook_sheet_by_name:
886 * @wb: workbook to lookup the sheet on
887 * @sheet_name: the sheet name we are looking for.
889 * Return value: (transfer none): a pointer to a Sheet or NULL if the sheet
893 workbook_sheet_by_name (Workbook
const *wb
, char const *name
)
898 g_return_val_if_fail (GNM_IS_WORKBOOK (wb
), NULL
);
899 g_return_val_if_fail (name
!= NULL
, NULL
);
901 tmp
= g_utf8_casefold (name
, -1);
902 sheet
= g_hash_table_lookup (wb
->sheet_hash_private
, tmp
);
909 * Find a sheet to focus on, left or right of sheet_index.
912 workbook_focus_other_sheet (Workbook
*wb
, Sheet
*sheet
)
916 int sheet_index
= sheet
->index_in_wb
;
918 for (i
= sheet_index
; !focus
&& --i
>= 0; ) {
919 Sheet
*this_sheet
= g_ptr_array_index (wb
->sheets
, i
);
920 if (this_sheet
->visibility
== GNM_SHEET_VISIBILITY_VISIBLE
)
924 for (i
= sheet_index
; !focus
&& ++i
< (int)wb
->sheets
->len
; ) {
925 Sheet
*this_sheet
= g_ptr_array_index (wb
->sheets
, i
);
926 if (this_sheet
->visibility
== GNM_SHEET_VISIBILITY_VISIBLE
)
930 WORKBOOK_FOREACH_VIEW (wb
, wbv
, {
931 if (sheet
== wb_view_cur_sheet (wbv
))
932 wb_view_sheet_focus (wbv
, focus
);
939 * workbook_sheet_remove_controls:
943 * Remove the visible SheetControls of a sheet and shut them down politely.
945 * Returns TRUE if there are any remaining sheets visible
948 workbook_sheet_remove_controls (Workbook
*wb
, Sheet
*sheet
)
952 g_return_val_if_fail (GNM_IS_WORKBOOK (wb
), TRUE
);
953 g_return_val_if_fail (IS_SHEET (sheet
), TRUE
);
954 g_return_val_if_fail (sheet
->workbook
== wb
, TRUE
);
955 g_return_val_if_fail (workbook_sheet_by_name (wb
, sheet
->name_unquoted
) == sheet
, TRUE
);
957 /* Finish any object editing */
958 SHEET_FOREACH_CONTROL (sheet
, view
, control
,
959 sc_mode_edit (control
););
961 /* If not exiting, adjust the focus for any views whose focus sheet
962 * was the one being deleted, and prepare to recalc */
963 if (!wb
->during_destruction
)
964 focus
= workbook_focus_other_sheet (wb
, sheet
);
966 WORKBOOK_FOREACH_CONTROL (wb
, wbv
, wbc
,
967 wb_control_sheet_remove (wbc
, sheet
););
969 return focus
!= NULL
;
973 * workbook_sheet_attach_at_pos:
978 * Add @new_sheet to @wb, placing it at @pos. This will add a ref to
982 workbook_sheet_attach_at_pos (Workbook
*wb
, Sheet
*new_sheet
, int pos
)
984 g_return_if_fail (GNM_IS_WORKBOOK (wb
));
985 g_return_if_fail (IS_SHEET (new_sheet
));
986 g_return_if_fail (new_sheet
->workbook
== wb
);
987 g_return_if_fail (pos
>= 0 && pos
<= (int)wb
->sheets
->len
);
989 pre_sheet_index_change (wb
);
991 g_object_ref (new_sheet
);
992 go_ptr_array_insert (wb
->sheets
, (gpointer
)new_sheet
, pos
);
993 workbook_sheet_index_update (wb
, pos
);
994 g_hash_table_insert (wb
->sheet_hash_private
,
995 new_sheet
->name_case_insensitive
,
998 WORKBOOK_FOREACH_VIEW (wb
, view
,
999 wb_view_sheet_add (view
, new_sheet
););
1001 /* Do not signal until after adding the views [#314208] */
1002 post_sheet_index_change (wb
);
1004 go_doc_set_dirty (GO_DOC (wb
), TRUE
);
1008 * workbook_sheet_attach:
1012 * Add @new_sheet to @wb, placing it at the end. SURPRISE: This assumes
1013 * a ref to the sheet.
1016 workbook_sheet_attach (Workbook
*wb
, Sheet
*new_sheet
)
1018 workbook_sheet_attach_at_pos (wb
, new_sheet
, wb
->sheets
->len
);
1019 /* Balance the ref added by the above call. */
1020 g_object_unref (new_sheet
);
1024 * workbook_sheet_add:
1026 * @pos: position to add, -1 meaning append.
1027 * @columns: the sheet columns number.
1028 * @rows: the sheet rows number.
1030 * Create and name a new sheet, putting it at position @pos. The sheet
1031 * returned is not ref'd. (The ref belongs to the workbook.)
1033 * Return value: (transfer none): the new sheet.
1036 workbook_sheet_add (Workbook
*wb
, int pos
, int columns
, int rows
)
1038 char *name
= workbook_sheet_get_free_name (wb
, _("Sheet"), TRUE
, FALSE
);
1039 Sheet
*new_sheet
= sheet_new (wb
, name
, columns
, rows
);
1043 pos
= wb
->sheets
->len
;
1044 workbook_sheet_attach_at_pos (wb
, new_sheet
, pos
);
1046 /* FIXME: Why here? */
1047 g_signal_emit (G_OBJECT (wb
), signals
[SHEET_ADDED
], 0);
1049 g_object_unref (new_sheet
);
1055 * workbook_sheet_add_with_type:
1057 * @sheet_type: the sheet type.
1058 * @pos: position to add, -1 meaning append.
1059 * @columns: the sheet columns number.
1060 * @rows: the sheet rows number.
1062 * Create and name a new sheet, putting it at position @pos. The sheet
1063 * returned is not ref'd. (The ref belongs to the workbook.)
1065 * Return value: (transfer none): the new sheet.
1068 workbook_sheet_add_with_type (Workbook
*wb
, GnmSheetType sheet_type
, int pos
, int columns
, int rows
)
1070 char *name
= workbook_sheet_get_free_name (wb
, (sheet_type
== GNM_SHEET_OBJECT
)? _("Graph"): _("Sheet"), TRUE
, FALSE
);
1071 Sheet
*new_sheet
= sheet_new_with_type (wb
, name
, sheet_type
, columns
, rows
);
1075 pos
= wb
->sheets
->len
;
1076 workbook_sheet_attach_at_pos (wb
, new_sheet
, pos
);
1078 /* FIXME: Why here? */
1079 g_signal_emit (G_OBJECT (wb
), signals
[SHEET_ADDED
], 0);
1081 g_object_unref (new_sheet
);
1087 * workbook_sheet_delete:
1088 * @sheet: the sheet that we want to delete from its workbook
1090 * This function detaches the given sheet from its parent workbook and
1091 * invalidates all references to the deleted sheet from other sheets and
1092 * clears all references in the clipboard to this sheet.
1095 workbook_sheet_delete (Sheet
*sheet
)
1100 g_return_if_fail (IS_SHEET (sheet
));
1101 g_return_if_fail (GNM_IS_WORKBOOK (sheet
->workbook
));
1103 wb
= sheet
->workbook
;
1104 sheet_index
= sheet
->index_in_wb
;
1106 if (gnm_debug_flag ("sheets"))
1107 g_printerr ("Removing sheet %s from %s\n",
1108 sheet
->name_unquoted
,
1109 go_doc_get_uri (GO_DOC (wb
)));
1111 gnm_app_clipboard_invalidate_sheet (sheet
);
1113 if (!wb
->during_destruction
) {
1114 workbook_focus_other_sheet (wb
, sheet
);
1115 /* During destruction this was already done. */
1116 dependents_invalidate_sheet (sheet
, FALSE
);
1117 workbook_sheet_remove_controls (wb
, sheet
);
1120 /* All is fine, remove the sheet */
1121 pre_sheet_index_change (wb
);
1122 g_ptr_array_remove_index (wb
->sheets
, sheet_index
);
1123 workbook_sheet_index_update (wb
, sheet_index
);
1124 sheet
->index_in_wb
= -1;
1125 g_hash_table_remove (wb
->sheet_hash_private
, sheet
->name_case_insensitive
);
1126 post_sheet_index_change (wb
);
1128 /* Clear the controls first, before we potentially update */
1129 SHEET_FOREACH_VIEW (sheet
, view
, sv_dispose (view
););
1131 g_signal_emit_by_name (G_OBJECT (sheet
), "detached_from_workbook", wb
);
1132 g_object_unref (sheet
);
1134 if (!wb
->during_destruction
)
1135 go_doc_set_dirty (GO_DOC (wb
), TRUE
);
1136 g_signal_emit (G_OBJECT (wb
), signals
[SHEET_DELETED
], 0);
1138 if (!wb
->during_destruction
)
1139 workbook_queue_all_recalc (wb
);
1143 * workbook_sheet_move:
1145 * Moves the sheet up or down @direction spots in the sheet list
1146 * If @direction is negative, move left. If positive, move right.
1149 workbook_sheet_move (Sheet
*sheet
, int direction
)
1152 gint old_pos
, new_pos
;
1154 g_return_if_fail (IS_SHEET (sheet
));
1156 wb
= sheet
->workbook
;
1158 pre_sheet_index_change (wb
);
1159 old_pos
= sheet
->index_in_wb
;
1160 new_pos
= old_pos
+ direction
;
1162 if (0 <= new_pos
&& new_pos
< workbook_sheet_count (wb
)) {
1163 int min_pos
= MIN (old_pos
, new_pos
);
1164 int max_pos
= MAX (old_pos
, new_pos
);
1166 g_ptr_array_remove_index (wb
->sheets
, old_pos
);
1167 go_ptr_array_insert (wb
->sheets
, sheet
, new_pos
);
1169 for (; max_pos
>= min_pos
; max_pos
--) {
1170 Sheet
*sheet
= g_ptr_array_index (wb
->sheets
, max_pos
);
1171 sheet
->index_in_wb
= max_pos
;
1175 post_sheet_index_change (wb
);
1177 go_doc_set_dirty (GO_DOC (wb
), TRUE
);
1181 * workbook_sheet_get_free_name:
1182 * @wb: workbook to look for
1183 * @base: base for the name, e. g. "Sheet"
1184 * @always_suffix: if true, add suffix even if the name "base" is not in use.
1185 * @handle_counter: strip counter if necessary
1187 * Gets a new unquoted name for a sheets such that it does not exist on the
1190 * Returns the name assigned to the sheet.
1193 workbook_sheet_get_free_name (Workbook
*wb
,
1195 gboolean always_suffix
,
1196 gboolean handle_counter
)
1198 char const *name_format
;
1199 char *name
, *base_name
;
1203 g_return_val_if_fail (wb
!= NULL
, NULL
);
1205 if (!always_suffix
&& (workbook_sheet_by_name (wb
, base
) == NULL
))
1206 return g_strdup (base
); /* Name not in use */
1208 base_name
= g_strdup (base
);
1209 if (handle_counter
) {
1210 workbook_sheet_name_strip_number (base_name
, &i
);
1211 name_format
= "%s(%u)";
1213 name_format
= "%s%u";
1215 limit
= workbook_sheet_count (wb
) + 2;
1216 name
= g_malloc (strlen (base_name
) + strlen (name_format
) + 10);
1217 while (limit
-- > 0) {
1219 sprintf (name
, name_format
, base_name
, i
);
1220 if (workbook_sheet_by_name (wb
, name
) == NULL
) {
1226 /* We should not get here. */
1227 g_warning ("There is trouble at the mill.");
1231 name
= g_strdup_printf ("%s (%i)", base
, 2);
1236 * workbook_sheet_rename:
1237 * @wb: workbook to look for
1238 * @sheet_indices: (element-type void): list of sheet indices (ignore -1)
1239 * @new_names: (element-type char): list of new names
1241 * Adjusts the names of the sheets. We assume that everything is
1242 * valid. If in doubt call workbook_sheet_reorder_check first.
1244 * Returns: FALSE when it was successful
1247 workbook_sheet_rename (Workbook
*wb
,
1248 GSList
*sheet_indices
,
1250 G_GNUC_UNUSED GOCmdContext
*cc
)
1252 GSList
*sheet_index
= sheet_indices
;
1253 GSList
*new_name
= new_names
;
1255 while (new_name
&& sheet_index
) {
1256 if (-1 != GPOINTER_TO_INT (sheet_index
->data
)) {
1257 g_hash_table_remove (wb
->sheet_hash_private
,
1260 sheet_index
= sheet_index
->next
;
1261 new_name
= new_name
->next
;
1264 sheet_index
= sheet_indices
;
1265 new_name
= new_names
;
1266 while (new_name
&& sheet_index
) {
1267 if (-1 != GPOINTER_TO_INT (sheet_index
->data
)) {
1268 Sheet
*sheet
= workbook_sheet_by_index
1269 (wb
, GPOINTER_TO_INT (sheet_index
->data
));
1270 g_object_set (sheet
, "name", new_name
->data
, NULL
);
1272 sheet_index
= sheet_index
->next
;
1273 new_name
= new_name
->next
;
1280 * workbook_find_command:
1282 * @is_undo: undo vs redo
1285 * Returns: the 1 based index of the @key command, or 0 if it is not found
1288 workbook_find_command (Workbook
*wb
, gboolean is_undo
, gpointer cmd
)
1293 g_return_val_if_fail (GNM_IS_WORKBOOK (wb
), 0);
1295 ptr
= is_undo
? wb
->undo_commands
: wb
->redo_commands
;
1296 for ( ; ptr
!= NULL
; ptr
= ptr
->next
, n
++)
1297 if (ptr
->data
== cmd
)
1299 g_warning ("%s command : %p not found", is_undo
? "undo" : "redo", cmd
);
1304 * workbook_sheet_reorder:
1305 * @wb: workbook to look for
1306 * @new_order: (element-type Sheet): list of sheets
1308 * Adjusts the order of the sheets.
1310 * Returns FALSE when it was successful
1313 workbook_sheet_reorder (Workbook
*wb
, GSList
*new_order
)
1319 g_return_val_if_fail (GNM_IS_WORKBOOK (wb
), FALSE
);
1320 g_return_val_if_fail (g_slist_length (new_order
) == wb
->sheets
->len
, FALSE
);
1322 pre_sheet_index_change (wb
);
1324 for (ptr
= new_order
; NULL
!= ptr
; ptr
= ptr
->next
, pos
++) {
1325 g_ptr_array_index (wb
->sheets
, pos
) = sheet
= ptr
->data
;
1326 sheet
->index_in_wb
= pos
;
1329 post_sheet_index_change (wb
);
1335 * workbook_date_conv:
1338 * Returns: the date convention in effect for the workbook.
1340 GODateConventions
const *
1341 workbook_date_conv (Workbook
const *wb
)
1343 g_return_val_if_fail (wb
!= NULL
, NULL
);
1344 return wb
->date_conv
;
1348 * workbook_set_date_conv:
1350 * @date_conv: new date convention
1352 * Sets the date convention @date_conv.
1353 * NOTE : THIS IS NOT A SMART ROUTINE. If you want to actually change this
1354 * We'll need to recalc and rerender everything. That will need to be done
1358 workbook_set_date_conv (Workbook
*wb
, GODateConventions
const *date_conv
)
1360 g_return_if_fail (GNM_IS_WORKBOOK (wb
));
1361 g_return_if_fail (date_conv
!= NULL
);
1363 wb
->date_conv
= date_conv
;
1367 workbook_set_1904 (Workbook
*wb
, gboolean base1904
)
1369 GODateConventions
const *date_conv
=
1370 go_date_conv_from_str (base1904
? "Apple:1904" : "Lotus:1900");
1371 workbook_set_date_conv (wb
, date_conv
);
1375 * workbook_get_sheet_size:
1378 * Returns: (transfer none): the current sheet size for @wb.
1380 GnmSheetSize
const *
1381 workbook_get_sheet_size (Workbook
const *wb
)
1383 if (wb
== NULL
|| workbook_sheet_count (wb
) == 0) {
1384 static const GnmSheetSize max_size
= {
1385 GNM_MAX_COLS
, GNM_MAX_ROWS
1391 return gnm_sheet_get_size (workbook_sheet_by_index (wb
, 0));
1394 /* ------------------------------------------------------------------------- */
1399 } WorkbookSheetStateSheet
;
1401 struct _WorkbookSheetState
{
1404 WorkbookSheetStateSheet
*sheets
;
1409 WorkbookSheetState
*
1410 workbook_sheet_state_new (const Workbook
*wb
)
1413 WorkbookSheetState
*wss
= g_new (WorkbookSheetState
, 1);
1415 wss
->properties
= go_object_properties_collect (G_OBJECT (wb
));
1416 wss
->n_sheets
= workbook_sheet_count (wb
);
1417 wss
->sheets
= g_new (WorkbookSheetStateSheet
, wss
->n_sheets
);
1418 for (i
= 0; i
< wss
->n_sheets
; i
++) {
1419 WorkbookSheetStateSheet
*wsss
= wss
->sheets
+ i
;
1420 wsss
->sheet
= g_object_ref (workbook_sheet_by_index (wb
, i
));
1421 wsss
->properties
= go_object_properties_collect (G_OBJECT (wsss
->sheet
));
1428 workbook_sheet_state_free (WorkbookSheetState
*wss
)
1432 if (!wss
|| wss
->ref_count
-- > 1)
1435 go_object_properties_free (wss
->properties
);
1437 for (i
= 0; i
< wss
->n_sheets
; i
++) {
1438 WorkbookSheetStateSheet
*wsss
= wss
->sheets
+ i
;
1439 g_object_unref (wsss
->sheet
);
1440 go_object_properties_free (wsss
->properties
);
1442 g_free (wss
->sheets
);
1446 static WorkbookSheetState
*
1447 workbook_sheet_state_ref (WorkbookSheetState
*wss
)
1454 workbook_sheet_state_get_type (void)
1459 t
= g_boxed_type_register_static ("WorkbookSheetState",
1460 (GBoxedCopyFunc
)workbook_sheet_state_ref
,
1461 (GBoxedFreeFunc
)workbook_sheet_state_free
);
1467 workbook_sheet_state_restore (Workbook
*wb
, const WorkbookSheetState
*wss
)
1471 /* Get rid of sheets that shouldn't be there. */
1472 for (i
= workbook_sheet_count (wb
) ; i
-- > 0; ) {
1473 Sheet
*sheet
= workbook_sheet_by_index (wb
, i
);
1475 for (j
= 0; j
< wss
->n_sheets
; j
++)
1476 if (sheet
== wss
->sheets
[j
].sheet
)
1478 if (j
== wss
->n_sheets
)
1479 workbook_sheet_delete (sheet
);
1482 /* Attach new sheets and handle order. */
1483 for (i
= 0; i
< wss
->n_sheets
; i
++) {
1484 Sheet
*sheet
= wss
->sheets
[i
].sheet
;
1485 if (sheet
->index_in_wb
!= i
) {
1486 if (sheet
->index_in_wb
== -1) {
1487 workbook_sheet_attach_at_pos (wb
, sheet
, i
);
1488 dependents_revive_sheet (sheet
);
1491 * There might be a smarter way of getting more
1492 * sheets into place faster. This will at
1495 workbook_sheet_move (sheet
, i
- sheet
->index_in_wb
);
1498 go_object_properties_apply (G_OBJECT (sheet
),
1499 wss
->sheets
[i
].properties
,
1503 go_object_properties_apply (G_OBJECT (wb
), wss
->properties
, TRUE
);
1507 workbook_sheet_state_size (const WorkbookSheetState
*wss
)
1509 int size
= 1 + g_slist_length (wss
->properties
);
1511 for (i
= 0; i
< wss
->n_sheets
; i
++) {
1512 WorkbookSheetStateSheet
*wsss
= wss
->sheets
+ i
;
1513 size
+= 5; /* For ->sheet. */
1514 size
+= g_slist_length (wsss
->properties
);
1519 GNM_BEGIN_KILL_SWITCH_WARNING
1521 workbook_sheet_state_diff (const WorkbookSheetState
*wss_a
, const WorkbookSheetState
*wss_b
)
1524 WSS_SHEET_RENAMED
= 1,
1525 WSS_SHEET_ADDED
= 2,
1526 WSS_SHEET_TAB_COLOR
= 4,
1527 WSS_SHEET_PROPERTIES
= 8,
1528 WSS_SHEET_DELETED
= 16,
1529 WSS_SHEET_ORDER
= 32,
1530 WSS_FUNNY
= 0x40000000
1534 int n_added
, n_deleted
= 0;
1536 for (ia
= 0; ia
< wss_a
->n_sheets
; ia
++) {
1537 Sheet
*sheet
= wss_a
->sheets
[ia
].sheet
;
1542 for (ib
= 0; ib
< wss_b
->n_sheets
; ib
++)
1543 if (sheet
== wss_b
->sheets
[ib
].sheet
)
1545 if (ib
== wss_b
->n_sheets
) {
1546 what
|= WSS_SHEET_DELETED
;
1553 what
|= WSS_SHEET_ORDER
;
1554 /* We do not count reordered sheet. */
1557 pa
= wss_a
->sheets
[ia
].properties
;
1558 pb
= wss_b
->sheets
[ib
].properties
;
1559 for (; pa
&& pb
; pa
= pa
->next
->next
, pb
= pb
->next
->next
) {
1560 GParamSpec
*pspec
= pa
->data
;
1561 const GValue
*va
= pa
->next
->data
;
1562 const GValue
*vb
= pb
->next
->data
;
1563 if (pspec
!= pb
->data
)
1566 if (g_param_values_cmp (pspec
, va
, vb
) == 0)
1570 if (strcmp (pspec
->name
, "name") == 0)
1571 what
|= WSS_SHEET_RENAMED
;
1572 else if (strcmp (pspec
->name
, "tab-foreground") == 0)
1573 what
|= WSS_SHEET_TAB_COLOR
;
1574 else if (strcmp (pspec
->name
, "tab-background") == 0)
1575 what
|= WSS_SHEET_TAB_COLOR
;
1577 what
|= WSS_SHEET_PROPERTIES
;
1585 n_added
= wss_b
->n_sheets
- (wss_a
->n_sheets
- n_deleted
);
1587 what
|= WSS_SHEET_ADDED
;
1592 case WSS_SHEET_RENAMED
:
1593 return g_strdup_printf (ngettext ("Renaming sheet", "Renaming %d sheets", n
), n
);
1594 case WSS_SHEET_ADDED
:
1595 return g_strdup_printf (ngettext ("Adding sheet", "Adding %d sheets", n
), n
);
1596 case WSS_SHEET_ADDED
| WSS_SHEET_ORDER
:
1598 * This is most likely just a sheet inserted, but it just
1599 * might be a compound operation. Lie.
1601 return g_strdup_printf (ngettext ("Inserting sheet", "Inserting %d sheets", n
), n
);
1602 case WSS_SHEET_TAB_COLOR
:
1603 return g_strdup (_("Changing sheet tab colors"));
1604 case WSS_SHEET_PROPERTIES
:
1605 return g_strdup (_("Changing sheet properties"));
1606 case WSS_SHEET_DELETED
:
1607 case WSS_SHEET_DELETED
| WSS_SHEET_ORDER
:
1609 * This is most likely just a sheet delete, but it just
1610 * might be a compound operation. Lie.
1612 return g_strdup_printf (ngettext ("Deleting sheet", "Deleting %d sheets", n
), n
);
1613 case WSS_SHEET_ORDER
:
1614 return g_strdup (_("Changing sheet order"));
1616 return g_strdup (_("Reorganizing Sheets"));
1619 GNM_END_KILL_SWITCH_WARNING
1621 /* ------------------------------------------------------------------------- */
1623 GSF_CLASS (Workbook
, workbook
,
1624 workbook_class_init
, workbook_init
,