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/.
10 #include <svtools/autocmpledit.hxx>
11 #include <vcl/svapp.hxx>
13 AutocompleteEdit::AutocompleteEdit( vcl::Window
* pParent
)
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()
33 void AutocompleteEdit::AutoCompleteHandler( Edit
* )
35 if( GetAutocompleteAction() != AUTOCOMPLETE_KEYINPUT
)
38 if( Application::AnyInput( VclInputFlags::KEYBOARD
) )
41 OUString aCurText
= GetText();
42 Selection
aSelection( GetSelection() );
44 if( aSelection
.Max() != aCurText
.getLength() )
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
) )
56 SetText( m_aMatching
[0] );
57 sal_uInt16 nNewLen
= m_aMatching
[0].getLength();
59 Selection
aSel( nLen
, nNewLen
);
66 bool AutocompleteEdit::Match( const OUString
& rText
)
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
] );
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() ) );
108 return Edit::PreNotify( rNEvt
);
111 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */