1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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>
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" );
55 if ( eState
!= SfxItemState::DEFAULT
)
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
) )
77 DocListenerBox::~DocListenerBox()
82 void DocListenerBox::dispose()
84 m_aNotifier
.dispose();
88 void DocListenerBox::onDocumentCreated( const ScriptDocument
& /*_rDocument*/ )
93 void DocListenerBox::onDocumentOpened( const ScriptDocument
& /*_rDocument*/ )
98 void DocListenerBox::onDocumentSave( const ScriptDocument
& /*_rDocument*/ )
103 void DocListenerBox::onDocumentSaveDone( const ScriptDocument
& /*_rDocument*/ )
108 void DocListenerBox::onDocumentSaveAs( const ScriptDocument
& /*_rDocument*/ )
113 void DocListenerBox::onDocumentSaveAsDone( const ScriptDocument
& /*_rDocument*/ )
118 void DocListenerBox::onDocumentClosed( const ScriptDocument
& /*_rDocument*/ )
123 void DocListenerBox::onDocumentTitleChanged( const ScriptDocument
& /*_rDocument*/ )
128 void DocListenerBox::onDocumentModeChanged( const ScriptDocument
& /*_rDocument*/ )
133 LibBox::LibBox( vcl::Window
* pParent
) :
134 DocListenerBox( pParent
)
137 bIgnoreSelect
= true; // do not yet transfer select of 0
140 aCurText
= GetEntry( 0 );
141 SetSizePixel( Size( 250, 200 ) );
142 bIgnoreSelect
= false;
151 void LibBox::dispose()
154 DocListenerBox::dispose();
157 void LibBox::Update( const SfxStringItem
* pItem
)
160 // if ( !pItem || !pItem->GetValue().Len() )
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!" );
181 vcl::Window
* pShellWin
= pCurSh
->GetWindow();
183 pShellWin
= Application::GetDefDialogParent();
185 pShellWin
->GrabFocus();
189 void LibBox::FillBox()
191 SetUpdateMode(false);
192 bIgnoreSelect
= true;
194 aCurText
= GetSelectedEntry();
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
);
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
)
245 if( rNEvt
.GetType() == MouseNotifyEvent::KEYINPUT
)
247 KeyEvent aKeyEvt
= *rNEvt
.GetKeyEvent();
248 sal_uInt16 nKeyCode
= aKeyEvt
.GetKeyCode().GetCode();
260 SelectEntry( aCurText
);
267 else if( rNEvt
.GetType() == MouseNotifyEvent::GETFOCUS
)
275 else if( rNEvt
.GetType() == MouseNotifyEvent::LOSEFOCUS
)
277 if ( !HasChildPathFocus(true) )
279 bIgnoreSelect
= true;
284 return bDone
|| ListBox::PreNotify( rNEvt
);
287 void LibBox::Select()
289 if ( !IsTravelSelect() )
291 if ( !bIgnoreSelect
)
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
}
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
));
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
)
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 ) );
372 LanguageBox::~LanguageBox()
377 void LanguageBox::dispose()
380 DocListenerBox::dispose();
383 void LanguageBox::FillBox()
385 SetUpdateMode(false);
386 m_bIgnoreSelect
= true;
387 m_sCurrentText
= GetSelectedEntry();
390 std::shared_ptr
<LocalizationMgr
> pCurMgr(GetShell()->GetCurLocalizationMgr());
391 if ( pCurMgr
->isLibraryLocalized() )
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
);
408 sLanguage
+= " " + m_sDefaultLanguageStr
;
410 sal_Int32 nPos
= InsertEntry( sLanguage
);
411 SetEntryData( nPos
, new LanguageEntry( pLocale
[i
], bIsDefault
) );
417 if ( nSelPos
!= LISTBOX_ENTRY_NOTFOUND
)
419 SelectEntryPos( nSelPos
);
420 m_sCurrentText
= GetSelectedEntry();
425 InsertEntry( m_sNotLocalizedStr
);
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
));
445 void LanguageBox::SetLanguage()
447 LanguageEntry
* pEntry
= static_cast<LanguageEntry
*>(GetSelectedEntryData());
449 GetShell()->GetCurLocalizationMgr()->handleSetCurrentLocale( pEntry
->m_aLocale
);
452 void LanguageBox::Select()
454 if ( !m_bIgnoreSelect
)
457 SelectEntry( m_sCurrentText
); // Select after Escape
460 bool LanguageBox::PreNotify( NotifyEvent
& rNEvt
)
463 if( rNEvt
.GetType() == MouseNotifyEvent::KEYINPUT
)
465 sal_uInt16 nKeyCode
= rNEvt
.GetKeyEvent()->GetKeyCode().GetCode();
477 SelectEntry( m_sCurrentText
);
484 return bDone
|| ListBox::PreNotify( rNEvt
);
487 void LanguageBox::Update( const SfxStringItem
* pItem
)
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: */