1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
29*cdf0e10cSrcweir #include "precompiled_framework.hxx"
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir #ifndef __FRAMEWORK_UIELEMENT_DROPDOWNBOXTOOLBARCONTROLLER_HXX
32*cdf0e10cSrcweir #include "uielement/dropdownboxtoolbarcontroller.hxx"
33*cdf0e10cSrcweir #endif
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
36*cdf0e10cSrcweir //	my own includes
37*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #ifndef __FRAMEWORK_TOOLBAR_HXX_
40*cdf0e10cSrcweir #include "uielement/toolbar.hxx"
41*cdf0e10cSrcweir #endif
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
44*cdf0e10cSrcweir //	interface includes
45*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
46*cdf0e10cSrcweir #include <com/sun/star/util/XURLTransformer.hpp>
47*cdf0e10cSrcweir #include <com/sun/star/frame/XDispatchProvider.hpp>
48*cdf0e10cSrcweir #include <com/sun/star/beans/PropertyValue.hpp>
49*cdf0e10cSrcweir #include <com/sun/star/frame/status/ItemStatus.hpp>
50*cdf0e10cSrcweir #include <com/sun/star/frame/status/ItemState.hpp>
51*cdf0e10cSrcweir #include <com/sun/star/frame/status/Visibility.hpp>
52*cdf0e10cSrcweir #include <com/sun/star/frame/XControlNotificationListener.hpp>
53*cdf0e10cSrcweir 
54*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
55*cdf0e10cSrcweir //	other includes
56*cdf0e10cSrcweir //_________________________________________________________________________________________________________________
57*cdf0e10cSrcweir #include <svtools/toolboxcontroller.hxx>
58*cdf0e10cSrcweir #include <vos/mutex.hxx>
59*cdf0e10cSrcweir #include <vcl/svapp.hxx>
60*cdf0e10cSrcweir #ifndef _VCL_MNEMONIC_HXX_
61*cdf0e10cSrcweir #include <vcl/mnemonic.hxx>
62*cdf0e10cSrcweir #endif
63*cdf0e10cSrcweir #include <tools/urlobj.hxx>
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir using namespace ::com::sun::star;
66*cdf0e10cSrcweir using namespace ::com::sun::star::awt;
67*cdf0e10cSrcweir using namespace ::com::sun::star::uno;
68*cdf0e10cSrcweir using namespace ::com::sun::star::beans;
69*cdf0e10cSrcweir using namespace ::com::sun::star::lang;
70*cdf0e10cSrcweir using namespace ::com::sun::star::frame;
71*cdf0e10cSrcweir using namespace ::com::sun::star::frame::status;
72*cdf0e10cSrcweir using namespace ::com::sun::star::util;
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir namespace framework
75*cdf0e10cSrcweir {
76*cdf0e10cSrcweir 
77*cdf0e10cSrcweir // ------------------------------------------------------------------
78*cdf0e10cSrcweir 
79*cdf0e10cSrcweir // Wrapper class to notify controller about events from ListBox.
80*cdf0e10cSrcweir // Unfortunaltly the events are notifed through virtual methods instead
81*cdf0e10cSrcweir // of Listeners.
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir class ListBoxControl : public ListBox
84*cdf0e10cSrcweir {
85*cdf0e10cSrcweir     public:
86*cdf0e10cSrcweir         ListBoxControl( Window* pParent, WinBits nStyle, IListBoxListener* pListBoxListener );
87*cdf0e10cSrcweir         virtual ~ListBoxControl();
88*cdf0e10cSrcweir 
89*cdf0e10cSrcweir         virtual void Select();
90*cdf0e10cSrcweir         virtual void DoubleClick();
91*cdf0e10cSrcweir 	    virtual void GetFocus();
92*cdf0e10cSrcweir 	    virtual void LoseFocus();
93*cdf0e10cSrcweir         virtual long PreNotify( NotifyEvent& rNEvt );
94*cdf0e10cSrcweir 
95*cdf0e10cSrcweir     private:
96*cdf0e10cSrcweir         IListBoxListener* m_pListBoxListener;
97*cdf0e10cSrcweir };
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir ListBoxControl::ListBoxControl( Window* pParent, WinBits nStyle, IListBoxListener* pListBoxListener ) :
100*cdf0e10cSrcweir     ListBox( pParent, nStyle )
101*cdf0e10cSrcweir     , m_pListBoxListener( pListBoxListener )
102*cdf0e10cSrcweir {
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir ListBoxControl::~ListBoxControl()
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir     m_pListBoxListener = 0;
108*cdf0e10cSrcweir }
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir void ListBoxControl::Select()
111*cdf0e10cSrcweir {
112*cdf0e10cSrcweir     ListBox::Select();
113*cdf0e10cSrcweir     if ( m_pListBoxListener )
114*cdf0e10cSrcweir         m_pListBoxListener->Select();
115*cdf0e10cSrcweir }
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir void ListBoxControl::DoubleClick()
118*cdf0e10cSrcweir {
119*cdf0e10cSrcweir     ListBox::DoubleClick();
120*cdf0e10cSrcweir     if ( m_pListBoxListener )
121*cdf0e10cSrcweir         m_pListBoxListener->DoubleClick();
122*cdf0e10cSrcweir }
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir void ListBoxControl::GetFocus()
125*cdf0e10cSrcweir {
126*cdf0e10cSrcweir     ListBox::GetFocus();
127*cdf0e10cSrcweir     if ( m_pListBoxListener )
128*cdf0e10cSrcweir         m_pListBoxListener->GetFocus();
129*cdf0e10cSrcweir }
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir void ListBoxControl::LoseFocus()
132*cdf0e10cSrcweir {
133*cdf0e10cSrcweir     ListBox::LoseFocus();
134*cdf0e10cSrcweir     if ( m_pListBoxListener )
135*cdf0e10cSrcweir         m_pListBoxListener->LoseFocus();
136*cdf0e10cSrcweir }
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir long ListBoxControl::PreNotify( NotifyEvent& rNEvt )
139*cdf0e10cSrcweir {
140*cdf0e10cSrcweir     long nRet( 0 );
141*cdf0e10cSrcweir     if ( m_pListBoxListener )
142*cdf0e10cSrcweir         nRet = m_pListBoxListener->PreNotify( rNEvt );
143*cdf0e10cSrcweir     if ( nRet == 0 )
144*cdf0e10cSrcweir         nRet = ListBox::PreNotify( rNEvt );
145*cdf0e10cSrcweir 
146*cdf0e10cSrcweir     return nRet;
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir // ------------------------------------------------------------------
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir DropdownToolbarController::DropdownToolbarController(
152*cdf0e10cSrcweir     const Reference< XMultiServiceFactory >& rServiceManager,
153*cdf0e10cSrcweir     const Reference< XFrame >&               rFrame,
154*cdf0e10cSrcweir     ToolBox*                                 pToolbar,
155*cdf0e10cSrcweir     sal_uInt16                                   nID,
156*cdf0e10cSrcweir     sal_Int32                                nWidth,
157*cdf0e10cSrcweir     const ::rtl::OUString&                          aCommand ) :
158*cdf0e10cSrcweir     ComplexToolbarController( rServiceManager, rFrame, pToolbar, nID, aCommand )
159*cdf0e10cSrcweir     ,   m_pListBoxControl( 0 )
160*cdf0e10cSrcweir {
161*cdf0e10cSrcweir     m_pListBoxControl = new ListBoxControl( m_pToolbar, WB_DROPDOWN|WB_AUTOHSCROLL|WB_BORDER, this );
162*cdf0e10cSrcweir     if ( nWidth == 0 )
163*cdf0e10cSrcweir         nWidth = 100;
164*cdf0e10cSrcweir 
165*cdf0e10cSrcweir     // default dropdown size
166*cdf0e10cSrcweir     ::Size aLogicalSize( 0, 160 );
167*cdf0e10cSrcweir     ::Size aPixelSize = m_pListBoxControl->LogicToPixel( aLogicalSize, MAP_APPFONT );
168*cdf0e10cSrcweir 
169*cdf0e10cSrcweir     m_pListBoxControl->SetSizePixel( ::Size( nWidth, aPixelSize.Height() ));
170*cdf0e10cSrcweir     m_pToolbar->SetItemWindow( m_nID, m_pListBoxControl );
171*cdf0e10cSrcweir     m_pListBoxControl->SetDropDownLineCount( 5 );
172*cdf0e10cSrcweir }
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir // ------------------------------------------------------------------
175*cdf0e10cSrcweir 
176*cdf0e10cSrcweir DropdownToolbarController::~DropdownToolbarController()
177*cdf0e10cSrcweir {
178*cdf0e10cSrcweir }
179*cdf0e10cSrcweir 
180*cdf0e10cSrcweir // ------------------------------------------------------------------
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir void SAL_CALL DropdownToolbarController::dispose()
183*cdf0e10cSrcweir throw ( RuntimeException )
184*cdf0e10cSrcweir {
185*cdf0e10cSrcweir     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
186*cdf0e10cSrcweir 
187*cdf0e10cSrcweir     m_pToolbar->SetItemWindow( m_nID, 0 );
188*cdf0e10cSrcweir     delete m_pListBoxControl;
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir     ComplexToolbarController::dispose();
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir     m_pListBoxControl = 0;
193*cdf0e10cSrcweir }
194*cdf0e10cSrcweir 
195*cdf0e10cSrcweir // ------------------------------------------------------------------
196*cdf0e10cSrcweir Sequence<PropertyValue> DropdownToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
197*cdf0e10cSrcweir {
198*cdf0e10cSrcweir     Sequence<PropertyValue> aArgs( 2 );
199*cdf0e10cSrcweir     ::rtl::OUString aSelectedText = m_pListBoxControl->GetText();
200*cdf0e10cSrcweir 
201*cdf0e10cSrcweir     // Add key modifier to argument list
202*cdf0e10cSrcweir     aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" ));
203*cdf0e10cSrcweir     aArgs[0].Value <<= KeyModifier;
204*cdf0e10cSrcweir     aArgs[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
205*cdf0e10cSrcweir     aArgs[1].Value <<= aSelectedText;
206*cdf0e10cSrcweir     return aArgs;
207*cdf0e10cSrcweir }
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir // ------------------------------------------------------------------
210*cdf0e10cSrcweir 
211*cdf0e10cSrcweir void DropdownToolbarController::Select()
212*cdf0e10cSrcweir {
213*cdf0e10cSrcweir     if ( m_pListBoxControl->GetEntryCount() > 0 )
214*cdf0e10cSrcweir     {
215*cdf0e10cSrcweir         Window::PointerState aState = m_pListBoxControl->GetPointerState();
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir         sal_uInt16 nKeyModifier = sal_uInt16( aState.mnState & KEY_MODTYPE );
218*cdf0e10cSrcweir         execute( nKeyModifier );
219*cdf0e10cSrcweir     }
220*cdf0e10cSrcweir }
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir void DropdownToolbarController::DoubleClick()
223*cdf0e10cSrcweir {
224*cdf0e10cSrcweir }
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir void DropdownToolbarController::GetFocus()
227*cdf0e10cSrcweir {
228*cdf0e10cSrcweir     notifyFocusGet();
229*cdf0e10cSrcweir }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir void DropdownToolbarController::LoseFocus()
232*cdf0e10cSrcweir {
233*cdf0e10cSrcweir     notifyFocusLost();
234*cdf0e10cSrcweir }
235*cdf0e10cSrcweir 
236*cdf0e10cSrcweir long DropdownToolbarController::PreNotify( NotifyEvent& /*rNEvt*/ )
237*cdf0e10cSrcweir {
238*cdf0e10cSrcweir     return 0;
239*cdf0e10cSrcweir }
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir // --------------------------------------------------------
242*cdf0e10cSrcweir 
243*cdf0e10cSrcweir void DropdownToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
244*cdf0e10cSrcweir {
245*cdf0e10cSrcweir     if ( rControlCommand.Command.equalsAsciiL( "SetList", 7 ))
246*cdf0e10cSrcweir     {
247*cdf0e10cSrcweir         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
248*cdf0e10cSrcweir         {
249*cdf0e10cSrcweir             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "List", 4 ))
250*cdf0e10cSrcweir             {
251*cdf0e10cSrcweir                 Sequence< ::rtl::OUString > aList;
252*cdf0e10cSrcweir                 m_pListBoxControl->Clear();
253*cdf0e10cSrcweir 
254*cdf0e10cSrcweir                 rControlCommand.Arguments[i].Value >>= aList;
255*cdf0e10cSrcweir                 for ( sal_Int32 j = 0; j < aList.getLength(); j++ )
256*cdf0e10cSrcweir                     m_pListBoxControl->InsertEntry( aList[j] );
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir                 m_pListBoxControl->SelectEntryPos( 0 );
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir                 // send notification
261*cdf0e10cSrcweir                 uno::Sequence< beans::NamedValue > aInfo( 1 );
262*cdf0e10cSrcweir                 aInfo[0].Name  = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "List" ));
263*cdf0e10cSrcweir                 aInfo[0].Value <<= aList;
264*cdf0e10cSrcweir                 addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ListChanged" )),
265*cdf0e10cSrcweir                                getDispatchFromCommand( m_aCommandURL ),
266*cdf0e10cSrcweir                                aInfo );
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir                 break;
269*cdf0e10cSrcweir             }
270*cdf0e10cSrcweir         }
271*cdf0e10cSrcweir     }
272*cdf0e10cSrcweir     else if ( rControlCommand.Command.equalsAsciiL( "AddEntry", 8 ))
273*cdf0e10cSrcweir     {
274*cdf0e10cSrcweir         sal_uInt16      nPos( LISTBOX_APPEND );
275*cdf0e10cSrcweir         rtl::OUString   aText;
276*cdf0e10cSrcweir         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
277*cdf0e10cSrcweir         {
278*cdf0e10cSrcweir             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
279*cdf0e10cSrcweir             {
280*cdf0e10cSrcweir                 if ( rControlCommand.Arguments[i].Value >>= aText )
281*cdf0e10cSrcweir                     m_pListBoxControl->InsertEntry( aText, nPos );
282*cdf0e10cSrcweir                 break;
283*cdf0e10cSrcweir             }
284*cdf0e10cSrcweir         }
285*cdf0e10cSrcweir     }
286*cdf0e10cSrcweir     else if ( rControlCommand.Command.equalsAsciiL( "InsertEntry", 11 ))
287*cdf0e10cSrcweir     {
288*cdf0e10cSrcweir         sal_uInt16      nPos( LISTBOX_APPEND );
289*cdf0e10cSrcweir         rtl::OUString   aText;
290*cdf0e10cSrcweir         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
291*cdf0e10cSrcweir         {
292*cdf0e10cSrcweir             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
293*cdf0e10cSrcweir             {
294*cdf0e10cSrcweir                 sal_Int32 nTmpPos = 0;
295*cdf0e10cSrcweir                 if ( rControlCommand.Arguments[i].Value >>= nTmpPos )
296*cdf0e10cSrcweir                 {
297*cdf0e10cSrcweir                     if (( nTmpPos >= 0 ) &&
298*cdf0e10cSrcweir                         ( nTmpPos < sal_Int32( m_pListBoxControl->GetEntryCount() )))
299*cdf0e10cSrcweir                         nPos = sal_uInt16( nTmpPos );
300*cdf0e10cSrcweir                 }
301*cdf0e10cSrcweir             }
302*cdf0e10cSrcweir             else if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
303*cdf0e10cSrcweir                 rControlCommand.Arguments[i].Value >>= aText;
304*cdf0e10cSrcweir         }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir         m_pListBoxControl->InsertEntry( aText, nPos );
307*cdf0e10cSrcweir     }
308*cdf0e10cSrcweir     else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryPos", 14 ))
309*cdf0e10cSrcweir     {
310*cdf0e10cSrcweir         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
311*cdf0e10cSrcweir         {
312*cdf0e10cSrcweir             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
313*cdf0e10cSrcweir             {
314*cdf0e10cSrcweir                 sal_Int32 nPos( -1 );
315*cdf0e10cSrcweir                 if ( rControlCommand.Arguments[i].Value >>= nPos )
316*cdf0e10cSrcweir                 {
317*cdf0e10cSrcweir                     if ( nPos < sal_Int32( m_pListBoxControl->GetEntryCount() ))
318*cdf0e10cSrcweir                         m_pListBoxControl->RemoveEntry( sal_uInt16( nPos ));
319*cdf0e10cSrcweir                 }
320*cdf0e10cSrcweir                 break;
321*cdf0e10cSrcweir             }
322*cdf0e10cSrcweir         }
323*cdf0e10cSrcweir     }
324*cdf0e10cSrcweir     else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryText", 15 ))
325*cdf0e10cSrcweir     {
326*cdf0e10cSrcweir         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
327*cdf0e10cSrcweir         {
328*cdf0e10cSrcweir             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
329*cdf0e10cSrcweir             {
330*cdf0e10cSrcweir                 rtl::OUString aText;
331*cdf0e10cSrcweir                 if ( rControlCommand.Arguments[i].Value >>= aText )
332*cdf0e10cSrcweir                     m_pListBoxControl->RemoveEntry( aText );
333*cdf0e10cSrcweir                 break;
334*cdf0e10cSrcweir             }
335*cdf0e10cSrcweir         }
336*cdf0e10cSrcweir     }
337*cdf0e10cSrcweir     else if ( rControlCommand.Command.equalsAsciiL( "SetDropDownLines", 16 ))
338*cdf0e10cSrcweir     {
339*cdf0e10cSrcweir         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
340*cdf0e10cSrcweir         {
341*cdf0e10cSrcweir             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Lines", 5 ))
342*cdf0e10cSrcweir             {
343*cdf0e10cSrcweir                 sal_Int32 nValue( 5 );
344*cdf0e10cSrcweir                 rControlCommand.Arguments[i].Value >>= nValue;
345*cdf0e10cSrcweir                 m_pListBoxControl->SetDropDownLineCount( sal_uInt16( nValue ));
346*cdf0e10cSrcweir                 break;
347*cdf0e10cSrcweir             }
348*cdf0e10cSrcweir         }
349*cdf0e10cSrcweir     }
350*cdf0e10cSrcweir }
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir } // namespace
353*cdf0e10cSrcweir 
354