xref: /trunk/main/svx/source/inc/fmcontrolbordermanager.hxx (revision e8df90a080f9dc7518c4b937d7dcd461d7b4d178)
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
137         {
138            typedef ControlData first_argument_type;
139            typedef ControlData second_argument_type;
140            typedef bool result_type;
operator ()svxform::ControlBorderManager::ControlDataCompare141            bool operator()( const ControlData& _rLHS, const ControlData& _rRHS ) const
142            {
143                return _rLHS.xControl.get() < _rRHS.xControl.get();
144            }
145         };
146 
147         typedef ::std::set< ControlData, ControlDataCompare > ControlBag;
148         typedef ::com::sun::star::awt::XVclWindowPeer                                       WindowPeer;
149         typedef ::com::sun::star::uno::Reference< ::com::sun::star::awt::XVclWindowPeer >   WindowPeerRef;
150         typedef ::std::set< WindowPeerRef, ::comphelper::OInterfaceCompare< WindowPeer > >  PeerBag;
151 
152         PeerBag     m_aColorableControls;
153         PeerBag     m_aNonColorableControls;
154 
155         ControlData m_aFocusControl;
156         ControlData m_aMouseHoverControl;
157         ControlBag  m_aInvalidControls;
158 
159         // ----------------
160         // attributes
161         sal_Int32   m_nFocusColor;
162         sal_Int32   m_nMouseHoveColor;
163         sal_Int32   m_nInvalidColor;
164         bool        m_bDynamicBorderColors;
165 
166     public:
167         ControlBorderManager();
168         ~ControlBorderManager();
169 
170     public:
171         void    focusGained( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxControl ) SAL_THROW(());
172         void    focusLost( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxControl ) SAL_THROW(());
173         void    mouseEntered( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxControl ) SAL_THROW(());
174         void    mouseExited( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxControl ) SAL_THROW(());
175 
176         void    validityChanged(
177                     const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl >& _rxControl,
178                     const ::com::sun::star::uno::Reference< ::com::sun::star::form::validation::XValidatableFormComponent >& _rxValidatable
179                 ) SAL_THROW(());
180 
181         /// enables dynamic border color for the controls
182         void    enableDynamicBorderColor( );
183         /// disables dynamic border color for the controls
184         void    disableDynamicBorderColor( );
185 
186         /** sets a color to be used for a given status
187             @param _nStatus
188                 the status which the color should be applied for. Must not be CONTROL_STATUS_NONE
189             @param _nColor
190                 the color to apply for the given status
191         */
192         void    setStatusColor( ControlStatus _nStatus, sal_Int32 _nColor );
193 
194         /** restores all colors of all controls where we possibly changed them
195         */
196         void    restoreAll();
197 
198     private:
199         /** called when a control got one of the two possible stati (focused, and hovered with the mouse)
200             @param _rxControl
201                 the control which gained the status
202             @param _rControlData
203                 the control's status data, as a reference to our respective member
204         */
205         void    controlStatusGained(
206                     const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxControl,
207                     ControlData& _rControlData
208                 ) SAL_THROW(());
209 
210         /** called when a control lost one of the two possible stati (focused, and hovered with the mouse)
211             @param _rxControl
212                 the control which lost the status
213             @param _rControlData
214                 the control's status data, as a reference to our respective member
215         */
216         void    controlStatusLost( const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _rxControl, ControlData& _rControlData ) SAL_THROW(());
217 
218         /** determines whether the border of a given peer can be colored
219             @param _rxPeer
220                 the peer to examine. Must not be <NULL/>
221         */
222         bool    canColorBorder( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XVclWindowPeer >& _rxPeer );
223 
224         /** determines the status of the given control
225         */
226         ControlStatus   getControlStatus( const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl >& _rxControl ) SAL_THROW(());
227 
228         /** retrieves the color associated with a given ControlStatus
229             @param _eStatus
230                 the status of the control. Must not be <member>ControlStatus::none</member>
231         */
232         sal_Int32       getControlColorByStatus( ControlStatus _eStatus );
233 
234         /** sets the border color for a given control, depending on its status
235             @param _rxControl
236                 the control to set the border color for. Must not be <NULL/>
237             @param _rxPeer
238                 the peer of the control, to be passed herein for optimization the caller usually needs it, anyway).
239                 Must not be <NULL/>
240             @param _rFallback
241                 the color/type to use when the control has the status CONTROL_STATUS_NONE
242         */
243         void            updateBorderStyle(
244                             const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl >& _rxControl,
245                             const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XVclWindowPeer >& _rxPeer,
246                             const BorderDescriptor& _rFallback
247                         ) SAL_THROW(());
248 
249         /** determines the to-be-remembered original border color and type for a control
250 
251             The method also takes into account that the control may currently have an overwritten
252             border style
253 
254             @param _rxControl
255                 the control to examine. Must not be <NULL/>, and have a non-<NULL/> peer
256         */
257         void determineOriginalBorderStyle(
258                     const ::com::sun::star::uno::Reference< ::com::sun::star::awt::XControl >& _rxControl,
259                     BorderDescriptor& _rData
260                 ) const;
261     };
262 
263 //........................................................................
264 } // namespace svxform
265 //........................................................................
266 
267 #endif // SVX_SOURCE_INC_FMCONTROLBORDERMANAGER_HXX
268