|
1 // Copyright (c) 2005-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 // All rights reserved. |
|
3 // This component and the accompanying materials are made available |
|
4 // under the terms of "Eclipse Public License v1.0" |
|
5 // which accompanies this distribution, and is available |
|
6 // at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // CImgDisplayAppView |
|
15 // |
|
16 // |
|
17 |
|
18 #include <eikenv.h> |
|
19 |
|
20 #include "TImageDisplayAppView.h" |
|
21 |
|
22 CImgDisplayAppView* CImgDisplayAppView::NewL(const TRect& aRect) |
|
23 { |
|
24 CImgDisplayAppView* self = new (ELeave) CImgDisplayAppView; |
|
25 CleanupStack::PushL(self); |
|
26 self->ConstructL(aRect); |
|
27 CleanupStack::Pop(); |
|
28 return self; |
|
29 } |
|
30 |
|
31 CImgDisplayAppView::CImgDisplayAppView(): |
|
32 CCoeControl() |
|
33 {} |
|
34 |
|
35 void CImgDisplayAppView::ConstructL(const TRect& /*aRect*/) |
|
36 { |
|
37 CreateWindowL(); |
|
38 #if defined(__WINS__) |
|
39 Window().SetRequiredDisplayMode(SystemGc().Device()->DisplayMode()); |
|
40 #endif |
|
41 iDisplayMode = Window().DisplayMode(); |
|
42 EnableDragEvents(); |
|
43 SetExtentToWholeScreen(); |
|
44 |
|
45 iBmBuffer = new (ELeave) CWsBitmap(iCoeEnv->WsSession()); |
|
46 ActivateL(); |
|
47 } |
|
48 |
|
49 CImgDisplayAppView::~CImgDisplayAppView() |
|
50 { |
|
51 delete iBmGc; |
|
52 delete iBmDevice; |
|
53 delete iBmBuffer; |
|
54 } |
|
55 |
|
56 void CImgDisplayAppView::Draw(const TRect& aRect) const |
|
57 { |
|
58 CWindowGc& gc = SystemGc(); |
|
59 TRect drawRect=Rect(); |
|
60 |
|
61 ASSERT(!iBitmapValid || iBmRect.Size() == iBmBuffer->SizeInPixels()); // either bitmap not valid or size of bmrect is same as the buffer |
|
62 |
|
63 if (iBitmapValid) |
|
64 { |
|
65 // if the required rect includes some background, then draw it |
|
66 // check is to see if the passed aRect is a pure subset of the bitmap rect |
|
67 TRect intersection(aRect); |
|
68 intersection.Intersection(iBmRect); |
|
69 if (intersection != aRect) |
|
70 { |
|
71 gc.SetPenStyle(CGraphicsContext::ENullPen); // solid background rect |
|
72 gc.SetBrushStyle(CGraphicsContext::ESolidBrush); |
|
73 gc.SetBrushColor(KRgbWhite); |
|
74 gc.DrawRect(drawRect); |
|
75 TRect frame(iBmRect); // draw a frame one pixel larger than bitmap |
|
76 frame.Grow(1,1); |
|
77 gc.SetBrushStyle(CGraphicsContext::ENullBrush); |
|
78 gc.SetPenStyle(CGraphicsContext::ESolidPen); |
|
79 gc.DrawRect(frame); |
|
80 } |
|
81 // now if there is some bitmap to be drawn... |
|
82 if (!intersection.IsEmpty()) |
|
83 { |
|
84 gc.BitBlt(iBmRect.iTl, iBmBuffer); |
|
85 } |
|
86 } |
|
87 else |
|
88 { |
|
89 gc.Clear(); |
|
90 drawRect.Shrink(10,10); |
|
91 gc.DrawRect(drawRect); |
|
92 gc.DrawLine(drawRect.iTl,drawRect.iBr); |
|
93 gc.DrawLine(TPoint(drawRect.iTl.iX,drawRect.iBr.iY),TPoint(drawRect.iBr.iX,drawRect.iTl.iY)); |
|
94 } |
|
95 } |
|
96 |
|
97 void CImgDisplayAppView::Reset(TDrawNow aDrawNow) |
|
98 { |
|
99 iBmBuffer->Reset(); |
|
100 iBitmapValid = EFalse; |
|
101 if (aDrawNow!=ENoDrawNow) |
|
102 DrawNow(); |
|
103 } |
|
104 |
|
105 void CImgDisplayAppView::DrawImage(const CFbsBitmap* aBitmap, const TPoint& aOffset, TDrawNow aDrawNow) |
|
106 { |
|
107 DrawImage(aBitmap, NULL, aOffset, aDrawNow); |
|
108 } |
|
109 |
|
110 void CImgDisplayAppView::DrawImage(const CFbsBitmap* aBitmap, const CFbsBitmap* aMask, const TPoint& aOffset, TDrawNow aDrawNow) |
|
111 { |
|
112 ASSERT(iBitmapValid && iBmBuffer->Handle()); // should only be called when size setup properly |
|
113 ASSERT(aMask==NULL || aBitmap->SizeInPixels()==aMask->SizeInPixels()); |
|
114 // if we have a mask, assumed to be the same size as the original |
|
115 |
|
116 const TPoint screenOffset = iBmRect.iTl + aOffset; // relative to screen instead of iBmRect |
|
117 const TRect bitmapRect (screenOffset, aBitmap->SizeInPixels()); // the rect for this bitmap |
|
118 |
|
119 #if defined(_DEBUG) |
|
120 TRect intersection (bitmapRect); // check that this rect is same or smaller than the bitmap |
|
121 ASSERT(iBmRect.Size() == iBmBuffer->SizeInPixels()); |
|
122 intersection.Intersection(iBmRect); |
|
123 //ASSERT(intersection==bitmapRect); |
|
124 #endif |
|
125 |
|
126 // first draw to bitmap buffer |
|
127 if (aMask) |
|
128 iBmGc->BitBltMasked(aOffset, aBitmap, TRect(aBitmap->SizeInPixels()), aMask, EFalse); |
|
129 else |
|
130 iBmGc->BitBlt(aOffset, aBitmap); |
|
131 |
|
132 // if required, also draw to screen |
|
133 if (aDrawNow!=ENoDrawNow) |
|
134 { |
|
135 ActivateGc(); |
|
136 CWindowGc& gc = SystemGc(); |
|
137 RWindow& window = Window(); |
|
138 window.Invalidate(bitmapRect); |
|
139 window.BeginRedraw(bitmapRect); |
|
140 if (aMask) |
|
141 gc.BitBltMasked(screenOffset, aBitmap, TRect(aBitmap->SizeInPixels()), aMask, EFalse); |
|
142 else |
|
143 gc.BitBlt(screenOffset, aBitmap); |
|
144 window.EndRedraw(); |
|
145 DeactivateGc(); |
|
146 } |
|
147 } |
|
148 |
|
149 TBool CImgDisplayAppView::ResizeL(const TSize& aNewSize, TBool aClear, TDrawNow aDrawNow) |
|
150 { |
|
151 //Resize iBmBuffer to iBmRect where iBmBuffer holds the aNewSize. |
|
152 if(iBmRect.Size() != aNewSize) |
|
153 { |
|
154 iBmBuffer->Resize(iBmRect.Size()); |
|
155 } |
|
156 ASSERT(!iBitmapValid || iBmRect.Size() == iBmBuffer->SizeInPixels()); // either bitmap not valid or size of bmrect is same as the buffer |
|
157 if (iBitmapValid && aNewSize==iBmRect.Size()) |
|
158 { |
|
159 // special cases where we don't actually modify the size |
|
160 if (aClear) |
|
161 Clear(EFalse, aDrawNow); |
|
162 else if (aDrawNow!=ENoDrawNow) |
|
163 DrawNow(); |
|
164 return EFalse; |
|
165 } |
|
166 |
|
167 CFbsBitmap* tempBuffer = NULL; |
|
168 |
|
169 TBool preserveOrig = !aClear && iBitmapValid; |
|
170 |
|
171 if (preserveOrig) |
|
172 { |
|
173 // tempBuffer becomes copy of original |
|
174 tempBuffer = new (ELeave) CWsBitmap(iCoeEnv->WsSession()); |
|
175 CleanupStack::PushL(tempBuffer); |
|
176 User::LeaveIfError(tempBuffer->Duplicate(iBmBuffer->Handle())); |
|
177 } |
|
178 |
|
179 ResizeBufferL(aNewSize, iDisplayMode); |
|
180 // resize bitmap |
|
181 |
|
182 iBitmapValid = ETrue; |
|
183 iBmRect.SetRect(iBmRect.iTl, aNewSize); // rect with same Tl but new size |
|
184 ASSERT(iBmRect.Size() == iBmBuffer->SizeInPixels()); // check resized bitmap OK |
|
185 |
|
186 if (preserveOrig) |
|
187 { |
|
188 // draw original back at new size |
|
189 EnsureSizeInTwipsSet(tempBuffer); |
|
190 EnsureSizeInTwipsSet(iBmBuffer); |
|
191 iBmGc->DrawBitmap(TPoint(0,0), tempBuffer); |
|
192 CleanupStack::PopAndDestroy(); // tempBuffer |
|
193 } |
|
194 else |
|
195 Clear(EFalse, ENoDrawNow); // get background correct colour |
|
196 |
|
197 if (aDrawNow!=ENoDrawNow) |
|
198 DrawNow(); |
|
199 |
|
200 return ETrue; |
|
201 } |
|
202 |
|
203 void CImgDisplayAppView::ResizeBufferL(const TSize& aNewSize, TDisplayMode aDisplayMode) |
|
204 { |
|
205 delete iBmGc; iBmGc = NULL; |
|
206 delete iBmDevice; iBmDevice = NULL; |
|
207 User::LeaveIfError(iBmBuffer->Create(aNewSize, aDisplayMode)); |
|
208 iBmDevice = CFbsBitmapDevice::NewL(iBmBuffer); |
|
209 iBmGc = CFbsBitGc::NewL(); |
|
210 iBmGc->Activate(iBmDevice); |
|
211 } |
|
212 |
|
213 void CImgDisplayAppView::EnsureSizeInTwipsSet(CFbsBitmap* aBitmap) const |
|
214 { |
|
215 // ensure the bitmap has twips size set - this allows us to use DrawBitmap |
|
216 // note this does not itself resize the bitmap - size in pixels remains unchanged |
|
217 TSize size = aBitmap->SizeInTwips(); |
|
218 ASSERT(size.iWidth==0 && size.iHeight==0 || size.iWidth>0 && size.iHeight>0); |
|
219 // assumption that if we've set the size it is properly formatted |
|
220 if (size==TSize(0,0)) |
|
221 { |
|
222 CWsScreenDevice *const screenDevice = iEikonEnv->ScreenDevice(); |
|
223 size = aBitmap->SizeInPixels(); |
|
224 size.iWidth = screenDevice->HorizontalTwipsToPixels(size.iWidth); |
|
225 size.iHeight = screenDevice->VerticalTwipsToPixels(size.iHeight); |
|
226 aBitmap->SetSizeInTwips(size); |
|
227 } |
|
228 } |
|
229 |
|
230 void CImgDisplayAppView::Clear(TBool aClearFull, TDrawNow aDrawNow) |
|
231 { |
|
232 // if we have a bitmap buffer clear that. Otherwise clear the whole screen depending |
|
233 // on aClearFull |
|
234 if (iBmGc) |
|
235 { |
|
236 iBmGc->Reset(); |
|
237 iBmGc->SetPenStyle(CGraphicsContext::ENullPen); |
|
238 iBmGc->SetBrushStyle(CGraphicsContext::ESolidBrush); |
|
239 iBmGc->SetBrushColor(iBackgroundColor); |
|
240 iBmGc->Clear(); |
|
241 } |
|
242 if (aDrawNow!=ENoDrawNow) |
|
243 { |
|
244 if (aClearFull) |
|
245 DrawNow(); |
|
246 else |
|
247 { |
|
248 ActivateGc(); |
|
249 CWindowGc& gc = SystemGc(); |
|
250 RWindow& window = Window(); |
|
251 window.Invalidate(iBmRect); |
|
252 window.BeginRedraw(iBmRect); |
|
253 gc.SetPenStyle(CGraphicsContext::ENullPen); |
|
254 gc.SetBrushStyle(CGraphicsContext::ESolidBrush); |
|
255 gc.SetBrushColor(iBackgroundColor); |
|
256 gc.Clear(); |
|
257 window.EndRedraw(); |
|
258 DeactivateGc(); |
|
259 } |
|
260 } |
|
261 } |
|
262 |
|
263 |
|
264 void CImgDisplayAppView::MoveBy(const TPoint& aRelMove, TDrawNow aDrawNow) |
|
265 { |
|
266 iBmRect.Move(aRelMove); |
|
267 |
|
268 if (aDrawNow!=ENoDrawNow) |
|
269 DrawNow(); |
|
270 } |
|
271 |
|
272 void CImgDisplayAppView::Center(TDrawNow aDrawNow) |
|
273 { |
|
274 ASSERT(iBitmapValid && iBmRect.Size() == iBmBuffer->SizeInPixels()); // should only be called when size setup properly |
|
275 |
|
276 const TPoint center = Rect().Center(); |
|
277 const TSize bitmapSize = iBmRect.Size(); |
|
278 const TPoint requiredTl (center.iX-bitmapSize.iWidth/2, center.iY-bitmapSize.iHeight/2); |
|
279 const TRect newRect(requiredTl, bitmapSize); |
|
280 iBmRect = newRect; |
|
281 |
|
282 ASSERT(iBitmapValid && iBmRect.Size() == iBmBuffer->SizeInPixels()); // checked worked |
|
283 |
|
284 if (aDrawNow!=ENoDrawNow) |
|
285 DrawNow(); |
|
286 } |
|
287 |
|
288 TBool CImgDisplayAppView::SetBackgroundColor(const TRgb& aColor, TDrawNow aDrawNow) |
|
289 { |
|
290 TBool changed = iBackgroundColor!=aColor; |
|
291 |
|
292 iBackgroundColor = aColor; |
|
293 |
|
294 if (aDrawNow!=ENoDrawNow) |
|
295 { |
|
296 DrawNow(); |
|
297 } |
|
298 |
|
299 return changed; |
|
300 } |
|
301 |
|
302 void CImgDisplayAppView::SetDisplayModeL(TDisplayMode aDisplayMode, TDrawNow aDrawNow) |
|
303 { |
|
304 iDisplayMode = aDisplayMode; |
|
305 |
|
306 Window().SetRequiredDisplayMode(iDisplayMode); |
|
307 |
|
308 if (aDrawNow!=ENoDrawNow) |
|
309 { |
|
310 DrawNow(); |
|
311 } |
|
312 } |
|
313 |