tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / preferences / mouse / SettingsView.cpp
blobbc806db17353b7a7758c38f3d2cefa9626b45072
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 <GroupLayout.h>
21 #include <GroupLayoutBuilder.h>
22 #include <InterfaceDefs.h>
23 #include <Locale.h>
24 #include <MenuField.h>
25 #include <MenuItem.h>
26 #include <PopUpMenu.h>
27 #include <SeparatorView.h>
28 #include <Slider.h>
29 #include <TextControl.h>
30 #include <TranslationUtils.h>
31 #include <Window.h>
33 #include "MouseConstants.h"
34 #include "MouseSettings.h"
35 #include "MouseView.h"
38 static int32
39 mouse_mode_to_index(mode_mouse mode)
41 switch (mode) {
42 case B_NORMAL_MOUSE:
43 default:
44 return 0;
45 case B_CLICK_TO_FOCUS_MOUSE:
46 return 1;
47 case B_FOCUS_FOLLOWS_MOUSE:
48 return 2;
53 static int32
54 focus_follows_mouse_mode_to_index(mode_focus_follows_mouse mode)
56 switch (mode) {
57 case B_NORMAL_FOCUS_FOLLOWS_MOUSE:
58 default:
59 return 0;
60 case B_WARP_FOCUS_FOLLOWS_MOUSE:
61 return 1;
62 case B_INSTANT_WARP_FOCUS_FOLLOWS_MOUSE:
63 return 2;
68 // #pragma mark -
70 #undef B_TRANSLATION_CONTEXT
71 #define B_TRANSLATION_CONTEXT "SettingsView"
73 SettingsView::SettingsView(MouseSettings& settings)
74 : BBox("main_view"),
75 fSettings(settings)
77 // Add the "Mouse Type" pop up menu
78 fTypeMenu = new BPopUpMenu("unknown");
79 fTypeMenu->AddItem(new BMenuItem(B_TRANSLATE("1-Button"),
80 new BMessage(kMsgMouseType)));
81 fTypeMenu->AddItem(new BMenuItem(B_TRANSLATE("2-Button"),
82 new BMessage(kMsgMouseType)));
83 fTypeMenu->AddItem(new BMenuItem(B_TRANSLATE("3-Button"),
84 new BMessage(kMsgMouseType)));
86 BMenuField* typeField = new BMenuField(B_TRANSLATE("Mouse type:"),
87 fTypeMenu);
88 typeField->SetAlignment(B_ALIGN_LEFT);
90 // Create the "Double-click speed slider...
91 fClickSpeedSlider = new BSlider("double_click_speed",
92 B_TRANSLATE("Double-click speed"), new BMessage(kMsgDoubleClickSpeed),
93 0, 1000, B_HORIZONTAL);
94 fClickSpeedSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
95 fClickSpeedSlider->SetHashMarkCount(7);
97 // Create the "Mouse Speed" slider...
98 fMouseSpeedSlider = new BSlider("mouse_speed", B_TRANSLATE("Mouse speed"),
99 new BMessage(kMsgMouseSpeed), 0, 1000, B_HORIZONTAL);
100 fMouseSpeedSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
101 fMouseSpeedSlider->SetHashMarkCount(7);
103 // Create the "Mouse Acceleration" slider...
104 fAccelerationSlider = new BSlider("mouse_acceleration",
105 B_TRANSLATE("Mouse acceleration"),
106 new BMessage(kMsgAccelerationFactor), 0, 1000, B_HORIZONTAL);
107 fAccelerationSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
108 fAccelerationSlider->SetHashMarkCount(7);
110 // Mouse image...
111 fMouseView = new MouseView(fSettings);
113 // Create the "Double-click test area" text box...
114 const char* label = B_TRANSLATE("Double-click test area");
115 BTextControl* doubleClickTextControl = new BTextControl(NULL,
116 label, NULL);
117 doubleClickTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_CENTER);
118 doubleClickTextControl->SetExplicitMinSize(
119 BSize(StringWidth(label), B_SIZE_UNSET));
121 // Add the "Mouse focus mode" pop up menu
122 fFocusMenu = new BPopUpMenu(B_TRANSLATE("Click to focus and raise"));
124 const char *focusLabels[] = {B_TRANSLATE_MARK("Click to focus and raise"),
125 B_TRANSLATE_MARK("Click to focus"),
126 B_TRANSLATE_MARK("Focus follows mouse")};
127 const mode_mouse focusModes[] = {B_NORMAL_MOUSE, B_CLICK_TO_FOCUS_MOUSE,
128 B_FOCUS_FOLLOWS_MOUSE};
130 for (int i = 0; i < 3; i++) {
131 BMessage* message = new BMessage(kMsgMouseFocusMode);
132 message->AddInt32("mode", focusModes[i]);
134 fFocusMenu->AddItem(new BMenuItem(B_TRANSLATE_NOCOLLECT(focusLabels[i]),
135 message));
138 BMenuField* focusField = new BMenuField(B_TRANSLATE("Focus mode:"),
139 fFocusMenu);
140 focusField->SetAlignment(B_ALIGN_RIGHT);
142 // Add the "Focus follows mouse mode" pop up menu
143 fFocusFollowsMouseMenu = new BPopUpMenu(B_TRANSLATE("Normal"));
145 const char *focusFollowsMouseLabels[] = {B_TRANSLATE_MARK("Normal"),
146 B_TRANSLATE_MARK("Warp"), B_TRANSLATE_MARK("Instant warp")};
147 const mode_focus_follows_mouse focusFollowsMouseModes[]
148 = {B_NORMAL_FOCUS_FOLLOWS_MOUSE, B_WARP_FOCUS_FOLLOWS_MOUSE,
149 B_INSTANT_WARP_FOCUS_FOLLOWS_MOUSE};
151 for (int i = 0; i < 3; i++) {
152 BMessage* message = new BMessage(kMsgFollowsMouseMode);
153 message->AddInt32("mode_focus_follows_mouse",
154 focusFollowsMouseModes[i]);
156 fFocusFollowsMouseMenu->AddItem(new BMenuItem(
157 B_TRANSLATE_NOCOLLECT(focusFollowsMouseLabels[i]), message));
160 BMenuField* focusFollowsMouseField = new BMenuField(
161 "Focus follows mouse mode:", fFocusFollowsMouseMenu);
162 focusFollowsMouseField->SetAlignment(B_ALIGN_RIGHT);
164 // Add the "Click-through" check box
165 fAcceptFirstClickBox = new BCheckBox(B_TRANSLATE("Accept first click"),
166 new BMessage(kMsgAcceptFirstClick));
168 // dividers
169 // This one is a vertical line for B_HORIZONTAL
170 BSeparatorView* hdivider = new BSeparatorView(B_VERTICAL, B_FANCY_BORDER);
172 // This one is a horizontal line for B_VERTICAL
173 BSeparatorView* vdivider = new BSeparatorView(B_HORIZONTAL, B_FANCY_BORDER);
175 // Build the layout
176 SetLayout(new BGroupLayout(B_VERTICAL));
178 // Layout is :
179 // A | B
180 // -----
181 // C
183 AddChild(BGroupLayoutBuilder(B_VERTICAL, B_USE_SMALL_SPACING)
185 // Horizontal : A|B
186 .AddGroup(B_HORIZONTAL, B_USE_SMALL_SPACING)
188 // Vertical block A: mouse type/view/test
189 .AddGroup(B_VERTICAL, 10)
190 .AddGroup(B_HORIZONTAL, 0)
191 .AddGlue()
192 .Add(typeField)
193 .AddGlue()
194 .End()
195 .AddGlue()
197 .Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
198 .AddGlue()
199 .Add(fMouseView)
200 .AddGlue()
202 .AddGlue()
203 .Add(doubleClickTextControl)
204 .End()
206 .Add(hdivider)
208 // Vertical block B: speed settings
209 .AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING, 3)
210 .Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
211 .Add(fClickSpeedSlider)
213 .Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
214 .Add(fMouseSpeedSlider)
216 .Add(BGroupLayoutBuilder(B_HORIZONTAL, 0)
217 .Add(fAccelerationSlider)
219 .End()
220 .End()
222 .Add(vdivider)
224 // Horizontal Block C: focus mode
225 .AddGroup(B_HORIZONTAL, B_USE_SMALL_SPACING)
226 .Add(focusField)
227 .AddGlue()
228 .AddGroup(B_VERTICAL, 0)
229 .Add(fAcceptFirstClickBox)
230 .End()
231 .End()
232 .SetInsets(B_USE_SMALL_SPACING, B_USE_SMALL_SPACING,
233 B_USE_SMALL_SPACING, B_USE_SMALL_SPACING)
238 SettingsView::~SettingsView()
244 void
245 SettingsView::AttachedToWindow()
247 UpdateFromSettings();
253 void
254 SettingsView::SetMouseType(int32 type)
256 fMouseView->SetMouseType(type);
260 void
261 SettingsView::MouseMapUpdated()
263 fMouseView->MouseMapUpdated();
267 void
268 SettingsView::UpdateFromSettings()
270 int32 value = int32(fSettings.ClickSpeed() / 1000);
271 // slow = 1000000, fast = 0
272 fClickSpeedSlider->SetValue(value);
274 value = int32((log(fSettings.MouseSpeed() / 8192.0) / log(2)) * 1000 / 6);
275 // slow = 8192, fast = 524287
276 fMouseSpeedSlider->SetValue(value);
278 value = int32(sqrt(fSettings.AccelerationFactor() / 16384.0) * 1000 / 4);
279 // slow = 0, fast = 262144
280 fAccelerationSlider->SetValue(value);
282 BMenuItem* item = fTypeMenu->ItemAt(fSettings.MouseType() - 1);
283 if (item != NULL)
284 item->SetMarked(true);
286 fMouseView->SetMouseType(fSettings.MouseType());
288 item = fFocusMenu->ItemAt(mouse_mode_to_index(fSettings.MouseMode()));
289 if (item != NULL)
290 item->SetMarked(true);
292 item = fFocusFollowsMouseMenu->ItemAt(
293 focus_follows_mouse_mode_to_index(fSettings.FocusFollowsMouseMode()));
294 if (item != NULL)
295 item->SetMarked(true);
297 fFocusFollowsMouseMenu->SetEnabled(fSettings.MouseMode()
298 == B_FOCUS_FOLLOWS_MOUSE);
300 fAcceptFirstClickBox->SetValue(fSettings.AcceptFirstClick()
301 ? B_CONTROL_ON : B_CONTROL_OFF);
302 fAcceptFirstClickBox->SetEnabled(fSettings.MouseMode()
303 != B_FOCUS_FOLLOWS_MOUSE);