OSDN Git Service

Rewrite CUtils::GetWindowTextType.
authorKazuhiro Fujieda <fujieda@users.sourceforge.jp>
Thu, 10 Nov 2011 13:33:20 +0000 (22:33 +0900)
committerKazuhiro Fujieda <fujieda@users.sourceforge.jp>
Mon, 9 Jan 2012 01:45:45 +0000 (10:45 +0900)
Move IsMatchWindowText from CXkeymacsDll to CUtils and rewrite it.

xkeymacsdll/Utils.cpp
xkeymacsdll/Utils.h
xkeymacsdll/xkeymacsdll.cpp
xkeymacsdll/xkeymacsdll.h

index b8c9756..1554364 100644 (file)
@@ -12,6 +12,39 @@ bool CUtils::IsConsole()
        return AppName::IsConsole();\r
 }\r
 \r
+int CUtils::GetWindowTextType(const CString& text)\r
+{\r
+       int len = text.GetAllocLength();\r
+       if (len >= 3 && text[0] == _T('*') && text[len - 1] == _T('*'))\r
+               return IDS_WINDOW_TEXT_MATCH;\r
+       else if (len >= 2 && text[0] != _T('*') && text[len - 1] == _T('*'))\r
+               return IDS_WINDOW_TEXT_MATCH_FORWARD;\r
+       else if (len >= 2 && text[0] == _T('*') && text[len - 1] != _T('*'))\r
+               return IDS_WINDOW_TEXT_MATCH_BACKWARD;\r
+       else if (len > 0 && text[0] != _T('*') && text[len - 1] != _T('*'))\r
+               return IDS_WINDOW_TEXT_MATCH_FULL;\r
+       return IDS_WINDOW_TEXT_IGNORE;\r
+}\r
+\r
+bool CUtils::IsMatchWindowText(const CString& text)\r
+{\r
+       int len = text.GetLength();\r
+       TCHAR buf[WINDOW_TEXT_LENGTH];\r
+       GetWindowText(GetForegroundWindow(), buf, sizeof(buf));\r
+       CString current(buf);\r
+       switch (GetWindowTextType(text)) {\r
+       case IDS_WINDOW_TEXT_MATCH:                                                             // *foo*\r
+               return current.Find(text.Mid(1, len - 2)) >= 0;\r
+       case IDS_WINDOW_TEXT_MATCH_FORWARD:                                             // foo*\r
+               return current.Left(len - 1) == text.Left(len - 1);\r
+       case IDS_WINDOW_TEXT_MATCH_BACKWARD:                                    // *foo\r
+               return current.Right(len - 1) == text.Right(len - 1);\r
+       case IDS_WINDOW_TEXT_MATCH_FULL:                                                // foo\r
+               return current == text;\r
+       }\r
+       return true;\r
+}\r
+\r
 BOOL CUtils::GetFindDialogTitle(CString *szDialogTitle)\r
 {\r
        {\r
@@ -687,31 +720,6 @@ BOOL CUtils::IsThunderbird()
        return AppName::Match(_T("thunderbird.exe"));\r
 }\r
 \r
-int CUtils::GetWindowTextType(CString strWindowText)\r
-{\r
-       int nWindowTextType = IDS_WINDOW_TEXT_IGNORE;\r
-\r
-       if (3 <= strWindowText.GetLength()\r
-        && strWindowText.Find(_T('*')) == 0\r
-        && strWindowText.ReverseFind(_T('*')) == strWindowText.GetLength() - 1) {\r
-               nWindowTextType = IDS_WINDOW_TEXT_MATCH;\r
-       } else if (2 <= strWindowText.GetLength()\r
-                       && strWindowText.Find(_T('*')) != 0\r
-                       && strWindowText.ReverseFind(_T('*')) == strWindowText.GetLength() - 1) {\r
-               nWindowTextType = IDS_WINDOW_TEXT_MATCH_FORWARD;\r
-       } else if (2 <= strWindowText.GetLength()\r
-                       && strWindowText.Find(_T('*')) == 0\r
-                       && strWindowText.ReverseFind(_T('*')) != strWindowText.GetLength() - 1) {\r
-               nWindowTextType = IDS_WINDOW_TEXT_MATCH_BACKWARD;\r
-       } else if (!strWindowText.IsEmpty()\r
-                       && strWindowText.Find(_T('*')) != 0\r
-                       && strWindowText.ReverseFind(_T('*')) != strWindowText.GetLength() - 1) {\r
-               nWindowTextType = IDS_WINDOW_TEXT_MATCH_FULL;\r
-       }\r
-\r
-       return nWindowTextType;\r
-}\r
-\r
 BOOL CUtils::IsLunascape()\r
 {\r
        return AppName::Match(_T("Luna.exe"));\r
index ef40e07..3cee61b 100644 (file)
@@ -15,10 +15,11 @@ class AFX_EXT_CLASS CUtils
 {\r
 public:\r
        static bool IsConsole();\r
+       static int GetWindowTextType(const CString& text);\r
+       static bool IsMatchWindowText(const CString& text);\r
        static BOOL GetFindDialogTitle(CString *szDialogTitle);\r
        static BOOL IsAtok();\r
        static BOOL IsLunascape();\r
-       static int GetWindowTextType(CString strWindowText);\r
        static BOOL IsThunderbird();\r
        static BOOL IsOpenJane();\r
        static BOOL IsVisualSlickEdit();\r
index ce1058a..292aff3 100644 (file)
@@ -439,10 +439,12 @@ void CXkeymacsDll::InitKeyboardProc(bool imeState)
        AppName::Init();\r
        AppName::SetIMEState(imeState);\r
 \r
-       if (_tcsnicmp(m_Config.szSpecialApp[m_nAppID], AppName::GetAppName(), 0xF) || !IsMatchWindowText(m_Config.szWindowText[m_nAppID])) {    // PROCESSENTRY32 has only 0xF bytes of Name\r
+       if (_tcsnicmp(m_Config.szSpecialApp[m_nAppID], AppName::GetAppName(), 0xF) || // PROCESSENTRY32 has only 0xF bytes of Name\r
+                       !CUtils::IsMatchWindowText(m_Config.szWindowText[m_nAppID])) {\r
                m_nAppID = -1;\r
                for (int nAppID = 0; nAppID < MAX_APP; ++nAppID) {\r
-                       if (_tcsnicmp(m_Config.szSpecialApp[nAppID], AppName::GetAppName(), 0xF) || !IsMatchWindowText(m_Config.szWindowText[nAppID]))\r
+                       if (_tcsnicmp(m_Config.szSpecialApp[nAppID], AppName::GetAppName(), 0xF) ||\r
+                                       !CUtils::IsMatchWindowText(m_Config.szWindowText[nAppID]))\r
                                continue;\r
                        if (m_nAppID < 0)\r
                                m_nAppID = nAppID;\r
@@ -1423,38 +1425,6 @@ BYTE CXkeymacsDll::a2v(TCHAR nAscii)
        }\r
 }\r
 \r
-BOOL CXkeymacsDll::IsMatchWindowText(CString szWindowText)\r
-{\r
-       BOOL bIsMatchWindowText = TRUE;\r
-\r
-       TCHAR szCurrentWindowText[WINDOW_TEXT_LENGTH] = {'\0'};\r
-       GetWindowText(GetForegroundWindow(), szCurrentWindowText, sizeof(szCurrentWindowText));\r
-\r
-       switch (CUtils::GetWindowTextType(szWindowText)) {\r
-       case IDS_WINDOW_TEXT_MATCH:                                                             // *foo*\r
-               szWindowText.Delete(0);                                                         // Delete first '*'\r
-               szWindowText.Delete(szWindowText.GetLength() - 1);      // Delete last '*'\r
-               bIsMatchWindowText = 0 <= CString(szCurrentWindowText).Find(szWindowText);\r
-               break;\r
-       case IDS_WINDOW_TEXT_MATCH_FORWARD:                                             // foo*\r
-               szWindowText.Delete(szWindowText.GetLength() - 1);      // Delete last '*'\r
-               bIsMatchWindowText = 0 == CString(szCurrentWindowText).Find(szWindowText);\r
-               break;\r
-       case IDS_WINDOW_TEXT_MATCH_BACKWARD:                                    // *foo\r
-               szWindowText.Delete(0);                                                         // Delete first '*'\r
-               bIsMatchWindowText = 0 <= CString(szCurrentWindowText).Find(szWindowText, CString(szCurrentWindowText).GetLength() - szWindowText.GetLength());\r
-               break;\r
-       case IDS_WINDOW_TEXT_MATCH_FULL:                                                // foo\r
-               bIsMatchWindowText = szWindowText == CString(szCurrentWindowText);\r
-               break;\r
-       case IDS_WINDOW_TEXT_IGNORE:                                                    // *\r
-               bIsMatchWindowText = TRUE;\r
-               break;\r
-       }\r
-//     CUtils::Log(_T("IsMatchWindowText: %d, _%s_, _%s_"), bIsMatchWindowText, szCurrentWindowText, szWindowText);\r
-       return bIsMatchWindowText;\r
-}\r
-\r
 void CXkeymacsDll::SetAccelerate(int nAccelerate)\r
 {\r
        m_nAccelerate = nAccelerate;\r
index ac4a4fd..7d0c3ed 100644 (file)
@@ -92,7 +92,6 @@ private:
        static KeyBind ParseKey(LPCTSTR& def);\r
        static BOOL IsShift(TCHAR nAscii);\r
        static BYTE a2v(TCHAR nAscii);\r
-       static BOOL IsMatchWindowText(CString szWindowText);\r
        static int m_nAccelerate;\r
        static int m_nKeyboardSpeed;\r
        static HCURSOR m_hCurrentCursor;\r