2 * workbook.c: Workbook format commands hooked to the menus
5 * Miguel de Icaza (miguel@gnu.org)
6 * Jody Goldberg (jody@gnome.org)
8 #include <gnumeric-config.h>
9 #include <glib/gi18n-lib.h>
11 #include <workbook-cmd-format.h>
14 #include <dependent.h>
19 #include <selection.h>
20 #include <sheet-merge.h>
21 #include <sheet-view.h>
23 #include <workbook-control.h>
24 #include <workbook-view.h>
26 #include <application.h>
27 #include <dialogs/dialogs.h>
30 #include <style-border.h>
31 #include <style-color.h>
33 struct closure_colrow_resize
{
35 ColRowIndexList
*selection
;
39 cb_colrow_collect (G_GNUC_UNUSED SheetView
*sv
, GnmRange
const *r
, gpointer user_data
)
41 struct closure_colrow_resize
*info
= user_data
;
52 info
->selection
= colrow_get_index_list (first
, last
, info
->selection
);
57 workbook_cmd_resize_selected_colrow (WorkbookControl
*wbc
, Sheet
*sheet
,
58 gboolean is_cols
, int new_size_pixels
)
60 struct closure_colrow_resize closure
;
61 closure
.is_cols
= is_cols
;
62 closure
.selection
= NULL
;
63 sv_selection_foreach (sheet_get_view (sheet
, wb_control_view (wbc
)),
64 &cb_colrow_collect
, &closure
);
65 cmd_resize_colrow (wbc
, sheet
, is_cols
, closure
.selection
, new_size_pixels
);
69 workbook_cmd_autofit_selection (WorkbookControl
*wbc
, Sheet
*sheet
,
72 SheetView
*sv
= sheet_get_view (sheet
, wb_control_view (wbc
));
73 struct closure_colrow_resize closure
;
74 closure
.is_cols
= is_cols
;
75 closure
.selection
= NULL
;
76 sv_selection_foreach (sv
, &cb_colrow_collect
, &closure
);
77 cmd_autofit_selection (wbc
, sv
, sheet
, is_cols
, closure
.selection
);
81 workbook_cmd_inc_indent (WorkbookControl
*wbc
)
83 WorkbookView
const *wbv
= wb_control_view (wbc
);
86 g_return_if_fail (wbv
!= NULL
);
87 g_return_if_fail (wbv
->current_style
!= NULL
);
89 i
= gnm_style_get_indent (wbv
->current_style
);
91 GnmStyle
*style
= gnm_style_new ();
93 if (GNM_HALIGN_LEFT
!= gnm_style_get_align_h (wbv
->current_style
))
94 gnm_style_set_align_h (style
, GNM_HALIGN_LEFT
);
95 gnm_style_set_indent (style
, i
+1);
96 cmd_selection_format (wbc
, style
, NULL
, _("Increase Indent"));
101 workbook_cmd_dec_indent (WorkbookControl
*wbc
)
103 WorkbookView
const *wbv
= wb_control_view (wbc
);
106 g_return_if_fail (wbv
!= NULL
);
107 g_return_if_fail (wbv
->current_style
!= NULL
);
109 i
= gnm_style_get_indent (wbv
->current_style
);
111 GnmStyle
*style
= gnm_style_new ();
112 gnm_style_set_indent (style
, i
-1);
113 cmd_selection_format (wbc
, style
, NULL
, _("Decrease Indent"));
117 struct workbook_cmd_wrap_sort_t
{
124 cb_get_cell_content (GnmCellIter
const *iter
, struct workbook_cmd_wrap_sort_t
*cl
)
128 if (iter
->cell
== NULL
)
129 expr
= gnm_expr_new_constant (value_new_empty ());
130 else if (gnm_cell_has_expr (iter
->cell
)) {
133 GnmExprTop
const *texpr
;
135 parse_pos_init (&pp
, cl
->wb
, iter
->pp
.sheet
,
136 cl
->r
->start
.col
, cl
->r
->start
.row
);
137 text
= gnm_expr_as_string ((iter
->cell
)->base
.texpr
->expr
,
139 texpr
= gnm_expr_parse_str (text
, &pp
, GNM_EXPR_PARSE_DEFAULT
,
142 expr
= gnm_expr_copy (texpr
->expr
);
143 gnm_expr_top_unref (texpr
);
145 } else if (iter
->cell
->value
!= NULL
)
146 expr
= gnm_expr_new_constant (value_dup (iter
->cell
->value
));
148 expr
= gnm_expr_new_constant (value_new_empty ());
150 cl
->args
= gnm_expr_list_prepend (cl
->args
, expr
);
155 workbook_cmd_wrap_sort (WorkbookControl
*wbc
, int type
)
157 WorkbookView
const *wbv
= wb_control_view (wbc
);
158 SheetView
*sv
= wb_view_cur_sheet_view (wbv
);
159 GSList
*l
= sv
->selections
, *merges
;
163 GnmExprTop
const *texpr
;
164 struct workbook_cmd_wrap_sort_t cl
= {NULL
, NULL
, NULL
};
166 cl
.r
= selection_first_range
167 (sv
, GO_CMD_CONTEXT (wbc
), _("Wrap SORT"));
168 cl
.wb
= wb_control_get_workbook (wbc
);
170 if (g_slist_length (l
) > 1) {
171 go_cmd_context_error_invalid (GO_CMD_CONTEXT (wbc
), _("Wrap SORT"),
172 _("A single selection is required."));
176 if (range_height (cl
.r
) > 1 && range_width (cl
.r
) > 1) {
177 go_cmd_context_error_invalid
178 (GO_CMD_CONTEXT (wbc
), _("Wrap SORT"),
179 _("An n\342\250\2571 or 1\342\250\257n "
180 "selection is required."));
183 if (range_height (cl
.r
) == 1 && range_width (cl
.r
) == 1) {
184 go_cmd_context_error_invalid
185 (GO_CMD_CONTEXT (wbc
), _("Wrap SORT"),
186 _("There is no point in sorting a single cell."));
189 merges
= gnm_sheet_merge_get_overlap (sv
->sheet
, cl
.r
);
190 if (merges
!= NULL
) {
191 g_slist_free (merges
);
192 go_cmd_context_error_invalid
193 (GO_CMD_CONTEXT (wbc
), _("Wrap SORT"),
194 _("The range to be sorted may not contain any merged cells."));
197 fd_sort
= gnm_func_lookup_or_add_placeholder ("sort");
198 fd_array
= gnm_func_lookup_or_add_placeholder ("array");
200 sheet_foreach_cell_in_range
201 (sv
->sheet
, CELL_ITER_ALL
, cl
.r
,
202 (CellIterFunc
)&cb_get_cell_content
, &cl
);
204 cl
.args
= g_slist_reverse (cl
.args
);
205 expr
= gnm_expr_new_funcall (fd_array
, cl
.args
);
206 expr
= gnm_expr_new_funcall2
207 (fd_sort
, expr
, gnm_expr_new_constant (value_new_int (type
)));
208 texpr
= gnm_expr_top_new (expr
);
209 cmd_area_set_array_expr (wbc
, sv
, texpr
);
210 gnm_expr_top_unref (texpr
);