Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / embeddedobj / test / Container1 / PaintThread.java
blob4e6b166fa94c28ca47dac3dae012e8c12a323948
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package embeddedobj.test;
21 import java.awt.*;
22 import java.applet.*;
23 import java.awt.event.*;
24 import java.net.*;
25 import java.io.*;
26 import java.lang.Thread;
28 import com.sun.star.awt.XBitmap;
29 import com.sun.star.awt.XDevice;
30 import com.sun.star.awt.XDisplayBitmap;
31 import com.sun.star.awt.XGraphics;
32 import com.sun.star.awt.XWindow;
33 import com.sun.star.awt.XWindowPeer;
34 import com.sun.star.awt.XToolkit;
35 import com.sun.star.awt.XSystemChildFactory;
36 import com.sun.star.awt.WindowDescriptor;
37 import com.sun.star.awt.WindowClass;
38 import com.sun.star.awt.WindowAttribute;
40 import com.sun.star.uno.UnoRuntime;
41 import com.sun.star.lang.XMultiServiceFactory;
43 class PaintThread extends java.lang.Thread
45 private XWindow m_xWindow;
46 private XBitmap m_xBitmap;
47 private com.sun.star.awt.Rectangle m_aRect;
49 private Object m_oRequestsLock;
50 private boolean m_bToPaint = false;
52 private boolean m_bDisposed = false;
54 public static boolean interceptedRects( com.sun.star.awt.Rectangle aRect1, com.sun.star.awt.Rectangle aRect2 )
56 return ( ( aRect1.X <= aRect2.X && aRect2.X <= aRect1.X + aRect1.Width
57 || aRect1.X <= aRect2.X + aRect2.Width && aRect2.X + aRect2.Width <= aRect1.X + aRect1.Width
58 || aRect2.X <= aRect1.X && aRect1.X <= aRect2.X + aRect2.Width
59 || aRect2.X <= aRect1.X + aRect1.Width && aRect1.X + aRect1.Width <= aRect2.X + aRect2.Width )
60 && ( aRect1.Y <= aRect2.Y && aRect2.Y <= aRect1.Y + aRect1.Height
61 || aRect1.Y <= aRect2.Y + aRect2.Height && aRect2.Y + aRect2.Height <= aRect1.Y + aRect1.Height
62 || aRect2.Y <= aRect1.Y && aRect1.Y <= aRect2.Y + aRect2.Height
63 || aRect2.Y <= aRect1.Y + aRect1.Height && aRect1.Y + aRect1.Height <= aRect2.Y + aRect2.Height ) );
66 public PaintThread( XWindow xWindow )
68 m_oRequestsLock = new Object();
69 m_xWindow = xWindow;
72 public void setPaintRequest( XBitmap xBitmap, com.sun.star.awt.Rectangle aRect, com.sun.star.awt.Rectangle aClip )
74 synchronized( m_oRequestsLock )
77 System.out.println( "Paint request Pos( "
78 + aRect.X + ", "
79 + aRect.Y + ", "
80 + aRect.Width + ", "
81 + aRect.Height + " ), Clip ( "
82 + aClip.X + ", "
83 + aClip.Y + ", "
84 + aClip.Width + ", "
85 + aClip.Height + " )" );
88 if ( PaintThread.interceptedRects( aRect, aClip ) )
90 m_xBitmap = xBitmap;
91 m_aRect = aRect;
92 m_bToPaint = true;
96 // System.out.println( "Paint request to paint thread is done! xBitmap = " + xBitmap );
99 public void disposeThread()
101 m_bDisposed = true;
104 public void run()
106 while( !m_bDisposed )
108 try {
109 Thread.sleep( 200 );
110 } catch( Exception e ) {}
112 XBitmap xBitmap = null;
113 com.sun.star.awt.Rectangle aRect = null;
114 boolean bPaint = false;
116 synchronized( m_oRequestsLock )
118 if ( m_bToPaint )
120 xBitmap = m_xBitmap;
121 aRect = m_aRect;
122 m_bToPaint = false;
123 bPaint = true;
127 if ( bPaint )
129 // System.out.println( "The bitmap is going to be painted!" );
130 XDevice xDevice = (XDevice)UnoRuntime.queryInterface( XDevice.class, m_xWindow );
131 if ( xDevice != null )
133 // System.out.println( "Step1" );
134 XGraphics xGraphics = xDevice.createGraphics();
135 if ( xBitmap != null )
137 // System.out.println( "Step2" );
138 XDisplayBitmap xDisplayBitmap = xDevice.createDisplayBitmap( xBitmap );
140 com.sun.star.awt.Size aSize = xBitmap.getSize();
141 xGraphics.draw( xDisplayBitmap, 0, 0, aSize.Width, aSize.Height,
142 aRect.X, aRect.Y, aRect.Width, aRect.Height );
145 // System.out.println( "Step3" );
146 // xGraphics.drawRect( aRect.X - 1, aRect.Y - 1, aRect.Width + 2, aRect.Height + 2 );
147 xGraphics.drawLine( aRect.X - 1, aRect.Y - 1,
148 aRect.X + aRect.Width + 1, aRect.Y - 1 );
149 xGraphics.drawLine( aRect.X + aRect.Width + 1, aRect.Y - 1,
150 aRect.X + aRect.Width + 1, aRect.Y + aRect.Height + 1 );
151 xGraphics.drawLine( aRect.X + aRect.Width + 1, aRect.Y + aRect.Height + 1,
152 aRect.X - 1, aRect.Y + aRect.Height + 1 );
153 xGraphics.drawLine( aRect.X - 1, aRect.Y + aRect.Height + 1,
154 aRect.X - 1, aRect.Y - 1 );
156 // draw resize squares
157 // System.out.println( "Step4" );
158 // xGraphics.drawRect( aRect.X - 2, aRect.Y - 2, 4, 4 );
159 // xGraphics.drawRect( aRect.X + aRect.Width - 2, aRect.Y - 2, 4, 4 );
160 // xGraphics.drawRect( aRect.X - 2, aRect.Y + aRect.Height - 2, 4, 4 );
161 // xGraphics.drawRect( aRect.X + aRect.Width - 2, aRect.Y + aRect.Height - 2, 4, 4 );
163 // System.out.println( "Step5" );
165 // System.out.println( "The bitmap is painted by paint thread!" );