update dev300-m58
[ooovba.git] / framework / source / uielement / comboboxtoolbarcontroller.cxx
blob937c099aafdbcd28985990bfade4563c41b6bbcc
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: comboboxtoolbarcontroller.cxx,v $
10 * $Revision: 1.11 $
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_COMBOBOXTOOLBARCONTROLLER_HXX
35 #include "uielement/comboboxtoolbarcontroller.hxx"
36 #endif
38 //_________________________________________________________________________________________________________________
39 // my own includes
40 //_________________________________________________________________________________________________________________
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/status/ItemStatus.hpp>
54 #include <com/sun/star/frame/status/ItemState.hpp>
55 #include <com/sun/star/frame/status/Visibility.hpp>
56 #include <com/sun/star/frame/XControlNotificationListener.hpp>
57 #include <com/sun/star/util/Color.hpp>
59 //_________________________________________________________________________________________________________________
60 // other includes
61 //_________________________________________________________________________________________________________________
62 #include <svtools/toolboxcontroller.hxx>
63 #include <vos/mutex.hxx>
64 #include <vcl/svapp.hxx>
65 #ifndef _VCL_MNEMONIC_HXX_
66 #include <vcl/mnemonic.hxx>
67 #endif
68 #include <vcl/toolbox.hxx>
69 #include <vcl/combobox.hxx>
70 #include <tools/urlobj.hxx>
72 using namespace ::com::sun::star;
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::frame::status;
78 using namespace ::com::sun::star::util;
80 namespace framework
83 // ------------------------------------------------------------------
85 // Wrapper class to notify controller about events from combobox.
86 // Unfortunaltly the events are notifed through virtual methods instead
87 // of Listeners.
89 class ComboBoxControl : public ComboBox
91 public:
92 ComboBoxControl( Window* pParent, WinBits nStyle, IComboBoxListener* pComboBoxListener );
93 virtual ~ComboBoxControl();
95 virtual void Select();
96 virtual void DoubleClick();
97 virtual void Modify();
98 virtual void KeyInput( const ::KeyEvent& rKEvt );
99 virtual void GetFocus();
100 virtual void LoseFocus();
101 virtual long PreNotify( NotifyEvent& rNEvt );
103 private:
104 IComboBoxListener* m_pComboBoxListener;
107 ComboBoxControl::ComboBoxControl( Window* pParent, WinBits nStyle, IComboBoxListener* pComboBoxListener ) :
108 ComboBox( pParent, nStyle )
109 , m_pComboBoxListener( pComboBoxListener )
113 ComboBoxControl::~ComboBoxControl()
115 m_pComboBoxListener = 0;
118 void ComboBoxControl::Select()
120 ComboBox::Select();
121 if ( m_pComboBoxListener )
122 m_pComboBoxListener->Select();
125 void ComboBoxControl::DoubleClick()
127 ComboBox::DoubleClick();
128 if ( m_pComboBoxListener )
129 m_pComboBoxListener->DoubleClick();
132 void ComboBoxControl::Modify()
134 ComboBox::Modify();
135 if ( m_pComboBoxListener )
136 m_pComboBoxListener->Modify();
139 void ComboBoxControl::KeyInput( const ::KeyEvent& rKEvt )
141 ComboBox::KeyInput( rKEvt );
142 if ( m_pComboBoxListener )
143 m_pComboBoxListener->KeyInput( rKEvt );
146 void ComboBoxControl::GetFocus()
148 ComboBox::GetFocus();
149 if ( m_pComboBoxListener )
150 m_pComboBoxListener->GetFocus();
153 void ComboBoxControl::LoseFocus()
155 ComboBox::LoseFocus();
156 if ( m_pComboBoxListener )
157 m_pComboBoxListener->LoseFocus();
160 long ComboBoxControl::PreNotify( NotifyEvent& rNEvt )
162 long nRet( 0 );
163 if ( m_pComboBoxListener )
164 nRet = m_pComboBoxListener->PreNotify( rNEvt );
165 if ( nRet == 0 )
166 nRet = ComboBox::PreNotify( rNEvt );
168 return nRet;
171 // ------------------------------------------------------------------
173 ComboboxToolbarController::ComboboxToolbarController(
174 const Reference< XMultiServiceFactory >& rServiceManager,
175 const Reference< XFrame >& rFrame,
176 ToolBox* pToolbar,
177 USHORT nID,
178 sal_Int32 nWidth,
179 const ::rtl::OUString& aCommand ) :
180 ComplexToolbarController( rServiceManager, rFrame, pToolbar, nID, aCommand )
181 , m_pComboBox( 0 )
183 m_pComboBox = new ComboBoxControl( m_pToolbar, WB_DROPDOWN, this );
184 if ( nWidth == 0 )
185 nWidth = 100;
187 // default dropdown size
188 ::Size aLogicalSize( 8, 160 );
189 ::Size aPixelSize = m_pComboBox->LogicToPixel( aLogicalSize, MAP_APPFONT );
191 m_pComboBox->SetSizePixel( ::Size( nWidth, aPixelSize.Height() ));
192 m_pToolbar->SetItemWindow( m_nID, m_pComboBox );
195 // ------------------------------------------------------------------
197 ComboboxToolbarController::~ComboboxToolbarController()
201 // ------------------------------------------------------------------
203 void SAL_CALL ComboboxToolbarController::dispose()
204 throw ( RuntimeException )
206 vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
208 m_pToolbar->SetItemWindow( m_nID, 0 );
209 delete m_pComboBox;
211 ComplexToolbarController::dispose();
213 m_pComboBox = 0;
216 // ------------------------------------------------------------------
217 Sequence<PropertyValue> ComboboxToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
219 Sequence<PropertyValue> aArgs( 2 );
220 ::rtl::OUString aSelectedText = m_pComboBox->GetText();
222 // Add key modifier to argument list
223 aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" ));
224 aArgs[0].Value <<= KeyModifier;
225 aArgs[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
226 aArgs[1].Value <<= aSelectedText;
227 return aArgs;
230 // ------------------------------------------------------------------
232 void ComboboxToolbarController::Select()
234 if ( m_pComboBox->GetEntryCount() > 0 )
236 Window::PointerState aState = m_pComboBox->GetPointerState();
238 sal_uInt16 nKeyModifier = sal_uInt16( aState.mnState & KEY_MODTYPE );
239 execute( nKeyModifier );
243 void ComboboxToolbarController::DoubleClick()
247 void ComboboxToolbarController::Modify()
249 notifyTextChanged( m_pComboBox->GetText() );
252 void ComboboxToolbarController::KeyInput( const ::KeyEvent& )
256 void ComboboxToolbarController::GetFocus()
258 notifyFocusGet();
261 void ComboboxToolbarController::LoseFocus()
263 notifyFocusLost();
266 long ComboboxToolbarController::PreNotify( NotifyEvent& rNEvt )
268 if( rNEvt.GetType() == EVENT_KEYINPUT )
270 const ::KeyEvent* pKeyEvent = rNEvt.GetKeyEvent();
271 const KeyCode& rKeyCode = pKeyEvent->GetKeyCode();
272 if(( rKeyCode.GetModifier() | rKeyCode.GetCode()) == KEY_RETURN )
274 // Call execute only with non-empty text
275 if ( m_pComboBox->GetText().Len() > 0 )
276 execute( rKeyCode.GetModifier() );
277 return 1;
281 return 0;
284 // --------------------------------------------------------
286 void ComboboxToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
288 if ( rControlCommand.Command.equalsAsciiL( "SetText", 7 ))
290 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
292 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
294 rtl::OUString aText;
295 rControlCommand.Arguments[i].Value >>= aText;
296 m_pComboBox->SetText( aText );
298 // send notification
299 notifyTextChanged( aText );
300 break;
304 else if ( rControlCommand.Command.equalsAsciiL( "SetList", 7 ))
306 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
308 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "List", 4 ))
310 Sequence< ::rtl::OUString > aList;
311 m_pComboBox->Clear();
313 rControlCommand.Arguments[i].Value >>= aList;
314 for ( sal_Int32 j = 0; j < aList.getLength(); j++ )
315 m_pComboBox->InsertEntry( aList[j] );
317 // send notification
318 uno::Sequence< beans::NamedValue > aInfo( 1 );
319 aInfo[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "List" ));
320 aInfo[0].Value <<= aList;
321 addNotifyInfo( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "ListChanged" )),
322 getDispatchFromCommand( m_aCommandURL ),
323 aInfo );
325 break;
329 else if ( rControlCommand.Command.equalsAsciiL( "AddEntry", 8 ))
331 sal_uInt16 nPos( COMBOBOX_APPEND );
332 rtl::OUString aText;
333 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
335 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
337 if ( rControlCommand.Arguments[i].Value >>= aText )
338 m_pComboBox->InsertEntry( aText, nPos );
339 break;
343 else if ( rControlCommand.Command.equalsAsciiL( "InsertEntry", 11 ))
345 sal_uInt16 nPos( COMBOBOX_APPEND );
346 rtl::OUString aText;
347 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
349 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
351 sal_Int32 nTmpPos = 0;
352 if ( rControlCommand.Arguments[i].Value >>= nTmpPos )
354 if (( nTmpPos >= 0 ) &&
355 ( nTmpPos < sal_Int32( m_pComboBox->GetEntryCount() )))
356 nPos = sal_uInt16( nTmpPos );
359 else if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
360 rControlCommand.Arguments[i].Value >>= aText;
363 m_pComboBox->InsertEntry( aText, nPos );
365 else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryPos", 14 ))
367 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
369 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
371 sal_Int32 nPos( -1 );
372 if ( rControlCommand.Arguments[i].Value >>= nPos )
374 if ( nPos < sal_Int32( m_pComboBox->GetEntryCount() ))
375 m_pComboBox->RemoveEntry( sal_uInt16( nPos ));
377 break;
381 else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryText", 15 ))
383 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
385 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
387 rtl::OUString aText;
388 if ( rControlCommand.Arguments[i].Value >>= aText )
389 m_pComboBox->RemoveEntry( aText );
390 break;
394 else if ( rControlCommand.Command.equalsAsciiL( "SetDropDownLines", 16 ))
396 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
398 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Lines", 5 ))
400 sal_Int32 nValue( 5 );
401 rControlCommand.Arguments[i].Value >>= nValue;
402 m_pComboBox->SetDropDownLineCount( sal_uInt16( nValue ));
403 break;
407 else if ( rControlCommand.Command.equalsAsciiL( "SetBackgroundColor", 18 ))
409 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
411 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Color", 5 ))
413 com::sun::star::util::Color aColor(0);
414 if ( rControlCommand.Arguments[i].Value >>= aColor )
416 ::Color aBackColor( static_cast< UINT32 >( aColor ));
417 m_pComboBox->SetControlBackground( aBackColor );
419 break;
423 else if ( rControlCommand.Command.equalsAsciiL( "SetTextColor", 12 ))
425 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
427 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Color", 5 ))
429 com::sun::star::util::Color aColor(0);
430 if ( rControlCommand.Arguments[i].Value >>= aColor )
432 ::Color aForeColor( static_cast< UINT32 >( aColor ));
433 m_pComboBox->SetControlForeground( aForeColor );
435 break;
441 } // namespace