180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/*
380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc.
480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *
580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be
680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file.
780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */
880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkWindow.h"
980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkCanvas.h"
1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkDevice.h"
1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkOSMenu.h"
1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkSystemEventTypes.h"
1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkTime.h"
1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define SK_EventDelayInval "\xd" "n" "\xa" "l"
1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#define TEST_BOUNDERx
1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkBounder.h"
2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruclass test_bounder : public SkBounder {
2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querupublic:
2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    test_bounder(const SkBitmap& bm) : fCanvas(bm) {}
2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprotected:
2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    virtual bool onIRect(const SkIRect& r)
2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkRect    rr;
2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        rr.set(SkIntToScalar(r.fLeft), SkIntToScalar(r.fTop),
2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                SkIntToScalar(r.fRight), SkIntToScalar(r.fBottom));
3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkPaint    p;
3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        p.setStyle(SkPaint::kStroke_Style);
3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        p.setColor(SK_ColorYELLOW);
3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if 0
3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        rr.inset(SK_ScalarHalf, SK_ScalarHalf);
3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#else
3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        rr.inset(-SK_ScalarHalf, -SK_ScalarHalf);
4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fCanvas.drawRect(rr, p);
4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return true;
4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruprivate:
4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkCanvas    fCanvas;
4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru};
4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkWindow::SkWindow() : fFocusView(NULL)
5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fClicks.reset();
5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fWaitingOnInval = false;
5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef SK_BUILD_FOR_WINCE
5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fConfig = SkBitmap::kRGB_565_Config;
5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#else
5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fConfig = SkBitmap::kARGB_8888_Config;
5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fMatrix.reset();
6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkWindow::~SkWindow()
6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fClicks.deleteAll();
6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fMenus.deleteAll();
6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkCanvas* SkWindow::createCanvas() {
7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return new SkCanvas(this->getBitmap());
7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkWindow::setMatrix(const SkMatrix& matrix) {
7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (fMatrix != matrix) {
7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fMatrix = matrix;
7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->inval(NULL);
7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkWindow::preConcat(const SkMatrix& matrix) {
8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkMatrix m;
8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    m.setConcat(fMatrix, matrix);
8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->setMatrix(m);
8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkWindow::postConcat(const SkMatrix& matrix) {
8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkMatrix m;
8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    m.setConcat(matrix, fMatrix);
8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->setMatrix(m);
9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkWindow::setConfig(SkBitmap::Config config)
9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->resize(fBitmap.width(), fBitmap.height(), config);
9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkWindow::resize(int width, int height, SkBitmap::Config config)
9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (config == SkBitmap::kNo_Config)
10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        config = fConfig;
10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (width != fBitmap.width() || height != fBitmap.height() || config != fConfig)
10380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
10480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fConfig = config;
10580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fBitmap.setConfig(config, width, height);
10680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fBitmap.allocPixels();
10780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fBitmap.setIsOpaque(true);
10880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
10980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->setSize(SkIntToScalar(width), SkIntToScalar(height));
11080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->inval(NULL);
11180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
11280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
11380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkWindow::eraseARGB(U8CPU a, U8CPU r, U8CPU g, U8CPU b)
11580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
11680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fBitmap.eraseARGB(a, r, g, b);
11780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
11880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
11980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkWindow::eraseRGB(U8CPU r, U8CPU g, U8CPU b)
12080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
12180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fBitmap.eraseRGB(r, g, b);
12280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
12380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkWindow::handleInval(const SkRect* localR)
12580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
12680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkIRect    ir;
12780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
12880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (localR) {
12980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkRect devR;
13080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkMatrix inverse;
13180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (!fMatrix.invert(&inverse)) {
13280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            return false;
13380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
13480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fMatrix.mapRect(&devR, *localR);
13580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        devR.round(&ir);
13680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    } else {
13780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        ir.set(0, 0,
13880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru               SkScalarRound(this->width()),
13980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru               SkScalarRound(this->height()));
14080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
14180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fDirtyRgn.op(ir, SkRegion::kUnion_Op);
14280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->onHandleInval(ir);
14480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return true;
14580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
14680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
14780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkWindow::forceInvalAll() {
14880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fDirtyRgn.setRect(0, 0,
14980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                      SkScalarCeil(this->width()),
15080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                      SkScalarCeil(this->height()));
15180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
15280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
15480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    #include <windows.h>
15580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    #include <gx.h>
15680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    extern GXDisplayProperties gDisplayProps;
15780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
15880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
15980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef SK_SIMULATE_FAILED_MALLOC
16080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruextern bool gEnableControlledThrow;
16180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
16280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
16380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkWindow::update(SkIRect* updateArea)
16480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
16580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (!fDirtyRgn.isEmpty())
16680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
16780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkBitmap bm = this->getBitmap();
16880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
16980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
17080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        char* buffer = (char*)GXBeginDraw();
17180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkASSERT(buffer);
17280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
17380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        RECT    rect;
17480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GetWindowRect((HWND)((SkOSWindow*)this)->getHWND(), &rect);
17580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        buffer += rect.top * gDisplayProps.cbyPitch + rect.left * gDisplayProps.cbxPitch;
17680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
17780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        bm.setPixels(buffer);
17880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
17980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
18080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkAutoTUnref<SkCanvas> canvas(this->createCanvas());
18180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
18280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->clipRegion(fDirtyRgn);
18380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (updateArea)
18480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            *updateArea = fDirtyRgn.getBounds();
18580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
18680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkAutoCanvasRestore acr(canvas, true);
18780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->concat(fMatrix);
18880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
18980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // empty this now, so we can correctly record any inval calls that
19080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        // might be made during the draw call.
19180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fDirtyRgn.setEmpty();
19280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
19380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef TEST_BOUNDER
19480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        test_bounder    b(bm);
19580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->setBounder(&b);
19680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
19780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef SK_SIMULATE_FAILED_MALLOC
19880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        gEnableControlledThrow = true;
19980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
20080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef SK_BUILD_FOR_WIN32
20180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        //try {
20280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            this->draw(canvas);
20380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        //}
20480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        //catch (...) {
20580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        //}
20680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#else
20780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        this->draw(canvas);
20880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
20980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef SK_SIMULATE_FAILED_MALLOC
21080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        gEnableControlledThrow = false;
21180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
21280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#ifdef TEST_BOUNDER
21380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        canvas->setBounder(NULL);
21480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
21580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
21680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#if defined(SK_BUILD_FOR_WINCE) && defined(USE_GX_SCREEN)
21780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        GXEndDraw();
21880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#endif
21980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
22080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return true;
22180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
22280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return false;
22380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
22480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
22580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkWindow::handleChar(SkUnichar uni)
22680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
22780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (this->onHandleChar(uni))
22880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return true;
22980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
23080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkView* focus = this->getFocusView();
23180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (focus == NULL)
23280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        focus = this;
23380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
23480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    SkEvent evt(SK_EventType_Unichar);
23580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    evt.setFast32(uni);
23680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return focus->doEvent(evt);
23780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
23880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
23980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkWindow::handleKey(SkKey key)
24080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
24180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (key == kNONE_SkKey)
24280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return false;
24380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
24480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (this->onHandleKey(key))
24580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return true;
24680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
24780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // send an event to the focus-view
24880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
24980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkView* focus = this->getFocusView();
25080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (focus == NULL)
25180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            focus = this;
25280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
25380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkEvent evt(SK_EventType_Key);
25480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        evt.setFast32(key);
25580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (focus->doEvent(evt))
25680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            return true;
25780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
25880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
25980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (key == kUp_SkKey || key == kDown_SkKey)
26080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
26180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (this->moveFocus(key == kUp_SkKey ? kPrev_FocusDirection : kNext_FocusDirection) == NULL)
26280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            this->onSetFocusView(NULL);
26380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return true;
26480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
26580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return false;
26680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
26780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
26880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkWindow::handleKeyUp(SkKey key)
26980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
27080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (key == kNONE_SkKey)
27180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return false;
27280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
27380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (this->onHandleKeyUp(key))
27480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return true;
27580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
27680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    //send an event to the focus-view
27780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
27880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkView* focus = this->getFocusView();
27980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (focus == NULL)
28080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            focus = this;
28180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
28280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        //should this one be the same?
28380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkEvent evt(SK_EventType_KeyUp);
28480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        evt.setFast32(key);
28580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (focus->doEvent(evt))
28680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            return true;
28780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
28880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return false;
28980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
29080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
29180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkWindow::addMenu(SkOSMenu* menu) {
29280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    *fMenus.append() = menu;
29380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->onAddMenu(menu);
29480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
29580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
29680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkWindow::setTitle(const char title[]) {
29780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (NULL == title) {
29880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        title = "";
29980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
30080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    fTitle.set(title);
30180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    this->onSetTitle(title);
30280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
30380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
30480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//////////////////////////////////////////////////////////////////////
30580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
30680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkWindow::onEvent(const SkEvent& evt)
30780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
30880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (evt.isType(SK_EventDelayInval))
30980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
31080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        SkRegion::Iterator    iter(fDirtyRgn);
31180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
31280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        for (; !iter.done(); iter.next())
31380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            this->onHandleInval(iter.rect());
31480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fWaitingOnInval = false;
31580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        return true;
31680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
31780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return this->INHERITED::onEvent(evt);
31880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
31980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
32080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkWindow::onGetFocusView(SkView** focus) const
32180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
32280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (focus)
32380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        *focus = fFocusView;
32480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return true;
32580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
32680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
32780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkWindow::onSetFocusView(SkView* focus)
32880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
32980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    if (fFocusView != focus)
33080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    {
33180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (fFocusView)
33280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            fFocusView->onFocusChange(false);
33380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        fFocusView = focus;
33480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (focus)
33580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            focus->onFocusChange(true);
33680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
33780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return true;
33880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
33980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
34080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru//////////////////////////////////////////////////////////////////////
34180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
34280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkWindow::onHandleInval(const SkIRect&)
34380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
34480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
34580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
34680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkWindow::onHandleChar(SkUnichar)
34780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
34880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return false;
34980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
35080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
35180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkWindow::onHandleKey(SkKey key)
35280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
35380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return false;
35480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
35580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
35680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkWindow::onHandleKeyUp(SkKey key)
35780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru{
35880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return false;
35980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
36080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
361d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenbergerbool SkWindow::handleClick(int x, int y, Click::State state, void *owner,
362d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenberger                           unsigned modifierKeys) {
363d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenberger    return this->onDispatchClick(x, y, state, owner, modifierKeys);
36480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
36580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
36680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkWindow::onDispatchClick(int x, int y, Click::State state,
367d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenberger                               void* owner, unsigned modifierKeys) {
36880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    bool handled = false;
36980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
37080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    // First, attempt to find an existing click with this owner.
37180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    int index = -1;
37280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    for (int i = 0; i < fClicks.count(); i++) {
37380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        if (owner == fClicks[i]->fOwner) {
37480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            index = i;
37580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            break;
37680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
37780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
37880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
37980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    switch (state) {
38080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        case Click::kDown_State: {
38180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            if (index != -1) {
38280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                delete fClicks[index];
38380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                fClicks.remove(index);
38480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
38580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            Click* click = this->findClickHandler(SkIntToScalar(x),
386d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenberger                                                  SkIntToScalar(y), modifierKeys);
38780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru
38880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            if (click) {
38980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                click->fOwner = owner;
39080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                *fClicks.append() = click;
391d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenberger                SkView::DoClickDown(click, x, y, modifierKeys);
39280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                handled = true;
39380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
39480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            break;
39580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        }
39680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        case Click::kMoved_State:
39780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            if (index != -1) {
398d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenberger                SkView::DoClickMoved(fClicks[index], x, y, modifierKeys);
39980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                handled = true;
40080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
40180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            break;
40280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        case Click::kUp_State:
40380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            if (index != -1) {
404d686ac77c2c485c4a3302eda9c1de597a6f8c568Derek Sollenberger                SkView::DoClickUp(fClicks[index], x, y, modifierKeys);
40580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                delete fClicks[index];
40680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                fClicks.remove(index);
40780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru                handled = true;
40880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            }
40980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            break;
41080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru        default:
41180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            // Do nothing
41280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru            break;
41380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    }
41480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru    return handled;
41580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru}
416