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 "uielement/togglebuttontoolbarcontroller.hxx"
22 :
23 : #include <framework/addonsoptions.hxx>
24 : #include "uielement/toolbar.hxx"
25 :
26 : #include <com/sun/star/util/XURLTransformer.hpp>
27 : #include <com/sun/star/frame/XDispatchProvider.hpp>
28 : #include <com/sun/star/beans/PropertyValue.hpp>
29 : #include <com/sun/star/frame/XControlNotificationListener.hpp>
30 : #include "com/sun/star/util/XMacroExpander.hpp"
31 : #include "com/sun/star/uno/XComponentContext.hpp"
32 : #include "com/sun/star/beans/XPropertySet.hpp"
33 :
34 : #include <rtl/uri.hxx>
35 : #include <osl/mutex.hxx>
36 : #include <comphelper/processfactory.hxx>
37 : #include <unotools/ucbstreamhelper.hxx>
38 : #include <vcl/svapp.hxx>
39 : #include <vcl/mnemonic.hxx>
40 : #include <vcl/window.hxx>
41 : #include <vcl/graph.hxx>
42 : #include <vcl/bitmap.hxx>
43 : #include <svtools/filter.hxx>
44 : #include <svtools/miscopt.hxx>
45 :
46 : using namespace ::com::sun::star;
47 : using namespace ::com::sun::star::awt;
48 : using namespace ::com::sun::star::uno;
49 : using namespace ::com::sun::star::beans;
50 : using namespace ::com::sun::star::lang;
51 : using namespace ::com::sun::star::frame;
52 : using namespace ::com::sun::star::util;
53 :
54 : namespace framework
55 : {
56 :
57 : // ------------------------------------------------------------------
58 :
59 0 : ToggleButtonToolbarController::ToggleButtonToolbarController(
60 : const Reference< XMultiServiceFactory >& rServiceManager,
61 : const Reference< XFrame >& rFrame,
62 : ToolBox* pToolbar,
63 : sal_uInt16 nID,
64 : Style eStyle,
65 : const ::rtl::OUString& aCommand ) :
66 : ComplexToolbarController( rServiceManager, rFrame, pToolbar, nID, aCommand ),
67 0 : m_eStyle( eStyle )
68 : {
69 0 : if ( eStyle == STYLE_DROPDOWNBUTTON )
70 0 : m_pToolbar->SetItemBits( m_nID, TIB_DROPDOWNONLY | m_pToolbar->GetItemBits( m_nID ) );
71 0 : else if ( eStyle == STYLE_TOGGLE_DROPDOWNBUTTON )
72 0 : m_pToolbar->SetItemBits( m_nID, TIB_DROPDOWN | m_pToolbar->GetItemBits( m_nID ) );
73 0 : }
74 :
75 : // ------------------------------------------------------------------
76 :
77 0 : ToggleButtonToolbarController::~ToggleButtonToolbarController()
78 : {
79 0 : }
80 :
81 : // ------------------------------------------------------------------
82 :
83 0 : void SAL_CALL ToggleButtonToolbarController::dispose()
84 : throw ( RuntimeException )
85 : {
86 0 : SolarMutexGuard aSolarMutexGuard;
87 0 : ComplexToolbarController::dispose();
88 0 : }
89 :
90 : // ------------------------------------------------------------------
91 0 : Sequence<PropertyValue> ToggleButtonToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
92 : {
93 0 : Sequence<PropertyValue> aArgs( 2 );
94 :
95 : // Add key modifier to argument list
96 0 : aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" ));
97 0 : aArgs[0].Value <<= KeyModifier;
98 0 : aArgs[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
99 0 : aArgs[1].Value <<= m_aCurrentSelection;
100 0 : return aArgs;
101 : }
102 :
103 : // ------------------------------------------------------------------
104 :
105 0 : uno::Reference< awt::XWindow > SAL_CALL ToggleButtonToolbarController::createPopupWindow()
106 : throw (::com::sun::star::uno::RuntimeException)
107 : {
108 0 : uno::Reference< awt::XWindow > xWindow;
109 :
110 0 : SolarMutexGuard aSolarMutexGuard;
111 0 : if (( m_eStyle == STYLE_DROPDOWNBUTTON ) ||
112 : ( m_eStyle == STYLE_TOGGLE_DROPDOWNBUTTON ))
113 : {
114 : // create popup menu
115 0 : PopupMenu aPopup;
116 0 : const sal_uInt32 nCount = m_aDropdownMenuList.size();
117 0 : for ( sal_uInt32 i = 0; i < nCount; i++ )
118 : {
119 0 : rtl::OUString aLabel( m_aDropdownMenuList[i] );
120 0 : aPopup.InsertItem( sal_uInt16( i+1 ), aLabel );
121 0 : if ( aLabel == m_aCurrentSelection )
122 0 : aPopup.CheckItem( sal_uInt16( i+1 ), sal_True );
123 : else
124 0 : aPopup.CheckItem( sal_uInt16( i+1 ), sal_False );
125 0 : }
126 :
127 0 : m_pToolbar->SetItemDown( m_nID, sal_True );
128 0 : aPopup.SetSelectHdl( LINK( this, ToggleButtonToolbarController, MenuSelectHdl ));
129 0 : aPopup.Execute( m_pToolbar, m_pToolbar->GetItemRect( m_nID ));
130 0 : m_pToolbar->SetItemDown( m_nID, sal_False );
131 : }
132 :
133 0 : return xWindow;
134 : }
135 :
136 : // ------------------------------------------------------------------
137 :
138 0 : void ToggleButtonToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
139 : {
140 0 : SolarMutexGuard aSolarMutexGuard;
141 :
142 0 : if (( m_eStyle == STYLE_DROPDOWNBUTTON ) ||
143 : ( m_eStyle == STYLE_TOGGLE_DROPDOWNBUTTON ))
144 : {
145 0 : if ( rControlCommand.Command.equalsAsciiL( "SetList", 7 ))
146 : {
147 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
148 : {
149 0 : if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "List", 4 ))
150 : {
151 0 : Sequence< ::rtl::OUString > aList;
152 0 : m_aDropdownMenuList.clear();
153 :
154 0 : rControlCommand.Arguments[i].Value >>= aList;
155 0 : for ( sal_Int32 j = 0; j < aList.getLength(); j++ )
156 0 : m_aDropdownMenuList.push_back( aList[j] );
157 :
158 : // send notification
159 0 : uno::Sequence< beans::NamedValue > aInfo( 1 );
160 0 : aInfo[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "List" ));
161 0 : aInfo[0].Value <<= aList;
162 : addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ListChanged" )),
163 : getDispatchFromCommand( m_aCommandURL ),
164 0 : aInfo );
165 :
166 0 : break;
167 : }
168 : }
169 : }
170 0 : else if ( rControlCommand.Command.equalsAsciiL( "CheckItemPos", 12 ))
171 : {
172 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
173 : {
174 0 : if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
175 : {
176 0 : sal_Int32 nPos( -1 );
177 :
178 0 : rControlCommand.Arguments[i].Value >>= nPos;
179 0 : if ( nPos >= 0 &&
180 0 : ( sal::static_int_cast< sal_uInt32 >(nPos)
181 0 : < m_aDropdownMenuList.size() ) )
182 : {
183 0 : m_aCurrentSelection = m_aDropdownMenuList[nPos];
184 :
185 : // send notification
186 0 : uno::Sequence< beans::NamedValue > aInfo( 1 );
187 0 : aInfo[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ItemChecked" ));
188 0 : aInfo[0].Value <<= nPos;
189 : addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Pos" )),
190 : getDispatchFromCommand( m_aCommandURL ),
191 0 : aInfo );
192 : }
193 : break;
194 : }
195 : }
196 : }
197 0 : else if ( rControlCommand.Command.equalsAsciiL( "AddEntry", 8 ))
198 : {
199 0 : rtl::OUString aText;
200 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
201 : {
202 0 : if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
203 : {
204 0 : if ( rControlCommand.Arguments[i].Value >>= aText )
205 0 : m_aDropdownMenuList.push_back( aText );
206 0 : break;
207 : }
208 0 : }
209 : }
210 0 : else if ( rControlCommand.Command.equalsAsciiL( "InsertEntry", 11 ))
211 : {
212 0 : sal_Int32 nPos( COMBOBOX_APPEND );
213 0 : sal_Int32 nSize = sal_Int32( m_aDropdownMenuList.size() );
214 0 : rtl::OUString aText;
215 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
216 : {
217 0 : if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
218 : {
219 0 : sal_Int32 nTmpPos = 0;
220 0 : if ( rControlCommand.Arguments[i].Value >>= nTmpPos )
221 : {
222 0 : if (( nTmpPos >= 0 ) && ( nTmpPos < sal_Int32( nSize )))
223 0 : nPos = nTmpPos;
224 : }
225 : }
226 0 : else if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
227 0 : rControlCommand.Arguments[i].Value >>= aText;
228 : }
229 :
230 0 : std::vector< ::rtl::OUString >::iterator aIter = m_aDropdownMenuList.begin();
231 0 : aIter += nPos;
232 0 : m_aDropdownMenuList.insert( aIter, aText );
233 : }
234 0 : else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryPos", 14 ))
235 : {
236 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
237 : {
238 0 : if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
239 : {
240 0 : sal_Int32 nPos( -1 );
241 0 : if ( rControlCommand.Arguments[i].Value >>= nPos )
242 : {
243 0 : if ( nPos < sal_Int32( m_aDropdownMenuList.size() ))
244 : {
245 0 : std::vector< ::rtl::OUString >::iterator aIter = m_aDropdownMenuList.begin();
246 0 : aIter += nPos;
247 0 : m_aDropdownMenuList.erase( aIter );
248 : }
249 : }
250 : break;
251 : }
252 : }
253 : }
254 0 : else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryText", 15 ))
255 : {
256 0 : for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
257 : {
258 0 : if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
259 : {
260 0 : rtl::OUString aText;
261 0 : if ( rControlCommand.Arguments[i].Value >>= aText )
262 : {
263 0 : sal_Int32 nSize = sal_Int32( m_aDropdownMenuList.size() );
264 0 : for ( sal_Int32 j = 0; j < nSize; j++ )
265 : {
266 0 : if ( m_aDropdownMenuList[j] == aText )
267 : {
268 0 : std::vector< ::rtl::OUString >::iterator aIter = m_aDropdownMenuList.begin();
269 0 : aIter += j;
270 0 : m_aDropdownMenuList.erase( aIter );
271 : break;
272 : }
273 : }
274 : }
275 0 : break;
276 : }
277 : }
278 : }
279 0 : }
280 0 : }
281 :
282 0 : IMPL_LINK( ToggleButtonToolbarController, MenuSelectHdl, Menu *, pMenu )
283 : {
284 0 : SolarMutexGuard aGuard;
285 :
286 0 : sal_uInt16 nItemId = pMenu->GetCurItemId();
287 0 : if ( nItemId > 0 && nItemId <= m_aDropdownMenuList.size() )
288 : {
289 0 : m_aCurrentSelection = m_aDropdownMenuList[nItemId-1];
290 :
291 0 : execute( 0 );
292 : }
293 0 : return 0;
294 : }
295 :
296 : } // namespace
297 :
298 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|