xref: /haiku/src/preferences/input/SettingsView.cpp (revision 909af08f4328301fbdef1ffb41f566c3b5bec0c7)
1 /*
2  * Copyright 2019, Haiku, Inc.
3  * Distributed under the terms of the MIT License.
4  *
5  * Author:
6  *		Preetpal Kaur <preetpalok123@gmail.com>
7  */
8 
9 
10 #include "SettingsView.h"
11 
12 #include <Box.h>
13 #include <Catalog.h>
14 #include <Debug.h>
15 #include <InterfaceDefs.h>
16 #include <LayoutBuilder.h>
17 #include <Locale.h>
18 #include <SeparatorView.h>
19 #include <Slider.h>
20 #include <TextControl.h>
21 
22 #include "InputConstants.h"
23 #include "MouseSettings.h"
24 #include "MouseView.h"
25 
26 
27 //	#pragma mark -
28 
29 #undef B_TRANSLATION_CONTEXT
30 #define B_TRANSLATION_CONTEXT "SettingsView"
31 
32 
33 SettingsView::SettingsView(MouseSettings& settings)
34 	:
35 	BBox("main_view"),
36 	fSettings(settings)
37 {
38 	if (fSettings.MouseType() > 6)
39 		debugger("Mouse type is invalid");
40 
41 	// Add the "Mouse Type" pop up menu
42 	fTypeMenu = new BOptionPopUp(
43 		"type", B_TRANSLATE("Mouse type:"), new BMessage(kMsgMouseType));
44 	fTypeMenu->AddOption(B_TRANSLATE("1-Button"), 1);
45 	fTypeMenu->AddOption(B_TRANSLATE("2-Button"), 2);
46 	fTypeMenu->AddOption(B_TRANSLATE("3-Button"), 3);
47 	fTypeMenu->AddOption(B_TRANSLATE("4-Button"), 4);
48 	fTypeMenu->AddOption(B_TRANSLATE("5-Button"), 5);
49 	fTypeMenu->AddOption(B_TRANSLATE("6-Button"), 6);
50 
51 	// Create the "Double-click speed slider...
52 	fClickSpeedSlider
53 		= new BSlider("double_click_speed", B_TRANSLATE("Double-click speed"),
54 			new BMessage(kMsgDoubleClickSpeed), 0, 1000, B_HORIZONTAL);
55 	fClickSpeedSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
56 	fClickSpeedSlider->SetHashMarkCount(7);
57 
58 	// Create the "Mouse Speed" slider...
59 	fMouseSpeedSlider = new BSlider("mouse_speed", B_TRANSLATE("Mouse speed"),
60 		new BMessage(kMsgMouseSpeed), 0, 1000, B_HORIZONTAL);
61 	fMouseSpeedSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
62 	fMouseSpeedSlider->SetHashMarkCount(7);
63 
64 	// Create the "Mouse Acceleration" slider...
65 	fAccelerationSlider
66 		= new BSlider("mouse_acceleration", B_TRANSLATE("Mouse acceleration"),
67 			new BMessage(kMsgAccelerationFactor), 0, 1000, B_HORIZONTAL);
68 	fAccelerationSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
69 	fAccelerationSlider->SetHashMarkCount(7);
70 
71 	fMouseView = new MouseView(fSettings);
72 
73 	// Create the "Double-click test area" text box...
74 	const char* label = B_TRANSLATE("Double-click test area");
75 	BTextControl* doubleClickTextControl = new BTextControl(NULL, label, NULL);
76 	doubleClickTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_CENTER);
77 	doubleClickTextControl->SetExplicitMinSize(
78 		BSize(StringWidth(label), B_SIZE_UNSET));
79 
80 	// Add the "Mouse focus mode" pop up menu
81 	fFocusMenu = new BOptionPopUp("focus_mode", B_TRANSLATE("Focus mode:"),
82 		new BMessage(kMsgMouseFocusMode));
83 
84 	const char* focusLabels[] = {B_TRANSLATE_MARK("Click to focus and raise"),
85 		B_TRANSLATE_MARK("Click to focus"),
86 		B_TRANSLATE_MARK("Focus follows mouse")};
87 	const mode_mouse focusModes[]
88 		= {B_NORMAL_MOUSE, B_CLICK_TO_FOCUS_MOUSE, B_FOCUS_FOLLOWS_MOUSE};
89 
90 	for (size_t i = 0; i < B_COUNT_OF(focusModes); i++)
91 		fFocusMenu->AddOption(
92 			B_TRANSLATE_NOCOLLECT(focusLabels[i]), focusModes[i]);
93 
94 	// Add the "Click-through" check box
95 	fAcceptFirstClickBox = new BCheckBox(
96 		B_TRANSLATE("Accept first click"), new BMessage(kMsgAcceptFirstClick));
97 
98 	// Build the layout
99 
100 	// Layout is :
101 	// A | B
102 	// -----
103 	//   C
104 
105 	BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_DEFAULT_SPACING)
106 		// Horizontal : A|B
107 		.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
108 
109 			// Vertical block A: mouse type/view/test
110 			.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING)
111 				.Add(fTypeMenu)
112 				.AddGroup(B_HORIZONTAL, 0)
113 					.AddGlue()
114 					.Add(fMouseView)
115 					.AddGlue()
116 					.End()
117 				.AddGlue()
118 				.Add(doubleClickTextControl)
119 				.End()
120 			.Add(new BSeparatorView(B_VERTICAL))
121 
122 			// Vertical block B: speed settings
123 			.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING, 3)
124 				.AddGroup(B_HORIZONTAL, 0)
125 					.Add(fClickSpeedSlider)
126 					.End()
127 				.AddGroup(B_HORIZONTAL, 0)
128 					.Add(fMouseSpeedSlider)
129 					.End()
130 				.AddGroup(B_HORIZONTAL, 0)
131 					.Add(fAccelerationSlider)
132 					.End()
133 				.End()
134 			.End()
135 		.AddStrut(B_USE_DEFAULT_SPACING)
136 
137 		// Horizontal Block C: focus mode
138 		.AddGroup(B_HORIZONTAL, B_USE_SMALL_SPACING)
139 			.Add(fFocusMenu)
140 			.AddGlue()
141 			.AddGroup(B_VERTICAL, 0)
142 				.Add(fAcceptFirstClickBox)
143 				.End()
144 			.End();
145 
146 	SetBorder(B_NO_BORDER);
147 }
148 
149 
150 SettingsView::~SettingsView()
151 {
152 }
153 
154 
155 void
156 SettingsView::AttachedToWindow()
157 {
158 	UpdateFromSettings();
159 }
160 
161 
162 void
163 SettingsView::SetMouseType(int32 type)
164 {
165 	if (type > 6)
166 		debugger("Mouse type is invalid");
167 	fMouseView->SetMouseType(type);
168 }
169 
170 
171 void
172 SettingsView::MouseMapUpdated()
173 {
174 	fMouseView->MouseMapUpdated();
175 }
176 
177 
178 void
179 SettingsView::UpdateFromSettings()
180 {
181 	int32 value = int32((1000000LL - fSettings.ClickSpeed()) / 1000);
182 	// slow = 1000000, fast = 0
183 	fClickSpeedSlider->SetValue(value);
184 
185 	value = int32((log(fSettings.MouseSpeed() / 8192.0) / log(2)) * 1000 / 6);
186 	// slow = 8192, fast = 524287
187 	fMouseSpeedSlider->SetValue(value);
188 
189 	value = int32(sqrt(fSettings.AccelerationFactor() / 16384.0) * 1000 / 4);
190 	// slow = 0, fast = 262144
191 	fAccelerationSlider->SetValue(value);
192 
193 	fTypeMenu->SelectOptionFor(fSettings.MouseType());
194 	fMouseView->SetMouseType(fSettings.MouseType());
195 
196 	fFocusMenu->SelectOptionFor(fSettings.MouseMode());
197 
198 	fAcceptFirstClickBox->SetValue(
199 		fSettings.AcceptFirstClick() ? B_CONTROL_ON : B_CONTROL_OFF);
200 	fAcceptFirstClickBox->SetEnabled(
201 		fSettings.MouseMode() != B_FOCUS_FOLLOWS_MOUSE);
202 }
203