jdk/src/solaris/native/sun/awt/awt_Button.c
changeset 1192 715cf9378c53
parent 1051 90cf935adb35
parent 1191 f142c1da78c2
child 1193 41afb8ee8f45
equal deleted inserted replaced
1051:90cf935adb35 1192:715cf9378c53
     1 /*
       
     2  * Copyright 1995-2001 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 
       
    26 #ifdef HEADLESS
       
    27     #error This file should not be included in headless library
       
    28 #endif
       
    29 
       
    30 #include "awt_p.h"
       
    31 
       
    32 #include <jni.h>
       
    33 #include <jni_util.h>
       
    34 #include "multi_font.h"
       
    35 
       
    36 #include "awt_Component.h"
       
    37 
       
    38 extern struct MComponentPeerIDs mComponentPeerIDs;
       
    39 extern AwtGraphicsConfigDataPtr
       
    40     copyGraphicsConfigToPeer(JNIEnv *env, jobject this);
       
    41 
       
    42 /*
       
    43  * When the -jni switch is thrown, these headers can be deleted.
       
    44  */
       
    45 #include "java_awt_Button.h"
       
    46 #include "sun_awt_motif_MButtonPeer.h"
       
    47 #include "sun_awt_motif_MComponentPeer.h"
       
    48 
       
    49 /* fieldIDs for Button fields that may be accessed from C */
       
    50 static struct ButtonIDs {
       
    51     jfieldID label;
       
    52 } buttonIDs;
       
    53 
       
    54 static char emptyString[] = "";
       
    55 
       
    56 
       
    57 /*
       
    58  * Class:     java_awt_Button
       
    59  * Method:    initIDs
       
    60  * Signature: ()V
       
    61  */
       
    62 
       
    63 /* This function gets called from the static initializer for Button.java
       
    64    to initialize the fieldIDs for fields that may be accessed from C */
       
    65 
       
    66 JNIEXPORT void JNICALL
       
    67 Java_java_awt_Button_initIDs
       
    68   (JNIEnv *env, jclass cls)
       
    69 {
       
    70     buttonIDs.label =
       
    71       (*env)->GetFieldID(env, cls, "label", "Ljava/lang/String;");
       
    72 }
       
    73 
       
    74 /*
       
    75  * client_data is MButtonPeer instance
       
    76  */
       
    77 static void
       
    78 Button_callback (Widget w,
       
    79                  XtPointer client_data,
       
    80                  XmPushButtonCallbackStruct * call_data)
       
    81 {
       
    82     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
       
    83     ConvertEventTimeAndModifiers converted;
       
    84 
       
    85     awt_util_convertEventTimeAndModifiers(call_data->event, &converted);
       
    86 
       
    87     JNU_CallMethodByName(env, NULL, (jobject)client_data, "action", "(JI)V",
       
    88                          converted.when, converted.modifiers);
       
    89     if ((*env)->ExceptionOccurred (env)) {
       
    90         (*env)->ExceptionDescribe (env);
       
    91         (*env)->ExceptionClear (env);
       
    92     }
       
    93 }
       
    94 
       
    95 /*
       
    96  * Class:     sun_awt_motif_MButtonPeer
       
    97  * Method:    create
       
    98  * Signature: (Lsun/awt/motif/MComponentPeer;)V
       
    99  */
       
   100 JNIEXPORT void JNICALL Java_sun_awt_motif_MButtonPeer_create
       
   101   (JNIEnv * env, jobject this, jobject parent)
       
   102 {
       
   103     jobject target;
       
   104     jobject label;
       
   105     struct ComponentData *cdata;
       
   106     struct ComponentData *wdata;
       
   107     char *clabel;
       
   108     Pixel bg;
       
   109     XmString mfstr = NULL;
       
   110     jobject globalRef = awtJNI_CreateAndSetGlobalRef (env, this);
       
   111     jobject font = awtJNI_GetFont (env, this);
       
   112     jboolean IsMultiFont = awtJNI_IsMultiFont (env, font);
       
   113     AwtGraphicsConfigDataPtr adata;
       
   114 
       
   115     AWT_LOCK ();
       
   116 
       
   117     if (JNU_IsNull (env, parent)) {
       
   118         JNU_ThrowNullPointerException (env, "NullPointerException");
       
   119         AWT_UNLOCK ();
       
   120 
       
   121         return;
       
   122     }
       
   123     target = (*env)->GetObjectField (env, this, mComponentPeerIDs.target);
       
   124     wdata = (struct ComponentData *)
       
   125       JNU_GetLongFieldAsPtr(env, parent, mComponentPeerIDs.pData);
       
   126 
       
   127     if (JNU_IsNull (env, target) || wdata == NULL) {
       
   128         JNU_ThrowNullPointerException (env, "NullPointerException");
       
   129         AWT_UNLOCK ();
       
   130 
       
   131         return;
       
   132     }
       
   133     cdata = ZALLOC (ComponentData);
       
   134     if (cdata == NULL) {
       
   135         JNU_ThrowOutOfMemoryError (env, "OutOfMemoryError");
       
   136         AWT_UNLOCK ();
       
   137         return;
       
   138     }
       
   139     JNU_SetLongFieldFromPtr(env, this, mComponentPeerIDs.pData, cdata);
       
   140 
       
   141     adata = copyGraphicsConfigToPeer(env, this);
       
   142 
       
   143     XtVaGetValues (wdata->widget, XmNbackground, &bg, NULL);
       
   144 
       
   145     label =
       
   146       (*env)->GetObjectField (env, target, buttonIDs.label);
       
   147 
       
   148     if (IsMultiFont) {
       
   149         /*
       
   150          * We don't use makeCString() function here.
       
   151          * We create Motif multi-font compound string to display
       
   152          * unicode on the platform which is not spporting unicode.
       
   153          */
       
   154         if (JNU_IsNull (env, label) || ((*env)->GetStringLength (env, label) == 0)) {
       
   155             mfstr = XmStringCreateLocalized ("");
       
   156         } else {
       
   157             mfstr = awtJNI_MakeMultiFontString (env, label, font);
       
   158         }
       
   159 
       
   160         cdata->widget = XtVaCreateManagedWidget
       
   161             ("", xmPushButtonWidgetClass,
       
   162              wdata->widget,
       
   163              XmNlabelString, mfstr,
       
   164              XmNrecomputeSize, False,
       
   165              XmNbackground, bg,
       
   166              XmNhighlightOnEnter, False,
       
   167              XmNshowAsDefault, 0,
       
   168              XmNdefaultButtonShadowThickness, 0,
       
   169              XmNmarginTop, 0,
       
   170              XmNmarginBottom, 0,
       
   171              XmNmarginLeft, 0,
       
   172              XmNmarginRight, 0,
       
   173              XmNuserData, (XtPointer) globalRef,
       
   174              XmNscreen, ScreenOfDisplay(awt_display,
       
   175                                         adata->awt_visInfo.screen),
       
   176              NULL);
       
   177         if (mfstr != NULL) {
       
   178             XmStringFree(mfstr);
       
   179             mfstr = NULL;
       
   180         }
       
   181 
       
   182     } else {
       
   183         if (JNU_IsNull (env, label)) {
       
   184             clabel = emptyString;
       
   185         } else {
       
   186             clabel = (char *) JNU_GetStringPlatformChars (env, label, NULL);
       
   187             if (clabel == NULL) {        /* Exception? */
       
   188                 AWT_UNLOCK ();
       
   189                 return;
       
   190             }
       
   191         }
       
   192 
       
   193         cdata->widget = XtVaCreateManagedWidget
       
   194             (clabel, xmPushButtonWidgetClass,
       
   195              wdata->widget,
       
   196              XmNrecomputeSize, False,
       
   197              XmNbackground, bg,
       
   198              XmNhighlightOnEnter, False,
       
   199              XmNshowAsDefault, 0,
       
   200              XmNdefaultButtonShadowThickness, 0,
       
   201              XmNmarginTop, 0,
       
   202              XmNmarginBottom, 0,
       
   203              XmNmarginLeft, 0,
       
   204              XmNmarginRight, 0,
       
   205              XmNuserData, (XtPointer) globalRef,
       
   206              XmNscreen, ScreenOfDisplay(awt_display,
       
   207                                         adata->awt_visInfo.screen),
       
   208              NULL);
       
   209 
       
   210         if (clabel != emptyString) {
       
   211             JNU_ReleaseStringPlatformChars (env, label, (const char *) clabel);;
       
   212         }
       
   213     }
       
   214 
       
   215     XtSetMappedWhenManaged (cdata->widget, False);
       
   216     XtAddCallback (cdata->widget,
       
   217                    XmNactivateCallback,
       
   218                    (XtCallbackProc) Button_callback,
       
   219                    (XtPointer) globalRef);
       
   220 
       
   221     AWT_UNLOCK ();
       
   222 }
       
   223 
       
   224 /*
       
   225  * Class:     sun_awt_motif_MButtonPeer
       
   226  * Method:    setLabel
       
   227  * Signature: (Ljava/lang/String;)V
       
   228  */
       
   229 JNIEXPORT void JNICALL Java_sun_awt_motif_MButtonPeer_setLabel
       
   230   (JNIEnv * env, jobject this, jstring label)
       
   231 {
       
   232     struct ComponentData *wdata;
       
   233     char *clabel;
       
   234     XmString xim;
       
   235 
       
   236     AWT_LOCK ();
       
   237 
       
   238     wdata = (struct ComponentData *)
       
   239       JNU_GetLongFieldAsPtr(env, this, mComponentPeerIDs.pData);
       
   240 
       
   241     if (wdata == NULL) {
       
   242         JNU_ThrowNullPointerException (env, "NullPointerException");
       
   243         AWT_UNLOCK ();
       
   244         return;
       
   245     }
       
   246     if (JNU_IsNull (env, label) || ((*env)->GetStringLength (env, label) == 0)) {
       
   247         xim = XmStringCreateLocalized ("");
       
   248     } else {
       
   249         jobject font = awtJNI_GetFont (env, this);
       
   250 
       
   251         if (awtJNI_IsMultiFont (env, font)) {
       
   252             xim = awtJNI_MakeMultiFontString (env, label, font);
       
   253         } else {
       
   254             if (JNU_IsNull (env, label)) {
       
   255                 clabel = emptyString;
       
   256             } else {
       
   257                 clabel = (char *) JNU_GetStringPlatformChars (env, label, NULL);
       
   258 
       
   259                 if (clabel == NULL) {      /* Exception? */
       
   260                     AWT_UNLOCK ();
       
   261                     return;
       
   262                 }
       
   263             }
       
   264 
       
   265             xim = XmStringCreate (clabel, "labelFont");
       
   266 
       
   267             if (clabel != emptyString) {
       
   268                 JNU_ReleaseStringPlatformChars (env, label, (const char *) clabel);;
       
   269             }
       
   270         }
       
   271     }
       
   272 
       
   273     XtVaSetValues (wdata->widget, XmNlabelString, xim, NULL);
       
   274     XmStringFree (xim);
       
   275     AWT_FLUSH_UNLOCK ();
       
   276 }