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/acceleratorcache.hxx> 21 22 #include <com/sun/star/container/NoSuchElementException.hpp> 23 24 #include <vcl/svapp.hxx> 25 26 namespace framework 27 { hasKey(const css::awt::KeyEvent & aKey) const28bool AcceleratorCache::hasKey(const css::awt::KeyEvent& aKey) const 29 { 30 SolarMutexGuard g; 31 return (m_lKey2Commands.find(aKey) != m_lKey2Commands.end()); 32 } 33 hasCommand(const OUString & sCommand) const34bool AcceleratorCache::hasCommand(const OUString& sCommand) const 35 { 36 SolarMutexGuard g; 37 return (m_lCommand2Keys.find(sCommand) != m_lCommand2Keys.end()); 38 } 39 getAllKeys() const40AcceleratorCache::TKeyList AcceleratorCache::getAllKeys() const 41 { 42 SolarMutexGuard g; 43 TKeyList lKeys; 44 lKeys.reserve(m_lKey2Commands.size()); 45 46 for (auto const& key2Command : m_lKey2Commands) 47 { 48 lKeys.push_back(key2Command.first); 49 } 50 51 return lKeys; 52 } 53 setKeyCommandPair(const css::awt::KeyEvent & aKey,const OUString & sCommand)54void AcceleratorCache::setKeyCommandPair(const css::awt::KeyEvent& aKey, const OUString& sCommand) 55 { 56 SolarMutexGuard g; 57 58 // register command for the specified key 59 m_lKey2Commands[aKey] = sCommand; 60 61 // update optimized structure to bind multiple keys to one command 62 TKeyList& rKeyList = m_lCommand2Keys[sCommand]; 63 rKeyList.push_back(aKey); 64 } 65 getKeysByCommand(const OUString & sCommand) const66AcceleratorCache::TKeyList AcceleratorCache::getKeysByCommand(const OUString& sCommand) const 67 { 68 SolarMutexGuard g; 69 TCommand2Keys::const_iterator pCommand = m_lCommand2Keys.find(sCommand); 70 if (pCommand == m_lCommand2Keys.end()) 71 throw css::container::NoSuchElementException(); 72 return pCommand->second; 73 } 74 getCommandByKey(const css::awt::KeyEvent & aKey) const75OUString AcceleratorCache::getCommandByKey(const css::awt::KeyEvent& aKey) const 76 { 77 SolarMutexGuard g; 78 TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey); 79 if (pKey == m_lKey2Commands.end()) 80 throw css::container::NoSuchElementException(); 81 return pKey->second; 82 } 83 removeKey(const css::awt::KeyEvent & aKey)84void AcceleratorCache::removeKey(const css::awt::KeyEvent& aKey) 85 { 86 SolarMutexGuard g; 87 88 // check if key exists 89 TKey2Commands::const_iterator pKey = m_lKey2Commands.find(aKey); 90 if (pKey == m_lKey2Commands.end()) 91 return; 92 93 // get its registered command 94 // Because we must know its place inside the optimized 95 // structure, which bind keys to commands, too! 96 OUString sCommand = pKey->second; 97 pKey = m_lKey2Commands.end(); // nobody should use an undefined value .-) 98 99 // remove key from primary list 100 m_lKey2Commands.erase(aKey); 101 102 // remove key from optimized command list 103 m_lCommand2Keys.erase(sCommand); 104 } 105 removeCommand(const OUString & sCommand)106void AcceleratorCache::removeCommand(const OUString& sCommand) 107 { 108 SolarMutexGuard g; 109 110 const TKeyList& lKeys = getKeysByCommand(sCommand); 111 for (auto const& lKey : lKeys) 112 { 113 removeKey(lKey); 114 } 115 m_lCommand2Keys.erase(sCommand); 116 } 117 118 } // namespace framework 119 120 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 121