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 and Producers) 6 */ 7 #include "UpDownButton.h" 8 #include "icon_button.h" 9 10 UpDownButton::UpDownButton(BRect _rect, BMessage *msg, uint32 resizeFlags) 11 : BControl(_rect, "button", NULL, msg, resizeFlags, B_WILL_DRAW), 12 fLastValue(B_CONTROL_ON) 13 { 14 BRect rect = BRect(0, 0, kUpDownButtonWidth - 1, kUpDownButtonHeight - 1); 15 fBitmapUp = new BBitmap(rect, B_CMAP8); 16 fBitmapUp->SetBits(kButtonUpBits, kUpDownButtonWidth * kUpDownButtonHeight, 0, B_CMAP8); 17 fBitmapDown = new BBitmap(rect, B_CMAP8); 18 fBitmapDown->SetBits(kButtonDownBits, kUpDownButtonWidth * kUpDownButtonHeight, 0, B_CMAP8); 19 fBitmapMiddle = new BBitmap(rect, B_CMAP8); 20 fBitmapMiddle->SetBits(kButtonMiddleBits, kUpDownButtonWidth * kUpDownButtonHeight, 0, B_CMAP8); 21 } 22 23 24 UpDownButton::~UpDownButton() 25 { 26 delete fBitmapUp; 27 delete fBitmapDown; 28 delete fBitmapMiddle; 29 } 30 31 32 void 33 UpDownButton::Draw(BRect updateRect) 34 { 35 SetDrawingMode(B_OP_OVER); 36 37 if(IsTracking()) { 38 if((Bounds().top + Bounds().Height()/2) > (fTrackingY + 3)) 39 DrawBitmap(fBitmapUp); 40 else if((Bounds().top + Bounds().Height()/2) < (fTrackingY - 3)) 41 DrawBitmap(fBitmapDown); 42 else 43 DrawBitmap(fBitmapMiddle); 44 } else { 45 if(Value()==B_CONTROL_OFF) 46 DrawBitmap(fBitmapUp); 47 else 48 DrawBitmap(fBitmapDown); 49 } 50 51 SetDrawingMode(B_OP_COPY); 52 } 53 54 55 void 56 UpDownButton::MouseDown(BPoint point) 57 { 58 if(!IsEnabled()) 59 return; 60 61 fLastValue = Value(); 62 fTrackingY = (Bounds().top + Bounds().Height()/2); 63 64 SetTracking(true); 65 SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS); 66 67 SetValue(Value() == B_CONTROL_ON ? B_CONTROL_OFF : B_CONTROL_ON); 68 } 69 70 71 void 72 UpDownButton::MouseMoved(BPoint point, uint32 transit, const BMessage *message) 73 { 74 if (!IsTracking()) 75 return; 76 77 fTrackingY = point.y; 78 Draw(Bounds()); 79 Flush(); 80 } 81 82 83 void 84 UpDownButton::MouseUp(BPoint point) 85 { 86 if (!IsTracking()) 87 return; 88 89 if((Bounds().top + Bounds().Height()/2) > (fTrackingY + 3)) 90 SetValue(B_CONTROL_ON); 91 else if((Bounds().top + Bounds().Height()/2) < (fTrackingY - 3)) 92 SetValue(B_CONTROL_OFF); 93 94 if(Value()!=fLastValue) 95 Invoke(); 96 SetTracking(false); 97 Draw(Bounds()); 98 Flush(); 99 fLastValue = Value(); 100 } 101