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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_vcl.hxx"
26 #include <vcl/mnemonicengine.hxx>
27 
28 #include <vcl/i18nhelp.hxx>
29 #include <vcl/svapp.hxx>
30 #include <vcl/event.hxx>
31 
32 //........................................................................
33 namespace vcl
34 {
35 //........................................................................
36 
37 	//====================================================================
38 	//= MnemonicEngine_Data
39 	//====================================================================
40     struct MnemonicEngine_Data
41     {
42         IMnemonicEntryList& rEntryList;
43 
MnemonicEngine_Datavcl::MnemonicEngine_Data44         MnemonicEngine_Data( IMnemonicEntryList& _rEntryList )
45             :rEntryList( _rEntryList )
46         {
47         }
48     };
49 
50 	//--------------------------------------------------------------------
51     namespace
52     {
lcl_getEntryForMnemonic(IMnemonicEntryList & _rEntryList,sal_Unicode _cMnemonic,bool & _rbAmbiguous)53         const void* lcl_getEntryForMnemonic( IMnemonicEntryList& _rEntryList, sal_Unicode _cMnemonic, bool& _rbAmbiguous )
54         {
55             _rbAmbiguous = false;
56 
57             const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
58 
59             String sEntryText;
60             const void* pSearchEntry = _rEntryList.FirstSearchEntry( sEntryText );
61 
62             const void* pFirstFoundEntry = NULL;
63             bool bCheckingAmbiguity = false;
64             const void* pStartedWith = pSearchEntry;
65             while ( pSearchEntry )
66             {
67 		        if ( rI18nHelper.MatchMnemonic( sEntryText, _cMnemonic ) )
68                 {
69                     if ( bCheckingAmbiguity )
70                     {
71                         // that's the second (at least) entry with this mnemonic
72                         _rbAmbiguous = true;
73                         return pFirstFoundEntry;
74                     }
75 
76                     pFirstFoundEntry = pSearchEntry;
77                     bCheckingAmbiguity = true;
78                 }
79 
80                 pSearchEntry = _rEntryList.NextSearchEntry( pSearchEntry, sEntryText );
81                 if ( pSearchEntry == pStartedWith )
82                     break;
83             }
84 
85             return pFirstFoundEntry;
86         }
87     }
88 
89 	//====================================================================
90 	//= MnemonicEngine
91 	//====================================================================
92 	//--------------------------------------------------------------------
MnemonicEngine(IMnemonicEntryList & _rEntryList)93     MnemonicEngine::MnemonicEngine( IMnemonicEntryList& _rEntryList )
94         :m_pData( new MnemonicEngine_Data( _rEntryList ) )
95     {
96     }
97 
98 	//--------------------------------------------------------------------
HandleKeyEvent(const KeyEvent & _rKEvt)99     bool MnemonicEngine::HandleKeyEvent( const KeyEvent& _rKEvt )
100     {
101 	    sal_Bool bAccelKey = _rKEvt.GetKeyCode().IsMod2();
102         if ( !bAccelKey )
103             return false;
104 
105 	    sal_Unicode cChar = _rKEvt.GetCharCode();
106         bool bAmbiguous = false;
107         const void* pEntry = lcl_getEntryForMnemonic( m_pData->rEntryList, cChar, bAmbiguous );
108         if ( !pEntry )
109             return false;
110 
111         m_pData->rEntryList.SelectSearchEntry( pEntry );
112         if ( !bAmbiguous )
113             m_pData->rEntryList.ExecuteSearchEntry( pEntry );
114 
115         // handled
116         return true;
117     }
118 
119 	//--------------------------------------------------------------------
~MnemonicEngine()120     MnemonicEngine::~MnemonicEngine()
121     {
122     }
123 
124 //........................................................................
125 } // namespace vcl
126 //........................................................................
127