summaryrefslogtreecommitdiff
path: root/touch/source/uno/Document.cxx
blob: 5163ef4b28e78bd6438e12e547a7b44a3eb512a8 (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
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * Copyright 2012 LibreOffice contributors.
 *
 * 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/.
 */

// NOTE: Work in progress, most likely makes little sense

#include <com/sun/star/awt/XBitmap.hpp>
#include <com/sun/star/awt/XDevice.hpp>
#include <com/sun/star/awt/XToolkit2.hpp>
#include <com/sun/star/beans/NamedValue.hpp>
#include <com/sun/star/beans/PropertyValues.hpp>
#include <com/sun/star/frame/XComponentLoader.hpp>
#include <com/sun/star/frame/XDesktop.hpp>
#include <com/sun/star/lang/XInitialization.hpp>
#include <com/sun/star/view/XRenderable.hpp>

#include <cppuhelper/implbase1.hxx>
#include <cppuhelper/basemutex.hxx>

#include <org/libreoffice/touch/Document.hpp>

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

using ::rtl::OUString;
using ::osl::MutexGuard;

namespace org { namespace libreoffice { namespace touch
{

class DocumentImpl:
    public XDocument
{

private:
    OUString m_sURI;
    uno::Reference< uno::XComponentContext > m_rContext;
    uno::Reference< lang::XComponent > m_xComponent;
    uno::Reference< awt::XToolkit2 > m_xToolkit;
    uno::Reference< frame::XController > m_xController;

    // XRenderable.getRendererCount() and .render() need an XController in the
    // properties, at least in the test Java code it seemed that a totally
    // dummy one works, so try that here, too.

    typedef ::cppu::WeakImplHelper1< frame::XController > MyXController_Base;

    class MyXController:
        public MyXController_Base,
        public ::cppu::BaseMutex
    {
    private:
        uno::Reference< frame::XFrame > m_xFrame;
        uno::Reference< frame::XModel > m_xModel;

    public:
        virtual void SAL_CALL
        attachFrame( const uno::Reference< frame::XFrame >& xFrame )
            throw( uno::RuntimeException )
        {
            m_xFrame = xFrame;
        }

        virtual sal_Bool SAL_CALL
        attachModel( const uno::Reference< frame::XModel >& xModel )
            throw( uno::RuntimeException )
        {
            m_xModel = xModel;
            return sal_True;
        }

        virtual sal_Bool SAL_CALL
        suspend( sal_Bool /* bSuspend */ )
            throw( uno::RuntimeException )
        {
            return sal_False;
        }

        virtual uno::Any SAL_CALL getViewData()
            throw( uno::RuntimeException )
        {
            return uno::Any();
        }

        virtual void SAL_CALL restoreViewData( const uno::Any& /* data */ )
            throw ( uno::RuntimeException )
        {
        }

        virtual uno::Reference< frame::XModel > SAL_CALL
        getModel()
            throw ( uno::RuntimeException )
        {
            return m_xModel;
        }

        virtual uno::Reference< frame::XFrame > SAL_CALL
        getFrame()
            throw ( uno::RuntimeException )
        {
            return m_xFrame;
        }

        virtual void SAL_CALL
        dispose()
            throw ( uno::RuntimeException )
        {
        }

        virtual void SAL_CALL
        addEventListener( const uno::Reference< lang::XEventListener >& /* xListener */ )
            throw ( uno::RuntimeException )
        {
        }

        virtual void SAL_CALL
        removeEventListener( const uno::Reference< lang::XEventListener >& /* xListener */ )
            throw ( uno::RuntimeException )
        {
        }
    };

protected:
    DocumentImpl( const uno::Reference< uno::XComponentContext > context ):
        m_rContext( context )
    {
    }

    virtual ~DocumentImpl()
    {
    }

public:
    // XInitialization
    virtual void SAL_CALL
    initialize( const uno::Sequence< uno::Any >& arguments )
        throw ( uno::Exception, uno::RuntimeException )
    {
        if ( arguments.getLength() != 1 )
            throw lang::IllegalArgumentException( OUString(), static_cast<uno::Reference< uno::XInterface> >(this), 1 );

        uno::Sequence< beans::NamedValue > settings;
        if ( arguments[0] >>= m_sURI )
        {
            // create( [in] string uri );
            uno::Reference< frame::XDesktop > desktop( m_rContext->getServiceManager()->createInstanceWithContext( "com.sun.star.frame.Desktop", m_rContext ), uno::UNO_QUERY_THROW );
            uno::Reference< frame::XComponentLoader > componentLoader( desktop, uno::UNO_QUERY_THROW );

            (void) componentLoader;

            beans::PropertyValues loadProps(3);
            loadProps[0].Name = "Hidden";
            loadProps[0].Value <<= sal_Bool(true);
            loadProps[1].Name = "ReadOnly";
            loadProps[1].Value <<= sal_Bool(true);
            loadProps[2].Name = "Preview";
            loadProps[2].Value <<= sal_Bool(true);

            m_xComponent = componentLoader->loadComponentFromURL( m_sURI, "_blank", 0, loadProps );

            m_xToolkit = uno::Reference< awt::XToolkit2 >(  m_rContext->getServiceManager()->createInstanceWithContext( "com.sun.star.awt.Toolkit2", m_rContext ), uno::UNO_QUERY_THROW );

            m_xController = new MyXController();
        }
    }

    // XDocument
    virtual sal_Int32 SAL_CALL
    getNumberOfPages()
        throw ( uno::RuntimeException )
    {
        uno::Any selection;
        selection <<= m_xComponent;

        uno::Reference< awt::XDevice > device;
        uno::Reference< view::XRenderable > renderable;

        beans::PropertyValues renderProps;

        device = m_xToolkit->createScreenCompatibleDevice( 128, 128 );

        renderable = uno::Reference< view::XRenderable >( m_rContext->getServiceManager()->createInstanceWithContext( "com.sun.star.view.Renderable", m_rContext ), uno::UNO_QUERY_THROW );

        renderProps.realloc( 3 );
        renderProps[0].Name = "IsPrinter";
        renderProps[0].Value <<= sal_Bool(true);
        renderProps[1].Name = "RenderDevice";
        renderProps[1].Value <<= device;
        renderProps[2].Name = "View";
        renderProps[2].Value <<= m_xController;

        return renderable->getRendererCount( selection, renderProps );
    }

    virtual void SAL_CALL
    render( sal_Int64 buffer,
            sal_Int32 width,
            sal_Int32 height,
            const uno::Reference< XDocumentRenderCallback >& listener,
            sal_Int32 pageNo,
            sal_Int32 zoomLevel,
            sal_Int32 x,
            sal_Int32 y )
        throw ( lang::IllegalArgumentException, uno::RuntimeException)
    {
        (void) listener;
        (void) zoomLevel;
        (void) x;
        (void) y;

        uno::Any selection;

        selection <<= m_xComponent;

        uno::Reference< awt::XDevice > device( m_xToolkit->createScreenCompatibleDeviceUsingBuffer( width, height, buffer ) );

        beans::PropertyValues renderProps;

        renderProps.realloc( 3 );
        renderProps[0].Name = "IsPrinter";
        renderProps[0].Value <<= sal_Bool(true);
        renderProps[1].Name = "RenderDevice";
        renderProps[1].Value <<= device;
        renderProps[2].Name = "View";
        renderProps[2].Value <<= m_xController;

        uno::Reference< view::XRenderable > renderable( m_rContext->getServiceManager()->createInstanceWithContext( "com.sun.star.view.Renderable", m_rContext ), uno::UNO_QUERY_THROW );

        renderable->render( pageNo, selection, renderProps );
    }

};

} } } // namespace org::libreoffice::touch

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