]> git.lyx.org Git - lyx.git/blobdiff - src/support/os_cygwin.cpp
remove lyxrc dependence from support/*
[lyx.git] / src / support / os_cygwin.cpp
index 886711e89b9d9cc1f1cb6e62e632e109070ed81d..c11a6fdc081e38c6f760291ebda87d80854e4bce 100644 (file)
@@ -22,7 +22,7 @@
 #include <windows.h>
 #include <io.h>
 #include <windef.h>
-#include <shellapi.h>  
+#include <shellapi.h>
 #include <shlwapi.h>
 
 #include <sys/cygwin.h>
@@ -40,6 +40,11 @@ using lyx::support::addName;
 using lyx::support::addPath;
 using lyx::support::package;
 
+// API definition for manually calling font functions on Windows 2000 and later
+typedef int (WINAPI *FONTAPI)(LPCSTR, DWORD, PVOID);
+#define FR_PRIVATE     0x10
+
+// Names of TrueType fonts to load
 string const win_fonts_truetype[] = {"cmex10", "cmmi10", "cmr10", "cmsy10",
        "eufm10", "msam10", "msbm10", "wasy10", "esint10"};
 const int num_fonts_truetype = sizeof(win_fonts_truetype) / sizeof(*win_fonts_truetype);
@@ -140,45 +145,11 @@ string convert_path_list(string const & p, PathStyle const & target)
 
 void os::init(int, char *[])
 {
-       // Copy cygwin environment variables to the Windows environment
-       // if they're not already there.
-
-       char **envp = environ;
-       char curval[2];
-       string var;
-       string val;
-       bool temp_seen = false;
-
-       while (envp && *envp) {
-               val = split(*envp++, var, '=');
-
-               if (var == "TEMP")
-                       temp_seen = true;
-               
-               if (GetEnvironmentVariable(var.c_str(), curval, 2) == 0
-                               && GetLastError() == ERROR_ENVVAR_NOT_FOUND) {
-                       /* Convert to Windows style where necessary */
-                       if (var == "PATH" || var == "LD_LIBRARY_PATH") {
-                               string const winpathlist =
-                                   convert_path_list(val, PathStyle(windows));
-                               if (!winpathlist.empty()) {
-                                       SetEnvironmentVariable(var.c_str(),
-                                               winpathlist.c_str());
-                               }
-                       } else if (var == "HOME" || var == "TMPDIR" ||
-                                       var == "TMP" || var == "TEMP") {
-                               string const winpath =
-                                       convert_path(val, PathStyle(windows));
-                               SetEnvironmentVariable(var.c_str(), winpath.c_str());
-                       } else {
-                               SetEnvironmentVariable(var.c_str(), val.c_str());
-                       }
-               }
-       }
-       if (!temp_seen) {
-               string const winpath = convert_path("/tmp", PathStyle(windows));
-               SetEnvironmentVariable("TEMP", winpath.c_str());
-       }
+       // Make sure that the TEMP variable is set
+       // and sync the Windows environment.
+
+       setenv("TEMP", "/tmp", false);
+       cygwin_internal(CW_SYNC_WINENV);
 }
 
 
@@ -329,13 +300,24 @@ void addFontResources()
 #ifdef X_DISPLAY_MISSING
        // Windows only: Add BaKoMa TrueType font resources
        string const fonts_dir = addPath(package().system_support().absFilename(), "fonts");
-       
+
+       HMODULE hDLL = LoadLibrary("gdi32");
+       FONTAPI pAddFontResourceEx =
+               (FONTAPI) GetProcAddress(hDLL, "AddFontResourceExA");
+
        for (int i = 0 ; i < num_fonts_truetype ; ++i) {
                string const font_current = to_local8bit(from_utf8(convert_path(
                        addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
                        PathStyle(windows))));
-               AddFontResource(font_current.c_str());
+               if (pAddFontResourceEx) {
+                       // Windows 2000 and later: Use AddFontResourceEx
+                       pAddFontResourceEx(font_current.c_str(), FR_PRIVATE, 0);
+               } else {
+                       // Older Windows versions: Use AddFontResource
+                       AddFontResource(font_current.c_str());
+               }
        }
+       FreeLibrary(hDLL);
 #endif
 }
 
@@ -345,13 +327,23 @@ void restoreFontResources()
 #ifdef X_DISPLAY_MISSING
        // Windows only: Remove BaKoMa TrueType font resources
        string const fonts_dir = addPath(package().system_support().absFilename(), "fonts");
-       
+
+       HMODULE hDLL = LoadLibrary("gdi32");
+       FONTAPI pRemoveFontResourceEx = (FONTAPI) GetProcAddress(hDLL, "RemoveFontResourceExA");
+
        for(int i = 0 ; i < num_fonts_truetype ; ++i) {
                string const font_current = to_local8bit(from_utf8(convert_path(
                        addName(fonts_dir, win_fonts_truetype[i] + ".ttf"),
                        PathStyle(windows))));
-               RemoveFontResource(font_current.c_str());
+               if (pRemoveFontResourceEx) {
+                       // Windows 2000 and later: Use RemoveFontResourceEx
+                       pRemoveFontResourceEx(font_current.c_str(), FR_PRIVATE, 0);
+               } else {
+                       // Older Windows versions: Use RemoveFontResource
+                       RemoveFontResource(font_current.c_str());
+               }
        }
+       FreeLibrary(hDLL);
 #endif
 }