summaryrefslogtreecommitdiff
path: root/embeddedobj/source/msole/olevisual.cxx
blob: d53ef5fea033e597c7a6dcae4605840ff900e802 (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
/* -*- 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 <com/sun/star/lang/DisposedException.hpp>
#include <com/sun/star/embed/EmbedStates.hpp>
#include <com/sun/star/embed/EmbedMapUnits.hpp>
#include <com/sun/star/embed/EmbedMisc.hpp>
#include <com/sun/star/embed/Aspects.hpp>
#include <com/sun/star/io/XSeekable.hpp>
#include <com/sun/star/embed/NoVisualAreaSizeException.hpp>

#include <oleembobj.hxx>
#include <comphelper/mimeconfighelper.hxx>
#include <comphelper/seqstream.hxx>
#include <filter/msfilter/classids.hxx>

#if defined(_WIN32)
#include <olecomponent.hxx>
#endif

using namespace ::com::sun::star;
using namespace ::comphelper;

embed::VisualRepresentation OleEmbeddedObject::GetVisualRepresentationInNativeFormat_Impl(
                    const uno::Reference< io::XStream > xCachedVisRepr )
        throw ( uno::Exception )
{
    embed::VisualRepresentation aVisualRepr;

    // TODO: detect the format in the future for now use workaround
    uno::Reference< io::XInputStream > xInStream = xCachedVisRepr->getInputStream();
    uno::Reference< io::XSeekable > xSeekable( xCachedVisRepr, uno::UNO_QUERY );
    if ( !xInStream.is() || !xSeekable.is() )
        throw uno::RuntimeException();

    uno::Sequence< sal_Int8 > aSeq( 2 );
    xInStream->readBytes( aSeq, 2 );
    xSeekable->seek( 0 );
    if ( aSeq.getLength() == 2 && aSeq[0] == 'B' && aSeq[1] == 'M' )
    {
        // it's a bitmap
        aVisualRepr.Flavor = datatransfer::DataFlavor(
            OUString( "application/x-openoffice-bitmap;windows_formatname=\"Bitmap\"" ),
            OUString( "Bitmap" ),
            cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
    }
    else
    {
        // it's a metafile
        aVisualRepr.Flavor = datatransfer::DataFlavor(
            OUString( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
            OUString( "Windows Metafile" ),
            cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );
    }

    sal_Int32 nStreamLength = (sal_Int32)xSeekable->getLength();
    uno::Sequence< sal_Int8 > aRepresent( nStreamLength );
    xInStream->readBytes( aRepresent, nStreamLength );
    aVisualRepr.Data <<= aRepresent;

    return aVisualRepr;
}

void SAL_CALL OleEmbeddedObject::setVisualAreaSize( sal_Int64 nAspect, const awt::Size& aSize )
        throw ( lang::IllegalArgumentException,
                embed::WrongStateException,
                uno::Exception,
                uno::RuntimeException, std::exception )
{
    // begin wrapping related part ====================
    uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
    if ( xWrappedObject.is() )
    {
        // the object was converted to OOo embedded object, the current implementation is now only a wrapper
        xWrappedObject->setVisualAreaSize( nAspect, aSize );
        return;
    }
    // end wrapping related part ====================

    ::osl::ResettableMutexGuard aGuard( m_aMutex );
    if ( m_bDisposed )
        throw lang::DisposedException(); // TODO

    SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!\n" );
    if ( nAspect == embed::Aspects::MSOLE_ICON )
        // no representation can be retrieved
        throw embed::WrongStateException( "Illegal call!",
                                    static_cast< ::cppu::OWeakObject* >(this) );

    if ( m_nObjectState == -1 )
        throw embed::WrongStateException( "The object is not loaded!",
                                    static_cast< ::cppu::OWeakObject* >(this) );

#ifdef _WIN32
    // RECOMPOSE_ON_RESIZE misc flag means that the object has to be switched to running state on resize.
    // SetExtent() is called only for objects that require it,
    // it should not be called for MSWord documents to workaround problem i49369
    // If cached size is not set, that means that this is the size initialization, so there is no need to set the real size
    sal_Bool bAllowToSetExtent =
      ( ( getStatus( nAspect ) & embed::EmbedMisc::MS_EMBED_RECOMPOSEONRESIZE )
      && !MimeConfigurationHelper::ClassIDsEqual(m_aClassID, MimeConfigurationHelper::GetSequenceClassID(MSO_WW8_CLASSID))
      && m_bHasCachedSize );

    if ( m_nObjectState == embed::EmbedStates::LOADED && bAllowToSetExtent )
    {
        aGuard.clear();
        try {
            changeState( embed::EmbedStates::RUNNING );
        }
        catch( const uno::Exception& )
        {
            SAL_WARN( "embeddedobj.ole", "The object should not be resized without activation!\n" );
        }
        aGuard.reset();
    }

    if ( m_pOleComponent && m_nObjectState != embed::EmbedStates::LOADED && bAllowToSetExtent )
    {
        awt::Size aSizeToSet = aSize;
        aGuard.clear();
        try {
            m_pOleComponent->SetExtent( aSizeToSet, nAspect ); // will throw an exception in case of failure
            m_bHasSizeToSet = sal_False;
        }
        catch( const uno::Exception& )
        {
            // some objects do not allow to set the size even in running state
            m_bHasSizeToSet = sal_True;
            m_aSizeToSet = aSizeToSet;
            m_nAspectToSet = nAspect;
        }
        aGuard.reset();
    }
#endif

    // cache the values
    m_bHasCachedSize = true;
    m_aCachedSize = aSize;
    m_nCachedAspect = nAspect;
}

awt::Size SAL_CALL OleEmbeddedObject::getVisualAreaSize( sal_Int64 nAspect )
        throw ( lang::IllegalArgumentException,
                embed::WrongStateException,
                uno::Exception,
                uno::RuntimeException, std::exception )
{
    // begin wrapping related part ====================
    uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
    if ( xWrappedObject.is() )
    {
        // the object was converted to OOo embedded object, the current implementation is now only a wrapper
        return xWrappedObject->getVisualAreaSize( nAspect );
    }
    // end wrapping related part ====================

    ::osl::ResettableMutexGuard aGuard( m_aMutex );
    if ( m_bDisposed )
        throw lang::DisposedException(); // TODO

    SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
    if ( nAspect == embed::Aspects::MSOLE_ICON )
        // no representation can be retrieved
        throw embed::WrongStateException( "Illegal call!",
                                    static_cast< ::cppu::OWeakObject* >(this) );

    if ( m_nObjectState == -1 )
        throw embed::WrongStateException( "The object is not loaded!",
                                    static_cast< ::cppu::OWeakObject* >(this) );

    awt::Size aResult;

#ifdef _WIN32
    // TODO/LATER: Support different aspects
    if ( m_pOleComponent && !m_bHasSizeToSet && nAspect == embed::Aspects::MSOLE_CONTENT )
    {
        try
        {
            // the cached size updated every time the object is stored
            if ( m_bHasCachedSize )
            {
                aResult = m_aCachedSize;
            }
            else
            {
                // there is no internal cache
                awt::Size aSize;
                aGuard.clear();

                sal_Bool bSuccess = sal_False;
                if ( getCurrentState() == embed::EmbedStates::LOADED )
                {
                    SAL_WARN( "embeddedobj.ole", "Loaded object has no cached size!" );

                    // try to switch the object to RUNNING state and request the value again
                    try {
                        changeState( embed::EmbedStates::RUNNING );
                    }
                    catch( const uno::Exception& )
                    {
                        throw embed::NoVisualAreaSizeException(
                                "No size available!",
                                static_cast< ::cppu::OWeakObject* >(this) );
                    }
                }

                try
                {
                    // first try to get size using replacement image
                    aSize = m_pOleComponent->GetExtent( nAspect ); // will throw an exception in case of failure
                    bSuccess = sal_True;
                }
                catch( const uno::Exception& )
                {
                }

                if ( !bSuccess )
                {
                    try
                    {
                        // second try the cached replacement image
                        aSize = m_pOleComponent->GetCachedExtent( nAspect ); // will throw an exception in case of failure
                        bSuccess = sal_True;
                    }
                    catch( const uno::Exception& )
                    {
                    }
                }

                if ( !bSuccess )
                {
                    try
                    {
                        // third try the size reported by the object
                        aSize = m_pOleComponent->GetReccomendedExtent( nAspect ); // will throw an exception in case of failure
                        bSuccess = sal_True;
                    }
                    catch( const uno::Exception& )
                    {
                    }
                }

                if ( !bSuccess )
                    throw embed::NoVisualAreaSizeException(
                                    "No size available!",
                                    static_cast< ::cppu::OWeakObject* >(this) );

                aGuard.reset();

                m_aCachedSize = aSize;
                m_nCachedAspect = nAspect;
                m_bHasCachedSize = sal_True;

                aResult = m_aCachedSize;
            }
        }
        catch ( const embed::NoVisualAreaSizeException& )
        {
            throw;
        }
        catch ( const uno::Exception& )
        {
            throw embed::NoVisualAreaSizeException(
                            "No size available!",
                            static_cast< ::cppu::OWeakObject* >(this) );
        }
    }
    else
#endif
    {
        // return cached value
        if ( m_bHasCachedSize )
        {
            SAL_WARN_IF( nAspect != m_nCachedAspect, "embeddedobj.ole", "Unexpected aspect is requested!" );
            aResult = m_aCachedSize;
        }
        else
        {
            throw embed::NoVisualAreaSizeException(
                            "No size available!",
                            static_cast< ::cppu::OWeakObject* >(this) );
        }
    }

    return aResult;
}

embed::VisualRepresentation SAL_CALL OleEmbeddedObject::getPreferredVisualRepresentation( sal_Int64 nAspect )
        throw ( lang::IllegalArgumentException,
                embed::WrongStateException,
                uno::Exception,
                uno::RuntimeException, std::exception )
{
    // begin wrapping related part ====================
    uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
    if ( xWrappedObject.is() )
    {
        // the object was converted to OOo embedded object, the current implementation is now only a wrapper
        return xWrappedObject->getPreferredVisualRepresentation( nAspect );
    }
    // end wrapping related part ====================

    ::osl::MutexGuard aGuard( m_aMutex );
    if ( m_bDisposed )
        throw lang::DisposedException(); // TODO

    SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
    if ( nAspect == embed::Aspects::MSOLE_ICON )
        // no representation can be retrieved
        throw embed::WrongStateException( "Illegal call!",
                                    static_cast< ::cppu::OWeakObject* >(this) );

    // TODO: if the object has cached representation then it should be returned
    // TODO: if the object has no cached representation and is in loaded state it should switch itself to the running state
    if ( m_nObjectState == -1 )
        throw embed::WrongStateException( "The object is not loaded!",
                                    static_cast< ::cppu::OWeakObject* >(this) );

    embed::VisualRepresentation aVisualRepr;

    // TODO: in case of different aspects they must be applied to the mediatype and XTransferable must be used
    // the cache is used only as a fallback if object is not in loaded state
    if ( !m_xCachedVisualRepresentation.is() && ( !m_bVisReplInitialized || m_bVisReplInStream )
      && m_nObjectState == embed::EmbedStates::LOADED )
    {
        m_xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream, true );
        SetVisReplInStream( m_xCachedVisualRepresentation.is() );
    }

    if ( m_xCachedVisualRepresentation.is() )
    {
        return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation );
    }
#ifdef _WIN32
    else if ( m_pOleComponent )
    {
        try
        {
            if ( m_nObjectState == embed::EmbedStates::LOADED )
                changeState( embed::EmbedStates::RUNNING );

            datatransfer::DataFlavor aDataFlavor(
                    OUString( "application/x-openoffice-wmf;windows_formatname=\"Image WMF\"" ),
                    OUString( "Windows Metafile" ),
                    cppu::UnoType<uno::Sequence< sal_Int8 >>::get() );

            aVisualRepr.Data = m_pOleComponent->getTransferData( aDataFlavor );
            aVisualRepr.Flavor = aDataFlavor;

            uno::Sequence< sal_Int8 > aVisReplSeq;
            aVisualRepr.Data >>= aVisReplSeq;
            if ( aVisReplSeq.getLength() )
            {
                m_xCachedVisualRepresentation = GetNewFilledTempStream_Impl(
                        uno::Reference< io::XInputStream > ( static_cast< io::XInputStream* > (
                            new ::comphelper::SequenceInputStream( aVisReplSeq ) ) ) );
            }

            return aVisualRepr;
        }
        catch( const uno::Exception& )
        {}
    }
#endif

    // the cache is used only as a fallback if object is not in loaded state
    if ( !m_xCachedVisualRepresentation.is() && ( !m_bVisReplInitialized || m_bVisReplInStream ) )
    {
        m_xCachedVisualRepresentation = TryToRetrieveCachedVisualRepresentation_Impl( m_xObjectStream );
        SetVisReplInStream( m_xCachedVisualRepresentation.is() );
    }

    if ( !m_xCachedVisualRepresentation.is() )
    {
        // no representation can be retrieved
        throw embed::WrongStateException( "Illegal call!",
                                    static_cast< ::cppu::OWeakObject* >(this) );
    }

    return GetVisualRepresentationInNativeFormat_Impl( m_xCachedVisualRepresentation );
}

sal_Int32 SAL_CALL OleEmbeddedObject::getMapUnit( sal_Int64 nAspect )
        throw ( uno::Exception,
                uno::RuntimeException, std::exception)
{
    // begin wrapping related part ====================
    uno::Reference< embed::XEmbeddedObject > xWrappedObject = m_xWrappedObject;
    if ( xWrappedObject.is() )
    {
        // the object was converted to OOo embedded object, the current implementation is now only a wrapper
        return xWrappedObject->getMapUnit( nAspect );
    }
    // end wrapping related part ====================

    ::osl::MutexGuard aGuard( m_aMutex );
    if ( m_bDisposed )
        throw lang::DisposedException(); // TODO

    SAL_WARN_IF( nAspect == embed::Aspects::MSOLE_ICON, "embeddedobj.ole", "For iconified objects no graphical replacement is required!" );
    if ( nAspect == embed::Aspects::MSOLE_ICON )
        // no representation can be retrieved
        throw embed::WrongStateException( "Illegal call!",
                                    static_cast< ::cppu::OWeakObject* >(this) );

    if ( m_nObjectState == -1 )
        throw embed::WrongStateException( "The object is not loaded!",
                                    static_cast< ::cppu::OWeakObject* >(this) );

    return embed::EmbedMapUnits::ONE_100TH_MM;
}


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