xref: /haiku/src/tests/kits/interface/ScrollViewTest.cpp (revision 68ea01249e1e2088933cb12f9c28d4e5c5d1c9ef)
1 /*
2 ** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3 ** Distributed under the terms of the MIT License.
4 */
5 
6 
7 #include <ScrollView.h>
8 
9 #include <Application.h>
10 #include <Window.h>
11 #include <Box.h>
12 
13 #include <stdio.h>
14 
15 
16 //#define ARCHIVE_TEST
17 //#define CHANGE_BORDER_TEST
18 //#define SIZE_TEST
19 
20 uint32 gNumWindows = 0;
21 
22 
23 class Window : public BWindow {
24 	public:
25 		Window();
26 
27 		virtual bool QuitRequested();
28 };
29 
30 
31 Window::Window()
32 	: BWindow(BRect(100, 100, 580, 580), "Scroll-Test",
33 			B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
34 {
35 	const bool horiz[] = {false, true, false, true};
36 	const bool vert[] = {false, false, true, true};
37 	const border_style border[] = {B_NO_BORDER, B_PLAIN_BORDER, B_FANCY_BORDER, B_FANCY_BORDER};
38 	BRect rect(1, 1, 119, 119);
39 	BRect frame = rect.InsetByCopy(20, 20);
40 
41 	for (int32 i = 0; i < 16; i++) {
42 		if (i > 0 && (i % 4) == 0)
43 			rect.OffsetBy(-480, 120);
44 
45 		int32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP;
46 		if (i % 4 == 3) {
47 			if (i == 15)
48 				resizingMode = B_FOLLOW_ALL;
49 			else
50 				resizingMode = B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP;
51 		} else if (i > 11)
52 			resizingMode = B_FOLLOW_TOP_BOTTOM | B_FOLLOW_LEFT;
53 
54 		BBox *box = new BBox(rect, "box", resizingMode);
55 		AddChild(box);
56 
57 		BView *view = new BView(frame, "main view", B_FOLLOW_ALL, B_WILL_DRAW);
58 		view->SetViewColor(200, 200, 21);
59 		BView *inner = new BView(frame.OffsetToCopy(B_ORIGIN).InsetBySelf(3, 3),
60 								"inner", B_FOLLOW_ALL, B_WILL_DRAW);
61 		inner->SetViewColor(200, 42, 42);
62 		view->AddChild(inner);
63 
64 		BScrollView *scroller = new BScrollView("scroller", view, resizingMode,
65 			0, horiz[i % 4], vert[i % 4], border[i / 4]);
66 		if (i >= 12)
67 			scroller->SetBorderHighlighted(true);
68 		box->AddChild(scroller);
69 
70 #ifdef ARCHIVE_TEST
71 		if (i == 7) {
72 			BMessage archive;
73 			if (scroller->Archive(&archive/*, false*/) == B_OK) {
74 				puts("BScrollView archived:");
75 				archive.PrintToStream();
76 			} else
77 				puts("archiving failed!");
78 
79 			BScrollView *second = (BScrollView *)BScrollView::Instantiate(&archive);
80 			archive.MakeEmpty();
81 			if (second != NULL && second->Archive(&archive) == B_OK) {
82 				puts("2. BScrollView archived:");
83 				archive.PrintToStream();
84 			}
85 		}
86 #endif
87 
88 #ifdef CHANGE_BORDER_TEST
89 		if (i % 4 == 1)
90 			scroller->SetBorder(B_FANCY_BORDER);
91 		else if (i % 4 == 2)
92 			scroller->SetBorder(B_PLAIN_BORDER);
93 		else if (i % 4 == 3)
94 			scroller->SetBorder(B_NO_BORDER);
95 #endif
96 
97 		rect.OffsetBy(120, 0);
98 	}
99 
100 #ifdef SIZE_TEST
101 	printf("sizeof = %lu (R5 == 212)\n", sizeof(BScrollView));
102 #endif
103 
104 	gNumWindows++;
105 }
106 
107 
108 bool
109 Window::QuitRequested()
110 {
111 	if (--gNumWindows <= 0)
112 		be_app->PostMessage(B_QUIT_REQUESTED);
113 	return true;
114 }
115 
116 
117 //	#pragma mark -
118 
119 
120 class KnobWindow : public BWindow {
121 	public:
122 		KnobWindow(bool horiz, bool vert);
123 
124 		virtual bool QuitRequested();
125 };
126 
127 
128 KnobWindow::KnobWindow(bool horiz, bool vert)
129 	: BWindow(BRect(200, 200, 400, 400), "Scroll Knob",
130 			B_DOCUMENT_WINDOW, B_ASYNCHRONOUS_CONTROLS)
131 {
132 	BRect frame = Bounds();
133 
134 	if (horiz) {
135 		MoveBy(50, 50);
136 		frame.bottom -= B_H_SCROLL_BAR_HEIGHT;
137 	}
138 	if (vert) {
139 		MoveBy(100, 100);
140 		frame.right -= B_V_SCROLL_BAR_WIDTH;
141 	}
142 
143 	BView *view = new BView(frame, "main view", B_FOLLOW_ALL, B_WILL_DRAW);
144 	BScrollView *scroller = new BScrollView("scroller", view, B_FOLLOW_ALL, 0, horiz, vert);
145 	AddChild(scroller);
146 
147 	// check the SetTarget() functionality
148 
149 	BView *outer = new BView(frame, "main view", B_FOLLOW_ALL, B_WILL_DRAW);
150 	outer->SetViewColor(200, 200, 21);
151 	BView *inner = new BView(frame.OffsetToCopy(B_ORIGIN).InsetBySelf(3, 3),
152 						"inner", B_FOLLOW_ALL, B_WILL_DRAW);
153 	inner->SetViewColor(200, 42, 42);
154 	outer->AddChild(inner);
155 
156 	scroller->RemoveChild(view);
157 		// works with and without it
158 
159 	scroller->SetTarget(NULL);
160 	scroller->SetTarget(outer);
161 	delete view;
162 
163 	gNumWindows++;
164 }
165 
166 
167 bool
168 KnobWindow::QuitRequested()
169 {
170 	if (--gNumWindows <= 0)
171 		be_app->PostMessage(B_QUIT_REQUESTED);
172 	return true;
173 }
174 
175 
176 //	#pragma mark -
177 
178 
179 class Application : public BApplication {
180 	public:
181 		Application();
182 
183 		virtual void ReadyToRun(void);
184 };
185 
186 
187 Application::Application()
188 	: BApplication("application/x-vnd.obos-test")
189 {
190 }
191 
192 
193 void
194 Application::ReadyToRun(void)
195 {
196 	BWindow *window = new Window();
197 	window->Show();
198 
199 	window = new KnobWindow(true, false);
200 	window->Show();
201 
202 	window = new KnobWindow(false, true);
203 	window->Show();
204 
205 	window = new KnobWindow(true, true);
206 	window->Show();
207 }
208 
209 
210 //	#pragma mark -
211 
212 
213 int
214 main(int argc, char **argv)
215 {
216 	Application app;
217 
218 	app.Run();
219 	return 0;
220 }
221 
222