GnuCash  2.6.99
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
recncell.c
1 /********************************************************************\
2  * recncell.c -- reconcile checkbox cell *
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  * recncell.c
26  *
27  * FUNCTION:
28  * Implements a mouse-click cell that allows a series
29  * of values to be clicked through.
30  *
31  * HISTORY:
32  * Copyright (c) 1998 Linas Vepstas
33  * Copyright (c) 2000 Dave Peticolas
34  * Copyright (c) 2001 Derek Atkins
35  */
36 
37 #include "config.h"
38 
39 #include <stdlib.h>
40 #include <string.h>
41 #include <time.h>
42 
43 #include "basiccell.h"
44 #include "gnc-engine.h"
45 #include "recncell.h"
46 
47 
48 static void gnc_recn_cell_set_value (BasicCell *_cell, const char *value);
49 
50 
51 static const char *
52 gnc_recn_cell_get_string (RecnCell *cell, char flag)
53 {
54  static char str[2] = { 0, 0 };
55 
56  if (cell->get_string != NULL)
57  return (cell->get_string)(flag);
58 
59  str[0] = flag;
60 
61  return str;
62 }
63 
64 static gboolean
65 gnc_recn_cell_enter (BasicCell *_cell,
66  int *cursor_position,
67  int *start_selection,
68  int *end_selection)
69 {
70  RecnCell *cell = (RecnCell *) _cell;
71  char * this_flag;
72 
73  if (cell->confirm_cb &&
74  ! (cell->confirm_cb (cell->flag, cell->confirm_data)))
75  return FALSE;
76 
77  /* Find the current flag in the list of flags */
78  this_flag = strchr (cell->flag_order, cell->flag);
79 
80  if (this_flag == NULL || *this_flag == '\0')
81  {
82  /* If it's not there (or the list is empty) use default_flag */
83  cell->flag = cell->default_flag;
84 
85  }
86  else
87  {
88  /* It is in the list -- choose the -next- item in the list (wrapping
89  * around as necessary).
90  */
91  this_flag++;
92  if (*this_flag != '\0')
93  cell->flag = *this_flag;
94  else
95  cell->flag = *(cell->flag_order);
96  }
97 
98  /* And set the display */
99  gnc_recn_cell_set_flag (cell, cell->flag);
100 
101  return FALSE;
102 }
103 
104 static void
105 gnc_recn_cell_init (RecnCell *cell)
106 {
107  gnc_basic_cell_init (&cell->cell);
108 
109  gnc_recn_cell_set_flag (cell, '\0');
110  cell->confirm_cb = NULL;
111  cell->get_string = NULL;
112  cell->valid_flags = "";
113  cell->flag_order = "";
114 
115  cell->cell.enter_cell = gnc_recn_cell_enter;
116  cell->cell.set_value = gnc_recn_cell_set_value;
117 }
118 
119 BasicCell *
120 gnc_recn_cell_new (void)
121 {
122  RecnCell * cell;
123 
124  cell = g_new0 (RecnCell, 1);
125 
126  gnc_recn_cell_init (cell);
127 
128  return &cell->cell;
129 }
130 
131 /* assumes we are given the untranslated form */
132 static void
133 gnc_recn_cell_set_value (BasicCell *_cell, const char *value)
134 {
135  RecnCell *cell = (RecnCell *) _cell;
136  char flag;
137 
138  if (!value || *value == '\0')
139  {
140  cell->flag = cell->default_flag;
141  gnc_basic_cell_set_value_internal (_cell, "");
142  return;
143  }
144 
145  flag = cell->default_flag;
146  if (strchr (cell->valid_flags, *value) != NULL)
147  flag = *value;
148 
149  gnc_recn_cell_set_flag (cell, flag);
150 }
151 
152 void
153 gnc_recn_cell_set_flag (RecnCell *cell, char flag)
154 {
155  const char *string;
156 
157  g_return_if_fail (cell != NULL);
158 
159  cell->flag = flag;
160  string = gnc_recn_cell_get_string (cell, flag);
161 
162  gnc_basic_cell_set_value_internal (&cell->cell, string);
163 }
164 
165 char
166 gnc_recn_cell_get_flag (RecnCell *cell)
167 {
168  g_return_val_if_fail (cell != NULL, '\0');
169 
170  return cell->flag;
171 }
172 
173 void
174 gnc_recn_cell_set_string_getter (RecnCell *cell,
175  RecnCellStringGetter get_string)
176 {
177  g_return_if_fail (cell != NULL);
178  cell->get_string = get_string;
179 }
180 
181 void
182 gnc_recn_cell_set_confirm_cb (RecnCell *cell, RecnCellConfirm confirm_cb,
183  gpointer data)
184 {
185  g_return_if_fail (cell != NULL);
186 
187  cell->confirm_cb = confirm_cb;
188  cell->confirm_data = data;
189 }
190 
191 void
192 gnc_recn_cell_set_valid_flags (RecnCell *cell, const char *flags,
193  char default_flag)
194 {
195  g_return_if_fail (cell != NULL);
196  g_return_if_fail (flags != NULL);
197 
198  cell->valid_flags = (char *)flags;
199  cell->default_flag = default_flag;
200 }
201 
202 void
203 gnc_recn_cell_set_flag_order (RecnCell *cell, const char *flags)
204 {
205  g_return_if_fail (cell != NULL);
206  g_return_if_fail (flags != NULL);
207 
208  cell->flag_order = (char *)flags;
209 }
All type declarations for the whole Gnucash engine.