Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / embeddedobj / test / Container1 / BitmapPainter.java
blob759536d5f6b0ae7864ff394d96d9088377f6edc0
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 package embeddedobj.test;
20 import java.awt.*;
21 import java.applet.*;
22 import java.awt.event.*;
23 import java.net.*;
24 import java.io.*;
26 import com.sun.star.awt.XBitmap;
27 import com.sun.star.awt.XDevice;
28 import com.sun.star.awt.XDisplayBitmap;
29 import com.sun.star.awt.XGraphics;
30 import com.sun.star.awt.XWindow;
31 import com.sun.star.awt.XWindowPeer;
32 import com.sun.star.awt.XToolkit;
33 import com.sun.star.awt.XSystemChildFactory;
34 import com.sun.star.awt.WindowDescriptor;
35 import com.sun.star.awt.WindowClass;
36 import com.sun.star.awt.WindowAttribute;
38 import com.sun.star.awt.XPaintListener;
39 import com.sun.star.awt.PaintEvent;
40 import com.sun.star.awt.XMouseListener;
41 import com.sun.star.awt.XMouseMotionListener;
42 import com.sun.star.awt.MouseEvent;
43 import com.sun.star.awt.Point;
45 import com.sun.star.uno.UnoRuntime;
46 import com.sun.star.uno.Any;
47 import com.sun.star.lang.XMultiServiceFactory;
49 import com.sun.star.task.XJob;
50 import com.sun.star.beans.NamedValue;
53 class BitmapPainter implements XPaintListener, XMouseListener, XMouseMotionListener, XJob
55 private XWindow m_xWindow;
56 private XBitmap m_xBitmap;
58 private com.sun.star.awt.Rectangle m_aDrawRect;
60 private Object m_oImageLock;
62 private PaintThread m_aPaintThread;
64 private boolean m_bFree = true;
66 private boolean m_bProceedWithPainting = true;
68 // Methods
70 public BitmapPainter( XJob xJob, XWindow xWindow, XBitmap xBitmap, com.sun.star.awt.Rectangle aDrawRect )
72 if ( xJob == null )
74 System.out.println( "No mainthreadexecutor is provided to BitmapPainter on init!" );
75 throw new com.sun.star.uno.RuntimeException();
78 if ( xWindow == null )
80 System.out.println( "No window is provided to BitmapPainter on init!" );
81 throw new com.sun.star.uno.RuntimeException();
84 m_xWindow = xWindow;
85 m_xBitmap = xBitmap;
87 m_aDrawRect = aDrawRect;
89 m_oImageLock = new Object();
91 m_aPaintThread = new PaintThread( m_xWindow );
92 m_aPaintThread.start();
94 m_xWindow.addPaintListener( this );
95 m_xWindow.addMouseListener( this );
96 m_xWindow.addMouseMotionListener( this );
100 public void disconnectListener()
102 m_aPaintThread.disposeThread();
103 m_xWindow.removePaintListener( this );
104 m_xWindow.removeMouseListener( this );
105 m_xWindow.removeMouseMotionListener( this );
109 public void setBitmap( XBitmap xBitmap )
111 synchronized( m_oImageLock )
113 m_xBitmap = xBitmap;
118 public void setPos( com.sun.star.awt.Point aPoint )
120 synchronized( m_oImageLock )
122 m_aDrawRect.X = aPoint.X;
123 m_aDrawRect.Y = aPoint.Y;
128 public void setRect( com.sun.star.awt.Rectangle aRect )
130 synchronized( m_oImageLock )
132 m_aDrawRect = aRect;
137 public void setSize( com.sun.star.awt.Size aSize )
139 synchronized( m_oImageLock )
141 m_aDrawRect.Width = aSize.Width;
142 m_aDrawRect.Height = aSize.Height;
147 public void stopPainting()
149 m_bProceedWithPainting = false;
153 public void startPainting()
155 m_bProceedWithPainting = true;
158 // XPaintListener
160 public void windowPaint( PaintEvent e )
162 if ( !m_bProceedWithPainting )
163 return;
165 XBitmap xBitmap = null;
166 com.sun.star.awt.Rectangle aRect = null;
168 synchronized( m_oImageLock )
170 xBitmap = m_xBitmap;
171 aRect = m_aDrawRect;
174 m_aPaintThread.setPaintRequest( xBitmap, aRect, e.UpdateRect );
176 System.out.println( "VCL window paint event!" );
179 // XMouseListener
181 public void mousePressed( MouseEvent e )
186 public void mouseReleased( MouseEvent e )
191 public void mouseEntered( MouseEvent e )
196 public void mouseExited( MouseEvent e )
200 // XMouseMotionListener
202 public void mouseDragged( MouseEvent e )
204 // TODO: react to resizing of object bitmap
205 // if the object is inplace active the object must control resizing
209 public void mouseMoved( MouseEvent e )
214 // XEventListener
216 public void disposing( com.sun.star.lang.EventObject e )
218 // do nothing, the window can die only when the application is closed
221 // XJob
223 public Object execute( NamedValue[] pValues )
226 // means request for painting
228 XBitmap xBitmap = null;
229 com.sun.star.awt.Rectangle aRect = null;
231 synchronized( m_oImageLock )
233 xBitmap = m_xBitmap;
234 aRect = m_aDrawRect;
237 System.out.println( "The bitmap is going to be painted!" );
239 try {
240 XDevice xDevice = (XDevice)UnoRuntime.queryInterface( XDevice.class, m_xWindow );
241 if ( xDevice != null )
243 System.out.println( "Step1" );
244 XGraphics xGraphics = xDevice.createGraphics();
245 if ( xBitmap != null )
247 System.out.println( "Step2" );
248 XDisplayBitmap xDisplayBitmap = xDevice.createDisplayBitmap( xBitmap );
250 com.sun.star.awt.Size aSize = xBitmap.getSize();
251 xGraphics.draw( xDisplayBitmap, 0, 0, aSize.Width, aSize.Height,
252 aRect.X, aRect.Y, aRect.Width, aRect.Height );
255 System.out.println( "Step3" );
256 xGraphics.drawRect( aRect.X - 1, aRect.Y - 1, aRect.Width + 2, aRect.Height + 2 );
258 // draw resize squares
259 System.out.println( "Step4" );
260 xGraphics.drawRect( aRect.X - 2, aRect.Y - 2, 4, 4 );
261 xGraphics.drawRect( aRect.X + aRect.Width - 2, aRect.Y - 2, 4, 4 );
262 xGraphics.drawRect( aRect.X - 2, aRect.Y + aRect.Height - 2, 4, 4 );
263 xGraphics.drawRect( aRect.X + aRect.Width - 2, aRect.Y + aRect.Height - 2, 4, 4 );
265 System.out.println( "Step5" );
267 System.out.println( "The bitmap is painted by BitmapPainter!" );
270 catch ( Exception e )
274 m_bFree = true;
277 return Any.VOID;