bump product version to 4.1.6.2
[LibreOffice.git] / cui / source / tabpages / macroass.cxx
blob084a171982905206d0a0e8da069c9589b79d5d5e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <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 "macroass.hrc"
33 #include "cuires.hrc"
34 #include <vcl/fixed.hxx>
35 #include "headertablistbox.hxx"
36 #include "svtools/svlbitm.hxx"
37 #include "svtools/treelistentry.hxx"
39 using ::com::sun::star::uno::Reference;
40 using ::com::sun::star::frame::XFrame;
42 class _SfxMacroTabPage_Impl
44 public:
45 _SfxMacroTabPage_Impl( void );
46 ~_SfxMacroTabPage_Impl();
48 String maStaticMacroLBLabel;
49 PushButton* pAssignPB;
50 PushButton* pDeletePB;
51 String* pStrEvent;
52 String* pAssignedMacro;
53 _HeaderTabListBox* pEventLB;
54 SfxConfigGroupListBox_Impl* pGroupLB;
55 FixedText* pFT_MacroLBLabel;
56 SfxConfigFunctionListBox_Impl* pMacroLB;
58 FixedText* pMacroFT;
59 String* pMacroStr;
61 sal_Bool bReadOnly;
62 Timer maFillGroupTimer;
63 sal_Bool bGotEvents;
64 bool m_bDummyActivated; ///< has this tab page already been activated
67 _SfxMacroTabPage_Impl::_SfxMacroTabPage_Impl( void ) :
68 pAssignPB( NULL ),
69 pDeletePB( NULL ),
70 pStrEvent( NULL ),
71 pAssignedMacro( NULL ),
72 pEventLB( NULL ),
73 pGroupLB( NULL ),
74 pFT_MacroLBLabel( NULL ),
75 pMacroLB( NULL ),
76 pMacroFT( NULL ),
77 pMacroStr( NULL ),
78 bReadOnly( sal_False ),
79 bGotEvents( sal_False )
80 , m_bDummyActivated(false)
84 _SfxMacroTabPage_Impl::~_SfxMacroTabPage_Impl()
86 delete pAssignPB;
87 delete pDeletePB;
88 delete pStrEvent;
89 delete pAssignedMacro;
90 delete pEventLB;
91 delete pGroupLB;
92 delete pMacroLB;
93 delete pFT_MacroLBLabel;
94 delete pMacroFT;
95 delete pMacroStr;
99 static sal_uInt16 aPageRg[] = {
100 SID_ATTR_MACROITEM, SID_ATTR_MACROITEM,
104 // attention, this array is indexed directly (0, 1, ...) in the code
105 static long nTabs[] =
107 2, // Number of Tabs
108 0, 90
111 // IDs for items in HeaderBar of EventLB
112 #define ITEMID_EVENT 1
113 #define ITMEID_ASSMACRO 2
116 #define LB_MACROS_ITEMPOS 2
118 String ConvertToUIName_Impl( SvxMacro *pMacro )
120 String aName( pMacro->GetMacName() );
121 String aEntry;
122 if ( pMacro->GetLanguage() != "JavaScript" )
124 sal_uInt16 nCount = comphelper::string::getTokenCount(aName, '.');
125 aEntry = aName.GetToken( nCount-1, '.' );
126 if ( nCount > 2 )
128 aEntry += '(';
129 aEntry += aName.GetToken( 0, '.' );
130 aEntry += '.';
131 aEntry += aName.GetToken( nCount-2, '.' );
132 aEntry += ')';
134 return aEntry;
136 else
137 return aName;
140 void _SfxMacroTabPage::EnableButtons()
142 // don't do anything as long as the eventbox is empty
143 const SvTreeListEntry* pE = mpImpl->pEventLB->GetListBox().FirstSelected();
144 if ( pE )
146 // get bound macro
147 const SvxMacro* pM = aTbl.Get( (sal_uInt16)(sal_uLong) pE->GetUserData() );
148 mpImpl->pDeletePB->Enable( 0 != pM && !mpImpl->bReadOnly );
150 String sEventMacro;
151 sEventMacro = ((SvLBoxString*)pE->GetItem( LB_MACROS_ITEMPOS ))->GetText();
153 String sScriptURI = mpImpl->pMacroLB->GetSelectedScriptURI();
154 mpImpl->pAssignPB->Enable( !mpImpl->bReadOnly && !sScriptURI.EqualsIgnoreCaseAscii( sEventMacro ) );
156 else
157 mpImpl->pAssignPB->Enable( sal_False );
160 _SfxMacroTabPage::_SfxMacroTabPage( Window* pParent, const ResId& rResId, const SfxItemSet& rAttrSet )
161 : SfxTabPage( pParent, rResId, rAttrSet )
164 mpImpl = new _SfxMacroTabPage_Impl;
167 _SfxMacroTabPage::~_SfxMacroTabPage()
169 DELETEZ( mpImpl );
172 void _SfxMacroTabPage::AddEvent( const String & rEventName, sal_uInt16 nEventId )
174 String sTmp( rEventName );
175 sTmp += '\t';
177 // if the table is valid already
178 SvxMacro* pM = aTbl.Get( nEventId );
179 if( pM )
181 String sNew( ConvertToUIName_Impl( pM ) );
182 sTmp += sNew;
185 SvTreeListEntry* pE = mpImpl->pEventLB->GetListBox().InsertEntry( sTmp );
186 pE->SetUserData( reinterpret_cast< void* >( sal::static_int_cast< sal_IntPtr >( nEventId )) );
189 void _SfxMacroTabPage::ScriptChanged()
191 // get new areas and their functions
193 mpImpl->pGroupLB->Show();
194 mpImpl->pMacroLB->Show();
195 mpImpl->pMacroFT->SetText( *mpImpl->pMacroStr );
198 EnableButtons();
201 sal_Bool _SfxMacroTabPage::FillItemSet( SfxItemSet& rSet )
203 SvxMacroItem aItem( GetWhich( aPageRg[0] ) );
204 ((SvxMacroTableDtor&)aItem.GetMacroTable()) = aTbl;
206 const SfxPoolItem* pItem;
207 if( SFX_ITEM_SET != GetItemSet().GetItemState( aItem.Which(), sal_True, &pItem )
208 || aItem != *(SvxMacroItem*)pItem )
210 rSet.Put( aItem );
211 return sal_True;
213 return sal_False;
216 void _SfxMacroTabPage::LaunchFillGroup()
218 if (!mpImpl->maFillGroupTimer.GetTimeoutHdl().IsSet())
220 mpImpl->maFillGroupTimer.SetTimeoutHdl( STATIC_LINK( this, _SfxMacroTabPage, TimeOut_Impl ) );
221 mpImpl->maFillGroupTimer.SetTimeout( 0 );
222 mpImpl->maFillGroupTimer.Start();
226 void _SfxMacroTabPage::ActivatePage( const SfxItemSet& )
228 // fdo#57553 lazily init script providers, because it is annoying if done
229 // on dialog open (SfxTabDialog::Start_Impl activates all tab pages once!)
230 if (!mpImpl->m_bDummyActivated)
232 mpImpl->m_bDummyActivated = true;
233 return;
235 LaunchFillGroup();
238 void _SfxMacroTabPage::PageCreated (SfxAllItemSet aSet)
240 const SfxPoolItem* pEventsItem;
241 if( !mpImpl->bGotEvents && SFX_ITEM_SET == aSet.GetItemState( SID_EVENTCONFIG, sal_True, &pEventsItem ) )
243 mpImpl->bGotEvents = sal_True;
244 const SfxEventNamesList& rList = ((SfxEventNamesItem*)pEventsItem)->GetEvents();
245 for ( size_t nNo = 0, nCnt = rList.size(); nNo < nCnt; ++nNo )
247 const SfxEventName *pOwn = rList.at(nNo);
248 AddEvent( pOwn->maUIName, pOwn->mnId );
253 void _SfxMacroTabPage::Reset( const SfxItemSet& rSet )
255 const SfxPoolItem* pItem;
256 if( SFX_ITEM_SET == rSet.GetItemState( GetWhich( aPageRg[0] ), sal_True, &pItem ))
257 aTbl = ((SvxMacroItem*)pItem)->GetMacroTable();
259 const SfxPoolItem* pEventsItem;
260 if( !mpImpl->bGotEvents && SFX_ITEM_SET == rSet.GetItemState( SID_EVENTCONFIG, sal_True, &pEventsItem ) )
262 mpImpl->bGotEvents = sal_True;
263 const SfxEventNamesList& rList = ((SfxEventNamesItem*)pEventsItem)->GetEvents();
264 for ( size_t nNo = 0, nCnt = rList.size(); nNo < nCnt; ++nNo )
266 const SfxEventName *pOwn = rList.at(nNo);
267 AddEvent( pOwn->maUIName, pOwn->mnId );
271 FillEvents();
273 SvHeaderTabListBox& rListBox = mpImpl->pEventLB->GetListBox();
274 SvTreeListEntry* pE = rListBox.GetEntry( 0 );
275 if( pE )
276 rListBox.SetCurEntry( pE );
279 sal_Bool _SfxMacroTabPage::IsReadOnly() const
281 return mpImpl->bReadOnly;
284 IMPL_STATIC_LINK( _SfxMacroTabPage, SelectEvent_Impl, SvTabListBox*, EMPTYARG )
286 _SfxMacroTabPage_Impl* pImpl = pThis->mpImpl;
287 SvHeaderTabListBox& rListBox = pImpl->pEventLB->GetListBox();
288 SvTreeListEntry* pE = rListBox.FirstSelected();
289 sal_uLong nPos;
290 if( !pE || LISTBOX_ENTRY_NOTFOUND ==
291 ( nPos = rListBox.GetModel()->GetAbsPos( pE ) ) )
293 DBG_ASSERT( pE, "wo kommt der leere Eintrag her?" );
294 return 0;
297 pThis->ScriptChanged();
298 pThis->EnableButtons();
299 return 0;
302 IMPL_STATIC_LINK( _SfxMacroTabPage, SelectGroup_Impl, ListBox*, EMPTYARG )
304 _SfxMacroTabPage_Impl* pImpl = pThis->mpImpl;
305 pImpl->pGroupLB->GroupSelected();
306 const String sScriptURI = pImpl->pMacroLB->GetSelectedScriptURI();
307 String aLabelText;
308 if( sScriptURI.Len() > 0 )
309 aLabelText = pImpl->maStaticMacroLBLabel;
310 pImpl->pFT_MacroLBLabel->SetText( aLabelText );
312 pThis->EnableButtons();
313 return 0;
316 IMPL_STATIC_LINK( _SfxMacroTabPage, SelectMacro_Impl, ListBox*, EMPTYARG )
318 _SfxMacroTabPage_Impl* pImpl = pThis->mpImpl;
319 pImpl->pMacroLB->FunctionSelected();
320 pThis->EnableButtons();
321 return 0;
324 IMPL_STATIC_LINK( _SfxMacroTabPage, AssignDeleteHdl_Impl, PushButton*, pBtn )
326 _SfxMacroTabPage_Impl* pImpl = pThis->mpImpl;
327 SvHeaderTabListBox& rListBox = pImpl->pEventLB->GetListBox();
328 SvTreeListEntry* pE = rListBox.FirstSelected();
329 sal_uLong nPos;
330 if( !pE || LISTBOX_ENTRY_NOTFOUND ==
331 ( nPos = rListBox.GetModel()->GetAbsPos( pE ) ) )
333 DBG_ASSERT( pE, "wo kommt der leere Eintrag her?" );
334 return 0;
337 const sal_Bool bAssEnabled = pBtn != pImpl->pDeletePB && pImpl->pAssignPB->IsEnabled();
339 // remove from the table
340 sal_uInt16 nEvent = (sal_uInt16)(sal_uLong)pE->GetUserData();
341 pThis->aTbl.Erase( nEvent );
343 String sScriptURI;
344 if( bAssEnabled )
346 sScriptURI = pImpl->pMacroLB->GetSelectedScriptURI();
347 if( sScriptURI.CompareToAscii( "vnd.sun.star.script:", 20 ) == COMPARE_EQUAL )
349 pThis->aTbl.Insert(
350 nEvent, SvxMacro( sScriptURI, OUString( SVX_MACRO_LANGUAGE_SF ) ) );
352 else
354 OSL_ENSURE( false, "_SfxMacroTabPage::AssignDeleteHdl_Impl: this branch is *not* dead? (out of interest: tell fs, please!)" );
355 pThis->aTbl.Insert(
356 nEvent, SvxMacro( sScriptURI, OUString( SVX_MACRO_LANGUAGE_STARBASIC ) ) );
360 pImpl->pEventLB->SetUpdateMode( sal_False );
361 pE->ReplaceItem( new SvLBoxString( pE, 0, sScriptURI ), LB_MACROS_ITEMPOS );
362 rListBox.GetModel()->InvalidateEntry( pE );
363 rListBox.Select( pE );
364 rListBox.MakeVisible( pE );
365 rListBox.SetUpdateMode( sal_True );
367 pThis->EnableButtons();
368 return 0;
371 IMPL_STATIC_LINK( _SfxMacroTabPage, TimeOut_Impl, Timer*, EMPTYARG )
373 // FillMacroList() can take a long time -> show wait cursor and disable input
374 SfxTabDialog* pTabDlg = pThis->GetTabDialog();
375 // perhaps the tabpage is part of a SingleTabDialog then pTabDlg == NULL
376 if ( pTabDlg )
378 pTabDlg->EnterWait();
379 pTabDlg->EnableInput( sal_False );
381 pThis->FillMacroList();
382 if ( pTabDlg )
384 pTabDlg->EnableInput( sal_True );
385 pTabDlg->LeaveWait();
387 return 0;
390 void _SfxMacroTabPage::InitAndSetHandler()
392 SvHeaderTabListBox& rListBox = mpImpl->pEventLB->GetListBox();
393 HeaderBar& rHeaderBar = mpImpl->pEventLB->GetHeaderBar();
394 Link aLnk(STATIC_LINK(this, _SfxMacroTabPage, AssignDeleteHdl_Impl ));
395 mpImpl->pMacroLB->SetDoubleClickHdl( aLnk );
396 mpImpl->pDeletePB->SetClickHdl( aLnk );
397 mpImpl->pAssignPB->SetClickHdl( aLnk );
398 rListBox.SetDoubleClickHdl( aLnk );
400 rListBox.SetSelectHdl( STATIC_LINK( this, _SfxMacroTabPage, SelectEvent_Impl ));
401 mpImpl->pGroupLB->SetSelectHdl( STATIC_LINK( this, _SfxMacroTabPage, SelectGroup_Impl ));
402 mpImpl->pMacroLB->SetSelectHdl( STATIC_LINK( this, _SfxMacroTabPage, SelectMacro_Impl ));
404 rListBox.SetSelectionMode( SINGLE_SELECTION );
405 rListBox.SetTabs( &nTabs[0], MAP_APPFONT );
406 Size aSize( nTabs[ 2 ], 0 );
407 rHeaderBar.InsertItem( ITEMID_EVENT, *mpImpl->pStrEvent, LogicToPixel( aSize, MapMode( MAP_APPFONT ) ).Width() );
408 aSize.Width() = 1764; // don't know what, so 42^2 is best to use...
409 rHeaderBar.InsertItem( ITMEID_ASSMACRO, *mpImpl->pAssignedMacro, LogicToPixel( aSize, MapMode( MAP_APPFONT ) ).Width() );
410 rListBox.SetSpaceBetweenEntries( 0 );
412 mpImpl->pEventLB->Show();
413 mpImpl->pEventLB->ConnectElements();
415 mpImpl->pEventLB->Enable( sal_True );
416 mpImpl->pGroupLB->Enable( sal_True );
417 mpImpl->pMacroLB->Enable( sal_True );
419 mpImpl->pGroupLB->SetFunctionListBox( mpImpl->pMacroLB );
423 void _SfxMacroTabPage::FillMacroList()
425 mpImpl->pGroupLB->Init(
426 ::com::sun::star::uno::Reference<
427 ::com::sun::star::uno::XComponentContext >(),
428 GetFrame(),
429 OUString() );
432 void _SfxMacroTabPage::FillEvents()
434 SvHeaderTabListBox& rListBox = mpImpl->pEventLB->GetListBox();
436 sal_uLong nEntryCnt = rListBox.GetEntryCount();
438 // get events from the table and fill the EventListBox respectively
439 for( sal_uLong n = 0 ; n < nEntryCnt ; ++n )
441 SvTreeListEntry* pE = rListBox.GetEntry( n );
442 if( pE )
444 SvLBoxString* pLItem = ( SvLBoxString* ) pE->GetItem( LB_MACROS_ITEMPOS );
445 DBG_ASSERT( pLItem && SV_ITEM_ID_LBOXSTRING == pLItem->GetType(), "_SfxMacroTabPage::FillEvents(): no LBoxString" );
447 String sOld( pLItem->GetText() );
448 String sNew;
449 sal_uInt16 nEventId = ( sal_uInt16 ) ( sal_uLong ) pE->GetUserData();
450 if( aTbl.IsKeyValid( nEventId ) )
451 sNew = ConvertToUIName_Impl( aTbl.Get( nEventId ) );
453 if( sOld != sNew )
455 pE->ReplaceItem( new SvLBoxString( pE, 0, sNew ), LB_MACROS_ITEMPOS );
456 rListBox.GetModel()->InvalidateEntry( pE );
462 SfxMacroTabPage::SfxMacroTabPage( Window* pParent, const ResId& rResId, const Reference< XFrame >& rxDocumentFrame, const SfxItemSet& rSet )
463 : _SfxMacroTabPage( pParent, rResId, rSet )
465 mpImpl->pStrEvent = new String( CUI_RES( STR_EVENT ) );
466 mpImpl->pAssignedMacro = new String( CUI_RES( STR_ASSMACRO ) );
467 mpImpl->pEventLB = new _HeaderTabListBox( this, CUI_RES( LB_EVENT ) );
468 mpImpl->pAssignPB = new PushButton( this, CUI_RES( PB_ASSIGN ) );
469 mpImpl->pDeletePB = new PushButton( this, CUI_RES( PB_DELETE ) );
470 mpImpl->pMacroFT = new FixedText( this, CUI_RES( FT_MACRO ) );
471 mpImpl->pGroupLB = new SfxConfigGroupListBox_Impl( this, CUI_RES( LB_GROUP ) );
472 mpImpl->pFT_MacroLBLabel = new FixedText( this, CUI_RES( FT_LABEL4LB_MACROS ) );
473 mpImpl->maStaticMacroLBLabel= mpImpl->pFT_MacroLBLabel->GetText();
474 mpImpl->pMacroLB = new SfxConfigFunctionListBox_Impl( this, CUI_RES( LB_MACROS ) );
475 mpImpl->pMacroStr = new String( CUI_RES( STR_MACROS ) );
477 FreeResource();
479 SetFrame( rxDocumentFrame );
481 InitAndSetHandler();
483 ScriptChanged();
486 namespace
488 SfxMacroTabPage* CreateSfxMacroTabPage( Window* pParent, const SfxItemSet& rAttrSet )
490 return new SfxMacroTabPage( pParent, CUI_RES( RID_SVXPAGE_EVENTASSIGN ), NULL, rAttrSet );
494 SfxTabPage* SfxMacroTabPage::Create( Window* pParent, const SfxItemSet& rAttrSet )
496 return CreateSfxMacroTabPage(pParent, rAttrSet);
499 SfxMacroAssignDlg::SfxMacroAssignDlg( Window* pParent, const Reference< XFrame >& rxDocumentFrame, const SfxItemSet& rSet )
500 : SfxNoLayoutSingleTabDialog( pParent, rSet, 0 )
502 SfxMacroTabPage* pPage = CreateSfxMacroTabPage(this, rSet);
503 pPage->SetFrame( rxDocumentFrame );
504 SetTabPage( pPage );
505 pPage->LaunchFillGroup();
508 SfxMacroAssignDlg::~SfxMacroAssignDlg()
513 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */