Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / embeddedobj / test / Container1 / BitmapPainter.java
blob6f688c8f99bce15d11ae8ad7bca48daf1f8dfa25
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 XJob m_xMainThreadExecutor;
65 // private NamedValue[] m_pValuesForExecutor;
67 private boolean m_bFree = true;
69 private boolean m_bProceedWithPainting = true;
71 // Methods
72 //------------------------------------------------------
73 public BitmapPainter( XJob xJob, XWindow xWindow, XBitmap xBitmap, com.sun.star.awt.Rectangle aDrawRect )
75 if ( xJob == null )
77 System.out.println( "No mainthreadexecutor is provided to BimapPainter on init!" );
78 throw new com.sun.star.uno.RuntimeException();
81 if ( xWindow == null )
83 System.out.println( "No window is provided to BimapPainter on init!" );
84 throw new com.sun.star.uno.RuntimeException();
87 // m_xMainThreadExecutor = xJob;
88 // m_pValuesForExecutor = new NamedValue[1];
89 // m_pValuesForExecutor[0] = new NamedValue( "JobToExecute", (Object)this );
91 m_xWindow = xWindow;
92 m_xBitmap = xBitmap;
94 m_aDrawRect = aDrawRect;
96 m_oImageLock = new Object();
98 m_aPaintThread = new PaintThread( m_xWindow );
99 m_aPaintThread.start();
101 m_xWindow.addPaintListener( this );
102 m_xWindow.addMouseListener( this );
103 m_xWindow.addMouseMotionListener( this );
106 //------------------------------------------------------
107 public void disconnectListener()
109 m_aPaintThread.disposeThread();
110 m_xWindow.removePaintListener( this );
111 m_xWindow.removeMouseListener( this );
112 m_xWindow.removeMouseMotionListener( this );
115 //------------------------------------------------------
116 public void setBitmap( XBitmap xBitmap )
118 synchronized( m_oImageLock )
120 m_xBitmap = xBitmap;
124 //------------------------------------------------------
125 public void setPos( com.sun.star.awt.Point aPoint )
127 synchronized( m_oImageLock )
129 m_aDrawRect.X = aPoint.X;
130 m_aDrawRect.Y = aPoint.Y;
134 //------------------------------------------------------
135 public void setRect( com.sun.star.awt.Rectangle aRect )
137 synchronized( m_oImageLock )
139 m_aDrawRect = aRect;
143 //------------------------------------------------------
144 public void setSize( com.sun.star.awt.Size aSize )
146 synchronized( m_oImageLock )
148 m_aDrawRect.Width = aSize.Width;
149 m_aDrawRect.Height = aSize.Height;
153 //------------------------------------------------------
154 public void stopPainting()
156 m_bProceedWithPainting = false;
159 //------------------------------------------------------
160 public void startPainting()
162 m_bProceedWithPainting = true;
165 // XPaintListener
166 //------------------------------------------------------
167 public void windowPaint( PaintEvent e )
169 if ( !m_bProceedWithPainting )
170 return;
172 XBitmap xBitmap = null;
173 com.sun.star.awt.Rectangle aRect = null;
174 // boolean bFree = false;
176 synchronized( m_oImageLock )
178 xBitmap = m_xBitmap;
179 aRect = m_aDrawRect;
180 // if ( m_bFree )
181 // {
182 // bFree = true;
183 // m_bFree = false;
184 // }
187 m_aPaintThread.setPaintRequest( xBitmap, aRect, e.UpdateRect );
188 // if ( bFree )
189 // {
190 // try {
191 // m_xMainThreadExecutor.execute( m_pValuesForExecutor );
192 // } catch( Exception ex )
193 // {
194 // m_bFree = true;
195 // }
196 // }
198 System.out.println( "VCL window paint event!" );
201 // XMouseListener
202 //------------------------------------------------------
203 public void mousePressed( MouseEvent e )
207 //------------------------------------------------------
208 public void mouseReleased( MouseEvent e )
212 //------------------------------------------------------
213 public void mouseEntered( MouseEvent e )
217 //------------------------------------------------------
218 public void mouseExited( MouseEvent e )
222 // XMouseMotionListener
223 //------------------------------------------------------
224 public void mouseDragged( MouseEvent e )
226 // TODO: react to resizing of object bitmap
227 // if the object is inplace active the object must control resizing
230 //------------------------------------------------------
231 public void mouseMoved( MouseEvent e )
236 // XEventListener
237 //------------------------------------------------------
238 public void disposing( com.sun.star.lang.EventObject e )
240 // do nothing, the window can die only when the application is closed
243 // XJob
244 //------------------------------------------------------
245 public Object execute( NamedValue[] pValues )
248 // means request for painting
250 XBitmap xBitmap = null;
251 com.sun.star.awt.Rectangle aRect = null;
253 synchronized( m_oImageLock )
255 xBitmap = m_xBitmap;
256 aRect = m_aDrawRect;
259 System.out.println( "The bitmap is going to be painted!" );
261 try {
262 XDevice xDevice = (XDevice)UnoRuntime.queryInterface( XDevice.class, m_xWindow );
263 if ( xDevice != null )
265 System.out.println( "Step1" );
266 XGraphics xGraphics = xDevice.createGraphics();
267 if ( xBitmap != null )
269 System.out.println( "Step2" );
270 XDisplayBitmap xDisplayBitmap = xDevice.createDisplayBitmap( xBitmap );
272 com.sun.star.awt.Size aSize = xBitmap.getSize();
273 xGraphics.draw( xDisplayBitmap, 0, 0, aSize.Width, aSize.Height,
274 aRect.X, aRect.Y, aRect.Width, aRect.Height );
277 System.out.println( "Step3" );
278 xGraphics.drawRect( aRect.X - 1, aRect.Y - 1, aRect.Width + 2, aRect.Height + 2 );
280 // draw resize squares
281 System.out.println( "Step4" );
282 xGraphics.drawRect( aRect.X - 2, aRect.Y - 2, 4, 4 );
283 xGraphics.drawRect( aRect.X + aRect.Width - 2, aRect.Y - 2, 4, 4 );
284 xGraphics.drawRect( aRect.X - 2, aRect.Y + aRect.Height - 2, 4, 4 );
285 xGraphics.drawRect( aRect.X + aRect.Width - 2, aRect.Y + aRect.Height - 2, 4, 4 );
287 System.out.println( "Step5" );
289 System.out.println( "The bitmap is painted by BitmapPainter!" );
292 catch ( Exception e )
296 m_bFree = true;
299 return Any.VOID;