]> git.lyx.org Git - lyx.git/blobdiff - src/support/os_win32.cpp
Reorder a bit status messages, but they are still cleared at the end of LyXFunc
[lyx.git] / src / support / os_win32.cpp
index 732cdc55aba613e579b39c7c42a39c19539ade43..f244ecbdd4823e5c8e9906e0d20b1fcbdd4a66e7 100644 (file)
@@ -71,6 +71,9 @@
 using namespace std;
 
 namespace lyx {
+
+void emergencyCleanup();
+
 namespace support {
 namespace os {
 
@@ -80,6 +83,17 @@ bool windows_style_tex_paths_ = true;
 
 string cygdrive = "/cygdrive";
 
+BOOL terminate_handler(DWORD event)
+{
+       if (event == CTRL_CLOSE_EVENT
+           || event == CTRL_LOGOFF_EVENT
+           || event == CTRL_SHUTDOWN_EVENT) {
+               lyx::emergencyCleanup();
+               return TRUE;
+       }
+       return FALSE;
+}
+
 } // namespace anon
 
 void init(int /* argc */, char * argv[])
@@ -161,6 +175,9 @@ void init(int /* argc */, char * argv[])
                if ((retVal == ERROR_SUCCESS) && (bufSize <= MAX_PATH))
                        cygdrive = rtrim(string(buf), "/");
        }
+
+       // Catch shutdown events.
+       SetConsoleCtrlHandler((PHANDLER_ROUTINE)terminate_handler, TRUE);
 }
 
 
@@ -193,17 +210,14 @@ docstring::size_type common_path(docstring const & p1, docstring const & p2)
                        --i;     // here was the last match
                while (i && p1[i] != '/')
                        --i;
-       } else
-               --i;
-
+       }
        return i;
 }
 
 
 bool path_prefix_is(string const & path, string const & pre)
 {
-       string tmp = path;
-       return path_prefix_is(tmp, pre, CASE_UNCHANGED);
+       return path_prefix_is(const_cast<string &>(path), pre, CASE_UNCHANGED);
 }
 
 
@@ -211,13 +225,23 @@ bool path_prefix_is(string & path, string const & pre, path_case how)
 {
        docstring const p1 = from_utf8(path);
        docstring const p2 = from_utf8(pre);
-       docstring::size_type i = common_path(p1, p2);
+       docstring::size_type const p1_len = p1.length();
+       docstring::size_type const p2_len = p2.length();
+       docstring::size_type common_len = common_path(p1, p2);
 
-       if (i + 1 != p2.length())
+       if (p2[p2_len - 1] == '/' && p1_len != p2_len)
+               ++common_len;
+
+       if (common_len != p2_len)
                return false;
 
-       if (!prefixIs(path, pre) && how == CASE_ADJUSTED)
-               path = to_utf8(p2 + p1.substr(i + 1, p1.length() - i + 1));
+       if (how == CASE_ADJUSTED && !prefixIs(path, pre)) {
+               if (p1_len < common_len)
+                       path = to_utf8(p2.substr(0, p1_len));
+               else
+                       path = to_utf8(p2 + p1.substr(common_len,
+                                                       p1_len - common_len));
+       }
 
        return true;
 }