Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / svx / source / mnuctrls / smarttagmenu.cxx
bloba53d00de888a8575d6fdbd1e74746e7e95e1c983
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 <memory>
21 #include <svtools/popupmenucontrollerbase.hxx>
22 #include <svx/SmartTagItem.hxx>
23 #include <vcl/commandinfoprovider.hxx>
24 #include <vcl/menu.hxx>
26 const sal_uInt16 MN_ST_INSERT_START = 500;
28 class SmartTagMenuController : public svt::PopupMenuControllerBase
30 public:
31 explicit SmartTagMenuController( const css::uno::Reference< css::uno::XComponentContext >& rxContext );
33 // XStatusListener
34 virtual void SAL_CALL statusChanged( const css::frame::FeatureStateEvent& rEvent ) override;
36 // XServiceInfo
37 virtual OUString SAL_CALL getImplementationName() override;
38 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
40 private:
41 void FillMenu();
42 DECL_LINK( MenuSelect, Menu*, bool );
43 struct InvokeAction
45 css::uno::Reference< css::smarttags::XSmartTagAction > m_xAction;
46 css::uno::Reference< css::container::XStringKeyMap > m_xSmartTagProperties;
47 sal_uInt32 m_nActionID;
48 InvokeAction( css::uno::Reference< css::smarttags::XSmartTagAction > const & xAction,
49 css::uno::Reference< css::container::XStringKeyMap > const & xSmartTagProperties,
50 sal_uInt32 nActionID ) : m_xAction( xAction ), m_xSmartTagProperties( xSmartTagProperties ), m_nActionID( nActionID ) {}
52 std::vector< InvokeAction > m_aInvokeActions;
53 std::unique_ptr< const SvxSmartTagItem > m_pSmartTagItem;
56 SmartTagMenuController::SmartTagMenuController( const css::uno::Reference< css::uno::XComponentContext >& rxContext )
57 : svt::PopupMenuControllerBase( rxContext )
61 void SmartTagMenuController::statusChanged( const css::frame::FeatureStateEvent& rEvent )
63 resetPopupMenu( m_xPopupMenu );
65 css::uno::Sequence< css::beans::PropertyValue > aProperties;
66 if ( rEvent.IsEnabled && ( rEvent.State >>= aProperties ) )
68 css::uno::Sequence< css::uno::Sequence< css::uno::Reference< css::smarttags::XSmartTagAction > > > aActionComponents;
69 css::uno::Sequence< css::uno::Sequence< sal_Int32 > > aActionIndices;
70 css::uno::Sequence< css::uno::Reference< css::container::XStringKeyMap > > aStringKeyMaps;
71 css::uno::Reference< css::text::XTextRange > xTextRange;
72 css::uno::Reference< css::frame::XController > xController;
73 css::lang::Locale aLocale;
74 OUString aApplicationName;
75 OUString aRangeText;
77 for ( const auto& aProperty : aProperties )
79 if ( aProperty.Name == "ActionComponents" )
80 aProperty.Value >>= aActionComponents;
81 else if ( aProperty.Name == "ActionIndices" )
82 aProperty.Value >>= aActionIndices;
83 else if ( aProperty.Name == "StringKeyMaps" )
84 aProperty.Value >>= aStringKeyMaps;
85 else if ( aProperty.Name == "TextRange" )
86 aProperty.Value >>= xTextRange;
87 else if ( aProperty.Name == "Controller" )
88 aProperty.Value >>= xController;
89 else if ( aProperty.Name == "Locale" )
90 aProperty.Value >>= aLocale;
91 else if ( aProperty.Name == "ApplicationName" )
92 aProperty.Value >>= aApplicationName;
93 else if ( aProperty.Name == "RangeText" )
94 aProperty.Value >>= aRangeText;
96 m_pSmartTagItem.reset( new SvxSmartTagItem( 0, aActionComponents, aActionIndices, aStringKeyMaps, xTextRange, xController, aLocale, aApplicationName, aRangeText ) );
97 FillMenu();
101 void SmartTagMenuController::FillMenu()
103 if ( !m_pSmartTagItem )
104 return;
106 sal_uInt16 nMenuId = 1;
107 sal_uInt16 nSubMenuId = MN_ST_INSERT_START;
109 VCLXMenu* pAwtMenu = VCLXMenu::GetImplementation( m_xPopupMenu );
110 PopupMenu* pVCLMenu = static_cast< PopupMenu* >( pAwtMenu->GetMenu() );
112 const css::uno::Sequence< css::uno::Sequence< css::uno::Reference< css::smarttags::XSmartTagAction > > >& rActionComponentsSequence = m_pSmartTagItem->GetActionComponentsSequence();
113 const css::uno::Sequence< css::uno::Sequence< sal_Int32 > >& rActionIndicesSequence = m_pSmartTagItem->GetActionIndicesSequence();
114 const css::uno::Sequence< css::uno::Reference< css::container::XStringKeyMap > >& rStringKeyMaps = m_pSmartTagItem->GetStringKeyMaps();
115 const css::lang::Locale& rLocale = m_pSmartTagItem->GetLocale();
116 const OUString aApplicationName = m_pSmartTagItem->GetApplicationName();
117 const OUString aRangeText = m_pSmartTagItem->GetRangeText();
118 const css::uno::Reference< css::text::XTextRange >& xTextRange = m_pSmartTagItem->GetTextRange();
119 const css::uno::Reference< css::frame::XController >& xController = m_pSmartTagItem->GetController();
121 for ( sal_Int32 i = 0; i < rActionComponentsSequence.getLength(); ++i )
123 css::uno::Reference< css::container::XStringKeyMap > xSmartTagProperties = rStringKeyMaps[i];
125 // Get all actions references associated with the current smart tag type
126 const css::uno::Sequence< css::uno::Reference< css::smarttags::XSmartTagAction > >& rActionComponents = rActionComponentsSequence[i];
127 const css::uno::Sequence< sal_Int32 >& rActionIndices = rActionIndicesSequence[i];
129 if ( 0 == rActionComponents.getLength() || 0 == rActionIndices.getLength() )
130 continue;
132 // Ask first entry for the smart tag type caption
133 css::uno::Reference< css::smarttags::XSmartTagAction > xFirstAction = rActionComponents[0];
135 if ( !xFirstAction.is() )
136 continue;
138 const sal_Int32 nSmartTagIndex = rActionIndices[0];
139 const OUString aSmartTagType = xFirstAction->getSmartTagName( nSmartTagIndex );
140 const OUString aSmartTagCaption = xFirstAction->getSmartTagCaption( nSmartTagIndex, rLocale );
142 // No sub-menus if there's only one smart tag type listed
143 PopupMenu* pSubMenu = pVCLMenu;
144 if ( 1 < rActionComponentsSequence.getLength() )
146 pVCLMenu->InsertItem( nMenuId, aSmartTagCaption );
147 VclPtrInstance<PopupMenu> pMenu;
148 pSubMenu = pMenu;
149 pVCLMenu->SetPopupMenu( nMenuId++, pSubMenu );
151 pSubMenu->SetSelectHdl( LINK( this, SmartTagMenuController, MenuSelect ) );
153 // Sub-menu starts with smart tag caption and separator
154 const OUString aSmartTagCaption2 = aSmartTagCaption + ": " + aRangeText;
155 pSubMenu->InsertItem( nMenuId++, aSmartTagCaption2, MenuItemBits::NOSELECT );
156 pSubMenu->InsertSeparator();
158 // Add subitem for every action reference for the current smart tag type
159 for ( const auto& xAction : rActionComponents )
161 for ( sal_Int32 j = 0; j < xAction->getActionCount( aSmartTagType, xController, xSmartTagProperties ); ++j )
163 const sal_uInt32 nActionID = xAction->getActionID( aSmartTagType, j, xController );
164 OUString aActionCaption = xAction->getActionCaptionFromID( nActionID,
165 aApplicationName,
166 rLocale,
167 xSmartTagProperties,
168 aRangeText,
169 OUString(),
170 xController,
171 xTextRange );
173 pSubMenu->InsertItem( nSubMenuId++, aActionCaption );
174 InvokeAction aEntry( xAction, xSmartTagProperties, nActionID );
175 m_aInvokeActions.push_back( aEntry );
180 if ( 0 < pVCLMenu->GetItemCount() )
182 const OUString aCommand = ".uno:AutoCorrectDlg?OpenSmartTag:bool=true";
183 pVCLMenu->InsertSeparator();
184 pVCLMenu->InsertItem( nMenuId, vcl::CommandInfoProvider::GetPopupLabelForCommand( aCommand, m_aModuleName ) );
185 pVCLMenu->SetItemCommand( nMenuId, aCommand );
189 IMPL_LINK( SmartTagMenuController, MenuSelect, Menu*, pMenu, bool )
191 if ( !m_pSmartTagItem )
192 return false;
194 sal_uInt16 nMyId = pMenu->GetCurItemId();
195 if ( nMyId < MN_ST_INSERT_START )
196 return false;
198 nMyId -= MN_ST_INSERT_START;
200 // Compute SmartTag lib index and action index
201 css::uno::Reference< css::smarttags::XSmartTagAction > xSmartTagAction = m_aInvokeActions[nMyId].m_xAction;
203 // Execute action
204 if ( xSmartTagAction.is() )
206 xSmartTagAction->invokeAction( m_aInvokeActions[nMyId].m_nActionID,
207 m_pSmartTagItem->GetApplicationName(),
208 m_pSmartTagItem->GetController(),
209 m_pSmartTagItem->GetTextRange(),
210 m_aInvokeActions[nMyId].m_xSmartTagProperties,
211 m_pSmartTagItem->GetRangeText(),
212 OUString(),
213 m_pSmartTagItem->GetLocale() );
215 return false;
218 OUString SmartTagMenuController::getImplementationName()
220 return OUString( "com.sun.star.comp.svx.SmartTagMenuController" );
223 css::uno::Sequence< OUString > SmartTagMenuController::getSupportedServiceNames()
225 return { "com.sun.star.frame.PopupMenuController" };
228 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface * SAL_CALL
229 com_sun_star_comp_svx_SmartTagMenuController_get_implementation(
230 css::uno::XComponentContext* xContext,
231 css::uno::Sequence< css::uno::Any > const & )
233 return cppu::acquire( new SmartTagMenuController( xContext ) );
236 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */