GnuCash  5.6-150-g038405b370+
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 gnu@gnu.org *
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  if (cell->read_only == TRUE)
78  return FALSE;
79 
80  /* Find the current flag in the list of flags */
81  this_flag = strchr (cell->flag_order, cell->flag);
82 
83  if (this_flag == NULL || *this_flag == '\0')
84  {
85  /* If it's not there (or the list is empty) use default_flag */
86  cell->flag = cell->default_flag;
87 
88  }
89  else
90  {
91  /* It is in the list -- choose the -next- item in the list (wrapping
92  * around as necessary).
93  */
94  this_flag++;
95  if (*this_flag != '\0')
96  cell->flag = *this_flag;
97  else
98  cell->flag = *(cell->flag_order);
99  }
100 
101  /* And set the display */
102  gnc_recn_cell_set_flag (cell, cell->flag);
103 
104  return FALSE;
105 }
106 
107 static void
108 gnc_recn_cell_init (RecnCell *cell)
109 {
110  gnc_basic_cell_init (&cell->cell);
111 
112  gnc_recn_cell_set_flag (cell, '\0');
113  cell->confirm_cb = NULL;
114  cell->get_string = NULL;
115  cell->valid_flags = "";
116  cell->flag_order = "";
117  cell->read_only = FALSE;
118 
119  cell->cell.enter_cell = gnc_recn_cell_enter;
120  cell->cell.set_value = gnc_recn_cell_set_value;
121 }
122 
123 BasicCell *
124 gnc_recn_cell_new (void)
125 {
126  RecnCell * cell;
127 
128  cell = g_new0 (RecnCell, 1);
129 
130  gnc_recn_cell_init (cell);
131 
132  return &cell->cell;
133 }
134 
135 /* assumes we are given the untranslated form */
136 static void
137 gnc_recn_cell_set_value (BasicCell *_cell, const char *value)
138 {
139  RecnCell *cell = (RecnCell *) _cell;
140  char flag;
141 
142  if (!value || *value == '\0')
143  {
144  cell->flag = cell->default_flag;
145  gnc_basic_cell_set_value_internal (_cell, "");
146  return;
147  }
148 
149  flag = cell->default_flag;
150  if (strchr (cell->valid_flags, *value) != NULL)
151  flag = *value;
152 
153  gnc_recn_cell_set_flag (cell, flag);
154 }
155 
156 void
157 gnc_recn_cell_set_flag (RecnCell *cell, char flag)
158 {
159  const char *string;
160 
161  g_return_if_fail (cell != NULL);
162 
163  cell->flag = flag;
164  string = gnc_recn_cell_get_string (cell, flag);
165 
166  gnc_basic_cell_set_value_internal (&cell->cell, string);
167 }
168 
169 char
170 gnc_recn_cell_get_flag (RecnCell *cell)
171 {
172  g_return_val_if_fail (cell != NULL, '\0');
173 
174  return cell->flag;
175 }
176 
177 void
178 gnc_recn_cell_set_string_getter (RecnCell *cell,
179  RecnCellStringGetter get_string)
180 {
181  g_return_if_fail (cell != NULL);
182  cell->get_string = get_string;
183 }
184 
185 void
186 gnc_recn_cell_set_confirm_cb (RecnCell *cell, RecnCellConfirm confirm_cb,
187  gpointer data)
188 {
189  g_return_if_fail (cell != NULL);
190 
191  cell->confirm_cb = confirm_cb;
192  cell->confirm_data = data;
193 }
194 
195 void
196 gnc_recn_cell_set_valid_flags (RecnCell *cell, const char *flags,
197  char default_flag)
198 {
199  g_return_if_fail (cell != NULL);
200  g_return_if_fail (flags != NULL);
201 
202  cell->valid_flags = (char *)flags;
203  cell->default_flag = default_flag;
204 }
205 
206 void
207 gnc_recn_cell_set_flag_order (RecnCell *cell, const char *flags)
208 {
209  g_return_if_fail (cell != NULL);
210  g_return_if_fail (flags != NULL);
211 
212  cell->flag_order = (char *)flags;
213 }
214 
215 void
216 gnc_recn_cell_set_read_only (RecnCell *cell, gboolean read_only)
217 {
218  g_return_if_fail (cell != NULL);
219  cell->read_only = read_only;
220 }
void gnc_recn_cell_set_valid_flags(RecnCell *cell, const char *flags, char default_flag)
note that chars is copied into the RecnCell directly, but remains the "property" of the caller...
Definition: recncell.c:196
The RecnCell object implements a cell handler that will cycle through a series of single-character va...
Definition: recncell.h:47
char default_flag
Automatic flag selection order.
Definition: recncell.h:55
RecnCellStringGetter get_string
Default flag for unknown user input.
Definition: recncell.h:57
char * flag_order
The list of valid flags.
Definition: recncell.h:54
char * valid_flags
The actual flag value.
Definition: recncell.h:53
All type declarations for the whole Gnucash engine.