Compilation: clean up dialog including.
[gnumeric.git] / src / workbook-cmd-format.c
blobe7f09625de94628c44b0ecdd7ef7629998f48df5
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 /*
3 * workbook.c: Workbook format commands hooked to the menus
5 * Author:
6 * Miguel de Icaza (miguel@gnu.org)
7 * Jody Goldberg (jody@gnome.org)
8 */
9 #include <gnumeric-config.h>
10 #include <glib/gi18n-lib.h>
11 #include "gnumeric.h"
12 #include "workbook-cmd-format.h"
14 #include "cell.h"
15 #include "dependent.h"
16 #include "expr.h"
17 #include "func.h"
18 #include "ranges.h"
19 #include "gui-util.h"
20 #include "selection.h"
21 #include "sheet-merge.h"
22 #include "sheet-view.h"
23 #include "value.h"
24 #include "workbook-control.h"
25 #include "workbook-view.h"
26 #include "workbook.h"
27 #include "application.h"
28 #include "dialogs/dialogs.h"
29 #include "sheet.h"
30 #include "commands.h"
31 #include "style-border.h"
32 #include "style-color.h"
34 struct closure_colrow_resize {
35 gboolean is_cols;
36 ColRowIndexList *selection;
39 static gboolean
40 cb_colrow_collect (G_GNUC_UNUSED SheetView *sv, GnmRange const *r, gpointer user_data)
42 struct closure_colrow_resize *info = user_data;
43 int first, last;
45 if (info->is_cols) {
46 first = r->start.col;
47 last = r->end.col;
48 } else {
49 first = r->start.row;
50 last = r->end.row;
53 info->selection = colrow_get_index_list (first, last, info->selection);
54 return TRUE;
57 void
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);
69 void
70 workbook_cmd_autofit_selection (WorkbookControl *wbc, Sheet *sheet,
71 gboolean is_cols)
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);
81 void
82 workbook_cmd_inc_indent (WorkbookControl *wbc)
84 WorkbookView const *wbv = wb_control_view (wbc);
85 int i;
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);
91 if (i < 20) {
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"));
101 void
102 workbook_cmd_dec_indent (WorkbookControl *wbc)
104 WorkbookView const *wbv = wb_control_view (wbc);
105 int i;
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);
111 if (i > 0) {
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 {
119 GnmExprList *args;
120 GnmRange const *r;
121 Workbook *wb;
124 static GnmValue *
125 cb_get_cell_content (GnmCellIter const *iter, struct workbook_cmd_wrap_sort_t *cl)
127 GnmExpr const *expr;
129 if (iter->cell == NULL)
130 expr = gnm_expr_new_constant (value_new_empty ());
131 else if (gnm_cell_has_expr (iter->cell)) {
132 char *text;
133 GnmParsePos pp;
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,
139 &iter->pp, NULL);
140 texpr = gnm_expr_parse_str (text, &pp, GNM_EXPR_PARSE_DEFAULT,
141 NULL, NULL);
142 g_free (text);
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));
148 else
149 expr = gnm_expr_new_constant (value_new_empty ());
151 cl->args = gnm_expr_list_prepend (cl->args, expr);
152 return NULL;
155 void
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;
161 GnmExpr const *expr;
162 GnmFunc *fd_sort;
163 GnmFunc *fd_array;
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."));
175 return;
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."));
182 return;
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."));
188 return;
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."));
196 return;
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);