Bump version to 24.04.3.4
[LibreOffice.git] / basctl / source / basicide / IDEComboBox.cxx
blobd25e143d62fad3d6a93a2d3788012767e86d055d
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>
22 #include <basobj.hxx>
23 #include <IDEComboBox.hxx>
24 #include <iderdll.hxx>
25 #include <iderid.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>
39 namespace basctl
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
47 * @code
48 * SfxToolBoxControl* LibBoxControl::CreateImpl(sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx)
49 * {
50 * return new LibBoxControl(nSlotId, nId, rTbx);
51 * }
53 * void LibBoxControl::RegisterControl(sal_uInt16 nSlotId, SfxModule* pMod)
54 * {
55 * SfxToolBoxControl::RegisterToolBoxControl(
56 * pMod, SfxTbxCtrlFactory(* LibBoxControl::CreateImpl, typeid(nItemClass), nSlotId));
57 * }
58 * @endcode
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");
74 if (!pBox)
75 return;
77 if (eState != SfxItemState::DEFAULT)
78 pBox->set_sensitive(false);
79 else
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"))
94 , maNotifier(*this)
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)
104 Enable(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();
122 m_xWidget.reset();
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)
156 FillBox();
157 mbIgnoreSelect = true; // do not yet transfer select of 0
158 mbFillBox = true;
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()
173 ClearBox();
174 DocListenerBox::dispose();
177 void LibBox::Update(const SfxStringItem* pItem)
179 // if ( !pItem || !pItem->GetValue().Len() )
180 FillBox();
182 if (pItem)
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!");
198 if (!pCurSh)
199 return;
201 vcl::Window* pShellWin = pCurSh->GetWindow();
202 if (pShellWin)
204 pShellWin->GrabFocus();
205 return;
208 weld::Window* pWin = Application::GetDefDialogParent();
209 if (!pWin)
210 return;
211 pWin->grab_focus();
214 void LibBox::FillBox()
216 m_xWidget->freeze();
217 mbIgnoreSelect = true;
219 maCurrentText = m_xWidget->get_active_text();
221 ClearBox();
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);
239 m_xWidget->thaw();
241 int nIndex = m_xWidget->find_text(maCurrentText);
242 if (nIndex != -1)
243 m_xWidget->set_active(nIndex);
244 else
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)
272 bool bDone = false;
274 sal_uInt16 nKeyCode = rKEvt.GetKeyCode().GetCode();
275 switch (nKeyCode)
277 case KEY_RETURN:
279 NotifyIDE();
280 bDone = true;
282 break;
283 case KEY_ESCAPE:
285 m_xWidget->set_active_text(maCurrentText);
286 ReleaseFocus();
287 bDone = true;
289 break;
292 return bDone || DocListenerBox::HandleKeyInput(rKEvt);
295 IMPL_LINK_NOARG(LibBox, FocusInHdl, weld::Widget&, void)
297 if (mbFillBox)
299 FillBox();
300 mbFillBox = false;
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())
308 mbFillBox = true;
311 void LibBox::Select()
313 if (m_xWidget->changed_by_direct_pick())
315 if (!mbIgnoreSelect)
316 NotifyIDE();
317 else
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());
325 if (pEntry)
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 });
336 ReleaseFocus();
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));
345 delete pEntry;
347 m_xWidget->clear();
350 // class LanguageBoxControl ----------------------------------------------
352 /*! Macro for implementation two methods for LanguageBoxControl Class
354 * @code
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));
365 * @endcode
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);
382 else
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)
402 FillBox();
404 SetSizePixel(m_xWidget->get_preferred_size());
407 LanguageBox::~LanguageBox() { disposeOnce(); }
409 void LanguageBox::dispose()
411 ClearBox();
412 DocListenerBox::dispose();
415 void LanguageBox::FillBox()
417 m_xWidget->freeze();
418 mbIgnoreSelect = true;
419 msCurrentText = m_xWidget->get_active_text();
420 ClearBox();
422 sal_Int32 nSelPos = -1;
424 std::shared_ptr<LocalizationMgr> pCurMgr(GetShell()->GetCurLocalizationMgr());
425 if (pCurMgr->isLibraryLocalized())
427 set_sensitive(true);
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);
439 if (bIsDefault)
441 sLanguage += " " + msDefaultLanguageStr;
443 LanguageEntry* pEntry = new LanguageEntry(pLocale[i], bIsDefault);
444 OUString sId(weld::toId(pEntry));
445 m_xWidget->append(sId, sLanguage);
447 if (bIsCurrent)
448 nSelPos = i;
451 if (nSelPos != -1)
452 msCurrentText = m_xWidget->get_text(nSelPos);
454 else
456 m_xWidget->append_text(msNotLocalizedStr);
457 nSelPos = 0;
458 set_sensitive(false);
461 m_xWidget->thaw();
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));
472 delete pEntry;
474 m_xWidget->clear();
477 void LanguageBox::SetLanguage()
479 LanguageEntry* pEntry = weld::fromId<LanguageEntry*>(m_xWidget->get_active_id());
480 if (pEntry)
481 GetShell()->GetCurLocalizationMgr()->handleSetCurrentLocale(pEntry->m_aLocale);
484 void LanguageBox::Select()
486 if (!mbIgnoreSelect)
487 SetLanguage();
488 else
489 m_xWidget->set_active_text(msCurrentText); // Select after Escape
492 bool LanguageBox::HandleKeyInput(const KeyEvent& rKEvt)
494 bool bDone = false;
496 sal_uInt16 nKeyCode = rKEvt.GetKeyCode().GetCode();
497 switch (nKeyCode)
499 case KEY_RETURN:
501 SetLanguage();
502 bDone = true;
504 break;
505 case KEY_ESCAPE:
507 m_xWidget->set_active_text(msCurrentText);
508 bDone = true;
510 break;
513 return bDone || DocListenerBox::HandleKeyInput(rKEvt);
516 void LanguageBox::Update(const SfxStringItem* pItem)
518 FillBox();
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: */