1 /*************************************************************************
2  *
3  *  The Contents of this file are made available subject to the terms of
4  *  the BSD license.
5  *
6  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7  *  All rights reserved.
8  *
9  *  Redistribution and use in source and binary forms, with or without
10  *  modification, are permitted provided that the following conditions
11  *  are met:
12  *  1. Redistributions of source code must retain the above copyright
13  *     notice, this list of conditions and the following disclaimer.
14  *  2. Redistributions in binary form must reproduce the above copyright
15  *     notice, this list of conditions and the following disclaimer in the
16  *     documentation and/or other materials provided with the distribution.
17  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18  *     contributors may be used to endorse or promote products derived
19  *     from this software without specific prior written permission.
20  *
21  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  *
33  *************************************************************************/
34 
35 #include <windows.h>
36 
37 #include "jawt.h"
38 #include "jawt_md.h"
39 #include "NativeView.h"
40 
41 #define MY_ASSERT(X,S) if (!X) { fprintf(stderr,"%s\n",S); return 0L;}
42 
43 #define SYSTEM_WIN32   1
44 #define SYSTEM_WIN16   2
45 #define SYSTEM_JAVA    3
46 #define SYSTEM_OS2     4
47 #define SYSTEM_MAC     5
48 #define SYSTEM_XWINDOW 6
49 
50 // property name to register own window procedure on hwnd
51 #define OLD_PROC_KEY "oldwindowproc"
52 // signature of this window procedure
53 static LRESULT APIENTRY NativeViewWndProc( HWND , UINT , WPARAM , LPARAM );
54 
55 /*****************************************************************************
56  *
57  * Class      : NativeView
58  * Method     : getNativeWindowSystemType
59  * Signature  : ()I
60  * Description: returns an identifier for the current operating system
61  */
62 JNIEXPORT jint JNICALL Java_NativeView_getNativeWindowSystemType
63   (JNIEnv * env, jobject obj_this)
64 {
65     return (SYSTEM_WIN32);
66 }
67 
68 /*****************************************************************************
69  *
70  * Class      : NativeView
71  * Method     : getNativeWindow
72  * Signature  : ()J
73  * Description: returns the native systemw window handle of this object
74  */
75 JNIEXPORT jlong JNICALL Java_NativeView_getNativeWindow
76   (JNIEnv * env, jobject obj_this)
77 {
78     jboolean                      result  ;
79     jint                          lock    ;
80     JAWT                          awt     ;
81     JAWT_DrawingSurface*          ds      ;
82     JAWT_DrawingSurfaceInfo*      dsi     ;
83     JAWT_Win32DrawingSurfaceInfo* dsi_win ;
84     HDC                           hdc     ;
85     HWND                          hWnd    ;
86     LONG                          hFuncPtr;
87 
88 	/* Get the AWT */
89 	awt.version = JAWT_VERSION_1_3;
90     result      = JAWT_GetAWT(env, &awt);
91     MY_ASSERT(result!=JNI_FALSE,"wrong jawt version");
92 
93     /* Get the drawing surface */
94 	if ((ds = awt.GetDrawingSurface(env, obj_this)) == NULL)
95 		return 0L;
96 
97 	/* Lock the drawing surface */
98 	lock = ds->Lock(ds);
99     MY_ASSERT((lock & JAWT_LOCK_ERROR)==0,"can't lock the drawing surface");
100 
101 	/* Get the drawing surface info */
102 	dsi = ds->GetDrawingSurfaceInfo(ds);
103 
104 	/* Get the platform-specific drawing info */
105 	dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
106     hdc     = dsi_win->hdc;
107     hWnd    = dsi_win->hwnd;
108 
109 	/* Free the drawing surface info */
110 	ds->FreeDrawingSurfaceInfo(dsi);
111 	/* Unlock the drawing surface */
112 	ds->Unlock(ds);
113 	/* Free the drawing surface */
114 	awt.FreeDrawingSurface(ds);
115 
116     /* Register own window procedure
117        Do it one times only! Otherwhise
118        multiple instances will be registered
119        and calls on such construct produce
120        a stack overflow.
121      */
122     if (GetProp( hWnd, OLD_PROC_KEY )==0)
123     {
124         hFuncPtr = SetWindowLong( hWnd, GWL_WNDPROC, (DWORD)NativeViewWndProc );
125         SetProp( hWnd, OLD_PROC_KEY, (HANDLE)hFuncPtr );
126     }
127 
128     return ((jlong)hWnd);
129 }
130 
131 /*****************************************************************************
132  *
133  * Class      : -
134  * Method     : NativeViewWndProc
135  * Signature  : -
136  * Description: registered window handler to intercept window messages between
137  *              java and office process
138  */
139 static LRESULT APIENTRY NativeViewWndProc(
140     HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
141 {
142     HANDLE hFuncPtr;
143 
144     /* resize new created child window to fill out the java window complete */
145     if (uMsg==WM_PARENTNOTIFY)
146     {
147         if (wParam == WM_CREATE)
148         {
149             RECT rect;
150             HWND hChild = (HWND) lParam;
151 
152             GetClientRect(hWnd, &rect);
153 
154             SetWindowPos(hChild,
155                         NULL,
156                         rect.left,
157                         rect.top,
158                         rect.right - rect.left,
159                         rect.bottom - rect.top,
160                         SWP_NOZORDER);
161         }
162     }
163     /* handle normal resize events */
164     else if(uMsg==WM_SIZE)
165     {
166         WORD newHeight = HIWORD(lParam);
167         WORD newWidth  = LOWORD(lParam);
168         HWND hChild    = GetWindow(hWnd, GW_CHILD);
169 
170         if (hChild != NULL)
171             SetWindowPos(hChild, NULL, 0, 0, newWidth, newHeight, SWP_NOZORDER);
172     }
173 
174     /* forward request to original handler which is intercepted by this window procedure */
175     hFuncPtr = GetProp(hWnd, OLD_PROC_KEY);
176     MY_ASSERT(hFuncPtr,"lost original window proc handler");
177     return CallWindowProc( hFuncPtr, hWnd, uMsg, wParam, lParam);
178 }
179