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