1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_framework.hxx"
26 
27 #ifndef __FRAMEWORK_UIELEMENT_EDITTOOLBARCONTROLLER_HXX
28 #include "uielement/edittoolbarcontroller.hxx"
29 #endif
30 
31 //_________________________________________________________________________________________________________________
32 //	my own includes
33 //_________________________________________________________________________________________________________________
34 
35 #ifndef __FRAMEWORK_TOOLBAR_HXX_
36 #include "uielement/toolbar.hxx"
37 #endif
38 
39 //_________________________________________________________________________________________________________________
40 //	interface includes
41 //_________________________________________________________________________________________________________________
42 #include <com/sun/star/util/XURLTransformer.hpp>
43 #include <com/sun/star/frame/XDispatchProvider.hpp>
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 #include <com/sun/star/frame/status/ItemStatus.hpp>
46 #include <com/sun/star/frame/status/ItemState.hpp>
47 #include <com/sun/star/frame/status/Visibility.hpp>
48 #include <com/sun/star/frame/XControlNotificationListener.hpp>
49 
50 //_________________________________________________________________________________________________________________
51 //	other includes
52 //_________________________________________________________________________________________________________________
53 #include <svtools/toolboxcontroller.hxx>
54 #include <vos/mutex.hxx>
55 #include <vcl/svapp.hxx>
56 #ifndef _VCL_MNEMONIC_HXX_
57 #include <vcl/mnemonic.hxx>
58 #endif
59 #include <tools/urlobj.hxx>
60 
61 using namespace ::com::sun::star;
62 using namespace ::com::sun::star::uno;
63 using namespace ::com::sun::star::beans;
64 using namespace ::com::sun::star::lang;
65 using namespace ::com::sun::star::frame;
66 using namespace ::com::sun::star::frame::status;
67 using namespace ::com::sun::star::util;
68 
69 namespace framework
70 {
71 
72 // ------------------------------------------------------------------
73 
74 // Wrapper class to notify controller about events from edit.
75 // Unfortunaltly the events are notifed through virtual methods instead
76 // of Listeners.
77 
78 class EditControl : public Edit
79 {
80     public:
81         EditControl( Window* pParent, WinBits nStyle, IEditListener* pEditListener );
82         virtual ~EditControl();
83 
84         virtual void Modify();
85         virtual void KeyInput( const ::KeyEvent& rKEvt );
86 	    virtual void GetFocus();
87 	    virtual void LoseFocus();
88         virtual long PreNotify( NotifyEvent& rNEvt );
89 
90     private:
91         IEditListener* m_pEditListener;
92 };
93 
EditControl(Window * pParent,WinBits nStyle,IEditListener * pEditListener)94 EditControl::EditControl( Window* pParent, WinBits nStyle, IEditListener* pEditListener ) :
95     Edit( pParent, nStyle )
96     , m_pEditListener( pEditListener )
97 {
98 }
99 
~EditControl()100 EditControl::~EditControl()
101 {
102     m_pEditListener = 0;
103 }
104 
Modify()105 void EditControl::Modify()
106 {
107     Edit::Modify();
108     if ( m_pEditListener )
109         m_pEditListener->Modify();
110 }
111 
KeyInput(const::KeyEvent & rKEvt)112 void EditControl::KeyInput( const ::KeyEvent& rKEvt )
113 {
114     Edit::KeyInput( rKEvt );
115     if ( m_pEditListener )
116         m_pEditListener->KeyInput( rKEvt );
117 }
118 
GetFocus()119 void EditControl::GetFocus()
120 {
121     Edit::GetFocus();
122     if ( m_pEditListener )
123         m_pEditListener->GetFocus();
124 }
125 
LoseFocus()126 void EditControl::LoseFocus()
127 {
128     Edit::LoseFocus();
129     if ( m_pEditListener )
130         m_pEditListener->LoseFocus();
131 }
132 
PreNotify(NotifyEvent & rNEvt)133 long EditControl::PreNotify( NotifyEvent& rNEvt )
134 {
135     long nRet( 0 );
136     if ( m_pEditListener )
137         nRet = m_pEditListener->PreNotify( rNEvt );
138     if ( nRet == 0 )
139         nRet = Edit::PreNotify( rNEvt );
140 
141     return nRet;
142 }
143 
144 // ------------------------------------------------------------------
145 
EditToolbarController(const Reference<XMultiServiceFactory> & rServiceManager,const Reference<XFrame> & rFrame,ToolBox * pToolbar,sal_uInt16 nID,sal_Int32 nWidth,const::rtl::OUString & aCommand)146 EditToolbarController::EditToolbarController(
147     const Reference< XMultiServiceFactory >& rServiceManager,
148     const Reference< XFrame >&               rFrame,
149     ToolBox*                                 pToolbar,
150     sal_uInt16                                   nID,
151     sal_Int32                                nWidth,
152     const ::rtl::OUString&                          aCommand ) :
153     ComplexToolbarController( rServiceManager, rFrame, pToolbar, nID, aCommand )
154     ,   m_pEditControl( 0 )
155 {
156     m_pEditControl = new EditControl( m_pToolbar, WB_BORDER, this );
157     if ( nWidth == 0 )
158         nWidth = 100;
159 
160     // Calculate height of the edit field according to the application font height
161     sal_Int32 nHeight = getFontSizePixel( m_pEditControl ) + 6 + 1;
162 
163     m_pEditControl->SetSizePixel( ::Size( nWidth, nHeight ));
164     m_pToolbar->SetItemWindow( m_nID, m_pEditControl );
165 }
166 
167 // ------------------------------------------------------------------
168 
~EditToolbarController()169 EditToolbarController::~EditToolbarController()
170 {
171 }
172 
173 // ------------------------------------------------------------------
174 
dispose()175 void SAL_CALL EditToolbarController::dispose()
176 throw ( RuntimeException )
177 {
178     vos::OGuard aSolarMutexGuard( Application::GetSolarMutex() );
179 
180     m_pToolbar->SetItemWindow( m_nID, 0 );
181     delete m_pEditControl;
182 
183     ComplexToolbarController::dispose();
184 
185     m_pEditControl = 0;
186 }
187 
188 // ------------------------------------------------------------------
getExecuteArgs(sal_Int16 KeyModifier) const189 Sequence<PropertyValue> EditToolbarController::getExecuteArgs(sal_Int16 KeyModifier) const
190 {
191     Sequence<PropertyValue> aArgs( 2 );
192     ::rtl::OUString aSelectedText = m_pEditControl->GetText();
193 
194     // Add key modifier to argument list
195     aArgs[0].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "KeyModifier" ));
196     aArgs[0].Value <<= KeyModifier;
197     aArgs[1].Name = rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Text" ));
198     aArgs[1].Value <<= aSelectedText;
199     return aArgs;
200 }
201 
202 // ------------------------------------------------------------------
203 
Modify()204 void EditToolbarController::Modify()
205 {
206     notifyTextChanged( m_pEditControl->GetText() );
207 }
208 
KeyInput(const::KeyEvent &)209 void EditToolbarController::KeyInput( const ::KeyEvent& /*rKEvt*/ )
210 {
211 }
212 
GetFocus()213 void EditToolbarController::GetFocus()
214 {
215     notifyFocusGet();
216 }
217 
LoseFocus()218 void EditToolbarController::LoseFocus()
219 {
220     notifyFocusLost();
221 }
222 
PreNotify(NotifyEvent & rNEvt)223 long EditToolbarController::PreNotify( NotifyEvent& rNEvt )
224 {
225     if( rNEvt.GetType() == EVENT_KEYINPUT )
226     {
227         const ::KeyEvent* pKeyEvent = rNEvt.GetKeyEvent();
228         const KeyCode& rKeyCode = pKeyEvent->GetKeyCode();
229         if(( rKeyCode.GetModifier() | rKeyCode.GetCode()) == KEY_RETURN )
230         {
231             // Call execute only with non-empty text
232             if ( m_pEditControl->GetText().Len() > 0 )
233                 execute( rKeyCode.GetModifier() );
234             return 1;
235         }
236     }
237 
238     return 0;
239 }
240 
241 // --------------------------------------------------------
242 
executeControlCommand(const::com::sun::star::frame::ControlCommand & rControlCommand)243 void EditToolbarController::executeControlCommand( const ::com::sun::star::frame::ControlCommand& rControlCommand )
244 {
245     if ( rControlCommand.Command.equalsAsciiL( "SetText", 7 ))
246     {
247         for ( sal_Int32 i = 0; i < rControlCommand.Arguments.getLength(); i++ )
248         {
249             if ( rControlCommand.Arguments[i].Name.equalsAsciiL( "Text", 4 ))
250             {
251                 rtl::OUString aText;
252                 rControlCommand.Arguments[i].Value >>= aText;
253                 m_pEditControl->SetText( aText );
254 
255                 // send notification
256                 notifyTextChanged( aText );
257                 break;
258             }
259         }
260     }
261 }
262 
263 } // namespace
264 
265