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 nSubMenuPos = 0;
47 0 : sal_uInt16 nMenuId = 1;
48 0 : sal_uInt16 nSubMenuId = MN_ST_INSERT_START;
49 :
50 0 : const OUString aRangeText = mxTextRange->getString();
51 :
52 0 : SmartTagMgr& rSmartTagMgr = SwSmartTagMgr::Get();
53 0 : const OUString aApplicationName( rSmartTagMgr.GetApplicationName() );
54 :
55 0 : Sequence < Sequence< Reference< smarttags::XSmartTagAction > > > aActionComponentsSequence;
56 0 : Sequence < Sequence< sal_Int32 > > aActionIndicesSequence;
57 :
58 : rSmartTagMgr.GetActionSequences( rSmartTagTypes,
59 : aActionComponentsSequence,
60 0 : aActionIndicesSequence );
61 :
62 0 : InsertSeparator(OString(), 0);
63 :
64 0 : for ( sal_uInt16 j = 0; j < aActionComponentsSequence.getLength(); ++j )
65 : {
66 0 : Reference< container::XStringKeyMap > xSmartTagProperties = rStringKeyMaps[j];
67 :
68 : // Get all actions references associated with the current smart tag type:
69 0 : const Sequence< Reference< smarttags::XSmartTagAction > >& rActionComponents = aActionComponentsSequence[j];
70 0 : const Sequence< sal_Int32 >& rActionIndices = aActionIndicesSequence[j];
71 :
72 0 : if ( 0 == rActionComponents.getLength() || 0 == rActionIndices.getLength() )
73 0 : continue;
74 :
75 : // Ask first entry for the smart tag type caption:
76 0 : Reference< smarttags::XSmartTagAction > xAction = rActionComponents[0];
77 :
78 0 : if ( !xAction.is() )
79 0 : continue;
80 :
81 0 : const sal_Int32 nSmartTagIndex = rActionIndices[0];
82 0 : const OUString aSmartTagType = xAction->getSmartTagName( nSmartTagIndex );
83 0 : const OUString aSmartTagCaption = xAction->getSmartTagCaption( nSmartTagIndex, aLocale );
84 :
85 : // no sub-menus if there's only one smart tag type listed:
86 0 : PopupMenu* pSbMenu = this;
87 0 : if ( 1 < aActionComponentsSequence.getLength() )
88 : {
89 0 : InsertItem(nMenuId, aSmartTagCaption, 0, OString(), nMenuPos++);
90 0 : pSbMenu = new PopupMenu;
91 0 : SetPopupMenu( nMenuId++, pSbMenu );
92 : }
93 :
94 : // sub-menu starts with smart tag caption and separator
95 0 : const OUString aSmartTagCaption2 = aSmartTagCaption + ": " + aRangeText;
96 0 : nSubMenuPos = 0;
97 0 : pSbMenu->InsertItem(nMenuId++, aSmartTagCaption2, MIB_NOSELECT, OString(), nSubMenuPos++);
98 0 : pSbMenu->InsertSeparator(OString(), nSubMenuPos++);
99 :
100 : // Add subitem for every action reference for the current smart tag type:
101 0 : for ( sal_uInt16 i = 0; i < rActionComponents.getLength(); ++i )
102 : {
103 0 : xAction = rActionComponents[i];
104 :
105 0 : for ( sal_Int32 k = 0; k < xAction->getActionCount( aSmartTagType, xController, xSmartTagProperties ); ++k )
106 : {
107 0 : const sal_uInt32 nActionID = xAction->getActionID( aSmartTagType, k, xController );
108 0 : OUString aActionCaption = xAction->getActionCaptionFromID( nActionID,
109 : aApplicationName,
110 : aLocale,
111 : xSmartTagProperties,
112 : aRangeText,
113 : OUString(),
114 : xController,
115 0 : mxTextRange );
116 :
117 0 : pSbMenu->InsertItem(nSubMenuId++, aActionCaption, 0, OString(), nSubMenuPos++);
118 0 : InvokeAction aEntry( xAction, xSmartTagProperties, nActionID );
119 0 : maInvokeActions.push_back( aEntry );
120 0 : }
121 : }
122 0 : }
123 0 : }
124 :
125 : /** Function: Execute
126 :
127 : executes actions by calling the invoke function of the appropriate
128 : smarttag library.
129 :
130 : */
131 0 : sal_uInt16 SwSmartTagPopup::Execute( const Rectangle& rWordPos, Window* pWin )
132 : {
133 0 : sal_uInt16 nId = PopupMenu::Execute(pWin, pWin->LogicToPixel(rWordPos));
134 :
135 0 : if ( nId == MN_SMARTTAG_OPTIONS )
136 : {
137 0 : SfxBoolItem aBool(SID_OPEN_SMARTTAGOPTIONS, true);
138 0 : mpSwView->GetViewFrame()->GetDispatcher()->Execute( SID_AUTO_CORRECT_DLG, SFX_CALLMODE_ASYNCHRON, &aBool, 0L );
139 : }
140 :
141 0 : if ( nId < MN_ST_INSERT_START) return nId;
142 0 : nId -= MN_ST_INSERT_START;
143 :
144 : // compute smarttag lib index and action index
145 0 : if ( nId < maInvokeActions.size() )
146 : {
147 0 : Reference< smarttags::XSmartTagAction > xSmartTagAction = maInvokeActions[ nId ].mxAction;
148 :
149 : // execute action
150 0 : if ( xSmartTagAction.is() )
151 : {
152 0 : SmartTagMgr& rSmartTagMgr = SwSmartTagMgr::Get();
153 :
154 0 : xSmartTagAction->invokeAction( maInvokeActions[ nId ].mnActionID,
155 : rSmartTagMgr.GetApplicationName(),
156 : mpSwView->GetController(),
157 : mxTextRange,
158 0 : maInvokeActions[ nId ].mxSmartTagProperties,
159 0 : mxTextRange->getString(),
160 : OUString(),
161 0 : SW_BREAKITER()->GetLocale( GetAppLanguageTag() ) );
162 0 : }
163 : }
164 :
165 0 : return nId;
166 : }
167 :
168 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|