1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 // MARKER(update_precomp.py): autogen include statement, do not remove 25 #include "precompiled_framework.hxx" 26 27 #ifndef __FRAMEWORK_UIELEMENT_TOGGLEBUTTONTOOLBARCONTROLLER_HXX 28 #include "uielement/togglebuttontoolbarcontroller.hxx" 29 #endif 30 31 //_________________________________________________________________________________________________________________ 32 // my own includes 33 //_________________________________________________________________________________________________________________ 34 #include <framework/addonsoptions.hxx> 35 #ifndef __FRAMEWORK_TOOLBAR_HXX_ 36 #include "uielement/toolbar.hxx" 37 #endif 38 39 //_________________________________________________________________________________________________________________ 40 // interface includes 41 //_________________________________________________________________________________________________________________ 42 #include <com/sun/star/util/XURLTransformer.hpp> 43 #include <com/sun/star/frame/XDispatchProvider.hpp> 44 #include <com/sun/star/beans/PropertyValue.hpp> 45 #include <com/sun/star/frame/XControlNotificationListener.hpp> 46 #include "com/sun/star/util/XMacroExpander.hpp" 47 #include "com/sun/star/uno/XComponentContext.hpp" 48 #include "com/sun/star/beans/XPropertySet.hpp" 49 50 //_________________________________________________________________________________________________________________ 51 // other includes 52 //_________________________________________________________________________________________________________________ 53 54 #include <rtl/uri.hxx> 55 #include <vos/mutex.hxx> 56 #include <comphelper/processfactory.hxx> 57 #include <unotools/ucbstreamhelper.hxx> 58 #include <tools/urlobj.hxx> 59 #include <vcl/svapp.hxx> 60 #include <vcl/mnemonic.hxx> 61 #include <vcl/window.hxx> 62 #include <vcl/graph.hxx> 63 #include <vcl/bitmap.hxx> 64 #include <svtools/filter.hxx> 65 #include <svtools/miscopt.hxx> 66 67 using namespace ::com::sun::star; 68 using namespace ::com::sun::star::awt; 69 using namespace ::com::sun::star::uno; 70 using namespace ::com::sun::star::beans; 71 using namespace ::com::sun::star::lang; 72 using namespace ::com::sun::star::frame; 73 using namespace ::com::sun::star::util; 74 75 namespace framework 76 { 77 78 // ------------------------------------------------------------------ 79 80 ToggleButtonToolbarController::ToggleButtonToolbarController( 81 const Reference< XMultiServiceFactory >& rServiceManager, 82 const Reference< XFrame >& rFrame, 83 ToolBox* pToolbar, 84 sal_uInt16 nID, 85 Style eStyle, 86 const ::rtl::OUString& aCommand ) : 87 ComplexToolbarController( rServiceManager, rFrame, pToolbar, nID, aCommand ), 88 m_eStyle( eStyle ) 89 { 90 if ( eStyle == STYLE_DROPDOWNBUTTON ) 91 m_pToolbar->SetItemBits( m_nID, TIB_DROPDOWNONLY | m_pToolbar->GetItemBits( m_nID ) ); 92 else if ( eStyle == STYLE_TOGGLE_DROPDOWNBUTTON ) 93 m_pToolbar->SetItemBits( m_nID, TIB_DROPDOWN | m_pToolbar->GetItemBits( m_nID ) ); 94 } 95 96 // ------------------------------------------------------------------ 97 98 ToggleButtonToolbarController::~ToggleButtonToolbarController() 99 { 100 } 101 102 // ------------------------------------------------------------------ 103 104 void SAL_CALL ToggleButtonToolbarController::dispose() 105 { 106 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() ); 107 ComplexToolbarController::dispose(); 108 } 109 110 // ------------------------------------------------------------------ 111 Sequence<PropertyValue> ToggleButtonToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const 112 { 113 Sequence<PropertyValue> aArgs( 2 ); 114 115 // Add key modifier to argument list 116 aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" )); 117 aArgs[0].Value <<= KeyModifier; 118 aArgs[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" )); 119 aArgs[1].Value <<= m_aCurrentSelection; 120 return aArgs; 121 } 122 123 // ------------------------------------------------------------------ 124 125 uno::Reference< awt::XWindow > SAL_CALL ToggleButtonToolbarController::createPopupWindow() 126 { 127 uno::Reference< awt::XWindow > xWindow; 128 129 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() ); 130 if (( m_eStyle == STYLE_DROPDOWNBUTTON ) || 131 ( m_eStyle == STYLE_TOGGLE_DROPDOWNBUTTON )) 132 { 133 // create popup menu 134 PopupMenu aPopup; 135 const sal_uInt32 nCount = m_aDropdownMenuList.size(); 136 for ( sal_uInt32 i = 0; i < nCount; i++ ) 137 { 138 rtl::OUString aLabel( m_aDropdownMenuList[i] ); 139 aPopup.InsertItem( sal_uInt16( i+1 ), aLabel ); 140 if ( aLabel == m_aCurrentSelection ) 141 aPopup.CheckItem( sal_uInt16( i+1 ), sal_True ); 142 else 143 aPopup.CheckItem( sal_uInt16( i+1 ), sal_False ); 144 } 145 146 m_pToolbar->SetItemDown( m_nID, sal_True ); 147 aPopup.SetSelectHdl( LINK( this, ToggleButtonToolbarController, MenuSelectHdl )); 148 aPopup.Execute( m_pToolbar, m_pToolbar->GetItemRect( m_nID )); 149 m_pToolbar->SetItemDown( m_nID, sal_False ); 150 } 151 152 return xWindow; 153 } 154 155 // ------------------------------------------------------------------ 156 157 void ToggleButtonToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand ) 158 { 159 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() ); 160 161 if (( m_eStyle == STYLE_DROPDOWNBUTTON ) || 162 ( m_eStyle == STYLE_TOGGLE_DROPDOWNBUTTON )) 163 { 164 if ( rControlCommand.Command.equalsAsciiL( "SetList", 7 )) 165 { 166 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ ) 167 { 168 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "List", 4 )) 169 { 170 Sequence< ::rtl::OUString > aList; 171 m_aDropdownMenuList.clear(); 172 173 rControlCommand.Arguments[i].Value >>= aList; 174 for ( sal_Int32 j = 0; j < aList.getLength(); j++ ) 175 m_aDropdownMenuList.push_back( aList[j] ); 176 177 // send notification 178 uno::Sequence< beans::NamedValue > aInfo( 1 ); 179 aInfo[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "List" )); 180 aInfo[0].Value <<= aList; 181 addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ListChanged" )), 182 getDispatchFromCommand( m_aCommandURL ), 183 aInfo ); 184 185 break; 186 } 187 } 188 } 189 else if ( rControlCommand.Command.equalsAsciiL( "CheckItemPos", 12 )) 190 { 191 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ ) 192 { 193 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 )) 194 { 195 sal_Int32 nPos( -1 ); 196 197 rControlCommand.Arguments[i].Value >>= nPos; 198 if ( nPos >= 0 && 199 ( sal::static_int_cast< sal_uInt32 >(nPos) 200 < m_aDropdownMenuList.size() ) ) 201 { 202 m_aCurrentSelection = m_aDropdownMenuList[nPos]; 203 204 // send notification 205 uno::Sequence< beans::NamedValue > aInfo( 1 ); 206 aInfo[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ItemChecked" )); 207 aInfo[0].Value <<= nPos; 208 addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Pos" )), 209 getDispatchFromCommand( m_aCommandURL ), 210 aInfo ); 211 } 212 break; 213 } 214 } 215 } 216 else if ( rControlCommand.Command.equalsAsciiL( "AddEntry", 8 )) 217 { 218 rtl::OUString aText; 219 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ ) 220 { 221 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 )) 222 { 223 if ( rControlCommand.Arguments[i].Value >>= aText ) 224 m_aDropdownMenuList.push_back( aText ); 225 break; 226 } 227 } 228 } 229 else if ( rControlCommand.Command.equalsAsciiL( "InsertEntry", 11 )) 230 { 231 sal_Int32 nPos( COMBOBOX_APPEND ); 232 sal_Int32 nSize = sal_Int32( m_aDropdownMenuList.size() ); 233 rtl::OUString aText; 234 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ ) 235 { 236 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 )) 237 { 238 sal_Int32 nTmpPos = 0; 239 if ( rControlCommand.Arguments[i].Value >>= nTmpPos ) 240 { 241 if (( nTmpPos >= 0 ) && ( nTmpPos < sal_Int32( nSize ))) 242 nPos = nTmpPos; 243 } 244 } 245 else if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 )) 246 rControlCommand.Arguments[i].Value >>= aText; 247 } 248 249 std::vector< ::rtl::OUString >::iterator aIter = m_aDropdownMenuList.begin(); 250 aIter += nPos; 251 m_aDropdownMenuList.insert( aIter, aText ); 252 } 253 else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryPos", 14 )) 254 { 255 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ ) 256 { 257 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 )) 258 { 259 sal_Int32 nPos( -1 ); 260 if ( rControlCommand.Arguments[i].Value >>= nPos ) 261 { 262 if ( nPos < sal_Int32( m_aDropdownMenuList.size() )) 263 { 264 std::vector< ::rtl::OUString >::iterator aIter = m_aDropdownMenuList.begin(); 265 aIter += nPos; 266 m_aDropdownMenuList.erase( aIter ); 267 } 268 } 269 break; 270 } 271 } 272 } 273 else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryText", 15 )) 274 { 275 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ ) 276 { 277 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 )) 278 { 279 rtl::OUString aText; 280 if ( rControlCommand.Arguments[i].Value >>= aText ) 281 { 282 sal_Int32 nSize = sal_Int32( m_aDropdownMenuList.size() ); 283 for ( sal_Int32 j = 0; j < nSize; j++ ) 284 { 285 if ( m_aDropdownMenuList[j] == aText ) 286 { 287 std::vector< ::rtl::OUString >::iterator aIter = m_aDropdownMenuList.begin(); 288 aIter += j; 289 m_aDropdownMenuList.erase( aIter ); 290 break; 291 } 292 } 293 } 294 break; 295 } 296 } 297 } 298 } 299 } 300 301 IMPL_LINK( ToggleButtonToolbarController, MenuSelectHdl, Menu *, pMenu ) 302 { 303 vos::OGuard aGuard( Application::GetSolarMutex() ); 304 305 sal_uInt16 nItemId = pMenu->GetCurItemId(); 306 if ( nItemId > 0 && nItemId <= m_aDropdownMenuList.size() ) 307 { 308 m_aCurrentSelection = m_aDropdownMenuList[nItemId-1]; 309 310 execute( 0 ); 311 } 312 return 0; 313 } 314 315 } // namespace 316