GnuCash 2.4.99
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 BasicCell *
00124 gnc_cellblock_get_cell_by_name(CellBlock *cellblock,
00125                                const char *cell_name,
00126                                int *row, int *col)
00127 {
00128     int r, c, num_rows, num_cols;
00129 
00130     if (cellblock == NULL)
00131         return NULL;
00132 
00133     if (cell_name == NULL)
00134         return NULL;
00135 
00136     num_rows = cellblock->num_rows;
00137     num_cols = cellblock->num_cols;
00138     for (r = 0; r < num_rows; r++)
00139         for (c = 0; c < num_cols; c++)
00140         {
00141             BasicCell *cell = cellblock->cells->pdata[(r * num_cols) + c];
00142             if (!cell) continue;
00143             if (gnc_cell_name_equal(cell->cell_name, cell_name))
00144             {
00145                 if (row)
00146                     *row = r;
00147                 if (col)
00148                     *col = c;
00149                 return cell;
00150             }
00151         }
00152 
00153     return NULL;
00154 }
00155 
00156 int
00157 gnc_cellblock_changed (CellBlock *cursor, gboolean include_conditional)
00158 {
00159     int changed = 0;
00160     int r, c;
00161 
00162     if (!cursor)
00163         return FALSE;
00164 
00165     for (r = 0; r < cursor->num_rows; r++)
00166         for (c = 0; c < cursor->num_cols; c++)
00167         {
00168             BasicCell *cell;
00169 
00170             cell = gnc_cellblock_get_cell (cursor, r, c);
00171             if (cell == NULL)
00172                 continue;
00173 
00174             if (gnc_basic_cell_get_changed (cell))
00175             {
00176                 changed++;
00177                 continue;
00178             }
00179 
00180             if (include_conditional &&
00181                     gnc_basic_cell_get_conditionally_changed (cell))
00182                 changed++;
00183         }
00184 
00185     return changed;
00186 }
00187 
00188 void
00189 gnc_cellblock_clear_changes (CellBlock *cursor)
00190 {
00191     int r, c;
00192 
00193     if (!cursor)
00194         return;
00195 
00196     for (r = 0; r < cursor->num_rows; r++)
00197         for (c = 0; c < cursor->num_cols; c++)
00198         {
00199             BasicCell *cell;
00200 
00201             cell = gnc_cellblock_get_cell (cursor, r, c);
00202             if (cell == NULL)
00203                 continue;
00204 
00205             gnc_basic_cell_set_changed (cell, FALSE);
00206             gnc_basic_cell_set_conditionally_changed (cell, FALSE);
00207         }
00208 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines