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