Update ooo320-m1
[ooovba.git] / chart2 / source / controller / dialogs / res_Trendline.cxx
blobfbc35bca53ee95bded5e24bd76836ee1905ed3c8
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: res_Trendline.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_chart2.hxx"
34 #include "dlg_InsertTrendline.hrc"
35 #include "res_Trendline.hxx"
36 #include "res_Trendline_IDs.hrc"
37 #include "ResId.hxx"
38 #include "Strings.hrc"
39 #include "Bitmaps.hrc"
40 #include "Bitmaps_HC.hrc"
41 #include "chartview/ChartSfxItemIds.hxx"
43 #include <vector>
44 #include <algorithm>
46 // macro for selecting a normal or high contrast bitmap the stack variable
47 // bIsHighContrast must exist and reflect the correct state
48 #define SELECT_IMAGE(name) Image( SchResId( bIsHighContrast ? name ## _HC : name ))
50 namespace
52 template< class T >
53 long lcl_getRightEdge( T & rControl )
55 return rControl.CalcMinimumSize().Width() + rControl.GetPosPixel().X() - rControl.GetParent()->GetPosPixel().X();
58 template< class T >
59 void lcl_AdjustControlSize( T & rControl )
61 Size aSize( rControl.GetSizePixel());
62 aSize.setWidth( rControl.CalcMinimumSize().Width());
63 rControl.SetSizePixel( aSize );
66 void lcl_AdjustControlSize( Control & rControl, long nRightEdge )
68 Size aSize( rControl.GetSizePixel());
69 Point aPosition( rControl.GetPosPixel());
70 aSize.setWidth( nRightEdge - aPosition.getX());
71 rControl.SetSizePixel( aSize );
74 } // anonymous namespace
76 namespace chart
79 enum StatTrendLine
81 TRENDLINE_NONE,
82 TRENDLINE_LINE,
83 TRENDLINE_LOG,
84 TRENDLINE_EXP,
85 TRENDLINE_POW
88 TrendlineResources::TrendlineResources( Window * pParent, const SfxItemSet& rInAttrs, bool bNoneAvailable ) :
89 m_aFLType( pParent, SchResId( FL_TYPE )),
91 m_aRBNone( pParent, SchResId( RB_NONE )),
92 m_aRBLinear( pParent, SchResId( RB_LINEAR )),
93 m_aRBLogarithmic( pParent, SchResId( RB_LOGARITHMIC )),
94 m_aRBExponential( pParent, SchResId( RB_EXPONENTIAL )),
95 m_aRBPower( pParent, SchResId( RB_POWER )),
97 m_aFINone( pParent, SchResId( FI_NONE )),
98 m_aFILinear( pParent, SchResId( FI_LINEAR )),
99 m_aFILogarithmic( pParent, SchResId( FI_LOGARITHMIC )),
100 m_aFIExponential( pParent, SchResId( FI_EXPONENTIAL )),
101 m_aFIPower( pParent, SchResId( FI_POWER )),
103 m_aFLEquation( pParent, SchResId( FL_EQUATION )),
104 m_aCBShowEquation( pParent, SchResId( CB_SHOW_EQUATION )),
105 m_aCBShowCorrelationCoeff( pParent, SchResId( CB_SHOW_CORRELATION_COEFF )),
106 m_eTrendLineType( CHREGRESS_NONE ),
107 m_bNoneAvailable( bNoneAvailable ),
108 m_bTrendLineUnique( true )
110 FillValueSets();
112 if( m_bNoneAvailable )
113 m_aRBNone.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
114 else
115 m_aRBNone.Hide();
117 m_aRBLinear.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
118 m_aRBLogarithmic.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
119 m_aRBExponential.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
120 m_aRBPower.SetClickHdl( LINK(this, TrendlineResources, SelectTrendLine ));
122 Reset( rInAttrs );
123 UpdateControlStates();
126 TrendlineResources::~TrendlineResources()
129 long TrendlineResources::adjustControlSizes()
131 // calculate right edge
132 ::std::vector< long > aControlRightEdges;
133 aControlRightEdges.push_back( lcl_getRightEdge( m_aRBNone ));
134 aControlRightEdges.push_back( lcl_getRightEdge( m_aRBLinear ));
135 aControlRightEdges.push_back( lcl_getRightEdge( m_aRBLogarithmic ));
136 aControlRightEdges.push_back( lcl_getRightEdge( m_aRBExponential ));
137 aControlRightEdges.push_back( lcl_getRightEdge( m_aRBPower ));
138 aControlRightEdges.push_back( lcl_getRightEdge( m_aCBShowEquation ));
139 aControlRightEdges.push_back( lcl_getRightEdge( m_aCBShowCorrelationCoeff ));
141 lcl_AdjustControlSize( m_aRBNone );
142 lcl_AdjustControlSize( m_aRBLinear );
143 lcl_AdjustControlSize( m_aRBLogarithmic );
144 lcl_AdjustControlSize( m_aRBExponential );
145 lcl_AdjustControlSize( m_aRBPower );
146 lcl_AdjustControlSize( m_aCBShowEquation );
147 lcl_AdjustControlSize( m_aCBShowCorrelationCoeff );
149 // Note: FixedLine has no CalcMinimumSize, workaround: use a FixedText
150 FixedText aDummyTextCtrl( m_aFLType.GetParent());
151 aDummyTextCtrl.SetText( m_aFLType.GetText());
152 aControlRightEdges.push_back( lcl_getRightEdge( aDummyTextCtrl ));
153 aDummyTextCtrl.SetText( m_aFLEquation.GetText());
154 aControlRightEdges.push_back( lcl_getRightEdge( aDummyTextCtrl ));
156 long nRightEdgeOfControls = *(::std::max_element( aControlRightEdges.begin(), aControlRightEdges.end()));
157 // leave some more space after the longest text
158 nRightEdgeOfControls += m_aFLType.LogicToPixel( Size( 6, 0 ), MapMode( MAP_APPFONT )).getWidth();
160 lcl_AdjustControlSize( m_aFLType, nRightEdgeOfControls );
161 lcl_AdjustControlSize( m_aFLEquation, nRightEdgeOfControls );
163 return nRightEdgeOfControls;
166 IMPL_LINK( TrendlineResources, SelectTrendLine, RadioButton *, pRadioButton )
168 if( pRadioButton == &m_aRBLinear )
169 m_eTrendLineType = CHREGRESS_LINEAR;
170 else if( pRadioButton == &m_aRBLogarithmic )
171 m_eTrendLineType = CHREGRESS_LOG;
172 else if( pRadioButton == &m_aRBExponential )
173 m_eTrendLineType = CHREGRESS_EXP;
174 else if( pRadioButton == &m_aRBPower )
175 m_eTrendLineType = CHREGRESS_POWER;
176 else if( pRadioButton == &m_aRBNone )
178 OSL_ASSERT( m_bNoneAvailable );
179 m_eTrendLineType = CHREGRESS_NONE;
181 m_bTrendLineUnique = true;
183 UpdateControlStates();
185 return 0;
188 void TrendlineResources::Reset( const SfxItemSet& rInAttrs )
190 const SfxPoolItem *pPoolItem = NULL;
191 SfxItemState aState = SFX_ITEM_UNKNOWN;
193 aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_TYPE, TRUE, &pPoolItem );
194 m_bTrendLineUnique = ( aState != SFX_ITEM_DONTCARE );
195 if( aState == SFX_ITEM_SET )
197 const SvxChartRegressItem * pItem = dynamic_cast< const SvxChartRegressItem * >( pPoolItem );
198 if( pItem )
199 m_eTrendLineType = pItem->GetValue();
202 aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SHOW_EQUATION, TRUE, &pPoolItem );
203 if( aState == SFX_ITEM_DONTCARE )
205 m_aCBShowEquation.EnableTriState( TRUE );
206 m_aCBShowEquation.SetState( STATE_DONTKNOW );
208 else
210 m_aCBShowEquation.EnableTriState( FALSE );
211 if( aState == SFX_ITEM_SET )
212 m_aCBShowEquation.Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
215 aState = rInAttrs.GetItemState( SCHATTR_REGRESSION_SHOW_COEFF, TRUE, &pPoolItem );
216 if( aState == SFX_ITEM_DONTCARE )
218 m_aCBShowCorrelationCoeff.EnableTriState( TRUE );
219 m_aCBShowCorrelationCoeff.SetState( STATE_DONTKNOW );
221 else
223 m_aCBShowCorrelationCoeff.EnableTriState( FALSE );
224 if( aState == SFX_ITEM_SET )
225 m_aCBShowCorrelationCoeff.Check( static_cast< const SfxBoolItem * >( pPoolItem )->GetValue());
228 if( m_bTrendLineUnique )
230 switch( m_eTrendLineType )
232 case CHREGRESS_LINEAR :
233 m_aRBLinear.Check();
234 break;
235 case CHREGRESS_LOG :
236 m_aRBLogarithmic.Check();
237 break;
238 case CHREGRESS_EXP :
239 m_aRBExponential.Check();
240 break;
241 case CHREGRESS_POWER :
242 m_aRBPower.Check();
243 break;
244 case CHREGRESS_NONE:
245 OSL_ASSERT( m_bNoneAvailable );
246 m_aRBNone.Check();
247 break;
252 BOOL TrendlineResources::FillItemSet(SfxItemSet& rOutAttrs) const
254 if( m_bTrendLineUnique )
255 rOutAttrs.Put( SvxChartRegressItem( m_eTrendLineType, SCHATTR_REGRESSION_TYPE ));
256 if( m_aCBShowEquation.GetState() != STATE_DONTKNOW )
257 rOutAttrs.Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_EQUATION, m_aCBShowEquation.IsChecked() ));
258 if( m_aCBShowCorrelationCoeff.GetState() != STATE_DONTKNOW )
259 rOutAttrs.Put( SfxBoolItem( SCHATTR_REGRESSION_SHOW_COEFF, m_aCBShowCorrelationCoeff.IsChecked() ));
260 return TRUE;
263 void TrendlineResources::FillValueSets()
265 bool bIsHighContrast = ( true && m_aFLType.GetDisplayBackground().GetColor().IsDark() );
267 if( m_bNoneAvailable )
268 m_aFINone.SetImage( SELECT_IMAGE( BMP_REGRESSION_NONE ));
269 m_aFILinear.SetImage( SELECT_IMAGE( BMP_REGRESSION_LINEAR ));
270 m_aFILogarithmic.SetImage( SELECT_IMAGE( BMP_REGRESSION_LOG ));
271 m_aFIExponential.SetImage( SELECT_IMAGE( BMP_REGRESSION_EXP ));
272 m_aFIPower.SetImage( SELECT_IMAGE( BMP_REGRESSION_POWER ));
275 void TrendlineResources::UpdateControlStates()
277 if( m_bNoneAvailable )
279 bool bEnableEquationControls = !m_bTrendLineUnique || (m_eTrendLineType != CHREGRESS_NONE);
280 m_aCBShowEquation.Enable( bEnableEquationControls );
281 m_aCBShowCorrelationCoeff.Enable( bEnableEquationControls );
285 } // namespace chart