1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svx.hxx"
26 
27 #include <svx/SmartTagCtl.hxx>
28 #include <com/sun/star/smarttags/XSmartTagAction.hpp>
29 #ifndef _COM_SUN_STAR_SMARTTAGS_XSTRINGKEYMAP_HPP_
30 #include <com/sun/star/container/XStringKeyMap.hpp>
31 #endif
32 #include <svtools/stdmenu.hxx>
33 #include <svl/eitem.hxx>
34 #include <sfx2/dispatch.hxx>
35 #ifndef _SVX_SVXIDS_HRC
36 #include <svx/svxids.hrc>
37 #endif
38 
39 #include <svx/SmartTagItem.hxx>
40 
41 using namespace ::com::sun::star;
42 using namespace ::com::sun::star::uno;
43 
44 #define C2U(cChar) rtl::OUString::createFromAscii(cChar)
45 
46 // STATIC DATA -----------------------------------------------------------
47 
48 SFX_IMPL_MENU_CONTROL(SvxSmartTagsControl, SvxSmartTagItem);
49 
50 //--------------------------------------------------------------------
51 
SvxSmartTagsControl(sal_uInt16 _nId,Menu & rMenu,SfxBindings &)52 SvxSmartTagsControl::SvxSmartTagsControl
53 (
54 	sal_uInt16 			_nId,
55 	Menu&			rMenu,
56 	SfxBindings&	/*rBindings*/
57 ) :
58 	mpMenu	( new PopupMenu ),
59 	mrParent	( rMenu ),
60 	mpSmartTagItem( 0 )
61 {
62 	rMenu.SetPopupMenu( _nId, mpMenu );
63 }
64 
65 //--------------------------------------------------------------------
66 
67 const sal_uInt16 MN_ST_INSERT_START = 500;
68 
FillMenu()69 void SvxSmartTagsControl::FillMenu()
70 {
71 	if ( !mpSmartTagItem )
72 		return;
73 
74 	sal_uInt16 nMenuPos = 0;
75 	sal_uInt16 nSubMenuPos = 0;
76 	sal_uInt16 nMenuId = 1;
77 	sal_uInt16 nSubMenuId = MN_ST_INSERT_START;
78 
79 	const Sequence < Sequence< Reference< smarttags::XSmartTagAction > > >& rActionComponentsSequence = mpSmartTagItem->GetActionComponentsSequence();
80 	const Sequence < Sequence< sal_Int32 > >& rActionIndicesSequence = mpSmartTagItem->GetActionIndicesSequence();
81 	const Sequence< Reference< container::XStringKeyMap > >& rStringKeyMaps = mpSmartTagItem->GetStringKeyMaps();
82 	const lang::Locale& rLocale = mpSmartTagItem->GetLocale();
83 	const rtl::OUString aApplicationName = mpSmartTagItem->GetApplicationName();
84 	const rtl::OUString aRangeText = mpSmartTagItem->GetRangeText();
85 	const Reference<text::XTextRange>& xTextRange = mpSmartTagItem->GetTextRange();
86 	const Reference<frame::XController>& xController = mpSmartTagItem->GetController();
87 
88 	for ( sal_uInt16 j = 0; j < rActionComponentsSequence.getLength(); ++j )
89 	{
90 		Reference< container::XStringKeyMap > xSmartTagProperties = rStringKeyMaps[j];
91 
92 		// Get all actions references associated with the current smart tag type:
93 		const Sequence< Reference< smarttags::XSmartTagAction > >& rActionComponents = rActionComponentsSequence[j];
94 		const Sequence< sal_Int32 >& rActionIndices = rActionIndicesSequence[j];
95 
96 		if ( 0 == rActionComponents.getLength() || 0 == rActionIndices.getLength() )
97 			continue;
98 
99 		// Ask first entry for the smart tag type caption:
100 		Reference< smarttags::XSmartTagAction > xAction = rActionComponents[0];
101 
102 		if ( !xAction.is() )
103 			continue;
104 
105 		const sal_Int32 nSmartTagIndex = rActionIndices[0];
106 		const rtl::OUString aSmartTagType = xAction->getSmartTagName( nSmartTagIndex );
107 		const rtl::OUString aSmartTagCaption = xAction->getSmartTagCaption( nSmartTagIndex, rLocale);
108 
109 		// no sub-menus if there's only one smart tag type listed:
110 		PopupMenu* pSbMenu = mpMenu;
111 		if ( 1 < rActionComponentsSequence.getLength() )
112 		{
113 			mpMenu->InsertItem( nMenuId, aSmartTagCaption, 0, nMenuPos++);
114 			pSbMenu = new PopupMenu;
115 			mpMenu->SetPopupMenu( nMenuId++, pSbMenu );
116 		}
117 		pSbMenu->SetSelectHdl( LINK( this, SvxSmartTagsControl, MenuSelect ) );
118 
119 		// sub-menu starts with smart tag caption and separator
120 		const rtl::OUString aSmartTagCaption2 = aSmartTagCaption + C2U(": ") + aRangeText;
121 		nSubMenuPos = 0;
122 		pSbMenu->InsertItem( nMenuId++, aSmartTagCaption2, MIB_NOSELECT, nSubMenuPos++ );
123 		pSbMenu->InsertSeparator( nSubMenuPos++ );
124 
125 		// Add subitem for every action reference for the current smart tag type:
126 		for ( sal_uInt16 i = 0; i < rActionComponents.getLength(); ++i )
127 		{
128 			xAction = rActionComponents[i];
129 
130 			for ( sal_Int32 k = 0; k < xAction->getActionCount( aSmartTagType, xController, xSmartTagProperties ); ++k )
131 			{
132                 const sal_uInt32 nActionID = xAction->getActionID( aSmartTagType, k, xController );
133                 rtl::OUString aActionCaption = xAction->getActionCaptionFromID( nActionID,
134                                                                                 aApplicationName,
135                                                                                 rLocale,
136                                                                                 xSmartTagProperties,
137                                                                                 aRangeText,
138                                                                                 rtl::OUString(),
139                                                                                 xController,
140                                                                                 xTextRange );
141 
142                 pSbMenu->InsertItem( nSubMenuId++, aActionCaption, 0, nSubMenuPos++ );
143                 InvokeAction aEntry( xAction, xSmartTagProperties, nActionID );
144                 maInvokeActions.push_back( aEntry );
145             }
146         }
147     }
148 }
149 
150 //--------------------------------------------------------------------
151 
StateChanged(sal_uInt16,SfxItemState eState,const SfxPoolItem * pState)152 void SvxSmartTagsControl::StateChanged( sal_uInt16, SfxItemState eState, const SfxPoolItem* pState )
153 
154 {
155 	mrParent.EnableItem( GetId(), SFX_ITEM_DISABLED != eState );
156 
157 	if ( SFX_ITEM_AVAILABLE == eState )
158 	{
159 		const SvxSmartTagItem* pSmartTagItem = PTR_CAST( SvxSmartTagItem, pState );
160 		if ( 0 != pSmartTagItem )
161 		{
162             delete mpSmartTagItem;
163             mpSmartTagItem = new SvxSmartTagItem( *pSmartTagItem );
164             FillMenu();
165 		}
166 	}
167 }
168 
169 //--------------------------------------------------------------------
170 
IMPL_LINK_INLINE_START(SvxSmartTagsControl,MenuSelect,PopupMenu *,pMen)171 IMPL_LINK_INLINE_START( SvxSmartTagsControl, MenuSelect, PopupMenu *, pMen )
172 {
173 	if ( !mpSmartTagItem )
174 		return 0;
175 
176 	sal_uInt16 nMyId = pMen->GetCurItemId();
177 
178 	if ( nMyId < MN_ST_INSERT_START) return 0;
179 	nMyId -= MN_ST_INSERT_START;
180 
181 	// compute smarttag lib index and action index
182 	Reference< smarttags::XSmartTagAction > xSmartTagAction = maInvokeActions[ nMyId ].mxAction;
183 
184 	// execute action
185 	if ( xSmartTagAction.is() )
186 	{
187 		xSmartTagAction->invokeAction( maInvokeActions[ nMyId ].mnActionID,
188                                        mpSmartTagItem->GetApplicationName(),
189                                        mpSmartTagItem->GetController(),
190                                        mpSmartTagItem->GetTextRange(),
191                                        maInvokeActions[ nMyId ].mxSmartTagProperties,
192                                        mpSmartTagItem->GetRangeText(),
193                                        rtl::OUString(),
194                                        mpSmartTagItem->GetLocale() );
195 	}
196 
197 	// ohne dispatcher!!!
198 	// GetBindings().Execute( GetId(), SFX_CALLMODE_RECORD,meine beiden items, 0L );*/
199 	//SfxBoolItem aBool(SID_OPEN_SMARTTAGOPTIONS, sal_True);
200 	//GetBindings().GetDispatcher()->Execute( SID_AUTO_CORRECT_DLG, SFX_CALLMODE_ASYNCHRON, &aBool, 0L );
201 
202 	return 0;
203 }
IMPL_LINK_INLINE_END(SvxSmartTagsControl,MenuSelect,PopupMenu *,pMen)204 IMPL_LINK_INLINE_END( SvxSmartTagsControl, MenuSelect, PopupMenu *, pMen )
205 
206 //--------------------------------------------------------------------
207 
208 SvxSmartTagsControl::~SvxSmartTagsControl()
209 {
210 	delete mpSmartTagItem;
211 	delete mpMenu;
212 }
213 
214 //--------------------------------------------------------------------
215 
GetPopup() const216 PopupMenu* SvxSmartTagsControl::GetPopup() const
217 {
218 	return mpMenu;
219 }
220 
221 /* vim: set noet sw=4 ts=4: */
222