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