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