1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
23 #include <com/sun/star/uno/Sequence.hxx>
24 #include <svl/eitem.hxx>
25 #include <sfx2/dispatch.hxx>
27 #include <SwSmartTagMgr.hxx>
31 #include <breakit.hxx>
33 using namespace ::com::sun::star
;
34 using namespace ::com::sun::star::uno
;
36 SwSmartTagPopup::SwSmartTagPopup( SwView
* pSwView
,
37 Sequence
< OUString
>& rSmartTagTypes
,
38 Sequence
< Reference
< container::XStringKeyMap
> >& rStringKeyMaps
,
39 Reference
< text::XTextRange
> xTextRange
) :
40 PopupMenu( SW_RES(MN_SMARTTAG_POPUP
) ),
42 mxTextRange( xTextRange
)
44 Reference
<frame::XController
> xController
= mpSwView
->GetController();
45 const lang::Locale
aLocale( SW_BREAKITER()->GetLocale( GetAppLanguageTag() ) );
47 sal_uInt16 nMenuPos
= 0;
48 sal_uInt16 nSubMenuPos
= 0;
49 sal_uInt16 nMenuId
= 1;
50 sal_uInt16 nSubMenuId
= MN_ST_INSERT_START
;
52 const OUString aRangeText
= mxTextRange
->getString();
54 SmartTagMgr
& rSmartTagMgr
= SwSmartTagMgr::Get();
55 const OUString
aApplicationName( rSmartTagMgr
.GetApplicationName() );
57 Sequence
< Sequence
< Reference
< smarttags::XSmartTagAction
> > > aActionComponentsSequence
;
58 Sequence
< Sequence
< sal_Int32
> > aActionIndicesSequence
;
60 rSmartTagMgr
.GetActionSequences( rSmartTagTypes
,
61 aActionComponentsSequence
,
62 aActionIndicesSequence
);
64 InsertSeparator(OString(), 0);
66 for ( sal_uInt16 j
= 0; j
< aActionComponentsSequence
.getLength(); ++j
)
68 Reference
< container::XStringKeyMap
> xSmartTagProperties
= rStringKeyMaps
[j
];
70 // Get all actions references associated with the current smart tag type:
71 const Sequence
< Reference
< smarttags::XSmartTagAction
> >& rActionComponents
= aActionComponentsSequence
[j
];
72 const Sequence
< sal_Int32
>& rActionIndices
= aActionIndicesSequence
[j
];
74 if ( 0 == rActionComponents
.getLength() || 0 == rActionIndices
.getLength() )
77 // Ask first entry for the smart tag type caption:
78 Reference
< smarttags::XSmartTagAction
> xAction
= rActionComponents
[0];
83 const sal_Int32 nSmartTagIndex
= rActionIndices
[0];
84 const OUString aSmartTagType
= xAction
->getSmartTagName( nSmartTagIndex
);
85 const OUString aSmartTagCaption
= xAction
->getSmartTagCaption( nSmartTagIndex
, aLocale
);
87 // no sub-menus if there's only one smart tag type listed:
88 PopupMenu
* pSbMenu
= this;
89 if ( 1 < aActionComponentsSequence
.getLength() )
91 InsertItem(nMenuId
, aSmartTagCaption
, 0, OString(), nMenuPos
++);
92 pSbMenu
= new PopupMenu
;
93 SetPopupMenu( nMenuId
++, pSbMenu
);
96 // sub-menu starts with smart tag caption and separator
97 const OUString aSmartTagCaption2
= aSmartTagCaption
+ ": " + aRangeText
;
99 pSbMenu
->InsertItem(nMenuId
++, aSmartTagCaption2
, MIB_NOSELECT
, OString(), nSubMenuPos
++);
100 pSbMenu
->InsertSeparator(OString(), nSubMenuPos
++);
102 // Add subitem for every action reference for the current smart tag type:
103 for ( sal_uInt16 i
= 0; i
< rActionComponents
.getLength(); ++i
)
105 xAction
= rActionComponents
[i
];
107 for ( sal_Int32 k
= 0; k
< xAction
->getActionCount( aSmartTagType
, xController
); ++k
)
109 const sal_uInt32 nActionID
= xAction
->getActionID( aSmartTagType
, k
, xController
);
110 OUString aActionCaption
= xAction
->getActionCaptionFromID( nActionID
,
119 pSbMenu
->InsertItem(nSubMenuId
++, aActionCaption
, 0, OString(), nSubMenuPos
++);
120 InvokeAction
aEntry( xAction
, xSmartTagProperties
, nActionID
);
121 maInvokeActions
.push_back( aEntry
);
127 /** Function: Execute
129 executes actions by calling the invoke function of the appropriate
133 sal_uInt16
SwSmartTagPopup::Execute( const Rectangle
& rWordPos
, Window
* pWin
)
135 sal_uInt16 nId
= PopupMenu::Execute(pWin
, pWin
->LogicToPixel(rWordPos
));
137 if ( nId
== MN_SMARTTAG_OPTIONS
)
139 SfxBoolItem
aBool(SID_OPEN_SMARTTAGOPTIONS
, sal_True
);
140 mpSwView
->GetViewFrame()->GetDispatcher()->Execute( SID_AUTO_CORRECT_DLG
, SFX_CALLMODE_ASYNCHRON
, &aBool
, 0L );
143 if ( nId
< MN_ST_INSERT_START
) return nId
;
144 nId
-= MN_ST_INSERT_START
;
146 // compute smarttag lib index and action index
147 if ( nId
< maInvokeActions
.size() )
149 Reference
< smarttags::XSmartTagAction
> xSmartTagAction
= maInvokeActions
[ nId
].mxAction
;
152 if ( xSmartTagAction
.is() )
154 SmartTagMgr
& rSmartTagMgr
= SwSmartTagMgr::Get();
156 xSmartTagAction
->invokeAction( maInvokeActions
[ nId
].mnActionID
,
157 rSmartTagMgr
.GetApplicationName(),
158 mpSwView
->GetController(),
160 maInvokeActions
[ nId
].mxSmartTagProperties
,
161 mxTextRange
->getString(),
163 SW_BREAKITER()->GetLocale( GetAppLanguageTag() ) );
170 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */