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>
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
47 * SfxToolBoxControl* LibBoxControl::CreateImpl(sal_uInt16 nSlotId, sal_uInt16 nId, ToolBox& rTbx)
49 * return new LibBoxControl(nSlotId, nId, rTbx);
52 * void LibBoxControl::RegisterControl(sal_uInt16 nSlotId, SfxModule* pMod)
54 * SfxToolBoxControl::RegisterToolBoxControl(
55 * pMod, SfxTbxCtrlFactory(* LibBoxControl::CreateImpl, typeid(nItemClass), nSlotId));
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");
75 if (eState
!= SfxItemState::DEFAULT
)
76 pBox
->set_sensitive(false);
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"))
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
)
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();
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
)
155 mbIgnoreSelect
= true; // do not yet transfer select of 0
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()
172 DocListenerBox::dispose();
175 void LibBox::Update(const SfxStringItem
* pItem
)
177 // if ( !pItem || !pItem->GetValue().Len() )
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!");
198 vcl::Window
* pShellWin
= pCurSh
->GetWindow();
200 pShellWin
= Application::GetDefDialogParent();
202 pShellWin
->GrabFocus();
206 void LibBox::FillBox()
209 mbIgnoreSelect
= true;
211 maCurrentText
= m_xWidget
->get_active_text();
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
);
233 int nIndex
= m_xWidget
->find_text(maCurrentText
);
235 m_xWidget
->set_active(nIndex
);
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
)
266 sal_uInt16 nKeyCode
= rKEvt
.GetKeyCode().GetCode();
277 m_xWidget
->set_active_text(maCurrentText
);
284 return bDone
|| DocListenerBox::HandleKeyInput(rKEvt
);
287 IMPL_LINK_NOARG(LibBox
, FocusInHdl
, weld::Widget
&, void)
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())
303 void LibBox::Select()
305 if (m_xWidget
->changed_by_direct_pick())
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());
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
});
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());
342 // class LanguageBoxControl ----------------------------------------------
344 /*! Macro for implementation two methods for LanguageBoxControl Class
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));
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);
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)
395 SetSizePixel(m_xWidget
->get_preferred_size());
398 LanguageBox::~LanguageBox() { disposeOnce(); }
400 void LanguageBox::dispose()
403 DocListenerBox::dispose();
406 void LanguageBox::FillBox()
409 mbIgnoreSelect
= true;
410 msCurrentText
= m_xWidget
->get_active_text();
413 sal_Int32 nSelPos
= -1;
415 std::shared_ptr
<LocalizationMgr
> pCurMgr(GetShell()->GetCurLocalizationMgr());
416 if (pCurMgr
->isLibraryLocalized())
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
);
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
);
443 msCurrentText
= m_xWidget
->get_text(nSelPos
);
447 m_xWidget
->append_text(msNotLocalizedStr
);
449 set_sensitive(false);
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());
468 void LanguageBox::SetLanguage()
470 LanguageEntry
* pEntry
= reinterpret_cast<LanguageEntry
*>(m_xWidget
->get_active_id().toInt64());
472 GetShell()->GetCurLocalizationMgr()->handleSetCurrentLocale(pEntry
->m_aLocale
);
475 void LanguageBox::Select()
480 m_xWidget
->set_active_text(msCurrentText
); // Select after Escape
483 bool LanguageBox::HandleKeyInput(const KeyEvent
& rKEvt
)
487 sal_uInt16 nKeyCode
= rKEvt
.GetKeyCode().GetCode();
498 m_xWidget
->set_active_text(msCurrentText
);
504 return bDone
|| DocListenerBox::HandleKeyInput(rKEvt
);
507 void LanguageBox::Update(const SfxStringItem
* pItem
)
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: */