Version 6.1.0.2, tag libreoffice-6.1.0.2
[LibreOffice.git] / basctl / source / basicide / basicbox.cxx
blobdd5eb2f36fdf18a3d4f63236ea4ef519f013ec4b
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 (auto const& doc : aDocuments)
208 InsertEntries(doc, LIBRARY_LOCATION_DOCUMENT);
211 SetUpdateMode(true);
213 SelectEntry( aCurText );
214 if ( !GetSelectedEntryCount() )
216 SelectEntryPos( GetEntryCount() );
217 aCurText = GetSelectedEntry();
219 bIgnoreSelect = false;
222 void LibBox::InsertEntries( const ScriptDocument& rDocument, LibraryLocation eLocation )
224 // get a sorted list of library names
225 Sequence< OUString > aLibNames = rDocument.getLibraryNames();
226 sal_Int32 nLibCount = aLibNames.getLength();
227 const OUString* pLibNames = aLibNames.getConstArray();
229 for ( sal_Int32 i = 0 ; i < nLibCount ; ++i )
231 OUString aLibName = pLibNames[ i ];
232 if ( eLocation == rDocument.getLibraryLocation( aLibName ) )
234 OUString aName( rDocument.getTitle( eLocation ) );
235 OUString aEntryText( CreateMgrAndLibStr( aName, aLibName ) );
236 sal_Int32 nPos = InsertEntry( aEntryText );
237 SetEntryData( nPos, new LibEntry( rDocument, eLocation, aLibName ) );
242 bool LibBox::PreNotify( NotifyEvent& rNEvt )
244 bool bDone = false;
245 if( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
247 KeyEvent aKeyEvt = *rNEvt.GetKeyEvent();
248 sal_uInt16 nKeyCode = aKeyEvt.GetKeyCode().GetCode();
249 switch( nKeyCode )
251 case KEY_RETURN:
253 NotifyIDE();
254 bDone = true;
256 break;
258 case KEY_ESCAPE:
260 SelectEntry( aCurText );
261 ReleaseFocus();
262 bDone = true;
264 break;
267 else if( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
269 if ( bFillBox )
271 FillBox();
272 bFillBox = false;
275 else if( rNEvt.GetType() == MouseNotifyEvent::LOSEFOCUS )
277 if ( !HasChildPathFocus(true) )
279 bIgnoreSelect = true;
280 bFillBox = true;
284 return bDone || ListBox::PreNotify( rNEvt );
287 void LibBox::Select()
289 if ( !IsTravelSelect() )
291 if ( !bIgnoreSelect )
292 NotifyIDE();
293 else
294 SelectEntry( aCurText ); // since 306... (Select after Escape)
298 void LibBox::NotifyIDE()
300 sal_Int32 nSelPos = GetSelectedEntryPos();
301 if (LibEntry* pEntry = static_cast<LibEntry*>(GetEntryData(nSelPos)))
303 ScriptDocument aDocument( pEntry->GetDocument() );
304 SfxUnoAnyItem aDocumentItem( SID_BASICIDE_ARG_DOCUMENT_MODEL, uno::Any( aDocument.getDocumentOrNull() ) );
305 OUString aLibName = pEntry->GetLibName();
306 SfxStringItem aLibNameItem( SID_BASICIDE_ARG_LIBNAME, aLibName );
307 if (SfxDispatcher* pDispatcher = GetDispatcher())
308 pDispatcher->ExecuteList(
309 SID_BASICIDE_LIBSELECTED, SfxCallMode::SYNCHRON,
310 { &aDocumentItem, &aLibNameItem }
313 ReleaseFocus();
316 void LibBox::ClearBox()
318 sal_Int32 nCount = GetEntryCount();
319 for ( sal_Int32 i = 0; i < nCount; ++i )
321 LibEntry* pEntry = static_cast<LibEntry*>(GetEntryData( i ));
322 delete pEntry;
324 ListBox::Clear();
327 // class LanguageBoxControl ----------------------------------------------
329 SFX_IMPL_TOOLBOX_CONTROL( LanguageBoxControl, SfxStringItem );
331 LanguageBoxControl::LanguageBoxControl( sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx )
332 : SfxToolBoxControl( nSlotId, nId, rTbx )
336 void LanguageBoxControl::StateChanged( sal_uInt16, SfxItemState eState, const SfxPoolItem* pItem )
338 if (LanguageBox* pBox = static_cast<LanguageBox*>(GetToolBox().GetItemWindow(GetId())))
340 if (eState != SfxItemState::DEFAULT)
341 pBox->Disable();
342 else
344 pBox->Enable();
345 pBox->Update(dynamic_cast<SfxStringItem const*>(pItem));
350 VclPtr<vcl::Window> LanguageBoxControl::CreateItemWindow( vcl::Window *pParent )
352 return VclPtr<LanguageBox>::Create( pParent );
355 // class basctl::LanguageBox -----------------------------------------------
357 LanguageBox::LanguageBox( vcl::Window* pParent ) :
359 DocListenerBox( pParent ),
361 m_sNotLocalizedStr( IDEResId( RID_STR_TRANSLATION_NOTLOCALIZED ) ),
362 m_sDefaultLanguageStr( IDEResId( RID_STR_TRANSLATION_DEFAULT ) ),
364 m_bIgnoreSelect( false )
367 SetSizePixel( Size( 210, 200 ) );
369 FillBox();
372 LanguageBox::~LanguageBox()
374 disposeOnce();
377 void LanguageBox::dispose()
379 ClearBox();
380 DocListenerBox::dispose();
383 void LanguageBox::FillBox()
385 SetUpdateMode(false);
386 m_bIgnoreSelect = true;
387 m_sCurrentText = GetSelectedEntry();
388 ClearBox();
390 std::shared_ptr<LocalizationMgr> pCurMgr(GetShell()->GetCurLocalizationMgr());
391 if ( pCurMgr->isLibraryLocalized() )
393 Enable();
394 Locale aDefaultLocale = pCurMgr->getStringResourceManager()->getDefaultLocale();
395 Locale aCurrentLocale = pCurMgr->getStringResourceManager()->getCurrentLocale();
396 Sequence< Locale > aLocaleSeq = pCurMgr->getStringResourceManager()->getLocales();
397 const Locale* pLocale = aLocaleSeq.getConstArray();
398 sal_Int32 i, nCount = aLocaleSeq.getLength();
399 sal_Int32 nSelPos = LISTBOX_ENTRY_NOTFOUND;
400 for ( i = 0; i < nCount; ++i )
402 bool bIsDefault = localesAreEqual( aDefaultLocale, pLocale[i] );
403 bool bIsCurrent = localesAreEqual( aCurrentLocale, pLocale[i] );
404 LanguageType eLangType = LanguageTag::convertToLanguageType( pLocale[i] );
405 OUString sLanguage = SvtLanguageTable::GetLanguageString( eLangType );
406 if ( bIsDefault )
408 sLanguage += " " + m_sDefaultLanguageStr;
410 sal_Int32 nPos = InsertEntry( sLanguage );
411 SetEntryData( nPos, new LanguageEntry( pLocale[i], bIsDefault ) );
413 if ( bIsCurrent )
414 nSelPos = nPos;
417 if ( nSelPos != LISTBOX_ENTRY_NOTFOUND )
419 SelectEntryPos( nSelPos );
420 m_sCurrentText = GetSelectedEntry();
423 else
425 InsertEntry( m_sNotLocalizedStr );
426 SelectEntryPos(0);
427 Disable();
430 SetUpdateMode(true);
431 m_bIgnoreSelect = false;
434 void LanguageBox::ClearBox()
436 sal_Int32 nCount = GetEntryCount();
437 for ( sal_Int32 i = 0; i < nCount; ++i )
439 LanguageEntry* pEntry = static_cast<LanguageEntry*>(GetEntryData(i));
440 delete pEntry;
442 ListBox::Clear();
445 void LanguageBox::SetLanguage()
447 LanguageEntry* pEntry = static_cast<LanguageEntry*>(GetSelectedEntryData());
448 if ( pEntry )
449 GetShell()->GetCurLocalizationMgr()->handleSetCurrentLocale( pEntry->m_aLocale );
452 void LanguageBox::Select()
454 if ( !m_bIgnoreSelect )
455 SetLanguage();
456 else
457 SelectEntry( m_sCurrentText ); // Select after Escape
460 bool LanguageBox::PreNotify( NotifyEvent& rNEvt )
462 bool bDone = false;
463 if( rNEvt.GetType() == MouseNotifyEvent::KEYINPUT )
465 sal_uInt16 nKeyCode = rNEvt.GetKeyEvent()->GetKeyCode().GetCode();
466 switch( nKeyCode )
468 case KEY_RETURN:
470 SetLanguage();
471 bDone = true;
473 break;
475 case KEY_ESCAPE:
477 SelectEntry( m_sCurrentText );
478 bDone = true;
480 break;
484 return bDone || ListBox::PreNotify( rNEvt );
487 void LanguageBox::Update( const SfxStringItem* pItem )
489 FillBox();
491 if ( pItem && !pItem->GetValue().isEmpty() )
493 m_sCurrentText = pItem->GetValue();
494 if ( GetSelectedEntry() != m_sCurrentText )
495 SelectEntry( m_sCurrentText );
500 } // namespace basctl
502 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */