1/*
2 * Copyright 2003-2009, Haiku, Inc.
3 * Distributed under the terms of the MIT license.
4 *
5 * Authors:
6 *		J��r��me Duval
7 *		Fran��ois Revol
8 *		Axel D��rfler, axeld@pinc-software.de.
9 */
10
11
12#include "VolumeWindow.h"
13
14#include <Box.h>
15#include <GroupLayout.h>
16#include <MessageRunner.h>
17#include <Screen.h>
18
19#include "VolumeControl.h"
20
21
22static const uint32 kMsgVolumeUpdate = 'vlup';
23static const uint32 kMsgVolumeChanged = 'vlcg';
24
25
26VolumeWindow::VolumeWindow(BRect frame, bool dontBeep, int32 volumeWhich)
27	: BWindow(frame, "VolumeWindow", B_BORDERED_WINDOW_LOOK,
28		B_FLOATING_ALL_WINDOW_FEEL,
29		B_ASYNCHRONOUS_CONTROLS | B_WILL_ACCEPT_FIRST_CLICK
30		| B_AUTO_UPDATE_SIZE_LIMITS, 0),
31	fUpdatedCount(0)
32{
33	SetLayout(new BGroupLayout(B_HORIZONTAL));
34
35	BGroupLayout* layout = new BGroupLayout(B_HORIZONTAL);
36	layout->SetInsets(5, 5, 5, 5);
37
38	BBox* box = new BBox("sliderbox");
39	box->SetLayout(layout);
40	box->SetBorder(B_PLAIN_BORDER);
41	AddChild(box);
42
43	BSlider* slider = new VolumeControl(volumeWhich, !dontBeep,
44		new BMessage(kMsgVolumeChanged));
45	slider->SetModificationMessage(new BMessage(kMsgVolumeUpdate));
46	box->AddChild(slider);
47
48	slider->SetTarget(this);
49	ResizeTo(300, 50);
50
51	// Make sure it's not outside the screen.
52	const int32 kMargin = 3;
53	BRect windowRect = Frame();
54	BRect screenFrame(BScreen(B_MAIN_SCREEN_ID).Frame());
55	if (screenFrame.right < windowRect.right + kMargin)
56		MoveBy(- kMargin - windowRect.right + screenFrame.right, 0);
57	if (screenFrame.bottom < windowRect.bottom + kMargin)
58		MoveBy(0, - kMargin - windowRect.bottom + screenFrame.bottom);
59	if (screenFrame.left > windowRect.left - kMargin)
60		MoveBy(kMargin + screenFrame.left - windowRect.left, 0);
61	if (screenFrame.top > windowRect.top - kMargin)
62		MoveBy(0, kMargin + screenFrame.top - windowRect.top);
63}
64
65
66VolumeWindow::~VolumeWindow()
67{
68}
69
70
71void
72VolumeWindow::MessageReceived(BMessage *msg)
73{
74	switch (msg->what) {
75		case kMsgVolumeUpdate:
76			fUpdatedCount++;
77			break;
78
79		case kMsgVolumeChanged:
80			if (fUpdatedCount < 2) {
81				// If the slider was set by click only, wait a bit until
82				// closing the window to give some feedback that how volume
83				// was changed, and that it is.
84				BMessage quit(B_QUIT_REQUESTED);
85				BMessageRunner::StartSending(this, &quit, 150000, 1);
86			} else
87				Quit();
88			break;
89
90		case B_QUIT_REQUESTED:
91			Quit();
92			break;
93
94		default:
95			BWindow::MessageReceived(msg);
96			break;
97	}
98}
99
100