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 // SMARTTAGS 25 26 // MARKER(update_precomp.py): autogen include statement, do not remove 27 #include "precompiled_sw.hxx" 28 #include <stmenu.hxx> 29 #include <com/sun/star/uno/Sequence.hxx> 30 #include <svl/eitem.hxx> 31 #include <sfx2/dispatch.hxx> 32 33 #include <SwSmartTagMgr.hxx> 34 35 #ifndef _STMENU_HRC 36 #include <stmenu.hrc> 37 #endif 38 #ifndef _VIEW_HXX 39 #include <view.hxx> 40 #endif 41 #include <breakit.hxx> 42 43 #define C2U(cChar) rtl::OUString::createFromAscii(cChar) 44 45 using namespace ::com::sun::star; 46 using namespace ::com::sun::star::uno; 47 48 SwSmartTagPopup::SwSmartTagPopup( SwView* pSwView, 49 Sequence< rtl::OUString >& rSmartTagTypes, 50 Sequence< Reference< container::XStringKeyMap > >& rStringKeyMaps, 51 Reference< text::XTextRange > xTextRange ) : 52 PopupMenu( SW_RES(MN_SMARTTAG_POPUP) ), 53 mpSwView ( pSwView ), 54 mxTextRange( xTextRange ) 55 { 56 //CreateAutoMnemonics(); 57 58 Reference <frame::XController> xController = mpSwView->GetController(); 59 const lang::Locale aLocale( SW_BREAKITER()->GetLocale( (LanguageType)GetAppLanguage() ) ); 60 61 sal_uInt16 nMenuPos = 0; 62 sal_uInt16 nSubMenuPos = 0; 63 sal_uInt16 nMenuId = 1; 64 sal_uInt16 nSubMenuId = MN_ST_INSERT_START; 65 66 const rtl::OUString aRangeText = mxTextRange->getString(); 67 68 SmartTagMgr& rSmartTagMgr = SwSmartTagMgr::Get(); 69 const rtl::OUString aApplicationName( rSmartTagMgr.GetApplicationName() ); 70 71 Sequence < Sequence< Reference< smarttags::XSmartTagAction > > > aActionComponentsSequence; 72 Sequence < Sequence< sal_Int32 > > aActionIndicesSequence; 73 74 rSmartTagMgr.GetActionSequences( rSmartTagTypes, 75 aActionComponentsSequence, 76 aActionIndicesSequence ); 77 78 InsertSeparator(0); 79 80 for ( sal_uInt16 j = 0; j < aActionComponentsSequence.getLength(); ++j ) 81 { 82 Reference< container::XStringKeyMap > xSmartTagProperties = rStringKeyMaps[j]; 83 84 // Get all actions references associated with the current smart tag type: 85 const Sequence< Reference< smarttags::XSmartTagAction > >& rActionComponents = aActionComponentsSequence[j]; 86 const Sequence< sal_Int32 >& rActionIndices = aActionIndicesSequence[j]; 87 88 if ( 0 == rActionComponents.getLength() || 0 == rActionIndices.getLength() ) 89 continue; 90 91 // Ask first entry for the smart tag type caption: 92 Reference< smarttags::XSmartTagAction > xAction = rActionComponents[0]; 93 94 if ( !xAction.is() ) 95 continue; 96 97 const sal_Int32 nSmartTagIndex = rActionIndices[0]; 98 const rtl::OUString aSmartTagType = xAction->getSmartTagName( nSmartTagIndex ); 99 const rtl::OUString aSmartTagCaption = xAction->getSmartTagCaption( nSmartTagIndex, aLocale ); 100 101 // no sub-menues if there's only one smart tag type listed: 102 PopupMenu* pSbMenu = this; 103 if ( 1 < aActionComponentsSequence.getLength() ) 104 { 105 InsertItem( nMenuId, aSmartTagCaption, 0, nMenuPos++); 106 pSbMenu = new PopupMenu; 107 SetPopupMenu( nMenuId++, pSbMenu ); 108 } 109 110 // sub-menu starts with smart tag caption and separator 111 const rtl::OUString aSmartTagCaption2 = aSmartTagCaption + C2U(": ") + aRangeText; 112 nSubMenuPos = 0; 113 pSbMenu->InsertItem( nMenuId++, aSmartTagCaption2, MIB_NOSELECT, nSubMenuPos++ ); 114 pSbMenu->InsertSeparator( nSubMenuPos++ ); 115 116 // Add subitem for every action reference for the current smart tag type: 117 for ( sal_uInt16 i = 0; i < rActionComponents.getLength(); ++i ) 118 { 119 xAction = rActionComponents[i]; 120 121 for ( sal_Int32 k = 0; k < xAction->getActionCount( aSmartTagType, xController ); ++k ) 122 { 123 const sal_uInt32 nActionID = xAction->getActionID( aSmartTagType, k, xController ); 124 rtl::OUString aActionCaption = xAction->getActionCaptionFromID( nActionID, 125 aApplicationName, 126 aLocale, 127 xSmartTagProperties, 128 aRangeText, 129 rtl::OUString(), 130 xController, 131 mxTextRange ); 132 133 pSbMenu->InsertItem( nSubMenuId++, aActionCaption, 0, nSubMenuPos++ ); 134 InvokeAction aEntry( xAction, xSmartTagProperties, nActionID ); 135 maInvokeActions.push_back( aEntry ); 136 } 137 } 138 } 139 } 140 141 /** Function: Execute 142 143 executes actions by calling the invoke function of the appropriate 144 smarttag library. 145 146 */ 147 sal_uInt16 SwSmartTagPopup::Execute( const Rectangle& rWordPos, Window* pWin ) 148 { 149 sal_uInt16 nId = PopupMenu::Execute(pWin, pWin->LogicToPixel(rWordPos)); 150 151 if ( nId == MN_SMARTTAG_OPTIONS ) 152 { 153 SfxBoolItem aBool(SID_OPEN_SMARTTAGOPTIONS, sal_True); 154 mpSwView->GetViewFrame()->GetDispatcher()->Execute( SID_AUTO_CORRECT_DLG, SFX_CALLMODE_ASYNCHRON, &aBool, 0L ); 155 } 156 157 if ( nId < MN_ST_INSERT_START) return nId; 158 nId -= MN_ST_INSERT_START; 159 160 // compute smarttag lib index and action index 161 if ( nId < maInvokeActions.size() ) 162 { 163 Reference< smarttags::XSmartTagAction > xSmartTagAction = maInvokeActions[ nId ].mxAction; 164 165 // execute action 166 if ( xSmartTagAction.is() ) 167 { 168 SmartTagMgr& rSmartTagMgr = SwSmartTagMgr::Get(); 169 170 xSmartTagAction->invokeAction( maInvokeActions[ nId ].mnActionID, 171 rSmartTagMgr.GetApplicationName(), 172 mpSwView->GetController(), 173 mxTextRange, 174 maInvokeActions[ nId ].mxSmartTagProperties, 175 mxTextRange->getString(), 176 rtl::OUString(), 177 SW_BREAKITER()->GetLocale( (LanguageType)GetAppLanguage() ) ); 178 } 179 } 180 181 return nId; 182 } 183