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