18afcf72aSAxel Dörfler /*
28afcf72aSAxel Dörfler ** Copyright 2004, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3*b6f76ebeSAugustin Cavalier ** Distributed under the terms of the MIT License.
48afcf72aSAxel Dörfler */
58afcf72aSAxel Dörfler
68afcf72aSAxel Dörfler
78afcf72aSAxel Dörfler #include <ScrollView.h>
88afcf72aSAxel Dörfler
98afcf72aSAxel Dörfler #include <Application.h>
108afcf72aSAxel Dörfler #include <Window.h>
118afcf72aSAxel Dörfler #include <Box.h>
128afcf72aSAxel Dörfler
13c81fdc88SAxel Dörfler #include <stdio.h>
14c81fdc88SAxel Dörfler
15c81fdc88SAxel Dörfler
16a8c4af5aSAxel Dörfler //#define ARCHIVE_TEST
17a8c4af5aSAxel Dörfler //#define CHANGE_BORDER_TEST
18a8c4af5aSAxel Dörfler //#define SIZE_TEST
19c81fdc88SAxel Dörfler
208c37ad55SAxel Dörfler uint32 gNumWindows = 0;
218c37ad55SAxel Dörfler
228afcf72aSAxel Dörfler
238afcf72aSAxel Dörfler class Window : public BWindow {
248afcf72aSAxel Dörfler public:
258afcf72aSAxel Dörfler Window();
268afcf72aSAxel Dörfler
278afcf72aSAxel Dörfler virtual bool QuitRequested();
288afcf72aSAxel Dörfler };
298afcf72aSAxel Dörfler
308afcf72aSAxel Dörfler
Window()318afcf72aSAxel Dörfler Window::Window()
328afcf72aSAxel Dörfler : BWindow(BRect(100, 100, 580, 580), "Scroll-Test",
33a8c4af5aSAxel Dörfler B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
348afcf72aSAxel Dörfler {
358afcf72aSAxel Dörfler const bool horiz[] = {false, true, false, true};
368afcf72aSAxel Dörfler const bool vert[] = {false, false, true, true};
378afcf72aSAxel Dörfler const border_style border[] = {B_NO_BORDER, B_PLAIN_BORDER, B_FANCY_BORDER, B_FANCY_BORDER};
388afcf72aSAxel Dörfler BRect rect(1, 1, 119, 119);
398afcf72aSAxel Dörfler BRect frame = rect.InsetByCopy(20, 20);
408afcf72aSAxel Dörfler
418afcf72aSAxel Dörfler for (int32 i = 0; i < 16; i++) {
428afcf72aSAxel Dörfler if (i > 0 && (i % 4) == 0)
438afcf72aSAxel Dörfler rect.OffsetBy(-480, 120);
448afcf72aSAxel Dörfler
45a8c4af5aSAxel Dörfler int32 resizingMode = B_FOLLOW_LEFT | B_FOLLOW_TOP;
46a8c4af5aSAxel Dörfler if (i % 4 == 3) {
47a8c4af5aSAxel Dörfler if (i == 15)
48a8c4af5aSAxel Dörfler resizingMode = B_FOLLOW_ALL;
49a8c4af5aSAxel Dörfler else
50a8c4af5aSAxel Dörfler resizingMode = B_FOLLOW_LEFT_RIGHT | B_FOLLOW_TOP;
51a8c4af5aSAxel Dörfler } else if (i > 11)
52a8c4af5aSAxel Dörfler resizingMode = B_FOLLOW_TOP_BOTTOM | B_FOLLOW_LEFT;
53a8c4af5aSAxel Dörfler
54a8c4af5aSAxel Dörfler BBox *box = new BBox(rect, "box", resizingMode);
558afcf72aSAxel Dörfler AddChild(box);
568afcf72aSAxel Dörfler
578afcf72aSAxel Dörfler BView *view = new BView(frame, "main view", B_FOLLOW_ALL, B_WILL_DRAW);
58a8c4af5aSAxel Dörfler view->SetViewColor(200, 200, 21);
59a8c4af5aSAxel Dörfler BView *inner = new BView(frame.OffsetToCopy(B_ORIGIN).InsetBySelf(3, 3),
608afcf72aSAxel Dörfler "inner", B_FOLLOW_ALL, B_WILL_DRAW);
618afcf72aSAxel Dörfler inner->SetViewColor(200, 42, 42);
628afcf72aSAxel Dörfler view->AddChild(inner);
638afcf72aSAxel Dörfler
64a8c4af5aSAxel Dörfler BScrollView *scroller = new BScrollView("scroller", view, resizingMode,
658afcf72aSAxel Dörfler 0, horiz[i % 4], vert[i % 4], border[i / 4]);
668afcf72aSAxel Dörfler if (i >= 12)
678afcf72aSAxel Dörfler scroller->SetBorderHighlighted(true);
688afcf72aSAxel Dörfler box->AddChild(scroller);
698afcf72aSAxel Dörfler
70a8c4af5aSAxel Dörfler #ifdef ARCHIVE_TEST
71c81fdc88SAxel Dörfler if (i == 7) {
72c81fdc88SAxel Dörfler BMessage archive;
73c81fdc88SAxel Dörfler if (scroller->Archive(&archive/*, false*/) == B_OK) {
74c81fdc88SAxel Dörfler puts("BScrollView archived:");
75c81fdc88SAxel Dörfler archive.PrintToStream();
76c81fdc88SAxel Dörfler } else
77c81fdc88SAxel Dörfler puts("archiving failed!");
78c81fdc88SAxel Dörfler
79c81fdc88SAxel Dörfler BScrollView *second = (BScrollView *)BScrollView::Instantiate(&archive);
80c81fdc88SAxel Dörfler archive.MakeEmpty();
81c81fdc88SAxel Dörfler if (second != NULL && second->Archive(&archive) == B_OK) {
82c81fdc88SAxel Dörfler puts("2. BScrollView archived:");
83c81fdc88SAxel Dörfler archive.PrintToStream();
84c81fdc88SAxel Dörfler }
85c81fdc88SAxel Dörfler }
86a8c4af5aSAxel Dörfler #endif
87c81fdc88SAxel Dörfler
88a8c4af5aSAxel Dörfler #ifdef CHANGE_BORDER_TEST
89a8c4af5aSAxel Dörfler if (i % 4 == 1)
90c81fdc88SAxel Dörfler scroller->SetBorder(B_FANCY_BORDER);
91a8c4af5aSAxel Dörfler else if (i % 4 == 2)
92a8c4af5aSAxel Dörfler scroller->SetBorder(B_PLAIN_BORDER);
93a8c4af5aSAxel Dörfler else if (i % 4 == 3)
94a8c4af5aSAxel Dörfler scroller->SetBorder(B_NO_BORDER);
95c81fdc88SAxel Dörfler #endif
96c81fdc88SAxel Dörfler
978afcf72aSAxel Dörfler rect.OffsetBy(120, 0);
988afcf72aSAxel Dörfler }
99c81fdc88SAxel Dörfler
100a8c4af5aSAxel Dörfler #ifdef SIZE_TEST
101a8c4af5aSAxel Dörfler printf("sizeof = %lu (R5 == 212)\n", sizeof(BScrollView));
102c81fdc88SAxel Dörfler #endif
1038afcf72aSAxel Dörfler
1048c37ad55SAxel Dörfler gNumWindows++;
1058afcf72aSAxel Dörfler }
1068afcf72aSAxel Dörfler
1078afcf72aSAxel Dörfler
1088afcf72aSAxel Dörfler bool
QuitRequested()1098afcf72aSAxel Dörfler Window::QuitRequested()
1108afcf72aSAxel Dörfler {
1118c37ad55SAxel Dörfler if (--gNumWindows <= 0)
1128c37ad55SAxel Dörfler be_app->PostMessage(B_QUIT_REQUESTED);
1138c37ad55SAxel Dörfler return true;
1148c37ad55SAxel Dörfler }
1158c37ad55SAxel Dörfler
1168c37ad55SAxel Dörfler
1178c37ad55SAxel Dörfler // #pragma mark -
1188c37ad55SAxel Dörfler
1198c37ad55SAxel Dörfler
1208c37ad55SAxel Dörfler class KnobWindow : public BWindow {
1218c37ad55SAxel Dörfler public:
1228c37ad55SAxel Dörfler KnobWindow(bool horiz, bool vert);
1238c37ad55SAxel Dörfler
1248c37ad55SAxel Dörfler virtual bool QuitRequested();
1258c37ad55SAxel Dörfler };
1268c37ad55SAxel Dörfler
1278c37ad55SAxel Dörfler
KnobWindow(bool horiz,bool vert)1288c37ad55SAxel Dörfler KnobWindow::KnobWindow(bool horiz, bool vert)
1298c37ad55SAxel Dörfler : BWindow(BRect(200, 200, 400, 400), "Scroll Knob",
1308c37ad55SAxel Dörfler B_DOCUMENT_WINDOW, B_ASYNCHRONOUS_CONTROLS)
1318c37ad55SAxel Dörfler {
1328c37ad55SAxel Dörfler BRect frame = Bounds();
1338c37ad55SAxel Dörfler
1348c37ad55SAxel Dörfler if (horiz) {
1358c37ad55SAxel Dörfler MoveBy(50, 50);
1368c37ad55SAxel Dörfler frame.bottom -= B_H_SCROLL_BAR_HEIGHT;
1378c37ad55SAxel Dörfler }
1388c37ad55SAxel Dörfler if (vert) {
1398c37ad55SAxel Dörfler MoveBy(100, 100);
1408c37ad55SAxel Dörfler frame.right -= B_V_SCROLL_BAR_WIDTH;
1418c37ad55SAxel Dörfler }
1428c37ad55SAxel Dörfler
1438c37ad55SAxel Dörfler BView *view = new BView(frame, "main view", B_FOLLOW_ALL, B_WILL_DRAW);
144b14df7acSAxel Dörfler BScrollView *scroller = new BScrollView("scroller", view, B_FOLLOW_ALL, 0, horiz, vert);
145b14df7acSAxel Dörfler AddChild(scroller);
146b14df7acSAxel Dörfler
147b14df7acSAxel Dörfler // check the SetTarget() functionality
148b14df7acSAxel Dörfler
149b14df7acSAxel Dörfler BView *outer = new BView(frame, "main view", B_FOLLOW_ALL, B_WILL_DRAW);
150b14df7acSAxel Dörfler outer->SetViewColor(200, 200, 21);
1518c37ad55SAxel Dörfler BView *inner = new BView(frame.OffsetToCopy(B_ORIGIN).InsetBySelf(3, 3),
1528c37ad55SAxel Dörfler "inner", B_FOLLOW_ALL, B_WILL_DRAW);
1538c37ad55SAxel Dörfler inner->SetViewColor(200, 42, 42);
154b14df7acSAxel Dörfler outer->AddChild(inner);
1558c37ad55SAxel Dörfler
156b14df7acSAxel Dörfler scroller->RemoveChild(view);
157b14df7acSAxel Dörfler // works with and without it
158b14df7acSAxel Dörfler
159b14df7acSAxel Dörfler scroller->SetTarget(NULL);
160b14df7acSAxel Dörfler scroller->SetTarget(outer);
161b14df7acSAxel Dörfler delete view;
1628c37ad55SAxel Dörfler
1638c37ad55SAxel Dörfler gNumWindows++;
1648c37ad55SAxel Dörfler }
1658c37ad55SAxel Dörfler
1668c37ad55SAxel Dörfler
1678c37ad55SAxel Dörfler bool
QuitRequested()1688c37ad55SAxel Dörfler KnobWindow::QuitRequested()
1698c37ad55SAxel Dörfler {
1708c37ad55SAxel Dörfler if (--gNumWindows <= 0)
1718afcf72aSAxel Dörfler be_app->PostMessage(B_QUIT_REQUESTED);
1728afcf72aSAxel Dörfler return true;
1738afcf72aSAxel Dörfler }
1748afcf72aSAxel Dörfler
1758afcf72aSAxel Dörfler
1768afcf72aSAxel Dörfler // #pragma mark -
1778afcf72aSAxel Dörfler
1788afcf72aSAxel Dörfler
1798afcf72aSAxel Dörfler class Application : public BApplication {
1808afcf72aSAxel Dörfler public:
1818afcf72aSAxel Dörfler Application();
1828afcf72aSAxel Dörfler
1838afcf72aSAxel Dörfler virtual void ReadyToRun(void);
1848afcf72aSAxel Dörfler };
1858afcf72aSAxel Dörfler
1868afcf72aSAxel Dörfler
Application()1878afcf72aSAxel Dörfler Application::Application()
1888afcf72aSAxel Dörfler : BApplication("application/x-vnd.obos-test")
1898afcf72aSAxel Dörfler {
1908afcf72aSAxel Dörfler }
1918afcf72aSAxel Dörfler
1928afcf72aSAxel Dörfler
1938afcf72aSAxel Dörfler void
ReadyToRun(void)1948afcf72aSAxel Dörfler Application::ReadyToRun(void)
1958afcf72aSAxel Dörfler {
1968c37ad55SAxel Dörfler BWindow *window = new Window();
1978c37ad55SAxel Dörfler window->Show();
1988c37ad55SAxel Dörfler
1998c37ad55SAxel Dörfler window = new KnobWindow(true, false);
2008c37ad55SAxel Dörfler window->Show();
2018c37ad55SAxel Dörfler
2028c37ad55SAxel Dörfler window = new KnobWindow(false, true);
2038c37ad55SAxel Dörfler window->Show();
2048c37ad55SAxel Dörfler
2058c37ad55SAxel Dörfler window = new KnobWindow(true, true);
2068afcf72aSAxel Dörfler window->Show();
2078afcf72aSAxel Dörfler }
2088afcf72aSAxel Dörfler
2098afcf72aSAxel Dörfler
2108afcf72aSAxel Dörfler // #pragma mark -
2118afcf72aSAxel Dörfler
2128afcf72aSAxel Dörfler
2138afcf72aSAxel Dörfler int
main(int argc,char ** argv)2148afcf72aSAxel Dörfler main(int argc, char **argv)
2158afcf72aSAxel Dörfler {
2168afcf72aSAxel Dörfler Application app;
2178afcf72aSAxel Dörfler
2188afcf72aSAxel Dörfler app.Run();
2198afcf72aSAxel Dörfler return 0;
2208afcf72aSAxel Dörfler }
2218afcf72aSAxel Dörfler
222