1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #include "tbunosearchcontrollers.hxx"
28 #include <svx/dialogs.hrc>
29 #include <svx/dialmgr.hxx>
30 
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/frame/XLayoutManager.hpp>
33 #include <com/sun/star/ui/XUIElement.hpp>
34 #include <com/sun/star/util/URL.hpp>
35 
36 #include <toolkit/helper/vclunohelper.hxx>
37 #include <vcl/toolbox.hxx>
38 #include <vcl/svapp.hxx>
39 #include <vos/mutex.hxx>
40 
41 namespace svx
42 {
43 
44 static const ::rtl::OUString SEARCHITEM_SEARCHSTRING   = ::rtl::OUString::createFromAscii("SearchItem.SearchString");
45 static const ::rtl::OUString SEARCHITEM_SEARCHBACKWARD = ::rtl::OUString::createFromAscii("SearchItem.Backward");
46 
47 static const ::rtl::OUString COMMAND_EXECUTESEARCH = ::rtl::OUString::createFromAscii(".uno:ExecuteSearch");
48 static const ::rtl::OUString COMMAND_FINDTEXT   = ::rtl::OUString::createFromAscii(".uno:FindText");
49 static const ::rtl::OUString COMMAND_DOWNSEARCH = ::rtl::OUString::createFromAscii(".uno:DownSearch");
50 static const ::rtl::OUString COMMAND_UPSEARCH   = ::rtl::OUString::createFromAscii(".uno:UpSearch");
51 static const ::rtl::OUString COMMAND_APPENDSEARCHHISTORY   = ::rtl::OUString::createFromAscii("AppendSearchHistory");
52 
53 static const ::rtl::OUString SERVICENAME_URLTRANSFORMER = ::rtl::OUString::createFromAscii("com.sun.star.util.URLTransformer");
54 static const sal_Int32       REMEMBER_SIZE = 10;
55 
impl_executeSearch(const css::uno::Reference<css::lang::XMultiServiceFactory> & rSMgr,const css::uno::Reference<css::frame::XFrame> & xFrame,const css::uno::Sequence<css::beans::PropertyValue> & lArgs)56 void impl_executeSearch( const css::uno::Reference< css::lang::XMultiServiceFactory >& rSMgr, const css::uno::Reference< css::frame::XFrame >& xFrame, const css::uno::Sequence< css::beans::PropertyValue >& lArgs )
57 {
58 	css::uno::Reference< css::util::XURLTransformer > xURLTransformer( rSMgr->createInstance(SERVICENAME_URLTRANSFORMER), css::uno::UNO_QUERY );
59 	if ( xURLTransformer.is() )
60 	{
61 		css::util::URL aURL;
62 		aURL.Complete = COMMAND_EXECUTESEARCH;
63 		xURLTransformer->parseStrict(aURL);
64 
65 		css::uno::Reference< css::frame::XDispatchProvider > xDispatchProvider(xFrame, css::uno::UNO_QUERY);
66 		if ( xDispatchProvider.is() )
67 		{
68 			css::uno::Reference< css::frame::XDispatch > xDispatch = xDispatchProvider->queryDispatch( aURL, ::rtl::OUString(), 0 );
69 			if ( xDispatch.is() && aURL.Complete.getLength() > 0 )
70 				xDispatch->dispatch( aURL, lArgs );
71 		}
72 	}
73 }
74 
FindTextFieldControl(Window * pParent,WinBits nStyle,css::uno::Reference<css::frame::XFrame> & xFrame,css::uno::Reference<css::lang::XMultiServiceFactory> & xServiceManager)75 FindTextFieldControl::FindTextFieldControl( Window* pParent, WinBits nStyle,
76 	css::uno::Reference< css::frame::XFrame >& xFrame,
77 	css::uno::Reference< css::lang::XMultiServiceFactory >& xServiceManager) :
78 	ComboBox( pParent, nStyle ),
79 	m_xFrame(xFrame),
80 	m_xServiceManager(xServiceManager),
81 	m_bToClearTextField(sal_True)
82 {
83 	InitControls_Impl();
84 }
85 
~FindTextFieldControl()86 FindTextFieldControl::~FindTextFieldControl()
87 {
88 }
89 
InitControls_Impl()90 void FindTextFieldControl::InitControls_Impl()
91 {
92 	SetText( SVX_RESSTR( RID_SVXSTR_FINDBAR_FIND ) );
93 	SetControlForeground(GetSettings().GetStyleSettings().GetDisableColor());
94 
95 	EnableAutocomplete(sal_True, sal_True);
96 }
97 
Remember_Impl(const String & rStr)98 void FindTextFieldControl::Remember_Impl(const String& rStr)
99 {
100 	sal_uInt16 nCount = GetEntryCount();
101 
102 	for (sal_uInt16 i=0; i<nCount; ++i)
103 	{
104 		if ( rStr == GetEntry(i))
105 			return;
106 	}
107 
108 	if (nCount == REMEMBER_SIZE)
109 		RemoveEntry(REMEMBER_SIZE-1);
110 
111 	InsertEntry(rStr, 0);
112 }
113 
Modify()114 void FindTextFieldControl::Modify()
115 {
116 	ComboBox::Modify();
117 
118 	SetControlForeground( GetSettings().GetStyleSettings().GetWindowTextColor() );
119 }
120 
PreNotify(NotifyEvent & rNEvt)121 long FindTextFieldControl::PreNotify( NotifyEvent& rNEvt )
122 {
123 	long nRet= ComboBox::PreNotify( rNEvt );
124 
125 	switch ( rNEvt.GetType() )
126 	{
127 		case EVENT_KEYINPUT:
128 		{
129 			const KeyEvent* pKeyEvent = rNEvt.GetKeyEvent();
130 			sal_Bool bCtrl = pKeyEvent->GetKeyCode().IsMod1();
131 			sal_Bool bAlt = pKeyEvent->GetKeyCode().IsMod2();
132 			sal_Bool bShift = pKeyEvent->GetKeyCode().IsShift();
133 			sal_uInt16 nCode = pKeyEvent->GetKeyCode().GetCode();
134 
135 			if ( (bCtrl && bAlt && KEY_F == nCode) || KEY_ESCAPE == nCode )
136 			{
137 				nRet = 1;
138 				GrabFocusToDocument();
139 			}
140 
141 			if ( KEY_RETURN == nCode )
142 			{
143 				Remember_Impl(GetText());
144 
145 				::rtl::OUString sFindText = GetText();
146 				css::uno::Sequence< css::beans::PropertyValue > lArgs(2);
147 
148 				lArgs[0].Name = SEARCHITEM_SEARCHSTRING;
149 				lArgs[0].Value <<= sFindText;
150 
151 				lArgs[1].Name = SEARCHITEM_SEARCHBACKWARD;
152 				if (bShift)
153 					lArgs[1].Value <<= sal_True;
154 				else
155 					lArgs[1].Value <<= sal_False;
156 
157 				impl_executeSearch(m_xServiceManager, m_xFrame, lArgs);
158 				nRet = 1;
159 			}
160 			break;
161 		}
162 
163 		case EVENT_GETFOCUS:
164 			if ( m_bToClearTextField )
165 			{
166 				SetText( String() );
167 				m_bToClearTextField = sal_False;
168 			}
169 			SetSelection( Selection( SELECTION_MIN, SELECTION_MAX ) );
170 			break;
171 
172 		case EVENT_LOSEFOCUS:
173 			if ( GetText().Len() == 0 )
174 			{
175 				SetText( SVX_RESSTR( RID_SVXSTR_FINDBAR_FIND ) );
176 				SetControlForeground(GetSettings().GetStyleSettings().GetDisableColor());
177 				m_bToClearTextField = sal_True;
178 			}
179 			break;
180 	}
181 
182 	return nRet;
183 }
184 
185 
186 //-----------------------------------------------------------------------------------------------------------
187 // SearchToolbarControllersManager
188 
189 SearchToolbarControllersManager* SearchToolbarControllersManager::m_pInstance = 0;
190 
SearchToolbarControllersManager()191 SearchToolbarControllersManager::SearchToolbarControllersManager()
192 {
193 }
194 
~SearchToolbarControllersManager()195 SearchToolbarControllersManager::~SearchToolbarControllersManager()
196 {
197 }
198 
createControllersManager()199 SearchToolbarControllersManager* SearchToolbarControllersManager::createControllersManager()
200 {
201 	if (!m_pInstance)
202 		m_pInstance = new SearchToolbarControllersManager();
203 
204 	return m_pInstance;
205 }
206 
registryController(const css::uno::Reference<css::frame::XFrame> & xFrame,const css::uno::Reference<css::frame::XStatusListener> & xStatusListener,const::rtl::OUString & sCommandURL)207 void SearchToolbarControllersManager::registryController( const css::uno::Reference< css::frame::XFrame >& xFrame, const css::uno::Reference< css::frame::XStatusListener >& xStatusListener, const ::rtl::OUString& sCommandURL )
208 {
209 	SearchToolbarControllersMap::iterator pIt = aSearchToolbarControllersMap.find(xFrame);
210 	if (pIt == aSearchToolbarControllersMap.end())
211 	{
212 		SearchToolbarControllersVec lControllers(1);
213 		lControllers[0].Name = sCommandURL;
214 		lControllers[0].Value <<= xStatusListener;
215 		aSearchToolbarControllersMap.insert(SearchToolbarControllersMap::value_type(xFrame, lControllers));
216 	}
217 	else
218 	{
219 		sal_Int32 nSize = pIt->second.size();
220 		for (sal_Int32 i=0; i<nSize; ++i)
221 		{
222 			if (pIt->second[i].Name.equals(sCommandURL))
223 				return;
224 		}
225 
226 		pIt->second.resize(nSize+1);
227 		pIt->second[nSize].Name = sCommandURL;
228 		pIt->second[nSize].Value <<= xStatusListener;
229 	}
230 }
231 
freeController(const css::uno::Reference<css::frame::XFrame> & xFrame,const css::uno::Reference<css::frame::XStatusListener> &,const::rtl::OUString & sCommandURL)232 void SearchToolbarControllersManager::freeController( const css::uno::Reference< css::frame::XFrame >& xFrame, const css::uno::Reference< css::frame::XStatusListener >& /*xStatusListener*/, const ::rtl::OUString& sCommandURL )
233 {
234 	SearchToolbarControllersMap::iterator pIt = aSearchToolbarControllersMap.find(xFrame);
235 	if (pIt != aSearchToolbarControllersMap.end())
236 	{
237 		for (SearchToolbarControllersVec::iterator pItCtrl=pIt->second.begin(); pItCtrl!=pIt->second.end(); ++pItCtrl)
238 		{
239 			if (pItCtrl->Name.equals(sCommandURL))
240 			{
241 				pIt->second.erase(pItCtrl);
242 				break;
243 			}
244 		}
245 
246 		if (pIt->second.empty())
247 			aSearchToolbarControllersMap.erase(pIt);
248 	}
249 }
250 
findController(const css::uno::Reference<css::frame::XFrame> & xFrame,const::rtl::OUString & sCommandURL)251 css::uno::Reference< css::frame::XStatusListener > SearchToolbarControllersManager::findController( const css::uno::Reference< css::frame::XFrame >& xFrame, const ::rtl::OUString& sCommandURL )
252 {
253 	css::uno::Reference< css::frame::XStatusListener > xStatusListener;
254 
255 	SearchToolbarControllersMap::iterator pIt = aSearchToolbarControllersMap.find(xFrame);
256 	if (pIt != aSearchToolbarControllersMap.end())
257 	{
258 		for (SearchToolbarControllersVec::iterator pItCtrl =pIt->second.begin(); pItCtrl != pIt->second.end(); ++pItCtrl)
259 		{
260 			if (pItCtrl->Name.equals(sCommandURL))
261 			{
262 				pItCtrl->Value >>= xStatusListener;
263 				break;
264 			}
265 		}
266 	}
267 
268 	return xStatusListener;
269 }
270 
271 //-----------------------------------------------------------------------------------------------------------
272 // FindTextToolbarController
273 
FindTextToolbarController(const css::uno::Reference<css::lang::XMultiServiceFactory> & rServiceManager)274 FindTextToolbarController::FindTextToolbarController( const css::uno::Reference< css::lang::XMultiServiceFactory >& rServiceManager )
275 	:svt::ToolboxController( rServiceManager,
276 	css::uno::Reference< css::frame::XFrame >(),
277 	COMMAND_FINDTEXT )
278 {
279 }
280 
~FindTextToolbarController()281 FindTextToolbarController::~FindTextToolbarController()
282 {
283 }
284 
285 // XInterface
queryInterface(const css::uno::Type & aType)286 css::uno::Any SAL_CALL FindTextToolbarController::queryInterface( const css::uno::Type& aType ) throw ( css::uno::RuntimeException )
287 {
288 	css::uno::Any a = ToolboxController::queryInterface( aType );
289 	if ( a.hasValue() )
290 		return a;
291 
292 	return ::cppu::queryInterface( aType, static_cast< css::lang::XServiceInfo* >( this ) );
293 }
294 
acquire()295 void SAL_CALL FindTextToolbarController::acquire() throw ()
296 {
297 	ToolboxController::acquire();
298 }
299 
release()300 void SAL_CALL FindTextToolbarController::release() throw ()
301 {
302 	ToolboxController::release();
303 }
304 
305 // XServiceInfo
getImplementationName()306 ::rtl::OUString SAL_CALL FindTextToolbarController::getImplementationName() throw( css::uno::RuntimeException )
307 {
308 	return getImplementationName_Static();
309 }
310 
supportsService(const::rtl::OUString & ServiceName)311 sal_Bool SAL_CALL FindTextToolbarController::supportsService( const ::rtl::OUString& ServiceName ) throw( css::uno::RuntimeException )
312 {
313 	const css::uno::Sequence< ::rtl::OUString > aSNL( getSupportedServiceNames() );
314 	const ::rtl::OUString * pArray = aSNL.getConstArray();
315 
316 	for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
317 		if( pArray[i] == ServiceName )
318 			return true;
319 
320 	return false;
321 }
322 
getSupportedServiceNames()323 css::uno::Sequence< ::rtl::OUString > SAL_CALL FindTextToolbarController::getSupportedServiceNames() throw( css::uno::RuntimeException )
324 {
325 	return getSupportedServiceNames_Static();
326 }
327 
getSupportedServiceNames_Static()328 css::uno::Sequence< ::rtl::OUString > FindTextToolbarController::getSupportedServiceNames_Static() throw()
329 {
330 	css::uno::Sequence< ::rtl::OUString > aSNS( 1 );
331 	aSNS.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.ToolbarController" ) );
332 	return aSNS;
333 }
334 
335 // XComponent
dispose()336 void SAL_CALL FindTextToolbarController::dispose() throw ( css::uno::RuntimeException )
337 {
338 	vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
339 
340 	SearchToolbarControllersManager::createControllersManager()->freeController(m_xFrame, css::uno::Reference< css::frame::XStatusListener >(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY), m_aCommandURL);
341 
342 	svt::ToolboxController::dispose();
343 	delete m_pFindTextFieldControl;
344 	m_pFindTextFieldControl = 0;
345 }
346 
347 // XInitialization
initialize(const css::uno::Sequence<::com::sun::star::uno::Any> & aArguments)348 void SAL_CALL FindTextToolbarController::initialize( const css::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw ( css::uno::Exception, css::uno::RuntimeException)
349 {
350 	svt::ToolboxController::initialize(aArguments);
351 
352 	Window* pWindow = VCLUnoHelper::GetWindow( getParent() );
353 	ToolBox* pToolBox = (ToolBox*)pWindow;
354 	if ( pToolBox )
355 	{
356 		sal_uInt16 nItemCount = pToolBox->GetItemCount();
357 		for ( sal_uInt16 i=0; i<nItemCount; ++i )
358 		{
359 			::rtl::OUString sItemCommand = pToolBox->GetItemCommand(i);
360 			if ( sItemCommand.equals( COMMAND_DOWNSEARCH ) )
361 				{
362 				pToolBox->EnableItem(i, sal_False);
363 				m_nDownSearchId = i;
364 				}
365 			else if (sItemCommand.equals( COMMAND_UPSEARCH ))
366 				{
367 				pToolBox->EnableItem(i, sal_False);
368 				m_nUpSearchId = i;
369 				}
370 		}
371 	}
372 
373 	SearchToolbarControllersManager::createControllersManager()->registryController(m_xFrame, css::uno::Reference< css::frame::XStatusListener >(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY), m_aCommandURL);
374 }
375 
376 // XToolbarController
execute(sal_Int16)377 void SAL_CALL FindTextToolbarController::execute( sal_Int16 /*KeyModifier*/ ) throw ( css::uno::RuntimeException )
378 {
379 }
380 
createItemWindow(const css::uno::Reference<css::awt::XWindow> & Parent)381 css::uno::Reference< css::awt::XWindow > SAL_CALL FindTextToolbarController::createItemWindow( const css::uno::Reference< css::awt::XWindow >& Parent ) throw ( css::uno::RuntimeException )
382 {
383 	css::uno::Reference< css::awt::XWindow > xItemWindow;
384 
385 	css::uno::Reference< css::awt::XWindow > xParent( Parent );
386 	Window* pParent = VCLUnoHelper::GetWindow( xParent );
387 	if ( pParent )
388 	{
389 		ToolBox* pToolbar = ( ToolBox* )pParent;
390 		m_pFindTextFieldControl = new FindTextFieldControl( pToolbar, WinBits( WB_DROPDOWN | WB_VSCROLL), m_xFrame, m_xServiceManager );
391 
392 		Size aSize(100, m_pFindTextFieldControl->GetTextHeight() + 200);
393 		m_pFindTextFieldControl->SetSizePixel( aSize );
394 		m_pFindTextFieldControl->SetModifyHdl(LINK(this, FindTextToolbarController, EditModifyHdl));
395 	}
396 	xItemWindow = VCLUnoHelper::GetInterface( m_pFindTextFieldControl );
397 
398 	return xItemWindow;
399 }
400 
401 // XStatusListener
statusChanged(const css::frame::FeatureStateEvent & rEvent)402 void SAL_CALL FindTextToolbarController::statusChanged( const css::frame::FeatureStateEvent& rEvent ) throw ( css::uno::RuntimeException )
403 {
404 	vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
405 	if ( m_bDisposed )
406 		return;
407 
408 	::rtl::OUString aFeatureURL = rEvent.FeatureURL.Complete;
409 	if (aFeatureURL.equalsAscii("AppendSearchHistory"))
410 	{
411 		m_pFindTextFieldControl->Remember_Impl(m_pFindTextFieldControl->GetText());
412 	}
413 }
414 
IMPL_LINK(FindTextToolbarController,EditModifyHdl,void *,EMPTYARG)415 IMPL_LINK( FindTextToolbarController, EditModifyHdl, void *, EMPTYARG )
416 {
417 	// enable or disable item DownSearch/UpSearch of findbar
418 	Window* pWindow = VCLUnoHelper::GetWindow( getParent() );
419 	ToolBox* pToolBox = (ToolBox*)pWindow;
420 	if ( pToolBox && m_pFindTextFieldControl )
421 	{
422 		if (m_pFindTextFieldControl->GetText().Len()>0)
423 		{
424 			if ( !pToolBox->IsItemEnabled(m_nDownSearchId) )
425 				pToolBox->EnableItem(m_nDownSearchId, sal_True);
426 			if ( !pToolBox->IsItemEnabled(m_nUpSearchId) )
427 				pToolBox->EnableItem(m_nUpSearchId, sal_True);
428 		}
429 		else
430 		{
431 			if ( pToolBox->IsItemEnabled(m_nDownSearchId) )
432 				pToolBox->EnableItem(m_nDownSearchId, sal_False);
433 			if ( pToolBox->IsItemEnabled(m_nUpSearchId) )
434 				pToolBox->EnableItem(m_nUpSearchId, sal_False);
435 		}
436 	}
437 
438 	return 0;
439 }
440 
441 //-----------------------------------------------------------------------------------------------------------
442 // class DownSearchToolboxController
443 
DownSearchToolboxController(const css::uno::Reference<css::lang::XMultiServiceFactory> & rServiceManager)444 DownSearchToolboxController::DownSearchToolboxController(const css::uno::Reference< css::lang::XMultiServiceFactory >& rServiceManager )
445 	: svt::ToolboxController( rServiceManager,
446 	css::uno::Reference< css::frame::XFrame >(),
447 	COMMAND_DOWNSEARCH )
448 {
449 }
450 
~DownSearchToolboxController()451 DownSearchToolboxController::~DownSearchToolboxController()
452 {
453 }
454 
455 // XInterface
queryInterface(const css::uno::Type & aType)456 css::uno::Any SAL_CALL DownSearchToolboxController::queryInterface( const css::uno::Type& aType ) throw ( css::uno::RuntimeException )
457 {
458 	css::uno::Any a = ToolboxController::queryInterface( aType );
459 	if ( a.hasValue() )
460 		return a;
461 
462 	return ::cppu::queryInterface( aType, static_cast< css::lang::XServiceInfo* >( this ) );
463 }
464 
acquire()465 void SAL_CALL DownSearchToolboxController::acquire() throw ()
466 {
467 	ToolboxController::acquire();
468 }
469 
release()470 void SAL_CALL DownSearchToolboxController::release() throw ()
471 {
472 	ToolboxController::release();
473 }
474 
475 // XServiceInfo
getImplementationName()476 ::rtl::OUString SAL_CALL DownSearchToolboxController::getImplementationName() throw( css::uno::RuntimeException )
477 {
478 	return getImplementationName_Static();
479 }
480 
supportsService(const::rtl::OUString & ServiceName)481 sal_Bool SAL_CALL DownSearchToolboxController::supportsService( const ::rtl::OUString& ServiceName ) throw( css::uno::RuntimeException )
482 {
483 	const css::uno::Sequence< ::rtl::OUString > aSNL( getSupportedServiceNames() );
484 	const ::rtl::OUString * pArray = aSNL.getConstArray();
485 
486 	for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
487 		if( pArray[i] == ServiceName )
488 			return true;
489 
490 	return false;
491 }
492 
getSupportedServiceNames()493 css::uno::Sequence< ::rtl::OUString > SAL_CALL DownSearchToolboxController::getSupportedServiceNames() throw( css::uno::RuntimeException )
494 {
495 	return getSupportedServiceNames_Static();
496 }
497 
getSupportedServiceNames_Static()498 css::uno::Sequence< ::rtl::OUString > DownSearchToolboxController::getSupportedServiceNames_Static() throw()
499 {
500 	css::uno::Sequence< ::rtl::OUString > aSNS( 1 );
501 	aSNS.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.ToolbarController" ));
502 	return aSNS;
503 }
504 
505 // XComponent
dispose()506 void SAL_CALL DownSearchToolboxController::dispose() throw ( css::uno::RuntimeException )
507 {
508 	vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
509 
510 	SearchToolbarControllersManager::createControllersManager()->freeController(m_xFrame, css::uno::Reference< css::frame::XStatusListener >(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY), m_aCommandURL);
511 
512 	svt::ToolboxController::dispose();
513 }
514 
515 // XInitialization
initialize(const css::uno::Sequence<css::uno::Any> & aArguments)516 void SAL_CALL DownSearchToolboxController::initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) throw ( css::uno::Exception, css::uno::RuntimeException )
517 {
518 	svt::ToolboxController::initialize( aArguments );
519 	SearchToolbarControllersManager::createControllersManager()->registryController(m_xFrame, css::uno::Reference< css::frame::XStatusListener >(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY), m_aCommandURL);
520 }
521 
522 // XToolbarController
execute(sal_Int16)523 void SAL_CALL DownSearchToolboxController::execute( sal_Int16 /*KeyModifier*/ ) throw ( css::uno::RuntimeException)
524 {
525 	if ( m_bDisposed )
526 		throw css::lang::DisposedException();
527 
528 	::rtl::OUString sFindText;
529 	Window* pWindow = VCLUnoHelper::GetWindow( getParent() );
530 	ToolBox* pToolBox = (ToolBox*)pWindow;
531 	if ( pToolBox )
532 	{
533 		sal_uInt16 nItemCount = pToolBox->GetItemCount();
534 		for ( sal_uInt16 i=0; i<nItemCount; ++i )
535 		{
536 			::rtl::OUString sItemCommand = pToolBox->GetItemCommand(i);
537 			if ( sItemCommand.equals( COMMAND_FINDTEXT ) )
538 			{
539 				Window* pItemWin = pToolBox->GetItemWindow(i);
540 				if (pItemWin)
541 					sFindText = pItemWin->GetText();
542 				break;
543 			}
544 		}
545 	}
546 
547 	css::uno::Sequence< css::beans::PropertyValue > lArgs(2);
548 	lArgs[0].Name = SEARCHITEM_SEARCHSTRING;
549 	lArgs[0].Value <<= sFindText;
550 	lArgs[1].Name = SEARCHITEM_SEARCHBACKWARD;
551 	lArgs[1].Value <<= sal_False;
552 
553 	impl_executeSearch(m_xServiceManager, m_xFrame, lArgs);
554 
555 	css::frame::FeatureStateEvent aEvent;
556 	aEvent.FeatureURL.Complete = COMMAND_APPENDSEARCHHISTORY;
557 	css::uno::Reference< css::frame::XStatusListener > xStatusListener = SearchToolbarControllersManager::createControllersManager()->findController(m_xFrame, COMMAND_FINDTEXT);
558 	if (xStatusListener.is())
559 		xStatusListener->statusChanged( aEvent );
560 }
561 
562 // XStatusListener
statusChanged(const css::frame::FeatureStateEvent &)563 void SAL_CALL DownSearchToolboxController::statusChanged( const css::frame::FeatureStateEvent& /*rEvent*/ ) throw ( css::uno::RuntimeException )
564 {
565 	vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
566 	if ( m_bDisposed )
567 		return;
568 }
569 
570 //-----------------------------------------------------------------------------------------------------------
571 // class UpSearchToolboxController
572 
UpSearchToolboxController(const css::uno::Reference<css::lang::XMultiServiceFactory> & rServiceManager)573 UpSearchToolboxController::UpSearchToolboxController( const css::uno::Reference< css::lang::XMultiServiceFactory > & rServiceManager )
574 	:svt::ToolboxController( rServiceManager,
575 	css::uno::Reference< css::frame::XFrame >(),
576 	COMMAND_UPSEARCH )
577 {
578 }
579 
~UpSearchToolboxController()580 UpSearchToolboxController::~UpSearchToolboxController()
581 {
582 }
583 
584 // XInterface
queryInterface(const css::uno::Type & aType)585 css::uno::Any SAL_CALL UpSearchToolboxController::queryInterface( const css::uno::Type& aType ) throw ( css::uno::RuntimeException )
586 {
587 	css::uno::Any a = ToolboxController::queryInterface( aType );
588 	if ( a.hasValue() )
589 		return a;
590 
591 	return ::cppu::queryInterface( aType, static_cast< css::lang::XServiceInfo* >( this ) );
592 }
593 
acquire()594 void SAL_CALL UpSearchToolboxController::acquire() throw ()
595 {
596 	ToolboxController::acquire();
597 }
598 
release()599 void SAL_CALL UpSearchToolboxController::release() throw ()
600 {
601 	ToolboxController::release();
602 }
603 
604 // XServiceInfo
getImplementationName()605 ::rtl::OUString SAL_CALL UpSearchToolboxController::getImplementationName() throw( css::uno::RuntimeException )
606 {
607 	return getImplementationName_Static();
608 }
609 
supportsService(const::rtl::OUString & ServiceName)610 sal_Bool SAL_CALL UpSearchToolboxController::supportsService( const ::rtl::OUString& ServiceName ) throw( css::uno::RuntimeException )
611 {
612 	const css::uno::Sequence< ::rtl::OUString > aSNL( getSupportedServiceNames() );
613 	const ::rtl::OUString * pArray = aSNL.getConstArray();
614 
615 	for( sal_Int32 i = 0; i < aSNL.getLength(); i++ )
616 		if( pArray[i] == ServiceName )
617 			return true;
618 
619 	return false;
620 }
621 
getSupportedServiceNames()622 css::uno::Sequence< ::rtl::OUString > SAL_CALL UpSearchToolboxController::getSupportedServiceNames() throw( css::uno::RuntimeException )
623 {
624 	return getSupportedServiceNames_Static();
625 }
626 
getSupportedServiceNames_Static()627 css::uno::Sequence< ::rtl::OUString > UpSearchToolboxController::getSupportedServiceNames_Static() throw()
628 {
629 	css::uno::Sequence< ::rtl::OUString > aSNS( 1 );
630 	aSNS.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.ToolbarController" ) );
631 	return aSNS;
632 }
633 
634 // XComponent
dispose()635 void SAL_CALL UpSearchToolboxController::dispose() throw ( css::uno::RuntimeException )
636 {
637 	vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
638 
639 	SearchToolbarControllersManager::createControllersManager()->freeController(m_xFrame, css::uno::Reference< css::frame::XStatusListener >(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY), m_aCommandURL);
640 
641 	svt::ToolboxController::dispose();
642 }
643 
644 // XInitialization
initialize(const css::uno::Sequence<css::uno::Any> & aArguments)645 void SAL_CALL UpSearchToolboxController::initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) throw ( css::uno::Exception, css::uno::RuntimeException )
646 {
647 	svt::ToolboxController::initialize( aArguments );
648 	SearchToolbarControllersManager::createControllersManager()->registryController(m_xFrame, css::uno::Reference< css::frame::XStatusListener >(static_cast< ::cppu::OWeakObject* >(this), css::uno::UNO_QUERY), m_aCommandURL);
649 }
650 
651 // XToolbarController
execute(sal_Int16)652 void SAL_CALL UpSearchToolboxController::execute( sal_Int16 /*KeyModifier*/ ) throw ( css::uno::RuntimeException )
653 {
654 	if ( m_bDisposed )
655 		throw css::lang::DisposedException();
656 
657 	::rtl::OUString sFindText;
658 	Window* pWindow = VCLUnoHelper::GetWindow( getParent() );
659 	ToolBox* pToolBox = (ToolBox*)pWindow;
660 	if ( pToolBox )
661 	{
662 		sal_uInt16 nItemCount = pToolBox->GetItemCount();
663 		for ( sal_uInt16 i=0; i<nItemCount; ++i )
664 		{
665 			::rtl::OUString sItemCommand = pToolBox->GetItemCommand(i);
666 			if ( sItemCommand.equals( COMMAND_FINDTEXT ) )
667 			{
668 				Window* pItemWin = pToolBox->GetItemWindow(i);
669 				if (pItemWin)
670 					sFindText = pItemWin->GetText();
671 				break;
672 			}
673 		}
674 	}
675 
676 	css::uno::Sequence< css::beans::PropertyValue > lArgs(2);
677 	lArgs[0].Name = SEARCHITEM_SEARCHSTRING;
678 	lArgs[0].Value <<= sFindText;
679 	lArgs[1].Name = SEARCHITEM_SEARCHBACKWARD;
680 	lArgs[1].Value <<= sal_True;
681 
682 	impl_executeSearch(m_xServiceManager, m_xFrame, lArgs);
683 
684 	css::frame::FeatureStateEvent aEvent;
685 	aEvent.FeatureURL.Complete = COMMAND_APPENDSEARCHHISTORY;
686 	css::uno::Reference< css::frame::XStatusListener > xStatusListener = SearchToolbarControllersManager::createControllersManager()->findController(m_xFrame, COMMAND_FINDTEXT);
687 	if (xStatusListener.is())
688 		xStatusListener->statusChanged( aEvent );
689 }
690 
691 // XStatusListener
statusChanged(const css::frame::FeatureStateEvent &)692 void SAL_CALL UpSearchToolboxController::statusChanged( const css::frame::FeatureStateEvent& /*rEvent*/ ) throw ( css::uno::RuntimeException )
693 {
694 	vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
695 	if ( m_bDisposed )
696 		return;
697 }
698 
699 //-----------------------------------------------------------------------------------------------------------
700 // class FindbarDispatcher
701 
FindbarDispatcher(const css::uno::Reference<css::lang::XMultiServiceFactory> & xFactory)702 FindbarDispatcher::FindbarDispatcher(const css::uno::Reference< css::lang::XMultiServiceFactory >& xFactory)
703 	: m_xFactory( xFactory )
704 {
705 }
706 
~FindbarDispatcher()707 FindbarDispatcher::~FindbarDispatcher()
708 {
709 	m_xFactory = NULL;
710 	m_xFrame = NULL;
711 }
712 
713 // XInterface
queryInterface(const css::uno::Type & aType)714 css::uno::Any SAL_CALL FindbarDispatcher::queryInterface( const css::uno::Type& aType ) throw( css::uno::RuntimeException )
715 {
716 	css::uno::Any aReturn( ::cppu::queryInterface( aType,
717 		static_cast< css::lang::XServiceInfo* >(this),
718 		static_cast< css::lang::XInitialization* >(this),
719 		static_cast< css::frame::XDispatchProvider* >(this),
720 		static_cast< css::frame::XDispatch* >(this)) );
721 
722 	if ( aReturn.hasValue() )
723 		return aReturn;
724 
725 	return OWeakObject::queryInterface( aType );
726 }
727 
acquire()728 void SAL_CALL FindbarDispatcher::acquire() throw()
729 {
730 	OWeakObject::acquire();
731 }
732 
release()733 void SAL_CALL FindbarDispatcher::release() throw()
734 {
735 	OWeakObject::release();
736 }
737 
738 // XServiceInfo
getImplementationName()739 ::rtl::OUString SAL_CALL FindbarDispatcher::getImplementationName() throw( css::uno::RuntimeException )
740 {
741 	return getImplementationName_Static();
742 }
743 
supportsService(const::rtl::OUString & ServiceName)744 sal_Bool SAL_CALL FindbarDispatcher::supportsService( const ::rtl::OUString& ServiceName ) throw( css::uno::RuntimeException )
745 {
746 	return (
747 		ServiceName.equalsAscii("com.sun.star.comp.svx.FindbarDispatcher") ||
748 		ServiceName.equalsAscii("com.sun.star.frame.ProtocolHandler")
749 		);
750 }
751 
getSupportedServiceNames()752 css::uno::Sequence< ::rtl::OUString > SAL_CALL FindbarDispatcher::getSupportedServiceNames() throw( css::uno::RuntimeException )
753 {
754 	return getSupportedServiceNames_Static();
755 }
756 
getSupportedServiceNames_Static()757 css::uno::Sequence< ::rtl::OUString > FindbarDispatcher::getSupportedServiceNames_Static() throw()
758 {
759 	css::uno::Sequence< ::rtl::OUString > aSNS( 2 );
760 	aSNS.getArray()[0] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.svx.FindbarDispatcher" ));
761 	aSNS.getArray()[1] = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.ProtocolHandler" ));
762 	return aSNS;
763 }
764 
765 // XInitialization
initialize(const css::uno::Sequence<css::uno::Any> & aArguments)766 void SAL_CALL FindbarDispatcher::initialize( const css::uno::Sequence< css::uno::Any >& aArguments ) throw ( css::uno::Exception, css::uno::RuntimeException )
767 {
768 	if ( aArguments.getLength() )
769 		aArguments[0] >>= m_xFrame;
770 }
771 
772 // XDispatchProvider
queryDispatch(const css::util::URL & aURL,const::rtl::OUString &,sal_Int32)773 css::uno::Reference< css::frame::XDispatch > SAL_CALL FindbarDispatcher::queryDispatch( const css::util::URL& aURL, const ::rtl::OUString& /*sTargetFrameName*/, sal_Int32 /*nSearchFlags*/ ) throw( css::uno::RuntimeException )
774 {
775 	css::uno::Reference< css::frame::XDispatch > xDispatch;
776 
777 	if ( aURL.Protocol.equalsAscii("vnd.sun.star.findbar:") )
778 		xDispatch = this;
779 
780 	return xDispatch;
781 }
782 
queryDispatches(const css::uno::Sequence<css::frame::DispatchDescriptor> & seqDescripts)783 css::uno::Sequence < css::uno::Reference< css::frame::XDispatch > > SAL_CALL FindbarDispatcher::queryDispatches( const css::uno::Sequence < css::frame::DispatchDescriptor >& seqDescripts ) throw( css::uno::RuntimeException )
784 {
785 	sal_Int32 nCount = seqDescripts.getLength();
786 	css::uno::Sequence < css::uno::Reference < XDispatch > > lDispatcher( nCount );
787 
788 	for( sal_Int32 i=0; i<nCount; ++i )
789 		lDispatcher[i] = queryDispatch( seqDescripts[i].FeatureURL, seqDescripts[i].FrameName, seqDescripts[i].SearchFlags );
790 
791 	return lDispatcher;
792 }
793 
794 // XDispatch
dispatch(const css::util::URL & aURL,const css::uno::Sequence<css::beans::PropertyValue> &)795 void SAL_CALL FindbarDispatcher::dispatch( const css::util::URL& aURL, const css::uno::Sequence < css::beans::PropertyValue >& /*lArgs*/ ) throw( css::uno::RuntimeException )
796 {
797 	//vnd.sun.star.findbar:FocusToFindbar - set cursor to the FindTextFieldControl of the findbar
798 	if ( aURL.Path.equalsAscii("FocusToFindbar") )
799 	{
800 		css::uno::Reference< css::beans::XPropertySet > xPropSet(m_xFrame, css::uno::UNO_QUERY);
801 		if(!xPropSet.is())
802 			return;
803 
804 		css::uno::Reference< css::frame::XLayoutManager > xLayoutManager;
805 		css::uno::Any aValue = xPropSet->getPropertyValue( ::rtl::OUString::createFromAscii("LayoutManager") );
806 		aValue >>= xLayoutManager;
807 		if (!xLayoutManager.is())
808 			return;
809 
810 		const ::rtl::OUString sResourceURL = ::rtl::OUString::createFromAscii("private:resource/toolbar/findbar");
811 		css::uno::Reference< css::ui::XUIElement > xUIElement = xLayoutManager->getElement(sResourceURL);
812 		if (!xUIElement.is())
813 			return;
814 
815 		css::uno::Reference< css::awt::XWindow > xWindow(xUIElement->getRealInterface(), css::uno::UNO_QUERY);
816 		Window* pWindow = VCLUnoHelper::GetWindow( xWindow );
817 		ToolBox* pToolBox = (ToolBox*)pWindow;
818 		if ( pToolBox )
819 		{
820 			sal_uInt16 nItemCount = pToolBox->GetItemCount();
821 			for ( sal_uInt16 i=0; i<nItemCount; ++i )
822 			{
823 				::rtl::OUString sItemCommand = pToolBox->GetItemCommand(i);
824 				if ( sItemCommand.equalsAscii(".uno:FindText") )
825 				{
826 					Window* pItemWin = pToolBox->GetItemWindow( i );
827 					if ( pItemWin )
828 					{
829 						pItemWin->GrabFocus();
830 						return;
831 					}
832 				}
833 			}
834 		}
835 
836 	}
837 }
838 
addStatusListener(const css::uno::Reference<css::frame::XStatusListener> &,const css::util::URL &)839 void SAL_CALL FindbarDispatcher::addStatusListener( const css::uno::Reference< css::frame::XStatusListener >& /*xControl*/, const css::util::URL& /*aURL*/ ) throw ( css::uno::RuntimeException )
840 {
841 }
842 
removeStatusListener(const css::uno::Reference<css::frame::XStatusListener> &,const css::util::URL &)843 void SAL_CALL FindbarDispatcher::removeStatusListener( const css::uno::Reference< css::frame::XStatusListener >& /*xControl*/, const css::util::URL& /*aURL*/ ) throw ( css::uno::RuntimeException )
844 {
845 }
846 
847 //-----------------------------------------------------------------------------------------------------------
848 // create Instance
849 
FindTextToolbarController_createInstance(const css::uno::Reference<css::lang::XMultiServiceFactory> & rSMgr)850 css::uno::Reference< css::uno::XInterface > SAL_CALL FindTextToolbarController_createInstance(
851 	const css::uno::Reference< css::lang::XMultiServiceFactory >& rSMgr )
852 {
853 	return *new FindTextToolbarController( rSMgr );
854 }
855 
DownSearchToolboxController_createInstance(const css::uno::Reference<css::lang::XMultiServiceFactory> & rSMgr)856 css::uno::Reference< css::uno::XInterface > SAL_CALL DownSearchToolboxController_createInstance(
857 	const css::uno::Reference< css::lang::XMultiServiceFactory >& rSMgr )
858 {
859 	return *new DownSearchToolboxController( rSMgr );
860 }
861 
UpSearchToolboxController_createInstance(const css::uno::Reference<css::lang::XMultiServiceFactory> & rSMgr)862 css::uno::Reference< css::uno::XInterface > SAL_CALL UpSearchToolboxController_createInstance(
863 	const css::uno::Reference< css::lang::XMultiServiceFactory >& rSMgr )
864 {
865 	return *new UpSearchToolboxController( rSMgr );
866 }
867 
FindbarDispatcher_createInstance(const css::uno::Reference<css::lang::XMultiServiceFactory> & rSMgr)868 css::uno::Reference< css::uno::XInterface > SAL_CALL FindbarDispatcher_createInstance(
869 	const css::uno::Reference< css::lang::XMultiServiceFactory >& rSMgr )
870 {
871 	return *new FindbarDispatcher( rSMgr );
872 }
873 
874 //-----------------------------------------------------------------------------------------------------------
875 }
876