blob: f8f52a2f71867200819c3f96807bf983f22830d7 [file] [log] [blame]
reed@android.com0bb6d062010-05-17 14:50:04 +00001#include "SampleCode.h"
2#include "SkView.h"
3#include "SkBlurMaskFilter.h"
4#include "SkCanvas.h"
5#include "SkGradientShader.h"
6#include "SkGraphics.h"
7#include "SkImageDecoder.h"
8#include "SkPath.h"
9#include "SkRandom.h"
10#include "SkRegion.h"
11#include "SkShader.h"
12#include "SkUtils.h"
13#include "SkXfermode.h"
14#include "SkColorPriv.h"
15#include "SkColorFilter.h"
16#include "SkTime.h"
17#include "SkTypeface.h"
18#include "SkTextBox.h"
19#include "SkOSFile.h"
20#include "SkStream.h"
21#include "SkKey.h"
22
23static const char gText[] =
24 "When in the Course of human events it becomes necessary for one people "
25 "to dissolve the political bands which have connected them with another "
26 "and to assume among the powers of the earth, the separate and equal "
27 "station to which the Laws of Nature and of Nature's God entitle them, "
28 "a decent respect to the opinions of mankind requires that they should "
29 "declare the causes which impel them to the separation.";
30
31class TextBoxView : public SkView {
32public:
33 TextBoxView() {
34 fTextSize = SkIntToScalar(32);
35 }
36
37protected:
38 // overrides from SkEventSink
39 virtual bool onQuery(SkEvent* evt) {
40 if (SampleCode::TitleQ(*evt)) {
41 SkString str("TextBox");
42 SampleCode::TitleR(evt, str.c_str());
43 return true;
44 }
45 return this->INHERITED::onQuery(evt);
46 }
47
48 void drawBG(SkCanvas* canvas) {
49 canvas->drawColor(SK_ColorWHITE);
50 }
51
52 virtual void onDraw(SkCanvas* canvas) {
53 this->drawBG(canvas);
54
55 SkScalar margin = 20;
56 SkTextBox tbox;
57 tbox.setMode(SkTextBox::kLineBreak_Mode);
58 tbox.setBox(margin, margin,
59 this->width() - margin, this->height() - margin);
60 tbox.setSpacing(SkIntToScalar(3)/3, 0);
61
62 SkPaint paint;
63 paint.setAntiAlias(true);
64 paint.setTextSize(fTextSize);
65
66 tbox.draw(canvas, gText, strlen(gText), paint);
67 }
68
69 virtual SkView::Click* onFindClickHandler(SkScalar x, SkScalar y) {
70 return new Click(this);
71 }
72
73 virtual bool onClick(Click* click) {
74 const SkScalar delta = SkIntToScalar(3);
75 if (click->fState == Click::kUp_State) {
76 if (click->fCurr.fY < this->height()/2) {
77 fTextSize += delta;
78 this->inval(NULL);
79 return true;
80 } else {
81 if (fTextSize > delta) {
82 fTextSize -= delta;
83 this->inval(NULL);
84 return true;
85 }
86 }
87 }
88 return this->INHERITED::onClick(click);
89 }
90
91private:
92 SkScalar fTextSize;
93
94 typedef SkView INHERITED;
95};
96
97//////////////////////////////////////////////////////////////////////////////
98
99static SkView* MyFactory() { return new TextBoxView; }
100static SkViewRegister reg(MyFactory);
101