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