Line data Source code
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 "macroass.hxx"
21 :
22 : #include <basic/basmgr.hxx>
23 : #include <comphelper/string.hxx>
24 : #include <dialmgr.hxx>
25 : #include <svl/macitem.hxx>
26 : #include <svx/dialogs.hrc>
27 : #include <svtools/svmedit.hxx>
28 : #include "cfgutil.hxx"
29 : #include <sfx2/app.hxx>
30 : #include <sfx2/evntconf.hxx>
31 : #include <sfx2/objsh.hxx>
32 : #include "cuires.hrc"
33 : #include <vcl/fixed.hxx>
34 : #include "headertablistbox.hxx"
35 : #include "svtools/svlbitm.hxx"
36 : #include "svtools/treelistentry.hxx"
37 :
38 : using ::com::sun::star::uno::Reference;
39 : using ::com::sun::star::frame::XFrame;
40 :
41 0 : class _SfxMacroTabPage_Impl
42 : {
43 : public:
44 : _SfxMacroTabPage_Impl();
45 :
46 : OUString maStaticMacroLBLabel;
47 : PushButton* pAssignPB;
48 : PushButton* pDeletePB;
49 : OUString sStrEvent;
50 : OUString sAssignedMacro;
51 : MacroEventListBox* pEventLB;
52 : VclFrame* pGroupFrame;
53 : SfxConfigGroupListBox* pGroupLB;
54 : VclFrame* pMacroFrame;
55 : SfxConfigFunctionListBox* pMacroLB;
56 :
57 : bool bReadOnly;
58 : Timer maFillGroupTimer;
59 : bool bGotEvents;
60 : bool m_bDummyActivated; ///< has this tab page already been activated
61 : };
62 :
63 0 : _SfxMacroTabPage_Impl::_SfxMacroTabPage_Impl()
64 : : pAssignPB(NULL)
65 : , pDeletePB(NULL)
66 : , pEventLB(NULL)
67 : , pGroupFrame(NULL)
68 : , pGroupLB(NULL)
69 : , pMacroFrame(NULL)
70 : , pMacroLB(NULL)
71 : , bReadOnly(false)
72 : , bGotEvents(false)
73 0 : , m_bDummyActivated(false)
74 : {
75 0 : }
76 :
77 : static sal_uInt16 aPageRg[] = {
78 : SID_ATTR_MACROITEM, SID_ATTR_MACROITEM,
79 : 0
80 : };
81 :
82 : // attention, this array is indexed directly (0, 1, ...) in the code
83 : static long nTabs[] =
84 : {
85 : 2, // Number of Tabs
86 : 0, 90
87 : };
88 :
89 : // IDs for items in HeaderBar of EventLB
90 : #define ITEMID_EVENT 1
91 : #define ITMEID_ASSMACRO 2
92 :
93 :
94 : #define LB_MACROS_ITEMPOS 2
95 :
96 0 : OUString ConvertToUIName_Impl( SvxMacro *pMacro )
97 : {
98 0 : OUString aName( pMacro->GetMacName() );
99 0 : OUString aEntry;
100 0 : if ( pMacro->GetLanguage() != "JavaScript" )
101 : {
102 0 : sal_uInt16 nCount = comphelper::string::getTokenCount(aName, '.');
103 0 : aEntry = aName.getToken( nCount-1, '.' );
104 0 : if ( nCount > 2 )
105 : {
106 0 : aEntry += "(";
107 0 : aEntry += aName.getToken( 0, '.' );
108 0 : aEntry += ".";
109 0 : aEntry += aName.getToken( nCount-2, '.' );
110 0 : aEntry += ")";
111 : }
112 0 : return aEntry;
113 : }
114 : else
115 0 : return aName;
116 : }
117 :
118 0 : void _SfxMacroTabPage::EnableButtons()
119 : {
120 : // don't do anything as long as the eventbox is empty
121 0 : const SvTreeListEntry* pE = mpImpl->pEventLB->GetListBox().FirstSelected();
122 0 : if ( pE )
123 : {
124 : // get bound macro
125 0 : const SvxMacro* pM = aTbl.Get( (sal_uInt16)reinterpret_cast<sal_uLong>(pE->GetUserData()) );
126 0 : mpImpl->pDeletePB->Enable( 0 != pM && !mpImpl->bReadOnly );
127 :
128 0 : OUString sEventMacro = static_cast<const SvLBoxString*>(pE->GetItem( LB_MACROS_ITEMPOS ))->GetText();
129 :
130 0 : OUString sScriptURI = mpImpl->pMacroLB->GetSelectedScriptURI();
131 0 : mpImpl->pAssignPB->Enable( !mpImpl->bReadOnly && !sScriptURI.equalsIgnoreAsciiCase( sEventMacro ) );
132 : }
133 : else
134 0 : mpImpl->pAssignPB->Enable( false );
135 0 : }
136 :
137 0 : _SfxMacroTabPage::_SfxMacroTabPage(vcl::Window* pParent, const SfxItemSet& rAttrSet)
138 0 : : SfxTabPage(pParent, "EventAssignPage", "cui/ui/eventassignpage.ui", &rAttrSet)
139 : {
140 0 : mpImpl = new _SfxMacroTabPage_Impl;
141 0 : }
142 :
143 0 : _SfxMacroTabPage::~_SfxMacroTabPage()
144 : {
145 0 : DELETEZ( mpImpl );
146 0 : }
147 :
148 0 : void _SfxMacroTabPage::AddEvent( const OUString & rEventName, sal_uInt16 nEventId )
149 : {
150 0 : OUString sTmp( rEventName );
151 0 : sTmp += "\t";
152 :
153 : // if the table is valid already
154 0 : SvxMacro* pM = aTbl.Get( nEventId );
155 0 : if( pM )
156 : {
157 0 : OUString sNew( ConvertToUIName_Impl( pM ) );
158 0 : sTmp += sNew;
159 : }
160 :
161 0 : SvTreeListEntry* pE = mpImpl->pEventLB->GetListBox().InsertEntry( sTmp );
162 0 : pE->SetUserData( reinterpret_cast< void* >( sal::static_int_cast< sal_IntPtr >( nEventId )) );
163 0 : }
164 :
165 0 : void _SfxMacroTabPage::ScriptChanged()
166 : {
167 : // get new areas and their functions
168 0 : mpImpl->pGroupFrame->Show();
169 0 : mpImpl->pMacroFrame->Show();
170 :
171 0 : EnableButtons();
172 0 : }
173 :
174 0 : bool _SfxMacroTabPage::FillItemSet( SfxItemSet* rSet )
175 : {
176 0 : SvxMacroItem aItem( GetWhich( aPageRg[0] ) );
177 0 : ((SvxMacroTableDtor&)aItem.GetMacroTable()) = aTbl;
178 :
179 : const SfxPoolItem* pItem;
180 0 : if( SfxItemState::SET != GetItemSet().GetItemState( aItem.Which(), true, &pItem )
181 0 : || aItem != *static_cast<const SvxMacroItem*>(pItem) )
182 : {
183 0 : rSet->Put( aItem );
184 0 : return true;
185 : }
186 0 : return false;
187 : }
188 :
189 0 : void _SfxMacroTabPage::LaunchFillGroup()
190 : {
191 0 : if (!mpImpl->maFillGroupTimer.GetTimeoutHdl().IsSet())
192 : {
193 0 : mpImpl->maFillGroupTimer.SetTimeoutHdl( STATIC_LINK( this, _SfxMacroTabPage, TimeOut_Impl ) );
194 0 : mpImpl->maFillGroupTimer.SetTimeout( 0 );
195 0 : mpImpl->maFillGroupTimer.Start();
196 : }
197 0 : }
198 :
199 0 : void _SfxMacroTabPage::ActivatePage( const SfxItemSet& )
200 : {
201 : // fdo#57553 lazily init script providers, because it is annoying if done
202 : // on dialog open (SfxTabDialog::Start_Impl activates all tab pages once!)
203 0 : if (!mpImpl->m_bDummyActivated)
204 : {
205 0 : mpImpl->m_bDummyActivated = true;
206 0 : return;
207 : }
208 0 : LaunchFillGroup();
209 : }
210 :
211 0 : void _SfxMacroTabPage::PageCreated(const SfxAllItemSet& aSet)
212 : {
213 : const SfxPoolItem* pEventsItem;
214 0 : if( !mpImpl->bGotEvents && SfxItemState::SET == aSet.GetItemState( SID_EVENTCONFIG, true, &pEventsItem ) )
215 : {
216 0 : mpImpl->bGotEvents = true;
217 0 : const SfxEventNamesList& rList = static_cast<const SfxEventNamesItem*>(pEventsItem)->GetEvents();
218 0 : for ( size_t nNo = 0, nCnt = rList.size(); nNo < nCnt; ++nNo )
219 : {
220 0 : const SfxEventName *pOwn = rList.at(nNo);
221 0 : AddEvent( pOwn->maUIName, pOwn->mnId );
222 : }
223 : }
224 0 : }
225 :
226 0 : void _SfxMacroTabPage::Reset( const SfxItemSet* rSet )
227 : {
228 : const SfxPoolItem* pItem;
229 0 : if( SfxItemState::SET == rSet->GetItemState( GetWhich( aPageRg[0] ), true, &pItem ))
230 0 : aTbl = static_cast<const SvxMacroItem*>(pItem)->GetMacroTable();
231 :
232 : const SfxPoolItem* pEventsItem;
233 0 : if( !mpImpl->bGotEvents && SfxItemState::SET == rSet->GetItemState( SID_EVENTCONFIG, true, &pEventsItem ) )
234 : {
235 0 : mpImpl->bGotEvents = true;
236 0 : const SfxEventNamesList& rList = static_cast<const SfxEventNamesItem*>(pEventsItem)->GetEvents();
237 0 : for ( size_t nNo = 0, nCnt = rList.size(); nNo < nCnt; ++nNo )
238 : {
239 0 : const SfxEventName *pOwn = rList.at(nNo);
240 0 : AddEvent( pOwn->maUIName, pOwn->mnId );
241 : }
242 : }
243 :
244 0 : FillEvents();
245 :
246 0 : SvHeaderTabListBox& rListBox = mpImpl->pEventLB->GetListBox();
247 0 : SvTreeListEntry* pE = rListBox.GetEntry( 0 );
248 0 : if( pE )
249 0 : rListBox.SetCurEntry( pE );
250 0 : }
251 :
252 0 : bool _SfxMacroTabPage::IsReadOnly() const
253 : {
254 0 : return mpImpl->bReadOnly;
255 : }
256 :
257 0 : IMPL_STATIC_LINK( _SfxMacroTabPage, SelectEvent_Impl, SvTabListBox*, EMPTYARG )
258 : {
259 0 : _SfxMacroTabPage_Impl* pImpl = pThis->mpImpl;
260 0 : SvHeaderTabListBox& rListBox = pImpl->pEventLB->GetListBox();
261 0 : SvTreeListEntry* pE = rListBox.FirstSelected();
262 : sal_uLong nPos;
263 0 : if( !pE || LISTBOX_ENTRY_NOTFOUND ==
264 0 : ( nPos = rListBox.GetModel()->GetAbsPos( pE ) ) )
265 : {
266 : DBG_ASSERT( pE, "wo kommt der leere Eintrag her?" );
267 0 : return 0;
268 : }
269 :
270 0 : pThis->ScriptChanged();
271 0 : pThis->EnableButtons();
272 0 : return 0;
273 : }
274 :
275 0 : IMPL_STATIC_LINK( _SfxMacroTabPage, SelectGroup_Impl, ListBox*, EMPTYARG )
276 : {
277 0 : _SfxMacroTabPage_Impl* pImpl = pThis->mpImpl;
278 0 : pImpl->pGroupLB->GroupSelected();
279 0 : const OUString sScriptURI = pImpl->pMacroLB->GetSelectedScriptURI();
280 0 : OUString aLabelText;
281 0 : if( !sScriptURI.isEmpty() )
282 0 : aLabelText = pImpl->maStaticMacroLBLabel;
283 0 : pImpl->pMacroFrame->set_label( aLabelText );
284 :
285 0 : pThis->EnableButtons();
286 0 : return 0;
287 : }
288 :
289 0 : IMPL_STATIC_LINK( _SfxMacroTabPage, SelectMacro_Impl, ListBox*, EMPTYARG )
290 : {
291 0 : _SfxMacroTabPage_Impl* pImpl = pThis->mpImpl;
292 0 : pImpl->pMacroLB->FunctionSelected();
293 0 : pThis->EnableButtons();
294 0 : return 0;
295 : }
296 :
297 0 : IMPL_STATIC_LINK( _SfxMacroTabPage, AssignDeleteHdl_Impl, PushButton*, pBtn )
298 : {
299 0 : _SfxMacroTabPage_Impl* pImpl = pThis->mpImpl;
300 0 : SvHeaderTabListBox& rListBox = pImpl->pEventLB->GetListBox();
301 0 : SvTreeListEntry* pE = rListBox.FirstSelected();
302 : sal_uLong nPos;
303 0 : if( !pE || LISTBOX_ENTRY_NOTFOUND ==
304 0 : ( nPos = rListBox.GetModel()->GetAbsPos( pE ) ) )
305 : {
306 : DBG_ASSERT( pE, "wo kommt der leere Eintrag her?" );
307 0 : return 0;
308 : }
309 :
310 0 : const bool bAssEnabled = pBtn != pImpl->pDeletePB && pImpl->pAssignPB->IsEnabled();
311 :
312 : // remove from the table
313 0 : sal_uInt16 nEvent = (sal_uInt16)reinterpret_cast<sal_uLong>(pE->GetUserData());
314 0 : pThis->aTbl.Erase( nEvent );
315 :
316 0 : OUString sScriptURI;
317 0 : if( bAssEnabled )
318 : {
319 0 : sScriptURI = pImpl->pMacroLB->GetSelectedScriptURI();
320 0 : if( sScriptURI.startsWith( "vnd.sun.star.script:" ) )
321 : {
322 : pThis->aTbl.Insert(
323 0 : nEvent, SvxMacro( sScriptURI, OUString( SVX_MACRO_LANGUAGE_SF ) ) );
324 : }
325 : else
326 : {
327 : OSL_ENSURE( false, "_SfxMacroTabPage::AssignDeleteHdl_Impl: this branch is *not* dead? (out of interest: tell fs, please!)" );
328 : pThis->aTbl.Insert(
329 0 : nEvent, SvxMacro( sScriptURI, OUString( SVX_MACRO_LANGUAGE_STARBASIC ) ) );
330 : }
331 : }
332 :
333 0 : pImpl->pEventLB->SetUpdateMode( false );
334 0 : pE->ReplaceItem( new SvLBoxString( pE, 0, sScriptURI ), LB_MACROS_ITEMPOS );
335 0 : rListBox.GetModel()->InvalidateEntry( pE );
336 0 : rListBox.Select( pE );
337 0 : rListBox.MakeVisible( pE );
338 0 : rListBox.SetUpdateMode( true );
339 :
340 0 : pThis->EnableButtons();
341 0 : return 0;
342 : }
343 :
344 0 : IMPL_STATIC_LINK( _SfxMacroTabPage, TimeOut_Impl, Timer*, EMPTYARG )
345 : {
346 : // FillMacroList() can take a long time -> show wait cursor and disable input
347 0 : SfxTabDialog* pTabDlg = pThis->GetTabDialog();
348 : // perhaps the tabpage is part of a SingleTabDialog then pTabDlg == NULL
349 0 : if ( pTabDlg )
350 : {
351 0 : pTabDlg->EnterWait();
352 0 : pTabDlg->EnableInput( false );
353 : }
354 0 : pThis->FillMacroList();
355 0 : if ( pTabDlg )
356 : {
357 0 : pTabDlg->EnableInput( true );
358 0 : pTabDlg->LeaveWait();
359 : }
360 0 : return 0;
361 : }
362 :
363 0 : void _SfxMacroTabPage::InitAndSetHandler()
364 : {
365 0 : SvHeaderTabListBox& rListBox = mpImpl->pEventLB->GetListBox();
366 0 : HeaderBar& rHeaderBar = mpImpl->pEventLB->GetHeaderBar();
367 0 : Link aLnk(STATIC_LINK(this, _SfxMacroTabPage, AssignDeleteHdl_Impl ));
368 0 : mpImpl->pMacroLB->SetDoubleClickHdl( aLnk );
369 0 : mpImpl->pDeletePB->SetClickHdl( aLnk );
370 0 : mpImpl->pAssignPB->SetClickHdl( aLnk );
371 0 : rListBox.SetDoubleClickHdl( aLnk );
372 :
373 0 : rListBox.SetSelectHdl( STATIC_LINK( this, _SfxMacroTabPage, SelectEvent_Impl ));
374 0 : mpImpl->pGroupLB->SetSelectHdl( STATIC_LINK( this, _SfxMacroTabPage, SelectGroup_Impl ));
375 0 : mpImpl->pMacroLB->SetSelectHdl( STATIC_LINK( this, _SfxMacroTabPage, SelectMacro_Impl ));
376 :
377 0 : rListBox.SetSelectionMode( SINGLE_SELECTION );
378 0 : rListBox.SetTabs( &nTabs[0], MAP_APPFONT );
379 0 : Size aSize( nTabs[ 2 ], 0 );
380 0 : rHeaderBar.InsertItem( ITEMID_EVENT, mpImpl->sStrEvent, LogicToPixel( aSize, MapMode( MAP_APPFONT ) ).Width() );
381 0 : aSize.Width() = 1764; // don't know what, so 42^2 is best to use...
382 0 : rHeaderBar.InsertItem( ITMEID_ASSMACRO, mpImpl->sAssignedMacro, LogicToPixel( aSize, MapMode( MAP_APPFONT ) ).Width() );
383 0 : rListBox.SetSpaceBetweenEntries( 0 );
384 :
385 0 : mpImpl->pEventLB->Show();
386 0 : mpImpl->pEventLB->ConnectElements();
387 :
388 0 : mpImpl->pEventLB->Enable( true );
389 0 : mpImpl->pGroupLB->Enable( true );
390 0 : mpImpl->pMacroLB->Enable( true );
391 :
392 0 : mpImpl->pGroupLB->SetFunctionListBox( mpImpl->pMacroLB );
393 :
394 0 : }
395 :
396 0 : void _SfxMacroTabPage::FillMacroList()
397 : {
398 : mpImpl->pGroupLB->Init(
399 : ::com::sun::star::uno::Reference<
400 : ::com::sun::star::uno::XComponentContext >(),
401 : GetFrame(),
402 0 : OUString(), false);
403 0 : }
404 :
405 0 : void _SfxMacroTabPage::FillEvents()
406 : {
407 0 : SvHeaderTabListBox& rListBox = mpImpl->pEventLB->GetListBox();
408 :
409 0 : sal_uLong nEntryCnt = rListBox.GetEntryCount();
410 :
411 : // get events from the table and fill the EventListBox respectively
412 0 : for( sal_uLong n = 0 ; n < nEntryCnt ; ++n )
413 : {
414 0 : SvTreeListEntry* pE = rListBox.GetEntry( n );
415 0 : if( pE )
416 : {
417 0 : SvLBoxString* pLItem = static_cast<SvLBoxString*>( pE->GetItem( LB_MACROS_ITEMPOS ) );
418 : DBG_ASSERT( pLItem && SV_ITEM_ID_LBOXSTRING == pLItem->GetType(), "_SfxMacroTabPage::FillEvents(): no LBoxString" );
419 :
420 0 : OUString sOld( pLItem->GetText() );
421 0 : OUString sNew;
422 0 : sal_uInt16 nEventId = ( sal_uInt16 ) reinterpret_cast<sal_uLong>( pE->GetUserData() );
423 0 : if( aTbl.IsKeyValid( nEventId ) )
424 0 : sNew = ConvertToUIName_Impl( aTbl.Get( nEventId ) );
425 :
426 0 : if( sOld != sNew )
427 : {
428 0 : pE->ReplaceItem( new SvLBoxString( pE, 0, sNew ), LB_MACROS_ITEMPOS );
429 0 : rListBox.GetModel()->InvalidateEntry( pE );
430 0 : }
431 : }
432 : }
433 0 : }
434 :
435 0 : SfxMacroTabPage::SfxMacroTabPage(vcl::Window* pParent, const Reference< XFrame >& rxDocumentFrame, const SfxItemSet& rSet )
436 0 : : _SfxMacroTabPage( pParent, rSet )
437 : {
438 0 : mpImpl->sStrEvent = get<FixedText>("eventft")->GetText();
439 0 : mpImpl->sAssignedMacro = get<FixedText>("assignft")->GetText();
440 0 : get(mpImpl->pEventLB , "assignments");
441 0 : get(mpImpl->pAssignPB, "assign");
442 0 : get(mpImpl->pDeletePB, "delete");
443 0 : get(mpImpl->pGroupFrame, "groupframe");
444 0 : get(mpImpl->pGroupLB, "libraries");
445 0 : get(mpImpl->pMacroFrame, "macroframe");
446 0 : mpImpl->maStaticMacroLBLabel = mpImpl->pMacroFrame->get_label();
447 0 : get(mpImpl->pMacroLB, "macros");
448 :
449 0 : SetFrame( rxDocumentFrame );
450 :
451 0 : InitAndSetHandler();
452 :
453 0 : ScriptChanged();
454 0 : }
455 :
456 : namespace
457 : {
458 0 : SfxMacroTabPage* CreateSfxMacroTabPage( vcl::Window* pParent, const SfxItemSet& rAttrSet )
459 : {
460 0 : return new SfxMacroTabPage( pParent, NULL, rAttrSet );
461 : }
462 : }
463 :
464 0 : SfxTabPage* SfxMacroTabPage::Create( vcl::Window* pParent, const SfxItemSet* rAttrSet )
465 : {
466 0 : return CreateSfxMacroTabPage(pParent, *rAttrSet);
467 : }
468 :
469 0 : SfxMacroAssignDlg::SfxMacroAssignDlg(vcl::Window* pParent,
470 : const Reference< XFrame >& rxDocumentFrame, const SfxItemSet& rSet)
471 : : SfxSingleTabDialog(pParent, rSet, "EventAssignDialog",
472 0 : "cui/ui/eventassigndialog.ui")
473 : {
474 0 : SfxMacroTabPage* pPage = CreateSfxMacroTabPage(get_content_area(), rSet);
475 0 : pPage->SetFrame( rxDocumentFrame );
476 0 : SetTabPage( pPage );
477 0 : pPage->LaunchFillGroup();
478 0 : }
479 :
480 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|