Update ooo320-m1
[ooovba.git] / framework / source / uielement / togglebuttontoolbarcontroller.cxx
blobb803edb1c36f1d5a1d3a828ce5389b8c9733578d
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: togglebuttontoolbarcontroller.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 #ifndef __FRAMEWORK_UIELEMENT_TOGGLEBUTTONTOOLBARCONTROLLER_HXX
35 #include "uielement/togglebuttontoolbarcontroller.hxx"
36 #endif
38 //_________________________________________________________________________________________________________________
39 // my own includes
40 //_________________________________________________________________________________________________________________
41 #include <classes/addonsoptions.hxx>
42 #ifndef __FRAMEWORK_TOOLBAR_HXX_
43 #include "uielement/toolbar.hxx"
44 #endif
46 //_________________________________________________________________________________________________________________
47 // interface includes
48 //_________________________________________________________________________________________________________________
49 #include <com/sun/star/util/XURLTransformer.hpp>
50 #include <com/sun/star/frame/XDispatchProvider.hpp>
51 #include <com/sun/star/beans/PropertyValue.hpp>
52 #include <com/sun/star/lang/DisposedException.hpp>
53 #include <com/sun/star/frame/XControlNotificationListener.hpp>
54 #include "com/sun/star/util/XMacroExpander.hpp"
55 #include "com/sun/star/uno/XComponentContext.hpp"
56 #include "com/sun/star/beans/XPropertySet.hpp"
58 //_________________________________________________________________________________________________________________
59 // other includes
60 //_________________________________________________________________________________________________________________
62 #include <rtl/uri.hxx>
63 #include <vos/mutex.hxx>
64 #include <comphelper/processfactory.hxx>
65 #include <unotools/ucbstreamhelper.hxx>
66 #include <tools/urlobj.hxx>
67 #include <vcl/svapp.hxx>
68 #include <vcl/mnemonic.hxx>
69 #include <vcl/window.hxx>
70 #include <vcl/graph.hxx>
71 #include <vcl/bitmap.hxx>
72 #include <svtools/filter.hxx>
73 #include <svtools/miscopt.hxx>
75 using namespace ::com::sun::star;
76 using namespace ::com::sun::star::awt;
77 using namespace ::com::sun::star::uno;
78 using namespace ::com::sun::star::beans;
79 using namespace ::com::sun::star::lang;
80 using namespace ::com::sun::star::frame;
81 using namespace ::com::sun::star::util;
83 namespace framework
86 // ------------------------------------------------------------------
88 ToggleButtonToolbarController::ToggleButtonToolbarController(
89 const Reference< XMultiServiceFactory >& rServiceManager,
90 const Reference< XFrame >& rFrame,
91 ToolBox* pToolbar,
92 USHORT nID,
93 Style eStyle,
94 const ::rtl::OUString& aCommand ) :
95 ComplexToolbarController( rServiceManager, rFrame, pToolbar, nID, aCommand ),
96 m_eStyle( eStyle )
98 if ( eStyle == STYLE_DROPDOWNBUTTON )
99 m_pToolbar->SetItemBits( m_nID, TIB_DROPDOWNONLY | m_pToolbar->GetItemBits( m_nID ) );
100 else if ( eStyle == STYLE_TOGGLE_DROPDOWNBUTTON )
101 m_pToolbar->SetItemBits( m_nID, TIB_DROPDOWN | m_pToolbar->GetItemBits( m_nID ) );
104 // ------------------------------------------------------------------
106 ToggleButtonToolbarController::~ToggleButtonToolbarController()
110 // ------------------------------------------------------------------
112 void SAL_CALL ToggleButtonToolbarController::dispose()
113 throw ( RuntimeException )
115 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
116 ComplexToolbarController::dispose();
119 // ------------------------------------------------------------------
120 Sequence<PropertyValue> ToggleButtonToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
122 Sequence<PropertyValue> aArgs( 2 );
124 // Add key modifier to argument list
125 aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" ));
126 aArgs[0].Value <<= KeyModifier;
127 aArgs[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
128 aArgs[1].Value <<= m_aCurrentSelection;
129 return aArgs;
132 // ------------------------------------------------------------------
134 uno::Reference< awt::XWindow > SAL_CALL ToggleButtonToolbarController::createPopupWindow()
135 throw (::com::sun::star::uno::RuntimeException)
137 uno::Reference< awt::XWindow > xWindow;
139 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
140 if (( m_eStyle == STYLE_DROPDOWNBUTTON ) ||
141 ( m_eStyle == STYLE_TOGGLE_DROPDOWNBUTTON ))
143 // create popup menu
144 PopupMenu aPopup;
145 const sal_uInt32 nCount = m_aDropdownMenuList.size();
146 for ( sal_uInt32 i = 0; i < nCount; i++ )
148 rtl::OUString aLabel( m_aDropdownMenuList[i] );
149 aPopup.InsertItem( sal_uInt16( i+1 ), aLabel );
150 if ( aLabel == m_aCurrentSelection )
151 aPopup.CheckItem( sal_uInt16( i+1 ), sal_True );
152 else
153 aPopup.CheckItem( sal_uInt16( i+1 ), sal_False );
156 m_pToolbar->SetItemDown( m_nID, TRUE );
157 aPopup.SetSelectHdl( LINK( this, ToggleButtonToolbarController, MenuSelectHdl ));
158 aPopup.Execute( m_pToolbar, m_pToolbar->GetItemRect( m_nID ));
159 m_pToolbar->SetItemDown( m_nID, FALSE );
162 return xWindow;
165 // ------------------------------------------------------------------
167 void ToggleButtonToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
169 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
171 if (( m_eStyle == STYLE_DROPDOWNBUTTON ) ||
172 ( m_eStyle == STYLE_TOGGLE_DROPDOWNBUTTON ))
174 if ( rControlCommand.Command.equalsAsciiL( "SetList", 7 ))
176 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
178 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "List", 4 ))
180 Sequence< ::rtl::OUString > aList;
181 m_aDropdownMenuList.clear();
183 rControlCommand.Arguments[i].Value >>= aList;
184 for ( sal_Int32 j = 0; j < aList.getLength(); j++ )
185 m_aDropdownMenuList.push_back( aList[j] );
187 // send notification
188 uno::Sequence< beans::NamedValue > aInfo( 1 );
189 aInfo[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "List" ));
190 aInfo[0].Value <<= aList;
191 addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ListChanged" )),
192 getDispatchFromCommand( m_aCommandURL ),
193 aInfo );
195 break;
199 else if ( rControlCommand.Command.equalsAsciiL( "CheckItemPos", 12 ))
201 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
203 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
205 sal_Int32 nPos( -1 );
207 rControlCommand.Arguments[i].Value >>= nPos;
208 if ( nPos >= 0 &&
209 ( sal::static_int_cast< sal_uInt32 >(nPos)
210 < m_aDropdownMenuList.size() ) )
212 m_aCurrentSelection = m_aDropdownMenuList[nPos];
214 // send notification
215 uno::Sequence< beans::NamedValue > aInfo( 1 );
216 aInfo[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ItemChecked" ));
217 aInfo[0].Value <<= nPos;
218 addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Pos" )),
219 getDispatchFromCommand( m_aCommandURL ),
220 aInfo );
222 break;
226 else if ( rControlCommand.Command.equalsAsciiL( "AddEntry", 8 ))
228 rtl::OUString aText;
229 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
231 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
233 if ( rControlCommand.Arguments[i].Value >>= aText )
234 m_aDropdownMenuList.push_back( aText );
235 break;
239 else if ( rControlCommand.Command.equalsAsciiL( "InsertEntry", 11 ))
241 sal_Int32 nPos( COMBOBOX_APPEND );
242 sal_Int32 nSize = sal_Int32( m_aDropdownMenuList.size() );
243 rtl::OUString aText;
244 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
246 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
248 sal_Int32 nTmpPos = 0;
249 if ( rControlCommand.Arguments[i].Value >>= nTmpPos )
251 if (( nTmpPos >= 0 ) && ( nTmpPos < sal_Int32( nSize )))
252 nPos = nTmpPos;
255 else if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
256 rControlCommand.Arguments[i].Value >>= aText;
259 std::vector< ::rtl::OUString >::iterator aIter = m_aDropdownMenuList.begin();
260 aIter += nPos;
261 m_aDropdownMenuList.insert( aIter, aText );
263 else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryPos", 14 ))
265 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
267 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
269 sal_Int32 nPos( -1 );
270 if ( rControlCommand.Arguments[i].Value >>= nPos )
272 if ( nPos < sal_Int32( m_aDropdownMenuList.size() ))
274 std::vector< ::rtl::OUString >::iterator aIter = m_aDropdownMenuList.begin();
275 aIter += nPos;
276 m_aDropdownMenuList.erase( aIter );
279 break;
283 else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryText", 15 ))
285 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
287 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
289 rtl::OUString aText;
290 if ( rControlCommand.Arguments[i].Value >>= aText )
292 sal_Int32 nSize = sal_Int32( m_aDropdownMenuList.size() );
293 for ( sal_Int32 j = 0; j < nSize; j++ )
295 if ( m_aDropdownMenuList[j] == aText )
297 std::vector< ::rtl::OUString >::iterator aIter = m_aDropdownMenuList.begin();
298 aIter += j;
299 m_aDropdownMenuList.erase( aIter );
300 break;
304 break;
311 IMPL_LINK( ToggleButtonToolbarController, MenuSelectHdl, Menu *, pMenu )
313 vos::OGuard aGuard( Application::GetSolarMutex() );
315 sal_uInt16 nItemId = pMenu->GetCurItemId();
316 if ( nItemId > 0 && nItemId <= m_aDropdownMenuList.size() )
318 m_aCurrentSelection = m_aDropdownMenuList[nItemId-1];
320 execute( 0 );
322 return 0;
325 } // namespace