xref: /haiku/src/apps/soundrecorder/UpDownButton.cpp (revision 1e60bdeab63fa7a57bc9a55b032052e95a18bd2c)
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
6  * and Producers)
7  */
8 #include "UpDownButton.h"
9 #include "icon_button.h"
10 
11 UpDownButton::UpDownButton(BRect _rect, BMessage *msg, uint32 resizeFlags)
12 	: BControl(_rect, "button", NULL, msg, resizeFlags, B_WILL_DRAW),
13 	fLastValue(B_CONTROL_ON)
14 {
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);
25 }
26 
27 
28 UpDownButton::~UpDownButton()
29 {
30 	delete fBitmapUp;
31 	delete fBitmapDown;
32 	delete fBitmapMiddle;
33 }
34 
35 
36 void
37 UpDownButton::Draw(BRect updateRect)
38 {
39 	SetDrawingMode(B_OP_OVER);
40 
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);
53 	}
54 
55 	SetDrawingMode(B_OP_COPY);
56 }
57 
58 
59 void
60 UpDownButton::MouseDown(BPoint point)
61 {
62 	if(!IsEnabled())
63 		return;
64 
65 	fLastValue = Value();
66 	fTrackingY = (Bounds().top + Bounds().Height()/2);
67 
68 	SetTracking(true);
69 	SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
70 
71 	SetValue(Value() == B_CONTROL_ON ? B_CONTROL_OFF : B_CONTROL_ON);
72 }
73 
74 
75 void
76 UpDownButton::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
77 {
78 	if (!IsTracking())
79 		return;
80 
81 	fTrackingY = point.y;
82 	Draw(Bounds());
83 	Flush();
84 }
85 
86 
87 void
88 UpDownButton::MouseUp(BPoint point)
89 {
90 	if (!IsTracking())
91 		return;
92 
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);
97 
98 	if(Value()!=fLastValue)
99 		Invoke();
100 	SetTracking(false);
101 	Draw(Bounds());
102 	Flush();
103 	fLastValue = Value();
104 }
105