1101704Smjacob/*
2139749Simp * Copyright 2005, J��r��me Duval. All rights reserved.
3101704Smjacob * Distributed under the terms of the MIT License.
4101704Smjacob *
5101704Smjacob * Inspired by SoundCapture from Be newsletter (Media Kit Basics: Consumers
6101704Smjacob * and Producers)
7101704Smjacob */
8101704Smjacob
9101704Smjacob#include <stdio.h>
10101704Smjacob
11101704Smjacob#include "VolumeSlider.h"
12101704Smjacob#include "icon_button.h"
13101704Smjacob
14101704Smjacob#define VOLUME_CHANGED 'vlcg'
15101704Smjacob#define RATIO 2.0f
16101704Smjacob
17101704SmjacobVolumeSlider::VolumeSlider(BRect rect, const char *title, uint32 resizeFlags)
18101704Smjacob	: BControl(rect, "slider", NULL, new BMessage(VOLUME_CHANGED),
19101704Smjacob		resizeFlags, B_WILL_DRAW),
20101704Smjacob	fLeftBitmap(BRect(0, 0, kLeftVolumeWidth - 1, kLeftVolumeHeight - 1),
21101704Smjacob		B_CMAP8),
22101704Smjacob	fRightBitmap(BRect(0, 0, kRightVolumeWidth - 1, kRightVolumeHeight - 1),
23101704Smjacob		B_CMAP8),
24101704Smjacob	fButtonBitmap(BRect(0, 0, kThumbWidth - 1, kThumbHeight - 1), B_CMAP8),
25101704Smjacob	fSoundPlayer(NULL)
26101704Smjacob{
27101704Smjacob	fLeftBitmap.SetBits(kLeftVolumeBits, kLeftVolumeWidth * kLeftVolumeHeight,
28156000Smjacob		0, B_CMAP8);
29156000Smjacob	fRightBitmap.SetBits(kRightVolumeBits, kRightVolumeWidth * kRightVolumeHeight,
30156000Smjacob		0, B_CMAP8);
31156000Smjacob	fButtonBitmap.SetBits(kThumbBits, kThumbWidth * kThumbHeight, 0, B_CMAP8);
32156000Smjacob
33156000Smjacob	fRight = Bounds().right - 15;
34156000Smjacob}
35156000Smjacob
36156000Smjacob
37156000SmjacobVolumeSlider::~VolumeSlider()
38156000Smjacob{
39156000Smjacob
40156000Smjacob}
41156000Smjacob
42156000Smjacob
43156000Smjacobvoid
44156000SmjacobVolumeSlider::Draw(BRect updateRect)
45156000Smjacob{
46156000Smjacob	SetHighColor(189, 186, 189);
47156000Smjacob	StrokeLine(BPoint(11, 1), BPoint(fRight, 1));
48156000Smjacob	SetHighColor(0, 0, 0);
49156000Smjacob	StrokeLine(BPoint(11, 2), BPoint(fRight, 2));
50156000Smjacob	SetHighColor(255, 255, 255);
51156000Smjacob	StrokeLine(BPoint(11, 14), BPoint(fRight, 14));
52156000Smjacob	SetHighColor(231, 227, 231);
53156000Smjacob	StrokeLine(BPoint(11, 15), BPoint(fRight, 15));
54156000Smjacob
55156000Smjacob	SetLowColor(ViewColor());
56156000Smjacob
57156000Smjacob	SetDrawingMode(B_OP_OVER);
58147883Sscottl
59156000Smjacob	DrawBitmapAsync(&fLeftBitmap, BPoint(5, 1));
60156000Smjacob	DrawBitmapAsync(&fRightBitmap, BPoint(fRight + 1, 1));
61159052Smjacob
62159052Smjacob	float position = 11 + (fRight - 11) * (fSoundPlayer
63159052Smjacob		? fSoundPlayer->Volume() / RATIO : 0);
64159052Smjacob	SetHighColor(102, 152, 102);
65101704Smjacob	FillRect(BRect(11, 3, position, 4));
66101704Smjacob	SetHighColor(152, 203, 152);
67147883Sscottl	FillRect(BRect(11, 5, position, 13));
68147883Sscottl
69147883Sscottl	if (fSoundPlayer)
70147883Sscottl		SetHighColor(152, 152, 152);
71147883Sscottl	else
72147883Sscottl		SetHighColor(200, 200, 200);
73147883Sscottl	FillRect(BRect(position, 3, fRight, 13));
74147883Sscottl
75147883Sscottl	SetHighColor(102, 152, 102);
76147883Sscottl	for (int i = 15; i <= fRight + 1; i += 5) {
77147883Sscottl		if (i > position)
78147883Sscottl			SetHighColor(128, 128, 128);
79147883Sscottl		StrokeLine(BPoint(i, 8), BPoint(i, 9));
80147883Sscottl	}
81147883Sscottl
82148679Sgibbs	DrawBitmapAsync(&fButtonBitmap, BPoint(position - 5, 3));
83148679Sgibbs
84148679Sgibbs	Sync();
85147883Sscottl
86147883Sscottl	SetDrawingMode(B_OP_COPY);
87147883Sscottl}
88147883Sscottl
89147883Sscottl
90147883Sscottlvoid
91147883SscottlVolumeSlider::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
92147883Sscottl{
93147883Sscottl	if (!IsTracking())
94147883Sscottl		return;
95147883Sscottl
96147883Sscottl	uint32 mouseButtons;
97101704Smjacob	BPoint where;
98101704Smjacob	GetMouse(&where, &mouseButtons, true);
99101704Smjacob
100101704Smjacob	// button not pressed, exit
101147883Sscottl	if (! (mouseButtons & B_PRIMARY_MOUSE_BUTTON)) {
102147883Sscottl		Invoke();
103147883Sscottl		SetTracking(false);
104147883Sscottl	}
105147883Sscottl
106147883Sscottl	if (!fSoundPlayer || !Bounds().InsetBySelf(2, 2).Contains(point))
107147883Sscottl		return;
108147883Sscottl
109147883Sscottl	_UpdateVolume(point);
110147883Sscottl}
111147883Sscottl
112169293Smjacob
113147883Sscottlvoid
114147883SscottlVolumeSlider::MouseDown(BPoint point)
115147883Sscottl{
116147883Sscottl	if (!fSoundPlayer || !Bounds().InsetBySelf(2, 2).Contains(point))
117147883Sscottl		return;
118147883Sscottl
119147883Sscottl	_UpdateVolume(point);
120147883Sscottl	SetTracking(true);
121147883Sscottl	SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
122147883Sscottl}
123147883Sscottl
124147883Sscottl
125147883Sscottlvoid
126147883SscottlVolumeSlider::MouseUp(BPoint point)
127147883Sscottl{
128157117Smjacob	if (!IsTracking())
129157117Smjacob		return;
130159179Smjacob	if (fSoundPlayer && Bounds().InsetBySelf(2, 2).Contains(point)) {
131157117Smjacob		_UpdateVolume(point);
132157117Smjacob	}
133147883Sscottl
134147883Sscottl	Invoke();
135155521Smjacob	SetTracking(false);
136155521Smjacob	Draw(Bounds());
137155521Smjacob	Flush();
138155521Smjacob}
139155521Smjacob
140155521Smjacob
141155521Smjacobvoid
142155521SmjacobVolumeSlider::_UpdateVolume(BPoint point)
143155521Smjacob{
144147883Sscottl	fVolume = MIN(MAX(point.x, 11), fRight);
145147883Sscottl	fVolume = (fVolume - 11) / (fRight - 11);
146147883Sscottl	fVolume = MAX(MIN(fVolume,1), 0);
147147883Sscottl	Draw(Bounds());
148147883Sscottl	Flush();
149147883Sscottl	if (fSoundPlayer)
150147883Sscottl		fSoundPlayer->SetVolume(fVolume * RATIO);
151147883Sscottl}
152147883Sscottl
153147883Sscottlvoid
154147883SscottlVolumeSlider::SetSoundPlayer(BSoundPlayer *player)
155147883Sscottl{
156147883Sscottl	fSoundPlayer = player;
157147883Sscottl	Invalidate();
158147883Sscottl}
159164990Smjacob
160164990Smjacob
161164990SmjacobSpeakerView::SpeakerView(BRect rect, uint32 resizeFlags)
162147883Sscottl	: BBox(rect, "speaker", resizeFlags, B_WILL_DRAW, B_NO_BORDER),
163165814Smjacob	fSpeakerBitmap(BRect(0, 0, kSpeakerIconBitmapWidth - 1,
164101704Smjacob		kSpeakerIconBitmapHeight - 1), B_CMAP8)
165101704Smjacob{
166101704Smjacob	fSpeakerBitmap.SetBits(kSpeakerIconBits, kSpeakerIconBitmapWidth
167147883Sscottl		* kSpeakerIconBitmapHeight, 0, B_CMAP8);
168101704Smjacob}
169157117Smjacob
170157117Smjacob
171157117SmjacobSpeakerView::~SpeakerView()
172157117Smjacob{
173157117Smjacob
174157117Smjacob}
175147883Sscottl
176147883Sscottl
177147883Sscottlvoid
178147883SscottlSpeakerView::Draw(BRect updateRect)
179101704Smjacob{
180147883Sscottl	SetDrawingMode(B_OP_OVER);
181147883Sscottl
182147883Sscottl	DrawBitmap(&fSpeakerBitmap);
183147883Sscottl
184157117Smjacob	SetDrawingMode(B_OP_COPY);
185162133Smjacob}
186147883Sscottl