xref: /AOO41X/main/odk/examples/DevelopersGuide/OfficeDev/DesktopEnvironment/NativeView.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir // __________ Imports __________
36*cdf0e10cSrcweir 
37*cdf0e10cSrcweir import java.awt.*;
38*cdf0e10cSrcweir import java.lang.*;
39*cdf0e10cSrcweir import java.awt.event.*;
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir // __________ Implementation __________
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir /**
44*cdf0e10cSrcweir  * Class to pass the system window handle to the OpenOffice.org toolkit.
45*cdf0e10cSrcweir  * It use special JNI methods to get the system handle of used java window.
46*cdf0e10cSrcweir  *
47*cdf0e10cSrcweir  * Attention!
48*cdf0e10cSrcweir  * Use JNI functions on already visible canvas objects only!
49*cdf0e10cSrcweir  * Otherwise they can make some trouble.
50*cdf0e10cSrcweir  *
51*cdf0e10cSrcweir  * @author  Andreas Schlüns
52*cdf0e10cSrcweir  * @created 22.02.2002 08:47
53*cdf0e10cSrcweir  */
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir public class NativeView extends java.awt.Canvas
56*cdf0e10cSrcweir {
57*cdf0e10cSrcweir     // ____________________
58*cdf0e10cSrcweir 
59*cdf0e10cSrcweir     /**
60*cdf0e10cSrcweir      * ctor
61*cdf0e10cSrcweir      * Does nothing realy.
62*cdf0e10cSrcweir      * We can use our JNI mechanism for an already visible
63*cdf0e10cSrcweir      * canvas only. So we overload the method for showing ("setVisible()")
64*cdf0e10cSrcweir      * and make our intialization there. BUt we try to show an empty clean
65*cdf0e10cSrcweir      * window till there.
66*cdf0e10cSrcweir      */
67*cdf0e10cSrcweir     public NativeView()
68*cdf0e10cSrcweir     {
69*cdf0e10cSrcweir         maHandle = null;
70*cdf0e10cSrcweir         maSystem = 0;
71*cdf0e10cSrcweir         this.setBackground(Color.white);
72*cdf0e10cSrcweir     }
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir     // ____________________
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir     /**
77*cdf0e10cSrcweir      * Overload this method to make neccessary initializations here.
78*cdf0e10cSrcweir      * (e.g. get the window handle and neccessary system informations)
79*cdf0e10cSrcweir      *
80*cdf0e10cSrcweir      * Why here?
81*cdf0e10cSrcweir      * Because the handle seams to be available for already visible windows
82*cdf0e10cSrcweir      * only. So it's the best place to get it. Special helper method
83*cdf0e10cSrcweir      * can be called more then ones - but call native code one times only
84*cdf0e10cSrcweir      * and safe the handle and the system type on our members maHandle/maSystem!
85*cdf0e10cSrcweir      */
86*cdf0e10cSrcweir     public void setVisible(boolean bState)
87*cdf0e10cSrcweir     {
88*cdf0e10cSrcweir         getHWND();
89*cdf0e10cSrcweir     }
90*cdf0e10cSrcweir 
91*cdf0e10cSrcweir     // ____________________
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir     /**
94*cdf0e10cSrcweir      * to guarantee right resize handling inside a swing container
95*cdf0e10cSrcweir      * (e.g. JSplitPane) we must provide some informations about our
96*cdf0e10cSrcweir      * prefered/minimum and maximum size.
97*cdf0e10cSrcweir      */
98*cdf0e10cSrcweir     public Dimension getPreferredSize()
99*cdf0e10cSrcweir     {
100*cdf0e10cSrcweir         return new Dimension(500,300);
101*cdf0e10cSrcweir     }
102*cdf0e10cSrcweir 
103*cdf0e10cSrcweir     public Dimension getMaximumSize()
104*cdf0e10cSrcweir     {
105*cdf0e10cSrcweir         return new Dimension(1024,768);
106*cdf0e10cSrcweir     }
107*cdf0e10cSrcweir 
108*cdf0e10cSrcweir     public Dimension getMinimumSize()
109*cdf0e10cSrcweir     {
110*cdf0e10cSrcweir         return new Dimension(100,100);
111*cdf0e10cSrcweir     }
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir     // ____________________
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     /**
116*cdf0e10cSrcweir      * overload paint routine to show provide against
117*cdf0e10cSrcweir      * repaint errors if no office view is realy plugged
118*cdf0e10cSrcweir      * into this canvas.
119*cdf0e10cSrcweir      * If handle is present - we shouldn't paint anything further.
120*cdf0e10cSrcweir      * May the remote window is already plugged. In such case we
121*cdf0e10cSrcweir      * shouldn't paint it over.
122*cdf0e10cSrcweir      */
123*cdf0e10cSrcweir     public void paint(Graphics aGraphic)
124*cdf0e10cSrcweir     {
125*cdf0e10cSrcweir         if(maHandle==null)
126*cdf0e10cSrcweir         {
127*cdf0e10cSrcweir             Dimension aSize = getSize();
128*cdf0e10cSrcweir             aGraphic.clearRect(0,0,aSize.width,aSize.height);
129*cdf0e10cSrcweir         }
130*cdf0e10cSrcweir     }
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir     // ____________________
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir     /**
135*cdf0e10cSrcweir      * JNI interface of this class
136*cdf0e10cSrcweir      * These two methods are implemented by using JNI mechanismen.
137*cdf0e10cSrcweir      * The will be used to get the platform dependent window handle
138*cdf0e10cSrcweir      * of a java awt canvas. This handle can be used to create an office
139*cdf0e10cSrcweir      * window as direct child of it. So it's possible to plug Office
140*cdf0e10cSrcweir      * windows in a java UI container.
141*cdf0e10cSrcweir      *
142*cdf0e10cSrcweir      * Note:
143*cdf0e10cSrcweir      * Native code for windows register special function pointer to handle
144*cdf0e10cSrcweir      * window messages ... But if it doesn't check for an already registered
145*cdf0e10cSrcweir      * instance of this handler it will do it twice and produce a stack overflow
146*cdf0e10cSrcweir      * because such method call herself in a never ending loop ...
147*cdf0e10cSrcweir      * So we try to use the JNI code one times only and safe already getted
148*cdf0e10cSrcweir      * informations inside this class.
149*cdf0e10cSrcweir      */
150*cdf0e10cSrcweir     public  native int  getNativeWindowSystemType();
151*cdf0e10cSrcweir     private native long getNativeWindow(); // private! => use getHWND() with cache mechanism!
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir     public Integer getHWND()
154*cdf0e10cSrcweir     {
155*cdf0e10cSrcweir         if(maHandle==null)
156*cdf0e10cSrcweir         {
157*cdf0e10cSrcweir             maHandle = new Integer((int)getNativeWindow());
158*cdf0e10cSrcweir             maSystem = getNativeWindowSystemType();
159*cdf0e10cSrcweir         }
160*cdf0e10cSrcweir         return maHandle;
161*cdf0e10cSrcweir     }
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir     // ____________________
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir     /**
166*cdf0e10cSrcweir      * for using of the JNI methods it's neccessary to load
167*cdf0e10cSrcweir      * system library which exports it.
168*cdf0e10cSrcweir      */
169*cdf0e10cSrcweir     static
170*cdf0e10cSrcweir     {
171*cdf0e10cSrcweir         System.loadLibrary("nativeview");
172*cdf0e10cSrcweir     }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir     // ____________________
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir     /**
177*cdf0e10cSrcweir      * @member  maHandle    system window handle
178*cdf0e10cSrcweir      * @member  maSystem    info about currently used platform
179*cdf0e10cSrcweir      */
180*cdf0e10cSrcweir     public Integer maHandle ;
181*cdf0e10cSrcweir     public int     maSystem ;
182*cdf0e10cSrcweir }
183