build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / control / quickselectionengine.cxx
blob1a0b6a56ec9b712c714a94b1beed4b5b3b36952b
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>
27 #include <boost/optional.hpp>
29 namespace vcl
32 struct QuickSelectionEngine_Data
34 ISearchableStringList& rEntryList;
35 OUString sCurrentSearchString;
36 ::boost::optional< sal_Unicode > aSingleSearchChar;
37 Timer aSearchTimeout;
39 explicit QuickSelectionEngine_Data( ISearchableStringList& _entryList )
40 :rEntryList( _entryList )
41 ,sCurrentSearchString()
42 ,aSingleSearchChar()
43 ,aSearchTimeout()
45 aSearchTimeout.SetTimeout( 2500 );
46 aSearchTimeout.SetTimeoutHdl( LINK( this, QuickSelectionEngine_Data, SearchStringTimeout ) );
47 aSearchTimeout.SetDebugName( "vcl::QuickSelectionEngine_Data aSearchTimeout" );
50 ~QuickSelectionEngine_Data()
52 aSearchTimeout.Stop();
55 DECL_LINK( SearchStringTimeout, Timer*, void );
58 namespace
60 void lcl_reset( QuickSelectionEngine_Data& _data )
62 _data.sCurrentSearchString.clear();
63 _data.aSingleSearchChar.reset();
64 _data.aSearchTimeout.Stop();
68 IMPL_LINK_NOARG( QuickSelectionEngine_Data, SearchStringTimeout, Timer*, void )
70 lcl_reset( *this );
73 static StringEntryIdentifier findMatchingEntry( const OUString& _searchString, QuickSelectionEngine_Data& _engineData )
75 const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetLocaleI18nHelper();
76 // TODO: do we really need the Window's settings here? The original code used it ...
78 OUString sEntryText;
79 // get the "current + 1" entry
80 StringEntryIdentifier pSearchEntry = _engineData.rEntryList.CurrentEntry( sEntryText );
81 if ( pSearchEntry )
82 pSearchEntry = _engineData.rEntryList.NextEntry( pSearchEntry, sEntryText );
83 // loop 'til we find another matching entry
84 StringEntryIdentifier pStartedWith = pSearchEntry;
85 while ( pSearchEntry )
87 if ( rI18nHelper.MatchString( _searchString, sEntryText ) )
88 break;
90 pSearchEntry = _engineData.rEntryList.NextEntry( pSearchEntry, sEntryText );
91 if ( pSearchEntry == pStartedWith )
92 pSearchEntry = nullptr;
95 return pSearchEntry;
98 QuickSelectionEngine::QuickSelectionEngine( ISearchableStringList& _entryList )
99 :m_pData( new QuickSelectionEngine_Data( _entryList ) ),
100 bEnabled( true )
104 QuickSelectionEngine::~QuickSelectionEngine()
108 void QuickSelectionEngine::SetEnabled( bool b )
110 bEnabled = b;
113 bool QuickSelectionEngine::HandleKeyEvent( const KeyEvent& _keyEvent )
115 if( bEnabled )
117 sal_Unicode c = _keyEvent.GetCharCode();
119 if ( ( c >= 32 ) && ( c != 127 ) && !_keyEvent.GetKeyCode().IsMod2() )
121 m_pData->sCurrentSearchString += OUStringLiteral1(c);
122 OSL_TRACE( "QuickSelectionEngine::HandleKeyEvent: searching for %s", OUStringToOString(m_pData->sCurrentSearchString, RTL_TEXTENCODING_UTF8).getStr() );
124 if ( m_pData->sCurrentSearchString.getLength() == 1 )
125 { // first character in the search -> remember
126 m_pData->aSingleSearchChar.reset( c );
128 else if ( m_pData->sCurrentSearchString.getLength() > 1 )
130 if ( !!m_pData->aSingleSearchChar && ( *m_pData->aSingleSearchChar != c ) )
131 // we already have a "single char", but the current one is different -> reset
132 m_pData->aSingleSearchChar.reset();
135 OUString aSearchTemp( m_pData->sCurrentSearchString );
137 StringEntryIdentifier pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
138 OSL_TRACE( "QuickSelectionEngine::HandleKeyEvent: found %p", pMatchingEntry );
139 if ( !pMatchingEntry && (aSearchTemp.getLength() > 1) && !!m_pData->aSingleSearchChar )
141 // if there's only one letter in the search string, use a different search mode
142 aSearchTemp = OUString(*m_pData->aSingleSearchChar);
143 pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
146 if ( pMatchingEntry )
148 m_pData->rEntryList.SelectEntry( pMatchingEntry );
149 m_pData->aSearchTimeout.Start();
151 else
153 lcl_reset( *m_pData );
156 return true;
158 return false;
160 else
162 return false;
166 void QuickSelectionEngine::Reset()
168 if( bEnabled )
169 lcl_reset( *m_pData );
172 } // namespace vcl
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */