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