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