3 * LICENSE: GPL - See COPYING in the top level directory
4 * FILE: base/applications/testset/user32/kbdlayout/kbdlayout.c
5 * PURPOSE: Keyboard layout testapp
6 * COPYRIGHT: Copyright 2007 Saveliy Tretiakov
16 LRESULT
MainDialogProc(HWND hDlg
, UINT Msg
, WPARAM wParam
, LPARAM lParam
);
28 DWORD WINAPI
ThreadProc(LPVOID lpParam
)
32 MAKEINTRESOURCE(IDD_MAINDIALOG
),
34 (DLGPROC
)MainDialogProc
,
40 INT WINAPI
WinMain(HINSTANCE hInstance
,
41 HINSTANCE hPrevInstance
,
55 int GetKlList(HKL
**list
)
60 n
= GetKeyboardLayoutList(0, NULL
);
61 ret
= HeapAlloc(GetProcessHeap(), 0, sizeof(HKL
)*n
);
62 GetKeyboardLayoutList(n
, ret
);
67 void FreeKlList(HKL
*list
)
69 HeapFree(GetProcessHeap(), 0, list
);
73 void UpdateData(HWND hDlg
)
75 WCHAR buf
[KL_NAMELENGTH
];
82 GetKeyboardLayoutName(buf
);
83 swprintf(buf2
, L
"Active: %s (%x)", buf
, GetKeyboardLayout(0));
84 SetWindowText(GetDlgItem(hDlg
, IDC_ACTIVE
), buf2
);
86 hList
= GetDlgItem(hDlg
, IDC_LIST
);
87 SendMessage(hList
, LB_RESETCONTENT
, 0, 0);
89 n
= GetKlList(&klList
);
90 hKl
= GetKeyboardLayout(0);
91 for(i
= 0; i
< n
; i
++)
93 swprintf(buf
, L
"%x", klList
[i
] );
94 j
= SendMessage(hList
, LB_ADDSTRING
, 0, (LPARAM
) buf
);
95 SendMessage(hList
, LB_SETITEMDATA
, j
, (LPARAM
) klList
[i
]);
96 if(klList
[i
] == hKl
) SendMessage(hList
, LB_SETCURSEL
, j
, 0);
102 void FormatMsg(WCHAR
*format
, ...)
106 va_start(argptr
, format
);
107 _vsnwprintf(buf
, sizeof(buf
)-1, format
, argptr
);
108 MessageBox(0, buf
, L
"msg", 0);
112 void FormatBox(HWND hWnd
, DWORD Flags
, WCHAR
*Caption
, WCHAR
*Format
, ...)
116 va_start(argptr
, Format
);
117 _vsnwprintf(buf
, sizeof(buf
)-1, Format
, argptr
);
118 MessageBox(hWnd
, buf
, Caption
, Flags
);
123 LRESULT CALLBACK
WndSubclassProc( HWND hwnd
, UINT uMsg
, WPARAM wParam
, LPARAM lParam
)
125 WND_DATA
*data
= (WND_DATA
*)GetWindowLong(hwnd
, GWL_USERDATA
);
127 if(uMsg
== WM_INPUTLANGCHANGE
)
129 FormatMsg(L
"%s: WM_INPUTLANGCHANGE lParam=%x wParam=%x\n", data
->WndName
, lParam
, wParam
);
130 UpdateData(hMainDlg
);
131 //Pass message to defwindowproc
133 else if(uMsg
== WM_INPUTLANGCHANGEREQUEST
)
135 FormatMsg(L
"%s: WM_INPUTLANGCHANGEREQUEST lParam=%x wParam=%x\n", data
->WndName
, lParam
, wParam
);
136 UpdateData(hMainDlg
);
137 //Pass message to defwindowproc
140 return ( CallWindowProc( data
->OrigProc
, hwnd
, uMsg
, wParam
, lParam
) );
143 void SubclassWnd(HWND hWnd
, WCHAR
* Name
)
145 WND_DATA
*data
= HeapAlloc(GetProcessHeap(), 0, sizeof(WND_DATA
));
146 data
->OrigProc
= (WNDPROC
)SetWindowLong( hWnd
, GWL_WNDPROC
, (LONG
)WndSubclassProc
);
147 wcsncpy(data
->WndName
, Name
, 25);
148 SetWindowLong(hWnd
, GWL_USERDATA
, (LONG
)data
);
152 DWORD
GetActivateFlags(HWND hDlg
)
156 if(IsDlgButtonChecked(hDlg
, IDC_KLF_REORDER
))
159 if(IsDlgButtonChecked(hDlg
, IDC_KLF_RESET
))
162 if(IsDlgButtonChecked(hDlg
, IDC_KLF_SHIFTLOCK
))
163 ret
|= KLF_SHIFTLOCK
;
165 if(IsDlgButtonChecked(hDlg
, IDC_KLF_SETFORPROCESS
))
166 ret
|= KLF_SETFORPROCESS
;
172 DWORD
GetLoadFlags(HWND hDlg
)
176 if(IsDlgButtonChecked(hDlg
, IDL_KLF_ACTIVATE
))
179 if(IsDlgButtonChecked(hDlg
, IDL_KLF_NOTELLSHELL
))
180 ret
|= KLF_NOTELLSHELL
;
182 if(IsDlgButtonChecked(hDlg
, IDL_KLF_REORDER
))
185 if(IsDlgButtonChecked(hDlg
, IDL_KLF_REPLACELANG
))
186 ret
|= KLF_REPLACELANG
;
188 if(IsDlgButtonChecked(hDlg
, IDL_KLF_SUBSTITUTE_OK
))
189 ret
|= KLF_SUBSTITUTE_OK
;
191 if(IsDlgButtonChecked(hDlg
, IDL_KLF_SETFORPROCESS
))
192 ret
|= KLF_SETFORPROCESS
;
197 UINT
GetDelayMilliseconds(HWND hDlg
)
202 GetWindowText(GetDlgItem(hDlg
, IDC_DELAY
), Buf
, sizeof(Buf
));
204 swscanf(Buf
, L
"%d", &ret
);
209 HKL
GetSelectedLayout(HWND hDlg
)
213 hList
= GetDlgItem(hDlg
, IDC_LIST
);
214 if((n
= SendMessage(hList
, LB_GETCURSEL
, 0, 0)) != LB_ERR
)
215 return (HKL
) SendMessage(hList
, LB_GETITEMDATA
, n
, 0);
216 else return INVALID_HANDLE_VALUE
;
219 HKL
GetActivateHandle(HWND hDlg
)
222 if(IsDlgButtonChecked(hDlg
, IDC_FROMLIST
))
223 return GetSelectedLayout(hDlg
);
224 else if(IsDlgButtonChecked(hDlg
, IDC_HKL_NEXT
))
225 return (HKL
)HKL_NEXT
;
227 return (HKL
)HKL_PREV
;
232 /***************************************************
234 ***************************************************/
236 LRESULT
MainDialogProc(HWND hDlg
,
251 SubclassWnd(GetDlgItem(hDlg
, IDC_LIST
), L
"List");
252 SubclassWnd(GetDlgItem(hDlg
, IDC_EDIT1
), L
"Edit1");
253 SubclassWnd(GetDlgItem(hDlg
, IDC_KLID
), L
"Klid");
254 SubclassWnd(GetDlgItem(hDlg
, ID_CANCEL
), L
"CancelB");
255 SubclassWnd(GetDlgItem(hDlg
, IDC_ACTIVATE
), L
"ActivateB");
256 SubclassWnd(GetDlgItem(hDlg
, IDC_REFRESH
), L
"RefreshB");
257 SubclassWnd(GetDlgItem(hDlg
, IDC_UNLOAD
), L
"UnloadB");
258 SubclassWnd(GetDlgItem(hDlg
, IDC_LOAD
), L
"LoadB");
260 CheckRadioButton(hDlg
, IDC_FROMLIST
, IDC_FROMEDIT
, IDC_FROMLIST
);
261 SetWindowText(GetDlgItem(hDlg
, IDC_KLID
), L
"00000419");
263 swprintf(Buf
, L
"Current thread id: %d", GetCurrentThreadId());
264 SetWindowText(GetDlgItem(hDlg
, IDC_CURTHREAD
), Buf
);
266 SetWindowText(GetDlgItem(hDlg
, IDC_DELAY
), L
"0");
269 } /* WM_INITDIALOG */
273 switch(LOWORD(wParam
))
277 EndDialog(hDlg
, ERROR_CANCELLED
);
283 if((hKl
= GetActivateHandle(hDlg
)) != INVALID_HANDLE_VALUE
)
285 Sleep(GetDelayMilliseconds(hDlg
));
286 if(!(hKl
= ActivateKeyboardLayout(hKl
, GetActivateFlags(hDlg
))))
287 FormatBox(hDlg
, MB_ICONERROR
, L
"Error",
288 L
"ActivateKeyboardLayout() failed. %d", GetLastError());
289 else UpdateData(hDlg
);
290 //FormatBox(hDlg, 0, L"Activated", L"Prev - %x, err - %d.", hKl,
293 else MessageBox(hDlg
, L
"No item selected", L
"Error", MB_ICONERROR
);
299 if((hKl
= GetSelectedLayout(hDlg
)) != INVALID_HANDLE_VALUE
)
301 Sleep(GetDelayMilliseconds(hDlg
));
302 if(!UnloadKeyboardLayout(hKl
))
303 FormatBox(hDlg
, MB_ICONERROR
, L
"Error",
304 L
"UnloadKeyboardLayout() failed. %d",
306 else UpdateData(hDlg
);
308 else MessageBox(hDlg
, L
"No item selected", L
"Error", MB_ICONERROR
);
315 GetWindowText(GetDlgItem(hDlg
, IDC_KLID
), buf
, sizeof(buf
));
316 Sleep(GetDelayMilliseconds(hDlg
));
317 if(!LoadKeyboardLayout(buf
, GetLoadFlags(hDlg
)))
318 FormatBox(hDlg
, MB_ICONERROR
, L
"Error",
319 L
"LoadKeyboardLayout() failed. %d",
321 else UpdateData(hDlg
);
333 if(!CreateThread(NULL
, 0, ThreadProc
, NULL
, 0, NULL
))
335 FormatBox(hDlg
, MB_ICONERROR
, L
"Error!",
336 L
"Can not create thread (%d).", GetLastError());
342 if(HIWORD(wParam
) == LBN_SELCHANGE
)
345 if((hKl
= GetSelectedLayout(hDlg
)) != NULL
)
347 swprintf(buf
, L
"%x", hKl
);
348 SetWindowText(GetDlgItem(hDlg
, IDC_HANDLE
), buf
);
360 case WM_INPUTLANGCHANGE
:
362 FormatMsg(L
"dlg WM_INPUTLANGCHANGE lParam=%x wParam=%x\n", lParam
, wParam
);
366 case WM_INPUTLANGCHANGEREQUEST
:
368 FormatMsg(L
"dlg WM_INPUTLANGCHANGEREQUEST lParam=%x wParam=%x\n", lParam
, wParam
);
375 EndDialog(hDlg
, ERROR_CANCELLED
);