]> git.lyx.org Git - lyx.git/blob - src/support/os_unix.cpp
Don't disable apply button if one (or more) of vertical alignment, rotation,
[lyx.git] / src / support / os_unix.cpp
1 /**
2  * \file os_unix.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author Ruurd A. Reitsma
7  *
8  * Full author contact details are available in file CREDITS.
9  *
10  * Various OS specific functions
11  */
12
13 #include <config.h>
14
15 #include "support/os.h"
16 #include "support/docstring.h"
17 #include "support/FileName.h"
18 #include "support/lstrings.h"
19 #include "support/lassert.h"
20
21 #include <limits.h>
22 #include <stdlib.h>
23
24 #ifdef __APPLE__
25 #include <Carbon/Carbon.h>
26 #endif
27
28 using namespace std;
29
30 namespace lyx {
31 namespace support {
32 namespace os {
33
34 namespace {
35
36 int argc_ = 0;
37 char ** argv_ = 0;
38
39 } // namespace anon
40
41 void init(int argc, char * argv[])
42 {
43         argc_ = argc;
44         argv_ = argv;
45 }
46
47
48 string utf8_argv(int i)
49 {
50         LASSERT(i < argc_, /**/);
51         return to_utf8(from_local8bit(argv_[i]));
52 }
53
54
55 void remove_internal_args(int, int)
56 {}
57
58
59 string current_root()
60 {
61         return "/";
62 }
63
64
65 bool isFilesystemCaseSensitive()
66 {
67 #ifdef __APPLE__
68         return false;
69 #else
70         return true;
71 #endif
72 }
73
74
75 docstring::size_type common_path(docstring const & p1, docstring const & p2)
76 {
77         docstring::size_type i = 0;
78         docstring::size_type const p1_len = p1.length();
79         docstring::size_type const p2_len = p2.length();
80 #ifdef __APPLE__
81         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
82                 ++i;
83 #else
84         while (i < p1_len && i < p2_len && p1[i] == p2[i])
85                 ++i;
86 #endif
87         if ((i < p1_len && i < p2_len)
88             || (i < p1_len && p1[i] != '/' && i == p2_len)
89             || (i < p2_len && p2[i] != '/' && i == p1_len))
90         {
91                 if (i)
92                         --i;     // here was the last match
93                 while (i && p1[i] != '/')
94                         --i;
95         }
96         return i;
97 }
98
99
100 bool path_prefix_is(string const & path, string const & pre)
101 {
102 #ifdef __APPLE__
103         return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
104 #else
105         return prefixIs(path, pre);
106 #endif
107 }
108
109
110 bool path_prefix_is(string & path, string const & pre, path_case how)
111 {
112 #ifdef __APPLE__
113         docstring const p1 = from_utf8(path);
114         docstring const p2 = from_utf8(pre);
115         docstring::size_type const p1_len = p1.length();
116         docstring::size_type const p2_len = p2.length();
117         docstring::size_type common_len = common_path(p1, p2);
118
119         if (p2[p2_len - 1] == '/' && p1_len != p2_len)
120                 ++common_len;
121
122         if (common_len != p2_len)
123                 return false;
124
125         if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
126                 if (p1_len < common_len)
127                         path = to_utf8(p2.substr(0, p1_len));
128                 else
129                         path = to_utf8(p2 + p1.substr(common_len,
130                                                         p1_len - common_len));
131         }
132
133         return true;
134 #else
135         // silence compiler warnings
136         (void)how;
137
138         return prefixIs(path, pre);
139 #endif
140 }
141
142
143 string external_path(string const & p)
144 {
145         return p;
146 }
147
148
149 string internal_path(string const & p)
150 {
151         return p;
152 }
153
154
155 string safe_internal_path(string const & p, file_access)
156 {
157         return p;
158 }
159
160
161 string external_path_list(string const & p)
162 {
163         return p;
164 }
165
166
167 string internal_path_list(string const & p)
168 {
169         return p;
170 }
171
172
173 string latex_path(string const & p)
174 {
175         return p;
176 }
177
178
179 bool is_valid_strftime(string const & p)
180 {
181         string::size_type pos = p.find_first_of('%');
182         while (pos != string::npos) {
183                 if (pos + 1 == string::npos)
184                         break;
185                 if (!containsOnly(p.substr(pos + 1, 1),
186                         "aAbBcCdDeEFgGhHIjklmMnOpPrRsStTuUVwWxXyYzZ%+"))
187                         return false;
188                 if (pos + 2 == string::npos)
189                       break;
190                 pos = p.find_first_of('%', pos + 2);
191         }
192         return true;
193 }
194
195
196 char const * popen_read_mode()
197 {
198         return "r";
199 }
200
201
202 string const & nulldev()
203 {
204         static string const nulldev_ = "/dev/null";
205         return nulldev_;
206 }
207
208
209 shell_type shell()
210 {
211         return UNIX;
212 }
213
214
215 char path_separator()
216 {
217         return ':';
218 }
219
220
221 void windows_style_tex_paths(bool)
222 {}
223
224 bool canAutoOpenFile(string const & ext, auto_open_mode const mode)
225 {
226 #ifdef __APPLE__
227 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
228         CFStringRef cfs_ext = CFStringCreateWithBytes(kCFAllocatorDefault,
229                                         (UInt8 *) ext.c_str(), ext.length(),
230                                         kCFStringEncodingISOLatin1, false);
231         // this is what we would like to do but it seems that the
232         // viewer for PDF is often quicktime...
233         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
234         (void)mode;
235         LSRolesMask role = kLSRolesAll;
236         FSRef outAppRef;
237         OSStatus status =
238                 LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
239                                         cfs_ext, role, &outAppRef, NULL);
240         CFRelease(cfs_ext);
241
242         return status != kLSApplicationNotFoundErr;
243 #else
244         // silence compiler warnings
245         (void)ext;
246         (void)mode;
247
248         // currently, no default viewer is tried for non-windows system
249         // support for KDE/Gnome/Macintosh may be added later
250         return false;
251 #endif
252 }
253
254
255 bool autoOpenFile(string const & filename, auto_open_mode const mode)
256 {
257 #ifdef __APPLE__
258 // Reference: http://developer.apple.com/documentation/Carbon/Reference/LaunchServicesReference/
259         FSRef fileref;
260         OSStatus status =
261                 FSPathMakeRef((UInt8 *) filename.c_str(), &fileref, NULL);
262         if (status != 0)
263                 return false;
264
265         // this is what we would like to do but it seems that the
266         // viewer for PDF is often quicktime...
267         //LSRolesMask role = (mode == VIEW) ? kLSRolesViewer :  kLSRolesEditor;
268         (void)mode;
269         LSRolesMask role = kLSRolesAll;
270         FSRef outAppRef;
271
272         status = LSGetApplicationForItem(&fileref, role, &outAppRef, NULL);
273         if (status == kLSApplicationNotFoundErr)
274                 return false;
275
276         LSLaunchFSRefSpec inLaunchSpec;
277         inLaunchSpec.appRef = &outAppRef;
278         inLaunchSpec.numDocs = 1;
279         inLaunchSpec.itemRefs = &fileref;
280         inLaunchSpec.passThruParams = NULL;
281         inLaunchSpec.launchFlags = kLSLaunchDefaults;
282         inLaunchSpec.asyncRefCon = NULL;
283         status = LSOpenFromRefSpec(&inLaunchSpec, NULL);
284
285         return status != kLSApplicationNotFoundErr;
286 #else
287         // silence compiler warnings
288         (void)filename;
289         (void)mode;
290
291         // currently, no default viewer is tried for non-windows system
292         // support for KDE/Gnome/Macintosh may be added later
293         return false;
294 #endif
295 }
296
297
298 string real_path(string const & path)
299 {
300 #ifdef HAVE_DEF_PATH_MAX
301         char rpath[PATH_MAX + 1];
302         char * result = realpath(path.c_str(), rpath);
303         return FileName::fromFilesystemEncoding(result ? rpath : path).absFileName();
304 #else
305         char * result = realpath(path.c_str(), NULL);
306         string ret = FileName::fromFilesystemEncoding(result ? result : path).absFileName();
307         free(result);
308         return ret;
309 #endif
310 }
311
312 } // namespace os
313 } // namespace support
314 } // namespace lyx