Update ooo320-m1
[ooovba.git] / svx / source / mnuctrls / SmartTagCtl.cxx
blobc3cd979955d7aa08bb1ad89ca07545b9cdc43a09
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: SmartTagCtl.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svx.hxx"
34 #include <svx/SmartTagCtl.hxx>
35 #include <com/sun/star/smarttags/XSmartTagAction.hpp>
36 #ifndef _COM_SUN_STAR_SMARTTAGS_XSTRINGKEYMAP_HPP_
37 #include <com/sun/star/container/XStringKeyMap.hpp>
38 #endif
39 #include <svtools/stdmenu.hxx>
40 #include <svtools/eitem.hxx>
41 #include <sfx2/dispatch.hxx>
42 #ifndef _SVX_SVXIDS_HRC
43 #include <svx/svxids.hrc>
44 #endif
46 #include <svx/SmartTagItem.hxx>
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::uno;
51 #define C2U(cChar) rtl::OUString::createFromAscii(cChar)
53 // STATIC DATA -----------------------------------------------------------
55 SFX_IMPL_MENU_CONTROL(SvxSmartTagsControl, SvxSmartTagItem);
57 //--------------------------------------------------------------------
59 SvxSmartTagsControl::SvxSmartTagsControl
61 USHORT _nId,
62 Menu& rMenu,
63 SfxBindings& /*rBindings*/
64 ) :
65 mpMenu ( new PopupMenu ),
66 mrParent ( rMenu ),
67 mpSmartTagItem( 0 )
69 rMenu.SetPopupMenu( _nId, mpMenu );
72 //--------------------------------------------------------------------
74 const USHORT MN_ST_INSERT_START = 500;
76 void SvxSmartTagsControl::FillMenu()
78 if ( !mpSmartTagItem )
79 return;
81 USHORT nMenuPos = 0;
82 USHORT nSubMenuPos = 0;
83 USHORT nMenuId = 1;
84 USHORT nSubMenuId = MN_ST_INSERT_START;
86 const Sequence < Sequence< Reference< smarttags::XSmartTagAction > > >& rActionComponentsSequence = mpSmartTagItem->GetActionComponentsSequence();
87 const Sequence < Sequence< sal_Int32 > >& rActionIndicesSequence = mpSmartTagItem->GetActionIndicesSequence();
88 const Sequence< Reference< container::XStringKeyMap > >& rStringKeyMaps = mpSmartTagItem->GetStringKeyMaps();
89 const lang::Locale& rLocale = mpSmartTagItem->GetLocale();
90 const rtl::OUString aApplicationName = mpSmartTagItem->GetApplicationName();
91 const rtl::OUString aRangeText = mpSmartTagItem->GetRangeText();
92 const Reference<text::XTextRange>& xTextRange = mpSmartTagItem->GetTextRange();
93 const Reference<frame::XController>& xController = mpSmartTagItem->GetController();
95 for ( USHORT j = 0; j < rActionComponentsSequence.getLength(); ++j )
97 Reference< container::XStringKeyMap > xSmartTagProperties = rStringKeyMaps[j];
99 // Get all actions references associated with the current smart tag type:
100 const Sequence< Reference< smarttags::XSmartTagAction > >& rActionComponents = rActionComponentsSequence[j];
101 const Sequence< sal_Int32 >& rActionIndices = rActionIndicesSequence[j];
103 if ( 0 == rActionComponents.getLength() || 0 == rActionIndices.getLength() )
104 continue;
106 // Ask first entry for the smart tag type caption:
107 Reference< smarttags::XSmartTagAction > xAction = rActionComponents[0];
109 if ( !xAction.is() )
110 continue;
112 const sal_Int32 nSmartTagIndex = rActionIndices[0];
113 const rtl::OUString aSmartTagType = xAction->getSmartTagName( nSmartTagIndex );
114 const rtl::OUString aSmartTagCaption = xAction->getSmartTagCaption( nSmartTagIndex, rLocale );
116 // no sub-menues if there's only one smart tag type listed:
117 PopupMenu* pSbMenu = mpMenu;
118 if ( 1 < rActionComponentsSequence.getLength() )
120 mpMenu->InsertItem( nMenuId, aSmartTagCaption, 0, nMenuPos++);
121 pSbMenu = new PopupMenu;
122 mpMenu->SetPopupMenu( nMenuId++, pSbMenu );
124 pSbMenu->SetSelectHdl( LINK( this, SvxSmartTagsControl, MenuSelect ) );
126 // sub-menu starts with smart tag caption and separator
127 const rtl::OUString aSmartTagCaption2 = aSmartTagCaption + C2U(": ") + aRangeText;
128 nSubMenuPos = 0;
129 pSbMenu->InsertItem( nMenuId++, aSmartTagCaption2, MIB_NOSELECT, nSubMenuPos++ );
130 pSbMenu->InsertSeparator( nSubMenuPos++ );
132 // Add subitem for every action reference for the current smart tag type:
133 for ( USHORT i = 0; i < rActionComponents.getLength(); ++i )
135 xAction = rActionComponents[i];
137 for ( sal_Int32 k = 0; k < xAction->getActionCount( aSmartTagType, xController ); ++k )
139 const sal_uInt32 nActionID = xAction->getActionID( aSmartTagType, k, xController );
140 rtl::OUString aActionCaption = xAction->getActionCaptionFromID( nActionID,
141 aApplicationName,
142 rLocale,
143 xSmartTagProperties,
144 aRangeText,
145 rtl::OUString(),
146 xController,
147 xTextRange );
149 pSbMenu->InsertItem( nSubMenuId++, aActionCaption, 0, nSubMenuPos++ );
150 InvokeAction aEntry( xAction, xSmartTagProperties, nActionID );
151 maInvokeActions.push_back( aEntry );
157 //--------------------------------------------------------------------
159 void SvxSmartTagsControl::StateChanged( USHORT, SfxItemState eState, const SfxPoolItem* pState )
162 mrParent.EnableItem( GetId(), SFX_ITEM_DISABLED != eState );
164 if ( SFX_ITEM_AVAILABLE == eState )
166 const SvxSmartTagItem* pSmartTagItem = PTR_CAST( SvxSmartTagItem, pState );
167 if ( 0 != pSmartTagItem )
169 delete mpSmartTagItem;
170 mpSmartTagItem = new SvxSmartTagItem( *pSmartTagItem );
171 FillMenu();
176 //--------------------------------------------------------------------
178 IMPL_LINK_INLINE_START( SvxSmartTagsControl, MenuSelect, PopupMenu *, pMen )
180 if ( !mpSmartTagItem )
181 return 0;
183 sal_uInt16 nMyId = pMen->GetCurItemId();
185 if ( nMyId < MN_ST_INSERT_START) return 0;
186 nMyId -= MN_ST_INSERT_START;
188 // compute smarttag lib index and action index
189 Reference< smarttags::XSmartTagAction > xSmartTagAction = maInvokeActions[ nMyId ].mxAction;
191 // execute action
192 if ( xSmartTagAction.is() )
194 xSmartTagAction->invokeAction( maInvokeActions[ nMyId ].mnActionID,
195 mpSmartTagItem->GetApplicationName(),
196 mpSmartTagItem->GetController(),
197 mpSmartTagItem->GetTextRange(),
198 maInvokeActions[ nMyId ].mxSmartTagProperties,
199 mpSmartTagItem->GetRangeText(),
200 rtl::OUString(),
201 mpSmartTagItem->GetLocale() );
204 // ohne dispatcher!!!
205 // GetBindings().Execute( GetId(), SFX_CALLMODE_RECORD,meine beiden items, 0L );*/
206 //SfxBoolItem aBool(SID_OPEN_SMARTTAGOPTIONS, TRUE);
207 //GetBindings().GetDispatcher()->Execute( SID_AUTO_CORRECT_DLG, SFX_CALLMODE_ASYNCHRON, &aBool, 0L );
209 return 0;
211 IMPL_LINK_INLINE_END( SvxSmartTagsControl, MenuSelect, PopupMenu *, pMen )
213 //--------------------------------------------------------------------
215 SvxSmartTagsControl::~SvxSmartTagsControl()
217 delete mpSmartTagItem;
218 delete mpMenu;
221 //--------------------------------------------------------------------
223 PopupMenu* SvxSmartTagsControl::GetPopup() const
225 return mpMenu;