1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 
21 // AccActionBase.cpp: implementation of the CAccActionBase class.
22 
23 #include "stdafx.h"
24 
25 #include "AccActionBase.h"
26 #include <com/sun/star/accessibility/XAccessible.hpp>
27 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
28 #include <com/sun/star/accessibility/AccessibleRole.hpp>
29 #include <com/sun/star/accessibility/XAccessibleContext.hpp>
30 
31 #include <vcl/svapp.hxx>
32 #include <o3tl/char16_t2wchar_t.hxx>
33 #include <comphelper/AccessibleImplementationHelper.hxx>
34 
35 #include "AccessibleKeyStroke.h"
36 
37 #include "acccommon.h"
38 
39 using namespace com::sun::star::accessibility::AccessibleRole;
40 using namespace com::sun::star::accessibility;
41 using namespace com::sun::star::uno;
42 using namespace com::sun::star::awt;
43 
44 
45 // Construction/Destruction
46 
47 
CAccActionBase()48 CAccActionBase::CAccActionBase()
49 {}
50 
~CAccActionBase()51 CAccActionBase::~CAccActionBase()
52 {}
53 
54 /**
55  * Returns the number of action.
56  *
57  * @param    nActions    the number of action.
58  */
nActions(long * nActions)59 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccActionBase::nActions(/*[out,retval]*/long* nActions)
60 {
61     SolarMutexGuard g;
62 
63     ENTER_PROTECTED_BLOCK
64 
65     // #CHECK#
66     if( pRXAct.is() && nActions != nullptr )
67     {
68         *nActions = GetXInterface()->getAccessibleActionCount();
69         return S_OK;
70     }
71     *nActions = 0;
72 
73     return S_OK;
74 
75     LEAVE_PROTECTED_BLOCK
76 }
77 
78 /**
79  * Performs specified action on the object.
80  *
81  * @param    actionIndex    the index of action.
82  */
doAction(long actionIndex)83 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccActionBase::doAction(/* [in] */ long actionIndex)
84 {
85     SolarMutexGuard g;
86 
87     ENTER_PROTECTED_BLOCK
88 
89     if( pRXAct.is() )
90     {
91         return GetXInterface()->doAccessibleAction( actionIndex )?S_OK:E_FAIL;
92     }
93     return E_FAIL;
94 
95     LEAVE_PROTECTED_BLOCK
96 }
97 
98 /**
99  * Gets description of specified action.
100  *
101  * @param    actionIndex    the index of action.
102  * @param    description    the description string of the specified action.
103  */
get_description(long actionIndex,BSTR __RPC_FAR * description)104 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccActionBase::get_description(long actionIndex,BSTR __RPC_FAR *description)
105 {
106     SolarMutexGuard g;
107 
108     ENTER_PROTECTED_BLOCK
109 
110     // #CHECK#
111     if(description == nullptr)
112         return E_INVALIDARG;
113 
114     // #CHECK XInterface#
115     if(!pRXAct.is())
116         return E_FAIL;
117 
118     OUString ouStr = GetXInterface()->getAccessibleActionDescription(actionIndex);
119     // #CHECK#
120 
121     SAFE_SYSFREESTRING(*description);
122     *description = SysAllocString(o3tl::toW(ouStr.getStr()));
123 
124     return S_OK;
125 
126     LEAVE_PROTECTED_BLOCK
127 }
128 
get_name(long,BSTR __RPC_FAR *)129 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccActionBase::get_name( long, BSTR __RPC_FAR *)
130 {
131     return E_NOTIMPL;
132 }
133 
get_localizedName(long,BSTR __RPC_FAR *)134 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccActionBase::get_localizedName( long, BSTR __RPC_FAR *)
135 {
136     return E_NOTIMPL;
137 }
138 
139 /**
140  * Returns key binding object (if any) associated with specified action
141  * key binding is string.
142  * e.g. "alt+d" (like IAccessible::get_accKeyboardShortcut).
143  *
144  * @param    actionIndex    the index of action.
145  * @param    nMaxBinding    the max number of key binding.
146  * @param    keyBinding     the key binding array.
147  * @param    nBinding       the actual number of key binding returned.
148  */
get_keyBinding(long actionIndex,long,BSTR __RPC_FAR * __RPC_FAR * keyBinding,long __RPC_FAR * nBinding)149 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccActionBase::get_keyBinding(
150     /* [in] */ long actionIndex,
151     /* [in] */ long,
152     /* [length_is][length_is][size_is][size_is][out] */ BSTR __RPC_FAR *__RPC_FAR *keyBinding,
153     /* [retval][out] */ long __RPC_FAR *nBinding)
154 {
155     SolarMutexGuard g;
156 
157     ENTER_PROTECTED_BLOCK
158 
159     if( !keyBinding || !nBinding)
160         return E_INVALIDARG;
161 
162     if( !pRXAct.is() )
163         return E_FAIL;
164 
165     Reference< XAccessibleKeyBinding > binding = GetXInterface()->getAccessibleActionKeyBinding(actionIndex);
166     if( !binding.is() )
167         return E_FAIL;
168 
169     sal_Int32 nCount = binding->getAccessibleKeyBindingCount();
170 
171     *keyBinding = static_cast<BSTR*>(::CoTaskMemAlloc(nCount*sizeof(BSTR)));
172 
173     // #CHECK Memory Allocation#
174     if(*keyBinding == nullptr)
175         return E_FAIL;
176 
177     for( sal_Int32 index = 0;index < nCount;index++ )
178     {
179         auto const wString = comphelper::GetkeyBindingStrByXkeyBinding(
180             binding->getAccessibleKeyBinding(index));
181 
182         (*keyBinding)[index] = SysAllocString(o3tl::toW(wString.getStr()));
183     }
184 
185     *nBinding = nCount;
186     return S_OK;
187 
188     LEAVE_PROTECTED_BLOCK
189 }
190 
191 /**
192  * Override of IUNOXWrapper.
193  *
194  * @param    pXInterface    the pointer of UNO interface.
195  */
put_XInterface(hyper pXInterface)196 COM_DECLSPEC_NOTHROW STDMETHODIMP CAccActionBase::put_XInterface(hyper pXInterface)
197 {
198     // internal IUNOXWrapper - no mutex meeded
199 
200     ENTER_PROTECTED_BLOCK
201 
202     CUNOXWrapper::put_XInterface(pXInterface);
203 
204     //special query.
205     if(pUNOInterface == nullptr)
206         return E_FAIL;
207     Reference<XAccessibleContext> pRContext = pUNOInterface->getAccessibleContext();
208     if( !pRContext.is() )
209         return E_FAIL;
210 
211     Reference<XAccessibleAction> pRXI(pRContext,UNO_QUERY);
212     if( !pRXI.is() )
213         pRXAct = nullptr;
214     else
215         pRXAct = pRXI.get();
216     return S_OK;
217 
218     LEAVE_PROTECTED_BLOCK
219 }
220 
221 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
222