2 * Copyright 2003-2014 Haiku Inc. All rights reserved.
3 * Distributed under the terms of the MIT License.
7 * Axel Dörfler (axeld@pinc-software.de)
8 * Andrew McCall (mccall@digitalparadise.co.uk)
9 * Brecht Machiels (brecht@mos6581.org)
13 #include <Application.h>
18 #include <LayoutBuilder.h>
21 #include <MenuField.h>
27 #include "MouseWindow.h"
28 #include "MouseConstants.h"
29 #include "SettingsView.h"
31 #undef B_TRANSLATION_CONTEXT
32 #define B_TRANSLATION_CONTEXT "MouseWindow"
34 MouseWindow::MouseWindow(BRect _rect
)
36 BWindow(_rect
, B_TRANSLATE_SYSTEM_NAME("Mouse"), B_TITLED_WINDOW
,
37 B_NOT_RESIZABLE
| B_NOT_ZOOMABLE
| B_ASYNCHRONOUS_CONTROLS
38 | B_AUTO_UPDATE_SIZE_LIMITS
)
40 // Add the main settings view
41 fSettingsView
= new SettingsView(fSettings
);
42 fSettingsBox
= new BBox("main box");
43 fSettingsBox
->AddChild(fSettingsView
);
45 // Add the "Default" button
46 fDefaultsButton
= new BButton(B_TRANSLATE("Defaults"),
47 new BMessage(kMsgDefaults
));
48 fDefaultsButton
->SetEnabled(fSettings
.IsDefaultable());
50 // Add the "Revert" button
51 fRevertButton
= new BButton(B_TRANSLATE("Revert"),
52 new BMessage(kMsgRevert
));
53 fRevertButton
->SetEnabled(false);
56 // we are using the pulse rate to scan pressed mouse
57 // buttons and draw the selected imagery
60 BLayoutBuilder::Group
<>(this, B_VERTICAL
)
61 .SetInsets(B_USE_DEFAULT_SPACING
)
63 .AddGroup(B_HORIZONTAL
)
68 // check if the window is on screen
69 BRect rect
= BScreen().Frame();
70 rect
.InsetBySelf(20, 20);
72 BPoint position
= fSettings
.WindowPosition();
73 BRect windowFrame
= Frame().OffsetToSelf(position
);
74 if (!rect
.Contains(windowFrame
)) {
75 // center window on screen as it doesn't fit on the saved position
76 position
.x
= (rect
.Width() - windowFrame
.Width()) / 2;
77 position
.y
= (rect
.Height() - windowFrame
.Height()) / 2;
88 MouseWindow::QuitRequested()
90 fSettings
.SetWindowPosition(Frame().LeftTop());
91 be_app
->PostMessage(B_QUIT_REQUESTED
);
98 MouseWindow::MessageReceived(BMessage
* message
)
100 switch (message
->what
) {
102 // reverts to default settings
103 fSettings
.Defaults();
104 fSettingsView
->UpdateFromSettings();
106 fDefaultsButton
->SetEnabled(false);
107 fRevertButton
->SetEnabled(fSettings
.IsRevertable());
112 // revert to last settings
114 fSettingsView
->UpdateFromSettings();
116 fDefaultsButton
->SetEnabled(fSettings
.IsDefaultable());
117 fRevertButton
->SetEnabled(false);
124 if (message
->FindInt32("index", &type
) == B_OK
) {
125 fSettings
.SetMouseType(++type
);
126 fSettingsView
->SetMouseType(type
);
127 fDefaultsButton
->SetEnabled(fSettings
.IsDefaultable());
128 fRevertButton
->SetEnabled(fSettings
.IsRevertable());
133 case kMsgMouseFocusMode
:
136 if (message
->FindInt32("mode", &mode
) == B_OK
) {
137 fSettings
.SetMouseMode((mode_mouse
)mode
);
138 fDefaultsButton
->SetEnabled(fSettings
.IsDefaultable());
139 fRevertButton
->SetEnabled(fSettings
.IsRevertable());
140 fSettingsView
->fFocusFollowsMouseMenu
->SetEnabled(
141 mode
== B_FOCUS_FOLLOWS_MOUSE
);
142 fSettingsView
->fAcceptFirstClickBox
->SetEnabled(
143 mode
!= B_FOCUS_FOLLOWS_MOUSE
);
148 case kMsgFollowsMouseMode
:
151 if (message
->FindInt32("mode_focus_follows_mouse", &mode
)
153 fSettings
.SetFocusFollowsMouseMode(
154 (mode_focus_follows_mouse
)mode
);
155 fDefaultsButton
->SetEnabled(fSettings
.IsDefaultable());
156 fRevertButton
->SetEnabled(fSettings
.IsRevertable());
161 case kMsgAcceptFirstClick
:
164 if (message
->FindPointer("source",
165 reinterpret_cast<void**>(&handler
)) == B_OK
) {
166 bool acceptFirstClick
= false;
167 BCheckBox
*acceptFirstClickBox
=
168 dynamic_cast<BCheckBox
*>(handler
);
169 if (acceptFirstClickBox
)
170 acceptFirstClick
= acceptFirstClickBox
->Value()
172 fSettings
.SetAcceptFirstClick(acceptFirstClick
);
173 fDefaultsButton
->SetEnabled(fSettings
.IsDefaultable());
174 fRevertButton
->SetEnabled(fSettings
.IsRevertable());
179 case kMsgDoubleClickSpeed
:
182 if (message
->FindInt32("be:value", &value
) == B_OK
) {
183 // slow = 1000000, fast = 0
184 fSettings
.SetClickSpeed(value
* 1000);
185 fDefaultsButton
->SetEnabled(fSettings
.IsDefaultable());
186 fRevertButton
->SetEnabled(fSettings
.IsRevertable());
194 if (message
->FindInt32("be:value", &value
) == B_OK
) {
195 // slow = 8192, fast = 524287
196 fSettings
.SetMouseSpeed((int32
)pow(2,
197 value
* 6.0 / 1000) * 8192);
198 fDefaultsButton
->SetEnabled(fSettings
.IsDefaultable());
199 fRevertButton
->SetEnabled(fSettings
.IsRevertable());
204 case kMsgAccelerationFactor
:
207 if (message
->FindInt32("be:value", &value
) == B_OK
) {
208 // slow = 0, fast = 262144
209 fSettings
.SetAccelerationFactor((int32
)pow(
210 value
* 4.0 / 1000, 2) * 16384);
211 fDefaultsButton
->SetEnabled(fSettings
.IsDefaultable());
212 fRevertButton
->SetEnabled(fSettings
.IsRevertable());
221 if (message
->FindInt32("index", &index
) == B_OK
222 && message
->FindInt32("button", &button
) == B_OK
) {
223 int32 mapping
= B_PRIMARY_MOUSE_BUTTON
;
226 mapping
= B_SECONDARY_MOUSE_BUTTON
;
229 mapping
= B_TERTIARY_MOUSE_BUTTON
;
233 fSettings
.SetMapping(button
, mapping
);
234 fDefaultsButton
->SetEnabled(fSettings
.IsDefaultable());
235 fRevertButton
->SetEnabled(fSettings
.IsRevertable());
236 fSettingsView
->MouseMapUpdated();
242 BWindow::MessageReceived(message
);