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