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/lstbox.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 
44 class SvxPopupWindowListBox final : public svtools::ToolbarPopup
45 {
48 
49  DECL_LINK( SelectHdl, ListBox&, void );
50 
51 public:
53  virtual ~SvxPopupWindowListBox() override;
54  virtual void dispose() override;
55 
56  ListBox & GetListBox() { return *m_pListBox; }
57 
58  void SetInfo(sal_Int32 nCount);
59 };
60 
62  : ToolbarPopup(pControl->getFrameInterface(), pParent, "FloatingUndoRedo", "svx/ui/floatingundoredo.ui")
63  , m_xControl(pControl)
64 {
65  get(m_pListBox, "treeview");
66  WinBits nBits(m_pListBox->GetStyle());
67  nBits &= ~WB_SIMPLEMODE;
68  m_pListBox->SetStyle(nBits);
69  Size aSize(LogicToPixel(Size(100, 85), MapMode(MapUnit::MapAppFont)));
72  m_pListBox->EnableMultiSelection( true, true );
73  SetBackground( GetSettings().GetStyleSettings().GetDialogColor() );
74 
75  m_pListBox->SetSelectHdl( LINK( this, SvxPopupWindowListBox, SelectHdl ) );
76 }
77 
79 {
80  disposeOnce();
81 }
82 
84 {
85  m_pListBox.clear();
86  ToolbarPopup::dispose();
87 }
88 
89 void SvxPopupWindowListBox::SetInfo( sal_Int32 nCount )
90 {
91  const char* pId;
92  if (nCount == 1)
93  pId = m_xControl->getCommandURL() == ".uno:Undo" ? RID_SVXSTR_NUM_UNDO_ACTION : RID_SVXSTR_NUM_REDO_ACTION;
94  else
95  pId = m_xControl->getCommandURL() == ".uno:Undo" ? RID_SVXSTR_NUM_UNDO_ACTIONS : RID_SVXSTR_NUM_REDO_ACTIONS;
96  OUString aActionStr = SvxResId(pId);
97  OUString aText = aActionStr.replaceAll("$(ARG1)", OUString::number(nCount));
98  SetText(aText);
99 }
100 
101 IMPL_LINK(SvxPopupWindowListBox, SelectHdl, ListBox&, rListBox, void)
102 {
103  if (rListBox.IsTravelSelect())
104  SetInfo(rListBox.GetSelectedEntryCount());
105  else
106  {
107  m_xControl->Do(GetListBox().GetSelectedEntryCount());
108  EndPopupMode();
109  }
110 }
111 
112 void SvxUndoRedoControl::Do(sal_Int16 nCount)
113 {
114  Reference< XDispatchProvider > xDispatchProvider( m_xFrame, UNO_QUERY );
115  if ( xDispatchProvider.is() )
116  {
117  css::util::URL aTargetURL;
118  Reference < XURLTransformer > xTrans( URLTransformer::create(::comphelper::getProcessComponentContext()) );
119  aTargetURL.Complete = m_aCommandURL;
120  xTrans->parseStrict( aTargetURL );
121 
122  Reference< XDispatch > xDispatch = xDispatchProvider->queryDispatch( aTargetURL, OUString(), 0 );
123  if ( xDispatch.is() )
124  {
125  INetURLObject aObj( m_aCommandURL );
126  Sequence< PropertyValue > aArgs( 1 );
127  aArgs[0].Name = aObj.GetURLPath();
128  aArgs[0].Value <<= nCount;
129  xDispatch->dispatch(aTargetURL, aArgs);
130  }
131  }
132 }
133 
134 SvxUndoRedoControl::SvxUndoRedoControl(const css::uno::Reference<css::uno::XComponentContext>& rContext)
135  : PopupWindowController(rContext, nullptr, OUString())
136 {
137 }
138 
139 void SvxUndoRedoControl::initialize( const css::uno::Sequence< css::uno::Any >& rArguments )
140 {
141  PopupWindowController::initialize(rArguments);
142 
143  ToolBox* pToolBox = nullptr;
144  sal_uInt16 nId = 0;
145  if (getToolboxId(nId, &pToolBox) && getModuleName() != "com.sun.star.script.BasicIDE")
146  {
147  pToolBox->SetItemBits(nId, ToolBoxItemBits::DROPDOWN | pToolBox->GetItemBits(nId));
148  aDefaultTooltip = pToolBox->GetQuickHelpText(nId);
149  }
150 }
151 
153 {
154 }
155 
156 // XStatusListener
157 void SAL_CALL SvxUndoRedoControl::statusChanged(const css::frame::FeatureStateEvent& rEvent)
158 {
159  if (rEvent.FeatureURL.Main == ".uno:GetUndoStrings" || rEvent.FeatureURL.Main == ".uno:GetRedoStrings")
160  {
161  css::uno::Sequence<OUString> aStrings;
162  rEvent.State >>= aStrings;
163  aUndoRedoList = comphelper::sequenceToContainer<std::vector<OUString>>(aStrings);
164  return;
165  }
166 
167  PopupWindowController::statusChanged(rEvent);
168 
169  ToolBox* pToolBox = nullptr;
170  sal_uInt16 nId = 0;
171  if (!getToolboxId(nId, &pToolBox))
172  return;
173 
174  if (!rEvent.IsEnabled)
175  {
176  pToolBox->SetQuickHelpText(nId, aDefaultTooltip);
177  return;
178  }
179 
180  OUString aQuickHelpText;
181  if (rEvent.State >>= aQuickHelpText)
182  pToolBox->SetQuickHelpText(nId, aQuickHelpText);
183 }
184 
186 {
187  if ( m_aCommandURL == ".uno:Undo" )
188  updateStatus( ".uno:GetUndoStrings");
189  else
190  updateStatus( ".uno:GetRedoStrings");
191 
192  auto xPopupWin = VclPtr<SvxPopupWindowListBox>::Create(this, pParent);
193 
194  ListBox &rListBox = xPopupWin->GetListBox();
195 
196  for(const OUString & s : aUndoRedoList)
197  rListBox.InsertEntry( s );
198 
199  rListBox.SelectEntryPos(0);
200  xPopupWin->SetInfo(rListBox.GetSelectedEntryCount());
201 
202  return xPopupWin;
203 }
204 
206 {
207  return "com.sun.star.comp.svx.UndoRedoToolBoxControl";
208 }
209 
210 css::uno::Sequence<OUString> SvxUndoRedoControl::getSupportedServiceNames()
211 {
212  return { "com.sun.star.frame.ToolbarController" };
213 }
214 
215 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
217  css::uno::XComponentContext* rContext,
218  css::uno::Sequence<css::uno::Any> const & )
219 {
220  return cppu::acquire(new SvxUndoRedoControl(rContext));
221 }
222 
223 /* 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:83
virtual ~SvxUndoRedoControl() override
Definition: lboxctrl.cxx:152
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:205
css::uno::Reference< css::lang::XComponent > m_xFrame
virtual void SAL_CALL statusChanged(const css::frame::FeatureStateEvent &rEvent) override
Definition: lboxctrl.cxx:157
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:134
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:112
void SelectEntryPos(sal_Int32 nPos, bool bSelect=true)
WinBits const WB_SIMPLEMODE
virtual ~SvxPopupWindowListBox() override
Definition: lboxctrl.cxx:78
void SetItemBits(sal_uInt16 nItemId, ToolBoxItemBits nBits)
sal_Int32 GetSelectedEntryCount() const
VclPtr< ListBox > m_pListBox
Definition: lboxctrl.cxx:46
sal_Int32 InsertEntry(const OUString &rStr, sal_Int32 nPos=LISTBOX_APPEND)
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: lboxctrl.cxx:210
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:139
Reference< XDispatch > xDispatch
ListBox & GetListBox()
Definition: lboxctrl.cxx:56
OUString aDefaultTooltip
Definition: lboxctrl.hxx:34
SvxPopupWindowListBox(SvxUndoRedoControl *pControl, vcl::Window *pParent)
Definition: lboxctrl.cxx:61
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:216
virtual VclPtr< vcl::Window > createVclPopupWindow(vcl::Window *pParent) override
Definition: lboxctrl.cxx:185
static VclPtr< reference_type > Create(Arg &&...arg)
rtl::Reference< SvxUndoRedoControl > m_xControl
Definition: lboxctrl.cxx:47
void EnableMultiSelection(bool bMulti, bool bStackSelection)
Reference< XComponentContext > getProcessComponentContext()
IMPL_LINK(SvxPopupWindowListBox, SelectHdl, ListBox &, rListBox, void)
Definition: lboxctrl.cxx:101
WinBits GetStyle() const
OUString aTargetURL
void SetInfo(sal_Int32 nCount)
Definition: lboxctrl.cxx:89