xref: /AOO42X/main/embeddedobj/test/Container1/WindowHelper.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir package embeddedobj.test;
2*cdf0e10cSrcweir 
3*cdf0e10cSrcweir import java.awt.*;
4*cdf0e10cSrcweir import java.applet.*;
5*cdf0e10cSrcweir import java.awt.event.*;
6*cdf0e10cSrcweir import java.net.*;
7*cdf0e10cSrcweir import java.io.*;
8*cdf0e10cSrcweir 
9*cdf0e10cSrcweir import com.sun.star.awt.XBitmap;
10*cdf0e10cSrcweir import com.sun.star.awt.XWindow;
11*cdf0e10cSrcweir import com.sun.star.awt.XWindowPeer;
12*cdf0e10cSrcweir import com.sun.star.awt.XToolkit;
13*cdf0e10cSrcweir import com.sun.star.awt.XSystemChildFactory;
14*cdf0e10cSrcweir import com.sun.star.awt.WindowDescriptor;
15*cdf0e10cSrcweir import com.sun.star.awt.WindowClass;
16*cdf0e10cSrcweir import com.sun.star.awt.WindowAttribute;
17*cdf0e10cSrcweir import com.sun.star.awt.VclWindowPeerAttribute;
18*cdf0e10cSrcweir 
19*cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
20*cdf0e10cSrcweir import com.sun.star.uno.AnyConverter;
21*cdf0e10cSrcweir import com.sun.star.uno.Any;
22*cdf0e10cSrcweir 
23*cdf0e10cSrcweir import com.sun.star.lang.XMultiServiceFactory;
24*cdf0e10cSrcweir import com.sun.star.lang.XSingleServiceFactory;
25*cdf0e10cSrcweir 
26*cdf0e10cSrcweir class WindowHelper {
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir     public static XWindow createWindow( XMultiServiceFactory xFactory, NativeView aParent, java.awt.Rectangle aBounds )
29*cdf0e10cSrcweir     {
30*cdf0e10cSrcweir         XWindow  xWindow = null;
31*cdf0e10cSrcweir         XToolkit    xToolkit = null;
32*cdf0e10cSrcweir 
33*cdf0e10cSrcweir         // get access to toolkit of remote office to create the container window of new target frame
34*cdf0e10cSrcweir         try{
35*cdf0e10cSrcweir             xToolkit = (XToolkit)UnoRuntime.queryInterface( XToolkit.class,
36*cdf0e10cSrcweir                                                             xFactory.createInstance("com.sun.star.awt.Toolkit") );
37*cdf0e10cSrcweir         }
38*cdf0e10cSrcweir         catch( Exception ex )
39*cdf0e10cSrcweir         {
40*cdf0e10cSrcweir             return null;
41*cdf0e10cSrcweir         }
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir         XSystemChildFactory xChildFactory = (XSystemChildFactory)UnoRuntime.queryInterface(
44*cdf0e10cSrcweir                 XSystemChildFactory.class,
45*cdf0e10cSrcweir                 xToolkit);
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir         try
48*cdf0e10cSrcweir         {
49*cdf0e10cSrcweir             XWindowPeer xPeer = null;
50*cdf0e10cSrcweir             Integer nHandle = aParent.getHWND();
51*cdf0e10cSrcweir             short   nSystem = (short)aParent.getNativeWindowSystemType();
52*cdf0e10cSrcweir             byte[]  lProcID = new byte[0];
53*cdf0e10cSrcweir /*
54*cdf0e10cSrcweir             try {
55*cdf0e10cSrcweir                 xPeer = xChildFactory.createSystemChild((Object)nHandle, lProcID, nSystem);
56*cdf0e10cSrcweir             }
57*cdf0e10cSrcweir             catch( Exception e )
58*cdf0e10cSrcweir             {}
59*cdf0e10cSrcweir */
60*cdf0e10cSrcweir             if (xPeer==null)
61*cdf0e10cSrcweir             {
62*cdf0e10cSrcweir                 JavaWindowPeerFake aWrapper = new JavaWindowPeerFake(aParent);
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir                 XWindowPeer xParentPeer = (XWindowPeer)UnoRuntime.queryInterface(
65*cdf0e10cSrcweir                         XWindowPeer.class,
66*cdf0e10cSrcweir                         aWrapper);
67*cdf0e10cSrcweir 
68*cdf0e10cSrcweir                 WindowDescriptor aDescriptor = new WindowDescriptor();
69*cdf0e10cSrcweir                 aDescriptor.Type = WindowClass.TOP;
70*cdf0e10cSrcweir                 aDescriptor.WindowServiceName = "workwindow";
71*cdf0e10cSrcweir                 aDescriptor.ParentIndex = 1;
72*cdf0e10cSrcweir                 aDescriptor.Parent = xParentPeer;
73*cdf0e10cSrcweir                 aDescriptor.Bounds = new com.sun.star.awt.Rectangle( (int)aBounds.getX(),
74*cdf0e10cSrcweir                                                                      (int)aBounds.getY(),
75*cdf0e10cSrcweir                                                                      (int)aBounds.getWidth(),
76*cdf0e10cSrcweir                                                                      (int)aBounds.getHeight() );
77*cdf0e10cSrcweir 
78*cdf0e10cSrcweir                 System.out.println( "The rectangle for vcl window is:\nx = " + (int)aBounds.getX()
79*cdf0e10cSrcweir                                                                 + "; y = " + (int)aBounds.getY()
80*cdf0e10cSrcweir                                                                 + "; width = " + (int)aBounds.getWidth()
81*cdf0e10cSrcweir                                                                 + "; height = " + (int)aBounds.getHeight() );
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir                 if (nSystem == com.sun.star.lang.SystemDependent.SYSTEM_WIN32)
84*cdf0e10cSrcweir                     aDescriptor.WindowAttributes = WindowAttribute.SHOW;
85*cdf0e10cSrcweir                 else
86*cdf0e10cSrcweir                     aDescriptor.WindowAttributes = WindowAttribute.SYSTEMDEPENDENT;
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir                 aDescriptor.WindowAttributes |= VclWindowPeerAttribute.CLIPCHILDREN;
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir                 xPeer = xToolkit.createWindow( aDescriptor );
91*cdf0e10cSrcweir             }
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir             xWindow = (XWindow)UnoRuntime.queryInterface( XWindow.class, xPeer);
94*cdf0e10cSrcweir             if ( xWindow != null )
95*cdf0e10cSrcweir                 xWindow.setPosSize( (int)aBounds.getX(),
96*cdf0e10cSrcweir                                     (int)aBounds.getY(),
97*cdf0e10cSrcweir                                     (int)aBounds.getWidth(),
98*cdf0e10cSrcweir                                     (int)aBounds.getHeight(),
99*cdf0e10cSrcweir                                     com.sun.star.awt.PosSize.POSSIZE );
100*cdf0e10cSrcweir         }
101*cdf0e10cSrcweir         catch( Exception ex1 )
102*cdf0e10cSrcweir         {
103*cdf0e10cSrcweir             System.out.println( "Exception on VCL window creation: " + ex1 );
104*cdf0e10cSrcweir             xWindow = null;
105*cdf0e10cSrcweir         }
106*cdf0e10cSrcweir 
107*cdf0e10cSrcweir         return xWindow;
108*cdf0e10cSrcweir     }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir     public static XBitmap getVCLBitmapFromBytes( XMultiServiceFactory xFactory, Object aAny )
111*cdf0e10cSrcweir     {
112*cdf0e10cSrcweir         if ( !AnyConverter.isArray( aAny ) )
113*cdf0e10cSrcweir             throw new com.sun.star.uno.RuntimeException();
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir         Object[] aArgs = new Object[1];
116*cdf0e10cSrcweir         aArgs[0] = aAny;
117*cdf0e10cSrcweir         XBitmap xResult = null;
118*cdf0e10cSrcweir 
119*cdf0e10cSrcweir         try {
120*cdf0e10cSrcweir             XSingleServiceFactory xBitmapFactory = (XSingleServiceFactory)UnoRuntime.queryInterface(
121*cdf0e10cSrcweir                     XSingleServiceFactory.class,
122*cdf0e10cSrcweir                     xFactory.createInstance( "com.sun.star.embed.BitmapCreator" ) );
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir             xResult = (XBitmap)UnoRuntime.queryInterface(
125*cdf0e10cSrcweir                     XBitmap.class,
126*cdf0e10cSrcweir                     xBitmapFactory.createInstanceWithArguments( aArgs ) );
127*cdf0e10cSrcweir         }
128*cdf0e10cSrcweir         catch( Exception e )
129*cdf0e10cSrcweir         {
130*cdf0e10cSrcweir             System.out.println( "Could not create VCL bitmap based on sequence," );
131*cdf0e10cSrcweir             System.out.println( "exception: " + e );
132*cdf0e10cSrcweir         }
133*cdf0e10cSrcweir 
134*cdf0e10cSrcweir         return xResult;
135*cdf0e10cSrcweir     }
136*cdf0e10cSrcweir };
137*cdf0e10cSrcweir 
138