1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
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.
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).
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.
27 ************************************************************************/
29 #include "AccessibleFrameSelector.hxx"
30 #include <com/sun/star/awt/KeyEvent.hpp>
31 #include <com/sun/star/awt/KeyModifier.hpp>
32 #include <com/sun/star/awt/Key.hpp>
33 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
34 #include <com/sun/star/accessibility/AccessibleRelationType.hpp>
35 #include <com/sun/star/accessibility/AccessibleRole.hpp>
36 #include <com/sun/star/accessibility/AccessibleEventId.hpp>
37 #include <com/sun/star/awt/FocusChangeReason.hpp>
38 #include <unotools/accessiblestatesethelper.hxx>
39 #include <unotools/accessiblerelationsethelper.hxx>
40 #include <osl/mutex.hxx>
41 #include <vcl/svapp.hxx>
42 #include <svx/frmsel.hxx>
43 #include <svx/dialmgr.hxx>
44 #include "editeng/unolingu.hxx"
46 #include <svx/dialogs.hrc>
50 #define MNEMONIC_CHAR ((sal_Unicode)'~')
56 using ::rtl::OUString
;
57 using ::com::sun::star::uno::Any
;
58 using ::com::sun::star::uno::UNO_QUERY
;
59 using ::com::sun::star::uno::Reference
;
60 using ::com::sun::star::uno::Sequence
;
61 using ::com::sun::star::uno::RuntimeException
;
62 using ::com::sun::star::uno::XInterface
;
63 using ::com::sun::star::lang::Locale
;
64 using ::com::sun::star::lang::EventObject
;
65 using ::com::sun::star::beans::XPropertyChangeListener
;
66 using ::com::sun::star::awt::XFocusListener
;
68 using namespace ::com::sun::star::accessibility
;
70 namespace AwtKey
= ::com::sun::star::awt::Key
;
71 namespace AwtKeyModifier
= ::com::sun::star::awt::KeyModifier
;
72 namespace AwtFocusChangeReason
= ::com::sun::star::awt::FocusChangeReason
;
74 typedef ::com::sun::star::awt::Point AwtPoint
;
75 typedef ::com::sun::star::awt::Size AwtSize
;
76 typedef ::com::sun::star::awt::Rectangle AwtRectangle
;
77 typedef ::com::sun::star::awt::KeyEvent AwtKeyEvent
;
78 typedef ::com::sun::star::awt::FocusEvent AwtFocusEvent
;
80 // ============================================================================
82 AccFrameSelector::AccFrameSelector( FrameSelector
& rFrameSel
, FrameBorderType eBorder
) :
83 Resource( SVX_RES( RID_SVXSTR_BORDER_CONTROL
) ),
84 mpFrameSel( &rFrameSel
),
86 maFocusListeners( maFocusMutex
),
87 maPropertyListeners( maPropertyMutex
),
88 maNames( SVX_RES( ARR_TEXTS
) ),
89 maDescriptions( SVX_RES(ARR_DESCRIPTIONS
) ),
96 mpFrameSel
->AddEventListener( LINK( this, AccFrameSelector
, WindowEventListener
) );
100 // ----------------------------------------------------------------------------
102 AccFrameSelector::~AccFrameSelector()
106 mpFrameSel
->RemoveEventListener( LINK( this, AccFrameSelector
, WindowEventListener
) );
110 // ----------------------------------------------------------------------------
112 Reference
< XAccessibleContext
> AccFrameSelector::getAccessibleContext( )
113 throw (RuntimeException
)
118 // ----------------------------------------------------------------------------
120 sal_Int32
AccFrameSelector::getAccessibleChildCount( ) throw (RuntimeException
)
122 SolarMutexGuard aGuard
;
124 return (meBorder
== FRAMEBORDER_NONE
) ? mpFrameSel
->GetEnabledBorderCount() : 0;
127 // ----------------------------------------------------------------------------
129 Reference
< XAccessible
> AccFrameSelector::getAccessibleChild( sal_Int32 i
)
130 throw (RuntimeException
)
132 SolarMutexGuard aGuard
;
134 Reference
< XAccessible
> xRet
;
135 if( meBorder
== FRAMEBORDER_NONE
)
136 xRet
= mpFrameSel
->GetChildAccessible( i
);
138 throw RuntimeException();
142 // ----------------------------------------------------------------------------
144 Reference
< XAccessible
> AccFrameSelector::getAccessibleParent( )
145 throw (RuntimeException
)
147 SolarMutexGuard aGuard
;
149 Reference
< XAccessible
> xRet
;
150 if(meBorder
== FRAMEBORDER_NONE
)
151 xRet
= mpFrameSel
->GetParent()->GetAccessible( sal_True
);
153 xRet
= mpFrameSel
->CreateAccessible();
157 // ----------------------------------------------------------------------------
159 sal_Int32
AccFrameSelector::getAccessibleIndexInParent( )
160 throw (RuntimeException
)
162 SolarMutexGuard aGuard
;
166 if( meBorder
== FRAMEBORDER_NONE
)
168 Window
* pTabPage
= mpFrameSel
->GetParent();
169 sal_Int32 nChildren
= pTabPage
->GetChildCount();
170 for( nIdx
= 0; nIdx
< nChildren
; ++nIdx
)
171 if( pTabPage
->GetChild( static_cast< sal_uInt16
>( nIdx
) ) == mpFrameSel
)
175 nIdx
= mpFrameSel
->GetEnabledBorderIndex( meBorder
);
178 throw RuntimeException();
182 // ----------------------------------------------------------------------------
184 sal_Int16
AccFrameSelector::getAccessibleRole( ) throw (RuntimeException
)
186 return AccessibleRole::OPTION_PANE
;
189 // ----------------------------------------------------------------------------
191 OUString
AccFrameSelector::getAccessibleDescription( )
192 throw (RuntimeException
)
194 SolarMutexGuard aGuard
;
196 return maDescriptions
.GetString(meBorder
);
199 // ----------------------------------------------------------------------------
201 OUString
AccFrameSelector::getAccessibleName( )
202 throw (RuntimeException
)
204 SolarMutexGuard aGuard
;
206 return maNames
.GetString(meBorder
);
209 // ----------------------------------------------------------------------------
211 Reference
< XAccessibleRelationSet
> AccFrameSelector::getAccessibleRelationSet( )
212 throw (RuntimeException
)
214 SolarMutexGuard aGuard
;
216 utl::AccessibleRelationSetHelper
* pHelper
;
217 Reference
< XAccessibleRelationSet
> xRet
= pHelper
= new utl::AccessibleRelationSetHelper
;
218 if(meBorder
== FRAMEBORDER_NONE
)
220 //add the label relation
221 Window
* pPrev
= mpFrameSel
->GetWindow( WINDOW_PREV
);
222 if(pPrev
&& WINDOW_FIXEDTEXT
== pPrev
->GetType())
224 AccessibleRelation aLabelRelation
;
225 aLabelRelation
.RelationType
= AccessibleRelationType::LABELED_BY
;
226 aLabelRelation
.TargetSet
.realloc(1);
227 aLabelRelation
.TargetSet
.getArray()[0] = pPrev
->GetAccessible();
228 pHelper
->AddRelation(aLabelRelation
);
234 // ----------------------------------------------------------------------------
236 Reference
< XAccessibleStateSet
> AccFrameSelector::getAccessibleStateSet( )
237 throw (RuntimeException
)
239 SolarMutexGuard aGuard
;
240 utl::AccessibleStateSetHelper
* pStateSetHelper
= new utl::AccessibleStateSetHelper
;
241 Reference
< XAccessibleStateSet
> xRet
= pStateSetHelper
;
244 pStateSetHelper
->AddState(AccessibleStateType::DEFUNC
);
247 const sal_Int16 aStandardStates
[] =
249 AccessibleStateType::EDITABLE
,
250 AccessibleStateType::FOCUSABLE
,
251 AccessibleStateType::MULTI_SELECTABLE
,
252 AccessibleStateType::SELECTABLE
,
253 AccessibleStateType::SHOWING
,
254 AccessibleStateType::VISIBLE
,
255 AccessibleStateType::OPAQUE
,
257 sal_Int16 nState
= 0;
258 while(aStandardStates
[nState
])
260 pStateSetHelper
->AddState(aStandardStates
[nState
++]);
262 if(mpFrameSel
->IsEnabled())
264 pStateSetHelper
->AddState(AccessibleStateType::ENABLED
);
265 pStateSetHelper
->AddState(AccessibleStateType::SENSITIVE
);
268 sal_Bool bIsParent
= meBorder
== FRAMEBORDER_NONE
;
269 if(mpFrameSel
->HasFocus() &&
270 (bIsParent
|| mpFrameSel
->IsBorderSelected(meBorder
)))
272 pStateSetHelper
->AddState(AccessibleStateType::ACTIVE
);
273 pStateSetHelper
->AddState(AccessibleStateType::FOCUSED
);
274 pStateSetHelper
->AddState(AccessibleStateType::SELECTED
);
280 // ----------------------------------------------------------------------------
282 Locale
AccFrameSelector::getLocale( )
283 throw (IllegalAccessibleComponentStateException
, RuntimeException
)
286 SvxLanguageToLocale( aRet
, Application::GetSettings().GetUILanguage() );
290 // ----------------------------------------------------------------------------
292 void AccFrameSelector::addPropertyChangeListener(
293 const Reference
< XPropertyChangeListener
>& xListener
)
294 throw (RuntimeException
)
296 maPropertyListeners
.addInterface( xListener
);
299 // ----------------------------------------------------------------------------
301 void AccFrameSelector::removePropertyChangeListener( const Reference
< XPropertyChangeListener
>& xListener
)
302 throw (RuntimeException
)
304 maPropertyListeners
.removeInterface( xListener
);
307 // ----------------------------------------------------------------------------
309 sal_Bool
AccFrameSelector::containsPoint( const AwtPoint
& aPt
)
310 throw (RuntimeException
)
312 SolarMutexGuard aGuard
;
314 //aPt is relative to the frame selector
315 return mpFrameSel
->ContainsClickPoint( Point( aPt
.X
, aPt
.Y
) );
318 // ----------------------------------------------------------------------------
320 Reference
< XAccessible
> AccFrameSelector::getAccessibleAtPoint(
321 const AwtPoint
& aPt
)
322 throw (RuntimeException
)
324 SolarMutexGuard aGuard
;
326 //aPt is relative to the frame selector
327 return mpFrameSel
->GetChildAccessible( Point( aPt
.X
, aPt
.Y
) );
330 AwtRectangle
AccFrameSelector::getBounds( ) throw (RuntimeException
)
332 SolarMutexGuard aGuard
;
338 case FRAMEBORDER_NONE
:
339 aSz
= mpFrameSel
->GetSizePixel();
340 aPos
= mpFrameSel
->GetPosPixel();
343 const Rectangle aSpot
= mpFrameSel
->GetClickBoundRect( meBorder
);
344 aPos
= aSpot
.TopLeft();
345 aSz
= aSpot
.GetSize();
350 aRet
.Width
= aSz
.Width();
351 aRet
.Height
= aSz
.Height();
355 // ----------------------------------------------------------------------------
357 AwtPoint
AccFrameSelector::getLocation( ) throw (RuntimeException
)
359 SolarMutexGuard aGuard
;
364 case FRAMEBORDER_NONE
:
365 aPos
= mpFrameSel
->GetPosPixel();
368 const Rectangle aSpot
= mpFrameSel
->GetClickBoundRect( meBorder
);
369 aPos
= aSpot
.TopLeft();
371 AwtPoint
aRet(aPos
.X(), aPos
.Y());
375 // ----------------------------------------------------------------------------
377 AwtPoint
AccFrameSelector::getLocationOnScreen( ) throw (RuntimeException
)
379 SolarMutexGuard aGuard
;
384 case FRAMEBORDER_NONE
:
385 aPos
= mpFrameSel
->GetPosPixel();
388 const Rectangle aSpot
= mpFrameSel
->GetClickBoundRect( meBorder
);
389 aPos
= aSpot
.TopLeft();
391 aPos
= mpFrameSel
->OutputToAbsoluteScreenPixel( aPos
);
392 AwtPoint
aRet(aPos
.X(), aPos
.Y());
396 // ----------------------------------------------------------------------------
398 AwtSize
AccFrameSelector::getSize( ) throw (RuntimeException
)
400 SolarMutexGuard aGuard
;
405 case FRAMEBORDER_NONE
:
406 aSz
= mpFrameSel
->GetSizePixel();
409 const Rectangle aSpot
= mpFrameSel
->GetClickBoundRect( meBorder
);
410 aSz
= aSpot
.GetSize();
412 AwtSize
aRet(aSz
.Width(), aSz
.Height());
416 // ----------------------------------------------------------------------------
418 sal_Bool
AccFrameSelector::isShowing( ) throw (RuntimeException
)
420 SolarMutexGuard aGuard
;
425 // ----------------------------------------------------------------------------
427 sal_Bool
AccFrameSelector::isVisible( ) throw (RuntimeException
)
429 SolarMutexGuard aGuard
;
434 // ----------------------------------------------------------------------------
436 sal_Bool
AccFrameSelector::isFocusTraversable( ) throw (RuntimeException
)
438 SolarMutexGuard aGuard
;
443 // ----------------------------------------------------------------------------
445 void AccFrameSelector::addFocusListener( const Reference
< XFocusListener
>& xListener
) throw (RuntimeException
)
447 maFocusListeners
.addInterface( xListener
);
450 // ----------------------------------------------------------------------------
452 void AccFrameSelector::removeFocusListener( const Reference
< XFocusListener
>& xListener
) throw (RuntimeException
)
454 maFocusListeners
.removeInterface( xListener
);
457 // ----------------------------------------------------------------------------
459 void AccFrameSelector::grabFocus( ) throw (RuntimeException
)
461 SolarMutexGuard aGuard
;
463 mpFrameSel
->GrabFocus();
466 // ----------------------------------------------------------------------------
468 Any
AccFrameSelector::getAccessibleKeyBinding( ) throw (RuntimeException
)
471 SolarMutexGuard aGuard
;
473 utl::AccessibleRelationSetHelper
* pHelper
;
474 Reference
< XAccessibleRelationSet
> xRet
= pHelper
= new utl::AccessibleRelationSetHelper
;
475 if(meBorder
== FRAMEBORDER_NONE
)
477 Window
* pPrev
= mpFrameSel
->GetWindow( WINDOW_PREV
);
478 if(pPrev
&& WINDOW_FIXEDTEXT
== pPrev
->GetType())
480 String sText
= pPrev
->GetText();
481 xub_StrLen nFound
= sText
.Search( MNEMONIC_CHAR
);
482 if(STRING_NOTFOUND
!= nFound
&& ++nFound
< sText
.Len())
484 sText
.ToUpperAscii();
485 sal_Unicode cChar
= sText
.GetChar(nFound
);
489 aEvent
.KeyChar
= cChar
;
491 if(cChar
>= 'A' && cChar
<= 'Z')
493 aEvent
.KeyCode
= AwtKey::A
+ cChar
- 'A';
495 aEvent
.Modifiers
= AwtKeyModifier::MOD2
;
503 // ----------------------------------------------------------------------------
505 sal_Int32
AccFrameSelector::getForeground( )
506 throw (RuntimeException
)
509 SolarMutexGuard aGuard
;
511 return mpFrameSel
->GetControlForeground().GetColor();
514 // ----------------------------------------------------------------------------
516 sal_Int32
AccFrameSelector::getBackground( )
517 throw (RuntimeException
)
520 SolarMutexGuard aGuard
;
522 return mpFrameSel
->GetControlBackground().GetColor();
525 // ----------------------------------------------------------------------------
527 void AccFrameSelector::addEventListener( const Reference
< XAccessibleEventListener
>& xListener
) throw (RuntimeException
)
529 SolarMutexGuard aGuard
;
531 if ( xListener
.is() )
535 mnClientId
= ::comphelper::AccessibleEventNotifier::registerClient();
537 ::comphelper::AccessibleEventNotifier::addEventListener( mnClientId
, xListener
);
541 // ----------------------------------------------------------------------------
543 void AccFrameSelector::removeEventListener( const Reference
< XAccessibleEventListener
>& xListener
) throw (RuntimeException
)
545 SolarMutexGuard aGuard
;
547 if ( xListener
.is() && mnClientId
!= 0 &&
548 ::comphelper::AccessibleEventNotifier::removeEventListener( mnClientId
, xListener
) == 0 )
550 // no listeners anymore
551 // -> revoke ourself. This may lead to the notifier thread dying (if we were the last client),
552 // and at least to us not firing any events anymore, in case somebody calls
553 // NotifyAccessibleEvent, again
554 ::comphelper::AccessibleEventNotifier::TClientId
nId( mnClientId
);
556 ::comphelper::AccessibleEventNotifier::revokeClient( nId
);
560 // ----------------------------------------------------------------------------
562 OUString
AccFrameSelector::getImplementationName( ) throw (RuntimeException
)
564 return OUString(RTL_CONSTASCII_USTRINGPARAM("AccFrameSelector"));
567 // ----------------------------------------------------------------------------
569 const sal_Char sAccessible
[] = "Accessible";
570 const sal_Char sAccessibleContext
[] = "AccessibleContext";
571 const sal_Char sAccessibleComponent
[] = "AccessibleComponent";
573 sal_Bool
AccFrameSelector::supportsService( const OUString
& rServiceName
)
574 throw (RuntimeException
)
576 return rServiceName
.equalsAsciiL( sAccessible
, sizeof(sAccessible
)-1 ) ||
577 rServiceName
.equalsAsciiL( sAccessibleContext
, sizeof(sAccessibleContext
)-1 ) ||
578 rServiceName
.equalsAsciiL( sAccessibleComponent
, sizeof(sAccessibleComponent
)-1 );
581 // ----------------------------------------------------------------------------
583 Sequence
< OUString
> AccFrameSelector::getSupportedServiceNames( )
584 throw (RuntimeException
)
586 Sequence
< OUString
> aRet(3);
587 OUString
* pArray
= aRet
.getArray();
588 pArray
[0] = OUString( RTL_CONSTASCII_USTRINGPARAM(sAccessible
) );
589 pArray
[1] = OUString( RTL_CONSTASCII_USTRINGPARAM(sAccessibleContext
) );
590 pArray
[2] = OUString( RTL_CONSTASCII_USTRINGPARAM(sAccessibleComponent
) );
594 // ----------------------------------------------------------------------------
596 void AccFrameSelector::IsValid() throw (RuntimeException
)
599 throw RuntimeException();
602 // ----------------------------------------------------------------------------
604 void AccFrameSelector::NotifyFocusListeners(sal_Bool bGetFocus
)
606 SolarMutexGuard aGuard
;
607 AwtFocusEvent aEvent
;
608 aEvent
.FocusFlags
= 0;
611 sal_uInt16 nFocusFlags
= mpFrameSel
->GetGetFocusFlags();
612 if(nFocusFlags
&GETFOCUS_TAB
)
613 aEvent
.FocusFlags
|= AwtFocusChangeReason::TAB
;
614 if(nFocusFlags
&GETFOCUS_CURSOR
)
615 aEvent
.FocusFlags
|= AwtFocusChangeReason::CURSOR
;
616 if(nFocusFlags
&GETFOCUS_MNEMONIC
)
617 aEvent
.FocusFlags
|= AwtFocusChangeReason::MNEMONIC
;
618 if(nFocusFlags
&GETFOCUS_FORWARD
)
619 aEvent
.FocusFlags
|= AwtFocusChangeReason::FORWARD
;
620 if(nFocusFlags
&GETFOCUS_BACKWARD
)
621 aEvent
.FocusFlags
|= AwtFocusChangeReason::BACKWARD
;
622 if(nFocusFlags
&GETFOCUS_AROUND
)
623 aEvent
.FocusFlags
|= AwtFocusChangeReason::AROUND
;
624 if(nFocusFlags
&GETFOCUS_UNIQUEMNEMONIC
)
625 aEvent
.FocusFlags
|= AwtFocusChangeReason::UNIQUEMNEMONIC
;
627 aEvent
.Temporary
= sal_False
;
629 Reference
< XAccessibleContext
> xThis( this );
630 aEvent
.Source
= xThis
;
632 ::cppu::OInterfaceIteratorHelper
aIter( maFocusListeners
);
633 while( aIter
.hasMoreElements() )
635 Reference
< XFocusListener
> xListener( aIter
.next(), UNO_QUERY
);
637 xListener
->focusGained( aEvent
);
639 xListener
->focusLost( aEvent
);
643 // ----------------------------------------------------------------------------
645 IMPL_LINK( AccFrameSelector
, WindowEventListener
, VclSimpleEvent
*, pEvent
)
647 VclWindowEvent
* pWinEvent
= dynamic_cast< VclWindowEvent
* >( pEvent
);
648 DBG_ASSERT( pWinEvent
, "AccFrameSelector::WindowEventListener - unknown window event" );
651 Window
* pWindow
= pWinEvent
->GetWindow();
652 DBG_ASSERT( pWindow
, "AccFrameSelector::WindowEventListener: no window!" );
653 if ( !pWindow
->IsAccessibilityEventsSuppressed() || ( pWinEvent
->GetId() == VCLEVENT_OBJECT_DYING
) )
655 ProcessWindowEvent( *pWinEvent
);
662 // ----------------------------------------------------------------------------
664 void AccFrameSelector::ProcessWindowEvent( const VclWindowEvent
& rVclWindowEvent
)
666 switch ( rVclWindowEvent
.GetId() )
668 case VCLEVENT_WINDOW_GETFOCUS
:
670 if ( meBorder
== FRAMEBORDER_NONE
)
672 Any aOldValue
, aNewValue
;
673 aNewValue
<<= AccessibleStateType::FOCUSED
;
674 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED
, aOldValue
, aNewValue
);
678 case VCLEVENT_WINDOW_LOSEFOCUS
:
680 if ( meBorder
== FRAMEBORDER_NONE
)
682 Any aOldValue
, aNewValue
;
683 aOldValue
<<= AccessibleStateType::FOCUSED
;
684 NotifyAccessibleEvent( AccessibleEventId::STATE_CHANGED
, aOldValue
, aNewValue
);
695 // ----------------------------------------------------------------------------
697 void AccFrameSelector::NotifyAccessibleEvent( const sal_Int16 _nEventId
,
698 const Any
& _rOldValue
, const Any
& _rNewValue
)
702 Reference
< XInterface
> xSource( *this );
703 AccessibleEventObject
aEvent( xSource
, _nEventId
, _rNewValue
, _rOldValue
);
704 ::comphelper::AccessibleEventNotifier::addEvent( mnClientId
, aEvent
);
708 // ----------------------------------------------------------------------------
710 void AccFrameSelector::Invalidate()
714 Reference
< XAccessibleContext
> xThis( this );
715 aEvent
.Source
= xThis
;
716 maFocusListeners
.disposeAndClear( aEvent
);
717 maPropertyListeners
.disposeAndClear( aEvent
);
720 // ============================================================================
725 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */