01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: /**
18: * @author Pavel Dolgov
19: * @version $Revision$
20: */package org.apache.harmony.awt.wtk.windows;
21:
22: import org.apache.harmony.awt.internal.nls.Messages;
23: import org.apache.harmony.awt.nativebridge.Int16Pointer;
24: import org.apache.harmony.awt.nativebridge.windows.Callback;
25: import org.apache.harmony.awt.nativebridge.windows.Win32;
26: import org.apache.harmony.awt.nativebridge.windows.WindowsDefs;
27: import org.apache.harmony.awt.wtk.NativeEventThread;
28:
29: /**
30: * Helper that registers native window class and forwards WindowProc calls to
31: * appropriate WinWTK object. This allows running concurrent event dispatch
32: * threads
33: */
34: public class WindowProcHandler implements Callback.Handler {
35:
36: static final Win32 win32 = Win32.getInstance();
37:
38: private static WindowProcHandler instance;
39:
40: static final String windowClassName = "org.apache.harmony.awt.wtk.window"; //$NON-NLS-1$
41:
42: private final long windowProcPtr;
43:
44: public static void registerCallback() {
45: if (instance != null) {
46: return;
47: }
48:
49: instance = new WindowProcHandler();
50: }
51:
52: private WindowProcHandler() {
53: windowProcPtr = Callback.registerCallback(this );
54: registerWindowClass(windowClassName, windowProcPtr);
55: }
56:
57: public long windowProc(long hwnd, int msg, long wParam, long lParam) {
58: NativeEventThread thread = (NativeEventThread) Thread
59: .currentThread();
60: WinWTK wtk = (WinWTK) thread.getWTK();
61: if (wtk == null || wtk.getWinEventQueue() == null) {
62: return win32.DefWindowProcW(hwnd, msg, wParam, lParam);
63: }
64: return wtk.getWinEventQueue().windowProc(hwnd, msg, wParam,
65: lParam);
66: }
67:
68: public static void registerWindowClass(String className,
69: long windowProc) {
70:
71: Int16Pointer namePtr = WinEventQueue.bridge.createInt16Pointer(
72: className, false);
73:
74: Win32.WNDCLASSEXW wndCls = win32.createWNDCLASSEXW(false);
75:
76: wndCls.set_cbSize(wndCls.size());
77: wndCls.set_style(WindowsDefs.CS_OWNDC);
78: wndCls.set_lpfnWndProc(windowProc);
79: wndCls.set_hCursor(0);
80: wndCls.set_lpszClassName(namePtr);
81:
82: short classAtom = win32.RegisterClassExW(wndCls);
83: int winError = win32.GetLastError();
84: namePtr.unlock();
85:
86: if (classAtom == 0) {
87: // awt.1A=Failed to register window class {0} GetLastError returned {1}
88: throw new InternalError(Messages.getString("awt.1A", //$NON-NLS-1$
89: className, winError));
90: }
91: }
92:
93: public static void registerWindowClass(String className) {
94: registerCallback();
95: registerWindowClass(className, instance.windowProcPtr);
96: }
97: }
|