20
|
1 |
/*
|
|
2 |
* Copyright (c) 2009 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:
|
|
15 |
*
|
|
16 |
*/
|
|
17 |
|
|
18 |
|
|
19 |
#include "HgScrollBufferManager.h"
|
|
20 |
#include <ganes/HgScrollBufferObserverIface.h>
|
|
21 |
|
|
22 |
#include <e32debug.h>
|
|
23 |
|
|
24 |
// -----------------------------------------------------------------------------
|
|
25 |
// CHgScrollBufferManager::NewL()
|
|
26 |
// -----------------------------------------------------------------------------
|
|
27 |
//
|
|
28 |
CHgScrollBufferManager* CHgScrollBufferManager::NewL(
|
|
29 |
MHgScrollBufferObserver& aObserver,
|
|
30 |
TInt aBufferSize,
|
|
31 |
TInt aBufferTreshold,
|
|
32 |
TInt aInitialPosition,
|
|
33 |
TInt aTotalCount )
|
|
34 |
{
|
|
35 |
CHgScrollBufferManager* self = new (ELeave) CHgScrollBufferManager(
|
|
36 |
aObserver,
|
|
37 |
aBufferSize,
|
|
38 |
aBufferTreshold,
|
|
39 |
aInitialPosition,
|
|
40 |
aTotalCount );
|
|
41 |
CleanupStack::PushL(self);
|
|
42 |
self->ConstructL();
|
|
43 |
CleanupStack::Pop(self);
|
|
44 |
return self;
|
|
45 |
}
|
|
46 |
|
|
47 |
// -----------------------------------------------------------------------------
|
|
48 |
// CHgScrollBufferManager::CHgScrollBufferManager()
|
|
49 |
// -----------------------------------------------------------------------------
|
|
50 |
//
|
|
51 |
CHgScrollBufferManager::CHgScrollBufferManager(
|
|
52 |
MHgScrollBufferObserver& aObserver,
|
|
53 |
TInt aBufferSize,
|
|
54 |
TInt aBufferTreshold,
|
|
55 |
TInt aInitialPosition,
|
|
56 |
TInt aTotalCount )
|
|
57 |
: CActive( CActive::EPriorityHigh ),
|
|
58 |
iObserver( aObserver ),
|
|
59 |
iBufferSize( aBufferSize ),
|
|
60 |
iBufferTreshold( aBufferTreshold ),
|
|
61 |
iBufferPosition( aInitialPosition ),
|
|
62 |
iTotalCount( aTotalCount )
|
|
63 |
{
|
|
64 |
|
|
65 |
}
|
|
66 |
|
|
67 |
// -----------------------------------------------------------------------------
|
|
68 |
// CHgScrollBufferManager::ConstructL()
|
|
69 |
// -----------------------------------------------------------------------------
|
|
70 |
//
|
|
71 |
void CHgScrollBufferManager::ConstructL()
|
|
72 |
{
|
|
73 |
CActiveScheduler::Add(this);
|
|
74 |
|
|
75 |
iResetOrdered = ETrue;
|
|
76 |
ResetBuffer(iBufferPosition, iTotalCount);
|
|
77 |
}
|
|
78 |
|
|
79 |
// -----------------------------------------------------------------------------
|
|
80 |
// CHgScrollBufferManager::~CHgScrollBufferManager()
|
|
81 |
// -----------------------------------------------------------------------------
|
|
82 |
//
|
|
83 |
CHgScrollBufferManager::~CHgScrollBufferManager()
|
|
84 |
{
|
|
85 |
Cancel();
|
|
86 |
}
|
|
87 |
|
|
88 |
// -----------------------------------------------------------------------------
|
|
89 |
// CHgScrollBufferManager::RunL()
|
|
90 |
// -----------------------------------------------------------------------------
|
|
91 |
//
|
|
92 |
void CHgScrollBufferManager::RunL()
|
|
93 |
{
|
|
94 |
MHgScrollBufferObserver::THgScrollDirection direction = MHgScrollBufferObserver::EHgBufferNoMove;
|
|
95 |
|
|
96 |
if(iResetOrdered)
|
|
97 |
{
|
|
98 |
iResetOrdered = EFalse;
|
|
99 |
direction = MHgScrollBufferObserver::EHgBufferReset;
|
|
100 |
}
|
|
101 |
else
|
|
102 |
{
|
|
103 |
if(iDiff < 0)
|
|
104 |
{
|
|
105 |
iReleaseStart = iBufferPosition;
|
|
106 |
iRequestStart = iBufferPosition + iBufferSize;
|
|
107 |
direction = MHgScrollBufferObserver::EHgBufferScrollDown;
|
|
108 |
}
|
|
109 |
else if( iDiff > 0)
|
|
110 |
{
|
|
111 |
iReleaseStart = iBufferPosition + iBufferSize - iDiff;
|
|
112 |
iRequestStart = iBufferPosition - iDiff;
|
|
113 |
direction = MHgScrollBufferObserver::EHgBufferScrollUp;
|
|
114 |
}
|
|
115 |
}
|
|
116 |
|
|
117 |
// Release
|
|
118 |
TInt end = iReleaseStart + iReleaseCount < iTotalCount ?
|
|
119 |
iReleaseStart + iReleaseCount: iTotalCount;
|
|
120 |
end--;
|
|
121 |
if(end >= iReleaseStart )
|
|
122 |
{
|
|
123 |
iObserver.Release(iReleaseStart, end);
|
|
124 |
if(iOwner) iOwner->Release( iReleaseStart, end );
|
|
125 |
}
|
|
126 |
|
|
127 |
iReleaseCount = 0;
|
|
128 |
|
|
129 |
// Request
|
|
130 |
end = iRequestStart + iRequestCount < iTotalCount ?
|
|
131 |
iRequestStart + iRequestCount : iTotalCount;
|
|
132 |
|
|
133 |
end--;
|
|
134 |
if(end >= iRequestStart )
|
|
135 |
iObserver.Request(iRequestStart, end, direction);
|
|
136 |
|
|
137 |
iRequestCount = 0;
|
|
138 |
|
|
139 |
// Move Buffer
|
|
140 |
iBufferPosition -= iDiff;
|
|
141 |
// Reset Diff
|
|
142 |
iDiff = 0;
|
|
143 |
}
|
|
144 |
|
|
145 |
// -----------------------------------------------------------------------------
|
|
146 |
// CHgScrollBufferManager::DoCancel()
|
|
147 |
// -----------------------------------------------------------------------------
|
|
148 |
//
|
|
149 |
void CHgScrollBufferManager::DoCancel()
|
|
150 |
{
|
|
151 |
// Nothing to do here
|
|
152 |
}
|
|
153 |
|
|
154 |
// -----------------------------------------------------------------------------
|
|
155 |
// CHgScrollBufferManager::RunError()
|
|
156 |
// -----------------------------------------------------------------------------
|
|
157 |
//
|
|
158 |
TInt CHgScrollBufferManager::RunError( TInt /*aError*/ )
|
|
159 |
{
|
|
160 |
return KErrNone;
|
|
161 |
}
|
|
162 |
|
|
163 |
// -----------------------------------------------------------------------------
|
|
164 |
// CHgScrollBufferManager::SetPosition()
|
|
165 |
// -----------------------------------------------------------------------------
|
|
166 |
//
|
|
167 |
void CHgScrollBufferManager::SetPosition( TInt aIndex )
|
|
168 |
{
|
|
169 |
// If all the items fit in the buffer no need to move the buffer
|
|
170 |
if(iTotalCount <= iBufferSize) return;
|
|
171 |
|
|
172 |
TBool forceUpdate = EFalse;
|
|
173 |
aIndex -= iBufferSize / 2; // normalize index to Buffer start
|
|
174 |
|
|
175 |
if(aIndex < 0)
|
|
176 |
{
|
|
177 |
aIndex = 0;
|
|
178 |
forceUpdate = ETrue;
|
|
179 |
}
|
|
180 |
else if( aIndex > iTotalCount - iBufferSize )
|
|
181 |
{
|
|
182 |
aIndex = iTotalCount - iBufferSize;
|
|
183 |
forceUpdate = ETrue;
|
|
184 |
}
|
|
185 |
|
|
186 |
iDiff = iBufferPosition - aIndex;
|
|
187 |
|
|
188 |
// Too large change reset whole buffer
|
|
189 |
if( iDiff >= iBufferSize || -iDiff >= iBufferSize || iResetOrdered )
|
|
190 |
{
|
|
191 |
ResetBuffer(aIndex + (iBufferSize/2), iTotalCount);
|
|
192 |
}
|
|
193 |
// Move Up
|
|
194 |
else if( iDiff >= iBufferTreshold )
|
|
195 |
{
|
|
196 |
iRequestCount = iDiff;
|
|
197 |
iReleaseCount = iDiff;
|
|
198 |
NotifyObserver();
|
|
199 |
}
|
|
200 |
// Move Down
|
|
201 |
else if( -iDiff >= iBufferTreshold )
|
|
202 |
{
|
|
203 |
iRequestCount = -iDiff;
|
|
204 |
iReleaseCount = -iDiff;
|
|
205 |
NotifyObserver();
|
|
206 |
}
|
|
207 |
// Top or bottom has been reached
|
|
208 |
else if( forceUpdate && iDiff )
|
|
209 |
{
|
|
210 |
TInt diff = iDiff < 0 ? -iDiff : iDiff;
|
|
211 |
iRequestCount = diff;
|
|
212 |
iReleaseCount = diff;
|
|
213 |
NotifyObserver();
|
|
214 |
}
|
|
215 |
}
|
|
216 |
|
|
217 |
// -----------------------------------------------------------------------------
|
|
218 |
// CHgScrollBufferManager::SetTotalCount()
|
|
219 |
// -----------------------------------------------------------------------------
|
|
220 |
//
|
|
221 |
void CHgScrollBufferManager::SetTotalCount( TInt aTotalCount )
|
|
222 |
{
|
|
223 |
iTotalCount = aTotalCount;
|
|
224 |
}
|
|
225 |
|
|
226 |
// -----------------------------------------------------------------------------
|
|
227 |
// CHgScrollBufferManager::SetBufferOwner()
|
|
228 |
// -----------------------------------------------------------------------------
|
|
229 |
//
|
|
230 |
void CHgScrollBufferManager::SetBufferOwner( MHgBufferOwner& aOwner )
|
|
231 |
{
|
|
232 |
iOwner = &aOwner;
|
|
233 |
}
|
|
234 |
|
|
235 |
// -----------------------------------------------------------------------------
|
|
236 |
// CHgScrollBufferManager::ResetBuffer()
|
|
237 |
// -----------------------------------------------------------------------------
|
|
238 |
//
|
|
239 |
void CHgScrollBufferManager::ResetBuffer( TInt aPosition, TInt aTotalCount)
|
|
240 |
{
|
|
241 |
if( !iResetOrdered )
|
|
242 |
{
|
|
243 |
// release Old buffer
|
|
244 |
iReleaseStart = iBufferPosition;
|
|
245 |
iReleaseCount = iBufferSize;
|
|
246 |
}
|
|
247 |
|
|
248 |
// set position and count
|
|
249 |
iBufferPosition = aPosition - (iBufferSize / 2);
|
|
250 |
iTotalCount = aTotalCount;
|
|
251 |
iDiff = 0;
|
|
252 |
|
|
253 |
if( iBufferPosition + iBufferSize > iTotalCount - 1 )
|
|
254 |
{
|
|
255 |
iBufferPosition = iTotalCount - iBufferSize;
|
|
256 |
}
|
|
257 |
|
|
258 |
if(iBufferPosition < 0 )
|
|
259 |
{
|
|
260 |
iBufferPosition = 0;
|
|
261 |
}
|
|
262 |
|
|
263 |
//request new Buffer
|
|
264 |
iRequestStart = iBufferPosition;
|
|
265 |
iRequestCount = iBufferSize;
|
|
266 |
iResetOrdered = ETrue;
|
|
267 |
|
|
268 |
NotifyObserver();
|
|
269 |
}
|
|
270 |
|
|
271 |
// -----------------------------------------------------------------------------
|
|
272 |
// CHgScrollBufferManager::NotifyObserver()
|
|
273 |
// -----------------------------------------------------------------------------
|
|
274 |
//
|
|
275 |
void CHgScrollBufferManager::NotifyObserver()
|
|
276 |
{
|
|
277 |
if( !IsActive() )
|
|
278 |
{
|
|
279 |
SetActive();
|
|
280 |
TRequestStatus* status = &iStatus;
|
|
281 |
User::RequestComplete(status, KErrNone);
|
|
282 |
}
|
|
283 |
}
|
|
284 |
|
|
285 |
// -----------------------------------------------------------------------------
|
|
286 |
// CHgScrollBufferManager::ItemChanged()
|
|
287 |
// -----------------------------------------------------------------------------
|
|
288 |
//
|
|
289 |
void CHgScrollBufferManager::ItemCountChanged(
|
|
290 |
TInt aIndex,
|
|
291 |
TBool aRemoved,
|
|
292 |
TInt aNewTotalCount )
|
|
293 |
{
|
|
294 |
iTotalCount = aNewTotalCount;
|
|
295 |
if ( aIndex >= iBufferPosition && aIndex < iBufferPosition + iBufferSize )
|
|
296 |
{
|
|
297 |
// Change inside the buffer
|
|
298 |
MHgScrollBufferObserver::THgScrollDirection direction = MHgScrollBufferObserver::EHgBufferNoMove;
|
|
299 |
if( aRemoved )
|
|
300 |
{
|
|
301 |
// Release removed item ( Item deleted already from the owner )
|
|
302 |
iObserver.Release( aIndex, aIndex );
|
|
303 |
if( iTotalCount >= iBufferSize )
|
|
304 |
{
|
|
305 |
// There are more items to keep the buffer filled
|
|
306 |
if( iBufferPosition + iBufferSize <= iTotalCount )
|
|
307 |
{
|
|
308 |
// Requested from the end
|
|
309 |
iObserver.Request( iBufferPosition + iBufferSize - 1,
|
|
310 |
iBufferPosition + iBufferSize - 1, direction );
|
|
311 |
}
|
|
312 |
else if( iBufferPosition > 0 )
|
|
313 |
{
|
|
314 |
// Move buffer and request from the beginning
|
|
315 |
iBufferPosition--;
|
|
316 |
iObserver.Request( iBufferPosition, iBufferPosition, direction );
|
|
317 |
}
|
|
318 |
}
|
|
319 |
}
|
|
320 |
else
|
|
321 |
{
|
|
322 |
if( iTotalCount > iBufferSize )
|
|
323 |
{
|
|
324 |
// One item needs to be released
|
|
325 |
if( iBufferPosition + iBufferSize < iTotalCount )
|
|
326 |
{
|
|
327 |
// Release from the end of the buffer
|
|
328 |
iObserver.Release(iBufferPosition + iBufferSize, iBufferPosition + iBufferSize);
|
|
329 |
if(iOwner)
|
|
330 |
iOwner->Release(iBufferPosition + iBufferSize, iBufferPosition + iBufferSize);
|
|
331 |
}
|
|
332 |
}
|
|
333 |
// Request added item
|
|
334 |
iObserver.Request(aIndex, aIndex, direction);
|
|
335 |
}
|
|
336 |
}
|
|
337 |
}
|
|
338 |
|