xref: /AOO41X/main/vcl/source/window/mnemonicengine.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_vcl.hxx"
30 #include <vcl/mnemonicengine.hxx>
31 
32 #include <vcl/i18nhelp.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/event.hxx>
35 
36 //........................................................................
37 namespace vcl
38 {
39 //........................................................................
40 
41 	//====================================================================
42 	//= MnemonicEngine_Data
43 	//====================================================================
44     struct MnemonicEngine_Data
45     {
46         IMnemonicEntryList& rEntryList;
47 
48         MnemonicEngine_Data( IMnemonicEntryList& _rEntryList )
49             :rEntryList( _rEntryList )
50         {
51         }
52     };
53 
54 	//--------------------------------------------------------------------
55     namespace
56     {
57         const void* lcl_getEntryForMnemonic( IMnemonicEntryList& _rEntryList, sal_Unicode _cMnemonic, bool& _rbAmbiguous )
58         {
59             _rbAmbiguous = false;
60 
61             const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetUILocaleI18nHelper();
62 
63             String sEntryText;
64             const void* pSearchEntry = _rEntryList.FirstSearchEntry( sEntryText );
65 
66             const void* pFirstFoundEntry = NULL;
67             bool bCheckingAmbiguity = false;
68             const void* pStartedWith = pSearchEntry;
69             while ( pSearchEntry )
70             {
71 		        if ( rI18nHelper.MatchMnemonic( sEntryText, _cMnemonic ) )
72                 {
73                     if ( bCheckingAmbiguity )
74                     {
75                         // that's the second (at least) entry with this mnemonic
76                         _rbAmbiguous = true;
77                         return pFirstFoundEntry;
78                     }
79 
80                     pFirstFoundEntry = pSearchEntry;
81                     bCheckingAmbiguity = true;
82                 }
83 
84                 pSearchEntry = _rEntryList.NextSearchEntry( pSearchEntry, sEntryText );
85                 if ( pSearchEntry == pStartedWith )
86                     break;
87             }
88 
89             return pFirstFoundEntry;
90         }
91     }
92 
93 	//====================================================================
94 	//= MnemonicEngine
95 	//====================================================================
96 	//--------------------------------------------------------------------
97     MnemonicEngine::MnemonicEngine( IMnemonicEntryList& _rEntryList )
98         :m_pData( new MnemonicEngine_Data( _rEntryList ) )
99     {
100     }
101 
102 	//--------------------------------------------------------------------
103     bool MnemonicEngine::HandleKeyEvent( const KeyEvent& _rKEvt )
104     {
105 	    sal_Bool bAccelKey = _rKEvt.GetKeyCode().IsMod2();
106         if ( !bAccelKey )
107             return false;
108 
109 	    sal_Unicode cChar = _rKEvt.GetCharCode();
110         bool bAmbiguous = false;
111         const void* pEntry = lcl_getEntryForMnemonic( m_pData->rEntryList, cChar, bAmbiguous );
112         if ( !pEntry )
113             return false;
114 
115         m_pData->rEntryList.SelectSearchEntry( pEntry );
116         if ( !bAmbiguous )
117             m_pData->rEntryList.ExecuteSearchEntry( pEntry );
118 
119         // handled
120         return true;
121     }
122 
123 	//--------------------------------------------------------------------
124     MnemonicEngine::~MnemonicEngine()
125     {
126     }
127 
128 //........................................................................
129 } // namespace vcl
130 //........................................................................
131