1 /* ************************************************************************** */
2 /* */
3 /* Copyright (C) 2000-2008 Cédric Auger (cedric@grisbi.org) */
4 /* 2003-2008 Benjamin Drieu (bdrieu@april.org) */
5 /* https://www.grisbi.org/ */
6 /* */
7 /* This program is free software; you can redistribute it and/or modify */
8 /* it under the terms of the GNU General Public License as published by */
9 /* the Free Software Foundation; either version 2 of the License, or */
10 /* (at your option) any later version. */
11 /* */
12 /* This program is distributed in the hope that it will be useful, */
13 /* but WITHOUT ANY WARRANTY; without even the implied warranty of */
14 /* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
15 /* GNU General Public License for more details. */
16 /* */
17 /* You should have received a copy of the GNU General Public License */
18 /* along with this program; if not, write to the Free Software */
19 /* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
20 /* */
21 /* ************************************************************************** */
22
23 /**
24 * \file gsb_fyear.c
25 * contains tools to work with the financial years
26 */
27
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include "include.h"
34 #include <glib/gi18n.h>
35
36 /*START_INCLUDE*/
37 #include "gsb_fyear.h"
38 #include "grisbi_app.h"
39 #include "gsb_data_fyear.h"
40 #include "structures.h"
41 /*END_INCLUDE*/
42
43
44 /**
45 * this is a tree model filter with 3 columns :
46 * the name, the number and a boolean to show it or not
47 * */
48 static GtkTreeModel *fyear_model;
49
50 /**
51 * this is a tree model filter from fyear_model_filter which
52 * show only the financial years which must be showed
53 * */
54 static GtkTreeModel *fyear_model_filter;
55
56
57 /*START_STATIC*/
58 static gboolean gsb_fyear_create_combobox_store ( void );
59 /*END_STATIC*/
60
61 /*START_EXTERN*/
62 /*END_EXTERN*/
63
64
65
66 /**
67 * set to NULL the static variables
68 *
69 * \param
70 *
71 * \return
72 * */
gsb_fyear_init_variables(void)73 void gsb_fyear_init_variables ( void )
74 {
75 if (fyear_model_filter
76 &&
77 GTK_IS_LIST_STORE (fyear_model))
78 gtk_list_store_clear (GTK_LIST_STORE (fyear_model));
79
80 fyear_model = NULL;
81 fyear_model_filter = NULL;
82 }
83
84
85 /**
86 * create and return a combobox with the financial years
87 *
88 * \param set_automatic if TRUE, will show the choice "Automatic"
89 *
90 * \return a widget combobox or NULL
91 * */
gsb_fyear_make_combobox(gboolean set_automatic)92 GtkWidget *gsb_fyear_make_combobox ( gboolean set_automatic )
93 {
94 GtkWidget *combo_box;
95
96 if (!fyear_model_filter)
97 gsb_fyear_create_combobox_store ();
98
99 combo_box = gsb_fyear_make_combobox_new ( fyear_model_filter, set_automatic );
100
101 return (combo_box);
102 }
103
104
105 /**
106 * create and return a combobox with the financial years
107 *
108 * \param model for the combobox
109 * \param set_automatic if TRUE, will show the choice "Automatic"
110 *
111 * \return a widget combobox or NULL
112 * */
gsb_fyear_make_combobox_new(GtkTreeModel * model,gboolean set_automatic)113 GtkWidget *gsb_fyear_make_combobox_new ( GtkTreeModel *model,
114 gboolean set_automatic )
115 {
116 GtkCellRenderer *renderer;
117 GtkWidget *combo_box;
118
119 combo_box = gtk_combo_box_new_with_model ( GTK_TREE_MODEL ( model ) );
120
121 renderer = gtk_cell_renderer_text_new ();
122 gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo_box), renderer, TRUE);
123 gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo_box),
124 renderer,
125 "text", FYEAR_COL_NAME,
126 NULL);
127
128 /* show or hide the automatic line */
129 gsb_fyear_set_automatic ( set_automatic );
130 gtk_combo_box_set_active ( GTK_COMBO_BOX (combo_box), 0 );
131
132 return (combo_box);
133 }
134
135
136 /**
137 * set the combobox on the fyear given in param
138 * if the fyear exists but is not showed normally, we show it because it's for
139 * a modification of a transaction (when the form will be freed, that fyear won't be showed again)
140 *
141 * \combo_box the combo-box to set
142 * \fyear_number the fyear we want to set on the combo-box, if 0, will be set on "Automatic"
143 *
144 * \return TRUE fyear found, FALSE fyear not found, nothing change
145 * */
gsb_fyear_set_combobox_history(GtkWidget * combo_box,gint fyear_number)146 gboolean gsb_fyear_set_combobox_history ( GtkWidget *combo_box,
147 gint fyear_number )
148 {
149 return gsb_fyear_select_iter_by_number ( combo_box,
150 fyear_model,
151 fyear_model_filter,
152 fyear_number );
153 }
154
155
156 /**
157 * Get and return the number of the fyear in the combobox given
158 * in param
159 * if the fyear is 0, try to find a fyear with the givent date
160 *
161 * \param combo_box a combo_box with the financials years
162 * \param date date to find a corresponding fyear if the combobox is on Automatic or NULL
163 *
164 * \return the number of fyear or 0 if problem
165 * */
gsb_fyear_get_fyear_from_combobox(GtkWidget * combo_box,const GDate * date)166 gint gsb_fyear_get_fyear_from_combobox ( GtkWidget *combo_box,
167 const GDate *date )
168 {
169 gint fyear_number = 0;
170 GtkTreeIter iter;
171
172 if ( !fyear_model_filter )
173 gsb_fyear_create_combobox_store ( );
174
175 if ( gtk_combo_box_get_active_iter ( GTK_COMBO_BOX (combo_box), &iter ) )
176 {
177
178 gtk_tree_model_get ( GTK_TREE_MODEL (fyear_model_filter),
179 &iter,
180 FYEAR_COL_NUMBER, &fyear_number,
181 -1 );
182 if ( !fyear_number && date )
183 fyear_number = gsb_data_fyear_get_from_date ( date );
184 }
185
186 return fyear_number;
187 }
188
189
190 /**
191 * show or hide the Automatic line in the fyear list
192 * this will apply to all fyears combobox
193 *
194 * \param set_automatic TRUE to show the "Automatic" line, FALSE to hide it
195 *
196 *
197 * \return TRUE done, FALSE problem
198 * */
gsb_fyear_set_automatic(gboolean set_automatic)199 gboolean gsb_fyear_set_automatic ( gboolean set_automatic )
200 {
201 GtkTreeIter iter;
202
203 if (!fyear_model)
204 gsb_fyear_create_combobox_store ();
205
206 if (!gtk_tree_model_get_iter_first ( GTK_TREE_MODEL (fyear_model),
207 &iter ))
208 return FALSE;
209
210 gtk_list_store_set ( GTK_LIST_STORE (fyear_model),
211 &iter,
212 FYEAR_COL_VIEW, set_automatic,
213 -1 );
214 return TRUE;
215 }
216
217
218 /**
219 * update the list of the financial years, which change all
220 * the current combobox content
221 * set the first row with Automatic with 0 as number
222 *
223 * \param
224 *
225 * \return FALSE
226 */
gsb_fyear_update_fyear_list(void)227 gboolean gsb_fyear_update_fyear_list ( void )
228 {
229 gsb_fyear_update_fyear_list_new ( fyear_model, fyear_model_filter, "Automatic" );
230
231 return FALSE;
232 }
233
234
235 /**
236 * update the list of the financial years, which change all
237 * the current combobox content
238 * set the first row with title with 0 as number
239 *
240 * \param model
241 * \param model_filter
242 * \param title
243 *
244 * \return FALSE
245 */
gsb_fyear_update_fyear_list_new(GtkTreeModel * model,GtkTreeModel * model_filter,const gchar * title)246 gboolean gsb_fyear_update_fyear_list_new ( GtkTreeModel *model,
247 GtkTreeModel *model_filter,
248 const gchar *title )
249 {
250 GSList *copy_list = NULL;
251 GSList *tmp_list;
252 GtkTreeIter iter;
253 GrisbiAppConf *a_conf;
254
255 /* if no filter, thats because not created, but don't create here
256 * because we can come here without needed of fyear button */
257 if ( !model_filter )
258 return FALSE;
259
260 a_conf = (GrisbiAppConf *) grisbi_app_get_a_conf ();
261 gtk_list_store_clear (GTK_LIST_STORE ( model ) );
262
263 /* put at the beginning title */
264 gtk_list_store_append ( GTK_LIST_STORE ( model ), &iter );
265 gtk_list_store_set ( GTK_LIST_STORE ( model ),
266 &iter,
267 FYEAR_COL_NAME, _(title),
268 FYEAR_COL_NUMBER, 0,
269 FYEAR_COL_VIEW, TRUE,
270 -1 );
271
272 /* fill the list with the copy of fyears_list */
273 tmp_list = gsb_data_fyear_get_fyears_list ();
274 while (tmp_list)
275 {
276 FyearStruct *fyear;
277
278 fyear = tmp_list ->data;
279 copy_list = g_slist_insert_sorted_with_data (copy_list,
280 fyear,
281 (GCompareDataFunc) gsb_data_fyear_compare_from_struct,
282 GINT_TO_POINTER (a_conf->fyear_combobox_sort_order));
283
284 tmp_list = tmp_list->next;
285 }
286
287 tmp_list = copy_list;
288 while (tmp_list)
289 {
290 gint fyear_number;
291
292 fyear_number = gsb_data_fyear_get_no_fyear (tmp_list->data);
293
294 gtk_list_store_append ( GTK_LIST_STORE ( model ), &iter );
295 gtk_list_store_set ( GTK_LIST_STORE ( model ),
296 &iter,
297 FYEAR_COL_NAME, gsb_data_fyear_get_name ( fyear_number ),
298 FYEAR_COL_NUMBER, fyear_number,
299 FYEAR_COL_VIEW, gsb_data_fyear_get_form_show ( fyear_number ),
300 -1 );
301 tmp_list = tmp_list->next;
302 }
303 g_slist_free (copy_list);
304
305 return FALSE;
306 }
307
308
309 /**
310 * create and fill the list store of the fyear
311 * come here mean that fyear_model_filter is NULL
312 *
313 * \param
314 *
315 * \return TRUE ok, FALSE problem
316 * */
gsb_fyear_create_combobox_store(void)317 gboolean gsb_fyear_create_combobox_store ( void )
318 {
319 /* the fyear list store, contains 3 columns :
320 * FYEAR_COL_NAME : the name of the fyear
321 * FYEAR_COL_NUMBER : the number of the fyear
322 * FYEAR_COL_VIEW : it tha fyear should be showed */
323
324 fyear_model = GTK_TREE_MODEL ( gtk_list_store_new ( 3,
325 G_TYPE_STRING,
326 G_TYPE_INT,
327 G_TYPE_BOOLEAN ));
328 fyear_model_filter = gtk_tree_model_filter_new ( fyear_model, NULL );
329 gtk_tree_model_filter_set_visible_column ( GTK_TREE_MODEL_FILTER ( fyear_model_filter ),
330 FYEAR_COL_VIEW );
331 gsb_fyear_update_fyear_list ( );
332
333 return TRUE;
334 }
335
336
337 /**
338 *
339 *
340 *
341 *
342 * */
gsb_fyear_hide_iter_by_name(GtkTreeModel * model,gchar * name)343 gboolean gsb_fyear_hide_iter_by_name ( GtkTreeModel *model, gchar *name )
344 {
345 GtkTreeIter iter;
346 gint result;
347
348 result = gtk_tree_model_get_iter_first ( GTK_TREE_MODEL ( model ),
349 &iter );
350 while (result)
351 {
352 gchar *value;
353 gboolean show;
354
355 gtk_tree_model_get ( GTK_TREE_MODEL ( model ),
356 &iter,
357 FYEAR_COL_NAME, &value,
358 FYEAR_COL_VIEW, &show,
359 -1 );
360
361 if ( g_utf8_collate ( value, name ) == 0 )
362 {
363 if ( show )
364 gtk_list_store_set ( GTK_LIST_STORE ( model ),
365 &iter,
366 FYEAR_COL_VIEW, FALSE,
367 -1 );
368 return TRUE;
369 }
370 result = gtk_tree_model_iter_next ( GTK_TREE_MODEL ( model ),
371 &iter );
372 }
373 return FALSE;
374 }
375
376
377 /**
378 * set the combobox on the fyear given in param
379 * if the fyear exists but is not showed normally, we show it because it's for
380 * a modification of a transaction (when the form will be freed, that fyear won't be showed again)
381 *
382 * \combo_box the combo-box to set
383 * \tree_model
384 * \tree_model_filter
385 * \fyear_number the fyear we want to set on the combo-box, if 0, will be set on "Automatic"
386 *
387 * \return TRUE fyear found, FALSE fyear not found, nothing change
388 * */
gsb_fyear_select_iter_by_number(GtkWidget * combo_box,GtkTreeModel * model,GtkTreeModel * model_filter,gint fyear_number)389 gboolean gsb_fyear_select_iter_by_number ( GtkWidget *combo_box,
390 GtkTreeModel *model,
391 GtkTreeModel *model_filter,
392 gint fyear_number )
393 {
394 GtkTreeIter iter;
395 gint result;
396
397 if ( !combo_box )
398 return FALSE;
399
400 if ( !model )
401 return FALSE;
402
403 /* we look for the fyear in the model and not in the filter
404 * because the fyear may not be visible */
405 result = gtk_tree_model_get_iter_first ( GTK_TREE_MODEL ( model ),
406 &iter );
407 while (result)
408 {
409 gint value;
410 gboolean show;
411
412 gtk_tree_model_get ( GTK_TREE_MODEL ( model ),
413 &iter,
414 FYEAR_COL_NUMBER, &value,
415 FYEAR_COL_VIEW, &show,
416 -1 );
417
418 if ( value == fyear_number )
419 {
420 GtkTreeIter child_iter;
421
422 /* if normally not showed, we show it now and later
423 * it will be back to show */
424 if ( !show )
425 gtk_list_store_set ( GTK_LIST_STORE ( model ),
426 &iter,
427 FYEAR_COL_VIEW, TRUE,
428 -1 );
429
430 /* as we were in the model and not the filter, we need to change the iter */
431 gtk_tree_model_filter_convert_child_iter_to_iter (
432 GTK_TREE_MODEL_FILTER ( model_filter ),
433 &child_iter,
434 &iter );
435 gtk_combo_box_set_active_iter ( GTK_COMBO_BOX ( combo_box ),
436 &child_iter );
437
438 return TRUE;
439 }
440 result = gtk_tree_model_iter_next ( GTK_TREE_MODEL ( model ),
441 &iter );
442 }
443 return FALSE;
444 }
445
446
447 /* Local Variables: */
448 /* c-basic-offset: 4 */
449 /* End: */
450