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