javauis/lcdui_akn/lcdui/src/CMIDTextBoxDialogControl.cpp
branchRCL_3
changeset 19 04becd199f91
child 60 6c158198356e
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008-2009 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:  Pop-up TextBox dialog control implementation for S60
       
    15 *
       
    16 */
       
    17 
       
    18 #include <eikenv.h>
       
    19 #include "CMIDTextBoxDialogControl.h"
       
    20 
       
    21 #include "lcdui.hrh"
       
    22 #include <lcdui.rsg>
       
    23 #include <EIKCOCTL.rsg>
       
    24 
       
    25 #include <j2me/jdebug.h>
       
    26 
       
    27 
       
    28 CMIDTextBoxDialogControl* CMIDTextBoxDialogControl::NewL(
       
    29     TInt aConstraints,
       
    30     const TDesC& aText,
       
    31     TInt aMaxSize,
       
    32     MMIDDisplayable* aDisplayable)
       
    33 {
       
    34     CMIDTextBoxDialogControl* self =
       
    35         new(ELeave) CMIDTextBoxDialogControl();
       
    36 
       
    37     CleanupStack::PushL(self);
       
    38     self->ConstructL(aConstraints, aText, aMaxSize, aDisplayable);
       
    39     CleanupStack::Pop(self);
       
    40 
       
    41     return self;
       
    42 }
       
    43 
       
    44 CMIDTextBoxDialogControl::~CMIDTextBoxDialogControl()
       
    45 {
       
    46     if (iDialog)
       
    47     {
       
    48         delete iDialog;
       
    49     }
       
    50 
       
    51     iTextContent.Close();
       
    52     delete iCurrentText;
       
    53 
       
    54     // Displayable is notified about content control deletion
       
    55     if (iDisplayable)
       
    56     {
       
    57         iDisplayable->NotifyContentDestroyed();
       
    58     }
       
    59 }
       
    60 
       
    61 void CMIDTextBoxDialogControl::ConstructL(
       
    62     TInt aConstraints,
       
    63     const TDesC& aText,
       
    64     TInt aMaxSize,
       
    65     MMIDDisplayable* aDisplayable)
       
    66 {
       
    67     iDisplayable = static_cast< CMIDDisplayable* >(aDisplayable);
       
    68     iTextContent.CreateL(KNullDesC, aMaxSize);
       
    69 
       
    70     CreateTextBoxQueryDialogL(iDialog, aConstraints, aMaxSize, aText);
       
    71 
       
    72     iDisplayable->SetPopupTextBox(ETrue);
       
    73     SetRect(iDisplayable->Rect());
       
    74     iDisplayable->SetComponentL(*this);
       
    75 
       
    76     SetTextL(aText);
       
    77     SetContainerWindowL(*iDisplayable);
       
    78 
       
    79     iCurrentText = GetTextL();
       
    80 
       
    81     if (iCurrentText)
       
    82     {
       
    83         iCursorPosition = iCurrentText->Length();
       
    84     }
       
    85     else
       
    86     {
       
    87         iCursorPosition = 0;
       
    88     }
       
    89 }
       
    90 
       
    91 CMIDTextBoxDialogControl::CMIDTextBoxDialogControl()
       
    92 {
       
    93 }
       
    94 
       
    95 void CMIDTextBoxDialogControl::CreateTextBoxQueryDialogL(
       
    96     CMIDTextBoxQueryDialog*& aDialog,
       
    97     TInt aConstraints, TInt aMaxSize,
       
    98     const TDesC& aText)
       
    99 {
       
   100     CMIDTextBoxQueryDialog* dialog = CMIDTextBoxQueryDialog::NewLC(
       
   101                                          aConstraints,
       
   102                                          iTextContent,
       
   103                                          aMaxSize,
       
   104                                          iDisplayable,
       
   105                                          aText);
       
   106 
       
   107     dialog->MakeVisible(EFalse);
       
   108     dialog->SetEditorL();
       
   109     aDialog = dialog;
       
   110     CleanupStack::Pop(dialog);
       
   111 }
       
   112 
       
   113 /** Deletes the existing text box dialog and recreates a new one. This is called by SetConstraints().
       
   114 If we fail creating the new text box we make sure the old one is still
       
   115 there. Makes sure the focus position is preserved. */
       
   116 void CMIDTextBoxDialogControl::RecreateTextBoxL(TUint aConstraints)
       
   117 {
       
   118     ASSERT(iDialog);
       
   119     HandleCurrentL(EFalse);
       
   120     HBufC* text = iDialog->GetTextL();
       
   121     CleanupStack::PushL(text);
       
   122 
       
   123     TInt maxSize = iDialog->GetMaxSize();
       
   124     iCursorPosition = iDialog->GetCaretPosition();
       
   125 
       
   126     // TextBox's focus state is stored because after recreating
       
   127     // TextBox the focus information is lost.
       
   128     TBool hasFocus = IsFocused();
       
   129     iTextContent.Close();
       
   130     iTextContent.CreateL(KNullDesC, maxSize);
       
   131 
       
   132     CMIDTextBoxQueryDialog* dialog = NULL;
       
   133     CreateTextBoxQueryDialogL(dialog, aConstraints, maxSize, KNullDesC);
       
   134     delete iDialog;
       
   135     iDialog = dialog;
       
   136 
       
   137     if (text)
       
   138     {
       
   139         iDialog->SetTextWithNewConstraintsL(text);
       
   140     }
       
   141 
       
   142     CleanupStack::PopAndDestroy(text);
       
   143 
       
   144     delete iCurrentText;
       
   145     iCurrentText = NULL;
       
   146     iCurrentText = GetTextL();
       
   147 
       
   148     // Check if the text size has changed and reposition cursor if needed
       
   149     if (iCursorPosition > iDialog->Size())
       
   150     {
       
   151         iCursorPosition = iDialog->Size();
       
   152     }
       
   153 
       
   154     // Activate new iTextBox with CoeControl
       
   155     ActivateL();
       
   156 
       
   157     // If TextBox had focus before recreation, the focus is restored.
       
   158     if (hasFocus)
       
   159     {
       
   160         iDialog->SetFocus(ETrue);
       
   161     }
       
   162 
       
   163     iDialog->SetTitleL(iDisplayable->Title());
       
   164     HandleCurrentL(iDisplayable->IsActive());
       
   165 }
       
   166 
       
   167 void CMIDTextBoxDialogControl::HandleCurrentL(TBool aCurrent)
       
   168 {
       
   169     if (aCurrent)
       
   170     {
       
   171         iEikonEnv->EikAppUi()->AddToStackL(iDialog, ECoeStackPriorityMenu);
       
   172 
       
   173         // Set dialog cursor to zero and back again after it returns from
       
   174         // iDialog->ShowL.
       
   175 
       
   176         TInt curPosition = iCursorPosition;
       
   177         iCursorPosition = 0;
       
   178         iDialog->SetCursorPositionL(0);
       
   179         iDialog->ShowL(ETrue);
       
   180 
       
   181         if (iCurrentText)
       
   182         {
       
   183             SetTextL(*iCurrentText);
       
   184         }
       
   185         iCursorPosition = curPosition;
       
   186         // Set cursor position back to original value.
       
   187         iDialog->SetCursorPositionL(iCursorPosition);
       
   188     }
       
   189     else
       
   190     {
       
   191         delete iCurrentText;
       
   192         iCurrentText = NULL;
       
   193         iCurrentText = GetTextL();
       
   194         iCursorPosition = iDialog->GetCaretPosition();
       
   195         iDialog->ShowL(EFalse);
       
   196         iEikonEnv->EikAppUi()->RemoveFromStack(iDialog);
       
   197     }
       
   198 }
       
   199 
       
   200 //
       
   201 // methods from MMIDTextBox
       
   202 //
       
   203 
       
   204 void CMIDTextBoxDialogControl::DeleteTextL(TInt aOffset, TInt aLength)
       
   205 {
       
   206     if (iDialog)
       
   207     {
       
   208         iDialog->DeleteTextL(aOffset, aLength);
       
   209         iCursorPosition = GetCaretPosition();
       
   210         delete iCurrentText;
       
   211         iCurrentText = NULL;
       
   212         iCurrentText = GetTextL();
       
   213     }
       
   214 }
       
   215 
       
   216 void CMIDTextBoxDialogControl::SetTextL(const TDesC& aText)
       
   217 {
       
   218     if (iDialog)
       
   219     {
       
   220         iDialog->SetTextL(aText);
       
   221         iCursorPosition = GetCaretPosition();
       
   222         delete iCurrentText;
       
   223         iCurrentText = NULL;
       
   224         iCurrentText = GetTextL();
       
   225     }
       
   226 }
       
   227 
       
   228 void CMIDTextBoxDialogControl::InsertTextL(
       
   229     const TDesC& aText, TInt aPosition)
       
   230 {
       
   231     if (iDialog)
       
   232     {
       
   233         iDialog->InsertTextL(aText, aPosition);
       
   234         iCursorPosition = GetCaretPosition();
       
   235         delete iCurrentText;
       
   236         iCurrentText = NULL;
       
   237         iCurrentText = GetTextL();
       
   238     }
       
   239 }
       
   240 
       
   241 void CMIDTextBoxDialogControl::SetConstraintsL(TUint aConstraints)
       
   242 {
       
   243     RecreateTextBoxL(aConstraints);
       
   244     delete iCurrentText;
       
   245     iCurrentText = NULL;
       
   246     iCurrentText = GetTextL();
       
   247 }
       
   248 
       
   249 TInt CMIDTextBoxDialogControl::SetMaxSizeL(TInt aSize)
       
   250 {
       
   251     TInt maxSize = iDialog->SetMaxSizeL(aSize);
       
   252     iCursorPosition = GetCaretPosition();
       
   253     delete iCurrentText;
       
   254     iCurrentText = NULL;
       
   255     iCurrentText = GetTextL();
       
   256     return maxSize;
       
   257 }
       
   258 
       
   259 TInt CMIDTextBoxDialogControl::GetMaxSize()
       
   260 {
       
   261     return iDialog->GetMaxSize();
       
   262 }
       
   263 
       
   264 TInt CMIDTextBoxDialogControl::Size()
       
   265 {
       
   266     return iDialog->Size();
       
   267 }
       
   268 
       
   269 TInt CMIDTextBoxDialogControl::GetCaretPosition()
       
   270 {
       
   271     return iDialog->GetCaretPosition();
       
   272 }
       
   273 
       
   274 HBufC* CMIDTextBoxDialogControl::GetTextL()
       
   275 {
       
   276     return iDialog->GetTextL();
       
   277 }
       
   278 
       
   279 void CMIDTextBoxDialogControl::SetInitialInputModeL(
       
   280     const TDesC& aCharacterSubset)
       
   281 {
       
   282     iDialog->SetInitialInputModeL(aCharacterSubset);
       
   283     delete iCurrentText;
       
   284     iCurrentText = NULL;
       
   285     iCurrentText = GetTextL();
       
   286 }
       
   287 
       
   288 TInt CMIDTextBoxDialogControl::CountComponentControls() const
       
   289 {
       
   290     return 1;  //  iTextBox
       
   291 }
       
   292 
       
   293 CCoeControl* CMIDTextBoxDialogControl::ComponentControl(TInt aIndex) const
       
   294 {
       
   295     if (aIndex == 0)
       
   296     {
       
   297         return iDialog;
       
   298     }
       
   299 
       
   300     return NULL;
       
   301 }
       
   302 
       
   303 TKeyResponse CMIDTextBoxDialogControl::OfferKeyEventL(
       
   304     const TKeyEvent& aKeyEvent, TEventCode aType)
       
   305 {
       
   306     // If midlet is closed from applications menu or by pressing end key, then
       
   307     // 'esc' key event is generated. In that case we must close the dialog.
       
   308     if (aKeyEvent.iCode == EKeyEscape &&
       
   309             aKeyEvent.iModifiers == 0 &&
       
   310             aType == EEventKey)
       
   311     {
       
   312         HandleCurrentL(EFalse);
       
   313 
       
   314         return EKeyWasConsumed;
       
   315     }
       
   316 
       
   317     return EKeyWasNotConsumed;
       
   318 }
       
   319 
       
   320 void CMIDTextBoxDialogControl::FocusChanged(TDrawNow /*aDrawNow*/)
       
   321 {
       
   322     if (iDialog)
       
   323     {
       
   324         if (IsFocused())
       
   325         {
       
   326             iDialog->SetFocus(ETrue);
       
   327         }
       
   328         else
       
   329         {
       
   330             iDialog->SetFocus(EFalse);
       
   331         }
       
   332     }
       
   333 }
       
   334 
       
   335 void CMIDTextBoxDialogControl::ActivateL()
       
   336 {
       
   337     CCoeControl::ActivateL();
       
   338 
       
   339     // Cursor position must be set, because CEikEdwin doesn't leave the cursor
       
   340     // at its current position when the CEikEdwin is re-activated.
       
   341     iDialog->SetCursorPositionL(iCursorPosition);
       
   342 
       
   343     // needed to be called explicitly to layout controls as ActivateL() will not call it if size has not changed
       
   344     iDialog->SizeChanged();
       
   345 }
       
   346 
       
   347 void CMIDTextBoxDialogControl::SetTitleL(const TDesC* aString)
       
   348 {
       
   349     if (aString)
       
   350     {
       
   351         iDialog->SetTitleL(aString);
       
   352     }
       
   353     else
       
   354     {
       
   355         iDialog->SetTitleL(&KNullTitle);
       
   356     }
       
   357 
       
   358     // By recreating the dialog we ensure that the layout is correct.
       
   359     // Recreation is not needed when dialog is visible.
       
   360     if (iDialog->Showing())
       
   361     {
       
   362         RecreateTextBoxL(iDialog->Constraints());
       
   363         delete iCurrentText;
       
   364         iCurrentText = NULL;
       
   365         iCurrentText = GetTextL();
       
   366     }
       
   367 }
       
   368 
       
   369 CMIDTextBoxQueryDialog* CMIDTextBoxDialogControl::Dialog()
       
   370 {
       
   371     return iDialog;
       
   372 }
       
   373 
       
   374 // End of file