1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
4 * sheet-utils.c: Utility routines for Sheet content
6 * Copyright (C) 2002-2008 Jody Goldberg (jody@gnome.org)
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) version 3.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 #include <gnumeric-config.h>
24 #include "libgnumeric.h"
25 #include "sheet-utils.h"
29 sheet_cell_or_one_below_is_not_empty (Sheet
*sheet
, int col
, int row
)
31 return !sheet_is_cell_empty (sheet
, col
, row
) ||
32 (row
< gnm_sheet_get_last_row (sheet
) &&
33 !sheet_is_cell_empty (sheet
, col
, row
+1));
37 * gnm_sheet_guess_region:
41 * Makes a guess at the logical containing @region and returns the possibly
42 * expanded result in @region.
45 gnm_sheet_guess_region (Sheet
*sheet
, GnmRange
*region
)
51 /* check in case only one cell selected */
52 if (region
->start
.col
== region
->end
.col
) {
53 int start
= region
->start
.col
;
54 /* look for previous empty column */
55 for (col
= start
- 1; col
> 0; col
--)
56 if (!sheet_cell_or_one_below_is_not_empty (sheet
, col
, region
->start
.row
))
58 region
->start
.col
= col
+ 1;
60 /* look for next empty column */
61 for (col
= start
+ 1; col
< gnm_sheet_get_max_cols (sheet
); col
++)
62 if (!sheet_cell_or_one_below_is_not_empty (sheet
, col
, region
->start
.row
))
64 region
->end
.col
= col
- 1;
67 /* find first and last non-empty cells in region */
68 for (col
= region
->start
.col
; col
<= region
->end
.col
; col
++)
69 if (sheet_cell_or_one_below_is_not_empty (sheet
, col
, region
->start
.row
))
72 if (col
> region
->end
.col
)
73 return; /* all empty -- give up */
74 region
->start
.col
= col
;
76 for (col
= region
->end
.col
; col
>= region
->start
.col
; col
--)
77 if (sheet_cell_or_one_below_is_not_empty(sheet
, col
, region
->start
.row
))
79 region
->end
.col
= col
;
81 /* now find length of longest column */
82 for (col
= region
->start
.col
; col
<= region
->end
.col
; col
++) {
84 if (sheet_is_cell_empty(sheet
, col
, region
->start
.row
))
86 end_row
= sheet_find_boundary_vertical (sheet
, col
,
87 region
->start
.row
+ offset
, col
, 1, TRUE
);
88 if (end_row
> region
->end
.row
)
89 region
->end
.row
= end_row
;
95 * gnm_sheet_guess_data_range:
99 * Makes a guess at the logical range containing @region and returns the possibly
100 * expanded result in @region. The range is also expanded upwards.
103 gnm_sheet_guess_data_range (Sheet
*sheet
, GnmRange
*region
)
107 int start
= region
->start
.col
;
109 /* look for previous empty column */
110 for (col
= start
- 1; col
>= 0; col
--)
111 if (!sheet_cell_or_one_below_is_not_empty (sheet
, col
, region
->start
.row
))
113 region
->start
.col
= col
+ 1;
115 /* look for next empty column */
116 start
= region
->end
.col
;
117 for (col
= start
+ 1; col
< gnm_sheet_get_max_cols (sheet
); col
++)
118 if (!sheet_cell_or_one_below_is_not_empty (sheet
, col
, region
->start
.row
))
120 region
->end
.col
= col
- 1;
122 for (col
= region
->start
.col
; col
<= region
->end
.col
; col
++) {
123 gboolean empties
= FALSE
;
124 for (row
= region
->start
.row
- 2; row
>= 0; row
--)
125 if (!sheet_cell_or_one_below_is_not_empty (sheet
, col
, row
)) {
129 region
->start
.row
= empties
? row
+ 2 : 0;
130 for (row
= region
->end
.row
+ 1; row
< gnm_sheet_get_max_rows (sheet
); row
++)
131 if (!sheet_cell_or_one_below_is_not_empty (sheet
, col
, row
))
133 region
->end
.row
= row
- 1;