nss: upgrade to release 3.73
[LibreOffice.git] / basctl / source / basicide / IDEComboBox.cxx
blob5063eb4f45a907c911fc51c89c65fc3dda022aa0
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>
38 namespace basctl
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::uno;
44 /*! Macro for implementation two methods for LibBoxControl Class
46 * @code
47 * SfxToolBoxControl* LibBoxControl::CreateImpl(sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx)
48 * {
49 * return new LibBoxControl(nSlotId, nId, rTbx);
50 * }
52 * void LibBoxControl::RegisterControl(sal_uInt16 nSlotId, SfxModule* pMod)
53 * {
54 * SfxToolBoxControl::RegisterToolBoxControl(
55 * pMod, SfxTbxCtrlFactory(* LibBoxControl::CreateImpl, typeid(nItemClass), nSlotId));
56 * }
57 * @endcode
58 * @see Macro SFX_DECL_TOOLBOX_CONTROL
60 SFX_IMPL_TOOLBOX_CONTROL(LibBoxControl, SfxStringItem);
62 LibBoxControl::LibBoxControl(sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx)
63 : SfxToolBoxControl(nSlotId, nId, rTbx)
67 void LibBoxControl::StateChanged(sal_uInt16, SfxItemState eState, const SfxPoolItem* pState)
69 LibBox* pBox = static_cast<LibBox*>(GetToolBox().GetItemWindow(GetId()));
71 DBG_ASSERT(pBox, "Box not found");
72 if (!pBox)
73 return;
75 if (eState != SfxItemState::DEFAULT)
76 pBox->set_sensitive(false);
77 else
79 pBox->set_sensitive(true);
80 pBox->Update(dynamic_cast<SfxStringItem const*>(pState));
84 VclPtr<InterimItemWindow> LibBoxControl::CreateItemWindow(vcl::Window* pParent)
86 return VclPtr<LibBox>::Create(pParent);
89 DocListenerBox::DocListenerBox(vcl::Window* pParent)
90 : InterimItemWindow(pParent, "modules/BasicIDE/ui/combobox.ui", "ComboBox")
91 , m_xWidget(m_xBuilder->weld_combo_box("combobox"))
92 , maNotifier(*this)
94 InitControlBase(m_xWidget.get());
96 m_xWidget->connect_changed(LINK(this, DocListenerBox, SelectHdl));
97 m_xWidget->connect_key_press(LINK(this, DocListenerBox, KeyInputHdl));
100 void DocListenerBox::set_sensitive(bool bSensitive)
102 Enable(bSensitive);
103 m_xWidget->set_sensitive(bSensitive);
106 IMPL_LINK(DocListenerBox, KeyInputHdl, const KeyEvent&, rKEvt, bool)
108 return HandleKeyInput(rKEvt);
111 bool DocListenerBox::HandleKeyInput(const KeyEvent& rKEvt) { return ChildKeyInput(rKEvt); }
113 IMPL_LINK_NOARG(DocListenerBox, SelectHdl, weld::ComboBox&, void) { Select(); }
115 DocListenerBox::~DocListenerBox() { disposeOnce(); }
117 void DocListenerBox::dispose()
119 maNotifier.dispose();
120 m_xWidget.reset();
121 InterimItemWindow::dispose();
124 /// Only calls FillBox(). Parameter is not used.
125 void DocListenerBox::onDocumentCreated(const ScriptDocument& /*_rDoc*/) { FillBox(); }
127 /// Only calls FillBox(). Parameter is not used.
128 void DocListenerBox::onDocumentOpened(const ScriptDocument& /*_rDoc*/) { FillBox(); }
130 /// Only calls FillBox(). Parameter is not used.
131 void DocListenerBox::onDocumentSaveAsDone(const ScriptDocument& /*_rDoc*/) { FillBox(); }
133 /// Only calls FillBox(). Parameter is not used.
134 void DocListenerBox::onDocumentClosed(const ScriptDocument& /*_rDoc*/) { FillBox(); }
136 /// Not interested in. Do nothing.
137 void DocListenerBox::onDocumentSave(const ScriptDocument& /*_rDoc*/) {}
139 /// Not interested in. Do nothing.
140 void DocListenerBox::onDocumentSaveDone(const ScriptDocument& /*_rDoc*/) {}
142 /// Not interested in. Do nothing.
143 void DocListenerBox::onDocumentSaveAs(const ScriptDocument& /*_rDoc*/) {}
145 /// Not interested in. Do nothing.
146 void DocListenerBox::onDocumentTitleChanged(const ScriptDocument& /*_rDoc*/) {}
148 /// Not interested in. Do nothing.
149 void DocListenerBox::onDocumentModeChanged(const ScriptDocument& /*_rDoc*/) {}
151 LibBox::LibBox(vcl::Window* pParent)
152 : DocListenerBox(pParent)
154 FillBox();
155 mbIgnoreSelect = true; // do not yet transfer select of 0
156 mbFillBox = true;
157 m_xWidget->set_active(0);
158 maCurrentText = m_xWidget->get_text(0);
159 mbIgnoreSelect = false;
161 m_xWidget->connect_focus_in(LINK(this, LibBox, FocusInHdl));
162 m_xWidget->connect_focus_out(LINK(this, LibBox, FocusOutHdl));
164 SetSizePixel(m_xWidget->get_preferred_size());
167 LibBox::~LibBox() { disposeOnce(); }
169 void LibBox::dispose()
171 ClearBox();
172 DocListenerBox::dispose();
175 void LibBox::Update(const SfxStringItem* pItem)
177 // if ( !pItem || !pItem->GetValue().Len() )
178 FillBox();
180 if (pItem)
182 maCurrentText = pItem->GetValue();
183 if (maCurrentText.isEmpty())
184 maCurrentText = IDEResId(RID_STR_ALL);
187 if (m_xWidget->get_active_text() != maCurrentText)
188 m_xWidget->set_active_text(maCurrentText);
191 void LibBox::ReleaseFocus()
193 SfxViewShell* pCurSh = SfxViewShell::Current();
194 DBG_ASSERT(pCurSh, "Current ViewShell not found!");
196 if (pCurSh)
198 vcl::Window* pShellWin = pCurSh->GetWindow();
199 if (!pShellWin)
200 pShellWin = Application::GetDefDialogParent();
202 pShellWin->GrabFocus();
206 void LibBox::FillBox()
208 m_xWidget->freeze();
209 mbIgnoreSelect = true;
211 maCurrentText = m_xWidget->get_active_text();
213 ClearBox();
215 // create list box entries
216 LibEntry* pEntry = new LibEntry(ScriptDocument::getApplicationScriptDocument(),
217 LIBRARY_LOCATION_UNKNOWN, OUString());
218 OUString sId(OUString::number(reinterpret_cast<sal_Int64>(pEntry)));
219 m_xWidget->append(sId, IDEResId(RID_STR_ALL));
221 InsertEntries(ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER);
222 InsertEntries(ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE);
224 ScriptDocuments aDocuments(
225 ScriptDocument::getAllScriptDocuments(ScriptDocument::DocumentsSorted));
226 for (auto const& doc : aDocuments)
228 InsertEntries(doc, LIBRARY_LOCATION_DOCUMENT);
231 m_xWidget->thaw();
233 int nIndex = m_xWidget->find_text(maCurrentText);
234 if (nIndex != -1)
235 m_xWidget->set_active(nIndex);
236 else
237 m_xWidget->set_active(0);
238 maCurrentText = m_xWidget->get_active_text();
239 mbIgnoreSelect = false;
242 void LibBox::InsertEntries(const ScriptDocument& rDocument, LibraryLocation eLocation)
244 // get a sorted list of library names
245 Sequence<OUString> aLibNames = rDocument.getLibraryNames();
246 sal_Int32 nLibCount = aLibNames.getLength();
247 const OUString* pLibNames = aLibNames.getConstArray();
249 for (sal_Int32 i = 0; i < nLibCount; ++i)
251 OUString aLibName = pLibNames[i];
252 if (eLocation == rDocument.getLibraryLocation(aLibName))
254 OUString aName(rDocument.getTitle(eLocation));
255 OUString aEntryText(CreateMgrAndLibStr(aName, aLibName));
256 LibEntry* pEntry = new LibEntry(rDocument, eLocation, aLibName);
257 m_xWidget->append(OUString::number(reinterpret_cast<sal_Int64>(pEntry)), aEntryText);
262 bool LibBox::HandleKeyInput(const KeyEvent& rKEvt)
264 bool bDone = false;
266 sal_uInt16 nKeyCode = rKEvt.GetKeyCode().GetCode();
267 switch (nKeyCode)
269 case KEY_RETURN:
271 NotifyIDE();
272 bDone = true;
274 break;
275 case KEY_ESCAPE:
277 m_xWidget->set_active_text(maCurrentText);
278 ReleaseFocus();
279 bDone = true;
281 break;
284 return bDone || DocListenerBox::HandleKeyInput(rKEvt);
287 IMPL_LINK_NOARG(LibBox, FocusInHdl, weld::Widget&, void)
289 if (mbFillBox)
291 FillBox();
292 mbFillBox = false;
296 IMPL_LINK_NOARG(LibBox, FocusOutHdl, weld::Widget&, void)
298 // comboboxes can be comprised of multiple widgets, ensure all have lost focus
299 if (m_xWidget && !m_xWidget->has_focus())
300 mbFillBox = true;
303 void LibBox::Select()
305 if (m_xWidget->changed_by_direct_pick())
307 if (!mbIgnoreSelect)
308 NotifyIDE();
309 else
310 m_xWidget->set_active_text(maCurrentText); // (Select after Escape)
314 void LibBox::NotifyIDE()
316 LibEntry* pEntry = reinterpret_cast<LibEntry*>(m_xWidget->get_active_id().toInt64());
317 if (pEntry)
319 const ScriptDocument& aDocument(pEntry->GetDocument());
320 SfxUnoAnyItem aDocumentItem(SID_BASICIDE_ARG_DOCUMENT_MODEL,
321 uno::Any(aDocument.getDocumentOrNull()));
322 const OUString& aLibName = pEntry->GetLibName();
323 SfxStringItem aLibNameItem(SID_BASICIDE_ARG_LIBNAME, aLibName);
324 if (SfxDispatcher* pDispatcher = GetDispatcher())
325 pDispatcher->ExecuteList(SID_BASICIDE_LIBSELECTED, SfxCallMode::SYNCHRON,
326 { &aDocumentItem, &aLibNameItem });
328 ReleaseFocus();
331 void LibBox::ClearBox()
333 sal_Int32 nCount = m_xWidget->get_count();
334 for (sal_Int32 i = 0; i < nCount; ++i)
336 LibEntry* pEntry = reinterpret_cast<LibEntry*>(m_xWidget->get_id(i).toInt64());
337 delete pEntry;
339 m_xWidget->clear();
342 // class LanguageBoxControl ----------------------------------------------
344 /*! Macro for implementation two methods for LanguageBoxControl Class
346 * @code
347 * SfxToolBoxControl* LanguageBoxControl::CreateImpl(sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx)
349 * return new LanguageBoxControl(nSlotId, nId, rTbx);
352 * void LanguageBoxControl::RegisterControl(sal_uInt16 nSlotId, SfxModule* pMod)
354 * SfxToolBoxControl::RegisterToolBoxControl(
355 * pMod, SfxTbxCtrlFactory(* LanguageBoxControl::CreateImpl, typeid(nItemClass), nSlotId));
357 * @endcode
358 * @see Macro SFX_DECL_TOOLBOX_CONTROL
360 SFX_IMPL_TOOLBOX_CONTROL(LanguageBoxControl, SfxStringItem);
362 LanguageBoxControl::LanguageBoxControl(sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx)
363 : SfxToolBoxControl(nSlotId, nId, rTbx)
367 void LanguageBoxControl::StateChanged(sal_uInt16, SfxItemState eState, const SfxPoolItem* pItem)
369 if (LanguageBox* pBox = static_cast<LanguageBox*>(GetToolBox().GetItemWindow(GetId())))
371 if (eState != SfxItemState::DEFAULT)
372 pBox->set_sensitive(false);
373 else
375 pBox->set_sensitive(true);
376 pBox->Update(dynamic_cast<SfxStringItem const*>(pItem));
381 VclPtr<InterimItemWindow> LanguageBoxControl::CreateItemWindow(vcl::Window* pParent)
383 return VclPtr<LanguageBox>::Create(pParent);
386 // class basctl::LanguageBox -----------------------------------------------
387 LanguageBox::LanguageBox(vcl::Window* pParent)
388 : DocListenerBox(pParent)
389 , msNotLocalizedStr(IDEResId(RID_STR_TRANSLATION_NOTLOCALIZED))
390 , msDefaultLanguageStr(IDEResId(RID_STR_TRANSLATION_DEFAULT))
391 , mbIgnoreSelect(false)
393 FillBox();
395 SetSizePixel(m_xWidget->get_preferred_size());
398 LanguageBox::~LanguageBox() { disposeOnce(); }
400 void LanguageBox::dispose()
402 ClearBox();
403 DocListenerBox::dispose();
406 void LanguageBox::FillBox()
408 m_xWidget->freeze();
409 mbIgnoreSelect = true;
410 msCurrentText = m_xWidget->get_active_text();
411 ClearBox();
413 sal_Int32 nSelPos = -1;
415 std::shared_ptr<LocalizationMgr> pCurMgr(GetShell()->GetCurLocalizationMgr());
416 if (pCurMgr->isLibraryLocalized())
418 set_sensitive(true);
419 Locale aDefaultLocale = pCurMgr->getStringResourceManager()->getDefaultLocale();
420 Locale aCurrentLocale = pCurMgr->getStringResourceManager()->getCurrentLocale();
421 Sequence<Locale> aLocaleSeq = pCurMgr->getStringResourceManager()->getLocales();
422 const Locale* pLocale = aLocaleSeq.getConstArray();
423 sal_Int32 i, nCount = aLocaleSeq.getLength();
424 for (i = 0; i < nCount; ++i)
426 bool bIsDefault = localesAreEqual(aDefaultLocale, pLocale[i]);
427 bool bIsCurrent = localesAreEqual(aCurrentLocale, pLocale[i]);
428 LanguageType eLangType = LanguageTag::convertToLanguageType(pLocale[i]);
429 OUString sLanguage = SvtLanguageTable::GetLanguageString(eLangType);
430 if (bIsDefault)
432 sLanguage += " " + msDefaultLanguageStr;
434 LanguageEntry* pEntry = new LanguageEntry(pLocale[i], bIsDefault);
435 OUString sId(OUString::number(reinterpret_cast<sal_Int64>(pEntry)));
436 m_xWidget->append(sId, sLanguage);
438 if (bIsCurrent)
439 nSelPos = i;
442 if (nSelPos != -1)
443 msCurrentText = m_xWidget->get_text(nSelPos);
445 else
447 m_xWidget->append_text(msNotLocalizedStr);
448 nSelPos = 0;
449 set_sensitive(false);
452 m_xWidget->thaw();
453 m_xWidget->set_active(nSelPos);
454 mbIgnoreSelect = false;
457 void LanguageBox::ClearBox()
459 sal_Int32 nCount = m_xWidget->get_count();
460 for (sal_Int32 i = 0; i < nCount; ++i)
462 LanguageEntry* pEntry = reinterpret_cast<LanguageEntry*>(m_xWidget->get_id(i).toInt64());
463 delete pEntry;
465 m_xWidget->clear();
468 void LanguageBox::SetLanguage()
470 LanguageEntry* pEntry = reinterpret_cast<LanguageEntry*>(m_xWidget->get_active_id().toInt64());
471 if (pEntry)
472 GetShell()->GetCurLocalizationMgr()->handleSetCurrentLocale(pEntry->m_aLocale);
475 void LanguageBox::Select()
477 if (!mbIgnoreSelect)
478 SetLanguage();
479 else
480 m_xWidget->set_active_text(msCurrentText); // Select after Escape
483 bool LanguageBox::HandleKeyInput(const KeyEvent& rKEvt)
485 bool bDone = false;
487 sal_uInt16 nKeyCode = rKEvt.GetKeyCode().GetCode();
488 switch (nKeyCode)
490 case KEY_RETURN:
492 SetLanguage();
493 bDone = true;
495 break;
496 case KEY_ESCAPE:
498 m_xWidget->set_active_text(msCurrentText);
499 bDone = true;
501 break;
504 return bDone || DocListenerBox::HandleKeyInput(rKEvt);
507 void LanguageBox::Update(const SfxStringItem* pItem)
509 FillBox();
511 if (pItem && !pItem->GetValue().isEmpty())
513 msCurrentText = pItem->GetValue();
514 if (m_xWidget->get_active_text() != msCurrentText)
515 m_xWidget->set_active_text(msCurrentText);
519 } // namespace basctl
521 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */