xref: /aoo42x/main/svx/source/dialog/dialcontrol.cxx (revision cdf0e10c)
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_svx.hxx"
30*cdf0e10cSrcweir #include "svx/dialcontrol.hxx"
31*cdf0e10cSrcweir 
32*cdf0e10cSrcweir #include <math.h>
33*cdf0e10cSrcweir #include <vcl/virdev.hxx>
34*cdf0e10cSrcweir #include <vcl/svapp.hxx>
35*cdf0e10cSrcweir #include <vcl/bitmap.hxx>
36*cdf0e10cSrcweir #include <vcl/field.hxx>
37*cdf0e10cSrcweir #include <svtools/colorcfg.hxx>
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir namespace svx {
40*cdf0e10cSrcweir 
41*cdf0e10cSrcweir // ============================================================================
42*cdf0e10cSrcweir 
43*cdf0e10cSrcweir const long DIAL_OUTER_WIDTH = 8;
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir // ============================================================================
46*cdf0e10cSrcweir 
47*cdf0e10cSrcweir class DialControlBmp : public VirtualDevice
48*cdf0e10cSrcweir {
49*cdf0e10cSrcweir public:
50*cdf0e10cSrcweir     explicit            DialControlBmp( Window& rParent );
51*cdf0e10cSrcweir 
52*cdf0e10cSrcweir     void                InitBitmap( const Size& rSize, const Font& rFont );
53*cdf0e10cSrcweir     void                CopyBackground( const DialControlBmp& rSrc );
54*cdf0e10cSrcweir     void                DrawBackground( const Size& rSize, bool bEnabled );
55*cdf0e10cSrcweir     void                DrawElements( const String& rText, sal_Int32 nAngle );
56*cdf0e10cSrcweir 
57*cdf0e10cSrcweir private:
58*cdf0e10cSrcweir     const Color&        GetBackgroundColor() const;
59*cdf0e10cSrcweir     const Color&        GetTextColor() const;
60*cdf0e10cSrcweir     const Color&        GetScaleLineColor() const;
61*cdf0e10cSrcweir     const Color&        GetButtonLineColor() const;
62*cdf0e10cSrcweir     const Color&        GetButtonFillColor( bool bMain ) const;
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir     void                Init( const Size& rSize );
65*cdf0e10cSrcweir     void                DrawBackground();
66*cdf0e10cSrcweir 
67*cdf0e10cSrcweir     Window&             mrParent;
68*cdf0e10cSrcweir     Rectangle           maRect;
69*cdf0e10cSrcweir     long                mnCenterX;
70*cdf0e10cSrcweir     long                mnCenterY;
71*cdf0e10cSrcweir     bool                mbEnabled;
72*cdf0e10cSrcweir };
73*cdf0e10cSrcweir 
74*cdf0e10cSrcweir // ----------------------------------------------------------------------------
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir DialControlBmp::DialControlBmp( Window& rParent ) :
77*cdf0e10cSrcweir     VirtualDevice( rParent, 0, 0 ),
78*cdf0e10cSrcweir     mrParent( rParent ),
79*cdf0e10cSrcweir     mbEnabled( true )
80*cdf0e10cSrcweir {
81*cdf0e10cSrcweir     EnableRTL( sal_False );
82*cdf0e10cSrcweir }
83*cdf0e10cSrcweir 
84*cdf0e10cSrcweir void DialControlBmp::InitBitmap( const Size& rSize, const Font& rFont )
85*cdf0e10cSrcweir {
86*cdf0e10cSrcweir     Init( rSize );
87*cdf0e10cSrcweir     SetFont( rFont );
88*cdf0e10cSrcweir }
89*cdf0e10cSrcweir 
90*cdf0e10cSrcweir void DialControlBmp::CopyBackground( const DialControlBmp& rSrc )
91*cdf0e10cSrcweir {
92*cdf0e10cSrcweir     Init( rSrc.maRect.GetSize() );
93*cdf0e10cSrcweir     mbEnabled = rSrc.mbEnabled;
94*cdf0e10cSrcweir     Point aPos;
95*cdf0e10cSrcweir     DrawBitmapEx( aPos, rSrc.GetBitmapEx( aPos, maRect.GetSize() ) );
96*cdf0e10cSrcweir }
97*cdf0e10cSrcweir 
98*cdf0e10cSrcweir void DialControlBmp::DrawBackground( const Size& rSize, bool bEnabled )
99*cdf0e10cSrcweir {
100*cdf0e10cSrcweir     Init( rSize );
101*cdf0e10cSrcweir     mbEnabled = bEnabled;
102*cdf0e10cSrcweir     DrawBackground();
103*cdf0e10cSrcweir }
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir void DialControlBmp::DrawElements( const String& rText, sal_Int32 nAngle )
106*cdf0e10cSrcweir {
107*cdf0e10cSrcweir     // *** rotated text ***
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir     Font aFont( GetFont() );
110*cdf0e10cSrcweir     aFont.SetColor( GetTextColor() );
111*cdf0e10cSrcweir     aFont.SetOrientation( static_cast< short >( (nAngle + 5) / 10 ) );  // Font uses 1/10 degrees
112*cdf0e10cSrcweir     aFont.SetWeight( WEIGHT_BOLD );
113*cdf0e10cSrcweir     SetFont( aFont );
114*cdf0e10cSrcweir 
115*cdf0e10cSrcweir     double fAngle = nAngle * F_PI180 / 100.0;
116*cdf0e10cSrcweir     double fSin = sin( fAngle );
117*cdf0e10cSrcweir     double fCos = cos( fAngle );
118*cdf0e10cSrcweir     double fWidth = GetTextWidth( rText ) / 2.0;
119*cdf0e10cSrcweir     double fHeight = GetTextHeight() / 2.0;
120*cdf0e10cSrcweir     long nX = static_cast< long >( mnCenterX - fWidth * fCos - fHeight * fSin );
121*cdf0e10cSrcweir     long nY = static_cast< long >( mnCenterY + fWidth * fSin - fHeight * fCos );
122*cdf0e10cSrcweir     Rectangle aRect( nX, nY, 2 * mnCenterX - nX, 2 * mnCenterY - nY );
123*cdf0e10cSrcweir     DrawText( aRect, rText, mbEnabled ? 0 : TEXT_DRAW_DISABLE );
124*cdf0e10cSrcweir 
125*cdf0e10cSrcweir     // *** drag button ***
126*cdf0e10cSrcweir 
127*cdf0e10cSrcweir     bool bMain = (nAngle % 4500) != 0;
128*cdf0e10cSrcweir     SetLineColor( GetButtonLineColor() );
129*cdf0e10cSrcweir     SetFillColor( GetButtonFillColor( bMain ) );
130*cdf0e10cSrcweir 
131*cdf0e10cSrcweir     nX = mnCenterX - static_cast< long >( (DIAL_OUTER_WIDTH / 2 - mnCenterX) * fCos );
132*cdf0e10cSrcweir     nY = mnCenterY - static_cast< long >( (mnCenterY - DIAL_OUTER_WIDTH / 2) * fSin );
133*cdf0e10cSrcweir     long nSize = bMain ? (DIAL_OUTER_WIDTH / 4) : (DIAL_OUTER_WIDTH / 2 - 1);
134*cdf0e10cSrcweir     DrawEllipse( Rectangle( nX - nSize, nY - nSize, nX + nSize, nY + nSize ) );
135*cdf0e10cSrcweir }
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir // private --------------------------------------------------------------------
138*cdf0e10cSrcweir 
139*cdf0e10cSrcweir const Color& DialControlBmp::GetBackgroundColor() const
140*cdf0e10cSrcweir {
141*cdf0e10cSrcweir     return GetSettings().GetStyleSettings().GetDialogColor();
142*cdf0e10cSrcweir }
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir const Color& DialControlBmp::GetTextColor() const
145*cdf0e10cSrcweir {
146*cdf0e10cSrcweir     return GetSettings().GetStyleSettings().GetLabelTextColor();
147*cdf0e10cSrcweir }
148*cdf0e10cSrcweir 
149*cdf0e10cSrcweir const Color& DialControlBmp::GetScaleLineColor() const
150*cdf0e10cSrcweir {
151*cdf0e10cSrcweir     const StyleSettings& rSett = GetSettings().GetStyleSettings();
152*cdf0e10cSrcweir     return mbEnabled ? rSett.GetButtonTextColor() : rSett.GetDisableColor();
153*cdf0e10cSrcweir }
154*cdf0e10cSrcweir 
155*cdf0e10cSrcweir const Color& DialControlBmp::GetButtonLineColor() const
156*cdf0e10cSrcweir {
157*cdf0e10cSrcweir     const StyleSettings& rSett = GetSettings().GetStyleSettings();
158*cdf0e10cSrcweir     return mbEnabled ? rSett.GetButtonTextColor() : rSett.GetDisableColor();
159*cdf0e10cSrcweir }
160*cdf0e10cSrcweir 
161*cdf0e10cSrcweir const Color& DialControlBmp::GetButtonFillColor( bool bMain ) const
162*cdf0e10cSrcweir {
163*cdf0e10cSrcweir     const StyleSettings& rSett = GetSettings().GetStyleSettings();
164*cdf0e10cSrcweir     return mbEnabled ? (bMain ? rSett.GetMenuColor() : rSett.GetHighlightColor()) : rSett.GetDisableColor();
165*cdf0e10cSrcweir }
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir void DialControlBmp::Init( const Size& rSize )
168*cdf0e10cSrcweir {
169*cdf0e10cSrcweir     SetSettings( mrParent.GetSettings() );
170*cdf0e10cSrcweir     maRect.SetPos( Point( 0, 0 ) );
171*cdf0e10cSrcweir     maRect.SetSize( rSize );
172*cdf0e10cSrcweir     mnCenterX = rSize.Width() / 2;
173*cdf0e10cSrcweir     mnCenterY = rSize.Height() / 2;
174*cdf0e10cSrcweir     SetOutputSize( rSize );
175*cdf0e10cSrcweir     SetBackground();
176*cdf0e10cSrcweir }
177*cdf0e10cSrcweir 
178*cdf0e10cSrcweir void DialControlBmp::DrawBackground()
179*cdf0e10cSrcweir {
180*cdf0e10cSrcweir     // *** background with 3D effect ***
181*cdf0e10cSrcweir 
182*cdf0e10cSrcweir     SetLineColor();
183*cdf0e10cSrcweir     SetFillColor();
184*cdf0e10cSrcweir     Erase();
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir     EnableRTL( sal_True ); // #107807# draw 3D effect in correct direction
187*cdf0e10cSrcweir 
188*cdf0e10cSrcweir     sal_uInt8 nDiff = mbEnabled ? 0x18 : 0x10;
189*cdf0e10cSrcweir     Color aColor;
190*cdf0e10cSrcweir 
191*cdf0e10cSrcweir     aColor = GetBackgroundColor();
192*cdf0e10cSrcweir     SetFillColor( aColor );
193*cdf0e10cSrcweir     DrawPie( maRect, maRect.TopRight(), maRect.TopCenter() );
194*cdf0e10cSrcweir     DrawPie( maRect, maRect.BottomLeft(), maRect.BottomCenter() );
195*cdf0e10cSrcweir 
196*cdf0e10cSrcweir     aColor.DecreaseLuminance( nDiff );
197*cdf0e10cSrcweir     SetFillColor( aColor );
198*cdf0e10cSrcweir     DrawPie( maRect, maRect.BottomCenter(), maRect.TopRight() );
199*cdf0e10cSrcweir 
200*cdf0e10cSrcweir     aColor.DecreaseLuminance( nDiff );
201*cdf0e10cSrcweir     SetFillColor( aColor );
202*cdf0e10cSrcweir     DrawPie( maRect, maRect.BottomRight(), maRect.RightCenter() );
203*cdf0e10cSrcweir 
204*cdf0e10cSrcweir     aColor = GetBackgroundColor();
205*cdf0e10cSrcweir     aColor.IncreaseLuminance( nDiff );
206*cdf0e10cSrcweir     SetFillColor( aColor );
207*cdf0e10cSrcweir     DrawPie( maRect, maRect.TopCenter(), maRect.BottomLeft() );
208*cdf0e10cSrcweir 
209*cdf0e10cSrcweir     aColor.IncreaseLuminance( nDiff );
210*cdf0e10cSrcweir     SetFillColor( aColor );
211*cdf0e10cSrcweir     DrawPie( maRect, maRect.TopLeft(), maRect.LeftCenter() );
212*cdf0e10cSrcweir 
213*cdf0e10cSrcweir     EnableRTL( sal_False );
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir     // *** calibration ***
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir     Point aStartPos( mnCenterX, mnCenterY );
218*cdf0e10cSrcweir     Color aFullColor( GetScaleLineColor() );
219*cdf0e10cSrcweir     Color aLightColor( GetBackgroundColor() );
220*cdf0e10cSrcweir     aLightColor.Merge( aFullColor, 128 );
221*cdf0e10cSrcweir 
222*cdf0e10cSrcweir     for( int nAngle = 0; nAngle < 360; nAngle += 15 )
223*cdf0e10cSrcweir     {
224*cdf0e10cSrcweir         SetLineColor( (nAngle % 45) ? aLightColor : aFullColor );
225*cdf0e10cSrcweir         double fAngle = nAngle * F_PI180;
226*cdf0e10cSrcweir         long nX = static_cast< long >( -mnCenterX * cos( fAngle ) );
227*cdf0e10cSrcweir         long nY = static_cast< long >( mnCenterY * sin( fAngle ) );
228*cdf0e10cSrcweir         DrawLine( aStartPos, Point( mnCenterX - nX, mnCenterY - nY ) );
229*cdf0e10cSrcweir     }
230*cdf0e10cSrcweir 
231*cdf0e10cSrcweir     // *** clear inner area ***
232*cdf0e10cSrcweir 
233*cdf0e10cSrcweir     SetLineColor();
234*cdf0e10cSrcweir     SetFillColor( GetBackgroundColor() );
235*cdf0e10cSrcweir     DrawEllipse( Rectangle( maRect.Left() + DIAL_OUTER_WIDTH, maRect.Top() + DIAL_OUTER_WIDTH,
236*cdf0e10cSrcweir         maRect.Right() - DIAL_OUTER_WIDTH, maRect.Bottom() - DIAL_OUTER_WIDTH ) );
237*cdf0e10cSrcweir }
238*cdf0e10cSrcweir 
239*cdf0e10cSrcweir // ============================================================================
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir struct DialControl_Impl
242*cdf0e10cSrcweir {
243*cdf0e10cSrcweir     DialControlBmp      maBmpEnabled;
244*cdf0e10cSrcweir     DialControlBmp      maBmpDisabled;
245*cdf0e10cSrcweir     DialControlBmp      maBmpBuffered;
246*cdf0e10cSrcweir     Link                maModifyHdl;
247*cdf0e10cSrcweir     NumericField*       mpLinkField;
248*cdf0e10cSrcweir     Size                maWinSize;
249*cdf0e10cSrcweir     Font                maWinFont;
250*cdf0e10cSrcweir     sal_Int32           mnAngle;
251*cdf0e10cSrcweir     sal_Int32           mnOldAngle;
252*cdf0e10cSrcweir     long                mnCenterX;
253*cdf0e10cSrcweir     long                mnCenterY;
254*cdf0e10cSrcweir     bool                mbNoRot;
255*cdf0e10cSrcweir 
256*cdf0e10cSrcweir     explicit            DialControl_Impl( Window& rParent );
257*cdf0e10cSrcweir     void                Init( const Size& rWinSize, const Font& rWinFont );
258*cdf0e10cSrcweir };
259*cdf0e10cSrcweir 
260*cdf0e10cSrcweir // ----------------------------------------------------------------------------
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir DialControl_Impl::DialControl_Impl( Window& rParent ) :
263*cdf0e10cSrcweir     maBmpEnabled( rParent ),
264*cdf0e10cSrcweir     maBmpDisabled( rParent ),
265*cdf0e10cSrcweir     maBmpBuffered( rParent ),
266*cdf0e10cSrcweir     mpLinkField( 0 ),
267*cdf0e10cSrcweir     mnAngle( 0 ),
268*cdf0e10cSrcweir     mbNoRot( false )
269*cdf0e10cSrcweir {
270*cdf0e10cSrcweir }
271*cdf0e10cSrcweir 
272*cdf0e10cSrcweir void DialControl_Impl::Init( const Size& rWinSize, const Font& rWinFont )
273*cdf0e10cSrcweir {
274*cdf0e10cSrcweir     // "(x - 1) | 1" creates odd value <= x, to have a well-defined center pixel position
275*cdf0e10cSrcweir     maWinSize = Size( (rWinSize.Width() - 1) | 1, (rWinSize.Height() - 1) | 1 );
276*cdf0e10cSrcweir     maWinFont = rWinFont;
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir     mnCenterX = maWinSize.Width() / 2;
279*cdf0e10cSrcweir     mnCenterY = maWinSize.Height() / 2;
280*cdf0e10cSrcweir     maWinFont.SetTransparent( sal_True );
281*cdf0e10cSrcweir 
282*cdf0e10cSrcweir     maBmpEnabled.DrawBackground( maWinSize, true );
283*cdf0e10cSrcweir     maBmpDisabled.DrawBackground( maWinSize, false );
284*cdf0e10cSrcweir     maBmpBuffered.InitBitmap( maWinSize, maWinFont );
285*cdf0e10cSrcweir }
286*cdf0e10cSrcweir 
287*cdf0e10cSrcweir // ============================================================================
288*cdf0e10cSrcweir 
289*cdf0e10cSrcweir DialControl::DialControl( Window* pParent, const Size& rSize, const Font& rFont, WinBits nWinStyle ) :
290*cdf0e10cSrcweir     Control( pParent, nWinStyle ),
291*cdf0e10cSrcweir     mpImpl( new DialControl_Impl( *this ) )
292*cdf0e10cSrcweir {
293*cdf0e10cSrcweir     Init( rSize, rFont );
294*cdf0e10cSrcweir }
295*cdf0e10cSrcweir 
296*cdf0e10cSrcweir DialControl::DialControl( Window* pParent, const Size& rSize, WinBits nWinStyle ) :
297*cdf0e10cSrcweir     Control( pParent, nWinStyle ),
298*cdf0e10cSrcweir     mpImpl( new DialControl_Impl( *this ) )
299*cdf0e10cSrcweir {
300*cdf0e10cSrcweir     if( pParent )
301*cdf0e10cSrcweir         Init( rSize, pParent->GetFont() );
302*cdf0e10cSrcweir     else
303*cdf0e10cSrcweir         Init( rSize );
304*cdf0e10cSrcweir }
305*cdf0e10cSrcweir 
306*cdf0e10cSrcweir DialControl::DialControl( Window* pParent, const ResId& rResId ) :
307*cdf0e10cSrcweir     Control( pParent, rResId ),
308*cdf0e10cSrcweir     mpImpl( new DialControl_Impl( *this ) )
309*cdf0e10cSrcweir {
310*cdf0e10cSrcweir     Init( GetOutputSizePixel() );
311*cdf0e10cSrcweir }
312*cdf0e10cSrcweir 
313*cdf0e10cSrcweir DialControl::~DialControl()
314*cdf0e10cSrcweir {
315*cdf0e10cSrcweir }
316*cdf0e10cSrcweir 
317*cdf0e10cSrcweir void DialControl::Paint( const Rectangle&  )
318*cdf0e10cSrcweir {
319*cdf0e10cSrcweir     Point aPos;
320*cdf0e10cSrcweir     DrawBitmapEx( aPos, mpImpl->maBmpBuffered.GetBitmapEx( aPos, mpImpl->maWinSize ) );
321*cdf0e10cSrcweir }
322*cdf0e10cSrcweir 
323*cdf0e10cSrcweir void DialControl::StateChanged( StateChangedType nStateChange )
324*cdf0e10cSrcweir {
325*cdf0e10cSrcweir     if( nStateChange == STATE_CHANGE_ENABLE )
326*cdf0e10cSrcweir         InvalidateControl();
327*cdf0e10cSrcweir 
328*cdf0e10cSrcweir     // update the linked edit field
329*cdf0e10cSrcweir     if( mpImpl->mpLinkField )
330*cdf0e10cSrcweir     {
331*cdf0e10cSrcweir         NumericField& rField = *mpImpl->mpLinkField;
332*cdf0e10cSrcweir         switch( nStateChange )
333*cdf0e10cSrcweir         {
334*cdf0e10cSrcweir             case STATE_CHANGE_VISIBLE:  rField.Show( IsVisible() );     break;
335*cdf0e10cSrcweir             case STATE_CHANGE_ENABLE:   rField.Enable( IsEnabled() );   break;
336*cdf0e10cSrcweir         }
337*cdf0e10cSrcweir     }
338*cdf0e10cSrcweir 
339*cdf0e10cSrcweir     Control::StateChanged( nStateChange );
340*cdf0e10cSrcweir }
341*cdf0e10cSrcweir 
342*cdf0e10cSrcweir void DialControl::DataChanged( const DataChangedEvent& rDCEvt )
343*cdf0e10cSrcweir {
344*cdf0e10cSrcweir     if( (rDCEvt.GetType() == DATACHANGED_SETTINGS) && (rDCEvt.GetFlags() & SETTINGS_STYLE) )
345*cdf0e10cSrcweir     {
346*cdf0e10cSrcweir         Init( mpImpl->maWinSize, mpImpl->maWinFont );
347*cdf0e10cSrcweir         InvalidateControl();
348*cdf0e10cSrcweir     }
349*cdf0e10cSrcweir     Control::DataChanged( rDCEvt );
350*cdf0e10cSrcweir }
351*cdf0e10cSrcweir 
352*cdf0e10cSrcweir void DialControl::MouseButtonDown( const MouseEvent& rMEvt )
353*cdf0e10cSrcweir {
354*cdf0e10cSrcweir     if( rMEvt.IsLeft() )
355*cdf0e10cSrcweir     {
356*cdf0e10cSrcweir         GrabFocus();
357*cdf0e10cSrcweir         CaptureMouse();
358*cdf0e10cSrcweir         mpImpl->mnOldAngle = mpImpl->mnAngle;
359*cdf0e10cSrcweir         HandleMouseEvent( rMEvt.GetPosPixel(), true );
360*cdf0e10cSrcweir     }
361*cdf0e10cSrcweir     Control::MouseButtonDown( rMEvt );
362*cdf0e10cSrcweir }
363*cdf0e10cSrcweir 
364*cdf0e10cSrcweir void DialControl::MouseMove( const MouseEvent& rMEvt )
365*cdf0e10cSrcweir {
366*cdf0e10cSrcweir     if( IsMouseCaptured() && rMEvt.IsLeft() )
367*cdf0e10cSrcweir         HandleMouseEvent( rMEvt.GetPosPixel(), false );
368*cdf0e10cSrcweir     Control::MouseMove(rMEvt );
369*cdf0e10cSrcweir }
370*cdf0e10cSrcweir 
371*cdf0e10cSrcweir void DialControl::MouseButtonUp( const MouseEvent& rMEvt )
372*cdf0e10cSrcweir {
373*cdf0e10cSrcweir     if( IsMouseCaptured() )
374*cdf0e10cSrcweir     {
375*cdf0e10cSrcweir         ReleaseMouse();
376*cdf0e10cSrcweir         if( mpImpl->mpLinkField )
377*cdf0e10cSrcweir             mpImpl->mpLinkField->GrabFocus();
378*cdf0e10cSrcweir     }
379*cdf0e10cSrcweir     Control::MouseButtonUp( rMEvt );
380*cdf0e10cSrcweir }
381*cdf0e10cSrcweir 
382*cdf0e10cSrcweir void DialControl::KeyInput( const KeyEvent& rKEvt )
383*cdf0e10cSrcweir {
384*cdf0e10cSrcweir     const KeyCode& rKCode = rKEvt.GetKeyCode();
385*cdf0e10cSrcweir     if( !rKCode.GetModifier() && (rKCode.GetCode() == KEY_ESCAPE) )
386*cdf0e10cSrcweir         HandleEscapeEvent();
387*cdf0e10cSrcweir     else
388*cdf0e10cSrcweir         Control::KeyInput( rKEvt );
389*cdf0e10cSrcweir }
390*cdf0e10cSrcweir 
391*cdf0e10cSrcweir void DialControl::LoseFocus()
392*cdf0e10cSrcweir {
393*cdf0e10cSrcweir     // release captured mouse
394*cdf0e10cSrcweir     HandleEscapeEvent();
395*cdf0e10cSrcweir     Control::LoseFocus();
396*cdf0e10cSrcweir }
397*cdf0e10cSrcweir 
398*cdf0e10cSrcweir bool DialControl::HasRotation() const
399*cdf0e10cSrcweir {
400*cdf0e10cSrcweir     return !mpImpl->mbNoRot;
401*cdf0e10cSrcweir }
402*cdf0e10cSrcweir 
403*cdf0e10cSrcweir void DialControl::SetNoRotation()
404*cdf0e10cSrcweir {
405*cdf0e10cSrcweir     if( !mpImpl->mbNoRot )
406*cdf0e10cSrcweir     {
407*cdf0e10cSrcweir         mpImpl->mbNoRot = true;
408*cdf0e10cSrcweir         InvalidateControl();
409*cdf0e10cSrcweir         if( mpImpl->mpLinkField )
410*cdf0e10cSrcweir             mpImpl->mpLinkField->SetText( String() );
411*cdf0e10cSrcweir     }
412*cdf0e10cSrcweir }
413*cdf0e10cSrcweir 
414*cdf0e10cSrcweir sal_Int32 DialControl::GetRotation() const
415*cdf0e10cSrcweir {
416*cdf0e10cSrcweir     return mpImpl->mnAngle;
417*cdf0e10cSrcweir }
418*cdf0e10cSrcweir 
419*cdf0e10cSrcweir void DialControl::SetRotation( sal_Int32 nAngle )
420*cdf0e10cSrcweir {
421*cdf0e10cSrcweir     ImplSetRotation( nAngle, false );
422*cdf0e10cSrcweir }
423*cdf0e10cSrcweir 
424*cdf0e10cSrcweir void DialControl::SetLinkedField( NumericField* pField )
425*cdf0e10cSrcweir {
426*cdf0e10cSrcweir     // remove modify handler from old linked field
427*cdf0e10cSrcweir     ImplSetFieldLink( Link() );
428*cdf0e10cSrcweir     // remember the new linked field
429*cdf0e10cSrcweir     mpImpl->mpLinkField = pField;
430*cdf0e10cSrcweir     // set modify handler at new linked field
431*cdf0e10cSrcweir     ImplSetFieldLink( LINK( this, DialControl, LinkedFieldModifyHdl ) );
432*cdf0e10cSrcweir }
433*cdf0e10cSrcweir 
434*cdf0e10cSrcweir NumericField* DialControl::GetLinkedField() const
435*cdf0e10cSrcweir {
436*cdf0e10cSrcweir     return mpImpl->mpLinkField;
437*cdf0e10cSrcweir }
438*cdf0e10cSrcweir 
439*cdf0e10cSrcweir void DialControl::SetModifyHdl( const Link& rLink )
440*cdf0e10cSrcweir {
441*cdf0e10cSrcweir     mpImpl->maModifyHdl = rLink;
442*cdf0e10cSrcweir }
443*cdf0e10cSrcweir 
444*cdf0e10cSrcweir const Link& DialControl::GetModifyHdl() const
445*cdf0e10cSrcweir {
446*cdf0e10cSrcweir     return mpImpl->maModifyHdl;
447*cdf0e10cSrcweir }
448*cdf0e10cSrcweir 
449*cdf0e10cSrcweir // private --------------------------------------------------------------------
450*cdf0e10cSrcweir 
451*cdf0e10cSrcweir void DialControl::Init( const Size& rWinSize, const Font& rWinFont )
452*cdf0e10cSrcweir {
453*cdf0e10cSrcweir     mpImpl->Init( rWinSize, rWinFont );
454*cdf0e10cSrcweir     EnableRTL( sal_False ); // #107807# don't mirror mouse handling
455*cdf0e10cSrcweir     SetOutputSizePixel( mpImpl->maWinSize );
456*cdf0e10cSrcweir     SetBackground();
457*cdf0e10cSrcweir }
458*cdf0e10cSrcweir 
459*cdf0e10cSrcweir void DialControl::Init( const Size& rWinSize )
460*cdf0e10cSrcweir {
461*cdf0e10cSrcweir     Font aFont( OutputDevice::GetDefaultFont(
462*cdf0e10cSrcweir         DEFAULTFONT_UI_SANS, Application::GetSettings().GetUILanguage(), DEFAULTFONT_FLAGS_ONLYONE ) );
463*cdf0e10cSrcweir     Init( rWinSize, aFont );
464*cdf0e10cSrcweir }
465*cdf0e10cSrcweir 
466*cdf0e10cSrcweir void DialControl::InvalidateControl()
467*cdf0e10cSrcweir {
468*cdf0e10cSrcweir     mpImpl->maBmpBuffered.CopyBackground( IsEnabled() ? mpImpl->maBmpEnabled : mpImpl->maBmpDisabled );
469*cdf0e10cSrcweir     if( !mpImpl->mbNoRot )
470*cdf0e10cSrcweir         mpImpl->maBmpBuffered.DrawElements( GetText(), mpImpl->mnAngle );
471*cdf0e10cSrcweir     Invalidate();
472*cdf0e10cSrcweir }
473*cdf0e10cSrcweir 
474*cdf0e10cSrcweir void DialControl::ImplSetRotation( sal_Int32 nAngle, bool bBroadcast )
475*cdf0e10cSrcweir {
476*cdf0e10cSrcweir     bool bOldSel = mpImpl->mbNoRot;
477*cdf0e10cSrcweir     mpImpl->mbNoRot = false;
478*cdf0e10cSrcweir 
479*cdf0e10cSrcweir     while( nAngle < 0 ) nAngle += 36000;
480*cdf0e10cSrcweir     nAngle = (((nAngle + 50) / 100) * 100) % 36000;
481*cdf0e10cSrcweir     if( !bOldSel || (mpImpl->mnAngle != nAngle) )
482*cdf0e10cSrcweir     {
483*cdf0e10cSrcweir         mpImpl->mnAngle = nAngle;
484*cdf0e10cSrcweir         InvalidateControl();
485*cdf0e10cSrcweir         if( mpImpl->mpLinkField )
486*cdf0e10cSrcweir             mpImpl->mpLinkField->SetValue( static_cast< long >( GetRotation() / 100 ) );
487*cdf0e10cSrcweir         if( bBroadcast )
488*cdf0e10cSrcweir             mpImpl->maModifyHdl.Call( this );
489*cdf0e10cSrcweir     }
490*cdf0e10cSrcweir }
491*cdf0e10cSrcweir 
492*cdf0e10cSrcweir void DialControl::ImplSetFieldLink( const Link& rLink )
493*cdf0e10cSrcweir {
494*cdf0e10cSrcweir     if( mpImpl->mpLinkField )
495*cdf0e10cSrcweir     {
496*cdf0e10cSrcweir         NumericField& rField = *mpImpl->mpLinkField;
497*cdf0e10cSrcweir         rField.SetModifyHdl( rLink );
498*cdf0e10cSrcweir         rField.SetUpHdl( rLink );
499*cdf0e10cSrcweir         rField.SetDownHdl( rLink );
500*cdf0e10cSrcweir         rField.SetFirstHdl( rLink );
501*cdf0e10cSrcweir         rField.SetLastHdl( rLink );
502*cdf0e10cSrcweir         rField.SetLoseFocusHdl( rLink );
503*cdf0e10cSrcweir     }
504*cdf0e10cSrcweir }
505*cdf0e10cSrcweir 
506*cdf0e10cSrcweir void DialControl::HandleMouseEvent( const Point& rPos, bool bInitial )
507*cdf0e10cSrcweir {
508*cdf0e10cSrcweir     long nX = rPos.X() - mpImpl->mnCenterX;
509*cdf0e10cSrcweir     long nY = mpImpl->mnCenterY - rPos.Y();
510*cdf0e10cSrcweir     double fH = sqrt( static_cast< double >( nX ) * nX + static_cast< double >( nY ) * nY );
511*cdf0e10cSrcweir     if( fH != 0.0 )
512*cdf0e10cSrcweir     {
513*cdf0e10cSrcweir         double fAngle = acos( nX / fH );
514*cdf0e10cSrcweir         sal_Int32 nAngle = static_cast< sal_Int32 >( fAngle / F_PI180 * 100.0 );
515*cdf0e10cSrcweir         if( nY < 0 )
516*cdf0e10cSrcweir             nAngle = 36000 - nAngle;
517*cdf0e10cSrcweir         if( bInitial )  // round to entire 15 degrees
518*cdf0e10cSrcweir             nAngle = ((nAngle + 750) / 1500) * 1500;
519*cdf0e10cSrcweir         ImplSetRotation( nAngle, true );
520*cdf0e10cSrcweir     }
521*cdf0e10cSrcweir }
522*cdf0e10cSrcweir 
523*cdf0e10cSrcweir void DialControl::HandleEscapeEvent()
524*cdf0e10cSrcweir {
525*cdf0e10cSrcweir     if( IsMouseCaptured() )
526*cdf0e10cSrcweir     {
527*cdf0e10cSrcweir         ReleaseMouse();
528*cdf0e10cSrcweir         ImplSetRotation( mpImpl->mnOldAngle, true );
529*cdf0e10cSrcweir         if( mpImpl->mpLinkField )
530*cdf0e10cSrcweir             mpImpl->mpLinkField->GrabFocus();
531*cdf0e10cSrcweir     }
532*cdf0e10cSrcweir }
533*cdf0e10cSrcweir 
534*cdf0e10cSrcweir IMPL_LINK( DialControl, LinkedFieldModifyHdl, NumericField*, pField )
535*cdf0e10cSrcweir {
536*cdf0e10cSrcweir     if( pField )
537*cdf0e10cSrcweir         ImplSetRotation( static_cast< sal_Int32 >( pField->GetValue() * 100 ), false );
538*cdf0e10cSrcweir     return 0;
539*cdf0e10cSrcweir }
540*cdf0e10cSrcweir 
541*cdf0e10cSrcweir // ============================================================================
542*cdf0e10cSrcweir 
543*cdf0e10cSrcweir DialControlWrapper::DialControlWrapper( DialControl& rDial ) :
544*cdf0e10cSrcweir     SingleControlWrapperType( rDial )
545*cdf0e10cSrcweir {
546*cdf0e10cSrcweir }
547*cdf0e10cSrcweir 
548*cdf0e10cSrcweir bool DialControlWrapper::IsControlDontKnow() const
549*cdf0e10cSrcweir {
550*cdf0e10cSrcweir     return !GetControl().HasRotation();
551*cdf0e10cSrcweir }
552*cdf0e10cSrcweir 
553*cdf0e10cSrcweir void DialControlWrapper::SetControlDontKnow( bool bSet )
554*cdf0e10cSrcweir {
555*cdf0e10cSrcweir     if( bSet )
556*cdf0e10cSrcweir         GetControl().SetNoRotation();
557*cdf0e10cSrcweir }
558*cdf0e10cSrcweir 
559*cdf0e10cSrcweir sal_Int32 DialControlWrapper::GetControlValue() const
560*cdf0e10cSrcweir {
561*cdf0e10cSrcweir     return GetControl().GetRotation();
562*cdf0e10cSrcweir }
563*cdf0e10cSrcweir 
564*cdf0e10cSrcweir void DialControlWrapper::SetControlValue( sal_Int32 nValue )
565*cdf0e10cSrcweir {
566*cdf0e10cSrcweir     GetControl().SetRotation( nValue );
567*cdf0e10cSrcweir }
568*cdf0e10cSrcweir 
569*cdf0e10cSrcweir // ============================================================================
570*cdf0e10cSrcweir 
571*cdf0e10cSrcweir } // namespace svx
572*cdf0e10cSrcweir 
573