1 /* 2 * Copyright 2009, Axel Dörfler, axeld@pinc-software.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "SettingsWindow.h" 8 9 #include <stdio.h> 10 #include <stdlib.h> 11 12 #include <GridLayoutBuilder.h> 13 #include <GroupLayoutBuilder.h> 14 #include <Slider.h> 15 #include <String.h> 16 17 18 static const uint32 kMsgUpdateTimeInterval = 'upti'; 19 20 static const bigtime_t kUpdateIntervals[] = { 21 25, 50, 75, 100, 250, 500, 1000, 2000 22 }; 23 static const size_t kNumUpdateIntervals 24 = sizeof(kUpdateIntervals) / sizeof(kUpdateIntervals[0]); 25 26 27 class IntervalSlider : public BSlider { 28 public: 29 IntervalSlider(const char* label, BMessage* message, uint32 levels) 30 : BSlider("intervalSlider", label, message, 0, levels - 1, B_HORIZONTAL) 31 { 32 BString min(_TextFor(0)); 33 BString max(_TextFor(levels - 1)); 34 SetLimitLabels(min.String(), max.String()); 35 SetHashMarks(B_HASH_MARKS_BOTTOM); 36 SetHashMarkCount(levels); 37 38 if (message != NULL) 39 SetModificationMessage(new BMessage(*message)); 40 } 41 42 void SetInterval(bigtime_t interval) 43 { 44 interval /= 1000; 45 46 // Find closest index 47 int32 bestDiff = LONG_MAX; 48 uint32 bestIndex = 0; 49 for (uint32 i = 0; i < kNumUpdateIntervals; i++) { 50 int32 diff = abs(kUpdateIntervals[i] - interval); 51 if (diff < bestDiff) { 52 bestDiff = diff; 53 bestIndex = i; 54 } 55 } 56 57 SetValue(bestIndex); 58 } 59 60 virtual const char* UpdateText() const 61 { 62 return _TextFor(Value()); 63 } 64 65 private: 66 const char* _TextFor(uint32 level) const 67 { 68 if (level >= kNumUpdateIntervals) 69 return NULL; 70 71 bigtime_t interval = kUpdateIntervals[level]; 72 if ((interval % 1000) == 0) 73 snprintf(fText, sizeof(fText), "%lld sec.", interval / 1000); 74 else 75 snprintf(fText, sizeof(fText), "%lld ms", interval); 76 77 return fText; 78 } 79 80 mutable char fText[64]; 81 }; 82 83 84 // #pragma mark - 85 86 87 SettingsWindow::SettingsWindow(ActivityWindow* target) 88 : BWindow(_RelativeTo(target), "Settings", B_FLOATING_WINDOW, 89 B_ASYNCHRONOUS_CONTROLS | B_NOT_ZOOMABLE | B_AUTO_UPDATE_SIZE_LIMITS), 90 fTarget(target) 91 { 92 SetLayout(new BGroupLayout(B_VERTICAL)); 93 94 fIntervalSlider = new IntervalSlider("Update time interval:", 95 new BMessage(kMsgUpdateTimeInterval), kNumUpdateIntervals); 96 fIntervalSlider->SetInterval(target->RefreshInterval()); 97 98 // controls pane 99 AddChild(BGroupLayoutBuilder(B_VERTICAL) 100 .Add(fIntervalSlider) 101 .SetInsets(10, 10, 10, 10) 102 ); 103 } 104 105 106 SettingsWindow::~SettingsWindow() 107 { 108 } 109 110 111 void 112 SettingsWindow::MessageReceived(BMessage* message) 113 { 114 switch (message->what) { 115 case kMsgUpdateTimeInterval: 116 { 117 int32 level = 0; 118 if (message->FindInt32("be:value", &level) != B_OK) 119 break; 120 121 BMessage update(kMsgTimeIntervalUpdated); 122 update.AddInt64("interval", kUpdateIntervals[level] * 1000LL); 123 124 fTarget.SendMessage(&update); 125 break; 126 } 127 128 default: 129 BWindow::MessageReceived(message); 130 break; 131 } 132 } 133 134 135 bool 136 SettingsWindow::QuitRequested() 137 { 138 return true; 139 } 140 141 142 BRect 143 SettingsWindow::_RelativeTo(BWindow* window) 144 { 145 BRect frame = window->Frame(); 146 return BRect(frame.right - 150, frame.top + frame.Height() / 4, 147 frame.right + 200, frame.top + frame.Height() / 4 + 50); 148 } 149 150