xref: /haiku/src/bin/desklink/VolumeWindow.cpp (revision 508f54795f39c3e7552d87c95aae9dd8ec6f505b)
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 
22 static const uint32 kMsgVolumeUpdate = 'vlup';
23 static const uint32 kMsgVolumeChanged = 'vlcg';
24 
25 
26 VolumeWindow::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 
66 VolumeWindow::~VolumeWindow()
67 {
68 }
69 
70 #include <stdio.h>
71 void
72 VolumeWindow::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 		default:
91 			BWindow::MessageReceived(msg);
92 			break;
93 	}
94 }
95 
96