1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #if defined _MSC_VER
29 #pragma warning(push, 1)
30 #endif
31 #include <windows.h>
32 #if defined _MSC_VER
33 #pragma warning(pop)
34 #endif
35 
36 #include "jawt.h"
37 
38 #if defined _MSC_VER
39 #pragma warning(push, 1)
40 #endif
41 #include "jawt_md.h"
42 #if defined _MSC_VER
43 #pragma warning(pop)
44 #endif
45 
46 #define SYSTEM_WIN32   1
47 #define SYSTEM_WIN16   2
48 #define SYSTEM_JAVA    3
49 #define SYSTEM_OS2     4
50 #define SYSTEM_MAC     5
51 #define SYSTEM_XWINDOW 6
52 
53 #define OLD_PROC_KEY "oldwindowproc"
54 
55 static LRESULT APIENTRY OpenOfficeWndProc( HWND , UINT , WPARAM , LPARAM );
56 
57 
58 
59 /* type must be something like java/lang/RuntimeException
60  */
61 static void ThrowException(JNIEnv * env, char const * type, char const * msg) {
62     jclass c;
63     (*env)->ExceptionClear(env);
64     c = (*env)->FindClass(env, type);
65     if (c == NULL) {
66         (*env)->ExceptionClear(env);
67         (*env)->FatalError(
68             env, "JNI FindClass failed");
69     }
70     if ((*env)->ThrowNew(env, c, msg) != 0) {
71         (*env)->ExceptionClear(env);
72         (*env)->FatalError(env, "JNI ThrowNew failed");
73     }
74 }
75 
76 
77 /*****************************************************************************/
78 /*
79  * Class:     com_sun_star_comp_beans_LocalOfficeWindow
80  * Method:    getNativeWindowSystemType
81  * Signature: ()I
82  */
83 JNIEXPORT jint JNICALL Java_com_sun_star_comp_beans_LocalOfficeWindow_getNativeWindowSystemType
84   (JNIEnv * env, jobject obj_this)
85 {
86     (void) env; // unused
87     (void) obj_this; // unused
88     return (SYSTEM_WIN32);
89 }
90 
91 
92 /*****************************************************************************/
93 /*
94  * Class:     com_sun_star_comp_beans_LocalOfficeWindow
95  * Method:    getNativeWindow
96  * Signature: ()J
97  */
98 JNIEXPORT jlong JNICALL Java_com_sun_star_comp_beans_LocalOfficeWindow_getNativeWindow
99   (JNIEnv * env, jobject obj_this)
100 {
101 	jboolean result;
102 	jint lock;
103 
104 	JAWT awt;
105 	JAWT_DrawingSurface* ds;
106 	JAWT_DrawingSurfaceInfo* dsi;
107 	JAWT_Win32DrawingSurfaceInfo* dsi_win;
108 	HDC hdc;
109 	HWND hWnd;
110     LONG hFuncPtr;
111 
112 	/* Get the AWT */
113 	awt.version = JAWT_VERSION_1_3;
114 	result = JAWT_GetAWT(env, &awt);
115 	if (result == JNI_FALSE)
116         ThrowException(env, "java/lang/RuntimeException", "JAWT_GetAWT failed");
117 
118 								/* Get the drawing surface */
119 	if ((ds = awt.GetDrawingSurface(env, obj_this)) == NULL)
120 		return 0L;
121 
122 	/* Lock the drawing surface */
123 	lock = ds->Lock(ds);
124 	if ( (lock & JAWT_LOCK_ERROR) != 0)
125         ThrowException(env, "java/lang/RuntimeException",
126                        "Could not get AWT drawing surface.");
127 
128 	/* Get the drawing surface info */
129 	dsi = ds->GetDrawingSurfaceInfo(ds);
130 
131 	/* Get the platform-specific drawing info */
132 	dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
133 
134 	hdc = dsi_win->hdc;
135 
136 	hWnd = dsi_win->hwnd;
137 
138 	/* Free the drawing surface info */
139 	ds->FreeDrawingSurfaceInfo(dsi);
140 	/* Unlock the drawing surface */
141 	ds->Unlock(ds);
142 	/* Free the drawing surface */
143 	awt.FreeDrawingSurface(ds);
144 
145     /* Register own window procedure
146        Do it one times only! Otherwhise
147        multiple instances will be registered
148        and calls on such construct produce
149        a stack overflow.
150      */
151     if (GetProp( hWnd, OLD_PROC_KEY )==0)
152     {
153         hFuncPtr = SetWindowLong( hWnd, GWL_WNDPROC, (DWORD)OpenOfficeWndProc );
154         SetProp( hWnd, OLD_PROC_KEY, (HANDLE)hFuncPtr );
155     }
156 
157 	return ((jlong)hWnd);
158 }
159 
160 
161 static LRESULT APIENTRY OpenOfficeWndProc(
162 	HWND hWnd,
163 	UINT uMsg,
164 	WPARAM wParam,
165 	LPARAM lParam)
166 {
167     switch(uMsg)
168     {
169 		case WM_PARENTNOTIFY: {
170 			if (wParam == WM_CREATE) {
171 				RECT rect;
172 				HWND hChild = (HWND) lParam;
173 
174 				GetClientRect(hWnd, &rect);
175 
176 				SetWindowPos(hChild,
177 							 NULL,
178 							 rect.left,
179 							 rect.top,
180 							 rect.right - rect.left,
181 							 rect.bottom - rect.top,
182 							 SWP_NOZORDER);
183 			}
184 			break;
185 		}
186 		case WM_SIZE: {
187 			WORD newHeight = HIWORD(lParam);
188 			WORD newWidth = LOWORD(lParam);
189 			HWND hChild = GetWindow(hWnd, GW_CHILD);
190 
191 			if (hChild != NULL) {
192 				SetWindowPos(hChild, NULL, 0, 0, newWidth, newHeight, SWP_NOZORDER);
193 			}
194 			break;
195 		}
196     }
197 
198 #if defined _MSC_VER
199 #pragma warning(push)
200 #pragma warning(disable: 4152) /* function/data pointer conversion: */
201 #endif
202 	return CallWindowProc(GetProp(hWnd, OLD_PROC_KEY),
203 						  hWnd, uMsg, wParam, lParam);
204 #if defined _MSC_VER
205 #pragma warning(pop)
206 #endif
207 }
208 
209 
210 
211 
212 
213 
214 
215 
216 
217 
218