summaryrefslogtreecommitdiffstats
path: root/svx/source/svdraw/svdouno.cxx
blob: 970fa60d370f99bf1aac420b1aa634c02a203d75 (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
495
496
497
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * This file incorporates work covered by the following license notice:
 *
 *   Licensed to the Apache Software Foundation (ASF) under one or more
 *   contributor license agreements. See the NOTICE file distributed
 *   with this work for additional information regarding copyright
 *   ownership. The ASF licenses this file to you under the Apache
 *   License, Version 2.0 (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.apache.org/licenses/LICENSE-2.0 .
 */

#include <sdr/contact/viewcontactofunocontrol.hxx>
#include <sdr/contact/viewobjectcontactofunocontrol.hxx>
#include <com/sun/star/container/XChild.hpp>
#include <com/sun/star/beans/XPropertySet.hpp>
#include <com/sun/star/util/XCloneable.hpp>
#include <com/sun/star/uno/XComponentContext.hpp>
#include <com/sun/star/lang/XEventListener.hpp>
#include <cppuhelper/implbase.hxx>
#include <comphelper/processfactory.hxx>
#include <svx/svdouno.hxx>
#include <svx/svdpagv.hxx>
#include <svx/svdmodel.hxx>
#include <svx/dialmgr.hxx>
#include <svx/strings.hrc>
#include <svx/svdview.hxx>
#include <svx/svdorect.hxx>
#include <svx/svdviter.hxx>
#include <rtl/ref.hxx>
#include <svx/sdrpagewindow.hxx>
#include <comphelper/diagnose_ex.hxx>
#include <tools/debug.hxx>
#include <o3tl/sorted_vector.hxx>

using namespace ::com::sun::star;
using namespace sdr::contact;


class SdrControlEventListenerImpl : public ::cppu::WeakImplHelper< css::lang::XEventListener >
{
protected:
    SdrUnoObj*                  pObj;

public:
    explicit SdrControlEventListenerImpl(SdrUnoObj* _pObj)
    :   pObj(_pObj)
    {}

    // XEventListener
    virtual void SAL_CALL disposing( const css::lang::EventObject& Source ) override;

    void StopListening(const uno::Reference< lang::XComponent >& xComp);
    void StartListening(const uno::Reference< lang::XComponent >& xComp);
};

// XEventListener
void SAL_CALL SdrControlEventListenerImpl::disposing( const css::lang::EventObject& /*Source*/)
{
    if (pObj)
    {
        pObj->xUnoControlModel = nullptr;
    }
}

void SdrControlEventListenerImpl::StopListening(const uno::Reference< lang::XComponent >& xComp)
{
    if (xComp.is())
        xComp->removeEventListener(this);
}

void SdrControlEventListenerImpl::StartListening(const uno::Reference< lang::XComponent >& xComp)
{
    if (xComp.is())
        xComp->addEventListener(this);
}


struct SdrUnoObjDataHolder
{
    mutable ::rtl::Reference< SdrControlEventListenerImpl >
                                    pEventListener;
};


namespace
{
    void lcl_ensureControlVisibility( SdrView const * _pView, const SdrUnoObj* _pObject, bool _bVisible )
    {
        OSL_PRECOND( _pObject, "lcl_ensureControlVisibility: no object -> no survival!" );

        SdrPageView* pPageView = _pView ? _pView->GetSdrPageView() : nullptr;
        DBG_ASSERT( pPageView, "lcl_ensureControlVisibility: no view found!" );
        if ( !pPageView )
            return;

        ViewContact& rUnoControlContact( _pObject->GetViewContact() );

        for ( sal_uInt32 i = 0; i < pPageView->PageWindowCount(); ++i )
        {
            SdrPageWindow* pPageWindow = pPageView->GetPageWindow( i );
            DBG_ASSERT( pPageWindow, "lcl_ensureControlVisibility: invalid PageViewWindow!" );
            if ( !pPageWindow )
                continue;

            if ( !pPageWindow->HasObjectContact() )
                continue;

            ObjectContact& rPageViewContact( pPageWindow->GetObjectContact() );
            const ViewObjectContact& rViewObjectContact( rUnoControlContact.GetViewObjectContact( rPageViewContact ) );
            const ViewObjectContactOfUnoControl* pUnoControlContact = dynamic_cast< const ViewObjectContactOfUnoControl* >( &rViewObjectContact );
            DBG_ASSERT( pUnoControlContact, "lcl_ensureControlVisibility: wrong ViewObjectContact type!" );
            if ( !pUnoControlContact )
                continue;

            pUnoControlContact->ensureControlVisibility( _bVisible );
        }
    }
}

SdrUnoObj::SdrUnoObj(
    SdrModel& rSdrModel,
    const OUString& rModelName)
:   SdrRectObj(rSdrModel),
    m_pImpl( new SdrUnoObjDataHolder )
{
    osl_atomic_increment(&m_refCount); // prevent deletion during creation
    m_bIsUnoObj = true;

    m_pImpl->pEventListener = new SdrControlEventListenerImpl(this);

    // only an owner may create independently
    if (!rModelName.isEmpty())
        CreateUnoControlModel(rModelName);
    osl_atomic_decrement(&m_refCount);
}

SdrUnoObj::SdrUnoObj( SdrModel& rSdrModel, SdrUnoObj const & rSource)
:   SdrRectObj(rSdrModel, rSource),
    m_pImpl( new SdrUnoObjDataHolder )
{
    m_bIsUnoObj = true;

    m_pImpl->pEventListener = new SdrControlEventListenerImpl(this);

    aUnoControlModelTypeName = rSource.aUnoControlModelTypeName;
    aUnoControlTypeName = rSource.aUnoControlTypeName;

    // copy the uno control model
    const uno::Reference< awt::XControlModel > xSourceControlModel = rSource.GetUnoControlModel();
    if ( xSourceControlModel.is() )
    {
        try
        {
            uno::Reference< util::XCloneable > xClone( xSourceControlModel, uno::UNO_QUERY_THROW );
            xUnoControlModel.set( xClone->createClone(), uno::UNO_QUERY_THROW );
        }
        catch( const uno::Exception& )
        {
            DBG_UNHANDLED_EXCEPTION("svx");
        }
    }

    // get service name of the control from the control model
    uno::Reference< beans::XPropertySet > xSet(xUnoControlModel, uno::UNO_QUERY);
    if (xSet.is())
    {
        uno::Any aValue( xSet->getPropertyValue("DefaultControl") );
        OUString aStr;

        if( aValue >>= aStr )
            aUnoControlTypeName = aStr;
    }

    uno::Reference< lang::XComponent > xComp(xUnoControlModel, uno::UNO_QUERY);
    if (xComp.is())
        m_pImpl->pEventListener->StartListening(xComp);
}

SdrUnoObj::SdrUnoObj(
    SdrModel& rSdrModel,
    const OUString& rModelName,
    const uno::Reference< lang::XMultiServiceFactory >& rxSFac)
:   SdrRectObj(rSdrModel),
    m_pImpl( new SdrUnoObjDataHolder )
{
    m_bIsUnoObj = true;

    m_pImpl->pEventListener = new SdrControlEventListenerImpl(this);

    // only an owner may create independently
    if (!rModelName.isEmpty())
        CreateUnoControlModel(rModelName,rxSFac);
}

SdrUnoObj::~SdrUnoObj()
{
    try
    {
        // clean up the control model
        uno::Reference< lang::XComponent > xComp(xUnoControlModel, uno::UNO_QUERY);
        if (xComp.is())
        {
            // is the control model owned by its environment?
            uno::Reference< container::XChild > xContent(xUnoControlModel, uno::UNO_QUERY);
            if (xContent.is() && !xContent->getParent().is())
                xComp->dispose();
            else
                m_pImpl->pEventListener->StopListening(xComp);
        }
    }
    catch( const uno::Exception& )
    {
        TOOLS_WARN_EXCEPTION( "svx", "SdrUnoObj::~SdrUnoObj" );
    }
}

void SdrUnoObj::TakeObjInfo(SdrObjTransformInfoRec& rInfo) const
{
    rInfo.bRotateFreeAllowed        =   false;
    rInfo.bRotate90Allowed          =   false;
    rInfo.bMirrorFreeAllowed        =   false;
    rInfo.bMirror45Allowed          =   false;
    rInfo.bMirror90Allowed          =   false;
    rInfo.bTransparenceAllowed = false;
    rInfo.bShearAllowed             =   false;
    rInfo.bEdgeRadiusAllowed        =   false;
    rInfo.bNoOrthoDesired           =   false;
    rInfo.bCanConvToPath            =   false;
    rInfo.bCanConvToPoly            =   false;
    rInfo.bCanConvToPathLineToArea  =   false;
    rInfo.bCanConvToPolyLineToArea  =   false;
    rInfo.bCanConvToContour = false;
}

SdrObjKind SdrUnoObj::GetObjIdentifier() const
{
    return SdrObjKind::UNO;
}

void SdrUnoObj::SetContextWritingMode( const sal_Int16 _nContextWritingMode )
{
    try
    {
        uno::Reference< beans::XPropertySet > xModelProperties( GetUnoControlModel(), uno::UNO_QUERY_THROW );
        xModelProperties->setPropertyValue( "ContextWritingMode", uno::Any( _nContextWritingMode ) );
    }
    catch( const uno::Exception& )
    {
        DBG_UNHANDLED_EXCEPTION("svx");
    }
}

OUString SdrUnoObj::TakeObjNameSingul() const
{
    OUString sName(SvxResId(STR_ObjNameSingulUno));

    OUString aName(GetName());
    if (!aName.isEmpty())
        sName += " '" + aName + "'";

    return sName;
}

OUString SdrUnoObj::TakeObjNamePlural() const
{
    return SvxResId(STR_ObjNamePluralUno);
}

rtl::Reference<SdrObject> SdrUnoObj::CloneSdrObject(SdrModel& rTargetModel) const
{
    return new SdrUnoObj(rTargetModel, *this);
}

void SdrUnoObj::NbcResize(const Point& rRef, const Fraction& xFact, const Fraction& yFact)
{
    SdrRectObj::NbcResize(rRef,xFact,yFact);

    if (maGeo.m_nShearAngle==0_deg100 && maGeo.m_nRotationAngle==0_deg100)
        return;

    // small correctors
    if (maGeo.m_nRotationAngle>=9000_deg100 && maGeo.m_nRotationAngle<27000_deg100)
    {
        moveRectangle(getRectangle().Left() - getRectangle().Right(), getRectangle().Top() - getRectangle().Bottom());
    }

    maGeo.m_nRotationAngle  = 0_deg100;
    maGeo.m_nShearAngle = 0_deg100;
    maGeo.mfSinRotationAngle       = 0.0;
    maGeo.mfCosRotationAngle       = 1.0;
    maGeo.mfTanShearAngle       = 0.0;
    SetBoundAndSnapRectsDirty();
}


bool SdrUnoObj::hasSpecialDrag() const
{
    // no special drag; we have no rounding rect and
    // do want frame handles
    return false;
}

void SdrUnoObj::NbcSetLayer( SdrLayerID _nLayer )
{
    if ( GetLayer() == _nLayer )
    {   // redundant call -> not interested in doing anything here
        SdrRectObj::NbcSetLayer( _nLayer );
        return;
    }

    // we need some special handling here in case we're moved from an invisible layer
    // to a visible one, or vice versa
    // (relative to a layer. Remember that the visibility of a layer is a view attribute
    // - the same layer can be visible in one view, and invisible in another view, at the
    // same time)

    // collect all views in which our old layer is visible
    o3tl::sorted_vector< SdrView* > aPreviouslyVisible;

    {
        SdrViewIter::ForAllViews(this,
            [&aPreviouslyVisible] (SdrView* pView)
            {
                aPreviouslyVisible.insert( pView );
                return false;
            });
    }

    SdrRectObj::NbcSetLayer( _nLayer );

    // collect all views in which our new layer is visible
    o3tl::sorted_vector< SdrView* > aNewlyVisible;

    SdrViewIter::ForAllViews( this,
        [&aPreviouslyVisible, &aNewlyVisible] (SdrView* pView)
        {
            if ( aPreviouslyVisible.erase(pView) == 0 )
            {
                // in pView, we were visible _before_ the layer change, and are
                // _not_ visible after the layer change
                // => remember this view, as our visibility there changed
                aNewlyVisible.insert( pView );
            }
        });

    // now aPreviouslyVisible contains all views where we became invisible
    for (const auto& rpView : aPreviouslyVisible)
    {
        lcl_ensureControlVisibility( rpView, this, false );
    }

    // and aNewlyVisible all views where we became visible
    for (const auto& rpView : aNewlyVisible)
    {
        lcl_ensureControlVisibility( rpView, this, true );
    }
}

void SdrUnoObj::CreateUnoControlModel(const OUString& rModelName)
{
    DBG_ASSERT(!xUnoControlModel.is(), "model already exists");

    aUnoControlModelTypeName = rModelName;

    uno::Reference< awt::XControlModel >   xModel;
    uno::Reference< uno::XComponentContext > xContext( ::comphelper::getProcessComponentContext() );
    if (!aUnoControlModelTypeName.isEmpty() )
    {
        xModel.set(xContext->getServiceManager()->createInstanceWithContext(
            aUnoControlModelTypeName, xContext), uno::UNO_QUERY);

        if (xModel.is())
            SetChanged();
    }

    SetUnoControlModel(xModel);
}

void SdrUnoObj::CreateUnoControlModel(const OUString& rModelName,
                                      const uno::Reference< lang::XMultiServiceFactory >& rxSFac)
{
    DBG_ASSERT(!xUnoControlModel.is(), "model already exists");

    aUnoControlModelTypeName = rModelName;

    uno::Reference< awt::XControlModel >   xModel;
    if (!aUnoControlModelTypeName.isEmpty() && rxSFac.is() )
    {
        xModel.set(rxSFac->createInstance(aUnoControlModelTypeName), uno::UNO_QUERY);

        if (xModel.is())
            SetChanged();
    }

    SetUnoControlModel(xModel);
}

void SdrUnoObj::SetUnoControlModel( const uno::Reference< awt::XControlModel >& xModel)
{
    if (xUnoControlModel.is())
    {
        uno::Reference< lang::XComponent > xComp(xUnoControlModel, uno::UNO_QUERY);
        if (xComp.is())
            m_pImpl->pEventListener->StopListening(xComp);
    }

    xUnoControlModel = xModel;

    // control model has to contain service name of the control
    if (xUnoControlModel.is())
    {
        uno::Reference< beans::XPropertySet > xSet(xUnoControlModel, uno::UNO_QUERY);
        if (xSet.is())
        {
            uno::Any aValue( xSet->getPropertyValue("DefaultControl") );
            OUString aStr;
            if( aValue >>= aStr )
                aUnoControlTypeName = aStr;
        }

        uno::Reference< lang::XComponent > xComp(xUnoControlModel, uno::UNO_QUERY);
        if (xComp.is())
            m_pImpl->pEventListener->StartListening(xComp);
    }

    // invalidate all ViewObject contacts
    ViewContactOfUnoControl* pVC = nullptr;
    if ( impl_getViewContact( pVC ) )
    {
        // flushViewObjectContacts() removes all existing VOCs for the local DrawHierarchy. This
        // is always allowed since they will be re-created on demand (and with the changed model)
        GetViewContact().flushViewObjectContacts();
    }
}


uno::Reference< awt::XControl > SdrUnoObj::GetUnoControl(const SdrView& _rView, const OutputDevice& _rOut) const
{
    uno::Reference< awt::XControl > xControl;

    SdrPageView* pPageView = _rView.GetSdrPageView();
    OSL_ENSURE( pPageView && getSdrPageFromSdrObject() == pPageView->GetPage(), "SdrUnoObj::GetUnoControl: This object is not displayed in that particular view!" );
    if ( !pPageView || getSdrPageFromSdrObject() != pPageView->GetPage() )
        return nullptr;

    SdrPageWindow* pPageWindow = pPageView->FindPageWindow( _rOut );
    OSL_ENSURE( pPageWindow, "SdrUnoObj::GetUnoControl: did not find my SdrPageWindow!" );
    if ( !pPageWindow )
        return nullptr;

    ViewObjectContact& rViewObjectContact( GetViewContact().GetViewObjectContact( pPageWindow->GetObjectContact() ) );
    ViewObjectContactOfUnoControl* pUnoContact = dynamic_cast< ViewObjectContactOfUnoControl* >( &rViewObjectContact );
    OSL_ENSURE( pUnoContact, "SdrUnoObj::GetUnoControl: wrong contact type!" );
    if ( pUnoContact )
        xControl = pUnoContact->getControl();

    return xControl;
}


uno::Reference< awt::XControl > SdrUnoObj::GetTemporaryControlForWindow(
    const vcl::Window& _rWindow, uno::Reference< awt::XControlContainer >& _inout_ControlContainer ) const
{
    uno::Reference< awt::XControl > xControl;

    ViewContactOfUnoControl* pVC = nullptr;
    if ( impl_getViewContact( pVC ) )
        xControl = pVC->getTemporaryControlForWindow( _rWindow, _inout_ControlContainer );

    return xControl;
}


bool SdrUnoObj::impl_getViewContact( ViewContactOfUnoControl*& _out_rpContact ) const
{
    ViewContact& rViewContact( GetViewContact() );
    _out_rpContact = dynamic_cast< ViewContactOfUnoControl* >( &rViewContact );
    DBG_ASSERT( _out_rpContact, "SdrUnoObj::impl_getViewContact: could not find my ViewContact!" );
    return ( _out_rpContact != nullptr );
}


std::unique_ptr<sdr::contact::ViewContact> SdrUnoObj::CreateObjectSpecificViewContact()
{
  return std::make_unique<sdr::contact::ViewContactOfUnoControl>( *this );
}


/* vim:set shiftwidth=4 softtabstop=4 expandtab: */