bump product version to 4.1.6.2
[LibreOffice.git] / vcl / source / control / quickselectionengine.cxx
blob2f379661dd7946095a343b6600d5763487d9a0fa
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 .
21 #include "vcl/quickselectionengine.hxx"
22 #include "vcl/event.hxx"
23 #include "vcl/timer.hxx"
24 #include "vcl/i18nhelp.hxx"
25 #include "vcl/svapp.hxx"
27 #include <boost/optional.hpp>
29 namespace vcl
32 struct QuickSelectionEngine_Data
34 ISearchableStringList& rEntryList;
35 String sCurrentSearchString;
36 ::boost::optional< sal_Unicode > aSingleSearchChar;
37 Timer aSearchTimeout;
39 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 ) );
49 ~QuickSelectionEngine_Data()
51 aSearchTimeout.Stop();
54 DECL_LINK( SearchStringTimeout, Timer* );
57 namespace
59 static void lcl_reset( QuickSelectionEngine_Data& _data )
61 _data.sCurrentSearchString.Erase();
62 _data.aSingleSearchChar.reset();
63 _data.aSearchTimeout.Stop();
67 IMPL_LINK( QuickSelectionEngine_Data, SearchStringTimeout, Timer*, /*EMPTYARG*/ )
69 lcl_reset( *this );
70 return 1;
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 String 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 ) != 0 )
88 break;
90 pSearchEntry = _engineData.rEntryList.NextEntry( pSearchEntry, sEntryText );
91 if ( pSearchEntry == pStartedWith )
92 pSearchEntry = NULL;
95 return pSearchEntry;
98 QuickSelectionEngine::QuickSelectionEngine( ISearchableStringList& _entryList )
99 :m_pData( new QuickSelectionEngine_Data( _entryList ) )
103 QuickSelectionEngine::~QuickSelectionEngine()
107 bool QuickSelectionEngine::HandleKeyEvent( const KeyEvent& _keyEvent )
109 sal_Unicode c = _keyEvent.GetCharCode();
111 if ( ( c >= 32 ) && ( c != 127 ) && !_keyEvent.GetKeyCode().IsMod2() )
113 m_pData->sCurrentSearchString += c;
114 OSL_TRACE( "QuickSelectionEngine::HandleKeyEvent: searching for %s", OUStringToOString(m_pData->sCurrentSearchString, RTL_TEXTENCODING_UTF8).getStr() );
116 if ( m_pData->sCurrentSearchString.Len() == 1 )
117 { // first character in the search -> remmeber
118 m_pData->aSingleSearchChar.reset( c );
120 else if ( m_pData->sCurrentSearchString.Len() > 1 )
122 if ( !!m_pData->aSingleSearchChar && ( *m_pData->aSingleSearchChar != c ) )
123 // we already have a "single char", but the current one is different -> reset
124 m_pData->aSingleSearchChar.reset();
127 OUString aSearchTemp( m_pData->sCurrentSearchString );
129 StringEntryIdentifier pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
130 OSL_TRACE( "QuickSelectionEngine::HandleKeyEvent: found %p", pMatchingEntry );
131 if ( !pMatchingEntry && (aSearchTemp.getLength() > 1) && !!m_pData->aSingleSearchChar )
133 // if there's only one letter in the search string, use a different search mode
134 aSearchTemp = OUString(*m_pData->aSingleSearchChar);
135 pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
138 if ( pMatchingEntry )
140 m_pData->rEntryList.SelectEntry( pMatchingEntry );
141 m_pData->aSearchTimeout.Start();
143 else
145 lcl_reset( *m_pData );
148 return true;
150 return false;
153 void QuickSelectionEngine::Reset()
155 lcl_reset( *m_pData );
158 } // namespace vcl
160 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */