GUI: Move .ui files from goffice resources to glib resources
[gnumeric.git] / src / sheet-utils.c
blobbd75467543d5e4c3fbe1aa96e371e8eb9e7dd287
1 /* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 /*
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
21 * USA
23 #include <gnumeric-config.h>
24 #include "libgnumeric.h"
25 #include "sheet-utils.h"
26 #include "sheet.h"
28 static gboolean
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));
36 /**
37 * gnm_sheet_guess_region:
38 * @sheet: #Sheet
39 * @region: #GnmRange
41 * Makes a guess at the logical containing @region and returns the possibly
42 * expanded result in @region.
43 **/
44 void
45 gnm_sheet_guess_region (Sheet *sheet, GnmRange *region)
47 int col;
48 int end_row;
49 int offset;
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))
57 break;
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))
63 break;
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))
70 break;
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))
78 break;
79 region->end.col = col;
81 /* now find length of longest column */
82 for (col = region->start.col; col <= region->end.col; col++) {
83 offset = 0;
84 if (sheet_is_cell_empty(sheet, col, region->start.row))
85 offset = 1;
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;
94 /**
95 * gnm_sheet_guess_data_range:
96 * @sheet: #Sheet
97 * @region: #GnmRange
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.
102 void
103 gnm_sheet_guess_data_range (Sheet *sheet, GnmRange *region)
105 int col;
106 int row;
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))
112 break;
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))
119 break;
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)) {
126 empties = TRUE;
127 break;
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))
132 break;
133 region->end.row = row - 1;
135 return;