desktop/source/lib/init.cxx    |   87 +++++++++++++++++++++--------------------
 include/sfx2/viewsh.hxx        |    2 
 sfx2/source/view/lokhelper.cxx |    3 -
 sfx2/source/view/viewsh.cxx    |   34 +++++++++-------
 4 files changed, 69 insertions(+), 57 deletions(-)

New commits:
commit fb1d24cbc66d37ac5f8217abe072a44a072af420
Author:     Marco Cecchetti <[email protected]>
AuthorDate: Sun May 7 11:52:14 2023 +0200
Commit:     Marco Cecchetti <[email protected]>
CommitDate: Sun May 7 20:31:01 2023 +0200

    fixup! lok: accessibility event listener for focused paragraph
    
    It seems it was not a good idea using a unique_ptr as smart pointer
    for an instance of LOKDocumentFocusListener
    
    Change-Id: I8e6b0f48fee3c5db3c9b074a663f7f3fb96a601e
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/151459
    Tested-by: Jenkins CollaboraOffice <[email protected]>
    Reviewed-by: Marco Cecchetti <[email protected]>

diff --git a/desktop/source/lib/init.cxx b/desktop/source/lib/init.cxx
index 62d8a93733e4..2d1ad0e5a89c 100644
--- a/desktop/source/lib/init.cxx
+++ b/desktop/source/lib/init.cxx
@@ -1368,6 +1368,45 @@ vcl::Font FindFont_FallbackToDefault(std::u16string_view 
rFontName)
     return OutputDevice::GetDefaultFont(DefaultFontType::SANS_UNICODE, 
LANGUAGE_NONE,
                                         GetDefaultFontFlags::NONE);
 }
+
+static int getDocumentType (LibreOfficeKitDocument* pThis)
+{
+    SetLastExceptionMsg();
+
+    LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
+
+    try
+    {
+        uno::Reference<lang::XServiceInfo> xDocument(pDocument->mxComponent, 
uno::UNO_QUERY_THROW);
+
+        if 
(xDocument->supportsService("com.sun.star.sheet.SpreadsheetDocument"))
+        {
+            return LOK_DOCTYPE_SPREADSHEET;
+        }
+        else if 
(xDocument->supportsService("com.sun.star.presentation.PresentationDocument"))
+        {
+            return LOK_DOCTYPE_PRESENTATION;
+        }
+        else if 
(xDocument->supportsService("com.sun.star.drawing.DrawingDocument"))
+        {
+            return LOK_DOCTYPE_DRAWING;
+        }
+        else if (xDocument->supportsService("com.sun.star.text.TextDocument") 
|| xDocument->supportsService("com.sun.star.text.WebDocument"))
+        {
+            return LOK_DOCTYPE_TEXT;
+        }
+        else
+        {
+            SetLastExceptionMsg("unknown document type");
+        }
+    }
+    catch (const uno::Exception& exception)
+    {
+        SetLastExceptionMsg("exception: " + exception.Message);
+    }
+    return LOK_DOCTYPE_OTHER;
+}
+
 } // anonymous namespace
 
 LibLODocument_Impl::LibLODocument_Impl(uno::Reference <css::lang::XComponent> 
xComponent, int nDocumentId)
@@ -3634,40 +3673,7 @@ static int doc_getDocumentType (LibreOfficeKitDocument* 
pThis)
     comphelper::ProfileZone aZone("doc_getDocumentType");
 
     SolarMutexGuard aGuard;
-    SetLastExceptionMsg();
-
-    LibLODocument_Impl* pDocument = static_cast<LibLODocument_Impl*>(pThis);
-
-    try
-    {
-        uno::Reference<lang::XServiceInfo> xDocument(pDocument->mxComponent, 
uno::UNO_QUERY_THROW);
-
-        if 
(xDocument->supportsService("com.sun.star.sheet.SpreadsheetDocument"))
-        {
-            return LOK_DOCTYPE_SPREADSHEET;
-        }
-        else if 
(xDocument->supportsService("com.sun.star.presentation.PresentationDocument"))
-        {
-            return LOK_DOCTYPE_PRESENTATION;
-        }
-        else if 
(xDocument->supportsService("com.sun.star.drawing.DrawingDocument"))
-        {
-            return LOK_DOCTYPE_DRAWING;
-        }
-        else if (xDocument->supportsService("com.sun.star.text.TextDocument") 
|| xDocument->supportsService("com.sun.star.text.WebDocument"))
-        {
-            return LOK_DOCTYPE_TEXT;
-        }
-        else
-        {
-            SetLastExceptionMsg("unknown document type");
-        }
-    }
-    catch (const uno::Exception& exception)
-    {
-        SetLastExceptionMsg("exception: " + exception.Message);
-    }
-    return LOK_DOCTYPE_OTHER;
+    return getDocumentType(pThis);
 }
 
 static int doc_getParts (LibreOfficeKitDocument* pThis)
@@ -3792,7 +3798,7 @@ static char* 
doc_getPartPageRectangles(LibreOfficeKitDocument* pThis)
 static char* doc_getA11yFocusedParagraph(LibreOfficeKitDocument* pThis)
 {
     SolarMutexGuard aGuard;
-
+    SetLastExceptionMsg();
 
     ITiledRenderable* pDoc = getTiledRenderable(pThis);
     if (!pDoc)
@@ -3812,7 +3818,7 @@ static char* 
doc_getA11yFocusedParagraph(LibreOfficeKitDocument* pThis)
 static int  doc_getA11yCaretPosition(LibreOfficeKitDocument* pThis)
 {
     SolarMutexGuard aGuard;
-
+    SetLastExceptionMsg();
 
     ITiledRenderable* pDoc = getTiledRenderable(pThis);
     if (!pDoc)
@@ -7002,14 +7008,13 @@ static void doc_setViewTimezone(SAL_UNUSED_PARAMETER 
LibreOfficeKitDocument* /*p
 
 static void doc_setAccessibilityState(SAL_UNUSED_PARAMETER 
LibreOfficeKitDocument* pThis, int nId, bool nEnabled)
 {
-    int nDocType = doc_getDocumentType(pThis);
+    SolarMutexGuard aGuard;
+    SetLastExceptionMsg();
+
+    int nDocType = getDocumentType(pThis);
     if (nDocType != LOK_DOCTYPE_TEXT)
         return;
 
-    SolarMutexGuard aGuard;
-    if (gImpl)
-        gImpl->maLastExceptionMsg.clear();
-
     SfxLokHelper::setAccessibilityState(nId, nEnabled);
 }
 
diff --git a/include/sfx2/viewsh.hxx b/include/sfx2/viewsh.hxx
index 8356e0856f46..142c50d4070b 100644
--- a/include/sfx2/viewsh.hxx
+++ b/include/sfx2/viewsh.hxx
@@ -171,7 +171,7 @@ friend class SfxPrinterController;
     LanguageTag                 maLOKLocale;
     LOKDeviceFormFactor         maLOKDeviceFormFactor;
     bool                        mbLOKAccessibilityEnabled;
-    std::unique_ptr<LOKDocumentFocusListener>   mpLOKDocumentFocusListener;
+    rtl::Reference<LOKDocumentFocusListener>   mpLOKDocumentFocusListener;
     std::unordered_set<OUString>    mvLOKBlockedCommandList;
     OUString maLOKTimezone;
     bool maLOKIsTimezoneSet;
diff --git a/sfx2/source/view/lokhelper.cxx b/sfx2/source/view/lokhelper.cxx
index 91c2e3830c99..3fd57f32200e 100644
--- a/sfx2/source/view/lokhelper.cxx
+++ b/sfx2/source/view/lokhelper.cxx
@@ -147,10 +147,11 @@ void SfxLokHelper::destroyView(int nId)
     const ViewShellId nViewShellId(nId);
     std::vector<SfxViewShell*>& rViewArr = pApp->GetViewShells_Impl();
 
-    for (const SfxViewShell* pViewShell : rViewArr)
+    for (SfxViewShell* pViewShell : rViewArr)
     {
         if (pViewShell->GetViewShellId() == nViewShellId)
         {
+            pViewShell->SetLOKAccessibilityState(false);
             SfxViewFrame* pViewFrame = pViewShell->GetViewFrame();
             SfxRequest aRequest(pViewFrame, SID_CLOSEWIN);
             pViewFrame->Exec_Impl(aRequest);
diff --git a/sfx2/source/view/viewsh.cxx b/sfx2/source/view/viewsh.cxx
index 1dcc9758f0bd..132497876eb7 100644
--- a/sfx2/source/view/viewsh.cxx
+++ b/sfx2/source/view/viewsh.cxx
@@ -238,6 +238,8 @@ void SAL_CALL SfxClipboardChangeListener::changedContents( 
const datatransfer::c
 class LOKDocumentFocusListener :
     public ::cppu::WeakImplHelper< accessibility::XAccessibleEventListener >
 {
+    static constexpr sal_Int64 MAX_ATTACHABLE_CHILDREN = 30;
+
     const SfxViewShell* m_pViewShell;
     std::set< uno::Reference< uno::XInterface > > m_aRefList;
     OUString m_sFocusedParagraph;
@@ -395,7 +397,6 @@ void LOKDocumentFocusListener::notifyTextSelectionChanged()
 
 void LOKDocumentFocusListener::disposing( const lang::EventObject& aEvent )
 {
-
     // Unref the object here, but do not remove as listener since the object
     // might no longer be in a state that safely allows this.
     if( aEvent.Source.is() )
@@ -814,17 +815,20 @@ void LOKDocumentFocusListener::attachRecursive(
         SAL_INFO("lok.a11y", "LOKDocumentFocusListener::attachRecursive(3) #3: 
m_aRefList.insert(xInterface).second");
         xBroadcaster->addAccessibleEventListener(static_cast< 
accessibility::XAccessibleEventListener *>(this));
 
-        const sal_Int32 MAX_ATTACHABLE_CHILDREN = 10;
-        sal_Int32 n, nmax = xContext->getAccessibleChildCount();
-        if( ( nStateSet & 
accessibility::AccessibleStateType::MANAGES_DESCENDANTS ) && nmax > 
MAX_ATTACHABLE_CHILDREN )
-            nmax = MAX_ATTACHABLE_CHILDREN;
 
-        for( n = 0; n < nmax; n++ )
+        if( !(nStateSet & 
accessibility::AccessibleStateType::MANAGES_DESCENDANTS) )
         {
-            uno::Reference< accessibility::XAccessible > xChild( 
xContext->getAccessibleChild( n ) );
+            sal_Int64 nmax = xContext->getAccessibleChildCount();
+            if( nmax > MAX_ATTACHABLE_CHILDREN )
+                nmax = MAX_ATTACHABLE_CHILDREN;
+
+            for( sal_Int64 n = 0; n < nmax; n++ )
+            {
+                uno::Reference< accessibility::XAccessible > xChild( 
xContext->getAccessibleChild( n ) );
 
-            if( xChild.is() )
-                attachRecursive(xChild);
+                if( xChild.is() )
+                    attachRecursive(xChild);
+            }
         }
     }
 }
@@ -878,10 +882,13 @@ void LOKDocumentFocusListener::detachRecursive(
     {
         xBroadcaster->removeAccessibleEventListener(static_cast< 
accessibility::XAccessibleEventListener *>(this));
 
-        if( ( nStateSet & 
accessibility::AccessibleStateType::MANAGES_DESCENDANTS ) == 0 )
+        if( !( nStateSet & 
accessibility::AccessibleStateType::MANAGES_DESCENDANTS ) )
         {
-            sal_Int32 n, nmax = xContext->getAccessibleChildCount();
-            for( n = 0; n < nmax; n++ )
+            sal_Int64 nmax = xContext->getAccessibleChildCount();
+            if( nmax > MAX_ATTACHABLE_CHILDREN )
+                nmax = MAX_ATTACHABLE_CHILDREN;
+
+            for( sal_Int64 n = 0; n < nmax; n++ )
             {
                 uno::Reference< accessibility::XAccessible > xChild( 
xContext->getAccessibleChild( n ) );
 
@@ -2334,7 +2341,7 @@ LOKDocumentFocusListener& 
SfxViewShell::GetLOKDocumentFocusListener()
     if (mpLOKDocumentFocusListener)
         return *mpLOKDocumentFocusListener;
 
-    mpLOKDocumentFocusListener.reset(new LOKDocumentFocusListener(this));
+    mpLOKDocumentFocusListener = new LOKDocumentFocusListener(this);
     return *mpLOKDocumentFocusListener;
 }
 
@@ -2349,7 +2356,6 @@ void SfxViewShell::SetLOKAccessibilityState(bool bEnabled)
         return;
     mbLOKAccessibilityEnabled = bEnabled;
 
-    SAL_DEBUG("SfxViewShell::SetLOKAccessibilityState: bEnabled: " << 
bEnabled);
     LOKDocumentFocusListener& rDocumentFocusListener = 
GetLOKDocumentFocusListener();
 
     if (!pWindow)

Reply via email to