cellblock.c

00001 /********************************************************************\
00002  * cellblock.c -- group of cells that act as cursor within a table  *
00003  *                                                                  *
00004  * This program is free software; you can redistribute it and/or    *
00005  * modify it under the terms of the GNU General Public License as   *
00006  * published by the Free Software Foundation; either version 2 of   *
00007  * the License, or (at your option) any later version.              *
00008  *                                                                  *
00009  * This program is distributed in the hope that it will be useful,  *
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of   *
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the    *
00012  * GNU General Public License for more details.                     *
00013  *                                                                  *
00014  * You should have received a copy of the GNU General Public License*
00015  * along with this program; if not, contact:                        *
00016  *                                                                  *
00017  * Free Software Foundation           Voice:  +1-617-542-5942       *
00018  * 51 Franklin Street, Fifth Floor    Fax:    +1-617-542-2652       *
00019  * Boston, MA  02110-1301,  USA       gnu@gnu.org                   *
00020  *                                                                  *
00021 \********************************************************************/
00022 
00023 /*
00024  * FILE:
00025  * cellblock.c
00026  * 
00027  * FUNCTION:
00028  * implements a rectangular array of cells. See the header file for
00029  * additional documentation.
00030  *
00031  * HISTORY:
00032  * Copyright (c) 1998 Linas Vepstas
00033  * Copyright (c) 2000-2001 Dave Peticolas <dave@krondo.com>
00034  */
00035 
00036 #include "config.h"
00037 
00038 #include "cellblock.h"
00039 
00040 static void gnc_cellblock_init (CellBlock *cellblock, int rows, int cols);
00041 
00042 
00043 CellBlock *
00044 gnc_cellblock_new (int rows, int cols, const char *cursor_name)
00045 {
00046   CellBlock *cellblock;
00047 
00048   g_return_val_if_fail (rows > 0, NULL);
00049   g_return_val_if_fail (cols > 0, NULL);
00050   g_return_val_if_fail (cursor_name != NULL, NULL);
00051 
00052   cellblock = g_new0 (CellBlock, 1);
00053 
00054   gnc_cellblock_init (cellblock, rows, cols);
00055 
00056   cellblock->cursor_name = g_strdup (cursor_name);
00057 
00058   return cellblock;
00059 }
00060 
00061 static void
00062 gnc_cellblock_init (CellBlock *cellblock, int rows, int cols)
00063 {
00064   /* record new size */
00065   cellblock->num_rows = rows;
00066   cellblock->num_cols = cols;
00067 
00068   cellblock->start_col = cols;
00069   cellblock->stop_col  = -1;
00070 
00071   /* malloc new cell table */
00072   cellblock->cells = g_ptr_array_new ();
00073 
00074   g_ptr_array_set_size (cellblock->cells, rows * cols);
00075 }
00076 
00077 void        
00078 gnc_cellblock_destroy (CellBlock *cellblock)
00079 {
00080    if (!cellblock) return;
00081 
00082    g_ptr_array_free (cellblock->cells, FALSE);
00083    cellblock->cells = NULL;
00084 
00085    g_free (cellblock->cursor_name);
00086    cellblock->cursor_name = NULL;
00087 
00088    g_free (cellblock);
00089 }
00090 
00091 void
00092 gnc_cellblock_set_cell (CellBlock *cellblock,
00093                         int row, int col,
00094                         BasicCell *cell)
00095 {
00096   if (cellblock == NULL)
00097     return;
00098 
00099   if (row < 0 || row >= cellblock->num_rows)
00100     return;
00101 
00102   if (col < 0 || col >= cellblock->num_cols)
00103     return;
00104 
00105   cellblock->cells->pdata[(row * cellblock->num_cols) + col] = cell;
00106 }
00107 
00108 BasicCell *
00109 gnc_cellblock_get_cell (CellBlock *cellblock, int row, int col)
00110 {
00111   if (cellblock == NULL)
00112     return NULL;
00113 
00114   if (row < 0 || row >= cellblock->num_rows)
00115     return NULL;
00116 
00117   if (col < 0 || col >= cellblock->num_cols)
00118     return NULL;
00119 
00120   return cellblock->cells->pdata[(row * cellblock->num_cols) + col];
00121 }
00122 
00123 int
00124 gnc_cellblock_changed (CellBlock *cursor, gboolean include_conditional)
00125 {
00126   int changed = 0;
00127   int r, c;
00128 
00129   if (!cursor)
00130     return FALSE;
00131 
00132   for (r = 0; r < cursor->num_rows; r++)
00133     for (c = 0; c < cursor->num_cols; c++)
00134     {
00135       BasicCell *cell;
00136 
00137       cell = gnc_cellblock_get_cell (cursor, r, c);
00138       if (cell == NULL)
00139         continue;
00140 
00141       if (gnc_basic_cell_get_changed (cell))
00142       {
00143         changed++;
00144         continue;
00145       }
00146 
00147       if (include_conditional &&
00148           gnc_basic_cell_get_conditionally_changed (cell))
00149         changed++;
00150     }
00151 
00152   return changed;
00153 }
00154 
00155 void
00156 gnc_cellblock_clear_changes (CellBlock *cursor)
00157 {
00158   int r, c;
00159 
00160   if (!cursor)
00161     return;
00162 
00163   for (r = 0; r < cursor->num_rows; r++)
00164     for (c = 0; c < cursor->num_cols; c++)
00165     {
00166       BasicCell *cell;
00167 
00168       cell = gnc_cellblock_get_cell (cursor, r, c);
00169       if (cell == NULL)
00170         continue;
00171 
00172       gnc_basic_cell_set_changed (cell, FALSE);
00173       gnc_basic_cell_set_conditionally_changed (cell, FALSE);
00174     }
00175 }

Generated on Mon Sep 8 05:03:45 2008 for GnuCash by  doxygen 1.5.2