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