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 <xml/acceleratorconfigurationreader.hxx>
24 #include <com/sun/star/container/ElementExistException.hpp>
26 #include <com/sun/star/container/NoSuchElementException.hpp>
28 #include <vcl/svapp.hxx>
33 AcceleratorCache::AcceleratorCache()
37 AcceleratorCache::AcceleratorCache(const AcceleratorCache
& rCopy
)
39 m_lCommand2Keys
= rCopy
.m_lCommand2Keys
;
40 m_lKey2Commands
= rCopy
.m_lKey2Commands
;
43 AcceleratorCache::~AcceleratorCache()
45 // Dont save anything automatically here.
46 // The user has to do that explicitly!
49 void AcceleratorCache::takeOver(const AcceleratorCache
& rCopy
)
52 m_lCommand2Keys
= rCopy
.m_lCommand2Keys
;
53 m_lKey2Commands
= rCopy
.m_lKey2Commands
;
56 AcceleratorCache
& AcceleratorCache::operator=(const AcceleratorCache
& rCopy
)
62 bool AcceleratorCache::hasKey(const css::awt::KeyEvent
& aKey
) const
65 return (m_lKey2Commands
.find(aKey
) != m_lKey2Commands
.end());
68 bool AcceleratorCache::hasCommand(const OUString
& sCommand
) const
71 return (m_lCommand2Keys
.find(sCommand
) != m_lCommand2Keys
.end());
74 AcceleratorCache::TKeyList
AcceleratorCache::getAllKeys() const
78 lKeys
.reserve(m_lKey2Commands
.size());
80 TKey2Commands::const_iterator pIt
;
81 TKey2Commands::const_iterator pEnd
= m_lKey2Commands
.end();
82 for ( pIt
= m_lKey2Commands
.begin();
86 lKeys
.push_back(pIt
->first
);
92 void AcceleratorCache::setKeyCommandPair(const css::awt::KeyEvent
& aKey
,
93 const OUString
& sCommand
)
97 // register command for the specified key
98 m_lKey2Commands
[aKey
] = sCommand
;
100 // update optimized structure to bind multiple keys to one command
101 TKeyList
& rKeyList
= m_lCommand2Keys
[sCommand
];
102 rKeyList
.push_back(aKey
);
105 AcceleratorCache::TKeyList
AcceleratorCache::getKeysByCommand(const OUString
& sCommand
) const
108 TCommand2Keys::const_iterator pCommand
= m_lCommand2Keys
.find(sCommand
);
109 if (pCommand
== m_lCommand2Keys
.end())
110 throw css::container::NoSuchElementException();
111 return pCommand
->second
;
114 OUString
AcceleratorCache::getCommandByKey(const css::awt::KeyEvent
& aKey
) const
117 TKey2Commands::const_iterator pKey
= m_lKey2Commands
.find(aKey
);
118 if (pKey
== m_lKey2Commands
.end())
119 throw css::container::NoSuchElementException();
123 void AcceleratorCache::removeKey(const css::awt::KeyEvent
& aKey
)
127 // check if key exists
128 TKey2Commands::const_iterator pKey
= m_lKey2Commands
.find(aKey
);
129 if (pKey
== m_lKey2Commands
.end())
132 // get its registered command
133 // Because we must know its place inside the optimized
134 // structure, which bind keys to commands, too!
135 OUString sCommand
= pKey
->second
;
136 pKey
= m_lKey2Commands
.end(); // nobody should use an undefined value .-)
138 // remove key from primary list
139 m_lKey2Commands
.erase(aKey
);
141 // remove key from optimized command list
142 m_lCommand2Keys
.erase(sCommand
);
145 void AcceleratorCache::removeCommand(const OUString
& sCommand
)
149 const TKeyList
& lKeys
= getKeysByCommand(sCommand
);
150 AcceleratorCache::TKeyList::const_iterator pKey
;
151 for ( pKey
= lKeys
.begin();
155 const css::awt::KeyEvent
& rKey
= *pKey
;
158 m_lCommand2Keys
.erase(sCommand
);
161 } // namespace framework
163 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */