void BButton::Draw(BRect updateRect) { BRect rect(Bounds()); rgb_color background = LowColor(); rgb_color base = background; uint32 flags = be_control_look->Flags(this); if (_Flag(FLAG_DEFAULT)) flags |= BControlLook::B_DEFAULT_BUTTON; if (_Flag(FLAG_FLAT) && !IsTracking()) flags |= BControlLook::B_FLAT; if (_Flag(FLAG_INSIDE)) flags |= BControlLook::B_HOVER; be_control_look->DrawButtonFrame(this, rect, updateRect, base, background, flags); if (fBehavior == B_POP_UP_BEHAVIOR) { be_control_look->DrawButtonWithPopUpBackground(this, rect, updateRect, base, flags); } else { be_control_look->DrawButtonBackground(this, rect, updateRect, base, flags); } // always leave some room around the label rect.InsetBy(kLabelMargin, kLabelMargin); const BBitmap* icon = IconBitmap( (Value() == B_CONTROL_OFF ? B_INACTIVE_ICON_BITMAP : B_ACTIVE_ICON_BITMAP) | (IsEnabled() ? 0 : B_DISABLED_ICON_BITMAP)); be_control_look->DrawLabel(this, Label(), icon, rect, updateRect, base, flags, BAlignment(B_ALIGN_CENTER, B_ALIGN_MIDDLE)); }
void BButton::MouseDown(BPoint point) { if (!IsEnabled()) return; if (fBehavior == B_POP_UP_BEHAVIOR && _PopUpRect().Contains(point)) { InvokeNotify(fPopUpMessage, B_CONTROL_MODIFIED); return; } bool toggleBehavior = fBehavior == B_TOGGLE_BEHAVIOR; if (toggleBehavior) { bool wasPressed = Value() == B_CONTROL_ON; _SetFlag(FLAG_WAS_PRESSED, wasPressed); SetValue(wasPressed ? B_CONTROL_OFF : B_CONTROL_ON); Invalidate(); } else SetValue(B_CONTROL_ON); if (Window()->Flags() & B_ASYNCHRONOUS_CONTROLS) { SetTracking(true); SetMouseEventMask(B_POINTER_EVENTS, B_LOCK_WINDOW_FOCUS); } else { BRect bounds = Bounds(); uint32 buttons; bool inside = false; do { Window()->UpdateIfNeeded(); snooze(40000); GetMouse(&point, &buttons, true); inside = bounds.Contains(point); if (toggleBehavior) { bool pressed = inside ^ _Flag(FLAG_WAS_PRESSED); SetValue(pressed ? B_CONTROL_ON : B_CONTROL_OFF); } else { if ((Value() == B_CONTROL_ON) != inside) SetValue(inside ? B_CONTROL_ON : B_CONTROL_OFF); } } while (buttons != 0); if (inside) { if (toggleBehavior) { SetValue( _Flag(FLAG_WAS_PRESSED) ? B_CONTROL_OFF : B_CONTROL_ON); } Invoke(); } else if (_Flag(FLAG_FLAT)) Invalidate(); } }
void BButton::MouseUp(BPoint where) { if (!IsTracking()) return; if (Bounds().Contains(where)) { if (fBehavior == B_TOGGLE_BEHAVIOR) SetValue(_Flag(FLAG_WAS_PRESSED) ? B_CONTROL_OFF : B_CONTROL_ON); Invoke(); } else if (_Flag(FLAG_FLAT)) Invalidate(); SetTracking(false); }
void BButton::MouseMoved(BPoint where, uint32 code, const BMessage* dragMessage) { bool inside = Bounds().Contains(where); if (_SetFlag(FLAG_INSIDE, inside)) Invalidate(); if (!IsTracking()) return; if (fBehavior == B_TOGGLE_BEHAVIOR) { bool pressed = inside ^ _Flag(FLAG_WAS_PRESSED); SetValue(pressed ? B_CONTROL_ON : B_CONTROL_OFF); } else { if ((Value() == B_CONTROL_ON) != inside) SetValue(inside ? B_CONTROL_ON : B_CONTROL_OFF); } }
void BButton::MakeDefault(bool flag) { BButton* oldDefault = NULL; BWindow* window = Window(); if (window != NULL) oldDefault = window->DefaultButton(); if (flag) { if (_Flag(FLAG_DEFAULT) && oldDefault == this) return; if (_SetFlag(FLAG_DEFAULT, true)) { if ((Flags() & B_SUPPORTS_LAYOUT) != 0) InvalidateLayout(); else { ResizeBy(6.0f, 6.0f); MoveBy(-3.0f, -3.0f); } } if (window && oldDefault != this) window->SetDefaultButton(this); } else { if (!_SetFlag(FLAG_DEFAULT, false)) return; if ((Flags() & B_SUPPORTS_LAYOUT) != 0) InvalidateLayout(); else { ResizeBy(-6.0f, -6.0f); MoveBy(3.0f, 3.0f); } if (window && oldDefault == this) window->SetDefaultButton(NULL); } }
bool BButton::IsFlat() const { return _Flag(FLAG_FLAT); }
bool BButton::IsDefault() const { return _Flag(FLAG_DEFAULT); }