157e2f323SJérôme Duval /*
257e2f323SJérôme Duval * Copyright 2005, Jérôme Duval. All rights reserved.
357e2f323SJérôme Duval * Distributed under the terms of the MIT License.
457e2f323SJérôme Duval *
5*3ee96407SJérôme Duval * Inspired by SoundCapture from Be newsletter (Media Kit Basics: Consumers
6*3ee96407SJérôme Duval * and Producers)
757e2f323SJérôme Duval */
857e2f323SJérôme Duval #include "UpDownButton.h"
957e2f323SJérôme Duval #include "icon_button.h"
1057e2f323SJérôme Duval
UpDownButton(BRect _rect,BMessage * msg,uint32 resizeFlags)11758b1d0eSIngo Weinhold UpDownButton::UpDownButton(BRect _rect, BMessage *msg, uint32 resizeFlags)
12758b1d0eSIngo Weinhold : BControl(_rect, "button", NULL, msg, resizeFlags, B_WILL_DRAW),
1357e2f323SJérôme Duval fLastValue(B_CONTROL_ON)
1457e2f323SJérôme Duval {
1557e2f323SJérôme Duval BRect rect = BRect(0, 0, kUpDownButtonWidth - 1, kUpDownButtonHeight - 1);
1657e2f323SJérôme Duval fBitmapUp = new BBitmap(rect, B_CMAP8);
17*3ee96407SJérôme Duval fBitmapUp->SetBits(kButtonUpBits, kUpDownButtonWidth * kUpDownButtonHeight,
18*3ee96407SJérôme Duval 0, B_CMAP8);
1957e2f323SJérôme Duval fBitmapDown = new BBitmap(rect, B_CMAP8);
20*3ee96407SJérôme Duval fBitmapDown->SetBits(kButtonDownBits, kUpDownButtonWidth
21*3ee96407SJérôme Duval * kUpDownButtonHeight, 0, B_CMAP8);
2257e2f323SJérôme Duval fBitmapMiddle = new BBitmap(rect, B_CMAP8);
23*3ee96407SJérôme Duval fBitmapMiddle->SetBits(kButtonMiddleBits, kUpDownButtonWidth
24*3ee96407SJérôme Duval * kUpDownButtonHeight, 0, B_CMAP8);
2557e2f323SJérôme Duval }
2657e2f323SJérôme Duval
2757e2f323SJérôme Duval
~UpDownButton()2857e2f323SJérôme Duval UpDownButton::~UpDownButton()
2957e2f323SJérôme Duval {
3057e2f323SJérôme Duval delete fBitmapUp;
3157e2f323SJérôme Duval delete fBitmapDown;
3257e2f323SJérôme Duval delete fBitmapMiddle;
3357e2f323SJérôme Duval }
3457e2f323SJérôme Duval
3557e2f323SJérôme Duval
3657e2f323SJérôme Duval void
Draw(BRect updateRect)3757e2f323SJérôme Duval UpDownButton::Draw(BRect updateRect)
3857e2f323SJérôme Duval {
3957e2f323SJérôme Duval SetDrawingMode(B_OP_OVER);
4057e2f323SJérôme Duval
4157e2f323SJérôme Duval if(IsTracking()) {
4257e2f323SJérôme Duval if((Bounds().top + Bounds().Height()/2) > (fTrackingY + 3))
4357e2f323SJérôme Duval DrawBitmap(fBitmapUp);
4457e2f323SJérôme Duval else if((Bounds().top + Bounds().Height()/2) < (fTrackingY - 3))
4557e2f323SJérôme Duval DrawBitmap(fBitmapDown);
4657e2f323SJérôme Duval else
4757e2f323SJérôme Duval DrawBitmap(fBitmapMiddle);
4857e2f323SJérôme Duval } else {
4957e2f323SJérôme Duval if(Value()==B_CONTROL_OFF)
5057e2f323SJérôme Duval DrawBitmap(fBitmapUp);
5157e2f323SJérôme Duval else
5257e2f323SJérôme Duval DrawBitmap(fBitmapDown);
5357e2f323SJérôme Duval }
5457e2f323SJérôme Duval
5557e2f323SJérôme Duval SetDrawingMode(B_OP_COPY);
5657e2f323SJérôme Duval }
5757e2f323SJérôme Duval
5857e2f323SJérôme Duval
5957e2f323SJérôme Duval void
MouseDown(BPoint point)6057e2f323SJérôme Duval UpDownButton::MouseDown(BPoint point)
6157e2f323SJérôme Duval {
6257e2f323SJérôme Duval if(!IsEnabled())
6357e2f323SJérôme Duval return;
6457e2f323SJérôme Duval
6557e2f323SJérôme Duval fLastValue = Value();
6657e2f323SJérôme Duval fTrackingY = (Bounds().top + Bounds().Height()/2);
6757e2f323SJérôme Duval
6857e2f323SJérôme Duval SetTracking(true);
6957e2f323SJérôme Duval SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS);
7057e2f323SJérôme Duval
7157e2f323SJérôme Duval SetValue(Value() == B_CONTROL_ON ? B_CONTROL_OFF : B_CONTROL_ON);
7257e2f323SJérôme Duval }
7357e2f323SJérôme Duval
7457e2f323SJérôme Duval
7557e2f323SJérôme Duval void
MouseMoved(BPoint point,uint32 transit,const BMessage * message)7657e2f323SJérôme Duval UpDownButton::MouseMoved(BPoint point, uint32 transit, const BMessage *message)
7757e2f323SJérôme Duval {
7857e2f323SJérôme Duval if (!IsTracking())
7957e2f323SJérôme Duval return;
8057e2f323SJérôme Duval
8157e2f323SJérôme Duval fTrackingY = point.y;
8257e2f323SJérôme Duval Draw(Bounds());
8357e2f323SJérôme Duval Flush();
8457e2f323SJérôme Duval }
8557e2f323SJérôme Duval
8657e2f323SJérôme Duval
8757e2f323SJérôme Duval void
MouseUp(BPoint point)8857e2f323SJérôme Duval UpDownButton::MouseUp(BPoint point)
8957e2f323SJérôme Duval {
9057e2f323SJérôme Duval if (!IsTracking())
9157e2f323SJérôme Duval return;
9257e2f323SJérôme Duval
9357e2f323SJérôme Duval if((Bounds().top + Bounds().Height()/2) > (fTrackingY + 3))
9457e2f323SJérôme Duval SetValue(B_CONTROL_ON);
9557e2f323SJérôme Duval else if((Bounds().top + Bounds().Height()/2) < (fTrackingY - 3))
9657e2f323SJérôme Duval SetValue(B_CONTROL_OFF);
9757e2f323SJérôme Duval
9857e2f323SJérôme Duval if(Value()!=fLastValue)
9957e2f323SJérôme Duval Invoke();
10057e2f323SJérôme Duval SetTracking(false);
10157e2f323SJérôme Duval Draw(Bounds());
10257e2f323SJérôme Duval Flush();
10357e2f323SJérôme Duval fLastValue = Value();
10457e2f323SJérôme Duval }
105