Coverage Report

Created: 2025-12-31 10:39

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/framework/source/accelerators/keymapping.cxx
Line
Count
Source
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 <com/sun/star/awt/Key.hpp>
23
#include <com/sun/star/lang/IllegalArgumentException.hpp>
24
#include <o3tl/string_view.hxx>
25
26
namespace framework
27
{
28
29
// helper
30
31
KeyMapping::KeyIdentifierInfo constexpr KeyMapping::KeyIdentifierMap[] =
32
{
33
    {css::awt::Key::NUM0          , u"KEY_0"_ustr          },
34
    {css::awt::Key::NUM1          , u"KEY_1"_ustr          },
35
    {css::awt::Key::NUM2          , u"KEY_2"_ustr          },
36
    {css::awt::Key::NUM3          , u"KEY_3"_ustr          },
37
    {css::awt::Key::NUM4          , u"KEY_4"_ustr          },
38
    {css::awt::Key::NUM5          , u"KEY_5"_ustr          },
39
    {css::awt::Key::NUM6          , u"KEY_6"_ustr          },
40
    {css::awt::Key::NUM7          , u"KEY_7"_ustr          },
41
    {css::awt::Key::NUM8          , u"KEY_8"_ustr          },
42
    {css::awt::Key::NUM9          , u"KEY_9"_ustr          },
43
    {css::awt::Key::A             , u"KEY_A"_ustr          },
44
    {css::awt::Key::B             , u"KEY_B"_ustr          },
45
    {css::awt::Key::C             , u"KEY_C"_ustr          },
46
    {css::awt::Key::D             , u"KEY_D"_ustr          },
47
    {css::awt::Key::E             , u"KEY_E"_ustr          },
48
    {css::awt::Key::F             , u"KEY_F"_ustr          },
49
    {css::awt::Key::G             , u"KEY_G"_ustr          },
50
    {css::awt::Key::H             , u"KEY_H"_ustr          },
51
    {css::awt::Key::I             , u"KEY_I"_ustr          },
52
    {css::awt::Key::J             , u"KEY_J"_ustr          },
53
    {css::awt::Key::K             , u"KEY_K"_ustr          },
54
    {css::awt::Key::L             , u"KEY_L"_ustr          },
55
    {css::awt::Key::M             , u"KEY_M"_ustr          },
56
    {css::awt::Key::N             , u"KEY_N"_ustr          },
57
    {css::awt::Key::O             , u"KEY_O"_ustr          },
58
    {css::awt::Key::P             , u"KEY_P"_ustr          },
59
    {css::awt::Key::Q             , u"KEY_Q"_ustr          },
60
    {css::awt::Key::R             , u"KEY_R"_ustr          },
61
    {css::awt::Key::S             , u"KEY_S"_ustr          },
62
    {css::awt::Key::T             , u"KEY_T"_ustr          },
63
    {css::awt::Key::U             , u"KEY_U"_ustr          },
64
    {css::awt::Key::V             , u"KEY_V"_ustr          },
65
    {css::awt::Key::W             , u"KEY_W"_ustr          },
66
    {css::awt::Key::X             , u"KEY_X"_ustr          },
67
    {css::awt::Key::Y             , u"KEY_Y"_ustr          },
68
    {css::awt::Key::Z             , u"KEY_Z"_ustr          },
69
    {css::awt::Key::F1            , u"KEY_F1"_ustr         },
70
    {css::awt::Key::F2            , u"KEY_F2"_ustr         },
71
    {css::awt::Key::F3            , u"KEY_F3"_ustr         },
72
    {css::awt::Key::F4            , u"KEY_F4"_ustr         },
73
    {css::awt::Key::F5            , u"KEY_F5"_ustr         },
74
    {css::awt::Key::F6            , u"KEY_F6"_ustr         },
75
    {css::awt::Key::F7            , u"KEY_F7"_ustr         },
76
    {css::awt::Key::F8            , u"KEY_F8"_ustr         },
77
    {css::awt::Key::F9            , u"KEY_F9"_ustr         },
78
    {css::awt::Key::F10           , u"KEY_F10"_ustr        },
79
    {css::awt::Key::F11           , u"KEY_F11"_ustr        },
80
    {css::awt::Key::F12           , u"KEY_F12"_ustr        },
81
    {css::awt::Key::F13           , u"KEY_F13"_ustr        },
82
    {css::awt::Key::F14           , u"KEY_F14"_ustr        },
83
    {css::awt::Key::F15           , u"KEY_F15"_ustr        },
84
    {css::awt::Key::F16           , u"KEY_F16"_ustr        },
85
    {css::awt::Key::F17           , u"KEY_F17"_ustr        },
86
    {css::awt::Key::F18           , u"KEY_F18"_ustr        },
87
    {css::awt::Key::F19           , u"KEY_F19"_ustr        },
88
    {css::awt::Key::F20           , u"KEY_F20"_ustr        },
89
    {css::awt::Key::F21           , u"KEY_F21"_ustr        },
90
    {css::awt::Key::F22           , u"KEY_F22"_ustr        },
91
    {css::awt::Key::F23           , u"KEY_F23"_ustr        },
92
    {css::awt::Key::F24           , u"KEY_F24"_ustr        },
93
    {css::awt::Key::F25           , u"KEY_F25"_ustr        },
94
    {css::awt::Key::F26           , u"KEY_F26"_ustr        },
95
    {css::awt::Key::DOWN          , u"KEY_DOWN"_ustr       },
96
    {css::awt::Key::UP            , u"KEY_UP"_ustr         },
97
    {css::awt::Key::LEFT          , u"KEY_LEFT"_ustr       },
98
    {css::awt::Key::RIGHT         , u"KEY_RIGHT"_ustr      },
99
    {css::awt::Key::HOME          , u"KEY_HOME"_ustr       },
100
    {css::awt::Key::END           , u"KEY_END"_ustr        },
101
    {css::awt::Key::PAGEUP        , u"KEY_PAGEUP"_ustr     },
102
    {css::awt::Key::PAGEDOWN      , u"KEY_PAGEDOWN"_ustr   },
103
    {css::awt::Key::RETURN        , u"KEY_RETURN"_ustr     },
104
    {css::awt::Key::ESCAPE        , u"KEY_ESCAPE"_ustr     },
105
    {css::awt::Key::TAB           , u"KEY_TAB"_ustr        },
106
    {css::awt::Key::BACKSPACE     , u"KEY_BACKSPACE"_ustr  },
107
    {css::awt::Key::SPACE         , u"KEY_SPACE"_ustr      },
108
    {css::awt::Key::INSERT        , u"KEY_INSERT"_ustr     },
109
    {css::awt::Key::DELETE        , u"KEY_DELETE"_ustr     },
110
    {css::awt::Key::ADD           , u"KEY_ADD"_ustr        },
111
    {css::awt::Key::SUBTRACT      , u"KEY_SUBTRACT"_ustr   },
112
    {css::awt::Key::MULTIPLY      , u"KEY_MULTIPLY"_ustr   },
113
    {css::awt::Key::DIVIDE        , u"KEY_DIVIDE"_ustr     },
114
    {css::awt::Key::POINT         , u"KEY_POINT"_ustr      },
115
    {css::awt::Key::COMMA         , u"KEY_COMMA"_ustr      },
116
    {css::awt::Key::LESS          , u"KEY_LESS"_ustr       },
117
    {css::awt::Key::GREATER       , u"KEY_GREATER"_ustr    },
118
    {css::awt::Key::EQUAL         , u"KEY_EQUAL"_ustr      },
119
    {css::awt::Key::OPEN          , u"KEY_OPEN"_ustr       },
120
    {css::awt::Key::CUT           , u"KEY_CUT"_ustr        },
121
    {css::awt::Key::COPY          , u"KEY_COPY"_ustr       },
122
    {css::awt::Key::PASTE         , u"KEY_PASTE"_ustr      },
123
    {css::awt::Key::UNDO          , u"KEY_UNDO"_ustr       },
124
    {css::awt::Key::REPEAT        , u"KEY_REPEAT"_ustr     },
125
    {css::awt::Key::FIND          , u"KEY_FIND"_ustr       },
126
    {css::awt::Key::PROPERTIES    , u"KEY_PROPERTIES"_ustr },
127
    {css::awt::Key::FRONT         , u"KEY_FRONT"_ustr      },
128
    {css::awt::Key::CONTEXTMENU   , u"KEY_CONTEXTMENU"_ustr},
129
    {css::awt::Key::HELP          , u"KEY_HELP"_ustr       },
130
    {css::awt::Key::MENU          , u"KEY_MENU"_ustr       },
131
    {css::awt::Key::HANGUL_HANJA  , u"KEY_HANGUL_HANJA"_ustr},
132
    {css::awt::Key::DECIMAL       , u"KEY_DECIMAL"_ustr    },
133
    {css::awt::Key::TILDE         , u"KEY_TILDE"_ustr      },
134
    {css::awt::Key::QUOTELEFT     , u"KEY_QUOTELEFT"_ustr  },
135
    {css::awt::Key::BRACKETLEFT   , u"KEY_BRACKETLEFT"_ustr },
136
    {css::awt::Key::BRACKETRIGHT  , u"KEY_BRACKETRIGHT"_ustr },
137
    {css::awt::Key::SEMICOLON     , u"KEY_SEMICOLON"_ustr },
138
    {css::awt::Key::QUOTERIGHT    , u"KEY_QUOTERIGHT"_ustr },
139
    {css::awt::Key::RIGHTCURLYBRACKET, u"KEY_RIGHTCURLYBRACKET"_ustr },
140
    {css::awt::Key::NUMBERSIGN, u"KEY_NUMBERSIGN"_ustr },
141
    {css::awt::Key::COLON         , u"KEY_COLON"_ustr },
142
    {0                            , u""_ustr               } // mark the end of this array!
143
};
144
145
KeyMapping::KeyMapping()
146
0
{
147
0
    sal_Int32 i = 0;
148
0
    while(KeyIdentifierMap[i].Code != 0)
149
0
    {
150
0
        m_lIdentifierHash[KeyIdentifierMap[i].Identifier] = KeyIdentifierMap[i].Code;
151
0
        m_lCodeHash      [KeyIdentifierMap[i].Code]       = KeyIdentifierMap[i].Identifier;
152
153
0
        ++i;
154
0
    }
155
0
}
156
157
0
KeyMapping & KeyMapping::get() {
158
0
    static KeyMapping KEYS;
159
0
    return KEYS;
160
0
}
161
162
sal_uInt16 KeyMapping::mapIdentifierToCode(const OUString& sIdentifier)
163
0
{
164
0
    Identifier2CodeHash::const_iterator pIt = m_lIdentifierHash.find(sIdentifier);
165
0
    if (pIt != m_lIdentifierHash.end())
166
0
        return pIt->second;
167
168
    // It's not well known identifier - but may be a pure key code formatted as string...
169
    // Check and convert it!
170
0
    sal_uInt16 nCode = 0;
171
0
    if (!KeyMapping::impl_st_interpretIdentifierAsPureKeyCode(sIdentifier, nCode))
172
0
        throw css::lang::IllegalArgumentException(
173
0
                u"Can not map given identifier to a valid key code value."_ustr,
174
0
                css::uno::Reference< css::uno::XInterface >(),
175
0
                0);
176
177
0
    return nCode;
178
0
}
179
180
OUString KeyMapping::mapCodeToIdentifier(sal_uInt16 nCode)
181
0
{
182
0
    Code2IdentifierHash::const_iterator pIt = m_lCodeHash.find(nCode);
183
0
    if (pIt != m_lCodeHash.end())
184
0
        return pIt->second;
185
186
    // If we have no well known identifier - use the pure code value!
187
0
    return OUString::number(nCode);
188
0
}
189
190
bool KeyMapping::impl_st_interpretIdentifierAsPureKeyCode(std::u16string_view sIdentifier,
191
                                                                sal_uInt16&      rCode      )
192
0
{
193
0
    sal_Int32 nCode = o3tl::toInt32(sIdentifier);
194
0
    if (nCode > 0)
195
0
    {
196
0
        rCode = static_cast<sal_uInt16>(nCode);
197
0
        return true;
198
0
    }
199
200
    // 0 is normally an error of the called method toInt32() ...
201
    // But we must be aware, that the identifier is "0"!
202
0
    rCode = 0;
203
0
    return sIdentifier == u"0";
204
0
}
205
206
} // namespace framework
207
208
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */