xref: /haiku/src/preferences/input/InputKeyboard.cpp (revision 02dabf4bd99b67bdec8a6650bef2efd3cb04a9a6)
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 "InputKeyboard.h"
11 
12 #include <Box.h>
13 #include <Button.h>
14 #include <Catalog.h>
15 #include <LayoutBuilder.h>
16 #include <Locale.h>
17 #include <Message.h>
18 #include <SeparatorView.h>
19 #include <Slider.h>
20 #include <TextControl.h>
21 
22 #include "InputConstants.h"
23 #include "KeyboardView.h"
24 
25 #undef B_TRANSLATION_CONTEXT
26 #define B_TRANSLATION_CONTEXT "InputKeyboard"
27 
28 
InputKeyboard(BInputDevice * dev)29 InputKeyboard::InputKeyboard(BInputDevice* dev)
30 	:
31 	BView("InputKeyboard", B_WILL_DRAW)
32 {
33 	// Add the main settings view
34 	fSettingsView = new KeyboardView();
35 
36 	// Add the "Default" button..
37 	fDefaultsButton = new BButton(B_TRANSLATE("Defaults"),
38         new BMessage(kMsgDefaults));
39 
40 	// Add the "Revert" button...
41 	fRevertButton = new BButton(B_TRANSLATE("Revert"),
42         new BMessage(kMsgRevert));
43 	fRevertButton->SetEnabled(false);
44 
45 	// Build the layout
46 	BLayoutBuilder::Group<>(this, B_VERTICAL)
47 		.AddGroup(B_HORIZONTAL)
48 			.SetInsets(B_USE_WINDOW_SPACING, B_USE_WINDOW_SPACING,
49 				B_USE_WINDOW_SPACING, 0)
50 			.Add(fSettingsView)
51 			.End()
52 		.Add(new BSeparatorView(B_HORIZONTAL))
53 		.AddGroup(B_HORIZONTAL)
54 			.Add(fDefaultsButton)
55 			.Add(fRevertButton)
56 			.AddGlue()
57 			.End();
58 
59 	BSlider* slider = (BSlider*)FindView("key_repeat_rate");
60 	if (slider != NULL)
61 		slider->SetValue(fSettings.KeyboardRepeatRate());
62 
63 	slider = (BSlider*)FindView("delay_until_key_repeat");
64 	if (slider != NULL)
65 		slider->SetValue(fSettings.KeyboardRepeatDelay());
66 
67 	fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
68 }
69 
70 
71 void
MessageReceived(BMessage * message)72 InputKeyboard::MessageReceived(BMessage* message)
73 {
74 	BSlider* slider = NULL;
75 
76 	switch (message->what) {
77 		case kMsgDefaults:
78 		{
79 			fSettings.Defaults();
80 
81 			slider = (BSlider*)FindView("key_repeat_rate");
82 			if (slider != NULL)
83 				slider->SetValue(fSettings.KeyboardRepeatRate());
84 
85 			slider = (BSlider*)FindView("delay_until_key_repeat");
86 			if (slider != NULL)
87 				slider->SetValue(fSettings.KeyboardRepeatDelay());
88 
89 			fDefaultsButton->SetEnabled(false);
90 
91 			fRevertButton->SetEnabled(true);
92 			break;
93 		}
94 		case kMsgRevert:
95 		{
96 			fSettings.Revert();
97 
98 			slider = (BSlider*)FindView("key_repeat_rate");
99 			if (slider != NULL)
100 				slider->SetValue(fSettings.KeyboardRepeatRate());
101 
102 			slider = (BSlider*)FindView("delay_until_key_repeat");
103 			if (slider != NULL)
104 				slider->SetValue(fSettings.KeyboardRepeatDelay());
105 
106 			fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
107 
108 			fRevertButton->SetEnabled(false);
109 			break;
110 		}
111 		case kMsgSliderrepeatrate:
112 		{
113 			int32 rate;
114 			if (message->FindInt32("be:value", &rate) != B_OK)
115 				break;
116 			fSettings.SetKeyboardRepeatRate(rate);
117 
118 			fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
119 
120 			fRevertButton->SetEnabled(true);
121 			break;
122 		}
123 		case kMsgSliderdelayrate:
124 		{
125 			int32 delay;
126 			if (message->FindInt32("be:value", &delay) != B_OK)
127 				break;
128 
129 			// We need to look at the value from the slider and make it "jump"
130 			// to the next notch along. Setting the min and max values of the
131 			// slider to 1 and 4 doesn't work like the real Keyboard app.
132 			if (delay < 375000)
133 				delay = 250000;
134 			if (delay >= 375000 && delay < 625000)
135 				delay = 500000;
136 			if (delay >= 625000 && delay < 875000)
137 				delay = 750000;
138 			if (delay >= 875000)
139 				delay = 1000000;
140 
141 			fSettings.SetKeyboardRepeatDelay(delay);
142 
143 			slider = (BSlider*)FindView("delay_until_key_repeat");
144 			if (slider != NULL)
145 				slider->SetValue(delay);
146 
147 			fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
148 
149 			fRevertButton->SetEnabled(true);
150 			break;
151 		}
152 		default:
153 			BView::MessageReceived(message);
154 	}
155 }
156