]> git.lyx.org Git - lyx.git/blob - src/support/os_win32.C
Fix bug 2029 (RtL space width)
[lyx.git] / src / support / os_win32.C
1 /**
2  * \file os_win32.C
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  * \author Claus Hentschel
8  * \author Angus Leeming
9  *
10  * Full author contact details are available in file CREDITS.
11  *
12  * Various OS specific functions
13  */
14
15 #include <config.h>
16
17 #include "support/os.h"
18 #include "support/lstrings.h"
19
20 #include "debug.h"
21
22 #include <windows.h>
23 #include <io.h>
24 #include <direct.h> // _getdrive
25
26 using std::endl;
27 using std::string;
28
29
30 namespace lyx {
31 namespace support {
32 namespace os {
33
34 void os::init(int /* argc */, char * argv[])
35 {
36         /* Note from Angus, 17 Jan 2005:
37          *
38          * The code below is taken verbatim from Ruurd's original patch
39          * porting LyX to Win32.
40          *
41          * Windows allows us to define LyX either as a console-based app
42          * or as a GUI-based app. Ruurd decided to define LyX as a
43          * console-based app with a "main" function rather than a "WinMain"
44          * function as the point of entry to the program, but to
45          * immediately close the console window that Windows helpfully
46          * opens for us. Doing so allows the user to see all of LyX's
47          * debug output simply by running LyX from a DOS or MSYS-shell
48          * prompt.
49          *
50          * The alternative approach is to define LyX as a genuine
51          * GUI-based app, with a "WinMain" function as the entry point to the
52          * executable rather than a "main" function, so:
53          *
54          * #if defined (_WIN32)
55          * # define WIN32_LEAN_AND_MEAN
56          * # include <stdlib.h>  // for __argc, __argv
57          * # include <windows.h> // for WinMain
58          * #endif
59          *
60          * // This will require the "-mwindows" flag when linking with
61          * // gcc under MinGW.
62          * // For MSVC, use "/subsystem:windows".
63          * #if defined (_WIN32)
64          * int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
65          * {
66          *     return mymain(__argc, __argv);
67          * }
68          * #endif
69          *
70          * where "mymain" is just a renamed "main".
71          *
72          * However, doing so means that the lyxerr messages would mysteriously
73          * disappear. They could be resurrected with something like:
74          *
75          * #ifdef WIN32
76          *  AllocConsole();
77          *  freopen("conin$","r",stdin);
78          *  freopen("conout$","w",stdout);
79          *  freopen("conout$","w",stderr);
80          * #endif
81          *
82          * This code could be invoked (say) the first time that lyxerr
83          * is called. However, Ruurd has tried this route and found that some
84          * shell scripts failed, for mysterious reasons...
85          *
86          * I've chosen for now, therefore, to simply add Ruurd's original
87          * code as-is.
88          */
89         // Close the console when run (probably)
90         // not run from command prompt
91         char WindowTitle[1024];
92         if (GetConsoleTitle(WindowTitle, sizeof(WindowTitle)) == 0) {
93                 // Could not get the title, so we just leave things as they are
94                 return;
95         }
96
97         if ((strcmp(WindowTitle, argv[0]) == 0) ||
98                 (strcmp(WindowTitle, "LyX") == 0)) {
99                 // format a "unique" newWindowTitle
100                 wsprintf(WindowTitle, "%d/%d",
101                         GetTickCount(),
102                         GetCurrentProcessId());
103                 // change current window title
104                 SetConsoleTitle(WindowTitle);
105                 // ensure window title has been updated
106                 Sleep(40);
107                 // look for newWindowTitle
108                 HWND const hwndFound = FindWindow(NULL, WindowTitle);
109                 // If found, hide it
110                 if (hwndFound != NULL)
111                         ShowWindow( hwndFound, SW_HIDE);
112         }
113 }
114
115
116 string current_root()
117 {
118         // _getdrive returns the current drive (1=A, 2=B, and so on).
119         char const drive = ::_getdrive() + 'A' - 1;
120         return string(1, drive) + ":/";
121 }
122
123
124 string::size_type common_path(string const & p1, string const & p2)
125 {
126         string::size_type i = 0;
127         string::size_type       p1_len = p1.length();
128         string::size_type       p2_len = p2.length();
129         while (i < p1_len && i < p2_len && uppercase(p1[i]) == uppercase(p2[i]))
130                 ++i;
131         if ((i < p1_len && i < p2_len)
132             || (i < p1_len && p1[i] != '/' && i == p2_len)
133             || (i < p2_len && p2[i] != '/' && i == p1_len))
134         {
135                 if (i)
136                         --i;     // here was the last match
137                 while (i && p1[i] != '/')
138                         --i;
139         }
140         return i;
141 }
142
143
144 string external_path(string const & p)
145 {
146         string const dos_path = subst(p, "/", "\\");
147
148         lyxerr[Debug::LATEX]
149                 << "<Win32 path correction> ["
150                 << p << "]->>["
151                 << dos_path << ']' << endl;
152         return dos_path;
153 }
154
155
156 // (Claus H.) Parsing the latex log file in an Win32 environment all
157 // files are mentioned in Win32/DOS syntax. Because LyX uses the dep file
158 // entries to check if any file has been changed we must retranslate
159 // the Win32/DOS pathnames into Cygwin pathnames.
160 string internal_path(string const & p)
161 {
162         return subst(p, "\\", "/");
163 }
164
165
166 // (Claus H.) On Win32 both Unix and Win32/DOS pathnames are used.
167 // Therefore an absolute path could be either a pathname starting
168 // with a slash (Unix) or a pathname starting with a drive letter
169 // followed by a colon. Because a colon is not valid in pathes in Unix
170 // and at another location in Win32 testing just for the existance
171 // of the colon in the 2nd position seems to be enough!
172 bool is_absolute_path(string const & p)
173 {
174         if (p.empty())
175                 return false;
176
177         bool isDosPath = (p.length() > 1 && p[1] == ':');
178         bool isUnixPath = (p[0] == '/');
179
180         return isDosPath || isUnixPath;
181 }
182
183
184 // returns a string suitable to be passed to popen when
185 // reading a pipe
186 char const * popen_read_mode()
187 {
188         return "r";
189 }
190
191
192 string const & nulldev()
193 {
194         static string const nulldev_ = "nul";
195         return nulldev_;
196 }
197
198
199 shell_type shell()
200 {
201         return CMD_EXE;
202 }
203
204
205 char path_separator()
206 {
207         return ';';
208 }
209
210
211 void cygwin_path_fix(bool)
212 {}
213
214 } // namespace os
215 } // namespace support
216 } // namespace lyx