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
11 #include "VolumeSlider.h"
12 #include "icon_button.h"
14 #define VOLUME_CHANGED 'vlcg'
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),
22 fRightBitmap(BRect(0, 0, kRightVolumeWidth
- 1, kRightVolumeHeight
- 1),
24 fButtonBitmap(BRect(0, 0, kThumbWidth
- 1, kThumbHeight
- 1), B_CMAP8
),
27 fLeftBitmap
.SetBits(kLeftVolumeBits
, kLeftVolumeWidth
* kLeftVolumeHeight
,
29 fRightBitmap
.SetBits(kRightVolumeBits
, kRightVolumeWidth
* kRightVolumeHeight
,
31 fButtonBitmap
.SetBits(kThumbBits
, kThumbWidth
* kThumbHeight
, 0, B_CMAP8
);
33 fRight
= Bounds().right
- 15;
37 VolumeSlider::~VolumeSlider()
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));
70 SetHighColor(152, 152, 152);
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) {
78 SetHighColor(128, 128, 128);
79 StrokeLine(BPoint(i
, 8), BPoint(i
, 9));
82 DrawBitmapAsync(&fButtonBitmap
, BPoint(position
- 5, 3));
86 SetDrawingMode(B_OP_COPY
);
91 VolumeSlider::MouseMoved(BPoint point
, uint32 transit
, const BMessage
*message
)
98 GetMouse(&where
, &mouseButtons
, true);
100 // button not pressed, exit
101 if (! (mouseButtons
& B_PRIMARY_MOUSE_BUTTON
)) {
106 if (!fSoundPlayer
|| !Bounds().InsetBySelf(2, 2).Contains(point
))
109 _UpdateVolume(point
);
114 VolumeSlider::MouseDown(BPoint point
)
116 if (!fSoundPlayer
|| !Bounds().InsetBySelf(2, 2).Contains(point
))
119 _UpdateVolume(point
);
121 SetMouseEventMask(B_POINTER_EVENTS
, B_LOCK_WINDOW_FOCUS
);
126 VolumeSlider::MouseUp(BPoint point
)
130 if (fSoundPlayer
&& Bounds().InsetBySelf(2, 2).Contains(point
)) {
131 _UpdateVolume(point
);
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);
150 fSoundPlayer
->SetVolume(fVolume
* RATIO
);
154 VolumeSlider::SetSoundPlayer(BSoundPlayer
*player
)
156 fSoundPlayer
= player
;
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()
178 SpeakerView::Draw(BRect updateRect
)
180 SetDrawingMode(B_OP_OVER
);
182 DrawBitmap(&fSpeakerBitmap
);
184 SetDrawingMode(B_OP_COPY
);