1/*
2 * Copyright 2009-2012, Axel D��rfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
5
6
7#include <Application.h>
8#include <Box.h>
9#include <GroupView.h>
10#include <LayoutBuilder.h>
11#include <MessageRunner.h>
12#include <String.h>
13#include <StringView.h>
14#include <Window.h>
15
16#include <ToolTip.h>
17
18#include <stdio.h>
19
20
21class CustomToolTip : public BToolTip {
22public:
23	CustomToolTip(const char* text)
24	{
25		fView = new BStringView("", text);
26		fView->SetFont(be_bold_font);
27		fView->SetHighColor(255, 0, 0);
28	}
29
30	virtual ~CustomToolTip()
31	{
32		delete fView;
33	}
34
35	virtual BView* View() const
36	{
37		return fView;
38	}
39
40private:
41	BStringView*	fView;
42};
43
44
45class ChangingView : public BStringView {
46public:
47	ChangingView()
48		:
49		BStringView("changing", ""),
50		fNumber(0)
51	{
52	}
53
54	virtual ~ChangingView()
55	{
56	}
57
58	virtual void AttachedToWindow()
59	{
60		BStringView::AttachedToWindow();
61
62		BMessage update('updt');
63		fRunner = new BMessageRunner(this, &update, 100000);
64	}
65
66	virtual void DetachedFromWindow()
67	{
68		delete fRunner;
69	}
70
71	virtual void MessageReceived(BMessage* message)
72	{
73		if (message->what == 'updt') {
74			char text[42];
75			snprintf(text, sizeof(text), "%d", ++fNumber);
76			SetText(text);
77		}
78	}
79
80private:
81	BMessageRunner*	fRunner;
82	int				fNumber;
83};
84
85
86class ChangingToolTip : public BToolTip {
87public:
88	ChangingToolTip()
89	{
90		fView = new ChangingView();
91		fView->SetFont(be_bold_font);
92	}
93
94	virtual ~ChangingToolTip()
95	{
96		delete fView;
97	}
98
99	virtual BView* View() const
100	{
101		return fView;
102	}
103
104private:
105	BStringView*	fView;
106};
107
108
109class MouseView : public BStringView {
110public:
111	MouseView()
112		:
113		BStringView("mouse", "x:y")
114	{
115	}
116
117	virtual ~MouseView()
118	{
119	}
120
121	virtual void AttachedToWindow()
122	{
123		BStringView::AttachedToWindow();
124		SetEventMask(B_POINTER_EVENTS, 0);
125	}
126
127	virtual void MouseMoved(BPoint where, uint32 transit,
128		const BMessage* dragMessage)
129	{
130		ConvertToScreen(&where);
131
132		char text[42];
133		snprintf(text, sizeof(text), "%g:%g", where.x, where.y);
134		SetText(text);
135	}
136};
137
138
139class MouseToolTip : public BToolTip {
140public:
141	MouseToolTip()
142	{
143		fView = new MouseView();
144		SetSticky(true);
145	}
146
147	virtual ~MouseToolTip()
148	{
149		delete fView;
150	}
151
152	virtual BView* View() const
153	{
154		return fView;
155	}
156
157private:
158	BStringView*	fView;
159};
160
161
162class ImmediateView : public BStringView {
163public:
164	ImmediateView(const char* name, const char* label)
165		:
166		BStringView(name, label)
167	{
168		SetToolTip("Easy but immediate!");
169		ToolTip()->SetSticky(true);
170	}
171
172	virtual void MouseMoved(BPoint where, uint32 transit,
173		const BMessage* dragMessage)
174	{
175		if (transit == B_ENTERED_VIEW)
176			ShowToolTip(ToolTip());
177	}
178};
179
180
181class PulseStringView : public BStringView {
182public:
183	PulseStringView(const char* name, const char* label)
184		:
185		BStringView(name, label, B_WILL_DRAW | B_PULSE_NEEDED)
186	{
187	}
188
189	virtual void Pulse()
190	{
191		char buffer[256];
192		time_t now = time(NULL);
193		strftime(buffer, sizeof(buffer), "%X", localtime(&now));
194		SetToolTip(buffer);
195	}
196};
197
198
199class PulseToolTipView : public BStringView {
200public:
201	PulseToolTipView(const char* name, const char* label)
202		:
203		BStringView(name, label, B_WILL_DRAW | B_PULSE_NEEDED),
204		fToolTip(NULL),
205		fCounter(0)
206	{
207	}
208
209	virtual void Pulse()
210	{
211		if (fToolTip != NULL)
212			fToolTip->ReleaseReference();
213
214		BString text;
215		text.SetToFormat("New tool tip every second! (%d)", fCounter++);
216
217		fToolTip = new CustomToolTip(text.String());
218		SetToolTip(fToolTip);
219	}
220
221private:
222			BToolTip*		fToolTip;
223			int				fCounter;
224};
225
226
227class Window : public BWindow {
228public:
229							Window();
230
231	virtual	bool			QuitRequested();
232};
233
234
235class Application : public BApplication {
236public:
237							Application();
238
239	virtual	void			ReadyToRun();
240};
241
242
243//	#pragma mark -
244
245
246Window::Window()
247	:
248	BWindow(BRect(100, 100, 520, 430), "ToolTip-Test",
249		B_TITLED_WINDOW, B_ASYNCHRONOUS_CONTROLS)
250{
251	BView* simple = new BStringView("1", "Simple Tool Tip");
252	simple->SetToolTip("This is a really\nsimple tool tip!");
253
254	BView* custom = new BStringView("2", "Custom Tool Tip");
255	custom->SetToolTip(new CustomToolTip("Custom tool tip!"));
256
257	BView* changing = new BStringView("3", "Changing Tool Tip");
258	changing->SetToolTip(new ChangingToolTip());
259
260	BView* mouse = new BStringView("4", "Mouse Tool Tip (sticky)");
261	mouse->SetToolTip(new MouseToolTip());
262
263	BView* immediate = new ImmediateView("5", "Immediate Tool Tip (sticky)");
264
265	BView* pulseString = new PulseStringView("pulseString",
266		"Periodically changing tool tip text");
267
268	BView* pulseToolTip = new PulseToolTipView("pulseToolTip",
269		"Periodically changing tool tip");
270
271	BGroupView* nested = new BGroupView();
272	nested->SetViewColor(50, 50, 90);
273	nested->GroupLayout()->SetInsets(30);
274	nested->SetToolTip("The outer view has a tool tip,\n"
275		"the inner one doesn't.");
276	nested->AddChild(new BGroupView("inner"));
277
278	BLayoutBuilder::Group<>(this, B_HORIZONTAL)
279		.SetInsets(B_USE_DEFAULT_SPACING)
280		.AddGroup(B_VERTICAL)
281			.Add(simple)
282			.Add(custom)
283			.Add(changing)
284			.Add(mouse)
285			.Add(immediate)
286			.End()
287		.AddGroup(B_VERTICAL)
288			.Add(pulseString)
289			.Add(pulseToolTip)
290			.Add(nested);
291
292	SetPulseRate(1000000LL);
293}
294
295
296bool
297Window::QuitRequested()
298{
299	be_app->PostMessage(B_QUIT_REQUESTED);
300	return true;
301}
302
303
304//	#pragma mark -
305
306
307Application::Application()
308	:
309	BApplication("application/x-vnd.haiku-tooltiptest")
310{
311}
312
313
314void
315Application::ReadyToRun()
316{
317	BWindow *window = new Window();
318	window->Show();
319}
320
321
322//	#pragma mark -
323
324
325int
326main(int argc, char **argv)
327{
328	Application app;
329
330	app.Run();
331	return 0;
332}
333
334