prepare windows i18n
[ardour.git] / gtk2_ardour / bundle_env_mingw.cc
1 /*
2     Copyright (C) 2001-2012 Paul Davis
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 #include <stdlib.h>
21 #include "bundle_env.h"
22 #include "i18n.h"
23
24 #include <glibmm.h>
25 #include <fontconfig/fontconfig.h>
26 #include <pango/pangoft2.h>
27 #include <pango/pangocairo.h>
28
29 #include <windows.h>
30 #include <wingdi.h>
31 #include <shlobj.h> // CSIDL_*
32
33 #include "ardour/ardour.h"
34 #include "ardour/search_paths.h"
35 #include "ardour/filesystem_paths.h"
36
37 #include "pbd/file_utils.h"
38 #include "pbd/epa.h"
39 #include "pbd/windows_special_dirs.h"
40
41 using namespace std;
42 using namespace PBD;
43 using namespace ARDOUR;
44
45
46 /* query top-level Ardour installation path.
47  * Typically, this will be somehwere like
48  * "C:\Program Files (x86)\Ardour"
49  */
50 const std::string
51 get_install_path ()
52 {
53         const gchar* pExeRoot = g_win32_get_package_installation_directory_of_module (0);
54
55         if (0 == pExeRoot) {
56                 HKEY key;
57                 DWORD size = PATH_MAX;
58                 char tmp[PATH_MAX+1];
59                 if (
60 #ifdef __MINGW64__
61                                 (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, "Software\\" PROGRAM_NAME "\\v" PROGRAM_VERSION "\\w64", 0, KEY_READ, &key))
62 #else
63                                 (ERROR_SUCCESS == RegOpenKeyExA (HKEY_LOCAL_MACHINE, "Software\\" PROGRAM_NAME "\\v" PROGRAM_VERSION "\\w32", 0, KEY_READ, &key))
64 #endif
65                                 &&(ERROR_SUCCESS == RegQueryValueExA (key, "Install_Dir", 0, NULL, reinterpret_cast<LPBYTE>(tmp), &size))
66                          )
67                 {
68                         pExeRoot = Glib::locale_to_utf8(tmp).c_str();
69                 }
70         }
71
72         if (0 == pExeRoot) {
73                 const char *program_files = PBD::get_win_special_folder (CSIDL_PROGRAM_FILES);
74                 if (program_files) {
75                         pExeRoot = g_build_filename(program_files, PROGRAM_NAME, NULL);
76                 }
77         }
78
79         if (pExeRoot && Glib::file_test(pExeRoot, Glib::FILE_TEST_EXISTS|Glib::FILE_TEST_IS_DIR)) {
80                 return std::string (pExeRoot);
81         }
82         return "";
83 }
84
85
86 void
87 fixup_bundle_environment (int, char* [], const char** localedir)
88 {
89         EnvironmentalProtectionAgency::set_global_epa (new EnvironmentalProtectionAgency (true));
90         /* what to do ? */
91         // we should at least set ARDOUR_DATA_PATH to prevent the warning message.
92         // setting a FONTCONFIG_FILE won't hurt either see bundle_env_msvc.cc
93         // (pangocairo prefers the windows gdi backend unless PANGOCAIRO_BACKEND=fc is set)
94
95         // Unset GTK_RC_FILES so that only ardour specific files are loaded
96         Glib::unsetenv ("GTK_RC_FILES");
97
98         if (ARDOUR::translations_are_enabled ()) {
99                 path = windows_search_path().to_string();
100                 path += "\\locale";
101                 Glib::setenv ("GTK_LOCALEDIR", path, true);
102
103                 // and return the same path to our caller
104                 (*localedir) = strdup (path.c_str());
105         }
106
107         std::string path;
108         const char *cstr;
109         cstr = getenv ("VAMP_PATH");
110         if (cstr) {
111                 path = cstr;
112                 path += G_SEARCHPATH_SEPARATOR;
113         } else {
114                 path = "";
115         }
116         path += Glib::build_filename(ardour_dll_directory(), "vamp");
117         path += G_SEARCHPATH_SEPARATOR;
118         path += "%ProgramFiles%\\Vamp Plugins"; // default vamp path
119         path += G_SEARCHPATH_SEPARATOR;
120         path += "%COMMONPROGRAMFILES%\\Vamp Plugins";
121         Glib::setenv ("VAMP_PATH", path, true);
122
123         Glib::setenv ("SUIL_MODULE_DIR", Glib::build_filename(ardour_dll_directory(), "suil"), true);
124 }
125
126 static __cdecl void
127 unload_custom_fonts()
128 {
129         std::string ardour_mono_file;
130         if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
131                 return;
132         }
133         RemoveFontResource(ardour_mono_file.c_str());
134 }
135
136 void
137 load_custom_fonts()
138 {
139         std::string ardour_mono_file;
140
141         if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
142                 cerr << _("Cannot find ArdourMono TrueType font") << endl;
143                 return;
144         }
145
146         if (pango_font_map_get_type() == PANGO_TYPE_FT2_FONT_MAP) {
147                 FcConfig *config = FcInitLoadConfigAndFonts();
148                 FcBool ret = FcConfigAppFontAddFile(config, reinterpret_cast<const FcChar8*>(ardour_mono_file.c_str()));
149
150                 if (ret == FcFalse) {
151                         cerr << _("Cannot load ArdourMono TrueType font.") << endl;
152                 }
153
154                 ret = FcConfigSetCurrent(config);
155
156                 if (ret == FcFalse) {
157                         cerr << _("Failed to set fontconfig configuration.") << endl;
158                 }
159         } else {
160                 // pango with win32 backend
161                 if (0 == AddFontResource(ardour_mono_file.c_str())) {
162                         cerr << _("Cannot register ArdourMono TrueType font with windows gdi.") << endl;
163                 } else {
164                         atexit (&unload_custom_fonts);
165                 }
166         }
167 }