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 <tools/debug.hxx>
23 #include <sal/types.h>
24 #include <vcl/lstbox.hxx>
25 #include <vcl/toolbox.hxx>
26 #include <vcl/event.hxx>
27 #include <sfx2/app.hxx>
28 #include <sfx2/bindings.hxx>
29 #include <sfx2/dispatch.hxx>
30 #include <sfx2/viewsh.hxx>
31 #include <svl/intitem.hxx>
32 #include <svl/eitem.hxx>
33 #include <svl/slstitm.hxx>
34 #include <svl/stritem.hxx>
35 #include <svtools/toolbarmenu.hxx>
36 #include <svx/dialmgr.hxx>
37 #include <svx/lboxctrl.hxx>
38 #include <vcl/mnemonic.hxx>
39 #include <vcl/settings.hxx>
40 #include <tools/urlobj.hxx>
41 
42 #include <svx/svxids.hrc>
43 #include <svx/strings.hrc>
44 
46 #include <com/sun/star/util/URLTransformer.hpp>
47 #include <com/sun/star/frame/XDispatchProvider.hpp>
48 #include <com/sun/star/frame/XFrame.hpp>
49 
50 
51 using namespace ::com::sun::star::uno;
52 using namespace ::com::sun::star::beans;
53 using namespace ::com::sun::star::util;
54 using namespace ::com::sun::star::frame;
55 
57 {
60 
61  DECL_LINK( SelectHdl, ListBox&, void );
62 
63 public:
65  virtual ~SvxPopupWindowListBox() override;
66  virtual void dispose() override;
67 
68  ListBox & GetListBox() { return *m_pListBox; }
69 
70  void SetInfo(sal_Int32 nCount);
71 };
72 
74  : ToolbarPopup(pControl->getFrameInterface(), pParent, "FloatingUndoRedo", "svx/ui/floatingundoredo.ui")
75  , m_xControl(pControl)
76 {
77  get(m_pListBox, "treeview");
78  WinBits nBits(m_pListBox->GetStyle());
79  nBits &= ~WB_SIMPLEMODE;
80  m_pListBox->SetStyle(nBits);
81  Size aSize(LogicToPixel(Size(100, 85), MapMode(MapUnit::MapAppFont)));
84  m_pListBox->EnableMultiSelection( true, true );
85  SetBackground( GetSettings().GetStyleSettings().GetDialogColor() );
86 
87  m_pListBox->SetSelectHdl( LINK( this, SvxPopupWindowListBox, SelectHdl ) );
88 }
89 
91 {
92  disposeOnce();
93 }
94 
96 {
97  m_pListBox.clear();
98  ToolbarPopup::dispose();
99 }
100 
101 void SvxPopupWindowListBox::SetInfo( sal_Int32 nCount )
102 {
103  const char* pId;
104  if (nCount == 1)
105  pId = m_xControl->getCommandURL() == ".uno:Undo" ? RID_SVXSTR_NUM_UNDO_ACTION : RID_SVXSTR_NUM_REDO_ACTION;
106  else
107  pId = m_xControl->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 
113 IMPL_LINK(SvxPopupWindowListBox, SelectHdl, ListBox&, rListBox, void)
114 {
115  if (rListBox.IsTravelSelect())
116  SetInfo(rListBox.GetSelectedEntryCount());
117  else
118  {
119  m_xControl->Do(GetListBox().GetSelectedEntryCount());
120  EndPopupMode();
121  }
122 }
123 
124 void SvxUndoRedoControl::Do(sal_Int16 nCount)
125 {
126  Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
127  if ( xDispatchProvider.is() )
128  {
129  css::util::URL aTargetURL;
130  Reference < XURLTransformer > xTrans( URLTransformer::create(::comphelper::getProcessComponentContext()) );
131  aTargetURL.Complete = m_aCommandURL;
132  xTrans->parseStrict( aTargetURL );
133 
134  Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
135  if ( xDispatch.is() )
136  {
137  INetURLObject aObj( m_aCommandURL );
138  Sequence< PropertyValue > aArgs( 1 );
139  aArgs[0].Name = aObj.GetURLPath();
140  aArgs[0].Value <<= nCount;
141  xDispatch->dispatch(aTargetURL, aArgs);
142  }
143  }
144 }
145 
146 SvxUndoRedoControl::SvxUndoRedoControl(const css::uno::Reference<css::uno::XComponentContext>& rContext)
147  : PopupWindowController(rContext, nullptr, OUString())
148 {
149 }
150 
151 void SvxUndoRedoControl::initialize( const css::uno::Sequence< css::uno::Any >& rArguments )
152 {
153  PopupWindowController::initialize(rArguments);
154 
155  ToolBox* pToolBox = nullptr;
156  sal_uInt16 nId = 0;
157  if (getToolboxId(nId, &pToolBox) && getModuleName() != "com.sun.star.script.BasicIDE")
158  {
159  pToolBox->SetItemBits(nId, ToolBoxItemBits::DROPDOWN | pToolBox->GetItemBits(nId));
160  aDefaultTooltip = pToolBox->GetQuickHelpText(nId);
161  }
162 }
163 
165 {
166 }
167 
168 // XStatusListener
169 void SAL_CALL SvxUndoRedoControl::statusChanged(const css::frame::FeatureStateEvent& rEvent)
170 {
171  if (rEvent.FeatureURL.Main == ".uno:GetUndoStrings" || rEvent.FeatureURL.Main == ".uno:GetRedoStrings")
172  {
173  css::uno::Sequence<OUString> aStrings;
174  rEvent.State >>= aStrings;
175  aUndoRedoList = comphelper::sequenceToContainer<std::vector<OUString>>(aStrings);
176  return;
177  }
178 
179  PopupWindowController::statusChanged(rEvent);
180 
181  ToolBox* pToolBox = nullptr;
182  sal_uInt16 nId = 0;
183  if (!getToolboxId(nId, &pToolBox))
184  return;
185 
186  if (!rEvent.IsEnabled)
187  {
188  pToolBox->SetQuickHelpText(nId, aDefaultTooltip);
189  return;
190  }
191 
192  OUString aQuickHelpText;
193  if (rEvent.State >>= aQuickHelpText)
194  pToolBox->SetQuickHelpText(nId, aQuickHelpText);
195 }
196 
198 {
199  if ( m_aCommandURL == ".uno:Undo" )
200  updateStatus( ".uno:GetUndoStrings");
201  else
202  updateStatus( ".uno:GetRedoStrings");
203 
204  auto xPopupWin = VclPtr<SvxPopupWindowListBox>::Create(this, pParent);
205 
206  ListBox &rListBox = xPopupWin->GetListBox();
207 
208  for(const OUString & s : aUndoRedoList)
209  rListBox.InsertEntry( s );
210 
211  rListBox.SelectEntryPos(0);
212  xPopupWin->SetInfo(rListBox.GetSelectedEntryCount());
213 
214  return xPopupWin;
215 }
216 
218 {
219  return "com.sun.star.comp.svx.UndoRedoToolBoxControl";
220 }
221 
222 css::uno::Sequence<OUString> SvxUndoRedoControl::getSupportedServiceNames()
223 {
224  return { "com.sun.star.frame.ToolbarController" };
225 }
226 
227 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
229  css::uno::XComponentContext* rContext,
230  css::uno::Sequence<css::uno::Any> const & )
231 {
232  return cppu::acquire(new SvxUndoRedoControl(rContext));
233 }
234 
235 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
long Width() const
void SetQuickHelpText(sal_uInt16 nItemId, const OUString &rText)
virtual void dispose() override
Definition: lboxctrl.cxx:95
virtual ~SvxUndoRedoControl() override
Definition: lboxctrl.cxx:164
Reference< XControl > m_xControl
long Height() const
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:217
css::uno::Reference< css::lang::XComponent > m_xFrame
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &rEvent) override
Definition: lboxctrl.cxx:169
sal_Int16 nId
void set_height_request(sal_Int32 nHeightRequest)
DECL_LINK(SelectHdl, ListBox &, void)
SvxUndoRedoControl(const css::uno::Reference< css::uno::XComponentContext > &rContext)
Definition: lboxctrl.cxx:146
sal_Int64 WinBits
OUString SvxResId(const char *pId)
Definition: dialmgr.cxx:28
void set_width_request(sal_Int32 nWidthRequest)
void Do(sal_Int16 nCount)
Definition: lboxctrl.cxx:124
void SelectEntryPos(sal_Int32 nPos, bool bSelect=true)
WinBits const WB_SIMPLEMODE
virtual ~SvxPopupWindowListBox() override
Definition: lboxctrl.cxx:90
void SetItemBits(sal_uInt16 nItemId, ToolBoxItemBits nBits)
sal_Int32 GetSelectedEntryCount() const
VclPtr< ListBox > m_pListBox
Definition: lboxctrl.cxx:58
sal_Int32 InsertEntry(const OUString &rStr, sal_Int32 nPos=LISTBOX_APPEND)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: lboxctrl.cxx:222
void SetSelectHdl(const Link< ListBox &, void > &rLink)
void SetStyle(WinBits nStyle)
virtual void SAL_CALL initialize(const css::uno::Sequence< css::uno::Any > &rArguments) override
Definition: lboxctrl.cxx:151
Reference< XDispatch > xDispatch
ListBox & GetListBox()
Definition: lboxctrl.cxx:68
OUString aDefaultTooltip
Definition: lboxctrl.hxx:34
SvxPopupWindowListBox(SvxUndoRedoControl *pControl, vcl::Window *pParent)
Definition: lboxctrl.cxx:73
std::vector< OUString > aUndoRedoList
Definition: lboxctrl.hxx:33
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:228
virtual VclPtr< vcl::Window > createVclPopupWindow(vcl::Window *pParent) override
Definition: lboxctrl.cxx:197
static VclPtr< reference_type > Create(Arg &&...arg)
rtl::Reference< SvxUndoRedoControl > m_xControl
Definition: lboxctrl.cxx:59
void EnableMultiSelection(bool bMulti, bool bStackSelection)
Reference< XComponentContext > getProcessComponentContext()
IMPL_LINK(SvxPopupWindowListBox, SelectHdl, ListBox &, rListBox, void)
Definition: lboxctrl.cxx:113
WinBits GetStyle() const
OUString aTargetURL
void SetInfo(sal_Int32 nCount)
Definition: lboxctrl.cxx:101