1/*
2 * Copyright 2005, 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 <Application.h>
11#include <String.h>
12#include <View.h>
13#include <Window.h>
14
15#include <ctype.h>
16#include <stdio.h>
17#include <stdlib.h>
18
19
20bool gAvoid = true;
21
22
23class View : public BView {
24	public:
25		View(BRect rect);
26		virtual ~View();
27
28		virtual void AttachedToWindow();
29
30		virtual void KeyDown(const char* bytes, int32 numBytes);
31		virtual void KeyUp(const char* bytes, int32 numBytes);
32
33		virtual void MouseDown(BPoint where);
34		virtual void MouseMoved(BPoint where, uint32 transit, const BMessage* dragMessage);
35
36		virtual void Draw(BRect updateRect);
37		virtual void Pulse();
38
39	private:
40		BString	fChar;
41		bool	fPressed;
42};
43
44
45View::View(BRect rect)
46	: BView(rect, "desktop view", B_FOLLOW_ALL, B_WILL_DRAW | B_PULSE_NEEDED)
47{
48	SetViewColor(255, 255, 255);
49	SetHighColor(0, 0, 0);
50	SetLowColor(ViewColor());
51}
52
53
54View::~View()
55{
56}
57
58
59void
60View::AttachedToWindow()
61{
62	MakeFocus(true);
63	KeyDown("<>", 2);
64	fPressed = false;
65}
66
67
68void
69View::KeyDown(const char* bytes, int32 numBytes)
70{
71	fChar = bytes;
72	fPressed = true;
73	SetHighColor(0, 0, 0);
74	Window()->SetPulseRate(200000);
75	Invalidate();
76}
77
78
79void
80View::KeyUp(const char* bytes, int32 numBytes)
81{
82	fPressed = false;
83	Invalidate();
84}
85
86
87void
88View::MouseDown(BPoint where)
89{
90	SetHighColor(0, 250, 0);
91	Invalidate();
92	Window()->SetPulseRate(150000);
93}
94
95
96void
97View::MouseMoved(BPoint where, uint32 transit, const BMessage* dragMessage)
98{
99	if (transit == B_ENTERED_VIEW)
100		SetHighColor(0, 150, 150);
101	else
102		SetHighColor(0, 0, 150);
103
104	Invalidate();
105	Window()->SetPulseRate(10000);
106}
107
108
109void
110View::Draw(BRect updateRect)
111{
112	BFont font;
113	font.SetSize(200);
114
115	font_height fontHeight;
116	font.GetHeight(&fontHeight);
117
118	SetFont(&font);
119	MovePenTo(20, ceilf(20 + fontHeight.ascent));
120
121	DrawString(fChar.String());
122}
123
124
125void
126View::Pulse()
127{
128	if (fPressed)
129		return;
130
131	rgb_color color = HighColor();
132	SetHighColor(tint_color(color, B_LIGHTEN_2_TINT));
133	Invalidate();
134
135	if (color.red > 253)
136		Window()->SetPulseRate(0);
137}
138
139
140//	#pragma mark -
141
142
143class Window : public BWindow {
144	public:
145		Window();
146		virtual ~Window();
147
148		virtual bool QuitRequested();
149};
150
151
152Window::Window()
153	: BWindow(BRect(100, 100, 400, 400), "AvoidFocus-Test",
154			B_TITLED_WINDOW, (gAvoid ? B_AVOID_FOCUS : 0) | B_ASYNCHRONOUS_CONTROLS)
155{
156	BView *view = new View(Bounds());
157	AddChild(view);
158}
159
160
161Window::~Window()
162{
163}
164
165
166bool
167Window::QuitRequested()
168{
169	be_app->PostMessage(B_QUIT_REQUESTED);
170	return true;
171}
172
173
174//	#pragma mark -
175
176
177class Application : public BApplication {
178	public:
179		Application();
180
181		virtual void ReadyToRun();
182};
183
184
185Application::Application()
186	: BApplication("application/x-vnd.haiku-avoid_focus")
187{
188}
189
190
191void
192Application::ReadyToRun()
193{
194	Window *window = new Window();
195	window->Show();
196}
197
198
199//	#pragma mark -
200
201
202int
203main(int argc, char **argv)
204{
205	if (argc > 1 && (!strcmp(argv[1], "false") || (isdigit(argv[1][0]) && atoi(argv[1]) == 0)))
206		gAvoid = false;
207
208	Application app;
209
210	app.Run();
211	return 0;
212}
213