|
1 /* |
|
2 * Copyright (c) 2008 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 */ |
|
15 |
|
16 // System includes |
|
17 #include <e32base.h> |
|
18 #include <e32const.h> |
|
19 #include <coecntrl.h> |
|
20 #include <coemain.h> |
|
21 #include <aknphysics.h> |
|
22 |
|
23 // User includes |
|
24 #include "xncontroladapter.h" |
|
25 #include "xnuiengine.h" |
|
26 #include "xnnode.h" |
|
27 #include "xnnodepluginif.h" |
|
28 #include "xnproperty.h" |
|
29 |
|
30 #include "xnscrollablecontroladapter.h" |
|
31 |
|
32 // Constants |
|
33 const TInt KOffset( 25 ); |
|
34 |
|
35 _LIT8(KViewportHeight, "viewport-height"); |
|
36 _LIT8(KViewportWidth, "viewport-width"); |
|
37 _LIT8(KViewportTop, "viewport-top"); |
|
38 _LIT8(KViewportLeft, "viewport-left"); |
|
39 |
|
40 // ============================ MEMBER FUNCTIONS =============================== |
|
41 |
|
42 // ----------------------------------------------------------------------------- |
|
43 // CXnScrollableControlAdapter::NewL |
|
44 // Two-phased constructor. Can leave. |
|
45 // ----------------------------------------------------------------------------- |
|
46 // |
|
47 CXnScrollableControlAdapter* CXnScrollableControlAdapter::NewL( |
|
48 CXnNodePluginIf& aNode ) |
|
49 { |
|
50 CXnScrollableControlAdapter* self = |
|
51 new ( ELeave ) CXnScrollableControlAdapter( aNode ); |
|
52 |
|
53 CleanupStack::PushL( self ); |
|
54 self->ConstructL(); |
|
55 CleanupStack::Pop( self ); |
|
56 return self; |
|
57 } |
|
58 |
|
59 // ----------------------------------------------------------------------------- |
|
60 // CXnScrollableControlAdapter::~CXnScrollableControlAdapter |
|
61 // Destructor. |
|
62 // ----------------------------------------------------------------------------- |
|
63 // |
|
64 CXnScrollableControlAdapter::~CXnScrollableControlAdapter() |
|
65 { |
|
66 delete iPhysics; |
|
67 } |
|
68 |
|
69 // ----------------------------------------------------------------------------- |
|
70 // CXnScrollableControlAdapter::CXnScrollableControlAdapter |
|
71 // C++ default constructor. Must not leave. |
|
72 // ----------------------------------------------------------------------------- |
|
73 // |
|
74 CXnScrollableControlAdapter::CXnScrollableControlAdapter( CXnNodePluginIf& aNode ) |
|
75 : iNode( aNode ), iStartPosition( 0, 0), iPreviousPosition( 0, 0 ), |
|
76 iCurrentPosition( 0, 0 ), iLayoutChanged( EFalse ) |
|
77 { |
|
78 } |
|
79 |
|
80 // ----------------------------------------------------------------------------- |
|
81 // CXnScrollableControlAdapter::ConstructL |
|
82 // 2nd phase constructor. Can leave. |
|
83 // ----------------------------------------------------------------------------- |
|
84 // |
|
85 void CXnScrollableControlAdapter::ConstructL() |
|
86 { |
|
87 CXnControlAdapter::ConstructL( iNode ); |
|
88 SetControlContext( this ); |
|
89 SetHitTest( this ); |
|
90 iUiEngine = iNode.Node().UiEngine(); |
|
91 |
|
92 iNode.Node().SetScrollableControl( this ); |
|
93 } |
|
94 |
|
95 // ----------------------------------------------------------------------------- |
|
96 // CXnScrollableControlAdapter::HandlePointerEventL |
|
97 // |
|
98 // ----------------------------------------------------------------------------- |
|
99 // |
|
100 void CXnScrollableControlAdapter::HandlePointerEventL( |
|
101 const TPointerEvent& aPointerEvent ) |
|
102 { |
|
103 if( !iPhysics ) |
|
104 { |
|
105 return; |
|
106 } |
|
107 TPoint stylusPos = aPointerEvent.iPosition; |
|
108 switch( aPointerEvent.iType ) |
|
109 { |
|
110 case TPointerEvent::EButton1Down: |
|
111 { |
|
112 iPhysics->StopPhysics(); |
|
113 iPhysics->ResetFriction(); |
|
114 iStartPosition = stylusPos; |
|
115 iStartTime.HomeTime(); |
|
116 iPreviousPosition = iCurrentPosition; |
|
117 iStylusPosition = stylusPos; |
|
118 } |
|
119 break; |
|
120 case TPointerEvent::EButton1Up: |
|
121 { |
|
122 TInt distance = iStartPosition.iY - stylusPos.iY; |
|
123 TPoint drag( 0, distance ); |
|
124 iPhysics->StartPhysics( drag, iStartTime ); |
|
125 } |
|
126 break; |
|
127 case TPointerEvent::EDrag: |
|
128 { |
|
129 TPoint distanceFromStart( iStartPosition - stylusPos ); |
|
130 |
|
131 if( Abs( distanceFromStart.iY ) > KOffset ) |
|
132 { |
|
133 CXnNode* focused( iUiEngine->FocusedNode() ); |
|
134 |
|
135 if ( focused ) |
|
136 { |
|
137 // Remove pressed down |
|
138 focused->UnsetStateL( |
|
139 XnPropertyNames::style::common::KPressedDown ); |
|
140 focused->HideTooltipsL(); |
|
141 } |
|
142 } |
|
143 |
|
144 TInt deltaY( iStylusPosition.iY - stylusPos.iY ); |
|
145 iStylusPosition = stylusPos; |
|
146 TPoint deltaPoint( 0, deltaY ); |
|
147 iPhysics->RegisterPanningPosition( deltaPoint ); |
|
148 } |
|
149 break; |
|
150 default: |
|
151 break; |
|
152 } |
|
153 |
|
154 CXnControlAdapter::HandlePointerEventL( aPointerEvent ); |
|
155 } |
|
156 |
|
157 // ----------------------------------------------------------------------------- |
|
158 // CXnScrollableControlAdapter::Draw |
|
159 // |
|
160 // ----------------------------------------------------------------------------- |
|
161 // |
|
162 void CXnScrollableControlAdapter::Draw( const TRect& aRect ) const |
|
163 { |
|
164 CXnControlAdapter::Draw( aRect ); |
|
165 } |
|
166 |
|
167 // ----------------------------------------------------------------------------- |
|
168 // CXnScrollableControlAdapter::SizeChanged |
|
169 // |
|
170 // ----------------------------------------------------------------------------- |
|
171 void CXnScrollableControlAdapter::SizeChanged() |
|
172 { |
|
173 TRAP_IGNORE( |
|
174 ReadPropertiesL(); |
|
175 InitPhysicEngineL(); ); |
|
176 |
|
177 CXnControlAdapter::SizeChanged(); |
|
178 } |
|
179 |
|
180 // ----------------------------------------------------------------------------- |
|
181 // CXnScrollableControlAdapter::MakeVisible |
|
182 // |
|
183 // ----------------------------------------------------------------------------- |
|
184 // |
|
185 void CXnScrollableControlAdapter::MakeVisible( TBool aVisible ) |
|
186 { |
|
187 TBool visible( IsVisible() ? ETrue : EFalse ); |
|
188 |
|
189 if ( visible == aVisible ) |
|
190 { |
|
191 return; |
|
192 } |
|
193 |
|
194 if( aVisible ) |
|
195 { |
|
196 TRAP_IGNORE( InitPhysicEngineL() ); |
|
197 } |
|
198 |
|
199 CCoeControl::MakeVisible( aVisible ); |
|
200 } |
|
201 |
|
202 // ----------------------------------------------------------------------------- |
|
203 // CXnScrollableControlAdapter::HitRegionContains |
|
204 // |
|
205 // ----------------------------------------------------------------------------- |
|
206 // |
|
207 TBool CXnScrollableControlAdapter::HitRegionContains( const TPoint& aPoint, |
|
208 const CCoeControl& /*aControl*/) const |
|
209 { |
|
210 return iViewPort.Contains( aPoint ); |
|
211 } |
|
212 |
|
213 // ----------------------------------------------------------------------------- |
|
214 // CXnScrollableControlAdapter::HandleScreenDeviceChangedL |
|
215 // |
|
216 // ----------------------------------------------------------------------------- |
|
217 void CXnScrollableControlAdapter::HandleScreenDeviceChangedL() |
|
218 { |
|
219 // The new layout has not been calculated yet. |
|
220 // Therefore we need to read new propertues and initialise engine when LayoutChagedL() is called next time. |
|
221 iLayoutChanged = ETrue; |
|
222 CXnControlAdapter::HandleScreenDeviceChangedL(); |
|
223 } |
|
224 |
|
225 // ----------------------------------------------------------------------------- |
|
226 // CXnScrollableControlAdapter::DeltaPosition |
|
227 // |
|
228 // ----------------------------------------------------------------------------- |
|
229 const TPoint CXnScrollableControlAdapter::DeltaPosition() const |
|
230 { |
|
231 return iStartViewPosition - iCurrentPosition; |
|
232 } |
|
233 |
|
234 // ----------------------------------------------------------------------------- |
|
235 // CXnScrollableControlAdapter::ResetState |
|
236 // |
|
237 // ----------------------------------------------------------------------------- |
|
238 void CXnScrollableControlAdapter::ResetState() |
|
239 { |
|
240 iStartViewPosition = TPoint( 0, 0 ); |
|
241 iCurrentPosition = TPoint( 0, 0 ); |
|
242 iPreviousPosition = TPoint( 0, 0 ); |
|
243 } |
|
244 |
|
245 // ----------------------------------------------------------------------------- |
|
246 // CXnScrollableControlAdapter::ShowItem |
|
247 // |
|
248 // ----------------------------------------------------------------------------- |
|
249 void CXnScrollableControlAdapter::ShowItem( CXnNode& aNode ) |
|
250 { |
|
251 TRect rect = aNode.MarginRect(); |
|
252 if( !iViewPort.Contains( rect.iTl ) || !iViewPort.Contains( rect.iBr ) ) |
|
253 { |
|
254 TInt delta( 0 ); |
|
255 if( rect.iTl.iY < iViewPort.iTl.iY ) |
|
256 { |
|
257 delta = rect.iTl.iY - iViewPort.iTl.iY; |
|
258 } |
|
259 else if( rect.iBr.iY > iViewPort.iBr.iY ) |
|
260 { |
|
261 delta = rect.iBr.iY - iViewPort.iBr.iY; |
|
262 } |
|
263 TPoint newPosition = iPreviousPosition + TPoint( 0, delta ); |
|
264 ViewPositionChanged( newPosition, ETrue, 0 ); |
|
265 } |
|
266 } |
|
267 |
|
268 // ----------------------------------------------------------------------------- |
|
269 // CXnScrollableControlAdapter::LayoutChangedL |
|
270 // |
|
271 // ----------------------------------------------------------------------------- |
|
272 void CXnScrollableControlAdapter::LayoutChangedL() |
|
273 { |
|
274 if( iLayoutChanged ) |
|
275 { |
|
276 iLayoutChanged = EFalse; |
|
277 ReadPropertiesL(); |
|
278 InitPhysicEngineL(); |
|
279 } |
|
280 } |
|
281 |
|
282 // --------------------------------------------------------------------------- |
|
283 // CXnScrollableControlAdapter::ViewPositionChanged |
|
284 // --------------------------------------------------------------------------- |
|
285 // |
|
286 void CXnScrollableControlAdapter::ViewPositionChanged( |
|
287 const TPoint& aNewPosition, TBool aDrawNow, TUint /*aFlags*/ ) |
|
288 { |
|
289 TPoint diff = TPoint( 0, aNewPosition.iY - iPreviousPosition.iY ); |
|
290 iPreviousPosition = aNewPosition; |
|
291 iCurrentPosition += diff; |
|
292 TPoint tmpPos = iNode.Control()->Position(); |
|
293 tmpPos -= diff; |
|
294 iNode.Control()->SetPosition( tmpPos ); |
|
295 if( aDrawNow ) |
|
296 { |
|
297 DrawNow( iViewPort ); |
|
298 } |
|
299 } |
|
300 |
|
301 // --------------------------------------------------------------------------- |
|
302 // CXnScrollableControlAdapter::PhysicEmulationEnded |
|
303 // --------------------------------------------------------------------------- |
|
304 // |
|
305 void CXnScrollableControlAdapter::PhysicEmulationEnded() |
|
306 { |
|
307 } |
|
308 |
|
309 // --------------------------------------------------------------------------- |
|
310 // CXnScrollableControlAdapter::ViewPosition |
|
311 // --------------------------------------------------------------------------- |
|
312 // |
|
313 TPoint CXnScrollableControlAdapter::ViewPosition() const |
|
314 { |
|
315 return iCurrentPosition; |
|
316 } |
|
317 |
|
318 // --------------------------------------------------------------------------- |
|
319 // CXnScrollableControlAdapter::ResetContext |
|
320 // --------------------------------------------------------------------------- |
|
321 // |
|
322 void CXnScrollableControlAdapter::ResetContext( CWindowGc& aGc ) const |
|
323 { |
|
324 aGc.Reset(); |
|
325 aGc.SetClippingRect( iViewPort ); |
|
326 } |
|
327 |
|
328 // --------------------------------------------------------------------------- |
|
329 // CXnScrollableControlAdapter::InitPhysicEngineL |
|
330 // --------------------------------------------------------------------------- |
|
331 // |
|
332 void CXnScrollableControlAdapter::InitPhysicEngineL() |
|
333 { |
|
334 // Init physic engine |
|
335 if ( !iPhysics && CAknPhysics::FeatureEnabled() ) |
|
336 { |
|
337 iPhysics = CAknPhysics::NewL( *this, this ); |
|
338 } |
|
339 if( !iPhysics ) |
|
340 { |
|
341 return; |
|
342 } |
|
343 TSize viewPortSize = iViewPort.Size(); |
|
344 TSize totalSize( iNode.MarginRect().Size() ); |
|
345 iPhysics->InitPhysicsL( totalSize, viewPortSize, EFalse ); |
|
346 iCurrentPosition = TPoint( 0, viewPortSize.iHeight / 2 ); |
|
347 iPreviousPosition = iStartViewPosition = iCurrentPosition; |
|
348 } |
|
349 |
|
350 // --------------------------------------------------------------------------- |
|
351 // CXnScrollableControlAdapter::ReadPropertiesL |
|
352 // --------------------------------------------------------------------------- |
|
353 // |
|
354 void CXnScrollableControlAdapter::ReadPropertiesL() |
|
355 { |
|
356 TInt width( 0 ); |
|
357 TInt height( 0 ); |
|
358 TInt top( 0 ); |
|
359 TInt left( 0 ); |
|
360 |
|
361 // Width |
|
362 CXnProperty* prop = iNode.GetPropertyL( KViewportWidth ); |
|
363 if( prop ) |
|
364 { |
|
365 width = iUiEngine->HorizontalPixelValueL( |
|
366 prop, iNode.MarginRect().Width() ); |
|
367 } |
|
368 if( width == 0 ) |
|
369 { |
|
370 width = iNode.MarginRect().Width(); |
|
371 } |
|
372 |
|
373 // Height |
|
374 prop = iNode.GetPropertyL( KViewportHeight ); |
|
375 if( prop ) |
|
376 { |
|
377 height = iUiEngine->VerticalPixelValueL( |
|
378 prop, iNode.MarginRect().Height() ); |
|
379 } |
|
380 if( height == 0 ) |
|
381 { |
|
382 height = iNode.MarginRect().Height(); |
|
383 } |
|
384 |
|
385 // Top |
|
386 prop = iNode.GetPropertyL( KViewportTop ); |
|
387 if( prop ) |
|
388 { |
|
389 top = iUiEngine->VerticalPixelValueL( |
|
390 prop, iNode.MarginRect().Height() ); |
|
391 } |
|
392 |
|
393 // Left |
|
394 prop = iNode.GetPropertyL( KViewportLeft ); |
|
395 if( prop ) |
|
396 { |
|
397 left = iUiEngine->HorizontalPixelValueL( |
|
398 prop, iNode.MarginRect().Width() ); |
|
399 } |
|
400 |
|
401 iViewPort = TRect( iNode.MarginRect().iTl, TSize( width, height ) ); |
|
402 iViewPort.Move( left, top ); |
|
403 } |
|
404 |
|
405 // End of File |