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>
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 
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  {
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 
67 static OUString GetCurrentMode()
68 {
69  OUString sResult;
70  if (SfxViewFrame* pViewFrame = SfxViewFrame::Current())
71  {
72  const auto xCurrentFrame = pViewFrame->GetFrame().GetFrameInterface();
73  const auto xContext = comphelper::getProcessComponentContext();
74  const auto xModuleManager = css::frame::ModuleManager::create(xContext);
75  const utl::OConfigurationTreeRoot aAppNode(
76  xContext, "org.openoffice.Office.UI.ToolbarMode/Applications/" + GetCurrentApp(), true);
77  if (aAppNode.isValid())
78  sResult = comphelper::getString(aAppNode.getNodeValue("Active"));
79  };
80  return sResult;
81 }
82 
84  : GenericDialogController(pParent, "cui/ui/toolbarmodedialog.ui", "ToolbarmodeDialog")
85  , m_pImage(m_xBuilder->weld_image("imImage"))
86  , m_pApply(m_xBuilder->weld_button("btnApply"))
87  , m_pApplyAll(m_xBuilder->weld_button("btnApplyAll"))
88  , m_pRadioButtons{ (m_xBuilder->weld_radio_button("rbButton1")),
89  (m_xBuilder->weld_radio_button("rbButton2")),
90  (m_xBuilder->weld_radio_button("rbButton3")),
91  (m_xBuilder->weld_radio_button("rbButton4")),
92  (m_xBuilder->weld_radio_button("rbButton5")),
93  (m_xBuilder->weld_radio_button("rbButton6")),
94  (m_xBuilder->weld_radio_button("rbButton7")),
95  (m_xBuilder->weld_radio_button("rbButton8")),
96  (m_xBuilder->weld_radio_button("rbButton9")) }
97  , m_pInfoLabel(m_xBuilder->weld_label("lbInfo"))
98 {
99  static_assert(SAL_N_ELEMENTS(m_pRadioButtons) == SAL_N_ELEMENTS(TOOLBARMODES_ARRAY));
100 
101  Link<weld::Toggleable&, void> aLink = LINK(this, ToolbarmodeDialog, SelectToolbarmode);
102 
103  const OUString sCurrentMode = GetCurrentMode();
105  {
106  m_pRadioButtons[i]->connect_toggled(aLink);
107  if (sCurrentMode == std::get<1>(TOOLBARMODES_ARRAY[i]))
108  {
109  m_pRadioButtons[i]->set_active(true);
110  UpdateImage(std::get<2>(TOOLBARMODES_ARRAY[i]));
111  m_pInfoLabel->set_label(CuiResId(std::get<0>(TOOLBARMODES_ARRAY[i])));
112  }
113  }
114 
115  m_pApply->set_label(CuiResId(RID_SVXSTR_UI_APPLYALL).replaceFirst("%MODULE", GetCurrentApp()));
116  m_pApply->connect_clicked(LINK(this, ToolbarmodeDialog, OnApplyClick));
117  m_pApplyAll->connect_clicked(LINK(this, ToolbarmodeDialog, OnApplyClick));
118 
119  if (!officecfg::Office::Common::Misc::ExperimentalMode::get())
120  {
121  m_pRadioButtons[nGroupedbarFull]->set_visible(false);
122  m_pRadioButtons[nContextualGroups]->set_visible(false);
123  }
124 }
125 
127 
128 static bool file_exists(const OUString& fileName)
129 {
130  osl::File aFile(fileName);
131  return aFile.open(osl_File_OpenFlag_Read) == osl::FileBase::E_None;
132 }
133 
135 {
137  {
138  if (m_pRadioButtons[i]->get_active())
139  return i;
140  }
141  return -1;
142 }
143 
144 void ToolbarmodeDialog::UpdateImage(std::u16string_view sFileName)
145 {
146  // load image
147  OUString aURL("$BRAND_BASE_DIR/$BRAND_SHARE_SUBDIR/toolbarmode/");
148  rtl::Bootstrap::expandMacros(aURL);
149  aURL += sFileName;
150  if (sFileName.empty() || !file_exists(aURL))
151  return;
152  // draw image
153  Graphic aGraphic;
154  if (GraphicFilter::LoadGraphic(aURL, OUString(), aGraphic) == ERRCODE_NONE)
155  {
156  ScopedVclPtr<VirtualDevice> m_pVirDev = m_pImage->create_virtual_device();
157  m_pVirDev->SetOutputSizePixel(aGraphic.GetSizePixel());
158  m_pVirDev->DrawBitmapEx(Point(0, 0), aGraphic.GetBitmapEx());
159  m_pImage->set_image(m_pVirDev.get());
160  m_pVirDev.disposeAndClear();
161  }
162 }
163 
165 {
166  const int i = GetActiveRadioButton();
167  if (i > -1)
168  {
169  UpdateImage(std::get<2>(TOOLBARMODES_ARRAY[i]));
170  m_pInfoLabel->set_label(CuiResId(std::get<0>(TOOLBARMODES_ARRAY[i])));
171  }
172 }
173 
174 IMPL_LINK(ToolbarmodeDialog, OnApplyClick, weld::Button&, rButton, void)
175 {
176  const int i = GetActiveRadioButton();
177  if (i == -1)
178  return;
179  const OUString sCmd = std::get<1>(TOOLBARMODES_ARRAY[i]);
180  //apply to all except current module
181  if (&rButton == m_pApplyAll.get())
182  {
183  std::shared_ptr<comphelper::ConfigurationChanges> aBatch(
185  officecfg::Office::UI::ToolbarMode::ActiveWriter::set(sCmd, aBatch);
186  officecfg::Office::UI::ToolbarMode::ActiveCalc::set(sCmd, aBatch);
187  officecfg::Office::UI::ToolbarMode::ActiveImpress::set(sCmd, aBatch);
188  officecfg::Office::UI::ToolbarMode::ActiveDraw::set(sCmd, aBatch);
189  aBatch->commit();
190 
191  OUString sCurrentApp = GetCurrentApp();
192  if (SfxViewFrame* pViewFrame = SfxViewFrame::Current())
193  {
194  const auto xCurrentFrame = pViewFrame->GetFrame().GetFrameInterface();
195  const auto xContext = comphelper::getProcessComponentContext();
196  const auto xModuleManager = css::frame::ModuleManager::create(xContext);
197  const utl::OConfigurationTreeRoot aAppNode(
198  xContext, "org.openoffice.Office.UI.ToolbarMode/Applications/", true);
199  if (sCurrentApp != "Writer")
200  aAppNode.setNodeValue("Writer/Active", css::uno::makeAny(sCmd));
201  if (sCurrentApp != "Calc")
202  aAppNode.setNodeValue("Calc/Active", css::uno::makeAny(sCmd));
203  if (sCurrentApp != "Impress")
204  aAppNode.setNodeValue("Impress/Active", css::uno::makeAny(sCmd));
205  if (sCurrentApp != "Draw")
206  aAppNode.setNodeValue("Draw/Active", css::uno::makeAny(sCmd));
207  aAppNode.commit();
208  };
209  }
210  //apply to current module
211  comphelper::dispatchCommand(".uno:ToolbarMode?Mode:string=" + sCmd, {});
212 }
213 
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
URL aURL
IMPL_LINK_NOARG(ToolbarmodeDialog, SelectToolbarmode, weld::Toggleable &, void)
std::unique_ptr< weld::Builder > m_xBuilder
Size GetSizePixel(const OutputDevice *pRefDevice=nullptr) const
static bool file_exists(const OUString &fileName)
IMPL_LINK(ToolbarmodeDialog, OnApplyClick, weld::Button &, rButton, void)
void UpdateImage(std::u16string_view sFileName)
OUString CuiResId(std::string_view aKey)
Definition: cuiresmgr.cxx:23
ToolbarmodeDialog(weld::Window *pWindow)
std::unique_ptr< weld::Image > m_pImage
unsigned long ULong
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)
static std::shared_ptr< ConfigurationChanges > create(css::uno::Reference< css::uno::XComponentContext > const &context=comphelper::getProcessComponentContext())
#define SAL_N_ELEMENTS(arr)
static ErrCode LoadGraphic(const OUString &rPath, const OUString &rFilter, Graphic &rGraphic, GraphicFilter *pFilter=nullptr, sal_uInt16 *pDeterminedFormat=nullptr)
int i
css::uno::Any getNodeValue(const OUString &_rPath) const noexcept
std::unique_ptr< weld::Label > m_pInfoLabel
std::unique_ptr< weld::RadioButton > m_pRadioButtons[9]
static Application GetApplicationEnum(const OUString &rsApplicationName)
BitmapEx GetBitmapEx(const GraphicConversionParameters &rParameters=GraphicConversionParameters()) const
std::unique_ptr< weld::Button > m_pApplyAll
static SfxViewFrame * Current()
#define ERRCODE_NONE
bool setNodeValue(const OUString &_rPath, const css::uno::Any &_rValue) const noexcept
Reference< XComponentContext > getProcessComponentContext()
static OUString GetCurrentApp()
VirtualDevice * get() const
OUString getString(const Any &_rAny)
std::unique_ptr< weld::Button > m_pApply
virtual ~ToolbarmodeDialog() override
static OUString GetCurrentMode()
bool commit() const noexcept