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 #ifndef SVX_SOURCE_INC_FMTEXTCONTROLSHELL_HXX
28 #define SVX_SOURCE_INC_FMTEXTCONTROLSHELL_HXX
29 
30 /** === begin UNO includes === **/
31 #include <com/sun/star/frame/XDispatchProvider.hpp>
32 #include <com/sun/star/awt/XTextComponent.hpp>
33 #include <com/sun/star/frame/XStatusListener.hpp>
34 #include <com/sun/star/awt/XFocusListener.hpp>
35 #include <com/sun/star/awt/XMouseListener.hpp>
36 #include <com/sun/star/form/runtime/XFormController.hpp>
37 #include <com/sun/star/awt/XControl.hpp>
38 #include <com/sun/star/util/XURLTransformer.hpp>
39 /** === end UNO includes === **/
40 #include <comphelper/implementationreference.hxx>
41 #include <tools/link.hxx>
42 #include <vcl/timer.hxx>
43 #include "fmslotinvalidator.hxx"
44 
45 #include <vector>
46 #include <map>
47 
48 class SfxRequest;
49 class SfxItemSet;
50 class SfxAllItemSet;
51 class SfxBindings;
52 class SfxViewFrame;
53 class Window;
54 class SfxApplication;
55 
56 //........................................................................
57 namespace svx
58 {
59 //........................................................................
60 
61     class FmFocusListenerAdapter;
62     class FmTextControlFeature;
63     class FmMouseListenerAdapter;
64 
65     //====================================================================
66 	//= IFocusObserver
67 	//====================================================================
68     class IFocusObserver
69     {
70     public:
71         virtual void    focusGained( const ::com::sun::star::awt::FocusEvent& _rEvent ) = 0;
72         virtual void    focusLost( const ::com::sun::star::awt::FocusEvent& _rEvent ) = 0;
73     };
74 
75     //====================================================================
76 	//= IFocusObserver
77 	//====================================================================
78     class IContextRequestObserver
79     {
80     public:
81         virtual void    contextMenuRequested( const ::com::sun::star::awt::MouseEvent& _rEvent ) = 0;
82     };
83 
84 	//====================================================================
85 	//= FmTextControlShell
86 	//====================================================================
87     class FmTextControlShell :public IFocusObserver
88                              ,public ISlotInvalidator
89                              ,public IContextRequestObserver
90 	{
91     private:
92         ::com::sun::star::uno::Reference< ::com::sun::star::util::XURLTransformer >             m_xURLTransformer;
93         ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl >                     m_xActiveControl;
94         ::com::sun::star::uno::Reference< ::com::sun::star::awt::XTextComponent >               m_xActiveTextComponent;
95         ::com::sun::star::uno::Reference< ::com::sun::star::form::runtime::XFormController >    m_xActiveController;
96 #ifndef DONT_REMEMBER_LAST_CONTROL
97         // without this define, m_xActiveControl remembers the *last* active control, even
98         // if it, in the meantime, already lost the focus
99         bool                                                                        m_bActiveControl;
100             // so we need an additional boolean flag telling whether the active cotrol
101             // is really focused
102 #endif
103         bool                                                                        m_bActiveControlIsReadOnly;
104         bool                                                                        m_bActiveControlIsRichText;
105 
106         // listening at all controls of the active controller for focus changes
107         typedef ::comphelper::ImplementationReference< FmFocusListenerAdapter, ::com::sun::star::awt::XFocusListener >
108                                                                                     FocusListenerAdapter;
109         typedef ::std::vector< FocusListenerAdapter >                               FocusListenerAdapters;
110         FocusListenerAdapters                                                       m_aControlObservers;
111 
112         typedef ::comphelper::ImplementationReference< FmMouseListenerAdapter, ::com::sun::star::awt::XMouseListener >
113                                                                                     MouseListenerAdapter;
114         MouseListenerAdapter                                                        m_aContextMenuObserver;
115 
116         // translating between "slots" of the framework and "features" of the active control
117         typedef ::comphelper::ImplementationReference< FmTextControlFeature, ::com::sun::star::frame::XStatusListener >
118                                                                                     ControlFeature;
119         typedef ::std::map< SfxSlotId, ControlFeature, ::std::less< SfxSlotId > >   ControlFeatures;
120         ControlFeatures                                                             m_aControlFeatures;
121 
122         SfxViewFrame*                                                               m_pViewFrame;
123         // invalidating slots
124         SfxBindings&                                                                m_rBindings;
125         Link                                                                        m_aControlActivationHandler;
126         AutoTimer                                                                   m_aClipboardInvalidation;
127         bool                                                                        m_bNeedClipboardInvalidation;
128 
129     public:
130         FmTextControlShell( SfxViewFrame* _pFrame );
131         virtual ~FmTextControlShell();
132 
133         // clean up any resources associated with this instance
134         void    dispose();
135 
136         void    ExecuteTextAttribute( SfxRequest& _rReq );
137         void    GetTextAttributeState( SfxItemSet& _rSet );
138         bool    IsActiveControl( bool _bCountRichTextOnly = false ) const;
139         void    ForgetActiveControl();
140         void    SetControlActivationHandler( const Link& _rHdl ) { m_aControlActivationHandler = _rHdl; }
141 
142         /** to be called when a form in our document has been activated
143         */
144         void    formActivated( const ::com::sun::star::uno::Reference< ::com::sun::star::form::runtime::XFormController >& _rxController );
145         /** to be called when a form in our document has been deactivated
146         */
147         void    formDeactivated( const ::com::sun::star::uno::Reference< ::com::sun::star::form::runtime::XFormController >& _rxController );
148 
149         /** notifies the instance that the design mode has changed
150         */
151         void    designModeChanged( bool _bNewDesignMode );
152 
153     protected:
154         // IFocusObserver
155         virtual void    focusGained( const ::com::sun::star::awt::FocusEvent& _rEvent );
156         virtual void    focusLost( const ::com::sun::star::awt::FocusEvent& _rEvent );
157 
158         // IContextRequestObserver
159         virtual void    contextMenuRequested( const ::com::sun::star::awt::MouseEvent& _rEvent );
160 
161         // ISlotInvalidator
162         virtual void    Invalidate( SfxSlotId _nSlot );
163 
164     protected:
165         enum AttributeSet { eCharAttribs, eParaAttribs };
166         void    executeAttributeDialog( AttributeSet _eSet, SfxRequest& _rReq );
167         bool    executeSelectAll( );
168         bool    executeClipboardSlot( SfxSlotId _nSlot );
169 
170     private:
171         inline  bool    isControllerListening() const { return !m_aControlObservers.empty(); }
172 
173         FmTextControlFeature*
174                         implGetFeatureDispatcher(
175                             const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XDispatchProvider >& _rxProvider,
176                             SfxApplication* _pApplication,
177                             SfxSlotId _nSlot
178                         );
179 
180         // fills the given structure with dispatchers for the given slots, for the given control
181         void            fillFeatureDispatchers(
182                             const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl > _rxControl,
183                             SfxSlotId* _pZeroTerminatedSlots,
184                             ControlFeatures& _rDispatchers
185                         );
186 
187         /// creates SfxPoolItes for all features in the given set, and puts them into the given SfxAllItemSet
188         void            transferFeatureStatesToItemSet(
189                             ControlFeatures& _rDispatchers,
190                             SfxAllItemSet& _rSet,
191                             bool _bTranslateLatin = false
192                         );
193 
194         /// to be called when a control has been activated
195         void    controlActivated( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl >& _rxControl );
196         /// to be called when the currently active control has been deactivated
197         void    controlDeactivated( );
198 
199         void    implClearActiveControlRef();
200 
201         /** starts listening at all controls of the given controller for focus events
202         @precond
203             we don't have an active controller currently
204         */
205         void    startControllerListening( const ::com::sun::star::uno::Reference< ::com::sun::star::form::runtime::XFormController >& _rxController );
206         /** stops listening at the active controller
207         @precond
208             we have an active controller currently
209         */
210         void    stopControllerListening( );
211 
212         /** parses the given URL's Complete member, by calling XURLTransformer::parseString
213         */
214         void    impl_parseURL_nothrow( ::com::sun::star::util::URL& _rURL );
215 
216         DECL_LINK( OnInvalidateClipboard, void* );
217     };
218 
219 //........................................................................
220 } // namespace svx
221 //........................................................................
222 
223 #endif // SVX_SOURCE_INC_FMTEXTCONTROLSHELL_HXX
224 
225