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
22 #include <gnumeric-config.h>
23 #include <libgnumeric.h>
24 #include <sheet-utils.h>
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));
36 * gnm_sheet_guess_region:
40 * Makes a guess at the logical containing @region and returns the possibly
41 * expanded result in @region.
44 gnm_sheet_guess_region (Sheet
*sheet
, GnmRange
*region
)
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
))
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
))
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
))
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
))
78 region
->end
.col
= col
;
80 /* now find length of longest column */
81 for (col
= region
->start
.col
; col
<= region
->end
.col
; col
++) {
83 if (sheet_is_cell_empty(sheet
, col
, region
->start
.row
))
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
;
94 * gnm_sheet_guess_data_range:
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.
102 gnm_sheet_guess_data_range (Sheet
*sheet
, GnmRange
*region
)
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
))
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
))
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
)) {
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
))
132 region
->end
.row
= row
- 1;