LibreOffice Module basctl (master) 1
IDEComboBox.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
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>
28
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
38namespace basctl
39{
40using namespace ::com::sun::star;
41using namespace ::com::sun::star::lang;
42using namespace ::com::sun::star::uno;
43
61
62LibBoxControl::LibBoxControl(sal_uInt16 nSlotId, ToolBoxItemId nId, ToolBox& rTbx)
63 : SfxToolBoxControl(nSlotId, nId, rTbx)
64{
65}
66
68 const SfxPoolItem* pState)
69{
70 LibBox* pBox = static_cast<LibBox*>(GetToolBox().GetItemWindow(GetId()));
71
72 DBG_ASSERT(pBox, "Box not found");
73 if (!pBox)
74 return;
75
76 if (eState != SfxItemState::DEFAULT)
77 pBox->set_sensitive(false);
78 else
79 {
80 pBox->set_sensitive(true);
81 pBox->Update(dynamic_cast<SfxStringItem const*>(pState));
82 }
83}
84
86{
87 return VclPtr<LibBox>::Create(pParent);
88}
89
91 : InterimItemWindow(pParent, "modules/BasicIDE/ui/combobox.ui", "ComboBox")
92 , m_xWidget(m_xBuilder->weld_combo_box("combobox"))
93 , maNotifier(*this)
94{
96
97 m_xWidget->connect_changed(LINK(this, DocListenerBox, SelectHdl));
98 m_xWidget->connect_key_press(LINK(this, DocListenerBox, KeyInputHdl));
99}
100
101void DocListenerBox::set_sensitive(bool bSensitive)
102{
103 Enable(bSensitive);
104 m_xWidget->set_sensitive(bSensitive);
105}
106
107IMPL_LINK(DocListenerBox, KeyInputHdl, const KeyEvent&, rKEvt, bool)
108{
109 return HandleKeyInput(rKEvt);
110}
111
112bool DocListenerBox::HandleKeyInput(const KeyEvent& rKEvt) { return ChildKeyInput(rKEvt); }
113
115
117
119{
121 m_xWidget.reset();
123}
124
127
130
133
136
139
142
145
148
151
153 : DocListenerBox(pParent)
154{
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;
161
162 m_xWidget->connect_focus_in(LINK(this, LibBox, FocusInHdl));
163 m_xWidget->connect_focus_out(LINK(this, LibBox, FocusOutHdl));
164
165 SetSizePixel(m_xWidget->get_preferred_size());
166}
167
169
171{
172 ClearBox();
174}
175
177{
178 // if ( !pItem || !pItem->GetValue().Len() )
179 FillBox();
180
181 if (pItem)
182 {
183 maCurrentText = pItem->GetValue();
184 if (maCurrentText.isEmpty())
185 maCurrentText = IDEResId(RID_STR_ALL);
186 }
187
188 if (m_xWidget->get_active_text() != maCurrentText)
189 m_xWidget->set_active_text(maCurrentText);
190}
191
193{
195 DBG_ASSERT(pCurSh, "Current ViewShell not found!");
196
197 if (!pCurSh)
198 return;
199
200 vcl::Window* pShellWin = pCurSh->GetWindow();
201 if (pShellWin)
202 {
203 pShellWin->GrabFocus();
204 return;
205 }
206
208 if (!pWin)
209 return;
210 pWin->grab_focus();
211}
212
214{
215 m_xWidget->freeze();
216 mbIgnoreSelect = true;
217
218 maCurrentText = m_xWidget->get_active_text();
219
220 ClearBox();
221
222 // create list box entries
224 LIBRARY_LOCATION_UNKNOWN, OUString());
225 OUString sId(weld::toId(pEntry));
226 m_xWidget->append(sId, IDEResId(RID_STR_ALL));
227
230
231 ScriptDocuments aDocuments(
233 for (auto const& doc : aDocuments)
234 {
236 }
237
238 m_xWidget->thaw();
239
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;
247}
248
249void LibBox::InsertEntries(const ScriptDocument& rDocument, LibraryLocation eLocation)
250{
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();
255
256 for (sal_Int32 i = 0; i < nLibCount; ++i)
257 {
258 OUString aLibName = pLibNames[i];
259 if (eLocation == rDocument.getLibraryLocation(aLibName))
260 {
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);
265 }
266 }
267}
268
270{
271 bool bDone = false;
272
273 sal_uInt16 nKeyCode = rKEvt.GetKeyCode().GetCode();
274 switch (nKeyCode)
275 {
276 case KEY_RETURN:
277 {
278 NotifyIDE();
279 bDone = true;
280 }
281 break;
282 case KEY_ESCAPE:
283 {
284 m_xWidget->set_active_text(maCurrentText);
285 ReleaseFocus();
286 bDone = true;
287 }
288 break;
289 }
290
291 return bDone || DocListenerBox::HandleKeyInput(rKEvt);
292}
293
295{
296 if (mbFillBox)
297 {
298 FillBox();
299 mbFillBox = false;
300 }
301}
302
304{
305 // comboboxes can be comprised of multiple widgets, ensure all have lost focus
306 if (m_xWidget && !m_xWidget->has_focus())
307 mbFillBox = true;
308}
309
311{
312 if (m_xWidget->changed_by_direct_pick())
313 {
314 if (!mbIgnoreSelect)
315 NotifyIDE();
316 else
317 m_xWidget->set_active_text(maCurrentText); // (Select after Escape)
318 }
319}
320
322{
323 LibEntry* pEntry = weld::fromId<LibEntry*>(m_xWidget->get_active_id());
324 if (pEntry)
325 {
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 });
334 }
335 ReleaseFocus();
336}
337
339{
340 sal_Int32 nCount = m_xWidget->get_count();
341 for (sal_Int32 i = 0; i < nCount; ++i)
342 {
343 LibEntry* pEntry = weld::fromId<LibEntry*>(m_xWidget->get_id(i));
344 delete pEntry;
345 }
346 m_xWidget->clear();
347}
348
349// class LanguageBoxControl ----------------------------------------------
350
368
370 : SfxToolBoxControl(nSlotId, nId, rTbx)
371{
372}
373
375 const SfxPoolItem* pItem)
376{
377 if (LanguageBox* pBox = static_cast<LanguageBox*>(GetToolBox().GetItemWindow(GetId())))
378 {
379 if (eState != SfxItemState::DEFAULT)
380 pBox->set_sensitive(false);
381 else
382 {
383 pBox->set_sensitive(true);
384 pBox->Update(dynamic_cast<SfxStringItem const*>(pItem));
385 }
386 }
387}
388
390{
391 return VclPtr<LanguageBox>::Create(pParent);
392}
393
394// class basctl::LanguageBox -----------------------------------------------
396 : DocListenerBox(pParent)
397 , msNotLocalizedStr(IDEResId(RID_STR_TRANSLATION_NOTLOCALIZED))
398 , msDefaultLanguageStr(IDEResId(RID_STR_TRANSLATION_DEFAULT))
399 , mbIgnoreSelect(false)
400{
401 FillBox();
402
403 SetSizePixel(m_xWidget->get_preferred_size());
404}
405
407
409{
410 ClearBox();
412}
413
415{
416 m_xWidget->freeze();
417 mbIgnoreSelect = true;
418 msCurrentText = m_xWidget->get_active_text();
419 ClearBox();
420
421 sal_Int32 nSelPos = -1;
422
423 std::shared_ptr<LocalizationMgr> pCurMgr(GetShell()->GetCurLocalizationMgr());
424 if (pCurMgr->isLibraryLocalized())
425 {
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)
433 {
434 bool bIsDefault = localesAreEqual(aDefaultLocale, pLocale[i]);
435 bool bIsCurrent = localesAreEqual(aCurrentLocale, pLocale[i]);
437 OUString sLanguage = SvtLanguageTable::GetLanguageString(eLangType);
438 if (bIsDefault)
439 {
440 sLanguage += " " + msDefaultLanguageStr;
441 }
442 LanguageEntry* pEntry = new LanguageEntry(pLocale[i], bIsDefault);
443 OUString sId(weld::toId(pEntry));
444 m_xWidget->append(sId, sLanguage);
445
446 if (bIsCurrent)
447 nSelPos = i;
448 }
449
450 if (nSelPos != -1)
451 msCurrentText = m_xWidget->get_text(nSelPos);
452 }
453 else
454 {
455 m_xWidget->append_text(msNotLocalizedStr);
456 nSelPos = 0;
457 set_sensitive(false);
458 }
459
460 m_xWidget->thaw();
461 m_xWidget->set_active(nSelPos);
462 mbIgnoreSelect = false;
463}
464
466{
467 sal_Int32 nCount = m_xWidget->get_count();
468 for (sal_Int32 i = 0; i < nCount; ++i)
469 {
470 LanguageEntry* pEntry = weld::fromId<LanguageEntry*>(m_xWidget->get_id(i));
471 delete pEntry;
472 }
473 m_xWidget->clear();
474}
475
477{
478 LanguageEntry* pEntry = weld::fromId<LanguageEntry*>(m_xWidget->get_active_id());
479 if (pEntry)
480 GetShell()->GetCurLocalizationMgr()->handleSetCurrentLocale(pEntry->m_aLocale);
481}
482
484{
485 if (!mbIgnoreSelect)
486 SetLanguage();
487 else
488 m_xWidget->set_active_text(msCurrentText); // Select after Escape
489}
490
492{
493 bool bDone = false;
494
495 sal_uInt16 nKeyCode = rKEvt.GetKeyCode().GetCode();
496 switch (nKeyCode)
497 {
498 case KEY_RETURN:
499 {
500 SetLanguage();
501 bDone = true;
502 }
503 break;
504 case KEY_ESCAPE:
505 {
506 m_xWidget->set_active_text(msCurrentText);
507 bDone = true;
508 }
509 break;
510 }
511
512 return bDone || DocListenerBox::HandleKeyInput(rKEvt);
513}
514
516{
517 FillBox();
518
519 if (pItem && !pItem->GetValue().isEmpty())
520 {
521 msCurrentText = pItem->GetValue();
522 if (m_xWidget->get_active_text() != msCurrentText)
523 m_xWidget->set_active_text(msCurrentText);
524 }
525}
526
527} // namespace basctl
528
529/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::unique_ptr< weld::Image > m_xWidget
ScriptDocument aDocument
Definition: basobj2.cxx:194
static weld::Window * GetDefDialogParent()
const OUString & GetValue() const
bool ChildKeyInput(const KeyEvent &rKEvt)
virtual void dispose() override
void InitControlBase(weld::Widget *pWidget)
const vcl::KeyCode & GetKeyCode() const
static LanguageType convertToLanguageType(const css::lang::Locale &rLocale, bool bResolveSystem=true)
ToolBoxItemId GetId() const
ToolBox & GetToolBox() const
static SAL_WARN_UNUSED_RESULT SfxViewShell * Current()
vcl::Window * GetWindow() const
static OUString GetLanguageString(const LanguageType eType)
vcl::Window * GetItemWindow(ToolBoxItemId nItemId) const
static VclPtr< reference_type > Create(Arg &&... arg)
Base class for all ComboBox elements.
Definition: IDEComboBox.hxx:82
virtual void onDocumentTitleChanged(const ScriptDocument &_rDoc) override
Not interested in. Do nothing.
virtual void onDocumentClosed(const ScriptDocument &_rDoc) override
Only calls FillBox(). Parameter is not used.
virtual void onDocumentModeChanged(const ScriptDocument &_rDoc) override
Not interested in. Do nothing.
virtual void dispose() override
void set_sensitive(bool bSensitive)
DocListenerBox(vcl::Window *pParent)
Definition: IDEComboBox.cxx:90
std::unique_ptr< weld::ComboBox > m_xWidget
Definition: IDEComboBox.hxx:88
virtual void onDocumentSaveAs(const ScriptDocument &_rDoc) override
Not interested in. Do nothing.
virtual void onDocumentCreated(const ScriptDocument &_rDoc) override
Only calls FillBox(). Parameter is not used.
virtual void onDocumentOpened(const ScriptDocument &_rDoc) override
Only calls FillBox(). Parameter is not used.
virtual void onDocumentSave(const ScriptDocument &_rDoc) override
Not interested in. Do nothing.
virtual void onDocumentSaveDone(const ScriptDocument &_rDoc) override
Not interested in. Do nothing.
DocumentEventNotifier maNotifier
virtual void onDocumentSaveAsDone(const ScriptDocument &_rDoc) override
Only calls FillBox(). Parameter is not used.
virtual void FillBox()=0
virtual ~DocListenerBox() override
virtual bool HandleKeyInput(const KeyEvent &rKEvt)
key strokes the ComboBox receives
ScriptDocument const & GetDocument() const
Definition: bastype2.hxx:99
Manage stats of Language ComboBox.
virtual void StateChangedAtToolBoxControl(sal_uInt16 nSID, SfxItemState eState, const SfxPoolItem *pState) override
LanguageBoxControl(sal_uInt16 nSlotId, ToolBoxItemId nId, ToolBox &rTbx)
virtual VclPtr< InterimItemWindow > CreateItemWindow(vcl::Window *pParent) override
Class language ComboBox.
virtual void FillBox() override
Fill up the language combobox.
void ClearBox()
Delete all languages from ComboBox.
OUString msNotLocalizedStr
virtual void dispose() override
void Update(const SfxStringItem *pItem)
bool mbIgnoreSelect
do not use in this class
virtual bool HandleKeyInput(const KeyEvent &rKEvt) override
OUString msDefaultLanguageStr
void SetLanguage()
Switch interface of dialog to selected language.
virtual void Select() override
Called for setting language when user selects a language in ComboBox.
virtual ~LanguageBox() override
LanguageBox(vcl::Window *pParent)
Manage states of macro and dialog Library ComboBox.
Definition: IDEComboBox.hxx:37
LibBoxControl(sal_uInt16 nSlotId, ToolBoxItemId nId, ToolBox &rTbx)
Definition: IDEComboBox.cxx:62
virtual void StateChangedAtToolBoxControl(sal_uInt16 nSlotID, SfxItemState eState, const SfxPoolItem *pState) override
Definition: IDEComboBox.cxx:67
virtual VclPtr< InterimItemWindow > CreateItemWindow(vcl::Window *pParent) override
Definition: IDEComboBox.cxx:85
Macros and Dialogs Library ComboBox.
virtual bool HandleKeyInput(const KeyEvent &rKEvt) override
virtual void dispose() override
virtual void Select() override
Called for setting language when user selects a language in ComboBox.
bool mbFillBox
If true, when FillBox() is called.
OUString maCurrentText
LibBox(vcl::Window *pParent)
virtual ~LibBox() override
virtual void FillBox() override
Fill up the combobox.
void InsertEntries(const ScriptDocument &rDocument, LibraryLocation eLocation)
void Update(const SfxStringItem *pItem)
static void ReleaseFocus()
OUString const & GetLibName() const
Definition: bastype2.hxx:116
encapsulates a document which contains Basic scripts and dialogs
OUString getTitle(LibraryLocation _eLocation, LibraryType _eType=LibraryType::All) const
returns the title for the document
@ DocumentsSorted
real documents only, sorted lexicographically by their title (using the sys locale's default collator...
static ScriptDocuments getAllScriptDocuments(ScriptDocumentList _eListType)
returns the set of ScriptDocument instances, one for each open document which contains Basic/Dialog c...
static const ScriptDocument & getApplicationScriptDocument()
returns a reference to a shared ScriptDocument instance which operates on the application-wide script...
css::uno::Sequence< OUString > getLibraryNames() const
retrieves the (combined) names of all script and dialog libraries
LibraryLocation getLibraryLocation(const OUString &_rLibName) const
returns the location of a library given by name
const std::shared_ptr< LocalizationMgr > & GetCurLocalizationMgr() const
Definition: basidesh.hxx:164
sal_uInt16 GetCode() const
virtual void SetSizePixel(const Size &rNewSize)
void GrabFocus()
void Enable(bool bEnable=true, bool bChild=true)
virtual void grab_focus()=0
int nCount
#define DBG_ASSERT(sCon, aError)
sal_Int32 nIndex
OUString aName
constexpr sal_uInt16 KEY_RETURN
constexpr sal_uInt16 KEY_ESCAPE
const char * pLocale
OUString CreateMgrAndLibStr(std::u16string_view rMgrName, std::u16string_view rLibName)
Definition: bastype3.cxx:432
@ LIBRARY_LOCATION_USER
@ LIBRARY_LOCATION_UNKNOWN
@ LIBRARY_LOCATION_SHARE
@ LIBRARY_LOCATION_DOCUMENT
bool localesAreEqual(const Locale &rLocaleLeft, const Locale &rLocaleRight)
Definition: managelang.cxx:48
Shell * GetShell()
Definition: iderdll.cxx:80
SfxDispatcher * GetDispatcher()
Definition: basobj3.cxx:454
IMPL_LINK(AccessibleDialogWindow, WindowEventListener, VclWindowEvent &, rEvent, void)
std::vector< ScriptDocument > ScriptDocuments
IMPL_LINK_NOARG(EditorWindow, SetSourceInBasicHdl, void *, void)
Definition: baside2b.cxx:987
OUString IDEResId(TranslateId aId)
Definition: iderdll.cxx:108
SFX_IMPL_TOOLBOX_CONTROL(LibBoxControl, SfxStringItem)
int i
OUString toId(const void *pValue)
sal_Int16 nId
SfxItemState
css::lang::Locale m_aLocale
Definition: managelang.hxx:34
OUString sId