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 import java.lang.Thread;
30 
31 import com.sun.star.awt.XBitmap;
32 import com.sun.star.awt.XDevice;
33 import com.sun.star.awt.XDisplayBitmap;
34 import com.sun.star.awt.XGraphics;
35 import com.sun.star.awt.XWindow;
36 import com.sun.star.awt.XWindowPeer;
37 import com.sun.star.awt.XToolkit;
38 import com.sun.star.awt.XSystemChildFactory;
39 import com.sun.star.awt.WindowDescriptor;
40 import com.sun.star.awt.WindowClass;
41 import com.sun.star.awt.WindowAttribute;
42 
43 import com.sun.star.uno.UnoRuntime;
44 import com.sun.star.lang.XMultiServiceFactory;
45 
46 class PaintThread extends java.lang.Thread
47 {
48 	private XWindow m_xWindow;
49 	private XBitmap m_xBitmap;
50 	private com.sun.star.awt.Rectangle m_aRect;
51 
52 	private Object m_oRequestsLock;
53 	private boolean m_bToPaint = false;
54 
55 	private boolean m_bDisposed = false;
56 
interceptedRects( com.sun.star.awt.Rectangle aRect1, com.sun.star.awt.Rectangle aRect2 )57 	public static boolean interceptedRects( com.sun.star.awt.Rectangle aRect1, com.sun.star.awt.Rectangle aRect2 )
58 	{
59 		return ( ( aRect1.X <= aRect2.X && aRect2.X <= aRect1.X + aRect1.Width
60 				|| aRect1.X <= aRect2.X + aRect2.Width && aRect2.X + aRect2.Width <= aRect1.X + aRect1.Width
61 				|| aRect2.X <= aRect1.X && aRect1.X <= aRect2.X + aRect2.Width
62 				|| aRect2.X <= aRect1.X + aRect1.Width && aRect1.X + aRect1.Width <= aRect2.X + aRect2.Width )
63 			  && ( aRect1.Y <= aRect2.Y && aRect2.Y <= aRect1.Y + aRect1.Height
64 				|| aRect1.Y <= aRect2.Y + aRect2.Height && aRect2.Y + aRect2.Height <= aRect1.Y + aRect1.Height
65 				|| aRect2.Y <= aRect1.Y && aRect1.Y <= aRect2.Y + aRect2.Height
66 				|| aRect2.Y <= aRect1.Y + aRect1.Height && aRect1.Y + aRect1.Height <= aRect2.Y + aRect2.Height ) );
67 	}
68 
PaintThread( XWindow xWindow )69 	public PaintThread( XWindow xWindow )
70 	{
71 		m_oRequestsLock = new Object();
72 		m_xWindow = xWindow;
73 	}
74 
setPaintRequest( XBitmap xBitmap, com.sun.star.awt.Rectangle aRect, com.sun.star.awt.Rectangle aClip )75 	public void setPaintRequest( XBitmap xBitmap, com.sun.star.awt.Rectangle aRect, com.sun.star.awt.Rectangle aClip )
76 	{
77 		synchronized( m_oRequestsLock )
78 		{
79 		/*
80 			System.out.println( "Paint request Pos( "
81 													+ aRect.X + ", "
82 													+ aRect.Y + ", "
83 													+ aRect.Width + ", "
84 													+ aRect.Height + " ), Clip ( "
85 													+ aClip.X + ", "
86 													+ aClip.Y + ", "
87 													+ aClip.Width + ", "
88 													+ aClip.Height + " )" );
89 		*/
90 
91 			if ( PaintThread.interceptedRects( aRect, aClip ) )
92 			{
93 				m_xBitmap = xBitmap;
94 				m_aRect = aRect;
95 				m_bToPaint = true;
96 			}
97 		}
98 
99 		// System.out.println( "Paint request to paint thread is done! xBitmap = " + xBitmap );
100 	}
101 
disposeThread()102 	public void disposeThread()
103 	{
104 		m_bDisposed = true;
105 	}
106 
run()107 	public void run()
108 	{
109 		while( !m_bDisposed )
110 		{
111 			try {
112 				Thread.sleep( 200 );
113 			} catch( Exception e ) {}
114 
115 			XBitmap xBitmap = null;
116 			com.sun.star.awt.Rectangle aRect = null;
117 			boolean bPaint = false;
118 
119 			synchronized( m_oRequestsLock )
120 			{
121 				if ( m_bToPaint )
122 				{
123 					xBitmap = m_xBitmap;
124 					aRect = m_aRect;
125 					m_bToPaint = false;
126 					bPaint = true;
127 				}
128 			}
129 
130 			if ( bPaint )
131 			{
132 				// System.out.println( "The bitmap is going to be painted!" );
133 				XDevice xDevice = (XDevice)UnoRuntime.queryInterface( XDevice.class, m_xWindow );
134 				if ( xDevice != null )
135 				{
136 				 	// System.out.println( "Step1" );
137 					XGraphics xGraphics = xDevice.createGraphics();
138 					if ( xBitmap != null )
139 					{
140 						// System.out.println( "Step2" );
141 						XDisplayBitmap xDisplayBitmap = xDevice.createDisplayBitmap( xBitmap );
142 
143 						com.sun.star.awt.Size aSize = xBitmap.getSize();
144 						xGraphics.draw( xDisplayBitmap, 0, 0, aSize.Width, aSize.Height,
145 													aRect.X, aRect.Y, aRect.Width, aRect.Height );
146 					}
147 
148 					// System.out.println( "Step3" );
149 					// xGraphics.drawRect( aRect.X - 1, aRect.Y - 1, aRect.Width + 2, aRect.Height + 2 );
150 					xGraphics.drawLine( aRect.X - 1, aRect.Y - 1,
151 										aRect.X + aRect.Width + 1, aRect.Y - 1 );
152 					xGraphics.drawLine( aRect.X + aRect.Width + 1, aRect.Y - 1,
153 										aRect.X + aRect.Width + 1, aRect.Y + aRect.Height + 1 );
154 					xGraphics.drawLine( aRect.X + aRect.Width + 1, aRect.Y + aRect.Height + 1,
155 										aRect.X - 1, aRect.Y + aRect.Height + 1 );
156 					xGraphics.drawLine( aRect.X - 1, aRect.Y + aRect.Height + 1,
157 										aRect.X - 1, aRect.Y - 1 );
158 
159 					// draw resize squares
160 					// System.out.println( "Step4" );
161 					// xGraphics.drawRect( aRect.X - 2, aRect.Y - 2, 4, 4 );
162 					// xGraphics.drawRect( aRect.X + aRect.Width - 2, aRect.Y - 2, 4, 4 );
163 					// xGraphics.drawRect( aRect.X - 2, aRect.Y + aRect.Height - 2, 4, 4 );
164 					// xGraphics.drawRect( aRect.X + aRect.Width - 2, aRect.Y + aRect.Height - 2, 4, 4 );
165 
166 					// System.out.println( "Step5" );
167 
168 					// System.out.println( "The bitmap is painted by paint thread!" );
169 				}
170 			}
171 		}
172 	}
173 };
174 
175