Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <stmenu.hxx>
21 : #include <com/sun/star/uno/Sequence.hxx>
22 : #include <svl/eitem.hxx>
23 : #include <sfx2/dispatch.hxx>
24 :
25 : #include <SwSmartTagMgr.hxx>
26 :
27 : #include <stmenu.hrc>
28 : #include <view.hxx>
29 : #include <breakit.hxx>
30 :
31 : using namespace ::com::sun::star;
32 : using namespace ::com::sun::star::uno;
33 :
34 0 : SwSmartTagPopup::SwSmartTagPopup( SwView* pSwView,
35 : Sequence< OUString >& rSmartTagTypes,
36 : Sequence< Reference< container::XStringKeyMap > >& rStringKeyMaps,
37 : Reference< text::XTextRange > xTextRange ) :
38 : PopupMenu( SW_RES(MN_SMARTTAG_POPUP) ),
39 : mpSwView ( pSwView ),
40 0 : mxTextRange( xTextRange )
41 : {
42 0 : Reference <frame::XController> xController = mpSwView->GetController();
43 0 : const lang::Locale aLocale( SW_BREAKITER()->GetLocale( GetAppLanguageTag() ) );
44 :
45 0 : sal_uInt16 nMenuPos = 0;
46 0 : sal_uInt16 nMenuId = 1;
47 0 : sal_uInt16 nSubMenuId = MN_ST_INSERT_START;
48 :
49 0 : const OUString aRangeText = mxTextRange->getString();
50 :
51 0 : SmartTagMgr& rSmartTagMgr = SwSmartTagMgr::Get();
52 0 : const OUString aApplicationName( rSmartTagMgr.GetApplicationName() );
53 :
54 0 : Sequence < Sequence< Reference< smarttags::XSmartTagAction > > > aActionComponentsSequence;
55 0 : Sequence < Sequence< sal_Int32 > > aActionIndicesSequence;
56 :
57 : rSmartTagMgr.GetActionSequences( rSmartTagTypes,
58 : aActionComponentsSequence,
59 0 : aActionIndicesSequence );
60 :
61 0 : InsertSeparator(OString(), 0);
62 :
63 0 : for ( sal_Int32 j = 0; j < aActionComponentsSequence.getLength(); ++j )
64 : {
65 0 : Reference< container::XStringKeyMap > xSmartTagProperties = rStringKeyMaps[j];
66 :
67 : // Get all actions references associated with the current smart tag type:
68 0 : const Sequence< Reference< smarttags::XSmartTagAction > >& rActionComponents = aActionComponentsSequence[j];
69 0 : const Sequence< sal_Int32 >& rActionIndices = aActionIndicesSequence[j];
70 :
71 0 : if ( 0 == rActionComponents.getLength() || 0 == rActionIndices.getLength() )
72 0 : continue;
73 :
74 : // Ask first entry for the smart tag type caption:
75 0 : Reference< smarttags::XSmartTagAction > xAction = rActionComponents[0];
76 :
77 0 : if ( !xAction.is() )
78 0 : continue;
79 :
80 0 : const sal_Int32 nSmartTagIndex = rActionIndices[0];
81 0 : const OUString aSmartTagType = xAction->getSmartTagName( nSmartTagIndex );
82 0 : const OUString aSmartTagCaption = xAction->getSmartTagCaption( nSmartTagIndex, aLocale );
83 :
84 : // no sub-menus if there's only one smart tag type listed:
85 0 : PopupMenu* pSbMenu = this;
86 0 : if ( 1 < aActionComponentsSequence.getLength() )
87 : {
88 0 : InsertItem(nMenuId, aSmartTagCaption, MenuItemBits::NONE, OString(), nMenuPos++);
89 0 : pSbMenu = new PopupMenu;
90 0 : SetPopupMenu( nMenuId++, pSbMenu );
91 : }
92 :
93 : // sub-menu starts with smart tag caption and separator
94 0 : const OUString aSmartTagCaption2 = aSmartTagCaption + ": " + aRangeText;
95 0 : sal_uInt16 nSubMenuPos = 0;
96 0 : pSbMenu->InsertItem(nMenuId++, aSmartTagCaption2, MenuItemBits::NOSELECT, OString(), nSubMenuPos++);
97 0 : pSbMenu->InsertSeparator(OString(), nSubMenuPos++);
98 :
99 : // Add subitem for every action reference for the current smart tag type:
100 0 : for ( sal_Int32 i = 0; i < rActionComponents.getLength(); ++i )
101 : {
102 0 : xAction = rActionComponents[i];
103 :
104 0 : for ( sal_Int32 k = 0; k < xAction->getActionCount( aSmartTagType, xController, xSmartTagProperties ); ++k )
105 : {
106 0 : const sal_uInt32 nActionID = xAction->getActionID( aSmartTagType, k, xController );
107 0 : OUString aActionCaption = xAction->getActionCaptionFromID( nActionID,
108 : aApplicationName,
109 : aLocale,
110 : xSmartTagProperties,
111 : aRangeText,
112 : OUString(),
113 : xController,
114 0 : mxTextRange );
115 :
116 0 : pSbMenu->InsertItem(nSubMenuId++, aActionCaption, MenuItemBits::NONE, OString(), nSubMenuPos++);
117 0 : InvokeAction aEntry( xAction, xSmartTagProperties, nActionID );
118 0 : maInvokeActions.push_back( aEntry );
119 0 : }
120 : }
121 0 : }
122 0 : }
123 :
124 : /** Function: Execute
125 :
126 : executes actions by calling the invoke function of the appropriate
127 : smarttag library.
128 :
129 : */
130 0 : sal_uInt16 SwSmartTagPopup::Execute( const Rectangle& rWordPos, vcl::Window* pWin )
131 : {
132 0 : sal_uInt16 nId = PopupMenu::Execute(pWin, pWin->LogicToPixel(rWordPos));
133 :
134 0 : if ( nId == MN_SMARTTAG_OPTIONS )
135 : {
136 0 : SfxBoolItem aBool(SID_OPEN_SMARTTAGOPTIONS, true);
137 0 : mpSwView->GetViewFrame()->GetDispatcher()->Execute( SID_AUTO_CORRECT_DLG, SfxCallMode::ASYNCHRON, &aBool, 0L );
138 : }
139 :
140 0 : if ( nId < MN_ST_INSERT_START) return nId;
141 0 : nId -= MN_ST_INSERT_START;
142 :
143 : // compute smarttag lib index and action index
144 0 : if ( nId < maInvokeActions.size() )
145 : {
146 0 : Reference< smarttags::XSmartTagAction > xSmartTagAction = maInvokeActions[ nId ].mxAction;
147 :
148 : // execute action
149 0 : if ( xSmartTagAction.is() )
150 : {
151 0 : SmartTagMgr& rSmartTagMgr = SwSmartTagMgr::Get();
152 :
153 0 : xSmartTagAction->invokeAction( maInvokeActions[ nId ].mnActionID,
154 : rSmartTagMgr.GetApplicationName(),
155 : mpSwView->GetController(),
156 : mxTextRange,
157 0 : maInvokeActions[ nId ].mxSmartTagProperties,
158 0 : mxTextRange->getString(),
159 : OUString(),
160 0 : SW_BREAKITER()->GetLocale( GetAppLanguageTag() ) );
161 0 : }
162 : }
163 :
164 0 : return nId;
165 270 : }
166 :
167 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|