lok: Don't attempt to select the exact text after a failed search.
[LibreOffice.git] / basctl / source / basicide / basicbox.cxx
blob4e3982f0eecb50e014d6b299281cdac4695c31a3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <basidesh.hrc>
21 #include <basidesh.hxx>
23 #include <basicbox.hxx>
24 #include <iderdll.hxx>
26 #include "localizationmgr.hxx"
27 #include "managelang.hxx"
28 #include "dlgresid.hrc"
30 #include <sfx2/dispatch.hxx>
31 #include <svtools/langtab.hxx>
33 namespace basctl
36 using namespace ::com::sun::star;
37 using namespace ::com::sun::star::lang;
38 using namespace ::com::sun::star::uno;
40 SFX_IMPL_TOOLBOX_CONTROL( LibBoxControl, SfxStringItem );
42 LibBoxControl::LibBoxControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx )
43 : SfxToolBoxControl( nSlotId, nId, rTbx )
49 void LibBoxControl::StateChanged( sal_uInt16, SfxItemState eState, const SfxPoolItem* pState )
51 LibBox* pBox = static_cast<LibBox*>(GetToolBox().GetItemWindow(GetId()));
53 DBG_ASSERT( pBox, "Box not found" );
54 if ( !pBox )
55 return;
57 if ( eState != SfxItemState::DEFAULT )
58 pBox->Disable();
59 else
61 pBox->Enable();
62 pBox->Update(dynamic_cast<SfxStringItem const*>(pState));
68 VclPtr<vcl::Window> LibBoxControl::CreateItemWindow( vcl::Window *pParent )
70 return VclPtr<LibBox>::Create( pParent, m_xFrame );
74 DocListenerBox::DocListenerBox( vcl::Window* pParent )
75 :ListBox( pParent, WinBits( WB_BORDER | WB_DROPDOWN ) )
76 ,m_aNotifier( *this )
80 DocListenerBox::~DocListenerBox()
82 disposeOnce();
85 void DocListenerBox::dispose()
87 m_aNotifier.dispose();
88 ListBox::dispose();
91 void DocListenerBox::onDocumentCreated( const ScriptDocument& /*_rDocument*/ )
93 FillBox();
96 void DocListenerBox::onDocumentOpened( const ScriptDocument& /*_rDocument*/ )
98 FillBox();
101 void DocListenerBox::onDocumentSave( const ScriptDocument& /*_rDocument*/ )
103 // not interested in
106 void DocListenerBox::onDocumentSaveDone( const ScriptDocument& /*_rDocument*/ )
108 // not interested in
111 void DocListenerBox::onDocumentSaveAs( const ScriptDocument& /*_rDocument*/ )
113 // not interested in
116 void DocListenerBox::onDocumentSaveAsDone( const ScriptDocument& /*_rDocument*/ )
118 FillBox();
121 void DocListenerBox::onDocumentClosed( const ScriptDocument& /*_rDocument*/ )
123 FillBox();
126 void DocListenerBox::onDocumentTitleChanged( const ScriptDocument& /*_rDocument*/ )
128 // not interested in
131 void DocListenerBox::onDocumentModeChanged( const ScriptDocument& /*_rDocument*/ )
133 // not interested in
136 LibBox::LibBox( vcl::Window* pParent, const uno::Reference< frame::XFrame >& rFrame ) :
137 DocListenerBox( pParent ),
138 m_xFrame( rFrame )
140 FillBox();
141 bIgnoreSelect = true; // do not yet transfer select of 0
142 bFillBox = true;
143 SelectEntryPos( 0 );
144 aCurText = GetEntry( 0 );
145 SetSizePixel( Size( 250, 200 ) );
146 bIgnoreSelect = false;
151 LibBox::~LibBox()
153 disposeOnce();
156 void LibBox::dispose()
158 ClearBox();
159 DocListenerBox::dispose();
162 void LibBox::Update( const SfxStringItem* pItem )
165 // if ( !pItem || !pItem->GetValue().Len() )
166 FillBox();
168 if ( pItem )
170 aCurText = pItem->GetValue();
171 if ( aCurText.isEmpty() )
172 aCurText = OUString( IDEResId( RID_STR_ALL ) );
175 if ( GetSelectEntry() != aCurText )
176 SelectEntry( aCurText );
179 void LibBox::ReleaseFocus()
181 SfxViewShell* pCurSh = SfxViewShell::Current();
182 DBG_ASSERT( pCurSh, "Current ViewShell not found!" );
184 if ( pCurSh )
186 vcl::Window* pShellWin = pCurSh->GetWindow();
187 if ( !pShellWin )
188 pShellWin = Application::GetDefDialogParent();
190 pShellWin->GrabFocus();
194 void LibBox::FillBox()
196 SetUpdateMode(false);
197 bIgnoreSelect = true;
199 aCurText = GetSelectEntry();
201 SelectEntryPos( 0 );
202 ClearBox();
204 // create list box entries
205 sal_Int32 nPos = InsertEntry( OUString( IDEResId( RID_STR_ALL ) ), LISTBOX_APPEND );
206 SetEntryData( nPos, new LibEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_UNKNOWN, OUString() ) );
207 InsertEntries( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER );
208 InsertEntries( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE );
210 ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::DocumentsSorted ) );
211 for ( ScriptDocuments::const_iterator doc = aDocuments.begin();
212 doc != aDocuments.end();
213 ++doc
216 InsertEntries( *doc, LIBRARY_LOCATION_DOCUMENT );
219 SetUpdateMode(true);
221 SelectEntry( aCurText );
222 if ( !GetSelectEntryCount() )
224 SelectEntryPos( GetEntryCount() );
225 aCurText = GetSelectEntry();
227 bIgnoreSelect = false;
230 void LibBox::InsertEntries( const ScriptDocument& rDocument, LibraryLocation eLocation )
232 // get a sorted list of library names
233 Sequence< OUString > aLibNames = rDocument.getLibraryNames();
234 sal_Int32 nLibCount = aLibNames.getLength();
235 const OUString* pLibNames = aLibNames.getConstArray();
237 for ( sal_Int32 i = 0 ; i < nLibCount ; ++i )
239 OUString aLibName = pLibNames[ i ];
240 if ( eLocation == rDocument.getLibraryLocation( aLibName ) )
242 OUString aName( rDocument.getTitle( eLocation ) );
243 OUString aEntryText( CreateMgrAndLibStr( aName, aLibName ) );
244 sal_Int32 nPos = InsertEntry( aEntryText, LISTBOX_APPEND );
245 SetEntryData( nPos, new LibEntry( rDocument, eLocation, aLibName ) );
250 bool LibBox::PreNotify( NotifyEvent& rNEvt )
252 bool nDone = false;
253 if( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
255 KeyEvent aKeyEvt = *rNEvt.GetKeyEvent();
256 sal_uInt16 nKeyCode = aKeyEvt.GetKeyCode().GetCode();
257 switch( nKeyCode )
259 case KEY_RETURN:
261 NotifyIDE();
262 nDone = true;
264 break;
266 case KEY_ESCAPE:
268 SelectEntry( aCurText );
269 ReleaseFocus();
270 nDone = true;
272 break;
275 else if( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
277 if ( bFillBox )
279 FillBox();
280 bFillBox = false;
283 else if( rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS )
285 if ( !HasChildPathFocus(true) )
287 bIgnoreSelect = true;
288 bFillBox = true;
292 return nDone || ListBox::PreNotify( rNEvt );
295 void LibBox::Select()
297 if ( !IsTravelSelect() )
299 if ( !bIgnoreSelect )
300 NotifyIDE();
301 else
302 SelectEntry( aCurText ); // since 306... (Select after Escape)
306 void LibBox::NotifyIDE()
308 sal_Int32 nSelPos = GetSelectEntryPos();
309 if (LibEntry* pEntry = static_cast<LibEntry*>(GetEntryData(nSelPos)))
311 ScriptDocument aDocument( pEntry->GetDocument() );
312 SfxUsrAnyItem aDocumentItem( SID_BASICIDE_ARG_DOCUMENT_MODEL, uno::makeAny( aDocument.getDocumentOrNull() ) );
313 OUString aLibName = pEntry->GetLibName();
314 SfxStringItem aLibNameItem( SID_BASICIDE_ARG_LIBNAME, aLibName );
315 if (SfxDispatcher* pDispatcher = GetDispatcher())
316 pDispatcher->Execute(
317 SID_BASICIDE_LIBSELECTED,
318 SfxCallMode::SYNCHRON, &aDocumentItem, &aLibNameItem, 0L
321 ReleaseFocus();
324 void LibBox::ClearBox()
326 sal_Int32 nCount = GetEntryCount();
327 for ( sal_Int32 i = 0; i < nCount; ++i )
329 LibEntry* pEntry = static_cast<LibEntry*>(GetEntryData( i ));
330 delete pEntry;
332 ListBox::Clear();
335 // class LanguageBoxControl ----------------------------------------------
337 SFX_IMPL_TOOLBOX_CONTROL( LanguageBoxControl, SfxStringItem );
339 LanguageBoxControl::LanguageBoxControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx )
340 : SfxToolBoxControl( nSlotId, nId, rTbx )
344 void LanguageBoxControl::StateChanged( sal_uInt16 nID, SfxItemState eState, const SfxPoolItem* pItem )
346 (void)nID;
347 if (LanguageBox* pBox = static_cast<LanguageBox*>(GetToolBox().GetItemWindow(GetId())))
349 if (eState != SfxItemState::DEFAULT)
350 pBox->Disable();
351 else
353 pBox->Enable();
354 pBox->Update(dynamic_cast<SfxStringItem const*>(pItem));
359 VclPtr<vcl::Window> LanguageBoxControl::CreateItemWindow( vcl::Window *pParent )
361 return VclPtr<LanguageBox>::Create( pParent );
364 // class basctl::LanguageBox -----------------------------------------------
366 LanguageBox::LanguageBox( vcl::Window* pParent ) :
368 DocListenerBox( pParent ),
370 m_sNotLocalizedStr( IDEResId( RID_STR_TRANSLATION_NOTLOCALIZED ) ),
371 m_sDefaultLanguageStr( IDEResId( RID_STR_TRANSLATION_DEFAULT ) ),
373 m_bIgnoreSelect( false )
376 SetSizePixel( Size( 210, 200 ) );
378 FillBox();
381 LanguageBox::~LanguageBox()
383 disposeOnce();
386 void LanguageBox::dispose()
388 ClearBox();
389 DocListenerBox::dispose();
392 void LanguageBox::FillBox()
394 SetUpdateMode(false);
395 m_bIgnoreSelect = true;
396 m_sCurrentText = GetSelectEntry();
397 ClearBox();
399 boost::shared_ptr<LocalizationMgr> pCurMgr(GetShell()->GetCurLocalizationMgr());
400 if ( pCurMgr->isLibraryLocalized() )
402 Enable();
403 Locale aDefaultLocale = pCurMgr->getStringResourceManager()->getDefaultLocale();
404 Locale aCurrentLocale = pCurMgr->getStringResourceManager()->getCurrentLocale();
405 Sequence< Locale > aLocaleSeq = pCurMgr->getStringResourceManager()->getLocales();
406 const Locale* pLocale = aLocaleSeq.getConstArray();
407 sal_Int32 i, nCount = aLocaleSeq.getLength();
408 sal_Int32 nSelPos = LISTBOX_ENTRY_NOTFOUND;
409 for ( i = 0; i < nCount; ++i )
411 bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] );
412 bool bIsCurrent = localesAreEqual( aCurrentLocale, pLocale[i] );
413 LanguageType eLangType = LanguageTag::convertToLanguageType( pLocale[i] );
414 OUString sLanguage = SvtLanguageTable::GetLanguageString( eLangType );
415 if ( bIsDefault )
417 sLanguage += " ";
418 sLanguage += m_sDefaultLanguageStr;
420 sal_Int32 nPos = InsertEntry( sLanguage );
421 SetEntryData( nPos, new LanguageEntry( sLanguage, pLocale[i], bIsDefault ) );
423 if ( bIsCurrent )
424 nSelPos = nPos;
427 if ( nSelPos != LISTBOX_ENTRY_NOTFOUND )
429 SelectEntryPos( nSelPos );
430 m_sCurrentText = GetSelectEntry();
433 else
435 InsertEntry( m_sNotLocalizedStr );
436 SelectEntryPos(0);
437 Disable();
440 SetUpdateMode(true);
441 m_bIgnoreSelect = false;
444 void LanguageBox::ClearBox()
446 sal_Int32 nCount = GetEntryCount();
447 for ( sal_Int32 i = 0; i < nCount; ++i )
449 LanguageEntry* pEntry = static_cast<LanguageEntry*>(GetEntryData(i));
450 delete pEntry;
452 ListBox::Clear();
455 void LanguageBox::SetLanguage()
457 LanguageEntry* pEntry = static_cast<LanguageEntry*>(GetSelectEntryData());
458 if ( pEntry )
459 GetShell()->GetCurLocalizationMgr()->handleSetCurrentLocale( pEntry->m_aLocale );
462 void LanguageBox::Select()
464 if ( !m_bIgnoreSelect )
465 SetLanguage();
466 else
467 SelectEntry( m_sCurrentText ); // Select after Escape
470 bool LanguageBox::PreNotify( NotifyEvent& rNEvt )
472 bool nDone = false;
473 if( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
475 sal_uInt16 nKeyCode = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
476 switch( nKeyCode )
478 case KEY_RETURN:
480 SetLanguage();
481 nDone = true;
483 break;
485 case KEY_ESCAPE:
487 SelectEntry( m_sCurrentText );
488 nDone = true;
490 break;
493 else if( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
496 else if( rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS )
500 return nDone || ListBox::PreNotify( rNEvt );
503 void LanguageBox::Update( const SfxStringItem* pItem )
505 FillBox();
507 if ( pItem && !pItem->GetValue().isEmpty() )
509 m_sCurrentText = pItem->GetValue();
510 if ( GetSelectEntry() != m_sCurrentText )
511 SelectEntry( m_sCurrentText );
516 } // namespace basctl
518 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */