1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * workbook.c: Workbook format commands hooked to the menus
6 * Miguel de Icaza (miguel@gnu.org)
7 * Jody Goldberg (jody@gnome.org)
9 #include <gnumeric-config.h>
10 #include <glib/gi18n-lib.h>
12 #include "workbook-cmd-format.h"
15 #include "dependent.h"
20 #include "selection.h"
21 #include "sheet-merge.h"
22 #include "sheet-view.h"
24 #include "workbook-control.h"
25 #include "workbook-view.h"
27 #include "application.h"
31 #include "style-border.h"
32 #include "style-color.h"
34 struct closure_colrow_resize
{
36 ColRowIndexList
*selection
;
40 cb_colrow_collect (G_GNUC_UNUSED SheetView
*sv
, GnmRange
const *r
, gpointer user_data
)
42 struct closure_colrow_resize
*info
= user_data
;
53 info
->selection
= colrow_get_index_list (first
, last
, info
->selection
);
58 workbook_cmd_resize_selected_colrow (WorkbookControl
*wbc
, Sheet
*sheet
,
59 gboolean is_cols
, int new_size_pixels
)
61 struct closure_colrow_resize closure
;
62 closure
.is_cols
= is_cols
;
63 closure
.selection
= NULL
;
64 sv_selection_foreach (sheet_get_view (sheet
, wb_control_view (wbc
)),
65 &cb_colrow_collect
, &closure
);
66 cmd_resize_colrow (wbc
, sheet
, is_cols
, closure
.selection
, new_size_pixels
);
70 workbook_cmd_autofit_selection (WorkbookControl
*wbc
, Sheet
*sheet
,
73 SheetView
*sv
= sheet_get_view (sheet
, wb_control_view (wbc
));
74 struct closure_colrow_resize closure
;
75 closure
.is_cols
= is_cols
;
76 closure
.selection
= NULL
;
77 sv_selection_foreach (sv
, &cb_colrow_collect
, &closure
);
78 cmd_autofit_selection (wbc
, sv
, sheet
, is_cols
, closure
.selection
);
82 workbook_cmd_inc_indent (WorkbookControl
*wbc
)
84 WorkbookView
const *wbv
= wb_control_view (wbc
);
87 g_return_if_fail (wbv
!= NULL
);
88 g_return_if_fail (wbv
->current_style
!= NULL
);
90 i
= gnm_style_get_indent (wbv
->current_style
);
92 GnmStyle
*style
= gnm_style_new ();
94 if (GNM_HALIGN_LEFT
!= gnm_style_get_align_h (wbv
->current_style
))
95 gnm_style_set_align_h (style
, GNM_HALIGN_LEFT
);
96 gnm_style_set_indent (style
, i
+1);
97 cmd_selection_format (wbc
, style
, NULL
, _("Increase Indent"));
102 workbook_cmd_dec_indent (WorkbookControl
*wbc
)
104 WorkbookView
const *wbv
= wb_control_view (wbc
);
107 g_return_if_fail (wbv
!= NULL
);
108 g_return_if_fail (wbv
->current_style
!= NULL
);
110 i
= gnm_style_get_indent (wbv
->current_style
);
112 GnmStyle
*style
= gnm_style_new ();
113 gnm_style_set_indent (style
, i
-1);
114 cmd_selection_format (wbc
, style
, NULL
, _("Decrease Indent"));
118 struct workbook_cmd_wrap_sort_t
{
125 cb_get_cell_content (GnmCellIter
const *iter
, struct workbook_cmd_wrap_sort_t
*cl
)
129 if (iter
->cell
== NULL
)
130 expr
= gnm_expr_new_constant (value_new_empty ());
131 else if (gnm_cell_has_expr (iter
->cell
)) {
134 GnmExprTop
const *texpr
;
136 parse_pos_init (&pp
, cl
->wb
, iter
->pp
.sheet
,
137 cl
->r
->start
.col
, cl
->r
->start
.row
);
138 text
= gnm_expr_as_string ((iter
->cell
)->base
.texpr
->expr
,
140 texpr
= gnm_expr_parse_str (text
, &pp
, GNM_EXPR_PARSE_DEFAULT
,
143 expr
= gnm_expr_copy (texpr
->expr
);
144 gnm_expr_top_unref (texpr
);
146 } else if (iter
->cell
->value
!= NULL
)
147 expr
= gnm_expr_new_constant (value_dup (iter
->cell
->value
));
149 expr
= gnm_expr_new_constant (value_new_empty ());
151 cl
->args
= gnm_expr_list_prepend (cl
->args
, expr
);
156 workbook_cmd_wrap_sort (WorkbookControl
*wbc
, int type
)
158 WorkbookView
const *wbv
= wb_control_view (wbc
);
159 SheetView
*sv
= wb_view_cur_sheet_view (wbv
);
160 GSList
*l
= sv
->selections
, *merges
;
164 GnmExprTop
const *texpr
;
165 struct workbook_cmd_wrap_sort_t cl
= {NULL
, NULL
, NULL
};
167 cl
.r
= selection_first_range
168 (sv
, GO_CMD_CONTEXT (wbc
), _("Wrap SORT"));
169 cl
.wb
= wb_control_get_workbook (wbc
);
171 if (g_slist_length (l
) > 1) {
172 go_cmd_context_error_invalid (GO_CMD_CONTEXT (wbc
), _("Wrap SORT"),
173 _("A single selection is required."));
177 if (range_height (cl
.r
) > 1 && range_width (cl
.r
) > 1) {
178 go_cmd_context_error_invalid
179 (GO_CMD_CONTEXT (wbc
), _("Wrap SORT"),
180 _("An n\342\250\2571 or 1\342\250\257n "
181 "selection is required."));
184 if (range_height (cl
.r
) == 1 && range_width (cl
.r
) == 1) {
185 go_cmd_context_error_invalid
186 (GO_CMD_CONTEXT (wbc
), _("Wrap SORT"),
187 _("There is no point in sorting a single cell."));
190 merges
= gnm_sheet_merge_get_overlap (sv
->sheet
, cl
.r
);
191 if (merges
!= NULL
) {
192 g_slist_free (merges
);
193 go_cmd_context_error_invalid
194 (GO_CMD_CONTEXT (wbc
), _("Wrap SORT"),
195 _("The range to be sorted may not contain any merged cells."));
198 fd_sort
= gnm_func_lookup_or_add_placeholder ("sort");
199 fd_array
= gnm_func_lookup_or_add_placeholder ("array");
201 sheet_foreach_cell_in_range
202 (sv
->sheet
, CELL_ITER_ALL
, cl
.r
,
203 (CellIterFunc
)&cb_get_cell_content
, &cl
);
205 cl
.args
= g_slist_reverse (cl
.args
);
206 expr
= gnm_expr_new_funcall (fd_array
, cl
.args
);
207 expr
= gnm_expr_new_funcall2
208 (fd_sort
, expr
, gnm_expr_new_constant (value_new_int (type
)));
209 texpr
= gnm_expr_top_new (expr
);
210 cmd_area_set_array_expr (wbc
, sv
, texpr
);
211 gnm_expr_top_unref (texpr
);