void AwtTextComponent::_SetText(void *param)
{
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);

    SetTextStruct *sts = (SetTextStruct *)param;
    jobject self = sts->textcomponent;
    jstring text = sts->text;

    AwtTextComponent *c = NULL;

    PDATA pData;
    JNI_CHECK_PEER_GOTO(self, ret);
    c = (AwtTextComponent *)pData;
    if (::IsWindow(c->GetHWnd()))
    {
        int length = env->GetStringLength(text);
        WCHAR* buffer = new WCHAR[length + 1];
        env->GetStringRegion(text, 0, length, reinterpret_cast<jchar*>(buffer));
        buffer[length] = 0;
        c->CheckLineSeparator(buffer);
        c->RemoveCR(buffer);
        c->SetText(buffer);
        delete[] buffer;
    }
ret:
    env->DeleteGlobalRef(self);
    env->DeleteGlobalRef(text);

    delete sts;
}
JNIEXPORT void JNICALL
Java_sun_awt_pocketpc_PPCTextComponentPeer_setText (JNIEnv *env, 
                                                    jobject thisObj,
                                                    jstring text)
{
    CHECK_PEER(thisObj);
    AwtTextComponent* c = PDATA(AwtTextComponent, thisObj);

    int length = env->GetStringLength(text);
#ifndef UNICODE
    TCHAR* buffer = new TCHAR[length * 2 + 1];
    buffer = TO_WSTRING(text);
#else
    WCHAR* buffer = new WCHAR[length  + 1];
    env->GetStringRegion(text, 0, length, (jchar*)buffer);
    buffer[length] = 0; //or it should be TEXT('\0') or L'\0'???
#endif

    buffer = AwtTextComponent::AddCR(buffer, length);
    c->SetText(buffer);
    /* Fix for TCK bug: setText() does not generate valueChangedEvent */
    if (env->IsInstanceOf(c->GetTarget(), WCachedIDs.java_awt_TextAreaClass)) {
        c->WmNotify(EN_CHANGE);
    }
    delete[] buffer;
}
JNIEXPORT jstring JNICALL
Java_sun_awt_pocketpc_PPCTextComponentPeer_getText (JNIEnv *env, 
                                                    jobject thisObj)
{
    CHECK_PEER_RETURN(thisObj);
    int len;
    AwtTextComponent* c = PDATA(AwtTextComponent, thisObj);

    len = c->GetTextLength();
    jstring js;
    if (len == 0) {
        // Make java null string
        jchar *jc = new jchar[0];
	js = env->NewString(jc, 0);
	delete [] jc;
        ASSERT(!env->ExceptionCheck());
    } else {
#ifndef UNICODE
       char* buffer = new char[len+1];
       c->GetText(buffer, len+1);
       AwtTextComponent::RemoveCR(buffer);
       js = env->NewString(buffer, wcslen(buf)); // convert to unicode. right???
       delete[] buffer;
#else
       TCHAR *buffer = new TCHAR[len+1];
       c->GetText(buffer, len+1);
       AwtTextComponent::RemoveCR(buffer);
       js = TO_JSTRING((LPWSTR)buffer);
       //js = env->NewString(buffer, wcslen(buffer));
       delete[] buffer;
#endif
    }
    return js;
}
JNIEXPORT void JNICALL
Java_sun_awt_pocketpc_PPCTextComponentPeer_enableEditing (JNIEnv *env, 
                                                          jobject thisObj,
                                                          jboolean editable)
{
    CHECK_PEER(thisObj);
    AwtTextComponent* c = PDATA(AwtTextComponent, thisObj);
    c->SendMessage(EM_SETREADONLY, !editable);
}
jstring AwtTextComponent::_GetText(void *param)
{
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);

    jobject self = (jobject)param;

    AwtTextComponent *c = NULL;
    jstring result = NULL;

    PDATA pData;
    JNI_CHECK_PEER_GOTO(self, ret);

    c = (AwtTextComponent *)pData;
    if (::IsWindow(c->GetHWnd()))
    {
        int len = ::GetWindowTextLength(c->GetHWnd());
        if (len == 0) {
            /* Make java null string */
            jchar *jc = new jchar[0];
            result = env->NewString(jc, 0);
            delete [] jc;
        } else {
            WCHAR* buf = new WCHAR[len + 1];
            c->GetText(buf, len + 1);
            c->RemoveCR(buf);
            result = JNU_NewStringPlatform(env, buf);
            delete [] buf;
        }
    }
ret:
    env->DeleteGlobalRef(self);

    if (result != NULL)
    {
        jstring globalRef = (jstring)env->NewGlobalRef(result);
        env->DeleteLocalRef(result);
        return globalRef;
    }
    else
    {
        return NULL;
    }
}
void AwtTextComponent::_EnableEditing(void *param)
{
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);

    EnableEditingStruct *ees = (EnableEditingStruct *)param;
    jobject self = ees->textcomponent;
    jboolean on = ees->on;

    AwtTextComponent *c = NULL;

    PDATA pData;
    JNI_CHECK_PEER_GOTO(self, ret);
    c = (AwtTextComponent *)pData;
    if (::IsWindow(c->GetHWnd()))
    {
        c->SendMessage(EM_SETREADONLY, !on);
    }
ret:
    env->DeleteGlobalRef(self);

    delete ees;
}
jint AwtTextComponent::_GetSelectionEnd(void *param)
{
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);

    jobject self = (jobject)param;

    jint result = 0;
    AwtTextComponent *c = NULL;

    PDATA pData;
    JNI_CHECK_PEER_GOTO(self, ret);
    c = (AwtTextComponent *)pData;
    if (::IsWindow(c->GetHWnd()))
    {
        long end;
        c->SendMessage(EM_GETSEL, 0, (LPARAM)&end);
        result = c->getJavaSelPos(end);
    }
ret:
    env->DeleteGlobalRef(self);

    return result;
}
void AwtTextComponent::_Select(void *param)
{
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);

    SelectStruct *ss = (SelectStruct *)param;
    jobject self = ss->textcomponent;
    jint start = ss->start;
    jint end = ss->end;

    AwtTextComponent *c = NULL;

    PDATA pData;
    JNI_CHECK_PEER_GOTO(self, ret);
    c = (AwtTextComponent *)pData;
    if (::IsWindow(c->GetHWnd()))
    {
        c->SetSelRange(start, end);
        c->SendMessage(EM_SCROLLCARET);
    }
ret:
    env->DeleteGlobalRef(self);

    delete ss;
}
/* Create a new AwtTextArea or AwtTextField object and window.   */
AwtTextComponent* AwtTextComponent::Create(jobject peer, jobject parent, BOOL isMultiline)
{
    JNIEnv *env = (JNIEnv *)JNU_GetEnv(jvm, JNI_VERSION_1_2);

    jobject target = NULL;
    AwtTextComponent* c = NULL;

    try {
        if (env->EnsureLocalCapacity(1) < 0) {
            return NULL;
        }

        PDATA pData;
        AwtCanvas* awtParent;
        JNI_CHECK_PEER_GOTO(parent, done);

        awtParent = (AwtCanvas*)pData;
        JNI_CHECK_NULL_GOTO(awtParent, "null awtParent", done);

        target = env->GetObjectField(peer, AwtObject::targetID);
        JNI_CHECK_NULL_GOTO(target, "null target", done);

        if(isMultiline) {
            c = new AwtTextArea();
        } else {
            c = new AwtTextField();
        }

        {
            /* Adjust style for scrollbar visibility and word wrap  */
            DWORD scroll_style;

            if(isMultiline) {

                jint scrollbarVisibility =
                    env->GetIntField(target, AwtTextArea::scrollbarVisibilityID);

                switch (scrollbarVisibility) {
                case java_awt_TextArea_SCROLLBARS_NONE:
                    scroll_style = ES_AUTOVSCROLL;
                    break;
                case java_awt_TextArea_SCROLLBARS_VERTICAL_ONLY:
                    scroll_style = WS_VSCROLL | ES_AUTOVSCROLL;
                    break;
                case java_awt_TextArea_SCROLLBARS_HORIZONTAL_ONLY:
                    scroll_style = WS_HSCROLL | ES_AUTOHSCROLL | ES_AUTOVSCROLL;
                    break;
                case java_awt_TextArea_SCROLLBARS_BOTH:
                    scroll_style = WS_VSCROLL | WS_HSCROLL |
                                   ES_AUTOVSCROLL | ES_AUTOHSCROLL;
                    break;
                }
            }

            DWORD style = WS_CHILD | WS_CLIPSIBLINGS | ES_LEFT;

            /*
             * Specify ES_DISABLENOSCROLL - RichEdit control style to disable
             * scrollbars instead of hiding them when not needed.
             */
            style |= isMultiline ?  ES_MULTILINE | ES_WANTRETURN | scroll_style
                     | ES_DISABLENOSCROLL : ES_AUTOHSCROLL;


            DWORD exStyle = WS_EX_CLIENTEDGE;
            if (GetRTL()) {
                exStyle |= WS_EX_RIGHT | WS_EX_LEFTSCROLLBAR;
                if (GetRTLReadingOrder())
                    exStyle |= WS_EX_RTLREADING;
            }


            jint x = env->GetIntField(target, AwtComponent::xID);
            jint y = env->GetIntField(target, AwtComponent::yID);
            jint width = env->GetIntField(target, AwtComponent::widthID);
            jint height = env->GetIntField(target, AwtComponent::heightID);

            c->CreateHWnd(env, L"", style, exStyle,
                          x, y, width, height,
                          awtParent->GetHWnd(),
                          reinterpret_cast<HMENU>(static_cast<INT_PTR>(
                                  awtParent->CreateControlID())),
                          ::GetSysColor(COLOR_WINDOWTEXT),
                          ::GetSysColor(COLOR_WINDOW),
                          peer);

            // Fix for 4753116.
            // If it is not win95 (we are using Richedit 2.0)
            // we set plain text mode, in which the control is
            // similar to a standard edit control:
            //  - The text in a plain text control can have only
            //    one format.
            //  - The user cannot paste rich text formats, such as RTF
            //    or embedded objects into a plain text control.
            //  - Rich text mode controls always have a default
            //    end-of-document marker or carriage return,
            //    to format paragraphs.
            // [email protected]
            c->SendMessage(EM_SETTEXTMODE, TM_PLAINTEXT, 0);

            c->m_backgroundColorSet = TRUE;
            /* suppress inheriting parent's color. */
            c->UpdateBackground(env, target);
            c->SendMessage(EM_SETMARGINS, EC_LEFTMARGIN | EC_RIGHTMARGIN,
                           MAKELPARAM(1, 1));
            /*
             * Fix for BugTraq Id 4260109.
             * Set the text limit to the maximum.
             * Use EM_EXLIMITTEXT for RichEdit controls.
             * For some reason RichEdit 1.0 becomes read-only if the
             * specified limit is greater than 0x7FFFFFFD.
             */
            c->SendMessage(EM_EXLIMITTEXT, 0, 0x7FFFFFFD);

            /* Unregister RichEdit built-in drop target. */
            VERIFY(::RevokeDragDrop(c->GetHWnd()) != DRAGDROP_E_INVALIDHWND);

            /* To enforce CF_TEXT format for paste operations. */
            VERIFY(c->SendMessage(EM_SETOLECALLBACK, 0,
                                  (LPARAM)&GetOleCallback()));

            c->SendMessage(EM_SETEVENTMASK, 0, ENM_CHANGE);
        }
    } catch (...) {
        env->DeleteLocalRef(target);
        throw;
    }

done:
    env->DeleteLocalRef(target);

    return c;
}