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