xref: /haiku/src/preferences/input/SettingsView.cpp (revision 85e84ddefb2aa36c93ee3a0c5545ba053f2d56bd)
15e9685caSPreetpal Kaur /*
25e9685caSPreetpal Kaur  * Copyright 2019, Haiku, Inc.
35e9685caSPreetpal Kaur  * Distributed under the terms of the MIT License.
45e9685caSPreetpal Kaur  *
55e9685caSPreetpal Kaur  * Author:
65e9685caSPreetpal Kaur  *		Preetpal Kaur <preetpalok123@gmail.com>
75e9685caSPreetpal Kaur  */
85e9685caSPreetpal Kaur 
95e9685caSPreetpal Kaur 
10*85e84ddeSMáximo Castañeda #include "SettingsView.h"
115e9685caSPreetpal Kaur 
125e9685caSPreetpal Kaur #include <Box.h>
135e9685caSPreetpal Kaur #include <Catalog.h>
145e9685caSPreetpal Kaur #include <Debug.h>
155e9685caSPreetpal Kaur #include <InterfaceDefs.h>
165e9685caSPreetpal Kaur #include <LayoutBuilder.h>
175e9685caSPreetpal Kaur #include <Locale.h>
185e9685caSPreetpal Kaur #include <SeparatorView.h>
195e9685caSPreetpal Kaur #include <Slider.h>
205e9685caSPreetpal Kaur #include <TextControl.h>
215e9685caSPreetpal Kaur 
225e9685caSPreetpal Kaur #include "InputConstants.h"
235e9685caSPreetpal Kaur #include "MouseSettings.h"
245e9685caSPreetpal Kaur #include "MouseView.h"
255e9685caSPreetpal Kaur 
265e9685caSPreetpal Kaur 
275e9685caSPreetpal Kaur //	#pragma mark -
285e9685caSPreetpal Kaur 
295e9685caSPreetpal Kaur #undef B_TRANSLATION_CONTEXT
305e9685caSPreetpal Kaur #define B_TRANSLATION_CONTEXT "SettingsView"
315e9685caSPreetpal Kaur 
3202dabf4bSSaloni 
SettingsView(MouseSettings & settings)335e9685caSPreetpal Kaur SettingsView::SettingsView(MouseSettings& settings)
3402dabf4bSSaloni 	:
3502dabf4bSSaloni 	BBox("main_view"),
365e9685caSPreetpal Kaur 	fSettings(settings)
375e9685caSPreetpal Kaur {
389aea709dSAdrien Destugues 	if (fSettings.MouseType() > 6)
399aea709dSAdrien Destugues 		debugger("Mouse type is invalid");
409aea709dSAdrien Destugues 
415e9685caSPreetpal Kaur 	// Add the "Mouse Type" pop up menu
4202dabf4bSSaloni 	fTypeMenu = new BOptionPopUp(
4302dabf4bSSaloni 		"type", B_TRANSLATE("Mouse type:"), new BMessage(kMsgMouseType));
445bbf7f1bSAdrien Destugues 	fTypeMenu->AddOption(B_TRANSLATE("1-Button"), 1);
455bbf7f1bSAdrien Destugues 	fTypeMenu->AddOption(B_TRANSLATE("2-Button"), 2);
465bbf7f1bSAdrien Destugues 	fTypeMenu->AddOption(B_TRANSLATE("3-Button"), 3);
475bbf7f1bSAdrien Destugues 	fTypeMenu->AddOption(B_TRANSLATE("4-Button"), 4);
485bbf7f1bSAdrien Destugues 	fTypeMenu->AddOption(B_TRANSLATE("5-Button"), 5);
49abbd654bSPreetpal Kaur 	fTypeMenu->AddOption(B_TRANSLATE("6-Button"), 6);
505e9685caSPreetpal Kaur 
515e9685caSPreetpal Kaur 	// Create the "Double-click speed slider...
5202dabf4bSSaloni 	fClickSpeedSlider
5302dabf4bSSaloni 		= new BSlider("double_click_speed", B_TRANSLATE("Double-click speed"),
5402dabf4bSSaloni 			new BMessage(kMsgDoubleClickSpeed), 0, 1000, B_HORIZONTAL);
555e9685caSPreetpal Kaur 	fClickSpeedSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
565e9685caSPreetpal Kaur 	fClickSpeedSlider->SetHashMarkCount(7);
575e9685caSPreetpal Kaur 
585e9685caSPreetpal Kaur 	// Create the "Mouse Speed" slider...
595e9685caSPreetpal Kaur 	fMouseSpeedSlider = new BSlider("mouse_speed", B_TRANSLATE("Mouse speed"),
605e9685caSPreetpal Kaur 		new BMessage(kMsgMouseSpeed), 0, 1000, B_HORIZONTAL);
615e9685caSPreetpal Kaur 	fMouseSpeedSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
625e9685caSPreetpal Kaur 	fMouseSpeedSlider->SetHashMarkCount(7);
635e9685caSPreetpal Kaur 
645e9685caSPreetpal Kaur 	// Create the "Mouse Acceleration" slider...
6502dabf4bSSaloni 	fAccelerationSlider
6602dabf4bSSaloni 		= new BSlider("mouse_acceleration", B_TRANSLATE("Mouse acceleration"),
675e9685caSPreetpal Kaur 			new BMessage(kMsgAccelerationFactor), 0, 1000, B_HORIZONTAL);
685e9685caSPreetpal Kaur 	fAccelerationSlider->SetHashMarks(B_HASH_MARKS_BOTTOM);
695e9685caSPreetpal Kaur 	fAccelerationSlider->SetHashMarkCount(7);
705e9685caSPreetpal Kaur 
715e9685caSPreetpal Kaur 	fMouseView = new MouseView(fSettings);
725e9685caSPreetpal Kaur 
735e9685caSPreetpal Kaur 	// Create the "Double-click test area" text box...
745e9685caSPreetpal Kaur 	const char* label = B_TRANSLATE("Double-click test area");
7502dabf4bSSaloni 	BTextControl* doubleClickTextControl = new BTextControl(NULL, label, NULL);
765e9685caSPreetpal Kaur 	doubleClickTextControl->SetAlignment(B_ALIGN_LEFT, B_ALIGN_CENTER);
775e9685caSPreetpal Kaur 	doubleClickTextControl->SetExplicitMinSize(
785e9685caSPreetpal Kaur 		BSize(StringWidth(label), B_SIZE_UNSET));
795e9685caSPreetpal Kaur 
805e9685caSPreetpal Kaur 	// Add the "Mouse focus mode" pop up menu
81ace58491SAdrien Destugues 	fFocusMenu = new BOptionPopUp("focus_mode", B_TRANSLATE("Focus mode:"),
82ace58491SAdrien Destugues 		new BMessage(kMsgMouseFocusMode));
835e9685caSPreetpal Kaur 
845e9685caSPreetpal Kaur 	const char* focusLabels[] = {B_TRANSLATE_MARK("Click to focus and raise"),
855e9685caSPreetpal Kaur 		B_TRANSLATE_MARK("Click to focus"),
865e9685caSPreetpal Kaur 		B_TRANSLATE_MARK("Focus follows mouse")};
8702dabf4bSSaloni 	const mode_mouse focusModes[]
8802dabf4bSSaloni 		= {B_NORMAL_MOUSE, B_CLICK_TO_FOCUS_MOUSE, B_FOCUS_FOLLOWS_MOUSE};
895e9685caSPreetpal Kaur 
9002dabf4bSSaloni 	for (size_t i = 0; i < B_COUNT_OF(focusModes); i++)
9102dabf4bSSaloni 		fFocusMenu->AddOption(
9202dabf4bSSaloni 			B_TRANSLATE_NOCOLLECT(focusLabels[i]), focusModes[i]);
935e9685caSPreetpal Kaur 
945e9685caSPreetpal Kaur 	// Add the "Click-through" check box
9502dabf4bSSaloni 	fAcceptFirstClickBox = new BCheckBox(
9602dabf4bSSaloni 		B_TRANSLATE("Accept first click"), new BMessage(kMsgAcceptFirstClick));
975e9685caSPreetpal Kaur 
985e9685caSPreetpal Kaur 	// Build the layout
995e9685caSPreetpal Kaur 
1005e9685caSPreetpal Kaur 	// Layout is :
1015e9685caSPreetpal Kaur 	// A | B
1025e9685caSPreetpal Kaur 	// -----
1035e9685caSPreetpal Kaur 	//   C
1045e9685caSPreetpal Kaur 
1055e9685caSPreetpal Kaur 	BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_DEFAULT_SPACING)
1065e9685caSPreetpal Kaur 		// Horizontal : A|B
1075e9685caSPreetpal Kaur 		.AddGroup(B_HORIZONTAL, B_USE_DEFAULT_SPACING)
1085e9685caSPreetpal Kaur 
1095e9685caSPreetpal Kaur 			// Vertical block A: mouse type/view/test
1105e9685caSPreetpal Kaur 			.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING)
1115bbf7f1bSAdrien Destugues 				.Add(fTypeMenu)
1125e9685caSPreetpal Kaur 				.AddGroup(B_HORIZONTAL, 0)
1135e9685caSPreetpal Kaur 					.AddGlue()
1145e9685caSPreetpal Kaur 					.Add(fMouseView)
1155e9685caSPreetpal Kaur 					.AddGlue()
1165e9685caSPreetpal Kaur 					.End()
1175e9685caSPreetpal Kaur 				.AddGlue()
1185e9685caSPreetpal Kaur 				.Add(doubleClickTextControl)
1195e9685caSPreetpal Kaur 				.End()
1205e9685caSPreetpal Kaur 			.Add(new BSeparatorView(B_VERTICAL))
1215e9685caSPreetpal Kaur 
1225e9685caSPreetpal Kaur 			// Vertical block B: speed settings
1235e9685caSPreetpal Kaur 			.AddGroup(B_VERTICAL, B_USE_DEFAULT_SPACING, 3)
1245e9685caSPreetpal Kaur 				.AddGroup(B_HORIZONTAL, 0)
1255e9685caSPreetpal Kaur 					.Add(fClickSpeedSlider)
1265e9685caSPreetpal Kaur 					.End()
1275e9685caSPreetpal Kaur 				.AddGroup(B_HORIZONTAL, 0)
1285e9685caSPreetpal Kaur 					.Add(fMouseSpeedSlider)
1295e9685caSPreetpal Kaur 					.End()
1305e9685caSPreetpal Kaur 				.AddGroup(B_HORIZONTAL, 0)
1315e9685caSPreetpal Kaur 					.Add(fAccelerationSlider)
1325e9685caSPreetpal Kaur 					.End()
1335e9685caSPreetpal Kaur 				.End()
1345e9685caSPreetpal Kaur 			.End()
1355e9685caSPreetpal Kaur 		.AddStrut(B_USE_DEFAULT_SPACING)
1365e9685caSPreetpal Kaur 
1375e9685caSPreetpal Kaur 		// Horizontal Block C: focus mode
1385e9685caSPreetpal Kaur 		.AddGroup(B_HORIZONTAL, B_USE_SMALL_SPACING)
139ace58491SAdrien Destugues 			.Add(fFocusMenu)
1405e9685caSPreetpal Kaur 			.AddGlue()
1415e9685caSPreetpal Kaur 			.AddGroup(B_VERTICAL, 0)
1425e9685caSPreetpal Kaur 				.Add(fAcceptFirstClickBox)
1435e9685caSPreetpal Kaur 				.End()
1445e9685caSPreetpal Kaur 			.End();
1455e9685caSPreetpal Kaur 
1465e9685caSPreetpal Kaur 	SetBorder(B_NO_BORDER);
1475e9685caSPreetpal Kaur }
1485e9685caSPreetpal Kaur 
1495e9685caSPreetpal Kaur 
~SettingsView()1505e9685caSPreetpal Kaur SettingsView::~SettingsView()
1515e9685caSPreetpal Kaur {
1525e9685caSPreetpal Kaur }
1535e9685caSPreetpal Kaur 
1545e9685caSPreetpal Kaur 
1555e9685caSPreetpal Kaur void
AttachedToWindow()1565e9685caSPreetpal Kaur SettingsView::AttachedToWindow()
1575e9685caSPreetpal Kaur {
1585e9685caSPreetpal Kaur 	UpdateFromSettings();
1595e9685caSPreetpal Kaur }
1605e9685caSPreetpal Kaur 
1615e9685caSPreetpal Kaur 
1625e9685caSPreetpal Kaur void
SetMouseType(int32 type)1635e9685caSPreetpal Kaur SettingsView::SetMouseType(int32 type)
1645e9685caSPreetpal Kaur {
16502ad22d6SPreetpal Kaur 	if (type > 6)
16602ad22d6SPreetpal Kaur 		debugger("Mouse type is invalid");
1675e9685caSPreetpal Kaur 	fMouseView->SetMouseType(type);
1685e9685caSPreetpal Kaur }
1695e9685caSPreetpal Kaur 
1705e9685caSPreetpal Kaur 
1715e9685caSPreetpal Kaur void
MouseMapUpdated()1725e9685caSPreetpal Kaur SettingsView::MouseMapUpdated()
1735e9685caSPreetpal Kaur {
1745e9685caSPreetpal Kaur 	fMouseView->MouseMapUpdated();
1755e9685caSPreetpal Kaur }
1765e9685caSPreetpal Kaur 
1775e9685caSPreetpal Kaur 
1785e9685caSPreetpal Kaur void
UpdateFromSettings()1795e9685caSPreetpal Kaur SettingsView::UpdateFromSettings()
1805e9685caSPreetpal Kaur {
181*85e84ddeSMáximo Castañeda 	int32 value = int32((1000000LL - fSettings.ClickSpeed()) / 1000);
1825e9685caSPreetpal Kaur 	// slow = 1000000, fast = 0
1835e9685caSPreetpal Kaur 	fClickSpeedSlider->SetValue(value);
1845e9685caSPreetpal Kaur 
1855e9685caSPreetpal Kaur 	value = int32((log(fSettings.MouseSpeed() / 8192.0) / log(2)) * 1000 / 6);
1865e9685caSPreetpal Kaur 	// slow = 8192, fast = 524287
1875e9685caSPreetpal Kaur 	fMouseSpeedSlider->SetValue(value);
1885e9685caSPreetpal Kaur 
1895e9685caSPreetpal Kaur 	value = int32(sqrt(fSettings.AccelerationFactor() / 16384.0) * 1000 / 4);
1905e9685caSPreetpal Kaur 	// slow = 0, fast = 262144
1915e9685caSPreetpal Kaur 	fAccelerationSlider->SetValue(value);
1925e9685caSPreetpal Kaur 
1935bbf7f1bSAdrien Destugues 	fTypeMenu->SelectOptionFor(fSettings.MouseType());
1945e9685caSPreetpal Kaur 	fMouseView->SetMouseType(fSettings.MouseType());
1955e9685caSPreetpal Kaur 
196ace58491SAdrien Destugues 	fFocusMenu->SelectOptionFor(fSettings.MouseMode());
1975e9685caSPreetpal Kaur 
19802dabf4bSSaloni 	fAcceptFirstClickBox->SetValue(
19902dabf4bSSaloni 		fSettings.AcceptFirstClick() ? B_CONTROL_ON : B_CONTROL_OFF);
20002dabf4bSSaloni 	fAcceptFirstClickBox->SetEnabled(
20102dabf4bSSaloni 		fSettings.MouseMode() != B_FOCUS_FOLLOWS_MOUSE);
2025e9685caSPreetpal Kaur }
203