1 /* |
|
2 * Copyright 1995-2002 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 "sun_awt_motif_MComponentPeer.h" |
|
32 #include "sun_awt_motif_MCheckboxPeer.h" |
|
33 #include "java_awt_Checkbox.h" |
|
34 #include "java_awt_CheckboxGroup.h" |
|
35 |
|
36 #include "awt_Component.h" |
|
37 |
|
38 #include "multi_font.h" |
|
39 #include <jni.h> |
|
40 #include <jni_util.h> |
|
41 |
|
42 extern struct MComponentPeerIDs mComponentPeerIDs; |
|
43 extern AwtGraphicsConfigDataPtr |
|
44 copyGraphicsConfigToPeer(JNIEnv *env, jobject this); |
|
45 |
|
46 /* fieldIDs for Checkbox fields that may be accessed from C */ |
|
47 static struct CheckboxIDs { |
|
48 jfieldID label; |
|
49 } checkboxIDs; |
|
50 |
|
51 static char emptyString[] = ""; |
|
52 |
|
53 |
|
54 /* |
|
55 * Class: java_awt_Checkbox |
|
56 * Method: initIDs |
|
57 * Signature: ()V |
|
58 */ |
|
59 |
|
60 /* This function gets called from the static initializer for Checkbox.java |
|
61 to initialize the fieldIDs for fields that may be accessed from C */ |
|
62 |
|
63 JNIEXPORT void JNICALL |
|
64 Java_java_awt_Checkbox_initIDs |
|
65 (JNIEnv *env, jclass cls) |
|
66 { |
|
67 checkboxIDs.label = |
|
68 (*env)->GetFieldID(env, cls, "label", "Ljava/lang/String;"); |
|
69 } |
|
70 |
|
71 /* |
|
72 * client_data is MCheckboxPeer instance pointer |
|
73 */ |
|
74 static void |
|
75 Toggle_callback(Widget w, |
|
76 XtPointer client_data, |
|
77 XmAnyCallbackStruct * call_data) |
|
78 { |
|
79 Boolean state; |
|
80 JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2); |
|
81 |
|
82 XtVaGetValues(w, XmNset, &state, NULL); |
|
83 |
|
84 JNU_CallMethodByName(env, NULL, (jobject) client_data, "action", "(Z)V", state); |
|
85 if ((*env)->ExceptionOccurred(env)) { |
|
86 (*env)->ExceptionDescribe(env); |
|
87 (*env)->ExceptionClear(env); |
|
88 } |
|
89 } |
|
90 |
|
91 /* |
|
92 * Class: sun_awt_motif_MCheckboxPeer |
|
93 * Method: create |
|
94 * Signature: (Lsun/awt/motif/MComponentPeer;)V |
|
95 */ |
|
96 JNIEXPORT void JNICALL Java_sun_awt_motif_MCheckboxPeer_create |
|
97 (JNIEnv * env, jobject this, jobject parent) |
|
98 { |
|
99 jobject target; |
|
100 struct ComponentData *bdata; |
|
101 struct ComponentData *wdata; |
|
102 char *clabel; |
|
103 #define MAX_ARGC 10 |
|
104 Arg args[MAX_ARGC]; |
|
105 Cardinal argc; |
|
106 jobject label; |
|
107 XmString mfstr = NULL; |
|
108 jobject font = awtJNI_GetFont(env, this); |
|
109 jboolean isMultiFont = awtJNI_IsMultiFont(env, font); |
|
110 jobject globalRef = awtJNI_CreateAndSetGlobalRef(env, this); |
|
111 AwtGraphicsConfigDataPtr adata; |
|
112 XmFontList fontlist = NULL; |
|
113 Dimension height; |
|
114 Boolean labelIsEmpty = FALSE; |
|
115 |
|
116 AWT_LOCK(); |
|
117 |
|
118 if (JNU_IsNull(env, parent)) { |
|
119 JNU_ThrowNullPointerException(env, "NullPointerException"); |
|
120 AWT_UNLOCK(); |
|
121 |
|
122 return; |
|
123 } |
|
124 target = (*env)->GetObjectField(env, this, mComponentPeerIDs.target); |
|
125 wdata = (struct ComponentData *) |
|
126 JNU_GetLongFieldAsPtr(env, parent, mComponentPeerIDs.pData); |
|
127 |
|
128 if (JNU_IsNull(env, target) || wdata == NULL) { |
|
129 JNU_ThrowNullPointerException(env, "NullPointerException"); |
|
130 AWT_UNLOCK(); |
|
131 |
|
132 return; |
|
133 } |
|
134 bdata = ZALLOC(ComponentData); |
|
135 if (bdata == NULL) { |
|
136 JNU_ThrowOutOfMemoryError(env, "OutOfMemoryError"); |
|
137 AWT_UNLOCK(); |
|
138 |
|
139 return; |
|
140 } |
|
141 JNU_SetLongFieldFromPtr(env, this, mComponentPeerIDs.pData, bdata); |
|
142 |
|
143 adata = copyGraphicsConfigToPeer(env, this); |
|
144 |
|
145 argc = 0; |
|
146 XtSetArg(args[argc], XmNrecomputeSize, False); |
|
147 argc++; |
|
148 XtSetArg(args[argc], XmNvisibleWhenOff, True); |
|
149 argc++; |
|
150 XtSetArg(args[argc], XmNtraversalOn, True); |
|
151 argc++; |
|
152 XtSetArg(args[argc], XmNspacing, 0); |
|
153 argc++; |
|
154 XtSetArg(args[argc], XmNuserData, (XtPointer) globalRef); |
|
155 argc++; |
|
156 XtSetArg (args[argc], XmNscreen, |
|
157 ScreenOfDisplay(awt_display, |
|
158 adata->awt_visInfo.screen)); |
|
159 argc++; |
|
160 |
|
161 label = (*env)->GetObjectField(env, target, checkboxIDs.label); |
|
162 |
|
163 // fix for 4383735. |
|
164 // If the label is empty we need to set the indicator size |
|
165 // proportional to the size of the font. |
|
166 // kdm@sparc.spb.su |
|
167 if (JNU_IsNull(env, label) || ((*env)->GetStringLength(env, label) == 0)) { |
|
168 labelIsEmpty = TRUE; |
|
169 if (!JNU_IsNull(env, font)) { |
|
170 mfstr = XmStringCreateLocalized(" "); |
|
171 if (mfstr != NULL) { |
|
172 fontlist = awtJNI_GetFontList(env, font); |
|
173 if (fontlist != NULL) { |
|
174 height = XmStringHeight(fontlist, mfstr); |
|
175 XtSetArg(args[argc], XmNindicatorSize, height); |
|
176 argc++; |
|
177 XmFontListFree(fontlist); |
|
178 fontlist = NULL; |
|
179 } |
|
180 XmStringFree(mfstr); |
|
181 mfstr = NULL; |
|
182 } |
|
183 } |
|
184 } |
|
185 |
|
186 if (isMultiFont) { |
|
187 /* |
|
188 * We don't use makeCString() function here. |
|
189 * We create Motif multi-font compound string to display |
|
190 * unicode on the platform which is not spporting unicode. |
|
191 */ |
|
192 if (labelIsEmpty) { |
|
193 mfstr = XmStringCreateLocalized(""); |
|
194 } else { |
|
195 mfstr = awtJNI_MakeMultiFontString(env, label, font); |
|
196 } |
|
197 |
|
198 XtSetArg(args[argc], XmNlabelString, mfstr); |
|
199 argc++; |
|
200 |
|
201 DASSERT(!(argc > MAX_ARGC)); |
|
202 bdata->widget = XmCreateToggleButton(wdata->widget, "", args, argc); |
|
203 |
|
204 if (mfstr != NULL) { |
|
205 XmStringFree(mfstr); |
|
206 mfstr = NULL; |
|
207 } |
|
208 } else { |
|
209 if (labelIsEmpty) { |
|
210 clabel = emptyString; |
|
211 } else { |
|
212 clabel = (char *) JNU_GetStringPlatformChars(env, label, NULL); |
|
213 |
|
214 if (clabel == NULL) { /* Exception? */ |
|
215 AWT_UNLOCK(); |
|
216 return; |
|
217 } |
|
218 } |
|
219 |
|
220 DASSERT(!(argc > MAX_ARGC)); |
|
221 bdata->widget = XmCreateToggleButton(wdata->widget, clabel, args, argc); |
|
222 |
|
223 if (clabel != emptyString) { |
|
224 JNU_ReleaseStringPlatformChars(env, label, (const char *) clabel);; |
|
225 } |
|
226 } |
|
227 |
|
228 XtAddCallback(bdata->widget, |
|
229 XmNvalueChangedCallback, |
|
230 (XtCallbackProc) Toggle_callback, |
|
231 (XtPointer) globalRef); |
|
232 |
|
233 XtSetMappedWhenManaged(bdata->widget, False); |
|
234 XtManageChild(bdata->widget); |
|
235 |
|
236 AWT_UNLOCK(); |
|
237 } |
|
238 |
|
239 /* |
|
240 * Class: sun_awt_motif_MCheckboxPeer |
|
241 * Method: setLabel |
|
242 * Signature: (Ljava/lang/String;)V |
|
243 */ |
|
244 JNIEXPORT void JNICALL Java_sun_awt_motif_MCheckboxPeer_setLabel |
|
245 (JNIEnv * env, jobject this, jstring label) |
|
246 { |
|
247 struct ComponentData *wdata; |
|
248 char *clabel; |
|
249 XmString xim; |
|
250 jobject font; |
|
251 |
|
252 AWT_LOCK(); |
|
253 |
|
254 wdata = (struct ComponentData *) |
|
255 JNU_GetLongFieldAsPtr(env, this, mComponentPeerIDs.pData); |
|
256 if (wdata == NULL) { |
|
257 JNU_ThrowNullPointerException(env, "NullPointerException"); |
|
258 AWT_UNLOCK(); |
|
259 return; |
|
260 } |
|
261 if (JNU_IsNull(env, label) || ((*env)->GetStringLength(env, label) == 0)) { |
|
262 xim = XmStringCreateLocalized(""); |
|
263 } else { |
|
264 font = awtJNI_GetFont(env, this); |
|
265 |
|
266 if (awtJNI_IsMultiFont(env, font)) { |
|
267 xim = awtJNI_MakeMultiFontString(env, label, font); |
|
268 } else { |
|
269 clabel = (char *) JNU_GetStringPlatformChars(env, label, NULL); |
|
270 |
|
271 if (clabel == NULL) { |
|
272 AWT_UNLOCK(); |
|
273 return; |
|
274 } |
|
275 xim = XmStringCreate(clabel, "labelFont"); |
|
276 |
|
277 JNU_ReleaseStringPlatformChars(env, label, (const char *) clabel);; |
|
278 } |
|
279 } |
|
280 |
|
281 XtVaSetValues(wdata->widget, XmNlabelString, xim, NULL); |
|
282 XmStringFree(xim); |
|
283 AWT_FLUSH_UNLOCK(); |
|
284 } |
|
285 |
|
286 /* |
|
287 * Class: sun_awt_motif_MCheckboxPeer |
|
288 * Method: pSetState |
|
289 * Signature: (Z)V |
|
290 */ |
|
291 JNIEXPORT void JNICALL Java_sun_awt_motif_MCheckboxPeer_pSetState |
|
292 (JNIEnv * env, jobject this, jboolean state) |
|
293 { |
|
294 struct ComponentData *bdata; |
|
295 |
|
296 AWT_LOCK(); |
|
297 |
|
298 bdata = (struct ComponentData *) |
|
299 JNU_GetLongFieldAsPtr(env, this, mComponentPeerIDs.pData); |
|
300 |
|
301 if (bdata == NULL || bdata->widget == NULL) { |
|
302 JNU_ThrowNullPointerException(env, "NullPointerException"); |
|
303 AWT_UNLOCK(); |
|
304 return; |
|
305 } |
|
306 XtVaSetValues(bdata->widget, XmNset, (Boolean) state, NULL); |
|
307 AWT_FLUSH_UNLOCK(); |
|
308 } |
|
309 |
|
310 /* |
|
311 * Class: sun_awt_motif_MCheckboxPeer |
|
312 * Method: pGetState |
|
313 * Signature: ()Z |
|
314 */ |
|
315 JNIEXPORT jboolean JNICALL Java_sun_awt_motif_MCheckboxPeer_pGetState |
|
316 (JNIEnv * env, jobject this) |
|
317 { |
|
318 struct ComponentData *bdata; |
|
319 Boolean state; |
|
320 |
|
321 AWT_LOCK(); |
|
322 |
|
323 bdata = (struct ComponentData *) |
|
324 JNU_GetLongFieldAsPtr(env,this,mComponentPeerIDs.pData); |
|
325 |
|
326 if (bdata == NULL || bdata->widget == NULL) { |
|
327 JNU_ThrowNullPointerException(env, "NullPointerException"); |
|
328 AWT_UNLOCK(); |
|
329 return JNI_FALSE; |
|
330 } |
|
331 XtVaGetValues(bdata->widget, XmNset, &state, NULL); |
|
332 AWT_FLUSH_UNLOCK(); |
|
333 return ((state) ? JNI_TRUE : JNI_FALSE); |
|
334 } |
|
335 |
|
336 |
|
337 /* |
|
338 * Class: sun_awt_motif_MCheckboxPeer |
|
339 * Method: setCheckboxGroup |
|
340 * Signature: (Ljava/awt/CheckboxGroup;)V |
|
341 */ |
|
342 JNIEXPORT void JNICALL Java_sun_awt_motif_MCheckboxPeer_setCheckboxGroup |
|
343 (JNIEnv * env, jobject this, jobject group) |
|
344 { |
|
345 struct ComponentData *bdata; |
|
346 |
|
347 AWT_LOCK(); |
|
348 |
|
349 bdata = (struct ComponentData *) |
|
350 JNU_GetLongFieldAsPtr(env, this, mComponentPeerIDs.pData); |
|
351 |
|
352 if (bdata == NULL || bdata->widget == NULL) { |
|
353 JNU_ThrowNullPointerException(env, "NullPointerException"); |
|
354 AWT_UNLOCK(); |
|
355 return; |
|
356 } |
|
357 if (JNU_IsNull(env, group)) { |
|
358 XtVaSetValues(bdata->widget, |
|
359 XmNindicatorType, XmN_OF_MANY, |
|
360 NULL); |
|
361 } else { |
|
362 XtVaSetValues(bdata->widget, |
|
363 XmNindicatorType, XmONE_OF_MANY, |
|
364 NULL); |
|
365 } |
|
366 |
|
367 AWT_FLUSH_UNLOCK(); |
|
368 } |
|
369 |
|
370 /* |
|
371 * Class: sun_awt_motif_MCheckboxPeer |
|
372 * Method: getIndicatorSize |
|
373 * Signature: (V)I |
|
374 */ |
|
375 JNIEXPORT jint JNICALL Java_sun_awt_motif_MCheckboxPeer_getIndicatorSize |
|
376 (JNIEnv * env, jobject this) |
|
377 { |
|
378 struct ComponentData *wdata; |
|
379 Dimension size; |
|
380 |
|
381 AWT_LOCK(); |
|
382 |
|
383 wdata = (struct ComponentData *) |
|
384 JNU_GetLongFieldAsPtr(env, this, mComponentPeerIDs.pData); |
|
385 |
|
386 if (wdata == NULL || wdata->widget == NULL) { |
|
387 JNU_ThrowNullPointerException(env, "Null pData"); |
|
388 AWT_UNLOCK(); |
|
389 return 0; |
|
390 } |
|
391 XtVaGetValues(wdata->widget, |
|
392 XmNindicatorSize, &size, |
|
393 NULL); |
|
394 |
|
395 AWT_FLUSH_UNLOCK(); |
|
396 |
|
397 return size; |
|
398 } |
|
399 |
|
400 /* |
|
401 * Class: sun_awt_motif_MCheckboxPeer |
|
402 * Method: getSpacing |
|
403 * Signature: (V)I |
|
404 */ |
|
405 JNIEXPORT jint JNICALL Java_sun_awt_motif_MCheckboxPeer_getSpacing |
|
406 (JNIEnv * env, jobject this) |
|
407 { |
|
408 struct ComponentData *wdata; |
|
409 Dimension dim; |
|
410 |
|
411 AWT_LOCK(); |
|
412 |
|
413 wdata = (struct ComponentData *) |
|
414 JNU_GetLongFieldAsPtr(env, this, mComponentPeerIDs.pData); |
|
415 |
|
416 if (wdata == NULL || wdata->widget == NULL) { |
|
417 JNU_ThrowNullPointerException(env, "Null pData"); |
|
418 AWT_UNLOCK(); |
|
419 return 0; |
|
420 } |
|
421 XtVaGetValues(wdata->widget, |
|
422 XmNspacing, &dim, |
|
423 NULL); |
|
424 |
|
425 AWT_FLUSH_UNLOCK(); |
|
426 |
|
427 return dim; |
|
428 } |
|