summaryrefslogtreecommitdiff
path: root/embeddedobj/test/Container1/BitmapPainter.java
blob: 482421bd52ae5fddc7b6886ee9d2497a907b9376 (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
/*
 * 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 .
 */
package embeddedobj.test;

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.net.*;
import java.io.*;

import com.sun.star.awt.XBitmap;
import com.sun.star.awt.XDevice;
import com.sun.star.awt.XDisplayBitmap;
import com.sun.star.awt.XGraphics;
import com.sun.star.awt.XWindow;
import com.sun.star.awt.XWindowPeer;
import com.sun.star.awt.XToolkit;
import com.sun.star.awt.XSystemChildFactory;
import com.sun.star.awt.WindowDescriptor;
import com.sun.star.awt.WindowClass;
import com.sun.star.awt.WindowAttribute;

import com.sun.star.awt.XPaintListener;
import com.sun.star.awt.PaintEvent;
import com.sun.star.awt.XMouseListener;
import com.sun.star.awt.XMouseMotionListener;
import com.sun.star.awt.MouseEvent;
import com.sun.star.awt.Point;

import com.sun.star.uno.UnoRuntime;
import com.sun.star.uno.Any;
import com.sun.star.lang.XMultiServiceFactory;

import com.sun.star.task.XJob;
import com.sun.star.beans.NamedValue;


class BitmapPainter implements XPaintListener, XMouseListener, XMouseMotionListener, XJob
{
    private XWindow m_xWindow;
    private XBitmap m_xBitmap;

    private com.sun.star.awt.Rectangle m_aDrawRect;

    private Object m_oImageLock;

    private PaintThread m_aPaintThread;

    private boolean m_bFree = true;

    private boolean m_bProceedWithPainting = true;

// Methods

    public BitmapPainter( XJob xJob, XWindow xWindow, XBitmap xBitmap, com.sun.star.awt.Rectangle aDrawRect )
    {
        if ( xJob == null )
        {
            System.out.println( "No mainthreadexecutor is provided to BimapPainter on init!" );
            throw new com.sun.star.uno.RuntimeException();
        }

        if ( xWindow == null )
        {
            System.out.println( "No window is provided to BimapPainter on init!" );
            throw new com.sun.star.uno.RuntimeException();
        }

        m_xWindow = xWindow;
        m_xBitmap = xBitmap;

        m_aDrawRect = aDrawRect;

        m_oImageLock = new Object();

        m_aPaintThread = new PaintThread( m_xWindow );
        m_aPaintThread.start();

        m_xWindow.addPaintListener( this );
        m_xWindow.addMouseListener( this );
        m_xWindow.addMouseMotionListener( this );
    }


    public void disconnectListener()
    {
        m_aPaintThread.disposeThread();
        m_xWindow.removePaintListener( this );
        m_xWindow.removeMouseListener( this );
        m_xWindow.removeMouseMotionListener( this );
    }


    public void setBitmap( XBitmap xBitmap )
    {
        synchronized( m_oImageLock )
        {
            m_xBitmap = xBitmap;
        }
    }


    public void setPos( com.sun.star.awt.Point aPoint )
    {
        synchronized( m_oImageLock )
        {
            m_aDrawRect.X = aPoint.X;
            m_aDrawRect.Y = aPoint.Y;
        }
    }


    public void setRect( com.sun.star.awt.Rectangle aRect )
    {
        synchronized( m_oImageLock )
        {
            m_aDrawRect = aRect;
        }
    }


    public void setSize( com.sun.star.awt.Size aSize )
    {
        synchronized( m_oImageLock )
        {
            m_aDrawRect.Width = aSize.Width;
            m_aDrawRect.Height = aSize.Height;
        }
    }


    public void stopPainting()
    {
        m_bProceedWithPainting = false;
    }


    public void startPainting()
    {
        m_bProceedWithPainting = true;
    }

    // XPaintListener

    public void windowPaint( PaintEvent e )
    {
        if ( !m_bProceedWithPainting )
            return;

        XBitmap xBitmap = null;
        com.sun.star.awt.Rectangle aRect = null;

        synchronized( m_oImageLock )
        {
            xBitmap = m_xBitmap;
            aRect = m_aDrawRect;
        }

        m_aPaintThread.setPaintRequest( xBitmap, aRect, e.UpdateRect );

        System.out.println( "VCL window paint event!" );
    }

    // XMouseListener

    public void mousePressed( MouseEvent e )
    {
    }


    public void mouseReleased( MouseEvent e )
    {
    }


    public void mouseEntered( MouseEvent e )
    {
    }


    public void mouseExited( MouseEvent e )
    {
    }

    // XMouseMotionListener

    public void mouseDragged( MouseEvent e )
    {
        // TODO: react to resizing of object bitmap
        // if the object is inplace active the object must control resizing
    }


    public void mouseMoved( MouseEvent e )
    {

    }

    // XEventListener

    public void disposing( com.sun.star.lang.EventObject e )
    {
        // do nothing, the window can die only when the application is closed
    }

    // XJob

    public Object execute( NamedValue[] pValues )
    {
/*
        // means request for painting

        XBitmap xBitmap = null;
        com.sun.star.awt.Rectangle aRect = null;

        synchronized( m_oImageLock )
        {
            xBitmap = m_xBitmap;
            aRect = m_aDrawRect;
        }

        System.out.println( "The bitmap is going to be painted!" );

        try {
            XDevice xDevice = (XDevice)UnoRuntime.queryInterface( XDevice.class, m_xWindow );
            if ( xDevice != null )
            {
                System.out.println( "Step1" );
                XGraphics xGraphics = xDevice.createGraphics();
                if ( xBitmap != null )
                {
                    System.out.println( "Step2" );
                    XDisplayBitmap xDisplayBitmap = xDevice.createDisplayBitmap( xBitmap );

                    com.sun.star.awt.Size aSize = xBitmap.getSize();
                    xGraphics.draw( xDisplayBitmap, 0, 0, aSize.Width, aSize.Height,
                                                aRect.X, aRect.Y, aRect.Width, aRect.Height );
                }

                System.out.println( "Step3" );
                xGraphics.drawRect( aRect.X - 1, aRect.Y - 1, aRect.Width + 2, aRect.Height + 2 );

                // draw resize squares
                System.out.println( "Step4" );
                xGraphics.drawRect( aRect.X - 2, aRect.Y - 2, 4, 4 );
                xGraphics.drawRect( aRect.X + aRect.Width - 2, aRect.Y - 2, 4, 4 );
                xGraphics.drawRect( aRect.X - 2, aRect.Y + aRect.Height - 2, 4, 4 );
                xGraphics.drawRect( aRect.X + aRect.Width - 2, aRect.Y + aRect.Height - 2, 4, 4 );

                System.out.println( "Step5" );

                System.out.println( "The bitmap is painted by BitmapPainter!" );
            }
        }
        catch ( Exception e )
        {
        }

        m_bFree = true;

*/
        return Any.VOID;
    }

};