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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
|
/* -*- 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 <macroass.hxx>
#include <o3tl/safeint.hxx>
#include <o3tl/string_view.hxx>
#include <osl/diagnose.h>
#include <comphelper/string.hxx>
#include <comphelper/processfactory.hxx>
#include <svl/macitem.hxx>
#include <svx/svxids.hrc>
#include <tools/debug.hxx>
#include <vcl/idle.hxx>
#include <cfgutil.hxx>
#include <sfx2/evntconf.hxx>
#include <headertablistbox.hxx>
using ::com::sun::star::uno::Reference;
using ::com::sun::star::frame::XFrame;
class SfxMacroTabPage_Impl
{
public:
SfxMacroTabPage_Impl();
OUString m_aStaticMacroLBLabel;
std::unique_ptr<weld::Button> m_xAssignPB;
std::unique_ptr<weld::Button> m_xDeletePB;
std::unique_ptr<MacroEventListBox> m_xEventLB;
std::unique_ptr<weld::Widget> m_xGroupFrame;
std::unique_ptr<CuiConfigGroupListBox> m_xGroupLB;
std::unique_ptr<weld::Frame> m_xMacroFrame;
std::unique_ptr<CuiConfigFunctionListBox> m_xMacroLB;
Idle m_aFillGroupIdle { "cui SfxMacroTabPage m_aFillGroupIdle" };
bool m_bGotEvents;
};
SfxMacroTabPage_Impl::SfxMacroTabPage_Impl()
: m_bGotEvents(false)
{
}
static sal_uInt16 aPageRg[] = {
SID_ATTR_MACROITEM, SID_ATTR_MACROITEM,
0
};
static OUString ConvertToUIName_Impl( SvxMacro const *pMacro )
{
OUString aName( pMacro->GetMacName() );
if ( pMacro->GetLanguage() != "JavaScript" )
{
const sal_Int32 nCount = comphelper::string::getTokenCount(aName, '.');
OUString aEntry = aName.getToken( nCount-1, '.' );
if ( nCount > 2 )
{
aEntry += OUString::Concat("(") + o3tl::getToken(aName, 0, '.' ) + "." + o3tl::getToken(aName, nCount-2, '.' ) + ")";
}
return aEntry;
}
else
return aName;
}
void SfxMacroTabPage::EnableButtons()
{
// don't do anything as long as the eventbox is empty
weld::TreeView& rTreeView = mpImpl->m_xEventLB->GetListBox();
int nSelected = rTreeView.get_selected_index();
if (nSelected != -1)
{
// get bound macro
const SvxMacro* pM = aTbl.Get(static_cast<SvMacroItemId>(rTreeView.get_selected_id().toInt32()));
mpImpl->m_xDeletePB->set_sensitive(nullptr != pM);
OUString sEventMacro = rTreeView.get_text(nSelected, 1);
OUString sScriptURI = mpImpl->m_xMacroLB->GetSelectedScriptURI();
mpImpl->m_xAssignPB->set_sensitive(!sScriptURI.equalsIgnoreAsciiCase(sEventMacro));
}
else
mpImpl->m_xAssignPB->set_sensitive(false);
}
SfxMacroTabPage::SfxMacroTabPage(weld::Container* pPage, weld::DialogController* pController, const Reference< XFrame >& rxDocumentFrame, const SfxItemSet& rAttrSet )
: SfxTabPage(pPage, pController, "cui/ui/eventassignpage.ui", "EventAssignPage", &rAttrSet)
{
mpImpl.reset(new SfxMacroTabPage_Impl);
mpImpl->m_aFillGroupIdle.SetInvokeHandler( LINK( this, SfxMacroTabPage, TimeOut_Impl ) );
mpImpl->m_aFillGroupIdle.SetPriority( TaskPriority::HIGHEST );
mpImpl->m_xEventLB.reset(new MacroEventListBox(m_xBuilder->weld_tree_view("assignments")));
mpImpl->m_xAssignPB = m_xBuilder->weld_button("assign");
mpImpl->m_xDeletePB = m_xBuilder->weld_button("delete");
mpImpl->m_xGroupFrame = m_xBuilder->weld_widget("groupframe");
mpImpl->m_xGroupLB.reset(new CuiConfigGroupListBox(m_xBuilder->weld_tree_view("libraries")));
mpImpl->m_xMacroFrame = m_xBuilder->weld_frame("macroframe");
mpImpl->m_aStaticMacroLBLabel = mpImpl->m_xMacroFrame->get_label();
mpImpl->m_xMacroLB.reset(new CuiConfigFunctionListBox(m_xBuilder->weld_tree_view("macros")));
SetFrame( rxDocumentFrame );
InitAndSetHandler();
ScriptChanged();
}
SfxMacroTabPage::~SfxMacroTabPage()
{
mpImpl.reset();
}
void SfxMacroTabPage::AddEvent(const OUString& rEventName, SvMacroItemId nEventId)
{
weld::TreeView& rTreeView = mpImpl->m_xEventLB->GetListBox();
rTreeView.append(OUString::number(static_cast<sal_Int32>(nEventId)), rEventName);
// if the table is valid already
SvxMacro* pM = aTbl.Get(nEventId);
if (pM)
{
OUString sNew(ConvertToUIName_Impl(pM));
rTreeView.set_text(rTreeView.n_children() - 1, sNew, 1);
}
}
void SfxMacroTabPage::ScriptChanged()
{
// get new areas and their functions
mpImpl->m_xGroupFrame->show();
mpImpl->m_xMacroFrame->show();
EnableButtons();
}
bool SfxMacroTabPage::FillItemSet( SfxItemSet* rSet )
{
SvxMacroItem aItem( GetWhich( aPageRg[0] ) );
const_cast<SvxMacroTableDtor&>(aItem.GetMacroTable()) = aTbl;
const SfxPoolItem* pItem = nullptr;
SfxItemState eState = GetItemSet().GetItemState(aItem.Which(), true, &pItem);
if (eState == SfxItemState::DEFAULT && aTbl.empty())
{
// Don't touch the item set if there was no input and our table is empty.
return false;
}
if (SfxItemState::SET != eState || aItem != *static_cast<const SvxMacroItem*>(pItem))
{
rSet->Put( aItem );
return true;
}
return false;
}
void SfxMacroTabPage::LaunchFillGroup()
{
if (! mpImpl->m_aFillGroupIdle.IsActive() )
mpImpl->m_aFillGroupIdle.Start();
}
void SfxMacroTabPage::ActivatePage( const SfxItemSet& )
{
LaunchFillGroup();
}
void SfxMacroTabPage::PageCreated(const SfxAllItemSet& aSet)
{
if( mpImpl->m_bGotEvents )
return;
if( const SfxEventNamesItem* pEventsItem = aSet.GetItemIfSet( SID_EVENTCONFIG ) )
{
mpImpl->m_bGotEvents = true;
const SfxEventNamesList& rList = pEventsItem->GetEvents();
for ( size_t nNo = 0, nCnt = rList.size(); nNo < nCnt; ++nNo )
{
const SfxEventName &rOwn = rList.at(nNo);
AddEvent( rOwn.maUIName, rOwn.mnId );
}
}
}
void SfxMacroTabPage::Reset( const SfxItemSet* rSet )
{
const SfxPoolItem* pItem;
if( SfxItemState::SET == rSet->GetItemState( GetWhich( aPageRg[0] ), true, &pItem ))
aTbl = static_cast<const SvxMacroItem*>(pItem)->GetMacroTable();
const SfxEventNamesItem* pEventsItem;
if( !mpImpl->m_bGotEvents && (pEventsItem = rSet->GetItemIfSet( SID_EVENTCONFIG ) ) )
{
mpImpl->m_bGotEvents = true;
const SfxEventNamesList& rList = pEventsItem->GetEvents();
for ( size_t nNo = 0, nCnt = rList.size(); nNo < nCnt; ++nNo )
{
const SfxEventName &rOwn = rList.at(nNo);
AddEvent( rOwn.maUIName, rOwn.mnId );
}
}
FillEvents();
weld::TreeView& rListBox = mpImpl->m_xEventLB->GetListBox();
std::unique_ptr<weld::TreeIter> xIter(rListBox.make_iterator());
if (rListBox.get_iter_first(*xIter))
rListBox.set_cursor(*xIter);
}
bool SfxMacroTabPage::IsReadOnly() const
{
return false;
}
IMPL_LINK_NOARG(SfxMacroTabPage, SelectEvent_Impl, weld::TreeView&, void)
{
weld::TreeView& rListBox = mpImpl->m_xEventLB->GetListBox();
int nSelected = rListBox.get_selected_index();
if (nSelected == -1)
{
DBG_ASSERT(nSelected != -1, "Where does the empty entry come from?");
return;
}
ScriptChanged();
EnableButtons();
}
IMPL_LINK_NOARG(SfxMacroTabPage, SelectGroup_Impl, weld::TreeView&, void)
{
mpImpl->m_xGroupLB->GroupSelected();
const OUString sScriptURI = mpImpl->m_xMacroLB->GetSelectedScriptURI();
OUString aLabelText;
if( !sScriptURI.isEmpty() )
aLabelText = mpImpl->m_aStaticMacroLBLabel;
mpImpl->m_xMacroFrame->set_label( aLabelText );
EnableButtons();
}
IMPL_LINK_NOARG(SfxMacroTabPage, SelectMacro_Impl, weld::TreeView&, void)
{
EnableButtons();
}
IMPL_LINK(SfxMacroTabPage, AssignDeleteClickHdl_Impl, weld::Button&, rBtn, void)
{
AssignDeleteHdl(&rBtn);
}
IMPL_LINK(SfxMacroTabPage, AssignDeleteHdl_Impl, weld::TreeView&, rBtn, bool)
{
AssignDeleteHdl(&rBtn);
return true;
}
void SfxMacroTabPage::AssignDeleteHdl(const weld::Widget* pBtn)
{
weld::TreeView& rListBox = mpImpl->m_xEventLB->GetListBox();
int nSelected = rListBox.get_selected_index();
if (nSelected == -1)
{
DBG_ASSERT(nSelected != -1, "Where does the empty entry come from?");
return;
}
const bool bAssEnabled = pBtn != mpImpl->m_xDeletePB.get() && mpImpl->m_xAssignPB->get_sensitive();
// remove from the table
SvMacroItemId nEvent = static_cast<SvMacroItemId>(rListBox.get_selected_id().toInt32());
aTbl.Erase( nEvent );
OUString sScriptURI;
if( bAssEnabled )
{
sScriptURI = mpImpl->m_xMacroLB->GetSelectedScriptURI();
if( sScriptURI.startsWith( "vnd.sun.star.script:" ) )
{
aTbl.Insert(
nEvent, SvxMacro( sScriptURI, SVX_MACRO_LANGUAGE_SF ) );
}
else
{
OSL_ENSURE( false, "SfxMacroTabPage::AssignDeleteHdl_Impl: this branch is *not* dead? (out of interest: tell fs, please!)" );
aTbl.Insert(
nEvent, SvxMacro( sScriptURI, SVX_MACRO_LANGUAGE_STARBASIC ) );
}
}
rListBox.set_text(nSelected, sScriptURI, 1);
EnableButtons();
}
IMPL_LINK( SfxMacroTabPage, TimeOut_Impl, Timer*,, void )
{
// FillMacroList() can take a long time -> show wait cursor and disable input
weld::Window* pDialog = GetFrameWeld();
// perhaps the tabpage is part of a SingleTabDialog then pDialog == nullptr
std::unique_ptr<weld::WaitObject> xWait(pDialog ? new weld::WaitObject(pDialog) : nullptr);
// fill macro list
mpImpl->m_xGroupLB->Init(comphelper::getProcessComponentContext(), GetFrame(),
OUString(), false);
}
void SfxMacroTabPage::InitAndSetHandler()
{
weld::TreeView& rListBox = mpImpl->m_xEventLB->GetListBox();
Link<weld::TreeView&,bool> aLnk(LINK(this, SfxMacroTabPage, AssignDeleteHdl_Impl));
mpImpl->m_xMacroLB->connect_row_activated( aLnk);
mpImpl->m_xDeletePB->connect_clicked(LINK(this, SfxMacroTabPage, AssignDeleteClickHdl_Impl));
mpImpl->m_xAssignPB->connect_clicked(LINK(this, SfxMacroTabPage, AssignDeleteClickHdl_Impl));
rListBox.connect_row_activated(aLnk);
rListBox.connect_changed(LINK(this, SfxMacroTabPage, SelectEvent_Impl));
mpImpl->m_xGroupLB->connect_changed(LINK(this, SfxMacroTabPage, SelectGroup_Impl));
mpImpl->m_xMacroLB->connect_changed(LINK(this, SfxMacroTabPage, SelectMacro_Impl));
std::vector<int> aWidths
{
o3tl::narrowing<int>(rListBox.get_approximate_digit_width() * 35)
};
rListBox.set_column_fixed_widths(aWidths);
mpImpl->m_xEventLB->show();
mpImpl->m_xEventLB->set_sensitive(true);
mpImpl->m_xGroupLB->set_sensitive(true);
mpImpl->m_xMacroLB->set_sensitive(true);
mpImpl->m_xGroupLB->SetFunctionListBox(mpImpl->m_xMacroLB.get());
}
void SfxMacroTabPage::FillEvents()
{
weld::TreeView& rListBox = mpImpl->m_xEventLB->GetListBox();
int nEntryCnt = rListBox.n_children();
// get events from the table and fill the EventListBox respectively
for (int n = 0 ; n < nEntryCnt; ++n)
{
OUString sOld = rListBox.get_text(n, 1);
OUString sNew;
SvMacroItemId nEventId = static_cast<SvMacroItemId>(rListBox.get_id(n).toInt32());
if (aTbl.IsKeyValid(nEventId))
sNew = ConvertToUIName_Impl(aTbl.Get(nEventId));
if (sOld == sNew)
continue;
rListBox.set_text(n, sNew, 1);
}
}
namespace
{
std::unique_ptr<SfxMacroTabPage> CreateSfxMacroTabPage(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet& rAttrSet)
{
return std::make_unique<SfxMacroTabPage>( pPage, pController, nullptr, rAttrSet );
}
}
std::unique_ptr<SfxTabPage> SfxMacroTabPage::Create(weld::Container* pPage, weld::DialogController* pController, const SfxItemSet* rAttrSet)
{
return CreateSfxMacroTabPage(pPage, pController, *rAttrSet);
}
SfxMacroAssignDlg::SfxMacroAssignDlg(weld::Widget* pParent,
const Reference< XFrame >& rxDocumentFrame, const SfxItemSet& rSet)
: SfxSingleTabDialogController(pParent, &rSet,"cui/ui/eventassigndialog.ui",
"EventAssignDialog")
{
std::unique_ptr<SfxMacroTabPage> xPage = CreateSfxMacroTabPage(get_content_area(), this, rSet);
xPage->SetFrame(rxDocumentFrame);
SetTabPage(std::move(xPage));
GetTabPage()->LaunchFillGroup();
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|