gfx/skia/trunk/src/views/SkWindow.cpp

branch
TOR_BUG_3246
changeset 7
129ffea94266
equal deleted inserted replaced
-1:000000000000 0:7aca65a2c837
1
2 /*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8 #include "SkWindow.h"
9 #include "SkCanvas.h"
10 #include "SkDevice.h"
11 #include "SkOSMenu.h"
12 #include "SkSystemEventTypes.h"
13 #include "SkTime.h"
14
15 #define SK_EventDelayInval "\xd" "n" "\xa" "l"
16
17 #define TEST_BOUNDERx
18
19 #include "SkBounder.h"
20 class TestSkBounder : public SkBounder {
21 public:
22 explicit TestSkBounder(const SkBitmap& bm) : fCanvas(bm) {}
23
24 protected:
25 virtual bool onIRect(const SkIRect& r) SK_OVERRIDE {
26 SkRect rr;
27
28 rr.set(SkIntToScalar(r.fLeft), SkIntToScalar(r.fTop),
29 SkIntToScalar(r.fRight), SkIntToScalar(r.fBottom));
30
31 SkPaint p;
32
33 p.setStyle(SkPaint::kStroke_Style);
34 p.setColor(SK_ColorYELLOW);
35
36 #if 0
37 rr.inset(SK_ScalarHalf, SK_ScalarHalf);
38 #else
39 rr.inset(-SK_ScalarHalf, -SK_ScalarHalf);
40 #endif
41
42 fCanvas.drawRect(rr, p);
43 return true;
44 }
45 private:
46 SkCanvas fCanvas;
47 };
48
49 SkWindow::SkWindow() : fFocusView(NULL) {
50 fClicks.reset();
51 fWaitingOnInval = false;
52
53 #ifdef SK_BUILD_FOR_WINCE
54 fColorType = kRGB_565_SkColorType;
55 #else
56 fColorType = kPMColor_SkColorType;
57 #endif
58
59 fMatrix.reset();
60 }
61
62 SkWindow::~SkWindow() {
63 fClicks.deleteAll();
64 fMenus.deleteAll();
65 }
66
67 SkCanvas* SkWindow::createCanvas() {
68 return new SkCanvas(this->getBitmap());
69 }
70
71 void SkWindow::setMatrix(const SkMatrix& matrix) {
72 if (fMatrix != matrix) {
73 fMatrix = matrix;
74 this->inval(NULL);
75 }
76 }
77
78 void SkWindow::preConcat(const SkMatrix& matrix) {
79 SkMatrix m;
80 m.setConcat(fMatrix, matrix);
81 this->setMatrix(m);
82 }
83
84 void SkWindow::postConcat(const SkMatrix& matrix) {
85 SkMatrix m;
86 m.setConcat(matrix, fMatrix);
87 this->setMatrix(m);
88 }
89
90 void SkWindow::setColorType(SkColorType ct) {
91 this->resize(fBitmap.width(), fBitmap.height(), ct);
92 }
93
94 void SkWindow::resize(int width, int height, SkColorType ct) {
95 if (ct == kUnknown_SkColorType)
96 ct = fColorType;
97
98 if (width != fBitmap.width() || height != fBitmap.height() || ct != fColorType) {
99 fColorType = ct;
100 fBitmap.allocPixels(SkImageInfo::Make(width, height,
101 ct, kPremul_SkAlphaType));
102
103 this->setSize(SkIntToScalar(width), SkIntToScalar(height));
104 this->inval(NULL);
105 }
106 }
107
108 bool SkWindow::handleInval(const SkRect* localR) {
109 SkIRect ir;
110
111 if (localR) {
112 SkRect devR;
113 SkMatrix inverse;
114 if (!fMatrix.invert(&inverse)) {
115 return false;
116 }
117 fMatrix.mapRect(&devR, *localR);
118 devR.round(&ir);
119 } else {
120 ir.set(0, 0,
121 SkScalarRoundToInt(this->width()),
122 SkScalarRoundToInt(this->height()));
123 }
124 fDirtyRgn.op(ir, SkRegion::kUnion_Op);
125
126 this->onHandleInval(ir);
127 return true;
128 }
129
130 void SkWindow::forceInvalAll() {
131 fDirtyRgn.setRect(0, 0,
132 SkScalarCeilToInt(this->width()),
133 SkScalarCeilToInt(this->height()));
134 }
135
136 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
137 #include <windows.h>
138 #include <gx.h>
139 extern GXDisplayProperties gDisplayProps;
140 #endif
141
142 #ifdef SK_SIMULATE_FAILED_MALLOC
143 extern bool gEnableControlledThrow;
144 #endif
145
146 bool SkWindow::update(SkIRect* updateArea) {
147 if (!fDirtyRgn.isEmpty()) {
148 SkBitmap bm = this->getBitmap();
149
150 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
151 char* buffer = (char*)GXBeginDraw();
152 SkASSERT(buffer);
153
154 RECT rect;
155 GetWindowRect((HWND)((SkOSWindow*)this)->getHWND(), &rect);
156 buffer += rect.top * gDisplayProps.cbyPitch + rect.left * gDisplayProps.cbxPitch;
157
158 bm.setPixels(buffer);
159 #endif
160
161 SkAutoTUnref<SkCanvas> canvas(this->createCanvas());
162
163 canvas->clipRegion(fDirtyRgn);
164 if (updateArea)
165 *updateArea = fDirtyRgn.getBounds();
166
167 SkAutoCanvasRestore acr(canvas, true);
168 canvas->concat(fMatrix);
169
170 // empty this now, so we can correctly record any inval calls that
171 // might be made during the draw call.
172 fDirtyRgn.setEmpty();
173
174 #ifdef TEST_BOUNDER
175 TestSkBounder bounder(bm);
176 canvas->setBounder(&bounder);
177 #endif
178 #ifdef SK_SIMULATE_FAILED_MALLOC
179 gEnableControlledThrow = true;
180 #endif
181 #ifdef SK_BUILD_FOR_WIN32
182 //try {
183 this->draw(canvas);
184 //}
185 //catch (...) {
186 //}
187 #else
188 this->draw(canvas);
189 #endif
190 #ifdef SK_SIMULATE_FAILED_MALLOC
191 gEnableControlledThrow = false;
192 #endif
193 #ifdef TEST_BOUNDER
194 canvas->setBounder(NULL);
195 #endif
196
197 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
198 GXEndDraw();
199 #endif
200
201 return true;
202 }
203 return false;
204 }
205
206 bool SkWindow::handleChar(SkUnichar uni) {
207 if (this->onHandleChar(uni))
208 return true;
209
210 SkView* focus = this->getFocusView();
211 if (focus == NULL)
212 focus = this;
213
214 SkEvent evt(SK_EventType_Unichar);
215 evt.setFast32(uni);
216 return focus->doEvent(evt);
217 }
218
219 bool SkWindow::handleKey(SkKey key) {
220 if (key == kNONE_SkKey)
221 return false;
222
223 if (this->onHandleKey(key))
224 return true;
225
226 // send an event to the focus-view
227 {
228 SkView* focus = this->getFocusView();
229 if (focus == NULL)
230 focus = this;
231
232 SkEvent evt(SK_EventType_Key);
233 evt.setFast32(key);
234 if (focus->doEvent(evt))
235 return true;
236 }
237
238 if (key == kUp_SkKey || key == kDown_SkKey) {
239 if (this->moveFocus(key == kUp_SkKey ? kPrev_FocusDirection : kNext_FocusDirection) == NULL)
240 this->onSetFocusView(NULL);
241 return true;
242 }
243 return false;
244 }
245
246 bool SkWindow::handleKeyUp(SkKey key) {
247 if (key == kNONE_SkKey)
248 return false;
249
250 if (this->onHandleKeyUp(key))
251 return true;
252
253 //send an event to the focus-view
254 {
255 SkView* focus = this->getFocusView();
256 if (focus == NULL)
257 focus = this;
258
259 //should this one be the same?
260 SkEvent evt(SK_EventType_KeyUp);
261 evt.setFast32(key);
262 if (focus->doEvent(evt))
263 return true;
264 }
265 return false;
266 }
267
268 void SkWindow::addMenu(SkOSMenu* menu) {
269 *fMenus.append() = menu;
270 this->onAddMenu(menu);
271 }
272
273 void SkWindow::setTitle(const char title[]) {
274 if (NULL == title) {
275 title = "";
276 }
277 fTitle.set(title);
278 this->onSetTitle(title);
279 }
280
281 bool SkWindow::onEvent(const SkEvent& evt) {
282 if (evt.isType(SK_EventDelayInval)) {
283 for (SkRegion::Iterator iter(fDirtyRgn); !iter.done(); iter.next())
284 this->onHandleInval(iter.rect());
285 fWaitingOnInval = false;
286 return true;
287 }
288 return this->INHERITED::onEvent(evt);
289 }
290
291 bool SkWindow::onGetFocusView(SkView** focus) const {
292 if (focus)
293 *focus = fFocusView;
294 return true;
295 }
296
297 bool SkWindow::onSetFocusView(SkView* focus) {
298 if (fFocusView != focus) {
299 if (fFocusView)
300 fFocusView->onFocusChange(false);
301 fFocusView = focus;
302 if (focus)
303 focus->onFocusChange(true);
304 }
305 return true;
306 }
307
308 void SkWindow::onHandleInval(const SkIRect&) {
309 }
310
311 bool SkWindow::onHandleChar(SkUnichar) {
312 return false;
313 }
314
315 bool SkWindow::onHandleKey(SkKey) {
316 return false;
317 }
318
319 bool SkWindow::onHandleKeyUp(SkKey) {
320 return false;
321 }
322
323 bool SkWindow::handleClick(int x, int y, Click::State state, void *owner,
324 unsigned modifierKeys) {
325 return this->onDispatchClick(x, y, state, owner, modifierKeys);
326 }
327
328 bool SkWindow::onDispatchClick(int x, int y, Click::State state,
329 void* owner, unsigned modifierKeys) {
330 bool handled = false;
331
332 // First, attempt to find an existing click with this owner.
333 int index = -1;
334 for (int i = 0; i < fClicks.count(); i++) {
335 if (owner == fClicks[i]->fOwner) {
336 index = i;
337 break;
338 }
339 }
340
341 switch (state) {
342 case Click::kDown_State: {
343 if (index != -1) {
344 delete fClicks[index];
345 fClicks.remove(index);
346 }
347 Click* click = this->findClickHandler(SkIntToScalar(x),
348 SkIntToScalar(y), modifierKeys);
349
350 if (click) {
351 click->fOwner = owner;
352 *fClicks.append() = click;
353 SkView::DoClickDown(click, x, y, modifierKeys);
354 handled = true;
355 }
356 break;
357 }
358 case Click::kMoved_State:
359 if (index != -1) {
360 SkView::DoClickMoved(fClicks[index], x, y, modifierKeys);
361 handled = true;
362 }
363 break;
364 case Click::kUp_State:
365 if (index != -1) {
366 SkView::DoClickUp(fClicks[index], x, y, modifierKeys);
367 delete fClicks[index];
368 fClicks.remove(index);
369 handled = true;
370 }
371 break;
372 default:
373 // Do nothing
374 break;
375 }
376 return handled;
377 }

mercurial