~ubuntu-branches/ubuntu/quantal/gnumeric/quantal

« back to all changes in this revision

Viewing changes to src/sheet-utils.c

  • Committer: Bazaar Package Importer
  • Author(s): Gauvain Pocentek
  • Date: 2009-06-22 13:37:20 UTC
  • mfrom: (1.1.20 upstream) (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090622133720-rtdazsiz2lx5q8l7
Tags: 1.9.9-1ubuntu1
* Merge from debian unstable, remaining changes:
  - Promoted gnumeric-doc to Recommends in gnumeric package for help to be
    installed automatically
  - gnumeric-gtk is a transitional package
  - gnumeric conflicts with gnumeric-gtk << 1.8.3-3ubuntu1
  - call initltool-update in po*
  - remove psiconv support (psiconv is in universe):
    o debian/control: remove B-D on libpsiconv-dev
    o debian/rules: don't pass --with-psiconv to ./configure
    o debian/gnumeric-plugins-extra.install: don't install the psiconv
      plugin

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
 
2
 
 
3
/*
 
4
 * sheet-utils.c: Utility routines for Sheet content
 
5
 *
 
6
 * Copyright (C) 2002-2008 Jody Goldberg (jody@gnome.org)
 
7
 *
 
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.
 
11
 *
 
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.
 
16
 *
 
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
 
21
 */
 
22
#include <gnumeric-config.h>
 
23
#include "libgnumeric.h"
 
24
#include "sheet-utils.h"
 
25
#include "sheet.h"
 
26
 
 
27
static gboolean
 
28
sheet_cell_or_one_below_is_not_empty (Sheet *sheet, int col, int row)
 
29
{
 
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));
 
33
}
 
34
 
 
35
/**
 
36
 * gnm_sheet_guess_region :
 
37
 * @sheet : #Sheet
 
38
 * @range : #GnmRange
 
39
 *
 
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)
 
45
{
 
46
        int col;
 
47
        int end_row;
 
48
        int offset;
 
49
 
 
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;
 
58
 
 
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;
 
64
        }
 
65
 
 
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;
 
70
 
 
71
        if (col > region->end.col)
 
72
                return; /* all empty -- give up */
 
73
        region->start.col = col;
 
74
 
 
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;
 
79
 
 
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;
 
89
        }
 
90
}