xref: /aoo42x/main/sw/source/ui/smartmenu/stmenu.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // SMARTTAGS
29 
30 // MARKER(update_precomp.py): autogen include statement, do not remove
31 #include "precompiled_sw.hxx"
32 #include <stmenu.hxx>
33 #include <com/sun/star/uno/Sequence.hxx>
34 #include <svl/eitem.hxx>
35 #include <sfx2/dispatch.hxx>
36 
37 #include <SwSmartTagMgr.hxx>
38 
39 #ifndef _STMENU_HRC
40 #include <stmenu.hrc>
41 #endif
42 #ifndef _VIEW_HXX
43 #include <view.hxx>
44 #endif
45 #include <breakit.hxx>
46 
47 #define C2U(cChar) rtl::OUString::createFromAscii(cChar)
48 
49 using namespace ::com::sun::star;
50 using namespace ::com::sun::star::uno;
51 
52 SwSmartTagPopup::SwSmartTagPopup( SwView* pSwView,
53                                   Sequence< rtl::OUString >& rSmartTagTypes,
54                                   Sequence< Reference< container::XStringKeyMap > >& rStringKeyMaps,
55                                   Reference< text::XTextRange > xTextRange ) :
56     PopupMenu( SW_RES(MN_SMARTTAG_POPUP) ),
57     mpSwView ( pSwView ),
58     mxTextRange( xTextRange )
59 {
60     //CreateAutoMnemonics();
61 
62     Reference <frame::XController> xController = mpSwView->GetController();
63     const lang::Locale aLocale( SW_BREAKITER()->GetLocale( (LanguageType)GetAppLanguage() ) );
64 
65     sal_uInt16 nMenuPos = 0;
66     sal_uInt16 nSubMenuPos = 0;
67     sal_uInt16 nMenuId = 1;
68     sal_uInt16 nSubMenuId = MN_ST_INSERT_START;
69 
70     const rtl::OUString aRangeText = mxTextRange->getString();
71 
72     SmartTagMgr& rSmartTagMgr = SwSmartTagMgr::Get();
73     const rtl::OUString aApplicationName( rSmartTagMgr.GetApplicationName() );
74 
75     Sequence < Sequence< Reference< smarttags::XSmartTagAction > > > aActionComponentsSequence;
76     Sequence < Sequence< sal_Int32 > > aActionIndicesSequence;
77 
78     rSmartTagMgr.GetActionSequences( rSmartTagTypes,
79                                      aActionComponentsSequence,
80                                      aActionIndicesSequence );
81 
82     InsertSeparator(0);
83 
84     for ( sal_uInt16 j = 0; j < aActionComponentsSequence.getLength(); ++j )
85     {
86         Reference< container::XStringKeyMap > xSmartTagProperties = rStringKeyMaps[j];
87 
88         // Get all actions references associated with the current smart tag type:
89         const Sequence< Reference< smarttags::XSmartTagAction > >& rActionComponents = aActionComponentsSequence[j];
90         const Sequence< sal_Int32 >& rActionIndices = aActionIndicesSequence[j];
91 
92         if ( 0 == rActionComponents.getLength() || 0 == rActionIndices.getLength() )
93             continue;
94 
95         // Ask first entry for the smart tag type caption:
96         Reference< smarttags::XSmartTagAction > xAction = rActionComponents[0];
97 
98         if ( !xAction.is() )
99             continue;
100 
101         const sal_Int32 nSmartTagIndex = rActionIndices[0];
102         const rtl::OUString aSmartTagType = xAction->getSmartTagName( nSmartTagIndex );
103         const rtl::OUString aSmartTagCaption = xAction->getSmartTagCaption( nSmartTagIndex, aLocale );
104 
105         // no sub-menues if there's only one smart tag type listed:
106         PopupMenu* pSbMenu = this;
107         if ( 1 < aActionComponentsSequence.getLength() )
108         {
109             InsertItem( nMenuId, aSmartTagCaption, 0, nMenuPos++);
110             pSbMenu = new PopupMenu;
111             SetPopupMenu( nMenuId++, pSbMenu );
112         }
113 
114         // sub-menu starts with smart tag caption and separator
115         const rtl::OUString aSmartTagCaption2 = aSmartTagCaption + C2U(": ") + aRangeText;
116         nSubMenuPos = 0;
117         pSbMenu->InsertItem( nMenuId++, aSmartTagCaption2, MIB_NOSELECT, nSubMenuPos++ );
118         pSbMenu->InsertSeparator( nSubMenuPos++ );
119 
120         // Add subitem for every action reference for the current smart tag type:
121         for ( sal_uInt16 i = 0; i < rActionComponents.getLength(); ++i )
122         {
123             xAction = rActionComponents[i];
124 
125             for ( sal_Int32 k = 0; k < xAction->getActionCount( aSmartTagType, xController ); ++k )
126             {
127                 const sal_uInt32 nActionID = xAction->getActionID( aSmartTagType, k, xController  );
128                 rtl::OUString aActionCaption = xAction->getActionCaptionFromID( nActionID,
129                                                                                 aApplicationName,
130                                                                                 aLocale,
131                                                                                 xSmartTagProperties,
132                                                                                 aRangeText,
133                                                                                 rtl::OUString(),
134                                                                                 xController,
135                                                                                 mxTextRange );
136 
137                 pSbMenu->InsertItem( nSubMenuId++, aActionCaption, 0, nSubMenuPos++ );
138                 InvokeAction aEntry( xAction, xSmartTagProperties, nActionID );
139                 maInvokeActions.push_back( aEntry );
140             }
141         }
142     }
143 }
144 
145 /** Function: Execute
146 
147    executes actions by calling the invoke function of the appropriate
148    smarttag library.
149 
150 */
151 sal_uInt16 SwSmartTagPopup::Execute( const Rectangle& rWordPos, Window* pWin )
152 {
153     sal_uInt16 nId = PopupMenu::Execute(pWin, pWin->LogicToPixel(rWordPos));
154 
155     if ( nId == MN_SMARTTAG_OPTIONS )
156     {
157         SfxBoolItem aBool(SID_OPEN_SMARTTAGOPTIONS, sal_True);
158         mpSwView->GetViewFrame()->GetDispatcher()->Execute( SID_AUTO_CORRECT_DLG, SFX_CALLMODE_ASYNCHRON, &aBool, 0L );
159     }
160 
161     if ( nId < MN_ST_INSERT_START) return nId;
162     nId -= MN_ST_INSERT_START;
163 
164     // compute smarttag lib index and action index
165     if ( nId < maInvokeActions.size() )
166     {
167         Reference< smarttags::XSmartTagAction > xSmartTagAction = maInvokeActions[ nId ].mxAction;
168 
169         // execute action
170         if ( xSmartTagAction.is() )
171         {
172             SmartTagMgr& rSmartTagMgr = SwSmartTagMgr::Get();
173 
174             xSmartTagAction->invokeAction( maInvokeActions[ nId ].mnActionID,
175                                            rSmartTagMgr.GetApplicationName(),
176                                            mpSwView->GetController(),
177                                            mxTextRange,
178                                            maInvokeActions[ nId ].mxSmartTagProperties,
179                                            mxTextRange->getString(),
180                                            rtl::OUString(),
181                                            SW_BREAKITER()->GetLocale( (LanguageType)GetAppLanguage() )  );
182         }
183     }
184 
185     return nId;
186 }
187