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 :
21 : #include <svx/SmartTagCtl.hxx>
22 : #include <com/sun/star/smarttags/XSmartTagAction.hpp>
23 : #include <com/sun/star/container/XStringKeyMap.hpp>
24 : #include <svtools/stdmenu.hxx>
25 : #include <svl/eitem.hxx>
26 : #include <sfx2/dispatch.hxx>
27 : #include <svx/svxids.hrc>
28 :
29 : #include <svx/SmartTagItem.hxx>
30 :
31 : using namespace ::com::sun::star;
32 : using namespace ::com::sun::star::uno;
33 :
34 : // STATIC DATA -----------------------------------------------------------
35 :
36 10 : SFX_IMPL_MENU_CONTROL(SvxSmartTagsControl, SvxSmartTagItem);
37 :
38 : //--------------------------------------------------------------------
39 :
40 0 : SvxSmartTagsControl::SvxSmartTagsControl
41 : (
42 : sal_uInt16 _nId,
43 : Menu& rMenu,
44 : SfxBindings& /*rBindings*/
45 : ) :
46 0 : mpMenu ( new PopupMenu ),
47 : mrParent ( rMenu ),
48 0 : mpSmartTagItem( 0 )
49 : {
50 0 : rMenu.SetPopupMenu( _nId, mpMenu );
51 0 : }
52 :
53 : //--------------------------------------------------------------------
54 :
55 : const sal_uInt16 MN_ST_INSERT_START = 500;
56 :
57 0 : void SvxSmartTagsControl::FillMenu()
58 : {
59 0 : if ( !mpSmartTagItem )
60 0 : return;
61 :
62 0 : sal_uInt16 nMenuPos = 0;
63 0 : sal_uInt16 nSubMenuPos = 0;
64 0 : sal_uInt16 nMenuId = 1;
65 0 : sal_uInt16 nSubMenuId = MN_ST_INSERT_START;
66 :
67 0 : const Sequence < Sequence< Reference< smarttags::XSmartTagAction > > >& rActionComponentsSequence = mpSmartTagItem->GetActionComponentsSequence();
68 0 : const Sequence < Sequence< sal_Int32 > >& rActionIndicesSequence = mpSmartTagItem->GetActionIndicesSequence();
69 0 : const Sequence< Reference< container::XStringKeyMap > >& rStringKeyMaps = mpSmartTagItem->GetStringKeyMaps();
70 0 : const lang::Locale& rLocale = mpSmartTagItem->GetLocale();
71 0 : const rtl::OUString aApplicationName = mpSmartTagItem->GetApplicationName();
72 0 : const rtl::OUString aRangeText = mpSmartTagItem->GetRangeText();
73 0 : const Reference<text::XTextRange>& xTextRange = mpSmartTagItem->GetTextRange();
74 0 : const Reference<frame::XController>& xController = mpSmartTagItem->GetController();
75 :
76 0 : for ( sal_uInt16 j = 0; j < rActionComponentsSequence.getLength(); ++j )
77 : {
78 0 : Reference< container::XStringKeyMap > xSmartTagProperties = rStringKeyMaps[j];
79 :
80 : // Get all actions references associated with the current smart tag type:
81 0 : const Sequence< Reference< smarttags::XSmartTagAction > >& rActionComponents = rActionComponentsSequence[j];
82 0 : const Sequence< sal_Int32 >& rActionIndices = rActionIndicesSequence[j];
83 :
84 0 : if ( 0 == rActionComponents.getLength() || 0 == rActionIndices.getLength() )
85 0 : continue;
86 :
87 : // Ask first entry for the smart tag type caption:
88 0 : Reference< smarttags::XSmartTagAction > xAction = rActionComponents[0];
89 :
90 0 : if ( !xAction.is() )
91 0 : continue;
92 :
93 0 : const sal_Int32 nSmartTagIndex = rActionIndices[0];
94 0 : const rtl::OUString aSmartTagType = xAction->getSmartTagName( nSmartTagIndex );
95 0 : const rtl::OUString aSmartTagCaption = xAction->getSmartTagCaption( nSmartTagIndex, rLocale );
96 :
97 : // no sub-menus if there's only one smart tag type listed:
98 0 : PopupMenu* pSbMenu = mpMenu;
99 0 : if ( 1 < rActionComponentsSequence.getLength() )
100 : {
101 0 : mpMenu->InsertItem( nMenuId, aSmartTagCaption, 0, nMenuPos++);
102 0 : pSbMenu = new PopupMenu;
103 0 : mpMenu->SetPopupMenu( nMenuId++, pSbMenu );
104 : }
105 0 : pSbMenu->SetSelectHdl( LINK( this, SvxSmartTagsControl, MenuSelect ) );
106 :
107 : // sub-menu starts with smart tag caption and separator
108 0 : const rtl::OUString aSmartTagCaption2 = aSmartTagCaption + rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(": ")) + aRangeText;
109 0 : nSubMenuPos = 0;
110 0 : pSbMenu->InsertItem( nMenuId++, aSmartTagCaption2, MIB_NOSELECT, nSubMenuPos++ );
111 0 : pSbMenu->InsertSeparator( nSubMenuPos++ );
112 :
113 : // Add subitem for every action reference for the current smart tag type:
114 0 : for ( sal_uInt16 i = 0; i < rActionComponents.getLength(); ++i )
115 : {
116 0 : xAction = rActionComponents[i];
117 :
118 0 : for ( sal_Int32 k = 0; k < xAction->getActionCount( aSmartTagType, xController ); ++k )
119 : {
120 0 : const sal_uInt32 nActionID = xAction->getActionID( aSmartTagType, k, xController );
121 0 : rtl::OUString aActionCaption = xAction->getActionCaptionFromID( nActionID,
122 : aApplicationName,
123 : rLocale,
124 : xSmartTagProperties,
125 : aRangeText,
126 : rtl::OUString(),
127 : xController,
128 0 : xTextRange );
129 :
130 0 : pSbMenu->InsertItem( nSubMenuId++, aActionCaption, 0, nSubMenuPos++ );
131 0 : InvokeAction aEntry( xAction, xSmartTagProperties, nActionID );
132 0 : maInvokeActions.push_back( aEntry );
133 0 : }
134 : }
135 0 : }
136 : }
137 :
138 : //--------------------------------------------------------------------
139 :
140 0 : void SvxSmartTagsControl::StateChanged( sal_uInt16, SfxItemState eState, const SfxPoolItem* pState )
141 :
142 : {
143 0 : mrParent.EnableItem( GetId(), SFX_ITEM_DISABLED != eState );
144 :
145 0 : if ( SFX_ITEM_AVAILABLE == eState )
146 : {
147 0 : const SvxSmartTagItem* pSmartTagItem = PTR_CAST( SvxSmartTagItem, pState );
148 0 : if ( 0 != pSmartTagItem )
149 : {
150 0 : delete mpSmartTagItem;
151 0 : mpSmartTagItem = new SvxSmartTagItem( *pSmartTagItem );
152 0 : FillMenu();
153 : }
154 : }
155 0 : }
156 :
157 : //--------------------------------------------------------------------
158 :
159 0 : IMPL_LINK_INLINE_START( SvxSmartTagsControl, MenuSelect, PopupMenu *, pMen )
160 : {
161 0 : if ( !mpSmartTagItem )
162 0 : return 0;
163 :
164 0 : sal_uInt16 nMyId = pMen->GetCurItemId();
165 :
166 0 : if ( nMyId < MN_ST_INSERT_START) return 0;
167 0 : nMyId -= MN_ST_INSERT_START;
168 :
169 : // compute smarttag lib index and action index
170 0 : Reference< smarttags::XSmartTagAction > xSmartTagAction = maInvokeActions[ nMyId ].mxAction;
171 :
172 : // execute action
173 0 : if ( xSmartTagAction.is() )
174 : {
175 0 : xSmartTagAction->invokeAction( maInvokeActions[ nMyId ].mnActionID,
176 : mpSmartTagItem->GetApplicationName(),
177 0 : mpSmartTagItem->GetController(),
178 0 : mpSmartTagItem->GetTextRange(),
179 0 : maInvokeActions[ nMyId ].mxSmartTagProperties,
180 : mpSmartTagItem->GetRangeText(),
181 : rtl::OUString(),
182 0 : mpSmartTagItem->GetLocale() );
183 : }
184 :
185 : // ohne dispatcher!!!
186 : // GetBindings().Execute( GetId(), SFX_CALLMODE_RECORD,meine beiden items, 0L );*/
187 : //SfxBoolItem aBool(SID_OPEN_SMARTTAGOPTIONS, sal_True);
188 : //GetBindings().GetDispatcher()->Execute( SID_AUTO_CORRECT_DLG, SFX_CALLMODE_ASYNCHRON, &aBool, 0L );
189 :
190 0 : return 0;
191 : }
192 0 : IMPL_LINK_INLINE_END( SvxSmartTagsControl, MenuSelect, PopupMenu *, pMen )
193 :
194 : //--------------------------------------------------------------------
195 :
196 0 : SvxSmartTagsControl::~SvxSmartTagsControl()
197 : {
198 0 : delete mpSmartTagItem;
199 0 : delete mpMenu;
200 0 : }
201 :
202 : //--------------------------------------------------------------------
203 :
204 0 : PopupMenu* SvxSmartTagsControl::GetPopup() const
205 : {
206 0 : return mpMenu;
207 : }
208 :
209 :
210 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|