Update Spanish translation
[gnumeric.git] / src / sheet-utils.c
blob13e626bf28293180c389b98f04b170f43265431e
2 /*
3 * sheet-utils.c: Utility routines for Sheet content
5 * Copyright (C) 2002-2008 Jody Goldberg (jody@gnome.org)
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License as
9 * published by the Free Software Foundation; either version 2 of the
10 * License, or (at your option) version 3.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
20 * USA
22 #include <gnumeric-config.h>
23 #include <libgnumeric.h>
24 #include <sheet-utils.h>
25 #include <sheet.h>
27 static gboolean
28 sheet_cell_or_one_below_is_not_empty (Sheet *sheet, int col, int row)
30 return !sheet_is_cell_empty (sheet, col, row) ||
31 (row < gnm_sheet_get_last_row (sheet) &&
32 !sheet_is_cell_empty (sheet, col, row+1));
35 /**
36 * gnm_sheet_guess_region:
37 * @sheet: #Sheet
38 * @region: #GnmRange
40 * Makes a guess at the logical containing @region and returns the possibly
41 * expanded result in @region.
42 **/
43 void
44 gnm_sheet_guess_region (Sheet *sheet, GnmRange *region)
46 int col;
47 int end_row;
48 int offset;
50 /* check in case only one cell selected */
51 if (region->start.col == region->end.col) {
52 int start = region->start.col;
53 /* look for previous empty column */
54 for (col = start - 1; col > 0; col--)
55 if (!sheet_cell_or_one_below_is_not_empty (sheet, col, region->start.row))
56 break;
57 region->start.col = col + 1;
59 /* look for next empty column */
60 for (col = start + 1; col < gnm_sheet_get_max_cols (sheet); col++)
61 if (!sheet_cell_or_one_below_is_not_empty (sheet, col, region->start.row))
62 break;
63 region->end.col = col - 1;
66 /* find first and last non-empty cells in region */
67 for (col = region->start.col; col <= region->end.col; col++)
68 if (sheet_cell_or_one_below_is_not_empty (sheet, col, region->start.row))
69 break;
71 if (col > region->end.col)
72 return; /* all empty -- give up */
73 region->start.col = col;
75 for (col = region->end.col; col >= region->start.col; col--)
76 if (sheet_cell_or_one_below_is_not_empty(sheet, col, region->start.row))
77 break;
78 region->end.col = col;
80 /* now find length of longest column */
81 for (col = region->start.col; col <= region->end.col; col++) {
82 offset = 0;
83 if (sheet_is_cell_empty(sheet, col, region->start.row))
84 offset = 1;
85 end_row = sheet_find_boundary_vertical (sheet, col,
86 region->start.row + offset, col, 1, TRUE);
87 if (end_row > region->end.row)
88 region->end.row = end_row;
93 /**
94 * gnm_sheet_guess_data_range:
95 * @sheet: #Sheet
96 * @region: #GnmRange
98 * Makes a guess at the logical range containing @region and returns the possibly
99 * expanded result in @region. The range is also expanded upwards.
101 void
102 gnm_sheet_guess_data_range (Sheet *sheet, GnmRange *region)
104 int col;
105 int row;
106 int start = region->start.col;
108 /* look for previous empty column */
109 for (col = start - 1; col >= 0; col--)
110 if (!sheet_cell_or_one_below_is_not_empty (sheet, col, region->start.row))
111 break;
112 region->start.col = col + 1;
114 /* look for next empty column */
115 start = region->end.col;
116 for (col = start + 1; col < gnm_sheet_get_max_cols (sheet); col++)
117 if (!sheet_cell_or_one_below_is_not_empty (sheet, col, region->start.row))
118 break;
119 region->end.col = col - 1;
121 for (col = region->start.col; col <= region->end.col; col++) {
122 gboolean empties = FALSE;
123 for (row = region->start.row - 2; row >= 0; row--)
124 if (!sheet_cell_or_one_below_is_not_empty (sheet, col, row)) {
125 empties = TRUE;
126 break;
128 region->start.row = empties ? row + 2 : 0;
129 for (row = region->end.row + 1; row < gnm_sheet_get_max_rows (sheet); row++)
130 if (!sheet_cell_or_one_below_is_not_empty (sheet, col, row))
131 break;
132 region->end.row = row - 1;
134 return;