jdk/src/solaris/native/sun/awt/awt_TextArea.c
changeset 1211 b659a7cee935
parent 1210 7798f9e88bf9
parent 1203 3e5496df0d2b
child 1212 d718a4419361
equal deleted inserted replaced
1210:7798f9e88bf9 1211:b659a7cee935
     1 /*
       
     2  * Copyright 1995-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 
       
    26 #ifdef HEADLESS
       
    27     #error This file should not be included in headless library
       
    28 #endif
       
    29 
       
    30 #include "awt_p.h"
       
    31 #include "canvas.h"
       
    32 #include "java_awt_TextArea.h"
       
    33 #include "java_awt_Cursor.h"
       
    34 #include "java_awt_Component.h"
       
    35 #include "java_awt_Color.h"
       
    36 #include "java_awt_AWTEvent.h"
       
    37 #include "java_awt_Font.h"
       
    38 #include "java_awt_event_MouseWheelEvent.h"
       
    39 #include "sun_awt_motif_MTextAreaPeer.h"
       
    40 #include "sun_awt_motif_MComponentPeer.h"
       
    41 
       
    42 #include "awt_Component.h"
       
    43 #include "awt_Cursor.h"
       
    44 #include "awt_TextArea.h"
       
    45 
       
    46 #include <jni.h>
       
    47 #include <jni_util.h>
       
    48 #include "multi_font.h"
       
    49 
       
    50 extern struct MComponentPeerIDs mComponentPeerIDs;
       
    51 extern struct CursorIDs cursorIDs;
       
    52 extern AwtGraphicsConfigDataPtr
       
    53     copyGraphicsConfigToPeer(JNIEnv *env, jobject this);
       
    54 struct TextAreaIDs textAreaIDs;
       
    55 struct MTextAreaPeerIDs mTextAreaPeerIDs;
       
    56 
       
    57 /*
       
    58  * Class:     java_awt_TextArea
       
    59  * Method:    initIDs
       
    60  * Signature: ()V
       
    61  */
       
    62 
       
    63 /* This function gets called from the static initializer for TextArea.java
       
    64    to initialize the fieldIDs for fields that may be accessed from C */
       
    65 
       
    66 JNIEXPORT void JNICALL
       
    67 Java_java_awt_TextArea_initIDs
       
    68   (JNIEnv *env, jclass cls)
       
    69 {
       
    70     textAreaIDs.scrollbarVisibility =
       
    71       (*env)->GetFieldID(env, cls, "scrollbarVisibility", "I");
       
    72 }
       
    73 
       
    74 /*
       
    75  * Class:     sun_awt_motif_MTextAreaPeer
       
    76  * Method:    initIDs
       
    77  * Signature: ()V
       
    78  */
       
    79 
       
    80 /* This function gets called from the static initializer for
       
    81    MTextAreaPeer.java to initialize the fieldIDs for fields that may
       
    82    be accessed from C */
       
    83 JNIEXPORT void JNICALL
       
    84 Java_sun_awt_motif_MTextAreaPeer_initIDs
       
    85   (JNIEnv *env, jclass cls)
       
    86 {
       
    87     mTextAreaPeerIDs.firstChangeSkipped =
       
    88       (*env)->GetFieldID(env, cls, "firstChangeSkipped", "Z");
       
    89 }
       
    90 
       
    91 /*
       
    92  * client_data is MTextAreaPeer instance
       
    93  */
       
    94 void
       
    95 TextArea_valueChanged(Widget w, XtPointer client_data, XtPointer call_data)
       
    96 {
       
    97     JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);
       
    98     jboolean skipped;
       
    99 
       
   100     skipped = (*env)->GetBooleanField(env, (jobject) client_data,
       
   101                                       mTextAreaPeerIDs.firstChangeSkipped);
       
   102     if (!(*env)->ExceptionOccurred(env)) {
       
   103         if (skipped == JNI_FALSE) {
       
   104             (*env)->SetBooleanField(env, (jobject) client_data,
       
   105                                     mTextAreaPeerIDs.firstChangeSkipped,
       
   106                                     JNI_TRUE);
       
   107         } else {
       
   108             JNU_CallMethodByName(env, NULL, (jobject) client_data,
       
   109                                  "valueChanged", "()V");
       
   110         }
       
   111     }
       
   112 
       
   113     if ((*env)->ExceptionOccurred(env)) {
       
   114         (*env)->ExceptionDescribe(env);
       
   115         (*env)->ExceptionClear(env);
       
   116     }
       
   117 }
       
   118 
       
   119 extern void Text_handlePaste(Widget w, XtPointer client_data, XEvent * event,
       
   120                              Boolean * cont);
       
   121 
       
   122 /*
       
   123  * Class:     sun_awt_motif_MTextAreaPeer
       
   124  * Method:    pCreate
       
   125  * Signature: (Lsun/awt/motif/MComponentPeer;)V
       
   126  */
       
   127 JNIEXPORT void JNICALL Java_sun_awt_motif_MTextAreaPeer_pCreate
       
   128   (JNIEnv *env, jobject this, jobject parent)
       
   129 {
       
   130     struct TextAreaData *tdata;
       
   131 #define MAX_ARGC 30
       
   132     Arg args[MAX_ARGC];
       
   133     int32_t argc;
       
   134     struct ComponentData *wdata;
       
   135     jobject target;
       
   136     Pixel bg;
       
   137     int32_t sbVisibility;
       
   138     Boolean wordWrap = False, hsb = False, vsb = False;
       
   139     jobject globalRef = awtJNI_CreateAndSetGlobalRef(env, this);
       
   140     AwtGraphicsConfigDataPtr adata;
       
   141     char *nonEmptyText = "* will never be shown *";
       
   142 
       
   143     AWT_LOCK();
       
   144 
       
   145     adata = copyGraphicsConfigToPeer(env, this);
       
   146 
       
   147     if (JNU_IsNull(env, parent)) {
       
   148         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   149         AWT_UNLOCK();
       
   150         return;
       
   151     }
       
   152     wdata = (struct ComponentData *)
       
   153         JNU_GetLongFieldAsPtr(env,parent,mComponentPeerIDs.pData);
       
   154     if (wdata == NULL) {
       
   155         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   156         AWT_UNLOCK();
       
   157         return;
       
   158     }
       
   159     target = (*env)->GetObjectField(env, this, mComponentPeerIDs.target);
       
   160 
       
   161     tdata = ZALLOC(TextAreaData);
       
   162     JNU_SetLongFieldFromPtr(env,this,mComponentPeerIDs.pData,tdata);
       
   163 
       
   164     if (tdata == NULL) {
       
   165         JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError");
       
   166         AWT_UNLOCK();
       
   167         return;
       
   168     }
       
   169     XtVaGetValues(wdata->widget, XmNbackground, &bg, NULL);
       
   170 
       
   171     sbVisibility = (*env)->GetIntField(env, target,
       
   172                                        textAreaIDs.scrollbarVisibility);
       
   173     switch (sbVisibility) {
       
   174         case java_awt_TextArea_SCROLLBARS_NONE:
       
   175             wordWrap = True;
       
   176             hsb = False;
       
   177             vsb = False;
       
   178             break;
       
   179 
       
   180         case java_awt_TextArea_SCROLLBARS_VERTICAL_ONLY:
       
   181             wordWrap = True;
       
   182             hsb = False;
       
   183             vsb = True;
       
   184             break;
       
   185 
       
   186         case java_awt_TextArea_SCROLLBARS_HORIZONTAL_ONLY:
       
   187             wordWrap = False;
       
   188             hsb = True;
       
   189             vsb = False;
       
   190             break;
       
   191 
       
   192         default:
       
   193         case java_awt_TextArea_SCROLLBARS_BOTH:
       
   194             wordWrap = False;
       
   195             hsb = True;
       
   196             vsb = True;
       
   197             break;
       
   198     }
       
   199 
       
   200     argc = 0;
       
   201     XtSetArg(args[argc], XmNrecomputeSize, False);
       
   202     argc++;
       
   203     XtSetArg(args[argc], XmNx, 0);
       
   204     argc++;
       
   205     XtSetArg(args[argc], XmNy, 0);
       
   206     argc++;
       
   207     XtSetArg(args[argc], XmNbackground, bg);
       
   208     argc++;
       
   209     XtSetArg(args[argc], XmNeditMode, XmMULTI_LINE_EDIT);
       
   210     argc++;
       
   211     XtSetArg(args[argc], XmNwordWrap, wordWrap);
       
   212     argc++;
       
   213     XtSetArg(args[argc], XmNscrollHorizontal, hsb);
       
   214     argc++;
       
   215     XtSetArg(args[argc], XmNscrollVertical, vsb);
       
   216     argc++;
       
   217     XtSetArg(args[argc], XmNmarginHeight, 2);
       
   218     argc++;
       
   219     XtSetArg(args[argc], XmNmarginWidth, 2);
       
   220     argc++;
       
   221     XtSetArg(args[argc], XmNuserData, (XtPointer) globalRef);
       
   222     argc++;
       
   223     XtSetArg (args[argc], XmNscreen,
       
   224               ScreenOfDisplay(awt_display,
       
   225                               adata->awt_visInfo.screen));
       
   226     argc++;
       
   227     XtSetArg(args[argc], XmNfontList, getMotifFontList());
       
   228     argc++;
       
   229 
       
   230     /* Initialize with a non-empty text, so the
       
   231      * TextArea_valueChanged callback will be called
       
   232      * even if the following conditions are true:
       
   233      * 1. TextArea constructed with an empty initial text.
       
   234      * 2. setText() with an empty argument is called
       
   235      *    immediately after the TextArea component is created.
       
   236      * For more details please see #4028580.
       
   237      */
       
   238     XtSetArg(args[argc], XmNvalue, nonEmptyText);
       
   239     argc++;
       
   240 
       
   241     DASSERT(!(argc > MAX_ARGC));
       
   242     tdata->txt = XmCreateScrolledText(wdata->widget, "textA",
       
   243                                       args, argc);
       
   244     tdata->comp.widget = XtParent(tdata->txt);
       
   245 
       
   246     /* Bug 4208972. Give the ScrolledWindow a minimum size. */
       
   247     XtVaSetValues(tdata->comp.widget,
       
   248         XmNwidth,  1,
       
   249         XmNheight, 1, NULL);
       
   250 
       
   251     XtSetMappedWhenManaged(tdata->comp.widget, False);
       
   252     XtManageChild(tdata->txt);
       
   253     XtManageChild(tdata->comp.widget);
       
   254 
       
   255     XtAddCallback(tdata->txt,
       
   256                   XmNvalueChangedCallback,
       
   257                   TextArea_valueChanged,
       
   258                   (XtPointer) globalRef);
       
   259 
       
   260     XtAddEventHandler(tdata->txt, FocusChangeMask,
       
   261                       True, awt_canvas_event_handler, globalRef);
       
   262 
       
   263     XtInsertEventHandler(tdata->txt,
       
   264                          KeyPressMask,
       
   265                          False, Text_handlePaste, (XtPointer) globalRef,
       
   266                          XtListHead);
       
   267 
       
   268     awt_addWidget(tdata->txt, tdata->comp.widget, globalRef,
       
   269                   java_awt_AWTEvent_KEY_EVENT_MASK |
       
   270                   java_awt_AWTEvent_MOUSE_EVENT_MASK |
       
   271                   java_awt_AWTEvent_MOUSE_MOTION_EVENT_MASK);
       
   272     /*
       
   273      * Fix for BugTraq ID 4349615.
       
   274      * Unregister Motif drop site to prevent it from crash
       
   275      * when dropping java objects.
       
   276      */
       
   277     XmDropSiteUnregister(tdata->txt);
       
   278 
       
   279     AWT_UNLOCK();
       
   280 }
       
   281 
       
   282 /*
       
   283  * Class:     sun_awt_motif_MTextAreaPeer
       
   284  * Method:    getExtraWidth
       
   285  * Signature: ()I
       
   286  */
       
   287 JNIEXPORT jint JNICALL Java_sun_awt_motif_MTextAreaPeer_getExtraWidth
       
   288   (JNIEnv *env, jobject this)
       
   289 {
       
   290     struct TextAreaData *tdata;
       
   291     Dimension spacing, shadowThickness, textMarginWidth, sbWidth;
       
   292     Widget verticalScrollBar;
       
   293 
       
   294     AWT_LOCK();
       
   295 
       
   296     tdata = (struct TextAreaData *) JNU_GetLongFieldAsPtr(env,this,mComponentPeerIDs.pData);
       
   297 
       
   298     if (tdata == NULL || tdata->txt == NULL) {
       
   299         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   300         AWT_UNLOCK();
       
   301         return 0;
       
   302     }
       
   303     XtVaGetValues(tdata->txt, XmNmarginWidth, &textMarginWidth, NULL);
       
   304     XtVaGetValues(tdata->comp.widget,
       
   305                   XmNspacing, &spacing,
       
   306                   XmNverticalScrollBar, &verticalScrollBar,
       
   307                   NULL);
       
   308     if (verticalScrollBar != NULL) {
       
   309         /* Assumption:  shadowThickness same for scrollbars and text area */
       
   310         XtVaGetValues(verticalScrollBar,
       
   311                       XmNwidth, &sbWidth,
       
   312                       XmNshadowThickness, &shadowThickness,
       
   313                       NULL);
       
   314     } else {
       
   315         sbWidth = 0;
       
   316         shadowThickness = 0;
       
   317     }
       
   318 
       
   319     AWT_UNLOCK();
       
   320 
       
   321     return (jint) (sbWidth + spacing + 2 * textMarginWidth + 4 * shadowThickness);
       
   322 }
       
   323 
       
   324 /*
       
   325  * Class:     sun_awt_motif_MTextAreaPeer
       
   326  * Method:    getExtraHeight
       
   327  * Signature: ()I
       
   328  */
       
   329 JNIEXPORT jint JNICALL Java_sun_awt_motif_MTextAreaPeer_getExtraHeight
       
   330   (JNIEnv *env, jobject this)
       
   331 {
       
   332     struct TextAreaData *tdata;
       
   333     Dimension spacing, shadowThickness, textMarginHeight, sbHeight;
       
   334     Dimension sbShadowThickness, highlightThickness, sbHighlightThickness;
       
   335     int32_t height;
       
   336     Widget horizontalScrollBar;
       
   337 
       
   338     AWT_LOCK();
       
   339 
       
   340     tdata = (struct TextAreaData *) JNU_GetLongFieldAsPtr(env,this,mComponentPeerIDs.pData);
       
   341 
       
   342     if (tdata == NULL || tdata->txt == NULL) {
       
   343         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   344         AWT_UNLOCK();
       
   345         return 0;
       
   346     }
       
   347 
       
   348     XtVaGetValues(tdata->txt, XmNmarginHeight, &textMarginHeight,
       
   349                               XmNshadowThickness, &shadowThickness,
       
   350                               XmNhighlightThickness, &highlightThickness, NULL);
       
   351     height = 2 * (textMarginHeight + shadowThickness + highlightThickness);
       
   352 
       
   353     XtVaGetValues(tdata->comp.widget,
       
   354                   XmNspacing, &spacing,
       
   355                   XmNhorizontalScrollBar, &horizontalScrollBar,
       
   356                   NULL);
       
   357 
       
   358     if (horizontalScrollBar != NULL) {
       
   359         XtVaGetValues(horizontalScrollBar,
       
   360                       XmNshadowThickness, &sbShadowThickness,
       
   361                       XmNhighlightThickness, &sbHighlightThickness,
       
   362                       XmNheight, &sbHeight,
       
   363                       NULL);
       
   364         height += sbHeight + spacing
       
   365                 + 2 * (sbShadowThickness + sbHighlightThickness);
       
   366     }
       
   367 
       
   368     AWT_UNLOCK();
       
   369 
       
   370     return (jint)height;
       
   371 }
       
   372 
       
   373 /*
       
   374  * Class:     sun_awt_motif_MTextAreaPeer
       
   375  * Method:    setTextBackground
       
   376  * Signature: (Ljava/awt/Color;)V
       
   377  */
       
   378 JNIEXPORT void JNICALL Java_sun_awt_motif_MTextAreaPeer_setTextBackground
       
   379   (JNIEnv *env, jobject this, jobject c)
       
   380 {
       
   381     struct TextAreaData *tdata;
       
   382     Pixel color;
       
   383 
       
   384     AWT_LOCK();
       
   385 
       
   386     tdata = (struct TextAreaData *) JNU_GetLongFieldAsPtr(env,this,mComponentPeerIDs.pData);
       
   387 
       
   388     if (tdata == NULL || tdata->txt == NULL || JNU_IsNull(env, c)) {
       
   389         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   390         AWT_UNLOCK();
       
   391         return;
       
   392     }
       
   393     color = awtJNI_GetColor(env, c);
       
   394     XtVaSetValues(tdata->txt,
       
   395                   XmNbackground, color,
       
   396                   NULL);
       
   397 
       
   398     AWT_FLUSH_UNLOCK();
       
   399 }
       
   400 
       
   401 /*
       
   402  * Class:     sun_awt_motif_MTextAreaPeer
       
   403  * Method:    pSetEditable
       
   404  * Signature: (Z)V
       
   405  */
       
   406 JNIEXPORT void JNICALL Java_sun_awt_motif_MTextAreaPeer_pSetEditable
       
   407   (JNIEnv *env, jobject this, jboolean editable)
       
   408 {
       
   409     struct TextAreaData *tdata;
       
   410 
       
   411     AWT_LOCK();
       
   412 
       
   413     tdata = (struct TextAreaData *) JNU_GetLongFieldAsPtr(env,this,mComponentPeerIDs.pData);
       
   414 
       
   415     if (tdata == NULL || tdata->txt == NULL) {
       
   416         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   417         AWT_UNLOCK();
       
   418         return;
       
   419     }
       
   420     XtVaSetValues(tdata->txt,
       
   421                   XmNeditable, (editable ? True : False),
       
   422                   XmNcursorPositionVisible, (editable ? True : False),
       
   423                   NULL);
       
   424 
       
   425     AWT_FLUSH_UNLOCK();
       
   426 }
       
   427 
       
   428 /*
       
   429  * Class:     sun_awt_motif_MTextAreaPeer
       
   430  * Method:    select
       
   431  * Signature: (II)V
       
   432  */
       
   433 JNIEXPORT void JNICALL Java_sun_awt_motif_MTextAreaPeer_select
       
   434   (JNIEnv *env, jobject this, jint start, jint end)
       
   435 {
       
   436     struct TextAreaData *tdata;
       
   437 
       
   438     AWT_LOCK();
       
   439 
       
   440     tdata = (struct TextAreaData *) JNU_GetLongFieldAsPtr(env,this,mComponentPeerIDs.pData);
       
   441 
       
   442     if (tdata == NULL || tdata->txt == NULL) {
       
   443         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   444         AWT_UNLOCK();
       
   445         return;
       
   446     }
       
   447     XmTextSetSelection(tdata->txt, (XmTextPosition) start, (XmTextPosition) end, 0);
       
   448     AWT_FLUSH_UNLOCK();
       
   449 }
       
   450 
       
   451 /*
       
   452  * Class:     sun_awt_motif_MTextAreaPeer
       
   453  * Method:    getSelectionStart
       
   454  * Signature: ()I
       
   455  */
       
   456 JNIEXPORT jint JNICALL Java_sun_awt_motif_MTextAreaPeer_getSelectionStart
       
   457   (JNIEnv *env, jobject this)
       
   458 {
       
   459     struct TextAreaData *tdata;
       
   460     XmTextPosition start, end, pos;
       
   461 
       
   462     AWT_LOCK();
       
   463 
       
   464     tdata = (struct TextAreaData *) JNU_GetLongFieldAsPtr(env,this,mComponentPeerIDs.pData);
       
   465 
       
   466     if (tdata == NULL || tdata->txt == NULL) {
       
   467         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   468         AWT_UNLOCK();
       
   469         return 0;
       
   470     }
       
   471     if (XmTextGetSelectionPosition(tdata->txt, &start, &end) &&
       
   472                                              (start != end)) {
       
   473         pos = start;
       
   474     } else {
       
   475         pos = XmTextGetInsertionPosition(tdata->txt);
       
   476     }
       
   477     AWT_UNLOCK();
       
   478 
       
   479     return (jint) pos;
       
   480 }
       
   481 
       
   482 /*
       
   483  * Class:     sun_awt_motif_MTextAreaPeer
       
   484  * Method:    getSelectionEnd
       
   485  * Signature: ()I
       
   486  */
       
   487 JNIEXPORT jint JNICALL Java_sun_awt_motif_MTextAreaPeer_getSelectionEnd
       
   488   (JNIEnv *env, jobject this)
       
   489 {
       
   490     struct TextAreaData *tdata;
       
   491     XmTextPosition start, end, pos;
       
   492 
       
   493     AWT_LOCK();
       
   494 
       
   495     tdata = (struct TextAreaData *) JNU_GetLongFieldAsPtr(env,this,mComponentPeerIDs.pData);
       
   496     if (tdata == NULL || tdata->txt == NULL) {
       
   497         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   498         AWT_UNLOCK();
       
   499         return 0;
       
   500     }
       
   501     if (XmTextGetSelectionPosition(tdata->txt, &start, &end) &&
       
   502                                              (start != end)) {
       
   503         pos = end;
       
   504     } else {
       
   505         pos = XmTextGetInsertionPosition(tdata->txt);
       
   506     }
       
   507     AWT_UNLOCK();
       
   508 
       
   509     return (jint) pos;
       
   510 }
       
   511 
       
   512 /*
       
   513  * Class:     sun_awt_motif_MTextAreaPeer
       
   514  * Method:    setText
       
   515  * Signature: (Ljava/lang/String;)V
       
   516  */
       
   517 JNIEXPORT void JNICALL Java_sun_awt_motif_MTextAreaPeer_setText
       
   518   (JNIEnv *env, jobject this, jstring txt)
       
   519 {
       
   520     struct TextAreaData *tdata;
       
   521     char *cTxt;
       
   522     jobject font = awtJNI_GetFont(env, this);
       
   523 
       
   524     if (JNU_IsNull(env, txt)) {
       
   525         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   526         return;
       
   527     }
       
   528     AWT_LOCK();
       
   529 
       
   530     tdata = (struct TextAreaData *)
       
   531       JNU_GetLongFieldAsPtr(env,this,mComponentPeerIDs.pData);
       
   532     if (tdata == NULL || tdata->txt == NULL) {
       
   533         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   534         AWT_UNLOCK();
       
   535         return;
       
   536     }
       
   537     cTxt = (char *) JNU_GetStringPlatformChars(env, txt, NULL);
       
   538 
       
   539     if (cTxt == NULL) {
       
   540         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   541         AWT_UNLOCK();
       
   542         return;
       
   543     }
       
   544     XtVaSetValues(tdata->txt, XmNvalue, cTxt, NULL);
       
   545 
       
   546     if (cTxt != NULL) {
       
   547         JNU_ReleaseStringPlatformChars(env, txt, cTxt);
       
   548     }
       
   549     AWT_FLUSH_UNLOCK();
       
   550 }
       
   551 
       
   552 /*
       
   553  * Class:     sun_awt_motif_MTextAreaPeer
       
   554  * Method:    getText
       
   555  * Signature: ()Ljava/lang/String;
       
   556  */
       
   557 JNIEXPORT jstring JNICALL Java_sun_awt_motif_MTextAreaPeer_getText
       
   558   (JNIEnv *env, jobject this)
       
   559 {
       
   560     struct TextAreaData *tdata;
       
   561     char *cTxt;
       
   562     jstring rval;
       
   563     jobject font = awtJNI_GetFont(env, this);
       
   564 
       
   565     AWT_LOCK();
       
   566 
       
   567     tdata = (struct TextAreaData *)
       
   568       JNU_GetLongFieldAsPtr(env,this, mComponentPeerIDs.pData);
       
   569     if (tdata == NULL || tdata->txt == NULL) {
       
   570         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   571         AWT_UNLOCK();
       
   572         return NULL;
       
   573     }
       
   574     cTxt = XmTextGetString(tdata->txt);
       
   575 
       
   576     rval = JNU_NewStringPlatform(env, (const char *) cTxt);
       
   577 
       
   578     XtFree(cTxt);
       
   579 
       
   580     AWT_UNLOCK();
       
   581 
       
   582     return rval;
       
   583 }
       
   584 
       
   585 /*
       
   586  * Class:     sun_awt_motif_MTextAreaPeer
       
   587  * Method:    insert
       
   588  * Signature: (Ljava/lang/String;I)V
       
   589  */
       
   590 JNIEXPORT void JNICALL Java_sun_awt_motif_MTextAreaPeer_insert
       
   591   (JNIEnv *env, jobject this, jstring txt, jint pos)
       
   592 {
       
   593     struct TextAreaData *tdata;
       
   594     char *cTxt;
       
   595     jobject font = awtJNI_GetFont(env, this);
       
   596 
       
   597     if (JNU_IsNull(env, txt)) {
       
   598         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   599         return;
       
   600     }
       
   601     AWT_LOCK();
       
   602     tdata = (struct TextAreaData *)
       
   603       JNU_GetLongFieldAsPtr(env, this, mComponentPeerIDs.pData);
       
   604     if (tdata == NULL || tdata->txt == NULL) {
       
   605         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   606         AWT_UNLOCK();
       
   607         return;
       
   608     }
       
   609     cTxt = (char *) JNU_GetStringPlatformChars(env, txt, NULL);
       
   610 
       
   611     if (cTxt == NULL) {
       
   612         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   613         AWT_UNLOCK();
       
   614         return;
       
   615     }
       
   616     XmTextInsert(tdata->txt, (XmTextPosition) pos, cTxt);
       
   617 
       
   618     if (cTxt != NULL) {
       
   619         JNU_ReleaseStringPlatformChars(env, txt, cTxt);
       
   620     }
       
   621     AWT_FLUSH_UNLOCK();
       
   622 }
       
   623 
       
   624 /*
       
   625  * Class:     sun_awt_motif_MTextAreaPeer
       
   626  * Method:    replaceRange
       
   627  * Signature: (Ljava/lang/String;II)V
       
   628  */
       
   629 JNIEXPORT void JNICALL Java_sun_awt_motif_MTextAreaPeer_replaceRange
       
   630   (JNIEnv *env, jobject this, jstring txt, jint start, jint end)
       
   631 {
       
   632     struct TextAreaData *tdata;
       
   633     char *cTxt;
       
   634     jobject font = awtJNI_GetFont(env, this);
       
   635 
       
   636     if (JNU_IsNull(env, txt)) {
       
   637         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   638         return;
       
   639     }
       
   640     AWT_LOCK();
       
   641     tdata = (struct TextAreaData *)
       
   642       JNU_GetLongFieldAsPtr(env, this, mComponentPeerIDs.pData);
       
   643     if (tdata == NULL || tdata->txt == NULL) {
       
   644         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   645         AWT_UNLOCK();
       
   646         return;
       
   647     }
       
   648     cTxt = (char *) JNU_GetStringPlatformChars(env, txt, NULL);
       
   649 
       
   650     if (cTxt == NULL) {
       
   651         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   652         AWT_UNLOCK();
       
   653         return;
       
   654     }
       
   655     XmTextReplace(tdata->txt,
       
   656                   (XmTextPosition) start,
       
   657                   (XmTextPosition) end,
       
   658                   cTxt);
       
   659 
       
   660     if (cTxt != NULL) {
       
   661         JNU_ReleaseStringPlatformChars(env, txt, cTxt);
       
   662     }
       
   663     AWT_FLUSH_UNLOCK();
       
   664 }
       
   665 
       
   666 /*
       
   667  * Class:     sun_awt_motif_MTextAreaPeer
       
   668  * Method:    setFont
       
   669  * Signature: (Ljava/awt/Font;)V
       
   670  */
       
   671 JNIEXPORT void JNICALL Java_sun_awt_motif_MTextAreaPeer_setFont
       
   672   (JNIEnv *env, jobject this, jobject f)
       
   673 {
       
   674     struct TextAreaData *tdata;
       
   675     struct FontData *fdata;
       
   676     XmFontList fontlist;
       
   677     char *err;
       
   678     XmFontListEntry fontentry;
       
   679 
       
   680     if (JNU_IsNull(env, f)) {
       
   681         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   682         return;
       
   683     }
       
   684     AWT_LOCK();
       
   685 
       
   686     fdata = awtJNI_GetFontData(env, f, &err);
       
   687     if (fdata == NULL) {
       
   688         JNU_ThrowInternalError(env, err);
       
   689         AWT_UNLOCK();
       
   690         return;
       
   691     }
       
   692     tdata = (struct TextAreaData *)
       
   693       JNU_GetLongFieldAsPtr(env, this, mComponentPeerIDs.pData);
       
   694     if (tdata == NULL || tdata->comp.widget == NULL) {
       
   695         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   696         AWT_UNLOCK();
       
   697         return;
       
   698     }
       
   699     if (awtJNI_IsMultiFont(env, f)) {
       
   700         if (fdata->xfs == NULL) {
       
   701             fdata->xfs = awtJNI_MakeFontSet(env, f);
       
   702         }
       
   703         if (fdata->xfs != NULL) {
       
   704             fontentry = XmFontListEntryCreate("labelFont",
       
   705                                               XmFONT_IS_FONTSET,
       
   706                                               (XtPointer) (fdata->xfs));
       
   707             fontlist = XmFontListAppendEntry(NULL, fontentry);
       
   708             /*
       
   709              * Some versions of motif have a bug in
       
   710              * XmFontListEntryFree() which causes it to free more than it
       
   711              * should.  Use XtFree() instead.  See O'Reilly's
       
   712              * Motif Reference Manual for more information.
       
   713              */
       
   714             XmFontListEntryFree(&fontentry);
       
   715 
       
   716         } else {
       
   717             fontlist = XmFontListCreate(fdata->xfont, "labelFont");
       
   718         }
       
   719     } else {
       
   720         fontlist = XmFontListCreate(fdata->xfont, "labelFont");
       
   721     }
       
   722 
       
   723     if (fontlist != NULL) {
       
   724         Dimension textw, texth;
       
   725         Dimension w, h;
       
   726 
       
   727         XtVaGetValues(tdata->txt,
       
   728                       XmNwidth, &textw,
       
   729                       XmNheight, &texth,
       
   730                       NULL);
       
   731         XtVaGetValues(tdata->comp.widget,
       
   732                       XmNwidth, &w,
       
   733                       XmNheight, &h,
       
   734                       NULL);
       
   735 
       
   736         /* Must set width/height when we set the font, else
       
   737          * Motif resets the text to a single row.
       
   738          */
       
   739         XtVaSetValues(tdata->txt,
       
   740                       XmNfontList, fontlist,
       
   741                       XmNwidth, textw,
       
   742                       XmNheight, texth,
       
   743                       NULL);
       
   744         XtVaSetValues(tdata->comp.widget,
       
   745                       XmNwidth, w,
       
   746                       XmNheight, h,
       
   747                       NULL);
       
   748 
       
   749         XmFontListFree(fontlist);
       
   750     } else {
       
   751         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   752     }
       
   753 
       
   754     AWT_UNLOCK();
       
   755 }
       
   756 
       
   757 /*
       
   758  * Class:     sun_awt_motif_MTextAreaPeer
       
   759  * Method:    setCaretPosition
       
   760  * Signature: (I)V
       
   761  */
       
   762 JNIEXPORT void JNICALL Java_sun_awt_motif_MTextAreaPeer_setCaretPosition
       
   763   (JNIEnv *env, jobject this, jint pos)
       
   764 {
       
   765     struct TextAreaData *tdata;
       
   766 
       
   767     AWT_LOCK();
       
   768 
       
   769     tdata = (struct TextAreaData *)
       
   770       JNU_GetLongFieldAsPtr(env, this, mComponentPeerIDs.pData);
       
   771 
       
   772     if (tdata == NULL || tdata->txt == NULL) {
       
   773         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   774         AWT_UNLOCK();
       
   775         return;
       
   776     }
       
   777     XmTextSetInsertionPosition(tdata->txt, (XmTextPosition) pos);
       
   778 
       
   779     AWT_FLUSH_UNLOCK();
       
   780 }
       
   781 
       
   782 /*
       
   783  * Class:     sun_awt_motif_MTextAreaPeer
       
   784  * Method:    getCaretPosition
       
   785  * Signature: ()I
       
   786  */
       
   787 JNIEXPORT jint JNICALL Java_sun_awt_motif_MTextAreaPeer_getCaretPosition
       
   788   (JNIEnv *env, jobject this)
       
   789 {
       
   790     struct TextAreaData *tdata;
       
   791     XmTextPosition pos;
       
   792 
       
   793     AWT_LOCK();
       
   794 
       
   795     tdata = (struct TextAreaData *)
       
   796       JNU_GetLongFieldAsPtr(env, this, mComponentPeerIDs.pData);
       
   797 
       
   798     if (tdata == NULL || tdata->txt == NULL) {
       
   799         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   800         AWT_UNLOCK();
       
   801         return 0;
       
   802     }
       
   803     pos = XmTextGetInsertionPosition(tdata->txt);
       
   804 
       
   805     AWT_UNLOCK();
       
   806 
       
   807     return (jint) pos;
       
   808 }
       
   809 
       
   810 /*
       
   811  * Class:     sun_awt_motif_MTextAreaPeer
       
   812  * Method:    pShow
       
   813  * Signature: ()V
       
   814  */
       
   815 JNIEXPORT void JNICALL Java_sun_awt_motif_MTextAreaPeer_pShow2
       
   816   (JNIEnv *env, jobject this)
       
   817 {
       
   818     struct TextAreaData *tdata;
       
   819 
       
   820     AWT_LOCK();
       
   821     tdata = (struct TextAreaData *) JNU_GetLongFieldAsPtr(env,this,mComponentPeerIDs.pData);
       
   822     if (tdata == NULL || tdata->comp.widget == NULL) {
       
   823         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   824         AWT_UNLOCK();
       
   825         return;
       
   826     }
       
   827 
       
   828     awt_util_show(tdata->comp.widget);
       
   829     AWT_FLUSH_UNLOCK();
       
   830 }
       
   831 
       
   832 
       
   833 /*
       
   834  * Class:     sun_awt_motif_MTextAreaPeer
       
   835  * Method:    pMakeCursorVisible
       
   836  * Signature: ()V
       
   837  */
       
   838 JNIEXPORT void JNICALL Java_sun_awt_motif_MTextAreaPeer_pMakeCursorVisible
       
   839   (JNIEnv *env, jobject this)
       
   840 {
       
   841     struct TextAreaData *tdata;
       
   842 
       
   843     AWT_LOCK();
       
   844     tdata = (struct TextAreaData *) JNU_GetLongFieldAsPtr(env,this,mComponentPeerIDs.pData);
       
   845     if (tdata == NULL || tdata->comp.widget == NULL) {
       
   846         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   847         AWT_UNLOCK();
       
   848         return;
       
   849     }
       
   850 
       
   851     AWT_FLUSH_UNLOCK();
       
   852 }
       
   853 
       
   854 /*
       
   855  * Class:     sun_awt_motif_MTextAreaPeer
       
   856  * Method:    pSetCursor
       
   857  * Signature: (L/java/awt/Cursor;)V
       
   858  */
       
   859 JNIEXPORT void JNICALL Java_sun_awt_motif_MTextAreaPeer_pSetCursor
       
   860   (JNIEnv *env, jobject this, jobject cursor)
       
   861 {
       
   862     Cursor xcursor;
       
   863     struct TextAreaData         *tdata;
       
   864 
       
   865     AWT_LOCK();
       
   866     tdata = (struct TextAreaData *)
       
   867         JNU_GetLongFieldAsPtr(env, this, mComponentPeerIDs.pData);
       
   868     if (tdata == NULL || tdata->comp.widget == NULL || JNU_IsNull(env, cursor)) {
       
   869         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   870         AWT_UNLOCK();
       
   871         return;
       
   872     }
       
   873 
       
   874     awt_util_setCursor(tdata->txt, getCursor(env, cursor));
       
   875 
       
   876     AWT_FLUSH_UNLOCK();
       
   877 }
       
   878 
       
   879 /*
       
   880  * Class:     sun_awt_motif_MTextAreaPeer
       
   881  * Method:    nativeHandleMouseWheel
       
   882  * Signature: (III)V
       
   883  */
       
   884 JNIEXPORT void JNICALL Java_sun_awt_motif_MTextAreaPeer_nativeHandleMouseWheel
       
   885   (JNIEnv *env, jobject this, jint scrollType, jint scrollAmt, jint wheelAmt)
       
   886 {
       
   887     struct TextAreaData         *tdata;
       
   888     Widget text = NULL;
       
   889     Widget scroll = NULL;
       
   890 
       
   891     AWT_LOCK();
       
   892     tdata = (struct TextAreaData *)
       
   893         JNU_GetLongFieldAsPtr(env, this, mComponentPeerIDs.pData);
       
   894     if (tdata == NULL || tdata->comp.widget == NULL) {
       
   895         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   896         AWT_UNLOCK();
       
   897         return;
       
   898     }
       
   899     // get the Text widget
       
   900     text = tdata->txt;
       
   901     if (text == NULL) {
       
   902         AWT_UNLOCK();
       
   903         return;
       
   904     }
       
   905 
       
   906     // get the ScrolledWindow
       
   907     scroll = XtParent(text);
       
   908     if (scroll == NULL) {
       
   909         AWT_UNLOCK();
       
   910         return;
       
   911     }
       
   912 
       
   913     awt_util_do_wheel_scroll(scroll, scrollType, scrollAmt, wheelAmt);
       
   914     AWT_UNLOCK();
       
   915 }
       
   916 
       
   917 
       
   918 
       
   919 /*  To be fully implemented in a future release
       
   920  *
       
   921  * Class:     sun_awt_windows_MTextAreaPeer
       
   922  * Method:    getIndexAtPoint
       
   923  * Signature: (II)I
       
   924  *
       
   925 JNIEXPORT jint JNICALL
       
   926 Java_sun_awt_motif_MTextAreaPeer_getIndexAtPoint(JNIEnv *env, jobject self,
       
   927  jint x, jint y)
       
   928 {
       
   929     struct TextAreaData *tdata;
       
   930     XmTextPosition pos;
       
   931 
       
   932     AWT_LOCK();
       
   933 
       
   934     tdata = (struct TextAreaData *)
       
   935         JNU_GetLongFieldAsPtr(env,self,mComponentPeerIDs.pData);
       
   936 
       
   937     if (tdata == NULL || tdata->txt == NULL) {
       
   938         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   939         AWT_UNLOCK();
       
   940         return -1;
       
   941     }
       
   942     pos = XmTextXYToPos(tdata->txt, x, y);
       
   943     AWT_UNLOCK();
       
   944 
       
   945     return (jint) pos;
       
   946 }
       
   947 */
       
   948 
       
   949 /*  To be fully implemented in a future release
       
   950  *
       
   951  * Class:     sun_awt_windows_MTextAreaPeer
       
   952  * Method:    getCharacterBounds
       
   953  * Signature: (I)Ljava/awt/Rectangle;
       
   954  *
       
   955 JNIEXPORT jobject JNICALL
       
   956 Java_sun_awt_motif_MTextAreaPeer_getCharacterBounds(JNIEnv *env, jobject self, jint i)
       
   957 {
       
   958 #define Text_FontAscent(tfg)                   (((XmTextWidget)(tfg)) -> \
       
   959                                            text.output->data->font_ascent)
       
   960 #define Text_FontDescent(tfg)                  (((XmTextWidget)(tfg)) -> \
       
   961                                            text.output->data->font_descent)
       
   962 
       
   963     struct TextAreaData *tdata;
       
   964     jobject rect=NULL;
       
   965     Position x=0, y=0;
       
   966     Position next_x=0, next_y=0;
       
   967     int32_t w=0, h=0;
       
   968 
       
   969     AWT_LOCK();
       
   970 
       
   971     tdata = (struct TextAreaData *)
       
   972         JNU_GetLongFieldAsPtr(env,self,mComponentPeerIDs.pData);
       
   973 
       
   974     if (tdata == NULL || tdata->txt == NULL) {
       
   975         JNU_ThrowNullPointerException(env, "NullPointerException");
       
   976         AWT_UNLOCK();
       
   977         return (jobject) NULL;
       
   978     }
       
   979 
       
   980     XmTextPosToXY(tdata->txt, i, &x, &y);
       
   981     y -= Text_FontAscent(tdata->txt);
       
   982     XmTextPosToXY(tdata->txt, i+1, &next_x, &next_y);
       
   983     w = next_x - x;
       
   984     h = Text_FontAscent(tdata->txt) + Text_FontDescent(tdata->txt);
       
   985 
       
   986     AWT_UNLOCK();
       
   987 
       
   988     if (w>0) {
       
   989         jclass clazz;
       
   990         jmethodID mid;
       
   991 
       
   992         clazz = (*env)->FindClass(env, "java/awt/Rectangle");
       
   993         mid = (*env)->GetMethodID(env, clazz, "<init>", "(IIII)V");
       
   994         if (mid != NULL) {
       
   995             rect = (*env)->NewObject(env, clazz, mid, x, y, w, h);
       
   996             if ((*env)->ExceptionOccurred(env)) {
       
   997                 return (jobject) NULL;
       
   998             }
       
   999         }
       
  1000     }
       
  1001     return rect;
       
  1002 }
       
  1003 */