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