3 * Copyright 2011 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
12 #include "SkSystemEventTypes.h"
15 #define SK_EventDelayInval "\xd" "n" "\xa" "l"
19 #include "SkBounder.h"
20 class TestSkBounder : public SkBounder {
22 explicit TestSkBounder(const SkBitmap& bm) : fCanvas(bm) {}
25 virtual bool onIRect(const SkIRect& r) SK_OVERRIDE {
28 rr.set(SkIntToScalar(r.fLeft), SkIntToScalar(r.fTop),
29 SkIntToScalar(r.fRight), SkIntToScalar(r.fBottom));
33 p.setStyle(SkPaint::kStroke_Style);
34 p.setColor(SK_ColorYELLOW);
37 rr.inset(SK_ScalarHalf, SK_ScalarHalf);
39 rr.inset(-SK_ScalarHalf, -SK_ScalarHalf);
42 fCanvas.drawRect(rr, p);
49 SkWindow::SkWindow() : fFocusView(NULL) {
51 fWaitingOnInval = false;
53 #ifdef SK_BUILD_FOR_WINCE
54 fColorType = kRGB_565_SkColorType;
56 fColorType = kN32_SkColorType;
62 SkWindow::~SkWindow() {
67 SkCanvas* SkWindow::createCanvas() {
68 return new SkCanvas(this->getBitmap());
71 void SkWindow::setMatrix(const SkMatrix& matrix) {
72 if (fMatrix != matrix) {
78 void SkWindow::preConcat(const SkMatrix& matrix) {
80 m.setConcat(fMatrix, matrix);
84 void SkWindow::postConcat(const SkMatrix& matrix) {
86 m.setConcat(matrix, fMatrix);
90 void SkWindow::setColorType(SkColorType ct) {
91 this->resize(fBitmap.width(), fBitmap.height(), ct);
94 void SkWindow::resize(int width, int height, SkColorType ct) {
95 if (ct == kUnknown_SkColorType)
98 if (width != fBitmap.width() || height != fBitmap.height() || ct != fColorType) {
100 fBitmap.allocPixels(SkImageInfo::Make(width, height,
101 ct, kPremul_SkAlphaType));
103 this->setSize(SkIntToScalar(width), SkIntToScalar(height));
108 bool SkWindow::handleInval(const SkRect* localR) {
114 if (!fMatrix.invert(&inverse)) {
117 fMatrix.mapRect(&devR, *localR);
121 SkScalarRoundToInt(this->width()),
122 SkScalarRoundToInt(this->height()));
124 fDirtyRgn.op(ir, SkRegion::kUnion_Op);
126 this->onHandleInval(ir);
130 void SkWindow::forceInvalAll() {
131 fDirtyRgn.setRect(0, 0,
132 SkScalarCeilToInt(this->width()),
133 SkScalarCeilToInt(this->height()));
136 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
139 extern GXDisplayProperties gDisplayProps;
142 #ifdef SK_SIMULATE_FAILED_MALLOC
143 extern bool gEnableControlledThrow;
146 bool SkWindow::update(SkIRect* updateArea) {
147 if (!fDirtyRgn.isEmpty()) {
148 SkBitmap bm = this->getBitmap();
150 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
151 char* buffer = (char*)GXBeginDraw();
155 GetWindowRect((HWND)((SkOSWindow*)this)->getHWND(), &rect);
156 buffer += rect.top * gDisplayProps.cbyPitch + rect.left * gDisplayProps.cbxPitch;
158 bm.setPixels(buffer);
161 SkAutoTUnref<SkCanvas> canvas(this->createCanvas());
163 canvas->clipRegion(fDirtyRgn);
165 *updateArea = fDirtyRgn.getBounds();
167 SkAutoCanvasRestore acr(canvas, true);
168 canvas->concat(fMatrix);
170 // empty this now, so we can correctly record any inval calls that
171 // might be made during the draw call.
172 fDirtyRgn.setEmpty();
175 TestSkBounder bounder(bm);
176 canvas->setBounder(&bounder);
178 #ifdef SK_SIMULATE_FAILED_MALLOC
179 gEnableControlledThrow = true;
181 #ifdef SK_BUILD_FOR_WIN32
190 #ifdef SK_SIMULATE_FAILED_MALLOC
191 gEnableControlledThrow = false;
194 canvas->setBounder(NULL);
197 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
206 bool SkWindow::handleChar(SkUnichar uni) {
207 if (this->onHandleChar(uni))
210 SkView* focus = this->getFocusView();
214 SkEvent evt(SK_EventType_Unichar);
216 return focus->doEvent(evt);
219 bool SkWindow::handleKey(SkKey key) {
220 if (key == kNONE_SkKey)
223 if (this->onHandleKey(key))
226 // send an event to the focus-view
228 SkView* focus = this->getFocusView();
232 SkEvent evt(SK_EventType_Key);
234 if (focus->doEvent(evt))
238 if (key == kUp_SkKey || key == kDown_SkKey) {
239 if (this->moveFocus(key == kUp_SkKey ? kPrev_FocusDirection : kNext_FocusDirection) == NULL)
240 this->onSetFocusView(NULL);
246 bool SkWindow::handleKeyUp(SkKey key) {
247 if (key == kNONE_SkKey)
250 if (this->onHandleKeyUp(key))
253 //send an event to the focus-view
255 SkView* focus = this->getFocusView();
259 //should this one be the same?
260 SkEvent evt(SK_EventType_KeyUp);
262 if (focus->doEvent(evt))
268 void SkWindow::addMenu(SkOSMenu* menu) {
269 *fMenus.append() = menu;
270 this->onAddMenu(menu);
273 void SkWindow::setTitle(const char title[]) {
278 this->onSetTitle(title);
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;
288 return this->INHERITED::onEvent(evt);
291 bool SkWindow::onGetFocusView(SkView** focus) const {
297 bool SkWindow::onSetFocusView(SkView* focus) {
298 if (fFocusView != focus) {
300 fFocusView->onFocusChange(false);
303 focus->onFocusChange(true);
308 void SkWindow::onHandleInval(const SkIRect&) {
311 bool SkWindow::onHandleChar(SkUnichar) {
315 bool SkWindow::onHandleKey(SkKey) {
319 bool SkWindow::onHandleKeyUp(SkKey) {
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);
328 bool SkWindow::onDispatchClick(int x, int y, Click::State state,
329 void* owner, unsigned modifierKeys) {
330 bool handled = false;
332 // First, attempt to find an existing click with this owner.
334 for (int i = 0; i < fClicks.count(); i++) {
335 if (owner == fClicks[i]->fOwner) {
342 case Click::kDown_State: {
344 delete fClicks[index];
345 fClicks.remove(index);
347 Click* click = this->findClickHandler(SkIntToScalar(x),
348 SkIntToScalar(y), modifierKeys);
351 click->fOwner = owner;
352 *fClicks.append() = click;
353 SkView::DoClickDown(click, x, y, modifierKeys);
358 case Click::kMoved_State:
360 SkView::DoClickMoved(fClicks[index], x, y, modifierKeys);
364 case Click::kUp_State:
366 SkView::DoClickUp(fClicks[index], x, y, modifierKeys);
367 delete fClicks[index];
368 fClicks.remove(index);