xref: /aoo42x/main/sw/source/ui/smartmenu/stmenu.cxx (revision d78c8e1e)
1efeef26fSAndrew Rist /**************************************************************
2*d78c8e1eSmseidel  *
3efeef26fSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4efeef26fSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5efeef26fSAndrew Rist  * distributed with this work for additional information
6efeef26fSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7efeef26fSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8efeef26fSAndrew Rist  * "License"); you may not use this file except in compliance
9efeef26fSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*d78c8e1eSmseidel  *
11efeef26fSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*d78c8e1eSmseidel  *
13efeef26fSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14efeef26fSAndrew Rist  * software distributed under the License is distributed on an
15efeef26fSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16efeef26fSAndrew Rist  * KIND, either express or implied.  See the License for the
17efeef26fSAndrew Rist  * specific language governing permissions and limitations
18efeef26fSAndrew Rist  * under the License.
19*d78c8e1eSmseidel  *
20efeef26fSAndrew Rist  *************************************************************/
21efeef26fSAndrew Rist 
22efeef26fSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // SMARTTAGS
25cdf0e10cSrcweir 
26cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
27cdf0e10cSrcweir #include "precompiled_sw.hxx"
28cdf0e10cSrcweir #include <stmenu.hxx>
29cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
30cdf0e10cSrcweir #include <svl/eitem.hxx>
31cdf0e10cSrcweir #include <sfx2/dispatch.hxx>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <SwSmartTagMgr.hxx>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #ifndef _STMENU_HRC
36cdf0e10cSrcweir #include <stmenu.hrc>
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir #ifndef _VIEW_HXX
39cdf0e10cSrcweir #include <view.hxx>
40cdf0e10cSrcweir #endif
41cdf0e10cSrcweir #include <breakit.hxx>
42cdf0e10cSrcweir 
43cdf0e10cSrcweir #define C2U(cChar) rtl::OUString::createFromAscii(cChar)
44cdf0e10cSrcweir 
45cdf0e10cSrcweir using namespace ::com::sun::star;
46cdf0e10cSrcweir using namespace ::com::sun::star::uno;
47cdf0e10cSrcweir 
SwSmartTagPopup(SwView * pSwView,Sequence<rtl::OUString> & rSmartTagTypes,Sequence<Reference<container::XStringKeyMap>> & rStringKeyMaps,Reference<text::XTextRange> xTextRange)48cdf0e10cSrcweir SwSmartTagPopup::SwSmartTagPopup( SwView* pSwView,
49*d78c8e1eSmseidel 								  Sequence< rtl::OUString >& rSmartTagTypes,
50*d78c8e1eSmseidel 								  Sequence< Reference< container::XStringKeyMap > >& rStringKeyMaps,
51*d78c8e1eSmseidel 								  Reference< text::XTextRange > xTextRange ) :
52*d78c8e1eSmseidel 	PopupMenu( SW_RES(MN_SMARTTAG_POPUP) ),
53*d78c8e1eSmseidel 	mpSwView ( pSwView ),
54*d78c8e1eSmseidel 	mxTextRange( xTextRange )
55cdf0e10cSrcweir {
56*d78c8e1eSmseidel 	//CreateAutoMnemonics();
57*d78c8e1eSmseidel 
58*d78c8e1eSmseidel 	Reference <frame::XController> xController = mpSwView->GetController();
59*d78c8e1eSmseidel 	const lang::Locale aLocale( SW_BREAKITER()->GetLocale( (LanguageType)GetAppLanguage() ) );
60*d78c8e1eSmseidel 
61*d78c8e1eSmseidel 	sal_uInt16 nMenuPos = 0;
62*d78c8e1eSmseidel 	sal_uInt16 nSubMenuPos = 0;
63*d78c8e1eSmseidel 	sal_uInt16 nMenuId = 1;
64*d78c8e1eSmseidel 	sal_uInt16 nSubMenuId = MN_ST_INSERT_START;
65*d78c8e1eSmseidel 
66*d78c8e1eSmseidel 	const rtl::OUString aRangeText = mxTextRange->getString();
67*d78c8e1eSmseidel 
68*d78c8e1eSmseidel 	SmartTagMgr& rSmartTagMgr = SwSmartTagMgr::Get();
69*d78c8e1eSmseidel 	const rtl::OUString aApplicationName( rSmartTagMgr.GetApplicationName() );
70*d78c8e1eSmseidel 
71*d78c8e1eSmseidel 	Sequence < Sequence< Reference< smarttags::XSmartTagAction > > > aActionComponentsSequence;
72*d78c8e1eSmseidel 	Sequence < Sequence< sal_Int32 > > aActionIndicesSequence;
73*d78c8e1eSmseidel 
74*d78c8e1eSmseidel 	rSmartTagMgr.GetActionSequences( rSmartTagTypes,
75*d78c8e1eSmseidel 									 aActionComponentsSequence,
76*d78c8e1eSmseidel 									 aActionIndicesSequence );
77*d78c8e1eSmseidel 
78*d78c8e1eSmseidel 	InsertSeparator(0);
79*d78c8e1eSmseidel 
80*d78c8e1eSmseidel 	for ( sal_uInt16 j = 0; j < aActionComponentsSequence.getLength(); ++j )
81*d78c8e1eSmseidel 	{
82*d78c8e1eSmseidel 		Reference< container::XStringKeyMap > xSmartTagProperties = rStringKeyMaps[j];
83*d78c8e1eSmseidel 
84*d78c8e1eSmseidel 		// Get all actions references associated with the current smart tag type:
85*d78c8e1eSmseidel 		const Sequence< Reference< smarttags::XSmartTagAction > >& rActionComponents = aActionComponentsSequence[j];
86*d78c8e1eSmseidel 		const Sequence< sal_Int32 >& rActionIndices = aActionIndicesSequence[j];
87*d78c8e1eSmseidel 
88*d78c8e1eSmseidel 		if ( 0 == rActionComponents.getLength() || 0 == rActionIndices.getLength() )
89*d78c8e1eSmseidel 			continue;
90*d78c8e1eSmseidel 
91*d78c8e1eSmseidel 		// Ask first entry for the smart tag type caption:
92*d78c8e1eSmseidel 		Reference< smarttags::XSmartTagAction > xAction = rActionComponents[0];
93*d78c8e1eSmseidel 
94*d78c8e1eSmseidel 		if ( !xAction.is() )
95*d78c8e1eSmseidel 			continue;
96*d78c8e1eSmseidel 
97*d78c8e1eSmseidel 		const sal_Int32 nSmartTagIndex = rActionIndices[0];
98*d78c8e1eSmseidel 		const rtl::OUString aSmartTagType = xAction->getSmartTagName( nSmartTagIndex );
99*d78c8e1eSmseidel 		const rtl::OUString aSmartTagCaption = xAction->getSmartTagCaption( nSmartTagIndex, aLocale );
100*d78c8e1eSmseidel 
101*d78c8e1eSmseidel 		// no sub-menus if there's only one smart tag type listed:
102*d78c8e1eSmseidel 		PopupMenu* pSbMenu = this;
103*d78c8e1eSmseidel 		if ( 1 < aActionComponentsSequence.getLength() )
104*d78c8e1eSmseidel 		{
105*d78c8e1eSmseidel 			InsertItem( nMenuId, aSmartTagCaption, 0, nMenuPos++);
106*d78c8e1eSmseidel 			pSbMenu = new PopupMenu;
107*d78c8e1eSmseidel 			SetPopupMenu( nMenuId++, pSbMenu );
108*d78c8e1eSmseidel 		}
109*d78c8e1eSmseidel 
110*d78c8e1eSmseidel 		// sub-menu starts with smart tag caption and separator
111*d78c8e1eSmseidel 		const rtl::OUString aSmartTagCaption2 = aSmartTagCaption + C2U(": ") + aRangeText;
112*d78c8e1eSmseidel 		nSubMenuPos = 0;
113*d78c8e1eSmseidel 		pSbMenu->InsertItem( nMenuId++, aSmartTagCaption2, MIB_NOSELECT, nSubMenuPos++ );
114*d78c8e1eSmseidel 		pSbMenu->InsertSeparator( nSubMenuPos++ );
115*d78c8e1eSmseidel 
116*d78c8e1eSmseidel 		// Add subitem for every action reference for the current smart tag type:
117*d78c8e1eSmseidel 		for ( sal_uInt16 i = 0; i < rActionComponents.getLength(); ++i )
118*d78c8e1eSmseidel 		{
119*d78c8e1eSmseidel 			xAction = rActionComponents[i];
120*d78c8e1eSmseidel 
121*d78c8e1eSmseidel 			for ( sal_Int32 k = 0; k < xAction->getActionCount( aSmartTagType, xController, xSmartTagProperties ); ++k )
122*d78c8e1eSmseidel 			{
123*d78c8e1eSmseidel 				const sal_uInt32 nActionID = xAction->getActionID( aSmartTagType, k, xController );
124*d78c8e1eSmseidel 				rtl::OUString aActionCaption = xAction->getActionCaptionFromID( nActionID,
125*d78c8e1eSmseidel 																				aApplicationName,
126*d78c8e1eSmseidel 																				aLocale,
127*d78c8e1eSmseidel 																				xSmartTagProperties,
128*d78c8e1eSmseidel 																				aRangeText,
129*d78c8e1eSmseidel 																				rtl::OUString(),
130*d78c8e1eSmseidel 																				xController,
131*d78c8e1eSmseidel 																				mxTextRange );
132*d78c8e1eSmseidel 
133*d78c8e1eSmseidel 				pSbMenu->InsertItem( nSubMenuId++, aActionCaption, 0, nSubMenuPos++ );
134*d78c8e1eSmseidel 				InvokeAction aEntry( xAction, xSmartTagProperties, nActionID );
135*d78c8e1eSmseidel 				maInvokeActions.push_back( aEntry );
136*d78c8e1eSmseidel 			}
137*d78c8e1eSmseidel 		}
138*d78c8e1eSmseidel 	}
139cdf0e10cSrcweir }
140cdf0e10cSrcweir 
141cdf0e10cSrcweir /** Function: Execute
142cdf0e10cSrcweir 
143*d78c8e1eSmseidel 	executes actions by calling the invoke function of the appropriate
144*d78c8e1eSmseidel 	smarttag library.
145cdf0e10cSrcweir 
146cdf0e10cSrcweir */
Execute(const Rectangle & rWordPos,Window * pWin)147cdf0e10cSrcweir sal_uInt16 SwSmartTagPopup::Execute( const Rectangle& rWordPos, Window* pWin )
148cdf0e10cSrcweir {
149*d78c8e1eSmseidel 	sal_uInt16 nId = PopupMenu::Execute(pWin, pWin->LogicToPixel(rWordPos));
150*d78c8e1eSmseidel 
151*d78c8e1eSmseidel 	if ( nId == MN_SMARTTAG_OPTIONS )
152*d78c8e1eSmseidel 	{
153*d78c8e1eSmseidel 		SfxBoolItem aBool(SID_OPEN_SMARTTAGOPTIONS, sal_True);
154*d78c8e1eSmseidel 		mpSwView->GetViewFrame()->GetDispatcher()->Execute( SID_AUTO_CORRECT_DLG, SFX_CALLMODE_ASYNCHRON, &aBool, 0L );
155*d78c8e1eSmseidel 	}
156*d78c8e1eSmseidel 
157*d78c8e1eSmseidel 	if ( nId < MN_ST_INSERT_START) return nId;
158*d78c8e1eSmseidel 	nId -= MN_ST_INSERT_START;
159*d78c8e1eSmseidel 
160*d78c8e1eSmseidel 	// compute smarttag lib index and action index
161*d78c8e1eSmseidel 	if ( nId < maInvokeActions.size() )
162*d78c8e1eSmseidel 	{
163*d78c8e1eSmseidel 		Reference< smarttags::XSmartTagAction > xSmartTagAction = maInvokeActions[ nId ].mxAction;
164*d78c8e1eSmseidel 
165*d78c8e1eSmseidel 		// execute action
166*d78c8e1eSmseidel 		if ( xSmartTagAction.is() )
167*d78c8e1eSmseidel 		{
168*d78c8e1eSmseidel 			SmartTagMgr& rSmartTagMgr = SwSmartTagMgr::Get();
169*d78c8e1eSmseidel 
170*d78c8e1eSmseidel 			xSmartTagAction->invokeAction( maInvokeActions[ nId ].mnActionID,
171*d78c8e1eSmseidel 										   rSmartTagMgr.GetApplicationName(),
172*d78c8e1eSmseidel 										   mpSwView->GetController(),
173*d78c8e1eSmseidel 										   mxTextRange,
174*d78c8e1eSmseidel 										   maInvokeActions[ nId ].mxSmartTagProperties,
175*d78c8e1eSmseidel 										   mxTextRange->getString(),
176*d78c8e1eSmseidel 										   rtl::OUString(),
177*d78c8e1eSmseidel 										   SW_BREAKITER()->GetLocale( (LanguageType)GetAppLanguage() ) );
178*d78c8e1eSmseidel 		}
179*d78c8e1eSmseidel 	}
180*d78c8e1eSmseidel 
181*d78c8e1eSmseidel 	return nId;
182cdf0e10cSrcweir }
183