bump product version to 5.0.4.1
[LibreOffice.git] / cui / source / customize / macropg.cxx
blob6400cff29ae271be1fd51a0375fbf3ace56129a8
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 <basic/basmgr.hxx>
22 #include "macropg.hxx"
23 #include <vcl/layout.hxx>
24 #include <vcl/msgbox.hxx>
25 #include <svtools/svmedit.hxx>
26 #include <svtools/svlbitm.hxx>
27 #include "svtools/treelistentry.hxx"
28 #include <svl/eitem.hxx>
29 #include <tools/diagnose_ex.h>
30 #include <sfx2/app.hxx>
31 #include <sfx2/objsh.hxx>
32 #include <com/sun/star/container/NoSuchElementException.hpp>
33 #include <com/sun/star/lang/IllegalArgumentException.hpp>
34 #include <dialmgr.hxx>
35 #include "selector.hxx"
36 #include "cfg.hxx"
37 #include "helpid.hrc"
38 #include <cuires.hrc>
39 #include "headertablistbox.hxx"
40 #include "macropg_impl.hxx"
41 #include <svx/dialogs.hrc>
42 #include <vcl/builderfactory.hxx>
43 #include <comphelper/namedvaluecollection.hxx>
45 #include <algorithm>
46 #include <set>
47 #include <boost/scoped_ptr.hpp>
49 using namespace ::com::sun::star;
50 using namespace ::com::sun::star::uno;
52 static const char aVndSunStarUNO[] = "vnd.sun.star.UNO:";
53 static const char aVndSunStarScript[] = "vnd.sun.star.script:";
55 _SvxMacroTabPage_Impl::_SvxMacroTabPage_Impl( const SfxItemSet& rAttrSet )
56 : pAssignPB(NULL)
57 , pAssignComponentPB(NULL)
58 , pDeletePB(NULL)
59 , pEventLB(NULL)
60 , bReadOnly(false)
61 , bIDEDialogMode(false)
63 const SfxPoolItem* pItem;
64 if ( SfxItemState::SET == rAttrSet.GetItemState( SID_ATTR_MACROITEM, false, &pItem ) )
65 bIDEDialogMode = static_cast<const SfxBoolItem*>(pItem)->GetValue();
68 // attention, this array is indexed directly (0, 1, ...) in the code
69 static long nTabs[] =
71 2, // Number of Tabs
72 0, 90
75 #define TAB_WIDTH_MIN 10
77 // IDs for items in HeaderBar of EventLB
78 #define ITEMID_EVENT 1
79 #define ITMEID_ASSMACRO 2
82 #define LB_MACROS_ITEMPOS 2
85 IMPL_LINK( MacroEventListBox, HeaderEndDrag_Impl, HeaderBar*, pBar )
87 DBG_ASSERT( pBar == maHeaderBar.get(), "*MacroEventListBox::HeaderEndDrag_Impl: something is wrong here..." );
88 (void)pBar;
90 if( !maHeaderBar->GetCurItemId() )
91 return 0;
93 if( !maHeaderBar->IsItemMode() )
95 Size aSz;
96 sal_uInt16 _nTabs = maHeaderBar->GetItemCount();
97 long nWidth = maHeaderBar->GetItemSize( ITEMID_EVENT );
98 long nBarWidth = maHeaderBar->GetSizePixel().Width();
100 if( nWidth < TAB_WIDTH_MIN )
101 maHeaderBar->SetItemSize( ITEMID_EVENT, TAB_WIDTH_MIN );
102 else if( ( nBarWidth - nWidth ) < TAB_WIDTH_MIN )
103 maHeaderBar->SetItemSize( ITEMID_EVENT, nBarWidth - TAB_WIDTH_MIN );
106 long nTmpSz = 0;
107 for( sal_uInt16 i = 1 ; i < _nTabs ; ++i )
109 long _nWidth = maHeaderBar->GetItemSize( i );
110 aSz.Width() = _nWidth + nTmpSz;
111 nTmpSz += _nWidth;
112 maListBox->SetTab( i, PixelToLogic( aSz, MapMode( MAP_APPFONT ) ).Width(), MAP_APPFONT );
116 return 1;
119 bool MacroEventListBox::Notify( NotifyEvent& rNEvt )
121 bool nRet = Control::Notify( rNEvt );
123 if( rNEvt.GetType() == MouseNotifyEvent::GETFOCUS )
125 if ( rNEvt.GetWindow() != maListBox.get() )
126 if (maListBox)
127 maListBox->GrabFocus();
130 return nRet;
133 MacroEventListBox::MacroEventListBox( vcl::Window* pParent, WinBits nStyle )
134 : Control( pParent, nStyle )
135 , maHeaderBar( VclPtr<HeaderBar>::Create( this, WB_BUTTONSTYLE | WB_BOTTOMBORDER ) )
136 , maListBox( VclPtr<SvHeaderTabListBox>::Create( this, WB_HSCROLL | WB_CLIPCHILDREN | WB_TABSTOP ) )
138 maListBox->SetHelpId( HID_MACRO_HEADERTABLISTBOX );
140 // enable the cell focus to show visible focus
141 maListBox->EnableCellFocus();
144 MacroEventListBox::~MacroEventListBox()
146 disposeOnce();
149 void MacroEventListBox::dispose()
151 maHeaderBar.disposeAndClear();
152 maListBox.disposeAndClear();
153 Control::dispose();
156 VCL_BUILDER_DECL_FACTORY(MacroEventListBox)
158 WinBits nWinBits = WB_TABSTOP;
160 OString sBorder = VclBuilder::extractCustomProperty(rMap);
161 if (!sBorder.isEmpty())
162 nWinBits |= WB_BORDER;
164 rRet = VclPtr<MacroEventListBox>::Create(pParent, nWinBits);
167 Size MacroEventListBox::GetOptimalSize() const
169 return LogicToPixel(Size(192, 72), MapMode(MAP_APPFONT ));
172 void MacroEventListBox::Resize()
174 Control::Resize();
176 // calc pos and size of header bar
177 Point aPnt( 0, 0 );
178 Size aSize( maHeaderBar->CalcWindowSizePixel() );
179 Size aCtrlSize( GetOutputSizePixel() );
180 aSize.Width() = aCtrlSize.Width();
181 maHeaderBar->SetPosSizePixel( aPnt, aSize );
183 // calc pos and size of ListBox
184 aPnt.Y() += aSize.Height();
185 aSize.Height() = aCtrlSize.Height() - aSize.Height();
186 maListBox->SetPosSizePixel( aPnt, aSize );
189 void MacroEventListBox::ConnectElements()
191 Resize();
193 // set handler
194 maHeaderBar->SetEndDragHdl( LINK( this, MacroEventListBox, HeaderEndDrag_Impl ) );
196 maListBox->InitHeaderBar( maHeaderBar.get() );
199 void MacroEventListBox::Show( bool bVisible, sal_uInt16 nFlags )
201 maListBox->Show( bVisible, nFlags );
202 maHeaderBar->Show( bVisible, nFlags );
205 void MacroEventListBox::Enable( bool bEnable, bool bChild )
207 maListBox->Enable( bEnable, bChild );
208 maHeaderBar->Enable( bEnable, bChild );
211 // assign button ("Add Command") is enabled only if it is not read only
212 // delete button ("Remove Command") is enabled if a current binding exists
213 // and it is not read only
214 void _SvxMacroTabPage::EnableButtons()
216 const SvTreeListEntry* pE = mpImpl->pEventLB->GetListBox().FirstSelected();
217 if ( pE )
219 const SvLBoxString* pEventMacro = static_cast<const SvLBoxString*>(pE->GetItem( LB_MACROS_ITEMPOS ));
220 mpImpl->pDeletePB->Enable( 0 != pEventMacro && !mpImpl->bReadOnly );
222 mpImpl->pAssignPB->Enable( !mpImpl->bReadOnly );
223 if( mpImpl->pAssignComponentPB )
224 mpImpl->pAssignComponentPB->Enable( !mpImpl->bReadOnly );
228 _SvxMacroTabPage::_SvxMacroTabPage(vcl::Window* pParent, const OString& rID,
229 const OUString& rUIXMLDescription, const SfxItemSet& rAttrSet)
230 : SfxTabPage( pParent, rID, rUIXMLDescription, &rAttrSet ),
231 m_xAppEvents(0),
232 m_xDocEvents(0),
233 bReadOnly(false),
234 bDocModified(false),
235 bAppEvents(false),
236 bInitialized(false)
238 mpImpl = new _SvxMacroTabPage_Impl( rAttrSet );
241 _SvxMacroTabPage::~_SvxMacroTabPage()
243 disposeOnce();
246 void _SvxMacroTabPage::dispose()
248 DELETEZ( mpImpl );
249 SfxTabPage::dispose();
252 void _SvxMacroTabPage::InitResources()
254 // Note: the order here controls the order in which the events are displayed in the UI!
256 // the event name to UI string mappings for App Events
257 aDisplayNames.push_back( EventDisplayName( "OnStartApp", RID_SVXSTR_EVENT_STARTAPP ) );
258 aDisplayNames.push_back( EventDisplayName( "OnCloseApp", RID_SVXSTR_EVENT_CLOSEAPP ) );
259 aDisplayNames.push_back( EventDisplayName( "OnCreate", RID_SVXSTR_EVENT_CREATEDOC ) );
260 aDisplayNames.push_back( EventDisplayName( "OnNew", RID_SVXSTR_EVENT_NEWDOC ) );
261 aDisplayNames.push_back( EventDisplayName( "OnLoadFinished", RID_SVXSTR_EVENT_LOADDOCFINISHED ) );
262 aDisplayNames.push_back( EventDisplayName( "OnLoad", RID_SVXSTR_EVENT_OPENDOC ) );
263 aDisplayNames.push_back( EventDisplayName( "OnPrepareUnload", RID_SVXSTR_EVENT_PREPARECLOSEDOC ) );
264 aDisplayNames.push_back( EventDisplayName( "OnUnload", RID_SVXSTR_EVENT_CLOSEDOC ) ) ;
265 aDisplayNames.push_back( EventDisplayName( "OnViewCreated", RID_SVXSTR_EVENT_VIEWCREATED ) );
266 aDisplayNames.push_back( EventDisplayName( "OnPrepareViewClosing", RID_SVXSTR_EVENT_PREPARECLOSEVIEW ) );
267 aDisplayNames.push_back( EventDisplayName( "OnViewClosed", RID_SVXSTR_EVENT_CLOSEVIEW ) ) ;
268 aDisplayNames.push_back( EventDisplayName( "OnFocus", RID_SVXSTR_EVENT_ACTIVATEDOC ) );
269 aDisplayNames.push_back( EventDisplayName( "OnUnfocus", RID_SVXSTR_EVENT_DEACTIVATEDOC ) );
270 aDisplayNames.push_back( EventDisplayName( "OnSave", RID_SVXSTR_EVENT_SAVEDOC ) );
271 aDisplayNames.push_back( EventDisplayName( "OnSaveDone", RID_SVXSTR_EVENT_SAVEDOCDONE ) );
272 aDisplayNames.push_back( EventDisplayName( "OnSaveFailed", RID_SVXSTR_EVENT_SAVEDOCFAILED ) );
273 aDisplayNames.push_back( EventDisplayName( "OnSaveAs", RID_SVXSTR_EVENT_SAVEASDOC ) );
274 aDisplayNames.push_back( EventDisplayName( "OnSaveAsDone", RID_SVXSTR_EVENT_SAVEASDOCDONE ) );
275 aDisplayNames.push_back( EventDisplayName( "OnSaveAsFailed", RID_SVXSTR_EVENT_SAVEASDOCFAILED ) );
276 aDisplayNames.push_back( EventDisplayName( "OnCopyTo", RID_SVXSTR_EVENT_COPYTODOC ) );
277 aDisplayNames.push_back( EventDisplayName( "OnCopyToDone", RID_SVXSTR_EVENT_COPYTODOCDONE ) );
278 aDisplayNames.push_back( EventDisplayName( "OnCopyToFailed", RID_SVXSTR_EVENT_COPYTODOCFAILED ) );
279 aDisplayNames.push_back( EventDisplayName( "OnPrint", RID_SVXSTR_EVENT_PRINTDOC ) );
280 aDisplayNames.push_back( EventDisplayName( "OnModifyChanged", RID_SVXSTR_EVENT_MODIFYCHANGED ) );
281 aDisplayNames.push_back( EventDisplayName( "OnTitleChanged", RID_SVXSTR_EVENT_TITLECHANGED ) );
283 // application specific events
284 aDisplayNames.push_back( EventDisplayName( "OnMailMerge", RID_SVXSTR_EVENT_MAILMERGE ) );
285 aDisplayNames.push_back( EventDisplayName( "OnMailMergeFinished", RID_SVXSTR_EVENT_MAILMERGE_END ) );
286 aDisplayNames.push_back( EventDisplayName( "OnFieldMerge", RID_SVXSTR_EVENT_FIELDMERGE ) );
287 aDisplayNames.push_back( EventDisplayName( "OnFieldMergeFinished", RID_SVXSTR_EVENT_FIELDMERGE_FINISHED ) );
288 aDisplayNames.push_back( EventDisplayName( "OnPageCountChange", RID_SVXSTR_EVENT_PAGECOUNTCHANGE ) );
289 aDisplayNames.push_back( EventDisplayName( "OnSubComponentOpened", RID_SVXSTR_EVENT_SUBCOMPONENT_OPENED ) );
290 aDisplayNames.push_back( EventDisplayName( "OnSubComponentClosed", RID_SVXSTR_EVENT_SUBCOMPONENT_CLOSED ) );
291 aDisplayNames.push_back( EventDisplayName( "OnSelect", RID_SVXSTR_EVENT_SELECTIONCHANGED ) );
292 aDisplayNames.push_back( EventDisplayName( "OnDoubleClick", RID_SVXSTR_EVENT_DOUBLECLICK ) );
293 aDisplayNames.push_back( EventDisplayName( "OnRightClick", RID_SVXSTR_EVENT_RIGHTCLICK ) );
294 aDisplayNames.push_back( EventDisplayName( "OnCalculate", RID_SVXSTR_EVENT_CALCULATE ) );
295 aDisplayNames.push_back( EventDisplayName( "OnChange", RID_SVXSTR_EVENT_CONTENTCHANGED ) );
297 // the event name to UI string mappings for forms & dialogs
299 aDisplayNames.push_back( EventDisplayName( "approveAction", RID_SVXSTR_EVENT_APPROVEACTIONPERFORMED ) );
300 aDisplayNames.push_back( EventDisplayName( "actionPerformed", RID_SVXSTR_EVENT_ACTIONPERFORMED ) );
301 aDisplayNames.push_back( EventDisplayName( "changed", RID_SVXSTR_EVENT_CHANGED ) );
302 aDisplayNames.push_back( EventDisplayName( "textChanged", RID_SVXSTR_EVENT_TEXTCHANGED ) );
303 aDisplayNames.push_back( EventDisplayName( "itemStateChanged", RID_SVXSTR_EVENT_ITEMSTATECHANGED ) );
304 aDisplayNames.push_back( EventDisplayName( "focusGained", RID_SVXSTR_EVENT_FOCUSGAINED ) );
305 aDisplayNames.push_back( EventDisplayName( "focusLost", RID_SVXSTR_EVENT_FOCUSLOST ) );
306 aDisplayNames.push_back( EventDisplayName( "keyPressed", RID_SVXSTR_EVENT_KEYTYPED ) );
307 aDisplayNames.push_back( EventDisplayName( "keyReleased", RID_SVXSTR_EVENT_KEYUP ) );
308 aDisplayNames.push_back( EventDisplayName( "mouseEntered", RID_SVXSTR_EVENT_MOUSEENTERED ) );
309 aDisplayNames.push_back( EventDisplayName( "mouseDragged", RID_SVXSTR_EVENT_MOUSEDRAGGED ) );
310 aDisplayNames.push_back( EventDisplayName( "mouseMoved", RID_SVXSTR_EVENT_MOUSEMOVED ) );
311 aDisplayNames.push_back( EventDisplayName( "mousePressed", RID_SVXSTR_EVENT_MOUSEPRESSED ) );
312 aDisplayNames.push_back( EventDisplayName( "mouseReleased", RID_SVXSTR_EVENT_MOUSERELEASED ) );
313 aDisplayNames.push_back( EventDisplayName( "mouseExited", RID_SVXSTR_EVENT_MOUSEEXITED ) );
314 aDisplayNames.push_back( EventDisplayName( "approveReset", RID_SVXSTR_EVENT_APPROVERESETTED ) );
315 aDisplayNames.push_back( EventDisplayName( "resetted", RID_SVXSTR_EVENT_RESETTED ) );
316 aDisplayNames.push_back( EventDisplayName( "approveSubmit", RID_SVXSTR_EVENT_SUBMITTED ) );
317 aDisplayNames.push_back( EventDisplayName( "approveUpdate", RID_SVXSTR_EVENT_BEFOREUPDATE ) );
318 aDisplayNames.push_back( EventDisplayName( "updated", RID_SVXSTR_EVENT_AFTERUPDATE ) );
319 aDisplayNames.push_back( EventDisplayName( "loaded", RID_SVXSTR_EVENT_LOADED ) );
320 aDisplayNames.push_back( EventDisplayName( "reloading", RID_SVXSTR_EVENT_RELOADING ) );
321 aDisplayNames.push_back( EventDisplayName( "reloaded", RID_SVXSTR_EVENT_RELOADED ) );
322 aDisplayNames.push_back( EventDisplayName( "unloading", RID_SVXSTR_EVENT_UNLOADING ) );
323 aDisplayNames.push_back( EventDisplayName( "unloaded", RID_SVXSTR_EVENT_UNLOADED ) );
324 aDisplayNames.push_back( EventDisplayName( "confirmDelete", RID_SVXSTR_EVENT_CONFIRMDELETE ) );
325 aDisplayNames.push_back( EventDisplayName( "approveRowChange", RID_SVXSTR_EVENT_APPROVEROWCHANGE ) );
326 aDisplayNames.push_back( EventDisplayName( "rowChanged", RID_SVXSTR_EVENT_ROWCHANGE ) );
327 aDisplayNames.push_back( EventDisplayName( "approveCursorMove", RID_SVXSTR_EVENT_POSITIONING ) );
328 aDisplayNames.push_back( EventDisplayName( "cursorMoved", RID_SVXSTR_EVENT_POSITIONED ) );
329 aDisplayNames.push_back( EventDisplayName( "approveParameter", RID_SVXSTR_EVENT_APPROVEPARAMETER ) );
330 aDisplayNames.push_back( EventDisplayName( "errorOccured", RID_SVXSTR_EVENT_ERROROCCURRED ) );
331 aDisplayNames.push_back( EventDisplayName( "adjustmentValueChanged", RID_SVXSTR_EVENT_ADJUSTMENTVALUECHANGED ) );
334 // the following method is called when the user clicks OK
335 // We use the contents of the hashes to replace the settings
336 bool _SvxMacroTabPage::FillItemSet( SfxItemSet* /*rSet*/ )
340 OUString eventName;
341 if( m_xAppEvents.is() )
343 EventsHash::iterator h_itEnd = m_appEventsHash.end();
344 EventsHash::iterator h_it = m_appEventsHash.begin();
345 for ( ; h_it != h_itEnd; ++h_it )
347 eventName = h_it->first;
350 m_xAppEvents->replaceByName( eventName, GetPropsByName( eventName, m_appEventsHash ) );
352 catch (const Exception&)
354 DBG_UNHANDLED_EXCEPTION();
358 if( m_xDocEvents.is() && bDocModified )
360 EventsHash::iterator h_itEnd = m_docEventsHash.end();
361 EventsHash::iterator h_it = m_docEventsHash.begin();
362 for ( ; h_it != h_itEnd; ++h_it )
364 eventName = h_it->first;
367 m_xDocEvents->replaceByName( eventName, GetPropsByName( eventName, m_docEventsHash ) );
369 catch (const Exception&)
371 DBG_UNHANDLED_EXCEPTION();
374 // if we have a valid XModifiable (in the case of doc events)
375 // call setModified(true)
376 // in principle this should not be necessary (see issue ??)
377 if(m_xModifiable.is())
379 m_xModifiable->setModified( sal_True );
383 catch (const Exception&)
386 // what is the return value about??
387 return false;
390 // the following method clears the bindings in the hashes for both doc & app
391 void _SvxMacroTabPage::Reset( const SfxItemSet* )
393 // called once in creation - don't reset the data this time
394 if(!bInitialized)
396 bInitialized = true;
397 return;
402 OUString sEmpty;
403 if( m_xAppEvents.is() )
405 EventsHash::iterator h_itEnd = m_appEventsHash.end();
406 EventsHash::iterator h_it = m_appEventsHash.begin();
407 for ( ; h_it != h_itEnd; ++h_it )
409 h_it->second.second = sEmpty;
412 if( m_xDocEvents.is() && bDocModified )
414 EventsHash::iterator h_itEnd = m_docEventsHash.end();
415 EventsHash::iterator h_it = m_docEventsHash.begin();
416 for ( ; h_it != h_itEnd; ++h_it )
418 h_it->second.second = sEmpty;
420 // if we have a valid XModifiable (in the case of doc events)
421 // call setModified(true)
422 if(m_xModifiable.is())
424 m_xModifiable->setModified( sal_True );
428 catch (const Exception&)
431 DisplayAppEvents(bAppEvents);
434 void _SvxMacroTabPage::SetReadOnly( bool bSet )
436 mpImpl->bReadOnly = bSet;
439 bool _SvxMacroTabPage::IsReadOnly() const
441 return mpImpl->bReadOnly;
445 class IconLBoxString : public SvLBoxString
447 Image* m_pMacroImg;
448 Image* m_pComponentImg;
449 int m_nxImageOffset;
451 public:
452 IconLBoxString( SvTreeListEntry* pEntry, sal_uInt16 nFlags, const OUString& sText,
453 Image* pMacroImg, Image* pComponentImg );
454 virtual void Paint(const Point& rPos, SvTreeListBox& rOutDev, vcl::RenderContext& rRenderContext,
455 const SvViewDataEntry* pView, const SvTreeListEntry* pEntry) SAL_OVERRIDE;
459 IconLBoxString::IconLBoxString( SvTreeListEntry* pEntry, sal_uInt16 nFlags, const OUString& sText,
460 Image* pMacroImg, Image* pComponentImg )
461 : SvLBoxString( pEntry, nFlags, sText )
462 , m_pMacroImg( pMacroImg )
463 , m_pComponentImg( pComponentImg )
465 m_nxImageOffset = 20;
469 void IconLBoxString::Paint(const Point& aPos, SvTreeListBox& /*aDevice*/, vcl::RenderContext& rRenderContext,
470 const SvViewDataEntry* /*pView*/, const SvTreeListEntry* /*pEntry*/)
472 OUString aTxt(GetText());
473 if (!aTxt.isEmpty())
475 OUString aURL(aTxt);
476 sal_Int32 nIndex = aURL.indexOf(aVndSunStarUNO);
477 bool bUNO = nIndex == 0;
479 const Image* pImg = bUNO ? m_pComponentImg : m_pMacroImg;
480 rRenderContext.DrawImage(aPos, *pImg);
482 OUString aPureMethod;
483 if (bUNO)
485 aPureMethod = aURL.copy(strlen(aVndSunStarUNO));
487 else
489 aPureMethod = aURL.copy(strlen(aVndSunStarScript));
490 aPureMethod = aPureMethod.copy( 0, aPureMethod.indexOf( '?' ) );
493 Point aPnt(aPos);
494 aPnt.X() += m_nxImageOffset;
495 rRenderContext.DrawText(aPnt, aPureMethod);
500 // displays the app events if appEvents=true, otherwise displays the doc events
501 void _SvxMacroTabPage::DisplayAppEvents( bool appEvents)
503 bAppEvents = appEvents;
505 SvHeaderTabListBox& rListBox = mpImpl->pEventLB->GetListBox();
506 mpImpl->pEventLB->SetUpdateMode( false );
507 rListBox.Clear();
508 EventsHash* eventsHash;
509 Reference< container::XNameReplace> nameReplace;
510 if(bAppEvents)
512 eventsHash = &m_appEventsHash;
513 nameReplace = m_xAppEvents;
515 else
517 eventsHash = &m_docEventsHash;
518 nameReplace = m_xDocEvents;
520 // have to use the original XNameReplace since the hash iterators do
521 // not guarantee the order in which the elements are returned
522 if(!nameReplace.is())
523 return;
525 Sequence< OUString > eventNames = nameReplace->getElementNames();
526 ::std::set< OUString > aEventNamesCache;
527 ::std::copy(
528 eventNames.getConstArray(),
529 eventNames.getConstArray() + eventNames.getLength(),
530 ::std::insert_iterator< ::std::set< OUString > >( aEventNamesCache, aEventNamesCache.end() )
533 for ( EventDisplayNames::const_iterator displayableEvent = aDisplayNames.begin();
534 displayableEvent != aDisplayNames.end();
535 ++displayableEvent
538 OUString sEventName( OUString::createFromAscii( displayableEvent->pAsciiEventName ) );
539 if ( !nameReplace->hasByName( sEventName ) )
540 continue;
542 EventsHash::iterator h_it = eventsHash->find( sEventName );
543 if( h_it == eventsHash->end() )
545 OSL_FAIL( "_SvxMacroTabPage::DisplayAppEvents: something's suspicious here!" );
546 continue;
549 OUString eventURL = h_it->second.second;
550 OUString displayName( CUI_RES( displayableEvent->nEventResourceID ) );
552 displayName += "\t";
554 SvTreeListEntry* _pE = rListBox.InsertEntry( displayName );
555 OUString* pEventName = new OUString( sEventName );
556 _pE->SetUserData( (void*)pEventName );
557 OUString sNew( eventURL );
558 _pE->ReplaceItem( new IconLBoxString( _pE, 0, sNew,
559 &mpImpl->aMacroImg, &mpImpl->aComponentImg ), LB_MACROS_ITEMPOS );
560 rListBox.GetModel()->InvalidateEntry( _pE );
561 rListBox.Select( _pE );
562 rListBox.MakeVisible( _pE );
565 SvTreeListEntry* pE = rListBox.GetEntry(0);
566 if( pE )
568 rListBox.Select( pE );
569 rListBox.MakeVisible( pE );
572 rListBox.SetUpdateMode( true );
573 EnableButtons();
576 // select event handler on the listbox
577 IMPL_LINK( _SvxMacroTabPage, SelectEvent_Impl, SvTabListBox*, )
579 SvHeaderTabListBox& rListBox = mpImpl->pEventLB->GetListBox();
580 SvTreeListEntry* pE = rListBox.FirstSelected();
581 sal_uLong nPos;
583 if( !pE || LISTBOX_ENTRY_NOTFOUND ==
584 ( nPos = rListBox.GetModel()->GetAbsPos( pE ) ) )
586 DBG_ASSERT( pE, "wo kommt der leere Eintrag her?" );
587 return 0;
590 EnableButtons();
591 return 0;
594 IMPL_LINK( _SvxMacroTabPage, AssignDeleteHdl_Impl, PushButton*, pBtn )
596 return GenericHandler_Impl( this, pBtn );
599 IMPL_LINK( _SvxMacroTabPage, DoubleClickHdl_Impl, SvTabListBox *, )
601 return GenericHandler_Impl( this, NULL );
604 // handler for double click on the listbox, and for the assign/delete buttons
605 long _SvxMacroTabPage::GenericHandler_Impl( _SvxMacroTabPage* pThis, PushButton* pBtn )
607 _SvxMacroTabPage_Impl* pImpl = pThis->mpImpl;
608 SvHeaderTabListBox& rListBox = pImpl->pEventLB->GetListBox();
609 SvTreeListEntry* pE = rListBox.FirstSelected();
610 sal_uLong nPos;
611 if( !pE || LISTBOX_ENTRY_NOTFOUND ==
612 ( nPos = rListBox.GetModel()->GetAbsPos( pE ) ) )
614 DBG_ASSERT( pE, "wo kommt der leere Eintrag her?" );
615 return 0;
618 const bool bAssEnabled = pBtn != pImpl->pDeletePB && pImpl->pAssignPB->IsEnabled();
620 OUString* pEventName = static_cast<OUString*>(pE->GetUserData());
622 OUString sEventURL;
623 OUString sEventType;
624 if(pThis->bAppEvents)
626 EventsHash::iterator h_it = pThis->m_appEventsHash.find( *pEventName );
627 if(h_it != pThis->m_appEventsHash.end() )
629 sEventType = h_it->second.first;
630 sEventURL = h_it->second.second;
633 else
635 EventsHash::iterator h_it = pThis->m_docEventsHash.find( *pEventName );
636 if(h_it != pThis->m_docEventsHash.end() )
638 sEventType = h_it->second.first;
639 sEventURL = h_it->second.second;
643 bool bDoubleClick = (pBtn == NULL);
644 bool bUNOAssigned = sEventURL.startsWith( aVndSunStarUNO );
645 if( pBtn == pImpl->pDeletePB )
647 // delete pressed
648 sEventType = "Script" ;
649 sEventURL.clear();
650 if(!pThis->bAppEvents)
651 pThis->bDocModified = true;
653 else if ( ( ( pBtn != NULL )
654 && ( pBtn == pImpl->pAssignComponentPB )
656 || ( bDoubleClick
657 && bUNOAssigned
661 VclPtrInstance< AssignComponentDialog > pAssignDlg( pThis, sEventURL );
663 short ret = pAssignDlg->Execute();
664 if( ret )
666 sEventType = "UNO";
667 sEventURL = pAssignDlg->getURL();
668 if(!pThis->bAppEvents)
669 pThis->bDocModified = true;
672 else if( bAssEnabled )
674 // assign pressed
675 VclPtrInstance< SvxScriptSelectorDialog > pDlg( pThis, false, pThis->GetFrame() );
676 if( pDlg )
678 short ret = pDlg->Execute();
679 if ( ret )
681 sEventType = "Script";
682 sEventURL = pDlg->GetScriptURL();
683 if(!pThis->bAppEvents)
684 pThis->bDocModified = true;
689 // update the hashes
690 if(pThis->bAppEvents)
692 EventsHash::iterator h_it = pThis->m_appEventsHash.find( *pEventName );
693 h_it->second.first = sEventType;
694 h_it->second.second = sEventURL;
696 else
698 EventsHash::iterator h_it = pThis->m_docEventsHash.find( *pEventName );
699 h_it->second.first = sEventType;
700 h_it->second.second = sEventURL;
703 // update the listbox entry
704 pImpl->pEventLB->SetUpdateMode( false );
705 pE->ReplaceItem( new IconLBoxString( pE, 0, sEventURL,
706 &pImpl->aMacroImg, &pImpl->aComponentImg ), LB_MACROS_ITEMPOS );
708 rListBox.GetModel()->InvalidateEntry( pE );
709 rListBox.Select( pE );
710 rListBox.MakeVisible( pE );
711 rListBox.SetUpdateMode( true );
713 pThis->EnableButtons();
714 return 0;
717 // pass in the XNameReplace.
718 // can remove the 3rd arg once issue ?? is fixed
719 void _SvxMacroTabPage::InitAndSetHandler( Reference< container::XNameReplace> xAppEvents, Reference< container::XNameReplace> xDocEvents, Reference< util::XModifiable > xModifiable )
721 m_xAppEvents = xAppEvents;
722 m_xDocEvents = xDocEvents;
723 m_xModifiable = xModifiable;
724 SvHeaderTabListBox& rListBox = mpImpl->pEventLB->GetListBox();
725 HeaderBar& rHeaderBar = mpImpl->pEventLB->GetHeaderBar();
726 Link<> aLnk(LINK(this, _SvxMacroTabPage, AssignDeleteHdl_Impl ));
727 mpImpl->pDeletePB->SetClickHdl( aLnk );
728 mpImpl->pAssignPB->SetClickHdl( aLnk );
729 if( mpImpl->pAssignComponentPB )
730 mpImpl->pAssignComponentPB->SetClickHdl( aLnk );
731 rListBox.SetDoubleClickHdl( LINK(this, _SvxMacroTabPage, DoubleClickHdl_Impl ) );
733 rListBox.SetSelectHdl( LINK( this, _SvxMacroTabPage, SelectEvent_Impl ));
735 rListBox.SetSelectionMode( SINGLE_SELECTION );
736 rListBox.SetTabs( &nTabs[0], MAP_APPFONT );
737 Size aSize( nTabs[ 2 ], 0 );
738 rHeaderBar.InsertItem( ITEMID_EVENT, mpImpl->sStrEvent, LogicToPixel( aSize, MapMode( MAP_APPFONT ) ).Width() );
739 aSize.Width() = 1764; // don't know what, so 42^2 is best to use...
740 rHeaderBar.InsertItem( ITMEID_ASSMACRO, mpImpl->sAssignedMacro, LogicToPixel( aSize, MapMode( MAP_APPFONT ) ).Width() );
741 rListBox.SetSpaceBetweenEntries( 0 );
743 mpImpl->pEventLB->Show();
744 mpImpl->pEventLB->ConnectElements();
746 long nMinLineHeight = mpImpl->aMacroImg.GetSizePixel().Height() + 2;
747 if( nMinLineHeight > mpImpl->pEventLB->GetListBox().GetEntryHeight() )
748 mpImpl->pEventLB->GetListBox().SetEntryHeight(
749 sal::static_int_cast< short >(nMinLineHeight) );
751 mpImpl->pEventLB->Enable( true );
753 if(!m_xAppEvents.is())
755 return;
757 Sequence< OUString > eventNames = m_xAppEvents->getElementNames();
758 sal_Int32 nEventCount = eventNames.getLength();
759 for(sal_Int32 nEvent = 0; nEvent < nEventCount; ++nEvent )
761 //need exception handling here
764 m_appEventsHash[ eventNames[nEvent] ] = GetPairFromAny( m_xAppEvents->getByName( eventNames[nEvent] ) );
766 catch (const Exception&)
770 if(m_xDocEvents.is())
772 eventNames = m_xDocEvents->getElementNames();
773 nEventCount = eventNames.getLength();
774 for(sal_Int32 nEvent = 0; nEvent < nEventCount; ++nEvent )
778 m_docEventsHash[ eventNames[nEvent] ] = GetPairFromAny( m_xDocEvents->getByName( eventNames[nEvent] ) );
780 catch (const Exception&)
787 // returns the two props EventType & Script for a given event name
788 Any _SvxMacroTabPage::GetPropsByName( const OUString& eventName, EventsHash& eventsHash )
790 const ::std::pair< OUString, OUString >& rAssignedEvent( eventsHash[ eventName ] );
792 Any aReturn;
793 ::comphelper::NamedValueCollection aProps;
794 if ( !(rAssignedEvent.first.isEmpty() || rAssignedEvent.second.isEmpty()) )
796 aProps.put( "EventType", rAssignedEvent.first );
797 aProps.put( "Script", rAssignedEvent.second );
799 aReturn <<= aProps.getPropertyValues();
801 return aReturn;
804 // converts the Any returned by GetByName into a pair which can be stored in
805 // the EventHash
806 ::std::pair< OUString, OUString > _SvxMacroTabPage::GetPairFromAny( Any aAny )
808 Sequence< beans::PropertyValue > props;
809 OUString type, url;
810 if( aAny >>= props )
812 ::comphelper::NamedValueCollection aProps( props );
813 type = aProps.getOrDefault( "EventType", type );
814 url = aProps.getOrDefault( "Script", url );
816 return ::std::make_pair( type, url );
819 SvxMacroTabPage::SvxMacroTabPage(vcl::Window* pParent,
820 const Reference< frame::XFrame >& _rxDocumentFrame,
821 const SfxItemSet& rSet,
822 Reference< container::XNameReplace > xNameReplace,
823 sal_uInt16 nSelectedIndex)
824 : _SvxMacroTabPage(pParent, "MacroAssignPage", "cui/ui/macroassignpage.ui", rSet)
826 mpImpl->sStrEvent = get<FixedText>("eventft")->GetText();
827 mpImpl->sAssignedMacro = get<FixedText>("assignft")->GetText();
828 get(mpImpl->pEventLB, "assignments");
829 get(mpImpl->pAssignPB, "assign");
830 get(mpImpl->pDeletePB, "delete");
831 get(mpImpl->pAssignComponentPB, "component");
832 mpImpl->aMacroImg = get<FixedImage>("macroimg")->GetImage();
833 mpImpl->aComponentImg = get<FixedImage>("componentimg")->GetImage();
835 SetFrame( _rxDocumentFrame );
837 if( !mpImpl->bIDEDialogMode )
839 mpImpl->pAssignComponentPB->Hide();
840 mpImpl->pAssignComponentPB->Disable();
843 InitResources();
845 InitAndSetHandler( xNameReplace, Reference< container::XNameReplace>(0), Reference< util::XModifiable >(0));
846 DisplayAppEvents(true);
847 SvHeaderTabListBox& rListBox = mpImpl->pEventLB->GetListBox();
848 SvTreeListEntry* pE = rListBox.GetEntry( (sal_uLong)nSelectedIndex );
849 if( pE )
850 rListBox.Select(pE);
853 SvxMacroAssignDlg::SvxMacroAssignDlg( vcl::Window* pParent, const Reference< frame::XFrame >& _rxDocumentFrame, const SfxItemSet& rSet,
854 const Reference< container::XNameReplace >& xNameReplace, sal_uInt16 nSelectedIndex )
855 : SvxMacroAssignSingleTabDialog(pParent, rSet)
857 SetTabPage(VclPtr<SvxMacroTabPage>::Create(get_content_area(), _rxDocumentFrame, rSet, xNameReplace, nSelectedIndex));
862 IMPL_LINK_NOARG(AssignComponentDialog, ButtonHandler)
864 OUString aMethodName = mpMethodEdit->GetText();
865 maURL.clear();
866 if( !aMethodName.isEmpty() )
868 maURL = aVndSunStarUNO;
869 maURL += aMethodName;
871 EndDialog(1);
872 return 0;
875 AssignComponentDialog::AssignComponentDialog( vcl::Window * pParent, const OUString& rURL )
876 : ModalDialog( pParent, "AssignComponent", "cui/ui/assigncomponentdialog.ui" )
877 , maURL( rURL )
879 get(mpMethodEdit, "methodEntry");
880 get(mpOKButton, "ok");
881 mpOKButton->SetClickHdl(LINK(this, AssignComponentDialog, ButtonHandler));
883 OUString aMethodName;
884 if( maURL.startsWith( aVndSunStarUNO ) )
886 aMethodName = maURL.copy( strlen(aVndSunStarUNO) );
888 mpMethodEdit->SetText( aMethodName, Selection( 0, SELECTION_MAX ) );
891 AssignComponentDialog::~AssignComponentDialog()
893 disposeOnce();
896 void AssignComponentDialog::dispose()
898 mpMethodEdit.clear();
899 mpOKButton.clear();
900 ModalDialog::dispose();
903 IMPL_LINK( SvxMacroAssignSingleTabDialog, OKHdl_Impl, Button *, pButton )
905 (void)pButton; //unused
906 GetTabPage()->FillItemSet( 0 );
907 EndDialog( RET_OK );
908 return 0;
913 SvxMacroAssignSingleTabDialog::SvxMacroAssignSingleTabDialog(vcl::Window *pParent,
914 const SfxItemSet& rSet)
915 : SfxSingleTabDialog(pParent, rSet, "MacroAssignDialog", "cui/ui/macroassigndialog.ui")
917 GetOKButton()->SetClickHdl( LINK( this, SvxMacroAssignSingleTabDialog, OKHdl_Impl ) );
920 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */