J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 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 | #include "java_awt_Color.h" |
| 32 | #include "java_awt_Font.h" |
| 33 | #include "java_awt_Label.h" |
| 34 | #include "sun_awt_motif_MLabelPeer.h" |
| 35 | #include "sun_awt_motif_MComponentPeer.h" |
| 36 | |
| 37 | #include "awt_Component.h" |
| 38 | |
| 39 | #include "multi_font.h" |
| 40 | #include <jni.h> |
| 41 | #include <jni_util.h> |
| 42 | |
| 43 | extern struct MComponentPeerIDs mComponentPeerIDs; |
| 44 | extern AwtGraphicsConfigDataPtr |
| 45 | copyGraphicsConfigToPeer(JNIEnv *env, jobject this); |
| 46 | |
| 47 | static char emptyString[] = ""; |
| 48 | |
| 49 | |
| 50 | /* |
| 51 | * Class: sun_awt_motif_MLabelPeer |
| 52 | * Method: create |
| 53 | * Signature: (Lsun/awt/motif/MComponentPeer;)V |
| 54 | */ |
| 55 | JNIEXPORT void JNICALL Java_sun_awt_motif_MLabelPeer_create |
| 56 | (JNIEnv *env, jobject this, jobject parent) |
| 57 | { |
| 58 | struct ComponentData *cdata; |
| 59 | struct ComponentData *wdata; |
| 60 | jobject target; |
| 61 | jobject globalRef = awtJNI_CreateAndSetGlobalRef(env, this); |
| 62 | AwtGraphicsConfigDataPtr adata; |
| 63 | AWT_LOCK(); |
| 64 | |
| 65 | if (JNU_IsNull(env, parent)) { |
| 66 | JNU_ThrowNullPointerException(env, "NullPointerException"); |
| 67 | AWT_UNLOCK(); |
| 68 | |
| 69 | return; |
| 70 | } |
| 71 | target = (*env)->GetObjectField(env, this, mComponentPeerIDs.target); |
| 72 | wdata = (struct ComponentData *) |
| 73 | JNU_GetLongFieldAsPtr(env, parent, mComponentPeerIDs.pData); |
| 74 | |
| 75 | if (JNU_IsNull(env, target) || wdata == NULL) { |
| 76 | JNU_ThrowNullPointerException(env, "NullPointerException"); |
| 77 | AWT_UNLOCK(); |
| 78 | |
| 79 | return; |
| 80 | } |
| 81 | cdata = ZALLOC(ComponentData); |
| 82 | if (cdata == NULL) { |
| 83 | JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError"); |
| 84 | AWT_UNLOCK(); |
| 85 | return; |
| 86 | } |
| 87 | JNU_SetLongFieldFromPtr(env, this, mComponentPeerIDs.pData,cdata); |
| 88 | |
| 89 | adata = copyGraphicsConfigToPeer(env, this); |
| 90 | |
| 91 | cdata->widget = XtVaCreateManagedWidget("", |
| 92 | xmLabelWidgetClass, wdata->widget, |
| 93 | XmNhighlightThickness, 0, |
| 94 | XmNalignment, XmALIGNMENT_BEGINNING, |
| 95 | XmNrecomputeSize, False, |
| 96 | XmNuserData, (XtPointer) globalRef, |
| 97 | XmNtraversalOn, True, |
| 98 | XmNscreen, |
| 99 | ScreenOfDisplay(awt_display, |
| 100 | adata->awt_visInfo.screen), |
| 101 | XmNfontList, getMotifFontList(), |
| 102 | NULL); |
| 103 | XtSetMappedWhenManaged(cdata->widget, False); |
| 104 | AWT_UNLOCK(); |
| 105 | } |
| 106 | |
| 107 | /* |
| 108 | * Class: sun_awt_motif_MLabelPeer |
| 109 | * Method: setText |
| 110 | * Signature: (Ljava/lang/String;)V |
| 111 | */ |
| 112 | JNIEXPORT void JNICALL Java_sun_awt_motif_MLabelPeer_setText |
| 113 | (JNIEnv *env, jobject this, jstring label) |
| 114 | { |
| 115 | char *clabel = NULL; |
| 116 | char *clabelEnd; |
| 117 | struct ComponentData *cdata; |
| 118 | XmString xim = NULL; |
| 119 | jobject font; |
| 120 | Boolean isMultiFont; |
| 121 | |
| 122 | AWT_LOCK(); |
| 123 | |
| 124 | font = awtJNI_GetFont(env, this); |
| 125 | isMultiFont = awtJNI_IsMultiFont(env, font); |
| 126 | |
| 127 | cdata = (struct ComponentData *) |
| 128 | JNU_GetLongFieldAsPtr(env,this,mComponentPeerIDs.pData); |
| 129 | if (cdata == NULL || cdata->widget == NULL) { |
| 130 | JNU_ThrowNullPointerException(env, "NullPointerException"); |
| 131 | AWT_UNLOCK(); |
| 132 | return; |
| 133 | } |
| 134 | if (JNU_IsNull(env, label)) { |
| 135 | clabel = emptyString; |
| 136 | } else { |
| 137 | if (isMultiFont) { |
| 138 | if ((*env)->GetStringLength(env, label) <= 0) { |
| 139 | xim = XmStringCreateLocalized(""); |
| 140 | } else { |
| 141 | xim = awtJNI_MakeMultiFontString(env, label, font); |
| 142 | } |
| 143 | } else { |
| 144 | clabel = (char *) JNU_GetStringPlatformChars(env, label, NULL); |
| 145 | |
| 146 | /* scan for any \n's and terminate the string at that point */ |
| 147 | clabelEnd = strchr(clabel, '\n'); |
| 148 | if (clabelEnd != NULL) { |
| 149 | *clabelEnd = '\0'; |
| 150 | } |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | if (!isMultiFont) { |
| 155 | xim = XmStringCreate(clabel, "labelFont"); |
| 156 | } |
| 157 | XtVaSetValues(cdata->widget, XmNlabelString, xim, NULL); |
| 158 | |
| 159 | if (!isMultiFont) { |
| 160 | /* Must test for "" too! */ |
| 161 | if (clabel != NULL && (*clabel != '\0')) { |
| 162 | JNU_ReleaseStringPlatformChars(env, label, (const char *) clabel); |
| 163 | } |
| 164 | } |
| 165 | XmStringFree(xim); |
| 166 | AWT_FLUSH_UNLOCK(); |
| 167 | } |
| 168 | |
| 169 | /* |
| 170 | * Class: sun_awt_motif_MLabelPeer |
| 171 | * Method: setAlignment |
| 172 | * Signature: (I)V |
| 173 | */ |
| 174 | JNIEXPORT void JNICALL Java_sun_awt_motif_MLabelPeer_setAlignment |
| 175 | (JNIEnv *env, jobject this, jint alignment) |
| 176 | { |
| 177 | struct ComponentData *cdata; |
| 178 | |
| 179 | AWT_LOCK(); |
| 180 | |
| 181 | cdata = (struct ComponentData *) |
| 182 | JNU_GetLongFieldAsPtr(env,this,mComponentPeerIDs.pData); |
| 183 | if (cdata == NULL || cdata->widget == NULL) { |
| 184 | JNU_ThrowNullPointerException(env, "NullPointerException"); |
| 185 | AWT_UNLOCK(); |
| 186 | return; |
| 187 | } |
| 188 | switch (alignment) { |
| 189 | case java_awt_Label_LEFT: |
| 190 | XtVaSetValues(cdata->widget, |
| 191 | XmNalignment, XmALIGNMENT_BEGINNING, |
| 192 | NULL); |
| 193 | break; |
| 194 | |
| 195 | case java_awt_Label_CENTER: |
| 196 | XtVaSetValues(cdata->widget, |
| 197 | XmNalignment, XmALIGNMENT_CENTER, |
| 198 | NULL); |
| 199 | break; |
| 200 | |
| 201 | case java_awt_Label_RIGHT: |
| 202 | XtVaSetValues(cdata->widget, |
| 203 | XmNalignment, XmALIGNMENT_END, |
| 204 | NULL); |
| 205 | break; |
| 206 | |
| 207 | default: |
| 208 | break; |
| 209 | } |
| 210 | |
| 211 | AWT_FLUSH_UNLOCK(); |
| 212 | } |