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 #ifndef SVX_SOURCE_INC_FMCONTROLBORDERMANAGER_HXX
25 #define SVX_SOURCE_INC_FMCONTROLBORDERMANAGER_HXX
26 
27 /** === begin UNO includes === **/
28 #include <com/sun/star/awt/VisualEffect.hpp>
29 #include <com/sun/star/awt/FontUnderline.hpp>
30 #include <com/sun/star/awt/XControl.hpp>
31 #include <com/sun/star/awt/XVclWindowPeer.hpp>
32 /** === end UNO includes === **/
33 #include <comphelper/stl_types.hxx>
34 #include <comphelper/stl_types.hxx>
35 
36 #include <set>
37 
38 namespace com { namespace sun { namespace star { namespace form { namespace validation {
39     class XValidatableFormComponent;
40 } } } } }
41 
42 //........................................................................
43 namespace svxform
44 {
45 //........................................................................
46 
47     typedef sal_Int16 ControlStatus;
48 
49     #define CONTROL_STATUS_NONE         0x00
50     #define CONTROL_STATUS_FOCUSED      0x01
51     #define CONTROL_STATUS_MOUSE_HOVER  0x02
52     #define CONTROL_STATUS_INVALID      0x04
53 
54     //====================================================================
55 	//= BorderDescriptor
56 	//====================================================================
57     struct BorderDescriptor
58     {
59         sal_Int16   nBorderType;
60         sal_Int32   nBorderColor;
61 
BorderDescriptorsvxform::BorderDescriptor62         BorderDescriptor()
63             :nBorderType( ::com::sun::star::awt::VisualEffect::FLAT )
64             ,nBorderColor( 0x00000000 )
65         {
66         }
clearsvxform::BorderDescriptor67 		inline void clear()
68 		{
69 			nBorderType = ::com::sun::star::awt::VisualEffect::FLAT;
70 			nBorderColor = 0x00000000;
71 		}
72     };
73 
74     //====================================================================
75 	//= UnderlineDescriptor
76 	//====================================================================
77     struct UnderlineDescriptor
78     {
79         sal_Int16 nUnderlineType;
80         sal_Int32 nUnderlineColor;
81 
UnderlineDescriptorsvxform::UnderlineDescriptor82         UnderlineDescriptor()
83             :nUnderlineType( ::com::sun::star::awt::FontUnderline::NONE )
84             ,nUnderlineColor( 0x00000000 )
85         {
86         }
87 
UnderlineDescriptorsvxform::UnderlineDescriptor88         UnderlineDescriptor( sal_Int16 _nUnderlineType, sal_Int32 _nUnderlineColor )
89             :nUnderlineType( _nUnderlineType )
90             ,nUnderlineColor( _nUnderlineColor )
91         {
92         }
93 
clearsvxform::UnderlineDescriptor94 		inline void clear()
95 		{
96 			nUnderlineType = ::com::sun::star::awt::FontUnderline::NONE;
97 			nUnderlineColor = 0x00000000;
98 		}
99     };
100 
101     //====================================================================
102     //= ControlData
103     //====================================================================
104     struct ControlData : public BorderDescriptor, UnderlineDescriptor
105     {
106         ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl > xControl;
107         ::rtl::OUString                                                     sOriginalHelpText;
108 
ControlDatasvxform::ControlData109         ControlData() : BorderDescriptor() { }
ControlDatasvxform::ControlData110         ControlData( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl >& _rxControl )
111             :xControl( _rxControl )
112         {
113         }
clearsvxform::ControlData114 		void clear()
115 		{
116 			BorderDescriptor::clear();
117 			UnderlineDescriptor::clear();
118 			xControl.clear();
119 			sOriginalHelpText = ::rtl::OUString();
120 		}
121     };
122 
123     //====================================================================
124 	//= ControlBorderManager
125 	//====================================================================
126     /** manages the dynamic border color for form controls
127 
128         Used by the <type>FormController</type>, this class manages the dynamic changes in the
129         border color of form controls. For this a set of events have to be forwarded to the manager
130         instance, which then will switch the border color depending on the mouse and focus status
131         of the controls.
132     */
133 	class ControlBorderManager
134 	{
135     private:
136         struct ControlDataCompare : public ::std::binary_function< ControlData, ControlData, bool >
137         {
operator ()svxform::ControlBorderManager::ControlDataCompare138            bool operator()( const ControlData& _rLHS, const ControlData& _rRHS ) const
139            {
140                return _rLHS.xControl.get() < _rRHS.xControl.get();
141            }
142         };
143 
144         typedef ::std::set< ControlData, ControlDataCompare > ControlBag;
145         typedef ::com::sun::star::awt::XVclWindowPeer                                       WindowPeer;
146         typedef ::com::sun::star::uno::Reference< ::com::sun::star::awt::XVclWindowPeer >   WindowPeerRef;
147         typedef ::std::set< WindowPeerRef, ::comphelper::OInterfaceCompare< WindowPeer > >  PeerBag;
148 
149         PeerBag     m_aColorableControls;
150         PeerBag     m_aNonColorableControls;
151 
152         ControlData m_aFocusControl;
153         ControlData m_aMouseHoverControl;
154         ControlBag  m_aInvalidControls;
155 
156         // ----------------
157         // attributes
158         sal_Int32   m_nFocusColor;
159         sal_Int32   m_nMouseHoveColor;
160         sal_Int32   m_nInvalidColor;
161         bool        m_bDynamicBorderColors;
162 
163     public:
164         ControlBorderManager();
165         ~ControlBorderManager();
166 
167     public:
168         void    focusGained( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxControl ) SAL_THROW(());
169         void    focusLost( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxControl ) SAL_THROW(());
170         void    mouseEntered( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxControl ) SAL_THROW(());
171         void    mouseExited( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxControl ) SAL_THROW(());
172 
173         void    validityChanged(
174                     const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl >& _rxControl,
175                     const ::com::sun::star::uno::Reference< ::com::sun::star::form::validation::XValidatableFormComponent >& _rxValidatable
176                 ) SAL_THROW(());
177 
178         /// enables dynamic border color for the controls
179         void    enableDynamicBorderColor( );
180         /// disables dynamic border color for the controls
181         void    disableDynamicBorderColor( );
182 
183         /** sets a color to be used for a given status
184             @param _nStatus
185                 the status which the color should be applied for. Must not be CONTROL_STATUS_NONE
186             @param _nColor
187                 the color to apply for the given status
188         */
189         void    setStatusColor( ControlStatus _nStatus, sal_Int32 _nColor );
190 
191         /** restores all colors of all controls where we possibly changed them
192         */
193         void    restoreAll();
194 
195     private:
196         /** called when a control got one of the two possible stati (focused, and hovered with the mouse)
197             @param _rxControl
198                 the control which gained the status
199             @param _rControlData
200                 the control's status data, as a reference to our respective member
201         */
202         void    controlStatusGained(
203                     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxControl,
204                     ControlData& _rControlData
205                 ) SAL_THROW(());
206 
207         /** called when a control lost one of the two possible stati (focused, and hovered with the mouse)
208             @param _rxControl
209                 the control which lost the status
210             @param _rControlData
211                 the control's status data, as a reference to our respective member
212         */
213         void    controlStatusLost( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxControl, ControlData& _rControlData ) SAL_THROW(());
214 
215         /** determines whether the border of a given peer can be colored
216             @param _rxPeer
217                 the peer to examine. Must not be <NULL/>
218         */
219         bool    canColorBorder( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XVclWindowPeer >& _rxPeer );
220 
221         /** determines the status of the given control
222         */
223         ControlStatus   getControlStatus( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl >& _rxControl ) SAL_THROW(());
224 
225         /** retrieves the color associated with a given ControlStatus
226             @param _eStatus
227                 the status of the control. Must not be <member>ControlStatus::none</member>
228         */
229         sal_Int32       getControlColorByStatus( ControlStatus _eStatus );
230 
231         /** sets the border color for a given control, depending on its status
232             @param _rxControl
233                 the control to set the border color for. Must not be <NULL/>
234             @param _rxPeer
235                 the peer of the control, to be passed herein for optimization the caller usually needs it, anyway).
236                 Must not be <NULL/>
237             @param _rFallback
238                 the color/type to use when the control has the status CONTROL_STATUS_NONE
239         */
240         void            updateBorderStyle(
241                             const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl >& _rxControl,
242                             const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XVclWindowPeer >& _rxPeer,
243                             const BorderDescriptor& _rFallback
244                         ) SAL_THROW(());
245 
246         /** determines the to-be-remembered original border color and type for a control
247 
248             The method also takes into account that the control may currently have an overwritten
249             border style
250 
251             @param _rxControl
252                 the control to examine. Must not be <NULL/>, and have a non-<NULL/> peer
253         */
254         void determineOriginalBorderStyle(
255                     const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl >& _rxControl,
256                     BorderDescriptor& _rData
257                 ) const;
258 	};
259 
260 //........................................................................
261 } // namespace svxform
262 //........................................................................
263 
264 #endif // SVX_SOURCE_INC_FMCONTROLBORDERMANAGER_HXX
265