BTRFS: Implement BTree::Path and change _Find.
[haiku.git] / src / apps / soundrecorder / VolumeSlider.cpp
blob775cba21681d8245e5e4ee52a79bd4cd803fab9f
1 /*
2 * Copyright 2005, Jérôme Duval. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Inspired by SoundCapture from Be newsletter (Media Kit Basics: Consumers
6 * and Producers)
7 */
9 #include <stdio.h>
11 #include "VolumeSlider.h"
12 #include "icon_button.h"
14 #define VOLUME_CHANGED 'vlcg'
15 #define RATIO 2.0f
17 VolumeSlider::VolumeSlider(BRect rect, const char *title, uint32 resizeFlags)
18 : BControl(rect, "slider", NULL, new BMessage(VOLUME_CHANGED),
19 resizeFlags, B_WILL_DRAW),
20 fLeftBitmap(BRect(0, 0, kLeftVolumeWidth - 1, kLeftVolumeHeight - 1),
21 B_CMAP8),
22 fRightBitmap(BRect(0, 0, kRightVolumeWidth - 1, kRightVolumeHeight - 1),
23 B_CMAP8),
24 fButtonBitmap(BRect(0, 0, kThumbWidth - 1, kThumbHeight - 1), B_CMAP8),
25 fSoundPlayer(NULL)
27 fLeftBitmap.SetBits(kLeftVolumeBits, kLeftVolumeWidth * kLeftVolumeHeight,
28 0, B_CMAP8);
29 fRightBitmap.SetBits(kRightVolumeBits, kRightVolumeWidth * kRightVolumeHeight,
30 0, B_CMAP8);
31 fButtonBitmap.SetBits(kThumbBits, kThumbWidth * kThumbHeight, 0, B_CMAP8);
33 fRight = Bounds().right - 15;
37 VolumeSlider::~VolumeSlider()
43 void
44 VolumeSlider::Draw(BRect updateRect)
46 SetHighColor(189, 186, 189);
47 StrokeLine(BPoint(11, 1), BPoint(fRight, 1));
48 SetHighColor(0, 0, 0);
49 StrokeLine(BPoint(11, 2), BPoint(fRight, 2));
50 SetHighColor(255, 255, 255);
51 StrokeLine(BPoint(11, 14), BPoint(fRight, 14));
52 SetHighColor(231, 227, 231);
53 StrokeLine(BPoint(11, 15), BPoint(fRight, 15));
55 SetLowColor(ViewColor());
57 SetDrawingMode(B_OP_OVER);
59 DrawBitmapAsync(&fLeftBitmap, BPoint(5, 1));
60 DrawBitmapAsync(&fRightBitmap, BPoint(fRight + 1, 1));
62 float position = 11 + (fRight - 11) * (fSoundPlayer
63 ? fSoundPlayer->Volume() / RATIO : 0);
64 SetHighColor(102, 152, 102);
65 FillRect(BRect(11, 3, position, 4));
66 SetHighColor(152, 203, 152);
67 FillRect(BRect(11, 5, position, 13));
69 if (fSoundPlayer)
70 SetHighColor(152, 152, 152);
71 else
72 SetHighColor(200, 200, 200);
73 FillRect(BRect(position, 3, fRight, 13));
75 SetHighColor(102, 152, 102);
76 for (int i = 15; i <= fRight + 1; i += 5) {
77 if (i > position)
78 SetHighColor(128, 128, 128);
79 StrokeLine(BPoint(i, 8), BPoint(i, 9));
82 DrawBitmapAsync(&fButtonBitmap, BPoint(position - 5, 3));
84 Sync();
86 SetDrawingMode(B_OP_COPY);
90 void
91 VolumeSlider::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
93 if (!IsTracking())
94 return;
96 uint32 mouseButtons;
97 BPoint where;
98 GetMouse(&where, &mouseButtons, true);
100 // button not pressed, exit
101 if (! (mouseButtons & B_PRIMARY_MOUSE_BUTTON)) {
102 Invoke();
103 SetTracking(false);
106 if (!fSoundPlayer || !Bounds().InsetBySelf(2, 2).Contains(point))
107 return;
109 _UpdateVolume(point);
113 void
114 VolumeSlider::MouseDown(BPoint point)
116 if (!fSoundPlayer || !Bounds().InsetBySelf(2, 2).Contains(point))
117 return;
119 _UpdateVolume(point);
120 SetTracking(true);
121 SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
125 void
126 VolumeSlider::MouseUp(BPoint point)
128 if (!IsTracking())
129 return;
130 if (fSoundPlayer && Bounds().InsetBySelf(2, 2).Contains(point)) {
131 _UpdateVolume(point);
134 Invoke();
135 SetTracking(false);
136 Draw(Bounds());
137 Flush();
141 void
142 VolumeSlider::_UpdateVolume(BPoint point)
144 fVolume = MIN(MAX(point.x, 11), fRight);
145 fVolume = (fVolume - 11) / (fRight - 11);
146 fVolume = MAX(MIN(fVolume,1), 0);
147 Draw(Bounds());
148 Flush();
149 if (fSoundPlayer)
150 fSoundPlayer->SetVolume(fVolume * RATIO);
153 void
154 VolumeSlider::SetSoundPlayer(BSoundPlayer *player)
156 fSoundPlayer = player;
157 Invalidate();
161 SpeakerView::SpeakerView(BRect rect, uint32 resizeFlags)
162 : BBox(rect, "speaker", resizeFlags, B_WILL_DRAW, B_NO_BORDER),
163 fSpeakerBitmap(BRect(0, 0, kSpeakerIconBitmapWidth - 1,
164 kSpeakerIconBitmapHeight - 1), B_CMAP8)
166 fSpeakerBitmap.SetBits(kSpeakerIconBits, kSpeakerIconBitmapWidth
167 * kSpeakerIconBitmapHeight, 0, B_CMAP8);
171 SpeakerView::~SpeakerView()
177 void
178 SpeakerView::Draw(BRect updateRect)
180 SetDrawingMode(B_OP_OVER);
182 DrawBitmap(&fSpeakerBitmap);
184 SetDrawingMode(B_OP_COPY);