update credits
[LibreOffice.git] / bean / native / win32 / com_sun_star_comp_beans_LocalOfficeWindow.c
blobb4e7d82921c948c981126d94e980011d35b20baf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #if defined _MSC_VER
21 #pragma warning(push, 1)
22 #endif
23 #include <windows.h>
24 #if defined _MSC_VER
25 #pragma warning(pop)
26 #endif
28 #include <windows.h>
29 #define JAWT_GetAWT hidden_JAWT_GetAWT
30 #include "jawt.h"
31 #undef JAWT_GetAWT
33 #if defined _MSC_VER
34 #pragma warning(push, 1)
35 #endif
36 /* When cross-compiling to Windows we don't have any Windows JDK
37 * available. Copying this short snippet from win32/jawt_md.h can
38 * surely not be against its license. The intent is to enable
39 * interoperation with real Oracle Java after all. We leave out the
40 * informative comments that might have "artistic merit" and be more
41 * copyrightable. Use this also for native Windows compilation for
42 * simplicity.
44 typedef struct jawt_Win32DrawingSurfaceInfo {
45 union {
46 HWND hwnd;
47 HBITMAP hbitmap;
48 void* pbits;
50 HDC hdc;
51 HPALETTE hpalette;
52 } JAWT_Win32DrawingSurfaceInfo;
54 extern __declspec(dllimport) unsigned char __stdcall JAWT_GetAWT(JNIEnv *, JAWT *);
55 #if defined _MSC_VER
56 #pragma warning(pop)
57 #endif
59 #define SYSTEM_WIN32 1
61 #define OLD_PROC_KEY "oldwindowproc"
63 static LRESULT APIENTRY OpenOfficeWndProc( HWND , UINT , WPARAM , LPARAM );
65 /* type must be something like java/lang/RuntimeException
67 static void ThrowException(JNIEnv * env, char const * type, char const * msg) {
68 jclass c;
69 (*env)->ExceptionClear(env);
70 c = (*env)->FindClass(env, type);
71 if (c == NULL) {
72 (*env)->ExceptionClear(env);
73 (*env)->FatalError(
74 env, "JNI FindClass failed");
76 if ((*env)->ThrowNew(env, c, msg) != 0) {
77 (*env)->ExceptionClear(env);
78 (*env)->FatalError(env, "JNI ThrowNew failed");
83 /*****************************************************************************/
85 * Class: com_sun_star_comp_beans_LocalOfficeWindow
86 * Method: getNativeWindowSystemType
87 * Signature: ()I
89 JNIEXPORT jint JNICALL Java_com_sun_star_comp_beans_LocalOfficeWindow_getNativeWindowSystemType
90 (JNIEnv * env, jobject obj_this)
92 (void) env; // unused
93 (void) obj_this; // unused
94 return (SYSTEM_WIN32);
98 /*****************************************************************************/
100 * Class: com_sun_star_comp_beans_LocalOfficeWindow
101 * Method: getNativeWindow
102 * Signature: ()J
104 JNIEXPORT jlong JNICALL Java_com_sun_star_comp_beans_LocalOfficeWindow_getNativeWindow
105 (JNIEnv * env, jobject obj_this)
107 jboolean result;
108 jint lock;
110 JAWT awt;
111 JAWT_DrawingSurface* ds;
112 JAWT_DrawingSurfaceInfo* dsi;
113 JAWT_Win32DrawingSurfaceInfo* dsi_win;
114 HWND hWnd;
115 LONG hFuncPtr;
117 /* Get the AWT */
118 awt.version = JAWT_VERSION_1_3;
119 result = JAWT_GetAWT(env, &awt);
120 if (result == JNI_FALSE)
121 ThrowException(env, "java/lang/RuntimeException", "JAWT_GetAWT failed");
123 /* Get the drawing surface */
124 if ((ds = awt.GetDrawingSurface(env, obj_this)) == NULL)
125 return 0L;
127 /* Lock the drawing surface */
128 lock = ds->Lock(ds);
129 if ( (lock & JAWT_LOCK_ERROR) != 0)
130 ThrowException(env, "java/lang/RuntimeException",
131 "Could not get AWT drawing surface.");
133 /* Get the drawing surface info */
134 dsi = ds->GetDrawingSurfaceInfo(ds);
136 /* Get the platform-specific drawing info */
137 dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
139 hWnd = dsi_win->hwnd;
141 /* Free the drawing surface info */
142 ds->FreeDrawingSurfaceInfo(dsi);
143 /* Unlock the drawing surface */
144 ds->Unlock(ds);
145 /* Free the drawing surface */
146 awt.FreeDrawingSurface(ds);
148 /* Register own window procedure
149 Do it one times only! Otherwhise
150 multiple instances will be registered
151 and calls on such construct produce
152 a stack overflow.
154 if (GetProp( hWnd, OLD_PROC_KEY )==0)
156 hFuncPtr = SetWindowLongPtr( hWnd, GWLP_WNDPROC, (LONG_PTR)OpenOfficeWndProc );
157 SetProp( hWnd, OLD_PROC_KEY, (HANDLE)hFuncPtr );
160 return ((jlong)(LONG)hWnd);
164 static LRESULT APIENTRY OpenOfficeWndProc(
165 HWND hWnd,
166 UINT uMsg,
167 WPARAM wParam,
168 LPARAM lParam)
170 switch(uMsg)
172 case WM_PARENTNOTIFY: {
173 if (wParam == WM_CREATE) {
174 RECT rect;
175 HWND hChild = (HWND) lParam;
177 GetClientRect(hWnd, &rect);
179 SetWindowPos(hChild,
180 NULL,
181 rect.left,
182 rect.top,
183 rect.right - rect.left,
184 rect.bottom - rect.top,
185 SWP_NOZORDER);
187 break;
189 case WM_SIZE: {
190 WORD newHeight = HIWORD(lParam);
191 WORD newWidth = LOWORD(lParam);
192 HWND hChild = GetWindow(hWnd, GW_CHILD);
194 if (hChild != NULL) {
195 SetWindowPos(hChild, NULL, 0, 0, newWidth, newHeight, SWP_NOZORDER);
197 break;
201 #if defined _MSC_VER
202 #pragma warning(push)
203 #pragma warning(disable: 4152) /* function/data pointer conversion: */
204 #endif
205 return CallWindowProc(GetProp(hWnd, OLD_PROC_KEY),
206 hWnd, uMsg, wParam, lParam);
207 #if defined _MSC_VER
208 #pragma warning(pop)
209 #endif
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */