OSDN Git Service

forgot to "git add" modified files, so re-commit:
[yamy/yamy.git] / dlginvestigate.cpp
1 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 // dlginvestigate.cpp
3
4
5 #include "misc.h"
6 #include "engine.h"
7 #include "focus.h"
8 #include "hook.h"
9 #include "mayurc.h"
10 #include "stringtool.h"
11 #include "target.h"
12 #include "windowstool.h"
13 #include "vkeytable.h"
14 #include "dlginvestigate.h"
15 #include <iomanip>
16
17
18 ///
19 class DlgInvestigate
20 {
21   HWND m_hwnd;                                  ///
22   UINT m_WM_MAYU_MESSAGE;                       ///
23   DlgInvestigateData m_data;                    /// 
24   
25 public:
26   ///
27   DlgInvestigate(HWND i_hwnd)
28     : m_hwnd(i_hwnd),
29       m_WM_MAYU_MESSAGE(RegisterWindowMessage(
30                             addSessionId(WM_MAYU_MESSAGE_NAME).c_str()))
31   {
32     m_data.m_engine = NULL;
33     m_data.m_hwndLog = NULL;
34   }
35   
36   /// WM_INITDIALOG
37   BOOL wmInitDialog(HWND /* i_focus */, LPARAM i_lParam)
38   {
39     m_data = *reinterpret_cast<DlgInvestigateData *>(i_lParam);
40     setSmallIcon(m_hwnd, IDI_ICON_mayu);
41     setBigIcon(m_hwnd, IDI_ICON_mayu);
42     return TRUE;
43   }
44   
45   /// WM_DESTROY
46   BOOL wmDestroy()
47   {
48     unsetSmallIcon(m_hwnd);
49     unsetBigIcon(m_hwnd);
50     return TRUE;
51   }
52   
53   /// WM_CLOSE
54   BOOL wmClose()
55   {
56     ShowWindow(m_hwnd, SW_HIDE);
57     return TRUE;
58   }
59
60   /// WM_COMMAND
61   BOOL wmCommand(int /* i_notifyCode */, int i_id, HWND /* i_hwndControl */)
62   {
63     switch (i_id)
64     {
65       case IDOK:
66       {
67         ShowWindow(m_hwnd, SW_HIDE);
68         return TRUE;
69       }
70     }
71     return FALSE;
72   }
73
74   /// WM_focusNotify
75   BOOL wmFocusNotify(bool i_isFocused, HWND i_hwndFocus)
76   {
77     if (m_data.m_engine &&
78         i_hwndFocus == GetDlgItem(m_hwnd, IDC_CUSTOM_scancode))
79       m_data.m_engine->enableLogMode(i_isFocused);
80     return TRUE;
81   }
82   
83   /// WM_targetNotify
84   BOOL wmTargetNotify(HWND i_hwndTarget)
85   {
86     _TCHAR className[GANA_MAX_ATOM_LENGTH];
87     bool ok = false;
88     if (GetClassName(i_hwndTarget, className, NUMBER_OF(className)))
89     {
90       if (_tcsicmp(className, _T("ConsoleWindowClass")) == 0)
91       {
92         _TCHAR titleName[1024];
93         if (GetWindowText(i_hwndTarget, titleName, NUMBER_OF(titleName)) == 0)
94           titleName[0] = _T('\0');
95         {
96           Acquire a(&m_data.m_engine->m_log, 1);
97           m_data.m_engine->m_log << _T("HWND:\t") << std::hex
98                                  << reinterpret_cast<int>(i_hwndTarget)
99                                  << std::dec << std::endl;
100         }
101         Acquire a(&m_data.m_engine->m_log, 0);
102         m_data.m_engine->m_log << _T("CLASS:\t") << className << std::endl;
103         m_data.m_engine->m_log << _T("TITLE:\t") << titleName << std::endl;
104         ok = true;
105       }
106     }
107     if (!ok)
108       CHECK_TRUE( PostMessage(i_hwndTarget, m_WM_MAYU_MESSAGE,
109                               MayuMessage_notifyName, 0) );
110     return TRUE;
111   }
112   
113   /// WM_vkeyNotify
114   BOOL wmVkeyNotify(int i_nVirtKey, int /* i_repeatCount */,
115                     BYTE /* i_scanCode */, bool i_isExtended,
116                     bool /* i_isAltDown */, bool i_isKeyup)
117   {
118     Acquire a(&m_data.m_engine->m_log, 0);
119     m_data.m_engine->m_log
120       << (i_isExtended ? _T(" E-") : _T("   "))
121       << _T("0x") << std::hex << std::setw(2) << std::setfill(_T('0'))
122       << i_nVirtKey << std::dec << _T("  &VK( ")
123       << (i_isExtended ? _T("E-") : _T("  "))
124       << (i_isKeyup ? _T("U-") : _T("D-"));
125     
126     for (const VKeyTable *vkt = g_vkeyTable; vkt->m_name; ++ vkt)
127     {
128       if (vkt->m_code == i_nVirtKey)
129       {
130         m_data.m_engine->m_log << vkt->m_name << _T(" )") << std::endl;
131         return TRUE;
132       }
133     }
134     m_data.m_engine->m_log << _T("0x") << std::hex << std::setw(2)
135                            << std::setfill(_T('0')) << i_nVirtKey << std::dec
136                            << _T(" )") << std::endl;
137     return TRUE;
138   }
139
140   BOOL wmMove(int /* i_x */, int /* i_y */)
141   {
142     RECT rc1, rc2;
143     GetWindowRect(m_hwnd, &rc1);
144     GetWindowRect(m_data.m_hwndLog, &rc2);
145     
146     MoveWindow(m_data.m_hwndLog, rc1.left, rc1.bottom,
147                rcWidth(&rc2), rcHeight(&rc2), TRUE);
148     
149     return TRUE;
150   }
151 };
152
153
154 //
155 #ifdef MAYU64
156 INT_PTR CALLBACK dlgInvestigate_dlgProc(HWND i_hwnd, UINT i_message,
157 #else
158 BOOL CALLBACK dlgInvestigate_dlgProc(HWND i_hwnd, UINT i_message,
159 #endif
160                                      WPARAM i_wParam, LPARAM i_lParam)
161 {
162   DlgInvestigate *wc;
163   getUserData(i_hwnd, &wc);
164   if (!wc)
165     switch (i_message)
166     {
167       case WM_INITDIALOG:
168         wc = setUserData(i_hwnd, new DlgInvestigate(i_hwnd));
169         return wc->wmInitDialog(reinterpret_cast<HWND>(i_wParam), i_lParam);
170     }
171   else
172     switch (i_message)
173     {
174       case WM_MOVE:
175         return wc->wmMove(static_cast<short>(LOWORD(i_lParam)),
176                           static_cast<short>(HIWORD(i_lParam)));
177       case WM_COMMAND:
178         return wc->wmCommand(HIWORD(i_wParam), LOWORD(i_wParam),
179                              reinterpret_cast<HWND>(i_lParam));
180       case WM_CLOSE:
181         return wc->wmClose();
182       case WM_DESTROY:
183         return wc->wmDestroy();
184       case WM_APP_notifyFocus:
185         return wc->wmFocusNotify(!!i_wParam,
186                                  reinterpret_cast<HWND>(i_lParam));
187       case WM_APP_targetNotify:
188         return wc->wmTargetNotify(reinterpret_cast<HWND>(i_lParam));
189       case WM_APP_notifyVKey:
190         return wc->wmVkeyNotify(
191           static_cast<int>(i_wParam), static_cast<int>(i_lParam & 0xffff),
192           static_cast<BYTE>((i_lParam >> 16) & 0xff),
193           !!(i_lParam & (1 << 24)),
194           !!(i_lParam & (1 << 29)),
195           !!(i_lParam & (1 << 31)));
196       case WM_NCDESTROY:
197         delete wc;
198         return TRUE;
199     }
200   return FALSE;
201 }