Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 : : *
5 : : * Copyright 2009 by Sun Microsystems, Inc.
6 : : *
7 : : * OpenOffice.org - a multi-platform office productivity suite
8 : : *
9 : : * This file is part of OpenOffice.org.
10 : : *
11 : : * OpenOffice.org is free software: you can redistribute it and/or modify
12 : : * it under the terms of the GNU Lesser General Public License version 3
13 : : * only, as published by the Free Software Foundation.
14 : : *
15 : : * OpenOffice.org is distributed in the hope that it will be useful,
16 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 : : * GNU Lesser General Public License version 3 for more details
19 : : * (a copy is included in the LICENSE file that accompanied this code).
20 : : *
21 : : * You should have received a copy of the GNU Lesser General Public License
22 : : * version 3 along with OpenOffice.org. If not, see
23 : : * <http://www.openoffice.org/license.html>
24 : : * for a copy of the LGPLv3 License.
25 : : ************************************************************************/
26 : :
27 : :
28 : : #include "vcl/quickselectionengine.hxx"
29 : : #include "vcl/event.hxx"
30 : : #include "vcl/timer.hxx"
31 : : #include "vcl/i18nhelp.hxx"
32 : : #include "vcl/svapp.hxx"
33 : :
34 : : #include <boost/optional.hpp>
35 : :
36 : : //........................................................................
37 : : namespace vcl
38 : : {
39 : : //........................................................................
40 : :
41 : : //====================================================================
42 : : //= QuickSelectionEngine_Data
43 : : //====================================================================
44 : : struct QuickSelectionEngine_Data
45 : : {
46 : : ISearchableStringList& rEntryList;
47 : : String sCurrentSearchString;
48 : : ::boost::optional< sal_Unicode > aSingleSearchChar;
49 : : Timer aSearchTimeout;
50 : :
51 : 3982 : QuickSelectionEngine_Data( ISearchableStringList& _entryList )
52 : : :rEntryList( _entryList )
53 : : ,sCurrentSearchString()
54 : : ,aSingleSearchChar()
55 [ + - ][ + - ]: 3982 : ,aSearchTimeout()
56 : : {
57 [ + - ]: 3982 : aSearchTimeout.SetTimeout( 2500 );
58 [ + - ]: 3982 : aSearchTimeout.SetTimeoutHdl( LINK( this, QuickSelectionEngine_Data, SearchStringTimeout ) );
59 : 3982 : }
60 : :
61 : 3978 : ~QuickSelectionEngine_Data()
62 [ + - ][ + - ]: 3978 : {
63 [ + - ]: 3978 : aSearchTimeout.Stop();
64 : 3978 : }
65 : :
66 : : DECL_LINK( SearchStringTimeout, Timer* );
67 : : };
68 : :
69 : : //--------------------------------------------------------------------
70 : : namespace
71 : : {
72 : 15637 : static void lcl_reset( QuickSelectionEngine_Data& _data )
73 : : {
74 : 15637 : _data.sCurrentSearchString.Erase();
75 : 15637 : _data.aSingleSearchChar.reset();
76 : 15637 : _data.aSearchTimeout.Stop();
77 : 15637 : }
78 : : }
79 : :
80 : : //--------------------------------------------------------------------
81 : 0 : IMPL_LINK( QuickSelectionEngine_Data, SearchStringTimeout, Timer*, /*EMPTYARG*/ )
82 : : {
83 : 0 : lcl_reset( *this );
84 : 0 : return 1;
85 : : }
86 : :
87 : : //--------------------------------------------------------------------
88 : 0 : static StringEntryIdentifier findMatchingEntry( const String& _searchString, QuickSelectionEngine_Data& _engineData )
89 : : {
90 [ # # ][ # # ]: 0 : const vcl::I18nHelper& rI18nHelper = Application::GetSettings().GetLocaleI18nHelper();
91 : : // TODO: do we really need the Window's settings here? The original code used it ...
92 : :
93 [ # # ]: 0 : String sEntryText;
94 : : // get the "current + 1" entry
95 [ # # ]: 0 : StringEntryIdentifier pSearchEntry = _engineData.rEntryList.CurrentEntry( sEntryText );
96 [ # # ]: 0 : if ( pSearchEntry )
97 [ # # ]: 0 : pSearchEntry = _engineData.rEntryList.NextEntry( pSearchEntry, sEntryText );
98 : : // loop 'til we find another matching entry
99 : 0 : StringEntryIdentifier pStartedWith = pSearchEntry;
100 [ # # ]: 0 : while ( pSearchEntry )
101 : : {
102 [ # # ][ # # ]: 0 : if ( rI18nHelper.MatchString( _searchString, sEntryText ) != 0 )
[ # # ][ # # ]
103 : 0 : break;
104 : :
105 [ # # ]: 0 : pSearchEntry = _engineData.rEntryList.NextEntry( pSearchEntry, sEntryText );
106 [ # # ]: 0 : if ( pSearchEntry == pStartedWith )
107 : 0 : pSearchEntry = NULL;
108 : : }
109 : :
110 [ # # ]: 0 : return pSearchEntry;
111 : : }
112 : :
113 : : //====================================================================
114 : : //= QuickSelectionEngine
115 : : //====================================================================
116 : : //--------------------------------------------------------------------
117 : 3982 : QuickSelectionEngine::QuickSelectionEngine( ISearchableStringList& _entryList )
118 [ + - ]: 3982 : :m_pData( new QuickSelectionEngine_Data( _entryList ) )
119 : : {
120 : 3982 : }
121 : :
122 : : //--------------------------------------------------------------------
123 : 3978 : QuickSelectionEngine::~QuickSelectionEngine()
124 : : {
125 : 3978 : }
126 : :
127 : : //--------------------------------------------------------------------
128 : 0 : bool QuickSelectionEngine::HandleKeyEvent( const KeyEvent& _keyEvent )
129 : : {
130 : 0 : xub_Unicode c = _keyEvent.GetCharCode();
131 : :
132 [ # # ][ # # ]: 0 : if ( ( c >= 32 ) && ( c != 127 ) && !_keyEvent.GetKeyCode().IsMod2() )
[ # # ][ # # ]
133 : : {
134 [ # # ]: 0 : m_pData->sCurrentSearchString += c;
135 : : OSL_TRACE( "QuickSelectionEngine::HandleKeyEvent: searching for %s", rtl::OUStringToOString(m_pData->sCurrentSearchString, RTL_TEXTENCODING_UTF8).getStr() );
136 : :
137 [ # # ]: 0 : if ( m_pData->sCurrentSearchString.Len() == 1 )
138 : : { // first character in the search -> remmeber
139 [ # # ]: 0 : m_pData->aSingleSearchChar.reset( c );
140 : : }
141 [ # # ]: 0 : else if ( m_pData->sCurrentSearchString.Len() > 1 )
142 : : {
143 [ # # ][ # # ]: 0 : if ( !!m_pData->aSingleSearchChar && ( *m_pData->aSingleSearchChar != c ) )
[ # # ][ # # ]
[ # # ]
144 : : // we already have a "single char", but the current one is different -> reset
145 [ # # ]: 0 : m_pData->aSingleSearchChar.reset();
146 : : }
147 : :
148 [ # # ]: 0 : XubString aSearchTemp( m_pData->sCurrentSearchString );
149 : :
150 [ # # ]: 0 : StringEntryIdentifier pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
151 : : OSL_TRACE( "QuickSelectionEngine::HandleKeyEvent: found %p", pMatchingEntry );
152 [ # # ][ # # ]: 0 : if ( !pMatchingEntry && ( aSearchTemp.Len() > 1 ) && !!m_pData->aSingleSearchChar )
[ # # ][ # # ]
[ # # ]
153 : : {
154 : : // if there's only one letter in the search string, use a different search mode
155 [ # # ][ # # ]: 0 : aSearchTemp = *m_pData->aSingleSearchChar;
156 [ # # ]: 0 : pMatchingEntry = findMatchingEntry( aSearchTemp, *m_pData );
157 : : }
158 : :
159 [ # # ]: 0 : if ( pMatchingEntry )
160 : : {
161 [ # # ]: 0 : m_pData->rEntryList.SelectEntry( pMatchingEntry );
162 [ # # ]: 0 : m_pData->aSearchTimeout.Start();
163 : : }
164 : : else
165 : : {
166 [ # # ]: 0 : lcl_reset( *m_pData );
167 : : }
168 : :
169 [ # # ]: 0 : return true;
170 : : }
171 : 0 : return false;
172 : : }
173 : :
174 : : //--------------------------------------------------------------------
175 : 15637 : void QuickSelectionEngine::Reset()
176 : : {
177 : 15637 : lcl_reset( *m_pData );
178 : 15637 : }
179 : :
180 : : //........................................................................
181 : : } // namespace vcl
182 : : //........................................................................
183 : :
184 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|