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 version 2 of the GNU General Public
10
* License as published by the Free Software Foundation.
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;