Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
5 : * This Source Code Form is subject to the terms of the Mozilla Public
6 : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
11 : * Licensed to the Apache Software Foundation (ASF) under one or more
12 : * contributor license agreements. See the NOTICE file distributed
13 : * with this work for additional information regarding copyright
14 : * ownership. The ASF licenses this file to you under the Apache
15 : * License, Version 2.0 (the "License"); you may not use this file
16 : * except in compliance with the License. You may obtain a copy of
17 : * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 : */
19 :
20 : #include <accelerators/keymapping.hxx>
21 :
22 : #include <macros/generic.hxx>
23 :
24 : #include <com/sun/star/awt/Key.hpp>
25 :
26 :
27 : namespace framework
28 : {
29 :
30 : //_______________________________________________
31 : // helper
32 :
33 : KeyMapping::KeyIdentifierInfo KeyMapping::KeyIdentifierMap[] =
34 : {
35 : {css::awt::Key::NUM0 , "KEY_0" },
36 : {css::awt::Key::NUM1 , "KEY_1" },
37 : {css::awt::Key::NUM2 , "KEY_2" },
38 : {css::awt::Key::NUM3 , "KEY_3" },
39 : {css::awt::Key::NUM4 , "KEY_4" },
40 : {css::awt::Key::NUM5 , "KEY_5" },
41 : {css::awt::Key::NUM6 , "KEY_6" },
42 : {css::awt::Key::NUM7 , "KEY_7" },
43 : {css::awt::Key::NUM8 , "KEY_8" },
44 : {css::awt::Key::NUM9 , "KEY_9" },
45 : {css::awt::Key::A , "KEY_A" },
46 : {css::awt::Key::B , "KEY_B" },
47 : {css::awt::Key::C , "KEY_C" },
48 : {css::awt::Key::D , "KEY_D" },
49 : {css::awt::Key::E , "KEY_E" },
50 : {css::awt::Key::F , "KEY_F" },
51 : {css::awt::Key::G , "KEY_G" },
52 : {css::awt::Key::H , "KEY_H" },
53 : {css::awt::Key::I , "KEY_I" },
54 : {css::awt::Key::J , "KEY_J" },
55 : {css::awt::Key::K , "KEY_K" },
56 : {css::awt::Key::L , "KEY_L" },
57 : {css::awt::Key::M , "KEY_M" },
58 : {css::awt::Key::N , "KEY_N" },
59 : {css::awt::Key::O , "KEY_O" },
60 : {css::awt::Key::P , "KEY_P" },
61 : {css::awt::Key::Q , "KEY_Q" },
62 : {css::awt::Key::R , "KEY_R" },
63 : {css::awt::Key::S , "KEY_S" },
64 : {css::awt::Key::T , "KEY_T" },
65 : {css::awt::Key::U , "KEY_U" },
66 : {css::awt::Key::V , "KEY_V" },
67 : {css::awt::Key::W , "KEY_W" },
68 : {css::awt::Key::X , "KEY_X" },
69 : {css::awt::Key::Y , "KEY_Y" },
70 : {css::awt::Key::Z , "KEY_Z" },
71 : {css::awt::Key::F1 , "KEY_F1" },
72 : {css::awt::Key::F2 , "KEY_F2" },
73 : {css::awt::Key::F3 , "KEY_F3" },
74 : {css::awt::Key::F4 , "KEY_F4" },
75 : {css::awt::Key::F5 , "KEY_F5" },
76 : {css::awt::Key::F6 , "KEY_F6" },
77 : {css::awt::Key::F7 , "KEY_F7" },
78 : {css::awt::Key::F8 , "KEY_F8" },
79 : {css::awt::Key::F9 , "KEY_F9" },
80 : {css::awt::Key::F10 , "KEY_F10" },
81 : {css::awt::Key::F11 , "KEY_F11" },
82 : {css::awt::Key::F12 , "KEY_F12" },
83 : {css::awt::Key::F13 , "KEY_F13" },
84 : {css::awt::Key::F14 , "KEY_F14" },
85 : {css::awt::Key::F15 , "KEY_F15" },
86 : {css::awt::Key::F16 , "KEY_F16" },
87 : {css::awt::Key::F17 , "KEY_F17" },
88 : {css::awt::Key::F18 , "KEY_F18" },
89 : {css::awt::Key::F19 , "KEY_F19" },
90 : {css::awt::Key::F20 , "KEY_F20" },
91 : {css::awt::Key::F21 , "KEY_F21" },
92 : {css::awt::Key::F22 , "KEY_F22" },
93 : {css::awt::Key::F23 , "KEY_F23" },
94 : {css::awt::Key::F24 , "KEY_F24" },
95 : {css::awt::Key::F25 , "KEY_F25" },
96 : {css::awt::Key::F26 , "KEY_F26" },
97 : {css::awt::Key::DOWN , "KEY_DOWN" },
98 : {css::awt::Key::UP , "KEY_UP" },
99 : {css::awt::Key::LEFT , "KEY_LEFT" },
100 : {css::awt::Key::RIGHT , "KEY_RIGHT" },
101 : {css::awt::Key::HOME , "KEY_HOME" },
102 : {css::awt::Key::END , "KEY_END" },
103 : {css::awt::Key::PAGEUP , "KEY_PAGEUP" },
104 : {css::awt::Key::PAGEDOWN , "KEY_PAGEDOWN" },
105 : {css::awt::Key::RETURN , "KEY_RETURN" },
106 : {css::awt::Key::ESCAPE , "KEY_ESCAPE" },
107 : {css::awt::Key::TAB , "KEY_TAB" },
108 : {css::awt::Key::BACKSPACE , "KEY_BACKSPACE" },
109 : {css::awt::Key::SPACE , "KEY_SPACE" },
110 : {css::awt::Key::INSERT , "KEY_INSERT" },
111 : {css::awt::Key::DELETE , "KEY_DELETE" },
112 : {css::awt::Key::ADD , "KEY_ADD" },
113 : {css::awt::Key::SUBTRACT , "KEY_SUBTRACT" },
114 : {css::awt::Key::MULTIPLY , "KEY_MULTIPLY" },
115 : {css::awt::Key::DIVIDE , "KEY_DIVIDE" },
116 : {css::awt::Key::POINT , "KEY_POINT" },
117 : {css::awt::Key::COMMA , "KEY_COMMA" },
118 : {css::awt::Key::LESS , "KEY_LESS" },
119 : {css::awt::Key::GREATER , "KEY_GREATER" },
120 : {css::awt::Key::EQUAL , "KEY_EQUAL" },
121 : {css::awt::Key::OPEN , "KEY_OPEN" },
122 : {css::awt::Key::CUT , "KEY_CUT" },
123 : {css::awt::Key::COPY , "KEY_COPY" },
124 : {css::awt::Key::PASTE , "KEY_PASTE" },
125 : {css::awt::Key::UNDO , "KEY_UNDO" },
126 : {css::awt::Key::REPEAT , "KEY_REPEAT" },
127 : {css::awt::Key::FIND , "KEY_FIND" },
128 : {css::awt::Key::PROPERTIES , "KEY_PROPERTIES" },
129 : {css::awt::Key::FRONT , "KEY_FRONT" },
130 : {css::awt::Key::CONTEXTMENU , "KEY_CONTEXTMENU"},
131 : {css::awt::Key::HELP , "KEY_HELP" },
132 : {css::awt::Key::MENU , "KEY_MENU" },
133 : {css::awt::Key::HANGUL_HANJA , "KEY_HANGUL_HANJA"},
134 : {css::awt::Key::DECIMAL , "KEY_DECIMAL" },
135 : {css::awt::Key::TILDE , "KEY_TILDE" },
136 : {css::awt::Key::QUOTELEFT , "KEY_QUOTELEFT" },
137 : {css::awt::Key::BRACKETLEFT , "KEY_BRACKETLEFT" },
138 : {css::awt::Key::BRACKETRIGHT , "KEY_BRACKETRIGHT" },
139 : {css::awt::Key::SEMICOLON , "KEY_SEMICOLON" },
140 : {0 , "" } // mark the end of this array!
141 : };
142 :
143 : //-----------------------------------------------
144 0 : KeyMapping::KeyMapping()
145 : {
146 0 : sal_Int32 i = 0;
147 0 : while(KeyIdentifierMap[i].Code != 0)
148 : {
149 0 : ::rtl::OUString sIdentifier = ::rtl::OUString::createFromAscii(KeyIdentifierMap[i].Identifier);
150 0 : sal_Int16 nCode = KeyIdentifierMap[i].Code;
151 :
152 0 : m_lIdentifierHash[sIdentifier] = nCode ;
153 0 : m_lCodeHash [nCode] = sIdentifier;
154 :
155 0 : ++i;
156 0 : }
157 0 : }
158 :
159 : //-----------------------------------------------
160 0 : KeyMapping::~KeyMapping()
161 : {
162 0 : }
163 :
164 : //-----------------------------------------------
165 0 : sal_uInt16 KeyMapping::mapIdentifierToCode(const ::rtl::OUString& sIdentifier)
166 : throw(css::lang::IllegalArgumentException)
167 : {
168 0 : Identifier2CodeHash::const_iterator pIt = m_lIdentifierHash.find(sIdentifier);
169 0 : if (pIt != m_lIdentifierHash.end())
170 0 : return pIt->second;
171 :
172 : // Its not well known identifier - but may be a pure key code formated as string ...
173 : // Check and convert it!
174 0 : sal_uInt16 nCode = 0;
175 0 : if (!KeyMapping::impl_st_interpretIdentifierAsPureKeyCode(sIdentifier, nCode))
176 : throw css::lang::IllegalArgumentException(
177 : DECLARE_ASCII("Cant map given identifier to a valid key code value."),
178 : css::uno::Reference< css::uno::XInterface >(),
179 0 : 0);
180 :
181 0 : return (sal_uInt16)nCode;
182 : }
183 :
184 : //-----------------------------------------------
185 0 : ::rtl::OUString KeyMapping::mapCodeToIdentifier(sal_uInt16 nCode)
186 : {
187 0 : Code2IdentifierHash::const_iterator pIt = m_lCodeHash.find(nCode);
188 0 : if (pIt != m_lCodeHash.end())
189 0 : return pIt->second;
190 :
191 : // If we have no well known identifier - use the pure code value!
192 0 : return ::rtl::OUString::valueOf((sal_Int32)nCode);
193 : }
194 :
195 : //-----------------------------------------------
196 0 : sal_Bool KeyMapping::impl_st_interpretIdentifierAsPureKeyCode(const ::rtl::OUString& sIdentifier,
197 : sal_uInt16& rCode )
198 : {
199 0 : sal_Int32 nCode = sIdentifier.toInt32();
200 0 : if (nCode > 0)
201 : {
202 0 : rCode = (sal_uInt16)nCode;
203 0 : return sal_True;
204 : }
205 :
206 : // 0 is normaly an error of the called method toInt32() ...
207 : // But we must be aware, that the identifier is "0"!
208 0 : rCode = 0;
209 0 : return sIdentifier == "0";
210 : }
211 :
212 : } // namespace framework
213 :
214 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|