xref: /haiku/src/preferences/input/InputKeyboard.cpp (revision 9d010ea47db677131e385b5e7855d38fd0c8103f)
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 			.SetInsets(B_USE_WINDOW_SPACING, 0, B_USE_WINDOW_SPACING,
54 				B_USE_WINDOW_SPACING)
55 			.Add(fDefaultsButton)
56 			.Add(fRevertButton)
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 void
71 InputKeyboard::MessageReceived(BMessage* message)
72 {
73 	BSlider* slider = NULL;
74 
75 	switch (message->what) {
76 		case kMsgDefaults:
77 		{
78 			fSettings.Defaults();
79 
80 			slider = (BSlider* )FindView("key_repeat_rate");
81 			if (slider !=NULL)
82 				slider->SetValue(fSettings.KeyboardRepeatRate());
83 
84 			slider = (BSlider* )FindView("delay_until_key_repeat");
85 			if (slider !=NULL)
86 				slider->SetValue(fSettings.KeyboardRepeatDelay());
87 
88 			fDefaultsButton->SetEnabled(false);
89 
90 			fRevertButton->SetEnabled(true);
91 			break;
92 		}
93 		case kMsgRevert:
94 		{
95 			fSettings.Revert();
96 
97 			slider = (BSlider* )FindView("key_repeat_rate");
98 			if (slider !=NULL)
99 				slider->SetValue(fSettings.KeyboardRepeatRate());
100 
101 			slider = (BSlider* )FindView("delay_until_key_repeat");
102 			if (slider !=NULL)
103 				slider->SetValue(fSettings.KeyboardRepeatDelay());
104 
105 			fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
106 
107 			fRevertButton->SetEnabled(false);
108 			break;
109 		}
110 		case kMsgSliderrepeatrate:
111 		{
112 			int32 rate;
113 			if (message->FindInt32("be:value", &rate) != B_OK)
114 				break;
115 			fSettings.SetKeyboardRepeatRate(rate);
116 
117 			fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
118 
119 			fRevertButton->SetEnabled(true);
120 			break;
121 		}
122 		case kMsgSliderdelayrate:
123 		{
124 			int32 delay;
125 			if (message->FindInt32("be:value", &delay) != B_OK)
126 				break;
127 
128 			// We need to look at the value from the slider and make it "jump"
129 			// to the next notch along. Setting the min and max values of the
130 			// slider to 1 and 4 doesn't work like the real Keyboard app.
131 			if (delay < 375000)
132 				delay = 250000;
133 			if (delay >= 375000 && delay < 625000)
134 				delay = 500000;
135 			if (delay >= 625000 && delay < 875000)
136 				delay = 750000;
137 			if (delay >= 875000)
138 				delay = 1000000;
139 
140 			fSettings.SetKeyboardRepeatDelay(delay);
141 
142 			slider = (BSlider* )FindView("delay_until_key_repeat");
143 			if (slider != NULL)
144 				slider->SetValue(delay);
145 
146 			fDefaultsButton->SetEnabled(fSettings.IsDefaultable());
147 
148 			fRevertButton->SetEnabled(true);
149 			break;
150 	    }
151     }
152 }