/**
 * Create the native peer of a DateField.
 * Upon successful return, *datefieldPtr should be filled in properly.
 * Param time is number of milliseconds since the standard base time known as
 * "the epoch", namely January 1, 1970, 00:00:00 GMT.
 * (defined in midp_datefield.h)
 */
extern "C" MidpError
lfpport_datefield_create(MidpItem* datefieldPtr,
			 MidpDisplayable* formPtr,
			 const pcsl_string* label, int layout,
			 int input_mode, long time, const pcsl_string* timezoneID)
{
  QString qlabel, qtzone;

  TRACE_DF(lfpport_datefield_Create);

  REPORT_INFO1(LC_HIGHUI, "\tinput_mode=%d\n", input_mode);

  pcsl_string2QString(*label, qlabel);
  pcsl_string2QString(*timezoneID, qtzone);

  // Fill in MidpItem structure
  datefieldPtr->widgetPtr = 
    new DateField((formPtr == INVALID_NATIVE_ID ? 
		   0 : (QWidget *)formPtr->frame.widgetPtr),
		  qlabel,
		  layout,
		  time,
		  input_mode,
		  qtzone);

  initItemPtr(datefieldPtr, formPtr);

  return KNI_OK;
}
/**
 * Create a TextField Qt peer without showing it yet.
 * Upon successful return, fields in *itemPtr should be set properly.
 */
extern "C" MidpError
lfpport_textfield_create(MidpItem* itemPtr, MidpDisplayable* formPtr,
			 const pcsl_string* label, int layout,
			 const pcsl_string* text, int maxSize,
			 int constraints, const pcsl_string* inputMode) {
    QString qlabel, qtext, qinputMode;

    pcsl_string2QString(*label, qlabel);
    pcsl_string2QString(*text, qtext);
    pcsl_string2QString(*inputMode, qinputMode);

    /* Fill in MidpItem structure */
    itemPtr->widgetPtr = 
      new TextField((formPtr == INVALID_NATIVE_ID
                     ? 0 : (QWidget *)formPtr->frame.widgetPtr),
		    qlabel,
		    layout,
		    qtext,
		    maxSize,
		    constraints,
		    qinputMode);

    initItemPtr(itemPtr, formPtr);
    return KNI_OK;
}
/**
 * Creates a StringImageItem native peer without showing it yet.
 * Upon successful return, fields in *itemPtr should be set properly.
 */
  MidpError lfpport_stringitem_create(MidpItem* itemPtr, 
				      MidpDisplayable* formPtr,
				      const pcsl_string* label,
				      int layout,
				      const pcsl_string* text,
				      PlatformFontPtr fontPtr, 
				      int appearanceMode) {

    /* Suppress unused-parameter warning */
    (void)fontPtr;

    QString labelStr;
    QString textStr;
   
    pcsl_string2QString(*label, labelStr);
    pcsl_string2QString(*text, textStr);


    itemPtr->widgetPtr =
      new StringImageItem((formPtr == INVALID_NATIVE_ID ? 
                           0 : (QWidget *)formPtr->frame.widgetPtr),
                          labelStr,
                          layout,
                          textStr, 
                          (QFont *)fontPtr,
                          appearanceMode); 
    
    initItemPtr(itemPtr, formPtr);
   

    return KNI_OK;
  }
/**
 * Creates a StringImageItem native peer without showing it yet.
 * Upon successful return, fields in *itemPtr should be set properly.
 */
MidpError lfpport_imageitem_create(MidpItem* itemPtr,
				   MidpDisplayable* formPtr,
				   const pcsl_string* label,
				   int layout,
				   unsigned char* imgPtr, 
				   const pcsl_string* altText,
				   int appearanceMode) {
    QString labelStr;
    QString altTextStr;
   
    pcsl_string2QString(*label, labelStr);
    pcsl_string2QString(*altText, altTextStr);

    itemPtr->widgetPtr = 
        new StringImageItem((formPtr == INVALID_NATIVE_ID ? 
                           0 : (QWidget *)formPtr->frame.widgetPtr),
                          labelStr,
                          layout,
                          gxpportqt_get_immutableimage_pixmap(imgPtr),
                          altTextStr, 
                          appearanceMode);

    initItemPtr(itemPtr, formPtr);

    return KNI_OK;
}
  MidpError lfpport_form_create(MidpDisplayable *formPtr, const pcsl_string *title, const pcsl_string *ticker)
  {
    JForm *form = new JForm(JDisplay::current(), formPtr,
                            pcsl_string2QString(*title), pcsl_string2QString(*ticker));

    qDebug("Create Form");
    if (!form)
      return KNI_ENOMEM;
    else
      return KNI_OK;
  }
 MidpError lfpport_textfield_create(MidpItem *itemPtr, MidpDisplayable *ownerPtr, const pcsl_string *label,
                                    int layout, const pcsl_string *text, int maxSize, int constraints,
                                    const pcsl_string *initialInputMode)
 {
   debug_trace();
   debug_dumpdisp(ownerPtr);
   JDisplayable *disp = static_cast<JDisplayable *>(ownerPtr->frame.widgetPtr);
   qDebug() << "Form:" << static_cast<QObject *>(disp->toForm());
   JTextField *tf = new JTextField(itemPtr, disp->toForm(),
                                   pcsl_string2QString(*label), layout, pcsl_string2QString(*text),
                                   maxSize, constraints, pcsl_string2QString(*initialInputMode));
   if (!tf)
     return KNI_ENOMEM;
   return KNI_OK;
 }
/**
 * Notifies native peer of a content change in the corresponding TextField.
 * @param text - the new string set
 */
extern "C" MidpError
lfpport_textfield_set_string(MidpItem* itemPtr, const pcsl_string* text)
{
    QString qtext;

    pcsl_string2QString(*text, qtext);
    
    return ((TextField *)itemPtr->widgetPtr)->setString(qtext);
}
/**
 * Notifies a content change in the corresponding StringImageItem.
 * @param text - the new string set in the StringImageItem
 */
  MidpError lfpport_stringitem_set_content(MidpItem* itemPtr, const pcsl_string* text,
					   int appearanceMode) {
    QString textStr;

    pcsl_string2QString(*text, textStr);

    return ((StringImageItem *)itemPtr->widgetPtr)->setContent(textStr, 
                                                               appearanceMode);
  }
/**
 * Notifies a content change in the corresponding StringImageItem.
 */
MidpError lfpport_imageitem_set_content(MidpItem* itemPtr, 
					unsigned char* imgPtr,
					const pcsl_string* altText,
					int appearanceMode) {
    QString altTextStr;

    pcsl_string2QString(*altText, altTextStr);

    return ((StringImageItem *)itemPtr->widgetPtr)->setContent(
                             gxpportqt_get_immutableimage_pixmap(imgPtr),
                             altTextStr, appearanceMode);
  }
void truncateQString(QString & str, QFont font, int widthLimit) {
    QFontMetrics fm(font);
    if (fm.width(str) > widthLimit) {
        QString qTruncMark;
        pcsl_string2QString(truncmark, qTruncMark);
        int truncMarkWidth = fm.width(qTruncMark,1);
        int len = str.length();
        while (len>0 && fm.width(str,len)>widthLimit-truncMarkWidth) {
            len--;
        }
        str.truncate(len);
        str.append(qTruncMark);
    }
}
/**
 * Creates a custom item's native peer, but does not display it.
 * When this function returns successfully, *itemPtr will be filled.
 *
 * @param itemPtr pointer to the custom item's MidpItem structure.
 * @param ownerPtr pointer to the item's owner(form)'s MidpDisplayable 
 *                 structure.
 * @param label the item label.
 * @param layout the item layout directive.
 *
 * @return an indication of success or the reason for failure
 */
extern "C" MidpError
lfpport_customitem_create(MidpItem* customitemPtr, 
			  MidpDisplayable* formPtr,
			  const pcsl_string* label, int layout)
{
  QString qlabel;

  pcsl_string2QString(*label, qlabel);

  // Fill in MidpItem structure
  customitemPtr->widgetPtr = 
    new CustomItem(formPtr == INVALID_NATIVE_ID ? 
		   0 : (QWidget *)formPtr->frame.widgetPtr,
		   qlabel, layout);

  initItemPtr(customitemPtr, formPtr);

  return KNI_OK;
}
 MidpError jitem_setLabel(MidpItem *itemPtr, const pcsl_string *label)
 {
   FETCH_ITEM(item);
   item->j_setLabel(pcsl_string2QString(*label));
   return KNI_OK;
 }
 MidpError lfpport_textfield_set_string(MidpItem *itemPtr, const pcsl_string *text)
 {
   JTextField *tf = (JTextField *)itemPtr->widgetPtr;
   return tf->setString(pcsl_string2QString(*text));
 }