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
|
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*- *
*
* This file is part of GtkSourceView
*
* Copyright (C) 2016, 2017 - Sébastien Wilmet <swilmet@gnome.org>
*
* GtkSourceView is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* GtkSourceView 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library; if not, see <http://www.gnu.org/licenses/>.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "gtksourceinit.h"
#include <glib/gi18n-lib.h>
#include "gtksourcelanguagemanager.h"
#include "gtksourcestyleschememanager.h"
#ifdef G_OS_WIN32
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
static HMODULE gtk_source_dll;
BOOL WINAPI DllMain (HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved);
BOOL WINAPI
DllMain (HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
gtk_source_dll = hinstDLL;
break;
default:
/* do nothing */
break;
}
return TRUE;
}
#endif /* G_OS_WIN32 */
#ifdef OS_OSX
#include <Cocoa/Cocoa.h>
static gchar *
dirs_os_x_get_bundle_resource_dir (void)
{
NSAutoreleasePool *pool;
gchar *str = NULL;
NSString *path;
pool = [[NSAutoreleasePool alloc] init];
if ([[NSBundle mainBundle] bundleIdentifier] == nil)
{
[pool release];
return NULL;
}
path = [[NSBundle mainBundle] resourcePath];
if (!path)
{
[pool release];
return NULL;
}
str = g_strdup ([path UTF8String]);
[pool release];
return str;
}
static gchar *
dirs_os_x_get_locale_dir (void)
{
gchar *res_dir;
gchar *ret;
res_dir = dirs_os_x_get_bundle_resource_dir ();
if (res_dir == NULL)
{
ret = g_build_filename (DATADIR, "locale", NULL);
}
else
{
ret = g_build_filename (res_dir, "share", "locale", NULL);
g_free (res_dir);
}
return ret;
}
#endif /* OS_OSX */
static gchar *
get_locale_dir (void)
{
gchar *locale_dir;
#if defined (G_OS_WIN32)
gchar *win32_dir;
win32_dir = g_win32_get_package_installation_directory_of_module (gtk_source_dll);
locale_dir = g_build_filename (win32_dir, "share", "locale", NULL);
g_free (win32_dir);
#elif defined (OS_OSX)
locale_dir = dirs_os_x_get_locale_dir ();
#else
locale_dir = g_build_filename (DATADIR, "locale", NULL);
#endif
return locale_dir;
}
/**
* gtk_source_init:
*
* Initializes the GtkSourceView library (e.g. for the internationalization).
*
* This function can be called several times, but is meant to be called at the
* beginning of main(), before any other GtkSourceView function call.
*
* Since: 4.0
*/
void
gtk_source_init (void)
{
static gboolean done = FALSE;
if (!done)
{
gchar *locale_dir;
locale_dir = get_locale_dir ();
bindtextdomain (GETTEXT_PACKAGE, locale_dir);
g_free (locale_dir);
bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
done = TRUE;
}
}
/**
* gtk_source_finalize:
*
* Free the resources allocated by GtkSourceView. For example it unrefs the
* singleton objects.
*
* It is not mandatory to call this function, it's just to be friendlier to
* memory debugging tools. This function is meant to be called at the end of
* main(). It can be called several times.
*
* Since: 4.0
*/
/* Another way is to use a DSO destructor, see gconstructor.h in GLib.
*
* The advantage of calling gtk_source_finalize() at the end of main() is that
* gobject-list [1] correctly reports that all GtkSource* objects have been
* finalized when quitting the application. On the other hand a DSO destructor
* runs after the gobject-list's last output, so it's much less convenient, see:
* commit e761de9c2bee90c232875bbc41e6e73e1f63e145
*
* [1] A tool for debugging the lifetime of GObjects:
* https://github.com/danni/gobject-list
*/
void
gtk_source_finalize (void)
{
static gboolean done = FALSE;
/* Unref the singletons only once, even if this function is called
* multiple times, to see if a reference is not released correctly.
* Normally the singleton have a ref count of 1. If for some reason the
* ref count is increased somewhere, it needs to be decreased
* accordingly, at the right place.
*/
if (!done)
{
GtkSourceLanguageManager *language_manager;
GtkSourceStyleSchemeManager *style_scheme_manager;
language_manager = _gtk_source_language_manager_peek_default ();
g_clear_object (&language_manager);
style_scheme_manager = _gtk_source_style_scheme_manager_peek_default ();
g_clear_object (&style_scheme_manager);
done = TRUE;
}
}
|