1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_cui.hxx"
31 #define ITEMID_MACRO 0
32 #include <svl/macitem.hxx>
35 #include "macroass.hxx"
37 #include <basic/basmgr.hxx>
38 #include <dialmgr.hxx>
39 #include <svx/dialogs.hrc>
40 #define _SVSTDARR_STRINGSDTOR
41 #include <svl/svstdarr.hxx>
43 #include <svtools/svmedit.hxx>
44 #include "cfgutil.hxx"
45 #include <sfx2/app.hxx>
46 #include <sfx2/evntconf.hxx>
47 #include <sfx2/objsh.hxx>
48 #include "macroass.hrc"
50 #include <vcl/fixed.hxx>
51 #include "headertablistbox.hxx"
53 using ::com::sun::star::uno::Reference
;
54 using ::com::sun::star::frame::XFrame
;
56 class _SfxMacroTabPage_Impl
59 _SfxMacroTabPage_Impl( void );
60 ~_SfxMacroTabPage_Impl();
62 String maStaticMacroLBLabel
;
63 PushButton
* pAssignPB
;
64 PushButton
* pDeletePB
;
66 String
* pAssignedMacro
;
67 _HeaderTabListBox
* pEventLB
;
68 SfxConfigGroupListBox_Impl
* pGroupLB
;
69 FixedText
* pFT_MacroLBLabel
;
70 SfxConfigFunctionListBox_Impl
* pMacroLB
;
76 Timer maFillGroupTimer
;
80 _SfxMacroTabPage_Impl::_SfxMacroTabPage_Impl( void ) :
84 pAssignedMacro( NULL
),
87 pFT_MacroLBLabel( NULL
),
91 bReadOnly( sal_False
),
92 bGotEvents( sal_False
)
96 _SfxMacroTabPage_Impl::~_SfxMacroTabPage_Impl()
101 delete pAssignedMacro
;
105 delete pFT_MacroLBLabel
;
111 static sal_uInt16 __FAR_DATA aPageRg
[] = {
112 SID_ATTR_MACROITEM
, SID_ATTR_MACROITEM
,
116 // Achtung im Code wird dieses Array direkt (0, 1, ...) indiziert
117 static long nTabs
[] =
123 #define TAB_WIDTH_MIN 10
125 // IDs for items in HeaderBar of EventLB
126 #define ITEMID_EVENT 1
127 #define ITMEID_ASSMACRO 2
130 #define LB_EVENTS_ITEMPOS 1
131 #define LB_MACROS_ITEMPOS 2
133 String
ConvertToUIName_Impl( SvxMacro
*pMacro
)
135 String
aName( pMacro
->GetMacName() );
137 if ( ! pMacro
->GetLanguage().EqualsAscii("JavaScript") )
139 sal_uInt16 nCount
= aName
.GetTokenCount('.');
140 aEntry
= aName
.GetToken( nCount
-1, '.' );
144 aEntry
+= aName
.GetToken( 0, '.' );
146 aEntry
+= aName
.GetToken( nCount
-2, '.' );
155 void _SfxMacroTabPage::EnableButtons()
157 // Solange die Eventbox leer ist, nichts tun
158 const SvLBoxEntry
* pE
= mpImpl
->pEventLB
->GetListBox().FirstSelected();
161 // Gebundenes Macro holen
162 const SvxMacro
* pM
= aTbl
.Get( (sal_uInt16
)(sal_uLong
) pE
->GetUserData() );
163 mpImpl
->pDeletePB
->Enable( 0 != pM
&& !mpImpl
->bReadOnly
);
166 sEventMacro
= ((SvLBoxString
*)pE
->GetItem( LB_MACROS_ITEMPOS
))->GetText();
168 String sScriptURI
= mpImpl
->pMacroLB
->GetSelectedScriptURI();
169 mpImpl
->pAssignPB
->Enable( !mpImpl
->bReadOnly
&& !sScriptURI
.EqualsIgnoreCaseAscii( sEventMacro
) );
172 mpImpl
->pAssignPB
->Enable( sal_False
);
175 _SfxMacroTabPage::_SfxMacroTabPage( Window
* pParent
, const ResId
& rResId
, const SfxItemSet
& rAttrSet
)
176 : SfxTabPage( pParent
, rResId
, rAttrSet
)
179 mpImpl
= new _SfxMacroTabPage_Impl
;
182 _SfxMacroTabPage::~_SfxMacroTabPage()
187 void _SfxMacroTabPage::AddEvent( const String
& rEventName
, sal_uInt16 nEventId
)
189 String
sTmp( rEventName
);
192 // falls die Tabelle schon gueltig ist
193 SvxMacro
* pM
= aTbl
.Get( nEventId
);
196 String
sNew( ConvertToUIName_Impl( pM
) );
200 SvLBoxEntry
* pE
= mpImpl
->pEventLB
->GetListBox().InsertEntry( sTmp
);
201 pE
->SetUserData( reinterpret_cast< void* >( sal::static_int_cast
< sal_IntPtr
>( nEventId
)) );
204 void _SfxMacroTabPage::ScriptChanged()
206 // neue Bereiche und deren Funktionen besorgen
208 mpImpl
->pGroupLB
->Show();
209 mpImpl
->pMacroLB
->Show();
210 mpImpl
->pMacroFT
->SetText( *mpImpl
->pMacroStr
);
216 sal_Bool
_SfxMacroTabPage::FillItemSet( SfxItemSet
& rSet
)
218 SvxMacroItem
aItem( GetWhich( aPageRg
[0] ) );
219 ((SvxMacroTableDtor
&)aItem
.GetMacroTable()) = aTbl
;
221 const SfxPoolItem
* pItem
;
222 if( SFX_ITEM_SET
!= GetItemSet().GetItemState( aItem
.Which(), sal_True
, &pItem
)
223 || aItem
!= *(SvxMacroItem
*)pItem
)
231 void _SfxMacroTabPage::PageCreated (SfxAllItemSet aSet
)
233 const SfxPoolItem
* pEventsItem
;
234 if( !mpImpl
->bGotEvents
&& SFX_ITEM_SET
== aSet
.GetItemState( SID_EVENTCONFIG
, sal_True
, &pEventsItem
) )
236 mpImpl
->bGotEvents
= sal_True
;
237 const SfxEventNamesList
& rList
= ((SfxEventNamesItem
*)pEventsItem
)->GetEvents();
238 for ( sal_uInt16 nNo
= 0; nNo
< rList
.Count(); ++nNo
)
240 const SfxEventName
*pOwn
= rList
.GetObject(nNo
);
241 AddEvent( pOwn
->maUIName
, pOwn
->mnId
);
246 void _SfxMacroTabPage::Reset( const SfxItemSet
& rSet
)
248 const SfxPoolItem
* pItem
;
249 if( SFX_ITEM_SET
== rSet
.GetItemState( GetWhich( aPageRg
[0] ), sal_True
, &pItem
))
250 aTbl
= ((SvxMacroItem
*)pItem
)->GetMacroTable();
252 const SfxPoolItem
* pEventsItem
;
253 if( !mpImpl
->bGotEvents
&& SFX_ITEM_SET
== rSet
.GetItemState( SID_EVENTCONFIG
, sal_True
, &pEventsItem
) )
255 mpImpl
->bGotEvents
= sal_True
;
256 const SfxEventNamesList
& rList
= ((SfxEventNamesItem
*)pEventsItem
)->GetEvents();
257 for ( sal_uInt16 nNo
= 0; nNo
< rList
.Count(); ++nNo
)
259 const SfxEventName
*pOwn
= rList
.GetObject(nNo
);
260 AddEvent( pOwn
->maUIName
, pOwn
->mnId
);
266 SvHeaderTabListBox
& rListBox
= mpImpl
->pEventLB
->GetListBox();
267 SvLBoxEntry
* pE
= rListBox
.GetEntry( 0 );
269 rListBox
.SetCurEntry( pE
);
272 sal_Bool
_SfxMacroTabPage::IsReadOnly() const
274 return mpImpl
->bReadOnly
;
277 IMPL_STATIC_LINK( _SfxMacroTabPage
, SelectEvent_Impl
, SvTabListBox
*, EMPTYARG
)
279 _SfxMacroTabPage_Impl
* pImpl
= pThis
->mpImpl
;
280 SvHeaderTabListBox
& rListBox
= pImpl
->pEventLB
->GetListBox();
281 SvLBoxEntry
* pE
= rListBox
.FirstSelected();
283 if( !pE
|| LISTBOX_ENTRY_NOTFOUND
==
284 ( nPos
= rListBox
.GetModel()->GetAbsPos( pE
) ) )
286 DBG_ASSERT( pE
, "wo kommt der leere Eintrag her?" );
290 pThis
->ScriptChanged();
291 pThis
->EnableButtons();
295 IMPL_STATIC_LINK( _SfxMacroTabPage
, SelectGroup_Impl
, ListBox
*, EMPTYARG
)
297 _SfxMacroTabPage_Impl
* pImpl
= pThis
->mpImpl
;
298 String
sSel( pImpl
->pGroupLB
->GetGroup() );
299 pImpl
->pGroupLB
->GroupSelected();
300 const String sScriptURI
= pImpl
->pMacroLB
->GetSelectedScriptURI();
302 if( sScriptURI
.Len() > 0 )
303 aLabelText
= pImpl
->maStaticMacroLBLabel
;
304 pImpl
->pFT_MacroLBLabel
->SetText( aLabelText
);
306 pThis
->EnableButtons();
310 IMPL_STATIC_LINK( _SfxMacroTabPage
, SelectMacro_Impl
, ListBox
*, EMPTYARG
)
312 _SfxMacroTabPage_Impl
* pImpl
= pThis
->mpImpl
;
313 pImpl
->pMacroLB
->FunctionSelected();
314 pThis
->EnableButtons();
318 IMPL_STATIC_LINK( _SfxMacroTabPage
, AssignDeleteHdl_Impl
, PushButton
*, pBtn
)
320 _SfxMacroTabPage_Impl
* pImpl
= pThis
->mpImpl
;
321 SvHeaderTabListBox
& rListBox
= pImpl
->pEventLB
->GetListBox();
322 SvLBoxEntry
* pE
= rListBox
.FirstSelected();
324 if( !pE
|| LISTBOX_ENTRY_NOTFOUND
==
325 ( nPos
= rListBox
.GetModel()->GetAbsPos( pE
) ) )
327 DBG_ASSERT( pE
, "wo kommt der leere Eintrag her?" );
331 const sal_Bool bAssEnabled
= pBtn
!= pImpl
->pDeletePB
&& pImpl
->pAssignPB
->IsEnabled();
333 // aus der Tabelle entfernen
334 sal_uInt16 nEvent
= (sal_uInt16
)(sal_uLong
)pE
->GetUserData();
335 SvxMacro
*pRemoveMacro
= pThis
->aTbl
.Remove( nEvent
);
341 sScriptURI
= pImpl
->pMacroLB
->GetSelectedScriptURI();
342 if( sScriptURI
.CompareToAscii( "vnd.sun.star.script:", 20 ) == COMPARE_EQUAL
)
345 nEvent
, new SvxMacro( sScriptURI
, String::CreateFromAscii( SVX_MACRO_LANGUAGE_SF
) ) );
349 OSL_ENSURE( false, "_SfxMacroTabPage::AssignDeleteHdl_Impl: this branch is *not* dead? (out of interest: tell fs, please!)" );
351 nEvent
, new SvxMacro( sScriptURI
, String::CreateFromAscii( SVX_MACRO_LANGUAGE_STARBASIC
) ) );
355 pImpl
->pEventLB
->SetUpdateMode( sal_False
);
356 pE
->ReplaceItem( new SvLBoxString( pE
, 0, sScriptURI
), LB_MACROS_ITEMPOS
);
357 rListBox
.GetModel()->InvalidateEntry( pE
);
358 rListBox
.Select( pE
);
359 rListBox
.MakeVisible( pE
);
360 rListBox
.SetUpdateMode( sal_True
);
362 pThis
->EnableButtons();
366 IMPL_STATIC_LINK( _SfxMacroTabPage
, TimeOut_Impl
, Timer
*, EMPTYARG
)
368 // FillMacroList() can take a long time -> show wait cursor and disable input
369 SfxTabDialog
* pTabDlg
= pThis
->GetTabDialog();
370 // perhaps the tabpage is part of a SingleTabDialog then pTabDlg == NULL
373 pTabDlg
->EnterWait();
374 pTabDlg
->EnableInput( sal_False
);
376 pThis
->FillMacroList();
379 pTabDlg
->EnableInput( sal_True
);
380 pTabDlg
->LeaveWait();
385 void _SfxMacroTabPage::InitAndSetHandler()
387 // Handler installieren
388 SvHeaderTabListBox
& rListBox
= mpImpl
->pEventLB
->GetListBox();
389 HeaderBar
& rHeaderBar
= mpImpl
->pEventLB
->GetHeaderBar();
390 Link
aLnk(STATIC_LINK(this, _SfxMacroTabPage
, AssignDeleteHdl_Impl
));
391 mpImpl
->pMacroLB
->SetDoubleClickHdl( aLnk
);
392 mpImpl
->pDeletePB
->SetClickHdl( aLnk
);
393 mpImpl
->pAssignPB
->SetClickHdl( aLnk
);
394 rListBox
.SetDoubleClickHdl( aLnk
);
396 rListBox
.SetSelectHdl( STATIC_LINK( this, _SfxMacroTabPage
, SelectEvent_Impl
));
397 mpImpl
->pGroupLB
->SetSelectHdl( STATIC_LINK( this, _SfxMacroTabPage
, SelectGroup_Impl
));
398 mpImpl
->pMacroLB
->SetSelectHdl( STATIC_LINK( this, _SfxMacroTabPage
, SelectMacro_Impl
));
400 rListBox
.SetSelectionMode( SINGLE_SELECTION
);
401 rListBox
.SetTabs( &nTabs
[0], MAP_APPFONT
);
402 Size
aSize( nTabs
[ 2 ], 0 );
403 rHeaderBar
.InsertItem( ITEMID_EVENT
, *mpImpl
->pStrEvent
, LogicToPixel( aSize
, MapMode( MAP_APPFONT
) ).Width() );
404 aSize
.Width() = 1764; // don't know what, so 42^2 is best to use...
405 rHeaderBar
.InsertItem( ITMEID_ASSMACRO
, *mpImpl
->pAssignedMacro
, LogicToPixel( aSize
, MapMode( MAP_APPFONT
) ).Width() );
406 rListBox
.SetSpaceBetweenEntries( 0 );
408 mpImpl
->pEventLB
->Show();
409 mpImpl
->pEventLB
->ConnectElements();
411 mpImpl
->pEventLB
->Enable( sal_True
);
412 mpImpl
->pGroupLB
->Enable( sal_True
);
413 mpImpl
->pMacroLB
->Enable( sal_True
);
415 mpImpl
->pGroupLB
->SetFunctionListBox( mpImpl
->pMacroLB
);
417 mpImpl
->maFillGroupTimer
.SetTimeoutHdl( STATIC_LINK( this, _SfxMacroTabPage
, TimeOut_Impl
) );
418 mpImpl
->maFillGroupTimer
.SetTimeout( 0 );
419 mpImpl
->maFillGroupTimer
.Start();
422 void _SfxMacroTabPage::FillMacroList()
424 mpImpl
->pGroupLB
->Init(
425 ::com::sun::star::uno::Reference
<
426 ::com::sun::star::lang::XMultiServiceFactory
>(),
431 void _SfxMacroTabPage::FillEvents()
433 SvHeaderTabListBox
& rListBox
= mpImpl
->pEventLB
->GetListBox();
435 sal_uLong nEntryCnt
= rListBox
.GetEntryCount();
437 // Events aus der Tabelle holen und die EventListBox entsprechen fuellen
438 for( sal_uLong n
= 0 ; n
< nEntryCnt
; ++n
)
440 SvLBoxEntry
* pE
= rListBox
.GetEntry( n
);
443 SvLBoxString
* pLItem
= ( SvLBoxString
* ) pE
->GetItem( LB_MACROS_ITEMPOS
);
444 DBG_ASSERT( pLItem
&& SV_ITEM_ID_LBOXSTRING
== pLItem
->IsA(), "_SfxMacroTabPage::FillEvents(): no LBoxString" );
446 String
sOld( pLItem
->GetText() );
448 sal_uInt16 nEventId
= ( sal_uInt16
) ( sal_uLong
) pE
->GetUserData();
449 if( aTbl
.IsKeyValid( nEventId
) )
450 sNew
= ConvertToUIName_Impl( aTbl
.Get( nEventId
) );
454 pE
->ReplaceItem( new SvLBoxString( pE
, 0, sNew
), LB_MACROS_ITEMPOS
);
455 rListBox
.GetModel()->InvalidateEntry( pE
);
461 SfxMacroTabPage::SfxMacroTabPage( Window
* pParent
, const ResId
& rResId
, const Reference
< XFrame
>& rxDocumentFrame
, const SfxItemSet
& rSet
)
462 : _SfxMacroTabPage( pParent
, rResId
, rSet
)
464 mpImpl
->pStrEvent
= new String( CUI_RES( STR_EVENT
) );
465 mpImpl
->pAssignedMacro
= new String( CUI_RES( STR_ASSMACRO
) );
466 mpImpl
->pEventLB
= new _HeaderTabListBox( this, CUI_RES( LB_EVENT
) );
467 mpImpl
->pAssignPB
= new PushButton( this, CUI_RES( PB_ASSIGN
) );
468 mpImpl
->pDeletePB
= new PushButton( this, CUI_RES( PB_DELETE
) );
469 mpImpl
->pMacroFT
= new FixedText( this, CUI_RES( FT_MACRO
) );
470 mpImpl
->pGroupLB
= new SfxConfigGroupListBox_Impl( this, CUI_RES( LB_GROUP
) );
471 mpImpl
->pFT_MacroLBLabel
= new FixedText( this, CUI_RES( FT_LABEL4LB_MACROS
) );
472 mpImpl
->maStaticMacroLBLabel
= mpImpl
->pFT_MacroLBLabel
->GetText();
473 mpImpl
->pMacroLB
= new SfxConfigFunctionListBox_Impl( this, CUI_RES( LB_MACROS
) );
474 mpImpl
->pMacroStr
= new String( CUI_RES( STR_MACROS
) );
478 SetFrame( rxDocumentFrame
);
485 SfxTabPage
* SfxMacroTabPage::Create( Window
* pParent
, const SfxItemSet
& rAttrSet
)
487 return new SfxMacroTabPage( pParent
, CUI_RES( RID_SVXPAGE_EVENTASSIGN
), NULL
, rAttrSet
);
490 SfxMacroAssignDlg::SfxMacroAssignDlg( Window
* pParent
, const Reference
< XFrame
>& rxDocumentFrame
, const SfxItemSet
& rSet
)
491 : SfxSingleTabDialog( pParent
, rSet
, 0 )
493 SfxTabPage
* pPage
= SfxMacroTabPage::Create( this, rSet
);
494 pPage
->SetFrame( rxDocumentFrame
);
498 SfxMacroAssignDlg::~SfxMacroAssignDlg()