javauis/lcdui_akn/lcdui/src/CMIDDateFieldItem.cpp
branchRCL_3
changeset 26 2455ef1f5bbc
equal deleted inserted replaced
25:ae942d28ec0e 26:2455ef1f5bbc
       
     1 /*
       
     2 * Copyright (c) 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:  ?Description
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <eikenv.h>
       
    20 #include <eiklabel.h>
       
    21 #include <eikon.hrh>
       
    22 #include <AknLayoutFont.h>
       
    23 
       
    24 // LAF API in several places
       
    25 #include <aknlayoutscalable_avkon.cdl.h>
       
    26 #include <AknUtils.h>
       
    27 #include "CMIDDateFieldItem.h"
       
    28 // API used for retrieving commands count (in IsSelectable function)
       
    29 #include "CMIDCommandList.h"
       
    30 // API needed for iLabelControl (inherited from CMIDControlItem)
       
    31 #include "CMIDItemLabel.h"
       
    32 #include "CMIDDisplayable.h"
       
    33 #include "CMIDUtils.h"
       
    34 #include "CMIDTicker.h"
       
    35 #include "CMIDUIManager.h"
       
    36 
       
    37 #include <j2me/jdebug.h>
       
    38 
       
    39 #undef  TRAP_INSTRUMENTATION_LEAVE
       
    40 #define TRAP_INSTRUMENTATION_LEAVE(aResult) DEBUG_INT2("In CMIDDateFieldItem.cpp, trapped method was called at line %D and got exception %D", __LINE__, aResult);
       
    41 
       
    42 
       
    43 // Must be used in TIME case due to strange definition of KAknMinimumDate
       
    44 #define KCMIDDateFieldMinTime (TTime(TDateTime(0, EJanuary, 0, 0, 0, 0, 0)))
       
    45 
       
    46 #define KCMIDDateFieldEpochDate (TTime(TDateTime(1970, EJanuary, 1-1, 0, 0, 0, 0)))
       
    47 _LIT(KTimeAndDateFieldSeparator," ");
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 CMIDDateFieldItem* CMIDDateFieldItem::NewL(
       
    54     const TDesC& aLabel, MMIDDateField::TInputMode aInputMode, CMIDUIManager* aUIManager)
       
    55 {
       
    56     CMIDDateFieldItem* item = new(ELeave)CMIDDateFieldItem(aUIManager);
       
    57     CleanupStack::PushL(item);
       
    58     item->ConstructL(aLabel,aInputMode);
       
    59     CleanupStack::Pop(item);
       
    60     return item;
       
    61 }
       
    62 
       
    63 CMIDDateFieldItem::~CMIDDateFieldItem()
       
    64 {
       
    65     if (iAmPmToggleCommand)
       
    66     {
       
    67         iAmPmToggleCommand->SetObserver(NULL);
       
    68         iAmPmToggleCommand->Dispose();
       
    69         iAmPmToggleCommand = NULL;
       
    70     }
       
    71     if (iEditor)
       
    72     {
       
    73 #ifndef RD_JAVA_S60_RELEASE_9_2
       
    74         iEditor->SetFocus(ETrue);   // needed to notify observers about editor destruction
       
    75 #endif // RD_JAVA_S60_RELEASE_9_2
       
    76         delete iEditor;
       
    77         iEditor = NULL;
       
    78     }
       
    79 }
       
    80 
       
    81 void CMIDDateFieldItem::SetDate(const TTime& aTime)
       
    82 {
       
    83     TTime time = aTime;
       
    84     if (iInputMode == MMIDDateField::ETime)
       
    85     {
       
    86         TDateTime dateTime = aTime.DateTime();
       
    87         time = TTime(TDateTime(0, EJanuary, 0,
       
    88                                dateTime.Hour(),
       
    89                                dateTime.Minute(),
       
    90                                dateTime.Second(),
       
    91                                dateTime.MicroSecond()));
       
    92     }
       
    93     else
       
    94     {
       
    95         TDateTime dateTime = aTime.DateTime();
       
    96         if (dateTime.Year() == 0)
       
    97         { // Date is not set or DateField type is switched
       
    98             // from ETime input mode. Date part set to "zero epoch"
       
    99             TDateTime epochDate = KCMIDDateFieldEpochDate.DateTime();
       
   100             time = TTime(TDateTime(epochDate.Year(), // set to "zero epoch"
       
   101                                    epochDate.Month(),
       
   102                                    epochDate.Day(),
       
   103                                    dateTime.Hour(),
       
   104                                    dateTime.Minute(),
       
   105                                    dateTime.Second(),
       
   106                                    dateTime.MicroSecond()));
       
   107         }
       
   108     }
       
   109 
       
   110     iEditor->SetTTime(time);
       
   111     SetInitialized(EFalse);
       
   112 }
       
   113 
       
   114 TTime CMIDDateFieldItem::Date() const
       
   115 {
       
   116     TInt err = KErrNone;
       
   117     TTime ret = KCMIDDateFieldEpochDate;
       
   118     TBool nonEmpty = IsDateTimeNonEmpty();
       
   119 
       
   120     if (nonEmpty)
       
   121     {
       
   122         // GetTTime leaves if there is zero in the day, month or year fields.
       
   123         TRAP(err, ret = iEditor->GetTTime());
       
   124     }
       
   125 
       
   126     if ((err != KErrNone) || !nonEmpty)
       
   127     {
       
   128         TRAP(err, iEditor->PrepareForFocusLossL())
       
   129         // GetTTime should not leave after calling PrepareForFocusLossL
       
   130         ret = iEditor->GetTTime();
       
   131     }
       
   132 
       
   133     return ret;
       
   134 }
       
   135 
       
   136 void CMIDDateFieldItem::SetUninitialized()
       
   137 {
       
   138     iInitialised = EFalse;
       
   139 
       
   140     iEditor->SetTTime(KCMIDDateFieldEpochDate);
       
   141 
       
   142     if (iInputMode == MMIDDateField::ETime)
       
   143     {
       
   144         iEditor->SetTTime(KCMIDDateFieldMinTime);
       
   145         reinterpret_cast<CEikTimeEditor*>(iEditor)->SetUninitialised(ETrue);
       
   146     }
       
   147     else if (iInputMode == MMIDDateField::EDate)
       
   148     {
       
   149         reinterpret_cast<CEikDateEditor*>(iEditor)->SetUninitialised(ETrue);
       
   150     }
       
   151     else if (iInputMode == MMIDDateField::EDateTime)
       
   152     {
       
   153         reinterpret_cast<CEikTimeAndDateEditor*>(iEditor)->SetUninitialised(ETrue);
       
   154     }
       
   155     DoSafeDraw();
       
   156 }
       
   157 
       
   158 void CMIDDateFieldItem::SetInputModeL(MMIDDateField::TInputMode aInputMode)
       
   159 {
       
   160     CEikTTimeEditor* editor = NULL;
       
   161 
       
   162     TBool validOldTime = IsDateTimeNonEmpty();
       
   163     TTime oldTime;
       
   164     if (validOldTime)
       
   165     {
       
   166         // GetTTime leaves if there is 0 in day, month or year fields
       
   167         TRAPD(err, oldTime = iEditor->GetTTime());
       
   168         if (err != KErrNone)
       
   169         {
       
   170             validOldTime = EFalse;
       
   171         }
       
   172     }
       
   173 
       
   174     switch (aInputMode)
       
   175     {
       
   176     case MMIDDateField::EDate:
       
   177     {
       
   178         CEikDateEditor* dateEditor = new(ELeave) CEikDateEditor();
       
   179         CleanupStack::PushL(dateEditor);
       
   180         dateEditor->ConstructL(KAknMinimumDate,KAknMaximumDate,KCMIDDateFieldEpochDate,ETrue);
       
   181         CleanupStack::Pop(dateEditor);
       
   182         dateEditor->SetUninitialised(ETrue);
       
   183         editor = dateEditor;
       
   184         break;
       
   185     }
       
   186     case MMIDDateField::ETime:
       
   187     {
       
   188         CEikTimeEditor* timeEditor = new(ELeave) CEikTimeEditor();
       
   189         CleanupStack::PushL(timeEditor);
       
   190         timeEditor->ConstructL(KCMIDDateFieldMinTime,
       
   191                                KAknMaximumDate,
       
   192                                KCMIDDateFieldMinTime,
       
   193                                EEikTimeWithoutSecondsField);
       
   194         CleanupStack::Pop(timeEditor);
       
   195         timeEditor->SetUninitialised(ETrue);
       
   196         editor = timeEditor;
       
   197         break;
       
   198     }
       
   199     case MMIDDateField::EDateTime:
       
   200     {
       
   201         CEikTimeAndDateEditor* timeDateEditor = new(ELeave) CEikTimeAndDateEditor();
       
   202         CleanupStack::PushL(timeDateEditor);
       
   203         TUint32 flags = EEikTimeWithoutSecondsField + EEikDateWithoutPopoutCalendar;
       
   204         TPtrC separatorPtr(KTimeAndDateFieldSeparator);
       
   205         HBufC* separator = separatorPtr.AllocLC();
       
   206         timeDateEditor->ConstructL(KAknMinimumDate,
       
   207                                    KAknMaximumDate,
       
   208                                    KCMIDDateFieldEpochDate,flags,separator);
       
   209         CleanupStack::Pop(separator); // Do not destroy separator as
       
   210         // CEikTimeAndDateEditor takes control of it
       
   211         CleanupStack::Pop(timeDateEditor);
       
   212         timeDateEditor->SetUninitialised(ETrue);
       
   213         editor = timeDateEditor;
       
   214         break;
       
   215     }
       
   216     default:
       
   217         ASSERT(EFalse);
       
   218     }
       
   219 
       
   220     delete iEditor;
       
   221     iEditor = editor;
       
   222 
       
   223     iInputMode = aInputMode;
       
   224 
       
   225     // Set font from LAF to the editor, so that MinimumSize() will return correct values.
       
   226     // AknLayoutUtils::LayoutMfne() will also set the font from LAF to the editor
       
   227     iEditor->SetFont(AknLayoutUtils::FontFromId(
       
   228                          AknLayoutScalable_Avkon::form2_midp_time_pane_t1().Font()));
       
   229 
       
   230     iEditor->SetBorder(TGulBorder::ENone);
       
   231 
       
   232 
       
   233     if (iForm)
       
   234     {
       
   235         iEditor->SetContainerWindowL(*this);
       
   236         iEditor->ActivateL();
       
   237     }
       
   238 
       
   239     if (validOldTime)
       
   240     {
       
   241         SetDate(oldTime);
       
   242         DoSafeDraw();
       
   243     }
       
   244     else
       
   245     {
       
   246         iInitialised = EFalse;
       
   247     }
       
   248 
       
   249     // Text colour from skin
       
   250     iEditor->SetSkinTextColorL(EAknsCIQsnTextColorsCG6);
       
   251 }
       
   252 
       
   253 void CMIDDateFieldItem::SetInitialized(TInt aSetCurrentTime /* = ETrue */)
       
   254 {
       
   255     iInitialised = ETrue;
       
   256 
       
   257     if (iInputMode == MMIDDateField::ETime)
       
   258     {
       
   259         reinterpret_cast<CEikTimeEditor*>(iEditor)->SetUninitialised(EFalse);
       
   260     }
       
   261     else if (iInputMode == MMIDDateField::EDate)
       
   262     {
       
   263         reinterpret_cast<CEikDateEditor*>(iEditor)->SetUninitialised(EFalse);
       
   264     }
       
   265     else if (iInputMode == MMIDDateField::EDateTime)
       
   266     {
       
   267         reinterpret_cast<CEikTimeAndDateEditor*>(iEditor)->SetUninitialised(EFalse);
       
   268     }
       
   269 
       
   270     if (aSetCurrentTime)
       
   271     {
       
   272         TTime time;
       
   273         time.HomeTime();
       
   274         iEditor->SetTTime(time);
       
   275     }
       
   276 
       
   277     iEditor->DrawDeferred();
       
   278 }
       
   279 
       
   280 // ---------------------------------------------------------------------------
       
   281 //
       
   282 // ---------------------------------------------------------------------------
       
   283 //
       
   284 CMIDDateFieldItem::CMIDDateFieldItem(CMIDUIManager* aUIManager)
       
   285         :CMIDControlItem(EDefault, aUIManager),
       
   286         iInitialised(EFalse),
       
   287         iAmPmToggleCommand(0)
       
   288 {
       
   289     iMMidItem = this;
       
   290 }
       
   291 
       
   292 void CMIDDateFieldItem::ConstructL(const TDesC& aLabel,MMIDDateField::TInputMode aInputMode)
       
   293 {
       
   294     CMIDControlItem::ConstructL();
       
   295     SetLabelL(aLabel);
       
   296     SetFocusing(ETrue);
       
   297 
       
   298     UpdateMemberVariables();
       
   299 
       
   300     SetInputModeL(aInputMode);
       
   301     // Create built-in command to toggle am/pm value
       
   302     HBufC* label = iEikonEnv->AllocReadResourceL(R_QTN_MSK_CHANGE);
       
   303     CleanupStack::PushL(label);
       
   304     iAmPmToggleCommand = CMIDCommand::NewBuiltInCommandL(label->Des(),
       
   305                          MMIDCommand::EItem,
       
   306                          CMIDCommand::EAmPmToggleCommandId);
       
   307     CleanupStack::PopAndDestroy(label); // command creates a copy of the label
       
   308     iAmPmToggleCommand->SetObserver(this);
       
   309 }
       
   310 
       
   311 void CMIDDateFieldItem::SetLabelL(const TDesC& aLabel)
       
   312 {
       
   313     CMIDControlItem::SetLabelL(aLabel);
       
   314 }
       
   315 
       
   316 /** Recalculate the preferred size.*/
       
   317 TSize CMIDDateFieldItem::ResetPreferredSize() const
       
   318 {
       
   319     CMIDDateFieldItem* self = const_cast<CMIDDateFieldItem*>(this);
       
   320     TRAP_IGNORE(self->SetPreferredSizeL(iRequestedPreferredSize));
       
   321     return iPreferredSize;
       
   322 }
       
   323 
       
   324 void CMIDDateFieldItem::SetPreferredSizeL(const TSize& aSize)
       
   325 {
       
   326     iRequestedPreferredSize = CheckRequestedSize(aSize);
       
   327     iPreferredSize = iRequestedPreferredSize;
       
   328 
       
   329     if (iPreferredSize.iWidth < 0)
       
   330     { // height is specified but width isn't
       
   331         iPreferredSize.iWidth = Max(iLabelControl->PreferredWidth(),
       
   332                                     iEditor->Size().iWidth + iMargins.iLeft + iMargins.iRight);
       
   333     }
       
   334 
       
   335     // make sure the width doesn't get bigger than the form width
       
   336     iPreferredSize.iWidth = Min(iPreferredSize.iWidth, FormClientAreaWidth());
       
   337 
       
   338     if (iPreferredSize.iHeight < 0)
       
   339     {  // width is specified but height isn't, do text wrapping (call SetWidthL)
       
   340         // so that we can then calculate the height
       
   341         if (HasLabel())
       
   342         {
       
   343             iLabelControl->SetWidthL(iPreferredSize.iWidth);
       
   344         }
       
   345 
       
   346         iPreferredSize.iHeight = LabelHeight() + ItemPreferredHeightWithoutLabel();
       
   347     }
       
   348 
       
   349     TSize minimumSize = MinimumSize();
       
   350 
       
   351     if ((minimumSize.iWidth == 0) && (minimumSize.iHeight == 0))
       
   352     { //it means there is no control and no label, so set our size to null
       
   353         iPreferredSize.iWidth = 0;
       
   354         iPreferredSize.iHeight = 0;
       
   355     }
       
   356     else
       
   357     { //make sure the preferred size is not smaller than the minimum size
       
   358         iPreferredSize.iWidth = Max(iPreferredSize.iWidth, minimumSize.iWidth);
       
   359         iPreferredSize.iHeight = Max(iPreferredSize.iHeight, minimumSize.iHeight);
       
   360     }
       
   361 
       
   362 }
       
   363 
       
   364 TBool CMIDDateFieldItem::IsSelectable() const
       
   365 {
       
   366     return (!IsNonFocusing() || CommandList()->Count() > 0);
       
   367 }
       
   368 
       
   369 TSize CMIDDateFieldItem::MinimumSize()
       
   370 {
       
   371     TBool hasLabel = HasLabel();
       
   372     TInt height = ItemPreferredHeightWithoutLabel();
       
   373 
       
   374     if (hasLabel)
       
   375     {
       
   376         height += OneLineLabelHeight();
       
   377     }
       
   378 
       
   379     TInt formWidth = FormClientAreaWidth();
       
   380     TInt width     = formWidth;
       
   381 
       
   382     if (Layout() & ELayout2)
       
   383     {
       
   384         TInt labelMinWidth = hasLabel ? iLabelControl->MinimumSize().iWidth : 0;
       
   385 
       
   386         width = Max(iEditor->MinimumSize().iWidth + iMargins.iLeft + iMargins.iRight,
       
   387                     labelMinWidth);
       
   388         width = Min(width, formWidth);
       
   389     }
       
   390 
       
   391     return TSize(width, height);
       
   392 }
       
   393 
       
   394 TInt CMIDDateFieldItem::CountComponentControls() const
       
   395 {
       
   396     return 2;
       
   397 }
       
   398 
       
   399 CCoeControl* CMIDDateFieldItem::ComponentControl(TInt aIndex) const
       
   400 {
       
   401 
       
   402     switch (aIndex)
       
   403     {
       
   404     case 0:
       
   405         return iLabelControl;
       
   406 
       
   407     case 1:
       
   408         return iEditor;
       
   409     }
       
   410 
       
   411     ASSERT(NULL);
       
   412     return NULL;
       
   413 }
       
   414 
       
   415 void CMIDDateFieldItem::Draw(const TRect& aRect) const
       
   416 {
       
   417     if (!iForm)
       
   418     {
       
   419         return; // if we don't have a form we shouldn't try to draw
       
   420     }
       
   421 
       
   422     CMIDControlItem::Draw(aRect);
       
   423 }
       
   424 
       
   425 void CMIDDateFieldItem::SizeChanged()
       
   426 {
       
   427     TRect rect = Rect();
       
   428     TPoint topLeft = Position();
       
   429     TInt labelHeight = LabelHeight();
       
   430 
       
   431     iLabelControl->SetExtent(topLeft, TSize(rect.Width(), labelHeight));
       
   432 
       
   433     rect.iTl.iY += labelHeight;
       
   434     TAknLayoutRect layoutRect;
       
   435     layoutRect.LayoutRect(rect, AknLayoutScalable_Avkon::form2_midp_time_pane().LayoutLine());
       
   436 
       
   437     AknLayoutUtils::LayoutMfne(iEditor, layoutRect.Rect(),
       
   438                                AknLayoutScalable_Avkon::form2_midp_time_pane_t1().LayoutLine());
       
   439 
       
   440     CMIDControlItem::SizeChanged();
       
   441 }
       
   442 
       
   443 TKeyResponse CMIDDateFieldItem::OfferKeyEventL(const TKeyEvent& aKeyEvent, TEventCode aType)
       
   444 {
       
   445     TInt oldCurrentField = iEditor->CurrentField();
       
   446     TInt code = aKeyEvent.iCode;
       
   447     TBool initialised = EFalse;
       
   448     //
       
   449     // Enter Key
       
   450     //
       
   451     if (aType == EEventKey)
       
   452     {
       
   453         usedKeyEvent=&aKeyEvent;
       
   454         prevInitialised=iInitialised;
       
   455     }
       
   456 #ifdef RD_JAVA_S60_RELEASE_9_2
       
   457     if (iInitialised && ((aType == EEventKey && (aKeyEvent.iScanCode == EStdKeyEnter || aKeyEvent.iScanCode == EStdKeyDevice3)) ||
       
   458                          (aType == EEventKey && (aKeyEvent.iCode == EKeyEnter || aKeyEvent.iCode == EKeyDevice3) && (usedKeyEvent != &aKeyEvent) ||
       
   459                           (aType == EEventKeyUp && (aKeyEvent.iScanCode == EStdKeyEnter || aKeyEvent.iScanCode == EStdKeyDevice3) &&  prevInitialised == iInitialised && (usedKeyEvent != &aKeyEvent)))))
       
   460 #else
       
   461     if (iInitialised && ((aType == EEventKey &&  aKeyEvent.iScanCode == EStdKeyEnter) ||
       
   462                          (aType == EEventKey && aKeyEvent.iCode == EKeyEnter && (usedKeyEvent != &aKeyEvent) ||
       
   463                           (aType == EEventKeyUp && aKeyEvent.iScanCode == EStdKeyEnter &&  prevInitialised == iInitialised && (usedKeyEvent != &aKeyEvent)))))
       
   464 #endif // RD_JAVA_S60_RELEASE_9_2        
       
   465     {
       
   466 
       
   467         // Toggle AmPm
       
   468         if (IsCurrentFieldAmPmField())
       
   469         {
       
   470             ToggleAmPmFieldValue();
       
   471             return EKeyWasConsumed;
       
   472         }
       
   473         else
       
   474         {
       
   475             CMIDDisplayable& displayable = iForm->CurrentDisplayable();
       
   476             TInt cntOpt = displayable.NumCommandsForOkOptionsMenu();
       
   477 
       
   478             // set first command from command list,
       
   479             // if command list <= 0 then command sets NULL
       
   480             const CMIDCommand* command = (CommandList()->Count() > 0 ?
       
   481                                           CommandList()->At(0).iCommand :
       
   482                                           NULL);
       
   483 
       
   484             // Activate Default command
       
   485             // For default command will run ProcessCommandL( KItemCommandIdBase + 1 )
       
   486             if (DefaultCommand())
       
   487             {
       
   488                 displayable.ProcessCommandL(CommandList()->CommandOffset() +
       
   489                                             CommandList()->FindCommandIndex(DefaultCommand()));
       
   490                 return EKeyWasConsumed;
       
   491             }
       
   492             // Show Menu or activate one command
       
   493             else
       
   494             {
       
   495                 TInt numScreenOrHelpCommands = displayable.NumCommandsForScreenOrHelpOptionsMenu();
       
   496 
       
   497                 // Active Command Show Menu
       
   498                 // if ( cntOpt > 1 ) will run menu, else execute ProcessCommandL( CommandOffset )
       
   499                 if (cntOpt > 1)
       
   500                 {
       
   501                     displayable.MenuHandler()->ShowMenuL(CMIDMenuHandler::EOkMenu);
       
   502                     return EKeyWasConsumed;
       
   503                 }
       
   504                 else if (cntOpt == 0 && numScreenOrHelpCommands > 1)
       
   505                 {
       
   506                     // If there is more than one screen command on form
       
   507                     // and there is no ok/item commands then show menu
       
   508                     displayable.MenuHandler()->ShowMenuL(CMIDMenuHandler::EOptionsMenu);
       
   509                     return EKeyWasConsumed;
       
   510                 }
       
   511                 else if (command && command->CommandType() != MMIDCommand::EBack &&
       
   512                          command->CommandType() != MMIDCommand::ECancel)
       
   513                 {
       
   514                     displayable.ProcessCommandL(CommandList()->CommandOffset());
       
   515                     return EKeyWasConsumed;
       
   516                 }
       
   517                 else
       
   518                 {
       
   519                     displayable.ProcessCommandL(displayable.MainCommandList()->CommandOffset());
       
   520                     return EKeyWasConsumed;
       
   521                 }
       
   522             }
       
   523         }
       
   524     }
       
   525 
       
   526     //
       
   527     // End Enter Key
       
   528     //
       
   529     if (code == EKeyDownArrow || code == EKeyUpArrow ||
       
   530             (code == EKeyApplication0 || aKeyEvent.iScanCode == EStdKeyApplication0))
       
   531         return EKeyWasNotConsumed;
       
   532 
       
   533     if (!iInitialised && aType == EEventKey && !CMIDUtils::IgnoreKeyEvent(aKeyEvent.iCode))
       
   534     {
       
   535         if (code == EKeyBackspace)
       
   536         {
       
   537             return EKeyWasNotConsumed;
       
   538         }
       
   539         // Use any key to set an uninitialised DateField to an initialised one
       
   540         else if (code != EKeyLeftArrow && code != EKeyRightArrow && code != EKeyDevice3)
       
   541         {
       
   542             SetInitialized();
       
   543             initialised = ETrue;
       
   544         }
       
   545     }
       
   546 
       
   547     if ((aType == EEventKey) && (code == EKeyLeftArrow || code == EKeyRightArrow))
       
   548     {
       
   549         // is opened VKB
       
   550         CMIDDisplayable& displayable = iForm->CurrentDisplayable();
       
   551         if (!displayable.IsVKBOnScreen())
       
   552         {
       
   553             // Check if focus moving is possible inside the editor, if not, do nothing
       
   554             if ((code == EKeyLeftArrow) && (iEditor->CurrentField() == 0))
       
   555             {
       
   556                 return EKeyWasNotConsumed;
       
   557             }
       
   558             else if ((code == EKeyRightArrow) && (iEditor->CurrentField() == (iEditor->NumFields()-1)))
       
   559             {
       
   560                 return EKeyWasNotConsumed;
       
   561             }
       
   562         }
       
   563         // Redraw is needed here for properly text drawing.
       
   564         iEditor->DrawDeferred();
       
   565     }
       
   566 
       
   567     TUint scanCode = aKeyEvent.iScanCode;
       
   568     TBool losingFocus = EFalse;
       
   569     //
       
   570     // keyboard menu
       
   571     //
       
   572     switch (scanCode)
       
   573     {
       
   574     case EStdKeyF1:
       
   575     case EStdKeyMenu:
       
   576         losingFocus = ETrue;
       
   577     }
       
   578     //
       
   579     // Non keyboard zoom,menu,etc.
       
   580     //
       
   581     if (scanCode>=ESpecialKeyBase && scanCode<(ESpecialKeyBase+ESpecialKeyCount))
       
   582         losingFocus=ETrue;
       
   583     //
       
   584     // CBA buttons
       
   585     //
       
   586     if (scanCode>=EStdKeyDevice0 && scanCode<=EStdKeyDeviceF)
       
   587         losingFocus=ETrue;
       
   588     //
       
   589     if (iInitialised && losingFocus)
       
   590     {
       
   591         TRAP_IGNORE(iEditor->PrepareForFocusLossL());
       
   592     }
       
   593 
       
   594     CEikMfneField* currentField = iEditor->Field(iEditor->CurrentField());
       
   595     TBool notEmpty = currentField->IsValid();
       
   596 
       
   597     TKeyResponse ret =  iEditor->OfferKeyEventL(aKeyEvent,aType);
       
   598 
       
   599     if (oldCurrentField != iEditor->CurrentField())
       
   600     { // focus was moved -> update commands (toggle command may be possible)
       
   601         UpdateCommands();
       
   602     }
       
   603 
       
   604     if (aType == EEventKey && iEditor->Field(iEditor->CurrentField())->IsValid())
       
   605     {
       
   606         // Check if change event should be fired. The contents of the field is changed if:
       
   607         // - a number key is pressed (always changes the contents of the field) and zero is not the first number
       
   608         // - a backspace key is pressed when the the field was not empty
       
   609         // - the AM/PM field is changed
       
   610         // - the field was empty but the native component set value to it (arrow key was pressed)
       
   611         // - it was initialised
       
   612         TBool amPmField = IsCurrentFieldAmPmField();
       
   613         TChar ch = TChar(code);
       
   614         if (ch.IsDigit() ||
       
   615                 (code == EKeyBackspace && notEmpty) ||
       
   616                 (amPmField && code != EKeyLeftArrow && code != EKeyRightArrow) ||
       
   617                 (!notEmpty && currentField->IsValid()) || initialised)
       
   618         {
       
   619             ReportEventL(MCoeControlObserver::EEventStateChanged);
       
   620         }
       
   621     }
       
   622     return ret;
       
   623 }
       
   624 
       
   625 #ifdef RD_SCALABLE_UI_V2
       
   626 void CMIDDateFieldItem::HandlePointerEventL(const TPointerEvent& aPointerEvent)
       
   627 {
       
   628     if (AknLayoutUtils::PenEnabled() && iForm)
       
   629     {
       
   630         TInt nOldCurrent = iEditor->CurrentField();
       
   631         TBool editorArea = iEditor->Rect().Contains(aPointerEvent.iPosition);
       
   632         TBool longTapArea = Rect().Contains(aPointerEvent.iPosition);
       
   633         TBool longTapDetected = EFalse;
       
   634         // On EButton1Down event check that event is on the allowed long tap area
       
   635         if (aPointerEvent.iType != TPointerEvent::EButton1Down || longTapArea)
       
   636         {
       
   637             longTapDetected = iForm->TryDetectLongTapL(aPointerEvent);
       
   638         }
       
   639 
       
   640         TPointerEvent pEvent = aPointerEvent;
       
   641         //if label area of area right from editor area is tapped the coordinates are transferred
       
   642         //to editor area. VKB is launched if tapped anywhere else than AM/PM field.
       
   643         if (aPointerEvent.iPosition.iY <= iLabelControl->Rect().iBr.iY)
       
   644         {
       
   645             pEvent.iPosition.iY = iEditor->Rect().iTl.iY;
       
   646         }
       
   647         if (aPointerEvent.iPosition.iX > iEditor->Rect().iBr.iX)
       
   648         {
       
   649             pEvent.iPosition.iX = iEditor->Rect().iBr.iX - 1;
       
   650         }
       
   651 
       
   652         // Keep track of the pointer grabbing control
       
   653         if (aPointerEvent.iType == TPointerEvent::EButton1Down)
       
   654         {
       
   655             iGrabbingControl = NULL;
       
   656 
       
   657             const TInt count = CountComponentControls();
       
   658             for (TInt i = (count - 1); i >= 0; --i)
       
   659             {
       
   660                 CCoeControl* ctrl = ComponentControl(i);
       
   661                 if (ctrl->Rect().Contains(pEvent.iPosition))
       
   662                 {
       
   663                     iGrabbingControl = ctrl;
       
   664                 }
       
   665             }
       
   666         }
       
   667 #ifdef RD_JAVA_S60_RELEASE_9_2
       
   668         // In single click UI VKB is opened with first tap.
       
   669         if (!longTapDetected && !iForm->PhysicsScrolling())
       
   670         {
       
   671             if (aPointerEvent.iType == TPointerEvent::EButton1Up
       
   672                     && iGrabbingControl == iEditor
       
   673                     && IsCurrentFieldAmPmField()
       
   674                     && editorArea)
       
   675             {
       
   676                 // Toggle AM/PM value
       
   677                 iEditor->HandleMfneCommandL(
       
   678                     MAknMfneCommandObserver::EMfneIncrementCurrentFieldValue);
       
   679                 ReportEventL(MCoeControlObserver::EEventStateChanged);
       
   680             }
       
   681             else if (iGrabbingControl)
       
   682             {
       
   683                 iGrabbingControl->HandlePointerEventL(aPointerEvent);
       
   684 
       
   685                 // Send PointerDown and PointerUp events again to the editor.
       
   686                 // This enables VKB to open with first tap even if internal
       
   687                 // focus is in another field.
       
   688                 if (aPointerEvent.iType == TPointerEvent::EButton1Up)
       
   689                 {
       
   690                     pEvent.iType = TPointerEvent::EButton1Down;
       
   691                     iGrabbingControl->HandlePointerEventL(pEvent);
       
   692                     pEvent.iType = TPointerEvent::EButton1Up;
       
   693                     iGrabbingControl->HandlePointerEventL(pEvent);
       
   694                 }
       
   695             }
       
   696         }
       
   697         // else do not forward pointer event to CCoeControl as
       
   698         // there is a long tap or physics scrolling going on
       
   699 #else
       
   700         // In non-single click UI,
       
   701         // VKB is opened when tapping already focused item.
       
   702         if (!longTapDetected && !iForm->IsFocusChangingWithPen() && !iForm->PhysicsScrolling())
       
   703         {
       
   704             if (aPointerEvent.iType == TPointerEvent::EButton1Up
       
   705                     && iGrabbingControl == iEditor
       
   706                     && IsCurrentFieldAmPmField()
       
   707                     && editorArea)
       
   708             {
       
   709                 // Toggle AM/PM value
       
   710                 iEditor->HandleMfneCommandL(
       
   711                     MAknMfneCommandObserver::EMfneIncrementCurrentFieldValue);
       
   712                 ReportEventL(MCoeControlObserver::EEventStateChanged);
       
   713             }
       
   714             else if (IsFocused() && iGrabbingControl)
       
   715             {
       
   716                 iGrabbingControl->HandlePointerEventL(aPointerEvent);
       
   717 
       
   718                 // Send PointerDown and PointerUp events again to the editor.
       
   719                 // This enables VKB to open with first tap even if internal
       
   720                 // focus is in another field.
       
   721                 if (aPointerEvent.iType == TPointerEvent::EButton1Up)
       
   722                 {
       
   723                     pEvent.iType = TPointerEvent::EButton1Down;
       
   724                     iGrabbingControl->HandlePointerEventL(pEvent);
       
   725                     pEvent.iType = TPointerEvent::EButton1Up;
       
   726                     iGrabbingControl->HandlePointerEventL(pEvent);
       
   727                 }
       
   728             }
       
   729         }
       
   730         // else do not forward pointer event to CCoeControl as
       
   731         // there is a long tap or physics scrolling going on
       
   732 
       
   733 #endif // RD_JAVA_S60_RELEASE_9_2
       
   734         // msk: notify if the current field has changed
       
   735         if (nOldCurrent != iEditor->CurrentField())
       
   736         {
       
   737             UpdateCommands();
       
   738         }
       
   739 
       
   740         if (aPointerEvent.iType == TPointerEvent::EButton1Up)
       
   741         {
       
   742             iGrabbingControl = NULL;
       
   743         }
       
   744     }
       
   745 }
       
   746 #endif // RD_SCALABLE_UI_V2
       
   747 
       
   748 // msk
       
   749 TBool CMIDDateFieldItem::ProcessCommandL(CMIDCommand* aCommand)
       
   750 {
       
   751     if (aCommand->Id() == iAmPmToggleCommand->Id() && IsCurrentFieldAmPmField())
       
   752     {
       
   753         ToggleAmPmFieldValue();
       
   754     }
       
   755     return ETrue;
       
   756 }
       
   757 
       
   758 // msk
       
   759 /**
       
   760  * Updates the items commands according to the currently focused field. Needs to be
       
   761  * called when ever focused field changes.
       
   762  */
       
   763 void CMIDDateFieldItem::UpdateCommands()
       
   764 {
       
   765     // If the current field is the AM/PM field, provide toggle command
       
   766     SetBuiltInMSKCommand(IsCurrentFieldAmPmField() ? iAmPmToggleCommand : NULL);
       
   767 }
       
   768 
       
   769 // msk
       
   770 TBool CMIDDateFieldItem::IsCurrentFieldAmPmField()
       
   771 {
       
   772     return IsFieldAmPmField(iEditor->CurrentField());
       
   773 }
       
   774 
       
   775 TBool CMIDDateFieldItem::IsFieldAmPmField(TInt fieldIndex)
       
   776 {
       
   777     // current field is am/pm field if it accepts something else than positive integers
       
   778     TBool amPmField =
       
   779         (iInputMode == MMIDDateField::ETime || iInputMode == MMIDDateField::EDateTime)  &&
       
   780         (iEditor->Field(fieldIndex)->InputCapabilities().Capabilities() &
       
   781          ~TCoeInputCapabilities::EWesternNumericIntegerPositive);
       
   782     return amPmField;
       
   783 }
       
   784 
       
   785 void CMIDDateFieldItem::ToggleAmPmFieldValue()
       
   786 {
       
   787     TInt numFields = iEditor->NumFields();
       
   788     for (TInt i = 0; i < numFields; i++)
       
   789     { // find am/pm field
       
   790         if (IsFieldAmPmField(i))
       
   791         { // toggle the field value
       
   792             CEikMfneSymbol* ampmField = static_cast<CEikMfneSymbol*>(iEditor->Field(i));
       
   793             TAmPm ampm = static_cast<TAmPm>(ampmField->IdOfCurrentSymbolicItem());
       
   794             if (ampm == EAm)
       
   795             {
       
   796                 ampm = EPm;
       
   797             }
       
   798             else
       
   799             {
       
   800                 ampm = EAm;
       
   801             }
       
   802             ampmField->SetCurrentSymbolicItemToId(ampm);
       
   803             iEditor->DrawDeferred();
       
   804             break; // assumes that there is only one am/pm field
       
   805         }
       
   806     }
       
   807 }
       
   808 
       
   809 void CMIDDateFieldItem::SetContainerWindowL(const CCoeControl& aContainer)
       
   810 {
       
   811     CMIDControlItem::SetContainerWindowL(aContainer);
       
   812     iEditor->SetContainerWindowL(*this);
       
   813     SetObserver(iForm);
       
   814     ActivateL();
       
   815 }
       
   816 
       
   817 void CMIDDateFieldItem::DoSafeDraw()
       
   818 {
       
   819     if (DrawableWindow() && iForm)
       
   820     {
       
   821         DrawDeferred();
       
   822     }
       
   823 }
       
   824 
       
   825 void CMIDDateFieldItem::FocusChanged(TDrawNow aDrawNow)
       
   826 {
       
   827     TBool focus = IsFocused();
       
   828 
       
   829     iEditor->SetFocus(focus);
       
   830     if (focus)
       
   831     {
       
   832         TRAP_IGNORE(iUIManager->OpenNaviPaneControllerL()->PauseTickerL(
       
   833                         TICKER_PAUSE_INTERVAL, this));
       
   834     }
       
   835     else
       
   836     {
       
   837         TRAP_IGNORE(iUIManager->OpenNaviPaneControllerL()->PauseTickerL(
       
   838                         0, this));
       
   839 #ifdef RD_SCALABLE_UI_V2
       
   840         iGrabbingControl = NULL;
       
   841 #endif
       
   842     }
       
   843 
       
   844     TRAP_IGNORE(UpdateTextColorsL());
       
   845 
       
   846     CMIDControlItem::FocusChanged(aDrawNow);
       
   847 
       
   848     DoSafeDraw();
       
   849 }
       
   850 
       
   851 TCoeInputCapabilities CMIDDateFieldItem::InputCapabilities() const
       
   852 {
       
   853     TCoeInputCapabilities inputCapabilities(TCoeInputCapabilities::ENone, NULL,
       
   854                                             const_cast<CMIDDateFieldItem*>(this));
       
   855     inputCapabilities.MergeWith(CMIDControlItem::InputCapabilities());
       
   856     return inputCapabilities;
       
   857 }
       
   858 
       
   859 TInt CMIDDateFieldItem::ItemPreferredHeightWithoutLabel()
       
   860 {
       
   861     return iMargins.iTop + iEditorHeight + iMargins.iBottom;
       
   862 }
       
   863 
       
   864 /* ResolutionChange
       
   865  *
       
   866  * This method is called after dynamic resolution change
       
   867  */
       
   868 void CMIDDateFieldItem::ResolutionChange(TInt /*aType*/)
       
   869 {
       
   870     UpdateMemberVariables();
       
   871     // Set font from LAF to the editor, so that MinimumSize() will return correct values.
       
   872     // AknLayoutUtils::LayoutMfne() will also set the font from LAF to the editor
       
   873     iEditor->SetFont(AknLayoutUtils::FontFromId(
       
   874                          AknLayoutScalable_Avkon::form2_midp_time_pane_t1().Font()));
       
   875 }
       
   876 
       
   877 void CMIDDateFieldItem::AdjustToSizeL(const TSize& aSize)
       
   878 {
       
   879     UpdateMemberVariables();
       
   880     if (HasLabel())
       
   881     {
       
   882         iLabelControl->AdjustToSizeL(
       
   883             TSize(aSize.iWidth, aSize.iHeight - ItemPreferredHeightWithoutLabel()));
       
   884     }
       
   885 }
       
   886 
       
   887 void CMIDDateFieldItem::Dispose()
       
   888 {
       
   889     delete this;
       
   890 }
       
   891 
       
   892 void CMIDDateFieldItem::AddCommandL(MMIDCommand* aCommand)
       
   893 {
       
   894     CMIDItem::AddCommandL(aCommand);
       
   895 }
       
   896 
       
   897 void CMIDDateFieldItem::RemoveCommand(MMIDCommand* aCommand)
       
   898 {
       
   899     CMIDItem::RemoveCommand(aCommand);
       
   900 }
       
   901 
       
   902 void CMIDDateFieldItem::SetDefaultCommand(MMIDCommand* aCommand)
       
   903 {
       
   904     CMIDItem::SetDefaultCommand(aCommand);
       
   905 }
       
   906 
       
   907 TSize CMIDDateFieldItem::PreferredSize() const
       
   908 {
       
   909     CMIDDateFieldItem* self = const_cast<CMIDDateFieldItem*>(this);
       
   910     CMIDItem* item = static_cast<CMIDItem*>(self);
       
   911     return item->PreferredSize();
       
   912 }
       
   913 
       
   914 TSize CMIDDateFieldItem::MinimumSize() const
       
   915 {
       
   916     CCoeControl* control = const_cast<CMIDDateFieldItem*>(this);
       
   917     return control->MinimumSize();
       
   918 }
       
   919 
       
   920 void CMIDDateFieldItem::SetLayoutL(TLayout aLayout)
       
   921 {
       
   922     CMIDItem::SetLayoutL(aLayout);
       
   923 }
       
   924 
       
   925 TBool CMIDDateFieldItem::IsDateTimeNonEmpty() const
       
   926 {
       
   927     if (iEditor)
       
   928     {
       
   929         TInt count = iEditor->NumFields();
       
   930         for (TInt i = 0; i < count; ++i)
       
   931         {
       
   932             if (!iEditor->Field(i)->IsValid())
       
   933             {
       
   934                 return EFalse;
       
   935             }
       
   936         }
       
   937 
       
   938         return ETrue;
       
   939     }
       
   940     else
       
   941     {
       
   942         return EFalse;
       
   943     }
       
   944 }
       
   945 
       
   946 void CMIDDateFieldItem::UpdateMemberVariables()
       
   947 {
       
   948     TAknWindowLineLayout layout = AknLayoutScalable_Avkon::form2_midp_time_pane().LayoutLine();
       
   949     iMargins.iTop    = layout.it;
       
   950     iMargins.iBottom = ItemContentBottomMargin();
       
   951     iMargins.iLeft   = layout.il;
       
   952     iMargins.iRight  = layout.ir;
       
   953     iEditorHeight    = layout.iH;
       
   954 }
       
   955 
       
   956 // Update right cursor position on Datefiled
       
   957 void CMIDDateFieldItem::CursorUpdate()
       
   958 {
       
   959     if (IsFocused())
       
   960     {
       
   961         iEditor->SetFocus(ETrue);
       
   962     }
       
   963 }
       
   964 
       
   965 void CMIDDateFieldItem::UpdateTextColorsL()
       
   966 {
       
   967     if (iHighlighted)
       
   968     {
       
   969         // Text colour from skin - highlighted
       
   970         iEditor->SetSkinTextColorL(EAknsCIQsnTextColorsCG8);
       
   971     }
       
   972     else
       
   973     {
       
   974         // Text colour from skin - unfocused
       
   975         iEditor->SetSkinTextColorL(EAknsCIQsnTextColorsCG6);
       
   976     }
       
   977 }
       
   978 
       
   979 // End of File