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