1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include <windows.h>
25 
26 #include "jawt.h"
27 #include "jawt_md.h"
28 #include "NativeView.h"
29 
30 #define MY_ASSERT(X,S) if (!X) { fprintf(stderr,"%s\n",S); return 0L;}
31 
32 #define SYSTEM_WIN32   1
33 #define SYSTEM_WIN16   2
34 #define SYSTEM_JAVA    3
35 #define SYSTEM_OS2     4
36 #define SYSTEM_MAC     5
37 #define SYSTEM_XWINDOW 6
38 
39 // property name to register own window procedure on hwnd
40 #define OLD_PROC_KEY "oldwindowproc"
41 // signature of this window procedure
42 static LRESULT APIENTRY NativeViewWndProc( HWND , UINT , WPARAM , LPARAM );
43 
44 /*****************************************************************************
45  *
46  * Class      : NativeView
47  * Method     : getNativeWindowSystemType
48  * Signature  : ()I
49  * Description: returns an identifier for the current operating system
50  */
Java_NativeView_getNativeWindowSystemType(JNIEnv * env,jobject obj_this)51 JNIEXPORT jint JNICALL Java_NativeView_getNativeWindowSystemType
52   (JNIEnv * env, jobject obj_this)
53 {
54     return (SYSTEM_WIN32);
55 }
56 
57 /*****************************************************************************
58  *
59  * Class      : NativeView
60  * Method     : getNativeWindow
61  * Signature  : ()J
62  * Description: returns the native systemw window handle of this object
63  */
Java_NativeView_getNativeWindow(JNIEnv * env,jobject obj_this)64 JNIEXPORT jlong JNICALL Java_NativeView_getNativeWindow
65   (JNIEnv * env, jobject obj_this)
66 {
67     jboolean                      result  ;
68     jint                          lock    ;
69     JAWT                          awt     ;
70     JAWT_DrawingSurface*          ds      ;
71     JAWT_DrawingSurfaceInfo*      dsi     ;
72     JAWT_Win32DrawingSurfaceInfo* dsi_win ;
73     HDC                           hdc     ;
74     HWND                          hWnd    ;
75     LONG                          hFuncPtr;
76 
77 	/* Get the AWT */
78 	awt.version = JAWT_VERSION_1_3;
79     result      = JAWT_GetAWT(env, &awt);
80     MY_ASSERT(result!=JNI_FALSE,"wrong jawt version");
81 
82     /* Get the drawing surface */
83 	if ((ds = awt.GetDrawingSurface(env, obj_this)) == NULL)
84 		return 0L;
85 
86 	/* Lock the drawing surface */
87 	lock = ds->Lock(ds);
88     MY_ASSERT((lock & JAWT_LOCK_ERROR)==0,"can't lock the drawing surface");
89 
90 	/* Get the drawing surface info */
91 	dsi = ds->GetDrawingSurfaceInfo(ds);
92 
93 	/* Get the platform-specific drawing info */
94 	dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
95     hdc     = dsi_win->hdc;
96     hWnd    = dsi_win->hwnd;
97 
98 	/* Free the drawing surface info */
99 	ds->FreeDrawingSurfaceInfo(dsi);
100 	/* Unlock the drawing surface */
101 	ds->Unlock(ds);
102 	/* Free the drawing surface */
103 	awt.FreeDrawingSurface(ds);
104 
105     /* Register own window procedure
106        Do it one time only! Otherwise
107        multiple instances will be registered
108        and calls on such construct produce
109        a stack overflow.
110      */
111     if (GetProp( hWnd, OLD_PROC_KEY )==0)
112     {
113         hFuncPtr = SetWindowLong( hWnd, GWL_WNDPROC, (DWORD)NativeViewWndProc );
114         SetProp( hWnd, OLD_PROC_KEY, (HANDLE)hFuncPtr );
115     }
116 
117     return ((jlong)hWnd);
118 }
119 
120 /*****************************************************************************
121  *
122  * Class      : -
123  * Method     : NativeViewWndProc
124  * Signature  : -
125  * Description: registered window handler to intercept window messages between
126  *              java and office process
127  */
NativeViewWndProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)128 static LRESULT APIENTRY NativeViewWndProc(
129     HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
130 {
131     HANDLE hFuncPtr;
132 
133     /* resize new created child window to fill out the java window complete */
134     if (uMsg==WM_PARENTNOTIFY)
135     {
136         if (wParam == WM_CREATE)
137         {
138             RECT rect;
139             HWND hChild = (HWND) lParam;
140 
141             GetClientRect(hWnd, &rect);
142 
143             SetWindowPos(hChild,
144                         NULL,
145                         rect.left,
146                         rect.top,
147                         rect.right - rect.left,
148                         rect.bottom - rect.top,
149                         SWP_NOZORDER);
150         }
151     }
152     /* handle normal resize events */
153     else if(uMsg==WM_SIZE)
154     {
155         WORD newHeight = HIWORD(lParam);
156         WORD newWidth  = LOWORD(lParam);
157         HWND hChild    = GetWindow(hWnd, GW_CHILD);
158 
159         if (hChild != NULL)
160             SetWindowPos(hChild, NULL, 0, 0, newWidth, newHeight, SWP_NOZORDER);
161     }
162 
163     /* forward request to original handler which is intercepted by this window procedure */
164     hFuncPtr = GetProp(hWnd, OLD_PROC_KEY);
165     MY_ASSERT(hFuncPtr,"lost original window proc handler");
166     return CallWindowProc( hFuncPtr, hWnd, uMsg, wParam, lParam);
167 }
168