1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <macroass.hxx>
22 #include <osl/diagnose.h>
23 #include <basic/basmgr.hxx>
24 #include <comphelper/string.hxx>
25 #include <comphelper/processfactory.hxx>
26 #include <svl/macitem.hxx>
27 #include <svx/dialogs.hrc>
28 #include <svx/svxids.hrc>
29 #include <tools/debug.hxx>
30 #include <vcl/idle.hxx>
31 #include <cfgutil.hxx>
32 #include <sfx2/app.hxx>
33 #include <sfx2/evntconf.hxx>
34 #include <sfx2/objsh.hxx>
35 #include <headertablistbox.hxx>
37 using ::com::sun::star::uno::Reference
;
38 using ::com::sun::star::frame::XFrame
;
40 class SfxMacroTabPage_Impl
43 SfxMacroTabPage_Impl();
45 OUString m_aStaticMacroLBLabel
;
46 std::unique_ptr
<weld::Button
> m_xAssignPB
;
47 std::unique_ptr
<weld::Button
> m_xDeletePB
;
48 std::unique_ptr
<MacroEventListBox
> m_xEventLB
;
49 std::unique_ptr
<weld::Widget
> m_xGroupFrame
;
50 std::unique_ptr
<CuiConfigGroupListBox
> m_xGroupLB
;
51 std::unique_ptr
<weld::Frame
> m_xMacroFrame
;
52 std::unique_ptr
<CuiConfigFunctionListBox
> m_xMacroLB
;
54 Idle m_aFillGroupIdle
;
58 SfxMacroTabPage_Impl::SfxMacroTabPage_Impl()
63 static sal_uInt16 aPageRg
[] = {
64 SID_ATTR_MACROITEM
, SID_ATTR_MACROITEM
,
68 static OUString
ConvertToUIName_Impl( SvxMacro
const *pMacro
)
70 OUString
aName( pMacro
->GetMacName() );
71 if ( pMacro
->GetLanguage() != "JavaScript" )
73 const sal_Int32 nCount
= comphelper::string::getTokenCount(aName
, '.');
74 OUString aEntry
= aName
.getToken( nCount
-1, '.' );
77 aEntry
+= "(" + aName
.getToken( 0, '.' ) + "." + aName
.getToken( nCount
-2, '.' ) + ")";
85 void SfxMacroTabPage::EnableButtons()
87 // don't do anything as long as the eventbox is empty
88 weld::TreeView
& rTreeView
= mpImpl
->m_xEventLB
->GetListBox();
89 int nSelected
= rTreeView
.get_selected_index();
93 const SvxMacro
* pM
= aTbl
.Get(static_cast<SvMacroItemId
>(rTreeView
.get_selected_id().toInt32()));
94 mpImpl
->m_xDeletePB
->set_sensitive(nullptr != pM
);
96 OUString sEventMacro
= rTreeView
.get_text(nSelected
, 1);
98 OUString sScriptURI
= mpImpl
->m_xMacroLB
->GetSelectedScriptURI();
99 mpImpl
->m_xAssignPB
->set_sensitive(!sScriptURI
.equalsIgnoreAsciiCase(sEventMacro
));
102 mpImpl
->m_xAssignPB
->set_sensitive(false);
105 SfxMacroTabPage::SfxMacroTabPage(TabPageParent pParent
, const Reference
< XFrame
>& rxDocumentFrame
, const SfxItemSet
& rAttrSet
)
106 : SfxTabPage(pParent
, "cui/ui/eventassignpage.ui", "EventAssignPage", &rAttrSet
)
108 mpImpl
.reset(new SfxMacroTabPage_Impl
);
110 mpImpl
->m_aFillGroupIdle
.SetInvokeHandler( LINK( this, SfxMacroTabPage
, TimeOut_Impl
) );
111 mpImpl
->m_aFillGroupIdle
.SetPriority( TaskPriority::HIGHEST
);
112 mpImpl
->m_aFillGroupIdle
.SetDebugName( "SfxMacroTabPage m_aFillGroupIdle" );
114 mpImpl
->m_xEventLB
.reset(new MacroEventListBox(m_xBuilder
->weld_tree_view("assignments")));
115 mpImpl
->m_xAssignPB
= m_xBuilder
->weld_button("assign");
116 mpImpl
->m_xDeletePB
= m_xBuilder
->weld_button("delete");
117 mpImpl
->m_xGroupFrame
= m_xBuilder
->weld_widget("groupframe");
118 mpImpl
->m_xGroupLB
.reset(new CuiConfigGroupListBox(m_xBuilder
->weld_tree_view("libraries")));
119 mpImpl
->m_xMacroFrame
= m_xBuilder
->weld_frame("macroframe");
120 mpImpl
->m_aStaticMacroLBLabel
= mpImpl
->m_xMacroFrame
->get_label();
121 mpImpl
->m_xMacroLB
.reset(new CuiConfigFunctionListBox(m_xBuilder
->weld_tree_view("macros")));
123 SetFrame( rxDocumentFrame
);
130 SfxMacroTabPage::~SfxMacroTabPage()
135 void SfxMacroTabPage::dispose()
138 SfxTabPage::dispose();
141 void SfxMacroTabPage::AddEvent(const OUString
& rEventName
, SvMacroItemId nEventId
)
143 weld::TreeView
& rTreeView
= mpImpl
->m_xEventLB
->GetListBox();
144 rTreeView
.append(OUString::number(static_cast<sal_Int32
>(nEventId
)), rEventName
);
146 // if the table is valid already
147 SvxMacro
* pM
= aTbl
.Get(nEventId
);
150 OUString
sNew(ConvertToUIName_Impl(pM
));
151 rTreeView
.set_text(rTreeView
.n_children() - 1, sNew
, 1);
155 void SfxMacroTabPage::ScriptChanged()
157 // get new areas and their functions
158 mpImpl
->m_xGroupFrame
->show();
159 mpImpl
->m_xMacroFrame
->show();
164 bool SfxMacroTabPage::FillItemSet( SfxItemSet
* rSet
)
166 SvxMacroItem
aItem( GetWhich( aPageRg
[0] ) );
167 const_cast<SvxMacroTableDtor
&>(aItem
.GetMacroTable()) = aTbl
;
169 const SfxPoolItem
* pItem
;
170 if( SfxItemState::SET
!= GetItemSet().GetItemState( aItem
.Which(), true, &pItem
)
171 || aItem
!= *static_cast<const SvxMacroItem
*>(pItem
) )
179 void SfxMacroTabPage::LaunchFillGroup()
181 if (! mpImpl
->m_aFillGroupIdle
.IsActive() )
182 mpImpl
->m_aFillGroupIdle
.Start();
185 void SfxMacroTabPage::ActivatePage( const SfxItemSet
& )
190 void SfxMacroTabPage::PageCreated(const SfxAllItemSet
& aSet
)
192 const SfxPoolItem
* pEventsItem
;
193 if( !mpImpl
->m_bGotEvents
&& SfxItemState::SET
== aSet
.GetItemState( SID_EVENTCONFIG
, true, &pEventsItem
) )
195 mpImpl
->m_bGotEvents
= true;
196 const SfxEventNamesList
& rList
= static_cast<const SfxEventNamesItem
*>(pEventsItem
)->GetEvents();
197 for ( size_t nNo
= 0, nCnt
= rList
.size(); nNo
< nCnt
; ++nNo
)
199 const SfxEventName
&rOwn
= rList
.at(nNo
);
200 AddEvent( rOwn
.maUIName
, rOwn
.mnId
);
205 void SfxMacroTabPage::Reset( const SfxItemSet
* rSet
)
207 const SfxPoolItem
* pItem
;
208 if( SfxItemState::SET
== rSet
->GetItemState( GetWhich( aPageRg
[0] ), true, &pItem
))
209 aTbl
= static_cast<const SvxMacroItem
*>(pItem
)->GetMacroTable();
211 const SfxPoolItem
* pEventsItem
;
212 if( !mpImpl
->m_bGotEvents
&& SfxItemState::SET
== rSet
->GetItemState( SID_EVENTCONFIG
, true, &pEventsItem
) )
214 mpImpl
->m_bGotEvents
= true;
215 const SfxEventNamesList
& rList
= static_cast<const SfxEventNamesItem
*>(pEventsItem
)->GetEvents();
216 for ( size_t nNo
= 0, nCnt
= rList
.size(); nNo
< nCnt
; ++nNo
)
218 const SfxEventName
&rOwn
= rList
.at(nNo
);
219 AddEvent( rOwn
.maUIName
, rOwn
.mnId
);
225 weld::TreeView
& rListBox
= mpImpl
->m_xEventLB
->GetListBox();
226 std::unique_ptr
<weld::TreeIter
> xIter(rListBox
.make_iterator());
227 if (rListBox
.get_iter_first(*xIter
))
228 rListBox
.set_cursor(*xIter
);
231 bool SfxMacroTabPage::IsReadOnly() const
236 IMPL_LINK_NOARG(SfxMacroTabPage
, SelectEvent_Impl
, weld::TreeView
&, void)
238 weld::TreeView
& rListBox
= mpImpl
->m_xEventLB
->GetListBox();
239 int nSelected
= rListBox
.get_selected_index();
242 DBG_ASSERT(nSelected
!= -1, "Where does the empty entry come from?");
250 IMPL_LINK_NOARG(SfxMacroTabPage
, SelectGroup_Impl
, weld::TreeView
&, void)
252 mpImpl
->m_xGroupLB
->GroupSelected();
253 const OUString sScriptURI
= mpImpl
->m_xMacroLB
->GetSelectedScriptURI();
255 if( !sScriptURI
.isEmpty() )
256 aLabelText
= mpImpl
->m_aStaticMacroLBLabel
;
257 mpImpl
->m_xMacroFrame
->set_label( aLabelText
);
262 IMPL_LINK_NOARG(SfxMacroTabPage
, SelectMacro_Impl
, weld::TreeView
&, void)
267 IMPL_LINK(SfxMacroTabPage
, AssignDeleteClickHdl_Impl
, weld::Button
&, rBtn
, void)
269 AssignDeleteHdl(&rBtn
);
272 IMPL_LINK(SfxMacroTabPage
, AssignDeleteHdl_Impl
, weld::TreeView
&, rBtn
, void)
274 AssignDeleteHdl(&rBtn
);
277 void SfxMacroTabPage::AssignDeleteHdl(const weld::Widget
* pBtn
)
279 weld::TreeView
& rListBox
= mpImpl
->m_xEventLB
->GetListBox();
280 int nSelected
= rListBox
.get_selected_index();
283 DBG_ASSERT(nSelected
!= -1, "Where does the empty entry come from?");
287 const bool bAssEnabled
= pBtn
!= mpImpl
->m_xDeletePB
.get() && mpImpl
->m_xAssignPB
->get_sensitive();
289 // remove from the table
290 SvMacroItemId nEvent
= static_cast<SvMacroItemId
>(rListBox
.get_selected_id().toInt32());
291 aTbl
.Erase( nEvent
);
296 sScriptURI
= mpImpl
->m_xMacroLB
->GetSelectedScriptURI();
297 if( sScriptURI
.startsWith( "vnd.sun.star.script:" ) )
300 nEvent
, SvxMacro( sScriptURI
, SVX_MACRO_LANGUAGE_SF
) );
304 OSL_ENSURE( false, "SfxMacroTabPage::AssignDeleteHdl_Impl: this branch is *not* dead? (out of interest: tell fs, please!)" );
306 nEvent
, SvxMacro( sScriptURI
, SVX_MACRO_LANGUAGE_STARBASIC
) );
310 rListBox
.set_text(nSelected
, sScriptURI
, 1);
315 IMPL_LINK( SfxMacroTabPage
, TimeOut_Impl
, Timer
*,, void )
317 // FillMacroList() can take a long time -> show wait cursor and disable input
318 weld::Window
* pDialog
= GetDialogFrameWeld();
319 // perhaps the tabpage is part of a SingleTabDialog then pDialog == nullptr
320 std::unique_ptr
<weld::WaitObject
> xWait(pDialog
? new weld::WaitObject(pDialog
) : nullptr);
322 mpImpl
->m_xGroupLB
->Init(comphelper::getProcessComponentContext(), GetFrame(),
326 void SfxMacroTabPage::InitAndSetHandler()
328 weld::TreeView
& rListBox
= mpImpl
->m_xEventLB
->GetListBox();
329 Link
<weld::TreeView
&,void> aLnk(LINK(this, SfxMacroTabPage
, AssignDeleteHdl_Impl
));
330 mpImpl
->m_xMacroLB
->connect_row_activated( aLnk
);
331 mpImpl
->m_xDeletePB
->connect_clicked(LINK(this, SfxMacroTabPage
, AssignDeleteClickHdl_Impl
));
332 mpImpl
->m_xAssignPB
->connect_clicked(LINK(this, SfxMacroTabPage
, AssignDeleteClickHdl_Impl
));
333 rListBox
.connect_row_activated(aLnk
);
335 rListBox
.connect_changed(LINK(this, SfxMacroTabPage
, SelectEvent_Impl
));
336 mpImpl
->m_xGroupLB
->connect_changed(LINK(this, SfxMacroTabPage
, SelectGroup_Impl
));
337 mpImpl
->m_xMacroLB
->connect_changed(LINK(this, SfxMacroTabPage
, SelectMacro_Impl
));
339 std::vector
<int> aWidths
;
340 aWidths
.push_back(rListBox
.get_approximate_digit_width() * 35);
341 rListBox
.set_column_fixed_widths(aWidths
);
343 mpImpl
->m_xEventLB
->show();
345 mpImpl
->m_xEventLB
->set_sensitive(true);
346 mpImpl
->m_xGroupLB
->set_sensitive(true);
347 mpImpl
->m_xMacroLB
->set_sensitive(true);
349 mpImpl
->m_xGroupLB
->SetFunctionListBox(mpImpl
->m_xMacroLB
.get());
352 void SfxMacroTabPage::FillEvents()
354 weld::TreeView
& rListBox
= mpImpl
->m_xEventLB
->GetListBox();
356 int nEntryCnt
= rListBox
.n_children();
358 // get events from the table and fill the EventListBox respectively
359 for (int n
= 0 ; n
< nEntryCnt
; ++n
)
361 OUString sOld
= rListBox
.get_text(n
, 1);
363 SvMacroItemId nEventId
= static_cast<SvMacroItemId
>(rListBox
.get_id(n
).toInt32());
364 if (aTbl
.IsKeyValid(nEventId
))
365 sNew
= ConvertToUIName_Impl(aTbl
.Get(nEventId
));
370 rListBox
.set_text(n
, sNew
, 1);
376 VclPtr
<SfxMacroTabPage
> CreateSfxMacroTabPage(TabPageParent pParent
, const SfxItemSet
& rAttrSet
)
378 return VclPtr
<SfxMacroTabPage
>::Create( pParent
, nullptr, rAttrSet
);
382 VclPtr
<SfxTabPage
> SfxMacroTabPage::Create(TabPageParent pParent
, const SfxItemSet
* rAttrSet
)
384 return CreateSfxMacroTabPage(pParent
, *rAttrSet
);
387 SfxMacroAssignDlg::SfxMacroAssignDlg(weld::Widget
* pParent
,
388 const Reference
< XFrame
>& rxDocumentFrame
, const SfxItemSet
& rSet
)
389 : SfxSingleTabDialogController(pParent
, &rSet
,"cui/ui/eventassigndialog.ui",
392 TabPageParent
pPageParent(get_content_area(), this);
393 VclPtr
<SfxMacroTabPage
> pPage
= CreateSfxMacroTabPage(pPageParent
, rSet
);
394 pPage
->SetFrame(rxDocumentFrame
);
396 pPage
->LaunchFillGroup();
399 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */