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 :
21 : #include "vcl/quickselectionengine.hxx"
22 : #include "vcl/event.hxx"
23 : #include "vcl/timer.hxx"
24 : #include "vcl/i18nhelp.hxx"
25 : #include "vcl/svapp.hxx"
26 :
27 : #include <boost/optional.hpp>
28 :
29 : namespace vcl
30 : {
31 :
32 : struct QuickSelectionEngine_Data
33 : {
34 : ISearchableStringList& rEntryList;
35 : String sCurrentSearchString;
36 : ::boost::optional< sal_Unicode > aSingleSearchChar;
37 : Timer aSearchTimeout;
38 :
39 2313 : QuickSelectionEngine_Data( ISearchableStringList& _entryList )
40 : :rEntryList( _entryList )
41 : ,sCurrentSearchString()
42 : ,aSingleSearchChar()
43 2313 : ,aSearchTimeout()
44 : {
45 2313 : aSearchTimeout.SetTimeout( 2500 );
46 2313 : aSearchTimeout.SetTimeoutHdl( LINK( this, QuickSelectionEngine_Data, SearchStringTimeout ) );
47 2313 : }
48 :
49 2311 : ~QuickSelectionEngine_Data()
50 2311 : {
51 2311 : aSearchTimeout.Stop();
52 2311 : }
53 :
54 : DECL_LINK( SearchStringTimeout, Timer* );
55 : };
56 :
57 : namespace
58 : {
59 8336 : static void lcl_reset( QuickSelectionEngine_Data& _data )
60 : {
61 8336 : _data.sCurrentSearchString.Erase();
62 8336 : _data.aSingleSearchChar.reset();
63 8336 : _data.aSearchTimeout.Stop();
64 8336 : }
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 : String 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 ) != 0 )
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 2313 : QuickSelectionEngine::QuickSelectionEngine( ISearchableStringList& _entryList )
99 2313 : :m_pData( new QuickSelectionEngine_Data( _entryList ) )
100 : {
101 2313 : }
102 :
103 2311 : QuickSelectionEngine::~QuickSelectionEngine()
104 : {
105 2311 : }
106 :
107 0 : bool QuickSelectionEngine::HandleKeyEvent( const KeyEvent& _keyEvent )
108 : {
109 0 : sal_Unicode c = _keyEvent.GetCharCode();
110 :
111 0 : if ( ( c >= 32 ) && ( c != 127 ) && !_keyEvent.GetKeyCode().IsMod2() )
112 : {
113 0 : m_pData->sCurrentSearchString += c;
114 : OSL_TRACE( "QuickSelectionEngine::HandleKeyEvent: searching for %s", OUStringToOString(m_pData->sCurrentSearchString, RTL_TEXTENCODING_UTF8).getStr() );
115 :
116 0 : if ( m_pData->sCurrentSearchString.Len() == 1 )
117 : { // first character in the search -> remmeber
118 0 : m_pData->aSingleSearchChar.reset( c );
119 : }
120 0 : else if ( m_pData->sCurrentSearchString.Len() > 1 )
121 : {
122 0 : if ( !!m_pData->aSingleSearchChar && ( *m_pData->aSingleSearchChar != c ) )
123 : // we already have a "single char", but the current one is different -> reset
124 0 : m_pData->aSingleSearchChar.reset();
125 : }
126 :
127 0 : OUString aSearchTemp( m_pData->sCurrentSearchString );
128 :
129 0 : StringEntryIdentifier pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
130 : OSL_TRACE( "QuickSelectionEngine::HandleKeyEvent: found %p", pMatchingEntry );
131 0 : if ( !pMatchingEntry && (aSearchTemp.getLength() > 1) && !!m_pData->aSingleSearchChar )
132 : {
133 : // if there's only one letter in the search string, use a different search mode
134 0 : aSearchTemp = OUString(*m_pData->aSingleSearchChar);
135 0 : pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
136 : }
137 :
138 0 : if ( pMatchingEntry )
139 : {
140 0 : m_pData->rEntryList.SelectEntry( pMatchingEntry );
141 0 : m_pData->aSearchTimeout.Start();
142 : }
143 : else
144 : {
145 0 : lcl_reset( *m_pData );
146 : }
147 :
148 0 : return true;
149 : }
150 0 : return false;
151 : }
152 :
153 8336 : void QuickSelectionEngine::Reset()
154 : {
155 8336 : lcl_reset( *m_pData );
156 8336 : }
157 :
158 465 : } // namespace vcl
159 :
160 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|