26#include <glib/gi18n.h>
27#include <libxml/xmlIO.h>
38#include "gnc-menu-extensions.h"
39#include "gnc-component-manager.h"
40#include "gnc-splash.h"
43#include "dialog-doclink-utils.h"
45#include "dialog-totd.h"
48#include "gnc-session.h"
49#include "qofbookslots.h"
52#include "gnc-help-utils.h"
55#import <Cocoa/Cocoa.h>
58extern SCM scm_init_sw_gnome_utils_module(
void);
60static QofLogModule log_module = GNC_MOD_GUI;
61static int gnome_is_running = FALSE;
62static int gnome_is_terminating = FALSE;
63static int gnome_is_initialized = FALSE;
66#define ACCEL_MAP_NAME "accelerator-map"
68const gchar *msg_no_help_found =
69 N_(
"GnuCash could not find the files of the help documentation.");
70const gchar *msg_no_help_reason =
71 N_(
"This is likely because the \"gnucash-docs\" package is not properly installed.");
73const gchar *msg_no_help_location = N_(
"Expected location");
78 gnc_component_manager_init ();
80 scm_init_sw_gnome_utils_module();
81 scm_c_use_module (
"sw_gnome_utils");
82 scm_c_use_module(
"gnucash gnome-utils");
93gnc_configure_date_format (
void)
96 GNC_PREF_DATE_FORMAT);
103 PERR(
"Incorrect date format");
120gnc_configure_date_completion (
void)
124 GNC_PREF_DATE_BACKMONTHS);
128 else if (backmonths > 11)
140 GtkCssProvider *provider_user, *provider_app, *provider_fallback;
146 provider_user = gtk_css_provider_new ();
147 provider_app = gtk_css_provider_new ();
148 provider_fallback = gtk_css_provider_new ();
149 display = gdk_display_get_default ();
150 screen = gdk_display_get_default_screen (display);
152 gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider_fallback), GTK_STYLE_PROVIDER_PRIORITY_FALLBACK);
153 gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider_app), GTK_STYLE_PROVIDER_PRIORITY_APPLICATION);
154 gtk_style_context_add_provider_for_screen (screen, GTK_STYLE_PROVIDER (provider_user), GTK_STYLE_PROVIDER_PRIORITY_USER);
156 gtk_css_provider_load_from_resource (provider_app, GNUCASH_RESOURCE_PREFIX
"/gnucash.css");
157 gtk_css_provider_load_from_resource (provider_fallback, GNUCASH_RESOURCE_PREFIX
"/gnucash-fallback.css");
163 str = g_build_filename (var,
"gtk-3.0.css", (
char *)NULL);
164 if (!gtk_css_provider_load_from_path (provider_user, str, &error))
166 PWARN (
"Could not load user CSS '%s': %s", str, error->message);
167 g_clear_error (&error);
171 g_object_unref (provider_user);
172 g_object_unref (provider_app);
173 g_object_unref (provider_fallback);
176#ifdef MAC_INTEGRATION
183gnc_gnome_help (GtkWindow *parent,
const char *dir,
const char *detail)
185 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
186 NSString *subdir = [NSString stringWithUTF8String: dir];
191 tag = [NSString stringWithUTF8String: detail];
195 if (![[NSBundle mainBundle] bundleIdentifier])
204 NSArray *components = [NSArray arrayWithObjects:
@"share",
@"doc",
@"gnucash-docs", nil ];
205 NSString *prefix = [[[NSBundle mainBundle] resourcePath]
206 stringByDeletingLastPathComponent];
207 NSArray *prefix_comps = [[prefix pathComponents]
208 arrayByAddingObjectsFromArray: components];
209 NSString *docs_dir = [NSString pathWithComponents: prefix_comps];
210 NSArray *languages = [[NSUserDefaults standardUserDefaults]
211 objectForKey:
@"AppleLanguages"];
213 subdir = [[[subdir lowercaseString] componentsSeparatedByString:
@" "]
214 componentsJoinedByString:
@"-"];
215 if (![[NSFileManager defaultManager] fileExistsAtPath: docs_dir])
217 gnc_error_dialog (parent,
"%s\n%s\n%s: %s",
_(msg_no_help_found),
218 _(msg_no_help_reason),
219 _(msg_no_help_location), [docs_dir UTF8String]);
223 if ([languages count] > 0)
225 NSEnumerator *lang_iter = [languages objectEnumerator];
228 while ((this_lang = [lang_iter nextObject]))
232 NSString *completed_path = [NSString alloc];
233 this_lang = [this_lang stringByTrimmingCharactersInSet:
234 [NSCharacterSet characterSetWithCharactersInString:
236 elements = [this_lang componentsSeparatedByString: @"-
"];
237 this_lang = [elements objectAtIndex: 0];
238 path = [docs_dir stringByAppendingPathComponent: this_lang];
239 paths = [path completePathIntoString: &completed_path
241 matchesIntoArray: NULL filterTypes: NULL];
243 [[NSFileManager defaultManager]
244 fileExistsAtPath: completed_path
250 url = [NSURL fileURLWithPath:
252 stringByAppendingPathComponent: subdir]
253 stringByAppendingPathComponent: tag]
254 stringByAppendingPathExtension: @"html
"]];
256 @catch (NSException *e)
258 PWARN("fileURLWithPath threw %s: %s
",
259 [[e name] UTF8String], [[e reason] UTF8String]);
264 if ([this_lang compare: @"en
"] == NSOrderedSame)
265 break; /* Special case, forces use of "C
" locale */
273 fileURLWithPath: [[[[docs_dir
274 stringByAppendingPathComponent: @"C
"]
275 stringByAppendingPathComponent: subdir]
276 stringByAppendingPathComponent: tag]
277 stringByAppendingPathExtension: @"html
"]];
279 @catch (NSException *e)
281 PWARN("fileURLWithPath threw %s: %s
",
282 [[e name] UTF8String], [[e reason] UTF8String]);
287 /* It's a lot easier in a bundle! OSX finds the best translation for us. */
292 url = [NSURL fileURLWithPath: [[NSBundle mainBundle]
295 inDirectory: subdir ]];
297 @catch (NSException *e)
299 PWARN("fileURLWithPath threw %s: %s
",
300 [[e name] UTF8String], [[e reason] UTF8String]);
304 /* Now just open the URL in the default app for opening URLs */
306 [[NSWorkspace sharedWorkspace] openURL: url];
309 gnc_error_dialog (parent, "%s\n%s
", _(msg_no_help_found), _(msg_no_help_reason));
313#elif defined G_OS_WIN32 /* G_OS_WIN32 */
315gnc_gnome_help (GtkWindow *parent, const char *file_name, const char *anchor)
317 const gchar * const *lang;
318 gchar *pkgdatadir, *fullpath, *found = NULL;
320 pkgdatadir = gnc_path_get_pkgdatadir ();
321 for (lang = g_get_language_names (); *lang; lang++)
323 fullpath = g_build_filename (pkgdatadir, "help
", *lang, file_name,
325 if (g_file_test (fullpath, G_FILE_TEST_IS_REGULAR))
327 found = g_strdup (fullpath);
337 gnc_error_dialog (parent, "%s\n%s
", _(msg_no_help_found), _(msg_no_help_reason));
341 gnc_show_htmlhelp (found, anchor);
347gnc_gnome_help (GtkWindow *parent, const char *file_name, const char *anchor)
349 GError *error = NULL;
351 gboolean success = TRUE;
354 uri = g_strconcat ("help:
", file_name, "/
", anchor, NULL);
356 uri = g_strconcat ("help:
", file_name, NULL);
358 DEBUG ("Attempting to opening help uri %s
", uri);
361 success = gtk_show_uri_on_window (NULL, uri, gtk_get_current_event_time (), &error);
367 g_assert(error != NULL);
369 gnc_error_dialog (parent, "%s\n%s
", _(msg_no_help_found), _(msg_no_help_reason));
371 PERR ("%s
", error->message);
378#ifdef MAC_INTEGRATION
380/* Don't be alarmed if this function looks strange to you: It's
381 * written in Objective-C, the native language of the OSX Cocoa
385gnc_launch_doclink (GtkWindow *parent, const char *uri)
387 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
388 NSString *uri_str = [NSString stringWithUTF8String: uri];
389 NSURL *url = [[[NSURL alloc] initWithString: uri_str] autorelease];
390 const gchar *message =
391 _("GnuCash could not find the linked document.
");
395 [[NSWorkspace sharedWorkspace] openURL: url];
400 gnc_error_dialog (parent, "%s
", message);
405#elif defined G_OS_WIN32 /* G_OS_WIN32 */
407gnc_launch_doclink (GtkWindow *parent, const char *uri)
409 wchar_t *winuri = NULL;
410 gchar *filename = NULL;
411 /* ShellExecuteW open doesn't decode http escapes if it's passed a
412 * file URI so we have to do it. */
413 if (gnc_uri_is_file_uri (uri))
415 gchar *uri_scheme = gnc_uri_get_scheme (uri);
416 filename = gnc_doclink_get_unescape_uri (NULL, uri, uri_scheme);
417 winuri = (wchar_t *)g_utf8_to_utf16(filename, -1, NULL, NULL, NULL);
421 winuri = (wchar_t *)g_utf8_to_utf16(uri, -1, NULL, NULL, NULL);
425 wchar_t *wincmd = (wchar_t *)g_utf8_to_utf16("open
", -1,
427 if ((INT_PTR)ShellExecuteW(NULL, wincmd, winuri,
428 NULL, NULL, SW_SHOWNORMAL) <= 32)
430 const gchar *message =
431 _("GnuCash could not find the linked document.
");
432 gnc_error_dialog (parent, "%s:\n%s
", message, filename);
442gnc_launch_doclink (GtkWindow *parent, const char *uri)
444 GError *error = NULL;
450 DEBUG ("Attempting to open uri %s
", uri);
452 success = gtk_show_uri_on_window (NULL, uri, gtk_get_current_event_time (), &error);
457 g_assert (error != NULL);
459 gchar *error_uri = NULL;
460 const gchar *message =
461 _("GnuCash could not open the linked document:
");
463 if (gnc_uri_is_file_uri (uri))
465 gchar *uri_scheme = gnc_uri_get_scheme (uri);
466 error_uri = gnc_doclink_get_unescape_uri (NULL, uri, uri_scheme);
470 error_uri = g_strdup (uri);
472 gnc_error_dialog (parent, "%s\n%s
", message, error_uri);
475 PERR ("%s
", error->message);
476 g_error_free (error);
489gnc_gnome_get_pixmap (const char *name)
494 g_return_val_if_fail (name != NULL, NULL);
496 fullname = gnc_filepath_locate_pixmap (name);
497 if (fullname == NULL)
500 DEBUG ("Loading pixmap file %s
", fullname);
502 pixmap = gtk_image_new_from_file (fullname);
505 PERR ("Could not load pixmap
");
520gnc_gnome_get_gdkpixbuf (const char *name)
523 GError *error = NULL;
526 g_return_val_if_fail (name != NULL, NULL);
528 fullname = gnc_filepath_locate_pixmap (name);
529 if (fullname == NULL)
532 DEBUG ("Loading pixbuf file %s
", fullname);
533 pixbuf = gdk_pixbuf_new_from_file (fullname, &error);
536 g_assert (pixbuf == NULL);
537 PERR ("Could not load pixbuf: %s
", error->message);
538 g_error_free (error);
546gnc_ui_check_events (gpointer not_used)
551 if (gtk_main_level() != 1)
554 if (!gnc_current_session_exist())
556 session = gnc_get_current_session ();
558 if (gnc_gui_refresh_suspended ())
561 if (!qof_session_events_pending (session))
564 gnc_suspend_gui_refresh ();
566 force = qof_session_process_events (session);
568 gnc_resume_gui_refresh ();
571 gnc_gui_refresh_all ();
578gnc_ui_start_event_loop (void)
582 gnome_is_running = TRUE;
584 id = g_timeout_add_full (G_PRIORITY_DEFAULT_IDLE, 10000, /* 10 secs */
585 gnc_ui_check_events, NULL, NULL);
587 scm_call_1(scm_c_eval_string("gnc:set-ui-status
"), SCM_BOOL_T);
589 /* Enter gnome event loop */
592 g_source_remove (id);
594 scm_call_1(scm_c_eval_string("gnc:set-ui-status
"), SCM_BOOL_F);
596 gnome_is_running = FALSE;
597 gnome_is_terminating = FALSE;
605 static GncMainWindow *main_window;
610 if (gnome_is_initialized)
613 /* use custom icon */
614 gnc_load_app_icons();
615 gtk_window_set_default_icon_name(GNC_ICON_APP);
617 g_set_application_name(PACKAGE_NAME);
620 gnc_show_splash_screen();
622 gnome_is_initialized = TRUE;
625 gnc_configure_date_format();
626 gnc_configure_date_completion();
628 gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL,
629 GNC_PREF_DATE_FORMAT,
630 gnc_configure_date_format,
632 gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL,
633 GNC_PREF_DATE_COMPL_THISYEAR,
634 gnc_configure_date_completion,
636 gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL,
637 GNC_PREF_DATE_COMPL_SLIDING,
638 gnc_configure_date_completion,
640 gnc_prefs_register_cb (GNC_PREFS_GROUP_GENERAL,
641 GNC_PREF_DATE_BACKMONTHS,
642 gnc_configure_date_completion,
644 gnc_prefs_register_group_cb (GNC_PREFS_GROUP_GENERAL,
648 gnc_file_set_shutdown_callback (gnc_shutdown);
650 main_window = gnc_main_window_new ();
652 // gtk_widget_show (GTK_WIDGET (main_window));
653 gnc_window_set_progressbar_window (GNC_WINDOW(main_window));
656 map = gnc_build_userdata_path(ACCEL_MAP_NAME);
657 if (!g_file_test (map, G_FILE_TEST_EXISTS))
662 gchar *data_dir = gnc_path_get_pkgdatadir();
663#ifdef MAC_INTEGRATION
664 map_source = g_build_filename (data_dir, "ui
", "accelerator-map-osx
", NULL);
666 map_source = g_build_filename (data_dir, "ui
", "accelerator-map
", NULL);
667#endif /* MAC_INTEGRATION */
669 if (map_source && g_file_get_contents (map_source, &text, &length, NULL))
672 g_file_set_contents (map, text, length, NULL);
679 gtk_accel_map_load(map);
682 /* Load css configuration file */
685 gnc_totd_dialog (gnc_get_splash_screen (), TRUE);
692gnucash_ui_is_running(void)
694 return gnome_is_running;
698gnc_gui_destroy (void)
700 if (!gnome_is_initialized)
703 if (gnc_prefs_is_set_up())
705 gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL,
706 GNC_PREF_DATE_FORMAT,
707 gnc_configure_date_format,
709 gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL,
710 GNC_PREF_DATE_COMPL_THISYEAR,
711 gnc_configure_date_completion,
713 gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL,
714 GNC_PREF_DATE_COMPL_SLIDING,
715 gnc_configure_date_completion,
717 gnc_prefs_remove_cb_by_func (GNC_PREFS_GROUP_GENERAL,
718 GNC_PREF_DATE_BACKMONTHS,
719 gnc_configure_date_completion,
721 gnc_prefs_remove_group_cb_by_func (GNC_PREFS_GROUP_GENERAL,
725 gnc_ui_util_remove_registered_prefs ();
726 gnc_prefs_remove_registered ();
728 gnc_extensions_shutdown ();
732gnc_gui_shutdown (void)
736 if (gnome_is_running && !gnome_is_terminating)
738 gnome_is_terminating = TRUE;
739// map = gnc_build_userdata_path(ACCEL_MAP_NAME);
740// gtk_accel_map_save(map);
742 gnc_component_manager_shutdown ();
747/* shutdown gnucash. This function will initiate an orderly
748 * shutdown, and when that has finished it will exit the program.
751gnc_shutdown (int exit_status)
753 if (gnucash_ui_is_running())
755 if (!gnome_is_terminating)
757 if (gnc_file_query_save (gnc_ui_get_main_window (NULL), FALSE))
759 gnc_hook_run(HOOK_UI_SHUTDOWN, NULL);
767 gnc_hook_run(HOOK_SHUTDOWN, NULL);
768 gnc_engine_shutdown();
"select" and "new" commodity windows
All type declarations for the whole Gnucash engine.
File path resolution utility functions.
const gchar * gnc_userconfig_dir(void)
Return the user's config directory for gnucash.
Gnome specific utility functions.
Preferences initialization function.
Generic api to store and retrieve preferences.
utility functions for the GnuCash UI
Utility functions for convert uri in separate components and back.
Functions that are supported by all types of windows.
void qof_date_format_set(QofDateFormat df)
The qof_date_format_set() routine sets date format to one of US, UK, CE, OR ISO.
void qof_date_completion_set(QofDateCompletion dc, int backmonths)
The qof_date_completion_set() routing sets the date completion method to one of QOF_DATE_COMPLETION_T...
QofDateFormat
Enum for determining a date format.
QofDateCompletion
Enum for date completion modes (for dates entered without year).
@ QOF_DATE_FORMAT_LOCALE
Take from locale information.
@ QOF_DATE_COMPLETION_THISYEAR
use current year
@ QOF_DATE_COMPLETION_SLIDING
use sliding 12-month window
void gnc_add_css_file(void)
Load a gtk resource configuration file to customize gtk appearance and behaviour.
void gnc_gnome_utils_init(void)
Initialize the gnome-utils library Should be run once before using any gnome-utils features.
void gnc_gnome_help(GtkWindow *parent, const char *file_name, const char *anchor)
Launch the systems default help browser, gnome's yelp for linux, and open to a given link within a gi...
#define PERR(format, args...)
Log a serious error.
#define PWARN(format, args...)
Log a warning.
gdouble gnc_prefs_get_float(const gchar *group, const gchar *pref_name)
Get an float value from the preferences backend.
gint gnc_prefs_get_int(const gchar *group, const gchar *pref_name)
Get an integer value from the preferences backend.
gboolean gnc_prefs_get_bool(const gchar *group, const gchar *pref_name)
Get a boolean value from the preferences backend.