-
Notifications
You must be signed in to change notification settings - Fork 762
/
gnc-main-window.c
5022 lines (4368 loc) · 165 KB
/
gnc-main-window.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* gnc-main-window.c -- GtkWindow which represents the
* GnuCash main window.
*
* Copyright (C) 2003 Jan Arne Petersen <jpetersen@uni-bonn.de>
* Copyright (C) 2003,2005,2006 David Hampton <hampton@employees.org>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, contact:
*
* Free Software Foundation Voice: +1-617-542-5942
* 51 Franklin Street, Fifth Floor Fax: +1-617-542-2652
* Boston, MA 02110-1301, USA gnu@gnu.org
*/
/** @addtogroup Windows
@{ */
/** @addtogroup GncMainWindow Main Window functions.
@{ */
/** @file gnc-main-window.c
@brief Functions for adding content to a window.
@author Copyright (C) 2003 Jan Arne Petersen <jpetersen@uni-bonn.de>
@author Copyright (C) 2003,2005,2006 David Hampton <hampton@employees.org>
*/
#include <config.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <gdk/gdk.h>
#include <gdk/gdkkeysyms.h>
#include "gnc-plugin.h"
#include "gnc-plugin-manager.h"
#include "gnc-main-window.h"
#include "dialog-options.h"
#include "dialog-preferences.h"
#include "dialog-reset-warnings.h"
#include "dialog-transfer.h"
#include "dialog-utils.h"
#include "engine-helpers.h"
#include "file-utils.h"
#include "gnc-component-manager.h"
#include "gnc-engine.h"
#include "gnc-features.h"
#include "gnc-file.h"
#include "gnc-filepath-utils.h"
#include "gnc-gkeyfile-utils.h"
#include "gnc-gnome-utils.h"
#include "gnc-gobject-utils.h"
#include "gnc-gui-query.h"
#include "gnc-hooks.h"
#include "gnc-icons.h"
#include "gnc-session.h"
#include "gnc-state.h"
#include "gnc-ui.h"
#include "gnc-ui-util.h"
#include "gnc-uri-utils.h"
#include "gnc-version.h"
#include "gnc-warnings.h"
#include "gnc-window.h"
#include "gnc-prefs.h"
#include "option-util.h"
// +JSLED
//#include "gnc-html.h"
#include "gnc-autosave.h"
#include "print-session.h"
#ifdef MAC_INTEGRATION
#include <gtkmacintegration/gtkosxapplication.h>
#endif
#ifdef HAVE_SYS_STAT_H
# define __need_system_sys_stat_h //To block Guile-2.0's evil substitute
# include <sys/types.h>
# include <sys/stat.h> // for stat(2)
#endif
/** Names of signals generated by the main window. */
enum
{
PAGE_ADDED,
PAGE_CHANGED,
LAST_SIGNAL
};
/** This label is used to provide a mapping from a visible page widget
* back to the corresponding GncPluginPage object. */
#define PLUGIN_PAGE_LABEL "plugin-page"
#define PLUGIN_PAGE_CLOSE_BUTTON "close-button"
#define PLUGIN_PAGE_TAB_LABEL "label"
#define GNC_PREF_SHOW_CLOSE_BUTTON "tab-close-buttons"
#define GNC_PREF_TAB_NEXT_RECENT "tab-next-recent"
#define GNC_PREF_TAB_POSITION_TOP "tab-position-top"
#define GNC_PREF_TAB_POSITION_BOTTOM "tab-position-bottom"
#define GNC_PREF_TAB_POSITION_LEFT "tab-position-left"
#define GNC_PREF_TAB_POSITION_RIGHT "tab-position-right"
#define GNC_PREF_TAB_WIDTH "tab-width"
#define GNC_PREF_TAB_COLOR "show-account-color-tabs"
#define GNC_PREF_SAVE_CLOSE_EXPIRES "save-on-close-expires"
#define GNC_PREF_SAVE_CLOSE_WAIT_TIME "save-on-close-wait-time"
#define GNC_MAIN_WINDOW_NAME "GncMainWindow"
#define DIALOG_BOOK_OPTIONS_CM_CLASS "dialog-book-options"
/* Static Globals *******************************************************/
/** The debugging module that this .o belongs to. */
static QofLogModule log_module = GNC_MOD_GUI;
/** A pointer to the parent class of an embedded window. */
static GObjectClass *parent_class = NULL;
/** An identifier that indicates a "main" window. */
static GQuark window_type = 0;
/** A list of all extant main windows. This is for convenience as the
* same information can be obtained from the object tracking code. */
static GList *active_windows = NULL;
/** Count down timer for the save changes dialog. If the timer reaches zero
* any changes will be saved and the save dialog closed automatically */
static guint secs_to_save = 0;
#define MSG_AUTO_SAVE _("Changes will be saved automatically in %u seconds")
/* Declarations *********************************************************/
static void gnc_main_window_class_init (GncMainWindowClass *klass);
static void gnc_main_window_init (GncMainWindow *window,
void *data);
static void gnc_main_window_finalize (GObject *object);
static void gnc_main_window_destroy (GtkWidget *widget);
static void gnc_main_window_setup_window (GncMainWindow *window);
static void gnc_window_main_window_init (GncWindowIface *iface);
#ifndef MAC_INTEGRATION
static void gnc_main_window_update_all_menu_items (void);
#endif
/* Callbacks */
static void gnc_main_window_add_widget (GtkUIManager *merge, GtkWidget *widget, GncMainWindow *window);
static void gnc_main_window_switch_page (GtkNotebook *notebook, gpointer *notebook_page, gint pos, GncMainWindow *window);
static void gnc_main_window_page_reordered (GtkNotebook *notebook, GtkWidget *child, guint pos, GncMainWindow *window);
static void gnc_main_window_plugin_added (GncPlugin *manager, GncPlugin *plugin, GncMainWindow *window);
static void gnc_main_window_plugin_removed (GncPlugin *manager, GncPlugin *plugin, GncMainWindow *window);
static void gnc_main_window_engine_commit_error_callback( gpointer data, QofBackendError errcode );
/* Command callbacks */
static void gnc_main_window_cmd_page_setup (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_file_properties (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_file_close (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_file_quit (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_edit_cut (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_edit_copy (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_edit_paste (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_edit_preferences (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_view_refresh (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_view_toolbar (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_view_summary (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_view_statusbar (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_actions_reset_warnings (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_actions_rename_page (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_window_new (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_window_move_page (GtkAction *action, GncMainWindow *window);
#ifndef MAC_INTEGRATION
static void gnc_main_window_cmd_window_raise (GtkAction *action, GtkRadioAction *current, GncMainWindow *window);
#endif
static void gnc_main_window_cmd_help_tutorial (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_help_contents (GtkAction *action, GncMainWindow *window);
static void gnc_main_window_cmd_help_about (GtkAction *action, GncMainWindow *window);
static void do_popup_menu(GncPluginPage *page, GdkEventButton *event);
static GtkWidget *gnc_main_window_get_statusbar (GncWindow *window_in);
static void statusbar_notification_lastmodified(void);
static void gnc_main_window_update_tab_position (gpointer prefs, gchar *pref, gpointer user_data);
static void gnc_main_window_remove_prefs (GncMainWindow *window);
#ifdef MAC_INTEGRATION
static void gnc_quartz_shutdown(GtkosxApplication *theApp, gpointer data);
static gboolean gnc_quartz_should_quit(GtkosxApplication *theApp, GncMainWindow *window);
static void gnc_quartz_set_menu(GncMainWindow* window);
#endif
/** The instance private data structure for an embedded window
* object. */
typedef struct GncMainWindowPrivate
{
/** The dock (vbox) at the top of the window containing the
* menubar and toolbar. These items are generated by the UI
* manager and stored here when the UI manager provides them
* to the main window. */
GtkWidget *menu_dock;
/** The toolbar created by the UI manager. This pointer
* provides easy access for showing/hiding the toolbar. */
GtkWidget *toolbar;
/** The notebook containing all the pages in this window. */
GtkWidget *notebook;
/** Show account color as background on tabs */
gboolean show_color_tabs;
/** A pointer to the status bar at the bottom edge of the
* window. This pointer provides easy access for
* updating/showing/hiding the status bar. */
GtkWidget *statusbar;
/** A pointer to the progress bar at the bottom right of the
* window that is contained in the status bar. This pointer
* provides easy access for updating the progressbar. */
GtkWidget *progressbar;
/** The group of all actions provided by the main window
* itself. This does not include any action provided by menu
* or content plugins. */
GtkActionGroup *action_group;
/** A list of all pages that are installed in this window. */
GList *installed_pages;
/** A list of pages in order of use (most recent -> least recent) */
GList *usage_order;
/** The currently selected page. */
GncPluginPage *current_page;
/** The identifier for this window's engine event handler. */
gint event_handler_id;
/** Array for window position. */
gint pos[2];
/** A hash table of all action groups that have been installed
* into this window. The keys are the name of an action
* group, the values are structures of type
* MergedActionEntry. */
GHashTable *merged_actions_table;
/** Set when restoring plugin pages */
gboolean restoring_pages;
} GncMainWindowPrivate;
GNC_DEFINE_TYPE_WITH_CODE(GncMainWindow, gnc_main_window, GTK_TYPE_WINDOW,
G_ADD_PRIVATE (GncMainWindow)
G_IMPLEMENT_INTERFACE (GNC_TYPE_WINDOW,
gnc_window_main_window_init))
#define GNC_MAIN_WINDOW_GET_PRIVATE(o) \
((GncMainWindowPrivate*)g_type_instance_get_private((GTypeInstance*)o, GNC_TYPE_MAIN_WINDOW))
/** This data structure maintains information about one action groups
* that has been installed in this window. */
typedef struct
{
/** The merge identifier for this action group. This number
* is provided by the UI manager. */
guint merge_id;
/** The action group itself. This contains all actions added
* by a single menu or content plugin. */
GtkActionGroup *action_group;
} MergedActionEntry;
/** A holding place for all the signals generated by the main window
* code. */
static guint main_window_signals[LAST_SIGNAL] = { 0 };
/** An array of all of the actions provided by the main window code.
* This includes some placeholder actions for the menus that are
* visible in the menu bar but have no action associated with
* them. */
static GtkActionEntry gnc_menu_actions [] =
{
/* Toplevel */
{ "FileAction", NULL, N_("_File"), NULL, NULL, NULL, },
{ "EditAction", NULL, N_("_Edit"), NULL, NULL, NULL },
{ "ViewAction", NULL, N_("_View"), NULL, NULL, NULL },
{ "ActionsAction", NULL, N_("_Actions"), NULL, NULL, NULL },
{ "TransactionAction", NULL, N_("Tra_nsaction"), NULL, NULL, NULL },
{ "ReportsAction", NULL, N_("_Reports"), NULL, NULL, NULL },
{ "ToolsAction", NULL, N_("_Tools"), NULL, NULL, NULL },
{ "ExtensionsAction", NULL, N_("E_xtensions"), NULL, NULL, NULL },
{ "WindowsAction", NULL, N_("_Windows"), NULL, NULL, NULL },
{ "HelpAction", NULL, N_("_Help"), NULL, NULL, NULL },
/* File menu */
{ "FileImportAction", NULL, N_("_Import"), NULL, NULL, NULL },
{ "FileExportAction", NULL, N_("_Export"), NULL, NULL, NULL },
{
"FilePrintAction", "document-print", N_("_Print..."), "<primary>p",
N_("Print the currently active page"), NULL
},
#ifndef GTK_STOCK_PAGE_SETUP
# define GTK_STOCK_PAGE_SETUP NULL
#endif
{
"FilePageSetupAction", "document-page-setup", N_("Pa_ge Setup..."), "<primary><shift>p",
N_("Specify the page size and orientation for printing"),
G_CALLBACK (gnc_main_window_cmd_page_setup)
},
{
"FilePropertiesAction", "document-properties", N_("Proper_ties"), "<Alt>Return",
N_("Edit the properties of the current file"),
G_CALLBACK (gnc_main_window_cmd_file_properties)
},
{
"FileCloseAction", "window-close", N_("_Close"), "<primary>W",
N_("Close the currently active page"),
G_CALLBACK (gnc_main_window_cmd_file_close)
},
{
"FileQuitAction", "application-exit", N_("_Quit"), "<primary>Q",
N_("Quit this application"),
G_CALLBACK (gnc_main_window_cmd_file_quit)
},
/* Edit menu */
{
"EditCutAction", "edit-cut", N_("Cu_t"), "<primary>X",
N_("Cut the current selection and copy it to clipboard"),
G_CALLBACK (gnc_main_window_cmd_edit_cut)
},
{
"EditCopyAction", "edit-copy", N_("_Copy"), "<primary>C",
N_("Copy the current selection to clipboard"),
G_CALLBACK (gnc_main_window_cmd_edit_copy)
},
{
"EditPasteAction", "edit-paste", N_("_Paste"), "<primary>V",
N_("Paste the clipboard content at the cursor position"),
G_CALLBACK (gnc_main_window_cmd_edit_paste)
},
{
"EditPreferencesAction", "preferences-system", N_("Pr_eferences"), NULL,
N_("Edit the global preferences of GnuCash"),
G_CALLBACK (gnc_main_window_cmd_edit_preferences)
},
/* View menu */
{
"ViewSortByAction", NULL, N_("_Sort By..."), NULL,
N_("Select sorting criteria for this page view"), NULL
},
{
"ViewFilterByAction", NULL, N_("_Filter By..."), NULL,
N_("Select the account types that should be displayed."), NULL
},
{
"ViewRefreshAction", "view-refresh", N_("_Refresh"), "<primary>r",
N_("Refresh this window"),
G_CALLBACK (gnc_main_window_cmd_view_refresh)
},
/* Actions menu */
{ "ScrubMenuAction", NULL, N_("_Check & Repair"), NULL, NULL, NULL },
{
"ActionsForgetWarningsAction", NULL, N_("Reset _Warnings..."), NULL,
N_("Reset the state of all warning messages so they will be shown again."),
G_CALLBACK (gnc_main_window_cmd_actions_reset_warnings)
},
{
"ActionsRenamePageAction", NULL, N_("Re_name Page"), NULL,
N_("Rename this page."),
G_CALLBACK (gnc_main_window_cmd_actions_rename_page)
},
/* Windows menu */
{
"WindowNewAction", NULL, N_("_New Window"), NULL,
N_("Open a new top-level GnuCash window."),
G_CALLBACK (gnc_main_window_cmd_window_new)
},
{
"WindowMovePageAction", NULL, N_("New Window with _Page"), NULL,
N_("Move the current page to a new top-level GnuCash window."),
G_CALLBACK (gnc_main_window_cmd_window_move_page)
},
/* Help menu */
{
"HelpTutorialAction", "help-browser", N_("Tutorial and Concepts _Guide"), "<primary>H",
N_("Open the GnuCash Tutorial"),
G_CALLBACK (gnc_main_window_cmd_help_tutorial)
},
{
"HelpContentsAction", "help-browser", N_("_Contents"), "F1",
N_("Open the GnuCash Help"),
G_CALLBACK (gnc_main_window_cmd_help_contents)
},
{
"HelpAboutAction", "help-about", N_("_About"), NULL,
N_("About GnuCash"),
G_CALLBACK (gnc_main_window_cmd_help_about)
},
};
/** The number of actions provided by the main window. */
static guint gnc_menu_n_actions = G_N_ELEMENTS (gnc_menu_actions);
/** An array of all of the toggle action provided by the main window
* code. */
static GtkToggleActionEntry toggle_actions [] =
{
{
"ViewToolbarAction", NULL, N_("_Toolbar"), NULL,
N_("Show/hide the toolbar on this window"),
G_CALLBACK (gnc_main_window_cmd_view_toolbar), TRUE
},
{
"ViewSummaryAction", NULL, N_("Su_mmary Bar"), NULL,
N_("Show/hide the summary bar on this window"),
G_CALLBACK (gnc_main_window_cmd_view_summary), TRUE
},
{
"ViewStatusbarAction", NULL, N_("Stat_us Bar"), NULL,
N_("Show/hide the status bar on this window"),
G_CALLBACK (gnc_main_window_cmd_view_statusbar), TRUE
},
};
/** The number of toggle actions provided by the main window. */
static guint n_toggle_actions = G_N_ELEMENTS (toggle_actions);
#ifndef MAC_INTEGRATION
/** An array of all of the radio action provided by the main window
* code. */
static GtkRadioActionEntry radio_entries [] =
{
{ "Window0Action", NULL, N_("Window _1"), NULL, NULL, 0 },
{ "Window1Action", NULL, N_("Window _2"), NULL, NULL, 1 },
{ "Window2Action", NULL, N_("Window _3"), NULL, NULL, 2 },
{ "Window3Action", NULL, N_("Window _4"), NULL, NULL, 3 },
{ "Window4Action", NULL, N_("Window _5"), NULL, NULL, 4 },
{ "Window5Action", NULL, N_("Window _6"), NULL, NULL, 5 },
{ "Window6Action", NULL, N_("Window _7"), NULL, NULL, 6 },
{ "Window7Action", NULL, N_("Window _8"), NULL, NULL, 7 },
{ "Window8Action", NULL, N_("Window _9"), NULL, NULL, 8 },
{ "Window9Action", NULL, N_("Window _0"), NULL, NULL, 9 },
};
/** The number of radio actions provided by the main window. */
static guint n_radio_entries = G_N_ELEMENTS (radio_entries);
#endif
/** These are the "important" actions provided by the main window.
* Their labels will appear when the toolbar is set to "Icons and
* important text" (e.g. GTK_TOOLBAR_BOTH_HORIZ) mode. */
static const gchar *gnc_menu_important_actions[] =
{
"FileCloseAction",
NULL,
};
/** The following are in the main window so they will always be
* present in the menu structure, but they are never sensitive.
* These actions should be overridden in child windows where they
* have meaning. */
static const gchar *always_insensitive_actions[] =
{
"FilePrintAction",
NULL
};
/** The following items in the main window should be made insensitive
* at startup time. The sensitivity will be changed by some later
* event. */
static const gchar *initially_insensitive_actions[] =
{
"FileCloseAction",
NULL
};
/** The following are in the main window so they will always be
* present in the menu structure, but they are always hidden.
* These actions should be overridden in child windows where they
* have meaning. */
static const gchar *always_hidden_actions[] =
{
"ViewSortByAction",
"ViewFilterByAction",
NULL
};
/** If a page is flagged as immutable, then the following actions
* cannot be performed on that page. */
static const gchar *immutable_page_actions[] =
{
"FileCloseAction",
NULL
};
/** The following actions can only be performed if there are multiple
* pages in a window. */
static const gchar *multiple_page_actions[] =
{
"WindowMovePageAction",
NULL
};
/************************************************************
* *
************************************************************/
#define WINDOW_COUNT "WindowCount"
#define WINDOW_STRING "Window %d"
#define WINDOW_GEOMETRY "WindowGeometry"
#define WINDOW_POSITION "WindowPosition"
#define WINDOW_MAXIMIZED "WindowMaximized"
#define TOOLBAR_VISIBLE "ToolbarVisible"
#define STATUSBAR_VISIBLE "StatusbarVisible"
#define SUMMARYBAR_VISIBLE "SummarybarVisible"
#define WINDOW_FIRSTPAGE "FirstPage"
#define WINDOW_PAGECOUNT "PageCount"
#define WINDOW_PAGEORDER "PageOrder"
#define PAGE_TYPE "PageType"
#define PAGE_NAME "PageName"
#define PAGE_STRING "Page %d"
typedef struct
{
GKeyFile *key_file;
const gchar *group_name;
gint window_num;
gint page_num;
gint page_offset;
} GncMainWindowSaveData;
gboolean
gnc_main_window_is_restoring_pages (GncMainWindow *window)
{
GncMainWindowPrivate *priv = GNC_MAIN_WINDOW_GET_PRIVATE(window);
return priv->restoring_pages;
}
/* Iterator function to walk all pages in all windows, calling the
* specified function for each page. */
void
gnc_main_window_foreach_page (GncMainWindowPageFunc fn, gpointer user_data)
{
GncMainWindowPrivate *priv;
GncMainWindow *window;
GncPluginPage *page;
GList *w, *p;
ENTER(" ");
for (w = active_windows; w; w = g_list_next(w))
{
window = w->data;
priv = GNC_MAIN_WINDOW_GET_PRIVATE(window);
for (p = priv->installed_pages; p; p = g_list_next(p))
{
page = p->data;
fn(page, user_data);
}
}
LEAVE(" ");
}
/** Restore a single page to a window. This function calls a page
* specific function to create the actual page. It then handles all
* the common tasks such as insuring the page is installed into a
* window, updating the page name, and anything else that might be
* common to all pages.
*
* @param window The GncMainWindow where the new page will be
* installed.
*
* @param data A data structure containing state about the
* window/page restoration process. */
static void
gnc_main_window_restore_page (GncMainWindow *window,
GncMainWindowSaveData *data)
{
GncMainWindowPrivate *priv;
GncPluginPage *page;
gchar *page_group, *page_type = NULL, *name = NULL;
const gchar *class_type;
GError *error = NULL;
ENTER("window %p, data %p (key file %p, window %d, page start %d, page num %d)",
window, data, data->key_file, data->window_num, data->page_offset,
data->page_num);
priv = GNC_MAIN_WINDOW_GET_PRIVATE(window);
page_group = g_strdup_printf(PAGE_STRING,
data->page_offset + data->page_num);
page_type = g_key_file_get_string(data->key_file, page_group,
PAGE_TYPE, &error);
if (error)
{
g_warning("error reading group %s key %s: %s",
page_group, PAGE_TYPE, error->message);
goto cleanup;
}
/* See if the page already exists. */
page = g_list_nth_data(priv->installed_pages, data->page_num);
if (page)
{
class_type = GNC_PLUGIN_PAGE_GET_CLASS(page)->plugin_name;
if (strcmp(page_type, class_type) != 0)
{
g_warning("error: page types don't match: state %s, existing page %s",
page_type, class_type);
goto cleanup;
}
}
else
{
/* create and install the page */
page = gnc_plugin_page_recreate_page(GTK_WIDGET(window), page_type,
data->key_file, page_group);
if (page)
{
/* Does the page still need to be installed into the window? */
if (page->window == NULL)
{
gnc_plugin_page_set_use_new_window(page, FALSE);
gnc_main_window_open_page(window, page);
}
/* Restore the page name */
name = g_key_file_get_string(data->key_file, page_group,
PAGE_NAME, &error);
if (error)
{
g_warning("error reading group %s key %s: %s",
page_group, PAGE_NAME, error->message);
/* Fall through and still show the page. */
}
else
{
DEBUG("updating page name for %p to %s.", page, name);
main_window_update_page_name(page, name);
g_free(name);
}
}
}
LEAVE("ok");
cleanup:
if (error)
g_error_free(error);
if (page_type)
g_free(page_type);
g_free(page_group);
}
/** Restore all the pages in a given window. This function restores
* all the window specific attributes, then calls a helper function
* to restore all the pages that are contained in the window.
*
* @param window The GncMainWindow whose pages should be restored.
*
* @param data A data structure containing state about the
* window/page restoration process. */
static void
gnc_main_window_restore_window (GncMainWindow *window, GncMainWindowSaveData *data)
{
GncMainWindowPrivate *priv;
GtkAction *action;
gint *pos, *geom, *order;
gsize length;
gboolean max, visible, desired_visibility;
gchar *window_group;
gint page_start, page_count, i;
GError *error = NULL;
/* Setup */
ENTER("window %p, data %p (key file %p, window %d)",
window, data, data->key_file, data->window_num);
window_group = g_strdup_printf(WINDOW_STRING, data->window_num + 1);
/* Deal with the uncommon case that the state file defines a window
* but no pages. An example to get in such a situation can be found
* here: https://bugs.gnucash.org/show_bug.cgi?id=436479#c3
* If this happens on the first window, we will open an account hierarchy
* to avoid confusing the user by presenting a completely empty window.
* If it happens on a later window, we'll just skip restoring that window.
*/
if (!g_key_file_has_group (data->key_file, window_group) ||
!g_key_file_has_key (data->key_file, window_group, WINDOW_PAGECOUNT, &error))
{
if (window)
{
gnc_main_window_restore_default_state (window);
PINFO ("saved state had an empty first main window\n"
"an account hierarchy page was added automatically to avoid confusion");
}
else
PINFO ("saved state had an empty main window, skipping restore");
goto cleanup;
}
/* Get this window's notebook info */
page_count = g_key_file_get_integer(data->key_file,
window_group, WINDOW_PAGECOUNT, &error);
if (error)
{
g_warning("error reading group %s key %s: %s",
window_group, WINDOW_PAGECOUNT, error->message);
goto cleanup;
}
if (page_count == 0)
{
/* Should never happen, but has during alpha testing. Having this
* check doesn't hurt anything. */
goto cleanup;
}
page_start = g_key_file_get_integer(data->key_file,
window_group, WINDOW_FIRSTPAGE, &error);
if (error)
{
g_warning("error reading group %s key %s: %s",
window_group, WINDOW_FIRSTPAGE, error->message);
goto cleanup;
}
/* Build a window if we don't already have one */
if (window == NULL)
{
DEBUG("Window %d doesn't exist. Creating new window.", data->window_num);
DEBUG("active_windows %p.", active_windows);
if (active_windows)
DEBUG("first window %p.", active_windows->data);
window = gnc_main_window_new();
}
priv = GNC_MAIN_WINDOW_GET_PRIVATE(window);
/* Get the window coordinates, etc. */
geom = g_key_file_get_integer_list(data->key_file, window_group,
WINDOW_GEOMETRY, &length, &error);
if (error)
{
g_warning("error reading group %s key %s: %s",
window_group, WINDOW_GEOMETRY, error->message);
g_error_free(error);
error = NULL;
}
else if (length != 2)
{
g_warning("invalid number of values for group %s key %s",
window_group, WINDOW_GEOMETRY);
}
else
{
gtk_window_resize(GTK_WINDOW(window), geom[0], geom[1]);
DEBUG("window (%p) size %dx%d", window, geom[0], geom[1]);
}
/* keep the geometry for a test whether the windows position
is offscreen */
pos = g_key_file_get_integer_list(data->key_file, window_group,
WINDOW_POSITION, &length, &error);
if (error)
{
g_warning("error reading group %s key %s: %s",
window_group, WINDOW_POSITION, error->message);
g_error_free(error);
error = NULL;
}
else if (length != 2)
{
g_warning("invalid number of values for group %s key %s",
window_group, WINDOW_POSITION);
}
/* Prevent restoring coordinates if this would move the window off-screen */
else if ((pos[0] + (geom ? geom[0] : 0) < 0) ||
(pos[0] > gdk_screen_width()) ||
(pos[1] + (geom ? geom[1] : 0) < 0) ||
(pos[1] > gdk_screen_height()))
{
g_debug("position %dx%d, size%dx%d is offscreen; will not move",
pos[0], pos[1], geom ? geom[0] : 0, geom ? geom[1] : 0);
}
else
{
gtk_window_move(GTK_WINDOW(window), pos[0], pos[1]);
priv->pos[0] = pos[0];
priv->pos[1] = pos[1];
DEBUG("window (%p) position %dx%d", window, pos[0], pos[1]);
}
if (geom)
{
g_free(geom);
}
if (pos)
{
g_free(pos);
}
max = g_key_file_get_boolean(data->key_file, window_group,
WINDOW_MAXIMIZED, &error);
if (error)
{
g_warning("error reading group %s key %s: %s",
window_group, WINDOW_MAXIMIZED, error->message);
g_error_free(error);
error = NULL;
}
else if (max)
{
gtk_window_maximize(GTK_WINDOW(window));
}
/* Common view menu items */
action = gnc_main_window_find_action(window, "ViewToolbarAction");
visible = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
desired_visibility = g_key_file_get_boolean(data->key_file, window_group,
TOOLBAR_VISIBLE, &error);
if (error)
{
g_warning("error reading group %s key %s: %s",
window_group, TOOLBAR_VISIBLE, error->message);
g_error_free(error);
error = NULL;
}
else if (visible != desired_visibility)
{
gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), desired_visibility);
}
action = gnc_main_window_find_action(window, "ViewSummaryAction");
visible = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
desired_visibility = g_key_file_get_boolean(data->key_file, window_group,
SUMMARYBAR_VISIBLE, &error);
if (error)
{
g_warning("error reading group %s key %s: %s",
window_group, TOOLBAR_VISIBLE, error->message);
g_error_free(error);
error = NULL;
}
else if (visible != desired_visibility)
{
gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), desired_visibility);
}
action = gnc_main_window_find_action(window, "ViewStatusbarAction");
visible = gtk_toggle_action_get_active(GTK_TOGGLE_ACTION(action));
desired_visibility = g_key_file_get_boolean(data->key_file, window_group,
STATUSBAR_VISIBLE, &error);
if (error)
{
g_warning("error reading group %s key %s: %s",
window_group, TOOLBAR_VISIBLE, error->message);
g_error_free(error);
error = NULL;
}
else if (visible != desired_visibility)
{
gtk_toggle_action_set_active(GTK_TOGGLE_ACTION(action), desired_visibility);
}
priv->restoring_pages = TRUE;
/* Now populate the window with pages. */
for (i = 0; i < page_count; i++)
{
data->page_offset = page_start;
data->page_num = i;
gnc_main_window_restore_page(window, data);
/* give the page a chance to display */
while (gtk_events_pending ())
gtk_main_iteration ();
}
priv->restoring_pages = FALSE;
/* Restore page ordering within the notebook. Use +1 notation so the
* numbers in the page order match the page sections, at least for
* the one window case. */
order = g_key_file_get_integer_list(data->key_file, window_group,
WINDOW_PAGEORDER, &length, &error);
if (error)
{
g_warning("error reading group %s key %s: %s",
window_group, WINDOW_PAGEORDER, error->message);
g_error_free(error);
error = NULL;
}
else if (length != page_count)
{
g_warning("%s key %s length %" G_GSIZE_FORMAT " differs from window page count %d",
window_group, WINDOW_PAGEORDER, length, page_count);
}
else
{
/* Dump any list that might exist */
g_list_free(priv->usage_order);
priv->usage_order = NULL;
/* Now rebuild the list from the key file. */
for (i = 0; i < length; i++)
{
gpointer page = g_list_nth_data(priv->installed_pages, order[i] - 1);
if (page)
{
priv->usage_order = g_list_append(priv->usage_order, page);
}
}
gtk_notebook_set_current_page (GTK_NOTEBOOK(priv->notebook),
order[0] - 1);
g_signal_emit_by_name (window, "page_changed",
g_list_nth_data (priv->usage_order, 0));
}
if (order)
{
g_free(order);
}
LEAVE("window %p", window);
cleanup:
if (error)
g_error_free(error);
g_free(window_group);
gtk_widget_show(GTK_WIDGET(window));
}
void
gnc_main_window_restore_all_windows(const GKeyFile *keyfile)
{
gint i, window_count;
GError *error = NULL;
GncMainWindowSaveData data;
GncMainWindow *window;
/* We use the same struct for reading and for writing, so we cast
away the const. */
data.key_file = (GKeyFile *) keyfile;
window_count = g_key_file_get_integer(data.key_file, STATE_FILE_TOP,
WINDOW_COUNT, &error);
if (error)
{
g_warning("error reading group %s key %s: %s",
STATE_FILE_TOP, WINDOW_COUNT, error->message);
g_error_free(error);
LEAVE("can't read count");
return;
}
/* Restore all state information on the open windows. Window
numbers in state file are 1-based. GList indices are 0-based. */
gnc_set_busy_cursor (NULL, TRUE);
for (i = 0; i < window_count; i++)
{
data.window_num = i;
window = g_list_nth_data(active_windows, i);
gnc_main_window_restore_window(window, &data);
}
gnc_unset_busy_cursor (NULL);
statusbar_notification_lastmodified();
}
void
gnc_main_window_restore_default_state(GncMainWindow *window)
{
GtkAction *action;
/* The default state should be to have an Account Tree page open
* in the window. */
DEBUG("no saved state file");
if (!window)
window = g_list_nth_data(active_windows, 0);
gtk_widget_show (GTK_WIDGET(window));
action = gnc_main_window_find_action(window, "ViewAccountTreeAction");
gtk_action_activate(action);
}
/** Save the state of a single page to a disk. This function handles
* all the common tasks such as saving the page type and name, and
* anything else that might be common to all pages. It then calls a
* page specific function to save the actual page.
*
* @param page The GncPluginPage whose state should be saved.
*
* @param data A data structure containing state about the
* window/page saving process. */
static void
gnc_main_window_save_page (GncPluginPage *page, GncMainWindowSaveData *data)
{
gchar *page_group;
const gchar *plugin_name, *page_name;
ENTER("page %p, data %p (key file %p, window %d, page %d)",
page, data, data->key_file, data->window_num, data->page_num);
plugin_name = gnc_plugin_page_get_plugin_name(page);
page_name = gnc_plugin_page_get_page_name(page);
if (!plugin_name || !page_name)