Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / embeddedobj / test / Container1 / BitmapPainter.java
blob2910eab3f224fb2186ddfc7f97ab43963d704e5f
1 package embeddedobj.test;
3 import java.awt.*;
4 import java.applet.*;
5 import java.awt.event.*;
6 import java.net.*;
7 import java.io.*;
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;
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;
28 import com.sun.star.uno.UnoRuntime;
29 import com.sun.star.uno.Any;
30 import com.sun.star.lang.XMultiServiceFactory;
32 import com.sun.star.task.XJob;
33 import com.sun.star.beans.NamedValue;
36 class BitmapPainter implements XPaintListener, XMouseListener, XMouseMotionListener, XJob
38 private XWindow m_xWindow;
39 private XBitmap m_xBitmap;
41 private com.sun.star.awt.Rectangle m_aDrawRect;
43 private Object m_oImageLock;
45 private PaintThread m_aPaintThread;
47 // private XJob m_xMainThreadExecutor;
48 // private NamedValue[] m_pValuesForExecutor;
50 private boolean m_bFree = true;
52 private boolean m_bProceedWithPainting = true;
54 // Methods
55 //------------------------------------------------------
56 public BitmapPainter( XJob xJob, XWindow xWindow, XBitmap xBitmap, com.sun.star.awt.Rectangle aDrawRect )
58 if ( xJob == null )
60 System.out.println( "No mainthreadexecutor is provided to BimapPainter on init!" );
61 throw new com.sun.star.uno.RuntimeException();
64 if ( xWindow == null )
66 System.out.println( "No window is provided to BimapPainter on init!" );
67 throw new com.sun.star.uno.RuntimeException();
70 // m_xMainThreadExecutor = xJob;
71 // m_pValuesForExecutor = new NamedValue[1];
72 // m_pValuesForExecutor[0] = new NamedValue( "JobToExecute", (Object)this );
74 m_xWindow = xWindow;
75 m_xBitmap = xBitmap;
77 m_aDrawRect = aDrawRect;
79 m_oImageLock = new Object();
81 m_aPaintThread = new PaintThread( m_xWindow );
82 m_aPaintThread.start();
84 m_xWindow.addPaintListener( this );
85 m_xWindow.addMouseListener( this );
86 m_xWindow.addMouseMotionListener( this );
89 //------------------------------------------------------
90 public void disconnectListener()
92 m_aPaintThread.disposeThread();
93 m_xWindow.removePaintListener( this );
94 m_xWindow.removeMouseListener( this );
95 m_xWindow.removeMouseMotionListener( this );
98 //------------------------------------------------------
99 public void setBitmap( XBitmap xBitmap )
101 synchronized( m_oImageLock )
103 m_xBitmap = xBitmap;
107 //------------------------------------------------------
108 public void setPos( com.sun.star.awt.Point aPoint )
110 synchronized( m_oImageLock )
112 m_aDrawRect.X = aPoint.X;
113 m_aDrawRect.Y = aPoint.Y;
117 //------------------------------------------------------
118 public void setRect( com.sun.star.awt.Rectangle aRect )
120 synchronized( m_oImageLock )
122 m_aDrawRect = aRect;
126 //------------------------------------------------------
127 public void setSize( com.sun.star.awt.Size aSize )
129 synchronized( m_oImageLock )
131 m_aDrawRect.Width = aSize.Width;
132 m_aDrawRect.Height = aSize.Height;
136 //------------------------------------------------------
137 public void stopPainting()
139 m_bProceedWithPainting = false;
142 //------------------------------------------------------
143 public void startPainting()
145 m_bProceedWithPainting = true;
148 // XPaintListener
149 //------------------------------------------------------
150 public void windowPaint( PaintEvent e )
152 if ( !m_bProceedWithPainting )
153 return;
155 XBitmap xBitmap = null;
156 com.sun.star.awt.Rectangle aRect = null;
157 // boolean bFree = false;
159 synchronized( m_oImageLock )
161 xBitmap = m_xBitmap;
162 aRect = m_aDrawRect;
163 // if ( m_bFree )
164 // {
165 // bFree = true;
166 // m_bFree = false;
167 // }
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 // }
181 System.out.println( "VCL window paint event!" );
184 // XMouseListener
185 //------------------------------------------------------
186 public void mousePressed( MouseEvent e )
190 //------------------------------------------------------
191 public void mouseReleased( MouseEvent e )
195 //------------------------------------------------------
196 public void mouseEntered( MouseEvent e )
200 //------------------------------------------------------
201 public void mouseExited( MouseEvent e )
205 // XMouseMotionListener
206 //------------------------------------------------------
207 public void mouseDragged( MouseEvent e )
209 // TODO: react to resizing of object bitmap
210 // if the object is inplace active the object must control resizing
213 //------------------------------------------------------
214 public void mouseMoved( MouseEvent e )
219 // XEventListener
220 //------------------------------------------------------
221 public void disposing( com.sun.star.lang.EventObject e )
223 // do nothing, the window can die only when the application is closed
226 // XJob
227 //------------------------------------------------------
228 public Object execute( NamedValue[] pValues )
231 // means request for painting
233 XBitmap xBitmap = null;
234 com.sun.star.awt.Rectangle aRect = null;
236 synchronized( m_oImageLock )
238 xBitmap = m_xBitmap;
239 aRect = m_aDrawRect;
242 System.out.println( "The bitmap is going to be painted!" );
244 try {
245 XDevice xDevice = (XDevice)UnoRuntime.queryInterface( XDevice.class, m_xWindow );
246 if ( xDevice != null )
248 System.out.println( "Step1" );
249 XGraphics xGraphics = xDevice.createGraphics();
250 if ( xBitmap != null )
252 System.out.println( "Step2" );
253 XDisplayBitmap xDisplayBitmap = xDevice.createDisplayBitmap( xBitmap );
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 );
260 System.out.println( "Step3" );
261 xGraphics.drawRect( aRect.X - 1, aRect.Y - 1, aRect.Width + 2, aRect.Height + 2 );
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 );
270 System.out.println( "Step5" );
272 System.out.println( "The bitmap is painted by BitmapPainter!" );
275 catch ( Exception e )
279 m_bFree = true;
282 return Any.VOID;