xref: /haiku/src/tests/kits/interface/StatusBarTest.cpp (revision 4bd0c1066b227cec4b79883bdef697c7a27f2e90)
1 /*
2  * Copyright 2006, Axel Dörfler, axeld@pinc-software.de. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 
7 #include <Application.h>
8 #include <MessageRunner.h>
9 #include <StatusBar.h>
10 #include <Window.h>
11 
12 #include <stdio.h>
13 
14 
15 const uint32 kMsgUpdate = 'updt';
16 
17 class Window : public BWindow {
18 	public:
19 		Window();
20 
21 		virtual void MessageReceived(BMessage* message);
22 		virtual bool QuitRequested();
23 
24 	private:
25 		BMessageRunner* fUpdater;
26 		BStatusBar*	fStatusBar;
27 };
28 
29 
30 Window::Window()
31 	: BWindow(BRect(100, 100, 520, 200), "StatusBar-Test",
32 			B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
33 {
34 	BView* main = new BView(Bounds(), NULL, B_FOLLOW_ALL, B_WILL_DRAW);
35 	main->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
36 	AddChild(main);
37 
38 	BRect rect(20, 10, 400, 30);
39 	fStatusBar = new BStatusBar(rect, NULL, "label", "trailing label");
40 	fStatusBar->SetResizingMode(B_FOLLOW_LEFT_RIGHT);
41 	//fStatusBar->ResizeToPreferred();
42 	float width, height;
43 	fStatusBar->GetPreferredSize(&width, &height);
44 	fStatusBar->ResizeTo(rect.Width(), height);
45 	fStatusBar->SetMaxValue(50.0f);
46 	main->AddChild(fStatusBar);
47 
48 	BMessage update(kMsgUpdate);
49 	fUpdater = new BMessageRunner(this, &update, 10000LL);
50 }
51 
52 
53 void
54 Window::MessageReceived(BMessage* message)
55 {
56 	switch (message->what) {
57 		case kMsgUpdate:
58 		{
59 			char buffer[100];
60 			snprintf(buffer, sizeof(buffer), "%ld ", (int32)fStatusBar->CurrentValue());
61 			fStatusBar->Update(1, fStatusBar->CurrentValue() > 25 ? " updated!" : NULL, buffer);
62 
63 			if (fStatusBar->CurrentValue() >= fStatusBar->MaxValue()) {
64 #if 1
65 				fStatusBar->Reset("-", "????");
66 #else
67 				fStatusBar->Reset();
68 				fStatusBar->SetText("-");
69 				fStatusBar->SetTrailingText("????");
70 #endif
71 				fStatusBar->SetMaxValue(50.0);
72 			}
73 		}
74 
75 		default:
76 			BWindow::MessageReceived(message);
77 	}
78 }
79 
80 
81 bool
82 Window::QuitRequested()
83 {
84 	be_app->PostMessage(B_QUIT_REQUESTED);
85 	delete fUpdater;
86 	return true;
87 }
88 
89 
90 //	#pragma mark -
91 
92 
93 class Application : public BApplication {
94 	public:
95 		Application();
96 
97 		virtual void ReadyToRun(void);
98 };
99 
100 
101 Application::Application()
102 	: BApplication("application/x-vnd.haiku-test")
103 {
104 }
105 
106 
107 void
108 Application::ReadyToRun(void)
109 {
110 	BWindow *window = new Window();
111 	window->Show();
112 }
113 
114 
115 //	#pragma mark -
116 
117 
118 int
119 main(int argc, char **argv)
120 {
121 	Application app;
122 
123 	app.Run();
124 	return 0;
125 }
126 
127