1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef SD_TOOLPANEL_FOCUS_MANAGER_HXX
25 #define SD_TOOLPANEL_FOCUS_MANAGER_HXX
26 
27 #include <tools/link.hxx>
28 
29 #include <memory>
30 
31 class KeyCode;
32 class VclSimpleEvent;
33 class Window;
34 
35 namespace sd { namespace toolpanel {
36 
37 /** On certain key presses the focus is moved from one window to another.
38     For this to work every window that wants its focus managed has to
39     register or be registered and tell where to put the focus on what key
40     press.
41 */
42 class FocusManager
43 {
44 public:
45     /** Return an instance of the focus manager.
46     */
47     static FocusManager& Instance (void);
48 
49     /** Register a link from one window to another so that any time the
50         specified key is pressed while the source window is focused, the
51         focus is transferred to the target window.
52         @param pSource
53             The window from which the focus will be transferred.
54         @param pTarget
55             The window to which the focus will be transferred.
56         @param rKey
57             The key for which the focus is transferred from the source
58             window to the target window.
59     */
60     void RegisterLink (
61         ::Window* pSource,
62         ::Window* pTarget,
63         const KeyCode& rKey);
64 
65     /** Register a link that will move the focus from the source window to
66         the target window when the source window is focused and KEY_ESCAPE
67         is pressed.
68         @param pSource
69             The window from which the focus will be transferred.
70         @param pTarget
71             The window to which the focus will be transferred.
72     */
73     void RegisterUpLink (::Window* pSource, ::Window* pTarget);
74 
75     /** Register a link that will move the focus from the source window to
76         the target window when the source window is focused and KEY_RETURN
77         is pressed.
78         @param pSource
79             The window from which the focus will be transferred.
80         @param pTarget
81             The window to which the focus will be transferred.
82     */
83     void RegisterDownLink (::Window* pSource, ::Window* pTarget);
84 
85     /** Remove all links from the source window to the target window.  When
86         there are links from the target window to the source window then
87         these are not touced.
88     */
89     void RemoveLinks (
90         ::Window* pSource,
91         ::Window* pTarget);
92 
93     /** Let the focus manager transfer the focus from the specified source
94         window to a target window that is determined according the the
95         registered links and the given key code.
96         When there is no rule for this combination of source window and key
97         code then the focus stays where it is.
98     */
99     bool TransferFocus (::Window* pSource, const KeyCode& rCode);
100 
101 private:
102     class LinkMap;
103     ::std::auto_ptr<LinkMap> mpLinks;
104 
105     FocusManager (void);
106     ~FocusManager (void);
107 
108     /** Clear the list of focus transfer links.  This removes all window
109         listeners.
110     */
111     void Clear (void);
112 
113     /** Remove all links from or to the given window.
114     */
115     void RemoveLinks (::Window* pWindow);
116 
117     /** Unregister as event listener from the given window when there are no
118         links from this window anymore.
119     */
120     void RemoveUnusedEventListener (::Window* pWindow);
121 
122     /** Listen for key events and on KEY_RETURN go down and on
123         KEY_ESCAPE go up.
124     */
125     DECL_LINK(WindowEventListener, VclSimpleEvent*);
126 };
127 
128 } } // end of namespace ::sd::toolpanel
129 
130 #endif
131