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>
16 #include <comphelper/dispatchcommand.hxx>
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>
25 #include <unotools/confignode.hxx>
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
GetCurrentApp()32 static 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 {
42 case vcl::EnumContext::Application::Writer:
43 sResult = "Writer";
44 break;
45 case vcl::EnumContext::Application::Calc:
46 sResult = "Calc";
47 break;
48 case vcl::EnumContext::Application::Impress:
49 sResult = "Impress";
50 break;
51 case vcl::EnumContext::Application::Draw:
52 sResult = "Draw";
53 break;
54 case vcl::EnumContext::Application::Formula:
55 sResult = "Formula";
56 break;
57 case vcl::EnumContext::Application::Base:
58 sResult = "Base";
59 break;
60 default:
61 sResult = "Unsupported";
62 }
63 }
64 return sResult;
65 }
66
GetCurrentMode()67 static OUString GetCurrentMode()
68 {
69 OUString sResult;
70 if (SfxViewFrame::Current())
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(u"Active"_ustr));
77 };
78 return sResult;
79 }
80
ToolbarmodeDialog(weld::Window * pParent)81 ToolbarmodeDialog::ToolbarmodeDialog(weld::Window* pParent)
82 : GenericDialogController(pParent, u"cui/ui/toolbarmodedialog.ui"_ustr,
83 u"ToolbarmodeDialog"_ustr)
84 , m_pImage(m_xBuilder->weld_image(u"imImage"_ustr))
85 , m_pApply(m_xBuilder->weld_button(u"btnApply"_ustr))
86 , m_pApplyAll(m_xBuilder->weld_button(u"btnApplyAll"_ustr))
87 , m_pRadioButtons{ (m_xBuilder->weld_radio_button(u"rbButton1"_ustr)),
88 (m_xBuilder->weld_radio_button(u"rbButton2"_ustr)),
89 (m_xBuilder->weld_radio_button(u"rbButton3"_ustr)),
90 (m_xBuilder->weld_radio_button(u"rbButton4"_ustr)),
91 (m_xBuilder->weld_radio_button(u"rbButton5"_ustr)),
92 (m_xBuilder->weld_radio_button(u"rbButton6"_ustr)),
93 (m_xBuilder->weld_radio_button(u"rbButton7"_ustr)),
94 (m_xBuilder->weld_radio_button(u"rbButton8"_ustr)),
95 (m_xBuilder->weld_radio_button(u"rbButton9"_ustr)) }
96 , m_pInfoLabel(m_xBuilder->weld_label(u"lbInfo"_ustr))
97 {
98 static_assert(SAL_N_ELEMENTS(m_pRadioButtons) == std::size(TOOLBARMODES_ARRAY));
99
100 Link<weld::Toggleable&, void> aLink = LINK(this, ToolbarmodeDialog, SelectToolbarmode);
101
102 const OUString sCurrentMode = GetCurrentMode();
103 for (std::size_t i = 0; i < std::size(m_pRadioButtons); ++i)
104 {
105 m_pRadioButtons[i]->connect_toggled(aLink);
106 if (sCurrentMode == std::get<1>(TOOLBARMODES_ARRAY[i]))
107 {
108 m_pRadioButtons[i]->set_active(true);
109 UpdateImage(std::get<2>(TOOLBARMODES_ARRAY[i]));
110 m_pInfoLabel->set_label(CuiResId(std::get<0>(TOOLBARMODES_ARRAY[i])));
111 }
112 }
113
114 m_pApply->set_label(CuiResId(RID_CUISTR_UI_APPLYALL).replaceFirst("%MODULE", GetCurrentApp()));
115 m_pApply->connect_clicked(LINK(this, ToolbarmodeDialog, OnApplyClick));
116 m_pApplyAll->connect_clicked(LINK(this, ToolbarmodeDialog, OnApplyClick));
117
118 if (!officecfg::Office::Common::Misc::ExperimentalMode::get())
119 {
120 m_pRadioButtons[nGroupedbarFull]->set_visible(false);
121 m_pRadioButtons[nContextualGroups]->set_visible(false);
122 }
123 }
124
125 ToolbarmodeDialog::~ToolbarmodeDialog() = default;
126
file_exists(const OUString & fileName)127 static bool file_exists(const OUString& fileName)
128 {
129 osl::File aFile(fileName);
130 return aFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None;
131 }
132
GetActiveRadioButton()133 int ToolbarmodeDialog::GetActiveRadioButton()
134 {
135 for (std::size_t i = 0; i < std::size(m_pRadioButtons); ++i)
136 {
137 if (m_pRadioButtons[i]->get_active())
138 return i;
139 }
140 return -1;
141 }
142
UpdateImage(std::u16string_view sFileName)143 void ToolbarmodeDialog::UpdateImage(std::u16string_view sFileName)
144 {
145 // load image
146 OUString aURL(u"$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/toolbarmode/"_ustr);
147 rtl::Bootstrap::expandMacros(aURL);
148 aURL += sFileName;
149 if (sFileName.empty() || !file_exists(aURL))
150 return;
151 // draw image
152 Graphic aGraphic;
153 if (GraphicFilter::LoadGraphic(aURL, OUString(), aGraphic) == ERRCODE_NONE)
154 {
155 ScopedVclPtr<VirtualDevice> m_pVirDev = m_pImage->create_virtual_device();
156 m_pVirDev->SetOutputSizePixel(aGraphic.GetSizePixel());
157 m_pVirDev->DrawBitmapEx(Point(0, 0), aGraphic.GetBitmapEx());
158 m_pImage->set_image(m_pVirDev.get());
159 m_pVirDev.disposeAndClear();
160 }
161 }
162
IMPL_LINK_NOARG(ToolbarmodeDialog,SelectToolbarmode,weld::Toggleable &,void)163 IMPL_LINK_NOARG(ToolbarmodeDialog, SelectToolbarmode, weld::Toggleable&, void)
164 {
165 const int i = GetActiveRadioButton();
166 if (i > -1)
167 {
168 UpdateImage(std::get<2>(TOOLBARMODES_ARRAY[i]));
169 m_pInfoLabel->set_label(CuiResId(std::get<0>(TOOLBARMODES_ARRAY[i])));
170 }
171 }
172
IMPL_LINK(ToolbarmodeDialog,OnApplyClick,weld::Button &,rButton,void)173 IMPL_LINK(ToolbarmodeDialog, OnApplyClick, weld::Button&, rButton, void)
174 {
175 const int i = GetActiveRadioButton();
176 if (i == -1)
177 return;
178 const OUString sCmd = std::get<1>(TOOLBARMODES_ARRAY[i]);
179 //apply to all except current module
180 if (&rButton == m_pApplyAll.get())
181 {
182 std::shared_ptr<comphelper::ConfigurationChanges> aBatch(
183 comphelper::ConfigurationChanges::create());
184 officecfg::Office::UI::ToolbarMode::ActiveWriter::set(sCmd, aBatch);
185 officecfg::Office::UI::ToolbarMode::ActiveCalc::set(sCmd, aBatch);
186 officecfg::Office::UI::ToolbarMode::ActiveImpress::set(sCmd, aBatch);
187 officecfg::Office::UI::ToolbarMode::ActiveDraw::set(sCmd, aBatch);
188 aBatch->commit();
189
190 OUString sCurrentApp = GetCurrentApp();
191 if (SfxViewFrame::Current())
192 {
193 const auto xContext = comphelper::getProcessComponentContext();
194 const utl::OConfigurationTreeRoot aAppNode(
195 xContext, u"org.openoffice.Office.UI.ToolbarMode/Applications/"_ustr, true);
196 if (sCurrentApp != "Writer")
197 aAppNode.setNodeValue(u"Writer/Active"_ustr, css::uno::Any(sCmd));
198 if (sCurrentApp != "Calc")
199 aAppNode.setNodeValue(u"Calc/Active"_ustr, css::uno::Any(sCmd));
200 if (sCurrentApp != "Impress")
201 aAppNode.setNodeValue(u"Impress/Active"_ustr, css::uno::Any(sCmd));
202 if (sCurrentApp != "Draw")
203 aAppNode.setNodeValue(u"Draw/Active"_ustr, css::uno::Any(sCmd));
204 aAppNode.commit();
205 };
206 }
207 //apply to current module
208 comphelper::dispatchCommand(".uno:ToolbarMode?Mode:string=" + sCmd, {});
209 }
210
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
212