Nokia N97 SDK
Example Applications Guide

styluspopupmenuexcontainer.cpp

00001 /**
00002  * Copyright (c) 2007 Nokia Corporation.
00003  * This material, including documentation and any related
00004  * computer programs, is protected by
00005  * copyright controlled by Nokia Corporation.
00006  */
00007 
00008 #include <coemain.h>
00009 #include <badesca.h> 
00010 #include <eiklabel.h> 
00011 #include <aknbutton.h>
00012 #include <barsread.h>
00013 #include <styluspopupmenuexample.rsg>
00014 //#include <styluspopupmenuexample.mbg>
00015 #include <aknsdrawutils.h>
00016 #include <aknsbasicbackgroundcontrolcontext.h>
00017 #include <stringloader.h>
00018 
00019 #include "styluspopupmenuexcontainer.h"
00020 #include "styluspopupmenuex.hrh"
00021 
00022 // ============================== CONSTANTS ==================================
00023 
00024 _LIT(KMenuItem1, "Item 1");
00025 _LIT(KMenuItem2, "Item 2");
00026 _LIT(KMenuItem3, "Item 3");
00027 _LIT(KMenuItem4, "Item 4");
00028 
00029 // =========================== MEMBER FUNCTIONS ==============================
00030 
00031 // ---------------------------------------------------------------------------
00032 // C++ default Constructor
00033 // ---------------------------------------------------------------------------
00034 //
00035 CStylusPopupMenuExContainer::CStylusPopupMenuExContainer() : iUseResource( EFalse )
00036     {
00037     }
00038 
00039 // ---------------------------------------------------------------------------
00040 // Destructor
00041 // ---------------------------------------------------------------------------
00042 //
00043 CStylusPopupMenuExContainer::~CStylusPopupMenuExContainer()
00044     {
00045 
00046     delete iLabel;
00047     delete iBgContext;
00048     delete iPopup;
00049     }
00050 
00051 // ---------------------------------------------------------------------------
00052 // Symbian 2nd Constructor
00053 // ---------------------------------------------------------------------------
00054 //
00055 void CStylusPopupMenuExContainer::ConstructL( const TRect& aRect )
00056     {
00057     CreateWindowL();
00058     SetRect( aRect );
00059 
00060     // Label for showing messages    
00061     iLabel = new ( ELeave ) CEikLabel;
00062     iLabel->SetContainerWindowL( *this );
00063     iLabel->SetLabelAlignment( ELayoutAlignCenter );
00064     HBufC* appName = StringLoader::LoadL( R_STYLUSPOPUPMENUEX_APPNAME );  
00065     iLabel->SetTextL( *appName );
00066     delete appName;
00067     
00068     // Background context for skinned background
00069     iBgContext = CAknsBasicBackgroundControlContext::NewL( 
00070         KAknsIIDSkinBmpMainPaneUsual, TRect( 0, 0, 0, 0 ), ETrue 
00071     );
00072     iBgContext->SetRect( Rect() );
00073     iBgContext->SetParentPos( PositionRelativeToScreen() );
00074     
00075     SizeChanged();
00076     ActivateL();
00077     }
00078 
00079 // ---------------------------------------------------------------------------
00080 // CStylusPopupMenuExContainer::Draw
00081 // ---------------------------------------------------------------------------
00082 //
00083 void CStylusPopupMenuExContainer::Draw( const TRect& aRect ) const
00084     {
00085     // Draw background
00086     CWindowGc& gc = SystemGc();
00087                 
00088     MAknsSkinInstance* skin = AknsUtils::SkinInstance();
00089     if ( !AknsDrawUtils::Background( skin, iBgContext, gc, aRect ) )
00090         {
00091         SystemGc().Clear( aRect );
00092         }
00093     
00094     TRgb textColor = KRgbBlack;
00095     AknsUtils::GetCachedColor( AknsUtils::SkinInstance(), textColor,
00096         KAknsIIDQsnTextColors, EAknsCIQsnTextColorsCG55 );
00097     AknLayoutUtils::OverrideControlColorL( *iLabel, EColorLabelText, textColor );
00098     }
00099 
00100 // ---------------------------------------------------------------------------
00101 // CStylusPopupMenuExContainer::CountComponentControls
00102 // ---------------------------------------------------------------------------
00103 //
00104 TInt CStylusPopupMenuExContainer::CountComponentControls() const
00105     {
00106     TInt count ( 1 ); // iLabel
00107     return count;
00108     }
00109 
00110 // ---------------------------------------------------------------------------
00111 // CStylusPopupMenuExContainer::ComponentControl
00112 // ---------------------------------------------------------------------------
00113 //
00114 CCoeControl* CStylusPopupMenuExContainer::ComponentControl( TInt aIndex ) const
00115     {
00116     if ( aIndex == 0 )
00117         {
00118         return iLabel;
00119         }
00120     
00121     return NULL;
00122     }
00123 
00124 void CStylusPopupMenuExContainer::SizeChanged()
00125     {
00126     if( iLabel )
00127         {
00128         iLabel->SetRect( TRect( TPoint(Rect().iTl.iX, Rect().iTl.iY + 35), TPoint(Rect().iBr.iX, Rect().iTl.iY + 75  ) ) );
00129         }
00130     DrawDeferred();
00131     }
00132     
00133 void CStylusPopupMenuExContainer::HandlePointerEventL(const TPointerEvent &aPointerEvent)
00134     {
00135     CCoeControl::HandlePointerEventL( aPointerEvent );
00136     if( aPointerEvent.iType ==TPointerEvent::EButton1Up )
00137         {
00138         if( iUseResource )
00139             {
00140             CreatePopupMenuFromResourceL( aPointerEvent.iPosition );
00141             }
00142         else
00143             {
00144             CreatePopupMenuL( aPointerEvent.iPosition );  
00145             }
00146         }
00147     
00148     }
00149 
00150 void CStylusPopupMenuExContainer::HandleResourceChange( TInt aType )
00151     {
00152     CCoeControl::HandleResourceChange( aType );
00153     if  ( aType == KEikDynamicLayoutVariantSwitch )
00154         {
00155         TRect rect;
00156         AknLayoutUtils::LayoutMetricsRect(AknLayoutUtils::EMainPane, rect);
00157         SetRect(rect);
00158         
00159         iBgContext->SetRect( Rect() );
00160         iBgContext->SetParentPos( PositionRelativeToScreen() );
00161         }
00162     }
00163 
00164 void CStylusPopupMenuExContainer::CreatePopupMenuFromResourceL( const TPoint &aPosition )
00165     {
00166     delete iPopup;
00167     
00168     iPopup = CAknStylusPopUpMenu::NewL( this, aPosition );
00169     TResourceReader reader;
00170     iCoeEnv->CreateResourceReaderLC( reader, R_STYLUSPOPUPMENUEX_STYLUSPOPUPMENU );
00171     iPopup->ConstructFromResourceL( reader );
00172     CleanupStack::PopAndDestroy();
00173     
00174     HBufC* labelText = StringLoader::LoadLC( R_STYLUSPOPUPMENUEX_MENU_CREATED );
00175     iLabel->SetTextL( *labelText );
00176     CleanupStack::PopAndDestroy();
00177     DrawDeferred();
00178     
00179     iPopup->ShowMenu();
00180     }
00181 
00182 void CStylusPopupMenuExContainer::UseResourceForPopupMenuL( TBool aValue )
00183     {
00184     iUseResource = aValue;
00185     
00186     HBufC* labelText = StringLoader::LoadLC( R_STYLUSPOPUPMENUEX_SETTING_CHANGED );
00187     iLabel->SetTextL( *labelText );
00188     CleanupStack::PopAndDestroy();
00189     DrawDeferred();
00190     }
00191 void CStylusPopupMenuExContainer::CreatePopupMenuL( const TPoint &aPosition )
00192     {
00193     delete iPopup;
00194     
00195     iPopup = CAknStylusPopUpMenu::NewL( this, aPosition );
00196     iPopup->AddMenuItemL( KMenuItem1, EMenuItem1 );
00197     iPopup->AddMenuItemL( KMenuItem2, EMenuItem2 );
00198     iPopup->AddMenuItemL( KMenuItem3, EMenuItem3 );
00199     iPopup->AddMenuItemL( KMenuItem4, EMenuItem4 );
00200     //iPopup->SetPosition( aPosition );
00201     
00202     HBufC* labelText = StringLoader::LoadLC( R_STYLUSPOPUPMENUEX_MENU_CREATED );
00203     iLabel->SetTextL( *labelText );
00204     CleanupStack::PopAndDestroy();
00205     DrawDeferred();
00206     
00207     iPopup->ShowMenu();
00208     }
00209 
00210 void CStylusPopupMenuExContainer::ProcessCommandL( TInt aCommandId )
00211     {
00212         
00213     TBuf<32> buf;
00214     HBufC* labelText;
00215     switch( aCommandId )
00216         {
00217         case EMenuItem1:
00218             labelText = StringLoader::LoadLC( R_STYLUSPOPUPMENUEX_ITEM_SELECTED );
00219             buf.Format( *labelText, 1 );
00220             CleanupStack::PopAndDestroy(); // labelText
00221             iLabel->SetTextL( buf );
00222             break;
00223         case EMenuItem2:
00224             labelText = StringLoader::LoadLC( R_STYLUSPOPUPMENUEX_ITEM_SELECTED );
00225             buf.Format( *labelText, 2 );
00226             CleanupStack::PopAndDestroy(); // labelText
00227             iLabel->SetTextL( buf );
00228             break;
00229         case EMenuItem3:
00230             labelText = StringLoader::LoadLC( R_STYLUSPOPUPMENUEX_ITEM_SELECTED );
00231             buf.Format( *labelText, 3 );
00232             CleanupStack::PopAndDestroy(); // labelText
00233             iLabel->SetTextL( buf );
00234             break;
00235         case EMenuItem4:
00236             labelText = StringLoader::LoadLC( R_STYLUSPOPUPMENUEX_ITEM_SELECTED );
00237             buf.Format( *labelText, 4 );
00238             CleanupStack::PopAndDestroy(); // labelText
00239             iLabel->SetTextL( buf );
00240             break;
00241         case KErrCancel:
00242             labelText = StringLoader::LoadLC( R_STYLUSPOPUPMENUEX_POPUPMENU_CANCELLED );
00243             iLabel->SetTextL( *labelText );
00244             CleanupStack::PopAndDestroy(); // labelText
00245             break;
00246         }
00247     
00248     DrawDeferred();
00249     }
00250     
00251 void CStylusPopupMenuExContainer::UpdateMarkableMenus( CEikMenuPane *aMenuPane )
00252     {
00253         aMenuPane->SetItemButtonState( ECmdCreateMenuFromCode, iUseResource ? EEikMenuItemSymbolIndeterminate:EEikMenuItemSymbolOn );
00254         aMenuPane->SetItemButtonState( ECmdCreateMenuFromResource, iUseResource ? EEikMenuItemSymbolOn:EEikMenuItemSymbolIndeterminate );
00255     }

© Nokia 2009

Back to top