Bump version to 6.0-36
[LibreOffice.git] / basctl / source / basicide / basicbox.cxx
blob6adcdcc54c4df76eec30d3787b44ba1d489004a1
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 <strings.hrc>
21 #include <basidesh.hxx>
23 #include "basicbox.hxx"
24 #include <iderdll.hxx>
26 #include <localizationmgr.hxx>
27 #include <managelang.hxx>
29 #include <sfx2/dispatch.hxx>
30 #include <svtools/langtab.hxx>
32 namespace basctl
35 using namespace ::com::sun::star;
36 using namespace ::com::sun::star::lang;
37 using namespace ::com::sun::star::uno;
39 SFX_IMPL_TOOLBOX_CONTROL( LibBoxControl, SfxStringItem );
41 LibBoxControl::LibBoxControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx )
42 : SfxToolBoxControl( nSlotId, nId, rTbx )
47 void LibBoxControl::StateChanged( sal_uInt16, SfxItemState eState, const SfxPoolItem* pState )
49 LibBox* pBox = static_cast<LibBox*>(GetToolBox().GetItemWindow(GetId()));
51 DBG_ASSERT( pBox, "Box not found" );
52 if ( !pBox )
53 return;
55 if ( eState != SfxItemState::DEFAULT )
56 pBox->Disable();
57 else
59 pBox->Enable();
60 pBox->Update(dynamic_cast<SfxStringItem const*>(pState));
65 VclPtr<vcl::Window> LibBoxControl::CreateItemWindow( vcl::Window *pParent )
67 return VclPtr<LibBox>::Create( pParent );
71 DocListenerBox::DocListenerBox( vcl::Window* pParent )
72 :ListBox( pParent, WinBits( WB_BORDER | WB_DROPDOWN ) )
73 ,m_aNotifier( *this )
77 DocListenerBox::~DocListenerBox()
79 disposeOnce();
82 void DocListenerBox::dispose()
84 m_aNotifier.dispose();
85 ListBox::dispose();
88 void DocListenerBox::onDocumentCreated( const ScriptDocument& /*_rDocument*/ )
90 FillBox();
93 void DocListenerBox::onDocumentOpened( const ScriptDocument& /*_rDocument*/ )
95 FillBox();
98 void DocListenerBox::onDocumentSave( const ScriptDocument& /*_rDocument*/ )
100 // not interested in
103 void DocListenerBox::onDocumentSaveDone( const ScriptDocument& /*_rDocument*/ )
105 // not interested in
108 void DocListenerBox::onDocumentSaveAs( const ScriptDocument& /*_rDocument*/ )
110 // not interested in
113 void DocListenerBox::onDocumentSaveAsDone( const ScriptDocument& /*_rDocument*/ )
115 FillBox();
118 void DocListenerBox::onDocumentClosed( const ScriptDocument& /*_rDocument*/ )
120 FillBox();
123 void DocListenerBox::onDocumentTitleChanged( const ScriptDocument& /*_rDocument*/ )
125 // not interested in
128 void DocListenerBox::onDocumentModeChanged( const ScriptDocument& /*_rDocument*/ )
130 // not interested in
133 LibBox::LibBox( vcl::Window* pParent ) :
134 DocListenerBox( pParent )
136 FillBox();
137 bIgnoreSelect = true; // do not yet transfer select of 0
138 bFillBox = true;
139 SelectEntryPos( 0 );
140 aCurText = GetEntry( 0 );
141 SetSizePixel( Size( 250, 200 ) );
142 bIgnoreSelect = false;
146 LibBox::~LibBox()
148 disposeOnce();
151 void LibBox::dispose()
153 ClearBox();
154 DocListenerBox::dispose();
157 void LibBox::Update( const SfxStringItem* pItem )
160 // if ( !pItem || !pItem->GetValue().Len() )
161 FillBox();
163 if ( pItem )
165 aCurText = pItem->GetValue();
166 if ( aCurText.isEmpty() )
167 aCurText = IDEResId(RID_STR_ALL);
170 if ( GetSelectedEntry() != aCurText )
171 SelectEntry( aCurText );
174 void LibBox::ReleaseFocus()
176 SfxViewShell* pCurSh = SfxViewShell::Current();
177 DBG_ASSERT( pCurSh, "Current ViewShell not found!" );
179 if ( pCurSh )
181 vcl::Window* pShellWin = pCurSh->GetWindow();
182 if ( !pShellWin )
183 pShellWin = Application::GetDefDialogParent();
185 pShellWin->GrabFocus();
189 void LibBox::FillBox()
191 SetUpdateMode(false);
192 bIgnoreSelect = true;
194 aCurText = GetSelectedEntry();
196 SelectEntryPos( 0 );
197 ClearBox();
199 // create list box entries
200 sal_Int32 nPos = InsertEntry(IDEResId(RID_STR_ALL));
201 SetEntryData( nPos, new LibEntry( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_UNKNOWN, OUString() ) );
202 InsertEntries( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER );
203 InsertEntries( ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE );
205 ScriptDocuments aDocuments( ScriptDocument::getAllScriptDocuments( ScriptDocument::DocumentsSorted ) );
206 for ( ScriptDocuments::const_iterator doc = aDocuments.begin();
207 doc != aDocuments.end();
208 ++doc
211 InsertEntries( *doc, LIBRARY_LOCATION_DOCUMENT );
214 SetUpdateMode(true);
216 SelectEntry( aCurText );
217 if ( !GetSelectedEntryCount() )
219 SelectEntryPos( GetEntryCount() );
220 aCurText = GetSelectedEntry();
222 bIgnoreSelect = false;
225 void LibBox::InsertEntries( const ScriptDocument& rDocument, LibraryLocation eLocation )
227 // get a sorted list of library names
228 Sequence< OUString > aLibNames = rDocument.getLibraryNames();
229 sal_Int32 nLibCount = aLibNames.getLength();
230 const OUString* pLibNames = aLibNames.getConstArray();
232 for ( sal_Int32 i = 0 ; i < nLibCount ; ++i )
234 OUString aLibName = pLibNames[ i ];
235 if ( eLocation == rDocument.getLibraryLocation( aLibName ) )
237 OUString aName( rDocument.getTitle( eLocation ) );
238 OUString aEntryText( CreateMgrAndLibStr( aName, aLibName ) );
239 sal_Int32 nPos = InsertEntry( aEntryText );
240 SetEntryData( nPos, new LibEntry( rDocument, eLocation, aLibName ) );
245 bool LibBox::PreNotify( NotifyEvent& rNEvt )
247 bool bDone = false;
248 if( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
250 KeyEvent aKeyEvt = *rNEvt.GetKeyEvent();
251 sal_uInt16 nKeyCode = aKeyEvt.GetKeyCode().GetCode();
252 switch( nKeyCode )
254 case KEY_RETURN:
256 NotifyIDE();
257 bDone = true;
259 break;
261 case KEY_ESCAPE:
263 SelectEntry( aCurText );
264 ReleaseFocus();
265 bDone = true;
267 break;
270 else if( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
272 if ( bFillBox )
274 FillBox();
275 bFillBox = false;
278 else if( rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS )
280 if ( !HasChildPathFocus(true) )
282 bIgnoreSelect = true;
283 bFillBox = true;
287 return bDone || ListBox::PreNotify( rNEvt );
290 void LibBox::Select()
292 if ( !IsTravelSelect() )
294 if ( !bIgnoreSelect )
295 NotifyIDE();
296 else
297 SelectEntry( aCurText ); // since 306... (Select after Escape)
301 void LibBox::NotifyIDE()
303 sal_Int32 nSelPos = GetSelectedEntryPos();
304 if (LibEntry* pEntry = static_cast<LibEntry*>(GetEntryData(nSelPos)))
306 ScriptDocument aDocument( pEntry->GetDocument() );
307 SfxUsrAnyItem aDocumentItem( SID_BASICIDE_ARG_DOCUMENT_MODEL, uno::Any( aDocument.getDocumentOrNull() ) );
308 OUString aLibName = pEntry->GetLibName();
309 SfxStringItem aLibNameItem( SID_BASICIDE_ARG_LIBNAME, aLibName );
310 if (SfxDispatcher* pDispatcher = GetDispatcher())
311 pDispatcher->ExecuteList(
312 SID_BASICIDE_LIBSELECTED, SfxCallMode::SYNCHRON,
313 { &aDocumentItem, &aLibNameItem }
316 ReleaseFocus();
319 void LibBox::ClearBox()
321 sal_Int32 nCount = GetEntryCount();
322 for ( sal_Int32 i = 0; i < nCount; ++i )
324 LibEntry* pEntry = static_cast<LibEntry*>(GetEntryData( i ));
325 delete pEntry;
327 ListBox::Clear();
330 // class LanguageBoxControl ----------------------------------------------
332 SFX_IMPL_TOOLBOX_CONTROL( LanguageBoxControl, SfxStringItem );
334 LanguageBoxControl::LanguageBoxControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx )
335 : SfxToolBoxControl( nSlotId, nId, rTbx )
339 void LanguageBoxControl::StateChanged( sal_uInt16, SfxItemState eState, const SfxPoolItem* pItem )
341 if (LanguageBox* pBox = static_cast<LanguageBox*>(GetToolBox().GetItemWindow(GetId())))
343 if (eState != SfxItemState::DEFAULT)
344 pBox->Disable();
345 else
347 pBox->Enable();
348 pBox->Update(dynamic_cast<SfxStringItem const*>(pItem));
353 VclPtr<vcl::Window> LanguageBoxControl::CreateItemWindow( vcl::Window *pParent )
355 return VclPtr<LanguageBox>::Create( pParent );
358 // class basctl::LanguageBox -----------------------------------------------
360 LanguageBox::LanguageBox( vcl::Window* pParent ) :
362 DocListenerBox( pParent ),
364 m_sNotLocalizedStr( IDEResId( RID_STR_TRANSLATION_NOTLOCALIZED ) ),
365 m_sDefaultLanguageStr( IDEResId( RID_STR_TRANSLATION_DEFAULT ) ),
367 m_bIgnoreSelect( false )
370 SetSizePixel( Size( 210, 200 ) );
372 FillBox();
375 LanguageBox::~LanguageBox()
377 disposeOnce();
380 void LanguageBox::dispose()
382 ClearBox();
383 DocListenerBox::dispose();
386 void LanguageBox::FillBox()
388 SetUpdateMode(false);
389 m_bIgnoreSelect = true;
390 m_sCurrentText = GetSelectedEntry();
391 ClearBox();
393 std::shared_ptr<LocalizationMgr> pCurMgr(GetShell()->GetCurLocalizationMgr());
394 if ( pCurMgr->isLibraryLocalized() )
396 Enable();
397 Locale aDefaultLocale = pCurMgr->getStringResourceManager()->getDefaultLocale();
398 Locale aCurrentLocale = pCurMgr->getStringResourceManager()->getCurrentLocale();
399 Sequence< Locale > aLocaleSeq = pCurMgr->getStringResourceManager()->getLocales();
400 const Locale* pLocale = aLocaleSeq.getConstArray();
401 sal_Int32 i, nCount = aLocaleSeq.getLength();
402 sal_Int32 nSelPos = LISTBOX_ENTRY_NOTFOUND;
403 for ( i = 0; i < nCount; ++i )
405 bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] );
406 bool bIsCurrent = localesAreEqual( aCurrentLocale, pLocale[i] );
407 LanguageType eLangType = LanguageTag::convertToLanguageType( pLocale[i] );
408 OUString sLanguage = SvtLanguageTable::GetLanguageString( eLangType );
409 if ( bIsDefault )
411 sLanguage += " ";
412 sLanguage += m_sDefaultLanguageStr;
414 sal_Int32 nPos = InsertEntry( sLanguage );
415 SetEntryData( nPos, new LanguageEntry( pLocale[i], bIsDefault ) );
417 if ( bIsCurrent )
418 nSelPos = nPos;
421 if ( nSelPos != LISTBOX_ENTRY_NOTFOUND )
423 SelectEntryPos( nSelPos );
424 m_sCurrentText = GetSelectedEntry();
427 else
429 InsertEntry( m_sNotLocalizedStr );
430 SelectEntryPos(0);
431 Disable();
434 SetUpdateMode(true);
435 m_bIgnoreSelect = false;
438 void LanguageBox::ClearBox()
440 sal_Int32 nCount = GetEntryCount();
441 for ( sal_Int32 i = 0; i < nCount; ++i )
443 LanguageEntry* pEntry = static_cast<LanguageEntry*>(GetEntryData(i));
444 delete pEntry;
446 ListBox::Clear();
449 void LanguageBox::SetLanguage()
451 LanguageEntry* pEntry = static_cast<LanguageEntry*>(GetSelectedEntryData());
452 if ( pEntry )
453 GetShell()->GetCurLocalizationMgr()->handleSetCurrentLocale( pEntry->m_aLocale );
456 void LanguageBox::Select()
458 if ( !m_bIgnoreSelect )
459 SetLanguage();
460 else
461 SelectEntry( m_sCurrentText ); // Select after Escape
464 bool LanguageBox::PreNotify( NotifyEvent& rNEvt )
466 bool bDone = false;
467 if( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
469 sal_uInt16 nKeyCode = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
470 switch( nKeyCode )
472 case KEY_RETURN:
474 SetLanguage();
475 bDone = true;
477 break;
479 case KEY_ESCAPE:
481 SelectEntry( m_sCurrentText );
482 bDone = true;
484 break;
487 else if( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
490 else if( rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS )
494 return bDone || ListBox::PreNotify( rNEvt );
497 void LanguageBox::Update( const SfxStringItem* pItem )
499 FillBox();
501 if ( pItem && !pItem->GetValue().isEmpty() )
503 m_sCurrentText = pItem->GetValue();
504 if ( GetSelectedEntry() != m_sCurrentText )
505 SelectEntry( m_sCurrentText );
510 } // namespace basctl
512 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */