diff options
author | Szymon Kłos <eszkadev@gmail.com> | 2015-08-17 23:01:53 +0200 |
---|---|---|
committer | Szymon Kłos <eszkadev@gmail.com> | 2015-08-29 12:33:08 +0200 |
commit | 5f2f8343927a447b5005ba7f49fb84603b92ea6c (patch) | |
tree | 53c1b94754d1548826a08a27cf8af5a92ee08d93 /svtools/source | |
parent | fb82388b3c3bc7b2e1e4c2b51b9c3165c5ac14da (diff) |
Edit control with autocompletion
Change-Id: Id3aefbffa6b36b475ca78856c9e103cef433f88c
Diffstat (limited to 'svtools/source')
-rw-r--r-- | svtools/source/control/autocmpledit.cxx | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/svtools/source/control/autocmpledit.cxx b/svtools/source/control/autocmpledit.cxx new file mode 100644 index 000000000000..14cf58b62986 --- /dev/null +++ b/svtools/source/control/autocmpledit.cxx @@ -0,0 +1,110 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#include <svtools/autocmpledit.hxx> +#include <vcl/svapp.hxx> + +AutocompleteEdit::AutocompleteEdit( vcl::Window* pParent ) + : Edit( pParent ) + , m_nCurrent( 0 ) +{ + SignalConnectAutocomplete( nullptr, + [this] ( Edit *const pEdit ) { this->AutoCompleteHandler( pEdit ); } ); +} + +void AutocompleteEdit::AddEntry( const OUString& rEntry ) +{ + m_aEntries.push_back( rEntry ); +} + +void AutocompleteEdit::ClearEntries() +{ + m_aEntries.clear(); + m_aMatching.clear(); +} + +void AutocompleteEdit::AutoCompleteHandler( Edit* ) +{ + if( GetAutocompleteAction() != AUTOCOMPLETE_KEYINPUT ) + return; + + if( Application::AnyInput( VclInputFlags::KEYBOARD ) ) + return; + + OUString aCurText = GetText(); + Selection aSelection( GetSelection() ); + + if( aSelection.Max() != aCurText.getLength() ) + return; + + sal_uInt16 nLen = ( sal_uInt16 )aSelection.Min(); + aCurText = aCurText.copy( 0, nLen ); + if( !aCurText.isEmpty() ) + { + if( m_aEntries.size() ) + { + if( Match( aCurText ) ) + { + m_nCurrent = 0; + SetText( m_aMatching[0] ); + sal_uInt16 nNewLen = m_aMatching[0].getLength(); + + Selection aSel( nLen, nNewLen ); + SetSelection( aSel ); + } + } + } +} + +bool AutocompleteEdit::Match( const OUString& rText ) +{ + bool bRet = false; + + m_aMatching.clear(); + + for( std::vector< OUString >::size_type i = 0; i < m_aEntries.size(); ++i ) + { + if( m_aEntries[i].startsWith( rText ) ) + { + m_aMatching.push_back( m_aEntries[i] ); + bRet = true; + } + } + + return bRet; +} + +bool AutocompleteEdit::PreNotify( NotifyEvent& rNEvt ) +{ + if( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT ) + { + const KeyEvent& rEvent = *rNEvt.GetKeyEvent(); + const vcl::KeyCode& rKey = rEvent.GetKeyCode(); + vcl::KeyCode aCode( rKey.GetCode() ); + + if( ( aCode == KEY_UP || aCode == KEY_DOWN ) && !rKey.IsMod2() ) + { + Selection aSelection( GetSelection() ); + sal_uInt16 nLen = ( sal_uInt16 )aSelection.Min(); + + if( m_aMatching.size() && + ( ( aCode == KEY_DOWN && m_nCurrent + 1 < m_aMatching.size() ) + || ( aCode == KEY_UP && m_nCurrent > 0 ) ) ) + { + SetText( m_aMatching[ aCode == KEY_DOWN ? ++m_nCurrent : --m_nCurrent ] ); + SetSelection( Selection( nLen, GetText().getLength() ) ); + return true; + } + } + } + + return Edit::PreNotify( rNEvt ); +} + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |