javauis/lcdui_akn/javalcdui/src/TextBox.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 1999 - 2003 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "CMIDToolkit.h"
       
    20 #include "MIDUtils.h"
       
    21 #include "javax_microedition_lcdui_TextBox.h"
       
    22 
       
    23 
       
    24 // assigned maximum capacity may be smaller than requested so must
       
    25 // go native to get real max size.
       
    26 // iMaxSize = NativeError.check(_getMaxSize(getContentHandle(),iToolkit.getHandle()));
       
    27 struct TTextBoxCreate
       
    28 {
       
    29     jweak iRef;
       
    30     jint  iDisplayable;
       
    31     jint  iConstraints;
       
    32     TPtrC iText;
       
    33 };
       
    34 
       
    35 struct TTextBoxAttribs
       
    36 {
       
    37     jint    iHandle;    // out
       
    38     TSize   iSize;      // out
       
    39     jint    iMaxSize;   // in-out
       
    40 };
       
    41 
       
    42 LOCAL_C void CreateTextBoxL(CMIDToolkit* aToolkit, TTextBoxCreate* aCreate, TTextBoxAttribs* aAttribs)
       
    43 {
       
    44     MMIDComponentFactory& factory = *aToolkit->ComponentFactory();
       
    45 
       
    46     TInt constraints = aCreate->iConstraints;
       
    47     TInt maxSize = aAttribs->iMaxSize;
       
    48     const TDesC& text = aCreate->iText;
       
    49     MMIDDisplayable& container = *MIDUnhand<MMIDDisplayable>(aCreate->iDisplayable);
       
    50 
       
    51     MMIDTextBox* textComponent = factory.CreateTextBoxL(constraints, maxSize, text, container);
       
    52     CleanupDisposePushL(textComponent);
       
    53     aAttribs->iHandle = aToolkit->RegisterComponentL(textComponent, aCreate->iRef);
       
    54     CleanupPopComponent(textComponent);
       
    55     aCreate->iRef = 0;
       
    56     aAttribs->iSize = container.ContentSize();
       
    57     aAttribs->iMaxSize = textComponent->GetMaxSize();
       
    58 }
       
    59 
       
    60 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_TextBox__1create
       
    61 (
       
    62     JNIEnv* aJni,
       
    63     jobject aTextBox,
       
    64     jint    aToolkit,
       
    65     jint    aDisplayable,
       
    66     jint    aConstraints,
       
    67     jint    aMaxSize,
       
    68     jstring aText,
       
    69     jintArray aCreateTextBoxReturn
       
    70 )
       
    71 {
       
    72     RJString text(*aJni,aText);
       
    73 
       
    74     TTextBoxCreate create;
       
    75     create.iRef = aJni->NewWeakGlobalRef(aTextBox);
       
    76     create.iDisplayable = aDisplayable;
       
    77     create.iConstraints = aConstraints;
       
    78     create.iText.Set(text);
       
    79 
       
    80     TTextBoxAttribs attribs;
       
    81     attribs.iHandle=0;
       
    82     attribs.iMaxSize=aMaxSize;
       
    83 
       
    84     CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit);
       
    85 
       
    86     jint error;
       
    87     if (create.iRef)
       
    88     {
       
    89         error = toolkit->ExecuteTrap(&CreateTextBoxL, toolkit, &create, &attribs);
       
    90     }
       
    91     else
       
    92     {
       
    93         error = KErrNoMemory;
       
    94     }
       
    95 
       
    96     if (create.iRef)
       
    97     {
       
    98         // cleared in CreateTextBoxL if successful
       
    99         aJni->DeleteWeakGlobalRef(create.iRef);
       
   100     }
       
   101 
       
   102     ASSERT(aJni->GetArrayLength(aCreateTextBoxReturn) == 4);
       
   103     jint attribArray[4];
       
   104     attribArray[0]=attribs.iHandle;
       
   105     attribArray[1]=attribs.iSize.iWidth;
       
   106     attribArray[2]=attribs.iSize.iHeight;
       
   107     attribArray[3]=attribs.iMaxSize;
       
   108     aJni->SetIntArrayRegion(aCreateTextBoxReturn, 0, 4, attribArray);
       
   109 
       
   110     return error;
       
   111 }
       
   112 
       
   113 LOCAL_C TInt GetCaretPosition(MMIDTextBox* aTextComponent)
       
   114 {
       
   115     return aTextComponent->GetCaretPosition();
       
   116 }
       
   117 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_TextBox__1getCaretPosition
       
   118 (JNIEnv*,jobject,jint aTextComponent,jint aToolkit)
       
   119 {
       
   120     MMIDTextBox* textComponent = MIDUnhandObject<MMIDTextBox>(aTextComponent);
       
   121     CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit);
       
   122     return toolkit->Execute(&GetCaretPosition,textComponent);
       
   123 }
       
   124 
       
   125 
       
   126 LOCAL_C void GetTextL(MMIDTextBox* aTextComponent, HBufC** aString)
       
   127 {
       
   128     *aString = aTextComponent->GetTextL();
       
   129 }
       
   130 JNIEXPORT jstring JNICALL Java_javax_microedition_lcdui_TextBox__1getString
       
   131 (JNIEnv* aJni,jobject,jint aTextComponent,jint aToolkit)
       
   132 {
       
   133     MMIDTextBox* textComponent = MIDUnhandObject<MMIDTextBox>(aTextComponent);
       
   134     CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit);
       
   135     HBufC* text=NULL;
       
   136     TInt error = toolkit->ExecuteTrap(&GetTextL, textComponent, &text);
       
   137     jstring str = (text!=NULL && error==KErrNone) ? CreateJavaString(*aJni,*text):NULL;
       
   138     delete text;
       
   139     return str;
       
   140 }
       
   141 
       
   142 
       
   143 LOCAL_C void SetTextL(MMIDTextBox* aTextComponent,const TDesC* aText)
       
   144 {
       
   145     aTextComponent->SetTextL(*aText);
       
   146 }
       
   147 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_TextBox__1setString
       
   148 (JNIEnv* aJni,jobject,jint aTextComponent,jint aToolkit,jstring aText)
       
   149 {
       
   150     MMIDTextBox* textComponent = MIDUnhandObject<MMIDTextBox>(aTextComponent);
       
   151     CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit);
       
   152     RJString text(*aJni,aText);
       
   153     return toolkit->ExecuteTrap(&SetTextL,textComponent,(const TDesC*)&text);
       
   154 }
       
   155 
       
   156 
       
   157 LOCAL_C void InsertTextL(MMIDTextBox* aTextComponent,const TDesC* aText,TInt aPosition)
       
   158 {
       
   159     aTextComponent->InsertTextL(*aText,aPosition);
       
   160 }
       
   161 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_TextBox__1insertString
       
   162 (JNIEnv* aJni,jobject,jint aTextComponent,jint aToolkit,jstring aText,jint aPosition)
       
   163 {
       
   164     MMIDTextBox* textComponent = MIDUnhandObject<MMIDTextBox>(aTextComponent);
       
   165     CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit);
       
   166     RJString text(*aJni,aText);
       
   167     return toolkit->ExecuteTrap(&InsertTextL,textComponent,(const TDesC*)&text,(TInt)aPosition);
       
   168 }
       
   169 
       
   170 
       
   171 LOCAL_C void DeleteTextL(MMIDTextBox* aTextComponent,TInt aOffset,TInt aLength)
       
   172 {
       
   173     aTextComponent->DeleteTextL(aOffset,aLength);
       
   174 }
       
   175 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_TextBox__1delete
       
   176 (JNIEnv*,jobject,jint aTextComponent,jint aToolkit,jint aOffset,jint aLength)
       
   177 {
       
   178     MMIDTextBox* textComponent = MIDUnhandObject<MMIDTextBox>(aTextComponent);
       
   179     CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit);
       
   180     return toolkit->ExecuteTrap(&DeleteTextL,textComponent,(TInt)aOffset,(TInt)aLength);
       
   181 }
       
   182 
       
   183 
       
   184 LOCAL_C void SetMaxSizeL(MMIDTextBox* aTextComponent,TInt aSize, TInt* aMaxSize)
       
   185 {
       
   186     *aMaxSize = aTextComponent->SetMaxSizeL(aSize);
       
   187 }
       
   188 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_TextBox__1setMaxSize
       
   189 (JNIEnv*,jobject,jint aTextComponent,jint aToolkit,jint aSize)
       
   190 {
       
   191     MMIDTextBox* textComponent = MIDUnhandObject<MMIDTextBox>(aTextComponent);
       
   192     CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit);
       
   193     // Do server side as if the current contents of the TextBox are larger than
       
   194     // maxSize,the contents are truncated to fit.
       
   195     TInt maxSize = 0;
       
   196     TInt err =  toolkit->ExecuteTrap(&SetMaxSizeL,textComponent,(TInt)aSize, &maxSize);
       
   197     return err == KErrNone ? maxSize : err;
       
   198 }
       
   199 
       
   200 
       
   201 LOCAL_C TInt GetMaxSize(MMIDTextBox* aTextComponent)
       
   202 {
       
   203     return aTextComponent->GetMaxSize();
       
   204 }
       
   205 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_TextBox__1getMaxSize
       
   206 (JNIEnv*,jobject,jint aTextComponent,jint aToolkit)
       
   207 {
       
   208     MMIDTextBox* textComponent = MIDUnhandObject<MMIDTextBox>(aTextComponent);
       
   209     CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit);
       
   210     return toolkit->Execute(&GetMaxSize,textComponent);
       
   211 }
       
   212 
       
   213 
       
   214 LOCAL_C TInt Size(MMIDTextBox* aTextComponent)
       
   215 {
       
   216     return aTextComponent->Size();
       
   217 }
       
   218 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_TextBox__1size(JNIEnv*,jobject,jint aTextComponent,jint aToolkit)
       
   219 {
       
   220     MMIDTextBox* textComponent = MIDUnhandObject<MMIDTextBox>(aTextComponent);
       
   221     CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit);
       
   222     return toolkit->Execute(&Size,textComponent);
       
   223 }
       
   224 
       
   225 
       
   226 LOCAL_C void SetConstraintsL(MMIDTextBox* aTextComponent,TUint aConstraints)
       
   227 {
       
   228     aTextComponent->SetConstraintsL(aConstraints);
       
   229 }
       
   230 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_TextBox__1setConstraints
       
   231 (JNIEnv*,jobject,jint aTextComponent,jint aToolkit,jint aConstraints)
       
   232 {
       
   233     MMIDTextBox* textComponent = MIDUnhandObject<MMIDTextBox>(aTextComponent);
       
   234     CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit);
       
   235     return toolkit->ExecuteTrap(&SetConstraintsL,textComponent,(TUint)aConstraints);
       
   236 }
       
   237 
       
   238 
       
   239 LOCAL_C void SetInitialInputModeL(MMIDTextBox* aTextComponent,const TDesC* aCharacterSubset)
       
   240 {
       
   241     aTextComponent->SetInitialInputModeL(*aCharacterSubset);
       
   242 }
       
   243 JNIEXPORT jint JNICALL Java_javax_microedition_lcdui_TextBox__1setInitialInputMode
       
   244 (JNIEnv* aJni,jobject,jint aItem,jint aToolkit,jstring aCharacterSubset)
       
   245 {
       
   246     MMIDTextBox* textComponent = MIDUnhandObject<MMIDTextBox>(aItem);
       
   247     CMIDToolkit* toolkit = JavaUnhand<CMIDToolkit>(aToolkit);
       
   248     RJString charSubset(*aJni,aCharacterSubset);
       
   249     return toolkit->ExecuteTrap(&SetInitialInputModeL,textComponent,(const TDesC*)&charSubset);
       
   250 }