remove unused fstream includes
[ardour.git] / gtk2_ardour / bundle_env_cocoa.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 <string>
21 #include <vector>
22 #include <cerrno>
23 #include <cstring>
24
25 #include <glib.h>
26 #include <glibmm/fileutils.h>
27 #include <glibmm/miscutils.h>
28
29 #include <fontconfig/fontconfig.h>
30
31 #include "ardour/ardour.h"
32 #include "ardour/filesystem_paths.h"
33
34 #include "pbd/epa.h"
35 #include "pbd/search_path.h"
36 #include "pbd/pathexpand.h"
37 #include "pbd/file_utils.h"
38
39 #include "bundle_env.h"
40
41 #include "i18n.h"
42
43 #include <Carbon/Carbon.h>
44 #include <mach-o/dyld.h>
45 #include <sys/param.h>
46
47 using namespace PBD;
48 using namespace ARDOUR;
49 using namespace std;
50
51 extern void set_language_preference (); // cocoacarbon.mm
52
53 void
54 fixup_bundle_environment (int, char* [], string & localedir)
55 {
56         if (!g_getenv ("ARDOUR_BUNDLED")) {
57                 return;
58         }
59
60         EnvironmentalProtectionAgency::set_global_epa (new EnvironmentalProtectionAgency (true, "PREBUNDLE_ENV"));
61
62         set_language_preference ();
63
64         char execpath[MAXPATHLEN+1];
65         uint32_t pathsz = sizeof (execpath);
66
67         _NSGetExecutablePath (execpath, &pathsz);
68
69         std::string path;
70         std::string exec_dir = Glib::path_get_dirname (execpath);
71         std::string bundle_dir;
72         std::string userconfigdir = user_config_directory();
73
74         bundle_dir = Glib::path_get_dirname (exec_dir);
75
76 #ifdef ENABLE_NLS
77         if (!ARDOUR::translations_are_enabled ()) {
78                 localedir = "/this/cannot/exist";
79         } else {
80                 /* force localedir into the bundle */
81
82                 vector<string> lpath;
83                 lpath.push_back (bundle_dir);
84                 lpath.push_back ("Resources");
85                 lpath.push_back ("locale");
86                 localedir = Glib::build_filename (lpath).c_str();
87         }
88 #endif
89
90         export_search_path (bundle_dir, "ARDOUR_DLL_PATH", "/lib");
91
92         /* inside an OS X .app bundle, there is no difference
93            between DATA and CONFIG locations, since OS X doesn't
94            attempt to do anything to expose the notion of
95            machine-independent shared data.
96         */
97
98         export_search_path (bundle_dir, "ARDOUR_DATA_PATH", "/Resources");
99         export_search_path (bundle_dir, "ARDOUR_CONFIG_PATH", "/Resources");
100         export_search_path (bundle_dir, "ARDOUR_INSTANT_XML_PATH", "/Resources");
101         export_search_path (bundle_dir, "LADSPA_PATH", "/Plugins");
102         export_search_path (bundle_dir, "VAMP_PATH", "/lib");
103         export_search_path (bundle_dir, "GTK_PATH", "/lib/gtkengines");
104
105         g_setenv ("SUIL_MODULE_DIR", (bundle_dir + "/lib").c_str(), 1);
106         g_setenv ("PATH", (bundle_dir + "/MacOS:" + std::string(g_getenv ("PATH"))).c_str(), 1);
107
108         /* unset GTK_RC_FILES so that we only load the RC files that we define
109          */
110
111         g_unsetenv ("GTK_RC_FILES");
112         g_setenv ("CHARSETALIASDIR", bundle_dir.c_str(), 1);
113         g_setenv ("FONTCONFIG_FILE", Glib::build_filename (bundle_dir, "Resources/fonts.conf").c_str(), 1);
114 }
115
116 void load_custom_fonts()
117 {
118         /* this code will only compile on OS X 10.6 and above, and we currently do not
119          * need it for earlier versions since we fall back on a non-monospace,
120          * non-custom font.
121          */
122
123 #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1060
124         std::string ardour_mono_file;
125
126         if (!find_file (ardour_data_search_path(), "ArdourMono.ttf", ardour_mono_file)) {
127                 cerr << _("Cannot find ArdourMono TrueType font") << endl;
128         }
129
130         CFStringRef ttf;
131         CFURLRef fontURL;
132         CFErrorRef error;
133         ttf = CFStringCreateWithBytes(
134                         kCFAllocatorDefault, (UInt8*) ardour_mono_file.c_str(),
135                         ardour_mono_file.length(),
136                         kCFStringEncodingUTF8, FALSE);
137         fontURL = CFURLCreateWithFileSystemPath(kCFAllocatorDefault, ttf, kCFURLPOSIXPathStyle, TRUE);
138         if (CTFontManagerRegisterFontsForURL(fontURL, kCTFontManagerScopeProcess, &error) != true) {
139                 cerr << _("Cannot load ArdourMono TrueType font.") << endl;
140         }
141 #endif
142 }