LibreOffice Module cui (master)  1
macroass.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 <macroass.hxx>
21 #include <o3tl/safeint.hxx>
22 #include <osl/diagnose.h>
23 #include <comphelper/string.hxx>
25 #include <svl/macitem.hxx>
26 #include <svx/svxids.hrc>
27 #include <tools/debug.hxx>
28 #include <vcl/idle.hxx>
29 #include <cfgutil.hxx>
30 #include <sfx2/evntconf.hxx>
31 #include <headertablistbox.hxx>
32 
33 using ::com::sun::star::uno::Reference;
34 using ::com::sun::star::frame::XFrame;
35 
37 {
38 public:
40 
42  std::unique_ptr<weld::Button> m_xAssignPB;
43  std::unique_ptr<weld::Button> m_xDeletePB;
44  std::unique_ptr<MacroEventListBox> m_xEventLB;
45  std::unique_ptr<weld::Widget> m_xGroupFrame;
46  std::unique_ptr<CuiConfigGroupListBox> m_xGroupLB;
47  std::unique_ptr<weld::Frame> m_xMacroFrame;
48  std::unique_ptr<CuiConfigFunctionListBox> m_xMacroLB;
49 
50  Idle m_aFillGroupIdle { "cui SfxMacroTabPage m_aFillGroupIdle" };
52 };
53 
55  : m_bGotEvents(false)
56 {
57 }
58 
59 static sal_uInt16 aPageRg[] = {
60  SID_ATTR_MACROITEM, SID_ATTR_MACROITEM,
61  0
62 };
63 
64 static OUString ConvertToUIName_Impl( SvxMacro const *pMacro )
65 {
66  OUString aName( pMacro->GetMacName() );
67  if ( pMacro->GetLanguage() != "JavaScript" )
68  {
69  const sal_Int32 nCount = comphelper::string::getTokenCount(aName, '.');
70  OUString aEntry = aName.getToken( nCount-1, '.' );
71  if ( nCount > 2 )
72  {
73  aEntry += "(" + aName.getToken( 0, '.' ) + "." + aName.getToken( nCount-2, '.' ) + ")";
74  }
75  return aEntry;
76  }
77  else
78  return aName;
79 }
80 
82 {
83  // don't do anything as long as the eventbox is empty
84  weld::TreeView& rTreeView = mpImpl->m_xEventLB->GetListBox();
85  int nSelected = rTreeView.get_selected_index();
86  if (nSelected != -1)
87  {
88  // get bound macro
89  const SvxMacro* pM = aTbl.Get(static_cast<SvMacroItemId>(rTreeView.get_selected_id().toInt32()));
90  mpImpl->m_xDeletePB->set_sensitive(nullptr != pM);
91 
92  OUString sEventMacro = rTreeView.get_text(nSelected, 1);
93 
94  OUString sScriptURI = mpImpl->m_xMacroLB->GetSelectedScriptURI();
95  mpImpl->m_xAssignPB->set_sensitive(!sScriptURI.equalsIgnoreAsciiCase(sEventMacro));
96  }
97  else
98  mpImpl->m_xAssignPB->set_sensitive(false);
99 }
100 
101 SfxMacroTabPage::SfxMacroTabPage(weld::Container* pPage, weld::DialogController* pController, const Reference< XFrame >& rxDocumentFrame, const SfxItemSet& rAttrSet )
102  : SfxTabPage(pPage, pController, "cui/ui/eventassignpage.ui", "EventAssignPage", &rAttrSet)
103 {
104  mpImpl.reset(new SfxMacroTabPage_Impl);
105 
106  mpImpl->m_aFillGroupIdle.SetInvokeHandler( LINK( this, SfxMacroTabPage, TimeOut_Impl ) );
107  mpImpl->m_aFillGroupIdle.SetPriority( TaskPriority::HIGHEST );
108 
109  mpImpl->m_xEventLB.reset(new MacroEventListBox(m_xBuilder->weld_tree_view("assignments")));
110  mpImpl->m_xAssignPB = m_xBuilder->weld_button("assign");
111  mpImpl->m_xDeletePB = m_xBuilder->weld_button("delete");
112  mpImpl->m_xGroupFrame = m_xBuilder->weld_widget("groupframe");
113  mpImpl->m_xGroupLB.reset(new CuiConfigGroupListBox(m_xBuilder->weld_tree_view("libraries")));
114  mpImpl->m_xMacroFrame = m_xBuilder->weld_frame("macroframe");
115  mpImpl->m_aStaticMacroLBLabel = mpImpl->m_xMacroFrame->get_label();
116  mpImpl->m_xMacroLB.reset(new CuiConfigFunctionListBox(m_xBuilder->weld_tree_view("macros")));
117 
118  SetFrame( rxDocumentFrame );
119 
121 
122  ScriptChanged();
123 }
124 
126 {
127  mpImpl.reset();
128 }
129 
130 void SfxMacroTabPage::AddEvent(const OUString& rEventName, SvMacroItemId nEventId)
131 {
132  weld::TreeView& rTreeView = mpImpl->m_xEventLB->GetListBox();
133  rTreeView.append(OUString::number(static_cast<sal_Int32>(nEventId)), rEventName);
134 
135  // if the table is valid already
136  SvxMacro* pM = aTbl.Get(nEventId);
137  if (pM)
138  {
139  OUString sNew(ConvertToUIName_Impl(pM));
140  rTreeView.set_text(rTreeView.n_children() - 1, sNew, 1);
141  }
142 }
143 
145 {
146  // get new areas and their functions
147  mpImpl->m_xGroupFrame->show();
148  mpImpl->m_xMacroFrame->show();
149 
150  EnableButtons();
151 }
152 
154 {
155  SvxMacroItem aItem( GetWhich( aPageRg[0] ) );
156  const_cast<SvxMacroTableDtor&>(aItem.GetMacroTable()) = aTbl;
157 
158  const SfxPoolItem* pItem;
159  if( SfxItemState::SET != GetItemSet().GetItemState( aItem.Which(), true, &pItem )
160  || aItem != *static_cast<const SvxMacroItem*>(pItem) )
161  {
162  rSet->Put( aItem );
163  return true;
164  }
165  return false;
166 }
167 
169 {
170  if (! mpImpl->m_aFillGroupIdle.IsActive() )
171  mpImpl->m_aFillGroupIdle.Start();
172 }
173 
175 {
176  LaunchFillGroup();
177 }
178 
180 {
181  const SfxPoolItem* pEventsItem;
182  if( !mpImpl->m_bGotEvents && SfxItemState::SET == aSet.GetItemState( SID_EVENTCONFIG, true, &pEventsItem ) )
183  {
184  mpImpl->m_bGotEvents = true;
185  const SfxEventNamesList& rList = static_cast<const SfxEventNamesItem*>(pEventsItem)->GetEvents();
186  for ( size_t nNo = 0, nCnt = rList.size(); nNo < nCnt; ++nNo )
187  {
188  const SfxEventName &rOwn = rList.at(nNo);
189  AddEvent( rOwn.maUIName, rOwn.mnId );
190  }
191  }
192 }
193 
195 {
196  const SfxPoolItem* pItem;
197  if( SfxItemState::SET == rSet->GetItemState( GetWhich( aPageRg[0] ), true, &pItem ))
198  aTbl = static_cast<const SvxMacroItem*>(pItem)->GetMacroTable();
199 
200  const SfxPoolItem* pEventsItem;
201  if( !mpImpl->m_bGotEvents && SfxItemState::SET == rSet->GetItemState( SID_EVENTCONFIG, true, &pEventsItem ) )
202  {
203  mpImpl->m_bGotEvents = true;
204  const SfxEventNamesList& rList = static_cast<const SfxEventNamesItem*>(pEventsItem)->GetEvents();
205  for ( size_t nNo = 0, nCnt = rList.size(); nNo < nCnt; ++nNo )
206  {
207  const SfxEventName &rOwn = rList.at(nNo);
208  AddEvent( rOwn.maUIName, rOwn.mnId );
209  }
210  }
211 
212  FillEvents();
213 
214  weld::TreeView& rListBox = mpImpl->m_xEventLB->GetListBox();
215  std::unique_ptr<weld::TreeIter> xIter(rListBox.make_iterator());
216  if (rListBox.get_iter_first(*xIter))
217  rListBox.set_cursor(*xIter);
218 }
219 
221 {
222  return false;
223 }
224 
226 {
227  weld::TreeView& rListBox = mpImpl->m_xEventLB->GetListBox();
228  int nSelected = rListBox.get_selected_index();
229  if (nSelected == -1)
230  {
231  DBG_ASSERT(nSelected != -1, "Where does the empty entry come from?");
232  return;
233  }
234 
235  ScriptChanged();
236  EnableButtons();
237 }
238 
240 {
241  mpImpl->m_xGroupLB->GroupSelected();
242  const OUString sScriptURI = mpImpl->m_xMacroLB->GetSelectedScriptURI();
243  OUString aLabelText;
244  if( !sScriptURI.isEmpty() )
245  aLabelText = mpImpl->m_aStaticMacroLBLabel;
246  mpImpl->m_xMacroFrame->set_label( aLabelText );
247 
248  EnableButtons();
249 }
250 
252 {
253  EnableButtons();
254 }
255 
256 IMPL_LINK(SfxMacroTabPage, AssignDeleteClickHdl_Impl, weld::Button&, rBtn, void)
257 {
258  AssignDeleteHdl(&rBtn);
259 }
260 
261 IMPL_LINK(SfxMacroTabPage, AssignDeleteHdl_Impl, weld::TreeView&, rBtn, bool)
262 {
263  AssignDeleteHdl(&rBtn);
264  return true;
265 }
266 
268 {
269  weld::TreeView& rListBox = mpImpl->m_xEventLB->GetListBox();
270  int nSelected = rListBox.get_selected_index();
271  if (nSelected == -1)
272  {
273  DBG_ASSERT(nSelected != -1, "Where does the empty entry come from?");
274  return;
275  }
276 
277  const bool bAssEnabled = pBtn != mpImpl->m_xDeletePB.get() && mpImpl->m_xAssignPB->get_sensitive();
278 
279  // remove from the table
280  SvMacroItemId nEvent = static_cast<SvMacroItemId>(rListBox.get_selected_id().toInt32());
281  aTbl.Erase( nEvent );
282 
283  OUString sScriptURI;
284  if( bAssEnabled )
285  {
286  sScriptURI = mpImpl->m_xMacroLB->GetSelectedScriptURI();
287  if( sScriptURI.startsWith( "vnd.sun.star.script:" ) )
288  {
289  aTbl.Insert(
290  nEvent, SvxMacro( sScriptURI, SVX_MACRO_LANGUAGE_SF ) );
291  }
292  else
293  {
294  OSL_ENSURE( false, "SfxMacroTabPage::AssignDeleteHdl_Impl: this branch is *not* dead? (out of interest: tell fs, please!)" );
295  aTbl.Insert(
296  nEvent, SvxMacro( sScriptURI, SVX_MACRO_LANGUAGE_STARBASIC ) );
297  }
298  }
299 
300  rListBox.set_text(nSelected, sScriptURI, 1);
301 
302  EnableButtons();
303 }
304 
305 IMPL_LINK( SfxMacroTabPage, TimeOut_Impl, Timer*,, void )
306 {
307  // FillMacroList() can take a long time -> show wait cursor and disable input
308  weld::Window* pDialog = GetFrameWeld();
309  // perhaps the tabpage is part of a SingleTabDialog then pDialog == nullptr
310  std::unique_ptr<weld::WaitObject> xWait(pDialog ? new weld::WaitObject(pDialog) : nullptr);
311  // fill macro list
312  mpImpl->m_xGroupLB->Init(comphelper::getProcessComponentContext(), GetFrame(),
313  OUString(), false);
314 }
315 
317 {
318  weld::TreeView& rListBox = mpImpl->m_xEventLB->GetListBox();
319  Link<weld::TreeView&,bool> aLnk(LINK(this, SfxMacroTabPage, AssignDeleteHdl_Impl));
320  mpImpl->m_xMacroLB->connect_row_activated( aLnk);
321  mpImpl->m_xDeletePB->connect_clicked(LINK(this, SfxMacroTabPage, AssignDeleteClickHdl_Impl));
322  mpImpl->m_xAssignPB->connect_clicked(LINK(this, SfxMacroTabPage, AssignDeleteClickHdl_Impl));
323  rListBox.connect_row_activated(aLnk);
324 
325  rListBox.connect_changed(LINK(this, SfxMacroTabPage, SelectEvent_Impl));
326  mpImpl->m_xGroupLB->connect_changed(LINK(this, SfxMacroTabPage, SelectGroup_Impl));
327  mpImpl->m_xMacroLB->connect_changed(LINK(this, SfxMacroTabPage, SelectMacro_Impl));
328 
329  std::vector<int> aWidths
330  {
331  o3tl::narrowing<int>(rListBox.get_approximate_digit_width() * 35)
332  };
333  rListBox.set_column_fixed_widths(aWidths);
334 
335  mpImpl->m_xEventLB->show();
336 
337  mpImpl->m_xEventLB->set_sensitive(true);
338  mpImpl->m_xGroupLB->set_sensitive(true);
339  mpImpl->m_xMacroLB->set_sensitive(true);
340 
341  mpImpl->m_xGroupLB->SetFunctionListBox(mpImpl->m_xMacroLB.get());
342 }
343 
345 {
346  weld::TreeView& rListBox = mpImpl->m_xEventLB->GetListBox();
347 
348  int nEntryCnt = rListBox.n_children();
349 
350  // get events from the table and fill the EventListBox respectively
351  for (int n = 0 ; n < nEntryCnt; ++n)
352  {
353  OUString sOld = rListBox.get_text(n, 1);
354  OUString sNew;
355  SvMacroItemId nEventId = static_cast<SvMacroItemId>(rListBox.get_id(n).toInt32());
356  if (aTbl.IsKeyValid(nEventId))
357  sNew = ConvertToUIName_Impl(aTbl.Get(nEventId));
358 
359  if (sOld == sNew)
360  continue;
361 
362  rListBox.set_text(n, sNew, 1);
363  }
364 }
365 
366 namespace
367 {
368  std::unique_ptr<SfxMacroTabPage> CreateSfxMacroTabPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rAttrSet)
369  {
370  return std::make_unique<SfxMacroTabPage>( pPage, pController, nullptr, rAttrSet );
371  }
372 }
373 
374 std::unique_ptr<SfxTabPage> SfxMacroTabPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet)
375 {
376  return CreateSfxMacroTabPage(pPage, pController, *rAttrSet);
377 }
378 
380  const Reference< XFrame >& rxDocumentFrame, const SfxItemSet& rSet)
381  : SfxSingleTabDialogController(pParent, &rSet,"cui/ui/eventassigndialog.ui",
382  "EventAssignDialog")
383 {
384  std::unique_ptr<SfxMacroTabPage> xPage = CreateSfxMacroTabPage(get_content_area(), this, rSet);
385  xPage->SetFrame(rxDocumentFrame);
386  SetTabPage(std::move(xPage));
388 }
389 
390 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void set_text(int row, const OUString &rText, int col=-1)=0
std::unique_ptr< CuiConfigFunctionListBox > m_xMacroLB
Definition: macroass.cxx:48
SvxMacro & Insert(SvMacroItemId nEvent, const SvxMacro &rMacro)
SvxMacroTableDtor aTbl
Definition: macroass.hxx:35
OUString GetLanguage() const
static std::unique_ptr< SfxTabPage > Create(weld::Container *pPage, weld::DialogController *pController, const SfxItemSet *rAttrSet)
Definition: macroass.cxx:374
sal_Int64 n
SfxMacroAssignDlg(weld::Widget *pParent, const css::uno::Reference< css::frame::XFrame > &rxDocumentFrame, const SfxItemSet &rSet)
Definition: macroass.cxx:379
SvMacroItemId
weld::Container * get_content_area()
virtual ~SfxMacroTabPage() override
Definition: macroass.cxx:125
SfxMacroTabPage(weld::Container *pPage, weld::DialogController *pController, const css::uno::Reference< css::frame::XFrame > &rxDocumentFrame, const SfxItemSet &rSet)
Definition: macroass.cxx:101
OUString m_aStaticMacroLBLabel
Definition: macroass.cxx:41
std::unique_ptr< CuiConfigGroupListBox > m_xGroupLB
Definition: macroass.cxx:46
virtual std::unique_ptr< TreeIter > make_iterator(const TreeIter *pOrig=nullptr) const =0
SfxEventName & at(size_t Index)
void connect_changed(const Link< TreeView &, void > &rLink)
SfxMacroTabPage * GetTabPage()
Definition: macroass.hxx:83
static OUString ConvertToUIName_Impl(SvxMacro const *pMacro)
Definition: macroass.cxx:64
std::unique_ptr< weld::Widget > m_xGroupFrame
Definition: macroass.cxx:45
virtual void ActivatePage(const SfxItemSet &) override
Definition: macroass.cxx:174
int nCount
virtual int n_children() const =0
virtual bool FillItemSet(SfxItemSet *rSet) override
Definition: macroass.cxx:153
virtual void set_column_fixed_widths(const std::vector< int > &rWidths)=0
virtual OUString get_id(int pos) const =0
void SetFrame(const css::uno::Reference< css::frame::XFrame > &xFrame)
virtual void Reset(const SfxItemSet *rSet) override
Definition: macroass.cxx:194
sal_Int32 getTokenCount(std::string_view rIn, char cTok)
void connect_row_activated(const Link< TreeView &, bool > &rLink)
void ScriptChanged()
Definition: macroass.cxx:144
const SfxItemSet & GetItemSet() const
void LaunchFillGroup()
Definition: macroass.cxx:168
IMPL_LINK(SfxMacroTabPage, AssignDeleteClickHdl_Impl, weld::Button &, rBtn, void)
Definition: macroass.cxx:256
SfxItemState GetItemState(sal_uInt16 nWhich, bool bSrchInParent=true, const SfxPoolItem **ppItem=nullptr) const
#define DBG_ASSERT(sCon, aError)
void InitAndSetHandler()
Definition: macroass.cxx:316
virtual int get_selected_index() const =0
std::unique_ptr< weld::Button > m_xDeletePB
Definition: macroass.cxx:43
OUString maUIName
void AddEvent(const OUString &rEventName, SvMacroItemId nEventId)
Definition: macroass.cxx:130
void Erase(SvMacroItemId nEvent)
std::unique_ptr< weld::Button > m_xAssignPB
Definition: macroass.cxx:42
void SetTabPage(std::unique_ptr< SfxTabPage > xTabPage)
virtual OUString get_text(int row, int col=-1) const =0
bool IsReadOnly() const override
Definition: macroass.cxx:220
SvMacroItemId mnId
virtual OUString get_selected_id() const =0
const SfxPoolItem * Put(const SfxPoolItem &rItem, sal_uInt16 nWhich)
bool IsKeyValid(SvMacroItemId nEvent) const
const SvxMacroTableDtor & GetMacroTable() const
void AssignDeleteHdl(const weld::Widget *)
Definition: macroass.cxx:267
const SvxMacro * Get(SvMacroItemId nEvent) const
static sal_uInt16 aPageRg[]
Definition: macroass.cxx:59
OUString aName
virtual bool get_iter_first(TreeIter &rIter) const =0
size_t size() const
Reference< XComponentContext > getProcessComponentContext()
virtual void PageCreated(const SfxAllItemSet &aSet) override
Definition: macroass.cxx:179
virtual void set_cursor(int pos)=0
std::unique_ptr< weld::Frame > m_xMacroFrame
Definition: macroass.cxx:47
void append(TreeIter *pRet=nullptr)
std::unique_ptr< SfxMacroTabPage_Impl > mpImpl
Definition: macroass.hxx:45
IMPL_LINK_NOARG(SfxMacroTabPage, SelectEvent_Impl, weld::TreeView &, void)
Definition: macroass.cxx:225
std::unique_ptr< MacroEventListBox > m_xEventLB
Definition: macroass.cxx:44
void EnableButtons()
Definition: macroass.cxx:81
weld::Window * GetFrameWeld(const SfxFrame *pFrame)
const OUString & GetMacName() const
void FillEvents()
Definition: macroass.cxx:344
sal_uInt16 Which() const
virtual float get_approximate_digit_width() const =0
sal_uInt16 GetWhich(sal_uInt16 nSlot, bool bDeep=true) const