basiccell.c

00001 /********************************************************************\
00002  * basiccell.c -- base class for editable cell in 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  * basiccell.c
00026  *
00027  * FUNCTION: 
00028  * Implements the base class for the cell handler object.
00029  * See the header file for additional documentation.
00030  *
00031  * HISTORY:
00032  * Copyright (c) 1998 Linas Vepstas
00033  * Copyright (c) 2000 Dave Peticolas <dave@krondo.com>
00034  */
00035 
00036 #include "config.h"
00037 
00038 #include <stdlib.h>
00039 #include <string.h>
00040 
00041 #include "basiccell.h"
00042 #include "gnc-engine.h"
00043 
00044 /* Debugging module */
00045 static QofLogModule log_module = GNC_MOD_REGISTER;
00046 
00047 gboolean
00048 gnc_cell_name_equal (const char * cell_name_1,
00049                      const char * cell_name_2)
00050 {
00051   return (safe_strcmp (cell_name_1, cell_name_2) == 0);
00052 }
00053 
00054 BasicCell *
00055 gnc_basic_cell_new (void)
00056 {
00057   BasicCell * cell;
00058 
00059   cell = g_new0 (BasicCell, 1);
00060 
00061   gnc_basic_cell_init (cell);
00062 
00063   return cell;
00064 }
00065 
00066 static void
00067 gnc_basic_cell_clear (BasicCell *cell)
00068 {
00069   g_free (cell->cell_name);
00070   cell->cell_name = NULL;
00071 
00072   cell->changed = FALSE;
00073   cell->conditionally_changed = FALSE;
00074 
00075   cell->value = NULL;
00076   cell->value_chars = 0;
00077 
00078   cell->set_value = NULL;
00079   cell->enter_cell = NULL;
00080   cell->modify_verify = NULL;
00081   cell->direct_update = NULL;
00082   cell->leave_cell = NULL;
00083   cell->gui_realize = NULL;
00084   cell->gui_move = NULL;
00085   cell->gui_destroy = NULL;
00086 
00087   cell->is_popup = FALSE;
00088 
00089   cell->gui_private = NULL;
00090 
00091   g_free (cell->sample_text);
00092   cell->sample_text = NULL;
00093 }
00094 
00095 void
00096 gnc_basic_cell_init (BasicCell *cell)
00097 {
00098   gnc_basic_cell_clear (cell);
00099 
00100   cell->value = g_strdup ("");
00101 }
00102 
00103 void
00104 gnc_basic_cell_destroy (BasicCell *cell)
00105 {
00106   ENTER(" ");
00107   if (cell->destroy)
00108     cell->destroy (cell);
00109 
00110   /* give any gui elements a chance to clean up */
00111   if (cell->gui_destroy)
00112     (*(cell->gui_destroy)) (cell);
00113 
00114   /* free up data strings */
00115   g_free (cell->value);
00116   cell->value = NULL;
00117 
00118   /* help prevent access to freed memory */
00119   gnc_basic_cell_clear (cell);
00120 
00121   /* free the object itself */
00122   g_free (cell);
00123   LEAVE(" ");
00124 }
00125 
00126 void
00127 gnc_basic_cell_set_name (BasicCell *cell, const char *name)
00128 {
00129   if (!cell) return;
00130   if (cell->cell_name == name) return;
00131 
00132   g_free (cell->cell_name);
00133   cell->cell_name = g_strdup (name);
00134 }
00135 
00136 gboolean
00137 gnc_basic_cell_has_name (BasicCell *cell, const char *name)
00138 {
00139   if (!cell) return FALSE;
00140   if (!name) return FALSE;
00141   if (!cell->cell_name) return FALSE;
00142 
00143   return (strcmp (name, cell->cell_name) == 0);
00144 }
00145 
00146 void
00147 gnc_basic_cell_set_sample_text (BasicCell *cell,
00148                                 const char *sample_text)
00149 {
00150   if (!cell) return;
00151   if (cell->sample_text == sample_text) return;
00152 
00153   g_free (cell->sample_text);
00154   cell->sample_text = g_strdup (sample_text);
00155 }
00156 
00157 void
00158 gnc_basic_cell_set_alignment (BasicCell *cell,
00159                               CellAlignment alignment)
00160 {
00161   if (!cell) return;
00162   cell->alignment = alignment;
00163 }
00164 
00165 void
00166 gnc_basic_cell_set_expandable (BasicCell *cell, gboolean expandable)
00167 {
00168   if (!cell) return;
00169   cell->expandable = expandable;
00170 }
00171 
00172 void
00173 gnc_basic_cell_set_span (BasicCell *cell, gboolean span)
00174 {
00175   if (!cell) return;
00176   cell->span = span;
00177 }
00178 
00179 const char *
00180 gnc_basic_cell_get_value (BasicCell *cell)
00181 {
00182   g_return_val_if_fail (cell != NULL, NULL);
00183 
00184   return cell->value;
00185 }
00186 
00187 void
00188 gnc_basic_cell_set_value (BasicCell *cell, const char *val)
00189 {
00190   CellSetValueFunc cb;
00191 
00192   cb = cell->set_value;
00193   if (cb)
00194   {
00195     /* avoid recursion by disabling the  
00196      * callback while it's being called. */
00197     cell->set_value = NULL;
00198     cb (cell, val);
00199     cell->set_value = cb;
00200   }
00201   else
00202     gnc_basic_cell_set_value_internal (cell, val);
00203 }
00204 
00205 gboolean
00206 gnc_basic_cell_get_changed (BasicCell *cell)
00207 {
00208   if (!cell) return FALSE;
00209 
00210   return cell->changed;
00211 }
00212 
00213 gboolean
00214 gnc_basic_cell_get_conditionally_changed (BasicCell *cell)
00215 {
00216   if (!cell) return FALSE;
00217 
00218   return cell->conditionally_changed;
00219 }
00220 
00221 void
00222 gnc_basic_cell_set_changed (BasicCell *cell, gboolean changed)
00223 {
00224   if (!cell) return;
00225 
00226   cell->changed = changed;
00227 }
00228 
00229 void
00230 gnc_basic_cell_set_conditionally_changed (BasicCell *cell, gboolean changed)
00231 {
00232   if (!cell) return;
00233 
00234   cell->conditionally_changed = changed;
00235 }
00236 
00237 void
00238 gnc_basic_cell_set_value_internal (BasicCell *cell, const char *value)
00239 {
00240   if (value == NULL)
00241     value = "";
00242 
00243   /* If the caller tries to set the value with our own value then do
00244    * nothing because we have no work to do (or, at least, all the work
00245    * will result in the status-quo, so why do anything?)  See bug
00246    * #103174 and the description in the changelog on 2003-09-04.
00247    */
00248   if (cell->value == value)
00249     return;
00250 
00251   g_free (cell->value);
00252   cell->value = g_strdup (value);
00253   cell->value_chars = g_utf8_strlen(value, -1);
00254 }

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