1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
33 struct QuickSelectionEngine_Data
35 ISearchableStringList
& rEntryList
;
36 OUString sCurrentSearchString
;
37 ::std::optional
< sal_Unicode
> aSingleSearchChar
;
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 );
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 )
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 ...
77 // get the "current + 1" entry
78 StringEntryIdentifier pSearchEntry
= _engineData
.rEntryList
.CurrentEntry( sEntryText
);
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
) )
88 pSearchEntry
= _engineData
.rEntryList
.NextEntry( pSearchEntry
, sEntryText
);
89 if ( pSearchEntry
== pStartedWith
)
90 pSearchEntry
= nullptr;
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();
143 lcl_reset( *m_pData
);
151 void QuickSelectionEngine::Reset()
153 lcl_reset( *m_pData
);
158 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */