1/*
2 * Copyright (c) 1996, 2011, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26#ifndef AWT_OBJECT_H
27#define AWT_OBJECT_H
28
29#include "awt.h"
30#include "awt_Toolkit.h"
31
32#include "java_awt_Event.h"
33#include "java_awt_AWTEvent.h"
34#include "sun_awt_windows_WObjectPeer.h"
35
36/************************************************************************
37 * AwtObject class
38 */
39
40class AwtObject {
41public:
42    class ExecuteArgs {
43        public:
44            UINT        cmdId;
45            LPARAM      param1;
46            LPARAM      param2;
47            LPARAM      param3;
48            LPARAM      param4;
49    };
50
51
52    /* sun.awt.windows.WObjectPeer field and method ids */
53    static jfieldID pDataID;
54    static jfieldID destroyedID;
55    static jfieldID targetID;
56
57    static jmethodID getPeerForTargetMID;
58    static jclass wObjectPeerClass;
59
60    static jfieldID createErrorID;
61
62    AwtObject();
63    virtual ~AwtObject();
64
65    // Frees all the resources used by this object and then sends a message to TT to delete it.
66    // After this method has been called, this object must not be used in any way.
67    virtual void Dispose();
68
69    // Static method to be called from JNI methods to dispose AwtObject
70    // specified by jobject
71    static void _Dispose(jobject self);
72
73    // Static method to be called from JNI methods to dispose AwtObject
74    // specified by pData
75    static void _Dispose(PDATA pData);
76
77    INLINE CriticalSection& GetLock() { return m_Lock; }
78
79    // Return the associated AWT peer or target object.
80    INLINE jobject GetPeer(JNIEnv *env) {
81        return m_peerObject;
82    }
83
84    INLINE jobject GetTarget(JNIEnv *env) {
85        jobject peer = GetPeer(env);
86        if (peer != NULL) {
87            return env->GetObjectField(peer, AwtObject::targetID);
88        } else {
89            return NULL;
90        }
91    }
92
93    INLINE jobject GetTargetAsGlobalRef(JNIEnv *env) {
94        jobject localRef = GetTarget(env);
95        if (localRef == NULL) {
96            return NULL;
97        }
98
99        jobject globalRef = env->NewGlobalRef(localRef);
100        env->DeleteLocalRef(localRef);
101        return globalRef;
102    }
103
104    // Return the peer associated with some target
105    static jobject GetPeerForTarget(JNIEnv *env, jobject target);
106
107    // Java callback routines
108    // Invoke a callback on the java peer object asynchronously
109    void DoCallback(const char* methodName, const char* methodSig, ...);
110
111    // Allocate and initialize a new event, and post it to the peer's
112    // target object.  No response is expected from the target.
113    void SendEvent(jobject event);
114
115    INLINE void EnableCallbacks(BOOL e) { m_callbacksEnabled = e; }
116
117    // Execute any code associated with a command ID -- only classes with
118    // DoCommand() defined should associate their instances with cmdIDs.
119    virtual void DoCommand(void) {
120        DASSERT(FALSE);
121    }
122
123    // execute given code on Windows message-pump thread
124    static LRESULT WinThreadExec(jobject peerObject, UINT cmdId, LPARAM param1 = 0L, LPARAM param2 = 0L, LPARAM param3 = 0L, LPARAM param4 = 0L);
125    // callback function to execute code on Windows message-pump thread
126    virtual LRESULT WinThreadExecProc(AwtObject::ExecuteArgs * args);
127
128    // overridden in AwtComponent to return FALSE if any messages
129    // are being processed by this component
130    virtual BOOL CanBeDeleted() {
131        return TRUE;
132    }
133
134protected:
135    jobject                       m_peerObject;
136    BOOL                          m_callbacksEnabled;
137
138private:
139    CriticalSection m_Lock;
140};
141
142#endif // AWT_OBJECT_H
143