1// main.cpp
2
3#include <stdio.h>
4
5#include "Application.h"
6#include "Message.h"
7#include "Button.h"
8#include "View.h"
9#include "Window.h"
10
11enum {
12	MSG_RESET = 'rset'
13};
14
15class TestView : public BView {
16
17 public:
18					TestView(BRect frame, const char* name,
19							  uint32 resizeFlags, uint32 flags)
20						: BView(frame, name, resizeFlags, flags),
21						  fTracking(false),
22						  fLastMousePos(0.0, 0.0)
23					{
24						SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
25						SetLowUIColor(ViewUIColor());
26					}
27
28	virtual	void	MessageReceived(BMessage* message);
29
30	virtual	void	Draw(BRect updateRect);
31
32	virtual	void	MouseDown(BPoint where);
33	virtual	void	MouseUp(BPoint where);
34	virtual	void	MouseMoved(BPoint where, uint32 transit,
35							   const BMessage* dragMessage);
36
37 private:
38			bool	fTracking;
39
40			BPoint	fLastMousePos;
41};
42
43// MessageReceived
44void
45TestView::MessageReceived(BMessage* message)
46{
47	if (message->what == MSG_RESET)
48		ScrollTo(0.0, 0.0);
49	else
50		BView::MessageReceived(message);
51}
52
53// Draw
54void
55TestView::Draw(BRect updateRect)
56{
57	// ellipses
58	SetHighColor(200, 90, 0, 100);
59	StrokeEllipse(BPoint(80.0, 50.0), 70.0, 40.0);
60
61	SetDrawingMode(B_OP_ALPHA);
62
63	SetPenSize(2.0);
64	SetHighColor(20, 40, 180, 150);
65	StrokeEllipse(BPoint(230.0, 90.0), 14.0, 60.0);
66
67	SetPenSize(5.0);
68	SetHighColor(60, 20, 110, 100);
69	StrokeEllipse(BPoint(100.0, 180.0), 35.0, 25.0);
70
71	// text
72	SetHighColor(0, 0, 0, 255);
73	SetDrawingMode(B_OP_OVER);
74	const char* message = "Click and drag to scroll this view!";
75	DrawString(message, BPoint(0.0, 30.0));
76}
77
78// MouseDown
79void
80TestView::MouseDown(BPoint where)
81{
82	fTracking = true;
83	fLastMousePos = where;
84	SetMouseEventMask(B_POINTER_EVENTS);
85}
86
87// MouseUp
88void
89TestView::MouseUp(BPoint where)
90{
91	fTracking = false;
92}
93
94// MouseMoved
95void
96TestView::MouseMoved(BPoint where, uint32 transit,
97					 const BMessage* dragMessage)
98{
99	if (fTracking) {
100		BPoint offset = fLastMousePos - where;
101		ScrollBy(offset.x, offset.y);
102		fLastMousePos = where + offset;
103	}
104}
105
106
107
108// show_window
109void
110show_window(BRect frame, const char* name)
111{
112	BWindow* window = new BWindow(frame, name,
113								  B_TITLED_WINDOW,
114								  B_ASYNCHRONOUS_CONTROLS | B_QUIT_ON_WINDOW_CLOSE);
115
116	BView* view = new TestView(window->Bounds(), "test", B_FOLLOW_ALL,
117							   B_WILL_DRAW/* | B_FULL_UPDATE_ON_RESIZE*/);
118
119	window->AddChild(view);
120	BRect b(0.0, 0.0, 60.0, 15.0);
121	b.OffsetTo(5.0, view->Bounds().bottom - (b.Height() + 15.0));
122	BButton* control = new BButton(b, "button", "Reset", new BMessage(MSG_RESET));
123	view->AddChild(control);
124	control->SetTarget(view);
125
126	window->Show();
127}
128
129// main
130int
131main(int argc, char** argv)
132{
133	BApplication* app = new BApplication("application/x.vnd-Haiku.Scrolling");
134
135	BRect frame(50.0, 50.0, 300.0, 250.0);
136	show_window(frame, "Scrolling Test");
137
138	app->Run();
139
140	delete app;
141	return 0;
142}
143