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