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