Update ooo320-m1
[ooovba.git] / odk / examples / DevelopersGuide / OfficeDev / DesktopEnvironment / nativelib / windows / nativeview.c
blobc956bcf266a1d302cd0ca20d511a3207e94ca5c2
1 /*************************************************************************
3 * $RCSfile: nativeview.c,v $
5 * $Revision: 1.4 $
7 * last change: $Author: rt $ $Date: 2005-01-31 16:42:03 $
9 * The Contents of this file are made available subject to the terms of
10 * the BSD license.
12 * Copyright (c) 2003 by Sun Microsystems, Inc.
13 * All rights reserved.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
24 * contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
30 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
31 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
33 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
34 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
36 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
37 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *************************************************************************/
41 #include <windows.h>
43 #include "jawt.h"
44 #include "jawt_md.h"
45 #include "NativeView.h"
47 #define MY_ASSERT(X,S) if (!X) { fprintf(stderr,"%s\n",S); return 0L;}
49 #define SYSTEM_WIN32 1
50 #define SYSTEM_WIN16 2
51 #define SYSTEM_JAVA 3
52 #define SYSTEM_OS2 4
53 #define SYSTEM_MAC 5
54 #define SYSTEM_XWINDOW 6
56 // property name to register own window procedure on hwnd
57 #define OLD_PROC_KEY "oldwindowproc"
58 // signature of this window procedure
59 static LRESULT APIENTRY NativeViewWndProc( HWND , UINT , WPARAM , LPARAM );
61 /*****************************************************************************
63 * Class : NativeView
64 * Method : getNativeWindowSystemType
65 * Signature : ()I
66 * Description: returns an identifier for the current operating system
68 JNIEXPORT jint JNICALL Java_NativeView_getNativeWindowSystemType
69 (JNIEnv * env, jobject obj_this)
71 return (SYSTEM_WIN32);
74 /*****************************************************************************
76 * Class : NativeView
77 * Method : getNativeWindow
78 * Signature : ()J
79 * Description: returns the native systemw window handle of this object
81 JNIEXPORT jlong JNICALL Java_NativeView_getNativeWindow
82 (JNIEnv * env, jobject obj_this)
84 jboolean result ;
85 jint lock ;
86 JAWT awt ;
87 JAWT_DrawingSurface* ds ;
88 JAWT_DrawingSurfaceInfo* dsi ;
89 JAWT_Win32DrawingSurfaceInfo* dsi_win ;
90 HDC hdc ;
91 HWND hWnd ;
92 LONG hFuncPtr;
94 /* Get the AWT */
95 awt.version = JAWT_VERSION_1_3;
96 result = JAWT_GetAWT(env, &awt);
97 MY_ASSERT(result!=JNI_FALSE,"wrong jawt version");
99 /* Get the drawing surface */
100 if ((ds = awt.GetDrawingSurface(env, obj_this)) == NULL)
101 return 0L;
103 /* Lock the drawing surface */
104 lock = ds->Lock(ds);
105 MY_ASSERT((lock & JAWT_LOCK_ERROR)==0,"can't lock the drawing surface");
107 /* Get the drawing surface info */
108 dsi = ds->GetDrawingSurfaceInfo(ds);
110 /* Get the platform-specific drawing info */
111 dsi_win = (JAWT_Win32DrawingSurfaceInfo*)dsi->platformInfo;
112 hdc = dsi_win->hdc;
113 hWnd = dsi_win->hwnd;
115 /* Free the drawing surface info */
116 ds->FreeDrawingSurfaceInfo(dsi);
117 /* Unlock the drawing surface */
118 ds->Unlock(ds);
119 /* Free the drawing surface */
120 awt.FreeDrawingSurface(ds);
122 /* Register own window procedure
123 Do it one times only! Otherwhise
124 multiple instances will be registered
125 and calls on such construct produce
126 a stack overflow.
128 if (GetProp( hWnd, OLD_PROC_KEY )==0)
130 hFuncPtr = SetWindowLong( hWnd, GWL_WNDPROC, (DWORD)NativeViewWndProc );
131 SetProp( hWnd, OLD_PROC_KEY, (HANDLE)hFuncPtr );
134 return ((jlong)hWnd);
137 /*****************************************************************************
139 * Class : -
140 * Method : NativeViewWndProc
141 * Signature : -
142 * Description: registered window handler to intercept window messages between
143 * java and office process
145 static LRESULT APIENTRY NativeViewWndProc(
146 HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
148 HANDLE hFuncPtr;
150 /* resize new created child window to fill out the java window complete */
151 if (uMsg==WM_PARENTNOTIFY)
153 if (wParam == WM_CREATE)
155 RECT rect;
156 HWND hChild = (HWND) lParam;
158 GetClientRect(hWnd, &rect);
160 SetWindowPos(hChild,
161 NULL,
162 rect.left,
163 rect.top,
164 rect.right - rect.left,
165 rect.bottom - rect.top,
166 SWP_NOZORDER);
169 /* handle normal resize events */
170 else if(uMsg==WM_SIZE)
172 WORD newHeight = HIWORD(lParam);
173 WORD newWidth = LOWORD(lParam);
174 HWND hChild = GetWindow(hWnd, GW_CHILD);
176 if (hChild != NULL)
177 SetWindowPos(hChild, NULL, 0, 0, newWidth, newHeight, SWP_NOZORDER);
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);