1*113d1ee9SAndrew Rist /**************************************************************
2*113d1ee9SAndrew Rist  *
3*113d1ee9SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*113d1ee9SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*113d1ee9SAndrew Rist  * distributed with this work for additional information
6*113d1ee9SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*113d1ee9SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*113d1ee9SAndrew Rist  * "License"); you may not use this file except in compliance
9*113d1ee9SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*113d1ee9SAndrew Rist  *
11*113d1ee9SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*113d1ee9SAndrew Rist  *
13*113d1ee9SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*113d1ee9SAndrew Rist  * software distributed under the License is distributed on an
15*113d1ee9SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*113d1ee9SAndrew Rist  * KIND, either express or implied.  See the License for the
17*113d1ee9SAndrew Rist  * specific language governing permissions and limitations
18*113d1ee9SAndrew Rist  * under the License.
19*113d1ee9SAndrew Rist  *
20*113d1ee9SAndrew Rist  *************************************************************/
21*113d1ee9SAndrew Rist 
22cdf0e10cSrcweir package embeddedobj.test;
23cdf0e10cSrcweir 
24cdf0e10cSrcweir import java.awt.*;
25cdf0e10cSrcweir import java.applet.*;
26cdf0e10cSrcweir import java.awt.event.*;
27cdf0e10cSrcweir import java.net.*;
28cdf0e10cSrcweir import java.io.*;
29cdf0e10cSrcweir 
30cdf0e10cSrcweir import com.sun.star.awt.XBitmap;
31cdf0e10cSrcweir import com.sun.star.awt.XDevice;
32cdf0e10cSrcweir import com.sun.star.awt.XDisplayBitmap;
33cdf0e10cSrcweir import com.sun.star.awt.XGraphics;
34cdf0e10cSrcweir import com.sun.star.awt.XWindow;
35cdf0e10cSrcweir import com.sun.star.awt.XWindowPeer;
36cdf0e10cSrcweir import com.sun.star.awt.XToolkit;
37cdf0e10cSrcweir import com.sun.star.awt.XSystemChildFactory;
38cdf0e10cSrcweir import com.sun.star.awt.WindowDescriptor;
39cdf0e10cSrcweir import com.sun.star.awt.WindowClass;
40cdf0e10cSrcweir import com.sun.star.awt.WindowAttribute;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir import com.sun.star.awt.XPaintListener;
43cdf0e10cSrcweir import com.sun.star.awt.PaintEvent;
44cdf0e10cSrcweir import com.sun.star.awt.XMouseListener;
45cdf0e10cSrcweir import com.sun.star.awt.XMouseMotionListener;
46cdf0e10cSrcweir import com.sun.star.awt.MouseEvent;
47cdf0e10cSrcweir import com.sun.star.awt.Point;
48cdf0e10cSrcweir 
49cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
50cdf0e10cSrcweir import com.sun.star.uno.Any;
51cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir import com.sun.star.task.XJob;
54cdf0e10cSrcweir import com.sun.star.beans.NamedValue;
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 
57cdf0e10cSrcweir class BitmapPainter implements XPaintListener, XMouseListener, XMouseMotionListener, XJob
58cdf0e10cSrcweir {
59cdf0e10cSrcweir 	private XWindow m_xWindow;
60cdf0e10cSrcweir 	private XBitmap m_xBitmap;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir 	private com.sun.star.awt.Rectangle m_aDrawRect;
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	private Object m_oImageLock;
65cdf0e10cSrcweir 
66cdf0e10cSrcweir 	private PaintThread m_aPaintThread;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 	// private XJob m_xMainThreadExecutor;
69cdf0e10cSrcweir 	// private NamedValue[] m_pValuesForExecutor;
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 	private boolean m_bFree = true;
72cdf0e10cSrcweir 
73cdf0e10cSrcweir 	private boolean m_bProceedWithPainting = true;
74cdf0e10cSrcweir 
75cdf0e10cSrcweir // Methods
76cdf0e10cSrcweir 	//------------------------------------------------------
BitmapPainter( XJob xJob, XWindow xWindow, XBitmap xBitmap, com.sun.star.awt.Rectangle aDrawRect )77cdf0e10cSrcweir 	public BitmapPainter( XJob xJob, XWindow xWindow, XBitmap xBitmap, com.sun.star.awt.Rectangle aDrawRect )
78cdf0e10cSrcweir 	{
79cdf0e10cSrcweir 		if ( xJob == null )
80cdf0e10cSrcweir 		{
81cdf0e10cSrcweir 			System.out.println( "No mainthreadexecutor is provided to BimapPainter on init!" );
82cdf0e10cSrcweir 			throw new com.sun.star.uno.RuntimeException();
83cdf0e10cSrcweir 		}
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 		if ( xWindow == null )
86cdf0e10cSrcweir 		{
87cdf0e10cSrcweir 			System.out.println( "No window is provided to BimapPainter on init!" );
88cdf0e10cSrcweir 			throw new com.sun.star.uno.RuntimeException();
89cdf0e10cSrcweir 		}
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 		// m_xMainThreadExecutor = xJob;
92cdf0e10cSrcweir 		// m_pValuesForExecutor = new NamedValue[1];
93cdf0e10cSrcweir 		// m_pValuesForExecutor[0] = new NamedValue( "JobToExecute", (Object)this );
94cdf0e10cSrcweir 
95cdf0e10cSrcweir 		m_xWindow = xWindow;
96cdf0e10cSrcweir 		m_xBitmap = xBitmap;
97cdf0e10cSrcweir 
98cdf0e10cSrcweir 		m_aDrawRect = aDrawRect;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir 		m_oImageLock = new Object();
101cdf0e10cSrcweir 
102cdf0e10cSrcweir 		m_aPaintThread = new PaintThread( m_xWindow );
103cdf0e10cSrcweir 		m_aPaintThread.start();
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 		m_xWindow.addPaintListener( this );
106cdf0e10cSrcweir 		m_xWindow.addMouseListener( this );
107cdf0e10cSrcweir 		m_xWindow.addMouseMotionListener( this );
108cdf0e10cSrcweir 	}
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	//------------------------------------------------------
disconnectListener()111cdf0e10cSrcweir 	public void disconnectListener()
112cdf0e10cSrcweir 	{
113cdf0e10cSrcweir 		m_aPaintThread.disposeThread();
114cdf0e10cSrcweir 		m_xWindow.removePaintListener( this );
115cdf0e10cSrcweir 		m_xWindow.removeMouseListener( this );
116cdf0e10cSrcweir 		m_xWindow.removeMouseMotionListener( this );
117cdf0e10cSrcweir 	}
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 	//------------------------------------------------------
setBitmap( XBitmap xBitmap )120cdf0e10cSrcweir 	public void setBitmap( XBitmap xBitmap )
121cdf0e10cSrcweir 	{
122cdf0e10cSrcweir 		synchronized( m_oImageLock )
123cdf0e10cSrcweir 		{
124cdf0e10cSrcweir 			m_xBitmap = xBitmap;
125cdf0e10cSrcweir 		}
126cdf0e10cSrcweir 	}
127cdf0e10cSrcweir 
128cdf0e10cSrcweir 	//------------------------------------------------------
setPos( com.sun.star.awt.Point aPoint )129cdf0e10cSrcweir 	public void setPos( com.sun.star.awt.Point aPoint )
130cdf0e10cSrcweir 	{
131cdf0e10cSrcweir 		synchronized( m_oImageLock )
132cdf0e10cSrcweir 		{
133cdf0e10cSrcweir 			m_aDrawRect.X = aPoint.X;
134cdf0e10cSrcweir 			m_aDrawRect.Y = aPoint.Y;
135cdf0e10cSrcweir 		}
136cdf0e10cSrcweir 	}
137cdf0e10cSrcweir 
138cdf0e10cSrcweir 	//------------------------------------------------------
setRect( com.sun.star.awt.Rectangle aRect )139cdf0e10cSrcweir 	public void setRect( com.sun.star.awt.Rectangle aRect )
140cdf0e10cSrcweir 	{
141cdf0e10cSrcweir 		synchronized( m_oImageLock )
142cdf0e10cSrcweir 		{
143cdf0e10cSrcweir 			m_aDrawRect = aRect;
144cdf0e10cSrcweir 		}
145cdf0e10cSrcweir 	}
146cdf0e10cSrcweir 
147cdf0e10cSrcweir 	//------------------------------------------------------
setSize( com.sun.star.awt.Size aSize )148cdf0e10cSrcweir 	public void setSize( com.sun.star.awt.Size aSize )
149cdf0e10cSrcweir 	{
150cdf0e10cSrcweir 		synchronized( m_oImageLock )
151cdf0e10cSrcweir 		{
152cdf0e10cSrcweir 			m_aDrawRect.Width = aSize.Width;
153cdf0e10cSrcweir 			m_aDrawRect.Height = aSize.Height;
154cdf0e10cSrcweir 		}
155cdf0e10cSrcweir 	}
156cdf0e10cSrcweir 
157cdf0e10cSrcweir 	//------------------------------------------------------
stopPainting()158cdf0e10cSrcweir 	public void stopPainting()
159cdf0e10cSrcweir 	{
160cdf0e10cSrcweir 		m_bProceedWithPainting = false;
161cdf0e10cSrcweir 	}
162cdf0e10cSrcweir 
163cdf0e10cSrcweir 	//------------------------------------------------------
startPainting()164cdf0e10cSrcweir 	public void startPainting()
165cdf0e10cSrcweir 	{
166cdf0e10cSrcweir 		m_bProceedWithPainting = true;
167cdf0e10cSrcweir 	}
168cdf0e10cSrcweir 
169cdf0e10cSrcweir 	// XPaintListener
170cdf0e10cSrcweir 	//------------------------------------------------------
windowPaint( PaintEvent e )171cdf0e10cSrcweir 	public void windowPaint( PaintEvent e )
172cdf0e10cSrcweir 	{
173cdf0e10cSrcweir 		if ( !m_bProceedWithPainting )
174cdf0e10cSrcweir 			return;
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 		XBitmap xBitmap = null;
177cdf0e10cSrcweir 		com.sun.star.awt.Rectangle aRect = null;
178cdf0e10cSrcweir 		// boolean bFree = false;
179cdf0e10cSrcweir 
180cdf0e10cSrcweir 		synchronized( m_oImageLock )
181cdf0e10cSrcweir 		{
182cdf0e10cSrcweir 			xBitmap = m_xBitmap;
183cdf0e10cSrcweir 			aRect = m_aDrawRect;
184cdf0e10cSrcweir 			// if ( m_bFree )
185cdf0e10cSrcweir 			// {
186cdf0e10cSrcweir 				// bFree = true;
187cdf0e10cSrcweir 				// m_bFree = false;
188cdf0e10cSrcweir 			// }
189cdf0e10cSrcweir 		}
190cdf0e10cSrcweir 
191cdf0e10cSrcweir 		m_aPaintThread.setPaintRequest( xBitmap, aRect, e.UpdateRect );
192cdf0e10cSrcweir 		// if ( bFree )
193cdf0e10cSrcweir 		// {
194cdf0e10cSrcweir 			// try {
195cdf0e10cSrcweir 				// m_xMainThreadExecutor.execute( m_pValuesForExecutor );
196cdf0e10cSrcweir 			// } catch( Exception ex )
197cdf0e10cSrcweir 			// {
198cdf0e10cSrcweir 				// m_bFree = true;
199cdf0e10cSrcweir 			// }
200cdf0e10cSrcweir 		// }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir 		System.out.println( "VCL window paint event!" );
203cdf0e10cSrcweir 	}
204cdf0e10cSrcweir 
205cdf0e10cSrcweir 	// XMouseListener
206cdf0e10cSrcweir 	//------------------------------------------------------
mousePressed( MouseEvent e )207cdf0e10cSrcweir 	public void mousePressed( MouseEvent e )
208cdf0e10cSrcweir 	{
209cdf0e10cSrcweir 	}
210cdf0e10cSrcweir 
211cdf0e10cSrcweir 	//------------------------------------------------------
mouseReleased( MouseEvent e )212cdf0e10cSrcweir 	public void mouseReleased( MouseEvent e )
213cdf0e10cSrcweir 	{
214cdf0e10cSrcweir 	}
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 	//------------------------------------------------------
mouseEntered( MouseEvent e )217cdf0e10cSrcweir 	public void mouseEntered( MouseEvent e )
218cdf0e10cSrcweir 	{
219cdf0e10cSrcweir 	}
220cdf0e10cSrcweir 
221cdf0e10cSrcweir 	//------------------------------------------------------
mouseExited( MouseEvent e )222cdf0e10cSrcweir 	public void mouseExited( MouseEvent e )
223cdf0e10cSrcweir 	{
224cdf0e10cSrcweir 	}
225cdf0e10cSrcweir 
226cdf0e10cSrcweir 	// XMouseMotionListener
227cdf0e10cSrcweir 	//------------------------------------------------------
mouseDragged( MouseEvent e )228cdf0e10cSrcweir 	public void mouseDragged( MouseEvent e )
229cdf0e10cSrcweir 	{
230cdf0e10cSrcweir 		// TODO: react to resizing of object bitmap
231cdf0e10cSrcweir 		// if the object is inplace active the object must control resizing
232cdf0e10cSrcweir 	}
233cdf0e10cSrcweir 
234cdf0e10cSrcweir 	//------------------------------------------------------
mouseMoved( MouseEvent e )235cdf0e10cSrcweir 	public void mouseMoved( MouseEvent e )
236cdf0e10cSrcweir 	{
237cdf0e10cSrcweir 
238cdf0e10cSrcweir 	}
239cdf0e10cSrcweir 
240cdf0e10cSrcweir 	// XEventListener
241cdf0e10cSrcweir 	//------------------------------------------------------
disposing( com.sun.star.lang.EventObject e )242cdf0e10cSrcweir 	public void disposing( com.sun.star.lang.EventObject e )
243cdf0e10cSrcweir 	{
244cdf0e10cSrcweir 		// do nothing, the window can die only when the application is closed
245cdf0e10cSrcweir 	}
246cdf0e10cSrcweir 
247cdf0e10cSrcweir 	// XJob
248cdf0e10cSrcweir 	//------------------------------------------------------
execute( NamedValue[] pValues )249cdf0e10cSrcweir 	public Object execute( NamedValue[] pValues )
250cdf0e10cSrcweir 	{
251cdf0e10cSrcweir /*
252cdf0e10cSrcweir 		// means request for painting
253cdf0e10cSrcweir 
254cdf0e10cSrcweir 		XBitmap xBitmap = null;
255cdf0e10cSrcweir 		com.sun.star.awt.Rectangle aRect = null;
256cdf0e10cSrcweir 
257cdf0e10cSrcweir 		synchronized( m_oImageLock )
258cdf0e10cSrcweir 		{
259cdf0e10cSrcweir 			xBitmap = m_xBitmap;
260cdf0e10cSrcweir 			aRect = m_aDrawRect;
261cdf0e10cSrcweir 		}
262cdf0e10cSrcweir 
263cdf0e10cSrcweir 		System.out.println( "The bitmap is going to be painted!" );
264cdf0e10cSrcweir 
265cdf0e10cSrcweir 		try {
266cdf0e10cSrcweir 			XDevice xDevice = (XDevice)UnoRuntime.queryInterface( XDevice.class, m_xWindow );
267cdf0e10cSrcweir 			if ( xDevice != null )
268cdf0e10cSrcweir 			{
269cdf0e10cSrcweir 				System.out.println( "Step1" );
270cdf0e10cSrcweir 				XGraphics xGraphics = xDevice.createGraphics();
271cdf0e10cSrcweir 				if ( xBitmap != null )
272cdf0e10cSrcweir 				{
273cdf0e10cSrcweir 					System.out.println( "Step2" );
274cdf0e10cSrcweir 					XDisplayBitmap xDisplayBitmap = xDevice.createDisplayBitmap( xBitmap );
275cdf0e10cSrcweir 
276cdf0e10cSrcweir 					com.sun.star.awt.Size aSize = xBitmap.getSize();
277cdf0e10cSrcweir 					xGraphics.draw( xDisplayBitmap, 0, 0, aSize.Width, aSize.Height,
278cdf0e10cSrcweir 												aRect.X, aRect.Y, aRect.Width, aRect.Height );
279cdf0e10cSrcweir 				}
280cdf0e10cSrcweir 
281cdf0e10cSrcweir 				System.out.println( "Step3" );
282cdf0e10cSrcweir 				xGraphics.drawRect( aRect.X - 1, aRect.Y - 1, aRect.Width + 2, aRect.Height + 2 );
283cdf0e10cSrcweir 
284cdf0e10cSrcweir 				// draw resize squares
285cdf0e10cSrcweir 				System.out.println( "Step4" );
286cdf0e10cSrcweir 				xGraphics.drawRect( aRect.X - 2, aRect.Y - 2, 4, 4 );
287cdf0e10cSrcweir 				xGraphics.drawRect( aRect.X + aRect.Width - 2, aRect.Y - 2, 4, 4 );
288cdf0e10cSrcweir 				xGraphics.drawRect( aRect.X - 2, aRect.Y + aRect.Height - 2, 4, 4 );
289cdf0e10cSrcweir 				xGraphics.drawRect( aRect.X + aRect.Width - 2, aRect.Y + aRect.Height - 2, 4, 4 );
290cdf0e10cSrcweir 
291cdf0e10cSrcweir 				System.out.println( "Step5" );
292cdf0e10cSrcweir 
293cdf0e10cSrcweir 				System.out.println( "The bitmap is painted by BitmapPainter!" );
294cdf0e10cSrcweir 			}
295cdf0e10cSrcweir 		}
296cdf0e10cSrcweir 		catch ( Exception e )
297cdf0e10cSrcweir 		{
298cdf0e10cSrcweir 		}
299cdf0e10cSrcweir 
300cdf0e10cSrcweir 		m_bFree = true;
301cdf0e10cSrcweir 
302cdf0e10cSrcweir */
303cdf0e10cSrcweir 		return Any.VOID;
304cdf0e10cSrcweir 	}
305cdf0e10cSrcweir 
306cdf0e10cSrcweir };
307cdf0e10cSrcweir 
308