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 : sal_Bool bReadOnly;
58 : Timer maFillGroupTimer;
59 : sal_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)(sal_uLong) pE->GetUserData() );
126 0 : mpImpl->pDeletePB->Enable( 0 != pM && !mpImpl->bReadOnly );
127 :
128 0 : OUString sEventMacro;
129 0 : sEventMacro = ((SvLBoxString*)pE->GetItem( LB_MACROS_ITEMPOS ))->GetText();
130 :
131 0 : OUString sScriptURI = mpImpl->pMacroLB->GetSelectedScriptURI();
132 0 : mpImpl->pAssignPB->Enable( !mpImpl->bReadOnly && !sScriptURI.equalsIgnoreAsciiCase( sEventMacro ) );
133 : }
134 : else
135 0 : mpImpl->pAssignPB->Enable( false );
136 0 : }
137 :
138 0 : _SfxMacroTabPage::_SfxMacroTabPage(Window* pParent, const SfxItemSet& rAttrSet)
139 0 : : SfxTabPage(pParent, "EventAssignPage", "cui/ui/eventassignpage.ui", rAttrSet)
140 : {
141 0 : mpImpl = new _SfxMacroTabPage_Impl;
142 0 : }
143 :
144 0 : _SfxMacroTabPage::~_SfxMacroTabPage()
145 : {
146 0 : DELETEZ( mpImpl );
147 0 : }
148 :
149 0 : void _SfxMacroTabPage::AddEvent( const OUString & rEventName, sal_uInt16 nEventId )
150 : {
151 0 : OUString sTmp( rEventName );
152 0 : sTmp += "\t";
153 :
154 : // if the table is valid already
155 0 : SvxMacro* pM = aTbl.Get( nEventId );
156 0 : if( pM )
157 : {
158 0 : OUString sNew( ConvertToUIName_Impl( pM ) );
159 0 : sTmp += sNew;
160 : }
161 :
162 0 : SvTreeListEntry* pE = mpImpl->pEventLB->GetListBox().InsertEntry( sTmp );
163 0 : pE->SetUserData( reinterpret_cast< void* >( sal::static_int_cast< sal_IntPtr >( nEventId )) );
164 0 : }
165 :
166 0 : void _SfxMacroTabPage::ScriptChanged()
167 : {
168 : // get new areas and their functions
169 0 : mpImpl->pGroupFrame->Show();
170 0 : mpImpl->pMacroFrame->Show();
171 :
172 0 : EnableButtons();
173 0 : }
174 :
175 0 : bool _SfxMacroTabPage::FillItemSet( SfxItemSet& rSet )
176 : {
177 0 : SvxMacroItem aItem( GetWhich( aPageRg[0] ) );
178 0 : ((SvxMacroTableDtor&)aItem.GetMacroTable()) = aTbl;
179 :
180 : const SfxPoolItem* pItem;
181 0 : if( SFX_ITEM_SET != GetItemSet().GetItemState( aItem.Which(), true, &pItem )
182 0 : || aItem != *(SvxMacroItem*)pItem )
183 : {
184 0 : rSet.Put( aItem );
185 0 : return true;
186 : }
187 0 : return false;
188 : }
189 :
190 0 : void _SfxMacroTabPage::LaunchFillGroup()
191 : {
192 0 : if (!mpImpl->maFillGroupTimer.GetTimeoutHdl().IsSet())
193 : {
194 0 : mpImpl->maFillGroupTimer.SetTimeoutHdl( STATIC_LINK( this, _SfxMacroTabPage, TimeOut_Impl ) );
195 0 : mpImpl->maFillGroupTimer.SetTimeout( 0 );
196 0 : mpImpl->maFillGroupTimer.Start();
197 : }
198 0 : }
199 :
200 0 : void _SfxMacroTabPage::ActivatePage( const SfxItemSet& )
201 : {
202 : // fdo#57553 lazily init script providers, because it is annoying if done
203 : // on dialog open (SfxTabDialog::Start_Impl activates all tab pages once!)
204 0 : if (!mpImpl->m_bDummyActivated)
205 : {
206 0 : mpImpl->m_bDummyActivated = true;
207 0 : return;
208 : }
209 0 : LaunchFillGroup();
210 : }
211 :
212 0 : void _SfxMacroTabPage::PageCreated (SfxAllItemSet aSet)
213 : {
214 : const SfxPoolItem* pEventsItem;
215 0 : if( !mpImpl->bGotEvents && SFX_ITEM_SET == aSet.GetItemState( SID_EVENTCONFIG, true, &pEventsItem ) )
216 : {
217 0 : mpImpl->bGotEvents = sal_True;
218 0 : const SfxEventNamesList& rList = ((SfxEventNamesItem*)pEventsItem)->GetEvents();
219 0 : for ( size_t nNo = 0, nCnt = rList.size(); nNo < nCnt; ++nNo )
220 : {
221 0 : const SfxEventName *pOwn = rList.at(nNo);
222 0 : AddEvent( pOwn->maUIName, pOwn->mnId );
223 : }
224 : }
225 0 : }
226 :
227 0 : void _SfxMacroTabPage::Reset( const SfxItemSet& rSet )
228 : {
229 : const SfxPoolItem* pItem;
230 0 : if( SFX_ITEM_SET == rSet.GetItemState( GetWhich( aPageRg[0] ), true, &pItem ))
231 0 : aTbl = ((SvxMacroItem*)pItem)->GetMacroTable();
232 :
233 : const SfxPoolItem* pEventsItem;
234 0 : if( !mpImpl->bGotEvents && SFX_ITEM_SET == rSet.GetItemState( SID_EVENTCONFIG, true, &pEventsItem ) )
235 : {
236 0 : mpImpl->bGotEvents = sal_True;
237 0 : const SfxEventNamesList& rList = ((SfxEventNamesItem*)pEventsItem)->GetEvents();
238 0 : for ( size_t nNo = 0, nCnt = rList.size(); nNo < nCnt; ++nNo )
239 : {
240 0 : const SfxEventName *pOwn = rList.at(nNo);
241 0 : AddEvent( pOwn->maUIName, pOwn->mnId );
242 : }
243 : }
244 :
245 0 : FillEvents();
246 :
247 0 : SvHeaderTabListBox& rListBox = mpImpl->pEventLB->GetListBox();
248 0 : SvTreeListEntry* pE = rListBox.GetEntry( 0 );
249 0 : if( pE )
250 0 : rListBox.SetCurEntry( pE );
251 0 : }
252 :
253 0 : bool _SfxMacroTabPage::IsReadOnly() const
254 : {
255 0 : return mpImpl->bReadOnly;
256 : }
257 :
258 0 : IMPL_STATIC_LINK( _SfxMacroTabPage, SelectEvent_Impl, SvTabListBox*, EMPTYARG )
259 : {
260 0 : _SfxMacroTabPage_Impl* pImpl = pThis->mpImpl;
261 0 : SvHeaderTabListBox& rListBox = pImpl->pEventLB->GetListBox();
262 0 : SvTreeListEntry* pE = rListBox.FirstSelected();
263 : sal_uLong nPos;
264 0 : if( !pE || LISTBOX_ENTRY_NOTFOUND ==
265 0 : ( nPos = rListBox.GetModel()->GetAbsPos( pE ) ) )
266 : {
267 : DBG_ASSERT( pE, "wo kommt der leere Eintrag her?" );
268 0 : return 0;
269 : }
270 :
271 0 : pThis->ScriptChanged();
272 0 : pThis->EnableButtons();
273 0 : return 0;
274 : }
275 :
276 0 : IMPL_STATIC_LINK( _SfxMacroTabPage, SelectGroup_Impl, ListBox*, EMPTYARG )
277 : {
278 0 : _SfxMacroTabPage_Impl* pImpl = pThis->mpImpl;
279 0 : pImpl->pGroupLB->GroupSelected();
280 0 : const OUString sScriptURI = pImpl->pMacroLB->GetSelectedScriptURI();
281 0 : OUString aLabelText;
282 0 : if( !sScriptURI.isEmpty() )
283 0 : aLabelText = pImpl->maStaticMacroLBLabel;
284 0 : pImpl->pMacroFrame->set_label( aLabelText );
285 :
286 0 : pThis->EnableButtons();
287 0 : return 0;
288 : }
289 :
290 0 : IMPL_STATIC_LINK( _SfxMacroTabPage, SelectMacro_Impl, ListBox*, EMPTYARG )
291 : {
292 0 : _SfxMacroTabPage_Impl* pImpl = pThis->mpImpl;
293 0 : pImpl->pMacroLB->FunctionSelected();
294 0 : pThis->EnableButtons();
295 0 : return 0;
296 : }
297 :
298 0 : IMPL_STATIC_LINK( _SfxMacroTabPage, AssignDeleteHdl_Impl, PushButton*, pBtn )
299 : {
300 0 : _SfxMacroTabPage_Impl* pImpl = pThis->mpImpl;
301 0 : SvHeaderTabListBox& rListBox = pImpl->pEventLB->GetListBox();
302 0 : SvTreeListEntry* pE = rListBox.FirstSelected();
303 : sal_uLong nPos;
304 0 : if( !pE || LISTBOX_ENTRY_NOTFOUND ==
305 0 : ( nPos = rListBox.GetModel()->GetAbsPos( pE ) ) )
306 : {
307 : DBG_ASSERT( pE, "wo kommt der leere Eintrag her?" );
308 0 : return 0;
309 : }
310 :
311 0 : const sal_Bool bAssEnabled = pBtn != pImpl->pDeletePB && pImpl->pAssignPB->IsEnabled();
312 :
313 : // remove from the table
314 0 : sal_uInt16 nEvent = (sal_uInt16)(sal_uLong)pE->GetUserData();
315 0 : pThis->aTbl.Erase( nEvent );
316 :
317 0 : OUString sScriptURI;
318 0 : if( bAssEnabled )
319 : {
320 0 : sScriptURI = pImpl->pMacroLB->GetSelectedScriptURI();
321 0 : if( sScriptURI.startsWith( "vnd.sun.star.script:" ) )
322 : {
323 : pThis->aTbl.Insert(
324 0 : nEvent, SvxMacro( sScriptURI, OUString( SVX_MACRO_LANGUAGE_SF ) ) );
325 : }
326 : else
327 : {
328 : OSL_ENSURE( false, "_SfxMacroTabPage::AssignDeleteHdl_Impl: this branch is *not* dead? (out of interest: tell fs, please!)" );
329 : pThis->aTbl.Insert(
330 0 : nEvent, SvxMacro( sScriptURI, OUString( SVX_MACRO_LANGUAGE_STARBASIC ) ) );
331 : }
332 : }
333 :
334 0 : pImpl->pEventLB->SetUpdateMode( false );
335 0 : pE->ReplaceItem( new SvLBoxString( pE, 0, sScriptURI ), LB_MACROS_ITEMPOS );
336 0 : rListBox.GetModel()->InvalidateEntry( pE );
337 0 : rListBox.Select( pE );
338 0 : rListBox.MakeVisible( pE );
339 0 : rListBox.SetUpdateMode( true );
340 :
341 0 : pThis->EnableButtons();
342 0 : return 0;
343 : }
344 :
345 0 : IMPL_STATIC_LINK( _SfxMacroTabPage, TimeOut_Impl, Timer*, EMPTYARG )
346 : {
347 : // FillMacroList() can take a long time -> show wait cursor and disable input
348 0 : SfxTabDialog* pTabDlg = pThis->GetTabDialog();
349 : // perhaps the tabpage is part of a SingleTabDialog then pTabDlg == NULL
350 0 : if ( pTabDlg )
351 : {
352 0 : pTabDlg->EnterWait();
353 0 : pTabDlg->EnableInput( false );
354 : }
355 0 : pThis->FillMacroList();
356 0 : if ( pTabDlg )
357 : {
358 0 : pTabDlg->EnableInput( true );
359 0 : pTabDlg->LeaveWait();
360 : }
361 0 : return 0;
362 : }
363 :
364 0 : void _SfxMacroTabPage::InitAndSetHandler()
365 : {
366 0 : SvHeaderTabListBox& rListBox = mpImpl->pEventLB->GetListBox();
367 0 : HeaderBar& rHeaderBar = mpImpl->pEventLB->GetHeaderBar();
368 0 : Link aLnk(STATIC_LINK(this, _SfxMacroTabPage, AssignDeleteHdl_Impl ));
369 0 : mpImpl->pMacroLB->SetDoubleClickHdl( aLnk );
370 0 : mpImpl->pDeletePB->SetClickHdl( aLnk );
371 0 : mpImpl->pAssignPB->SetClickHdl( aLnk );
372 0 : rListBox.SetDoubleClickHdl( aLnk );
373 :
374 0 : rListBox.SetSelectHdl( STATIC_LINK( this, _SfxMacroTabPage, SelectEvent_Impl ));
375 0 : mpImpl->pGroupLB->SetSelectHdl( STATIC_LINK( this, _SfxMacroTabPage, SelectGroup_Impl ));
376 0 : mpImpl->pMacroLB->SetSelectHdl( STATIC_LINK( this, _SfxMacroTabPage, SelectMacro_Impl ));
377 :
378 0 : rListBox.SetSelectionMode( SINGLE_SELECTION );
379 0 : rListBox.SetTabs( &nTabs[0], MAP_APPFONT );
380 0 : Size aSize( nTabs[ 2 ], 0 );
381 0 : rHeaderBar.InsertItem( ITEMID_EVENT, mpImpl->sStrEvent, LogicToPixel( aSize, MapMode( MAP_APPFONT ) ).Width() );
382 0 : aSize.Width() = 1764; // don't know what, so 42^2 is best to use...
383 0 : rHeaderBar.InsertItem( ITMEID_ASSMACRO, mpImpl->sAssignedMacro, LogicToPixel( aSize, MapMode( MAP_APPFONT ) ).Width() );
384 0 : rListBox.SetSpaceBetweenEntries( 0 );
385 :
386 0 : mpImpl->pEventLB->Show();
387 0 : mpImpl->pEventLB->ConnectElements();
388 :
389 0 : mpImpl->pEventLB->Enable( true );
390 0 : mpImpl->pGroupLB->Enable( true );
391 0 : mpImpl->pMacroLB->Enable( true );
392 :
393 0 : mpImpl->pGroupLB->SetFunctionListBox( mpImpl->pMacroLB );
394 :
395 0 : }
396 :
397 0 : void _SfxMacroTabPage::FillMacroList()
398 : {
399 : mpImpl->pGroupLB->Init(
400 : ::com::sun::star::uno::Reference<
401 : ::com::sun::star::uno::XComponentContext >(),
402 : GetFrame(),
403 0 : OUString(), false);
404 0 : }
405 :
406 0 : void _SfxMacroTabPage::FillEvents()
407 : {
408 0 : SvHeaderTabListBox& rListBox = mpImpl->pEventLB->GetListBox();
409 :
410 0 : sal_uLong nEntryCnt = rListBox.GetEntryCount();
411 :
412 : // get events from the table and fill the EventListBox respectively
413 0 : for( sal_uLong n = 0 ; n < nEntryCnt ; ++n )
414 : {
415 0 : SvTreeListEntry* pE = rListBox.GetEntry( n );
416 0 : if( pE )
417 : {
418 0 : SvLBoxString* pLItem = ( SvLBoxString* ) pE->GetItem( LB_MACROS_ITEMPOS );
419 : DBG_ASSERT( pLItem && SV_ITEM_ID_LBOXSTRING == pLItem->GetType(), "_SfxMacroTabPage::FillEvents(): no LBoxString" );
420 :
421 0 : OUString sOld( pLItem->GetText() );
422 0 : OUString sNew;
423 0 : sal_uInt16 nEventId = ( sal_uInt16 ) ( sal_uLong ) pE->GetUserData();
424 0 : if( aTbl.IsKeyValid( nEventId ) )
425 0 : sNew = ConvertToUIName_Impl( aTbl.Get( nEventId ) );
426 :
427 0 : if( sOld != sNew )
428 : {
429 0 : pE->ReplaceItem( new SvLBoxString( pE, 0, sNew ), LB_MACROS_ITEMPOS );
430 0 : rListBox.GetModel()->InvalidateEntry( pE );
431 0 : }
432 : }
433 : }
434 0 : }
435 :
436 0 : SfxMacroTabPage::SfxMacroTabPage(Window* pParent, const Reference< XFrame >& rxDocumentFrame, const SfxItemSet& rSet )
437 0 : : _SfxMacroTabPage( pParent, rSet )
438 : {
439 0 : mpImpl->sStrEvent = get<FixedText>("eventft")->GetText();
440 0 : mpImpl->sAssignedMacro = get<FixedText>("assignft")->GetText();
441 0 : get(mpImpl->pEventLB , "assignments");
442 0 : get(mpImpl->pAssignPB, "assign");
443 0 : get(mpImpl->pDeletePB, "delete");
444 0 : get(mpImpl->pGroupFrame, "groupframe");
445 0 : get(mpImpl->pGroupLB, "libraries");
446 0 : get(mpImpl->pMacroFrame, "macroframe");
447 0 : mpImpl->maStaticMacroLBLabel = mpImpl->pMacroFrame->get_label();
448 0 : get(mpImpl->pMacroLB, "macros");
449 :
450 0 : SetFrame( rxDocumentFrame );
451 :
452 0 : InitAndSetHandler();
453 :
454 0 : ScriptChanged();
455 0 : }
456 :
457 : namespace
458 : {
459 0 : SfxMacroTabPage* CreateSfxMacroTabPage( Window* pParent, const SfxItemSet& rAttrSet )
460 : {
461 0 : return new SfxMacroTabPage( pParent, NULL, rAttrSet );
462 : }
463 : }
464 :
465 0 : SfxTabPage* SfxMacroTabPage::Create( Window* pParent, const SfxItemSet& rAttrSet )
466 : {
467 0 : return CreateSfxMacroTabPage(pParent, rAttrSet);
468 : }
469 :
470 0 : SfxMacroAssignDlg::SfxMacroAssignDlg(Window* pParent,
471 : const Reference< XFrame >& rxDocumentFrame, const SfxItemSet& rSet)
472 : : SfxSingleTabDialog(pParent, rSet, "EventAssignDialog",
473 0 : "cui/ui/eventassigndialog.ui")
474 : {
475 0 : SfxMacroTabPage* pPage = CreateSfxMacroTabPage(get_content_area(), rSet);
476 0 : pPage->SetFrame( rxDocumentFrame );
477 0 : SetTabPage( pPage );
478 0 : pPage->LaunchFillGroup();
479 0 : }
480 :
481 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|