1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <accelerators/acceleratorcache.hxx>
22 #include <com/sun/star/container/NoSuchElementException.hpp>
24 #include <vcl/svapp.hxx>
28 bool AcceleratorCache::hasKey(const css::awt::KeyEvent
& aKey
) const
30 return (m_lKey2Commands
.find(aKey
) != m_lKey2Commands
.end());
33 bool AcceleratorCache::hasCommand(const OUString
& sCommand
) const
35 return (m_lCommand2Keys
.find(sCommand
) != m_lCommand2Keys
.end());
38 AcceleratorCache::TKeyList
AcceleratorCache::getAllKeys() const
41 lKeys
.reserve(m_lKey2Commands
.size());
43 for (auto const& key2Command
: m_lKey2Commands
)
45 lKeys
.push_back(key2Command
.first
);
51 void AcceleratorCache::setKeyCommandPair(const css::awt::KeyEvent
& aKey
, const OUString
& sCommand
)
53 // register command for the specified key
54 m_lKey2Commands
[aKey
] = sCommand
;
56 // update optimized structure to bind multiple keys to one command
57 TKeyList
& rKeyList
= m_lCommand2Keys
[sCommand
];
58 rKeyList
.push_back(aKey
);
61 AcceleratorCache::TKeyList
AcceleratorCache::getKeysByCommand(const OUString
& sCommand
) const
63 TCommand2Keys::const_iterator pCommand
= m_lCommand2Keys
.find(sCommand
);
64 if (pCommand
== m_lCommand2Keys
.end())
65 throw css::container::NoSuchElementException();
66 return pCommand
->second
;
69 OUString
AcceleratorCache::getCommandByKey(const css::awt::KeyEvent
& aKey
) const
71 TKey2Commands::const_iterator pKey
= m_lKey2Commands
.find(aKey
);
72 if (pKey
== m_lKey2Commands
.end())
73 throw css::container::NoSuchElementException();
77 void AcceleratorCache::removeKey(const css::awt::KeyEvent
& aKey
)
79 // check if key exists
80 TKey2Commands::const_iterator pKey
= m_lKey2Commands
.find(aKey
);
81 if (pKey
== m_lKey2Commands
.end())
84 // get its registered command
85 // Because we must know its place inside the optimized
86 // structure, which bind keys to commands, too!
87 OUString sCommand
= pKey
->second
;
88 pKey
= m_lKey2Commands
.end(); // nobody should use an undefined value .-)
90 // remove key from primary list
91 m_lKey2Commands
.erase(aKey
);
93 // get keylist for that command
94 TCommand2Keys::iterator pCommand
= m_lCommand2Keys
.find(sCommand
);
95 if (pCommand
== m_lCommand2Keys
.end())
97 TKeyList
& lKeys
= pCommand
->second
;
99 // one or more keys assign
100 if (lKeys
.size() == 1)
101 // remove key from optimized command list
102 m_lCommand2Keys
.erase(sCommand
);
103 else // only remove this key from the keylist
105 auto pKeys
= ::std::find(lKeys
.begin(), lKeys
.end(), aKey
);
107 if (pKeys
!= lKeys
.end())
112 void AcceleratorCache::removeCommand(const OUString
& sCommand
)
114 const TKeyList
& lKeys
= getKeysByCommand(sCommand
);
115 for (auto const& lKey
: lKeys
)
121 } // namespace framework
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */