Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <AK/String.h>
28#include <AK/StringBuilder.h>
29#include <AK/Vector.h>
30#include <LibGUI/Shortcut.h>
31
32namespace GUI {
33
34static String key_code_to_string(KeyCode key)
35{
36 switch (key) {
37 case Key_Escape:
38 return "Escape";
39 case Key_Tab:
40 return "Tab";
41 case Key_Backspace:
42 return "Backspace";
43 case Key_Return:
44 return "Return";
45 case Key_Insert:
46 return "Insert";
47 case Key_Delete:
48 return "Delete";
49 case Key_PrintScreen:
50 return "PrintScreen";
51 case Key_SysRq:
52 return "SysRq";
53 case Key_Home:
54 return "Home";
55 case Key_End:
56 return "End";
57 case Key_Left:
58 return "Left";
59 case Key_Up:
60 return "Up";
61 case Key_Right:
62 return "Right";
63 case Key_Down:
64 return "Down";
65 case Key_PageUp:
66 return "PageUp";
67 case Key_PageDown:
68 return "PageDown";
69 case Key_Shift:
70 return "Shift";
71 case Key_Control:
72 return "Control";
73 case Key_Alt:
74 return "Alt";
75 case Key_CapsLock:
76 return "CapsLock";
77 case Key_NumLock:
78 return "NumLock";
79 case Key_ScrollLock:
80 return "ScrollLock";
81 case Key_F1:
82 return "F1";
83 case Key_F2:
84 return "F2";
85 case Key_F3:
86 return "F3";
87 case Key_F4:
88 return "F4";
89 case Key_F5:
90 return "F5";
91 case Key_F6:
92 return "F6";
93 case Key_F7:
94 return "F7";
95 case Key_F8:
96 return "F8";
97 case Key_F9:
98 return "F9";
99 case Key_F10:
100 return "F10";
101 case Key_F11:
102 return "F11";
103 case Key_F12:
104 return "F12";
105 case Key_Space:
106 return "Space";
107 case Key_ExclamationPoint:
108 return "!";
109 case Key_DoubleQuote:
110 return "\"";
111 case Key_Hashtag:
112 return "#";
113 case Key_Dollar:
114 return "$";
115 case Key_Percent:
116 return "%";
117 case Key_Ampersand:
118 return "&";
119 case Key_Apostrophe:
120 return "'";
121 case Key_LeftParen:
122 return "(";
123 case Key_RightParen:
124 return ")";
125 case Key_Asterisk:
126 return "*";
127 case Key_Plus:
128 return "+";
129 case Key_Comma:
130 return ",";
131 case Key_Minus:
132 return "-";
133 case Key_Period:
134 return ",";
135 case Key_Slash:
136 return "/";
137 case Key_0:
138 return "0";
139 case Key_1:
140 return "1";
141 case Key_2:
142 return "2";
143 case Key_3:
144 return "3";
145 case Key_4:
146 return "4";
147 case Key_5:
148 return "5";
149 case Key_6:
150 return "6";
151 case Key_7:
152 return "7";
153 case Key_8:
154 return "8";
155 case Key_9:
156 return "9";
157 case Key_Colon:
158 return ":";
159 case Key_Semicolon:
160 return ";";
161 case Key_LessThan:
162 return "<";
163 case Key_Equal:
164 return "=";
165 case Key_GreaterThan:
166 return ">";
167 case Key_QuestionMark:
168 return "?";
169 case Key_AtSign:
170 return "@";
171 case Key_A:
172 return "A";
173 case Key_B:
174 return "B";
175 case Key_C:
176 return "C";
177 case Key_D:
178 return "D";
179 case Key_E:
180 return "E";
181 case Key_F:
182 return "F";
183 case Key_G:
184 return "G";
185 case Key_H:
186 return "H";
187 case Key_I:
188 return "I";
189 case Key_J:
190 return "J";
191 case Key_K:
192 return "K";
193 case Key_L:
194 return "L";
195 case Key_M:
196 return "M";
197 case Key_N:
198 return "N";
199 case Key_O:
200 return "O";
201 case Key_P:
202 return "P";
203 case Key_Q:
204 return "Q";
205 case Key_R:
206 return "R";
207 case Key_S:
208 return "S";
209 case Key_T:
210 return "T";
211 case Key_U:
212 return "U";
213 case Key_V:
214 return "V";
215 case Key_W:
216 return "W";
217 case Key_X:
218 return "X";
219 case Key_Y:
220 return "Y";
221 case Key_Z:
222 return "Z";
223 case Key_LeftBracket:
224 return "[";
225 case Key_RightBracket:
226 return "]";
227 case Key_Backslash:
228 return "\\";
229 case Key_Circumflex:
230 return "^";
231 case Key_Underscore:
232 return "_";
233 case Key_LeftBrace:
234 return "{";
235 case Key_RightBrace:
236 return "}";
237 case Key_Pipe:
238 return "|";
239 case Key_Tilde:
240 return "~";
241 case Key_Backtick:
242 return "`";
243
244 case Key_Invalid:
245 return "Invalid";
246 default:
247 ASSERT_NOT_REACHED();
248 }
249}
250
251String Shortcut::to_string() const
252{
253 Vector<String, 8> parts;
254
255 if (m_modifiers & Mod_Ctrl)
256 parts.append("Ctrl");
257 if (m_modifiers & Mod_Shift)
258 parts.append("Shift");
259 if (m_modifiers & Mod_Alt)
260 parts.append("Alt");
261 if (m_modifiers & Mod_Logo)
262 parts.append("Logo");
263
264 parts.append(key_code_to_string(m_key));
265
266 StringBuilder builder;
267 for (size_t i = 0; i < parts.size(); ++i) {
268 builder.append(parts[i]);
269 if (i != parts.size() - 1)
270 builder.append('+');
271 }
272 return builder.to_string();
273}
274
275}