1928ffe6dSJohn Scipione /*
249c29fa9Swaddlesplash * Copyright 2010-2012 Haiku, Inc. All rights reserved.
3928ffe6dSJohn Scipione * Distributed under the terms of the MIT license.
4928ffe6dSJohn Scipione *
5928ffe6dSJohn Scipione * Authors:
649c29fa9Swaddlesplash * DarkWyrm <bpmagic@columbus.rr.com>
749c29fa9Swaddlesplash * John Scipione <jscipione@gmail.com>
8928ffe6dSJohn Scipione */
9928ffe6dSJohn Scipione
10928ffe6dSJohn Scipione
11928ffe6dSJohn Scipione #include "FakeScrollBar.h"
12928ffe6dSJohn Scipione
13928ffe6dSJohn Scipione #include <Box.h>
14928ffe6dSJohn Scipione #include <ControlLook.h>
15928ffe6dSJohn Scipione #include <Message.h>
16928ffe6dSJohn Scipione #include <ScrollBar.h>
17928ffe6dSJohn Scipione #include <Shape.h>
18928ffe6dSJohn Scipione #include <Size.h>
19928ffe6dSJohn Scipione #include <Window.h>
20928ffe6dSJohn Scipione
21928ffe6dSJohn Scipione
22928ffe6dSJohn Scipione typedef enum {
23928ffe6dSJohn Scipione ARROW_LEFT = 0,
24928ffe6dSJohn Scipione ARROW_RIGHT,
25928ffe6dSJohn Scipione ARROW_UP,
26928ffe6dSJohn Scipione ARROW_DOWN,
27928ffe6dSJohn Scipione ARROW_NONE
28928ffe6dSJohn Scipione } arrow_direction;
29928ffe6dSJohn Scipione
30928ffe6dSJohn Scipione
FakeScrollBar(bool drawArrows,bool doubleArrows,BMessage * message)31928ffe6dSJohn Scipione FakeScrollBar::FakeScrollBar(bool drawArrows, bool doubleArrows,
3249c29fa9Swaddlesplash BMessage* message)
33928ffe6dSJohn Scipione :
3412a7abb6SJohn Scipione BControl("FakeScrollBar", NULL, message, B_WILL_DRAW | B_NAVIGABLE),
35928ffe6dSJohn Scipione fDrawArrows(drawArrows),
3649c29fa9Swaddlesplash fDoubleArrows(doubleArrows)
37928ffe6dSJohn Scipione {
38fe66a314Swaddlesplash SetExplicitMinSize(BSize(160, 20));
39fe66a314Swaddlesplash SetExplicitMaxSize(BSize(B_SIZE_UNLIMITED, 20));
40928ffe6dSJohn Scipione }
41928ffe6dSJohn Scipione
42928ffe6dSJohn Scipione
~FakeScrollBar(void)43928ffe6dSJohn Scipione FakeScrollBar::~FakeScrollBar(void)
44928ffe6dSJohn Scipione {
45928ffe6dSJohn Scipione }
46928ffe6dSJohn Scipione
47928ffe6dSJohn Scipione
48928ffe6dSJohn Scipione void
Draw(BRect updateRect)49928ffe6dSJohn Scipione FakeScrollBar::Draw(BRect updateRect)
50928ffe6dSJohn Scipione {
51*b2c77987SJohn Scipione rgb_color base = ui_color(B_PANEL_BACKGROUND_COLOR);
52928ffe6dSJohn Scipione
53*b2c77987SJohn Scipione uint32 flags = BControlLook::B_PARTIALLY_ACTIVATED;
54928ffe6dSJohn Scipione
55928ffe6dSJohn Scipione if (Value() == B_CONTROL_ON)
56928ffe6dSJohn Scipione SetHighColor(ui_color(B_CONTROL_MARK_COLOR));
57928ffe6dSJohn Scipione else
58*b2c77987SJohn Scipione SetHighColor(base);
59928ffe6dSJohn Scipione
60*b2c77987SJohn Scipione BRect rect(Bounds());
6112a7abb6SJohn Scipione
62*b2c77987SJohn Scipione // draw the selected border (2px)
63*b2c77987SJohn Scipione StrokeRect(rect);
64*b2c77987SJohn Scipione rect.InsetBy(1, 1);
65*b2c77987SJohn Scipione StrokeRect(rect);
66*b2c77987SJohn Scipione rect.InsetBy(1, 1);
67928ffe6dSJohn Scipione
68*b2c77987SJohn Scipione // draw a 1px gap
69*b2c77987SJohn Scipione SetHighColor(base);
70*b2c77987SJohn Scipione StrokeRect(rect);
71*b2c77987SJohn Scipione rect.InsetBy(1, 1);
72928ffe6dSJohn Scipione
73*b2c77987SJohn Scipione // draw a 1px border around the entire scroll bar
74*b2c77987SJohn Scipione be_control_look->DrawScrollBarBorder(this, rect, updateRect, base, flags,
75*b2c77987SJohn Scipione B_HORIZONTAL);
76fe66a314Swaddlesplash
77*b2c77987SJohn Scipione // inset past border
78*b2c77987SJohn Scipione rect.InsetBy(1, 1);
79fe66a314Swaddlesplash
80*b2c77987SJohn Scipione // draw arrow buttons
81fe66a314Swaddlesplash if (fDrawArrows) {
82*b2c77987SJohn Scipione BRect buttonFrame(rect.left, rect.top, rect.left + rect.Height(),
83*b2c77987SJohn Scipione rect.bottom);
84*b2c77987SJohn Scipione be_control_look->DrawScrollBarButton(this, buttonFrame, updateRect,
85*b2c77987SJohn Scipione base, flags, BControlLook::B_LEFT_ARROW, B_HORIZONTAL);
86fe66a314Swaddlesplash if (fDoubleArrows) {
87*b2c77987SJohn Scipione buttonFrame.OffsetBy(rect.Height() + 1, 0.0f);
88*b2c77987SJohn Scipione be_control_look->DrawScrollBarButton(this, buttonFrame,
89*b2c77987SJohn Scipione updateRect, base, flags, BControlLook::B_RIGHT_ARROW,
90*b2c77987SJohn Scipione B_HORIZONTAL);
91*b2c77987SJohn Scipione buttonFrame.OffsetTo(rect.right - ((rect.Height() * 2) + 1),
92*b2c77987SJohn Scipione rect.top);
93*b2c77987SJohn Scipione be_control_look->DrawScrollBarButton(this, buttonFrame,
94*b2c77987SJohn Scipione updateRect, base, flags, BControlLook::B_LEFT_ARROW,
95*b2c77987SJohn Scipione B_HORIZONTAL);
96*b2c77987SJohn Scipione }
97*b2c77987SJohn Scipione buttonFrame.OffsetTo(rect.right - rect.Height(), rect.top);
98*b2c77987SJohn Scipione be_control_look->DrawScrollBarButton(this, buttonFrame, updateRect,
99*b2c77987SJohn Scipione base, flags, BControlLook::B_RIGHT_ARROW, B_HORIZONTAL);
100fe66a314Swaddlesplash }
101fe66a314Swaddlesplash
102*b2c77987SJohn Scipione // inset rect to make room for arrows
103*b2c77987SJohn Scipione if (fDrawArrows) {
104*b2c77987SJohn Scipione if (fDoubleArrows)
105*b2c77987SJohn Scipione rect.InsetBy((rect.Height() + 1) * 2, 0.0f);
106*b2c77987SJohn Scipione else
107*b2c77987SJohn Scipione rect.InsetBy(rect.Height() + 1, 0.0f);
108*b2c77987SJohn Scipione }
109fe66a314Swaddlesplash
110*b2c77987SJohn Scipione // draw background and thumb
111*b2c77987SJohn Scipione float less = floorf(rect.Width() / 3);
112*b2c77987SJohn Scipione BRect thumbRect(rect.left + less, rect.top, rect.right - less,
113*b2c77987SJohn Scipione rect.bottom);
114*b2c77987SJohn Scipione BRect leftOfThumb(rect.left, thumbRect.top, thumbRect.left - 1,
115*b2c77987SJohn Scipione thumbRect.bottom);
116*b2c77987SJohn Scipione BRect rightOfThumb(thumbRect.right + 1, thumbRect.top, rect.right,
117*b2c77987SJohn Scipione thumbRect.bottom);
118fe66a314Swaddlesplash
119fe66a314Swaddlesplash be_control_look->DrawScrollBarBackground(this, leftOfThumb,
120*b2c77987SJohn Scipione rightOfThumb, updateRect, base, flags, B_HORIZONTAL);
121*b2c77987SJohn Scipione be_control_look->DrawScrollBarThumb(this, thumbRect, updateRect,
122*b2c77987SJohn Scipione ui_color(B_SCROLL_BAR_THUMB_COLOR), flags, B_HORIZONTAL, fKnobStyle);
123928ffe6dSJohn Scipione }
124928ffe6dSJohn Scipione
125928ffe6dSJohn Scipione
126928ffe6dSJohn Scipione void
MouseDown(BPoint point)12749c29fa9Swaddlesplash FakeScrollBar::MouseDown(BPoint point)
128928ffe6dSJohn Scipione {
12949c29fa9Swaddlesplash BControl::MouseDown(point);
130928ffe6dSJohn Scipione }
131928ffe6dSJohn Scipione
132928ffe6dSJohn Scipione
133928ffe6dSJohn Scipione void
MouseMoved(BPoint point,uint32 transit,const BMessage * message)13449c29fa9Swaddlesplash FakeScrollBar::MouseMoved(BPoint point, uint32 transit,
13549c29fa9Swaddlesplash const BMessage* message)
136928ffe6dSJohn Scipione {
13749c29fa9Swaddlesplash BControl::MouseMoved(point, transit, message);
138928ffe6dSJohn Scipione }
139928ffe6dSJohn Scipione
140928ffe6dSJohn Scipione
141928ffe6dSJohn Scipione void
MouseUp(BPoint point)14249c29fa9Swaddlesplash FakeScrollBar::MouseUp(BPoint point)
143928ffe6dSJohn Scipione {
144928ffe6dSJohn Scipione SetValue(B_CONTROL_ON);
145928ffe6dSJohn Scipione Invoke();
146928ffe6dSJohn Scipione
147928ffe6dSJohn Scipione Invalidate();
148928ffe6dSJohn Scipione
14949c29fa9Swaddlesplash BControl::MouseUp(point);
150928ffe6dSJohn Scipione }
151928ffe6dSJohn Scipione
152928ffe6dSJohn Scipione
153928ffe6dSJohn Scipione void
SetValue(int32 value)154928ffe6dSJohn Scipione FakeScrollBar::SetValue(int32 value)
155928ffe6dSJohn Scipione {
156928ffe6dSJohn Scipione if (value != Value()) {
157928ffe6dSJohn Scipione BControl::SetValueNoUpdate(value);
158928ffe6dSJohn Scipione Invalidate();
159928ffe6dSJohn Scipione }
160928ffe6dSJohn Scipione
16149c29fa9Swaddlesplash if (!value)
162928ffe6dSJohn Scipione return;
163928ffe6dSJohn Scipione
164928ffe6dSJohn Scipione BView* parent = Parent();
165928ffe6dSJohn Scipione BView* child = NULL;
166928ffe6dSJohn Scipione
167928ffe6dSJohn Scipione if (parent != NULL) {
168928ffe6dSJohn Scipione // If the parent is a BBox, the group parent is the parent of the BBox
169928ffe6dSJohn Scipione BBox* box = dynamic_cast<BBox*>(parent);
170928ffe6dSJohn Scipione
171928ffe6dSJohn Scipione if (box && box->LabelView() == this)
172928ffe6dSJohn Scipione parent = box->Parent();
173928ffe6dSJohn Scipione
174928ffe6dSJohn Scipione if (parent != NULL) {
175928ffe6dSJohn Scipione BBox* box = dynamic_cast<BBox*>(parent);
176928ffe6dSJohn Scipione
177928ffe6dSJohn Scipione // If the parent is a BBox, skip the label if there is one
178928ffe6dSJohn Scipione if (box && box->LabelView())
179928ffe6dSJohn Scipione child = parent->ChildAt(1);
180928ffe6dSJohn Scipione else
181928ffe6dSJohn Scipione child = parent->ChildAt(0);
182928ffe6dSJohn Scipione } else
183928ffe6dSJohn Scipione child = Window()->ChildAt(0);
18449c29fa9Swaddlesplash } else if (Window())
185928ffe6dSJohn Scipione child = Window()->ChildAt(0);
186928ffe6dSJohn Scipione
18749c29fa9Swaddlesplash while (child) {
188928ffe6dSJohn Scipione FakeScrollBar* scrollbar = dynamic_cast<FakeScrollBar*>(child);
189928ffe6dSJohn Scipione
190928ffe6dSJohn Scipione if (scrollbar != NULL && (scrollbar != this))
191928ffe6dSJohn Scipione scrollbar->SetValue(B_CONTROL_OFF);
192928ffe6dSJohn Scipione else {
193928ffe6dSJohn Scipione // If the child is a BBox, check if the label is a scrollbarbutton
194928ffe6dSJohn Scipione BBox* box = dynamic_cast<BBox*>(child);
195928ffe6dSJohn Scipione
196928ffe6dSJohn Scipione if (box && box->LabelView()) {
197928ffe6dSJohn Scipione scrollbar = dynamic_cast<FakeScrollBar*>(box->LabelView());
198928ffe6dSJohn Scipione
199928ffe6dSJohn Scipione if (scrollbar != NULL && (scrollbar != this))
200928ffe6dSJohn Scipione scrollbar->SetValue(B_CONTROL_OFF);
201928ffe6dSJohn Scipione }
202928ffe6dSJohn Scipione }
203928ffe6dSJohn Scipione
204928ffe6dSJohn Scipione child = child->NextSibling();
205928ffe6dSJohn Scipione }
206928ffe6dSJohn Scipione
207928ffe6dSJohn Scipione //ASSERT(Value() == B_CONTROL_ON);
208928ffe6dSJohn Scipione }
209928ffe6dSJohn Scipione
210928ffe6dSJohn Scipione
21149c29fa9Swaddlesplash // #pragma mark -
21249c29fa9Swaddlesplash
21349c29fa9Swaddlesplash
214928ffe6dSJohn Scipione void
SetDoubleArrows(bool doubleArrows)21568c70f9bSJohn Scipione FakeScrollBar::SetDoubleArrows(bool doubleArrows)
216928ffe6dSJohn Scipione {
21768c70f9bSJohn Scipione fDoubleArrows = doubleArrows;
218928ffe6dSJohn Scipione Invalidate();
219928ffe6dSJohn Scipione }
220928ffe6dSJohn Scipione
221928ffe6dSJohn Scipione
222928ffe6dSJohn Scipione void
SetKnobStyle(uint32 knobStyle)22368c70f9bSJohn Scipione FakeScrollBar::SetKnobStyle(uint32 knobStyle)
224928ffe6dSJohn Scipione {
22568c70f9bSJohn Scipione fKnobStyle = knobStyle;
226928ffe6dSJohn Scipione Invalidate();
227928ffe6dSJohn Scipione }
228928ffe6dSJohn Scipione
229928ffe6dSJohn Scipione
230928ffe6dSJohn Scipione void
SetFromScrollBarInfo(const scroll_bar_info & info)231928ffe6dSJohn Scipione FakeScrollBar::SetFromScrollBarInfo(const scroll_bar_info &info)
232928ffe6dSJohn Scipione {
233928ffe6dSJohn Scipione fDoubleArrows = info.double_arrows;
234928ffe6dSJohn Scipione fKnobStyle = info.knob;
235928ffe6dSJohn Scipione Invalidate();
236928ffe6dSJohn Scipione }
237928ffe6dSJohn Scipione
238928ffe6dSJohn Scipione
23949c29fa9Swaddlesplash // #pragma mark -
240928ffe6dSJohn Scipione
241928ffe6dSJohn Scipione
242928ffe6dSJohn Scipione void
_DrawArrowButton(int32 direction,BRect rect,const BRect & updateRect)243f9b1a47fSRyan Leavengood FakeScrollBar::_DrawArrowButton(int32 direction, BRect rect,
244928ffe6dSJohn Scipione const BRect& updateRect)
245928ffe6dSJohn Scipione {
246f9b1a47fSRyan Leavengood if (!updateRect.Intersects(rect))
247928ffe6dSJohn Scipione return;
248928ffe6dSJohn Scipione
249f9b1a47fSRyan Leavengood uint32 flags = 0;
250928ffe6dSJohn Scipione
251f9b1a47fSRyan Leavengood rgb_color baseColor = tint_color(ui_color(B_PANEL_BACKGROUND_COLOR),
252f9b1a47fSRyan Leavengood B_LIGHTEN_1_TINT);
253928ffe6dSJohn Scipione
254f9b1a47fSRyan Leavengood be_control_look->DrawButtonBackground(this, rect, updateRect, baseColor,
255f9b1a47fSRyan Leavengood flags, BControlLook::B_ALL_BORDERS, B_HORIZONTAL);
256928ffe6dSJohn Scipione
257f9b1a47fSRyan Leavengood rect.InsetBy(-1, -1);
258f9b1a47fSRyan Leavengood be_control_look->DrawArrowShape(this, rect, updateRect,
259f9b1a47fSRyan Leavengood baseColor, direction, flags, B_DARKEN_MAX_TINT);
260928ffe6dSJohn Scipione }
261