Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / embeddedobj / test / Container1 / NativeView.java
blob2435e236d687ffc572471e13a03bebd159ff3f6a
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 .
18 package embeddedobj.test;
20 // __________ Imports __________
22 import java.awt.*;
23 import java.lang.*;
24 import java.awt.event.*;
26 // __________ Implementation __________
28 /**
29 * Class to pass the system window handle to the OpenOffice.org toolkit.
30 * It use special JNI methods to get the system handle of used java window.
32 * Attention!
33 * Use JNI functions on already visible canvas objects only!
34 * Otherwise they can make some trouble.
38 public class NativeView extends java.awt.Canvas
42 /**
43 * ctor
44 * Does nothing really.
45 * We can use our JNI mechanism for an already visible
46 * canvas only. So we override the method for showing ( "setVisible()" )
47 * and make our initialization there. BUt we try to show an empty clean
48 * window till there.
50 public NativeView()
52 maHandle = null;
53 maSystem = 0;
54 this.setBackground( Color.white );
59 /**
60 * Override this method to make necessary initializations here.
61 * ( e.g. get the window handle and necessary system information )
63 * Why here?
64 * Because the handle seems to be available for already-visible windows
65 * only. So it's the best place to get it. The special helper method
66 * can be called more than once - but call native code one time only
67 * and save the handle and the system type on our members maHandle/maSystem!
69 public void setVisible( boolean bState )
71 getHWND();
76 /**
77 * to guarantee right resize handling inside a swing container
78 * ( e.g. JSplitPane ) we must provide some information about our
79 * preferred/minimum and maximum size.
81 public Dimension getPreferredSize()
83 return new Dimension( 800, 600 );
86 public Dimension getMaximumSize()
88 return new Dimension( 1024, 768 );
91 public Dimension getMinimumSize()
93 return new Dimension( 300, 300 );
98 /**
99 * override paint routine to show provide against
100 * repaint errors if no office view is really plugged
101 * into this canvas.
102 * If handle is present - we shouldn't paint anything further.
103 * May the remote window is already plugged. In such case we
104 * shouldn't paint it over.
106 public void paint( Graphics aGraphic )
108 if( maHandle == null )
110 Dimension aSize = getSize();
111 aGraphic.clearRect( 0, 0, aSize.width, aSize.height );
118 * JNI interface of this class
119 * These two methods are implemented by using JNI mechanismen.
120 * The will be used to get the platform dependent window handle
121 * of a java awt canvas. This handle can be used to create an office
122 * window as direct child of it. So it's possible to plug Office
123 * windows in a java UI container.
125 * Note:
126 * Native code for Windows registers a special function pointer to handle
127 * window messages... But if it doesn't check for an already-registered
128 * instance of this handler it will do it twice and produce a stack overflow
129 * because such method calls itself in a never-ending loop...
130 * So we try to use the JNI code one time only and save already-obtained
131 * information inside this class.
133 public native int getNativeWindowSystemType();
134 private native long getNativeWindow(); // private! => use getHWND() with cache mechanism!
136 public Integer getHWND()
138 if( maHandle == null )
140 maHandle = Integer.valueOf( (int )getNativeWindow() );
141 maSystem = getNativeWindowSystemType();
143 return maHandle;
149 * for using of the JNI methods it's necessary to load
150 * system library which exports it.
152 static
154 System.loadLibrary( "nativeview" );
160 * @member maHandle system window handle
161 * @member maSystem info about currently used platform
163 public Integer maHandle ;
164 public int maSystem ;