GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
table-model.c
1 /********************************************************************\
2  * table-model.c -- 2D grid table object model *
3  * Copyright (c) 2001 Free Software Foundation *
4  * Author: Dave Peticolas <[email protected]> *
5  * *
6  * This program is free software; you can redistribute it and/or *
7  * modify it under the terms of the GNU General Public License as *
8  * published by the Free Software Foundation; either version 2 of *
9  * the License, or (at your option) any later version. *
10  * *
11  * This program is distributed in the hope that it will be useful, *
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14  * GNU General Public License for more details. *
15  * *
16  * You should have received a copy of the GNU General Public License*
17  * along with this program; if not, contact: *
18  * *
19  * Free Software Foundation Voice: +1-617-542-5942 *
20  * 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652 *
21  * Boston, MA 02110-1301, USA [email protected] *
22  * *
23 \********************************************************************/
24 
25 #include "config.h"
26 
27 #include <glib.h>
28 
29 #include "table-model.h"
30 
31 
32 #define DEFAULT_HANDLER ""
33 
34 typedef struct
35 {
36  char *cell_name;
37  gpointer handler;
38 } HandlerNode;
39 
40 
41 static GHashTable *
42 gnc_table_model_handler_hash_new (void)
43 {
44  return g_hash_table_new (g_str_hash, g_str_equal);
45 }
46 
47 static void
48 hash_destroy_helper (gpointer key, gpointer value, gpointer user_data)
49 {
50  HandlerNode *node = value;
51 
52  g_free (node->cell_name);
53  node->cell_name = NULL;
54 
55  g_free (node);
56 }
57 
58 static void
59 gnc_table_model_handler_hash_destroy (GHashTable *hash)
60 {
61  if (!hash) return;
62 
63  g_hash_table_foreach (hash, hash_destroy_helper, NULL);
64  g_hash_table_destroy (hash);
65 }
66 
67 static void
68 gnc_table_model_handler_hash_remove (GHashTable *hash, const char *cell_name)
69 {
70  HandlerNode *node;
71 
72  if (!hash) return;
73 
74  node = g_hash_table_lookup (hash, cell_name);
75  if (!node) return;
76 
77  g_hash_table_remove (hash, cell_name);
78 
79  g_free (node->cell_name);
80  node->cell_name = NULL;
81 
82  g_free (node);
83 }
84 
85 static void
86 gnc_table_model_handler_hash_insert (GHashTable *hash,
87  const char *cell_name,
88  gpointer handler)
89 {
90  HandlerNode *node;
91 
92  g_return_if_fail (hash != NULL);
93  g_return_if_fail (cell_name != NULL);
94 
95  gnc_table_model_handler_hash_remove (hash, cell_name);
96  if (!handler) return;
97 
98  node = g_new0 (HandlerNode, 1);
99 
100  node->cell_name = g_strdup (cell_name);
101  node->handler = handler;
102 
103  g_hash_table_insert (hash, node->cell_name, node);
104 }
105 
106 static gpointer
107 gnc_table_model_handler_hash_lookup (GHashTable *hash, const char *cell_name)
108 {
109  HandlerNode *node;
110 
111  if (!hash) return NULL;
112 
113  if (cell_name)
114  {
115  node = g_hash_table_lookup (hash, cell_name);
116  if (node) return node->handler;
117  }
118 
119  cell_name = DEFAULT_HANDLER;
120  node = g_hash_table_lookup (hash, cell_name);
121  if (node) return node->handler;
122 
123  return NULL;
124 }
125 
126 TableModel *
127 gnc_table_model_new (void)
128 {
129  TableModel *model;
130 
131  model = g_new0 (TableModel, 1);
132 
133  model->entry_handlers = gnc_table_model_handler_hash_new ();
134  model->label_handlers = gnc_table_model_handler_hash_new ();
135  model->help_handlers = gnc_table_model_handler_hash_new ();
136  model->io_flags_handlers = gnc_table_model_handler_hash_new ();
137  model->fg_color_handlers = gnc_table_model_handler_hash_new ();
138  model->bg_color_handlers = gnc_table_model_handler_hash_new ();
139  model->cell_border_handlers = gnc_table_model_handler_hash_new ();
140  model->confirm_handlers = gnc_table_model_handler_hash_new ();
141  model->save_handlers = gnc_table_model_handler_hash_new ();
142 
143  model->read_only = FALSE;
144  model->dividing_row_upper = -1;
145  model->dividing_row = -1;
146  model->dividing_row_lower = -1;
147 
148  return model;
149 }
150 
151 void
152 gnc_table_model_destroy (TableModel *model)
153 {
154  if (!model) return;
155 
156  gnc_table_model_handler_hash_destroy (model->entry_handlers);
157  model->entry_handlers = NULL;
158 
159  gnc_table_model_handler_hash_destroy (model->label_handlers);
160  model->label_handlers = NULL;
161 
162  gnc_table_model_handler_hash_destroy (model->help_handlers);
163  model->help_handlers = NULL;
164 
165  gnc_table_model_handler_hash_destroy (model->io_flags_handlers);
166  model->io_flags_handlers = NULL;
167 
168  gnc_table_model_handler_hash_destroy (model->fg_color_handlers);
169  model->fg_color_handlers = NULL;
170 
171  gnc_table_model_handler_hash_destroy (model->bg_color_handlers);
172  model->bg_color_handlers = NULL;
173 
174  gnc_table_model_handler_hash_destroy (model->cell_border_handlers);
175  model->cell_border_handlers = NULL;
176 
177  gnc_table_model_handler_hash_destroy (model->confirm_handlers);
178  model->confirm_handlers = NULL;
179 
180  gnc_table_model_handler_hash_destroy (model->save_handlers);
181  model->save_handlers = NULL;
182 
183  g_free (model);
184 }
185 
186 void
187 gnc_table_model_set_read_only (TableModel *model, gboolean read_only)
188 {
189  g_return_if_fail (model);
190 
191  model->read_only = read_only;
192 }
193 
194 gboolean
195 gnc_table_model_read_only (TableModel *model)
196 {
197  g_return_val_if_fail (model, FALSE);
198 
199  return model->read_only;
200 }
201 
202 void
203 gnc_table_model_set_entry_handler (TableModel *model,
204  TableGetEntryHandler entry_handler,
205  const char * cell_name)
206 {
207  g_return_if_fail (model != NULL);
208  g_return_if_fail (cell_name != NULL);
209 
210  gnc_table_model_handler_hash_insert (model->entry_handlers,
211  cell_name,
212  entry_handler);
213 }
214 
215 void
216 gnc_table_model_set_default_entry_handler
217 (TableModel *model, TableGetEntryHandler entry_handler)
218 {
219  g_return_if_fail (model != NULL);
220 
221  gnc_table_model_handler_hash_insert (model->entry_handlers,
222  DEFAULT_HANDLER,
223  entry_handler);
224 }
225 
226 TableGetEntryHandler
227 gnc_table_model_get_entry_handler (TableModel *model, const char * cell_name)
228 {
229  g_return_val_if_fail (model != NULL, NULL);
230 
231  return gnc_table_model_handler_hash_lookup (model->entry_handlers,
232  cell_name);
233 }
234 
235 void
236 gnc_table_model_set_label_handler (TableModel *model,
237  TableGetLabelHandler label_handler,
238  const char * cell_name)
239 {
240  g_return_if_fail (model != NULL);
241  g_return_if_fail (cell_name != NULL);
242 
243  gnc_table_model_handler_hash_insert (model->label_handlers,
244  cell_name,
245  label_handler);
246 }
247 
248 void
249 gnc_table_model_set_default_label_handler
250 (TableModel *model, TableGetLabelHandler label_handler)
251 {
252  g_return_if_fail (model != NULL);
253 
254  gnc_table_model_handler_hash_insert (model->label_handlers,
255  DEFAULT_HANDLER,
256  label_handler);
257 }
258 
259 TableGetLabelHandler
260 gnc_table_model_get_label_handler (TableModel *model, const char * cell_name)
261 {
262  g_return_val_if_fail (model != NULL, NULL);
263 
264  return gnc_table_model_handler_hash_lookup (model->label_handlers,
265  cell_name);
266 }
267 
268 void
269 gnc_table_model_set_help_handler (TableModel *model,
270  TableGetHelpHandler help_handler,
271  const char * cell_name)
272 {
273  g_return_if_fail (model != NULL);
274  g_return_if_fail (cell_name != NULL);
275 
276  gnc_table_model_handler_hash_insert (model->help_handlers,
277  cell_name,
278  help_handler);
279 }
280 
281 void
282 gnc_table_model_set_default_help_handler (TableModel *model,
283  TableGetHelpHandler help_handler)
284 {
285  g_return_if_fail (model != NULL);
286 
287  gnc_table_model_handler_hash_insert (model->help_handlers,
288  DEFAULT_HANDLER,
289  help_handler);
290 }
291 
292 TableGetHelpHandler
293 gnc_table_model_get_help_handler (TableModel *model, const char * cell_name)
294 {
295  g_return_val_if_fail (model != NULL, NULL);
296 
297  return gnc_table_model_handler_hash_lookup (model->help_handlers, cell_name);
298 }
299 
300 void
301 gnc_table_model_set_io_flags_handler
302 (TableModel *model,
303  TableGetCellIOFlagsHandler io_flags_handler,
304  const char * cell_name)
305 {
306  g_return_if_fail (model != NULL);
307  g_return_if_fail (cell_name != NULL);
308 
309  gnc_table_model_handler_hash_insert (model->io_flags_handlers,
310  cell_name,
311  io_flags_handler);
312 }
313 
314 void
315 gnc_table_model_set_default_io_flags_handler
316 (TableModel *model,
317  TableGetCellIOFlagsHandler io_flags_handler)
318 {
319  g_return_if_fail (model != NULL);
320 
321  gnc_table_model_handler_hash_insert (model->io_flags_handlers,
322  DEFAULT_HANDLER,
323  io_flags_handler);
324 }
325 
326 TableGetCellIOFlagsHandler
327 gnc_table_model_get_io_flags_handler (TableModel *model,
328  const char * cell_name)
329 {
330  g_return_val_if_fail (model != NULL, NULL);
331 
332  return gnc_table_model_handler_hash_lookup (model->io_flags_handlers,
333  cell_name);
334 }
335 
336 void
337 gnc_table_model_set_fg_color_handler
338 (TableModel *model,
339  TableGetFGColorHandler fg_color_handler,
340  const char * cell_name)
341 {
342  g_return_if_fail (model != NULL);
343  g_return_if_fail (cell_name != NULL);
344 
345  gnc_table_model_handler_hash_insert (model->fg_color_handlers,
346  cell_name,
347  fg_color_handler);
348 }
349 
350 void
351 gnc_table_model_set_default_fg_color_handler
352 (TableModel *model,
353  TableGetFGColorHandler fg_color_handler)
354 {
355  g_return_if_fail (model != NULL);
356 
357  gnc_table_model_handler_hash_insert (model->fg_color_handlers,
358  DEFAULT_HANDLER,
359  fg_color_handler);
360 }
361 
362 TableGetFGColorHandler
363 gnc_table_model_get_fg_color_handler (TableModel *model,
364  const char * cell_name)
365 {
366  g_return_val_if_fail (model != NULL, NULL);
367 
368  return gnc_table_model_handler_hash_lookup (model->fg_color_handlers,
369  cell_name);
370 }
371 
372 void
373 gnc_table_model_set_bg_color_handler
374 (TableModel *model,
375  TableGetBGColorHandler bg_color_handler,
376  const char * cell_name)
377 {
378  g_return_if_fail (model != NULL);
379  g_return_if_fail (cell_name != NULL);
380 
381  gnc_table_model_handler_hash_insert (model->bg_color_handlers,
382  cell_name,
383  bg_color_handler);
384 }
385 
386 void
387 gnc_table_model_set_default_bg_color_handler
388 (TableModel *model,
389  TableGetBGColorHandler bg_color_handler)
390 {
391  g_return_if_fail (model != NULL);
392 
393  gnc_table_model_handler_hash_insert (model->bg_color_handlers,
394  DEFAULT_HANDLER,
395  bg_color_handler);
396 }
397 
398 TableGetBGColorHandler
399 gnc_table_model_get_bg_color_handler (TableModel *model,
400  const char * cell_name)
401 {
402  g_return_val_if_fail (model != NULL, NULL);
403 
404  return gnc_table_model_handler_hash_lookup (model->bg_color_handlers,
405  cell_name);
406 }
407 
408 void
409 gnc_table_model_set_cell_border_handler
410 (TableModel *model,
411  TableGetCellBorderHandler cell_border_handler,
412  const char * cell_name)
413 {
414  g_return_if_fail (model != NULL);
415  g_return_if_fail (cell_name != NULL);
416 
417  gnc_table_model_handler_hash_insert (model->cell_border_handlers,
418  cell_name,
419  cell_border_handler);
420 }
421 
422 void
423 gnc_table_model_set_default_cell_border_handler
424 (TableModel *model,
425  TableGetCellBorderHandler cell_border_handler)
426 {
427  g_return_if_fail (model != NULL);
428 
429  gnc_table_model_handler_hash_insert (model->cell_border_handlers,
430  DEFAULT_HANDLER,
431  cell_border_handler);
432 }
433 
434 TableGetCellBorderHandler
435 gnc_table_model_get_cell_border_handler (TableModel *model,
436  const char * cell_name)
437 {
438  g_return_val_if_fail (model != NULL, NULL);
439 
440  return gnc_table_model_handler_hash_lookup (model->cell_border_handlers,
441  cell_name);
442 }
443 
444 void
445 gnc_table_model_set_confirm_handler
446 (TableModel *model,
447  TableConfirmHandler confirm_handler,
448  const char * cell_name)
449 {
450  g_return_if_fail (model != NULL);
451  g_return_if_fail (cell_name != NULL);
452 
453  gnc_table_model_handler_hash_insert (model->confirm_handlers,
454  cell_name,
455  confirm_handler);
456 }
457 
458 void
459 gnc_table_model_set_default_confirm_handler
460 (TableModel *model,
461  TableConfirmHandler confirm_handler)
462 {
463  g_return_if_fail (model != NULL);
464 
465  gnc_table_model_handler_hash_insert (model->confirm_handlers,
466  DEFAULT_HANDLER,
467  confirm_handler);
468 }
469 
470 TableConfirmHandler
471 gnc_table_model_get_confirm_handler (TableModel *model,
472  const char * cell_name)
473 {
474  g_return_val_if_fail (model != NULL, NULL);
475 
476  return gnc_table_model_handler_hash_lookup (model->confirm_handlers,
477  cell_name);
478 }
479 
480 void
481 gnc_table_model_set_save_handler
482 (TableModel *model,
483  TableSaveCellHandler save_handler,
484  const char * cell_name)
485 {
486  g_return_if_fail (model != NULL);
487  g_return_if_fail (cell_name != NULL);
488 
489  gnc_table_model_handler_hash_insert (model->save_handlers,
490  cell_name,
491  save_handler);
492 }
493 
494 void
495 gnc_table_model_set_pre_save_handler
496 (TableModel *model,
497  TableSaveHandler save_handler)
498 {
499  g_return_if_fail (model != NULL);
500 
501  model->pre_save_handler = save_handler;
502 }
503 
504 void
505 gnc_table_model_set_post_save_handler
506 (TableModel *model,
507  TableSaveHandler save_handler)
508 {
509  g_return_if_fail (model != NULL);
510 
511  model->post_save_handler = save_handler;
512 }
513 
514 TableSaveCellHandler
515 gnc_table_model_get_save_handler
516 (TableModel *model,
517  const char * cell_name)
518 {
519  g_return_val_if_fail (model != NULL, NULL);
520 
521  return gnc_table_model_handler_hash_lookup (model->save_handlers, cell_name);
522 }
523 
524 TableSaveHandler
525 gnc_table_model_get_pre_save_handler
526 (TableModel *model)
527 {
528  g_return_val_if_fail (model != NULL, NULL);
529 
530  return model->pre_save_handler;
531 }
532 
533 TableSaveHandler
534 gnc_table_model_get_post_save_handler
535 (TableModel *model)
536 {
537  g_return_val_if_fail (model != NULL, NULL);
538 
539  return model->post_save_handler;
540 }