1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: dpgroupdlg.cxx,v $
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_sc.hxx"
34 #ifdef SC_DLLIMPLEMENTATION
35 #undef SC_DLLIMPLEMENTATION
39 #include "dpgroupdlg.hxx"
40 #ifndef SC_DPGROUPDLG_HRC
41 #include "dpgroupdlg.hrc"
43 #include <tools/resary.hxx>
44 #include "scresid.hxx"
48 #include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
50 // ============================================================================
54 /** Date part flags in order of the list box entries. */
55 static const sal_Int32 spnDateParts
[] =
57 com::sun::star::sheet::DataPilotFieldGroupBy::SECONDS
,
58 com::sun::star::sheet::DataPilotFieldGroupBy::MINUTES
,
59 com::sun::star::sheet::DataPilotFieldGroupBy::HOURS
,
60 com::sun::star::sheet::DataPilotFieldGroupBy::DAYS
,
61 com::sun::star::sheet::DataPilotFieldGroupBy::MONTHS
,
62 com::sun::star::sheet::DataPilotFieldGroupBy::QUARTERS
,
63 com::sun::star::sheet::DataPilotFieldGroupBy::YEARS
68 // ============================================================================
70 ScDPGroupEditHelper::ScDPGroupEditHelper( RadioButton
& rRbAuto
, RadioButton
& rRbMan
, Edit
& rEdValue
) :
75 mrRbAuto
.SetClickHdl( LINK( this, ScDPGroupEditHelper
, ClickHdl
) );
76 mrRbMan
.SetClickHdl( LINK( this, ScDPGroupEditHelper
, ClickHdl
) );
79 bool ScDPGroupEditHelper::IsAuto() const
81 return mrRbAuto
.IsChecked();
84 double ScDPGroupEditHelper::GetValue() const
87 if( !ImplGetValue( fValue
) )
92 void ScDPGroupEditHelper::SetValue( bool bAuto
, double fValue
)
97 ClickHdl( &mrRbAuto
);
102 ClickHdl( &mrRbMan
);
104 ImplSetValue( fValue
);
107 IMPL_LINK( ScDPGroupEditHelper
, ClickHdl
, RadioButton
*, pButton
)
109 if( pButton
== &mrRbAuto
)
111 // disable edit field on clicking "automatic" radio button
114 else if( pButton
== &mrRbMan
)
116 // enable and set focus to edit field on clicking "manual" radio button
118 mrEdValue
.GrabFocus();
123 // ----------------------------------------------------------------------------
125 ScDPNumGroupEditHelper::ScDPNumGroupEditHelper(
126 RadioButton
& rRbAuto
, RadioButton
& rRbMan
, ScDoubleField
& rEdValue
) :
127 ScDPGroupEditHelper( rRbAuto
, rRbMan
, rEdValue
),
128 mrEdValue( rEdValue
)
132 bool ScDPNumGroupEditHelper::ImplGetValue( double& rfValue
) const
134 return mrEdValue
.GetValue( rfValue
);
137 void ScDPNumGroupEditHelper::ImplSetValue( double fValue
)
139 mrEdValue
.SetValue( fValue
);
142 // ----------------------------------------------------------------------------
144 ScDPDateGroupEditHelper::ScDPDateGroupEditHelper(
145 RadioButton
& rRbAuto
, RadioButton
& rRbMan
, DateField
& rEdValue
, const Date
& rNullDate
) :
146 ScDPGroupEditHelper( rRbAuto
, rRbMan
, rEdValue
),
147 mrEdValue( rEdValue
),
148 maNullDate( rNullDate
)
152 bool ScDPDateGroupEditHelper::ImplGetValue( double& rfValue
) const
154 rfValue
= mrEdValue
.GetDate() - maNullDate
;
158 void ScDPDateGroupEditHelper::ImplSetValue( double fValue
)
160 Date
aDate( maNullDate
);
161 aDate
+= static_cast< sal_Int32
>( fValue
);
162 mrEdValue
.SetDate( aDate
);
165 // ============================================================================
166 // ============================================================================
168 ScDPNumGroupDlg::ScDPNumGroupDlg( Window
* pParent
, const ScDPNumGroupInfo
& rInfo
) :
169 ModalDialog ( pParent
, ScResId( RID_SCDLG_DPNUMGROUP
) ),
170 maFlStart ( this, ScResId( FL_START
) ),
171 maRbAutoStart ( this, ScResId( RB_AUTOSTART
) ),
172 maRbManStart ( this, ScResId( RB_MANSTART
) ),
173 maEdStart ( this, ScResId( ED_START
) ),
174 maFlEnd ( this, ScResId( FL_END
) ),
175 maRbAutoEnd ( this, ScResId( RB_AUTOEND
) ),
176 maRbManEnd ( this, ScResId( RB_MANEND
) ),
177 maEdEnd ( this, ScResId( ED_END
) ),
178 maFlBy ( this, ScResId( FL_BY
) ),
179 maEdBy ( this, ScResId( ED_BY
) ),
180 maBtnOk ( this, ScResId( BTN_OK
) ),
181 maBtnCancel ( this, ScResId( BTN_CANCEL
) ),
182 maBtnHelp ( this, ScResId( BTN_HELP
) ),
183 maStartHelper ( maRbAutoStart
, maRbManStart
, maEdStart
),
184 maEndHelper ( maRbAutoEnd
, maRbManEnd
, maEdEnd
)
188 maStartHelper
.SetValue( rInfo
.AutoStart
, rInfo
.Start
);
189 maEndHelper
.SetValue( rInfo
.AutoEnd
, rInfo
.End
);
190 maEdBy
.SetValue( (rInfo
.Step
<= 0.0) ? 1.0 : rInfo
.Step
);
192 /* Set the initial focus, currently it is somewhere after calling all the radio
193 button click handlers. Now the first enabled editable control is focused. */
194 if( maEdStart
.IsEnabled() )
195 maEdStart
.GrabFocus();
196 else if( maEdEnd
.IsEnabled() )
202 ScDPNumGroupInfo
ScDPNumGroupDlg::GetGroupInfo() const
204 ScDPNumGroupInfo aInfo
;
205 aInfo
.Enable
= sal_True
;
206 aInfo
.DateValues
= sal_False
;
207 aInfo
.AutoStart
= maStartHelper
.IsAuto();
208 aInfo
.AutoEnd
= maEndHelper
.IsAuto();
210 // get values and silently auto-correct them, if they are not valid
211 // TODO: error messages in OK event?
212 aInfo
.Start
= maStartHelper
.GetValue();
213 aInfo
.End
= maEndHelper
.GetValue();
214 if( !maEdBy
.GetValue( aInfo
.Step
) || (aInfo
.Step
<= 0.0) )
216 if( aInfo
.End
<= aInfo
.Start
)
217 aInfo
.End
= aInfo
.Start
+ aInfo
.Step
;
222 // ============================================================================
224 ScDPDateGroupDlg::ScDPDateGroupDlg( Window
* pParent
,
225 const ScDPNumGroupInfo
& rInfo
, sal_Int32 nDatePart
, const Date
& rNullDate
) :
226 ModalDialog ( pParent
, ScResId( RID_SCDLG_DPDATEGROUP
) ),
227 maFlStart ( this, ScResId( FL_START
) ),
228 maRbAutoStart ( this, ScResId( RB_AUTOSTART
) ),
229 maRbManStart ( this, ScResId( RB_MANSTART
) ),
230 maEdStart ( this, ScResId( ED_START
) ),
231 maFlEnd ( this, ScResId( FL_END
) ),
232 maRbAutoEnd ( this, ScResId( RB_AUTOEND
) ),
233 maRbManEnd ( this, ScResId( RB_MANEND
) ),
234 maEdEnd ( this, ScResId( ED_END
) ),
235 maFlBy ( this, ScResId( FL_BY
) ),
236 maRbNumDays ( this, ScResId( RB_NUMDAYS
) ),
237 maRbUnits ( this, ScResId( RB_UNITS
) ),
238 maEdNumDays ( this, ScResId( ED_NUMDAYS
) ),
239 maLbUnits ( this, ScResId( LB_UNITS
) ),
240 maBtnOk ( this, ScResId( BTN_OK
) ),
241 maBtnCancel ( this, ScResId( BTN_CANCEL
) ),
242 maBtnHelp ( this, ScResId( BTN_HELP
) ),
243 maStartHelper ( maRbAutoStart
, maRbManStart
, maEdStart
, rNullDate
),
244 maEndHelper ( maRbAutoEnd
, maRbManEnd
, maEdEnd
, rNullDate
)
246 maLbUnits
.SetHelpId( HID_SC_DPDATEGROUP_LB
);
247 ResStringArray
aArr( ScResId( STR_UNITS
) );
248 for( USHORT nIdx
= 0, nCount
= sal::static_int_cast
<USHORT
>(aArr
.Count()); nIdx
< nCount
; ++nIdx
)
249 maLbUnits
.InsertEntry( aArr
.GetString( nIdx
) );
253 maEdStart
.SetShowDateCentury( TRUE
);
254 maEdEnd
.SetShowDateCentury( TRUE
);
256 maStartHelper
.SetValue( rInfo
.AutoStart
, rInfo
.Start
);
257 maEndHelper
.SetValue( rInfo
.AutoEnd
, rInfo
.End
);
260 nDatePart
= com::sun::star::sheet::DataPilotFieldGroupBy::MONTHS
;
261 for( ULONG nIdx
= 0, nCount
= maLbUnits
.GetEntryCount(); nIdx
< nCount
; ++nIdx
)
262 maLbUnits
.CheckEntryPos( static_cast< USHORT
>( nIdx
), (nDatePart
& spnDateParts
[ nIdx
]) != 0 );
264 if( rInfo
.DateValues
)
267 ClickHdl( &maRbNumDays
);
269 double fNumDays
= rInfo
.Step
;
272 else if( fNumDays
> 32767.0 )
274 maEdNumDays
.SetValue( static_cast< long >( fNumDays
) );
279 ClickHdl( &maRbUnits
);
282 /* Set the initial focus, currently it is somewhere after calling all the radio
283 button click handlers. Now the first enabled editable control is focused. */
284 if( maEdStart
.IsEnabled() )
285 maEdStart
.GrabFocus();
286 else if( maEdEnd
.IsEnabled() )
288 else if( maEdNumDays
.IsEnabled() )
289 maEdNumDays
.GrabFocus();
290 else if( maLbUnits
.IsEnabled() )
291 maLbUnits
.GrabFocus();
293 maRbNumDays
.SetClickHdl( LINK( this, ScDPDateGroupDlg
, ClickHdl
) );
294 maRbUnits
.SetClickHdl( LINK( this, ScDPDateGroupDlg
, ClickHdl
) );
295 maLbUnits
.SetCheckButtonHdl( LINK( this, ScDPDateGroupDlg
, CheckHdl
) );
298 ScDPNumGroupInfo
ScDPDateGroupDlg::GetGroupInfo() const
300 ScDPNumGroupInfo aInfo
;
301 aInfo
.Enable
= sal_True
;
302 aInfo
.DateValues
= maRbNumDays
.IsChecked();
303 aInfo
.AutoStart
= maStartHelper
.IsAuto();
304 aInfo
.AutoEnd
= maEndHelper
.IsAuto();
306 // get values and silently auto-correct them, if they are not valid
307 // TODO: error messages in OK event?
308 aInfo
.Start
= maStartHelper
.GetValue();
309 aInfo
.End
= maEndHelper
.GetValue();
310 sal_Int64 nNumDays
= maEdNumDays
.GetValue();
311 aInfo
.Step
= static_cast<double>( aInfo
.DateValues
? nNumDays
: 0L );
312 if( aInfo
.End
<= aInfo
.Start
)
313 aInfo
.End
= aInfo
.Start
+ nNumDays
;
318 sal_Int32
ScDPDateGroupDlg::GetDatePart() const
320 // return DAYS for special "number of days" mode
321 if( maRbNumDays
.IsChecked() )
322 return com::sun::star::sheet::DataPilotFieldGroupBy::DAYS
;
324 // return listbox contents for "units" mode
325 sal_Int32 nDatePart
= 0;
326 for( ULONG nIdx
= 0, nCount
= maLbUnits
.GetEntryCount(); nIdx
< nCount
; ++nIdx
)
327 if( maLbUnits
.IsChecked( static_cast< USHORT
>( nIdx
) ) )
328 nDatePart
|= spnDateParts
[ nIdx
];
332 IMPL_LINK( ScDPDateGroupDlg
, ClickHdl
, RadioButton
*, pButton
)
334 if( pButton
== &maRbNumDays
)
337 // enable and set focus to edit field on clicking "num of days" radio button
338 maEdNumDays
.Enable();
339 maEdNumDays
.GrabFocus();
342 else if( pButton
== &maRbUnits
)
344 maEdNumDays
.Disable();
345 // enable and set focus to listbox on clicking "units" radio button
347 maLbUnits
.GrabFocus();
348 // disable OK button if no date part selected
349 CheckHdl( &maLbUnits
);
354 IMPL_LINK( ScDPDateGroupDlg
, CheckHdl
, SvxCheckListBox
*, pListBox
)
356 // enable/disable OK button on modifying check list box
357 if( pListBox
== &maLbUnits
)
358 maBtnOk
.Enable( maLbUnits
.GetCheckedEntryCount() > 0 );
362 // ============================================================================