2 * Copyright 2001-2016 Haiku, Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
6 * Stephan Aßmus <superstippi@gmx.de>
7 * Axel Dörfler, axeld@pinc-software.de
8 * Marc Flerackers (mflerackers@androme.be)
21 #include <ControlLook.h>
23 #include <LayoutUtils.h>
29 #include <binary_compatibility/Interface.h>
32 #define USE_OFF_SCREEN_VIEW 0
36 BRect frame
, const char* name
, const char* label
, BMessage
* message
,
37 int32 minValue
, int32 maxValue
, thumb_style thumbType
, uint32 resizingMode
,
40 BControl(frame
, name
, label
, message
, resizingMode
, flags
),
41 fModificationMessage(NULL
),
49 fKeyIncrementValue(1),
52 fHashMarks(B_HASH_MARKS_NONE
),
56 fOrientation(B_HORIZONTAL
),
66 BSlider::BSlider(BRect frame
, const char* name
, const char* label
,
67 BMessage
* message
, int32 minValue
, int32 maxValue
, orientation posture
,
68 thumb_style thumbType
, uint32 resizingMode
, uint32 flags
)
70 BControl(frame
, name
, label
, message
, resizingMode
, flags
),
71 fModificationMessage(NULL
),
79 fKeyIncrementValue(1),
82 fHashMarks(B_HASH_MARKS_NONE
),
86 fOrientation(posture
),
96 BSlider::BSlider(const char* name
, const char* label
, BMessage
* message
,
97 int32 minValue
, int32 maxValue
, orientation posture
, thumb_style thumbType
,
100 BControl(name
, label
, message
, flags
),
101 fModificationMessage(NULL
),
102 fSnoozeAmount(20000),
104 fMinLimitLabel(NULL
),
105 fMaxLimitLabel(NULL
),
109 fKeyIncrementValue(1),
112 fHashMarks(B_HASH_MARKS_NONE
),
116 fOrientation(posture
),
126 BSlider::BSlider(BMessage
* archive
)
130 fModificationMessage
= NULL
;
132 if (archive
->HasMessage("_mod_msg")) {
133 BMessage
* message
= new BMessage
;
135 archive
->FindMessage("_mod_msg", message
);
137 SetModificationMessage(message
);
140 if (archive
->FindInt32("_sdelay", &fSnoozeAmount
) != B_OK
)
141 SetSnoozeAmount(20000);
144 if (archive
->FindInt32("_fcolor", (int32
*)&color
) == B_OK
)
145 UseFillColor(true, &color
);
150 if (archive
->FindInt32("_orient", &orient
) == B_OK
)
151 fOrientation
= (orientation
)orient
;
153 fOrientation
= B_HORIZONTAL
;
155 fMinLimitLabel
= NULL
;
156 fMaxLimitLabel
= NULL
;
158 const char* minlbl
= NULL
;
159 const char* maxlbl
= NULL
;
161 archive
->FindString("_minlbl", &minlbl
);
162 archive
->FindString("_maxlbl", &maxlbl
);
164 SetLimitLabels(minlbl
, maxlbl
);
166 if (archive
->FindInt32("_min", &fMinValue
) != B_OK
)
169 if (archive
->FindInt32("_max", &fMaxValue
) != B_OK
)
172 if (archive
->FindInt32("_incrementvalue", &fKeyIncrementValue
) != B_OK
)
173 fKeyIncrementValue
= 1;
175 if (archive
->FindInt32("_hashcount", &fHashMarkCount
) != B_OK
)
179 if (archive
->FindInt16("_hashloc", &hashloc
) == B_OK
)
180 fHashMarks
= (hash_mark_location
)hashloc
;
182 fHashMarks
= B_HASH_MARKS_NONE
;
185 if (archive
->FindInt16("_sstyle", &sstyle
) == B_OK
)
186 fStyle
= (thumb_style
)sstyle
;
188 fStyle
= B_BLOCK_THUMB
;
190 if (archive
->FindInt32("_bcolor", (int32
*)&color
) != B_OK
)
191 color
= tint_color(ui_color(B_PANEL_BACKGROUND_COLOR
), B_DARKEN_4_TINT
);
195 if (archive
->FindFloat("_bthickness", &bthickness
) == B_OK
)
196 fBarThickness
= bthickness
;
198 fBarThickness
= 6.0f
;
206 #if USE_OFF_SCREEN_VIEW
207 delete fOffScreenBits
;
210 delete fModificationMessage
;
211 free(fMinLimitLabel
);
212 free(fMaxLimitLabel
);
217 BSlider::_InitBarColor()
219 SetBarColor(be_control_look
->SliderBarColor(
220 ui_color(B_PANEL_BACKGROUND_COLOR
)));
221 UseFillColor(false, NULL
);
226 BSlider::_InitObject()
230 fInitialLocation
.x
= 0;
231 fInitialLocation
.y
= 0;
233 #if USE_OFF_SCREEN_VIEW
234 fOffScreenBits
= NULL
;
235 fOffScreenView
= NULL
;
239 fMinSize
.Set(-1, -1);
240 fMaxUpdateTextWidth
= -1.0;
245 BSlider::Instantiate(BMessage
* archive
)
247 if (validate_instantiation(archive
, "BSlider"))
248 return new BSlider(archive
);
255 BSlider::Archive(BMessage
* archive
, bool deep
) const
257 status_t ret
= BControl::Archive(archive
, deep
);
259 if (ModificationMessage() && ret
== B_OK
)
260 ret
= archive
->AddMessage("_mod_msg", ModificationMessage());
263 ret
= archive
->AddInt32("_sdelay", fSnoozeAmount
);
266 ret
= archive
->AddInt32("_bcolor", (const uint32
&)fBarColor
);
268 if (FillColor(NULL
) && ret
== B_OK
)
269 ret
= archive
->AddInt32("_fcolor", (const uint32
&)fFillColor
);
271 if (ret
== B_OK
&& fMinLimitLabel
!= NULL
)
272 ret
= archive
->AddString("_minlbl", fMinLimitLabel
);
274 if (ret
== B_OK
&& fMaxLimitLabel
!= NULL
)
275 ret
= archive
->AddString("_maxlbl", fMaxLimitLabel
);
278 ret
= archive
->AddInt32("_min", fMinValue
);
281 ret
= archive
->AddInt32("_max", fMaxValue
);
284 ret
= archive
->AddInt32("_incrementvalue", fKeyIncrementValue
);
287 ret
= archive
->AddInt32("_hashcount", fHashMarkCount
);
290 ret
= archive
->AddInt16("_hashloc", fHashMarks
);
293 ret
= archive
->AddInt16("_sstyle", fStyle
);
296 ret
= archive
->AddInt32("_orient", fOrientation
);
299 ret
= archive
->AddFloat("_bthickness", fBarThickness
);
306 BSlider::Perform(perform_code code
, void* _data
)
309 case PERFORM_CODE_MIN_SIZE
:
310 ((perform_data_min_size
*)_data
)->return_value
= BSlider::MinSize();
313 case PERFORM_CODE_MAX_SIZE
:
314 ((perform_data_max_size
*)_data
)->return_value
= BSlider::MaxSize();
317 case PERFORM_CODE_PREFERRED_SIZE
:
318 ((perform_data_preferred_size
*)_data
)->return_value
319 = BSlider::PreferredSize();
322 case PERFORM_CODE_LAYOUT_ALIGNMENT
:
323 ((perform_data_layout_alignment
*)_data
)->return_value
324 = BSlider::LayoutAlignment();
327 case PERFORM_CODE_HAS_HEIGHT_FOR_WIDTH
:
328 ((perform_data_has_height_for_width
*)_data
)->return_value
329 = BSlider::HasHeightForWidth();
332 case PERFORM_CODE_GET_HEIGHT_FOR_WIDTH
:
334 perform_data_get_height_for_width
* data
335 = (perform_data_get_height_for_width
*)_data
;
336 BSlider::GetHeightForWidth(data
->width
, &data
->min
, &data
->max
,
341 case PERFORM_CODE_SET_LAYOUT
:
343 perform_data_set_layout
* data
= (perform_data_set_layout
*)_data
;
344 BSlider::SetLayout(data
->layout
);
348 case PERFORM_CODE_LAYOUT_INVALIDATED
:
350 perform_data_layout_invalidated
* data
351 = (perform_data_layout_invalidated
*)_data
;
352 BSlider::LayoutInvalidated(data
->descendants
);
356 case PERFORM_CODE_DO_LAYOUT
:
362 case PERFORM_CODE_SET_ICON
:
364 perform_data_set_icon
* data
= (perform_data_set_icon
*)_data
;
365 return BSlider::SetIcon(data
->icon
, data
->flags
);
369 return BControl::Perform(code
, _data
);
374 BSlider::WindowActivated(bool state
)
376 BControl::WindowActivated(state
);
381 BSlider::AttachedToWindow()
385 #if USE_OFF_SCREEN_VIEW
386 BRect
bounds(Bounds());
388 if (!fOffScreenView
) {
389 fOffScreenView
= new BView(bounds
, "", B_FOLLOW_ALL
, B_WILL_DRAW
);
393 fOffScreenView
->SetFont(&font
);
396 if (!fOffScreenBits
) {
397 fOffScreenBits
= new BBitmap(bounds
, B_RGBA32
, true, false);
399 if (fOffScreenBits
&& fOffScreenView
)
400 fOffScreenBits
->AddChild(fOffScreenView
);
402 } else if (fOffScreenView
)
403 fOffScreenBits
->AddChild(fOffScreenView
);
404 #endif // USE_OFF_SCREEN_VIEW
406 BControl::AttachedToWindow();
408 BView
* view
= OffscreenView();
409 if (view
!= NULL
&& view
->LockLooper()) {
410 view
->SetViewColor(B_TRANSPARENT_COLOR
);
411 if (LowUIColor() != B_NO_COLOR
)
412 view
->SetLowUIColor(LowUIColor());
414 view
->SetLowColor(LowColor());
416 view
->UnlockLooper();
419 int32 value
= Value();
421 // makes sure the value is within valid bounds
422 _SetLocationForValue(Value());
423 // makes sure the location is correct
429 BSlider::AllAttached()
431 BControl::AllAttached();
433 // When using a layout we may not have a parent, so we need to employ the
434 // standard system colors manually. Due to how layouts work, this must
435 // happen here, rather than in AttachedToWindow().
436 if (Parent() == NULL
)
437 SetLowUIColor(B_PANEL_BACKGROUND_COLOR
);
442 BSlider::AllDetached()
444 BControl::AllDetached();
449 BSlider::DetachedFromWindow()
451 BControl::DetachedFromWindow();
453 #if USE_OFF_SCREEN_VIEW
454 if (fOffScreenBits
) {
455 delete fOffScreenBits
;
456 fOffScreenBits
= NULL
;
457 fOffScreenView
= NULL
;
464 BSlider::MessageReceived(BMessage
* message
)
466 BControl::MessageReceived(message
);
471 BSlider::FrameMoved(BPoint new_position
)
473 BControl::FrameMoved(new_position
);
478 BSlider::FrameResized(float w
,float h
)
480 BControl::FrameResized(w
, h
);
482 BRect
bounds(Bounds());
484 if (bounds
.right
<= 0.0f
|| bounds
.bottom
<= 0.0f
)
487 #if USE_OFF_SCREEN_VIEW
488 if (fOffScreenBits
) {
489 fOffScreenBits
->RemoveChild(fOffScreenView
);
490 delete fOffScreenBits
;
492 fOffScreenView
->ResizeTo(bounds
.Width(), bounds
.Height());
494 fOffScreenBits
= new BBitmap(Bounds(), B_RGBA32
, true, false);
495 fOffScreenBits
->AddChild(fOffScreenView
);
504 BSlider::KeyDown(const char* bytes
, int32 numBytes
)
506 if (!IsEnabled() || IsHidden())
509 int32 newValue
= Value();
514 newValue
-= KeyIncrementValue();
519 newValue
+= KeyIncrementValue();
523 newValue
= fMinValue
;
527 newValue
= fMaxValue
;
531 BControl::KeyDown(bytes
, numBytes
);
535 if (newValue
< fMinValue
)
536 newValue
= fMinValue
;
538 if (newValue
> fMaxValue
)
539 newValue
= fMaxValue
;
541 if (newValue
!= Value()) {
542 fInitialLocation
= _Location();
544 InvokeNotify(ModificationMessage(), B_CONTROL_MODIFIED
);
549 BSlider::KeyUp(const char* bytes
, int32 numBytes
)
551 if (fInitialLocation
!= _Location()) {
552 // The last KeyDown event triggered the modification message or no
553 // notification at all, we may also have sent the modification message
554 // continually while the user kept pressing the key. In either case,
555 // finish with the final message to make the behavior consistent with
556 // changing the value by mouse.
563 Makes sure the \a point is within valid bounds.
564 Returns \c true if the relevant coordinate (depending on the orientation
565 of the slider) differs from \a comparePoint.
568 BSlider::_ConstrainPoint(BPoint
& point
, BPoint comparePoint
) const
570 if (fOrientation
== B_HORIZONTAL
) {
571 if (point
.x
!= comparePoint
.x
) {
572 if (point
.x
< _MinPosition())
573 point
.x
= _MinPosition();
574 else if (point
.x
> _MaxPosition())
575 point
.x
= _MaxPosition();
580 if (point
.y
!= comparePoint
.y
) {
581 if (point
.y
> _MinPosition())
582 point
.y
= _MinPosition();
583 else if (point
.y
< _MaxPosition())
584 point
.y
= _MaxPosition();
595 BSlider::MouseDown(BPoint point
)
600 if (BarFrame().Contains(point
) || ThumbFrame().Contains(point
))
601 fInitialLocation
= _Location();
604 GetMouse(&point
, &buttons
, true);
606 _ConstrainPoint(point
, fInitialLocation
);
607 SetValue(ValueForPoint(point
));
609 if (_Location() != fInitialLocation
)
610 InvokeNotify(ModificationMessage(), B_CONTROL_MODIFIED
);
612 if (Window()->Flags() & B_ASYNCHRONOUS_CONTROLS
) {
614 SetMouseEventMask(B_POINTER_EVENTS
,
615 B_LOCK_WINDOW_FOCUS
| B_NO_POINTER_HISTORY
);
617 // synchronous mouse tracking
623 snooze(SnoozeAmount());
624 GetMouse(&point
, &buttons
, true);
626 if (_ConstrainPoint(point
, prevPoint
)) {
627 int32 value
= ValueForPoint(point
);
628 if (value
!= Value()) {
630 InvokeNotify(ModificationMessage(), B_CONTROL_MODIFIED
);
634 if (_Location() != fInitialLocation
)
641 BSlider::MouseUp(BPoint point
)
644 if (_Location() != fInitialLocation
)
649 BControl::MouseUp(point
);
654 BSlider::MouseMoved(BPoint point
, uint32 transit
, const BMessage
* message
)
657 if (_ConstrainPoint(point
, _Location())) {
658 int32 value
= ValueForPoint(point
);
659 if (value
!= Value()) {
661 InvokeNotify(ModificationMessage(), B_CONTROL_MODIFIED
);
665 BControl::MouseMoved(point
, transit
, message
);
677 BSlider::SetLabel(const char* label
)
679 BControl::SetLabel(label
);
684 BSlider::SetLimitLabels(const char* minLabel
, const char* maxLabel
)
686 free(fMinLimitLabel
);
687 fMinLimitLabel
= minLabel
? strdup(minLabel
) : NULL
;
689 free(fMaxLimitLabel
);
690 fMaxLimitLabel
= maxLabel
? strdup(maxLabel
) : NULL
;
694 // TODO: This is for backwards compatibility and should
695 // probably be removed when breaking binary compatiblity.
696 // Applications like our own Mouse rely on this behavior.
697 if ((Flags() & B_SUPPORTS_LAYOUT
) == 0)
705 BSlider::MinLimitLabel() const
707 return fMinLimitLabel
;
712 BSlider::MaxLimitLabel() const
714 return fMaxLimitLabel
;
719 BSlider::SetValue(int32 value
)
721 if (value
< fMinValue
)
724 if (value
> fMaxValue
)
727 if (value
== Value())
730 _SetLocationForValue(value
);
732 BRect oldThumbFrame
= ThumbFrame();
734 // While it would be enough to do this dependent on fUseFillColor,
735 // that doesn't work out if DrawBar() has been overridden by a sub class
736 if (fOrientation
== B_HORIZONTAL
)
737 oldThumbFrame
.top
= BarFrame().top
;
739 oldThumbFrame
.left
= BarFrame().left
;
741 BControl::SetValueNoUpdate(value
);
742 BRect invalid
= oldThumbFrame
| ThumbFrame();
744 if (Style() == B_TRIANGLE_THUMB
) {
745 // 1) We need to take care of pixels touched because of anti-aliasing.
746 // 2) We need to update the region with the focus mark as well. (A
747 // method BSlider::FocusMarkFrame() would be nice as well.)
748 if (fOrientation
== B_HORIZONTAL
) {
751 invalid
.InsetBy(-1, 0);
755 invalid
.InsetBy(0, -1);
766 BSlider::ValueForPoint(BPoint location
) const
771 if (fOrientation
== B_HORIZONTAL
) {
772 min
= _MinPosition();
773 max
= _MaxPosition();
774 position
= location
.x
;
776 max
= _MinPosition();
777 min
= _MaxPosition();
778 position
= min
+ (max
- location
.y
);
787 return (int32
)roundf(((position
- min
) * (fMaxValue
- fMinValue
)
788 / (max
- min
)) + fMinValue
);
793 BSlider::SetPosition(float position
)
795 if (position
<= 0.0f
)
797 else if (position
>= 1.0f
)
800 SetValue((int32
)(position
* (fMaxValue
- fMinValue
) + fMinValue
));
805 BSlider::Position() const
807 float range
= (float)(fMaxValue
- fMinValue
);
811 return (float)(Value() - fMinValue
) / range
;
816 BSlider::SetEnabled(bool on
)
818 BControl::SetEnabled(on
);
823 BSlider::GetLimits(int32
* minimum
, int32
* maximum
) const
826 *minimum
= fMinValue
;
829 *maximum
= fMaxValue
;
833 // #pragma mark - drawing
837 BSlider::Draw(BRect updateRect
)
839 // clear out background
840 BRegion
background(updateRect
);
841 background
.Exclude(BarFrame());
842 bool drawBackground
= true;
843 if (Parent() != NULL
&& (Parent()->Flags() & B_DRAW_ON_CHILDREN
) != 0) {
844 // This view is embedded somewhere, most likely the Tracker Desktop
846 drawBackground
= false;
849 #if USE_OFF_SCREEN_VIEW
853 if (fOffScreenBits
->Lock()) {
854 fOffScreenView
->SetViewColor(ViewColor());
855 fOffScreenView
->SetLowColor(LowColor());
858 if (drawBackground
&& background
.Frame().IsValid())
859 OffscreenView()->FillRegion(&background
, B_SOLID_LOW
);
861 #if USE_OFF_SCREEN_VIEW
862 fOffScreenView
->Sync();
863 fOffScreenBits
->Unlock();
872 BSlider::DrawSlider()
875 #if USE_OFF_SCREEN_VIEW
876 if (fOffScreenBits
== NULL
)
879 if (fOffScreenBits
->Lock()) {
887 #if USE_OFF_SCREEN_VIEW
888 fOffScreenView
->Sync();
889 fOffScreenBits
->Unlock();
891 DrawBitmap(fOffScreenBits
, B_ORIGIN
);
902 BRect frame
= BarFrame();
903 BView
* view
= OffscreenView();
905 uint32 flags
= be_control_look
->Flags(this);
906 rgb_color base
= ui_color(B_PANEL_BACKGROUND_COLOR
);
907 rgb_color rightFillColor
= fBarColor
;
908 rgb_color leftFillColor
= fUseFillColor
? fFillColor
: fBarColor
;
909 be_control_look
->DrawSliderBar(view
, frame
, frame
, base
, leftFillColor
,
910 rightFillColor
, Position(), flags
, fOrientation
);
915 BSlider::DrawHashMarks()
917 if (fHashMarks
== B_HASH_MARKS_NONE
)
920 BRect frame
= HashMarksFrame();
921 BView
* view
= OffscreenView();
923 rgb_color base
= ui_color(B_PANEL_BACKGROUND_COLOR
);
924 uint32 flags
= be_control_look
->Flags(this);
925 be_control_look
->DrawSliderHashMarks(view
, frame
, frame
, base
,
926 fHashMarkCount
, fHashMarks
, flags
, fOrientation
);
933 if (Style() == B_BLOCK_THUMB
)
936 _DrawTriangleThumb();
941 BSlider::DrawFocusMark()
946 OffscreenView()->SetHighColor(ui_color(B_KEYBOARD_NAVIGATION_COLOR
));
948 BRect frame
= ThumbFrame();
950 if (fStyle
== B_BLOCK_THUMB
) {
954 frame
.bottom
-= 3.0f
;
955 OffscreenView()->StrokeRect(frame
);
957 if (fOrientation
== B_HORIZONTAL
) {
958 OffscreenView()->StrokeLine(BPoint(frame
.left
, frame
.bottom
+ 2.0f
),
959 BPoint(frame
.right
, frame
.bottom
+ 2.0f
));
961 OffscreenView()->StrokeLine(BPoint(frame
.left
- 2.0f
, frame
.top
),
962 BPoint(frame
.left
- 2.0f
, frame
.bottom
));
971 BRect
bounds(Bounds());
972 BView
* view
= OffscreenView();
974 rgb_color base
= LowColor();
975 uint32 flags
= be_control_look
->Flags(this);
977 // erase the is control flag before drawing the label so that the label
978 // will get drawn using B_PANEL_TEXT_COLOR
979 flags
&= ~BControlLook::B_IS_CONTROL
;
981 font_height fontHeight
;
982 GetFontHeight(&fontHeight
);
983 if (Orientation() == B_HORIZONTAL
) {
984 if (Label() != NULL
) {
985 be_control_look
->DrawLabel(view
, Label(), base
, flags
,
986 BPoint(0.0f
, ceilf(fontHeight
.ascent
)));
989 // the update text is updated in SetValue() only
990 if (fUpdateText
!= NULL
) {
991 be_control_look
->DrawLabel(view
, fUpdateText
, base
, flags
,
992 BPoint(bounds
.right
- StringWidth(fUpdateText
),
993 ceilf(fontHeight
.ascent
)));
996 if (fMinLimitLabel
!= NULL
) {
997 be_control_look
->DrawLabel(view
, fMinLimitLabel
, base
, flags
,
998 BPoint(0.0f
, bounds
.bottom
- fontHeight
.descent
));
1001 if (fMaxLimitLabel
!= NULL
) {
1002 be_control_look
->DrawLabel(view
, fMaxLimitLabel
, base
, flags
,
1003 BPoint(bounds
.right
- StringWidth(fMaxLimitLabel
),
1004 bounds
.bottom
- fontHeight
.descent
));
1007 float lineHeight
= ceilf(fontHeight
.ascent
) + ceilf(fontHeight
.descent
)
1008 + ceilf(fontHeight
.leading
);
1009 float baseLine
= ceilf(fontHeight
.ascent
);
1011 if (Label() != NULL
) {
1012 be_control_look
->DrawLabel(view
, Label(), base
, flags
,
1013 BPoint((bounds
.Width() - StringWidth(Label())) / 2.0,
1015 baseLine
+= lineHeight
;
1018 if (fMaxLimitLabel
!= NULL
) {
1019 be_control_look
->DrawLabel(view
, fMaxLimitLabel
, base
, flags
,
1020 BPoint((bounds
.Width() - StringWidth(fMaxLimitLabel
)) / 2.0,
1024 baseLine
= bounds
.bottom
- ceilf(fontHeight
.descent
);
1026 if (fMinLimitLabel
!= NULL
) {
1027 be_control_look
->DrawLabel(view
, fMinLimitLabel
, base
, flags
,
1028 BPoint((bounds
.Width() - StringWidth(fMinLimitLabel
)) / 2.0,
1030 baseLine
-= lineHeight
;
1033 if (fUpdateText
!= NULL
) {
1034 be_control_look
->DrawLabel(view
, fUpdateText
, base
, flags
,
1035 BPoint((bounds
.Width() - StringWidth(fUpdateText
)) / 2.0,
1046 BSlider::UpdateText() const
1053 BSlider::UpdateTextChanged()
1055 // update text label
1056 float oldWidth
= 0.0;
1057 if (fUpdateText
!= NULL
)
1058 oldWidth
= StringWidth(fUpdateText
);
1060 const char* oldUpdateText
= fUpdateText
;
1061 fUpdateText
= UpdateText();
1062 bool updateTextOnOff
= (fUpdateText
== NULL
&& oldUpdateText
!= NULL
)
1063 || (fUpdateText
!= NULL
&& oldUpdateText
== NULL
);
1065 float newWidth
= 0.0;
1066 if (fUpdateText
!= NULL
)
1067 newWidth
= StringWidth(fUpdateText
);
1069 float width
= ceilf(std::max(newWidth
, oldWidth
)) + 2.0f
;
1071 font_height fontHeight
;
1072 GetFontHeight(&fontHeight
);
1074 float height
= ceilf(fontHeight
.ascent
) + ceilf(fontHeight
.descent
);
1075 float lineHeight
= height
+ ceilf(fontHeight
.leading
);
1076 BRect
invalid(Bounds());
1077 if (fOrientation
== B_HORIZONTAL
)
1078 invalid
= BRect(invalid
.right
- width
, 0, invalid
.right
, height
);
1080 if (!updateTextOnOff
) {
1081 invalid
.left
= (invalid
.left
+ invalid
.right
- width
) / 2;
1082 invalid
.right
= invalid
.left
+ width
;
1083 if (fMinLimitLabel
!= NULL
)
1084 invalid
.bottom
-= lineHeight
;
1086 invalid
.top
= invalid
.bottom
- height
;
1089 Invalidate(invalid
);
1092 float oldMaxUpdateTextWidth
= fMaxUpdateTextWidth
;
1093 fMaxUpdateTextWidth
= MaxUpdateTextWidth();
1094 if (oldMaxUpdateTextWidth
!= fMaxUpdateTextWidth
)
1100 BSlider::BarFrame() const
1102 BRect
frame(Bounds());
1104 font_height fontHeight
;
1105 GetFontHeight(&fontHeight
);
1107 float textHeight
= ceilf(fontHeight
.ascent
) + ceilf(fontHeight
.descent
);
1108 float leading
= ceilf(fontHeight
.leading
);
1111 if (fStyle
== B_BLOCK_THUMB
)
1116 if (Orientation() == B_HORIZONTAL
) {
1117 frame
.left
= thumbInset
;
1118 frame
.top
= 6.0 + (Label() || fUpdateText
? textHeight
+ 4.0 : 0.0);
1119 frame
.right
-= thumbInset
;
1120 frame
.bottom
= frame
.top
+ fBarThickness
;
1122 frame
.left
= floorf((frame
.Width() - fBarThickness
) / 2.0);
1123 frame
.top
= thumbInset
;
1124 if (Label() != NULL
)
1125 frame
.top
+= textHeight
;
1127 if (fMaxLimitLabel
!= NULL
) {
1128 frame
.top
+= textHeight
;
1130 frame
.top
+= leading
;
1133 frame
.right
= frame
.left
+ fBarThickness
;
1134 frame
.bottom
= frame
.bottom
- thumbInset
;
1135 if (fMinLimitLabel
!= NULL
)
1136 frame
.bottom
-= textHeight
;
1138 if (fUpdateText
!= NULL
) {
1139 frame
.bottom
-= textHeight
;
1140 if (fMinLimitLabel
!= NULL
)
1141 frame
.bottom
-= leading
;
1150 BSlider::HashMarksFrame() const
1152 BRect
frame(BarFrame());
1154 if (fOrientation
== B_HORIZONTAL
) {
1156 frame
.bottom
+= 6.0;
1167 BSlider::ThumbFrame() const
1169 // TODO: The slider looks really ugly and broken when it is too little.
1170 // I would suggest using BarFrame() here to get the top and bottom coords
1171 // and spread them further apart for the thumb
1173 BRect frame
= Bounds();
1175 font_height fontHeight
;
1176 GetFontHeight(&fontHeight
);
1178 float textHeight
= ceilf(fontHeight
.ascent
) + ceilf(fontHeight
.descent
);
1180 if (fStyle
== B_BLOCK_THUMB
) {
1181 if (Orientation() == B_HORIZONTAL
) {
1182 frame
.left
= floorf(Position() * (_MaxPosition()
1183 - _MinPosition()) + _MinPosition()) - 8;
1184 frame
.top
= 2 + (Label() || fUpdateText
? textHeight
+ 4 : 0);
1185 frame
.right
= frame
.left
+ 17;
1186 frame
.bottom
= frame
.top
+ fBarThickness
+ 7;
1188 frame
.left
= floor((frame
.Width() - fBarThickness
) / 2) - 4;
1189 frame
.top
= floorf(Position() * (_MaxPosition()
1190 - _MinPosition()) + _MinPosition()) - 8;
1191 frame
.right
= frame
.left
+ fBarThickness
+ 7;
1192 frame
.bottom
= frame
.top
+ 17;
1195 if (Orientation() == B_HORIZONTAL
) {
1196 frame
.left
= floorf(Position() * (_MaxPosition()
1197 - _MinPosition()) + _MinPosition()) - 6;
1198 frame
.right
= frame
.left
+ 12;
1199 frame
.top
= 3 + fBarThickness
+ (Label() ? textHeight
+ 4 : 0);
1200 frame
.bottom
= frame
.top
+ 8;
1202 frame
.left
= floorf((frame
.Width() + fBarThickness
) / 2) - 3;
1203 frame
.top
= floorf(Position() * (_MaxPosition()
1204 - _MinPosition())) + _MinPosition() - 6;
1205 frame
.right
= frame
.left
+ 8;
1206 frame
.bottom
= frame
.top
+ 12;
1215 BSlider::SetFlags(uint32 flags
)
1217 BControl::SetFlags(flags
);
1222 BSlider::SetResizingMode(uint32 mode
)
1224 BControl::SetResizingMode(mode
);
1229 BSlider::GetPreferredSize(float* _width
, float* _height
)
1231 BSize preferredSize
= PreferredSize();
1233 if (Orientation() == B_HORIZONTAL
) {
1234 if (_width
!= NULL
) {
1235 // NOTE: For compatibility reasons, a horizontal BSlider
1236 // never shrinks horizontally. This only affects applications
1237 // which do not use the new layout system.
1238 *_width
= std::max(Bounds().Width(), preferredSize
.width
);
1241 if (_height
!= NULL
)
1242 *_height
= preferredSize
.height
;
1245 *_width
= preferredSize
.width
;
1247 if (_height
!= NULL
) {
1248 // NOTE: Similarly, a vertical BSlider never shrinks
1249 // vertically. This only affects applications which do not
1250 // use the new layout system.
1251 *_height
= std::max(Bounds().Height(), preferredSize
.height
);
1258 BSlider::ResizeToPreferred()
1260 BControl::ResizeToPreferred();
1265 BSlider::Invoke(BMessage
* message
)
1267 return BControl::Invoke(message
);
1272 BSlider::ResolveSpecifier(BMessage
* message
, int32 index
, BMessage
* specifier
,
1273 int32 command
, const char* property
)
1275 return BControl::ResolveSpecifier(message
, index
, specifier
, command
,
1281 BSlider::GetSupportedSuites(BMessage
* message
)
1283 return BControl::GetSupportedSuites(message
);
1288 BSlider::SetModificationMessage(BMessage
* message
)
1290 delete fModificationMessage
;
1291 fModificationMessage
= message
;
1296 BSlider::ModificationMessage() const
1298 return fModificationMessage
;
1303 BSlider::SetSnoozeAmount(int32 snoozeTime
)
1305 if (snoozeTime
< 10000)
1307 else if (snoozeTime
> 1000000)
1308 snoozeTime
= 1000000;
1310 fSnoozeAmount
= snoozeTime
;
1315 BSlider::SnoozeAmount() const
1317 return fSnoozeAmount
;
1322 BSlider::SetKeyIncrementValue(int32 incrementValue
)
1324 fKeyIncrementValue
= incrementValue
;
1329 BSlider::KeyIncrementValue() const
1331 return fKeyIncrementValue
;
1336 BSlider::SetHashMarkCount(int32 hashMarkCount
)
1338 fHashMarkCount
= hashMarkCount
;
1344 BSlider::HashMarkCount() const
1346 return fHashMarkCount
;
1351 BSlider::SetHashMarks(hash_mark_location where
)
1354 // TODO: enable if the hashmark look is influencing the control size!
1355 // InvalidateLayout();
1361 BSlider::HashMarks() const
1368 BSlider::SetStyle(thumb_style style
)
1377 BSlider::Style() const
1384 BSlider::SetBarColor(rgb_color barColor
)
1386 fBarColor
= barColor
;
1387 Invalidate(BarFrame());
1392 BSlider::BarColor() const
1399 BSlider::UseFillColor(bool useFill
, const rgb_color
* barColor
)
1401 fUseFillColor
= useFill
;
1403 if (useFill
&& barColor
)
1404 fFillColor
= *barColor
;
1406 Invalidate(BarFrame());
1411 BSlider::FillColor(rgb_color
* barColor
) const
1413 if (barColor
&& fUseFillColor
)
1414 *barColor
= fFillColor
;
1416 return fUseFillColor
;
1421 BSlider::OffscreenView() const
1423 #if USE_OFF_SCREEN_VIEW
1424 return fOffScreenView
;
1426 return (BView
*)this;
1432 BSlider::Orientation() const
1434 return fOrientation
;
1439 BSlider::SetOrientation(orientation posture
)
1441 if (fOrientation
== posture
)
1444 fOrientation
= posture
;
1451 BSlider::BarThickness() const
1453 return fBarThickness
;
1458 BSlider::SetBarThickness(float thickness
)
1460 if (thickness
< 1.0)
1463 thickness
= roundf(thickness
);
1465 if (thickness
!= fBarThickness
) {
1466 // calculate invalid barframe and extend by hashmark size
1469 if (fOrientation
== B_HORIZONTAL
)
1473 BRect invalid
= BarFrame().InsetByCopy(hInset
, vInset
) | ThumbFrame();
1475 fBarThickness
= thickness
;
1477 invalid
= invalid
| BarFrame().InsetByCopy(hInset
, vInset
)
1479 Invalidate(invalid
);
1486 BSlider::SetFont(const BFont
* font
, uint32 properties
)
1488 BControl::SetFont(font
, properties
);
1490 #if USE_OFF_SCREEN_VIEW
1491 if (fOffScreenView
&& fOffScreenBits
) {
1492 if (fOffScreenBits
->Lock()) {
1493 fOffScreenView
->SetFont(font
, properties
);
1494 fOffScreenBits
->Unlock();
1504 BSlider::SetLimits(int32 minimum
, int32 maximum
)
1506 if (minimum
<= maximum
) {
1507 fMinValue
= minimum
;
1508 fMaxValue
= maximum
;
1510 int32 value
= Value();
1511 value
= std::max(minimum
, value
);
1512 value
= std::min(maximum
, value
);
1514 if (value
!= Value())
1521 BSlider::MaxUpdateTextWidth()
1523 // very simplistic implementation that assumes the string will be widest
1524 // at the maximum value
1525 int32 value
= Value();
1526 SetValueNoUpdate(fMaxValue
);
1527 float width
= StringWidth(UpdateText());
1528 SetValueNoUpdate(value
);
1529 // in case the derived class uses a fixed buffer, the contents
1530 // should be reset for the old value
1537 // #pragma mark - layout related
1543 return BLayoutUtils::ComposeSize(ExplicitMinSize(), _ValidateMinSize());
1550 BSize maxSize
= _ValidateMinSize();
1551 if (fOrientation
== B_HORIZONTAL
)
1552 maxSize
.width
= B_SIZE_UNLIMITED
;
1554 maxSize
.height
= B_SIZE_UNLIMITED
;
1556 return BLayoutUtils::ComposeSize(ExplicitMaxSize(), maxSize
);
1561 BSlider::PreferredSize()
1563 BSize preferredSize
= _ValidateMinSize();
1564 if (fOrientation
== B_HORIZONTAL
)
1565 preferredSize
.width
= std::max(100.0f
, preferredSize
.width
);
1567 preferredSize
.height
= std::max(100.0f
, preferredSize
.height
);
1569 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(), preferredSize
);
1574 BSlider::SetIcon(const BBitmap
* icon
, uint32 flags
)
1576 return BControl::SetIcon(icon
, flags
);
1581 BSlider::LayoutInvalidated(bool descendants
)
1583 // invalidate cached preferred size
1584 fMinSize
.Set(-1, -1);
1588 // #pragma mark - private
1592 BSlider::_DrawBlockThumb()
1594 BRect frame
= ThumbFrame();
1595 BView
* view
= OffscreenView();
1597 rgb_color base
= ui_color(B_PANEL_BACKGROUND_COLOR
);
1598 uint32 flags
= be_control_look
->Flags(this);
1599 be_control_look
->DrawSliderThumb(view
, frame
, frame
, base
, flags
,
1605 BSlider::_DrawTriangleThumb()
1607 BRect frame
= ThumbFrame();
1608 BView
* view
= OffscreenView();
1609 rgb_color base
= ui_color(B_PANEL_BACKGROUND_COLOR
);
1610 uint32 flags
= be_control_look
->Flags(this);
1611 be_control_look
->DrawSliderTriangle(view
, frame
, frame
, base
, flags
,
1617 BSlider::_Location() const
1624 BSlider::_SetLocationForValue(int32 value
)
1627 float range
= (float)(fMaxValue
- fMinValue
);
1631 float pos
= (float)(value
- fMinValue
) / range
*
1632 (_MaxPosition() - _MinPosition());
1634 if (fOrientation
== B_HORIZONTAL
) {
1635 loc
.x
= ceil(_MinPosition() + pos
);
1639 loc
.y
= floor(_MaxPosition() - pos
);
1646 BSlider::_MinPosition() const
1648 if (fOrientation
== B_HORIZONTAL
)
1649 return BarFrame().left
+ 1.0f
;
1651 return BarFrame().bottom
- 1.0f
;
1656 BSlider::_MaxPosition() const
1658 if (fOrientation
== B_HORIZONTAL
)
1659 return BarFrame().right
- 1.0f
;
1661 return BarFrame().top
+ 1.0f
;
1666 BSlider::_ValidateMinSize()
1668 if (fMinSize
.width
>= 0) {
1669 // the preferred size is up to date
1673 font_height fontHeight
;
1674 GetFontHeight(&fontHeight
);
1677 float height
= 0.0f
;
1679 if (fMaxUpdateTextWidth
< 0.0f
)
1680 fMaxUpdateTextWidth
= MaxUpdateTextWidth();
1682 if (Orientation() == B_HORIZONTAL
) {
1683 height
= 12.0f
+ fBarThickness
;
1686 float labelWidth
= 0;
1687 int32 labelRows
= 0;
1688 float labelSpacing
= StringWidth("M") * 2;
1689 if (Label() != NULL
) {
1690 labelWidth
= StringWidth(Label());
1693 if (fMaxUpdateTextWidth
> 0.0f
) {
1695 labelWidth
+= labelSpacing
;
1697 labelWidth
+= fMaxUpdateTextWidth
;
1702 if (MinLimitLabel() != NULL
)
1703 width
= StringWidth(MinLimitLabel());
1705 if (MaxLimitLabel() != NULL
) {
1706 // some space between the labels
1707 if (MinLimitLabel() != NULL
)
1708 width
+= labelSpacing
;
1710 width
+= StringWidth(MaxLimitLabel());
1713 if (labelWidth
> width
)
1719 if (MinLimitLabel() || MaxLimitLabel())
1722 height
+= rows
* (ceilf(fontHeight
.ascent
)
1723 + ceilf(fontHeight
.descent
) + 4.0);
1726 width
= 12.0f
+ fBarThickness
;
1729 float lineHeightNoLeading
= ceilf(fontHeight
.ascent
)
1730 + ceilf(fontHeight
.descent
);
1731 float lineHeight
= lineHeightNoLeading
+ ceilf(fontHeight
.leading
);
1733 // find largest label
1734 float labelWidth
= 0;
1735 if (Label() != NULL
) {
1736 labelWidth
= StringWidth(Label());
1737 height
+= lineHeightNoLeading
;
1739 if (MaxLimitLabel() != NULL
) {
1740 labelWidth
= std::max(labelWidth
, StringWidth(MaxLimitLabel()));
1741 height
+= Label() ? lineHeight
: lineHeightNoLeading
;
1743 if (MinLimitLabel() != NULL
) {
1744 labelWidth
= std::max(labelWidth
, StringWidth(MinLimitLabel()));
1745 height
+= lineHeightNoLeading
;
1747 if (fMaxUpdateTextWidth
> 0.0f
) {
1748 labelWidth
= std::max(labelWidth
, fMaxUpdateTextWidth
);
1749 height
+= MinLimitLabel() ? lineHeight
: lineHeightNoLeading
;
1752 width
= std::max(labelWidth
, width
);
1755 fMinSize
.width
= width
;
1756 fMinSize
.height
= height
;
1758 ResetLayoutInvalidation();
1764 // #pragma mark - FBC padding
1766 void BSlider::_ReservedSlider6() {}
1767 void BSlider::_ReservedSlider7() {}
1768 void BSlider::_ReservedSlider8() {}
1769 void BSlider::_ReservedSlider9() {}
1770 void BSlider::_ReservedSlider10() {}
1771 void BSlider::_ReservedSlider11() {}
1772 void BSlider::_ReservedSlider12() {}
1776 BSlider::operator=(const BSlider
&)
1782 // #pragma mark - BeOS compatibility
1788 GetLimits__7BSliderPlT1(BSlider
* slider
, int32
* minimum
, int32
* maximum
)
1790 slider
->GetLimits(minimum
, maximum
);
1795 _ReservedSlider4__7BSlider(BSlider
* slider
, int32 minimum
, int32 maximum
)
1797 slider
->BSlider::SetLimits(minimum
, maximum
);
1801 _ReservedSlider5__7BSlider(BSlider
* slider
)
1803 return slider
->BSlider::MaxUpdateTextWidth();
1808 _ReservedSlider1__7BSlider(BSlider
* slider
, orientation _orientation
)
1810 slider
->BSlider::SetOrientation(_orientation
);
1815 _ReservedSlider2__7BSlider(BSlider
* slider
, float thickness
)
1817 slider
->BSlider::SetBarThickness(thickness
);
1822 _ReservedSlider3__7BSlider(BSlider
* slider
, const BFont
* font
,
1825 slider
->BSlider::SetFont(font
, properties
);
1829 #endif // __GNUC__ < 3
1833 B_IF_GCC_2(InvalidateLayout__7BSliderb
, _ZN7BSlider16InvalidateLayoutEb
)(
1834 BView
* view
, bool descendants
)
1836 perform_data_layout_invalidated data
;
1837 data
.descendants
= descendants
;
1839 view
->Perform(PERFORM_CODE_LAYOUT_INVALIDATED
, &data
);