android: Update app-specific/MIME type icons
[LibreOffice.git] / basctl / source / basicide / IDEComboBox.cxx
blob423e5c5d1cda97064e52fd8cc4d24d8d304d3ab3
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, ToolBoxItemId nId, ToolBox& rTbx)
63 : SfxToolBoxControl(nSlotId, nId, rTbx)
67 void LibBoxControl::StateChangedAtToolBoxControl(sal_uInt16, SfxItemState eState,
68 const SfxPoolItem* pState)
70 LibBox* pBox = static_cast<LibBox*>(GetToolBox().GetItemWindow(GetId()));
72 DBG_ASSERT(pBox, "Box not found");
73 if (!pBox)
74 return;
76 if (eState != SfxItemState::DEFAULT)
77 pBox->set_sensitive(false);
78 else
80 pBox->set_sensitive(true);
81 pBox->Update(dynamic_cast<SfxStringItem const*>(pState));
85 VclPtr<InterimItemWindow> LibBoxControl::CreateItemWindow(vcl::Window* pParent)
87 return VclPtr<LibBox>::Create(pParent);
90 DocListenerBox::DocListenerBox(vcl::Window* pParent)
91 : InterimItemWindow(pParent, "modules/BasicIDE/ui/combobox.ui", "ComboBox")
92 , m_xWidget(m_xBuilder->weld_combo_box("combobox"))
93 , maNotifier(*this)
95 InitControlBase(m_xWidget.get());
97 m_xWidget->connect_changed(LINK(this, DocListenerBox, SelectHdl));
98 m_xWidget->connect_key_press(LINK(this, DocListenerBox, KeyInputHdl));
101 void DocListenerBox::set_sensitive(bool bSensitive)
103 Enable(bSensitive);
104 m_xWidget->set_sensitive(bSensitive);
107 IMPL_LINK(DocListenerBox, KeyInputHdl, const KeyEvent&, rKEvt, bool)
109 return HandleKeyInput(rKEvt);
112 bool DocListenerBox::HandleKeyInput(const KeyEvent& rKEvt) { return ChildKeyInput(rKEvt); }
114 IMPL_LINK_NOARG(DocListenerBox, SelectHdl, weld::ComboBox&, void) { Select(); }
116 DocListenerBox::~DocListenerBox() { disposeOnce(); }
118 void DocListenerBox::dispose()
120 maNotifier.dispose();
121 m_xWidget.reset();
122 InterimItemWindow::dispose();
125 /// Only calls FillBox(). Parameter is not used.
126 void DocListenerBox::onDocumentCreated(const ScriptDocument& /*_rDoc*/) { FillBox(); }
128 /// Only calls FillBox(). Parameter is not used.
129 void DocListenerBox::onDocumentOpened(const ScriptDocument& /*_rDoc*/) { FillBox(); }
131 /// Only calls FillBox(). Parameter is not used.
132 void DocListenerBox::onDocumentSaveAsDone(const ScriptDocument& /*_rDoc*/) { FillBox(); }
134 /// Only calls FillBox(). Parameter is not used.
135 void DocListenerBox::onDocumentClosed(const ScriptDocument& /*_rDoc*/) { FillBox(); }
137 /// Not interested in. Do nothing.
138 void DocListenerBox::onDocumentSave(const ScriptDocument& /*_rDoc*/) {}
140 /// Not interested in. Do nothing.
141 void DocListenerBox::onDocumentSaveDone(const ScriptDocument& /*_rDoc*/) {}
143 /// Not interested in. Do nothing.
144 void DocListenerBox::onDocumentSaveAs(const ScriptDocument& /*_rDoc*/) {}
146 /// Not interested in. Do nothing.
147 void DocListenerBox::onDocumentTitleChanged(const ScriptDocument& /*_rDoc*/) {}
149 /// Not interested in. Do nothing.
150 void DocListenerBox::onDocumentModeChanged(const ScriptDocument& /*_rDoc*/) {}
152 LibBox::LibBox(vcl::Window* pParent)
153 : DocListenerBox(pParent)
155 FillBox();
156 mbIgnoreSelect = true; // do not yet transfer select of 0
157 mbFillBox = true;
158 m_xWidget->set_active(0);
159 maCurrentText = m_xWidget->get_text(0);
160 mbIgnoreSelect = false;
162 m_xWidget->connect_focus_in(LINK(this, LibBox, FocusInHdl));
163 m_xWidget->connect_focus_out(LINK(this, LibBox, FocusOutHdl));
165 SetSizePixel(m_xWidget->get_preferred_size());
168 LibBox::~LibBox() { disposeOnce(); }
170 void LibBox::dispose()
172 ClearBox();
173 DocListenerBox::dispose();
176 void LibBox::Update(const SfxStringItem* pItem)
178 // if ( !pItem || !pItem->GetValue().Len() )
179 FillBox();
181 if (pItem)
183 maCurrentText = pItem->GetValue();
184 if (maCurrentText.isEmpty())
185 maCurrentText = IDEResId(RID_STR_ALL);
188 if (m_xWidget->get_active_text() != maCurrentText)
189 m_xWidget->set_active_text(maCurrentText);
192 void LibBox::ReleaseFocus()
194 SfxViewShell* pCurSh = SfxViewShell::Current();
195 DBG_ASSERT(pCurSh, "Current ViewShell not found!");
197 if (!pCurSh)
198 return;
200 vcl::Window* pShellWin = pCurSh->GetWindow();
201 if (pShellWin)
203 pShellWin->GrabFocus();
204 return;
207 weld::Window* pWin = Application::GetDefDialogParent();
208 if (!pWin)
209 return;
210 pWin->grab_focus();
213 void LibBox::FillBox()
215 m_xWidget->freeze();
216 mbIgnoreSelect = true;
218 maCurrentText = m_xWidget->get_active_text();
220 ClearBox();
222 // create list box entries
223 LibEntry* pEntry = new LibEntry(ScriptDocument::getApplicationScriptDocument(),
224 LIBRARY_LOCATION_UNKNOWN, OUString());
225 OUString sId(weld::toId(pEntry));
226 m_xWidget->append(sId, IDEResId(RID_STR_ALL));
228 InsertEntries(ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_USER);
229 InsertEntries(ScriptDocument::getApplicationScriptDocument(), LIBRARY_LOCATION_SHARE);
231 ScriptDocuments aDocuments(
232 ScriptDocument::getAllScriptDocuments(ScriptDocument::DocumentsSorted));
233 for (auto const& doc : aDocuments)
235 InsertEntries(doc, LIBRARY_LOCATION_DOCUMENT);
238 m_xWidget->thaw();
240 int nIndex = m_xWidget->find_text(maCurrentText);
241 if (nIndex != -1)
242 m_xWidget->set_active(nIndex);
243 else
244 m_xWidget->set_active(0);
245 maCurrentText = m_xWidget->get_active_text();
246 mbIgnoreSelect = false;
249 void LibBox::InsertEntries(const ScriptDocument& rDocument, LibraryLocation eLocation)
251 // get a sorted list of library names
252 Sequence<OUString> aLibNames = rDocument.getLibraryNames();
253 sal_Int32 nLibCount = aLibNames.getLength();
254 const OUString* pLibNames = aLibNames.getConstArray();
256 for (sal_Int32 i = 0; i < nLibCount; ++i)
258 OUString aLibName = pLibNames[i];
259 if (eLocation == rDocument.getLibraryLocation(aLibName))
261 OUString aName(rDocument.getTitle(eLocation));
262 OUString aEntryText(CreateMgrAndLibStr(aName, aLibName));
263 LibEntry* pEntry = new LibEntry(rDocument, eLocation, aLibName);
264 m_xWidget->append(weld::toId(pEntry), aEntryText);
269 bool LibBox::HandleKeyInput(const KeyEvent& rKEvt)
271 bool bDone = false;
273 sal_uInt16 nKeyCode = rKEvt.GetKeyCode().GetCode();
274 switch (nKeyCode)
276 case KEY_RETURN:
278 NotifyIDE();
279 bDone = true;
281 break;
282 case KEY_ESCAPE:
284 m_xWidget->set_active_text(maCurrentText);
285 ReleaseFocus();
286 bDone = true;
288 break;
291 return bDone || DocListenerBox::HandleKeyInput(rKEvt);
294 IMPL_LINK_NOARG(LibBox, FocusInHdl, weld::Widget&, void)
296 if (mbFillBox)
298 FillBox();
299 mbFillBox = false;
303 IMPL_LINK_NOARG(LibBox, FocusOutHdl, weld::Widget&, void)
305 // comboboxes can be comprised of multiple widgets, ensure all have lost focus
306 if (m_xWidget && !m_xWidget->has_focus())
307 mbFillBox = true;
310 void LibBox::Select()
312 if (m_xWidget->changed_by_direct_pick())
314 if (!mbIgnoreSelect)
315 NotifyIDE();
316 else
317 m_xWidget->set_active_text(maCurrentText); // (Select after Escape)
321 void LibBox::NotifyIDE()
323 LibEntry* pEntry = weld::fromId<LibEntry*>(m_xWidget->get_active_id());
324 if (pEntry)
326 const ScriptDocument& aDocument(pEntry->GetDocument());
327 SfxUnoAnyItem aDocumentItem(SID_BASICIDE_ARG_DOCUMENT_MODEL,
328 uno::Any(aDocument.getDocumentOrNull()));
329 const OUString& aLibName = pEntry->GetLibName();
330 SfxStringItem aLibNameItem(SID_BASICIDE_ARG_LIBNAME, aLibName);
331 if (SfxDispatcher* pDispatcher = GetDispatcher())
332 pDispatcher->ExecuteList(SID_BASICIDE_LIBSELECTED, SfxCallMode::SYNCHRON,
333 { &aDocumentItem, &aLibNameItem });
335 ReleaseFocus();
338 void LibBox::ClearBox()
340 sal_Int32 nCount = m_xWidget->get_count();
341 for (sal_Int32 i = 0; i < nCount; ++i)
343 LibEntry* pEntry = weld::fromId<LibEntry*>(m_xWidget->get_id(i));
344 delete pEntry;
346 m_xWidget->clear();
349 // class LanguageBoxControl ----------------------------------------------
351 /*! Macro for implementation two methods for LanguageBoxControl Class
353 * @code
354 * SfxToolBoxControl* LanguageBoxControl::CreateImpl(sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx)
356 * return new LanguageBoxControl(nSlotId, nId, rTbx);
359 * void LanguageBoxControl::RegisterControl(sal_uInt16 nSlotId, SfxModule* pMod)
361 * SfxToolBoxControl::RegisterToolBoxControl(
362 * pMod, SfxTbxCtrlFactory(* LanguageBoxControl::CreateImpl, typeid(nItemClass), nSlotId));
364 * @endcode
365 * @see Macro SFX_DECL_TOOLBOX_CONTROL
367 SFX_IMPL_TOOLBOX_CONTROL(LanguageBoxControl, SfxStringItem);
369 LanguageBoxControl::LanguageBoxControl(sal_uInt16 nSlotId, ToolBoxItemId nId, ToolBox& rTbx)
370 : SfxToolBoxControl(nSlotId, nId, rTbx)
374 void LanguageBoxControl::StateChangedAtToolBoxControl(sal_uInt16, SfxItemState eState,
375 const SfxPoolItem* pItem)
377 if (LanguageBox* pBox = static_cast<LanguageBox*>(GetToolBox().GetItemWindow(GetId())))
379 if (eState != SfxItemState::DEFAULT)
380 pBox->set_sensitive(false);
381 else
383 pBox->set_sensitive(true);
384 pBox->Update(dynamic_cast<SfxStringItem const*>(pItem));
389 VclPtr<InterimItemWindow> LanguageBoxControl::CreateItemWindow(vcl::Window* pParent)
391 return VclPtr<LanguageBox>::Create(pParent);
394 // class basctl::LanguageBox -----------------------------------------------
395 LanguageBox::LanguageBox(vcl::Window* pParent)
396 : DocListenerBox(pParent)
397 , msNotLocalizedStr(IDEResId(RID_STR_TRANSLATION_NOTLOCALIZED))
398 , msDefaultLanguageStr(IDEResId(RID_STR_TRANSLATION_DEFAULT))
399 , mbIgnoreSelect(false)
401 FillBox();
403 SetSizePixel(m_xWidget->get_preferred_size());
406 LanguageBox::~LanguageBox() { disposeOnce(); }
408 void LanguageBox::dispose()
410 ClearBox();
411 DocListenerBox::dispose();
414 void LanguageBox::FillBox()
416 m_xWidget->freeze();
417 mbIgnoreSelect = true;
418 msCurrentText = m_xWidget->get_active_text();
419 ClearBox();
421 sal_Int32 nSelPos = -1;
423 std::shared_ptr<LocalizationMgr> pCurMgr(GetShell()->GetCurLocalizationMgr());
424 if (pCurMgr->isLibraryLocalized())
426 set_sensitive(true);
427 Locale aDefaultLocale = pCurMgr->getStringResourceManager()->getDefaultLocale();
428 Locale aCurrentLocale = pCurMgr->getStringResourceManager()->getCurrentLocale();
429 Sequence<Locale> aLocaleSeq = pCurMgr->getStringResourceManager()->getLocales();
430 const Locale* pLocale = aLocaleSeq.getConstArray();
431 sal_Int32 i, nCount = aLocaleSeq.getLength();
432 for (i = 0; i < nCount; ++i)
434 bool bIsDefault = localesAreEqual(aDefaultLocale, pLocale[i]);
435 bool bIsCurrent = localesAreEqual(aCurrentLocale, pLocale[i]);
436 LanguageType eLangType = LanguageTag::convertToLanguageType(pLocale[i]);
437 OUString sLanguage = SvtLanguageTable::GetLanguageString(eLangType);
438 if (bIsDefault)
440 sLanguage += " " + msDefaultLanguageStr;
442 LanguageEntry* pEntry = new LanguageEntry(pLocale[i], bIsDefault);
443 OUString sId(weld::toId(pEntry));
444 m_xWidget->append(sId, sLanguage);
446 if (bIsCurrent)
447 nSelPos = i;
450 if (nSelPos != -1)
451 msCurrentText = m_xWidget->get_text(nSelPos);
453 else
455 m_xWidget->append_text(msNotLocalizedStr);
456 nSelPos = 0;
457 set_sensitive(false);
460 m_xWidget->thaw();
461 m_xWidget->set_active(nSelPos);
462 mbIgnoreSelect = false;
465 void LanguageBox::ClearBox()
467 sal_Int32 nCount = m_xWidget->get_count();
468 for (sal_Int32 i = 0; i < nCount; ++i)
470 LanguageEntry* pEntry = weld::fromId<LanguageEntry*>(m_xWidget->get_id(i));
471 delete pEntry;
473 m_xWidget->clear();
476 void LanguageBox::SetLanguage()
478 LanguageEntry* pEntry = weld::fromId<LanguageEntry*>(m_xWidget->get_active_id());
479 if (pEntry)
480 GetShell()->GetCurLocalizationMgr()->handleSetCurrentLocale(pEntry->m_aLocale);
483 void LanguageBox::Select()
485 if (!mbIgnoreSelect)
486 SetLanguage();
487 else
488 m_xWidget->set_active_text(msCurrentText); // Select after Escape
491 bool LanguageBox::HandleKeyInput(const KeyEvent& rKEvt)
493 bool bDone = false;
495 sal_uInt16 nKeyCode = rKEvt.GetKeyCode().GetCode();
496 switch (nKeyCode)
498 case KEY_RETURN:
500 SetLanguage();
501 bDone = true;
503 break;
504 case KEY_ESCAPE:
506 m_xWidget->set_active_text(msCurrentText);
507 bDone = true;
509 break;
512 return bDone || DocListenerBox::HandleKeyInput(rKEvt);
515 void LanguageBox::Update(const SfxStringItem* pItem)
517 FillBox();
519 if (pItem && !pItem->GetValue().isEmpty())
521 msCurrentText = pItem->GetValue();
522 if (m_xWidget->get_active_text() != msCurrentText)
523 m_xWidget->set_active_text(msCurrentText);
527 } // namespace basctl
529 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */