bump product version to 5.0.4.1
[LibreOffice.git] / bean / native / win32 / com_sun_star_comp_beans_LocalOfficeWindow.c
blob42f7714c2dca68edf510446bc81ac11f21601609
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 #define JAWT_GetAWT hidden_JAWT_GetAWT
29 #include "jawt.h"
30 #undef JAWT_GetAWT
32 #if defined _MSC_VER
33 #pragma warning(push, 1)
34 #endif
35 /* When cross-compiling to Windows we don't have any Windows JDK
36 * available. Copying this short snippet from win32/jawt_md.h can
37 * surely not be against its license. The intent is to enable
38 * interoperation with real Oracle Java after all. We leave out the
39 * informative comments that might have "artistic merit" and be more
40 * copyrightable. Use this also for native Windows compilation for
41 * simplicity.
43 typedef struct jawt_Win32DrawingSurfaceInfo {
44 union {
45 HWND hwnd;
46 HBITMAP hbitmap;
47 void* pbits;
49 HDC hdc;
50 HPALETTE hpalette;
51 } JAWT_Win32DrawingSurfaceInfo;
53 JNIIMPORT unsigned char JNICALL JAWT_GetAWT(JNIEnv *, JAWT *);
54 #if defined _MSC_VER
55 #pragma warning(pop)
56 #endif
58 #define SYSTEM_WIN32 1
60 #define OLD_PROC_KEY "oldwindowproc"
62 static LRESULT APIENTRY OpenOfficeWndProc( HWND , UINT , WPARAM , LPARAM );
64 /* type must be something like java/lang/RuntimeException
66 static void ThrowException(JNIEnv * env, char const * type, char const * msg) {
67 jclass c;
68 (*env)->ExceptionClear(env);
69 c = (*env)->FindClass(env, type);
70 if (c == NULL) {
71 (*env)->ExceptionClear(env);
72 (*env)->FatalError(
73 env, "JNI FindClass failed");
75 if ((*env)->ThrowNew(env, c, msg) != 0) {
76 (*env)->ExceptionClear(env);
77 (*env)->FatalError(env, "JNI ThrowNew failed");
82 /*****************************************************************************/
84 * Class: com_sun_star_comp_beans_LocalOfficeWindow
85 * Method: getNativeWindowSystemType
86 * Signature: ()I
88 JNIEXPORT jint JNICALL Java_com_sun_star_comp_beans_LocalOfficeWindow_getNativeWindowSystemType
89 (JNIEnv * env, jobject obj_this)
91 (void) env; // unused
92 (void) obj_this; // unused
93 return SYSTEM_WIN32;
97 /*****************************************************************************/
99 * Class: com_sun_star_comp_beans_LocalOfficeWindow
100 * Method: getNativeWindow
101 * Signature: ()J
103 JNIEXPORT jlong JNICALL Java_com_sun_star_comp_beans_LocalOfficeWindow_getNativeWindow
104 (JNIEnv * env, jobject obj_this)
106 jboolean result;
107 jint lock;
109 JAWT awt;
110 JAWT_DrawingSurface* ds;
111 JAWT_DrawingSurfaceInfo* dsi;
112 JAWT_Win32DrawingSurfaceInfo* dsi_win;
113 HWND hWnd;
114 LONG_PTR hFuncPtr;
116 /* Get the AWT */
117 awt.version = JAWT_VERSION_1_3;
118 result = JAWT_GetAWT(env, &awt);
119 if (result == JNI_FALSE)
120 ThrowException(env, "java/lang/RuntimeException", "JAWT_GetAWT failed");
122 /* Get the drawing surface */
123 if ((ds = awt.GetDrawingSurface(env, obj_this)) == NULL)
124 return 0L;
126 /* Lock the drawing surface */
127 lock = ds->Lock(ds);
128 if ( (lock & JAWT_LOCK_ERROR) != 0)
129 ThrowException(env, "java/lang/RuntimeException",
130 "Could not get AWT drawing surface.");
132 /* Get the drawing surface info */
133 dsi = ds->GetDrawingSurfaceInfo(ds);
135 /* Get the platform-specific drawing info */
136 dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
138 hWnd = dsi_win->hwnd;
140 /* Free the drawing surface info */
141 ds->FreeDrawingSurfaceInfo(dsi);
142 /* Unlock the drawing surface */
143 ds->Unlock(ds);
144 /* Free the drawing surface */
145 awt.FreeDrawingSurface(ds);
147 /* Register own window procedure
148 Do it one times only! Otherwhise
149 multiple instances will be registered
150 and calls on such construct produce
151 a stack overflow.
153 if (GetProp( hWnd, OLD_PROC_KEY )==0)
155 hFuncPtr = SetWindowLongPtr( hWnd, GWLP_WNDPROC, (LONG_PTR)OpenOfficeWndProc );
156 SetProp( hWnd, OLD_PROC_KEY, (HANDLE)hFuncPtr );
159 return (jlong)hWnd;
163 static LRESULT APIENTRY OpenOfficeWndProc(
164 HWND hWnd,
165 UINT uMsg,
166 WPARAM wParam,
167 LPARAM lParam)
169 switch(uMsg)
171 case WM_PARENTNOTIFY: {
172 if (wParam == WM_CREATE) {
173 RECT rect;
174 HWND hChild = (HWND) lParam;
176 GetClientRect(hWnd, &rect);
178 SetWindowPos(hChild,
179 NULL,
180 rect.left,
181 rect.top,
182 rect.right - rect.left,
183 rect.bottom - rect.top,
184 SWP_NOZORDER);
186 break;
188 case WM_SIZE: {
189 WORD newHeight = HIWORD(lParam);
190 WORD newWidth = LOWORD(lParam);
191 HWND hChild = GetWindow(hWnd, GW_CHILD);
193 if (hChild != NULL) {
194 SetWindowPos(hChild, NULL, 0, 0, newWidth, newHeight, SWP_NOZORDER);
196 break;
200 #if defined _MSC_VER
201 #pragma warning(push)
202 #pragma warning(disable: 4152) /* function/data pointer conversion: */
203 #endif
204 return CallWindowProc(GetProp(hWnd, OLD_PROC_KEY),
205 hWnd, uMsg, wParam, lParam);
206 #if defined _MSC_VER
207 #pragma warning(pop)
208 #endif
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */