libroot/posix/stdio: Remove unused portions.
[haiku.git] / src / preferences / mouse / SettingsView.cpp
blobfdb9b27c1ded21abb73c236d6f8cc86a63ae74e9
1 /*
2 * Copyright 2003-2014 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Jérôme Duval,
7 * Axel Dörfler (axeld@pinc-software.de)
8 * Andrew McCall (mccall@digitalparadise.co.uk)
9 * Brecht Machiels (brecht@mos6581.org)
13 #include "SettingsView.h"
15 #include <Bitmap.h>
16 #include <Box.h>
17 #include <Button.h>
18 #include <Catalog.h>
19 #include <Debug.h>
20 #include <InterfaceDefs.h>
21 #include <LayoutBuilder.h>
22 #include <Locale.h>
23 #include <MenuField.h>
24 #include <MenuItem.h>
25 #include <PopUpMenu.h>
26 #include <SeparatorView.h>
27 #include <Slider.h>
28 #include <TextControl.h>
29 #include <TranslationUtils.h>
30 #include <Window.h>
32 #include "MouseConstants.h"
33 #include "MouseSettings.h"
34 #include "MouseView.h"
37 static int32
38 mouse_mode_to_index(mode_mouse mode)
40 switch (mode) {
41 case B_NORMAL_MOUSE:
42 default:
43 return 0;
44 case B_CLICK_TO_FOCUS_MOUSE:
45 return 1;
46 case B_FOCUS_FOLLOWS_MOUSE:
47 return 2;
52 static int32
53 focus_follows_mouse_mode_to_index(mode_focus_follows_mouse mode)
55 switch (mode) {
56 case B_NORMAL_FOCUS_FOLLOWS_MOUSE:
57 default:
58 return 0;
59 case B_WARP_FOCUS_FOLLOWS_MOUSE:
60 return 1;
61 case B_INSTANT_WARP_FOCUS_FOLLOWS_MOUSE:
62 return 2;
67 // #pragma mark -
69 #undef B_TRANSLATION_CONTEXT
70 #define B_TRANSLATION_CONTEXT "SettingsView"
72 SettingsView::SettingsView(MouseSettings& settings)
73 : BBox("main_view"),
74 fSettings(settings)
76 // Add the "Mouse Type" pop up menu
77 fTypeMenu = new BPopUpMenu("unknown");
78 fTypeMenu->AddItem(new BMenuItem(B_TRANSLATE("1-Button"),
79 new BMessage(kMsgMouseType)));
80 fTypeMenu->AddItem(new BMenuItem(B_TRANSLATE("2-Button"),
81 new BMessage(kMsgMouseType)));
82 fTypeMenu->AddItem(new BMenuItem(B_TRANSLATE("3-Button"),
83 new BMessage(kMsgMouseType)));
85 BMenuField* typeField = new BMenuField(B_TRANSLATE("Mouse type:"),
86 fTypeMenu);
87 typeField->SetAlignment(B_ALIGN_LEFT);
89 // Create the "Double-click speed slider...
90 fClickSpeedSlider = new BSlider("double_click_speed",
91 B_TRANSLATE("Double-click speed"), new BMessage(kMsgDoubleClickSpeed),
92 0, 1000, B_HORIZONTAL);
93 fClickSpeedSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
94 fClickSpeedSlider->SetHashMarkCount(7);
96 // Create the "Mouse Speed" slider...
97 fMouseSpeedSlider = new BSlider("mouse_speed", B_TRANSLATE("Mouse speed"),
98 new BMessage(kMsgMouseSpeed), 0, 1000, B_HORIZONTAL);
99 fMouseSpeedSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
100 fMouseSpeedSlider->SetHashMarkCount(7);
102 // Create the "Mouse Acceleration" slider...
103 fAccelerationSlider = new BSlider("mouse_acceleration",
104 B_TRANSLATE("Mouse acceleration"),
105 new BMessage(kMsgAccelerationFactor), 0, 1000, B_HORIZONTAL);
106 fAccelerationSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
107 fAccelerationSlider->SetHashMarkCount(7);
109 // Mouse image...
110 fMouseView = new MouseView(fSettings);
112 // Create the "Double-click test area" text box...
113 const char* label = B_TRANSLATE("Double-click test area");
114 BTextControl* doubleClickTextControl = new BTextControl(NULL,
115 label, NULL);
116 doubleClickTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_CENTER);
117 doubleClickTextControl->SetExplicitMinSize(
118 BSize(StringWidth(label), B_SIZE_UNSET));
120 // Add the "Mouse focus mode" pop up menu
121 fFocusMenu = new BPopUpMenu(B_TRANSLATE("Click to focus and raise"));
123 const char *focusLabels[] = {B_TRANSLATE_MARK("Click to focus and raise"),
124 B_TRANSLATE_MARK("Click to focus"),
125 B_TRANSLATE_MARK("Focus follows mouse")};
126 const mode_mouse focusModes[] = {B_NORMAL_MOUSE, B_CLICK_TO_FOCUS_MOUSE,
127 B_FOCUS_FOLLOWS_MOUSE};
129 for (int i = 0; i < 3; i++) {
130 BMessage* message = new BMessage(kMsgMouseFocusMode);
131 message->AddInt32("mode", focusModes[i]);
133 fFocusMenu->AddItem(new BMenuItem(B_TRANSLATE_NOCOLLECT(focusLabels[i]),
134 message));
137 BMenuField* focusField = new BMenuField(B_TRANSLATE("Focus mode:"),
138 fFocusMenu);
139 focusField->SetAlignment(B_ALIGN_LEFT);
141 // Add the "Focus follows mouse mode" pop up menu
142 fFocusFollowsMouseMenu = new BPopUpMenu(B_TRANSLATE("Normal"));
144 const char *focusFollowsMouseLabels[] = {B_TRANSLATE_MARK("Normal"),
145 B_TRANSLATE_MARK("Warp"), B_TRANSLATE_MARK("Instant warp")};
146 const mode_focus_follows_mouse focusFollowsMouseModes[]
147 = {B_NORMAL_FOCUS_FOLLOWS_MOUSE, B_WARP_FOCUS_FOLLOWS_MOUSE,
148 B_INSTANT_WARP_FOCUS_FOLLOWS_MOUSE};
150 for (int i = 0; i < 3; i++) {
151 BMessage* message = new BMessage(kMsgFollowsMouseMode);
152 message->AddInt32("mode_focus_follows_mouse",
153 focusFollowsMouseModes[i]);
155 fFocusFollowsMouseMenu->AddItem(new BMenuItem(
156 B_TRANSLATE_NOCOLLECT(focusFollowsMouseLabels[i]), message));
159 BMenuField* focusFollowsMouseField = new BMenuField(
160 "Focus follows mouse mode:", fFocusFollowsMouseMenu);
161 focusFollowsMouseField->SetAlignment(B_ALIGN_RIGHT);
163 // Add the "Click-through" check box
164 fAcceptFirstClickBox = new BCheckBox(B_TRANSLATE("Accept first click"),
165 new BMessage(kMsgAcceptFirstClick));
167 // Build the layout
169 // Layout is :
170 // A | B
171 // -----
172 // C
174 BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_DEFAULT_SPACING)
175 // Horizontal : A|B
176 .AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
178 // Vertical block A: mouse type/view/test
179 .AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING)
180 .AddGroup(B_HORIZONTAL, 0)
181 .AddGlue()
182 .Add(typeField)
183 .AddGlue()
184 .End()
185 .AddGlue()
186 .AddGroup(B_HORIZONTAL, 0)
187 .AddGlue()
188 .Add(fMouseView)
189 .AddGlue()
190 .End()
191 .AddGlue()
192 .Add(doubleClickTextControl)
193 .End()
194 .Add(new BSeparatorView(B_VERTICAL))
196 // Vertical block B: speed settings
197 .AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING, 3)
198 .AddGroup(B_HORIZONTAL, 0)
199 .Add(fClickSpeedSlider)
200 .End()
201 .AddGroup(B_HORIZONTAL, 0)
202 .Add(fMouseSpeedSlider)
203 .End()
204 .AddGroup(B_HORIZONTAL, 0)
205 .Add(fAccelerationSlider)
206 .End()
207 .End()
208 .End()
209 .AddStrut(B_USE_DEFAULT_SPACING)
211 // Horizontal Block C: focus mode
212 .AddGroup(B_HORIZONTAL, B_USE_SMALL_SPACING)
213 .Add(focusField)
214 .AddGlue()
215 .AddGroup(B_VERTICAL, 0)
216 .Add(fAcceptFirstClickBox)
217 .End()
218 .End();
220 SetBorder(B_NO_BORDER);
224 SettingsView::~SettingsView()
230 void
231 SettingsView::AttachedToWindow()
233 UpdateFromSettings();
239 void
240 SettingsView::SetMouseType(int32 type)
242 fMouseView->SetMouseType(type);
246 void
247 SettingsView::MouseMapUpdated()
249 fMouseView->MouseMapUpdated();
253 void
254 SettingsView::UpdateFromSettings()
256 int32 value = int32(fSettings.ClickSpeed() / 1000);
257 // slow = 1000000, fast = 0
258 fClickSpeedSlider->SetValue(value);
260 value = int32((log(fSettings.MouseSpeed() / 8192.0) / log(2)) * 1000 / 6);
261 // slow = 8192, fast = 524287
262 fMouseSpeedSlider->SetValue(value);
264 value = int32(sqrt(fSettings.AccelerationFactor() / 16384.0) * 1000 / 4);
265 // slow = 0, fast = 262144
266 fAccelerationSlider->SetValue(value);
268 BMenuItem* item = fTypeMenu->ItemAt(fSettings.MouseType() - 1);
269 if (item != NULL)
270 item->SetMarked(true);
272 fMouseView->SetMouseType(fSettings.MouseType());
274 item = fFocusMenu->ItemAt(mouse_mode_to_index(fSettings.MouseMode()));
275 if (item != NULL)
276 item->SetMarked(true);
278 item = fFocusFollowsMouseMenu->ItemAt(
279 focus_follows_mouse_mode_to_index(fSettings.FocusFollowsMouseMode()));
280 if (item != NULL)
281 item->SetMarked(true);
283 fFocusFollowsMouseMenu->SetEnabled(fSettings.MouseMode()
284 == B_FOCUS_FOLLOWS_MOUSE);
286 fAcceptFirstClickBox->SetValue(fSettings.AcceptFirstClick()
287 ? B_CONTROL_ON : B_CONTROL_OFF);
288 fAcceptFirstClickBox->SetEnabled(fSettings.MouseMode()
289 != B_FOCUS_FOLLOWS_MOUSE);