javauis/lcdui_akn/lcdui/src/CMIDControlItem.cpp
branchRCL_3
changeset 19 04becd199f91
child 34 71c436fe3ce0
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2003-2006 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 <coemain.h>
       
    20 #include <eikcapc.h>
       
    21 
       
    22 #include "CMIDControlItem.h"
       
    23 #include "CMIDGc.h"
       
    24 #include "CMIDUtils.h"
       
    25 #include "CMIDCommand.h"
       
    26 #include "CMIDForm.h"
       
    27 #include "CMIDCommandList.h"
       
    28 // needed API for iLabelControl
       
    29 #include "CMIDItemLabel.h"
       
    30 // API for retrieving background control context
       
    31 #include "CMIDDisplayable.h"
       
    32 #include "CMIDMenuHandler.h"
       
    33 
       
    34 #include <AknUtils.h>
       
    35 #include <AknsDrawUtils.h>
       
    36 // using API for CAknsFrameBackgroundControlContext (member iHighlightedBackgroundCc)
       
    37 #include <AknsFrameBackgroundControlContext.h>
       
    38 // LAF
       
    39 // AknLayoutScalable_Avkon::form2_midp_content_pane
       
    40 #include <aknlayoutscalable_avkon.cdl.h>
       
    41 
       
    42 #include <j2me/jdebug.h>
       
    43 
       
    44 #undef  TRAP_INSTRUMENTATION_LEAVE
       
    45 #define TRAP_INSTRUMENTATION_LEAVE(aResult) DEBUG_INT2("In CMIDControlItem.cpp, trapped method was called at line %D and got exception %D", __LINE__, aResult);
       
    46 
       
    47 /** This constant limits the maximum number of lines in an item label */
       
    48 const TInt KMaxNumLines  = 10;
       
    49 
       
    50 ///////////////////////////////////////////////////////////////////////////////////////////////////
       
    51 // class CMIDControlItem
       
    52 ///////////////////////////////////////////////////////////////////////////////////////////////////
       
    53 
       
    54 CMIDControlItem::CMIDControlItem(MMIDItem::TLayout aLayout, CMIDUIManager* aUIManager)
       
    55         : CMIDItem(aLayout, aUIManager),
       
    56         iCoeEnv(CCoeEnv::Static())
       
    57 {
       
    58 }
       
    59 
       
    60 CMIDControlItem::~CMIDControlItem()
       
    61 {
       
    62     delete iLabelControl;
       
    63     if (iForm)
       
    64     {
       
    65         iForm->RemoveDeletedItem(*this);
       
    66     }
       
    67 }
       
    68 
       
    69 void CMIDControlItem::ConstructL()
       
    70 {
       
    71     CMIDItem::ConstructL();
       
    72 
       
    73     iLabelControl = CMIDItemLabel::NewL(FormClientAreaWidth(),
       
    74                                         Type() != MMIDComponent::EImageItem,
       
    75                                         KMaxNumLines);
       
    76 
       
    77     if (iLabel)
       
    78     {
       
    79         iLabelControl->SetTextL(*iLabel);
       
    80     }
       
    81 }
       
    82 
       
    83 TInt CMIDControlItem::FormClientAreaWidth() const
       
    84 {
       
    85     return iForm ? iForm->Width(): CMIDForm::FormWidth();
       
    86 }
       
    87 
       
    88 TRect CMIDControlItem::FormClientAreaRect() const
       
    89 {
       
    90     return iForm ? iForm->FormRect(): CMIDForm::StaticFormRect();
       
    91 }
       
    92 
       
    93 // Returns bottom margin of form2_midp_content_pane
       
    94 TInt CMIDControlItem::ItemContentBottomMargin() const
       
    95 {
       
    96 
       
    97     TAknLayoutRect form2MidpContentPaneRect;
       
    98     TRect formRect;
       
    99     if (iForm)
       
   100     {
       
   101         formRect = iForm->FormRect();
       
   102     }
       
   103     else
       
   104     {
       
   105         formRect = CMIDForm::StaticFormRect();
       
   106     }
       
   107     form2MidpContentPaneRect.LayoutRect(formRect,
       
   108                                         AknLayoutScalable_Avkon::form2_midp_content_pane(0).LayoutLine());
       
   109     return formRect.iBr.iY - form2MidpContentPaneRect.Rect().iBr.iY;
       
   110 }
       
   111 
       
   112 /**
       
   113  * Resets the preferred size if the minimum size has changed.
       
   114  * The minimum size will change if the label or content is modified.
       
   115  */
       
   116 TSize CMIDControlItem::ResetPreferredSize() const
       
   117 {
       
   118     CMIDControlItem* self = const_cast<CMIDControlItem*>(this);
       
   119     self->SetPreferredSize(iRequestedPreferredSize,self->MinimumSize());
       
   120     return iPreferredSize;
       
   121 }
       
   122 
       
   123 /** This method is called by the form row when sizing the items
       
   124 once it has determined the width available to the item. The item
       
   125 will typically wrap its label to this width so that its height
       
   126 will then be modified accordingly. See CMIDFormRow::SizeItemsL().
       
   127 
       
   128 Items that have other width dependent behavious may want to extend
       
   129 this method. For example string items may have to wrap their content
       
   130 as well as the label to the given width.
       
   131 */
       
   132 void CMIDControlItem::AdjustToNewWidthL(TInt aWidth)
       
   133 {
       
   134     ASSERT(aWidth >= 0);
       
   135 
       
   136     if (iLabelControl && iLabelControl->Text()->Length() > 0)
       
   137     {
       
   138 
       
   139         iLabelControl->SetWidthL(aWidth);
       
   140 
       
   141         if (iRequestedPreferredSize.iHeight < 0)
       
   142         {
       
   143             iPreferredSize.iHeight = LabelHeight() + ItemPreferredHeightWithoutLabel();
       
   144         }
       
   145     }
       
   146 }
       
   147 
       
   148 /** After having called AdjustToNewWidthL() and done its calculations, the form
       
   149 row knows what size should be assigned to items and calls this method after having
       
   150 called SetSizeQuiet. The elements inside an item should made to fit this size.
       
   151 Typically this size is the same as the requested size (the width passed to  AdjustToNewWidthL()
       
   152 plus the resulting height. However if layout shrink applies, this may well be the minimum size
       
   153 instead. This method is equally called by some items after having calculated their preferred
       
   154 size. See SetPreferredSizeL().
       
   155 
       
   156 Items that do not implement this method (text field, date field, choice group and gauge)
       
   157 are such that their minimum size can always fit the entire label and any other element. This
       
   158 is typical of items that fill the entire form width, basically they always get the size they
       
   159 want in this case (the entire width plus as much height as needed).
       
   160 
       
   161 Finally, we do not provide a common implementation that adjusts the label because we
       
   162 do not know how much space of the given size is reserved to the label. */
       
   163 void CMIDControlItem::AdjustToSizeL(const TSize& /*aSize*/)
       
   164 {
       
   165 
       
   166 }
       
   167 
       
   168 TRect CMIDControlItem::FocusableRect()
       
   169 {
       
   170     return Rect();
       
   171 }
       
   172 
       
   173 TBool CMIDControlItem::IsSelectable() const
       
   174 {
       
   175     return (!IsNonFocusing() || (CommandList() ? CommandList()->Count() > 0 : 0));
       
   176 }
       
   177 
       
   178 void CMIDControlItem::SetLabelL(const TDesC& aLabel)
       
   179 {
       
   180     CMIDItem::SetLabelL(aLabel);
       
   181     if (iLabelControl && iLabel)
       
   182     {
       
   183         iLabelControl->SetTextL(*iLabel);
       
   184     }
       
   185 }
       
   186 
       
   187 void CMIDControlItem::SetContainerWindowL(const CCoeControl& aContainer)
       
   188 {
       
   189     CCoeControl::SetContainerWindowL(aContainer);
       
   190     if (iLabelControl)
       
   191     {
       
   192         iLabelControl->SetContainerWindowL(*this);
       
   193     }
       
   194 }
       
   195 
       
   196 void CMIDControlItem::SetFocus(TBool aFocus, TDrawNow aDrawNow)
       
   197 {
       
   198     CCoeControl::SetFocus(aFocus, aDrawNow);
       
   199 }
       
   200 
       
   201 void CMIDControlItem::FocusChanged(TDrawNow aDrawNow)
       
   202 {
       
   203     if (!iForm)
       
   204     {
       
   205         return;
       
   206     }
       
   207 
       
   208     SetLabelColor(iLabelControl);
       
   209 
       
   210     if (aDrawNow && DrawableWindow())
       
   211     {
       
   212         iLabelControl->DrawNow();
       
   213     }
       
   214 }
       
   215 
       
   216 TTypeUid::Ptr CMIDControlItem::MopSupplyObject(TTypeUid aId)
       
   217 {
       
   218     if (aId.iUid == MAknsControlContext::ETypeId && iForm)
       
   219     {
       
   220         return IsFocused() ?
       
   221                MAknsControlContext::SupplyMopObject(aId, iForm->GetHighlightBackgroundCc()) :
       
   222                MAknsControlContext::SupplyMopObject(aId,
       
   223                                                     iForm->CurrentDisplayable().BackGroundControlContext());
       
   224     }
       
   225     return SupplyMopObject(aId, iMenuHandler->Cba(), iMenuHandler->MenuBar());
       
   226 }
       
   227 
       
   228 void CMIDControlItem::GetCaptionForFep(TDes& aCaption) const
       
   229 {
       
   230     const TDesC* labelText = iLabelControl->Text();
       
   231     if (labelText)
       
   232     {
       
   233         const TInt maximumLength = aCaption.MaxLength();
       
   234         if (labelText->Length() > maximumLength)
       
   235         {
       
   236             aCaption = labelText->Left(maximumLength);
       
   237         }
       
   238         else
       
   239         {
       
   240             aCaption = *labelText;
       
   241         }
       
   242     }
       
   243     else
       
   244     {
       
   245         aCaption = KNullDesC;
       
   246     }
       
   247 }
       
   248 
       
   249 void CMIDControlItem::SetLabelColor(CMIDItemLabel* aLabelControl)
       
   250 {
       
   251     TRgb color;
       
   252 
       
   253     if ((AknsUtils::GetCachedColor(AknsUtils::SkinInstance(), color,
       
   254                                    KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG8) == KErrNone) && iLabelControl)
       
   255     {//data entry text color
       
   256         TRAP_IGNORE(aLabelControl->SetColorL(color));
       
   257     }
       
   258 }
       
   259 
       
   260 TInt CMIDControlItem::LabelHeight() const
       
   261 {
       
   262     if (iLabelControl && iLabelControl->Text()->Length())
       
   263     {
       
   264         return iLabelControl->Size().iHeight;
       
   265     }
       
   266 
       
   267     return 0;
       
   268 }
       
   269 
       
   270 /** Returns height of a label that has only one line of text */
       
   271 TInt CMIDControlItem::OneLineLabelHeight() const
       
   272 {
       
   273     return iLabelControl->LineHeight() + iLabelControl->ItemLabelMargin();
       
   274 }
       
   275 
       
   276 void CMIDControlItem::ResolutionChange(TInt /*aType*/)
       
   277 {
       
   278 
       
   279 }
       
   280 
       
   281 void CMIDControlItem::ColorChange(TInt /*aType*/)
       
   282 {
       
   283 
       
   284 }
       
   285 
       
   286 TBool CMIDControlItem::HasLabel()
       
   287 {
       
   288     return iLabelControl && (iLabelControl->Text()->Length() > 0);
       
   289 }
       
   290 
       
   291 void CMIDControlItem::PostFocusTransferEvent(TBool /*aFocus*/, CMIDForm::TDirection /*aDirection*/)
       
   292 {
       
   293     // override the default implementation if needed
       
   294 }
       
   295 
       
   296 void CMIDControlItem::RestoreInnerFocus()
       
   297 {
       
   298     // override the default implementation if needed
       
   299 }
       
   300 
       
   301 void CMIDControlItem::HandleWindowFade(TBool /*aFaded*/)
       
   302 {
       
   303     // override the default implementation if needed
       
   304 }