libroot/posix/stdio: Remove unused portions.
[haiku.git] / src / preferences / mouse / MouseWindow.cpp
blob4ce29a87760a9e0162a887953d219599de4745e5
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)
12 #include <Alert.h>
13 #include <Application.h>
14 #include <Button.h>
15 #include <Catalog.h>
16 #include <CheckBox.h>
17 #include <Debug.h>
18 #include <LayoutBuilder.h>
19 #include <Locale.h>
20 #include <Menu.h>
21 #include <MenuField.h>
22 #include <MenuItem.h>
23 #include <Message.h>
24 #include <Screen.h>
25 #include <SeparatorView.h>
26 #include <Slider.h>
28 #include "MouseWindow.h"
29 #include "MouseConstants.h"
30 #include "SettingsView.h"
32 #undef B_TRANSLATION_CONTEXT
33 #define B_TRANSLATION_CONTEXT "MouseWindow"
35 MouseWindow::MouseWindow(BRect _rect)
37 BWindow(_rect, B_TRANSLATE_SYSTEM_NAME("Mouse"), B_TITLED_WINDOW,
38 B_NOT_RESIZABLE | B_NOT_ZOOMABLE | B_ASYNCHRONOUS_CONTROLS
39 | B_AUTO_UPDATE_SIZE_LIMITS)
41 // Add the main settings view
42 fSettingsView = new SettingsView(fSettings);
44 // Add the "Default" button
45 fDefaultsButton = new BButton(B_TRANSLATE("Defaults"),
46 new BMessage(kMsgDefaults));
47 fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
49 // Add the "Revert" button
50 fRevertButton = new BButton(B_TRANSLATE("Revert"),
51 new BMessage(kMsgRevert));
52 fRevertButton->SetEnabled(false);
54 SetPulseRate(100000);
55 // we are using the pulse rate to scan pressed mouse
56 // buttons and draw the selected imagery
58 // Build the layout
59 BLayoutBuilder::Group<>(this, B_VERTICAL)
60 .AddGroup(B_HORIZONTAL)
61 .SetInsets(B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING,
62 B_USE_WINDOW_SPACING, 0)
63 .Add(fSettingsView)
64 .End()
65 .Add(new BSeparatorView(B_HORIZONTAL))
66 .AddGroup(B_HORIZONTAL)
67 .SetInsets(B_USE_WINDOW_SPACING, 0, B_USE_WINDOW_SPACING,
68 B_USE_WINDOW_SPACING)
69 .Add(fDefaultsButton)
70 .Add(fRevertButton)
71 .AddGlue()
72 .End();
74 // check if the window is on screen
75 BRect rect = BScreen().Frame();
76 rect.InsetBySelf(20, 20);
78 BPoint position = fSettings.WindowPosition();
79 BRect windowFrame = Frame().OffsetToSelf(position);
80 if (!rect.Contains(windowFrame)) {
81 // center window on screen as it doesn't fit on the saved position
82 position.x = (rect.Width() - windowFrame.Width()) / 2;
83 position.y = (rect.Height() - windowFrame.Height()) / 2;
84 if (position.x < 0)
85 position.x = 0;
86 if (position.y < 0)
87 position.y = 15;
89 MoveTo(position);
93 bool
94 MouseWindow::QuitRequested()
96 fSettings.SetWindowPosition(Frame().LeftTop());
97 be_app->PostMessage(B_QUIT_REQUESTED);
99 return true;
103 void
104 MouseWindow::MessageReceived(BMessage* message)
106 switch (message->what) {
107 case kMsgDefaults: {
108 // reverts to default settings
109 fSettings.Defaults();
110 fSettingsView->UpdateFromSettings();
112 fDefaultsButton->SetEnabled(false);
113 fRevertButton->SetEnabled(fSettings.IsRevertable());
114 break;
117 case kMsgRevert: {
118 // revert to last settings
119 fSettings.Revert();
120 fSettingsView->UpdateFromSettings();
122 fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
123 fRevertButton->SetEnabled(false);
124 break;
127 case kMsgMouseType:
129 int32 type;
130 if (message->FindInt32("index", &type) == B_OK) {
131 fSettings.SetMouseType(++type);
132 fSettingsView->SetMouseType(type);
133 fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
134 fRevertButton->SetEnabled(fSettings.IsRevertable());
136 break;
139 case kMsgMouseFocusMode:
141 int32 mode;
142 if (message->FindInt32("mode", &mode) == B_OK) {
143 fSettings.SetMouseMode((mode_mouse)mode);
144 fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
145 fRevertButton->SetEnabled(fSettings.IsRevertable());
146 fSettingsView->fFocusFollowsMouseMenu->SetEnabled(
147 mode == B_FOCUS_FOLLOWS_MOUSE);
148 fSettingsView->fAcceptFirstClickBox->SetEnabled(
149 mode != B_FOCUS_FOLLOWS_MOUSE);
151 break;
154 case kMsgFollowsMouseMode:
156 int32 mode;
157 if (message->FindInt32("mode_focus_follows_mouse", &mode)
158 == B_OK) {
159 fSettings.SetFocusFollowsMouseMode(
160 (mode_focus_follows_mouse)mode);
161 fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
162 fRevertButton->SetEnabled(fSettings.IsRevertable());
164 break;
167 case kMsgAcceptFirstClick:
169 BHandler *handler;
170 if (message->FindPointer("source",
171 reinterpret_cast<void**>(&handler)) == B_OK) {
172 bool acceptFirstClick = false;
173 BCheckBox *acceptFirstClickBox =
174 dynamic_cast<BCheckBox*>(handler);
175 if (acceptFirstClickBox)
176 acceptFirstClick = acceptFirstClickBox->Value()
177 == B_CONTROL_ON;
178 fSettings.SetAcceptFirstClick(acceptFirstClick);
179 fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
180 fRevertButton->SetEnabled(fSettings.IsRevertable());
182 break;
185 case kMsgDoubleClickSpeed:
187 int32 value;
188 if (message->FindInt32("be:value", &value) == B_OK) {
189 // slow = 1000000, fast = 0
190 fSettings.SetClickSpeed(value * 1000);
191 fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
192 fRevertButton->SetEnabled(fSettings.IsRevertable());
194 break;
197 case kMsgMouseSpeed:
199 int32 value;
200 if (message->FindInt32("be:value", &value) == B_OK) {
201 // slow = 8192, fast = 524287
202 fSettings.SetMouseSpeed((int32)pow(2,
203 value * 6.0 / 1000) * 8192);
204 fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
205 fRevertButton->SetEnabled(fSettings.IsRevertable());
207 break;
210 case kMsgAccelerationFactor:
212 int32 value;
213 if (message->FindInt32("be:value", &value) == B_OK) {
214 // slow = 0, fast = 262144
215 fSettings.SetAccelerationFactor((int32)pow(
216 value * 4.0 / 1000, 2) * 16384);
217 fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
218 fRevertButton->SetEnabled(fSettings.IsRevertable());
220 break;
223 case kMsgMouseMap:
225 int32 index;
226 int32 button;
227 if (message->FindInt32("index", &index) == B_OK
228 && message->FindInt32("button", &button) == B_OK) {
229 int32 mapping = B_PRIMARY_MOUSE_BUTTON;
230 switch (index) {
231 case 1:
232 mapping = B_SECONDARY_MOUSE_BUTTON;
233 break;
234 case 2:
235 mapping = B_TERTIARY_MOUSE_BUTTON;
236 break;
239 fSettings.SetMapping(button, mapping);
240 fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
241 fRevertButton->SetEnabled(fSettings.IsRevertable());
242 fSettingsView->MouseMapUpdated();
244 break;
247 default:
248 BWindow::MessageReceived(message);
249 break;