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"
17 SkWindow::SkWindow() : fFocusView(NULL) {
19 fWaitingOnInval = false;
21 #ifdef SK_BUILD_FOR_WINCE
22 fColorType = kRGB_565_SkColorType;
24 fColorType = kN32_SkColorType;
30 SkWindow::~SkWindow() {
35 SkCanvas* SkWindow::createCanvas() {
36 return new SkCanvas(this->getBitmap());
39 void SkWindow::setMatrix(const SkMatrix& matrix) {
40 if (fMatrix != matrix) {
46 void SkWindow::preConcat(const SkMatrix& matrix) {
48 m.setConcat(fMatrix, matrix);
52 void SkWindow::postConcat(const SkMatrix& matrix) {
54 m.setConcat(matrix, fMatrix);
58 void SkWindow::setColorType(SkColorType ct) {
59 this->resize(fBitmap.width(), fBitmap.height(), ct);
62 void SkWindow::resize(int width, int height, SkColorType ct) {
63 if (ct == kUnknown_SkColorType)
66 if (width != fBitmap.width() || height != fBitmap.height() || ct != fColorType) {
68 fBitmap.allocPixels(SkImageInfo::Make(width, height,
69 ct, kPremul_SkAlphaType));
71 this->setSize(SkIntToScalar(width), SkIntToScalar(height));
76 bool SkWindow::handleInval(const SkRect* localR) {
82 if (!fMatrix.invert(&inverse)) {
85 fMatrix.mapRect(&devR, *localR);
89 SkScalarRoundToInt(this->width()),
90 SkScalarRoundToInt(this->height()));
92 fDirtyRgn.op(ir, SkRegion::kUnion_Op);
94 this->onHandleInval(ir);
98 void SkWindow::forceInvalAll() {
99 fDirtyRgn.setRect(0, 0,
100 SkScalarCeilToInt(this->width()),
101 SkScalarCeilToInt(this->height()));
104 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
107 extern GXDisplayProperties gDisplayProps;
110 #ifdef SK_SIMULATE_FAILED_MALLOC
111 extern bool gEnableControlledThrow;
114 bool SkWindow::update(SkIRect* updateArea) {
115 if (!fDirtyRgn.isEmpty()) {
116 SkBitmap bm = this->getBitmap();
118 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
119 char* buffer = (char*)GXBeginDraw();
123 GetWindowRect((HWND)((SkOSWindow*)this)->getHWND(), &rect);
124 buffer += rect.top * gDisplayProps.cbyPitch + rect.left * gDisplayProps.cbxPitch;
126 bm.setPixels(buffer);
129 SkAutoTUnref<SkCanvas> canvas(this->createCanvas());
131 canvas->clipRegion(fDirtyRgn);
133 *updateArea = fDirtyRgn.getBounds();
135 SkAutoCanvasRestore acr(canvas, true);
136 canvas->concat(fMatrix);
138 // empty this now, so we can correctly record any inval calls that
139 // might be made during the draw call.
140 fDirtyRgn.setEmpty();
142 #ifdef SK_SIMULATE_FAILED_MALLOC
143 gEnableControlledThrow = true;
145 #ifdef SK_BUILD_FOR_WIN32
154 #ifdef SK_SIMULATE_FAILED_MALLOC
155 gEnableControlledThrow = false;
158 #if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
167 bool SkWindow::handleChar(SkUnichar uni) {
168 if (this->onHandleChar(uni))
171 SkView* focus = this->getFocusView();
175 SkEvent evt(SK_EventType_Unichar);
177 return focus->doEvent(evt);
180 bool SkWindow::handleKey(SkKey key) {
181 if (key == kNONE_SkKey)
184 if (this->onHandleKey(key))
187 // send an event to the focus-view
189 SkView* focus = this->getFocusView();
193 SkEvent evt(SK_EventType_Key);
195 if (focus->doEvent(evt))
199 if (key == kUp_SkKey || key == kDown_SkKey) {
200 if (this->moveFocus(key == kUp_SkKey ? kPrev_FocusDirection : kNext_FocusDirection) == NULL)
201 this->onSetFocusView(NULL);
207 bool SkWindow::handleKeyUp(SkKey key) {
208 if (key == kNONE_SkKey)
211 if (this->onHandleKeyUp(key))
214 //send an event to the focus-view
216 SkView* focus = this->getFocusView();
220 //should this one be the same?
221 SkEvent evt(SK_EventType_KeyUp);
223 if (focus->doEvent(evt))
229 void SkWindow::addMenu(SkOSMenu* menu) {
230 *fMenus.append() = menu;
231 this->onAddMenu(menu);
234 void SkWindow::setTitle(const char title[]) {
239 this->onSetTitle(title);
242 bool SkWindow::onEvent(const SkEvent& evt) {
243 if (evt.isType(SK_EventDelayInval)) {
244 for (SkRegion::Iterator iter(fDirtyRgn); !iter.done(); iter.next())
245 this->onHandleInval(iter.rect());
246 fWaitingOnInval = false;
249 return this->INHERITED::onEvent(evt);
252 bool SkWindow::onGetFocusView(SkView** focus) const {
258 bool SkWindow::onSetFocusView(SkView* focus) {
259 if (fFocusView != focus) {
261 fFocusView->onFocusChange(false);
264 focus->onFocusChange(true);
269 void SkWindow::onHandleInval(const SkIRect&) {
272 bool SkWindow::onHandleChar(SkUnichar) {
276 bool SkWindow::onHandleKey(SkKey) {
280 bool SkWindow::onHandleKeyUp(SkKey) {
284 bool SkWindow::handleClick(int x, int y, Click::State state, void *owner,
285 unsigned modifierKeys) {
286 return this->onDispatchClick(x, y, state, owner, modifierKeys);
289 bool SkWindow::onDispatchClick(int x, int y, Click::State state,
290 void* owner, unsigned modifierKeys) {
291 bool handled = false;
293 // First, attempt to find an existing click with this owner.
295 for (int i = 0; i < fClicks.count(); i++) {
296 if (owner == fClicks[i]->fOwner) {
303 case Click::kDown_State: {
305 delete fClicks[index];
306 fClicks.remove(index);
308 Click* click = this->findClickHandler(SkIntToScalar(x),
309 SkIntToScalar(y), modifierKeys);
312 click->fOwner = owner;
313 *fClicks.append() = click;
314 SkView::DoClickDown(click, x, y, modifierKeys);
319 case Click::kMoved_State:
321 SkView::DoClickMoved(fClicks[index], x, y, modifierKeys);
325 case Click::kUp_State:
327 SkView::DoClickUp(fClicks[index], x, y, modifierKeys);
328 delete fClicks[index];
329 fClicks.remove(index);