bump product version to 7.6.3.2-android
[LibreOffice.git] / vcl / source / window / accessibility.cxx
blobcd6e50eddfe57d32944fd6e18a7ef6032e9b8d3b
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 .
20 #include <vcl/layout.hxx>
21 #include <vcl/toolkit/fixed.hxx>
22 #include <vcl/window.hxx>
23 #include <vcl/menu.hxx>
24 #include <vcl/mnemonic.hxx>
25 #include <vcl/wrkwin.hxx>
27 #include <window.h>
28 #include <brdwin.hxx>
30 #include <com/sun/star/accessibility/XAccessible.hpp>
31 #include <com/sun/star/accessibility/AccessibleRole.hpp>
32 #include <com/sun/star/accessibility/AccessibleStateType.hpp>
33 #include <com/sun/star/accessibility/XAccessibleEditableText.hpp>
34 #include <com/sun/star/awt/XVclWindowPeer.hpp>
36 #include <sal/log.hxx>
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::lang;
40 using namespace ::com::sun::star::datatransfer::clipboard;
41 using namespace ::com::sun::star::datatransfer::dnd;
42 using namespace ::com::sun::star;
45 ImplAccessibleInfos::ImplAccessibleInfos()
47 nAccessibleRole = 0xFFFF;
48 pLabeledByWindow = nullptr;
49 pLabelForWindow = nullptr;
52 ImplAccessibleInfos::~ImplAccessibleInfos()
56 namespace vcl {
58 css::uno::Reference< css::accessibility::XAccessible > Window::GetAccessible( bool bCreate )
60 // do not optimize hierarchy for the top level border win (ie, when there is no parent)
61 /* // do not optimize accessible hierarchy at all to better reflect real VCL hierarchy
62 if ( GetParent() && ( GetType() == WindowType::BORDERWINDOW ) && ( GetChildCount() == 1 ) )
63 //if( !ImplIsAccessibleCandidate() )
65 vcl::Window* pChild = GetAccessibleChildWindow( 0 );
66 if ( pChild )
67 return pChild->GetAccessible();
70 if ( !mpWindowImpl )
71 return css::uno::Reference< css::accessibility::XAccessible >();
72 if ( !mpWindowImpl->mxAccessible.is() && bCreate )
73 mpWindowImpl->mxAccessible = CreateAccessible();
75 return mpWindowImpl->mxAccessible;
78 css::uno::Reference< css::accessibility::XAccessible > Window::CreateAccessible()
80 css::uno::Reference< css::accessibility::XAccessible > xAcc( GetComponentInterface(), css::uno::UNO_QUERY );
81 return xAcc;
84 void Window::SetAccessible( const css::uno::Reference< css::accessibility::XAccessible >& x )
86 if (!mpWindowImpl)
87 return;
89 mpWindowImpl->mxAccessible = x;
92 // skip all border windows that are not top level frames
93 bool Window::ImplIsAccessibleCandidate() const
95 if( !mpWindowImpl->mbBorderWin )
96 return true;
97 else
98 // #101741 do not check for WB_CLOSEABLE because undecorated floaters (like menus!) are closeable
99 if( mpWindowImpl->mbFrame && mpWindowImpl->mnStyle & (WB_MOVEABLE | WB_SIZEABLE) )
100 return true;
101 else
102 return false;
105 bool Window::ImplIsAccessibleNativeFrame() const
107 if( mpWindowImpl->mbFrame )
108 // #101741 do not check for WB_CLOSEABLE because undecorated floaters (like menus!) are closeable
109 if( mpWindowImpl->mnStyle & (WB_MOVEABLE | WB_SIZEABLE) )
110 return true;
111 else
112 return false;
113 else
114 return false;
117 vcl::Window* Window::GetAccessibleParentWindow() const
119 if (!mpWindowImpl || ImplIsAccessibleNativeFrame())
120 return nullptr;
122 vcl::Window* pParent = mpWindowImpl->mpParent;
123 if( GetType() == WindowType::MENUBARWINDOW )
125 // report the menubar as a child of THE workwindow
126 vcl::Window *pWorkWin = GetParent()->mpWindowImpl->mpFirstChild;
127 while( pWorkWin && (pWorkWin == this) )
128 pWorkWin = pWorkWin->mpWindowImpl->mpNext;
129 pParent = pWorkWin;
131 // If this is a floating window which has a native border window, then that border should be reported as
132 // the accessible parent
133 else if( GetType() == WindowType::FLOATINGWINDOW &&
134 mpWindowImpl->mpBorderWindow && mpWindowImpl->mpBorderWindow->mpWindowImpl->mbFrame )
136 pParent = mpWindowImpl->mpBorderWindow;
138 else if( pParent && !pParent->ImplIsAccessibleCandidate() )
140 pParent = pParent->mpWindowImpl->mpParent;
142 return pParent;
145 sal_uInt16 Window::GetAccessibleChildWindowCount()
147 if (!mpWindowImpl)
148 return 0;
150 sal_uInt16 nChildren = 0;
151 vcl::Window* pChild = mpWindowImpl->mpFirstChild;
152 while( pChild )
154 if( pChild->IsVisible() )
155 nChildren++;
156 pChild = pChild->mpWindowImpl->mpNext;
159 // report the menubarwindow as a child of THE workwindow
160 if( GetType() == WindowType::BORDERWINDOW )
162 ImplBorderWindow *pBorderWindow = static_cast<ImplBorderWindow*>(this);
163 if( pBorderWindow->mpMenuBarWindow &&
164 pBorderWindow->mpMenuBarWindow->IsVisible()
166 --nChildren;
168 else if( GetType() == WindowType::WORKWINDOW )
170 WorkWindow *pWorkWindow = static_cast<WorkWindow*>(this);
171 if( pWorkWindow->GetMenuBar() &&
172 pWorkWindow->GetMenuBar()->GetWindow() &&
173 pWorkWindow->GetMenuBar()->GetWindow()->IsVisible()
175 ++nChildren;
178 return nChildren;
181 vcl::Window* Window::GetAccessibleChildWindow( sal_uInt16 n )
183 // report the menubarwindow as the first child of THE workwindow
184 if( GetType() == WindowType::WORKWINDOW && static_cast<WorkWindow *>(this)->GetMenuBar() )
186 if( n == 0 )
188 MenuBar *pMenuBar = static_cast<WorkWindow *>(this)->GetMenuBar();
189 if( pMenuBar->GetWindow() && pMenuBar->GetWindow()->IsVisible() )
190 return pMenuBar->GetWindow();
192 else
193 --n;
196 // transform n to child number including invisible children
197 sal_uInt16 nChildren = n;
198 vcl::Window* pChild = mpWindowImpl->mpFirstChild;
199 while( pChild )
201 if( pChild->IsVisible() )
203 if( ! nChildren )
204 break;
205 nChildren--;
207 pChild = pChild->mpWindowImpl->mpNext;
210 if( GetType() == WindowType::BORDERWINDOW && pChild && pChild->GetType() == WindowType::MENUBARWINDOW )
212 do pChild = pChild->mpWindowImpl->mpNext; while( pChild && ! pChild->IsVisible() );
213 SAL_WARN_IF( !pChild, "vcl", "GetAccessibleChildWindow(): wrong index in border window");
216 if ( pChild && ( pChild->GetType() == WindowType::BORDERWINDOW ) && ( pChild->GetChildCount() == 1 ) )
218 pChild = pChild->GetChild( 0 );
220 return pChild;
223 void Window::SetAccessibleRole( sal_uInt16 nRole )
225 if ( !mpWindowImpl->mpAccessibleInfos )
226 mpWindowImpl->mpAccessibleInfos.reset( new ImplAccessibleInfos );
228 SAL_WARN_IF( mpWindowImpl->mpAccessibleInfos->nAccessibleRole != 0xFFFF, "vcl", "AccessibleRole already set!" );
229 mpWindowImpl->mpAccessibleInfos->nAccessibleRole = nRole;
232 sal_uInt16 Window::getDefaultAccessibleRole() const
234 sal_uInt16 nRole = 0xFFFF;
235 switch ( GetType() )
237 case WindowType::MESSBOX: // MT: Would be nice to have special roles!
238 case WindowType::INFOBOX:
239 case WindowType::WARNINGBOX:
240 case WindowType::ERRORBOX:
241 case WindowType::QUERYBOX: nRole = accessibility::AccessibleRole::ALERT; break;
243 case WindowType::MODELESSDIALOG:
244 case WindowType::TABDIALOG:
245 case WindowType::BUTTONDIALOG:
246 case WindowType::DIALOG: nRole = accessibility::AccessibleRole::DIALOG; break;
248 case WindowType::PUSHBUTTON:
249 case WindowType::OKBUTTON:
250 case WindowType::CANCELBUTTON:
251 case WindowType::HELPBUTTON:
252 case WindowType::IMAGEBUTTON:
253 case WindowType::MOREBUTTON: nRole = accessibility::AccessibleRole::PUSH_BUTTON; break;
254 case WindowType::MENUBUTTON: nRole = accessibility::AccessibleRole::BUTTON_MENU; break;
256 case WindowType::RADIOBUTTON: nRole = accessibility::AccessibleRole::RADIO_BUTTON; break;
257 case WindowType::TRISTATEBOX:
258 case WindowType::CHECKBOX: nRole = accessibility::AccessibleRole::CHECK_BOX; break;
260 case WindowType::MULTILINEEDIT: nRole = accessibility::AccessibleRole::SCROLL_PANE; break;
262 case WindowType::PATTERNFIELD:
263 case WindowType::EDIT: nRole = static_cast<Edit const *>(this)->IsPassword() ? accessibility::AccessibleRole::PASSWORD_TEXT : accessibility::AccessibleRole::TEXT; break;
265 case WindowType::PATTERNBOX:
266 case WindowType::NUMERICBOX:
267 case WindowType::METRICBOX:
268 case WindowType::CURRENCYBOX:
269 case WindowType::LONGCURRENCYBOX:
270 case WindowType::COMBOBOX: nRole = accessibility::AccessibleRole::COMBO_BOX; break;
272 case WindowType::LISTBOX:
273 case WindowType::MULTILISTBOX: nRole = accessibility::AccessibleRole::LIST; break;
275 case WindowType::TREELISTBOX: nRole = accessibility::AccessibleRole::TREE; break;
277 case WindowType::FIXEDTEXT: nRole = accessibility::AccessibleRole::LABEL; break;
278 case WindowType::FIXEDLINE:
279 if( !GetText().isEmpty() )
280 nRole = accessibility::AccessibleRole::LABEL;
281 else
282 nRole = accessibility::AccessibleRole::SEPARATOR;
283 break;
285 case WindowType::FIXEDBITMAP:
286 case WindowType::FIXEDIMAGE: nRole = accessibility::AccessibleRole::ICON; break;
287 case WindowType::GROUPBOX: nRole = accessibility::AccessibleRole::GROUP_BOX; break;
288 case WindowType::SCROLLBAR: nRole = accessibility::AccessibleRole::SCROLL_BAR; break;
290 case WindowType::SLIDER:
291 case WindowType::SPLITTER:
292 case WindowType::SPLITWINDOW: nRole = accessibility::AccessibleRole::SPLIT_PANE; break;
294 case WindowType::DATEBOX:
295 case WindowType::TIMEBOX:
296 case WindowType::DATEFIELD:
297 case WindowType::TIMEFIELD: nRole = accessibility::AccessibleRole::DATE_EDITOR; break;
299 case WindowType::METRICFIELD:
300 case WindowType::CURRENCYFIELD:
301 case WindowType::SPINBUTTON:
302 case WindowType::SPINFIELD:
303 case WindowType::FORMATTEDFIELD: nRole = accessibility::AccessibleRole::SPIN_BOX; break;
305 case WindowType::TOOLBOX: nRole = accessibility::AccessibleRole::TOOL_BAR; break;
306 case WindowType::STATUSBAR: nRole = accessibility::AccessibleRole::STATUS_BAR; break;
308 case WindowType::TABPAGE: nRole = accessibility::AccessibleRole::PANEL; break;
309 case WindowType::TABCONTROL: nRole = accessibility::AccessibleRole::PAGE_TAB_LIST; break;
311 case WindowType::DOCKINGWINDOW: nRole = (mpWindowImpl->mbFrame) ? accessibility::AccessibleRole::FRAME :
312 accessibility::AccessibleRole::PANEL; break;
314 case WindowType::FLOATINGWINDOW: nRole = ( mpWindowImpl->mbFrame ||
315 (mpWindowImpl->mpBorderWindow && mpWindowImpl->mpBorderWindow->mpWindowImpl->mbFrame) ||
316 (GetStyle() & WB_OWNERDRAWDECORATION) ) ? accessibility::AccessibleRole::FRAME :
317 accessibility::AccessibleRole::WINDOW; break;
319 case WindowType::WORKWINDOW: nRole = accessibility::AccessibleRole::ROOT_PANE; break;
321 case WindowType::SCROLLBARBOX: nRole = accessibility::AccessibleRole::FILLER; break;
323 case WindowType::HELPTEXTWINDOW: nRole = accessibility::AccessibleRole::TOOL_TIP; break;
325 case WindowType::RULER: nRole = accessibility::AccessibleRole::RULER; break;
327 case WindowType::SCROLLWINDOW: nRole = accessibility::AccessibleRole::SCROLL_PANE; break;
329 case WindowType::WINDOW:
330 case WindowType::CONTROL:
331 case WindowType::BORDERWINDOW:
332 case WindowType::SYSTEMCHILDWINDOW:
333 default:
334 if (ImplIsAccessibleNativeFrame() )
335 nRole = accessibility::AccessibleRole::FRAME;
336 else if( IsScrollable() )
337 nRole = accessibility::AccessibleRole::SCROLL_PANE;
338 else if( this->ImplGetWindow()->IsMenuFloatingWindow() )
339 nRole = accessibility::AccessibleRole::WINDOW; // #106002#, contextmenus are windows (i.e. toplevel)
340 else
341 // #104051# WINDOW seems to be a bad default role, use LAYEREDPANE instead
342 // a WINDOW is interpreted as a top-level window, which is typically not the case
343 //nRole = accessibility::AccessibleRole::WINDOW;
344 nRole = accessibility::AccessibleRole::PANEL;
346 return nRole;
349 sal_uInt16 Window::GetAccessibleRole() const
351 if (!mpWindowImpl)
352 return 0;
354 sal_uInt16 nRole = mpWindowImpl->mpAccessibleInfos ? mpWindowImpl->mpAccessibleInfos->nAccessibleRole : 0xFFFF;
355 if ( nRole == 0xFFFF )
356 nRole = getDefaultAccessibleRole();
357 return nRole;
360 void Window::SetAccessibleName( const OUString& rName )
362 if ( !mpWindowImpl->mpAccessibleInfos )
363 mpWindowImpl->mpAccessibleInfos.reset( new ImplAccessibleInfos );
365 OUString oldName = GetAccessibleName();
367 mpWindowImpl->mpAccessibleInfos->pAccessibleName = rName;
369 CallEventListeners( VclEventId::WindowFrameTitleChanged, &oldName );
372 OUString Window::GetAccessibleName() const
374 if (!mpWindowImpl)
375 return OUString();
377 if (mpWindowImpl->mpAccessibleInfos && mpWindowImpl->mpAccessibleInfos->pAccessibleName)
378 return *mpWindowImpl->mpAccessibleInfos->pAccessibleName;
379 return getDefaultAccessibleName();
382 OUString Window::getDefaultAccessibleName() const
384 OUString aAccessibleName;
385 switch ( GetType() )
387 case WindowType::MULTILINEEDIT:
388 case WindowType::PATTERNFIELD:
389 case WindowType::METRICFIELD:
390 case WindowType::CURRENCYFIELD:
391 case WindowType::EDIT:
393 case WindowType::DATEBOX:
394 case WindowType::TIMEBOX:
395 case WindowType::CURRENCYBOX:
396 case WindowType::LONGCURRENCYBOX:
397 case WindowType::DATEFIELD:
398 case WindowType::TIMEFIELD:
399 case WindowType::SPINFIELD:
400 case WindowType::FORMATTEDFIELD:
402 case WindowType::COMBOBOX:
403 case WindowType::LISTBOX:
404 case WindowType::MULTILISTBOX:
405 case WindowType::TREELISTBOX:
406 case WindowType::METRICBOX:
408 vcl::Window *pLabel = GetAccessibleRelationLabeledBy();
409 if ( pLabel && pLabel != this )
410 aAccessibleName = pLabel->GetText();
411 if (aAccessibleName.isEmpty())
412 aAccessibleName = GetQuickHelpText();
413 if (aAccessibleName.isEmpty())
414 aAccessibleName = GetText();
416 break;
418 case WindowType::IMAGEBUTTON:
419 case WindowType::PUSHBUTTON:
420 aAccessibleName = GetText();
421 if (aAccessibleName.isEmpty())
423 aAccessibleName = GetQuickHelpText();
424 if (aAccessibleName.isEmpty())
425 aAccessibleName = GetHelpText();
427 break;
429 case WindowType::TOOLBOX:
430 aAccessibleName = GetText();
431 break;
433 case WindowType::MOREBUTTON:
434 aAccessibleName = mpWindowImpl->maText;
435 break;
437 default:
438 aAccessibleName = GetText();
439 break;
442 return removeMnemonicFromString( aAccessibleName );
445 void Window::SetAccessibleDescription( const OUString& rDescription )
447 if ( ! mpWindowImpl->mpAccessibleInfos )
448 mpWindowImpl->mpAccessibleInfos.reset( new ImplAccessibleInfos );
450 std::optional<OUString>& rCurrentDescription = mpWindowImpl->mpAccessibleInfos->pAccessibleDescription;
451 SAL_WARN_IF( rCurrentDescription && *rCurrentDescription != rDescription, "vcl", "AccessibleDescription already set" );
452 rCurrentDescription = rDescription;
455 OUString Window::GetAccessibleDescription() const
457 if (!mpWindowImpl)
458 return OUString();
460 OUString aAccessibleDescription;
461 if ( mpWindowImpl->mpAccessibleInfos && mpWindowImpl->mpAccessibleInfos->pAccessibleDescription )
463 aAccessibleDescription = *mpWindowImpl->mpAccessibleInfos->pAccessibleDescription;
465 else
467 // Special code for help text windows. ZT asks the border window for the
468 // description so we have to forward this request to our inner window.
469 const vcl::Window* pWin = this->ImplGetWindow();
470 if ( pWin->GetType() == WindowType::HELPTEXTWINDOW )
471 aAccessibleDescription = pWin->GetHelpText();
472 else
473 aAccessibleDescription = GetHelpText();
476 return aAccessibleDescription;
479 void Window::SetAccessibleRelationLabeledBy( vcl::Window* pLabeledBy )
481 if ( !mpWindowImpl->mpAccessibleInfos )
482 mpWindowImpl->mpAccessibleInfos.reset( new ImplAccessibleInfos );
483 mpWindowImpl->mpAccessibleInfos->pLabeledByWindow = pLabeledBy;
486 void Window::SetAccessibleRelationLabelFor( vcl::Window* pLabelFor )
488 if ( !mpWindowImpl->mpAccessibleInfos )
489 mpWindowImpl->mpAccessibleInfos.reset( new ImplAccessibleInfos );
490 mpWindowImpl->mpAccessibleInfos->pLabelForWindow = pLabelFor;
493 vcl::Window* Window::GetAccessibleRelationMemberOf() const
495 if (!isContainerWindow(this) && !isContainerWindow(GetParent()))
496 return getLegacyNonLayoutAccessibleRelationMemberOf();
498 return nullptr;
501 vcl::Window* Window::getAccessibleRelationLabelFor() const
503 if (mpWindowImpl->mpAccessibleInfos && mpWindowImpl->mpAccessibleInfos->pLabelForWindow)
504 return mpWindowImpl->mpAccessibleInfos->pLabelForWindow;
506 return nullptr;
509 vcl::Window* Window::GetAccessibleRelationLabelFor() const
511 vcl::Window* pWindow = getAccessibleRelationLabelFor();
513 if (pWindow)
514 return pWindow;
516 if (!isContainerWindow(this) && !isContainerWindow(GetParent()))
517 return getLegacyNonLayoutAccessibleRelationLabelFor();
519 return nullptr;
522 vcl::Window* Window::GetAccessibleRelationLabeledBy() const
524 if (mpWindowImpl->mpAccessibleInfos && mpWindowImpl->mpAccessibleInfos->pLabeledByWindow)
525 return mpWindowImpl->mpAccessibleInfos->pLabeledByWindow;
527 std::vector<VclPtr<FixedText> > aMnemonicLabels(list_mnemonic_labels());
528 if (!aMnemonicLabels.empty())
530 //if we have multiple labels, then prefer the first that is visible
531 for (auto const & rCandidate : aMnemonicLabels)
533 if (rCandidate->IsVisible())
534 return rCandidate;
536 return aMnemonicLabels[0];
539 if (!isContainerWindow(this) && !isContainerWindow(GetParent()))
540 return getLegacyNonLayoutAccessibleRelationLabeledBy();
542 return nullptr;
545 bool Window::IsAccessibilityEventsSuppressed( bool bTraverseParentPath )
547 if( !bTraverseParentPath )
548 return mpWindowImpl->mbSuppressAccessibilityEvents;
549 else
551 vcl::Window *pParent = this;
552 while ( pParent && pParent->mpWindowImpl)
554 if( pParent->mpWindowImpl->mbSuppressAccessibilityEvents )
555 return true;
556 else
557 pParent = pParent->mpWindowImpl->mpParent; // do not use GetParent() to find borderwindows that are frames
559 return false;
563 void Window::SetAccessibilityEventsSuppressed(bool bSuppressed)
565 mpWindowImpl->mbSuppressAccessibilityEvents = bSuppressed;
568 } /* namespace vcl */
570 uno::Reference<accessibility::XAccessibleEditableText>
571 FindFocusedEditableText(uno::Reference<accessibility::XAccessibleContext> const& xContext)
573 if (!xContext.is())
574 return uno::Reference<accessibility::XAccessibleEditableText>();
576 sal_Int64 nState = xContext->getAccessibleStateSet();
577 if (nState & accessibility::AccessibleStateType::FOCUSED)
579 uno::Reference<accessibility::XAccessibleEditableText> xText(xContext, uno::UNO_QUERY);
580 if (xText.is())
581 return xText;
582 if (nState & accessibility::AccessibleStateType::MANAGES_DESCENDANTS)
583 return uno::Reference<accessibility::XAccessibleEditableText>();
586 bool bSafeToIterate = true;
587 sal_Int64 nCount = xContext->getAccessibleChildCount();
588 if (nCount < 0 || nCount > SAL_MAX_UINT16 /* slow enough for anyone */)
589 bSafeToIterate = false;
590 if (!bSafeToIterate)
591 return uno::Reference<accessibility::XAccessibleEditableText>();
593 for (sal_Int64 i = 0; i < xContext->getAccessibleChildCount(); ++i)
595 uno::Reference<accessibility::XAccessible> xChild = xContext->getAccessibleChild(i);
596 if (!xChild.is())
597 continue;
598 uno::Reference<accessibility::XAccessibleContext> xChildContext
599 = xChild->getAccessibleContext();
600 if (!xChildContext.is())
601 continue;
602 uno::Reference<accessibility::XAccessibleEditableText> xText
603 = FindFocusedEditableText(xChildContext);
604 if (xText.is())
605 return xText;
607 return uno::Reference<accessibility::XAccessibleEditableText>();
610 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */