bump product version to 6.4.0.3
[LibreOffice.git] / vcl / source / control / quickselectionengine.cxx
blobaff324a4eb65c728186e61df24cfb28c1182222c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <vcl/quickselectionengine.hxx>
21 #include <vcl/event.hxx>
22 #include <vcl/timer.hxx>
23 #include <vcl/i18nhelp.hxx>
24 #include <vcl/svapp.hxx>
25 #include <vcl/settings.hxx>
26 #include <sal/log.hxx>
28 #include <boost/optional.hpp>
30 namespace vcl
33 struct QuickSelectionEngine_Data
35 ISearchableStringList& rEntryList;
36 OUString sCurrentSearchString;
37 ::boost::optional< sal_Unicode > aSingleSearchChar;
38 Timer aSearchTimeout;
40 explicit QuickSelectionEngine_Data( ISearchableStringList& _entryList )
41 :rEntryList( _entryList )
42 ,sCurrentSearchString()
43 ,aSingleSearchChar()
44 ,aSearchTimeout()
46 aSearchTimeout.SetTimeout( 2500 );
47 aSearchTimeout.SetInvokeHandler( LINK( this, QuickSelectionEngine_Data, SearchStringTimeout ) );
48 aSearchTimeout.SetDebugName( "vcl::QuickSelectionEngine_Data aSearchTimeout" );
51 ~QuickSelectionEngine_Data()
53 aSearchTimeout.Stop();
56 DECL_LINK( SearchStringTimeout, Timer*, void );
59 namespace
61 void lcl_reset( QuickSelectionEngine_Data& _data )
63 _data.sCurrentSearchString.clear();
64 _data.aSingleSearchChar.reset();
65 _data.aSearchTimeout.Stop();
69 IMPL_LINK_NOARG( QuickSelectionEngine_Data, SearchStringTimeout, Timer*, void )
71 lcl_reset( *this );
74 static StringEntryIdentifier findMatchingEntry( const OUString& _searchString, QuickSelectionEngine_Data const & _engineData )
76 const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetLocaleI18nHelper();
77 // TODO: do we really need the Window's settings here? The original code used it ...
79 OUString sEntryText;
80 // get the "current + 1" entry
81 StringEntryIdentifier pSearchEntry = _engineData.rEntryList.CurrentEntry( sEntryText );
82 if ( pSearchEntry )
83 pSearchEntry = _engineData.rEntryList.NextEntry( pSearchEntry, sEntryText );
84 // loop 'til we find another matching entry
85 StringEntryIdentifier pStartedWith = pSearchEntry;
86 while ( pSearchEntry )
88 if ( rI18nHelper.MatchString( _searchString, sEntryText ) )
89 break;
91 pSearchEntry = _engineData.rEntryList.NextEntry( pSearchEntry, sEntryText );
92 if ( pSearchEntry == pStartedWith )
93 pSearchEntry = nullptr;
96 return pSearchEntry;
99 QuickSelectionEngine::QuickSelectionEngine( ISearchableStringList& _entryList )
100 :m_pData( new QuickSelectionEngine_Data( _entryList ) ),
101 bEnabled( true )
105 QuickSelectionEngine::~QuickSelectionEngine()
109 void QuickSelectionEngine::SetEnabled( bool b )
111 bEnabled = b;
114 bool QuickSelectionEngine::HandleKeyEvent( const KeyEvent& _keyEvent )
116 if( bEnabled )
118 sal_Unicode c = _keyEvent.GetCharCode();
120 if ( ( c >= 32 ) && ( c != 127 ) && !_keyEvent.GetKeyCode().IsMod2() )
122 m_pData->sCurrentSearchString += OUStringChar(c);
123 SAL_INFO( "vcl", "QuickSelectionEngine::HandleKeyEvent: searching for " << m_pData->sCurrentSearchString );
125 if ( m_pData->sCurrentSearchString.getLength() == 1 )
126 { // first character in the search -> remember
127 m_pData->aSingleSearchChar = c;
129 else if ( m_pData->sCurrentSearchString.getLength() > 1 )
131 if ( !!m_pData->aSingleSearchChar && ( *m_pData->aSingleSearchChar != c ) )
132 // we already have a "single char", but the current one is different -> reset
133 m_pData->aSingleSearchChar.reset();
136 OUString aSearchTemp( m_pData->sCurrentSearchString );
138 StringEntryIdentifier pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
139 SAL_INFO( "vcl", "QuickSelectionEngine::HandleKeyEvent: found " << pMatchingEntry );
140 if ( !pMatchingEntry && (aSearchTemp.getLength() > 1) && !!m_pData->aSingleSearchChar )
142 // if there's only one letter in the search string, use a different search mode
143 aSearchTemp = OUString(*m_pData->aSingleSearchChar);
144 pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
147 if ( pMatchingEntry )
149 m_pData->rEntryList.SelectEntry( pMatchingEntry );
150 m_pData->aSearchTimeout.Start();
152 else
154 lcl_reset( *m_pData );
157 return true;
159 return false;
161 else
163 return false;
167 void QuickSelectionEngine::Reset()
169 if( bEnabled )
170 lcl_reset( *m_pData );
173 } // namespace vcl
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */