commonuis/CommonUi/src/ItemFinder.cpp
branchRCL_3
changeset 15 08e69e956a8c
parent 9 aabf2c525e0f
child 23 3d340a0166ff
equal deleted inserted replaced
10:9f56a4e1b8ab 15:08e69e956a8c
    19 
    19 
    20 
    20 
    21 // INCLUDES
    21 // INCLUDES
    22 #include <eikrted.h>
    22 #include <eikrted.h>
    23 #include <txtrich.h> // for CRichText
    23 #include <txtrich.h> // for CRichText
    24 #include "ItemFinder.h"
    24 #include <ItemFinder.h>
       
    25 #include <itemfinderobserver.h>
       
    26 #include <aknphysicscrkeys.h>
    25 #include <finditemengine.h>
    27 #include <finditemengine.h>
    26 #include <commonphoneparser.h>
    28 #include <commonphoneparser.h>
    27 #include <txtfrmat.h>
    29 #include <txtfrmat.h>
    28 #include <AknUtils.h> // for AknUtils
    30 #include <AknUtils.h> // for AknUtils
    29 #include <AknsUtils.h>
    31 #include <AknsUtils.h>
    30 
    32 
    31 // scheme recog
    33 // scheme recog
    32 #include <ecom/ecom.h>
    34 #include <ecom/ecom.h>
    33 #include <ecom/implementationinformation.h>
    35 #include <ecom/implementationinformation.h>
       
    36 #include <coemain.h>
    34 
    37 
    35 #include <centralrepository.h>
    38 #include <centralrepository.h>
    36 #include <CommonUiInternalCRKeys.h>
    39 #include <CommonUiInternalCRKeys.h>
    37 
    40 
    38 // CONSTANTS
    41 // CONSTANTS
    39 const TInt KMINTAGLENGTH(5);
    42 const TInt KMINTAGLENGTH(5);
       
    43 const TInt KDragTresholdDefault(20);
    40 _LIT( KHTTPPREFIX, "http://");
    44 _LIT( KHTTPPREFIX, "http://");
    41 _LIT( RTSP, "rtsp");
    45 _LIT( RTSP, "rtsp");
    42 _LIT( KTELPREFIX, "tel:");
    46 _LIT( KTELPREFIX, "tel:");
    43 
    47 
    44 const TInt KItemFinderMinDigitsToFind = 5;
    48 const TInt KItemFinderMinDigitsToFind = 5;
   194         }    
   198         }    
   195     if ( iSchemeResolver )
   199     if ( iSchemeResolver )
   196         {
   200         {
   197         delete iSchemeResolver;
   201         delete iSchemeResolver;
   198         }    
   202         }    
   199     }
   203     if ( iItemFinderObserver && iCoeEnv ) // if still listening to events
   200 
   204         {
   201 CItemFinder::CItemFinder( TInt aFindFlags ): iFlags( aFindFlags )
   205         iCoeEnv->RemoveMessageMonitorObserver( *this );
       
   206         }
       
   207     }
       
   208 
       
   209 CItemFinder::CItemFinder( TInt aFindFlags ): iFlags( aFindFlags ),
       
   210     iCoeEnv( CCoeEnv::Static() )
   202     {}
   211     {}
   203 
   212 
   204 void CItemFinder::ConstructL()
   213 void CItemFinder::ConstructL()
   205     {
   214     {
   206     iMinDigitsToFind = KItemFinderMinDigitsToFind; // default
   215     iMinDigitsToFind = KItemFinderMinDigitsToFind; // default
   238     TBool allowScroll = ETrue;
   247     TBool allowScroll = ETrue;
   239     if ( !iEditor || !(*iEditor)  )
   248     if ( !iEditor || !(*iEditor)  )
   240         {
   249         {
   241         return EFalse;
   250         return EFalse;
   242         }
   251         }
       
   252     // if a key was not pressed, do not move highlight to next item. 
       
   253     if ( iItemFinderObserver && !iAllowHighlight )
       
   254         {
       
   255         return EFalse;
       
   256         }
       
   257     
   243     if ( aDirection == EInit || aDirection == EInitUp || aDirection == EInitDown )
   258     if ( aDirection == EInit || aDirection == EInitUp || aDirection == EInitDown )
   244         {
   259         {
   245         if ( aDirection == EInitUp )
   260         if ( aDirection == EInitUp )
   246             {
   261             {
   247             iPreviousDirection = ENextUp;
   262             iPreviousDirection = ENextUp;
   463     }
   478     }
   464 
   479 
   465 EXPORT_C void CItemFinder::SetEditor( CEikRichTextEditor** aEditor )
   480 EXPORT_C void CItemFinder::SetEditor( CEikRichTextEditor** aEditor )
   466     {
   481     {
   467     iEditor=aEditor;
   482     iEditor=aEditor;
       
   483     SetWindowControl( *aEditor ); 
   468     if ( aEditor && *aEditor && (*aEditor)->TextLength() )
   484     if ( aEditor && *aEditor && (*aEditor)->TextLength() )
   469         {
   485         {
   470         TRAP_IGNORE( (*aEditor)->SetHighlightStyleL( EEikEdwinHighlightLink ) );
   486         TRAP_IGNORE( (*aEditor)->SetHighlightStyleL( EEikEdwinHighlightLink ) );
   471         if ( iAsyncParser )
   487         if ( iAsyncParser )
   472             {
   488             {
   903 EXPORT_C TBool CItemFinder::ItemWasTappedL( const TPoint aTappedPoint )
   919 EXPORT_C TBool CItemFinder::ItemWasTappedL( const TPoint aTappedPoint )
   904     {
   920     {
   905     if ( !iAsyncParser || iAsyncParser->IsActive() )
   921     if ( !iAsyncParser || iAsyncParser->IsActive() )
   906         {
   922         {
   907         return EFalse;
   923         return EFalse;
       
   924         }
       
   925     if ( iItemFinderObserver && !iAllowHighlight )
       
   926         {
       
   927         // if observer is given, return EFalse here 
       
   928         return EFalse; 
   908         }
   929         }
   909     TInt pos = 0;
   930     TInt pos = 0;
   910     TInt len = (*iEditor)->TextLayout()->PosRangeInBand( pos );
   931     TInt len = (*iEditor)->TextLayout()->PosRangeInBand( pos );
   911     TRect rect;
   932     TRect rect;
   912     TInt i = 0;
   933     TInt i = 0;
  1109             return KNullDesC();
  1130             return KNullDesC();
  1110             }
  1131             }
  1111         }
  1132         }
  1112     }
  1133     }
  1113 
  1134 
       
  1135 // -----------------------------------------------------------------------------
       
  1136 // CItemFinder::SetItemFinderObserverL()
       
  1137 // -----------------------------------------------------------------------------
       
  1138 //
       
  1139 EXPORT_C void CItemFinder::SetItemFinderObserverL(
       
  1140         MAknItemFinderObserver* aObserver )
       
  1141     {
       
  1142     if ( !iItemFinderObserver && aObserver ) // client registers
       
  1143         {
       
  1144         if ( iCoeEnv )
       
  1145             {
       
  1146             iCoeEnv->AddMessageMonitorObserverL( *this );
       
  1147             }
       
  1148         }
       
  1149     else if ( iItemFinderObserver && !aObserver ) // client unregisters
       
  1150         {
       
  1151         if ( iCoeEnv )
       
  1152             {
       
  1153             iCoeEnv->RemoveMessageMonitorObserver( *this );
       
  1154             }
       
  1155         }
       
  1156     
       
  1157     iItemFinderObserver = aObserver;
       
  1158     }
       
  1159 
       
  1160 // -----------------------------------------------------------------------------
       
  1161 // CItemFinder::MonitorWsMessage
       
  1162 // -----------------------------------------------------------------------------
       
  1163 //
       
  1164 void CItemFinder::MonitorWsMessage( const TWsEvent& aEvent )
       
  1165     {
       
  1166     if ( !iItemFinderObserver )
       
  1167         {
       
  1168         // if no observer given, nothing is done here.
       
  1169         return; 
       
  1170         }
       
  1171     if ( !iEditor )
       
  1172         {
       
  1173         return;
       
  1174         }
       
  1175     if ( EEventPointer == aEvent.Type() )
       
  1176         {
       
  1177         CCoeControl* targetControl = 
       
  1178             reinterpret_cast<CCoeControl*>( aEvent.Handle() );
       
  1179         if ( targetControl )
       
  1180             {
       
  1181             TRAP_IGNORE ( MonitorPointerEventL( 
       
  1182                 *aEvent.Pointer(), targetControl ) );
       
  1183             }
       
  1184         }
       
  1185     else if ( aEvent.Type() == EEventKeyDown )
       
  1186         {
       
  1187         iAllowHighlight = ETrue; 
       
  1188         
       
  1189         // if an item is selected and select was pressed, send key event
       
  1190         TKeyEvent* keyEvent = aEvent.Key();
       
  1191         
       
  1192         if ( keyEvent && ( EStdKeyDevice3 == keyEvent->iScanCode
       
  1193                 || EStdKeyEnter == keyEvent->iScanCode
       
  1194                 || EStdKeyNkpEnter == keyEvent->iScanCode )
       
  1195                 && iItemFinderObserver
       
  1196                 && ( KNullDesC().Compare ( CurrentSelection() ) ) )
       
  1197             {
       
  1198             // calling observer
       
  1199             TRAP_IGNORE( iItemFinderObserver->HandleFindItemEventL(
       
  1200                     *iCurrentItemExt, MAknItemFinderObserver::EKeyEvent ) );
       
  1201             }
       
  1202         }
       
  1203     else if ( aEvent.Type() == EEventKeyUp )
       
  1204         {
       
  1205         iAllowHighlight = EFalse; 
       
  1206         }
       
  1207     }
       
  1208 
       
  1209 // -----------------------------------------------------------------------------
       
  1210 // CItemFinder::RefreshEditor
       
  1211 // -----------------------------------------------------------------------------
       
  1212 //
  1114 TInt CItemFinder::RefreshEditor()
  1213 TInt CItemFinder::RefreshEditor()
  1115     {
  1214     {
  1116     TInt error = KErrNone;
  1215     TInt error = KErrNone;
  1117     TRAP( error, // Non-leaving method.
  1216     TRAP( error, // Non-leaving method.
  1118         {
  1217         {
  1139             }
  1238             }
  1140         } ); // TRAP
  1239         } ); // TRAP
  1141     return error;
  1240     return error;
  1142     }
  1241     }
  1143 
  1242 
       
  1243 // ---------------------------------------------------------------------------
       
  1244 // CItemFinder::SetWindowControl
       
  1245 // ---------------------------------------------------------------------------
       
  1246 //
       
  1247 void CItemFinder::SetWindowControl( CCoeControl* aControl )
       
  1248     {
       
  1249     if ( aControl )
       
  1250         {
       
  1251         CCoeControl* windowControl( aControl );
       
  1252         while ( windowControl && !windowControl->OwnsWindow() )
       
  1253             {
       
  1254             if ( windowControl == windowControl->Parent() ) 
       
  1255                 {
       
  1256                 return; 
       
  1257                 }
       
  1258             windowControl = windowControl->Parent();
       
  1259             }
       
  1260         if ( windowControl ) 
       
  1261             {
       
  1262             iWindowControl = windowControl;
       
  1263             }
       
  1264         }
       
  1265     }
       
  1266 
       
  1267 // --------------------------------------------------------------------------
       
  1268 // CItemFinder::MonitorPointerEventL
       
  1269 // --------------------------------------------------------------------------
       
  1270 //
       
  1271 void CItemFinder::MonitorPointerEventL( TPointerEvent &aPointerEvent, 
       
  1272         CCoeControl* aTargetControl )
       
  1273     {
       
  1274     if ( !iWindowControl )
       
  1275         {
       
  1276         SetWindowControl( *iEditor ); 
       
  1277         }
       
  1278     if ( aTargetControl == iWindowControl )
       
  1279         {
       
  1280         TPoint tapPoint ( aPointerEvent.iPosition );
       
  1281         ModifyPointerEvent( tapPoint );
       
  1282         if ( aPointerEvent.iType == TPointerEvent::EButton1Down )
       
  1283             {
       
  1284             iAllowHighlight = ETrue; 
       
  1285             TBool tapped ( EFalse );
       
  1286             // Trapping because iAllowHighlight needs to be set to value EFalse
       
  1287             // also if ItemWasTappedL leaves
       
  1288             TRAP_IGNORE( tapped = ItemWasTappedL( tapPoint ) );
       
  1289             iAllowHighlight = EFalse;
       
  1290             // if no item was tapped remove selection
       
  1291             if ( tapped )
       
  1292                 {
       
  1293                 iTapPoint = tapPoint;
       
  1294                 }
       
  1295             else
       
  1296                 {
       
  1297                 (*iEditor)->ClearSelectionL();
       
  1298                 ResetCurrentItem();
       
  1299                 }
       
  1300             }
       
  1301         else if ( aPointerEvent.iType == TPointerEvent::EDrag )
       
  1302             {
       
  1303             if ( iCurrentItemExt->iItemType != ENoneSelected )
       
  1304                 { 
       
  1305                 if ( Abs( iTapPoint.iX - tapPoint.iX ) > DragThresholdL() ||
       
  1306                     Abs( iTapPoint.iY - tapPoint.iY ) > DragThresholdL() )
       
  1307                     {
       
  1308                     (*iEditor)->ClearSelectionL();
       
  1309                     ResetCurrentItem();
       
  1310                     }
       
  1311                 }
       
  1312             }
       
  1313         else if ( aPointerEvent.iType == TPointerEvent::EButton1Up )
       
  1314             {
       
  1315             if ( iCurrentItemExt->iItemType != ENoneSelected )
       
  1316                 { 
       
  1317                 if ( iItemFinderObserver )
       
  1318                     {
       
  1319                     // calling observer
       
  1320                     iItemFinderObserver->HandleFindItemEventL(
       
  1321                             *iCurrentItemExt,
       
  1322                             MAknItemFinderObserver::EPointerEvent );
       
  1323                     }
       
  1324                 // remove selection with button up event
       
  1325                 (*iEditor)->ClearSelectionL();
       
  1326                 if ( !(*iEditor)->IsFocused() )
       
  1327                     {
       
  1328                     // ClearSelectionL() does not draw itself if focus was lost
       
  1329 
       
  1330                     TRect rect = (*iEditor)->TextLayout()->GetLineRectL( 
       
  1331                             iCurrentItemExt->iStart, iCurrentItemExt->iEnd );
       
  1332                     rect.Move( (*iEditor)->Position() );
       
  1333                     rect.iTl.iX = 0;
       
  1334                     rect.iBr.iX = (*iEditor)->Rect().iBr.iX;
       
  1335 
       
  1336                     TPoint begin ( 0, 0 );
       
  1337                     TPoint end ( 0, 0 );
       
  1338                     (*iEditor)->TextLayout()->DocPosToXyPosL(
       
  1339                             iCurrentItemExt->iStart, begin );
       
  1340                     (*iEditor)->TextLayout()->DocPosToXyPosL(
       
  1341                             iCurrentItemExt->iEnd, end );
       
  1342                     
       
  1343                     if ( begin.iY != end.iY )
       
  1344                         {
       
  1345                         // item spawns two lines
       
  1346                         // make rectangle cover both lines
       
  1347                         rect.iBr.iY += ( end.iY - begin.iY );
       
  1348                         }
       
  1349                     // draw clearable area
       
  1350                     (*iEditor)->DrawNow( rect );
       
  1351                     }
       
  1352                 }
       
  1353             }
       
  1354         }
       
  1355     }
       
  1356 
       
  1357 // --------------------------------------------------------------------------
       
  1358 // CItemFinder::ModifyPointerEvent
       
  1359 // --------------------------------------------------------------------------
       
  1360 //
       
  1361 void CItemFinder::ModifyPointerEvent( TPoint &aTapPoint )
       
  1362     {
       
  1363     aTapPoint -= (*iEditor)->Position(); 
       
  1364     }
       
  1365 
       
  1366 // --------------------------------------------------------------------------
       
  1367 // CItemFinder::DragThresholdL
       
  1368 // --------------------------------------------------------------------------
       
  1369 //
       
  1370 TInt CItemFinder::DragThresholdL()
       
  1371     {
       
  1372     if ( 0 == iDragThreshold )
       
  1373         {
       
  1374         CRepository* cenrep ( NULL );
       
  1375         TRAPD( err, cenrep = CRepository::NewL( KCRUidAknPhysicsSettings ) );
       
  1376         if ( KErrNone == err && cenrep )
       
  1377             {
       
  1378             CleanupStack::PushL( cenrep );
       
  1379             cenrep->Get( KDragTreshold, iDragThreshold );
       
  1380             CleanupStack::PopAndDestroy( cenrep );
       
  1381             }
       
  1382         }
       
  1383     if ( 0 == iDragThreshold )
       
  1384         {
       
  1385         // cenrep failed, use fallback value
       
  1386         iDragThreshold = KDragTresholdDefault;
       
  1387         }
       
  1388     return iDragThreshold; 
       
  1389     }
       
  1390 
       
  1391 // --------------------------------------------------------------------------
       
  1392 // CItemFinder::HasSelection
       
  1393 // --------------------------------------------------------------------------
       
  1394 //
       
  1395 TBool CItemFinder::HasSelection() const
       
  1396     {
       
  1397     return static_cast<TBool>( (*iEditor)->SelectionLength() );
       
  1398     }
       
  1399 
  1144 // End of File
  1400 // End of File