Line data Source code
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 : #include <accessibility/standard/accessiblemenuitemcomponent.hxx>
21 :
22 :
23 : #include <accessibility/helper/accresmgr.hxx>
24 : #include <accessibility/helper/accessiblestrings.hrc>
25 : #include <toolkit/awt/vclxwindows.hxx>
26 : #include <toolkit/helper/externallock.hxx>
27 : #include <toolkit/helper/convert.hxx>
28 :
29 : #include <com/sun/star/accessibility/AccessibleEventId.hpp>
30 : #include <com/sun/star/accessibility/AccessibleRole.hpp>
31 : #include <com/sun/star/accessibility/AccessibleStateType.hpp>
32 : #include <com/sun/star/datatransfer/clipboard/XClipboard.hpp>
33 : #include <com/sun/star/datatransfer/clipboard/XFlushableClipboard.hpp>
34 :
35 : #include <unotools/accessiblestatesethelper.hxx>
36 : #include <unotools/accessiblerelationsethelper.hxx>
37 : #include <cppuhelper/typeprovider.hxx>
38 : #include <comphelper/sequence.hxx>
39 : #include <comphelper/accessibletexthelper.hxx>
40 : #include <vcl/svapp.hxx>
41 : #include <vcl/window.hxx>
42 : #include <vcl/menu.hxx>
43 : #include <vcl/unohelp2.hxx>
44 : #include <vcl/settings.hxx>
45 :
46 : using namespace ::com::sun::star::accessibility;
47 : using namespace ::com::sun::star::uno;
48 : using namespace ::com::sun::star::beans;
49 : using namespace ::com::sun::star::lang;
50 : using namespace ::com::sun::star;
51 : using namespace ::comphelper;
52 :
53 :
54 :
55 : // class OAccessibleMenuItemComponent
56 :
57 :
58 1964 : OAccessibleMenuItemComponent::OAccessibleMenuItemComponent( Menu* pParent, sal_uInt16 nItemPos, Menu* pMenu )
59 : :OAccessibleMenuBaseComponent( pMenu )
60 : ,m_pParent( pParent )
61 1964 : ,m_nItemPos( nItemPos )
62 : {
63 1964 : m_sAccessibleName = GetAccessibleName();
64 1964 : m_sItemText = GetItemText();
65 1964 : }
66 :
67 :
68 :
69 1964 : OAccessibleMenuItemComponent::~OAccessibleMenuItemComponent()
70 : {
71 1964 : }
72 :
73 :
74 :
75 4206 : sal_Bool OAccessibleMenuItemComponent::IsEnabled()
76 : {
77 4206 : OExternalLockGuard aGuard( this );
78 :
79 4206 : sal_Bool bEnabled = sal_False;
80 4206 : if ( m_pParent )
81 4206 : bEnabled = m_pParent->IsItemEnabled( m_pParent->GetItemId( m_nItemPos ) );
82 :
83 4206 : return bEnabled;
84 : }
85 :
86 :
87 :
88 4374 : sal_Bool OAccessibleMenuItemComponent::IsVisible()
89 : {
90 4374 : sal_Bool bVisible = sal_False;
91 :
92 4374 : if ( m_pParent )
93 4374 : bVisible = m_pParent->IsItemPosVisible( m_nItemPos );
94 :
95 4374 : return bVisible;
96 : }
97 :
98 :
99 :
100 0 : void OAccessibleMenuItemComponent::Select()
101 : {
102 : // open the parent menu
103 0 : Reference< XAccessible > xParent( getAccessibleParent() );
104 0 : if ( xParent.is() )
105 : {
106 0 : OAccessibleMenuBaseComponent* pComp = static_cast< OAccessibleMenuBaseComponent* >( xParent.get() );
107 0 : if ( pComp && pComp->getAccessibleRole() == AccessibleRole::MENU && !pComp->IsPopupMenuOpen() )
108 0 : pComp->Click();
109 : }
110 :
111 : // highlight the menu item
112 0 : if ( m_pParent )
113 0 : m_pParent->HighlightItem( m_nItemPos );
114 0 : }
115 :
116 :
117 :
118 0 : void OAccessibleMenuItemComponent::DeSelect()
119 : {
120 0 : if ( m_pParent && IsSelected() )
121 0 : m_pParent->DeHighlight();
122 0 : }
123 :
124 :
125 :
126 8 : void OAccessibleMenuItemComponent::Click()
127 : {
128 : // open the parent menu
129 8 : Reference< XAccessible > xParent( getAccessibleParent() );
130 8 : if ( xParent.is() )
131 : {
132 8 : OAccessibleMenuBaseComponent* pComp = static_cast< OAccessibleMenuBaseComponent* >( xParent.get() );
133 8 : if ( pComp && pComp->getAccessibleRole() == AccessibleRole::MENU && !pComp->IsPopupMenuOpen() )
134 0 : pComp->Click();
135 : }
136 :
137 : // click the menu item
138 8 : if ( m_pParent )
139 : {
140 8 : Window* pWindow = m_pParent->GetWindow();
141 8 : if ( pWindow )
142 : {
143 : // #102438# Menu items are not selectable
144 : // Popup menus are executed asynchronously, triggered by a timer.
145 : // As Menu::SelectItem only works, if the corresponding menu window is
146 : // already created, we have to set the menu delay to 0, so
147 : // that the popup menus are executed synchronously.
148 8 : AllSettings aSettings = pWindow->GetSettings();
149 16 : MouseSettings aMouseSettings = aSettings.GetMouseSettings();
150 8 : sal_uLong nDelay = aMouseSettings.GetMenuDelay();
151 8 : aMouseSettings.SetMenuDelay( 0 );
152 8 : aSettings.SetMouseSettings( aMouseSettings );
153 8 : pWindow->SetSettings( aSettings );
154 :
155 8 : m_pParent->SelectItem( m_pParent->GetItemId( m_nItemPos ) );
156 :
157 : // meanwhile the window pointer may be invalid
158 8 : pWindow = m_pParent->GetWindow();
159 8 : if ( pWindow )
160 : {
161 : // set the menu delay back to the old value
162 8 : aSettings = pWindow->GetSettings();
163 8 : aMouseSettings = aSettings.GetMouseSettings();
164 8 : aMouseSettings.SetMenuDelay( nDelay );
165 8 : aSettings.SetMouseSettings( aMouseSettings );
166 8 : pWindow->SetSettings( aSettings );
167 8 : }
168 : }
169 8 : }
170 8 : }
171 :
172 :
173 :
174 1536 : void OAccessibleMenuItemComponent::SetItemPos( sal_uInt16 nItemPos )
175 : {
176 1536 : m_nItemPos = nItemPos;
177 1536 : }
178 :
179 :
180 :
181 0 : void OAccessibleMenuItemComponent::SetAccessibleName( const OUString& sAccessibleName )
182 : {
183 0 : if ( !m_sAccessibleName.equals( sAccessibleName ) )
184 : {
185 0 : Any aOldValue, aNewValue;
186 0 : aOldValue <<= m_sAccessibleName;
187 0 : aNewValue <<= sAccessibleName;
188 0 : m_sAccessibleName = sAccessibleName;
189 0 : NotifyAccessibleEvent( AccessibleEventId::NAME_CHANGED, aOldValue, aNewValue );
190 : }
191 0 : }
192 :
193 :
194 :
195 1964 : OUString OAccessibleMenuItemComponent::GetAccessibleName()
196 : {
197 1964 : OUString sName;
198 1964 : if ( m_pParent )
199 : {
200 1964 : sal_uInt16 nItemId = m_pParent->GetItemId( m_nItemPos );
201 1964 : sName = m_pParent->GetAccessibleName( nItemId );
202 1964 : if ( sName.isEmpty() )
203 1964 : sName = m_pParent->GetItemText( nItemId );
204 1964 : sName = OutputDevice::GetNonMnemonicString( sName );
205 : }
206 :
207 1964 : return sName;
208 : }
209 :
210 :
211 :
212 0 : void OAccessibleMenuItemComponent::SetItemText( const OUString& sItemText )
213 : {
214 0 : Any aOldValue, aNewValue;
215 0 : if ( OCommonAccessibleText::implInitTextChangedEvent( m_sItemText, sItemText, aOldValue, aNewValue ) )
216 : {
217 0 : m_sItemText = sItemText;
218 0 : NotifyAccessibleEvent( AccessibleEventId::TEXT_CHANGED, aOldValue, aNewValue );
219 0 : }
220 0 : }
221 :
222 :
223 :
224 1964 : OUString OAccessibleMenuItemComponent::GetItemText()
225 : {
226 1964 : OUString sText;
227 1964 : if ( m_pParent )
228 1964 : sText = OutputDevice::GetNonMnemonicString( m_pParent->GetItemText( m_pParent->GetItemId( m_nItemPos ) ) );
229 :
230 1964 : return sText;
231 : }
232 :
233 :
234 :
235 2242 : void OAccessibleMenuItemComponent::FillAccessibleStateSet( utl::AccessibleStateSetHelper& rStateSet )
236 : {
237 2242 : sal_Bool bEnabled = IsEnabled();
238 2242 : if ( bEnabled )
239 : {
240 2189 : rStateSet.AddState( AccessibleStateType::ENABLED );
241 2189 : rStateSet.AddState( AccessibleStateType::SENSITIVE );
242 : }
243 :
244 2242 : if ( IsVisible() )
245 : {
246 79 : rStateSet.AddState( AccessibleStateType::SHOWING );
247 79 : if( !IsMenuHideDisabledEntries() || bEnabled )
248 79 : rStateSet.AddState( AccessibleStateType::VISIBLE );
249 : }
250 2242 : rStateSet.AddState( AccessibleStateType::OPAQUE );
251 2242 : }
252 :
253 :
254 : // OCommonAccessibleComponent
255 :
256 :
257 1552 : awt::Rectangle OAccessibleMenuItemComponent::implGetBounds() throw (RuntimeException)
258 : {
259 1552 : awt::Rectangle aBounds( 0, 0, 0, 0 );
260 :
261 1552 : if ( m_pParent )
262 : {
263 : // get bounding rectangle of the item relative to the containing window
264 1552 : aBounds = AWTRectangle( m_pParent->GetBoundingRectangle( m_nItemPos ) );
265 :
266 : // get position of containing window in screen coordinates
267 1552 : Window* pWindow = m_pParent->GetWindow();
268 1552 : if ( pWindow )
269 : {
270 1552 : Rectangle aRect = pWindow->GetWindowExtentsRelative( NULL );
271 1552 : awt::Point aWindowScreenLoc = AWTPoint( aRect.TopLeft() );
272 :
273 : // get position of accessible parent in screen coordinates
274 1552 : Reference< XAccessible > xParent = getAccessibleParent();
275 1552 : if ( xParent.is() )
276 : {
277 1552 : Reference< XAccessibleComponent > xParentComponent( xParent->getAccessibleContext(), UNO_QUERY );
278 1552 : if ( xParentComponent.is() )
279 : {
280 1552 : awt::Point aParentScreenLoc = xParentComponent->getLocationOnScreen();
281 :
282 : // calculate bounding rectangle of the item relative to the accessible parent
283 1552 : aBounds.X += aWindowScreenLoc.X - aParentScreenLoc.X;
284 1552 : aBounds.Y += aWindowScreenLoc.Y - aParentScreenLoc.Y;
285 1552 : }
286 1552 : }
287 : }
288 : }
289 :
290 1552 : return aBounds;
291 : }
292 :
293 :
294 : // XComponent
295 :
296 :
297 1964 : void SAL_CALL OAccessibleMenuItemComponent::disposing()
298 : {
299 1964 : OAccessibleMenuBaseComponent::disposing();
300 :
301 1964 : m_pParent = NULL;
302 1964 : m_sAccessibleName = OUString();
303 1964 : m_sItemText = OUString();
304 1964 : }
305 :
306 :
307 : // XAccessibleContext
308 :
309 :
310 3887 : sal_Int32 OAccessibleMenuItemComponent::getAccessibleChildCount() throw (RuntimeException, std::exception)
311 : {
312 3887 : OExternalLockGuard aGuard( this );
313 :
314 3887 : return 0;
315 : }
316 :
317 :
318 :
319 0 : Reference< XAccessible > OAccessibleMenuItemComponent::getAccessibleChild( sal_Int32 i ) throw (IndexOutOfBoundsException, RuntimeException, std::exception)
320 : {
321 0 : OExternalLockGuard aGuard( this );
322 :
323 0 : if ( i < 0 || i >= getAccessibleChildCount() )
324 0 : throw IndexOutOfBoundsException();
325 :
326 0 : return Reference< XAccessible >();
327 : }
328 :
329 :
330 :
331 2446 : Reference< XAccessible > OAccessibleMenuItemComponent::getAccessibleParent( ) throw (RuntimeException, std::exception)
332 : {
333 2446 : OExternalLockGuard aGuard( this );
334 :
335 2446 : return m_pParent->GetAccessible();
336 : }
337 :
338 :
339 :
340 53 : sal_Int32 OAccessibleMenuItemComponent::getAccessibleIndexInParent( ) throw (RuntimeException, std::exception)
341 : {
342 53 : OExternalLockGuard aGuard( this );
343 :
344 53 : return m_nItemPos;
345 : }
346 :
347 :
348 :
349 0 : sal_Int16 OAccessibleMenuItemComponent::getAccessibleRole( ) throw (RuntimeException, std::exception)
350 : {
351 0 : OExternalLockGuard aGuard( this );
352 :
353 0 : return AccessibleRole::UNKNOWN;
354 : }
355 :
356 :
357 :
358 206 : OUString OAccessibleMenuItemComponent::getAccessibleDescription( ) throw (RuntimeException, std::exception)
359 : {
360 206 : OExternalLockGuard aGuard( this );
361 :
362 206 : OUString sDescription;
363 206 : if ( m_pParent )
364 206 : sDescription = m_pParent->GetHelpText( m_pParent->GetItemId( m_nItemPos ) );
365 :
366 206 : return sDescription;
367 : }
368 :
369 :
370 :
371 206 : OUString OAccessibleMenuItemComponent::getAccessibleName( ) throw (RuntimeException, std::exception)
372 : {
373 206 : OExternalLockGuard aGuard( this );
374 :
375 206 : return m_sAccessibleName;
376 : }
377 :
378 :
379 :
380 2 : Reference< XAccessibleRelationSet > OAccessibleMenuItemComponent::getAccessibleRelationSet( ) throw (RuntimeException, std::exception)
381 : {
382 2 : OExternalLockGuard aGuard( this );
383 :
384 2 : utl::AccessibleRelationSetHelper* pRelationSetHelper = new utl::AccessibleRelationSetHelper;
385 2 : Reference< XAccessibleRelationSet > xSet = pRelationSetHelper;
386 2 : return xSet;
387 : }
388 :
389 :
390 :
391 2 : Locale OAccessibleMenuItemComponent::getLocale( ) throw (IllegalAccessibleComponentStateException, RuntimeException, std::exception)
392 : {
393 2 : OExternalLockGuard aGuard( this );
394 :
395 2 : return Application::GetSettings().GetLanguageTag().getLocale();
396 : }
397 :
398 :
399 : // XAccessibleComponent
400 :
401 :
402 0 : Reference< XAccessible > OAccessibleMenuItemComponent::getAccessibleAtPoint( const awt::Point& ) throw (RuntimeException, std::exception)
403 : {
404 0 : OExternalLockGuard aGuard( this );
405 :
406 0 : return Reference< XAccessible >();
407 : }
408 :
409 :
410 :
411 1 : void OAccessibleMenuItemComponent::grabFocus( ) throw (RuntimeException, std::exception)
412 : {
413 : // no focus for items
414 1 : }
415 :
416 :
417 :
418 1 : sal_Int32 OAccessibleMenuItemComponent::getForeground( ) throw (RuntimeException, std::exception)
419 : {
420 1 : OExternalLockGuard aGuard( this );
421 :
422 1 : sal_Int32 nColor = 0;
423 2 : Reference< XAccessible > xParent = getAccessibleParent();
424 1 : if ( xParent.is() )
425 : {
426 1 : Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
427 1 : if ( xParentComp.is() )
428 1 : nColor = xParentComp->getForeground();
429 : }
430 :
431 2 : return nColor;
432 : }
433 :
434 :
435 :
436 1 : sal_Int32 OAccessibleMenuItemComponent::getBackground( ) throw (RuntimeException, std::exception)
437 : {
438 1 : OExternalLockGuard aGuard( this );
439 :
440 1 : sal_Int32 nColor = 0;
441 2 : Reference< XAccessible > xParent = getAccessibleParent();
442 1 : if ( xParent.is() )
443 : {
444 1 : Reference< XAccessibleComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
445 1 : if ( xParentComp.is() )
446 1 : nColor = xParentComp->getBackground();
447 : }
448 :
449 2 : return nColor;
450 : }
451 :
452 :
453 : // XAccessibleExtendedComponent
454 :
455 :
456 3 : Reference< awt::XFont > OAccessibleMenuItemComponent::getFont( ) throw (RuntimeException, std::exception)
457 : {
458 3 : OExternalLockGuard aGuard( this );
459 :
460 3 : Reference< awt::XFont > xFont;
461 6 : Reference< XAccessible > xParent = getAccessibleParent();
462 3 : if ( xParent.is() )
463 : {
464 3 : Reference< XAccessibleExtendedComponent > xParentComp( xParent->getAccessibleContext(), UNO_QUERY );
465 3 : if ( xParentComp.is() )
466 3 : xFont = xParentComp->getFont();
467 : }
468 :
469 6 : return xFont;
470 : }
471 :
472 :
473 :
474 2 : OUString OAccessibleMenuItemComponent::getTitledBorderText( ) throw (RuntimeException, std::exception)
475 : {
476 2 : OExternalLockGuard aGuard( this );
477 :
478 2 : return OUString();
479 : }
480 :
481 :
482 :
483 2 : OUString OAccessibleMenuItemComponent::getToolTipText( ) throw (RuntimeException, std::exception)
484 : {
485 2 : OExternalLockGuard aGuard( this );
486 :
487 2 : OUString sRet;
488 2 : if ( m_pParent )
489 2 : sRet = m_pParent->GetTipHelpText( m_pParent->GetItemId( m_nItemPos ) );
490 :
491 2 : return sRet;
492 : }
493 :
494 :
495 :
496 79 : sal_Bool OAccessibleMenuItemComponent::IsMenuHideDisabledEntries()
497 : {
498 79 : if (m_pParent )
499 : {
500 79 : if( m_pParent->GetMenuFlags() & MENU_FLAG_HIDEDISABLEDENTRIES)
501 : {
502 0 : return sal_True;
503 : }
504 : }
505 79 : return sal_False;
506 : }
507 :
508 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|