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