LibreOffice Module cui (master) 1
toolbarmodedlg.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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
10#include <sal/config.h>
11
12#include <toolbarmodedlg.hxx>
13#include <toolbarmode.hrc>
14
15#include <com/sun/star/frame/ModuleManager.hpp>
17#include <comphelper/types.hxx>
18#include <dialmgr.hxx>
19#include <officecfg/Office/Common.hxx>
20#include <officecfg/Office/UI/ToolbarMode.hxx>
21#include <osl/file.hxx>
22#include <rtl/bootstrap.hxx>
23#include <sfx2/viewfrm.hxx>
24#include <strings.hrc>
26#include <vcl/virdev.hxx>
27#include <vcl/graphicfilter.hxx>
28#include <vcl/EnumContext.hxx>
29#include <vcl/weld.hxx>
30#include <com/sun/star/beans/PropertyValue.hpp>
31
32static OUString GetCurrentApp()
33{
34 OUString sResult;
35 if (SfxViewFrame* pViewFrame = SfxViewFrame::Current())
36 {
37 const auto xCurrentFrame = pViewFrame->GetFrame().GetFrameInterface();
38 const auto xContext = comphelper::getProcessComponentContext();
39 const auto xModuleManager = css::frame::ModuleManager::create(xContext);
40 switch (vcl::EnumContext::GetApplicationEnum(xModuleManager->identify(xCurrentFrame)))
41 {
43 sResult = "Writer";
44 break;
46 sResult = "Calc";
47 break;
49 sResult = "Impress";
50 break;
52 sResult = "Draw";
53 break;
55 sResult = "Formula";
56 break;
58 sResult = "Base";
59 break;
60 default:
61 sResult = "Unsupported";
62 }
63 }
64 return sResult;
65}
66
67static OUString GetCurrentMode()
68{
69 OUString sResult;
71 {
72 const auto xContext = comphelper::getProcessComponentContext();
73 const utl::OConfigurationTreeRoot aAppNode(
74 xContext, "org.openoffice.Office.UI.ToolbarMode/Applications/" + GetCurrentApp(), true);
75 if (aAppNode.isValid())
76 sResult = comphelper::getString(aAppNode.getNodeValue("Active"));
77 };
78 return sResult;
79}
80
82 : GenericDialogController(pParent, "cui/ui/toolbarmodedialog.ui", "ToolbarmodeDialog")
83 , m_pImage(m_xBuilder->weld_image("imImage"))
84 , m_pApply(m_xBuilder->weld_button("btnApply"))
85 , m_pApplyAll(m_xBuilder->weld_button("btnApplyAll"))
86 , m_pRadioButtons{ (m_xBuilder->weld_radio_button("rbButton1")),
87 (m_xBuilder->weld_radio_button("rbButton2")),
88 (m_xBuilder->weld_radio_button("rbButton3")),
89 (m_xBuilder->weld_radio_button("rbButton4")),
90 (m_xBuilder->weld_radio_button("rbButton5")),
91 (m_xBuilder->weld_radio_button("rbButton6")),
92 (m_xBuilder->weld_radio_button("rbButton7")),
93 (m_xBuilder->weld_radio_button("rbButton8")),
94 (m_xBuilder->weld_radio_button("rbButton9")) }
95 , m_pInfoLabel(m_xBuilder->weld_label("lbInfo"))
96{
97 static_assert(SAL_N_ELEMENTS(m_pRadioButtons) == std::size(TOOLBARMODES_ARRAY));
98
99 Link<weld::Toggleable&, void> aLink = LINK(this, ToolbarmodeDialog, SelectToolbarmode);
100
101 const OUString sCurrentMode = GetCurrentMode();
102 for (std::size_t i = 0; i < std::size(m_pRadioButtons); ++i)
103 {
104 m_pRadioButtons[i]->connect_toggled(aLink);
105 if (sCurrentMode == std::get<1>(TOOLBARMODES_ARRAY[i]))
106 {
107 m_pRadioButtons[i]->set_active(true);
108 UpdateImage(std::get<2>(TOOLBARMODES_ARRAY[i]));
109 m_pInfoLabel->set_label(CuiResId(std::get<0>(TOOLBARMODES_ARRAY[i])));
110 }
111 }
112
113 m_pApply->set_label(CuiResId(RID_CUISTR_UI_APPLYALL).replaceFirst("%MODULE", GetCurrentApp()));
114 m_pApply->connect_clicked(LINK(this, ToolbarmodeDialog, OnApplyClick));
115 m_pApplyAll->connect_clicked(LINK(this, ToolbarmodeDialog, OnApplyClick));
116
117 if (!officecfg::Office::Common::Misc::ExperimentalMode::get())
118 {
119 m_pRadioButtons[nGroupedbarFull]->set_visible(false);
120 m_pRadioButtons[nContextualGroups]->set_visible(false);
121 }
122}
123
125
126static bool file_exists(const OUString& fileName)
127{
128 osl::File aFile(fileName);
129 return aFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None;
130}
131
133{
134 for (std::size_t i = 0; i < std::size(m_pRadioButtons); ++i)
135 {
136 if (m_pRadioButtons[i]->get_active())
137 return i;
138 }
139 return -1;
140}
141
142void ToolbarmodeDialog::UpdateImage(std::u16string_view sFileName)
143{
144 // load image
145 OUString aURL("$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/toolbarmode/");
146 rtl::Bootstrap::expandMacros(aURL);
147 aURL += sFileName;
148 if (sFileName.empty() || !file_exists(aURL))
149 return;
150 // draw image
151 Graphic aGraphic;
152 if (GraphicFilter::LoadGraphic(aURL, OUString(), aGraphic) == ERRCODE_NONE)
153 {
154 ScopedVclPtr<VirtualDevice> m_pVirDev = m_pImage->create_virtual_device();
155 m_pVirDev->SetOutputSizePixel(aGraphic.GetSizePixel());
156 m_pVirDev->DrawBitmapEx(Point(0, 0), aGraphic.GetBitmapEx());
157 m_pImage->set_image(m_pVirDev.get());
158 m_pVirDev.disposeAndClear();
159 }
160}
161
163{
164 const int i = GetActiveRadioButton();
165 if (i > -1)
166 {
167 UpdateImage(std::get<2>(TOOLBARMODES_ARRAY[i]));
168 m_pInfoLabel->set_label(CuiResId(std::get<0>(TOOLBARMODES_ARRAY[i])));
169 }
170}
171
172IMPL_LINK(ToolbarmodeDialog, OnApplyClick, weld::Button&, rButton, void)
173{
174 const int i = GetActiveRadioButton();
175 if (i == -1)
176 return;
177 const OUString sCmd = std::get<1>(TOOLBARMODES_ARRAY[i]);
178 //apply to all except current module
179 if (&rButton == m_pApplyAll.get())
180 {
181 std::shared_ptr<comphelper::ConfigurationChanges> aBatch(
183 officecfg::Office::UI::ToolbarMode::ActiveWriter::set(sCmd, aBatch);
184 officecfg::Office::UI::ToolbarMode::ActiveCalc::set(sCmd, aBatch);
185 officecfg::Office::UI::ToolbarMode::ActiveImpress::set(sCmd, aBatch);
186 officecfg::Office::UI::ToolbarMode::ActiveDraw::set(sCmd, aBatch);
187 aBatch->commit();
188
189 OUString sCurrentApp = GetCurrentApp();
191 {
192 const auto xContext = comphelper::getProcessComponentContext();
193 const utl::OConfigurationTreeRoot aAppNode(
194 xContext, "org.openoffice.Office.UI.ToolbarMode/Applications/", true);
195 if (sCurrentApp != "Writer")
196 aAppNode.setNodeValue("Writer/Active", css::uno::Any(sCmd));
197 if (sCurrentApp != "Calc")
198 aAppNode.setNodeValue("Calc/Active", css::uno::Any(sCmd));
199 if (sCurrentApp != "Impress")
200 aAppNode.setNodeValue("Impress/Active", css::uno::Any(sCmd));
201 if (sCurrentApp != "Draw")
202 aAppNode.setNodeValue("Draw/Active", css::uno::Any(sCmd));
203 aAppNode.commit();
204 };
205 }
206 //apply to current module
207 comphelper::dispatchCommand(".uno:ToolbarMode?Mode:string=" + sCmd, {});
208}
209
210/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
static ErrCode LoadGraphic(const OUString &rPath, const OUString &rFilter, Graphic &rGraphic, GraphicFilter *pFilter=nullptr, sal_uInt16 *pDeterminedFormat=nullptr)
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
Size GetSizePixel(const OutputDevice *pRefDevice=nullptr) const
static SAL_WARN_UNUSED_RESULT SfxViewFrame * Current()
void UpdateImage(std::u16string_view sFileName)
std::unique_ptr< weld::Label > m_pInfoLabel
std::unique_ptr< weld::Button > m_pApplyAll
std::unique_ptr< weld::Image > m_pImage
virtual ~ToolbarmodeDialog() override
ToolbarmodeDialog(weld::Window *pWindow)
std::unique_ptr< weld::Button > m_pApply
std::unique_ptr< weld::RadioButton > m_pRadioButtons[9]
void disposeAndClear()
reference_type * get() const
static std::shared_ptr< ConfigurationChanges > create()
css::uno::Any getNodeValue(const OUString &_rPath) const noexcept
bool setNodeValue(const OUString &_rPath, const css::uno::Any &_rValue) const noexcept
bool commit() const noexcept
static Application GetApplicationEnum(const OUString &rsApplicationName)
OUString CuiResId(TranslateId aKey)
Definition: cuiresmgr.cxx:23
URL aURL
#define ERRCODE_NONE
#define SAL_N_ELEMENTS(arr)
bool dispatchCommand(const OUString &rCommand, const uno::Reference< css::frame::XFrame > &rFrame, const css::uno::Sequence< css::beans::PropertyValue > &rArguments, const uno::Reference< css::frame::XDispatchResultListener > &rListener)
Reference< XComponentContext > getProcessComponentContext()
OUString getString(const Any &_rAny)
int i
std::shared_ptr< LabelProvider > m_pInfoLabel
static OUString GetCurrentApp()
IMPL_LINK_NOARG(ToolbarmodeDialog, SelectToolbarmode, weld::Toggleable &, void)
IMPL_LINK(ToolbarmodeDialog, OnApplyClick, weld::Button &, rButton, void)
static OUString GetCurrentMode()
static bool file_exists(const OUString &fileName)