Update git submodules
[LibreOffice.git] / vcl / source / control / quickselectionengine.cxx
blob777e00e0bc73dcd5aa47d72e8fed733f47eb7d58
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 <optional>
30 namespace vcl
33 struct QuickSelectionEngine_Data
35 ISearchableStringList& rEntryList;
36 OUString sCurrentSearchString;
37 ::std::optional< sal_Unicode > aSingleSearchChar;
38 Timer aSearchTimeout;
40 explicit QuickSelectionEngine_Data( ISearchableStringList& _entryList )
41 :rEntryList( _entryList )
42 ,aSearchTimeout( "vcl::QuickSelectionEngine_Data aSearchTimeout" )
44 aSearchTimeout.SetTimeout( 2500 );
45 aSearchTimeout.SetInvokeHandler( LINK( this, QuickSelectionEngine_Data, SearchStringTimeout ) );
48 ~QuickSelectionEngine_Data()
50 aSearchTimeout.Stop();
53 DECL_LINK( SearchStringTimeout, Timer*, void );
56 namespace
58 void lcl_reset( QuickSelectionEngine_Data& _data )
60 _data.sCurrentSearchString.clear();
61 _data.aSingleSearchChar.reset();
62 _data.aSearchTimeout.Stop();
66 IMPL_LINK_NOARG( QuickSelectionEngine_Data, SearchStringTimeout, Timer*, void )
68 lcl_reset( *this );
71 static StringEntryIdentifier findMatchingEntry( const OUString& _searchString, QuickSelectionEngine_Data const & _engineData )
73 const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetLocaleI18nHelper();
74 // TODO: do we really need the Window's settings here? The original code used it ...
76 OUString sEntryText;
77 // get the "current + 1" entry
78 StringEntryIdentifier pSearchEntry = _engineData.rEntryList.CurrentEntry( sEntryText );
79 if ( pSearchEntry )
80 pSearchEntry = _engineData.rEntryList.NextEntry( pSearchEntry, sEntryText );
81 // loop 'til we find another matching entry
82 StringEntryIdentifier pStartedWith = pSearchEntry;
83 while ( pSearchEntry )
85 if ( rI18nHelper.MatchString( _searchString, sEntryText ) )
86 break;
88 pSearchEntry = _engineData.rEntryList.NextEntry( pSearchEntry, sEntryText );
89 if ( pSearchEntry == pStartedWith )
90 pSearchEntry = nullptr;
93 return pSearchEntry;
96 QuickSelectionEngine::QuickSelectionEngine( ISearchableStringList& _entryList )
97 :m_pData( new QuickSelectionEngine_Data( _entryList ) )
101 QuickSelectionEngine::~QuickSelectionEngine()
105 bool QuickSelectionEngine::HandleKeyEvent( const KeyEvent& _keyEvent )
107 sal_Unicode c = _keyEvent.GetCharCode();
109 if ( ( c >= 32 ) && ( c != 127 ) && !_keyEvent.GetKeyCode().IsMod2() )
111 m_pData->sCurrentSearchString += OUStringChar(c);
112 SAL_INFO( "vcl", "QuickSelectionEngine::HandleKeyEvent: searching for " << m_pData->sCurrentSearchString );
114 if ( m_pData->sCurrentSearchString.getLength() == 1 )
115 { // first character in the search -> remember
116 m_pData->aSingleSearchChar = c;
118 else if ( m_pData->sCurrentSearchString.getLength() > 1 )
120 if ( !!m_pData->aSingleSearchChar && ( *m_pData->aSingleSearchChar != c ) )
121 // we already have a "single char", but the current one is different -> reset
122 m_pData->aSingleSearchChar.reset();
125 OUString aSearchTemp( m_pData->sCurrentSearchString );
127 StringEntryIdentifier pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
128 SAL_INFO( "vcl", "QuickSelectionEngine::HandleKeyEvent: found " << pMatchingEntry );
129 if ( !pMatchingEntry && (aSearchTemp.getLength() > 1) && !!m_pData->aSingleSearchChar )
131 // if there's only one letter in the search string, use a different search mode
132 aSearchTemp = OUString(*m_pData->aSingleSearchChar);
133 pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
136 if ( pMatchingEntry )
138 m_pData->rEntryList.SelectEntry( pMatchingEntry );
139 m_pData->aSearchTimeout.Start();
141 else
143 lcl_reset( *m_pData );
146 return true;
148 return false;
151 void QuickSelectionEngine::Reset()
153 lcl_reset( *m_pData );
156 } // namespace vcl
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */