jdk/src/solaris/native/sun/awt/awt_KeyboardFocusManager.c
changeset 1192 715cf9378c53
parent 1051 90cf935adb35
parent 1191 f142c1da78c2
child 1193 41afb8ee8f45
equal deleted inserted replaced
1051:90cf935adb35 1192:715cf9378c53
     1 /*
       
     2  * Copyright 2000-2003 Sun Microsystems, Inc.  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.  Sun designates this
       
     8  * particular file as subject to the "Classpath" exception as provided
       
     9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
       
    22  * CA 95054 USA or visit www.sun.com if you need additional information or
       
    23  * have any questions.
       
    24  */
       
    25 #ifdef HEADLESS
       
    26     #error This file should not be included in headless library
       
    27 #endif
       
    28 
       
    29 #include "awt_p.h"
       
    30 #include "jni.h"
       
    31 #include "jni_util.h"
       
    32 
       
    33 #include "awt_KeyboardFocusManager.h"
       
    34 #include "java_awt_KeyboardFocusManager.h"
       
    35 #include "java_awt_event_FocusEvent.h"
       
    36 #include "awt_Component.h"
       
    37 #include "canvas.h"
       
    38 #include "awt_MToolkit.h"
       
    39 
       
    40 extern struct MComponentPeerIDs mComponentPeerIDs;
       
    41 
       
    42 struct KeyboardFocusManagerIDs keyboardFocusManagerIDs;
       
    43 
       
    44 /*
       
    45  * Class:     java_awt_KeyboardFocusManager
       
    46  * Method:    initIDs
       
    47  * Signature: ()V
       
    48  */
       
    49 JNIEXPORT void JNICALL
       
    50 Java_java_awt_KeyboardFocusManager_initIDs
       
    51     (JNIEnv *env, jclass cls)
       
    52 {
       
    53     jclass keyclass = NULL;
       
    54 
       
    55     keyboardFocusManagerIDs.keyboardFocusManagerCls = (jclass)
       
    56         (*env)->NewGlobalRef(env, cls);
       
    57     keyboardFocusManagerIDs.shouldNativelyFocusHeavyweightMID =
       
    58         (*env)->GetStaticMethodID(env, cls, "shouldNativelyFocusHeavyweight",
       
    59             "(Ljava/awt/Component;Ljava/awt/Component;ZZJLsun/awt/CausedFocusEvent$Cause;)I");
       
    60     keyboardFocusManagerIDs.heavyweightButtonDownMID =
       
    61         (*env)->GetStaticMethodID(env, cls, "heavyweightButtonDown",
       
    62             "(Ljava/awt/Component;J)V");
       
    63     keyboardFocusManagerIDs.heavyweightButtonDownZMID =
       
    64         (*env)->GetStaticMethodID(env, cls, "heavyweightButtonDown",
       
    65             "(Ljava/awt/Component;JZ)V");
       
    66     keyboardFocusManagerIDs.markClearGlobalFocusOwnerMID =
       
    67         (*env)->GetStaticMethodID(env, cls, "markClearGlobalFocusOwner",
       
    68                                   "()Ljava/awt/Window;");
       
    69 
       
    70     keyboardFocusManagerIDs.processSynchronousTransferMID =
       
    71         (*env)->GetStaticMethodID(env, cls, "processSynchronousLightweightTransfer",
       
    72                                   "(Ljava/awt/Component;Ljava/awt/Component;ZZJ)Z");
       
    73 
       
    74     keyclass = (*env)->FindClass(env, "java/awt/event/KeyEvent");
       
    75     DASSERT (keyclass != NULL);
       
    76 
       
    77     keyboardFocusManagerIDs.isProxyActive =
       
    78         (*env)->GetFieldID(env, keyclass, "isProxyActive",
       
    79                            "Z");
       
    80 
       
    81     (*env)->DeleteLocalRef(env, keyclass);
       
    82 
       
    83     DASSERT(keyboardFocusManagerIDs.keyboardFocusManagerCls != NULL);
       
    84     DASSERT(keyboardFocusManagerIDs.shouldNativelyFocusHeavyweightMID !=
       
    85             NULL);
       
    86     DASSERT(keyboardFocusManagerIDs.heavyweightButtonDownMID != NULL);
       
    87     DASSERT(keyboardFocusManagerIDs.heavyweightButtonDownZMID != NULL);
       
    88     DASSERT(keyboardFocusManagerIDs.markClearGlobalFocusOwnerMID != NULL);
       
    89     DASSERT(keyboardFocusManagerIDs.processSynchronousTransferMID != NULL);
       
    90 }
       
    91 
       
    92 /*
       
    93  * Class:     java_awt_KeyboardFocusManager
       
    94  * Method:    getNativeFocusOwner
       
    95  * Signature: ()Ljava/awt/Component;
       
    96  */
       
    97 JNIEXPORT jobject JNICALL
       
    98 Java_sun_awt_KeyboardFocusManagerPeerImpl_getNativeFocusOwner
       
    99     (JNIEnv *env, jclass cls)
       
   100 {
       
   101     jobject l_peer;
       
   102 
       
   103     AWT_LOCK();
       
   104     l_peer = awt_canvas_getFocusOwnerPeer();
       
   105     AWT_UNLOCK();
       
   106 
       
   107     return (l_peer != NULL)
       
   108         ? (*env)->GetObjectField(env, l_peer, mComponentPeerIDs.target)
       
   109         : NULL;
       
   110 }
       
   111 
       
   112 /*
       
   113  * Class:     java_awt_KeyboardFocusManager
       
   114  * Method:    getNativeFocusedWindow
       
   115  * Signature: ()Ljava/awt/Window;
       
   116  */
       
   117 JNIEXPORT jobject JNICALL
       
   118 Java_sun_awt_KeyboardFocusManagerPeerImpl_getNativeFocusedWindow
       
   119     (JNIEnv *env, jclass cls)
       
   120 {
       
   121     jobject l_peer;
       
   122 
       
   123     AWT_LOCK();
       
   124     l_peer = awt_canvas_getFocusedWindowPeer();
       
   125     AWT_UNLOCK();
       
   126 
       
   127     return (l_peer != NULL)
       
   128         ? (*env)->GetObjectField(env, l_peer, mComponentPeerIDs.target)
       
   129         : NULL;
       
   130 }
       
   131 
       
   132 /*
       
   133  * Class:     java_awt_KeyboardFocusManager
       
   134  * Method:    clearGlobalFocusOwner
       
   135  * Signature: ()V
       
   136  */
       
   137 JNIEXPORT void JNICALL
       
   138 Java_sun_awt_KeyboardFocusManagerPeerImpl_clearNativeGlobalFocusOwner
       
   139     (JNIEnv *env, jobject self, jobject activeWindow)
       
   140 {
       
   141   /* Redirect focus to the focus proxy of the active Window. The effect
       
   142      we want is for the active Window to remain active, but for none of
       
   143      its children to be the focus owner. AWT maintains state to know
       
   144      that any key events delivered after this call (but before focus is
       
   145      re-established elsewhere) get ignored. */
       
   146 
       
   147     Widget proxy;
       
   148 
       
   149     if ((*env)->EnsureLocalCapacity(env, 1) < 0) {
       
   150         return;
       
   151     }
       
   152 
       
   153     AWT_LOCK();
       
   154 
       
   155     if (activeWindow != NULL) {
       
   156         // Setting focus owner to proxy will be equivalent to having
       
   157         // null focus owner in Java layer while we will still be
       
   158         // able to receive key events.
       
   159         proxy = findWindowsProxy(activeWindow, env);
       
   160 
       
   161         if (proxy != NULL) {
       
   162             Widget curFocusWidget = XmGetFocusWidget(proxy);
       
   163             if (curFocusWidget != NULL) {
       
   164                 callFocusHandler(curFocusWidget, FocusOut, NULL);
       
   165             }
       
   166 
       
   167             // Disable all but proxy widgets
       
   168             processTree(curFocusWidget, proxy, False);
       
   169 
       
   170             XmProcessTraversal(proxy, XmTRAVERSE_CURRENT);
       
   171         }
       
   172     }
       
   173 
       
   174     AWT_UNLOCK();
       
   175 }