2 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
7 #include "SettingsWindow.h"
13 #include <LayoutBuilder.h>
14 #include <GroupLayoutBuilder.h>
18 #undef B_TRANSLATION_CONTEXT
19 #define B_TRANSLATION_CONTEXT "SettingsWindow"
22 static const uint32 kMsgUpdateTimeInterval
= 'upti';
24 static const bigtime_t kUpdateIntervals
[] = {
25 25, 50, 75, 100, 250, 500, 1000, 2000
27 static const size_t kNumUpdateIntervals
28 = sizeof(kUpdateIntervals
) / sizeof(kUpdateIntervals
[0]);
31 class IntervalSlider
: public BSlider
{
33 IntervalSlider(const char* label
, BMessage
* message
, uint32 levels
)
34 : BSlider("intervalSlider", label
, message
, 0, levels
- 1, B_HORIZONTAL
)
36 BString
min(_TextFor(0));
37 BString
max(_TextFor(levels
- 1));
38 SetLimitLabels(min
.String(), max
.String());
39 SetHashMarks(B_HASH_MARKS_BOTTOM
);
40 SetHashMarkCount(levels
);
41 SetViewUIColor(B_PANEL_BACKGROUND_COLOR
);
44 SetModificationMessage(new BMessage(*message
));
47 void SetInterval(bigtime_t interval
)
52 int32 bestDiff
= INT32_MAX
;
54 for (uint32 i
= 0; i
< kNumUpdateIntervals
; i
++) {
55 int32 diff
= abs(kUpdateIntervals
[i
] - interval
);
56 if (diff
< bestDiff
) {
65 virtual const char* UpdateText() const
67 return _TextFor(Value());
71 const char* _TextFor(uint32 level
) const
73 if (level
>= kNumUpdateIntervals
)
76 bigtime_t interval
= kUpdateIntervals
[level
];
77 if ((interval
% 1000) == 0)
78 snprintf(fText
, sizeof(fText
), B_TRANSLATE("%lld sec."), interval
/ 1000);
80 snprintf(fText
, sizeof(fText
), B_TRANSLATE("%lld ms"), interval
);
85 mutable char fText
[64];
92 SettingsWindow::SettingsWindow(ActivityWindow
* target
)
93 : BWindow(_RelativeTo(target
),
94 B_TRANSLATE_CONTEXT("Settings", "ActivityWindow"), B_FLOATING_WINDOW
,
95 B_ASYNCHRONOUS_CONTROLS
| B_NOT_ZOOMABLE
| B_AUTO_UPDATE_SIZE_LIMITS
),
98 fIntervalSlider
= new IntervalSlider(B_TRANSLATE("Update time interval:"),
99 new BMessage(kMsgUpdateTimeInterval
), kNumUpdateIntervals
);
100 fIntervalSlider
->SetInterval(target
->RefreshInterval());
103 BLayoutBuilder::Group
<>(this, B_VERTICAL
)
104 .Add(fIntervalSlider
)
105 .SetInsets(B_USE_WINDOW_SPACING
);
107 if (target
->IsAlwaysOnTop())
108 SetFeel(B_MODAL_ALL_WINDOW_FEEL
);
112 SettingsWindow::~SettingsWindow()
118 SettingsWindow::MessageReceived(BMessage
* message
)
120 switch (message
->what
) {
121 case kMsgUpdateTimeInterval
:
124 if (message
->FindInt32("be:value", &level
) != B_OK
)
127 BMessage
update(kMsgTimeIntervalUpdated
);
128 update
.AddInt64("interval", kUpdateIntervals
[level
] * 1000LL);
130 fTarget
.SendMessage(&update
);
135 BWindow::MessageReceived(message
);
142 SettingsWindow::QuitRequested()
149 SettingsWindow::_RelativeTo(BWindow
* window
)
151 BRect frame
= window
->Frame();
152 return BRect(frame
.right
- 150, frame
.top
+ frame
.Height() / 4,
153 frame
.right
+ 200, frame
.top
+ frame
.Height() / 4 + 50);