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