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 
35 
36 #include <com/sun/star/util/URLTransformer.hpp>
37 #include <com/sun/star/frame/XDispatchProvider.hpp>
38 
39 
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::beans;
42 using namespace ::com::sun::star::util;
43 using namespace ::com::sun::star::frame;
44 
46 {
48  std::unique_ptr<weld::TreeView> m_xListBox;
49  std::unique_ptr<weld::TreeIter> m_xScratchIter;
52 
53  void UpdateRow(int nRow);
54 
55  DECL_LINK(KeyInputHdl, const KeyEvent&, bool);
56  DECL_LINK(ActivateHdl, weld::TreeView&, bool);
57  DECL_LINK(MouseMoveHdl, const MouseEvent&, bool);
58  DECL_LINK(MousePressHdl, const MouseEvent&, bool);
59  DECL_LINK(MouseReleaseHdl, const MouseEvent&, bool);
60 
61 public:
63  const std::vector<OUString>& rUndoRedoList);
64 
65  virtual void GrabFocus() override
66  {
67  m_xListBox->grab_focus();
68  }
69 };
70 
72  const std::vector<OUString>& rUndoRedoList)
73  : WeldToolbarPopup(pControl->getFrameInterface(), pParent, "svx/ui/floatingundoredo.ui", "FloatingUndoRedo")
74  , m_xControl(pControl)
75  , m_xListBox(m_xBuilder->weld_tree_view("treeview"))
76  , m_xScratchIter(m_xListBox->make_iterator())
77  , m_nVisRows(10)
78 {
79  m_xListBox->set_selection_mode(SelectionMode::Multiple);
80 
81  for (const OUString& s : rUndoRedoList)
82  m_xListBox->append_text(s);
83  if (!rUndoRedoList.empty())
84  {
85  m_xListBox->set_cursor(0);
86  m_xListBox->select(0);
87  m_nSelectedRows = 1;
88  }
89  else
90  m_nSelectedRows = 0;
91 
92  m_xListBox->set_size_request(m_xListBox->get_approximate_digit_width() * 25,
93  m_xListBox->get_height_rows(m_nVisRows) + 2);
94 
95  m_xListBox->connect_row_activated(LINK(this, SvxPopupWindowListBox, ActivateHdl));
96  m_xListBox->connect_mouse_move(LINK(this, SvxPopupWindowListBox, MouseMoveHdl));
97  m_xListBox->connect_mouse_press(LINK(this, SvxPopupWindowListBox, MousePressHdl));
98  m_xListBox->connect_mouse_release(LINK(this, SvxPopupWindowListBox, MouseReleaseHdl));
99  m_xListBox->connect_key_press(LINK(this, SvxPopupWindowListBox, KeyInputHdl));
100 }
101 
102 void SvxUndoRedoControl::SetInfo( sal_Int32 nCount )
103 {
104  TranslateId pId;
105  if (nCount == 1)
106  pId = getCommandURL() == ".uno:Undo" ? RID_SVXSTR_NUM_UNDO_ACTION : RID_SVXSTR_NUM_REDO_ACTION;
107  else
108  pId = getCommandURL() == ".uno:Undo" ? RID_SVXSTR_NUM_UNDO_ACTIONS : RID_SVXSTR_NUM_REDO_ACTIONS;
109  OUString aActionStr = SvxResId(pId);
110  OUString aText = aActionStr.replaceAll("$(ARG1)", OUString::number(nCount));
111  SetText(aText);
112 }
113 
115 {
116  int nOldSelectedRows = m_nSelectedRows;
117  while (m_nSelectedRows < nRow + 1)
118  {
119  m_xListBox->select(m_nSelectedRows++);
120  }
121  while (m_nSelectedRows - 1 > nRow)
122  {
123  m_xListBox->unselect(--m_nSelectedRows);
124  }
125  if (nOldSelectedRows != m_nSelectedRows)
126  m_xControl->SetInfo(m_nSelectedRows);
127 }
128 
129 IMPL_LINK(SvxPopupWindowListBox, MouseMoveHdl, const MouseEvent&, rMEvt, bool)
130 {
131  if (m_xListBox->get_dest_row_at_pos(rMEvt.GetPosPixel(), m_xScratchIter.get(), false))
132  UpdateRow(m_xListBox->get_iter_index_in_parent(*m_xScratchIter));
133  return false;
134 }
135 
136 IMPL_LINK(SvxPopupWindowListBox, MousePressHdl, const MouseEvent&, rMEvt, bool)
137 {
138  if (m_xListBox->get_dest_row_at_pos(rMEvt.GetPosPixel(), m_xScratchIter.get(), false))
139  {
140  UpdateRow(m_xListBox->get_iter_index_in_parent(*m_xScratchIter));
141  ActivateHdl(*m_xListBox);
142  }
143  return true;
144 }
145 
146 IMPL_LINK(SvxPopupWindowListBox, MouseReleaseHdl, const MouseEvent&, rMEvt, bool)
147 {
148  if (m_xListBox->get_dest_row_at_pos(rMEvt.GetPosPixel(), m_xScratchIter.get(), false))
149  UpdateRow(m_xListBox->get_iter_index_in_parent(*m_xScratchIter));
150  return true;
151 }
152 
153 IMPL_LINK(SvxPopupWindowListBox, KeyInputHdl, const KeyEvent&, rKEvt, bool)
154 {
155  const vcl::KeyCode& rKCode = rKEvt.GetKeyCode();
156  if (rKCode.GetModifier()) // only with no modifiers held
157  return true;
158 
159  sal_uInt16 nCode = rKCode.GetCode();
160 
161  if (nCode == KEY_UP || nCode == KEY_PAGEUP ||
162  nCode == KEY_DOWN || nCode == KEY_PAGEDOWN)
163  {
164  sal_Int32 nIndex = m_nSelectedRows - 1;
165  sal_Int32 nOrigIndex = nIndex;
166  sal_Int32 nCount = m_xListBox->n_children();
167 
168  if (nCode == KEY_UP)
169  --nIndex;
170  else if (nCode == KEY_DOWN)
171  ++nIndex;
172  else if (nCode == KEY_PAGEUP)
173  nIndex -= m_nVisRows;
174  else if (nCode == KEY_PAGEDOWN)
175  nIndex += m_nVisRows;
176 
177  if (nIndex < 0)
178  nIndex = 0;
179  if (nIndex >= nCount)
180  nIndex = nCount - 1;
181 
182  if (nIndex != nOrigIndex)
183  {
184  m_xListBox->scroll_to_row(nIndex);
185  if (nIndex > nOrigIndex)
186  {
187  for (int i = nOrigIndex + 1; i <= nIndex; ++i)
188  UpdateRow(i);
189  }
190  else
191  {
192  for (int i = nOrigIndex - 1; i >= nIndex; --i)
193  UpdateRow(i);
194  }
195  }
196  return true;
197  }
198 
199  return false;
200 }
201 
203 {
204  m_xControl->Do(m_nSelectedRows);
205  m_xControl->EndPopupMode();
206  return true;
207 }
208 
209 void SvxUndoRedoControl::Do(sal_Int16 nCount)
210 {
211  Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
212  if ( !xDispatchProvider.is() )
213  return;
214 
215  css::util::URL aTargetURL;
216  Reference < XURLTransformer > xTrans( URLTransformer::create(::comphelper::getProcessComponentContext()) );
217  aTargetURL.Complete = m_aCommandURL;
218  xTrans->parseStrict( aTargetURL );
219 
220  Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
221  if ( xDispatch.is() )
222  {
223  INetURLObject aObj( m_aCommandURL );
224  Sequence< PropertyValue > aArgs{ comphelper::makePropertyValue(aObj.GetURLPath(), 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
css::beans::PropertyValue makePropertyValue(const OUString &rName, T &&rValue)
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:65
void SetItemBits(ToolBoxItemId nItemId, ToolBoxItemBits nBits)
int nCount
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
constexpr sal_uInt16 KEY_PAGEUP
sal_uInt16 GetModifier() const
IMPL_LINK(SvxPopupWindowListBox, MouseMoveHdl, const MouseEvent &, rMEvt, bool)
Definition: lboxctrl.cxx:129
sal_uInt16 nCode
constexpr sal_uInt16 KEY_DOWN
PopupWindowController(const css::uno::Reference< css::uno::XComponentContext > &rxContext, const css::uno::Reference< css::frame::XFrame > &xFrame, const OUString &aCommandURL)
int i
SvxPopupWindowListBox(SvxUndoRedoControl *pControl, weld::Widget *pParent, const std::vector< OUString > &rUndoRedoList)
Definition: lboxctrl.cxx:71
void Do(sal_Int16 nCount)
Definition: lboxctrl.cxx:209
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:102
Reference< XDispatch > xDispatch
OUString aDefaultTooltip
Definition: lboxctrl.hxx:32
std::unique_ptr< weld::TreeIter > m_xScratchIter
Definition: lboxctrl.cxx:49
std::unique_ptr< weld::TreeView > m_xListBox
Definition: lboxctrl.cxx:48
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:114
static VclPtr< reference_type > Create(Arg &&...arg)
rtl::Reference< SvxUndoRedoControl > m_xControl
Definition: lboxctrl.cxx:47
Reference< XComponentContext > getProcessComponentContext()
IMPL_LINK_NOARG(SvxPopupWindowListBox, ActivateHdl, weld::TreeView &, bool)
Definition: lboxctrl.cxx:202
OUString aTargetURL
OUString GetQuickHelpText(ToolBoxItemId nItemId) const
virtual std::unique_ptr< WeldToolbarPopup > weldPopupWindow() override
Definition: lboxctrl.cxx:300