1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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
;
49 // Wrapper class to notify controller about events from combobox.
50 // Unfortunaltly the events are notifed through virtual methods instead
53 class ComboBoxControl
: public ComboBox
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
;
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()
83 void ComboBoxControl::dispose()
85 m_pComboBoxListener
= 0;
89 void ComboBoxControl::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()
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
)
134 if ( m_pComboBoxListener
)
135 nRet
= m_pComboBoxListener
->PreNotify( rNEvt
);
137 nRet
= ComboBox::PreNotify( rNEvt
);
142 ComboboxToolbarController::ComboboxToolbarController(
143 const Reference
< XComponentContext
>& rxContext
,
144 const Reference
< XFrame
>& rFrame
,
148 const OUString
& aCommand
) :
149 ComplexToolbarController( rxContext
, rFrame
, pToolbar
, nID
, aCommand
)
152 m_pComboBox
= VclPtr
<ComboBoxControl
>::Create( m_pToolbar
, WB_DROPDOWN
, this );
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
;
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()
221 void ComboboxToolbarController::LoseFocus()
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() );
243 case MouseNotifyEvent::GETFOCUS
:
246 case MouseNotifyEvent::LOSEFOCUS
:
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" )
264 rControlCommand
.Arguments
[i
].Value
>>= aText
;
265 m_pComboBox
->SetText( aText
);
268 notifyTextChanged( aText
);
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
] );
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
),
298 else if ( rControlCommand
.Command
== "AddEntry" )
300 sal_Int32
nPos( COMBOBOX_APPEND
);
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
);
312 else if ( rControlCommand
.Command
== "InsertEntry" )
314 sal_Int32
nPos( COMBOBOX_APPEND
);
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() )))
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
);
350 else if ( rControlCommand
.Command
== "RemoveEntryText" )
352 for ( sal_Int32 i
= 0; i
< rControlCommand
.Arguments
.getLength(); i
++ )
354 if ( rControlCommand
.Arguments
[i
].Name
== "Text")
357 if ( rControlCommand
.Arguments
[i
].Value
>>= aText
)
358 m_pComboBox
->RemoveEntry( aText
);
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
));
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
);
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
);
412 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */