btrfs: Attempt to fix GCC2 build.
[haiku.git] / src / apps / activitymonitor / SettingsWindow.cpp
blob57b54571aa9f854a289b5619c3e2e35ba0064606
1 /*
2 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "SettingsWindow.h"
9 #include <stdio.h>
10 #include <stdlib.h>
12 #include <Catalog.h>
13 #include <LayoutBuilder.h>
14 #include <GroupLayoutBuilder.h>
15 #include <Slider.h>
16 #include <String.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 {
32 public:
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);
43 if (message != NULL)
44 SetModificationMessage(new BMessage(*message));
47 void SetInterval(bigtime_t interval)
49 interval /= 1000;
51 // Find closest index
52 int32 bestDiff = INT32_MAX;
53 uint32 bestIndex = 0;
54 for (uint32 i = 0; i < kNumUpdateIntervals; i++) {
55 int32 diff = abs(kUpdateIntervals[i] - interval);
56 if (diff < bestDiff) {
57 bestDiff = diff;
58 bestIndex = i;
62 SetValue(bestIndex);
65 virtual const char* UpdateText() const
67 return _TextFor(Value());
70 private:
71 const char* _TextFor(uint32 level) const
73 if (level >= kNumUpdateIntervals)
74 return NULL;
76 bigtime_t interval = kUpdateIntervals[level];
77 if ((interval % 1000) == 0)
78 snprintf(fText, sizeof(fText), B_TRANSLATE("%lld sec."), interval / 1000);
79 else
80 snprintf(fText, sizeof(fText), B_TRANSLATE("%lld ms"), interval);
82 return fText;
85 mutable char fText[64];
89 // #pragma mark -
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),
96 fTarget(target)
98 fIntervalSlider = new IntervalSlider(B_TRANSLATE("Update time interval:"),
99 new BMessage(kMsgUpdateTimeInterval), kNumUpdateIntervals);
100 fIntervalSlider->SetInterval(target->RefreshInterval());
102 // controls pane
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()
117 void
118 SettingsWindow::MessageReceived(BMessage* message)
120 switch (message->what) {
121 case kMsgUpdateTimeInterval:
123 int32 level = 0;
124 if (message->FindInt32("be:value", &level) != B_OK)
125 break;
127 BMessage update(kMsgTimeIntervalUpdated);
128 update.AddInt64("interval", kUpdateIntervals[level] * 1000LL);
130 fTarget.SendMessage(&update);
131 break;
134 default:
135 BWindow::MessageReceived(message);
136 break;
141 bool
142 SettingsWindow::QuitRequested()
144 return true;
148 BRect
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);