コミットメタ情報

リビジョン6802fefaae9627933d5dc314d22c8a0dfda69190 (tree)
日時2012-01-27 23:58:24
作者Kazuhiro Fujieda <fujieda@user...>
コミッターKazuhiro Fujieda

ログメッセージ

Make some members related to status icons static in CMainFrame.
Simplify the code related to them.

変更サマリ

差分

--- a/xkeymacs/mainfrm.cpp
+++ b/xkeymacs/mainfrm.cpp
@@ -17,6 +17,13 @@ static char THIS_FILE[] = __FILE__;
1717 /////////////////////////////////////////////////////////////////////////////
1818 // CMainFrame
1919
20+BOOL CMainFrame::m_bIcon[MAX_ICON_TYPE];
21+HICON CMainFrame::m_hIcon[MAX_ICON_TYPE][MAX_STATUS];
22+DWORD CMainFrame::m_dwOldMessage[MAX_ICON_TYPE];
23+NOTIFYICONDATA CMainFrame::m_stNtfyIcon[MAX_ICON_TYPE];
24+NOTIFYICONDATA CMainFrame::m_stOldNtfyIcon[MAX_ICON_TYPE];
25+bool CMainFrame::m_bPollIconMessage;
26+
2027 IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)
2128
2229 BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
@@ -59,8 +66,6 @@ CMainFrame::CMainFrame()
5966 }
6067 memset(m_nResultKeyboardDlg, -1, sizeof(m_nResultKeyboardDlg));
6168 memset(m_bKeyboardDlgExist, 0, sizeof(m_bKeyboardDlgExist));
62- memset(m_stOldNtfyIcon, 0, sizeof(m_stOldNtfyIcon));
63- memset(m_dwOldMessage, 0, sizeof(m_dwOldMessage));
6469
6570 // register window class
6671 WNDCLASS stWndClass;
@@ -107,41 +112,8 @@ CMainFrame::~CMainFrame()
107112
108113 int CMainFrame::OnCreate(const LPCREATESTRUCT lpCreateStruct)
109114 {
110- if (CFrameWnd::OnCreate(lpCreateStruct) == -1) {
115+ if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
111116 return -1;
112- }
113-
114- // init notify icon data
115- NOTIFYICONDATA notifyIconData[MAX_ICON_TYPE] = {
116- { sizeof(NOTIFYICONDATA), m_hWnd, MAIN_ICON, NIF_MESSAGE | NIF_ICON | NIF_TIP, WM_USER_NTFYICON,
117- (HICON)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 16, 16, LR_SHARED) },
118- { sizeof(NOTIFYICONDATA), m_hWnd, CX_ICON, NIF_MESSAGE | NIF_ICON | NIF_TIP, WM_USER_NTFYICON,
119- AfxGetApp()->LoadIcon(IDI_CX_OFF) },
120- { sizeof(NOTIFYICONDATA), m_hWnd, MX_ICON, NIF_MESSAGE | NIF_ICON | NIF_TIP, WM_USER_NTFYICON,
121- AfxGetApp()->LoadIcon(IDI_MX_OFF) },
122- { sizeof(NOTIFYICONDATA), m_hWnd, META_ICON, NIF_MESSAGE | NIF_ICON | NIF_TIP, WM_USER_NTFYICON,
123- AfxGetApp()->LoadIcon(IDI_META_OFF) },
124- { sizeof(NOTIFYICONDATA), m_hWnd, SHIFT_ICON, NIF_MESSAGE | NIF_ICON | NIF_TIP, WM_USER_NTFYICON,
125- AfxGetApp()->LoadIcon(IDI_SHIFT_OFF) },
126- { sizeof(NOTIFYICONDATA), m_hWnd, CTRL_ICON, NIF_MESSAGE | NIF_ICON | NIF_TIP, WM_USER_NTFYICON,
127- AfxGetApp()->LoadIcon(IDI_CTRL_OFF) },
128- { sizeof(NOTIFYICONDATA), m_hWnd, ALT_ICON, NIF_MESSAGE | NIF_ICON | NIF_TIP, WM_USER_NTFYICON,
129- AfxGetApp()->LoadIcon(IDI_ALT_OFF) },
130- };
131- LoadString(AfxGetInstanceHandle(), AFX_IDS_APP_TITLE, notifyIconData[MAIN_ICON].szTip,
132- sizeof(notifyIconData[MAIN_ICON].szTip));
133- LoadString(AfxGetInstanceHandle(), IDS_TIP_CX, notifyIconData[CX_ICON].szTip,
134- sizeof(notifyIconData[CX_ICON].szTip));
135- LoadString(AfxGetInstanceHandle(), IDS_TIP_MX, notifyIconData[MX_ICON].szTip,
136- sizeof(notifyIconData[MX_ICON].szTip));
137- LoadString(AfxGetInstanceHandle(), IDS_TIP_META, notifyIconData[META_ICON].szTip,
138- sizeof(notifyIconData[META_ICON].szTip));
139- LoadString(AfxGetInstanceHandle(), IDS_TIP_SHIFT, notifyIconData[SHIFT_ICON].szTip,
140- sizeof(notifyIconData[SHIFT_ICON].szTip));
141- LoadString(AfxGetInstanceHandle(), IDS_TIP_CTRL, notifyIconData[CTRL_ICON].szTip,
142- sizeof(notifyIconData[CTRL_ICON].szTip));
143- LoadString(AfxGetInstanceHandle(), IDS_TIP_ALT, notifyIconData[ALT_ICON].szTip,
144- sizeof(notifyIconData[ALT_ICON].szTip));
145117
146118 // set notify icon data
147119 m_hIcon[MAIN_ICON][STATUS_ENABLE] = (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 16, 16, LR_SHARED),
@@ -149,50 +121,24 @@ int CMainFrame::OnCreate(const LPCREATESTRUCT lpCreateStruct)
149121 m_hIcon[MAIN_ICON][STATUS_DISABLE_WOCQ] = (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAIN_DISABLE_WOCQ), IMAGE_ICON, 16, 16, LR_SHARED);
150122 m_hIcon[MAIN_ICON][STATUS_DISABLE] = (HICON)LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAIN_DISABLE), IMAGE_ICON, 16, 16, LR_SHARED);
151123 m_bIcon[MAIN_ICON] = AfxGetApp()->GetProfileInt(CString(), CString(MAKEINTRESOURCE(IDS_REG_ENTRY_MAIN)), 1);
152- m_stNtfyIcon[MAIN_ICON] = notifyIconData[MAIN_ICON];
124+ NOTIFYICONDATA main = { sizeof(NOTIFYICONDATA), m_hWnd, MAIN_ICON, NIF_MESSAGE | NIF_ICON | NIF_TIP, WM_USER_NTFYICON,
125+ (HICON)::LoadImage(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME), IMAGE_ICON, 16, 16, LR_SHARED) };
126+ LoadString(AfxGetInstanceHandle(), AFX_IDS_APP_TITLE, main.szTip, _countof(main.szTip));
127+ m_stNtfyIcon[MAIN_ICON] = main;
153128 AddShell_NotifyIcon(MAIN_ICON);
154129
155130 // set notify ^X icon data
156- m_hIcon[CX_ICON][ON_ICON] = AfxGetApp()->LoadIcon(IDI_CX_ON);
157- m_hIcon[CX_ICON][OFF_ICON] = AfxGetApp()->LoadIcon(IDI_CX_OFF);
158- m_bIcon[CX_ICON] = AfxGetApp()->GetProfileInt(CString(), CString(MAKEINTRESOURCE(IDS_REG_ENTRY_CX)), 0);
159- m_stNtfyIcon[CX_ICON] = notifyIconData[CX_ICON];
160- AddShell_NotifyIcon(CX_ICON);
161-
131+ SetIconData(CX_ICON, IDS_TIP_CX, IDI_CX_ON, IDI_CX_OFF, IDS_REG_ENTRY_CX);
162132 // set notify M-x icon data
163- m_hIcon[MX_ICON][ON_ICON] = AfxGetApp()->LoadIcon(IDI_MX_ON);
164- m_hIcon[MX_ICON][OFF_ICON] = AfxGetApp()->LoadIcon(IDI_MX_OFF);
165- m_bIcon[MX_ICON] = AfxGetApp()->GetProfileInt(CString(), CString(MAKEINTRESOURCE(IDS_REG_ENTRY_MX)), 0);
166- m_stNtfyIcon[MX_ICON] = notifyIconData[MX_ICON];
167- AddShell_NotifyIcon(MX_ICON);
168-
133+ SetIconData(MX_ICON, IDS_TIP_MX, IDI_MX_ON, IDI_MX_OFF, IDS_REG_ENTRY_MX);
169134 // set notify Meta icon data
170- m_hIcon[META_ICON][ON_ICON] = AfxGetApp()->LoadIcon(IDI_META_ON);
171- m_hIcon[META_ICON][OFF_ICON] = AfxGetApp()->LoadIcon(IDI_META_OFF);
172- m_bIcon[META_ICON] = AfxGetApp()->GetProfileInt(CString(), CString(MAKEINTRESOURCE(IDS_REG_ENTRY_META)), 0);
173- m_stNtfyIcon[META_ICON] = notifyIconData[META_ICON];
174- AddShell_NotifyIcon(META_ICON);
175-
135+ SetIconData(META_ICON, IDS_TIP_META, IDI_META_ON, IDI_META_OFF, IDS_REG_ENTRY_META);
176136 // set notify Shift icon data
177- m_hIcon[SHIFT_ICON][ON_ICON] = AfxGetApp()->LoadIcon(IDI_SHIFT_ON);
178- m_hIcon[SHIFT_ICON][OFF_ICON] = AfxGetApp()->LoadIcon(IDI_SHIFT_OFF);
179- m_bIcon[SHIFT_ICON] = AfxGetApp()->GetProfileInt(CString(), CString(MAKEINTRESOURCE(IDS_REG_ENTRY_SHIFT)), 0);
180- m_stNtfyIcon[SHIFT_ICON] = notifyIconData[SHIFT_ICON];
181- AddShell_NotifyIcon(SHIFT_ICON);
182-
137+ SetIconData(SHIFT_ICON, IDS_TIP_SHIFT, IDI_SHIFT_ON, IDI_SHIFT_OFF, IDS_REG_ENTRY_SHIFT);
183138 // set notify Ctrl icon data
184- m_hIcon[CTRL_ICON][ON_ICON] = AfxGetApp()->LoadIcon(IDI_CTRL_ON);
185- m_hIcon[CTRL_ICON][OFF_ICON] = AfxGetApp()->LoadIcon(IDI_CTRL_OFF);
186- m_bIcon[CTRL_ICON] = AfxGetApp()->GetProfileInt(CString(), CString(MAKEINTRESOURCE(IDS_REG_ENTRY_CTRL)), 0);
187- m_stNtfyIcon[CTRL_ICON] = notifyIconData[CTRL_ICON];
188- AddShell_NotifyIcon(CTRL_ICON);
189-
139+ SetIconData(CTRL_ICON, IDS_TIP_CTRL, IDI_CTRL_ON, IDI_CTRL_OFF, IDS_REG_ENTRY_CTRL);
190140 // set notify Alt icon data
191- m_hIcon[ALT_ICON][ON_ICON] = AfxGetApp()->LoadIcon(IDI_ALT_ON);
192- m_hIcon[ALT_ICON][OFF_ICON] = AfxGetApp()->LoadIcon(IDI_ALT_OFF);
193- m_bIcon[ALT_ICON] = AfxGetApp()->GetProfileInt(CString(), CString(MAKEINTRESOURCE(IDS_REG_ENTRY_ALT)), 0);
194- m_stNtfyIcon[ALT_ICON] = notifyIconData[ALT_ICON];
195- AddShell_NotifyIcon(ALT_ICON);
141+ SetIconData(ALT_ICON, IDS_TIP_ALT, IDI_ALT_ON, IDI_ALT_OFF, IDS_REG_ENTRY_ALT);
196142
197143 CXkeymacsDll::SetKeyboardSpeed(CProfile::GetKeyboardSpeed());
198144 CXkeymacsDll::SetAccelerate(AfxGetApp()->GetProfileInt(CString(), CString(MAKEINTRESOURCE(IDS_REG_ENTRY_ACCELERATE)), 1));
@@ -224,9 +170,8 @@ void CMainFrame::TerminatePollThread()
224170 CloseHandle(m_hThread);
225171 }
226172
227-DWORD WINAPI CMainFrame::PollIconMessage(LPVOID lpParam)
173+DWORD WINAPI CMainFrame::PollIconMessage(LPVOID)
228174 {
229- CMainFrame *pThis = reinterpret_cast<CMainFrame *>(lpParam);
230175 HANDLE hPipe = CreateNamedPipe(ICON_PIPE, PIPE_ACCESS_DUPLEX, PIPE_TYPE_MESSAGE | PIPE_READMODE_MESSAGE, 1,
231176 sizeof(DWORD), sizeof(IconMsg) * MAX_ICON_TYPE, 0, NULL);
232177 if (hPipe == INVALID_HANDLE_VALUE)
@@ -243,41 +188,46 @@ DWORD WINAPI CMainFrame::PollIconMessage(LPVOID lpParam)
243188 if (!WriteFile(hPipe, &ack, sizeof(DWORD), &written, NULL) || written != sizeof(DWORD) ||
244189 !FlushFileBuffers(hPipe) || !DisconnectNamedPipe(hPipe))
245190 break;
246- if (!pThis->m_bPollIconMessage)
191+ if (!m_bPollIconMessage)
247192 break;
248193 for (DWORD i = 0; i < read / sizeof(IconMsg); ++i) {
249- pThis->m_stNtfyIcon[msg[i].nType].hIcon = pThis->m_hIcon[msg[i].nType][msg[i].nState];
194+ m_stNtfyIcon[msg[i].nType].hIcon = m_hIcon[msg[i].nType][msg[i].nState];
250195 if (msg[i].nType == MX_ICON && msg[i].szTip[0] != 0)
251- memcpy(pThis->m_stNtfyIcon[MX_ICON].szTip, msg[i].szTip, 128);
252- pThis->DoShell_NotifyIcon(msg[i].nType, NIM_MODIFY);
196+ memcpy(m_stNtfyIcon[MX_ICON].szTip, msg[i].szTip, 128);
197+ DoShell_NotifyIcon(msg[i].nType, NIM_MODIFY);
253198 }
254199 }
255200 CloseHandle(hPipe);
256201 return 0;
257202 }
258203
259-BOOL CMainFrame::DoShell_NotifyIcon(ICON_TYPE icon, DWORD dwMessage)
204+void CMainFrame::SetIconData(ICON_TYPE icon, int tip, int on, int off, int reg)
260205 {
261- if (m_bIcon[icon]
262- && (m_dwOldMessage[icon] != dwMessage
263- || memcmp(&m_stOldNtfyIcon[icon], &m_stNtfyIcon[icon], sizeof(m_stNtfyIcon[icon])))) {
264- m_dwOldMessage[icon] = dwMessage;
265- m_stOldNtfyIcon[icon] = m_stNtfyIcon[icon];
206+ m_hIcon[icon][ON_ICON] = AfxGetApp()->LoadIcon(on);
207+ m_hIcon[icon][OFF_ICON] = AfxGetApp()->LoadIcon(off);
208+ m_bIcon[icon] = AfxGetApp()->GetProfileInt(CString(), CString(MAKEINTRESOURCE(reg)), 0);
209+ NOTIFYICONDATA data = { sizeof(NOTIFYICONDATA), m_hWnd, icon, NIF_MESSAGE | NIF_ICON | NIF_TIP, WM_USER_NTFYICON, AfxGetApp()->LoadIcon(off) };
210+ LoadString(AfxGetInstanceHandle(), tip, data.szTip, _countof(data.szTip));
211+ m_stNtfyIcon[icon] = data;
212+ AddShell_NotifyIcon(icon);
213+}
266214
267- BOOL rc = FALSE;
268- for (int retry_count = 0; retry_count < 20; ++retry_count) { // retry for timeout
269- rc = Shell_NotifyIcon(dwMessage, &m_stNtfyIcon[icon]);
270- if (dwMessage != NIM_ADD || rc || (GetLastError() != ERROR_TIMEOUT && 5 < retry_count)) {
271- break;
272- }
273- Sleep(1000); // 1sec
274- if ((rc = Shell_NotifyIcon(NIM_MODIFY, &m_stNtfyIcon[icon])) != FALSE) {
275- break; // ERROR_TIMEOUT was returned but the icon was also added.
276- }
277- }
278- return rc;
279- } else {
280- return TRUE;
215+void CMainFrame::DoShell_NotifyIcon(ICON_TYPE icon, DWORD dwMessage)
216+{
217+ if (!m_bIcon[icon] ||
218+ (m_dwOldMessage[icon] == dwMessage &&
219+ !memcmp(&m_stOldNtfyIcon[icon], &m_stNtfyIcon[icon], sizeof(m_stNtfyIcon[icon]))))
220+ return;
221+ m_dwOldMessage[icon] = dwMessage;
222+ m_stOldNtfyIcon[icon] = m_stNtfyIcon[icon];
223+ for (int retry_count = 0; retry_count < 20; ++retry_count) { // retry for timeout
224+ if (Shell_NotifyIcon(dwMessage, &m_stNtfyIcon[icon]))
225+ return;
226+ if (dwMessage != NIM_ADD || (GetLastError() != ERROR_TIMEOUT && 5 < retry_count))
227+ return;
228+ Sleep(1000); // 1sec
229+ if (Shell_NotifyIcon(NIM_MODIFY, &m_stNtfyIcon[icon]))
230+ return; // ERROR_TIMEOUT was returned but the icon was also added.
281231 }
282232 }
283233
--- a/xkeymacs/mainfrm.h
+++ b/xkeymacs/mainfrm.h
@@ -90,17 +90,18 @@ private:
9090 int m_nResultPropertiesDlg;
9191 CProperties *m_pPropertiesDlg;
9292 // notify icons
93- BOOL m_bIcon[MAX_ICON_TYPE];
94- HICON m_hIcon[MAX_ICON_TYPE][MAX_STATUS];
95- DWORD m_dwOldMessage[MAX_ICON_TYPE];
96- NOTIFYICONDATA m_stNtfyIcon[MAX_ICON_TYPE];
97- NOTIFYICONDATA m_stOldNtfyIcon[MAX_ICON_TYPE];
98- void AddShell_NotifyIcon(ICON_TYPE icon);
99- void DeleteShell_NotifyIcon(ICON_TYPE icon);
100- BOOL DoShell_NotifyIcon(ICON_TYPE icon, DWORD dwMessage);
101- void DeleteAllShell_NotifyIcon();
102- void AddAllShell_NotifyIcon();
103- BOOL m_bPollIconMessage;
93+ static BOOL m_bIcon[MAX_ICON_TYPE];
94+ static HICON m_hIcon[MAX_ICON_TYPE][MAX_STATUS];
95+ static DWORD m_dwOldMessage[MAX_ICON_TYPE];
96+ static NOTIFYICONDATA m_stNtfyIcon[MAX_ICON_TYPE];
97+ static NOTIFYICONDATA m_stOldNtfyIcon[MAX_ICON_TYPE];
98+ void SetIconData(ICON_TYPE icon, int tip, int on, int off, int reg);
99+ static void AddShell_NotifyIcon(ICON_TYPE icon);
100+ static void DeleteShell_NotifyIcon(ICON_TYPE icon);
101+ static void DoShell_NotifyIcon(ICON_TYPE icon, DWORD dwMessage);
102+ static void DeleteAllShell_NotifyIcon();
103+ static void AddAllShell_NotifyIcon();
104+ static bool m_bPollIconMessage;
104105 HANDLE m_hThread;
105106 void StartPollThread();
106107 void TerminatePollThread();
旧リポジトリブラウザで表示