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 <IDEComboBox.hxx>
24 #include <iderdll.hxx>
26 #include <localizationmgr.hxx>
27 #include <managelang.hxx>
29 #include <sfx2/dispatch.hxx>
30 #include <sfx2/frame.hxx>
31 #include <sfx2/sfxsids.hrc>
32 #include <svtools/langtab.hxx>
33 #include <tools/debug.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/toolbox.hxx>
36 #include <vcl/event.hxx>
37 #include <svl/itemset.hxx>
41 using namespace ::com::sun::star
;
42 using namespace ::com::sun::star::lang
;
43 using namespace ::com::sun::star::uno
;
45 /*! Macro for implementation two methods for LibBoxControl Class
48 * SfxToolBoxControl* LibBoxControl::CreateImpl(sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx)
50 * return new LibBoxControl(nSlotId, nId, rTbx);
53 * void LibBoxControl::RegisterControl(sal_uInt16 nSlotId, SfxModule* pMod)
55 * SfxToolBoxControl::RegisterToolBoxControl(
56 * pMod, SfxTbxCtrlFactory(* LibBoxControl::CreateImpl, typeid(nItemClass), nSlotId));
59 * @see Macro SFX_DECL_TOOLBOX_CONTROL
61 SFX_IMPL_TOOLBOX_CONTROL(LibBoxControl
, SfxStringItem
);
63 LibBoxControl::LibBoxControl(sal_uInt16 nSlotId
, ToolBoxItemId nId
, ToolBox
& rTbx
)
64 : SfxToolBoxControl(nSlotId
, nId
, rTbx
)
68 void LibBoxControl::StateChangedAtToolBoxControl(sal_uInt16
, SfxItemState eState
,
69 const SfxPoolItem
* pState
)
71 LibBox
* pBox
= static_cast<LibBox
*>(GetToolBox().GetItemWindow(GetId()));
73 DBG_ASSERT(pBox
, "Box not found");
77 if (eState
!= SfxItemState::DEFAULT
)
78 pBox
->set_sensitive(false);
81 pBox
->set_sensitive(true);
82 pBox
->Update(dynamic_cast<SfxStringItem
const*>(pState
));
86 VclPtr
<InterimItemWindow
> LibBoxControl::CreateItemWindow(vcl::Window
* pParent
)
88 return VclPtr
<LibBox
>::Create(pParent
);
91 DocListenerBox::DocListenerBox(vcl::Window
* pParent
)
92 : InterimItemWindow(pParent
, "modules/BasicIDE/ui/combobox.ui", "ComboBox")
93 , m_xWidget(m_xBuilder
->weld_combo_box("combobox"))
96 InitControlBase(m_xWidget
.get());
98 m_xWidget
->connect_changed(LINK(this, DocListenerBox
, SelectHdl
));
99 m_xWidget
->connect_key_press(LINK(this, DocListenerBox
, KeyInputHdl
));
102 void DocListenerBox::set_sensitive(bool bSensitive
)
105 m_xWidget
->set_sensitive(bSensitive
);
108 IMPL_LINK(DocListenerBox
, KeyInputHdl
, const KeyEvent
&, rKEvt
, bool)
110 return HandleKeyInput(rKEvt
);
113 bool DocListenerBox::HandleKeyInput(const KeyEvent
& rKEvt
) { return ChildKeyInput(rKEvt
); }
115 IMPL_LINK_NOARG(DocListenerBox
, SelectHdl
, weld::ComboBox
&, void) { Select(); }
117 DocListenerBox::~DocListenerBox() { disposeOnce(); }
119 void DocListenerBox::dispose()
121 maNotifier
.dispose();
123 InterimItemWindow::dispose();
126 /// Only calls FillBox(). Parameter is not used.
127 void DocListenerBox::onDocumentCreated(const ScriptDocument
& /*_rDoc*/) { FillBox(); }
129 /// Only calls FillBox(). Parameter is not used.
130 void DocListenerBox::onDocumentOpened(const ScriptDocument
& /*_rDoc*/) { FillBox(); }
132 /// Only calls FillBox(). Parameter is not used.
133 void DocListenerBox::onDocumentSaveAsDone(const ScriptDocument
& /*_rDoc*/) { FillBox(); }
135 /// Only calls FillBox(). Parameter is not used.
136 void DocListenerBox::onDocumentClosed(const ScriptDocument
& /*_rDoc*/) { FillBox(); }
138 /// Not interested in. Do nothing.
139 void DocListenerBox::onDocumentSave(const ScriptDocument
& /*_rDoc*/) {}
141 /// Not interested in. Do nothing.
142 void DocListenerBox::onDocumentSaveDone(const ScriptDocument
& /*_rDoc*/) {}
144 /// Not interested in. Do nothing.
145 void DocListenerBox::onDocumentSaveAs(const ScriptDocument
& /*_rDoc*/) {}
147 /// Not interested in. Do nothing.
148 void DocListenerBox::onDocumentTitleChanged(const ScriptDocument
& /*_rDoc*/) {}
150 /// Not interested in. Do nothing.
151 void DocListenerBox::onDocumentModeChanged(const ScriptDocument
& /*_rDoc*/) {}
153 LibBox::LibBox(vcl::Window
* pParent
)
154 : DocListenerBox(pParent
)
157 mbIgnoreSelect
= true; // do not yet transfer select of 0
159 m_xWidget
->set_active(0);
160 maCurrentText
= m_xWidget
->get_text(0);
161 mbIgnoreSelect
= false;
163 m_xWidget
->connect_focus_in(LINK(this, LibBox
, FocusInHdl
));
164 m_xWidget
->connect_focus_out(LINK(this, LibBox
, FocusOutHdl
));
166 SetSizePixel(m_xWidget
->get_preferred_size());
169 LibBox::~LibBox() { disposeOnce(); }
171 void LibBox::dispose()
174 DocListenerBox::dispose();
177 void LibBox::Update(const SfxStringItem
* pItem
)
179 // if ( !pItem || !pItem->GetValue().Len() )
184 maCurrentText
= pItem
->GetValue();
185 if (maCurrentText
.isEmpty())
186 maCurrentText
= IDEResId(RID_STR_ALL
);
189 if (m_xWidget
->get_active_text() != maCurrentText
)
190 m_xWidget
->set_active_text(maCurrentText
);
193 void LibBox::ReleaseFocus()
195 SfxViewShell
* pCurSh
= SfxViewShell::Current();
196 DBG_ASSERT(pCurSh
, "Current ViewShell not found!");
201 vcl::Window
* pShellWin
= pCurSh
->GetWindow();
204 pShellWin
->GrabFocus();
208 weld::Window
* pWin
= Application::GetDefDialogParent();
214 void LibBox::FillBox()
217 mbIgnoreSelect
= true;
219 maCurrentText
= m_xWidget
->get_active_text();
223 // create list box entries
224 LibEntry
* pEntry
= new LibEntry(ScriptDocument::getApplicationScriptDocument(),
225 LIBRARY_LOCATION_UNKNOWN
, OUString());
226 OUString
sId(weld::toId(pEntry
));
227 m_xWidget
->append(sId
, IDEResId(RID_STR_ALL
));
229 InsertEntries(ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER
);
230 InsertEntries(ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE
);
232 ScriptDocuments
aDocuments(
233 ScriptDocument::getAllScriptDocuments(ScriptDocument::DocumentsSorted
));
234 for (auto const& doc
: aDocuments
)
236 InsertEntries(doc
, LIBRARY_LOCATION_DOCUMENT
);
241 int nIndex
= m_xWidget
->find_text(maCurrentText
);
243 m_xWidget
->set_active(nIndex
);
245 m_xWidget
->set_active(0);
246 maCurrentText
= m_xWidget
->get_active_text();
247 mbIgnoreSelect
= false;
250 void LibBox::InsertEntries(const ScriptDocument
& rDocument
, LibraryLocation eLocation
)
252 // get a sorted list of library names
253 Sequence
<OUString
> aLibNames
= rDocument
.getLibraryNames();
254 sal_Int32 nLibCount
= aLibNames
.getLength();
255 const OUString
* pLibNames
= aLibNames
.getConstArray();
257 for (sal_Int32 i
= 0; i
< nLibCount
; ++i
)
259 OUString aLibName
= pLibNames
[i
];
260 if (eLocation
== rDocument
.getLibraryLocation(aLibName
))
262 OUString
aName(rDocument
.getTitle(eLocation
));
263 OUString
aEntryText(CreateMgrAndLibStr(aName
, aLibName
));
264 LibEntry
* pEntry
= new LibEntry(rDocument
, eLocation
, aLibName
);
265 m_xWidget
->append(weld::toId(pEntry
), aEntryText
);
270 bool LibBox::HandleKeyInput(const KeyEvent
& rKEvt
)
274 sal_uInt16 nKeyCode
= rKEvt
.GetKeyCode().GetCode();
285 m_xWidget
->set_active_text(maCurrentText
);
292 return bDone
|| DocListenerBox::HandleKeyInput(rKEvt
);
295 IMPL_LINK_NOARG(LibBox
, FocusInHdl
, weld::Widget
&, void)
304 IMPL_LINK_NOARG(LibBox
, FocusOutHdl
, weld::Widget
&, void)
306 // comboboxes can be comprised of multiple widgets, ensure all have lost focus
307 if (m_xWidget
&& !m_xWidget
->has_focus())
311 void LibBox::Select()
313 if (m_xWidget
->changed_by_direct_pick())
318 m_xWidget
->set_active_text(maCurrentText
); // (Select after Escape)
322 void LibBox::NotifyIDE()
324 LibEntry
* pEntry
= weld::fromId
<LibEntry
*>(m_xWidget
->get_active_id());
327 const ScriptDocument
& aDocument(pEntry
->GetDocument());
328 SfxUnoAnyItem
aDocumentItem(SID_BASICIDE_ARG_DOCUMENT_MODEL
,
329 uno::Any(aDocument
.getDocumentOrNull()));
330 const OUString
& aLibName
= pEntry
->GetLibName();
331 SfxStringItem
aLibNameItem(SID_BASICIDE_ARG_LIBNAME
, aLibName
);
332 if (SfxDispatcher
* pDispatcher
= GetDispatcher())
333 pDispatcher
->ExecuteList(SID_BASICIDE_LIBSELECTED
, SfxCallMode::SYNCHRON
,
334 { &aDocumentItem
, &aLibNameItem
});
339 void LibBox::ClearBox()
341 sal_Int32 nCount
= m_xWidget
->get_count();
342 for (sal_Int32 i
= 0; i
< nCount
; ++i
)
344 LibEntry
* pEntry
= weld::fromId
<LibEntry
*>(m_xWidget
->get_id(i
));
350 // class LanguageBoxControl ----------------------------------------------
352 /*! Macro for implementation two methods for LanguageBoxControl Class
355 * SfxToolBoxControl* LanguageBoxControl::CreateImpl(sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx)
357 * return new LanguageBoxControl(nSlotId, nId, rTbx);
360 * void LanguageBoxControl::RegisterControl(sal_uInt16 nSlotId, SfxModule* pMod)
362 * SfxToolBoxControl::RegisterToolBoxControl(
363 * pMod, SfxTbxCtrlFactory(* LanguageBoxControl::CreateImpl, typeid(nItemClass), nSlotId));
366 * @see Macro SFX_DECL_TOOLBOX_CONTROL
368 SFX_IMPL_TOOLBOX_CONTROL(LanguageBoxControl
, SfxStringItem
);
370 LanguageBoxControl::LanguageBoxControl(sal_uInt16 nSlotId
, ToolBoxItemId nId
, ToolBox
& rTbx
)
371 : SfxToolBoxControl(nSlotId
, nId
, rTbx
)
375 void LanguageBoxControl::StateChangedAtToolBoxControl(sal_uInt16
, SfxItemState eState
,
376 const SfxPoolItem
* pItem
)
378 if (LanguageBox
* pBox
= static_cast<LanguageBox
*>(GetToolBox().GetItemWindow(GetId())))
380 if (eState
!= SfxItemState::DEFAULT
)
381 pBox
->set_sensitive(false);
384 pBox
->set_sensitive(true);
385 pBox
->Update(dynamic_cast<SfxStringItem
const*>(pItem
));
390 VclPtr
<InterimItemWindow
> LanguageBoxControl::CreateItemWindow(vcl::Window
* pParent
)
392 return VclPtr
<LanguageBox
>::Create(pParent
);
395 // class basctl::LanguageBox -----------------------------------------------
396 LanguageBox::LanguageBox(vcl::Window
* pParent
)
397 : DocListenerBox(pParent
)
398 , msNotLocalizedStr(IDEResId(RID_STR_TRANSLATION_NOTLOCALIZED
))
399 , msDefaultLanguageStr(IDEResId(RID_STR_TRANSLATION_DEFAULT
))
400 , mbIgnoreSelect(false)
404 SetSizePixel(m_xWidget
->get_preferred_size());
407 LanguageBox::~LanguageBox() { disposeOnce(); }
409 void LanguageBox::dispose()
412 DocListenerBox::dispose();
415 void LanguageBox::FillBox()
418 mbIgnoreSelect
= true;
419 msCurrentText
= m_xWidget
->get_active_text();
422 sal_Int32 nSelPos
= -1;
424 std::shared_ptr
<LocalizationMgr
> pCurMgr(GetShell()->GetCurLocalizationMgr());
425 if (pCurMgr
->isLibraryLocalized())
428 Locale aDefaultLocale
= pCurMgr
->getStringResourceManager()->getDefaultLocale();
429 Locale aCurrentLocale
= pCurMgr
->getStringResourceManager()->getCurrentLocale();
430 Sequence
<Locale
> aLocaleSeq
= pCurMgr
->getStringResourceManager()->getLocales();
431 const Locale
* pLocale
= aLocaleSeq
.getConstArray();
432 sal_Int32 i
, nCount
= aLocaleSeq
.getLength();
433 for (i
= 0; i
< nCount
; ++i
)
435 bool bIsDefault
= localesAreEqual(aDefaultLocale
, pLocale
[i
]);
436 bool bIsCurrent
= localesAreEqual(aCurrentLocale
, pLocale
[i
]);
437 LanguageType eLangType
= LanguageTag::convertToLanguageType(pLocale
[i
]);
438 OUString sLanguage
= SvtLanguageTable::GetLanguageString(eLangType
);
441 sLanguage
+= " " + msDefaultLanguageStr
;
443 LanguageEntry
* pEntry
= new LanguageEntry(pLocale
[i
], bIsDefault
);
444 OUString
sId(weld::toId(pEntry
));
445 m_xWidget
->append(sId
, sLanguage
);
452 msCurrentText
= m_xWidget
->get_text(nSelPos
);
456 m_xWidget
->append_text(msNotLocalizedStr
);
458 set_sensitive(false);
462 m_xWidget
->set_active(nSelPos
);
463 mbIgnoreSelect
= false;
466 void LanguageBox::ClearBox()
468 sal_Int32 nCount
= m_xWidget
->get_count();
469 for (sal_Int32 i
= 0; i
< nCount
; ++i
)
471 LanguageEntry
* pEntry
= weld::fromId
<LanguageEntry
*>(m_xWidget
->get_id(i
));
477 void LanguageBox::SetLanguage()
479 LanguageEntry
* pEntry
= weld::fromId
<LanguageEntry
*>(m_xWidget
->get_active_id());
481 GetShell()->GetCurLocalizationMgr()->handleSetCurrentLocale(pEntry
->m_aLocale
);
484 void LanguageBox::Select()
489 m_xWidget
->set_active_text(msCurrentText
); // Select after Escape
492 bool LanguageBox::HandleKeyInput(const KeyEvent
& rKEvt
)
496 sal_uInt16 nKeyCode
= rKEvt
.GetKeyCode().GetCode();
507 m_xWidget
->set_active_text(msCurrentText
);
513 return bDone
|| DocListenerBox::HandleKeyInput(rKEvt
);
516 void LanguageBox::Update(const SfxStringItem
* pItem
)
520 if (pItem
&& !pItem
->GetValue().isEmpty())
522 msCurrentText
= pItem
->GetValue();
523 if (m_xWidget
->get_active_text() != msCurrentText
)
524 m_xWidget
->set_active_text(msCurrentText
);
528 } // namespace basctl
530 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */