summaryrefslogtreecommitdiff
path: root/svx/source/accessibility/AccessibleControlShape.cxx
blob: 9e3754f411cb3e684a465da8d2fab5cbfabab568 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*************************************************************************
 *
 *  $RCSfile: AccessibleControlShape.cxx,v $
 *
 *  $Revision: 1.7 $
 *
 *  last change: $Author: af $ $Date: 2002-05-08 09:41:05 $
 *
 *  The Contents of this file are made available subject to the terms of
 *  either of the following licenses
 *
 *         - GNU Lesser General Public License Version 2.1
 *         - Sun Industry Standards Source License Version 1.1
 *
 *  Sun Microsystems Inc., October, 2000
 *
 *  GNU Lesser General Public License Version 2.1
 *  =============================================
 *  Copyright 2000 by Sun Microsystems, Inc.
 *  901 San Antonio Road, Palo Alto, CA 94303, USA
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License version 2.1, as published by the Free Software Foundation.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 *  MA  02111-1307  USA
 *
 *
 *  Sun Industry Standards Source License Version 1.1
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.1 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://www.openoffice.org/license.html.
 *
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 *
 *  The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 *
 *  Copyright: 2000 by Sun Microsystems, Inc.
 *
 *  All Rights Reserved.
 *
 *  Contributor(s): _______________________________________
 *
 *
 ************************************************************************/

#ifndef _SVX_ACCESSIBILITY_ACCESSIBLE_CONTROL_SHAPE_HXX
#include "AccessibleControlShape.hxx"
#endif
#ifndef _SVX_ACCESSIBILITY_ACCESSIBLE_SHAPE_INFO_HXX
#include "AccessibleShapeInfo.hxx"
#endif
#ifndef _SVX_ACCESSIBILITY_DESCRIPTION_GENERATOR_HXX
#include "DescriptionGenerator.hxx"
#endif
#ifndef _COM_SUN_STAR_DRAWING_XCONTROLSHAPE_HPP_
#include <com/sun/star/drawing/XControlShape.hpp>
#endif
#ifndef _DRAFTS_COM_SUN_STAR_ACCESSIBILITY_ACCESSIBLEEVENTID_HPP_
#include <drafts/com/sun/star/accessibility/AccessibleEventId.hpp>
#endif
#ifndef _COM_SUN_STAR_REFLECTION_XPROXYFACTORY_HPP_
#include <com/sun/star/reflection/XProxyFactory.hpp>
#endif
#ifndef _COMPHELPER_PROCESSFACTORY_HXX_
#include <comphelper/processfactory.hxx>
#endif
#ifndef _SVDOUNO_HXX
#include "svdouno.hxx"
#endif
#ifndef _SVX_UNOAPI_HXX_
#include "unoapi.hxx"
#endif
#include "ShapeTypeHandler.hxx"
#include "SvxShapeTypes.hxx"

using namespace accessibility;
using namespace ::rtl;
using namespace ::com::sun::star;
using namespace ::drafts::com::sun::star::accessibility;
using ::com::sun::star::uno::Reference;
using ::com::sun::star::reflection::XProxyFactory;
using ::com::sun::star::uno::XAggregation;
using ::com::sun::star::lang::XComponent;

//=====  internal  ============================================================

AccessibleControlShape::AccessibleControlShape (
    const AccessibleShapeInfo& rShapeInfo,
    const AccessibleShapeTreeInfo& rShapeTreeInfo)
    :      AccessibleShape (rShapeInfo, rShapeTreeInfo)
    ,   mbListeningForName( sal_False )
    ,   mbListeningForDesc( sal_False )
    ,   mbDisposeNativeContext( sal_False )
{
}




AccessibleControlShape::~AccessibleControlShape (void)
{
    if ( m_xControlContextProxy.is() )
        m_xControlContextProxy->setDelegator( NULL );
    m_xControlContextProxy.clear();
        // this should remove the _one_and_only_ "real" reference (means not delegated to
        // ourself) to this proxy, and thus delete it
}

//=============================================================================
SdrObject* AccessibleControlShape::getSdrObject() const
{
    return GetSdrObjectFromXShape (mxShape);
}

//-----------------------------------------------------------------------------
void AccessibleControlShape::Init()
{
    AccessibleShape::Init();

    OSL_ENSURE( !m_xControlContextProxy.is(), "AccessibleControlShape::Init: already initialized!" );
    try
    {
        // What we need to do here is merge the functionality of the AccessibleContext of our UNO control
        // with our own AccessibleContext-related functionality.
        //
        // The problem is that we do not know the interfaces our "inner" context supports - this may be any
        // XAccessibleXXX interface (or even any other) which makes sense for it.
        //
        // In theory, we could implement all possible interfaces ourself, and re-route all functionality to
        // the inner context (except those we implement ourself, like XAccessibleComponent). But this is in no
        // way future-proof - as soon as an inner context appears which implements an additional interface,
        // we would need to adjust our implementation to support this new interface, too. Bad idea.
        //
        // The usual solution for such a problem is aggregation. Aggregation means using UNO's own meachnisms
        // for merging an inner with an outer component, an get a component which behaves as it is exactly one.
        // This is what XAggregation is for. Unfortunately, aggregation requires _exact_ control over the ref count
        // of the inner object, which we do not have at all.
        // Bad, too.
        //
        // But there is a solution: com.sun.star.reflection.ProxyFactory. This service is able to create a proxy
        // for any component, which supports _exactly_ the same interfaces as the component. In addition, it can
        // be aggregated, as by definition the proxy's ref count is exactly one when returned from the factory.
        // Sounds better. Though this yields the problem of slightly degraded performance, it's the only solution
        // I'm aware of at the moment .....
        //
        // 98750 - 30.04.2002 - fs@openoffice.org
        //

        // get the control which belongs to our model (relative to our view)
        const Window* pViewWindow = mShapeTreeInfo.GetWindow();
        SdrUnoObj* pUnoObjectImpl = PTR_CAST( SdrUnoObj, getSdrObject() );
        OSL_ENSURE( pViewWindow && pUnoObjectImpl, "AccessibleControlShape::Init: no view, or no SdrUnoObj!" );

        if ( pViewWindow && pUnoObjectImpl )
        {
            // get the context of the control - it will be our "inner" context
            Reference< XAccessible > xControlAccessible( pUnoObjectImpl->GetUnoControl( pViewWindow ), uno::UNO_QUERY );
            Reference< XAccessibleContext > xNativeControlContext;
            if ( xControlAccessible.is() )
                xNativeControlContext = xControlAccessible->getAccessibleContext();

            // get a proxy for this context
            // first a factory for the proxy
            Reference< XProxyFactory > xFactory;
            xFactory = xFactory.query( ::comphelper::createProcessComponent(
                ::rtl::OUString::createFromAscii( "com.sun.star.reflection.ProxyFactory" ) ) );
            OSL_ENSURE( xFactory.is(), "AccessibleControlShape::Init: could not create a proxy factory!" );
            // then the proxy itself
            if ( xFactory.is() && xNativeControlContext.is() )
            {
                m_xControlContextProxy = xFactory->createProxy( xNativeControlContext );

                // aggregate the proxy
                osl_incrementInterlockedCount( &m_refCount );
                if ( m_xControlContextProxy.is() )
                {
                    // At this point in time, the proxy has a ref count of exactly one - in m_xControlContextProxy.
                    // Remember to _not_ reset this member unles the delegator of the proxy has been reset, too!
                    m_xControlContextProxy->setDelegator( *this );
                }
                osl_decrementInterlockedCount( &m_refCount );

                mbDisposeNativeContext = sal_True;

                // Finally, we need to add ourself as dispose listener to the native context. In case it is disposed,
                // we need to dispose ourself, too. With a "real" aggregation (i.e. not using a proxy), this would be
                // done automatically, as every access to the XComponent interface of the aggregated object would
                // be re-routed to ourself. But with aggregating a proxy only we have to take care for this ourself.
                mxNativeContextComponent = mxNativeContextComponent.query( xNativeControlContext );
                if ( mxNativeContextComponent.is() )
                    mxNativeContextComponent->addEventListener( static_cast< AccessibleControlShape_Base* >( this ) );
            }
        }
    }
    catch( const uno::Exception& )
    {
        OSL_ENSURE( sal_False, "AccessibleControlShape::Init: could not \"aggregate\" the controls XAccessibleContext!" );
    }
}

//=====  XAccessible  =========================================================

Reference<XAccessibleContext> SAL_CALL
    AccessibleControlShape::getAccessibleContext (void)
    throw (::com::sun::star::uno::RuntimeException)
{
    return AccessibleShape::getAccessibleContext ();
}


//=====  XServiceInfo  ========================================================

::rtl::OUString SAL_CALL
    AccessibleControlShape::getImplementationName (void)
    throw (::com::sun::star::uno::RuntimeException)
{
    return ::rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("AccessibleControlShape"));
}




/// Set this object's name if is different to the current name.
::rtl::OUString
    AccessibleControlShape::CreateAccessibleBaseName (void)
    throw (::com::sun::star::uno::RuntimeException)
{
    ::rtl::OUString sName;

    ShapeTypeId nShapeType = ShapeTypeHandler::Instance().GetTypeId (mxShape);
    switch (nShapeType)
    {
        case DRAWING_CONTROL:
            sName = ::rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("ControlShape"));
            break;
        default:
            sName = ::rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("UnknownAccessibleControlShape"));
            uno::Reference<drawing::XShapeDescriptor> xDescriptor (mxShape, uno::UNO_QUERY);
            if (xDescriptor.is())
                sName += ::rtl::OUString (RTL_CONSTASCII_USTRINGPARAM(": "))
                    + xDescriptor->getShapeType();
    }

    return sName;
}




//--------------------------------------------------------------------
namespace
{
    //................................................................
    const ::rtl::OUString& lcl_getNamePropertyName( )
    {
        static ::rtl::OUString s_sNamePropertyName( RTL_CONSTASCII_USTRINGPARAM( "Name" ) );
        return s_sNamePropertyName;
    }
    //................................................................
    const ::rtl::OUString& lcl_getDescPropertyName( )
    {
        static ::rtl::OUString s_sDescPropertyDesc( RTL_CONSTASCII_USTRINGPARAM( "HelpText" ) );
        return s_sDescPropertyDesc;
    }
}

//--------------------------------------------------------------------
::rtl::OUString
    AccessibleControlShape::CreateAccessibleDescription (void)
    throw (::com::sun::star::uno::RuntimeException)
{
    DescriptionGenerator aDG (mxShape);
    ShapeTypeId nShapeType = ShapeTypeHandler::Instance().GetTypeId (mxShape);
    switch (nShapeType)
    {
        case DRAWING_CONTROL:
        {
            // check if we can obtain the "Desc" property from the model
            ::rtl::OUString sDesc( getControlModelStringProperty( lcl_getDescPropertyName() ) );
            if ( !sDesc.getLength() )
            {   // no -> use the default
                aDG.Initialize (OUString::createFromAscii("Control"));
                aDG.AddProperty (OUString::createFromAscii ("ControlBackground"),
                    DescriptionGenerator::COLOR,
                    OUString());
                aDG.AddProperty (OUString::createFromAscii ("ControlBorder"),
                    DescriptionGenerator::INTEGER,
                    OUString());
            }
            // ensure that we are listening to the Name property
            mbListeningForDesc = ensureListeningState( mbListeningForDesc, sal_True, lcl_getDescPropertyName() );
        }
        break;

        default:
            aDG.Initialize (::rtl::OUString::createFromAscii (
                "Unknown accessible control shape"));
            uno::Reference<drawing::XShapeDescriptor> xDescriptor (mxShape, uno::UNO_QUERY);
            if (xDescriptor.is())
            {
                aDG.AppendString (::rtl::OUString (RTL_CONSTASCII_USTRINGPARAM("service name=")));
                aDG.AppendString (xDescriptor->getShapeType());
            }
    }

    return aDG();
}

//--------------------------------------------------------------------
IMPLEMENT_FORWARD_XINTERFACE2( AccessibleControlShape, AccessibleShape, AccessibleControlShape_Base )
IMPLEMENT_FORWARD_XTYPEPROVIDER2( AccessibleControlShape, AccessibleShape, AccessibleControlShape_Base )
    // order matters in both cases: the second argument is the base class doing the ref counting

//--------------------------------------------------------------------
void SAL_CALL AccessibleControlShape::propertyChange( const beans::PropertyChangeEvent& _rEvent ) throw (uno::RuntimeException)
{
    ::osl::MutexGuard aGuard( maMutex );

    // check if it is the name or the description
    if ( _rEvent.PropertyName.equals( lcl_getNamePropertyName() ) )
    {
        SetAccessibleName (CreateAccessibleName());
    }
    else if ( _rEvent.PropertyName.equals( lcl_getDescPropertyName() ) )
    {
        SetAccessibleDescription (CreateAccessibleDescription());
    }
#ifdef _DEBUG
    else
    {
        OSL_ENSURE( sal_False, "AccessibleControlShape::propertyChange: where did this come from?" );
    }
#endif
}

//--------------------------------------------------------------------
void SAL_CALL AccessibleControlShape::disposing (const lang::EventObject& _rSource) throw (uno::RuntimeException)
{
    // did it come from our inner context (the real one, not it's proxy!)?
    OSL_TRACE ("AccessibleControlShape::disposing");
    if ( _rSource.Source == mxNativeContextComponent )
    {
        // If our "pseudo-aggregated" inner context does not live anymore,
        // we don't want to live, too.  This is accomplished by asking our
        // parent to replace this object with a new one.  Disposing this
        // object and sending notifications about the replacement are the
        // task of our parent.
        mpParent->ReplaceChild (this,
            ShapeTypeHandler::Instance().CreateAccessibleObject (
                AccessibleShapeInfo (mxShape, getAccessibleParent(), mpParent, mnIndex),
                mShapeTreeInfo));
    }
    else
        AccessibleShape::disposing( _rSource );
}

//--------------------------------------------------------------------
sal_Bool AccessibleControlShape::ensureListeningState(
        const sal_Bool _bCurrentlyListening, const sal_Bool _bNeedNewListening,
        const ::rtl::OUString& _rPropertyName )
{
    if ( ( _bCurrentlyListening == _bNeedNewListening ) || !ensureControlModelAccess() )
        //  nothing to do
        return _bCurrentlyListening;

    try
    {
        if ( !mxModelPropsMeta.is() || mxModelPropsMeta->hasPropertyByName( _rPropertyName ) )
        {
            // add or revoke as listener
            if ( _bNeedNewListening )
                mxControlModel->addPropertyChangeListener( _rPropertyName, static_cast< beans::XPropertyChangeListener* >( this ) );
            else
                mxControlModel->removePropertyChangeListener( _rPropertyName, static_cast< beans::XPropertyChangeListener* >( this ) );
        }
    }
    catch( const uno::Exception& e )
    {
        e;  // make compiler happy
        OSL_ENSURE( sal_False, "AccessibleControlShape::ensureListeningState: could not change the listening state!" );
    }

    return _bNeedNewListening;
}

//--------------------------------------------------------------------
::rtl::OUString AccessibleControlShape::CreateAccessibleName (void) throw (uno::RuntimeException)
{
    // check if we can obtain the "Name" property from the model
    ::rtl::OUString sName( getControlModelStringProperty( lcl_getNamePropertyName() ) );
    if ( !sName.getLength() )
    {   // no -> use the default
        sName = AccessibleShape::CreateAccessibleName();
    }

    // now that somebody first asked us for our name, ensure that we are listening to name changes on the model
    mbListeningForName = ensureListeningState( mbListeningForName, sal_True, lcl_getNamePropertyName() );

    return sName;
}

//--------------------------------------------------------------------
void SAL_CALL AccessibleControlShape::disposing (void)
{
    // ensure we're not listening
    mbListeningForName = ensureListeningState( mbListeningForName, sal_False, lcl_getNamePropertyName() );
    mbListeningForDesc = ensureListeningState( mbListeningForDesc, sal_False, lcl_getDescPropertyName() );

    // release the model
    mxControlModel.clear();
    mxModelPropsMeta.clear();

    // forward the disposel to our inner context
    if ( mbDisposeNativeContext )
    {
        // don't listen for disposals anymore
        if ( mxNativeContextComponent.is() )
            mxNativeContextComponent->removeEventListener( static_cast< AccessibleControlShape_Base* >( this ) );

        Reference< XComponent > xInnerComponent;
        if ( ::comphelper::query_aggregation( m_xControlContextProxy, xInnerComponent ) )
            xInnerComponent->dispose();
        // do _not_ clear m_xControlContextProxy! This has to be done in the dtor for correct ref-count handling

        // no need to dispose the proxy/inner context anymore
        mbDisposeNativeContext = sal_False;
    }
    mxNativeContextComponent.clear();

    // let the base do it's stuff
    AccessibleShape::disposing();
}

//--------------------------------------------------------------------
sal_Bool AccessibleControlShape::ensureControlModelAccess() SAL_THROW(())
{
    if ( mxControlModel.is() )
        return sal_True;

    try
    {
        Reference< drawing::XControlShape > xShape( mxShape, uno::UNO_QUERY );
        if ( xShape.is() )
            mxControlModel = mxControlModel.query( xShape->getControl() );

        if ( mxControlModel.is() )
            mxModelPropsMeta = mxControlModel->getPropertySetInfo();
    }
    catch( const uno::Exception& e )
    {
        e;  // make compiler happy
        OSL_ENSURE( sal_False, "AccessibleControlShape::ensureControlModelAccess: caught an exception!" );
    }

    return mxControlModel.is();
}

//--------------------------------------------------------------------
::rtl::OUString AccessibleControlShape::getControlModelStringProperty( const ::rtl::OUString& _rPropertyName ) const SAL_THROW(())
{
    ::rtl::OUString sReturn;
    try
    {
        if ( const_cast< AccessibleControlShape* >( this ) ->ensureControlModelAccess() )
        {
            if ( !mxModelPropsMeta.is() ||  mxModelPropsMeta->hasPropertyByName( _rPropertyName ) )
                // ask only if a) the control does not have a PropertySetInfo object or b) it has, and the
                // property in question is available
                mxControlModel->getPropertyValue( _rPropertyName ) >>= sReturn;
        }
    }
    catch( const uno::Exception& )
    {
        OSL_ENSURE( sal_False, "OAccessibleControlContext::getModelStringProperty: caught an exception!" );
    }
    return sReturn;
}