blob: d7633452bc3c98e2864564e1ab59b14640ea7576 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
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 */
bsalomon@google.comab9e2c62012-03-08 16:20:16 +00007
tfarina@chromium.orgb1b7f7072012-09-18 01:52:20 +00008#include "OverView.h"
reed@android.com34245c72009-11-03 04:00:48 +00009#include "SampleCode.h"
10#include "SkCanvas.h"
11#include "SkView.h"
12
commit-bot@chromium.orgab131672013-09-13 12:39:55 +000013static const char gIsOverview[] = "is-overview";
bsalomon@google.comab9e2c62012-03-08 16:20:16 +000014
reedbf015c32015-02-03 15:02:24 -080015static int to_lower(int c) {
16 if ('A' <= c && c <= 'Z') {
17 c = c - 'A' + 'a';
18 }
19 return c;
20}
21
22static void make_lc(SkString* str) {
23 char* ptr = str->writable_str();
24 while (*ptr) {
25 *ptr = to_lower(*ptr);
26 ptr += 1;
27 }
28}
29
30static bool case_insensitive_find(const SkString& base, const SkString& sub) {
31 SkString lcBase(base);
32 SkString lcSub(sub);
33 make_lc(&lcBase);
34 make_lc(&lcSub);
35 return lcBase.find(lcSub.c_str()) >= 0;
36}
37
38static bool draw_this_name(const SkString& name, const SkString& filter) {
39 if (filter.isEmpty()) {
40 return true;
41 }
42 return case_insensitive_find(name, filter);
43}
44
reed@android.com34245c72009-11-03 04:00:48 +000045class OverView : public SkView {
46public:
bsalomon@google.com48dd1a22011-10-31 14:18:20 +000047 OverView(int count, const SkViewFactory* factories[]);
reed@android.com34245c72009-11-03 04:00:48 +000048 virtual ~OverView();
bsalomon@google.comab9e2c62012-03-08 16:20:16 +000049
reed@android.com34245c72009-11-03 04:00:48 +000050protected:
mtklein36352bf2015-03-25 18:17:31 -070051 bool onEvent(const SkEvent&) override;
52 bool onQuery(SkEvent* evt) override {
reed@android.com34245c72009-11-03 04:00:48 +000053 if (SampleCode::TitleQ(*evt)) {
54 SampleCode::TitleR(evt, "Overview");
55 return true;
56 }
yangsu@google.comef7bdfa2011-08-12 14:27:47 +000057 if (evt->isType(gIsOverview)) {
58 return true;
59 }
reedbf015c32015-02-03 15:02:24 -080060 SkUnichar uni;
61 if (SampleCode::CharQ(*evt, &uni)) {
62 fMatchStr.appendUnichar(uni);
halcanary96fcdcc2015-08-27 07:41:13 -070063 this->inval(nullptr);
reedbf015c32015-02-03 15:02:24 -080064 return true;
65 }
reed@android.com34245c72009-11-03 04:00:48 +000066 return this->INHERITED::onQuery(evt);
67 }
68
mtklein36352bf2015-03-25 18:17:31 -070069 void onDraw(SkCanvas* canvas) override;
bsalomon@google.comab9e2c62012-03-08 16:20:16 +000070
mtklein36352bf2015-03-25 18:17:31 -070071 bool onSendClickToChildren(SkScalar x, SkScalar y, unsigned modi) override {
reed@android.come72fee52009-11-16 14:52:01 +000072 return false;
73 }
74
mtklein36352bf2015-03-25 18:17:31 -070075 Click* onFindClickHandler(SkScalar cx, SkScalar cy, unsigned modi) override {
reedbf015c32015-02-03 15:02:24 -080076 const SkRect crect = SkRect::MakeXYWH(cx - 0.5f, cy - 0.5f, 1, 1);
77 SkPoint loc = this->start();
78 for (int i = 0; i < fCount; ++i) {
79 if (draw_this_name(fNames[i], fMatchStr)) {
80 if (this->bounds(loc).intersects(crect)) {
81 SkEvent evt("set-curr-index");
82 evt.setFast32(i);
83 this->sendEventToParents(evt);
84 break;
85 }
86 this->next(&loc);
87 }
reed@android.com34245c72009-11-03 04:00:48 +000088 }
halcanary96fcdcc2015-08-27 07:41:13 -070089 return nullptr;
reed@android.com34245c72009-11-03 04:00:48 +000090 }
91
92private:
bsalomon@google.comab9e2c62012-03-08 16:20:16 +000093 int fCount;
94 const SkViewFactory** fFactories;
reedbf015c32015-02-03 15:02:24 -080095 SkString* fNames;
96 SkString fMatchStr;
97 SkPaint fNamePaint;
98 SkPaint::FontMetrics fNameMetrics;
99 SkScalar fNameW;
100 SkScalar fNameH;
101
102 SkRect bounds(const SkPoint& loc) const {
103 return SkRect::MakeXYWH(loc.x(), loc.y() + fNameMetrics.fAscent, fNameW, fNameH);
104 }
105
106 SkPoint start() const {
107 return SkPoint::Make(10, -fNameMetrics.fTop);
108 }
109
110 void next(SkPoint* loc) const {
111 loc->fY += fNameH;
112 if (loc->fY > this->height() - fNameMetrics.fBottom) {
113 loc->fY = -fNameMetrics.fTop;
114 loc->fX += fNameW;
115 }
116 }
reed@android.com34245c72009-11-03 04:00:48 +0000117
118 typedef SkView INHERITED;
119};
120
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000121SkView* create_overview(int count, const SkViewFactory* factories[]) {
halcanary385fe4d2015-08-26 13:07:48 -0700122 return new OverView(count, factories);
bsalomon@google.comab9e2c62012-03-08 16:20:16 +0000123}
124
125bool is_overview(SkView* view) {
126 SkEvent isOverview(gIsOverview);
127 return view->doQuery(&isOverview);
128}
reed@android.com34245c72009-11-03 04:00:48 +0000129
bsalomon@google.com48dd1a22011-10-31 14:18:20 +0000130OverView::OverView(int count, const SkViewFactory* factories[]) {
reed@android.com34245c72009-11-03 04:00:48 +0000131 fCount = count;
132 fFactories = factories;
reedbf015c32015-02-03 15:02:24 -0800133
134 fNames = new SkString[count];
135 for (int i = 0; i < count; ++i) {
136 SkView* view = (*fFactories[i])();
137 if (view) {
138 (void)SampleCode::RequestTitle(view, &fNames[i]);
139 if (0 == fNames[i].find("GM:")) {
140 fNames[i].remove(0, 3);
141 }
142 }
143 }
144
145 fNamePaint.setAntiAlias(true);
146 fNamePaint.setTextSize(12);
147 fNameW = 160;
148 fNameH = fNamePaint.getFontMetrics(&fNameMetrics);
reed@android.com34245c72009-11-03 04:00:48 +0000149}
150
151OverView::~OverView() {
reedbf015c32015-02-03 15:02:24 -0800152 delete[] fNames;
reed@android.com34245c72009-11-03 04:00:48 +0000153}
154
155bool OverView::onEvent(const SkEvent& evt) {
156 return this->INHERITED::onEvent(evt);
157}
158
reedbf015c32015-02-03 15:02:24 -0800159void OverView::onDraw(SkCanvas* canvas) {
160 SkPaint paint;
161 paint.setColor(0xFFF8F8F8);
162 canvas->drawPaint(paint);
bsalomon@google.comab9e2c62012-03-08 16:20:16 +0000163
reedbf015c32015-02-03 15:02:24 -0800164 SkPoint loc = this->start();
165 for (int i = 0; i < fCount; ++i) {
166 if (draw_this_name(fNames[i], fMatchStr)) {
167 canvas->drawRect(this->bounds(loc), paint);
168 canvas->drawText(fNames[i].c_str(), fNames[i].size(), loc.x(), loc.y(), fNamePaint);
169 this->next(&loc);
reed@android.com34245c72009-11-03 04:00:48 +0000170 }
171 }
172}
173