fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / framework / source / uielement / comboboxtoolbarcontroller.cxx
blob15fb15cfd3a9592a740d46cf6cc2711c84743396
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 .
20 #include "uielement/comboboxtoolbarcontroller.hxx"
22 #include <com/sun/star/util/XURLTransformer.hpp>
23 #include <com/sun/star/frame/XDispatchProvider.hpp>
24 #include <com/sun/star/beans/PropertyValue.hpp>
25 #include <com/sun/star/frame/status/ItemStatus.hpp>
26 #include <com/sun/star/frame/status/ItemState.hpp>
27 #include <com/sun/star/frame/status/Visibility.hpp>
28 #include <com/sun/star/frame/XControlNotificationListener.hpp>
29 #include <com/sun/star/util/Color.hpp>
31 #include <svtools/toolboxcontroller.hxx>
32 #include <osl/mutex.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/mnemonic.hxx>
35 #include <vcl/toolbox.hxx>
36 #include <vcl/combobox.hxx>
38 using namespace ::com::sun::star;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::beans;
41 using namespace ::com::sun::star::lang;
42 using namespace ::com::sun::star::frame;
43 using namespace ::com::sun::star::frame::status;
44 using namespace ::com::sun::star::util;
46 namespace framework
49 // Wrapper class to notify controller about events from combobox.
50 // Unfortunaltly the events are notifed through virtual methods instead
51 // of Listeners.
53 class ComboBoxControl : public ComboBox
55 public:
56 ComboBoxControl( vcl::Window* pParent, WinBits nStyle, IComboBoxListener* pComboBoxListener );
57 virtual ~ComboBoxControl();
58 virtual void dispose() SAL_OVERRIDE;
60 virtual void Select() SAL_OVERRIDE;
61 virtual void DoubleClick() SAL_OVERRIDE;
62 virtual void Modify() SAL_OVERRIDE;
63 virtual void KeyInput( const ::KeyEvent& rKEvt ) SAL_OVERRIDE;
64 virtual void GetFocus() SAL_OVERRIDE;
65 virtual void LoseFocus() SAL_OVERRIDE;
66 virtual bool PreNotify( NotifyEvent& rNEvt ) SAL_OVERRIDE;
68 private:
69 IComboBoxListener* m_pComboBoxListener;
72 ComboBoxControl::ComboBoxControl( vcl::Window* pParent, WinBits nStyle, IComboBoxListener* pComboBoxListener ) :
73 ComboBox( pParent, nStyle )
74 , m_pComboBoxListener( pComboBoxListener )
78 ComboBoxControl::~ComboBoxControl()
80 disposeOnce();
83 void ComboBoxControl::dispose()
85 m_pComboBoxListener = 0;
86 ComboBox::dispose();
89 void ComboBoxControl::Select()
91 ComboBox::Select();
92 if ( m_pComboBoxListener )
93 m_pComboBoxListener->Select();
96 void ComboBoxControl::DoubleClick()
98 ComboBox::DoubleClick();
99 if ( m_pComboBoxListener )
100 m_pComboBoxListener->DoubleClick();
103 void ComboBoxControl::Modify()
105 ComboBox::Modify();
106 if ( m_pComboBoxListener )
107 m_pComboBoxListener->Modify();
110 void ComboBoxControl::KeyInput( const ::KeyEvent& rKEvt )
112 ComboBox::KeyInput( rKEvt );
113 if ( m_pComboBoxListener )
114 m_pComboBoxListener->KeyInput( rKEvt );
117 void ComboBoxControl::GetFocus()
119 ComboBox::GetFocus();
120 if ( m_pComboBoxListener )
121 m_pComboBoxListener->GetFocus();
124 void ComboBoxControl::LoseFocus()
126 ComboBox::LoseFocus();
127 if ( m_pComboBoxListener )
128 m_pComboBoxListener->LoseFocus();
131 bool ComboBoxControl::PreNotify( NotifyEvent& rNEvt )
133 bool nRet = false;
134 if ( m_pComboBoxListener )
135 nRet = m_pComboBoxListener->PreNotify( rNEvt );
136 if ( !nRet )
137 nRet = ComboBox::PreNotify( rNEvt );
139 return nRet;
142 ComboboxToolbarController::ComboboxToolbarController(
143 const Reference< XComponentContext >& rxContext,
144 const Reference< XFrame >& rFrame,
145 ToolBox* pToolbar,
146 sal_uInt16 nID,
147 sal_Int32 nWidth,
148 const OUString& aCommand ) :
149 ComplexToolbarController( rxContext, rFrame, pToolbar, nID, aCommand )
150 , m_pComboBox( 0 )
152 m_pComboBox = VclPtr<ComboBoxControl>::Create( m_pToolbar, WB_DROPDOWN, this );
153 if ( nWidth == 0 )
154 nWidth = 100;
156 // default dropdown size
157 ::Size aLogicalSize( 8, 160 );
158 ::Size aPixelSize = m_pComboBox->LogicToPixel( aLogicalSize, MAP_APPFONT );
160 m_pComboBox->SetSizePixel( ::Size( nWidth, aPixelSize.Height() ));
161 m_pToolbar->SetItemWindow( m_nID, m_pComboBox );
164 ComboboxToolbarController::~ComboboxToolbarController()
168 void SAL_CALL ComboboxToolbarController::dispose()
169 throw ( RuntimeException, std::exception )
171 SolarMutexGuard aSolarMutexGuard;
173 m_pToolbar->SetItemWindow( m_nID, 0 );
174 m_pComboBox.disposeAndClear();
176 ComplexToolbarController::dispose();
179 Sequence<PropertyValue> ComboboxToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
181 Sequence<PropertyValue> aArgs( 2 );
182 OUString aSelectedText = m_pComboBox->GetText();
184 // Add key modifier to argument list
185 aArgs[0].Name = "KeyModifier";
186 aArgs[0].Value <<= KeyModifier;
187 aArgs[1].Name = "Text";
188 aArgs[1].Value <<= aSelectedText;
189 return aArgs;
192 void ComboboxToolbarController::Select()
194 if ( m_pComboBox->GetEntryCount() > 0 )
196 vcl::Window::PointerState aState = m_pComboBox->GetPointerState();
198 sal_uInt16 nKeyModifier = sal_uInt16( aState.mnState & KEY_MODIFIERS_MASK );
199 execute( nKeyModifier );
203 void ComboboxToolbarController::DoubleClick()
207 void ComboboxToolbarController::Modify()
209 notifyTextChanged( m_pComboBox->GetText() );
212 void ComboboxToolbarController::KeyInput( const ::KeyEvent& )
216 void ComboboxToolbarController::GetFocus()
218 notifyFocusGet();
221 void ComboboxToolbarController::LoseFocus()
223 notifyFocusLost();
226 bool ComboboxToolbarController::PreNotify( NotifyEvent& rNEvt )
228 switch ( rNEvt.GetType() )
230 case MouseNotifyEvent::KEYINPUT :
232 const ::KeyEvent* pKeyEvent = rNEvt.GetKeyEvent();
233 const vcl::KeyCode& rKeyCode = pKeyEvent->GetKeyCode();
234 if(( rKeyCode.GetModifier() | rKeyCode.GetCode()) == KEY_RETURN )
236 // Call execute only with non-empty text
237 if ( !m_pComboBox->GetText().isEmpty() )
238 execute( rKeyCode.GetModifier() );
239 return true;
242 break;
243 case MouseNotifyEvent::GETFOCUS :
244 notifyFocusGet();
245 break;
246 case MouseNotifyEvent::LOSEFOCUS :
247 notifyFocusLost();
248 break;
249 default :
250 break;
252 return false;
255 void ComboboxToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
257 if ( rControlCommand.Command == "SetText" )
259 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
261 if ( rControlCommand.Arguments[i].Name == "Text" )
263 OUString aText;
264 rControlCommand.Arguments[i].Value >>= aText;
265 m_pComboBox->SetText( aText );
267 // send notification
268 notifyTextChanged( aText );
269 break;
273 else if ( rControlCommand.Command == "SetList" )
275 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
277 if ( rControlCommand.Arguments[i].Name == "List" )
279 Sequence< OUString > aList;
280 m_pComboBox->Clear();
282 rControlCommand.Arguments[i].Value >>= aList;
283 for ( sal_Int32 j = 0; j < aList.getLength(); j++ )
284 m_pComboBox->InsertEntry( aList[j] );
286 // send notification
287 uno::Sequence< beans::NamedValue > aInfo( 1 );
288 aInfo[0].Name = "List";
289 aInfo[0].Value <<= aList;
290 addNotifyInfo( OUString( "ListChanged" ),
291 getDispatchFromCommand( m_aCommandURL ),
292 aInfo );
294 break;
298 else if ( rControlCommand.Command == "AddEntry" )
300 sal_Int32 nPos( COMBOBOX_APPEND );
301 OUString aText;
302 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
304 if ( rControlCommand.Arguments[i].Name == "Text" )
306 if ( rControlCommand.Arguments[i].Value >>= aText )
307 m_pComboBox->InsertEntry( aText, nPos );
308 break;
312 else if ( rControlCommand.Command == "InsertEntry" )
314 sal_Int32 nPos( COMBOBOX_APPEND );
315 OUString aText;
316 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
318 if ( rControlCommand.Arguments[i].Name == "Pos" )
320 sal_Int32 nTmpPos = 0;
321 if ( rControlCommand.Arguments[i].Value >>= nTmpPos )
323 if (( nTmpPos >= 0 ) &&
324 ( nTmpPos < sal_Int32( m_pComboBox->GetEntryCount() )))
325 nPos = nTmpPos;
328 else if ( rControlCommand.Arguments[i].Name == "Text" )
329 rControlCommand.Arguments[i].Value >>= aText;
332 m_pComboBox->InsertEntry( aText, nPos );
334 else if ( rControlCommand.Command == "RemoveEntryPos" )
336 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
338 if ( rControlCommand.Arguments[i].Name == "Pos" )
340 sal_Int32 nPos( -1 );
341 if ( rControlCommand.Arguments[i].Value >>= nPos )
343 if ( 0 <= nPos && nPos < sal_Int32( m_pComboBox->GetEntryCount() ))
344 m_pComboBox->RemoveEntryAt(nPos);
346 break;
350 else if ( rControlCommand.Command == "RemoveEntryText" )
352 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
354 if ( rControlCommand.Arguments[i].Name == "Text")
356 OUString aText;
357 if ( rControlCommand.Arguments[i].Value >>= aText )
358 m_pComboBox->RemoveEntry( aText );
359 break;
363 else if ( rControlCommand.Command == "SetDropDownLines" )
365 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
367 if ( rControlCommand.Arguments[i].Name == "Lines" )
369 sal_Int32 nValue( 5 );
370 rControlCommand.Arguments[i].Value >>= nValue;
371 m_pComboBox->SetDropDownLineCount( sal_uInt16( nValue ));
372 break;
376 else if ( rControlCommand.Command == "SetBackgroundColor" )
378 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
380 if ( rControlCommand.Arguments[i].Name == "Color" )
382 com::sun::star::util::Color aColor(0);
383 if ( rControlCommand.Arguments[i].Value >>= aColor )
385 ::Color aBackColor( static_cast< sal_uInt32 >( aColor ));
386 m_pComboBox->SetControlBackground( aBackColor );
388 break;
392 else if ( rControlCommand.Command == "SetTextColor" )
394 for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
396 if ( rControlCommand.Arguments[i].Name == "Color" )
398 com::sun::star::util::Color aColor(0);
399 if ( rControlCommand.Arguments[i].Value >>= aColor )
401 ::Color aForeColor( static_cast< sal_uInt32 >( aColor ));
402 m_pComboBox->SetControlForeground( aForeColor );
404 break;
410 } // namespace
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */