Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / winaccessibility / source / UAccCOM / AccActionBase.cxx
blob0ad4db09c8674ccc0446b256955aa3c2d07cb601
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
21 // AccActionBase.cpp: implementation of the CAccActionBase class.
23 #include "stdafx.h"
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>
31 #include <vcl/svapp.hxx>
33 #include "AccessibleKeyStroke.h"
35 #include "acccommon.h"
37 using namespace com::sun::star::accessibility::AccessibleRole;
38 using namespace com::sun::star::accessibility;
39 using namespace com::sun::star::uno;
40 using namespace com::sun::star::awt;
43 // Construction/Destruction
46 CAccActionBase::CAccActionBase()
49 CAccActionBase::~CAccActionBase()
52 /**
53 * Helper function used for getting default action by UNO role.
55 * @param pRContext UNO context interface pointer.
56 * @param pRet the corresponding string to be returned.
58 void GetDfActionByUNORole(XAccessibleContext* pRContext, BSTR* pRet)
60 // #CHECK#
61 if(pRContext == NULL || pRet == NULL)
63 return;
66 long Role = pRContext->getAccessibleRole();
68 switch(Role)
70 case PUSH_BUTTON:
71 *pRet = ::SysAllocString(PRESS_STR);
72 break;
73 case RADIO_BUTTON:
74 case MENU_ITEM:
75 case LIST_ITEM:
76 *pRet = ::SysAllocString(SELECT_STR);
77 break;
78 case CHECK_BOX:
80 Reference< XAccessibleStateSet > pRState = pRContext->getAccessibleStateSet();
81 if( !pRState.is() )
83 return;
86 Sequence<short> pStates = pRState->getStates();
87 int count = pStates.getLength();
88 *pRet = ::SysAllocString(CHECK_STR);
89 for( int iIndex = 0;iIndex < count;iIndex++ )
91 if( pStates[iIndex] == AccessibleStateType::CHECKED )
93 SAFE_SYSFREESTRING(*pRet);
94 *pRet = ::SysAllocString(UNCHECK_STR);
95 break;
98 break;
104 * Returns the number of action.
106 * @param nActions the number of action.
108 STDMETHODIMP CAccActionBase::nActions(/*[out,retval]*/long* nActions)
110 SolarMutexGuard g;
112 ENTER_PROTECTED_BLOCK
114 // #CHECK#
115 if( pRXAct.is() && nActions != NULL )
117 *nActions = GetXInterface()->getAccessibleActionCount();
118 return S_OK;
120 *nActions = 0;
122 return S_OK;
124 LEAVE_PROTECTED_BLOCK
128 * Performs specified action on the object.
130 * @param actionIndex the index of action.
132 STDMETHODIMP CAccActionBase::doAction(/* [in] */ long actionIndex)
134 SolarMutexGuard g;
136 ENTER_PROTECTED_BLOCK
138 if( pRXAct.is() )
140 return GetXInterface()->doAccessibleAction( actionIndex )?S_OK:E_FAIL;
142 return E_FAIL;
144 LEAVE_PROTECTED_BLOCK
148 * Gets description of specified action.
150 * @param actionIndex the index of action.
151 * @param description the description string of the specified action.
153 STDMETHODIMP CAccActionBase::get_description(long actionIndex,BSTR __RPC_FAR *description)
155 SolarMutexGuard g;
157 ENTER_PROTECTED_BLOCK
159 // #CHECK#
160 if(description == NULL)
161 return E_INVALIDARG;
163 // #CHECK XInterface#
164 if(!pRXAct.is())
165 return E_FAIL;
167 ::rtl::OUString ouStr = GetXInterface()->getAccessibleActionDescription(actionIndex);
168 // #CHECK#
170 SAFE_SYSFREESTRING(*description);
171 *description = SysAllocString((OLECHAR*)ouStr.getStr());
173 return S_OK;
175 LEAVE_PROTECTED_BLOCK
178 STDMETHODIMP CAccActionBase::get_name( long, BSTR __RPC_FAR *)
180 return E_NOTIMPL;
183 STDMETHODIMP CAccActionBase::get_localizedName( long, BSTR __RPC_FAR *)
185 return E_NOTIMPL;
189 * Returns key binding object (if any) associated with specified action
190 * key binding is string.
191 * e.g. "alt+d" (like IAccessible::get_accKeyboardShortcut).
193 * @param actionIndex the index of action.
194 * @param nMaxBinding the max number of key binding.
195 * @param keyBinding the key binding array.
196 * @param nBinding the actual number of key binding returned.
198 STDMETHODIMP CAccActionBase::get_keyBinding(
199 /* [in] */ long actionIndex,
200 /* [in] */ long,
201 /* [length_is][length_is][size_is][size_is][out] */ BSTR __RPC_FAR *__RPC_FAR *keyBinding,
202 /* [retval][out] */ long __RPC_FAR *nBinding)
204 SolarMutexGuard g;
206 ENTER_PROTECTED_BLOCK
208 if( !keyBinding || !nBinding)
209 return E_INVALIDARG;
211 if( !pRXAct.is() )
212 return E_FAIL;
214 Reference< XAccessibleKeyBinding > binding = GetXInterface()->getAccessibleActionKeyBinding(actionIndex);
215 if( !binding.is() )
216 return E_FAIL;
218 long nCount = (binding.get())->getAccessibleKeyBindingCount();
220 OLECHAR wString[64];
222 *keyBinding = (BSTR*)::CoTaskMemAlloc(nCount*sizeof(BSTR));
224 // #CHECK Memory Allocation#
225 if(*keyBinding == NULL)
226 return E_FAIL;
228 for( int index = 0;index < nCount;index++ )
230 memset(wString,0,sizeof(wString));
231 GetkeyBindingStrByXkeyBinding( (binding.get())->getAccessibleKeyBinding(index), wString );
233 (*keyBinding)[index] = SysAllocString(wString);
236 *nBinding = nCount;
237 return S_OK;
239 LEAVE_PROTECTED_BLOCK
243 * Override of IUNOXWrapper.
245 * @param pXInterface the pointer of UNO interface.
247 STDMETHODIMP CAccActionBase::put_XInterface(hyper pXInterface)
249 // internal IUNOXWrapper - no mutex meeded
251 ENTER_PROTECTED_BLOCK
253 CUNOXWrapper::put_XInterface(pXInterface);
255 //special query.
256 if(pUNOInterface == NULL)
257 return E_FAIL;
258 Reference<XAccessibleContext> pRContext = pUNOInterface->getAccessibleContext();
259 if( !pRContext.is() )
260 return E_FAIL;
262 Reference<XAccessibleAction> pRXI(pRContext,UNO_QUERY);
263 if( !pRXI.is() )
264 pRXAct = NULL;
265 else
266 pRXAct = pRXI.get();
267 return S_OK;
269 LEAVE_PROTECTED_BLOCK
273 * Helper function used for converting keybinding to string.
275 * @param keySet the key stroke sequence.
276 * @param pString the output keybinding string.
278 void CAccActionBase::GetkeyBindingStrByXkeyBinding( const Sequence< KeyStroke > &keySet, OLECHAR* pString )
280 // #CHECK#
281 if(pString == NULL)
282 return;
284 for( int iIndex = 0;iIndex < keySet.getLength();iIndex++ )
286 KeyStroke stroke = keySet[iIndex];
287 OLECHAR wString[64] = {NULL};
288 wcscat(wString, OLESTR("\n"));
289 wcscat(wString, &stroke.KeyChar);
291 wcscat( pString, wString);
296 * Helper function used for converting key code to ole string.
298 * @param key the key code.
300 OLECHAR const * CAccActionBase::getOLECHARFromKeyCode(long key)
302 static struct keyMap
304 int keyCode;
305 OLECHAR const * key;
307 map[] =
309 #define CODEENTRY(key) {KEYCODE_##key, L#key}
310 {MODIFIER_SHIFT, L"SHIFT" },
311 {MODIFIER_CTRL, L"CTRL" },
312 {MODIFIER_ALT, L"ALT" },
313 CODEENTRY(NUM0),CODEENTRY(NUM1),CODEENTRY(NUM2),CODEENTRY(NUM3),CODEENTRY(NUM4),CODEENTRY(NUM5),
314 CODEENTRY(NUM6),CODEENTRY(NUM7),CODEENTRY(NUM8),CODEENTRY(NUM9),
315 CODEENTRY(A),CODEENTRY(B),CODEENTRY(C),CODEENTRY(D),CODEENTRY(E),CODEENTRY(F),
316 CODEENTRY(G),CODEENTRY(H),CODEENTRY(I),CODEENTRY(J),CODEENTRY(K),CODEENTRY(L),
317 CODEENTRY(M),CODEENTRY(N),CODEENTRY(O),CODEENTRY(P),CODEENTRY(Q),CODEENTRY(R),
318 CODEENTRY(S),CODEENTRY(T),CODEENTRY(U),CODEENTRY(V),CODEENTRY(W),CODEENTRY(X),
319 CODEENTRY(Y),CODEENTRY(Z),
320 CODEENTRY(F1),CODEENTRY(F2),CODEENTRY(F3),CODEENTRY(F4),CODEENTRY(F5),CODEENTRY(F6),
321 CODEENTRY(F7),CODEENTRY(F8),CODEENTRY(F9),CODEENTRY(F10),CODEENTRY(F11),CODEENTRY(F12),
322 CODEENTRY(F13),CODEENTRY(F14),CODEENTRY(F15),CODEENTRY(F16),CODEENTRY(F17),CODEENTRY(F18),
323 CODEENTRY(F19),CODEENTRY(F20),CODEENTRY(F21),CODEENTRY(F22),CODEENTRY(F23),CODEENTRY(F24),
324 CODEENTRY(F25),CODEENTRY(F26),
326 { KEYCODE_DOWN, L"DOWN" },
327 { KEYCODE_UP, L"UP" },
328 { KEYCODE_LEFT, L"LEFT" },
329 { KEYCODE_RIGHT, L"RIGHT" },
330 { KEYCODE_HOME, L"HOME" },
331 { KEYCODE_END, L"END" },
332 { KEYCODE_PAGEUP, L"PAGEUP" },
333 { KEYCODE_PAGEDOWN, L"PAGEDOWN" },
334 { KEYCODE_RETURN, L"RETURN" },
335 { KEYCODE_ESCAPE, L"ESCAPE" },
336 { KEYCODE_TAB, L"TAB" },
337 { KEYCODE_BACKSPACE, L"BACKSPACE" },
338 { KEYCODE_SPACE, L"SPACE" },
339 { KEYCODE_INSERT, L"INSERT" },
340 { KEYCODE_DELETE, L"DELETE" },
341 { KEYCODE_ADD, L"ADD" },
342 { KEYCODE_SUBTRACT, L"SUBTRACT" },
343 { KEYCODE_MULTIPLY, L"MULTIPLY" },
344 { KEYCODE_DIVIDE, L"DIVIDE" },
345 { KEYCODE_POINT, L"POINT" },
346 { KEYCODE_COMMA, L"COMMA" },
347 { KEYCODE_LESS, L"LESS" },
348 { KEYCODE_GREATER, L"GREATER" },
349 { KEYCODE_EQUAL, L"EQUAL" },
350 { KEYCODE_OPEN, L"OPEN" },
351 { KEYCODE_CUT, L"CUT" },
352 { KEYCODE_COPY, L"COPY" },
353 { KEYCODE_PASTE, L"PASTE" },
354 { KEYCODE_UNDO, L"UNDO" },
355 { KEYCODE_REPEAT, L"REPEAT" },
356 { KEYCODE_FIND, L"FIND" },
357 { KEYCODE_PROPERTIES, L"PROPERTIES" },
358 { KEYCODE_FRONT, L"FRONT" },
359 { KEYCODE_CONTEXTMENU, L"CONTEXTMENU" },
360 { KEYCODE_HELP, L"HELP" },
362 static long nCount = SAL_N_ELEMENTS(map);
364 long min = 0;
365 long max = nCount-1;
366 long mid = nCount/2;
367 while(min<max)
369 if(key<map[mid].keyCode)
370 max = mid-1;
371 else if(key>map[mid].keyCode)
372 min = mid+1;
373 else
374 break;
375 mid = (min+max)/2;
378 if(key == map[mid].keyCode)
380 return map[mid].key;
382 else
384 return NULL;
388 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */