bump product version to 5.0.4.1
[LibreOffice.git] / svx / source / mnuctrls / SmartTagCtl.cxx
blob71e917c2999323063ff3edba1d86303ccb2af4e9
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 .
21 #include <svx/SmartTagCtl.hxx>
22 #include <com/sun/star/smarttags/XSmartTagAction.hpp>
23 #include <com/sun/star/container/XStringKeyMap.hpp>
24 #include <svtools/stdmenu.hxx>
25 #include <svl/eitem.hxx>
26 #include <sfx2/dispatch.hxx>
27 #include <svx/svxids.hrc>
29 #include <svx/SmartTagItem.hxx>
31 using namespace ::com::sun::star;
32 using namespace ::com::sun::star::uno;
34 SFX_IMPL_MENU_CONTROL(SvxSmartTagsControl, SvxSmartTagItem);
38 SvxSmartTagsControl::SvxSmartTagsControl
40 sal_uInt16 _nId,
41 Menu& rMenu,
42 SfxBindings& /*rBindings*/
43 ) :
44 mpMenu ( new PopupMenu ),
45 mrParent ( rMenu ),
46 mpSmartTagItem( 0 )
48 rMenu.SetPopupMenu( _nId, mpMenu );
53 const sal_uInt16 MN_ST_INSERT_START = 500;
55 void SvxSmartTagsControl::FillMenu()
57 if ( !mpSmartTagItem )
58 return;
60 sal_uInt16 nMenuPos = 0;
61 sal_uInt16 nSubMenuPos = 0;
62 sal_uInt16 nMenuId = 1;
63 sal_uInt16 nSubMenuId = MN_ST_INSERT_START;
65 const Sequence < Sequence< Reference< smarttags::XSmartTagAction > > >& rActionComponentsSequence = mpSmartTagItem->GetActionComponentsSequence();
66 const Sequence < Sequence< sal_Int32 > >& rActionIndicesSequence = mpSmartTagItem->GetActionIndicesSequence();
67 const Sequence< Reference< container::XStringKeyMap > >& rStringKeyMaps = mpSmartTagItem->GetStringKeyMaps();
68 const lang::Locale& rLocale = mpSmartTagItem->GetLocale();
69 const OUString aApplicationName = mpSmartTagItem->GetApplicationName();
70 const OUString aRangeText = mpSmartTagItem->GetRangeText();
71 const Reference<text::XTextRange>& xTextRange = mpSmartTagItem->GetTextRange();
72 const Reference<frame::XController>& xController = mpSmartTagItem->GetController();
74 for ( sal_uInt16 j = 0; j < rActionComponentsSequence.getLength(); ++j )
76 Reference< container::XStringKeyMap > xSmartTagProperties = rStringKeyMaps[j];
78 // Get all actions references associated with the current smart tag type:
79 const Sequence< Reference< smarttags::XSmartTagAction > >& rActionComponents = rActionComponentsSequence[j];
80 const Sequence< sal_Int32 >& rActionIndices = rActionIndicesSequence[j];
82 if ( 0 == rActionComponents.getLength() || 0 == rActionIndices.getLength() )
83 continue;
85 // Ask first entry for the smart tag type caption:
86 Reference< smarttags::XSmartTagAction > xAction = rActionComponents[0];
88 if ( !xAction.is() )
89 continue;
91 const sal_Int32 nSmartTagIndex = rActionIndices[0];
92 const OUString aSmartTagType = xAction->getSmartTagName( nSmartTagIndex );
93 const OUString aSmartTagCaption = xAction->getSmartTagCaption( nSmartTagIndex, rLocale);
95 // no sub-menus if there's only one smart tag type listed:
96 PopupMenu* pSbMenu = mpMenu;
97 if ( 1 < rActionComponentsSequence.getLength() )
99 mpMenu->InsertItem(nMenuId, aSmartTagCaption, MenuItemBits::NONE, OString(), nMenuPos++);
100 pSbMenu = new PopupMenu;
101 mpMenu->SetPopupMenu( nMenuId++, pSbMenu );
103 pSbMenu->SetSelectHdl( LINK( this, SvxSmartTagsControl, MenuSelect ) );
105 // sub-menu starts with smart tag caption and separator
106 const OUString aSmartTagCaption2 = aSmartTagCaption + ": " + aRangeText;
107 nSubMenuPos = 0;
108 pSbMenu->InsertItem(nMenuId++, aSmartTagCaption2, MenuItemBits::NOSELECT, OString(), nSubMenuPos++);
109 pSbMenu->InsertSeparator(OString(), nSubMenuPos++);
111 // Add subitem for every action reference for the current smart tag type:
112 for ( sal_uInt16 i = 0; i < rActionComponents.getLength(); ++i )
114 xAction = rActionComponents[i];
116 for ( sal_Int32 k = 0; k < xAction->getActionCount( aSmartTagType, xController, xSmartTagProperties ); ++k )
118 const sal_uInt32 nActionID = xAction->getActionID( aSmartTagType, k, xController );
119 OUString aActionCaption = xAction->getActionCaptionFromID( nActionID,
120 aApplicationName,
121 rLocale,
122 xSmartTagProperties,
123 aRangeText,
124 OUString(),
125 xController,
126 xTextRange );
128 pSbMenu->InsertItem( nSubMenuId++, aActionCaption, MenuItemBits::NONE, OString(), nSubMenuPos++ );
129 InvokeAction aEntry( xAction, xSmartTagProperties, nActionID );
130 maInvokeActions.push_back( aEntry );
138 void SvxSmartTagsControl::StateChanged( sal_uInt16, SfxItemState eState, const SfxPoolItem* pState )
141 mrParent.EnableItem( GetId(), SfxItemState::DISABLED != eState );
143 if ( SfxItemState::DEFAULT == eState )
145 const SvxSmartTagItem* pSmartTagItem = PTR_CAST( SvxSmartTagItem, pState );
146 if ( 0 != pSmartTagItem )
148 delete mpSmartTagItem;
149 mpSmartTagItem = new SvxSmartTagItem( *pSmartTagItem );
150 FillMenu();
157 IMPL_LINK( SvxSmartTagsControl, MenuSelect, PopupMenu *, pMen )
159 if ( !mpSmartTagItem )
160 return 0;
162 sal_uInt16 nMyId = pMen->GetCurItemId();
164 if ( nMyId < MN_ST_INSERT_START) return 0;
165 nMyId -= MN_ST_INSERT_START;
167 // compute smarttag lib index and action index
168 Reference< smarttags::XSmartTagAction > xSmartTagAction = maInvokeActions[ nMyId ].mxAction;
170 // execute action
171 if ( xSmartTagAction.is() )
173 xSmartTagAction->invokeAction( maInvokeActions[ nMyId ].mnActionID,
174 mpSmartTagItem->GetApplicationName(),
175 mpSmartTagItem->GetController(),
176 mpSmartTagItem->GetTextRange(),
177 maInvokeActions[ nMyId ].mxSmartTagProperties,
178 mpSmartTagItem->GetRangeText(),
179 OUString(),
180 mpSmartTagItem->GetLocale() );
183 // ohne dispatcher!!!
184 // GetBindings().Execute( GetId(), SfxCallMode::RECORD,meine beiden items, 0L );*/
185 //SfxBoolItem aBool(SID_OPEN_SMARTTAGOPTIONS, sal_True);
186 //GetBindings().GetDispatcher()->Execute( SID_AUTO_CORRECT_DLG, SfxCallMode::ASYNCHRON, &aBool, 0L );
188 return 0;
191 SvxSmartTagsControl::~SvxSmartTagsControl()
193 delete mpSmartTagItem;
194 delete mpMenu;
199 PopupMenu* SvxSmartTagsControl::GetPopup() const
201 return mpMenu;
205 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */