wordpad: Add a dropdown menu to the bullet button.
[wine.git] / dlls / oleacc / main.c
blob3cfe7c105ad8ac6cf543e12dac74edd271d7314f
1 /*
2 * Implementation of the OLEACC dll
4 * Copyright 2003 Mike McCormack for CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #define COBJMACROS
23 #include <stdarg.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "ole2.h"
27 #include "commctrl.h"
28 #include "rpcproxy.h"
30 #include "initguid.h"
31 #include "oleacc_private.h"
32 #include "resource.h"
34 #include "wine/unicode.h"
35 #include "wine/debug.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(oleacc);
39 static const WCHAR lresult_atom_prefix[] = {'w','i','n','e','_','o','l','e','a','c','c',':'};
41 static const WCHAR menuW[] = {'#','3','2','7','6','8',0};
42 static const WCHAR desktopW[] = {'#','3','2','7','6','9',0};
43 static const WCHAR dialogW[] = {'#','3','2','7','7','0',0};
44 static const WCHAR winswitchW[] = {'#','3','2','7','7','1',0};
45 static const WCHAR mdi_clientW[] = {'M','D','I','C','l','i','e','n','t',0};
46 static const WCHAR richeditW[] = {'R','I','C','H','E','D','I','T',0};
47 static const WCHAR richedit20aW[] = {'R','i','c','h','E','d','i','t','2','0','A',0};
48 static const WCHAR richedit20wW[] = {'R','i','c','h','E','d','i','t','2','0','W',0};
50 typedef HRESULT (WINAPI *accessible_create)(HWND, const IID*, void**);
52 extern HRESULT WINAPI OLEACC_DllGetClassObject(REFCLSID, REFIID, void**) DECLSPEC_HIDDEN;
53 extern BOOL WINAPI OLEACC_DllMain(HINSTANCE, DWORD, void*) DECLSPEC_HIDDEN;
54 extern HRESULT WINAPI OLEACC_DllRegisterServer(void) DECLSPEC_HIDDEN;
55 extern HRESULT WINAPI OLEACC_DllUnregisterServer(void) DECLSPEC_HIDDEN;
57 static struct {
58 const WCHAR *name;
59 DWORD idx;
60 accessible_create create_client;
61 accessible_create create_window;
62 } builtin_classes[] = {
63 {WC_LISTBOXW, 0x10000, NULL, NULL},
64 {menuW, 0x10001, NULL, NULL},
65 {WC_BUTTONW, 0x10002, NULL, NULL},
66 {WC_STATICW, 0x10003, NULL, NULL},
67 {WC_EDITW, 0x10004, NULL, NULL},
68 {WC_COMBOBOXW, 0x10005, NULL, NULL},
69 {dialogW, 0x10006, NULL, NULL},
70 {winswitchW, 0x10007, NULL, NULL},
71 {mdi_clientW, 0x10008, NULL, NULL},
72 {desktopW, 0x10009, NULL, NULL},
73 {WC_SCROLLBARW, 0x1000a, NULL, NULL},
74 {STATUSCLASSNAMEW, 0x1000b, NULL, NULL},
75 {TOOLBARCLASSNAMEW, 0x1000c, NULL, NULL},
76 {PROGRESS_CLASSW, 0x1000d, NULL, NULL},
77 {ANIMATE_CLASSW, 0x1000e, NULL, NULL},
78 {WC_TABCONTROLW, 0x1000f, NULL, NULL},
79 {HOTKEY_CLASSW, 0x10010, NULL, NULL},
80 {WC_HEADERW, 0x10011, NULL, NULL},
81 {TRACKBAR_CLASSW, 0x10012, NULL, NULL},
82 {WC_LISTVIEWW, 0x10013, NULL, NULL},
83 {UPDOWN_CLASSW, 0x10016, NULL, NULL},
84 {TOOLTIPS_CLASSW, 0x10018, NULL, NULL},
85 {WC_TREEVIEWW, 0x10019, NULL, NULL},
86 {MONTHCAL_CLASSW, 0, NULL, NULL},
87 {DATETIMEPICK_CLASSW, 0, NULL, NULL},
88 {WC_IPADDRESSW, 0, NULL, NULL},
89 {richeditW, 0x1001c, NULL, NULL},
90 {richedit20aW, 0, NULL, NULL},
91 {richedit20wW, 0, NULL, NULL},
94 static HINSTANCE oleacc_handle = 0;
96 int convert_child_id(VARIANT *v)
98 switch(V_VT(v)) {
99 case VT_I4:
100 return V_I4(v);
101 default:
102 FIXME("unhandled child ID variant type: %d\n", V_VT(v));
103 return -1;
107 static accessible_create get_builtin_accessible_obj(HWND hwnd, LONG objid)
109 WCHAR class_name[64];
110 int i, idx;
112 if(!RealGetWindowClassW(hwnd, class_name, sizeof(class_name)/sizeof(WCHAR)))
113 return NULL;
114 TRACE("got window class: %s\n", debugstr_w(class_name));
116 for(i=0; i<sizeof(builtin_classes)/sizeof(builtin_classes[0]); i++) {
117 if(!strcmpiW(class_name, builtin_classes[i].name)) {
118 accessible_create ret;
120 ret = (objid==OBJID_CLIENT ?
121 builtin_classes[i].create_client :
122 builtin_classes[i].create_window);
123 if(!ret)
124 FIXME("unhandled window class: %s\n", debugstr_w(class_name));
125 return ret;
129 idx = SendMessageW(hwnd, WM_GETOBJECT, 0, OBJID_QUERYCLASSNAMEIDX);
130 if(idx) {
131 for(i=0; i<sizeof(builtin_classes)/sizeof(builtin_classes[0]); i++) {
132 if(idx == builtin_classes[i].idx) {
133 accessible_create ret;
135 ret = (objid==OBJID_CLIENT ?
136 builtin_classes[i].create_client :
137 builtin_classes[i].create_window);
138 if(!ret)
139 FIXME("unhandled class name idx: %x\n", idx);
140 return ret;
144 WARN("unhandled class name idx: %x\n", idx);
147 return NULL;
150 HRESULT WINAPI CreateStdAccessibleObject( HWND hwnd, LONG idObject,
151 REFIID riidInterface, void** ppvObject )
153 accessible_create create;
155 TRACE("%p %d %s %p\n", hwnd, idObject,
156 debugstr_guid( riidInterface ), ppvObject );
158 switch(idObject) {
159 case OBJID_CLIENT:
160 create = get_builtin_accessible_obj(hwnd, idObject);
161 if(create) return create(hwnd, riidInterface, ppvObject);
162 return create_client_object(hwnd, riidInterface, ppvObject);
163 case OBJID_WINDOW:
164 create = get_builtin_accessible_obj(hwnd, idObject);
165 if(create) return create(hwnd, riidInterface, ppvObject);
166 return create_window_object(hwnd, riidInterface, ppvObject);
167 default:
168 FIXME("unhandled object id: %d\n", idObject);
169 return E_NOTIMPL;
173 HRESULT WINAPI ObjectFromLresult( LRESULT result, REFIID riid, WPARAM wParam, void **ppObject )
175 WCHAR atom_str[sizeof(lresult_atom_prefix)/sizeof(WCHAR)+3*8+3];
176 HANDLE server_proc, server_mapping, mapping;
177 DWORD proc_id, size;
178 IStream *stream;
179 HGLOBAL data;
180 void *view;
181 HRESULT hr;
182 WCHAR *p;
184 TRACE("%ld %s %ld %p\n", result, debugstr_guid(riid), wParam, ppObject );
186 if(wParam)
187 FIXME("unsupported wParam = %lx\n", wParam);
189 if(!ppObject)
190 return E_INVALIDARG;
191 *ppObject = NULL;
193 if(result != (ATOM)result)
194 return E_FAIL;
196 if(!GlobalGetAtomNameW(result, atom_str, sizeof(atom_str)/sizeof(WCHAR)))
197 return E_FAIL;
198 if(memcmp(atom_str, lresult_atom_prefix, sizeof(lresult_atom_prefix)))
199 return E_FAIL;
200 p = atom_str + sizeof(lresult_atom_prefix)/sizeof(WCHAR);
201 proc_id = strtoulW(p, &p, 16);
202 if(*p != ':')
203 return E_FAIL;
204 server_mapping = ULongToHandle( strtoulW(p+1, &p, 16) );
205 if(*p != ':')
206 return E_FAIL;
207 size = strtoulW(p+1, &p, 16);
208 if(*p != 0)
209 return E_FAIL;
211 server_proc = OpenProcess(PROCESS_DUP_HANDLE, FALSE, proc_id);
212 if(!server_proc)
213 return E_FAIL;
215 if(!DuplicateHandle(server_proc, server_mapping, GetCurrentProcess(), &mapping,
216 0, FALSE, DUPLICATE_CLOSE_SOURCE|DUPLICATE_SAME_ACCESS))
217 return E_FAIL;
218 CloseHandle(server_proc);
219 GlobalDeleteAtom(result);
221 view = MapViewOfFile(mapping, FILE_MAP_READ, 0, 0, 0);
222 CloseHandle(mapping);
223 if(!view)
224 return E_FAIL;
226 data = GlobalAlloc(GMEM_FIXED, size);
227 if(!data) {
228 UnmapViewOfFile(view);
229 return E_OUTOFMEMORY;
231 memcpy(data, view, size);
232 UnmapViewOfFile(view);
234 hr = CreateStreamOnHGlobal(data, TRUE, &stream);
235 if(FAILED(hr)) {
236 GlobalFree(data);
237 return hr;
240 hr = CoUnmarshalInterface(stream, riid, ppObject);
241 IStream_Release(stream);
242 return hr;
245 LRESULT WINAPI LresultFromObject( REFIID riid, WPARAM wParam, LPUNKNOWN pAcc )
247 static const WCHAR atom_fmt[] = {'%','0','8','x',':','%','0','8','x',':','%','0','8','x',0};
248 static const LARGE_INTEGER seek_zero = {{0}};
250 WCHAR atom_str[sizeof(lresult_atom_prefix)/sizeof(WCHAR)+3*8+3];
251 IStream *stream;
252 HANDLE mapping;
253 STATSTG stat;
254 HRESULT hr;
255 ATOM atom;
256 void *view;
258 TRACE("%s %ld %p\n", debugstr_guid(riid), wParam, pAcc);
260 if(wParam)
261 FIXME("unsupported wParam = %lx\n", wParam);
263 if(!pAcc)
264 return E_INVALIDARG;
266 hr = CreateStreamOnHGlobal(NULL, TRUE, &stream);
267 if(FAILED(hr))
268 return hr;
270 hr = CoMarshalInterface(stream, riid, pAcc, MSHCTX_LOCAL, NULL, MSHLFLAGS_NORMAL);
271 if(FAILED(hr)) {
272 IStream_Release(stream);
273 return hr;
276 hr = IStream_Seek(stream, seek_zero, STREAM_SEEK_SET, NULL);
277 if(FAILED(hr)) {
278 IStream_Release(stream);
279 return hr;
282 hr = IStream_Stat(stream, &stat, STATFLAG_NONAME);
283 if(FAILED(hr)) {
284 CoReleaseMarshalData(stream);
285 IStream_Release(stream);
286 return hr;
287 }else if(stat.cbSize.u.HighPart) {
288 FIXME("stream size to big\n");
289 CoReleaseMarshalData(stream);
290 IStream_Release(stream);
291 return E_NOTIMPL;
294 mapping = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE,
295 stat.cbSize.u.HighPart, stat.cbSize.u.LowPart, NULL);
296 if(!mapping) {
297 CoReleaseMarshalData(stream);
298 IStream_Release(stream);
299 return hr;
302 view = MapViewOfFile(mapping, FILE_MAP_WRITE, 0, 0, 0);
303 if(!view) {
304 CloseHandle(mapping);
305 CoReleaseMarshalData(stream);
306 IStream_Release(stream);
307 return E_FAIL;
310 hr = IStream_Read(stream, view, stat.cbSize.u.LowPart, NULL);
311 UnmapViewOfFile(view);
312 if(FAILED(hr)) {
313 CloseHandle(mapping);
314 hr = IStream_Seek(stream, seek_zero, STREAM_SEEK_SET, NULL);
315 if(SUCCEEDED(hr))
316 CoReleaseMarshalData(stream);
317 IStream_Release(stream);
318 return hr;
322 memcpy(atom_str, lresult_atom_prefix, sizeof(lresult_atom_prefix));
323 sprintfW(atom_str+sizeof(lresult_atom_prefix)/sizeof(WCHAR),
324 atom_fmt, GetCurrentProcessId(), HandleToUlong(mapping), stat.cbSize.u.LowPart);
325 atom = GlobalAddAtomW(atom_str);
326 if(!atom) {
327 CloseHandle(mapping);
328 hr = IStream_Seek(stream, seek_zero, STREAM_SEEK_SET, NULL);
329 if(SUCCEEDED(hr))
330 CoReleaseMarshalData(stream);
331 IStream_Release(stream);
332 return E_FAIL;
335 IStream_Release(stream);
336 return atom;
339 HRESULT WINAPI AccessibleObjectFromPoint( POINT ptScreen, IAccessible** ppacc, VARIANT* pvarChild )
341 FIXME("{%d,%d} %p %p: stub\n", ptScreen.x, ptScreen.y, ppacc, pvarChild );
342 return E_NOTIMPL;
345 HRESULT WINAPI AccessibleObjectFromWindow( HWND hwnd, DWORD dwObjectID,
346 REFIID riid, void** ppvObject )
348 TRACE("%p %d %s %p\n", hwnd, dwObjectID,
349 debugstr_guid( riid ), ppvObject );
351 if(!ppvObject)
352 return E_INVALIDARG;
353 *ppvObject = NULL;
355 if(IsWindow(hwnd)) {
356 LRESULT lres;
358 lres = SendMessageW(hwnd, WM_GETOBJECT, 0xffffffff, dwObjectID);
359 if(FAILED(lres))
360 return lres;
361 else if(lres)
362 return ObjectFromLresult(lres, riid, 0, ppvObject);
365 return CreateStdAccessibleObject(hwnd, dwObjectID, riid, ppvObject);
368 HRESULT WINAPI WindowFromAccessibleObject(IAccessible *acc, HWND *phwnd)
370 IDispatch *parent;
371 IOleWindow *ow;
372 HRESULT hres;
374 TRACE("%p %p\n", acc, phwnd);
376 IAccessible_AddRef(acc);
377 while(1) {
378 hres = IAccessible_QueryInterface(acc, &IID_IOleWindow, (void**)&ow);
379 if(SUCCEEDED(hres)) {
380 hres = IOleWindow_GetWindow(ow, phwnd);
381 IOleWindow_Release(ow);
382 IAccessible_Release(acc);
383 return hres;
386 hres = IAccessible_get_accParent(acc, &parent);
387 IAccessible_Release(acc);
388 if(FAILED(hres))
389 return hres;
390 if(hres!=S_OK || !parent) {
391 *phwnd = NULL;
392 return hres;
395 hres = IDispatch_QueryInterface(parent, &IID_IAccessible, (void**)&acc);
396 IDispatch_Release(parent);
397 if(FAILED(hres))
398 return hres;
402 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason,
403 LPVOID lpvReserved)
405 TRACE("%p, %d, %p\n", hinstDLL, fdwReason, lpvReserved);
407 switch (fdwReason)
409 case DLL_PROCESS_ATTACH:
410 oleacc_handle = hinstDLL;
411 DisableThreadLibraryCalls(hinstDLL);
412 break;
415 return OLEACC_DllMain(hinstDLL, fdwReason, lpvReserved);
418 HRESULT WINAPI DllRegisterServer(void)
420 return OLEACC_DllRegisterServer();
423 HRESULT WINAPI DllUnregisterServer(void)
425 return OLEACC_DllUnregisterServer();
428 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, void **ppv)
430 if(IsEqualGUID(&CLSID_CAccPropServices, rclsid)) {
431 TRACE("(CLSID_CAccPropServices %s %p)\n", debugstr_guid(iid), ppv);
432 return get_accpropservices_factory(iid, ppv);
435 if(IsEqualGUID(&CLSID_PSFactoryBuffer, rclsid)) {
436 TRACE("(CLSID_PSFactoryBuffer %s %p)\n", debugstr_guid(iid), ppv);
437 return OLEACC_DllGetClassObject(rclsid, iid, ppv);
440 FIXME("%s %s %p: stub\n", debugstr_guid(rclsid), debugstr_guid(iid), ppv);
441 return E_NOTIMPL;
444 void WINAPI GetOleaccVersionInfo(DWORD* pVersion, DWORD* pBuild)
446 *pVersion = MAKELONG(0,7); /* Windows 7 version of oleacc: 7.0.0.0 */
447 *pBuild = MAKELONG(0,0);
450 HANDLE WINAPI GetProcessHandleFromHwnd(HWND hwnd)
452 DWORD proc_id;
454 TRACE("%p\n", hwnd);
456 if(!GetWindowThreadProcessId(hwnd, &proc_id))
457 return NULL;
458 return OpenProcess(PROCESS_DUP_HANDLE | PROCESS_VM_OPERATION |
459 PROCESS_VM_READ | PROCESS_VM_WRITE | SYNCHRONIZE, TRUE, proc_id);
462 UINT WINAPI GetRoleTextW(DWORD role, LPWSTR lpRole, UINT rolemax)
464 INT ret;
465 WCHAR *resptr;
467 TRACE("%u %p %u\n", role, lpRole, rolemax);
469 /* return role text length */
470 if(!lpRole)
471 return LoadStringW(oleacc_handle, role, (LPWSTR)&resptr, 0);
473 ret = LoadStringW(oleacc_handle, role, lpRole, rolemax);
474 if(!(ret > 0)){
475 if(rolemax > 0) lpRole[0] = '\0';
476 return 0;
479 return ret;
482 UINT WINAPI GetRoleTextA(DWORD role, LPSTR lpRole, UINT rolemax)
484 UINT length;
485 WCHAR *roletextW;
487 TRACE("%u %p %u\n", role, lpRole, rolemax);
489 if(lpRole && !rolemax)
490 return 0;
492 length = GetRoleTextW(role, NULL, 0);
493 if(!length) {
494 if(lpRole && rolemax)
495 lpRole[0] = 0;
496 return 0;
499 roletextW = HeapAlloc(GetProcessHeap(), 0, (length + 1)*sizeof(WCHAR));
500 if(!roletextW)
501 return 0;
503 GetRoleTextW(role, roletextW, length + 1);
505 length = WideCharToMultiByte( CP_ACP, 0, roletextW, -1, NULL, 0, NULL, NULL );
507 if(!lpRole){
508 HeapFree(GetProcessHeap(), 0, roletextW);
509 return length - 1;
512 if(rolemax < length) {
513 HeapFree(GetProcessHeap(), 0, roletextW);
514 lpRole[0] = 0;
515 return 0;
518 WideCharToMultiByte( CP_ACP, 0, roletextW, -1, lpRole, rolemax, NULL, NULL );
520 if(rolemax < length){
521 lpRole[rolemax-1] = '\0';
522 length = rolemax;
525 HeapFree(GetProcessHeap(), 0, roletextW);
527 return length - 1;
530 UINT WINAPI GetStateTextW(DWORD state_bit, WCHAR *state_str, UINT state_str_len)
532 DWORD state_id;
534 TRACE("%x %p %u\n", state_bit, state_str, state_str_len);
536 if(state_bit & ~(STATE_SYSTEM_VALID | STATE_SYSTEM_HASPOPUP)) {
537 if(state_str && state_str_len)
538 state_str[0] = 0;
539 return 0;
542 state_id = IDS_STATE_NORMAL;
543 while(state_bit) {
544 state_id++;
545 state_bit /= 2;
548 if(state_str) {
549 UINT ret = LoadStringW(oleacc_handle, state_id, state_str, state_str_len);
550 if(!ret && state_str_len)
551 state_str[0] = 0;
552 return ret;
553 }else {
554 WCHAR *tmp;
555 return LoadStringW(oleacc_handle, state_id, (WCHAR*)&tmp, 0);
560 UINT WINAPI GetStateTextA(DWORD state_bit, CHAR *state_str, UINT state_str_len)
562 DWORD state_id;
564 TRACE("%x %p %u\n", state_bit, state_str, state_str_len);
566 if(state_str && !state_str_len)
567 return 0;
569 if(state_bit & ~(STATE_SYSTEM_VALID | STATE_SYSTEM_HASPOPUP)) {
570 if(state_str && state_str_len)
571 state_str[0] = 0;
572 return 0;
575 state_id = IDS_STATE_NORMAL;
576 while(state_bit) {
577 state_id++;
578 state_bit /= 2;
581 if(state_str) {
582 UINT ret = LoadStringA(oleacc_handle, state_id, state_str, state_str_len);
583 if(!ret && state_str_len)
584 state_str[0] = 0;
585 return ret;
586 }else {
587 CHAR tmp[256];
588 return LoadStringA(oleacc_handle, state_id, tmp, sizeof(tmp));
592 HRESULT WINAPI AccessibleChildren(IAccessible *container,
593 LONG start, LONG count, VARIANT *children, LONG *children_cnt)
595 IEnumVARIANT *ev;
596 LONG i, child_no;
597 HRESULT hr;
599 TRACE("%p %d %d %p %p\n", container, start, count, children, children_cnt);
601 if(!container || !children || !children_cnt)
602 return E_INVALIDARG;
604 for(i=0; i<count; i++)
605 VariantInit(children+i);
607 hr = IAccessible_QueryInterface(container, &IID_IEnumVARIANT, (void**)&ev);
608 if(SUCCEEDED(hr)) {
609 hr = IEnumVARIANT_Reset(ev);
610 if(SUCCEEDED(hr))
611 hr = IEnumVARIANT_Skip(ev, start);
612 if(SUCCEEDED(hr))
613 hr = IEnumVARIANT_Next(ev, count, children, (ULONG*)children_cnt);
614 IEnumVARIANT_Release(ev);
615 return hr;
618 hr = IAccessible_get_accChildCount(container, &child_no);
619 if(FAILED(hr))
620 return hr;
622 for(i=0; i<count && start+i+1<=child_no; i++) {
623 IDispatch *disp;
625 V_VT(children+i) = VT_I4;
626 V_I4(children+i) = start+i+1;
628 hr = IAccessible_get_accChild(container, children[i], &disp);
629 if(SUCCEEDED(hr) && disp) {
630 V_VT(children+i) = VT_DISPATCH;
631 V_DISPATCH(children+i) = disp;
635 *children_cnt = i;
636 return i==count ? S_OK : S_FALSE;