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 <comphelper/string.hxx>
24 #include <comphelper/processfactory.hxx>
25 #include <svl/macitem.hxx>
26 #include <svx/svxids.hrc>
27 #include <tools/debug.hxx>
28 #include <vcl/idle.hxx>
29 #include <cfgutil.hxx>
30 #include <sfx2/evntconf.hxx>
31 #include <headertablistbox.hxx>
33 using ::com::sun::star::uno::Reference
;
34 using ::com::sun::star::frame::XFrame
;
36 class SfxMacroTabPage_Impl
39 SfxMacroTabPage_Impl();
41 OUString m_aStaticMacroLBLabel
;
42 std::unique_ptr
<weld::Button
> m_xAssignPB
;
43 std::unique_ptr
<weld::Button
> m_xDeletePB
;
44 std::unique_ptr
<MacroEventListBox
> m_xEventLB
;
45 std::unique_ptr
<weld::Widget
> m_xGroupFrame
;
46 std::unique_ptr
<CuiConfigGroupListBox
> m_xGroupLB
;
47 std::unique_ptr
<weld::Frame
> m_xMacroFrame
;
48 std::unique_ptr
<CuiConfigFunctionListBox
> m_xMacroLB
;
50 Idle m_aFillGroupIdle
;
54 SfxMacroTabPage_Impl::SfxMacroTabPage_Impl()
59 static sal_uInt16 aPageRg
[] = {
60 SID_ATTR_MACROITEM
, SID_ATTR_MACROITEM
,
64 static OUString
ConvertToUIName_Impl( SvxMacro
const *pMacro
)
66 OUString
aName( pMacro
->GetMacName() );
67 if ( pMacro
->GetLanguage() != "JavaScript" )
69 const sal_Int32 nCount
= comphelper::string::getTokenCount(aName
, '.');
70 OUString aEntry
= aName
.getToken( nCount
-1, '.' );
73 aEntry
+= "(" + aName
.getToken( 0, '.' ) + "." + aName
.getToken( nCount
-2, '.' ) + ")";
81 void SfxMacroTabPage::EnableButtons()
83 // don't do anything as long as the eventbox is empty
84 weld::TreeView
& rTreeView
= mpImpl
->m_xEventLB
->GetListBox();
85 int nSelected
= rTreeView
.get_selected_index();
89 const SvxMacro
* pM
= aTbl
.Get(static_cast<SvMacroItemId
>(rTreeView
.get_selected_id().toInt32()));
90 mpImpl
->m_xDeletePB
->set_sensitive(nullptr != pM
);
92 OUString sEventMacro
= rTreeView
.get_text(nSelected
, 1);
94 OUString sScriptURI
= mpImpl
->m_xMacroLB
->GetSelectedScriptURI();
95 mpImpl
->m_xAssignPB
->set_sensitive(!sScriptURI
.equalsIgnoreAsciiCase(sEventMacro
));
98 mpImpl
->m_xAssignPB
->set_sensitive(false);
101 SfxMacroTabPage::SfxMacroTabPage(weld::Container
* pPage
, weld::DialogController
* pController
, const Reference
< XFrame
>& rxDocumentFrame
, const SfxItemSet
& rAttrSet
)
102 : SfxTabPage(pPage
, pController
, "cui/ui/eventassignpage.ui", "EventAssignPage", &rAttrSet
)
104 mpImpl
.reset(new SfxMacroTabPage_Impl
);
106 mpImpl
->m_aFillGroupIdle
.SetInvokeHandler( LINK( this, SfxMacroTabPage
, TimeOut_Impl
) );
107 mpImpl
->m_aFillGroupIdle
.SetPriority( TaskPriority::HIGHEST
);
108 mpImpl
->m_aFillGroupIdle
.SetDebugName( "SfxMacroTabPage m_aFillGroupIdle" );
110 mpImpl
->m_xEventLB
.reset(new MacroEventListBox(m_xBuilder
->weld_tree_view("assignments")));
111 mpImpl
->m_xAssignPB
= m_xBuilder
->weld_button("assign");
112 mpImpl
->m_xDeletePB
= m_xBuilder
->weld_button("delete");
113 mpImpl
->m_xGroupFrame
= m_xBuilder
->weld_widget("groupframe");
114 mpImpl
->m_xGroupLB
.reset(new CuiConfigGroupListBox(m_xBuilder
->weld_tree_view("libraries")));
115 mpImpl
->m_xMacroFrame
= m_xBuilder
->weld_frame("macroframe");
116 mpImpl
->m_aStaticMacroLBLabel
= mpImpl
->m_xMacroFrame
->get_label();
117 mpImpl
->m_xMacroLB
.reset(new CuiConfigFunctionListBox(m_xBuilder
->weld_tree_view("macros")));
119 SetFrame( rxDocumentFrame
);
126 SfxMacroTabPage::~SfxMacroTabPage()
131 void SfxMacroTabPage::AddEvent(const OUString
& rEventName
, SvMacroItemId nEventId
)
133 weld::TreeView
& rTreeView
= mpImpl
->m_xEventLB
->GetListBox();
134 rTreeView
.append(OUString::number(static_cast<sal_Int32
>(nEventId
)), rEventName
);
136 // if the table is valid already
137 SvxMacro
* pM
= aTbl
.Get(nEventId
);
140 OUString
sNew(ConvertToUIName_Impl(pM
));
141 rTreeView
.set_text(rTreeView
.n_children() - 1, sNew
, 1);
145 void SfxMacroTabPage::ScriptChanged()
147 // get new areas and their functions
148 mpImpl
->m_xGroupFrame
->show();
149 mpImpl
->m_xMacroFrame
->show();
154 bool SfxMacroTabPage::FillItemSet( SfxItemSet
* rSet
)
156 SvxMacroItem
aItem( GetWhich( aPageRg
[0] ) );
157 const_cast<SvxMacroTableDtor
&>(aItem
.GetMacroTable()) = aTbl
;
159 const SfxPoolItem
* pItem
;
160 if( SfxItemState::SET
!= GetItemSet().GetItemState( aItem
.Which(), true, &pItem
)
161 || aItem
!= *static_cast<const SvxMacroItem
*>(pItem
) )
169 void SfxMacroTabPage::LaunchFillGroup()
171 if (! mpImpl
->m_aFillGroupIdle
.IsActive() )
172 mpImpl
->m_aFillGroupIdle
.Start();
175 void SfxMacroTabPage::ActivatePage( const SfxItemSet
& )
180 void SfxMacroTabPage::PageCreated(const SfxAllItemSet
& aSet
)
182 const SfxPoolItem
* pEventsItem
;
183 if( !mpImpl
->m_bGotEvents
&& SfxItemState::SET
== aSet
.GetItemState( SID_EVENTCONFIG
, true, &pEventsItem
) )
185 mpImpl
->m_bGotEvents
= true;
186 const SfxEventNamesList
& rList
= static_cast<const SfxEventNamesItem
*>(pEventsItem
)->GetEvents();
187 for ( size_t nNo
= 0, nCnt
= rList
.size(); nNo
< nCnt
; ++nNo
)
189 const SfxEventName
&rOwn
= rList
.at(nNo
);
190 AddEvent( rOwn
.maUIName
, rOwn
.mnId
);
195 void SfxMacroTabPage::Reset( const SfxItemSet
* rSet
)
197 const SfxPoolItem
* pItem
;
198 if( SfxItemState::SET
== rSet
->GetItemState( GetWhich( aPageRg
[0] ), true, &pItem
))
199 aTbl
= static_cast<const SvxMacroItem
*>(pItem
)->GetMacroTable();
201 const SfxPoolItem
* pEventsItem
;
202 if( !mpImpl
->m_bGotEvents
&& SfxItemState::SET
== rSet
->GetItemState( SID_EVENTCONFIG
, true, &pEventsItem
) )
204 mpImpl
->m_bGotEvents
= true;
205 const SfxEventNamesList
& rList
= static_cast<const SfxEventNamesItem
*>(pEventsItem
)->GetEvents();
206 for ( size_t nNo
= 0, nCnt
= rList
.size(); nNo
< nCnt
; ++nNo
)
208 const SfxEventName
&rOwn
= rList
.at(nNo
);
209 AddEvent( rOwn
.maUIName
, rOwn
.mnId
);
215 weld::TreeView
& rListBox
= mpImpl
->m_xEventLB
->GetListBox();
216 std::unique_ptr
<weld::TreeIter
> xIter(rListBox
.make_iterator());
217 if (rListBox
.get_iter_first(*xIter
))
218 rListBox
.set_cursor(*xIter
);
221 bool SfxMacroTabPage::IsReadOnly() const
226 IMPL_LINK_NOARG(SfxMacroTabPage
, SelectEvent_Impl
, weld::TreeView
&, void)
228 weld::TreeView
& rListBox
= mpImpl
->m_xEventLB
->GetListBox();
229 int nSelected
= rListBox
.get_selected_index();
232 DBG_ASSERT(nSelected
!= -1, "Where does the empty entry come from?");
240 IMPL_LINK_NOARG(SfxMacroTabPage
, SelectGroup_Impl
, weld::TreeView
&, void)
242 mpImpl
->m_xGroupLB
->GroupSelected();
243 const OUString sScriptURI
= mpImpl
->m_xMacroLB
->GetSelectedScriptURI();
245 if( !sScriptURI
.isEmpty() )
246 aLabelText
= mpImpl
->m_aStaticMacroLBLabel
;
247 mpImpl
->m_xMacroFrame
->set_label( aLabelText
);
252 IMPL_LINK_NOARG(SfxMacroTabPage
, SelectMacro_Impl
, weld::TreeView
&, void)
257 IMPL_LINK(SfxMacroTabPage
, AssignDeleteClickHdl_Impl
, weld::Button
&, rBtn
, void)
259 AssignDeleteHdl(&rBtn
);
262 IMPL_LINK(SfxMacroTabPage
, AssignDeleteHdl_Impl
, weld::TreeView
&, rBtn
, bool)
264 AssignDeleteHdl(&rBtn
);
268 void SfxMacroTabPage::AssignDeleteHdl(const weld::Widget
* pBtn
)
270 weld::TreeView
& rListBox
= mpImpl
->m_xEventLB
->GetListBox();
271 int nSelected
= rListBox
.get_selected_index();
274 DBG_ASSERT(nSelected
!= -1, "Where does the empty entry come from?");
278 const bool bAssEnabled
= pBtn
!= mpImpl
->m_xDeletePB
.get() && mpImpl
->m_xAssignPB
->get_sensitive();
280 // remove from the table
281 SvMacroItemId nEvent
= static_cast<SvMacroItemId
>(rListBox
.get_selected_id().toInt32());
282 aTbl
.Erase( nEvent
);
287 sScriptURI
= mpImpl
->m_xMacroLB
->GetSelectedScriptURI();
288 if( sScriptURI
.startsWith( "vnd.sun.star.script:" ) )
291 nEvent
, SvxMacro( sScriptURI
, SVX_MACRO_LANGUAGE_SF
) );
295 OSL_ENSURE( false, "SfxMacroTabPage::AssignDeleteHdl_Impl: this branch is *not* dead? (out of interest: tell fs, please!)" );
297 nEvent
, SvxMacro( sScriptURI
, SVX_MACRO_LANGUAGE_STARBASIC
) );
301 rListBox
.set_text(nSelected
, sScriptURI
, 1);
306 IMPL_LINK( SfxMacroTabPage
, TimeOut_Impl
, Timer
*,, void )
308 // FillMacroList() can take a long time -> show wait cursor and disable input
309 weld::Window
* pDialog
= GetFrameWeld();
310 // perhaps the tabpage is part of a SingleTabDialog then pDialog == nullptr
311 std::unique_ptr
<weld::WaitObject
> xWait(pDialog
? new weld::WaitObject(pDialog
) : nullptr);
313 mpImpl
->m_xGroupLB
->Init(comphelper::getProcessComponentContext(), GetFrame(),
317 void SfxMacroTabPage::InitAndSetHandler()
319 weld::TreeView
& rListBox
= mpImpl
->m_xEventLB
->GetListBox();
320 Link
<weld::TreeView
&,bool> aLnk(LINK(this, SfxMacroTabPage
, AssignDeleteHdl_Impl
));
321 mpImpl
->m_xMacroLB
->connect_row_activated( aLnk
);
322 mpImpl
->m_xDeletePB
->connect_clicked(LINK(this, SfxMacroTabPage
, AssignDeleteClickHdl_Impl
));
323 mpImpl
->m_xAssignPB
->connect_clicked(LINK(this, SfxMacroTabPage
, AssignDeleteClickHdl_Impl
));
324 rListBox
.connect_row_activated(aLnk
);
326 rListBox
.connect_changed(LINK(this, SfxMacroTabPage
, SelectEvent_Impl
));
327 mpImpl
->m_xGroupLB
->connect_changed(LINK(this, SfxMacroTabPage
, SelectGroup_Impl
));
328 mpImpl
->m_xMacroLB
->connect_changed(LINK(this, SfxMacroTabPage
, SelectMacro_Impl
));
330 std::vector
<int> aWidths
;
331 aWidths
.push_back(rListBox
.get_approximate_digit_width() * 35);
332 rListBox
.set_column_fixed_widths(aWidths
);
334 mpImpl
->m_xEventLB
->show();
336 mpImpl
->m_xEventLB
->set_sensitive(true);
337 mpImpl
->m_xGroupLB
->set_sensitive(true);
338 mpImpl
->m_xMacroLB
->set_sensitive(true);
340 mpImpl
->m_xGroupLB
->SetFunctionListBox(mpImpl
->m_xMacroLB
.get());
343 void SfxMacroTabPage::FillEvents()
345 weld::TreeView
& rListBox
= mpImpl
->m_xEventLB
->GetListBox();
347 int nEntryCnt
= rListBox
.n_children();
349 // get events from the table and fill the EventListBox respectively
350 for (int n
= 0 ; n
< nEntryCnt
; ++n
)
352 OUString sOld
= rListBox
.get_text(n
, 1);
354 SvMacroItemId nEventId
= static_cast<SvMacroItemId
>(rListBox
.get_id(n
).toInt32());
355 if (aTbl
.IsKeyValid(nEventId
))
356 sNew
= ConvertToUIName_Impl(aTbl
.Get(nEventId
));
361 rListBox
.set_text(n
, sNew
, 1);
367 std::unique_ptr
<SfxMacroTabPage
> CreateSfxMacroTabPage(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
& rAttrSet
)
369 return std::make_unique
<SfxMacroTabPage
>( pPage
, pController
, nullptr, rAttrSet
);
373 std::unique_ptr
<SfxTabPage
> SfxMacroTabPage::Create(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
* rAttrSet
)
375 return CreateSfxMacroTabPage(pPage
, pController
, *rAttrSet
);
378 SfxMacroAssignDlg::SfxMacroAssignDlg(weld::Widget
* pParent
,
379 const Reference
< XFrame
>& rxDocumentFrame
, const SfxItemSet
& rSet
)
380 : SfxSingleTabDialogController(pParent
, &rSet
,"cui/ui/eventassigndialog.ui",
383 std::unique_ptr
<SfxMacroTabPage
> xPage
= CreateSfxMacroTabPage(get_content_area(), this, rSet
);
384 xPage
->SetFrame(rxDocumentFrame
);
385 SetTabPage(std::move(xPage
));
386 GetTabPage()->LaunchFillGroup();
389 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */