xref: /haiku/src/tests/servers/app/hide_and_show/HideAndShow.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
1 /*
2  * Copyright 2005-2009, Haiku Inc.
3  * Distributed under the terms of the MIT License.
4  *
5  * Authors:
6  *		Axel Dörfler, axeld@pinc-software.de
7  */
8 
9 
10 #include <stdio.h>
11 
12 #include <Application.h>
13 #include <MessageRunner.h>
14 #include <TextView.h>
15 #include <Window.h>
16 
17 
18 static const uint32 kMsgToggleShow = 'tgsh';
19 
20 class Window : public BWindow {
21 public:
22 							Window();
23 	virtual					~Window();
24 
25 	virtual	void			MessageReceived(BMessage* message);
26 	virtual void			FrameResized(float width, float height);
27 	virtual	bool			QuitRequested();
28 
29 private:
30 	BMessageRunner*			fRunner;
31 };
32 
33 
34 Window::Window()
35 	: BWindow(BRect(100, 100, 400, 400), "HideAndShow-Test",
36 			B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
37 {
38 	BRect rect(Bounds());
39 	rect.left += 20;
40 	rect.right -= 20;
41 	rect.top = 100;
42 	rect.bottom = 200;
43 	BTextView* view = new BTextView(Bounds(), "", rect, B_FOLLOW_ALL,
44 		B_FRAME_EVENTS | B_WILL_DRAW);
45 	view->MakeEditable(false);
46 	view->SetAlignment(B_ALIGN_CENTER);
47 	view->SetViewUIColor(B_PANEL_BACKGROUND_COLOR);
48 	view->SetText("The window will be hidden and shown every 2 seconds.");
49 	AddChild(view);
50 
51 	BMessage showAndHide(kMsgToggleShow);
52 	fRunner = new BMessageRunner(this, &showAndHide, 2000000);
53 }
54 
55 Window::~Window()
56 {
57 	delete fRunner;
58 }
59 
60 
61 void
62 Window::MessageReceived(BMessage* message)
63 {
64 	switch (message->what) {
65 		case kMsgToggleShow:
66 			if (IsHidden())
67 				Show();
68 			else
69 				Hide();
70 			break;
71 
72 		default:
73 			BWindow::MessageReceived(message);
74 			break;
75 	}
76 }
77 
78 
79 void
80 Window::FrameResized(float width, float height)
81 {
82 	BTextView* view = dynamic_cast<BTextView*>(ChildAt(0));
83 	if (view == NULL)
84 		return;
85 
86 	BRect rect = Bounds();
87 	rect.left += 20;
88 	rect.right -= 20;
89 	rect.top = 100;
90 	rect.bottom = 200;
91 	view->SetTextRect(rect);
92 }
93 
94 
95 bool
96 Window::QuitRequested()
97 {
98 	be_app->PostMessage(B_QUIT_REQUESTED);
99 	return true;
100 }
101 
102 
103 //	#pragma mark -
104 
105 
106 class Application : public BApplication {
107 public:
108 							Application();
109 
110 	virtual	void			ReadyToRun();
111 };
112 
113 
114 Application::Application()
115 	: BApplication("application/x-vnd.haiku-hide_and_show")
116 {
117 }
118 
119 
120 void
121 Application::ReadyToRun(void)
122 {
123 	Window* window = new Window();
124 	window->Show();
125 }
126 
127 
128 //	#pragma mark -
129 
130 
131 int
132 main(int argc, char** argv)
133 {
134 	Application app;
135 
136 	app.Run();
137 	return 0;
138 }
139