xref: /haiku/src/preferences/input/InputMouse.cpp (revision caed67a8cba83913b9c21ac2b06ebc6bd1cb3111)
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 "InputMouse.h"
11 
12 #include <Button.h>
13 #include <Catalog.h>
14 #include <CheckBox.h>
15 #include <ControlLook.h>
16 #include <Debug.h>
17 #include <LayoutBuilder.h>
18 #include <Locale.h>
19 #include <SeparatorView.h>
20 
21 #include "InputConstants.h"
22 #include "MouseSettings.h"
23 
24 
25 #undef B_TRANSLATION_CONTEXT
26 #define B_TRANSLATION_CONTEXT "InputMouse"
27 
28 
29 InputMouse::InputMouse(BInputDevice* dev, MouseSettings* settings)
30 	:
31 	BView("InputMouse", B_WILL_DRAW)
32 {
33 	fSettings = settings;
34 
35 	fSettingsView = new SettingsView(*fSettings);
36 
37 	fDefaultsButton = new BButton(B_TRANSLATE("Defaults"),
38 		new BMessage(kMsgDefaults));
39 	fDefaultsButton->SetEnabled(fSettings->IsDefaultable());
40 
41 	fRevertButton = new BButton(B_TRANSLATE("Revert"),
42 		new BMessage(kMsgRevert));
43 	fRevertButton->SetEnabled(false);
44 
45 	BLayoutBuilder::Group<>(this, B_VERTICAL, B_USE_DEFAULT_SPACING)
46 		.Add(fSettingsView)
47 			.Add(new BSeparatorView(B_HORIZONTAL))
48 				.AddGroup(B_HORIZONTAL)
49 				.Add(fDefaultsButton)
50 				.Add(fRevertButton)
51 				.AddGlue()
52 				.End()
53 		.End();
54 }
55 
56 
57 InputMouse::~InputMouse()
58 {
59 }
60 
61 
62 void
63 InputMouse::MessageReceived(BMessage* message)
64 {
65 	switch (message->what) {
66 		case kMsgDefaults:
67 		{
68 			fSettings->Defaults();
69 			fSettingsView->UpdateFromSettings();
70 
71 			fDefaultsButton->SetEnabled(false);
72 			fRevertButton->SetEnabled(fSettings->IsRevertable());
73 			break;
74 		}
75 
76 		case kMsgRevert:
77 		{
78 			fSettings->Revert();
79 			fSettingsView->UpdateFromSettings();
80 
81 			fDefaultsButton->SetEnabled(fSettings->IsDefaultable());
82 			fRevertButton->SetEnabled(false);
83 			break;
84 		}
85 
86 		case kMsgMouseType:
87 		{
88 			int32 type;
89 			if (message->FindInt32("be:value", &type) == B_OK) {
90 				if (type > 6)
91 					debugger("Mouse type is invalid");
92 				fSettings->SetMouseType(type);
93 				fSettingsView->SetMouseType(type);
94 				fDefaultsButton->SetEnabled(fSettings->IsDefaultable());
95 				fRevertButton->SetEnabled(fSettings->IsRevertable());
96 			}
97 			break;
98 		}
99 
100 		case kMsgMouseFocusMode:
101 		{
102 			int32 mode;
103 			if (message->FindInt32("be:value", &mode) == B_OK) {
104 				fSettings->SetMouseMode((mode_mouse)mode);
105 				fDefaultsButton->SetEnabled(fSettings->IsDefaultable());
106 				fRevertButton->SetEnabled(fSettings->IsRevertable());
107 				fSettingsView->fAcceptFirstClickBox->SetEnabled(
108 					mode != B_FOCUS_FOLLOWS_MOUSE);
109 			}
110 			break;
111 		}
112 
113 		case kMsgFollowsMouseMode:
114 		{
115 			int32 mode;
116 			if (message->FindInt32("mode_focus_follows_mouse", &mode) == B_OK) {
117 				fSettings->SetFocusFollowsMouseMode(
118 					(mode_focus_follows_mouse)mode);
119 				fDefaultsButton->SetEnabled(fSettings->IsDefaultable());
120 				fRevertButton->SetEnabled(fSettings->IsRevertable());
121 			}
122 			break;
123 		}
124 
125 		case kMsgAcceptFirstClick:
126 		{
127 			BHandler* handler;
128 			if (message->FindPointer(
129 					"source", reinterpret_cast<void**>(&handler))
130 				== B_OK) {
131 				bool acceptFirstClick = true;
132 				BCheckBox* acceptFirstClickBox
133 					= dynamic_cast<BCheckBox*>(handler);
134 				if (acceptFirstClickBox)
135 					acceptFirstClick
136 						= acceptFirstClickBox->Value() == B_CONTROL_ON;
137 				fSettings->SetAcceptFirstClick(acceptFirstClick);
138 				fDefaultsButton->SetEnabled(fSettings->IsDefaultable());
139 				fRevertButton->SetEnabled(fSettings->IsRevertable());
140 			}
141 			break;
142 		}
143 
144 		case kMsgDoubleClickSpeed:
145 		{
146 			int32 value;
147 			if (message->FindInt32("be:value", &value) == B_OK) {
148 				// slow = 1000000, fast = 0
149 				fSettings->SetClickSpeed(1000000LL - value * 1000);
150 				fDefaultsButton->SetEnabled(fSettings->IsDefaultable());
151 				fRevertButton->SetEnabled(fSettings->IsRevertable());
152 			}
153 			break;
154 		}
155 
156 		case kMsgMouseSpeed:
157 		{
158 			int32 value;
159 			if (message->FindInt32("be:value", &value) == B_OK) {
160 				// slow = 8192, fast = 524287
161 				fSettings->SetMouseSpeed(
162 					(int32)pow(2, value * 6.0 / 1000) * 8192);
163 				fDefaultsButton->SetEnabled(fSettings->IsDefaultable());
164 				fRevertButton->SetEnabled(fSettings->IsRevertable());
165 			}
166 			break;
167 		}
168 
169 		case kMsgAccelerationFactor:
170 		{
171 			int32 value;
172 			if (message->FindInt32("be:value", &value) == B_OK) {
173 				// slow = 0, fast = 262144
174 				fSettings->SetAccelerationFactor(
175 					(int32)pow(value * 4.0 / 1000, 2) * 16384);
176 				fDefaultsButton->SetEnabled(fSettings->IsDefaultable());
177 				fRevertButton->SetEnabled(fSettings->IsRevertable());
178 			}
179 			break;
180 		}
181 
182 		case kMsgMouseMap:
183 		{
184 			int32 index;
185 			int32 button;
186 			if (message->FindInt32("index", &index) == B_OK
187 				&& message->FindInt32("button", &button) == B_OK) {
188 				int32 mapping = B_MOUSE_BUTTON(index + 1);
189 				fSettings->SetMapping(button, mapping);
190 				fDefaultsButton->SetEnabled(fSettings->IsDefaultable());
191 				fRevertButton->SetEnabled(fSettings->IsRevertable());
192 				fSettingsView->MouseMapUpdated();
193 			}
194 			break;
195 		}
196 
197 		default:
198 			BView::MessageReceived(message);
199 	}
200 }
201