lfp_choicegroup.c

来自「This is a resource based on j2me embedde」· C语言 代码 · 共 655 行 · 第 1/2 页

C
655
字号
/* *    * * Copyright  1990-2007 Sun Microsystems, Inc. All Rights Reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER *  * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License version * 2 only, as published by the Free Software Foundation. *  * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License version 2 for more details (a copy is * included at /legal/license.txt). *  * You should have received a copy of the GNU General Public License * version 2 along with this work; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA * 02110-1301 USA *  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa * Clara, CA 95054 or visit www.sun.com if you need additional * information or have any questions. *//** * @file * * Cross platform ChoiceGroup's native functions. */#include <kni.h>#include <midpError.h>#include <midpMalloc.h>#include <lfpport_choicegroup.h>#include "lfp_intern_registry.h"#include <midpUtilKni.h>#include <gxp_image.h>/* cached field ids for CGElement class */static jfieldID _cgEl_stringEl_cache    = NULL;static jfieldID _cgEl_imageDataEl_cache = NULL;static jfieldID _cgEl_selected_cache    = NULL;static jfieldID _cgEl_font_cache        = NULL;/* cached field ids for Font class */static jfieldID _f_face_cache  = NULL;static jfieldID _f_style_cache = NULL;static jfieldID _f_size_cache  = NULL;#define _CACHE_FIELDID(HANDLE, name, type, cache) \     ((cache)==NULL?((cache)=KNI_GetFieldID((HANDLE), (name), (type))):(cache))/** * KNI function that creates native resource for the current StringItem. * <p> * Java declaration: * <pre> *     createNativeResource0(ISIII[OII)I * </pre> * * @param ownerId Owner screen's native resource id (MidpDisplayable *) * @param label - label to be used for this ChoiceGroup * @param layout layout directive associated with this ChoiceGroup * @param choiceType - should be EXCLUSIVE, MULTIPLE, IMPLICIT, POPUP * @param fitPolicy  - to be used to display created ChoiceGroup * @param cgElements - elements array with string, image, font, selected state *                     information per element * @param numChoices - number of elements in the ChoiceGroup * @param selectedIndex - currently selected index (for EXCLUSIVE, IMPLICIT, and *                        POPUP) * @return native resource id (MidpItem *) of this StringItem */KNIEXPORT KNI_RETURNTYPE_INTJava_javax_microedition_lcdui_ChoiceGroupLFImpl_createNativeResource0() {  MidpError err = KNI_OK;  MidpDisplayable  *ownerPtr = NULL;  MidpItem *cgPtr = NULL;  pcsl_string label_str;  MidpChoiceGroupElement *cgChoices = NULL;  int choiceType, layout;  int fitPolicy;  int numChoices = 0;  int selectedIndex;  int i = 0;  pcsl_string_status perr;  ownerPtr = (MidpDisplayable *)KNI_GetParameterAsInt(1);  layout = KNI_GetParameterAsInt(3);  choiceType = KNI_GetParameterAsInt(4);  fitPolicy  = KNI_GetParameterAsInt(5);  numChoices = KNI_GetParameterAsInt(7);  selectedIndex  = KNI_GetParameterAsInt(8);  KNI_StartHandles(8);    KNI_DeclareHandle(labelJString);  KNI_DeclareHandle(cgElementsJObject);  KNI_DeclareHandle(cgElement);  KNI_DeclareHandle(strJString);  KNI_DeclareHandle(imgJImage);  KNI_DeclareHandle(fontJFont);  KNI_DeclareHandle(cgElementHandle);  KNI_DeclareHandle(fontHandle);  KNI_GetParameterAsObject(2, labelJString);  KNI_GetParameterAsObject(6, cgElementsJObject);   if (numChoices > 0) {    jobjectArray cgElementsArray;    KNI_FindClass("javax/microedition/lcdui/ChoiceGroup$CGElement", 		  cgElementHandle);    KNI_FindClass("javax/microedition/lcdui/Font", fontHandle);	      cgElementsArray = (jobjectArray)cgElementsJObject;    cgChoices = (MidpChoiceGroupElement *)		midpMalloc(sizeof(MidpChoiceGroupElement) * numChoices);    if (cgChoices == NULL) {      err = KNI_ENOMEM;    }    for (i = 0; err == KNI_OK && i < numChoices; i++) {      KNI_GetObjectArrayElement(cgElementsArray, i, cgElement);      KNI_GetObjectField(cgElement, 			 _CACHE_FIELDID(cgElementHandle, "stringEl", 					"Ljava/lang/String;", 					_cgEl_stringEl_cache), strJString);      perr = midp_jstring_to_pcsl_string(strJString, &cgChoices[i].string);      if (PCSL_STRING_OK != perr) {        err = KNI_ENOMEM;      } else {	KNI_GetObjectField(cgElement, 			   _CACHE_FIELDID(cgElementHandle, "imageDataEl", 					  "Ljavax/microedition/lcdui/ImageData;", 					  _cgEl_imageDataEl_cache), imgJImage);	if (KNI_IsNullHandle(imgJImage) == KNI_TRUE) {	  cgChoices[i].image = NULL;	} else {	  cgChoices[i].image = gxp_get_imagedata(imgJImage);	}	cgChoices[i].selected = 	  KNI_GetBooleanField(cgElement, _CACHE_FIELDID(cgElementHandle, 							"selected", "Z",							_cgEl_selected_cache));		KNI_GetObjectField(cgElement,			   _CACHE_FIELDID(cgElementHandle, "fontEl",					  "Ljavax/microedition/lcdui/Font;", 					  _cgEl_font_cache), fontJFont); 		if (KNI_IsNullHandle(fontJFont) == KNI_TRUE) {	  cgChoices[i].font = NULL;	} else {	  	  int face, style, size; /* usually only few fonts are set */	  face = KNI_GetIntField(fontJFont, _CACHE_FIELDID(fontHandle, "face",                              "I", _f_face_cache));	  style = KNI_GetIntField(fontJFont, _CACHE_FIELDID(fontHandle,                              "style",                             "I", _f_style_cache));      size = KNI_GetIntField(fontJFont, _CACHE_FIELDID(fontHandle, "size",                             "I", _f_size_cache));            if ((err = lfpport_get_font(&(cgChoices[i].font), face, style, size))                != KNI_OK) {                  err = KNI_ENOMEM;                  i++;                  break;            }        }      }    }  }   if (err == KNI_OK) {    if(PCSL_STRING_OK        != midp_jstring_to_pcsl_string(labelJString, &label_str)) {      err = KNI_ENOMEM;    }  }  KNI_EndHandles();  if (err == KNI_OK) {    cgPtr = MidpNewItem(ownerPtr, 			MIDP_EXCLUSIVE_CHOICE_GROUP_TYPE + choiceType - 1);        if (cgPtr == NULL) {      err = KNI_ENOMEM;    } else {      err = lfpport_choicegroup_create(cgPtr, ownerPtr, &label_str, layout,				    choiceType, cgChoices, numChoices,				    selectedIndex, fitPolicy);    }  }  // do clean up  pcsl_string_free(&label_str);  for (i--; i >= 0; i--) {    pcsl_string_free(&cgChoices[i].string);  }  midpFree(cgChoices);    if (err != KNI_OK) {    MidpDeleteItem(cgPtr);    KNI_ThrowNew(midpOutOfMemoryError, NULL);  }  KNI_ReturnInt(cgPtr);}/** * KNI function that inserts new element with passed in string and image  *  into the native resource corresponding to the current ChoiceGroup. * * Java declaration: * <pre> *     insert0(IISOB)V * </pre> * @param nativeId - id of the native resource corresponding to the current *                   ChoiceGroup * @param elementNum - location at which new element should be inserted * @param stringPart - string part of the element to be inserted * @param imagePart - image part of the element to be inserted * @param selected  - current selection state of the inserted element */KNIEXPORT KNI_RETURNTYPE_VOIDJava_javax_microedition_lcdui_ChoiceGroupLFImpl_insert0() {  MidpError err = KNI_OK;  MidpItem *cgPtr = (MidpItem *)KNI_GetParameterAsInt(1);  MidpChoiceGroupElement cgElement;  pcsl_string_status perr;  int elementNum = KNI_GetParameterAsInt(2);    KNI_StartHandles(2);  KNI_DeclareHandle(stringPartJString);  KNI_DeclareHandle(imgPartJImage);    KNI_GetParameterAsObject(3, stringPartJString);  KNI_GetParameterAsObject(4, imgPartJImage);  if (KNI_IsNullHandle(imgPartJImage) == KNI_TRUE) {    cgElement.image = NULL;  } else {    cgElement.image = gxp_get_imagedata(imgPartJImage);  }  perr = midp_jstring_to_pcsl_string(stringPartJString, &cgElement.string);  cgElement.selected = KNI_GetParameterAsBoolean(5);  cgElement.font = NULL;  KNI_EndHandles();  if (PCSL_STRING_OK == perr) {    err = lfpport_choicegroup_insert(cgPtr, elementNum, cgElement);  }    pcsl_string_free(&cgElement.string);    if (err == KNI_ENOMEM) {    KNI_ThrowNew(midpOutOfMemoryError, NULL);  }    KNI_ReturnVoid();}/** * KNI function that deletes an element at the passed in location   * from the native resource corresponding to the current ChoiceGroup. * * Java declaration: * <pre> *     delete0(III)V * </pre> * @param nativeId - id of the native resource corresponding to the current *                   ChoiceGroup * @param elementNum - location at which an element should be deleted * @param selectedIndex  - index of an element that should be selected  *                        after deletion is done */KNIEXPORT KNI_RETURNTYPE_VOIDJava_javax_microedition_lcdui_ChoiceGroupLFImpl_delete0() {  MidpError err = KNI_OK;  MidpItem *cgPtr = (MidpItem *)KNI_GetParameterAsInt(1);  int elementNum = KNI_GetParameterAsInt(2);  int selectedIndex = KNI_GetParameterAsInt(3);  err = lfpport_choicegroup_delete(cgPtr, elementNum, selectedIndex);  if (err == KNI_ENOMEM) {    KNI_ThrowNew(midpOutOfMemoryError, NULL);  }    KNI_ReturnVoid();  }/** * KNI function that deletes an element at the passed in location   * from the native resource corresponding to the current ChoiceGroup. * * Java declaration: * <pre> *     deleteAll0(I)V * </pre> * @param nativeId - id of the native resource corresponding to the current *                   ChoiceGroup */KNIEXPORT KNI_RETURNTYPE_VOIDJava_javax_microedition_lcdui_ChoiceGroupLFImpl_deleteAll0() {  MidpError err = KNI_OK;  MidpItem *cgPtr = (MidpItem *)KNI_GetParameterAsInt(1);  err = lfpport_choicegroup_delete_all(cgPtr);  if (err == KNI_ENOMEM) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?