xref: /core/framework/source/inc/accelerators/acceleratorcache.hxx (revision a997f6224201dff31dcadf91163876b4d8f557d0)
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 #pragma once
21 
22 #include <stdtypes.h>
23 
24 #include <com/sun/star/awt/KeyEvent.hpp>
25 
26 #include <unordered_map>
27 #include <vector>
28 
29 // definition
30 
31 namespace framework
32 {
33 
34 /**
35     @short  implements a cache for any accelerator configuration.
36 
37     @descr  It's implemented threadsafe, supports copy-on-write pattern
38             and a flush mechanism to support concurrent access to the same
39             configuration.
40 
41             copy-on-write ... How? Do the following:
42  */
43 class AcceleratorCache
44 {
45     public:
46 
47         /**
48             commands -> keys
49         */
50         typedef ::std::vector< css::awt::KeyEvent > TKeyList;
51 
52     private:
53 
54         typedef std::unordered_map<OUString, TKeyList> TCommand2Keys;
55 
56         /**
57             keys -> commands
58         */
59         typedef std::unordered_map< css::awt::KeyEvent ,
60                                     OUString    ,
61                                     KeyEventHashCode   ,
62                                     KeyEventEqualsFunc > TKey2Commands;
63 
64         /** map commands to keys in relation 1:n.
65             First key is interpreted as preferred one! */
66         TCommand2Keys m_lCommand2Keys;
67 
68         /** map keys to commands in relation 1:1. */
69         TKey2Commands m_lKey2Commands;
70 
71     public:
72         /** @short  checks if the specified key exists.
73 
74             @param  aKey
75                     the key, which should be checked.
76 
77             @return [bool]
78                     sal_True if the specified key exists inside this container.
79          */
80         bool hasKey(const css::awt::KeyEvent& aKey) const;
81         bool hasCommand(const OUString& sCommand) const;
82 
83         TKeyList getAllKeys() const;
84 
85         /** @short  add a new or change an existing key-command pair
86                     of this container.
87 
88             @param  aKey
89                     describe the key.
90 
91             @param  sCommand
92                     describe the command.
93           */
94         void setKeyCommandPair(const css::awt::KeyEvent& aKey    ,
95                                        const OUString&    sCommand);
96 
97         /** @short  returns the list of keys, which are registered
98                     for this command.
99 
100             @param  sCommand
101                     describe the command.
102 
103             @return [TKeyList]
104                     the list of registered keys. Can be empty!
105           */
106         TKeyList getKeysByCommand(const OUString& sCommand) const;
107 
108         OUString getCommandByKey(const css::awt::KeyEvent& aKey) const;
109         void removeKey(const css::awt::KeyEvent& aKey);
110         void removeCommand(const OUString& sCommand);
111 };
112 
113 } // namespace framework
114 
115 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
116