1/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "OverView.h"
9
10#include "SampleCode.h"
11
12#include "SkCanvas.h"
13#include "SkView.h"
14
15static const int N = 8;
16static const SkScalar kWidth = SkIntToScalar(640);
17static const SkScalar kHeight = SkIntToScalar(480);
18static const char gIsOverview[] = "is-overview";
19
20class OverView : public SkView {
21public:
22    OverView(int count, const SkViewFactory* factories[]);
23    virtual ~OverView();
24
25protected:
26    // Overridden from SkEventSink:
27    virtual bool onEvent(const SkEvent&) SK_OVERRIDE;
28    virtual bool onQuery(SkEvent* evt) SK_OVERRIDE {
29        if (SampleCode::TitleQ(*evt)) {
30            SampleCode::TitleR(evt, "Overview");
31            return true;
32        }
33        if (evt->isType(gIsOverview)) {
34            return true;
35        }
36        return this->INHERITED::onQuery(evt);
37    }
38
39
40    // Overridden from SkView:
41    virtual void onSizeChange() SK_OVERRIDE;
42    virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE {
43        canvas->drawColor(SK_ColorLTGRAY);
44    }
45
46    virtual SkCanvas* beforeChildren(SkCanvas*) SK_OVERRIDE;
47
48    virtual bool onSendClickToChildren(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
49        return false;
50    }
51
52    virtual Click* onFindClickHandler(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
53        int ix = (int)(SkScalarDiv(x * N, kWidth));
54        int iy = (int)(SkScalarDiv(y * N, kHeight));
55        if (ix >= 0 && iy >= 0) {
56            SkEvent evt("set-curr-index");
57            evt.setFast32(iy * N + ix);
58            this->sendEventToParents(evt);
59        }
60        return NULL;
61    }
62
63private:
64    int fCount;
65    const SkViewFactory** fFactories;
66
67    typedef SkView INHERITED;
68};
69
70SkView* create_overview(int count, const SkViewFactory* factories[]) {
71    return SkNEW_ARGS(OverView, (count, factories));
72}
73
74bool is_overview(SkView* view) {
75    SkEvent isOverview(gIsOverview);
76    return view->doQuery(&isOverview);
77}
78
79OverView::OverView(int count, const SkViewFactory* factories[]) {
80    fCount = count;
81    fFactories = factories;
82}
83
84OverView::~OverView() {
85}
86
87bool OverView::onEvent(const SkEvent& evt) {
88    return this->INHERITED::onEvent(evt);
89}
90
91void OverView::onSizeChange() {
92    this->detachAllChildren();
93
94    SkScalar locX = 0;
95    SkScalar locY = 0;
96    for (int i = 0; i < fCount; i++) {
97        SkView* view = (*fFactories[i])();
98        view->setVisibleP(true);
99        this->attachChildToBack(view)->unref();
100        view->setLoc(locX, locY);
101        view->setSize(kWidth, kHeight);
102        locX += kWidth;
103        if ((i % N) == N - 1) {
104            locY += kHeight;
105            locX = 0;
106        }
107    }
108}
109
110SkCanvas* OverView::beforeChildren(SkCanvas* canvas) {
111    canvas->scale(SK_Scalar1 / N, SK_Scalar1 / N);
112    return canvas;
113}
114