GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
basiccell.c
1 /********************************************************************\
2  * basiccell.c -- base class for editable cell in a table *
3  * *
4  * This program is free software; you can redistribute it and/or *
5  * modify it under the terms of the GNU General Public License as *
6  * published by the Free Software Foundation; either version 2 of *
7  * the License, or (at your option) any later version. *
8  * *
9  * This program is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU General Public License*
15  * along with this program; if not, contact: *
16  * *
17  * Free Software Foundation Voice: +1-617-542-5942 *
18  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
19  * Boston, MA 02110-1301, USA [email protected] *
20  * *
21 \********************************************************************/
22 
23 /*
24  * FILE:
25  * basiccell.c
26  *
27  * FUNCTION:
28  * Implements the base class for the cell handler object.
29  * See the header file for additional documentation.
30  *
31  * HISTORY:
32  * Copyright (c) 1998 Linas Vepstas
33  * Copyright (c) 2000 Dave Peticolas <[email protected]>
34  */
35 
36 #include "config.h"
37 
38 #include <stdlib.h>
39 #include <string.h>
40 
41 #include "basiccell.h"
42 #include "gnc-engine.h"
43 
44 /* Debugging module */
45 static QofLogModule log_module = GNC_MOD_REGISTER;
46 
47 gboolean
48 gnc_cell_name_equal (const char * cell_name_1,
49  const char * cell_name_2)
50 {
51  return (g_strcmp0 (cell_name_1, cell_name_2) == 0);
52 }
53 
54 BasicCell *
55 gnc_basic_cell_new (void)
56 {
57  BasicCell * cell;
58 
59  cell = g_new0 (BasicCell, 1);
60 
61  gnc_basic_cell_init (cell);
62 
63  return cell;
64 }
65 
66 static void
67 gnc_basic_cell_clear (BasicCell *cell)
68 {
69  g_free (cell->cell_name);
70  cell->cell_name = NULL;
71  g_free (cell->cell_type_name);
72  cell->cell_type_name = NULL;
73  cell->changed = FALSE;
74  cell->conditionally_changed = FALSE;
75 
76  cell->value = NULL;
77  cell->value_chars = 0;
78 
79  cell->set_value = NULL;
80  cell->enter_cell = NULL;
81  cell->modify_verify = NULL;
82  cell->direct_update = NULL;
83  cell->leave_cell = NULL;
84  cell->gui_realize = NULL;
85  cell->gui_move = NULL;
86  cell->gui_destroy = NULL;
87 
88  cell->is_popup = FALSE;
89 
90  cell->gui_private = NULL;
91 
92  g_free (cell->sample_text);
93  cell->sample_text = NULL;
94 }
95 
96 void
97 gnc_basic_cell_init (BasicCell *cell)
98 {
99  gnc_basic_cell_clear (cell);
100 
101  cell->value = g_strdup ("");
102 }
103 
104 void
105 gnc_basic_cell_destroy (BasicCell *cell)
106 {
107  ENTER(" ");
108  if (cell->destroy)
109  cell->destroy (cell);
110 
111  /* give any gui elements a chance to clean up */
112  if (cell->gui_destroy)
113  (*(cell->gui_destroy)) (cell);
114 
115  /* free up data strings */
116  g_free (cell->value);
117  cell->value = NULL;
118 
119  /* help prevent access to freed memory */
120  gnc_basic_cell_clear (cell);
121 
122  /* free the object itself */
123  g_free (cell);
124  LEAVE(" ");
125 }
126 
127 void
128 gnc_basic_cell_set_name (BasicCell *cell, const char *name)
129 {
130  if (!cell) return;
131  if (cell->cell_name == name) return;
132 
133  g_free (cell->cell_name);
134  cell->cell_name = g_strdup (name);
135 }
136 
137 gboolean
138 gnc_basic_cell_has_name (BasicCell *cell, const char *name)
139 {
140  if (!cell) return FALSE;
141  if (!name) return FALSE;
142  if (!cell->cell_name) return FALSE;
143 
144  return (strcmp (name, cell->cell_name) == 0);
145 }
146 
147 
148 void
149 gnc_basic_cell_set_type_name (BasicCell *cell, const gchar *type_name)
150 {
151  if (!cell) return;
152  if (cell->cell_type_name == type_name) return;
153 
154  g_free (cell->cell_type_name);
155  cell->cell_type_name = g_strdup(type_name);
156 }
157 
158 gboolean
159 gnc_basic_cell_has_type_name (BasicCell *cell, const gchar *type_name)
160 {
161  if (!cell) return FALSE;
162  if (!type_name) return FALSE;
163  if (!cell->cell_type_name) return FALSE;
164 
165  return (g_strcmp0 (type_name, cell->cell_type_name));
166 }
167 
168 void
169 gnc_basic_cell_set_sample_text (BasicCell *cell,
170  const char *sample_text)
171 {
172  if (!cell) return;
173  if (cell->sample_text == sample_text) return;
174 
175  g_free (cell->sample_text);
176  cell->sample_text = g_strdup (sample_text);
177 }
178 
179 void
180 gnc_basic_cell_set_alignment (BasicCell *cell,
181  CellAlignment alignment)
182 {
183  if (!cell) return;
184  cell->alignment = alignment;
185 }
186 
187 void
188 gnc_basic_cell_set_expandable (BasicCell *cell, gboolean expandable)
189 {
190  if (!cell) return;
191  cell->expandable = expandable;
192 }
193 
194 void
195 gnc_basic_cell_set_span (BasicCell *cell, gboolean span)
196 {
197  if (!cell) return;
198  cell->span = span;
199 }
200 
201 const char *
202 gnc_basic_cell_get_value (BasicCell *cell)
203 {
204  g_return_val_if_fail (cell != NULL, NULL);
205 
206  return cell->value;
207 }
208 
209 void
210 gnc_basic_cell_set_value (BasicCell *cell, const char *val)
211 {
212  CellSetValueFunc cb;
213 
214  cb = cell->set_value;
215  if (cb)
216  {
217  /* avoid recursion by disabling the
218  * callback while it's being called. */
219  cell->set_value = NULL;
220  cb (cell, val);
221  cell->set_value = cb;
222  }
223  else
224  gnc_basic_cell_set_value_internal (cell, val);
225 }
226 
227 gboolean
228 gnc_basic_cell_get_changed (BasicCell *cell)
229 {
230  if (!cell) return FALSE;
231 
232  return cell->changed;
233 }
234 
235 gboolean
236 gnc_basic_cell_get_conditionally_changed (BasicCell *cell)
237 {
238  if (!cell) return FALSE;
239 
240  return cell->conditionally_changed;
241 }
242 
243 void
244 gnc_basic_cell_set_changed (BasicCell *cell, gboolean changed)
245 {
246  if (!cell) return;
247 
248  cell->changed = changed;
249 }
250 
251 void
252 gnc_basic_cell_set_conditionally_changed (BasicCell *cell, gboolean changed)
253 {
254  if (!cell) return;
255 
256  cell->conditionally_changed = changed;
257 }
258 
259 void
260 gnc_basic_cell_set_value_internal (BasicCell *cell, const char *value)
261 {
262  if (value == NULL)
263  value = "";
264 
265  /* If the caller tries to set the value with our own value then do
266  * nothing because we have no work to do (or, at least, all the work
267  * will result in the status-quo, so why do anything?) See bug
268  * #103174 and the description in the changelog on 2003-09-04.
269  */
270  if (cell->value == value)
271  return;
272 
273  g_free (cell->value);
274  cell->value = g_strdup (value);
275  cell->value_chars = g_utf8_strlen(value, -1);
276 }
#define ENTER(format, args...)
Definition: qoflog.h:261
All type declarations for the whole Gnucash engine.
#define LEAVE(format, args...)
Definition: qoflog.h:271
const gchar * QofLogModule
Definition: qofid.h:89