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/edittoolbarcontroller.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>
30 #include <svtools/toolboxcontroller.hxx>
31 #include <osl/mutex.hxx>
32 #include <vcl/svapp.hxx>
33 #include <vcl/mnemonic.hxx>
34 #include <vcl/toolbox.hxx>
36 using namespace ::com::sun::star
;
37 using namespace ::com::sun::star::uno
;
38 using namespace ::com::sun::star::beans
;
39 using namespace ::com::sun::star::lang
;
40 using namespace ::com::sun::star::frame
;
41 using namespace ::com::sun::star::frame::status
;
42 using namespace ::com::sun::star::util
;
47 // Wrapper class to notify controller about events from edit.
48 // Unfortunaltly the events are notifed through virtual methods instead
51 class EditControl
: public Edit
54 EditControl( vcl::Window
* pParent
, WinBits nStyle
, IEditListener
* pEditListener
);
55 virtual ~EditControl();
56 virtual void dispose() SAL_OVERRIDE
;
58 virtual void Modify() SAL_OVERRIDE
;
59 virtual void KeyInput( const ::KeyEvent
& rKEvt
) SAL_OVERRIDE
;
60 virtual void GetFocus() SAL_OVERRIDE
;
61 virtual void LoseFocus() SAL_OVERRIDE
;
62 virtual bool PreNotify( NotifyEvent
& rNEvt
) SAL_OVERRIDE
;
65 IEditListener
* m_pEditListener
;
68 EditControl::EditControl( vcl::Window
* pParent
, WinBits nStyle
, IEditListener
* pEditListener
) :
69 Edit( pParent
, nStyle
)
70 , m_pEditListener( pEditListener
)
74 EditControl::~EditControl()
79 void EditControl::dispose()
85 void EditControl::Modify()
88 if ( m_pEditListener
)
89 m_pEditListener
->Modify();
92 void EditControl::KeyInput( const ::KeyEvent
& rKEvt
)
94 Edit::KeyInput( rKEvt
);
95 if ( m_pEditListener
)
96 m_pEditListener
->KeyInput( rKEvt
);
99 void EditControl::GetFocus()
102 if ( m_pEditListener
)
103 m_pEditListener
->GetFocus();
106 void EditControl::LoseFocus()
109 if ( m_pEditListener
)
110 m_pEditListener
->LoseFocus();
113 bool EditControl::PreNotify( NotifyEvent
& rNEvt
)
116 if ( m_pEditListener
)
117 nRet
= m_pEditListener
->PreNotify( rNEvt
);
119 nRet
= Edit::PreNotify( rNEvt
);
124 EditToolbarController::EditToolbarController(
125 const Reference
< XComponentContext
>& rxContext
,
126 const Reference
< XFrame
>& rFrame
,
130 const OUString
& aCommand
) :
131 ComplexToolbarController( rxContext
, rFrame
, pToolbar
, nID
, aCommand
)
132 , m_pEditControl( 0 )
134 m_pEditControl
= VclPtr
<EditControl
>::Create( m_pToolbar
, WB_BORDER
, this );
138 // Calculate height of the edit field according to the application font height
139 sal_Int32 nHeight
= getFontSizePixel( m_pEditControl
) + 6 + 1;
141 m_pEditControl
->SetSizePixel( ::Size( nWidth
, nHeight
));
142 m_pToolbar
->SetItemWindow( m_nID
, m_pEditControl
);
145 EditToolbarController::~EditToolbarController()
149 void SAL_CALL
EditToolbarController::dispose()
150 throw ( RuntimeException
, std::exception
)
152 SolarMutexGuard aSolarMutexGuard
;
154 m_pToolbar
->SetItemWindow( m_nID
, 0 );
155 m_pEditControl
.disposeAndClear();
157 ComplexToolbarController::dispose();
160 Sequence
<PropertyValue
> EditToolbarController::getExecuteArgs(sal_Int16 KeyModifier
) const
162 Sequence
<PropertyValue
> aArgs( 2 );
163 OUString aSelectedText
= m_pEditControl
->GetText();
165 // Add key modifier to argument list
166 aArgs
[0].Name
= "KeyModifier";
167 aArgs
[0].Value
<<= KeyModifier
;
168 aArgs
[1].Name
= "Text";
169 aArgs
[1].Value
<<= aSelectedText
;
173 void EditToolbarController::Modify()
175 notifyTextChanged( m_pEditControl
->GetText() );
178 void EditToolbarController::KeyInput( const ::KeyEvent
& /*rKEvt*/ )
182 void EditToolbarController::GetFocus()
187 void EditToolbarController::LoseFocus()
192 bool EditToolbarController::PreNotify( NotifyEvent
& rNEvt
)
194 if( rNEvt
.GetType() == MouseNotifyEvent::KEYINPUT
)
196 const ::KeyEvent
* pKeyEvent
= rNEvt
.GetKeyEvent();
197 const vcl::KeyCode
& rKeyCode
= pKeyEvent
->GetKeyCode();
198 if(( rKeyCode
.GetModifier() | rKeyCode
.GetCode()) == KEY_RETURN
)
200 // Call execute only with non-empty text
201 if ( !m_pEditControl
->GetText().isEmpty() )
202 execute( rKeyCode
.GetModifier() );
210 void EditToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand
& rControlCommand
)
212 if ( rControlCommand
.Command
.startsWith( "SetText" ))
214 for ( sal_Int32 i
= 0; i
< rControlCommand
.Arguments
.getLength(); i
++ )
216 if ( rControlCommand
.Arguments
[i
].Name
.startsWith( "Text" ))
219 rControlCommand
.Arguments
[i
].Value
>>= aText
;
220 m_pEditControl
->SetText( aText
);
223 notifyTextChanged( aText
);
232 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */