2 * Copyright 2004-2011, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
6 * Mike Berg <mike@berg-net.us>
7 * Julun <host.haiku@gmx.de>
8 * Hamish Morrison <hamish@lavabit.com>
12 #include "SectionEdit.h"
15 #include <ControlLook.h>
16 #include <LayoutUtils.h>
20 #include "TimeMessages.h"
23 const uint32 kArrowAreaWidth
= 16;
26 TSectionEdit::TSectionEdit(const char* name
, uint32 sections
)
28 BControl(name
, NULL
, NULL
, B_WILL_DRAW
| B_NAVIGABLE
),
30 fSectionCount(sections
),
36 TSectionEdit::~TSectionEdit()
42 TSectionEdit::AttachedToWindow()
45 SetViewColor(Parent()->ViewColor());
50 TSectionEdit::Draw(BRect updateRect
)
52 DrawBorder(updateRect
);
54 for (uint32 idx
= 0; idx
< fSectionCount
; idx
++) {
55 DrawSection(idx
, FrameForSection(idx
),
56 ((uint32
)fFocus
== idx
) && IsFocus());
57 if (idx
< fSectionCount
- 1)
58 DrawSeparator(idx
, FrameForSeparator(idx
));
64 TSectionEdit::MouseDown(BPoint where
)
68 if (fUpRect
.Contains(where
))
70 else if (fDownRect
.Contains(where
))
72 else if (fSectionCount
> 0) {
73 for (uint32 idx
= 0; idx
< fSectionCount
; idx
++) {
74 if (FrameForSection(idx
).Contains(where
)) {
84 TSectionEdit::MaxSize()
86 return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
87 BSize(B_SIZE_UNLIMITED
, PreferredHeight()));
92 TSectionEdit::MinSize()
95 minSize
.height
= PreferredHeight();
96 minSize
.width
= (SeparatorWidth() + MinSectionWidth())
98 return BLayoutUtils::ComposeSize(ExplicitMinSize(),
104 TSectionEdit::PreferredSize()
106 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(),
112 TSectionEdit::FrameForSection(uint32 index
)
114 BRect area
= SectionArea();
115 float sepWidth
= SeparatorWidth();
117 float width
= (area
.Width() -
118 sepWidth
* (fSectionCount
- 1))
120 area
.left
+= index
* (width
+ sepWidth
);
121 area
.right
= area
.left
+ width
;
128 TSectionEdit::FrameForSeparator(uint32 index
)
130 BRect area
= SectionArea();
131 float sepWidth
= SeparatorWidth();
133 float width
= (area
.Width() -
134 sepWidth
* (fSectionCount
- 1))
136 area
.left
+= (index
+ 1) * width
+ index
* sepWidth
;
137 area
.right
= area
.left
+ sepWidth
;
144 TSectionEdit::MakeFocus(bool focused
)
146 if (focused
== IsFocus())
149 BControl::MakeFocus(focused
);
154 SectionFocus(fFocus
);
159 TSectionEdit::KeyDown(const char* bytes
, int32 numbytes
)
168 fFocus
= fSectionCount
- 1;
169 SectionFocus(fFocus
);
174 if ((uint32
)fFocus
>= fSectionCount
)
176 SectionFocus(fFocus
);
188 BControl::KeyDown(bytes
, numbytes
);
196 TSectionEdit::DispatchMessage()
198 BMessage
message(H_USER_CHANGE
);
199 BuildDispatch(&message
);
200 Window()->PostMessage(&message
);
205 TSectionEdit::CountSections() const
207 return fSectionCount
;
212 TSectionEdit::FocusIndex() const
219 TSectionEdit::SectionArea() const
221 BRect sectionArea
= Bounds().InsetByCopy(2, 2);
222 sectionArea
.right
-= kArrowAreaWidth
;
228 TSectionEdit::DrawBorder(const BRect
& updateRect
)
230 BRect
bounds(Bounds());
231 bool showFocus
= (IsFocus() && Window() && Window()->IsActive());
233 be_control_look
->DrawBorder(this, bounds
, updateRect
, ViewColor(),
234 B_FANCY_BORDER
, showFocus
? BControlLook::B_FOCUSED
: 0);
236 // draw up/down control
238 bounds
.left
= bounds
.right
- kArrowAreaWidth
;
239 bounds
.right
= Bounds().right
- 2;
240 fUpRect
.Set(bounds
.left
+ 3, bounds
.top
+ 2, bounds
.right
,
241 bounds
.bottom
/ 2.0);
242 fDownRect
= fUpRect
.OffsetByCopy(0, fUpRect
.Height() + 2);
244 BPoint
middle(floorf(fUpRect
.left
+ fUpRect
.Width() / 2),
246 BPoint
left(fUpRect
.left
+ 3, fUpRect
.bottom
- 1);
247 BPoint
right(left
.x
+ 2 * (middle
.x
- left
.x
), fUpRect
.bottom
- 1);
250 SetLowColor(ViewColor());
252 if (updateRect
.Intersects(fUpRect
)) {
253 FillRect(fUpRect
, B_SOLID_LOW
);
255 AddLine(left
, middle
, HighColor());
256 AddLine(middle
, right
, HighColor());
259 if (updateRect
.Intersects(fDownRect
)) {
260 middle
.y
= fDownRect
.bottom
- 1;
261 left
.y
= right
.y
= fDownRect
.top
+ 1;
263 FillRect(fDownRect
, B_SOLID_LOW
);
265 AddLine(left
, middle
, HighColor());
266 AddLine(middle
, right
, HighColor());