Bump version to 5.0-14
[LibreOffice.git] / svtools / source / control / autocmpledit.cxx
blob70df7408cd3d727b6e729464d067eb72541eadb4
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/.
8 */
10 #include <svtools/autocmpledit.hxx>
11 #include <vcl/svapp.hxx>
13 AutocompleteEdit::AutocompleteEdit( vcl::Window* pParent )
14 : Edit( pParent )
15 , m_nCurrent( 0 )
17 /* SignalConnectAutocomplete( nullptr,
18 [this] ( Edit *const pEdit ) { this->AutoCompleteHandler( pEdit ); } ); */
19 autocompleteSignal.connect( [this] ( Edit *const pEdit ) { this->AutoCompleteHandler( pEdit ); });
22 void AutocompleteEdit::AddEntry( const OUString& rEntry )
24 m_aEntries.push_back( rEntry );
27 void AutocompleteEdit::ClearEntries()
29 m_aEntries.clear();
30 m_aMatching.clear();
33 void AutocompleteEdit::AutoCompleteHandler( Edit* )
35 if( GetAutocompleteAction() != AUTOCOMPLETE_KEYINPUT )
36 return;
38 if( Application::AnyInput( VclInputFlags::KEYBOARD ) )
39 return;
41 OUString aCurText = GetText();
42 Selection aSelection( GetSelection() );
44 if( aSelection.Max() != aCurText.getLength() )
45 return;
47 sal_uInt16 nLen = ( sal_uInt16 )aSelection.Min();
48 aCurText = aCurText.copy( 0, nLen );
49 if( !aCurText.isEmpty() )
51 if( m_aEntries.size() )
53 if( Match( aCurText ) )
55 m_nCurrent = 0;
56 SetText( m_aMatching[0] );
57 sal_uInt16 nNewLen = m_aMatching[0].getLength();
59 Selection aSel( nLen, nNewLen );
60 SetSelection( aSel );
66 bool AutocompleteEdit::Match( const OUString& rText )
68 bool bRet = false;
70 m_aMatching.clear();
72 for( std::vector< OUString >::size_type i = 0; i < m_aEntries.size(); ++i )
74 if( m_aEntries[i].startsWithIgnoreAsciiCase( rText ) )
76 m_aMatching.push_back( m_aEntries[i] );
77 bRet = true;
81 return bRet;
84 bool AutocompleteEdit::PreNotify( NotifyEvent& rNEvt )
86 if( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
88 const KeyEvent& rEvent = *rNEvt.GetKeyEvent();
89 const vcl::KeyCode& rKey = rEvent.GetKeyCode();
90 vcl::KeyCode aCode( rKey.GetCode() );
92 if( ( aCode == KEY_UP || aCode == KEY_DOWN ) && !rKey.IsMod2() )
94 Selection aSelection( GetSelection() );
95 sal_uInt16 nLen = ( sal_uInt16 )aSelection.Min();
97 if( m_aMatching.size() &&
98 ( ( aCode == KEY_DOWN && m_nCurrent + 1 < m_aMatching.size() )
99 || ( aCode == KEY_UP && m_nCurrent > 0 ) ) )
101 SetText( m_aMatching[ aCode == KEY_DOWN ? ++m_nCurrent : --m_nCurrent ] );
102 SetSelection( Selection( nLen, GetText().getLength() ) );
103 return true;
108 return Edit::PreNotify( rNEvt );
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */