LibreOffice Module svx (master)  1
lboxctrl.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 <sal/config.h>
21 
22 #include <sal/types.h>
23 #include <vcl/event.hxx>
24 #include <vcl/toolbox.hxx>
25 #include <sfx2/bindings.hxx>
26 #include <svtools/toolbarmenu.hxx>
27 #include <svx/dialmgr.hxx>
28 #include <svx/lboxctrl.hxx>
29 #include <vcl/settings.hxx>
30 #include <tools/urlobj.hxx>
31 
32 #include <svx/strings.hrc>
33 
35 #include <com/sun/star/util/URLTransformer.hpp>
36 #include <com/sun/star/frame/XDispatchProvider.hpp>
37 
38 
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::util;
42 using namespace ::com::sun::star::frame;
43 
45 {
47  std::unique_ptr<weld::TreeView> m_xListBox;
48  std::unique_ptr<weld::TreeIter> m_xScratchIter;
51 
52  void UpdateRow(int nRow);
53 
54  DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
55  DECL_LINK(ActivateHdl, weld::TreeView&, bool);
56  DECL_LINK(MouseMoveHdl, const MouseEvent&, bool);
57  DECL_LINK(MousePressHdl, const MouseEvent&, bool);
58  DECL_LINK(MouseReleaseHdl, const MouseEvent&, bool);
59 
60 public:
62  const std::vector<OUString>& rUndoRedoList);
63 
64  virtual void GrabFocus() override
65  {
66  m_xListBox->grab_focus();
67  }
68 };
69 
71  const std::vector<OUString>& rUndoRedoList)
72  : WeldToolbarPopup(pControl->getFrameInterface(), pParent, "svx/ui/floatingundoredo.ui", "FloatingUndoRedo")
73  , m_xControl(pControl)
74  , m_xListBox(m_xBuilder->weld_tree_view("treeview"))
75  , m_xScratchIter(m_xListBox->make_iterator())
76  , m_nVisRows(10)
77 {
78  m_xListBox->set_selection_mode(SelectionMode::Multiple);
79 
80  for (const OUString& s : rUndoRedoList)
81  m_xListBox->append_text(s);
82  if (!rUndoRedoList.empty())
83  {
84  m_xListBox->set_cursor(0);
85  m_xListBox->select(0);
86  m_nSelectedRows = 1;
87  }
88  else
89  m_nSelectedRows = 0;
90 
91  m_xListBox->set_size_request(m_xListBox->get_approximate_digit_width() * 25,
92  m_xListBox->get_height_rows(m_nVisRows) + 2);
93 
94  m_xListBox->connect_row_activated(LINK(this, SvxPopupWindowListBox, ActivateHdl));
95  m_xListBox->connect_mouse_move(LINK(this, SvxPopupWindowListBox, MouseMoveHdl));
96  m_xListBox->connect_mouse_press(LINK(this, SvxPopupWindowListBox, MousePressHdl));
97  m_xListBox->connect_mouse_release(LINK(this, SvxPopupWindowListBox, MouseReleaseHdl));
98  m_xListBox->connect_key_press(LINK(this, SvxPopupWindowListBox, KeyInputHdl));
99 }
100 
101 void SvxUndoRedoControl::SetInfo( sal_Int32 nCount )
102 {
103  const char* pId;
104  if (nCount == 1)
105  pId = getCommandURL() == ".uno:Undo" ? RID_SVXSTR_NUM_UNDO_ACTION : RID_SVXSTR_NUM_REDO_ACTION;
106  else
107  pId = getCommandURL() == ".uno:Undo" ? RID_SVXSTR_NUM_UNDO_ACTIONS : RID_SVXSTR_NUM_REDO_ACTIONS;
108  OUString aActionStr = SvxResId(pId);
109  OUString aText = aActionStr.replaceAll("$(ARG1)", OUString::number(nCount));
110  SetText(aText);
111 }
112 
114 {
115  int nOldSelectedRows = m_nSelectedRows;
116  while (m_nSelectedRows < nRow + 1)
117  {
118  m_xListBox->select(m_nSelectedRows++);
119  }
120  while (m_nSelectedRows - 1 > nRow)
121  {
122  m_xListBox->unselect(--m_nSelectedRows);
123  }
124  if (nOldSelectedRows != m_nSelectedRows)
125  m_xControl->SetInfo(m_nSelectedRows);
126 }
127 
128 IMPL_LINK(SvxPopupWindowListBox, MouseMoveHdl, const MouseEvent&, rMEvt, bool)
129 {
130  if (m_xListBox->get_dest_row_at_pos(rMEvt.GetPosPixel(), m_xScratchIter.get(), false))
131  UpdateRow(m_xListBox->get_iter_index_in_parent(*m_xScratchIter));
132  return false;
133 }
134 
135 IMPL_LINK(SvxPopupWindowListBox, MousePressHdl, const MouseEvent&, rMEvt, bool)
136 {
137  if (m_xListBox->get_dest_row_at_pos(rMEvt.GetPosPixel(), m_xScratchIter.get(), false))
138  {
139  UpdateRow(m_xListBox->get_iter_index_in_parent(*m_xScratchIter));
140  ActivateHdl(*m_xListBox);
141  }
142  return true;
143 }
144 
145 IMPL_LINK(SvxPopupWindowListBox, MouseReleaseHdl, const MouseEvent&, rMEvt, bool)
146 {
147  if (m_xListBox->get_dest_row_at_pos(rMEvt.GetPosPixel(), m_xScratchIter.get(), false))
148  UpdateRow(m_xListBox->get_iter_index_in_parent(*m_xScratchIter));
149  return true;
150 }
151 
152 IMPL_LINK(SvxPopupWindowListBox, KeyInputHdl, const KeyEvent&, rKEvt, bool)
153 {
154  const vcl::KeyCode& rKCode = rKEvt.GetKeyCode();
155  if (rKCode.GetModifier()) // only with no modifiers held
156  return true;
157 
158  sal_uInt16 nCode = rKCode.GetCode();
159 
160  if (nCode == KEY_UP || nCode == KEY_PAGEUP ||
161  nCode == KEY_DOWN || nCode == KEY_PAGEDOWN)
162  {
163  sal_Int32 nIndex = m_nSelectedRows - 1;
164  sal_Int32 nOrigIndex = nIndex;
165  sal_Int32 nCount = m_xListBox->n_children();
166 
167  if (nCode == KEY_UP)
168  --nIndex;
169  else if (nCode == KEY_DOWN)
170  ++nIndex;
171  else if (nCode == KEY_PAGEUP)
172  nIndex -= m_nVisRows;
173  else if (nCode == KEY_PAGEDOWN)
174  nIndex += m_nVisRows;
175 
176  if (nIndex < 0)
177  nIndex = 0;
178  if (nIndex >= nCount)
179  nIndex = nCount - 1;
180 
181  if (nIndex != nOrigIndex)
182  {
183  m_xListBox->scroll_to_row(nIndex);
184  if (nIndex > nOrigIndex)
185  {
186  for (int i = nOrigIndex + 1; i <= nIndex; ++i)
187  UpdateRow(i);
188  }
189  else
190  {
191  for (int i = nOrigIndex - 1; i >= nIndex; --i)
192  UpdateRow(i);
193  }
194  }
195  return true;
196  }
197 
198  return false;
199 }
200 
202 {
203  m_xControl->Do(m_nSelectedRows);
204  m_xControl->EndPopupMode();
205  return true;
206 }
207 
208 void SvxUndoRedoControl::Do(sal_Int16 nCount)
209 {
210  Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
211  if ( !xDispatchProvider.is() )
212  return;
213 
214  css::util::URL aTargetURL;
215  Reference < XURLTransformer > xTrans( URLTransformer::create(::comphelper::getProcessComponentContext()) );
216  aTargetURL.Complete = m_aCommandURL;
217  xTrans->parseStrict( aTargetURL );
218 
219  Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
220  if ( xDispatch.is() )
221  {
222  INetURLObject aObj( m_aCommandURL );
223  Sequence< PropertyValue > aArgs( 1 );
224  aArgs[0].Name = aObj.GetURLPath();
225  aArgs[0].Value <<= nCount;
226  xDispatch->dispatch(aTargetURL, aArgs);
227  }
228 }
229 
230 SvxUndoRedoControl::SvxUndoRedoControl(const css::uno::Reference<css::uno::XComponentContext>& rContext)
231  : PopupWindowController(rContext, nullptr, OUString())
232 {
233 }
234 
235 void SvxUndoRedoControl::initialize( const css::uno::Sequence< css::uno::Any >& rArguments )
236 {
237  PopupWindowController::initialize(rArguments);
238 
239  ToolBox* pToolBox = nullptr;
240  sal_uInt16 nId = 0;
241  if (!getToolboxId(nId, &pToolBox) && !m_pToolbar)
242  return;
243 
244  if (getModuleName() != "com.sun.star.script.BasicIDE")
245  {
246  if (pToolBox)
247  pToolBox->SetItemBits(nId, ToolBoxItemBits::DROPDOWN | pToolBox->GetItemBits(nId));
248  if (m_pToolbar)
249  aDefaultTooltip = m_pToolbar->get_item_tooltip_text(m_aCommandURL.toUtf8());
250  else
251  aDefaultTooltip = pToolBox->GetQuickHelpText(nId);
252  }
253 }
254 
256 {
257 }
258 
259 void SvxUndoRedoControl::SetText(const OUString& rText)
260 {
261  mxInterimPopover->SetText(rText);
262 }
263 
264 // XStatusListener
265 void SAL_CALL SvxUndoRedoControl::statusChanged(const css::frame::FeatureStateEvent& rEvent)
266 {
267  if (rEvent.FeatureURL.Main == ".uno:GetUndoStrings" || rEvent.FeatureURL.Main == ".uno:GetRedoStrings")
268  {
269  css::uno::Sequence<OUString> aStrings;
270  rEvent.State >>= aStrings;
271  aUndoRedoList = comphelper::sequenceToContainer<std::vector<OUString>>(aStrings);
272  return;
273  }
274 
275  PopupWindowController::statusChanged(rEvent);
276 
277  ToolBox* pToolBox = nullptr;
278  sal_uInt16 nId = 0;
279  if (!getToolboxId(nId, &pToolBox) && !m_pToolbar)
280  return;
281 
282  if (!rEvent.IsEnabled)
283  {
284  if (m_pToolbar)
285  m_pToolbar->set_item_tooltip_text(m_aCommandURL.toUtf8(), aDefaultTooltip);
286  else
287  pToolBox->SetQuickHelpText(nId, aDefaultTooltip);
288  return;
289  }
290 
291  OUString aQuickHelpText;
292  if (rEvent.State >>= aQuickHelpText)
293  {
294  if (m_pToolbar)
295  m_pToolbar->set_item_tooltip_text(m_aCommandURL.toUtf8(), aQuickHelpText);
296  else
297  pToolBox->SetQuickHelpText(nId, aQuickHelpText);
298  }
299 }
300 
301 std::unique_ptr<WeldToolbarPopup> SvxUndoRedoControl::weldPopupWindow()
302 {
303  if ( m_aCommandURL == ".uno:Undo" )
304  updateStatus( ".uno:GetUndoStrings");
305  else
306  updateStatus( ".uno:GetRedoStrings");
307 
308  return std::make_unique<SvxPopupWindowListBox>(this, m_pToolbar, aUndoRedoList);
309 }
310 
312 {
313  if ( m_aCommandURL == ".uno:Undo" )
314  updateStatus( ".uno:GetUndoStrings");
315  else
316  updateStatus( ".uno:GetRedoStrings");
317 
318  auto xPopupWin = std::make_unique<SvxPopupWindowListBox>(this, pParent->GetFrameWeld(), aUndoRedoList);
319 
320  mxInterimPopover = VclPtr<InterimToolbarPopup>::Create(getFrameInterface(), pParent,
321  std::move(xPopupWin));
322 
323  SetInfo(1); // count of selected rows
324 
325  mxInterimPopover->Show();
326 
327  return mxInterimPopover;
328 }
329 
331 {
332  return "com.sun.star.comp.svx.UndoRedoToolBoxControl";
333 }
334 
335 css::uno::Sequence<OUString> SvxUndoRedoControl::getSupportedServiceNames()
336 {
337  return { "com.sun.star.frame.ToolbarController" };
338 }
339 
340 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
342  css::uno::XComponentContext* rContext,
343  css::uno::Sequence<css::uno::Any> const & )
344 {
345  return cppu::acquire(new SvxUndoRedoControl(rContext));
346 }
347 
348 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
DECL_LINK(KeyInputHdl, const KeyEvent &, bool)
sal_Int32 nIndex
void SetQuickHelpText(sal_uInt16 nItemId, const OUString &rText)
virtual ~SvxUndoRedoControl() override
Definition: lboxctrl.cxx:255
Reference< XControl > m_xControl
OUString GetURLPath(DecodeMechanism eMechanism=DecodeMechanism::ToIUri, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8) const
OUString GetQuickHelpText(sal_uInt16 nItemId) const
ToolBoxItemBits GetItemBits(sal_uInt16 nItemId) const
virtual OUString SAL_CALL getImplementationName() override
Definition: lboxctrl.cxx:330
css::uno::Reference< css::lang::XComponent > m_xFrame
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &rEvent) override
Definition: lboxctrl.cxx:265
sal_Int16 nId
sal_uInt16 GetCode() const
SvxUndoRedoControl(const css::uno::Reference< css::uno::XComponentContext > &rContext)
Definition: lboxctrl.cxx:230
constexpr sal_uInt16 KEY_UP
void SetText(const OUString &rText)
Definition: lboxctrl.cxx:259
weld::Window * GetFrameWeld() const
OUString SvxResId(const char *pId)
Definition: dialmgr.cxx:28
virtual void GrabFocus() override
Definition: lboxctrl.cxx:64
int nCount
constexpr sal_uInt16 KEY_PAGEUP
sal_uInt16 GetModifier() const
IMPL_LINK(SvxPopupWindowListBox, MouseMoveHdl, const MouseEvent &, rMEvt, bool)
Definition: lboxctrl.cxx:128
sal_uInt16 nCode
constexpr sal_uInt16 KEY_DOWN
int i
SvxPopupWindowListBox(SvxUndoRedoControl *pControl, weld::Widget *pParent, const std::vector< OUString > &rUndoRedoList)
Definition: lboxctrl.cxx:70
void Do(sal_Int16 nCount)
Definition: lboxctrl.cxx:208
void SetItemBits(sal_uInt16 nItemId, ToolBoxItemBits nBits)
constexpr sal_uInt16 KEY_PAGEDOWN
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: lboxctrl.cxx:335
VclPtr< InterimToolbarPopup > mxInterimPopover
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &rArguments) override
Definition: lboxctrl.cxx:235
void SetInfo(sal_Int32 nCount)
Definition: lboxctrl.cxx:101
Reference< XDispatch > xDispatch
OUString aDefaultTooltip
Definition: lboxctrl.hxx:33
std::vector< OUString > aUndoRedoList
Definition: lboxctrl.hxx:32
std::unique_ptr< weld::TreeIter > m_xScratchIter
Definition: lboxctrl.cxx:48
std::unique_ptr< weld::TreeView > m_xListBox
Definition: lboxctrl.cxx:47
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_svx_UndoRedoToolBoxControl_get_implementation(css::uno::XComponentContext *rContext, css::uno::Sequence< css::uno::Any > const &)
Definition: lboxctrl.cxx:341
virtual VclPtr< vcl::Window > createVclPopupWindow(vcl::Window *pParent) override
Definition: lboxctrl.cxx:311
void UpdateRow(int nRow)
Definition: lboxctrl.cxx:113
static VclPtr< reference_type > Create(Arg &&...arg)
rtl::Reference< SvxUndoRedoControl > m_xControl
Definition: lboxctrl.cxx:46
Reference< XComponentContext > getProcessComponentContext()
IMPL_LINK_NOARG(SvxPopupWindowListBox, ActivateHdl, weld::TreeView &, bool)
Definition: lboxctrl.cxx:201
OUString aTargetURL
virtual std::unique_ptr< WeldToolbarPopup > weldPopupWindow() override
Definition: lboxctrl.cxx:301