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 #ifdef WNT
29 
30 #include <windows.h>
31 // property name to register own window procedure on hwnd
32 #define OLD_PROC_KEY "oldwindowproc"
33 // signature of this window procedure
34 static LRESULT APIENTRY NativeViewWndProc( HWND , UINT , WPARAM , LPARAM );
35 
36 #endif
37 
38 #include "jawt.h"
39 #include "jawt_md.h"
40 #include "NativeView.h"
41 
42 #define MY_ASSERT(X,S) if (!X) { fprintf(stderr,"%s\n",S); return 0L;}
43 
44 #define SYSTEM_WIN32   1
45 #define SYSTEM_WIN16   2
46 #define SYSTEM_JAVA    3
47 #define SYSTEM_OS2     4
48 #define SYSTEM_MAC     5
49 #define SYSTEM_XWINDOW 6
50 
51 /*****************************************************************************
52  *
53  * Class      : NativeView
54  * Method     : getNativeWindowSystemType
55  * Signature  : ()I
56  * Description: returns an identifier for the current operating system
57  */
58 JNIEXPORT jint JNICALL Java_embeddedobj_test_NativeView_getNativeWindowSystemType
59   (JNIEnv * env, jobject obj_this)
60 {
61     return (SYSTEM_WIN32);
62 }
63 
64 /*****************************************************************************
65  *
66  * Class      : NativeView
67  * Method     : getNativeWindow
68  * Signature  : ()J
69  * Description: returns the native systemw window handle of this object
70  */
71 JNIEXPORT jlong JNICALL Java_embeddedobj_test_NativeView_getNativeWindow
72   (JNIEnv * env, jobject obj_this)
73 {
74     jboolean                      result  ;
75     jint                          lock    ;
76     JAWT                          awt     ;
77     JAWT_DrawingSurface*          ds      ;
78     JAWT_DrawingSurfaceInfo*      dsi     ;
79     JAWT_Win32DrawingSurfaceInfo* dsi_win ;
80 	jlong						  drawable;
81 
82 #if 0
83 	LONG						  hFuncPtr;
84 #endif
85 
86 	/* Get the AWT */
87 	awt.version = JAWT_VERSION_1_3;
88     result      = JAWT_GetAWT(env, &awt);
89     MY_ASSERT(result!=JNI_FALSE,"wrong jawt version");
90 
91     /* Get the drawing surface */
92 	if ((ds = awt.GetDrawingSurface(env, obj_this)) == NULL)
93 		return 0L;
94 
95 	/* Lock the drawing surface */
96 	lock = ds->Lock(ds);
97     MY_ASSERT((lock & JAWT_LOCK_ERROR)==0,"can't lock the drawing surface");
98 
99 	/* Get the drawing surface info */
100 	dsi = ds->GetDrawingSurfaceInfo(ds);
101 
102 	/* Get the platform-specific drawing info */
103 #ifdef WNT
104 	dsi_win  = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
105     drawable = (jlong)dsi_win->hwnd;
106 #else
107     dsi_x11  = (JAWT_X11DrawingSurfaceInfo*)dsi->platformInfo;
108 	drawable = (jlong)dsi_x11->drawable;
109 #endif
110 
111 	/* Free the drawing surface info */
112 	ds->FreeDrawingSurfaceInfo(dsi);
113 	/* Unlock the drawing surface */
114 	ds->Unlock(ds);
115 	/* Free the drawing surface */
116 	awt.FreeDrawingSurface(ds);
117 
118 #if 0
119     /* Register own window procedure
120        Do it one times only! Otherwhise
121        multiple instances will be registered
122        and calls on such construct produce
123        a stack overflow.
124      */
125 
126     if (GetProp( (HWND)drawable, OLD_PROC_KEY )==0)
127     {
128         hFuncPtr = SetWindowLong( (HWND)drawable, GWL_WNDPROC, (DWORD)NativeViewWndProc );
129         SetProp( (HWND)drawable, OLD_PROC_KEY, (HANDLE)hFuncPtr );
130     }
131 #endif
132 
133     return drawable;
134 }
135 
136 #if 0
137 /*****************************************************************************
138  *
139  * Class      : -
140  * Method     : NativeViewWndProc
141  * Signature  : -
142  * Description: registered window handler to intercept window messages between
143  *              java and office process
144  */
145 static LRESULT APIENTRY NativeViewWndProc(
146     HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
147 {
148     HANDLE hFuncPtr;
149 
150     /* resize new created child window to fill out the java window complete */
151     if (uMsg==WM_PARENTNOTIFY)
152     {
153         if (wParam == WM_CREATE)
154         {
155             RECT rect;
156             HWND hChild = (HWND) lParam;
157 
158             GetClientRect(hWnd, &rect);
159 
160             SetWindowPos(hChild,
161                         NULL,
162                         rect.left,
163                         rect.top,
164                         rect.right - rect.left,
165                         rect.bottom - rect.top,
166                         SWP_NOZORDER);
167         }
168     }
169     /* handle normal resize events */
170     else if(uMsg==WM_SIZE)
171     {
172         WORD newHeight = HIWORD(lParam);
173         WORD newWidth  = LOWORD(lParam);
174         HWND hChild    = GetWindow(hWnd, GW_CHILD);
175 
176         if (hChild != NULL)
177             SetWindowPos(hChild, NULL, 0, 0, newWidth, newHeight, SWP_NOZORDER);
178     }
179 
180     /* forward request to original handler which is intercepted by this window procedure */
181     hFuncPtr = GetProp(hWnd, OLD_PROC_KEY);
182     MY_ASSERT(hFuncPtr,"lost original window proc handler");
183     return CallWindowProc( hFuncPtr, hWnd, uMsg, wParam, lParam);
184 }
185 #endif
186 
187