1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright IBM Corporation 2010.
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org.  If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 #ifndef __MACCESSIBLE_H_
30 #define __MACCESSIBLE_H_
31 
32 #include <Windows.h>
33 #include "resource.h"       // main symbols
34 #include <vector>
35 #include <map>
36 #include <com/sun/star/accessibility/XAccessible.hpp>
37 #include <com/sun/star/accessibility/XAccessibleSelection.hpp>
38 #include <com/sun/star/accessibility/XAccessibleAction.hpp>
39 #include <AccObjectManagerAgent.hxx>
40 #include "EnumVariant.h"
41 #ifndef __ACCCOMMON_H_
42 #include "acccommon.h"
43 #endif
44 #ifndef __AccessibleApplication_h__
45 #include "AccessibleApplication.h"
46 #endif
47 #ifndef _RTL_USTRING_HXX_
48 #include <rtl/ustring.hxx>
49 #endif
50 
51 #define     CHILDID_SELF        0
52 #define     OBJID_WINDOW        ((LONG)0x00000000)
53 #define     OBJID_SYSMENU       ((LONG)0xFFFFFFFF)
54 #define     OBJID_TITLEBAR      ((LONG)0xFFFFFFFE)
55 #define     OBJID_MENU          ((LONG)0xFFFFFFFD)
56 #define     OBJID_CLIENT        ((LONG)0xFFFFFFFC)
57 #define     OBJID_VSCROLL       ((LONG)0xFFFFFFFB)
58 #define     OBJID_HSCROLL       ((LONG)0xFFFFFFFA)
59 #define     OBJID_SIZEGRIP      ((LONG)0xFFFFFFF9)
60 #define     OBJID_CARET         ((LONG)0xFFFFFFF8)
61 #define     OBJID_CURSOR        ((LONG)0xFFFFFFF7)
62 #define     OBJID_ALERT         ((LONG)0xFFFFFFF6)
63 #define     OBJID_SOUND         ((LONG)0xFFFFFFF5)
64 #define     OBJID_QUERYCLASSNAMEIDX ((LONG)0xFFFFFFF4)
65 #define     OBJID_NATIVEOM      ((LONG)0xFFFFFFF0)
66 
67 using namespace rtl;
68 /**
69  *This class implements IMAccessible interface, which inherits from IAccessible2, and
70  *in turn inherits from IAccessible. So its methods include the methods defined only in
71  *IAccessible, plus the methods defined only in IAccessible2, plus the methods defined
72  *only in IMAccessible.
73  */
74 class ATL_NO_VTABLE CMAccessible :
75             public CComObjectRoot,
76             public CComCoClass<CMAccessible, &CLSID_MAccessible>,
77             public IDispatchImpl<IMAccessible, &IID_IMAccessible, &LIBID_UACCCOMLib>,
78             public IServiceProvider,
79             public IAccessibleApplication
80 {
81     typedef map< const GUID, CComPtr<IUnknown> ,ltComp > XGUIDToComObjHash;
82 
83     typedef HRESULT (WINAPI _UNO_AGGCREATORFUNC)(void*, REFIID, LPVOID*);
84 
85     struct _UNO_AGGMAP_ENTRY
86     {
87         const IID* piid;
88         _UNO_AGGCREATORFUNC* pfnCreateInstance;
89         int XIFIndex;
90     };
91 
92     enum XInterfaceIndex {
93         XI_COMPONENT	= 0x01,
94         XI_TEXT			= 0x02,
95         XI_TABLE		= 0x03,
96         XI_EDITABLETEXT	= 0x04,
97         XI_IMAGE		= 0x05,
98         XI_SELECTION	= 0x06,
99         XI_EXTENDEDCOMP	= 0x07,
100         XI_VALUE		= 0x08,
101         XI_KEYBINDING	= 0x09,
102         XI_ACTION		= 0x0A,
103         XI_HYPERTEXT	= 0x0B,
104         XI_HYPERLINK	= 0x0C,
105         XI_ATTRIBUTE	= 0x0D,
106         XI_NULL			= -1
107     };
108 
109 public:
110     CMAccessible();
111     virtual ~CMAccessible();
112     DECLARE_REGISTRY_RESOURCEID(IDR_MACCESSIBLE)
113 
114     DECLARE_GET_CONTROLLING_UNKNOWN()
115 
116     DECLARE_PROTECT_FINAL_CONSTRUCT()
117 
118     BEGIN_COM_MAP(CMAccessible)
119     COM_INTERFACE_ENTRY(IMAccessible)
120     COM_INTERFACE_ENTRY(IAccessible)
121     COM_INTERFACE_ENTRY(IAccessible2)
122     COM_INTERFACE_ENTRY(IDispatch)
123     COM_INTERFACE_ENTRY(IAccessibleApplication)
124     COM_INTERFACE_ENTRY(IServiceProvider)
125     COM_INTERFACE_ENTRY_FUNC_BLIND(NULL,_SmartQI)
126     END_COM_MAP()
127 
128     // AGGREGATE OBJECT MAP
129     BEGIN_AGGOBJECT_MAP(CMAccessible)
130     AGGOBJECT_ENTRY(IID_IAccessibleComponent, CLSID_AccComponent, COMPONENT)
131     AGGOBJECT_ENTRY(IID_IAccessibleText, CLSID_AccText, TEXT)
132     AGGOBJECT_ENTRY(IID_IAccessibleEditableText, CLSID_AccEditableText, EDITABLETEXT)
133     AGGOBJECT_ENTRY(IID_IAccessibleImage, CLSID_AccImage, IMAGE)
134     AGGOBJECT_ENTRY(IID_IAccessibleTable, CLSID_AccTable, TABLE)
135     AGGOBJECT_ENTRY(IID_IAccessibleAction, CLSID_AccAction, ACTION)
136     AGGOBJECT_ENTRY(IID_IAccessibleValue, CLSID_AccValue, VALUE)
137     AGGOBJECT_ENTRY(IID_IAccessibleHypertext, CLSID_AccHypertext, HYPERTEXT)
138     AGGOBJECT_ENTRY(IID_IAccessibleHyperlink, CLSID_AccHyperLink, HYPERLINK)
139 
140 
141     END_AGGOBJECT_MAP()
142 
143     // IMAccessible
144     STDMETHOD(put_accValue)(VARIANT varChild,BSTR szValue);
145     STDMETHOD(put_accName)(VARIANT varChild,BSTR szName);
146     STDMETHOD(accDoDefaultAction)(VARIANT varChild);
147     STDMETHOD(accHitTest)(long xLeft,long yTop,VARIANT *pvarChild);
148     STDMETHOD(accNavigate)(long navDir,VARIANT varStart,VARIANT *pvarEndUpAt);
149     STDMETHOD(accLocation)(long *pxLeft,long *pyTop,long *pcxWidth,long *pcyHeight,VARIANT varChild);
150     STDMETHOD(accSelect)(long flagsSelect,VARIANT varChild);
151     STDMETHOD(get_accDefaultAction)( VARIANT varChild,BSTR *pszDefaultAction);
152     STDMETHOD(get_accSelection)(VARIANT *pvarChildren);
153     STDMETHOD(get_accFocus)(VARIANT *pvarChild);
154     STDMETHOD(get_accKeyboardShortcut)( VARIANT varChild,BSTR *pszKeyboardShortcut);
155     STDMETHOD(get_accHelpTopic)(BSTR *pszHelpFile,VARIANT varChild,long *pidTopic);
156     STDMETHOD(get_accHelp)(VARIANT varChild,BSTR *pszHelp);
157     STDMETHOD(get_accState)(VARIANT varChild,VARIANT *pvarState);
158     STDMETHOD(get_accRole)(VARIANT varChild,VARIANT *pvarRole);
159     STDMETHOD(get_accDescription)(VARIANT varChild,BSTR *pszDescription);
160     STDMETHOD(get_accValue)( VARIANT varChild,BSTR *pszValue);
161     STDMETHOD(get_accName)(VARIANT varChild,BSTR *pszName);
162     STDMETHOD(get_accChild)(VARIANT varChild,IDispatch **ppdispChild);
163     STDMETHOD(get_accChildCount)(long *pcountChildren);
164     STDMETHOD(get_accParent)( IDispatch **ppdispParent);
165 
166     // methods which are defined only in the IAccessible2
167     // These methods only declare here, and their implementation bodies are empty now.
168     STDMETHOD(get_nRelations)( long __RPC_FAR *nRelations) ;
169     STDMETHOD(get_relation)( long relationIndex, IAccessibleRelation __RPC_FAR *__RPC_FAR *relation) ;
170     STDMETHOD(get_relations)( long maxRelations, IAccessibleRelation __RPC_FAR *__RPC_FAR *relation, long __RPC_FAR *nRelations) ;
171     STDMETHOD(role)(long __RPC_FAR *role);
172     STDMETHOD(get_nActions)(long __RPC_FAR *nActions);
173     STDMETHOD(scrollTo)(enum IA2ScrollType scrollType);
174     STDMETHOD(scrollToPoint)(enum IA2CoordinateType coordinateType, long x, long y);
175     STDMETHOD(get_groupPosition)(long __RPC_FAR *groupLevel,long __RPC_FAR *similarItemsInGroup,long __RPC_FAR *positionInGroup);
176     STDMETHOD(get_states)( AccessibleStates __RPC_FAR *states );
177     STDMETHOD(get_extendedRole)( BSTR __RPC_FAR *extendedRole );
178     STDMETHOD(get_localizedExtendedRole)( BSTR __RPC_FAR *localizedExtendedRole );
179     STDMETHOD(get_nExtendedStates)( long __RPC_FAR *nExtendedStates);
180     STDMETHOD(get_extendedStates)( long maxExtendedStates, BSTR __RPC_FAR *__RPC_FAR *extendedStates, long __RPC_FAR *nExtendedStates);
181     STDMETHOD(get_localizedExtendedStates)(long maxLocalizedExtendedStates,BSTR __RPC_FAR *__RPC_FAR *localizedExtendedStates,long __RPC_FAR *nLocalizedExtendedStates);
182     STDMETHOD(get_uniqueID)(long __RPC_FAR *uniqueID);
183     STDMETHOD(get_windowHandle)(HWND __RPC_FAR *windowHandle);
184     STDMETHOD(get_indexInParent)( long __RPC_FAR *accParentIndex );
185     STDMETHOD(get_locale)( IA2Locale __RPC_FAR *locale );
186     STDMETHOD(get_attributes)(/*[out]*/ BSTR *pAttr);
187 
188     //IServiceProvider.
189     STDMETHOD(QueryService)(REFGUID guidService, REFIID riid, void** ppvObject);
190 
191     //IAccessibleApplication
192     STDMETHOD(get_appName)(BSTR __RPC_FAR *name);
193     STDMETHOD(get_appVersion)(BSTR __RPC_FAR *version);
194     STDMETHOD(get_toolkitName)(BSTR __RPC_FAR *name);
195     STDMETHOD(get_toolkitVersion)(BSTR __RPC_FAR *version);
196 
197     // methods which are defined only in IMAccessible
198     // These methods are provided for UNO management system.
199     // The UNO management system use these methods to put Accessibility
200     // information to COM.
201     STDMETHOD(Put_XAccName)(const OLECHAR __RPC_FAR *pszName);
202     STDMETHOD(Put_XAccRole)(unsigned short pRole);
203     STDMETHOD(DecreaseState)(DWORD pXSate);
204     STDMETHOD(IncreaseState)(DWORD pXSate);
205     STDMETHOD(SetState)(DWORD pXSate);
206     STDMETHOD(Put_XAccDescription)(const OLECHAR __RPC_FAR *pszDescription);
207     STDMETHOD(Put_XAccValue)(const OLECHAR __RPC_FAR *pszAccValue);
208     STDMETHOD(Put_XAccLocation)(const Location sLocation);
209     STDMETHOD(Put_XAccFocus)(long dChildID);
210     STDMETHOD(Put_XAccParent)(IMAccessible __RPC_FAR *pIParent);
211     STDMETHOD(Put_XAccWindowHandle)(HWND hwnd);
212     STDMETHOD(Put_XAccChildID)(long dChildID);
213     STDMETHOD(Put_XAccAgent)(long pAgent);
214     STDMETHOD(NotifyDestroy)(BOOL isDestroy);
215     STDMETHOD(Put_ActionDescription)( const OLECHAR* szAction);
216     STDMETHOD(SetDefaultAction)(long pAction);
217     STDMETHOD(GetUNOInterface)(long*);
218     STDMETHOD(SetXAccessible)(long);
219 
220 private:
221     OLECHAR* m_pszName;
222     OLECHAR* m_pszValue;
223     OLECHAR* m_pszActionDescription;
224     unsigned short m_iRole;
225     DWORD	m_dState;
226     OLECHAR* m_pszDescription;
227     IMAccessible* m_pIParent;
228     Location m_sLocation;
229 
230     // identify a COM object/Acc object uniquely
231     long m_dChildID;
232     // specify the focus child ID in object self and its direct children
233 
234     long m_dFocusChildID;
235     // parent window handle,will be used in the future application, its value comes from UNO
236 
237     HWND m_hwnd;
238 
239     // the COM class which implements IEnumVARIANT interface,currently only used in
240     // the implementation of get_accSelection
241     CEnumVariant* m_pEnumVar;
242 
243     // specify if the XAccessible is invalid
244     BOOL m_isDestroy;
245 
246     XAccessible*  pUNOInterface;
247     Reference< XAccessible > pRef;
248     XAccessible*  pAchorUNOInterface;
249     XAccessibleAction*    m_pXAction;
250     XAccessibleContext*  pRContextInterface;
251     Reference<XAccessibleContext> pRContext;
252 
253 private:
254 
255     // the helper methods in order to implement the above public methods
256     IMAccessible* GetChildInterface(long dChildIndex);//notice here the parameter is child index,not child id
257     IMAccessible* GetNavigateChildForDM(VARIANT varCur,short flags);//for decendant manage
258     HRESULT GetFirstChild(VARIANT varStart,VARIANT* pvarEndUpAt);//for accNavigate implementation
259     HRESULT GetLastChild(VARIANT varStart,VARIANT* pvarEndUpAt);//for accNavigate implementation
260     HRESULT GetNextSibling(VARIANT varStart,VARIANT* pvarEndUpAt);//for accNavigate implementation
261     HRESULT GetPreSibling(VARIANT varStart,VARIANT* pvarEndUpAt);//for accNavigate implementation
262     BOOL IsDecendantManage();//identify whether the current COM belongs to manage_decendant roles
263 
264     // the following private methods are used to implement accSelect method
265     HRESULT SelectChild(XAccessible* pItem);
266     HRESULT DeSelectChild(XAccessible* pItem);
267     HRESULT	SelectMutipleChidren( XAccessible** pItem,int size );
268     HRESULT DeSelectMutipleChildren( XAccessible** pItem,int size );
269     XAccessibleContext* GetContextByXAcc( XAccessible* pXAcc );
270     Reference< XAccessibleSelection > GetSelection();
271     // end accSelect implementation methods
272     BOOL GetXInterfaceFromXAccessible(XAccessible*, XInterface**, int);
273     HRESULT WINAPI SmartQI(void* pv, REFIID iid, void** ppvObject);
274 
275 public:
276     STDMETHOD(Get_XAccChildID)(/*[out,retval]*/ long* childID);
277     // AccObjectManagerAgent is a management object in UNO, here keep its pointer for
278     // the implementation of accNavigate when decendant manage happens for List,Tree, or Table
279     // AccObjectManagerAgent and the following UNO objects XAccessble,XAccessibleSelection,
280     // XAccessibleAction are all used to operate UNO accessiblility information directly when
281     // implement some specific MSAA methods,such as accSelection,accNavigate
282     static AccObjectManagerAgent* g_pAgent;
283 
284     static BOOL get_IAccessibleFromXAccessible(long pXAcc,IAccessible** ppIA);
285     BOOL m_bRequiresSave;
286     XGUIDToComObjHash m_containedObjects;
287 
288     static HRESULT WINAPI _SmartQI(void* pv,
289                                    REFIID iid, void** ppvObject, DWORD)
290     {
291         return ((CMAccessible*)pv)->SmartQI(pv,iid,ppvObject);
292     }
293 
294     static void get_OLECHARFromAny(Any& pAny, OLECHAR* pChar);
295 
296     static void get_OLECHAR4Numbering(const Any& pAny, short numberingLevel, const OUString& numberingPrefix,OLECHAR* pChar);
297 
298     // Helper function for data conversion.
299     static void ConvertAnyToVariant(const ::com::sun::star::uno::Any &rAnyVal, VARIANT *pvData);
300 };
301 
302 
303 
304 #endif //__MACCESSIBLE_H_
305