cgv
Loading...
Searching...
No Matches
shortcut.cxx
1#include "shortcut.h"
2#include "event.h"
3#include <cgv/utils/scan.h>
4
5using namespace cgv::utils;
6
7namespace cgv {
8 namespace gui {
9
10const char* key_strings[] = {
11 "F1",
12 "F2",
13 "F3",
14 "F4",
15 "F5",
16 "F6",
17 "F7",
18 "F8",
19 "F9",
20 "F10",
21 "F11",
22 "F12",
23 "Space",
24 "Enter",
25 "Tab",
26 "Print",
27 "Pause",
28 "Break",
29 "Escape",
30 "Left_Shift",
31 "Right_Shift",
32 "Left_Alt",
33 "Right_Alt",
34 "Left_Ctrl",
35 "Right_Ctrl",
36 "Left_Meta",
37 "Right_Meta",
38 "Left",
39 "Right",
40 "Up",
41 "Down",
42 "Home",
43 "End",
44 "Page_Up",
45 "Page_Down",
46 "Back_Space",
47 "Delete",
48 "Insert",
49 "Caps_Lock",
50 "Num_Lock",
51 "Scroll_Lock",
52 "Num_0",
53 "Num_1",
54 "Num_2",
55 "Num_3",
56 "Num_4",
57 "Num_5",
58 "Num_6",
59 "Num_7",
60 "Num_8",
61 "Num_9",
62 "Num_Div",
63 "Num_Mul",
64 "Num_Sub",
65 "Num_Add",
66 "Num_Dot",
67 "Num_Enter",
68};
69
70std::string get_key_string(unsigned short key)
71{
72 if (key > 255) {
73 unsigned idx = key - 256;
74 if (idx < 57)
75 return key_strings[key - 256];
76 return "";
77 }
78 else {
79 if (key == 0)
80 return "";
81 return std::string(1, (unsigned char) key);
82 }
83}
84
85shortcut::shortcut(unsigned short _key, unsigned char _modifiers) : key(_key), modifiers(_modifiers)
86{
87}
88
89
91void shortcut::validate()
92{
93 if (key == KEY_Left_Shift || key == KEY_Right_Shift)
94 modifiers &= ~EM_SHIFT;
95 if (key == KEY_Left_Ctrl || key == KEY_Right_Ctrl)
96 modifiers &= ~EM_CTRL;
97 if (key == KEY_Left_Alt || key == KEY_Right_Alt)
98 modifiers &= ~EM_ALT;
99}
100
102void shortcut::stream_out(std::ostream& os) const
103{
104 os << get_modifier_string(EventModifier(modifiers)) << get_key_string(key);
105}
106
108bool shortcut::stream_in(std::istream& is)
109{
110 std::string token, read;
111 unsigned char _modifiers = 0;
112 unsigned short _key = 0;
113 char last_sep = 0;
114 while (!is.eof()) {
115 char c;
116 is.get(c);
117 if (is.fail())
118 break;
119 bool do_not_extend_token = false;
120 if (token.empty()) {
121 switch ((unsigned char)c) {
122 case '#' :
123 case '1' :
124 case '2' :
125 case '3' :
126 case '4' :
127 case '5' :
128 case '6' :
129 case '7' :
130 case '8' :
131 case '9' :
132 case '0' :
133 case '+' :
134 case '-' :
135 case ',' :
136 case '.' :
137 case '^' :
138 case '<' :
139 case '\\' :
140 case 0xB4 : // forward tick
141 case ' ' :
142 key = c;
143 modifiers = _modifiers;
144 return true;
145 }
146 }
147 if (c == '-' || c == '+') {
148 if (to_upper(token) == "SHIFT")
149 _modifiers |= EM_SHIFT;
150 else if (to_upper(token) == "ALT")
151 _modifiers |= EM_ALT;
152 else if (to_upper(token) == "CTRL")
153 _modifiers |= EM_CTRL;
154 else if (to_upper(token) == "META")
155 _modifiers |= EM_META;
156 else {
157 is.putback(c);
158 while (read.size() > 0) {
159 is.putback(read[read.size()-1]);
160 read = read.substr(0,read.size()-1);
161 }
162 return false;
163 }
164 do_not_extend_token = true;
165 token.clear();
166 }
167 if (c == ' ' || c == ',' || c == ';') {
168 last_sep = c;
169 break;
170 }
171 if (!do_not_extend_token)
172 token.push_back(c);
173
174 read.push_back(c);
175 }
176 if (token.size() == 1) {
177 key = token[0];
178 modifiers = _modifiers;
179 return true;
180 }
181 for (unsigned i = 256; i < KEY_Last; ++i) {
182 if (to_upper(token) == to_upper(key_strings[i-256])) {
183 key = i;
184 modifiers = _modifiers;
185 return true;
186 }
187 }
188 if (last_sep)
189 is.putback(last_sep);
190 while (read.size() > 0) {
191 is.putback(read[read.size()-1]);
192 read = read.substr(0,read.size()-1);
193 }
194 return false;
195}
196
198std::ostream& operator << (std::ostream& os, const shortcut& sc)
199{
200 sc.stream_out(os);
201 return os;
202}
203
205std::istream& operator >> (std::istream& is, shortcut& sc)
206{
207 if (!sc.stream_in(is))
208 is.setstate(std::ios::failbit);
209 else
210 is.clear();
211 return is;
212}
213
214 }
215}
the shortcut class encapsulates a key with modifiers
Definition shortcut.h:84
bool stream_in(std::istream &is)
read from stream
Definition shortcut.cxx:108
void stream_out(std::ostream &os) const
write to stream
Definition shortcut.cxx:102
std::string get_modifier_string(EventModifier modifiers)
convert a modifier combination into a readable string ending on a '+' sign if not empty,...
Definition event.cxx:37
EventModifier
define constants for event modifiers
Definition event.h:41
@ EM_SHIFT
shift modifier
Definition event.h:42
@ EM_ALT
alt modifier
Definition event.h:43
@ EM_META
meta modifier (windows or mac key)
Definition event.h:45
@ EM_CTRL
ctrl modifier
Definition event.h:44
@ KEY_Left_Ctrl
left ctrl key
Definition shortcut.h:37
@ KEY_Left_Alt
left alt key
Definition shortcut.h:35
@ KEY_Right_Shift
right shift key
Definition shortcut.h:34
@ KEY_Left_Shift
left shift key
Definition shortcut.h:33
@ KEY_Right_Alt
right alt key
Definition shortcut.h:36
@ KEY_Right_Ctrl
right ctrl key
Definition shortcut.h:38
std::string get_key_string(unsigned short key)
convert a key code into a readable string
Definition shortcut.cxx:70
namespace that holds tools that dont fit any other namespace
char to_upper(char c)
convert char to upper case
Definition scan.cxx:106
the cgv namespace
Definition print.h:11
Helper functions to process strings.
representation of a token in a text by two pointers begin and end, that point to the first character ...
Definition token.h:18
bool empty() const
return whether the token is empty
Definition token.h:56
size_t size() const
return the length of the token in number of characters
Definition token.h:54