1123474Swpaul/*
2123474Swpaul * Copyright 2003-2009, Haiku, Inc.
3123474Swpaul * Distributed under the terms of the MIT license.
4123474Swpaul *
5123474Swpaul * Authors:
6123474Swpaul *		J��r��me Duval
7123474Swpaul *		Fran��ois Revol
8123474Swpaul *		Axel D��rfler, axeld@pinc-software.de.
9123474Swpaul */
10123474Swpaul
11123474Swpaul
12123474Swpaul#include "VolumeWindow.h"
13123474Swpaul
14123474Swpaul#include <Box.h>
15123474Swpaul#include <GroupLayout.h>
16123474Swpaul#include <MessageRunner.h>
17123474Swpaul#include <Screen.h>
18123474Swpaul
19123474Swpaul#include "VolumeControl.h"
20123474Swpaul
21123474Swpaul
22123474Swpaulstatic const uint32 kMsgVolumeUpdate = 'vlup';
23123474Swpaulstatic const uint32 kMsgVolumeChanged = 'vlcg';
24123474Swpaul
25123474Swpaul
26123474SwpaulVolumeWindow::VolumeWindow(BRect frame, bool dontBeep, int32 volumeWhich)
27123474Swpaul	: BWindow(frame, "VolumeWindow", B_BORDERED_WINDOW_LOOK,
28123474Swpaul		B_FLOATING_ALL_WINDOW_FEEL,
29123474Swpaul		B_ASYNCHRONOUS_CONTROLS | B_WILL_ACCEPT_FIRST_CLICK
30123474Swpaul		| B_AUTO_UPDATE_SIZE_LIMITS, 0),
31123474Swpaul	fUpdatedCount(0)
32123474Swpaul{
33123474Swpaul	SetLayout(new BGroupLayout(B_HORIZONTAL));
34123474Swpaul
35123474Swpaul	BGroupLayout* layout = new BGroupLayout(B_HORIZONTAL);
36123474Swpaul	layout->SetInsets(5, 5, 5, 5);
37124697Swpaul
38124697Swpaul	BBox* box = new BBox("sliderbox");
39123474Swpaul	box->SetLayout(layout);
40123474Swpaul	box->SetBorder(B_PLAIN_BORDER);
41123474Swpaul	AddChild(box);
42123474Swpaul
43123474Swpaul	BSlider* slider = new VolumeControl(volumeWhich, !dontBeep,
44123474Swpaul		new BMessage(kMsgVolumeChanged));
45124697Swpaul	slider->SetModificationMessage(new BMessage(kMsgVolumeUpdate));
46123474Swpaul	box->AddChild(slider);
47123474Swpaul
48123474Swpaul	slider->SetTarget(this);
49123474Swpaul	ResizeTo(300, 50);
50123474Swpaul
51123474Swpaul	// Make sure it's not outside the screen.
52124697Swpaul	const int32 kMargin = 3;
53123474Swpaul	BRect windowRect = Frame();
54123474Swpaul	BRect screenFrame(BScreen(B_MAIN_SCREEN_ID).Frame());
55123474Swpaul	if (screenFrame.right < windowRect.right + kMargin)
56123474Swpaul		MoveBy(- kMargin - windowRect.right + screenFrame.right, 0);
57123474Swpaul	if (screenFrame.bottom < windowRect.bottom + kMargin)
58124060Swpaul		MoveBy(0, - kMargin - windowRect.bottom + screenFrame.bottom);
59124060Swpaul	if (screenFrame.left > windowRect.left - kMargin)
60123474Swpaul		MoveBy(kMargin + screenFrame.left - windowRect.left, 0);
61123474Swpaul	if (screenFrame.top > windowRect.top - kMargin)
62123474Swpaul		MoveBy(0, kMargin + screenFrame.top - windowRect.top);
63123474Swpaul}
64123474Swpaul
65123474Swpaul
66123695SwpaulVolumeWindow::~VolumeWindow()
67123695Swpaul{
68123695Swpaul}
69123474Swpaul
70123474Swpaul#include <stdio.h>
71125551Swpaulvoid
72123474SwpaulVolumeWindow::MessageReceived(BMessage *msg)
73123474Swpaul{
74123474Swpaul	switch (msg->what) {
75123474Swpaul		case kMsgVolumeUpdate:
76123474Swpaul			fUpdatedCount++;
77123474Swpaul			break;
78123474Swpaul
79123474Swpaul		case kMsgVolumeChanged:
80123474Swpaul			if (fUpdatedCount < 2) {
81123474Swpaul				// If the slider was set by click only, wait a bit until
82123474Swpaul				// closing the window to give some feedback that how volume
83123535Swpaul				// was changed, and that it is.
84123474Swpaul				BMessage quit(B_QUIT_REQUESTED);
85124100Swpaul				BMessageRunner::StartSending(this, &quit, 150000, 1);
86123474Swpaul			} else
87125057Swpaul				Quit();
88125057Swpaul			break;
89124724Swpaul
90124724Swpaul		case B_QUIT_REQUESTED:
91124724Swpaul			Quit();
92124724Swpaul			break;
93124724Swpaul
94124724Swpaul		default:
95124724Swpaul			BWindow::MessageReceived(msg);
96124724Swpaul			break;
97124724Swpaul	}
98124724Swpaul}
99125814Swpaul
100124724Swpaul