bump product version to 4.1.6.2
[LibreOffice.git] / framework / source / uielement / comboboxtoolbarcontroller.cxx
blobde69836e1b8ff79ed573368b6e5bac5a9224c7fe
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include "uielement/comboboxtoolbarcontroller.hxx"
23 #include <com/sun/star/util/XURLTransformer.hpp>
24 #include <com/sun/star/frame/XDispatchProvider.hpp>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/frame/status/ItemStatus.hpp>
27 #include <com/sun/star/frame/status/ItemState.hpp>
28 #include <com/sun/star/frame/status/Visibility.hpp>
29 #include <com/sun/star/frame/XControlNotificationListener.hpp>
30 #include <com/sun/star/util/Color.hpp>
32 #include <svtools/toolboxcontroller.hxx>
33 #include <osl/mutex.hxx>
34 #include <vcl/svapp.hxx>
35 #include <vcl/mnemonic.hxx>
36 #include <vcl/toolbox.hxx>
37 #include <vcl/combobox.hxx>
39 using namespace ::com::sun::star;
40 using namespace ::com::sun::star::uno;
41 using namespace ::com::sun::star::beans;
42 using namespace ::com::sun::star::lang;
43 using namespace ::com::sun::star::frame;
44 using namespace ::com::sun::star::frame::status;
45 using namespace ::com::sun::star::util;
47 namespace framework
50 // ------------------------------------------------------------------
52 // Wrapper class to notify controller about events from combobox.
53 // Unfortunaltly the events are notifed through virtual methods instead
54 // of Listeners.
56 class ComboBoxControl : public ComboBox
58 public:
59 ComboBoxControl( Window* pParent, WinBits nStyle, IComboBoxListener* pComboBoxListener );
60 virtual ~ComboBoxControl();
62 virtual void Select();
63 virtual void DoubleClick();
64 virtual void Modify();
65 virtual void KeyInput( const ::KeyEvent& rKEvt );
66 virtual void GetFocus();
67 virtual void LoseFocus();
68 virtual long PreNotify( NotifyEvent& rNEvt );
70 private:
71 IComboBoxListener* m_pComboBoxListener;
74 ComboBoxControl::ComboBoxControl( Window* pParent, WinBits nStyle, IComboBoxListener* pComboBoxListener ) :
75 ComboBox( pParent, nStyle )
76 , m_pComboBoxListener( pComboBoxListener )
80 ComboBoxControl::~ComboBoxControl()
82 m_pComboBoxListener = 0;
85 void ComboBoxControl::Select()
87 ComboBox::Select();
88 if ( m_pComboBoxListener )
89 m_pComboBoxListener->Select();
92 void ComboBoxControl::DoubleClick()
94 ComboBox::DoubleClick();
95 if ( m_pComboBoxListener )
96 m_pComboBoxListener->DoubleClick();
99 void ComboBoxControl::Modify()
101 ComboBox::Modify();
102 if ( m_pComboBoxListener )
103 m_pComboBoxListener->Modify();
106 void ComboBoxControl::KeyInput( const ::KeyEvent& rKEvt )
108 ComboBox::KeyInput( rKEvt );
109 if ( m_pComboBoxListener )
110 m_pComboBoxListener->KeyInput( rKEvt );
113 void ComboBoxControl::GetFocus()
115 ComboBox::GetFocus();
116 if ( m_pComboBoxListener )
117 m_pComboBoxListener->GetFocus();
120 void ComboBoxControl::LoseFocus()
122 ComboBox::LoseFocus();
123 if ( m_pComboBoxListener )
124 m_pComboBoxListener->LoseFocus();
127 long ComboBoxControl::PreNotify( NotifyEvent& rNEvt )
129 long nRet( 0 );
130 if ( m_pComboBoxListener )
131 nRet = m_pComboBoxListener->PreNotify( rNEvt );
132 if ( nRet == 0 )
133 nRet = ComboBox::PreNotify( rNEvt );
135 return nRet;
138 // ------------------------------------------------------------------
140 ComboboxToolbarController::ComboboxToolbarController(
141 const Reference< XComponentContext >& rxContext,
142 const Reference< XFrame >& rFrame,
143 ToolBox* pToolbar,
144 sal_uInt16 nID,
145 sal_Int32 nWidth,
146 const OUString& aCommand ) :
147 ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
148 , m_pComboBox( 0 )
150 m_pComboBox = new ComboBoxControl( m_pToolbar, WB_DROPDOWN, this );
151 if ( nWidth == 0 )
152 nWidth = 100;
154 // default dropdown size
155 ::Size aLogicalSize( 8, 160 );
156 ::Size aPixelSize = m_pComboBox->LogicToPixel( aLogicalSize, MAP_APPFONT );
158 m_pComboBox->SetSizePixel( ::Size( nWidth, aPixelSize.Height() ));
159 m_pToolbar->SetItemWindow( m_nID, m_pComboBox );
162 // ------------------------------------------------------------------
164 ComboboxToolbarController::~ComboboxToolbarController()
168 // ------------------------------------------------------------------
170 void SAL_CALL ComboboxToolbarController::dispose()
171 throw ( RuntimeException )
173 SolarMutexGuard aSolarMutexGuard;
175 m_pToolbar->SetItemWindow( m_nID, 0 );
176 delete m_pComboBox;
178 ComplexToolbarController::dispose();
180 m_pComboBox = 0;
183 // ------------------------------------------------------------------
184 Sequence<PropertyValue> ComboboxToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
186 Sequence<PropertyValue> aArgs( 2 );
187 OUString aSelectedText = m_pComboBox->GetText();
189 // Add key modifier to argument list
190 aArgs[0].Name = OUString( "KeyModifier" );
191 aArgs[0].Value <<= KeyModifier;
192 aArgs[1].Name = OUString( "Text" );
193 aArgs[1].Value <<= aSelectedText;
194 return aArgs;
197 // ------------------------------------------------------------------
199 void ComboboxToolbarController::Select()
201 if ( m_pComboBox->GetEntryCount() > 0 )
203 Window::PointerState aState = m_pComboBox->GetPointerState();
205 sal_uInt16 nKeyModifier = sal_uInt16( aState.mnState & KEY_MODTYPE );
206 execute( nKeyModifier );
210 void ComboboxToolbarController::DoubleClick()
214 void ComboboxToolbarController::Modify()
216 notifyTextChanged( m_pComboBox->GetText() );
219 void ComboboxToolbarController::KeyInput( const ::KeyEvent& )
223 void ComboboxToolbarController::GetFocus()
225 notifyFocusGet();
228 void ComboboxToolbarController::LoseFocus()
230 notifyFocusLost();
233 long ComboboxToolbarController::PreNotify( NotifyEvent& rNEvt )
235 switch ( rNEvt.GetType() )
237 case EVENT_KEYINPUT :
239 const ::KeyEvent* pKeyEvent = rNEvt.GetKeyEvent();
240 const KeyCode& rKeyCode = pKeyEvent->GetKeyCode();
241 if(( rKeyCode.GetModifier() | rKeyCode.GetCode()) == KEY_RETURN )
243 // Call execute only with non-empty text
244 if ( !m_pComboBox->GetText().isEmpty() )
245 execute( rKeyCode.GetModifier() );
246 return 1;
249 break;
250 case EVENT_GETFOCUS :
251 notifyFocusGet();
252 break;
253 case EVENT_LOSEFOCUS :
254 notifyFocusLost();
255 break;
256 default :
257 break;
259 return 0;
262 // --------------------------------------------------------
264 void ComboboxToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
266 if ( rControlCommand.Command.equalsAsciiL( "SetText", 7 ))
268 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
270 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
272 OUString aText;
273 rControlCommand.Arguments[i].Value >>= aText;
274 m_pComboBox->SetText( aText );
276 // send notification
277 notifyTextChanged( aText );
278 break;
282 else if ( rControlCommand.Command.equalsAsciiL( "SetList", 7 ))
284 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
286 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "List", 4 ))
288 Sequence< OUString > aList;
289 m_pComboBox->Clear();
291 rControlCommand.Arguments[i].Value >>= aList;
292 for ( sal_Int32 j = 0; j < aList.getLength(); j++ )
293 m_pComboBox->InsertEntry( aList[j] );
295 // send notification
296 uno::Sequence< beans::NamedValue > aInfo( 1 );
297 aInfo[0].Name = OUString( "List" );
298 aInfo[0].Value <<= aList;
299 addNotifyInfo( OUString( "ListChanged" ),
300 getDispatchFromCommand( m_aCommandURL ),
301 aInfo );
303 break;
307 else if ( rControlCommand.Command.equalsAsciiL( "AddEntry", 8 ))
309 sal_uInt16 nPos( COMBOBOX_APPEND );
310 OUString aText;
311 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
313 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
315 if ( rControlCommand.Arguments[i].Value >>= aText )
316 m_pComboBox->InsertEntry( aText, nPos );
317 break;
321 else if ( rControlCommand.Command.equalsAsciiL( "InsertEntry", 11 ))
323 sal_uInt16 nPos( COMBOBOX_APPEND );
324 OUString aText;
325 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
327 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
329 sal_Int32 nTmpPos = 0;
330 if ( rControlCommand.Arguments[i].Value >>= nTmpPos )
332 if (( nTmpPos >= 0 ) &&
333 ( nTmpPos < sal_Int32( m_pComboBox->GetEntryCount() )))
334 nPos = sal_uInt16( nTmpPos );
337 else if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
338 rControlCommand.Arguments[i].Value >>= aText;
341 m_pComboBox->InsertEntry( aText, nPos );
343 else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryPos", 14 ))
345 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
347 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Pos", 3 ))
349 sal_Int32 nPos( -1 );
350 if ( rControlCommand.Arguments[i].Value >>= nPos )
352 if ( nPos < sal_Int32( m_pComboBox->GetEntryCount() ))
353 m_pComboBox->RemoveEntry( sal_uInt16( nPos ));
355 break;
359 else if ( rControlCommand.Command.equalsAsciiL( "RemoveEntryText", 15 ))
361 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
363 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
365 OUString aText;
366 if ( rControlCommand.Arguments[i].Value >>= aText )
367 m_pComboBox->RemoveEntry( aText );
368 break;
372 else if ( rControlCommand.Command.equalsAsciiL( "SetDropDownLines", 16 ))
374 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
376 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Lines", 5 ))
378 sal_Int32 nValue( 5 );
379 rControlCommand.Arguments[i].Value >>= nValue;
380 m_pComboBox->SetDropDownLineCount( sal_uInt16( nValue ));
381 break;
385 else if ( rControlCommand.Command.equalsAsciiL( "SetBackgroundColor", 18 ))
387 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
389 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Color", 5 ))
391 com::sun::star::util::Color aColor(0);
392 if ( rControlCommand.Arguments[i].Value >>= aColor )
394 ::Color aBackColor( static_cast< sal_uInt32 >( aColor ));
395 m_pComboBox->SetControlBackground( aBackColor );
397 break;
401 else if ( rControlCommand.Command.equalsAsciiL( "SetTextColor", 12 ))
403 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
405 if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Color", 5 ))
407 com::sun::star::util::Color aColor(0);
408 if ( rControlCommand.Arguments[i].Value >>= aColor )
410 ::Color aForeColor( static_cast< sal_uInt32 >( aColor ));
411 m_pComboBox->SetControlForeground( aForeColor );
413 break;
419 } // namespace
421 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */