import homebank-5.1.2
[chaz/homebank] / src / ui-assist-start.c
1 /* HomeBank -- Free, easy, personal accounting for everyone.
2 * Copyright (C) 1995-2016 Maxime DOYEN
3 *
4 * This file is part of HomeBank.
5 *
6 * HomeBank is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * HomeBank 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "homebank.h"
21
22 #include "ui-assist-start.h"
23 #include "dsp_mainwindow.h"
24 #include "ui-currency.h"
25
26
27 #define MYDEBUG 0
28
29 #if MYDEBUG
30 #define DB(x) (x);
31 #else
32 #define DB(x);
33 #endif
34
35 /* our global datas */
36 extern struct HomeBank *GLOBALS;
37
38 extern gchar *CYA_ACC_TYPE[];
39
40 /* = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = */
41
42
43 static void
44 on_assistant_apply (GtkWidget *widget, gpointer user_data)
45 {
46 struct assist_start_data *data = user_data;
47 Account *item;
48 gdouble value;
49
50 /* set owner */
51 gchar *owner = (gchar *)gtk_entry_get_text(GTK_ENTRY(data->ST_owner));
52 if (owner && *owner)
53 {
54 hbfile_change_owner(g_strdup(owner));
55 GLOBALS->changes_count++;
56 }
57
58 if( data->curfmt != NULL )
59 {
60 hbfile_replace_basecurrency(data->curfmt);
61 }
62
63 /* load preset categories */
64 if(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(data->CM_load)))
65 {
66 if(data->pathfilename != NULL)
67 {
68 gchar *error;
69 category_load_csv(data->pathfilename, &error);
70 //DB( g_print(" -> loaded=%d\n", ok) );
71 }
72 }
73
74 /* initialise an account */
75 item = da_acc_malloc();
76
77 gchar *txt = (gchar *)gtk_entry_get_text(GTK_ENTRY(data->ST_name));
78 if (txt && *txt)
79 {
80 item->name = g_strdup(txt);
81 }
82
83 item->kcur = GLOBALS->kcur;
84
85 item->type = gtk_combo_box_get_active(GTK_COMBO_BOX(data->CY_type));
86
87 item->number = g_strdup(gtk_entry_get_text(GTK_ENTRY(data->ST_number)));
88
89 gtk_spin_button_update(GTK_SPIN_BUTTON(data->ST_initial));
90 value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(data->ST_initial));
91 item->initial = value;
92
93 gtk_spin_button_update(GTK_SPIN_BUTTON(data->ST_minimum));
94 value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(data->ST_minimum));
95 item->minimum = value;
96
97 da_acc_append(item);
98 GLOBALS->changes_count++;
99
100 //our global list has changed, so update the treeview
101 ui_mainwindow_update(GLOBALS->mainwindow, GINT_TO_POINTER(UF_TITLE+UF_SENSITIVE+UF_BALANCE+UF_REFRESHALL));
102
103 }
104
105 static void
106 on_assistant_close_cancel (GtkWidget *widget, gpointer user_data)
107 {
108 struct assist_start_data *data = user_data;
109
110 //data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
111 gtk_widget_destroy (data->window);
112
113 g_free(data->pathfilename);
114
115 g_free(data);
116
117 }
118
119 static void
120 on_assistant_prepare (GtkWidget *widget, GtkWidget *page, gpointer user_data)
121 {
122 struct assist_start_data *data = user_data;
123 gint current_page, n_pages;
124 gchar *title;
125
126 current_page = gtk_assistant_get_current_page (GTK_ASSISTANT (widget));
127 n_pages = gtk_assistant_get_n_pages (GTK_ASSISTANT (widget));
128
129 title = g_strdup_printf (_("New HomeBank file (%d of %d)"), current_page + 1, n_pages);
130 gtk_window_set_title (GTK_WINDOW (widget), title);
131 g_free (title);
132
133
134 switch( current_page )
135 {
136 case 1:
137 {
138 gchar **langs = (gchar **)g_get_language_names ();
139 gchar *txt = g_strjoinv(", ", langs);
140
141 DB( g_print("%s\n", txt) );;
142
143 gtk_label_set_label(GTK_LABEL(data->TX_lang), txt);
144 g_free(txt);
145
146
147 gchar *lang;
148 data->pathfilename = category_find_preset(&lang);
149 if(data->pathfilename != NULL)
150 {
151 gtk_label_set_label(GTK_LABEL(data->TX_file), lang);
152 gtk_widget_show(data->CM_load);
153 gtk_widget_show(data->ok_image);
154 gtk_widget_hide(data->ko_image);
155 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(data->CM_load), TRUE);
156 }
157 else
158 {
159 gtk_widget_hide(data->CM_load);
160 gtk_label_set_label(GTK_LABEL(data->TX_file), _("Not found"));
161 gtk_widget_show(data->ko_image);
162 gtk_widget_hide(data->ok_image);
163 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(data->CM_load), FALSE);
164 }
165
166
167 }
168 break;
169
170
171 }
172
173 }
174
175 static void
176 on_entry_changed (GtkWidget *widget, gpointer data)
177 {
178 GtkAssistant *assistant = GTK_ASSISTANT (data);
179 GtkWidget *current_page;
180 gint page_number;
181 const gchar *text;
182
183 page_number = gtk_assistant_get_current_page (assistant);
184 current_page = gtk_assistant_get_nth_page (assistant, page_number);
185 text = gtk_entry_get_text (GTK_ENTRY (widget));
186
187 if (text && *text)
188 gtk_assistant_set_page_complete (assistant, current_page, TRUE);
189 else
190 gtk_assistant_set_page_complete (assistant, current_page, FALSE);
191 }
192
193
194 static void ui_start_assistant_property_change_action(GtkWidget *widget, gpointer user_data)
195 {
196 struct assist_start_data *data;
197 Currency4217 *curfmt;
198
199 DB( g_print("\n[ui-start] property_change_action\n") );
200
201 data = g_object_get_data(G_OBJECT(gtk_widget_get_ancestor(widget, GTK_TYPE_WINDOW)), "inst_data");
202
203 data->curfmt = NULL;
204
205 curfmt = ui_cur_select_dialog_new(GTK_WINDOW(data->window), CUR_SELECT_MODE_BASE);
206 if( curfmt != NULL )
207 {
208 gchar label[128];
209 gchar *name;
210
211 DB( g_printf("- user selected: '%s' '%s'\n", curfmt->curr_iso_code, curfmt->name) );
212
213 data->curfmt = curfmt;
214
215 name = curfmt->name;
216
217 g_snprintf(label, 127, "%s - %s", curfmt->curr_iso_code, name);
218 gtk_label_set_text (GTK_LABEL(data->LB_currency), label);
219 }
220 }
221
222
223 static void
224 ui_start_assistant_property_fill (GtkWidget *assistant, struct assist_start_data *data)
225 {
226 Currency *cur;
227 gchar label[128];
228
229 gtk_entry_set_text(GTK_ENTRY(data->ST_owner), g_get_real_name ());
230
231 cur = da_cur_get (GLOBALS->kcur);
232
233 g_snprintf(label, 127, "%s - %s", cur->iso_code, cur->name);
234 gtk_label_set_text (GTK_LABEL(data->LB_currency), label);
235
236 }
237
238
239 static void
240 ui_start_assistant_property_create (GtkWidget *assistant, struct assist_start_data *data)
241 {
242 GtkWidget *table, *label, *widget;
243 gint row;
244
245 table = gtk_grid_new ();
246 gtk_grid_set_row_spacing (GTK_GRID (table), SPACING_SMALL);
247 gtk_grid_set_column_spacing (GTK_GRID (table), SPACING_MEDIUM);
248 //gtk_container_set_border_width (GTK_CONTAINER (table), SPACING_MEDIUM);
249 gtk_widget_set_valign (table, GTK_ALIGN_CENTER);
250
251 row = 0;
252 label = make_label_widget(_("_Owner:"));
253 gtk_grid_attach (GTK_GRID (table), label, 1, row, 1, 1);
254 widget = make_string(label);
255 data->ST_owner = widget;
256 gtk_widget_set_hexpand(widget, TRUE);
257 gtk_grid_attach (GTK_GRID (table), widget, 2, row, 2, 1);
258
259 row++;
260 label = make_label_widget(_("Currency:"));
261 gtk_grid_attach (GTK_GRID (table), label, 1, row, 1, 1);
262 widget = make_label (NULL, 0, 0.5);
263 data->LB_currency = widget;
264 gtk_grid_attach (GTK_GRID (table), widget, 2, row, 1, 1);
265 widget = gtk_button_new_with_mnemonic (_("_Change"));
266 data->BT_change = widget;
267 gtk_grid_attach (GTK_GRID (table), widget, 3, row, 1, 1);
268
269 g_signal_connect (G_OBJECT (data->ST_owner), "changed", G_CALLBACK (on_entry_changed), assistant);
270 g_signal_connect (G_OBJECT (data->BT_change), "clicked", G_CALLBACK (ui_start_assistant_property_change_action), data);
271
272
273 gtk_widget_show_all (table);
274 gtk_assistant_append_page (GTK_ASSISTANT (assistant), table);
275
276 gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), table, _("File properties"));
277 gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), table, GTK_ASSISTANT_PAGE_INTRO);
278
279 }
280
281 static void
282 ui_start_assistant_create_page2 (GtkWidget *assistant, struct assist_start_data *data)
283 {
284 GtkWidget *box, *hbox, *label, *table, *widget;
285 gint row;
286
287 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, SPACING_SMALL);
288 gtk_container_set_border_width (GTK_CONTAINER (box), SPACING_MEDIUM);
289
290 table = gtk_grid_new ();
291 //gtk_container_set_border_width (GTK_CONTAINER (table), SP_BORDER);
292 gtk_grid_set_row_spacing (GTK_GRID (table), SPACING_SMALL*2);
293 gtk_grid_set_column_spacing (GTK_GRID (table), SPACING_MEDIUM*2);
294
295 gtk_box_pack_start (GTK_BOX (box), table, TRUE, TRUE, 0);
296
297 row = 0;
298 label = make_label_group(_("System detection"));
299 gtk_grid_attach (GTK_GRID (table), label, 0, row, 3, 1);
300
301 row++;
302 label = make_label_widget(_("Languages:"));
303 gtk_grid_attach (GTK_GRID (table), label, 1, row, 1, 1);
304 widget = make_label(NULL, 0.0, 0.5);
305 data->TX_lang = widget;
306 gtk_grid_attach (GTK_GRID (table), widget, 2, row, 1, 1);
307
308 row++;
309 label = make_label_widget(_("Preset file:"));
310 gtk_grid_attach (GTK_GRID (table), label, 1, row, 1, 1);
311 hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, SPACING_SMALL);
312 gtk_grid_attach (GTK_GRID (table), hbox, 2, row, 1, 1);
313
314 widget = gtk_image_new_from_icon_name(ICONNAME_HB_FILE_VALID, GTK_ICON_SIZE_LARGE_TOOLBAR);
315 data->ok_image = widget;
316 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
317
318 widget = gtk_image_new_from_icon_name(ICONNAME_HB_FILE_INVALID, GTK_ICON_SIZE_LARGE_TOOLBAR);
319 data->ko_image = widget;
320 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
321
322 widget = make_label(NULL, 0.0, 0.5);
323 data->TX_file = widget;
324 gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
325
326 row++;
327 widget = gtk_check_button_new_with_mnemonic (_("Initialize my categories with this file"));
328 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(widget), TRUE);
329 data->CM_load = widget;
330 gtk_grid_attach (GTK_GRID (table), widget, 1, row, 3, 1);
331
332 gtk_widget_show_all (box);
333
334 gtk_widget_hide(data->ok_image);
335 gtk_widget_hide(data->ko_image);
336
337 gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);
338 gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), box, TRUE);
339 gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, _("Preset categories"));
340
341 }
342
343 static void
344 ui_start_assistant_create_page3 (GtkWidget *assistant, struct assist_start_data *data)
345 {
346 GtkWidget *box, *label, *widget, *table;
347 gint row;
348
349 box = gtk_box_new (GTK_ORIENTATION_VERTICAL, SPACING_SMALL);
350 gtk_container_set_border_width (GTK_CONTAINER (box), SPACING_MEDIUM);
351
352
353 table = gtk_grid_new ();
354 //gtk_container_set_border_width (GTK_CONTAINER (table), SP_BORDER);
355 gtk_grid_set_row_spacing (GTK_GRID (table), SPACING_SMALL);
356 gtk_grid_set_column_spacing (GTK_GRID (table), SPACING_MEDIUM);
357 gtk_box_pack_start (GTK_BOX (box), table, TRUE, TRUE, 0);
358
359 row = 0;
360 label = make_label_group(_("Informations"));
361 gtk_grid_attach (GTK_GRID (table), label, 0, row, 3, 1);
362
363 row++;
364 label = make_label_widget(_("_Name:"));
365 gtk_grid_attach (GTK_GRID (table), label, 1, row, 1, 1);
366 widget = make_string(label);
367 data->ST_name = widget;
368 gtk_widget_set_hexpand(widget, TRUE);
369 gtk_grid_attach (GTK_GRID (table), widget, 2, row, 1, 1);
370
371 g_signal_connect (G_OBJECT (widget), "changed",
372 G_CALLBACK (on_entry_changed), assistant);
373
374
375 row++;
376 label = make_label_widget(_("_Type:"));
377 gtk_grid_attach (GTK_GRID (table), label, 1, row, 1, 1);
378 widget = make_cycle(label, CYA_ACC_TYPE);
379 data->CY_type = widget;
380 gtk_widget_set_hexpand(widget, TRUE);
381 gtk_grid_attach (GTK_GRID (table), widget, 2, row, 1, 1);
382
383 row++;
384 label = make_label_widget(_("N_umber:"));
385 gtk_grid_attach (GTK_GRID (table), label, 1, row, 1, 1);
386 widget = make_string(label);
387 data->ST_number = widget;
388 gtk_widget_set_hexpand(widget, TRUE);
389 gtk_grid_attach (GTK_GRID (table), widget, 2, row, 1, 1);
390
391 //other
392
393 //row = 0;
394 row++;
395 label = make_label_group(_("Balances"));
396 gtk_grid_attach (GTK_GRID (table), label, 0, row, 3, 1);
397
398 row++;
399 label = make_label_widget(_("_Initial:"));
400 gtk_grid_attach (GTK_GRID (table), label, 1, row, 1, 1);
401 widget = make_amount(label);
402 data->ST_initial = widget;
403 gtk_grid_attach (GTK_GRID (table), widget, 2, row, 1, 1);
404
405 row++;
406 label = make_label_widget(_("_Overdrawn at:"));
407 gtk_grid_attach (GTK_GRID (table), label, 1, row, 1, 1);
408 widget = make_amount(label);
409 data->ST_minimum = widget;
410 gtk_grid_attach (GTK_GRID (table), widget, 2, row, 1, 1);
411
412 gtk_widget_show_all (box);
413 gtk_assistant_append_page (GTK_ASSISTANT (assistant), box);
414
415 gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), box, _("Create an account"));
416 }
417
418
419 static void
420 ui_start_assistant_create_page4 (GtkWidget *assistant, struct assist_start_data *data)
421 {
422 GtkWidget *label;
423
424
425 label = gtk_label_new (_("This is a confirmation page, press 'Apply' to apply changes"));
426
427 gtk_widget_show (label);
428 gtk_assistant_append_page (GTK_ASSISTANT (assistant), label);
429 gtk_assistant_set_page_type (GTK_ASSISTANT (assistant), label, GTK_ASSISTANT_PAGE_CONFIRM);
430 gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), label, TRUE);
431 gtk_assistant_set_page_title (GTK_ASSISTANT (assistant), label, _("Confirmation"));
432
433
434 }
435
436
437
438
439
440 GtkWidget*
441 ui_start_assistant (void)
442 {
443 struct assist_start_data *data;
444 GtkWidget *assistant, *page;
445
446 data = g_malloc0(sizeof(struct assist_start_data));
447 if(!data) return NULL;
448
449 assistant = gtk_assistant_new ();
450 data->window = assistant;
451
452 //store our window private data
453 g_object_set_data(G_OBJECT(assistant), "inst_data", (gpointer)data);
454 //DB( g_print("** (import) window=%x, inst_data=%x\n", assistant, data) );
455
456 gtk_window_set_default_size (GTK_WINDOW (assistant), 400, -1);
457
458 gtk_window_set_modal(GTK_WINDOW (assistant), TRUE);
459 gtk_window_set_transient_for(GTK_WINDOW(assistant), GTK_WINDOW(GLOBALS->mainwindow));
460
461 ui_start_assistant_property_create (assistant, data);
462 ui_start_assistant_create_page2 (assistant, data);
463 ui_start_assistant_create_page3 (assistant, data);
464 ui_start_assistant_create_page4 (assistant, data);
465
466 ui_start_assistant_property_fill(assistant, data);
467
468 page = gtk_assistant_get_nth_page(GTK_ASSISTANT (assistant), 0);
469 gtk_assistant_set_page_complete (GTK_ASSISTANT (assistant), page, TRUE);
470
471
472 g_signal_connect (G_OBJECT (assistant), "cancel",
473 G_CALLBACK (on_assistant_close_cancel), data);
474 g_signal_connect (G_OBJECT (assistant), "close",
475 G_CALLBACK (on_assistant_close_cancel), data);
476 g_signal_connect (G_OBJECT (assistant), "apply",
477 G_CALLBACK (on_assistant_apply), data);
478 g_signal_connect (G_OBJECT (assistant), "prepare",
479 G_CALLBACK (on_assistant_prepare), data);
480
481 gtk_widget_show (assistant);
482
483 return assistant;
484 }
This page took 0.067674 seconds and 5 git commands to generate.