tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / preferences / time / SectionEdit.cpp
bloba00461daba33f8fcf64aa1110dfb921fa0ca370f
1 /*
2 * Copyright 2004-2011, Haiku, Inc. All Rights Reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Mike Berg <mike@berg-net.us>
7 * Julun <host.haiku@gmx.de>
8 * Hamish Morrison <hamish@lavabit.com>
9 */
12 #include "SectionEdit.h"
14 #include <Bitmap.h>
15 #include <ControlLook.h>
16 #include <LayoutUtils.h>
17 #include <List.h>
18 #include <Window.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),
29 fFocus(-1),
30 fSectionCount(sections),
31 fHoldValue(0)
36 TSectionEdit::~TSectionEdit()
41 void
42 TSectionEdit::AttachedToWindow()
44 if (Parent())
45 SetViewColor(Parent()->ViewColor());
49 void
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));
63 void
64 TSectionEdit::MouseDown(BPoint where)
66 MakeFocus(true);
68 if (fUpRect.Contains(where))
69 DoUpPress();
70 else if (fDownRect.Contains(where))
71 DoDownPress();
72 else if (fSectionCount > 0) {
73 for (uint32 idx = 0; idx < fSectionCount; idx++) {
74 if (FrameForSection(idx).Contains(where)) {
75 SectionFocus(idx);
76 return;
83 BSize
84 TSectionEdit::MaxSize()
86 return BLayoutUtils::ComposeSize(ExplicitMaxSize(),
87 BSize(B_SIZE_UNLIMITED, PreferredHeight()));
91 BSize
92 TSectionEdit::MinSize()
94 BSize minSize;
95 minSize.height = PreferredHeight();
96 minSize.width = (SeparatorWidth() + MinSectionWidth())
97 * fSectionCount;
98 return BLayoutUtils::ComposeSize(ExplicitMinSize(),
99 minSize);
103 BSize
104 TSectionEdit::PreferredSize()
106 return BLayoutUtils::ComposeSize(ExplicitPreferredSize(),
107 MinSize());
111 BRect
112 TSectionEdit::FrameForSection(uint32 index)
114 BRect area = SectionArea();
115 float sepWidth = SeparatorWidth();
117 float width = (area.Width() -
118 sepWidth * (fSectionCount - 1))
119 / fSectionCount;
120 area.left += index * (width + sepWidth);
121 area.right = area.left + width;
123 return area;
127 BRect
128 TSectionEdit::FrameForSeparator(uint32 index)
130 BRect area = SectionArea();
131 float sepWidth = SeparatorWidth();
133 float width = (area.Width() -
134 sepWidth * (fSectionCount - 1))
135 / fSectionCount;
136 area.left += (index + 1) * width + index * sepWidth;
137 area.right = area.left + sepWidth;
139 return area;
143 void
144 TSectionEdit::MakeFocus(bool focused)
146 if (focused == IsFocus())
147 return;
149 BControl::MakeFocus(focused);
151 if (fFocus == -1)
152 SectionFocus(0);
153 else
154 SectionFocus(fFocus);
158 void
159 TSectionEdit::KeyDown(const char* bytes, int32 numbytes)
161 if (fFocus == -1)
162 SectionFocus(0);
164 switch (bytes[0]) {
165 case B_LEFT_ARROW:
166 fFocus -= 1;
167 if (fFocus < 0)
168 fFocus = fSectionCount - 1;
169 SectionFocus(fFocus);
170 break;
172 case B_RIGHT_ARROW:
173 fFocus += 1;
174 if ((uint32)fFocus >= fSectionCount)
175 fFocus = 0;
176 SectionFocus(fFocus);
177 break;
179 case B_UP_ARROW:
180 DoUpPress();
181 break;
183 case B_DOWN_ARROW:
184 DoDownPress();
185 break;
187 default:
188 BControl::KeyDown(bytes, numbytes);
189 break;
191 Draw(Bounds());
195 void
196 TSectionEdit::DispatchMessage()
198 BMessage message(H_USER_CHANGE);
199 BuildDispatch(&message);
200 Window()->PostMessage(&message);
204 uint32
205 TSectionEdit::CountSections() const
207 return fSectionCount;
211 int32
212 TSectionEdit::FocusIndex() const
214 return fFocus;
218 BRect
219 TSectionEdit::SectionArea() const
221 BRect sectionArea = Bounds().InsetByCopy(2, 2);
222 sectionArea.right -= kArrowAreaWidth;
223 return sectionArea;
227 void
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),
245 fUpRect.top + 1);
246 BPoint left(fUpRect.left + 3, fUpRect.bottom - 1);
247 BPoint right(left.x + 2 * (middle.x - left.x), fUpRect.bottom - 1);
249 SetPenSize(2);
250 SetLowColor(ViewColor());
252 if (updateRect.Intersects(fUpRect)) {
253 FillRect(fUpRect, B_SOLID_LOW);
254 BeginLineArray(2);
255 AddLine(left, middle, HighColor());
256 AddLine(middle, right, HighColor());
257 EndLineArray();
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);
264 BeginLineArray(2);
265 AddLine(left, middle, HighColor());
266 AddLine(middle, right, HighColor());
267 EndLineArray();
270 SetPenSize(1);