vfs: check userland buffers before reading them.
[haiku.git] / src / apps / soundrecorder / UpDownButton.cpp
blob70d9ed30ab3c5d28070d76c0e4419f97c2030e54
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 */
8 #include "UpDownButton.h"
9 #include "icon_button.h"
11 UpDownButton::UpDownButton(BRect _rect, BMessage *msg, uint32 resizeFlags)
12 : BControl(_rect, "button", NULL, msg, resizeFlags, B_WILL_DRAW),
13 fLastValue(B_CONTROL_ON)
15 BRect rect = BRect(0, 0, kUpDownButtonWidth - 1, kUpDownButtonHeight - 1);
16 fBitmapUp = new BBitmap(rect, B_CMAP8);
17 fBitmapUp->SetBits(kButtonUpBits, kUpDownButtonWidth * kUpDownButtonHeight,
18 0, B_CMAP8);
19 fBitmapDown = new BBitmap(rect, B_CMAP8);
20 fBitmapDown->SetBits(kButtonDownBits, kUpDownButtonWidth
21 * kUpDownButtonHeight, 0, B_CMAP8);
22 fBitmapMiddle = new BBitmap(rect, B_CMAP8);
23 fBitmapMiddle->SetBits(kButtonMiddleBits, kUpDownButtonWidth
24 * kUpDownButtonHeight, 0, B_CMAP8);
28 UpDownButton::~UpDownButton()
30 delete fBitmapUp;
31 delete fBitmapDown;
32 delete fBitmapMiddle;
36 void
37 UpDownButton::Draw(BRect updateRect)
39 SetDrawingMode(B_OP_OVER);
41 if(IsTracking()) {
42 if((Bounds().top + Bounds().Height()/2) > (fTrackingY + 3))
43 DrawBitmap(fBitmapUp);
44 else if((Bounds().top + Bounds().Height()/2) < (fTrackingY - 3))
45 DrawBitmap(fBitmapDown);
46 else
47 DrawBitmap(fBitmapMiddle);
48 } else {
49 if(Value()==B_CONTROL_OFF)
50 DrawBitmap(fBitmapUp);
51 else
52 DrawBitmap(fBitmapDown);
55 SetDrawingMode(B_OP_COPY);
59 void
60 UpDownButton::MouseDown(BPoint point)
62 if(!IsEnabled())
63 return;
65 fLastValue = Value();
66 fTrackingY = (Bounds().top + Bounds().Height()/2);
68 SetTracking(true);
69 SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
71 SetValue(Value() == B_CONTROL_ON ? B_CONTROL_OFF : B_CONTROL_ON);
75 void
76 UpDownButton::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
78 if (!IsTracking())
79 return;
81 fTrackingY = point.y;
82 Draw(Bounds());
83 Flush();
87 void
88 UpDownButton::MouseUp(BPoint point)
90 if (!IsTracking())
91 return;
93 if((Bounds().top + Bounds().Height()/2) > (fTrackingY + 3))
94 SetValue(B_CONTROL_ON);
95 else if((Bounds().top + Bounds().Height()/2) < (fTrackingY - 3))
96 SetValue(B_CONTROL_OFF);
98 if(Value()!=fLastValue)
99 Invoke();
100 SetTracking(false);
101 Draw(Bounds());
102 Flush();
103 fLastValue = Value();