xref: /haiku/src/apps/soundrecorder/VolumeSlider.cpp (revision 1d9d47fc72028bb71b5f232a877231e59cfe2438)
1 /*
2  * Copyright 2005, Jérôme Duval. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  *
5  * Inspired by SoundCapture from Be newsletter (Media Kit Basics: Consumers and Producers)
6  */
7 
8 #include <stdio.h>
9 
10 #include "VolumeSlider.h"
11 #include "icon_button.h"
12 
13 #define VOLUME_CHANGED 'vlcg'
14 #define RATIO 2.0f
15 
16 VolumeSlider::VolumeSlider(BRect rect, const char *title, uint32 resizeFlags)
17 	: BControl(rect, "slider", NULL, new BMessage(VOLUME_CHANGED), resizeFlags, B_WILL_DRAW),
18 	fLeftBitmap(BRect(0, 0, kLeftVolumeWidth - 1, kLeftVolumeHeight - 1), B_CMAP8),
19 	fRightBitmap(BRect(0, 0, kRightVolumeWidth - 1, kRightVolumeHeight - 1), B_CMAP8),
20 	fButtonBitmap(BRect(0, 0, kThumbWidth - 1, kThumbHeight - 1), B_CMAP8),
21 	fSoundPlayer(NULL)
22 {
23 	fLeftBitmap.SetBits(kLeftVolumeBits, kLeftVolumeWidth * kLeftVolumeHeight, 0, B_CMAP8);
24 	fRightBitmap.SetBits(kRightVolumeBits, kRightVolumeWidth * kRightVolumeHeight, 0, B_CMAP8);
25 	fButtonBitmap.SetBits(kThumbBits, kThumbWidth * kThumbHeight, 0, B_CMAP8);
26 
27 	fRight = Bounds().right - 15;
28 }
29 
30 
31 VolumeSlider::~VolumeSlider()
32 {
33 
34 }
35 
36 
37 void
38 VolumeSlider::Draw(BRect updateRect)
39 {
40 	SetHighColor(189,186,189);
41 	StrokeLine(BPoint(11,1), BPoint(fRight,1));
42 	SetHighColor(0,0,0);
43 	StrokeLine(BPoint(11,2), BPoint(fRight,2));
44 	SetHighColor(255,255,255);
45 	StrokeLine(BPoint(11,14), BPoint(fRight,14));
46 	SetHighColor(231,227,231);
47 	StrokeLine(BPoint(11,15), BPoint(fRight,15));
48 
49 	SetLowColor(ViewColor());
50 
51 	SetDrawingMode(B_OP_OVER);
52 
53 	DrawBitmapAsync(&fLeftBitmap, BPoint(5,1));
54 	DrawBitmapAsync(&fRightBitmap, BPoint(fRight + 1,1));
55 
56 	float position = 11 + (fRight - 11) * (fSoundPlayer ? fSoundPlayer->Volume() / RATIO : 0);
57 	SetHighColor(102,152,102);
58 	FillRect(BRect(11,3,position,4));
59 	SetHighColor(152,203,152);
60 	FillRect(BRect(11,5,position,13));
61 
62 	if (fSoundPlayer)
63 		SetHighColor(152,152,152);
64 	else
65 		SetHighColor(200,200,200);
66 	FillRect(BRect(position,3,fRight,13));
67 
68 	SetHighColor(102,152,102);
69 	for (int i=15; i<=fRight + 1; i+=5) {
70 		if (i>position)
71 			SetHighColor(128,128,128);
72 		StrokeLine(BPoint(i,8), BPoint(i,9));
73 	}
74 
75 	DrawBitmapAsync(&fButtonBitmap, BPoint(position-5,3));
76 
77 	Sync();
78 
79 	SetDrawingMode(B_OP_COPY);
80 }
81 
82 
83 void
84 VolumeSlider::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
85 {
86 	if (!IsTracking())
87 		return;
88 
89 	uint32 mouseButtons;
90 	BPoint where;
91 	GetMouse(&where, &mouseButtons, true);
92 
93 	// button not pressed, exit
94 	if (! (mouseButtons & B_PRIMARY_MOUSE_BUTTON)) {
95 		Invoke();
96 		SetTracking(false);
97 	}
98 
99 	if (!fSoundPlayer || !Bounds().InsetBySelf(2,2).Contains(point))
100 		return;
101 
102 	UpdateVolume(point);
103 }
104 
105 
106 void
107 VolumeSlider::MouseDown(BPoint point)
108 {
109 	if (!fSoundPlayer || !Bounds().InsetBySelf(2,2).Contains(point))
110 		return;
111 
112 	UpdateVolume(point);
113 	SetTracking(true);
114 	SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
115 }
116 
117 
118 void
119 VolumeSlider::MouseUp(BPoint point)
120 {
121 	if (!IsTracking())
122 		return;
123 	if (fSoundPlayer && Bounds().InsetBySelf(2,2).Contains(point)) {
124 		UpdateVolume(point);
125 	}
126 
127 	Invoke();
128 	SetTracking(false);
129 	Draw(Bounds());
130 	Flush();
131 }
132 
133 
134 void
135 VolumeSlider::UpdateVolume(BPoint point)
136 {
137 	fVolume = MIN(MAX(point.x, 11), fRight);
138 	fVolume = (fVolume - 11) / (fRight - 11);
139 	fVolume = MAX(MIN(fVolume,1), 0);
140 	Draw(Bounds());
141 	Flush();
142 	if (fSoundPlayer)
143 		fSoundPlayer->SetVolume(fVolume * RATIO);
144 }
145 
146 void
147 VolumeSlider::SetSoundPlayer(BSoundPlayer *player)
148 {
149 	fSoundPlayer = player;
150 	Invalidate();
151 }
152 
153 
154 SpeakerView::SpeakerView(BRect rect, uint32 resizeFlags)
155 	: BBox(rect, "speaker", resizeFlags, B_WILL_DRAW, B_NO_BORDER),
156 	fSpeakerBitmap(BRect(0, 0, kSpeakerIconBitmapWidth - 1, kSpeakerIconBitmapHeight - 1), B_CMAP8)
157 {
158 	fSpeakerBitmap.SetBits(kSpeakerIconBits, kSpeakerIconBitmapWidth * kSpeakerIconBitmapHeight, 0, B_CMAP8);
159 }
160 
161 
162 SpeakerView::~SpeakerView()
163 {
164 
165 }
166 
167 
168 void
169 SpeakerView::Draw(BRect updateRect)
170 {
171 	SetDrawingMode(B_OP_OVER);
172 
173 	DrawBitmap(&fSpeakerBitmap);
174 
175 	SetDrawingMode(B_OP_COPY);
176 }
177