2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
9 #include "SampleCode.h"
13 static const char gIsOverview[] = "is-overview";
15 static int to_lower(int c) {
16 if ('A' <= c && c <= 'Z') {
22 static void make_lc(SkString* str) {
23 char* ptr = str->writable_str();
25 *ptr = to_lower(*ptr);
30 static bool case_insensitive_find(const SkString& base, const SkString& sub) {
31 SkString lcBase(base);
35 return lcBase.find(lcSub.c_str()) >= 0;
38 static bool draw_this_name(const SkString& name, const SkString& filter) {
39 if (filter.isEmpty()) {
42 return case_insensitive_find(name, filter);
45 class OverView : public SkView {
47 OverView(int count, const SkViewFactory* factories[]);
51 bool onEvent(const SkEvent&) SK_OVERRIDE;
52 bool onQuery(SkEvent* evt) SK_OVERRIDE {
53 if (SampleCode::TitleQ(*evt)) {
54 SampleCode::TitleR(evt, "Overview");
57 if (evt->isType(gIsOverview)) {
61 if (SampleCode::CharQ(*evt, &uni)) {
62 fMatchStr.appendUnichar(uni);
66 return this->INHERITED::onQuery(evt);
69 void onDraw(SkCanvas* canvas) SK_OVERRIDE;
71 bool onSendClickToChildren(SkScalar x, SkScalar y, unsigned modi) SK_OVERRIDE {
75 Click* onFindClickHandler(SkScalar cx, SkScalar cy, unsigned modi) SK_OVERRIDE {
76 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");
83 this->sendEventToParents(evt);
94 const SkViewFactory** fFactories;
98 SkPaint::FontMetrics fNameMetrics;
102 SkRect bounds(const SkPoint& loc) const {
103 return SkRect::MakeXYWH(loc.x(), loc.y() + fNameMetrics.fAscent, fNameW, fNameH);
106 SkPoint start() const {
107 return SkPoint::Make(10, -fNameMetrics.fTop);
110 void next(SkPoint* loc) const {
112 if (loc->fY > this->height() - fNameMetrics.fBottom) {
113 loc->fY = -fNameMetrics.fTop;
118 typedef SkView INHERITED;
121 SkView* create_overview(int count, const SkViewFactory* factories[]) {
122 return SkNEW_ARGS(OverView, (count, factories));
125 bool is_overview(SkView* view) {
126 SkEvent isOverview(gIsOverview);
127 return view->doQuery(&isOverview);
130 OverView::OverView(int count, const SkViewFactory* factories[]) {
132 fFactories = factories;
134 fNames = new SkString[count];
135 for (int i = 0; i < count; ++i) {
136 SkView* view = (*fFactories[i])();
138 (void)SampleCode::RequestTitle(view, &fNames[i]);
139 if (0 == fNames[i].find("GM:")) {
140 fNames[i].remove(0, 3);
145 fNamePaint.setAntiAlias(true);
146 fNamePaint.setTextSize(12);
148 fNameH = fNamePaint.getFontMetrics(&fNameMetrics);
151 OverView::~OverView() {
155 bool OverView::onEvent(const SkEvent& evt) {
156 return this->INHERITED::onEvent(evt);
159 void OverView::onDraw(SkCanvas* canvas) {
161 paint.setColor(0xFFF8F8F8);
162 canvas->drawPaint(paint);
164 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);