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