Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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 :
27 : #include <boost/optional.hpp>
28 :
29 : namespace vcl
30 : {
31 :
32 : struct QuickSelectionEngine_Data
33 : {
34 : ISearchableStringList& rEntryList;
35 : OUString sCurrentSearchString;
36 : ::boost::optional< sal_Unicode > aSingleSearchChar;
37 : Timer aSearchTimeout;
38 :
39 3340 : explicit QuickSelectionEngine_Data( ISearchableStringList& _entryList )
40 : :rEntryList( _entryList )
41 : ,sCurrentSearchString()
42 : ,aSingleSearchChar()
43 3340 : ,aSearchTimeout()
44 : {
45 3340 : aSearchTimeout.SetTimeout( 2500 );
46 3340 : aSearchTimeout.SetTimeoutHdl( LINK( this, QuickSelectionEngine_Data, SearchStringTimeout ) );
47 3340 : }
48 :
49 3336 : ~QuickSelectionEngine_Data()
50 3336 : {
51 3336 : aSearchTimeout.Stop();
52 3336 : }
53 :
54 : DECL_LINK_TYPED( SearchStringTimeout, Timer*, void );
55 : };
56 :
57 : namespace
58 : {
59 9652 : static void lcl_reset( QuickSelectionEngine_Data& _data )
60 : {
61 9652 : _data.sCurrentSearchString.clear();
62 9652 : _data.aSingleSearchChar.reset();
63 9652 : _data.aSearchTimeout.Stop();
64 9652 : }
65 : }
66 :
67 0 : IMPL_LINK_NOARG_TYPED( QuickSelectionEngine_Data, SearchStringTimeout, Timer*, void )
68 : {
69 0 : lcl_reset( *this );
70 0 : }
71 :
72 0 : static StringEntryIdentifier findMatchingEntry( const OUString& _searchString, QuickSelectionEngine_Data& _engineData )
73 : {
74 0 : const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetLocaleI18nHelper();
75 : // TODO: do we really need the Window's settings here? The original code used it ...
76 :
77 0 : OUString sEntryText;
78 : // get the "current + 1" entry
79 0 : StringEntryIdentifier pSearchEntry = _engineData.rEntryList.CurrentEntry( sEntryText );
80 0 : if ( pSearchEntry )
81 0 : pSearchEntry = _engineData.rEntryList.NextEntry( pSearchEntry, sEntryText );
82 : // loop 'til we find another matching entry
83 0 : StringEntryIdentifier pStartedWith = pSearchEntry;
84 0 : while ( pSearchEntry )
85 : {
86 0 : if ( rI18nHelper.MatchString( _searchString, sEntryText ) )
87 0 : break;
88 :
89 0 : pSearchEntry = _engineData.rEntryList.NextEntry( pSearchEntry, sEntryText );
90 0 : if ( pSearchEntry == pStartedWith )
91 0 : pSearchEntry = NULL;
92 : }
93 :
94 0 : return pSearchEntry;
95 : }
96 :
97 3340 : QuickSelectionEngine::QuickSelectionEngine( ISearchableStringList& _entryList )
98 3340 : :m_pData( new QuickSelectionEngine_Data( _entryList ) ),
99 6680 : bEnabled( true )
100 : {
101 3340 : }
102 :
103 3336 : QuickSelectionEngine::~QuickSelectionEngine()
104 : {
105 3336 : }
106 :
107 0 : void QuickSelectionEngine::SetEnabled( const bool& b )
108 : {
109 0 : bEnabled = b;
110 0 : }
111 :
112 0 : bool QuickSelectionEngine::HandleKeyEvent( const KeyEvent& _keyEvent )
113 : {
114 0 : if( bEnabled )
115 : {
116 0 : sal_Unicode c = _keyEvent.GetCharCode();
117 :
118 0 : if ( ( c >= 32 ) && ( c != 127 ) && !_keyEvent.GetKeyCode().IsMod2() )
119 : {
120 0 : m_pData->sCurrentSearchString += OUString(c);
121 : OSL_TRACE( "QuickSelectionEngine::HandleKeyEvent: searching for %s", OUStringToOString(m_pData->sCurrentSearchString, RTL_TEXTENCODING_UTF8).getStr() );
122 :
123 0 : if ( m_pData->sCurrentSearchString.getLength() == 1 )
124 : { // first character in the search -> remember
125 0 : m_pData->aSingleSearchChar.reset( c );
126 : }
127 0 : else if ( m_pData->sCurrentSearchString.getLength() > 1 )
128 : {
129 0 : if ( !!m_pData->aSingleSearchChar && ( *m_pData->aSingleSearchChar != c ) )
130 : // we already have a "single char", but the current one is different -> reset
131 0 : m_pData->aSingleSearchChar.reset();
132 : }
133 :
134 0 : OUString aSearchTemp( m_pData->sCurrentSearchString );
135 :
136 0 : StringEntryIdentifier pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
137 : OSL_TRACE( "QuickSelectionEngine::HandleKeyEvent: found %p", pMatchingEntry );
138 0 : if ( !pMatchingEntry && (aSearchTemp.getLength() > 1) && !!m_pData->aSingleSearchChar )
139 : {
140 : // if there's only one letter in the search string, use a different search mode
141 0 : aSearchTemp = OUString(*m_pData->aSingleSearchChar);
142 0 : pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
143 : }
144 :
145 0 : if ( pMatchingEntry )
146 : {
147 0 : m_pData->rEntryList.SelectEntry( pMatchingEntry );
148 0 : m_pData->aSearchTimeout.Start();
149 : }
150 : else
151 : {
152 0 : lcl_reset( *m_pData );
153 : }
154 :
155 0 : return true;
156 : }
157 0 : return false;
158 : }
159 : else
160 : {
161 0 : return false;
162 : }
163 : }
164 :
165 9652 : void QuickSelectionEngine::Reset()
166 : {
167 9652 : if( bEnabled )
168 9652 : lcl_reset( *m_pData );
169 9652 : }
170 :
171 : } // namespace vcl
172 :
173 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|