Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #ifdef SC_DLLIMPLEMENTATION
31 : : #undef SC_DLLIMPLEMENTATION
32 : : #endif
33 : :
34 : :
35 : : #include "dpgroupdlg.hxx"
36 : : #include "dpgroupdlg.hrc"
37 : : #include <tools/resary.hxx>
38 : : #include "scresid.hxx"
39 : : #include "sc.hrc"
40 : : #include <com/sun/star/sheet/DataPilotFieldGroupBy.hpp>
41 : :
42 : : // ============================================================================
43 : :
44 : : namespace {
45 : :
46 : : /** Date part flags in order of the list box entries. */
47 : : static const sal_Int32 spnDateParts[] =
48 : : {
49 : : com::sun::star::sheet::DataPilotFieldGroupBy::SECONDS,
50 : : com::sun::star::sheet::DataPilotFieldGroupBy::MINUTES,
51 : : com::sun::star::sheet::DataPilotFieldGroupBy::HOURS,
52 : : com::sun::star::sheet::DataPilotFieldGroupBy::DAYS,
53 : : com::sun::star::sheet::DataPilotFieldGroupBy::MONTHS,
54 : : com::sun::star::sheet::DataPilotFieldGroupBy::QUARTERS,
55 : : com::sun::star::sheet::DataPilotFieldGroupBy::YEARS
56 : : };
57 : :
58 : : } // namespace
59 : :
60 : : // ============================================================================
61 : :
62 : 0 : ScDPGroupEditHelper::ScDPGroupEditHelper( RadioButton& rRbAuto, RadioButton& rRbMan, Edit& rEdValue ) :
63 : : mrRbAuto( rRbAuto ),
64 : : mrRbMan( rRbMan ),
65 : 0 : mrEdValue( rEdValue )
66 : : {
67 : 0 : mrRbAuto.SetClickHdl( LINK( this, ScDPGroupEditHelper, ClickHdl ) );
68 : 0 : mrRbMan.SetClickHdl( LINK( this, ScDPGroupEditHelper, ClickHdl ) );
69 : 0 : }
70 : :
71 : 0 : bool ScDPGroupEditHelper::IsAuto() const
72 : : {
73 : 0 : return mrRbAuto.IsChecked();
74 : : }
75 : :
76 : 0 : double ScDPGroupEditHelper::GetValue() const
77 : : {
78 : : double fValue;
79 : 0 : if( !ImplGetValue( fValue ) )
80 : 0 : fValue = 0.0;
81 : 0 : return fValue;
82 : : }
83 : :
84 : 0 : void ScDPGroupEditHelper::SetValue( bool bAuto, double fValue )
85 : : {
86 : 0 : if( bAuto )
87 : : {
88 : 0 : mrRbAuto.Check();
89 : 0 : ClickHdl( &mrRbAuto );
90 : : }
91 : : else
92 : : {
93 : 0 : mrRbMan.Check();
94 : 0 : ClickHdl( &mrRbMan );
95 : : }
96 : 0 : ImplSetValue( fValue );
97 : 0 : }
98 : :
99 : 0 : IMPL_LINK( ScDPGroupEditHelper, ClickHdl, RadioButton*, pButton )
100 : : {
101 : 0 : if( pButton == &mrRbAuto )
102 : : {
103 : : // disable edit field on clicking "automatic" radio button
104 : 0 : mrEdValue.Disable();
105 : : }
106 : 0 : else if( pButton == &mrRbMan )
107 : : {
108 : : // enable and set focus to edit field on clicking "manual" radio button
109 : 0 : mrEdValue.Enable();
110 : 0 : mrEdValue.GrabFocus();
111 : : }
112 : 0 : return 0;
113 : : }
114 : :
115 : : // ----------------------------------------------------------------------------
116 : :
117 : 0 : ScDPNumGroupEditHelper::ScDPNumGroupEditHelper(
118 : : RadioButton& rRbAuto, RadioButton& rRbMan, ScDoubleField& rEdValue ) :
119 : : ScDPGroupEditHelper( rRbAuto, rRbMan, rEdValue ),
120 : 0 : mrEdValue( rEdValue )
121 : : {
122 : 0 : }
123 : :
124 : 0 : bool ScDPNumGroupEditHelper::ImplGetValue( double& rfValue ) const
125 : : {
126 : 0 : return mrEdValue.GetValue( rfValue );
127 : : }
128 : :
129 : 0 : void ScDPNumGroupEditHelper::ImplSetValue( double fValue )
130 : : {
131 : 0 : mrEdValue.SetValue( fValue );
132 : 0 : }
133 : :
134 : : // ----------------------------------------------------------------------------
135 : :
136 : 0 : ScDPDateGroupEditHelper::ScDPDateGroupEditHelper(
137 : : RadioButton& rRbAuto, RadioButton& rRbMan, DateField& rEdValue, const Date& rNullDate ) :
138 : : ScDPGroupEditHelper( rRbAuto, rRbMan, rEdValue ),
139 : : mrEdValue( rEdValue ),
140 : 0 : maNullDate( rNullDate )
141 : : {
142 : 0 : }
143 : :
144 : 0 : bool ScDPDateGroupEditHelper::ImplGetValue( double& rfValue ) const
145 : : {
146 : 0 : rfValue = mrEdValue.GetDate() - maNullDate;
147 : 0 : return true;
148 : : }
149 : :
150 : 0 : void ScDPDateGroupEditHelper::ImplSetValue( double fValue )
151 : : {
152 : 0 : Date aDate( maNullDate );
153 : 0 : aDate += static_cast< sal_Int32 >( fValue );
154 : 0 : mrEdValue.SetDate( aDate );
155 : 0 : }
156 : :
157 : : // ============================================================================
158 : : // ============================================================================
159 : :
160 : 0 : ScDPNumGroupDlg::ScDPNumGroupDlg( Window* pParent, const ScDPNumGroupInfo& rInfo ) :
161 : : ModalDialog ( pParent, ScResId( RID_SCDLG_DPNUMGROUP ) ),
162 : : maFlStart ( this, ScResId( FL_START ) ),
163 : : maRbAutoStart ( this, ScResId( RB_AUTOSTART ) ),
164 : : maRbManStart ( this, ScResId( RB_MANSTART ) ),
165 : : maEdStart ( this, ScResId( ED_START ) ),
166 : : maFlEnd ( this, ScResId( FL_END ) ),
167 : : maRbAutoEnd ( this, ScResId( RB_AUTOEND ) ),
168 : : maRbManEnd ( this, ScResId( RB_MANEND ) ),
169 : : maEdEnd ( this, ScResId( ED_END ) ),
170 : : maFlBy ( this, ScResId( FL_BY ) ),
171 : : maEdBy ( this, ScResId( ED_BY ) ),
172 : : maBtnOk ( this, ScResId( BTN_OK ) ),
173 : : maBtnCancel ( this, ScResId( BTN_CANCEL ) ),
174 : : maBtnHelp ( this, ScResId( BTN_HELP ) ),
175 : : maStartHelper ( maRbAutoStart, maRbManStart, maEdStart ),
176 : 0 : maEndHelper ( maRbAutoEnd, maRbManEnd, maEdEnd )
177 : : {
178 : 0 : FreeResource();
179 : :
180 : 0 : maStartHelper.SetValue( rInfo.mbAutoStart, rInfo.mfStart );
181 : 0 : maEndHelper.SetValue( rInfo.mbAutoEnd, rInfo.mfEnd );
182 : 0 : maEdBy.SetValue( (rInfo.mfStep <= 0.0) ? 1.0 : rInfo.mfStep );
183 : :
184 : : /* Set the initial focus, currently it is somewhere after calling all the radio
185 : : button click handlers. Now the first enabled editable control is focused. */
186 : 0 : if( maEdStart.IsEnabled() )
187 : 0 : maEdStart.GrabFocus();
188 : 0 : else if( maEdEnd.IsEnabled() )
189 : 0 : maEdEnd.GrabFocus();
190 : : else
191 : 0 : maEdBy.GrabFocus();
192 : 0 : }
193 : :
194 : 0 : ScDPNumGroupInfo ScDPNumGroupDlg::GetGroupInfo() const
195 : : {
196 : 0 : ScDPNumGroupInfo aInfo;
197 : 0 : aInfo.mbEnable = sal_True;
198 : 0 : aInfo.mbDateValues = false;
199 : 0 : aInfo.mbAutoStart = maStartHelper.IsAuto();
200 : 0 : aInfo.mbAutoEnd = maEndHelper.IsAuto();
201 : :
202 : : // get values and silently auto-correct them, if they are not valid
203 : : // TODO: error messages in OK event?
204 : 0 : aInfo.mfStart = maStartHelper.GetValue();
205 : 0 : aInfo.mfEnd = maEndHelper.GetValue();
206 : 0 : if( !maEdBy.GetValue( aInfo.mfStep ) || (aInfo.mfStep <= 0.0) )
207 : 0 : aInfo.mfStep = 1.0;
208 : 0 : if( aInfo.mfEnd <= aInfo.mfStart )
209 : 0 : aInfo.mfEnd = aInfo.mfStart + aInfo.mfStep;
210 : :
211 : 0 : return aInfo;
212 : : }
213 : :
214 : : // ============================================================================
215 : :
216 : 0 : ScDPDateGroupDlg::ScDPDateGroupDlg( Window* pParent,
217 : : const ScDPNumGroupInfo& rInfo, sal_Int32 nDatePart, const Date& rNullDate ) :
218 : : ModalDialog ( pParent, ScResId( RID_SCDLG_DPDATEGROUP ) ),
219 : : maFlStart ( this, ScResId( FL_START ) ),
220 : : maRbAutoStart ( this, ScResId( RB_AUTOSTART ) ),
221 : : maRbManStart ( this, ScResId( RB_MANSTART ) ),
222 : : maEdStart ( this, ScResId( ED_START ) ),
223 : : maFlEnd ( this, ScResId( FL_END ) ),
224 : : maRbAutoEnd ( this, ScResId( RB_AUTOEND ) ),
225 : : maRbManEnd ( this, ScResId( RB_MANEND ) ),
226 : : maEdEnd ( this, ScResId( ED_END ) ),
227 : : maFlBy ( this, ScResId( FL_BY ) ),
228 : : maRbNumDays ( this, ScResId( RB_NUMDAYS ) ),
229 : : maRbUnits ( this, ScResId( RB_UNITS ) ),
230 : : maEdNumDays ( this, ScResId( ED_NUMDAYS ) ),
231 : : maLbUnits ( this, ScResId( LB_UNITS ) ),
232 : : maBtnOk ( this, ScResId( BTN_OK ) ),
233 : : maBtnCancel ( this, ScResId( BTN_CANCEL ) ),
234 : : maBtnHelp ( this, ScResId( BTN_HELP ) ),
235 : : maStartHelper ( maRbAutoStart, maRbManStart, maEdStart, rNullDate ),
236 : 0 : maEndHelper ( maRbAutoEnd, maRbManEnd, maEdEnd, rNullDate )
237 : : {
238 : 0 : maLbUnits.SetHelpId( HID_SC_DPDATEGROUP_LB );
239 : 0 : ResStringArray aArr( ScResId( STR_UNITS ) );
240 : 0 : for( sal_uInt16 nIdx = 0, nCount = sal::static_int_cast<sal_uInt16>(aArr.Count()); nIdx < nCount; ++nIdx )
241 : 0 : maLbUnits.InsertEntry( aArr.GetString( nIdx ) );
242 : :
243 : 0 : FreeResource();
244 : :
245 : 0 : maEdStart.SetShowDateCentury( sal_True );
246 : 0 : maEdEnd.SetShowDateCentury( sal_True );
247 : :
248 : 0 : maStartHelper.SetValue( rInfo.mbAutoStart, rInfo.mfStart );
249 : 0 : maEndHelper.SetValue( rInfo.mbAutoEnd, rInfo.mfEnd );
250 : :
251 : 0 : if( nDatePart == 0 )
252 : 0 : nDatePart = com::sun::star::sheet::DataPilotFieldGroupBy::MONTHS;
253 : 0 : for( sal_uLong nIdx = 0, nCount = maLbUnits.GetEntryCount(); nIdx < nCount; ++nIdx )
254 : 0 : maLbUnits.CheckEntryPos( static_cast< sal_uInt16 >( nIdx ), (nDatePart & spnDateParts[ nIdx ]) != 0 );
255 : :
256 : 0 : if( rInfo.mbDateValues )
257 : : {
258 : 0 : maRbNumDays.Check();
259 : 0 : ClickHdl( &maRbNumDays );
260 : :
261 : 0 : double fNumDays = rInfo.mfStep;
262 : 0 : if( fNumDays < 1.0 )
263 : 0 : fNumDays = 1.0;
264 : 0 : else if( fNumDays > 32767.0 )
265 : 0 : fNumDays = 32767.0;
266 : 0 : maEdNumDays.SetValue( static_cast< long >( fNumDays ) );
267 : : }
268 : : else
269 : : {
270 : 0 : maRbUnits.Check();
271 : 0 : ClickHdl( &maRbUnits );
272 : : }
273 : :
274 : : /* Set the initial focus, currently it is somewhere after calling all the radio
275 : : button click handlers. Now the first enabled editable control is focused. */
276 : 0 : if( maEdStart.IsEnabled() )
277 : 0 : maEdStart.GrabFocus();
278 : 0 : else if( maEdEnd.IsEnabled() )
279 : 0 : maEdEnd.GrabFocus();
280 : 0 : else if( maEdNumDays.IsEnabled() )
281 : 0 : maEdNumDays.GrabFocus();
282 : 0 : else if( maLbUnits.IsEnabled() )
283 : 0 : maLbUnits.GrabFocus();
284 : :
285 : 0 : maRbNumDays.SetClickHdl( LINK( this, ScDPDateGroupDlg, ClickHdl ) );
286 : 0 : maRbUnits.SetClickHdl( LINK( this, ScDPDateGroupDlg, ClickHdl ) );
287 : 0 : maLbUnits.SetCheckButtonHdl( LINK( this, ScDPDateGroupDlg, CheckHdl ) );
288 : 0 : }
289 : :
290 : 0 : ScDPNumGroupInfo ScDPDateGroupDlg::GetGroupInfo() const
291 : : {
292 : 0 : ScDPNumGroupInfo aInfo;
293 : 0 : aInfo.mbEnable = sal_True;
294 : 0 : aInfo.mbDateValues = maRbNumDays.IsChecked();
295 : 0 : aInfo.mbAutoStart = maStartHelper.IsAuto();
296 : 0 : aInfo.mbAutoEnd = maEndHelper.IsAuto();
297 : :
298 : : // get values and silently auto-correct them, if they are not valid
299 : : // TODO: error messages in OK event?
300 : 0 : aInfo.mfStart = maStartHelper.GetValue();
301 : 0 : aInfo.mfEnd = maEndHelper.GetValue();
302 : 0 : sal_Int64 nNumDays = maEdNumDays.GetValue();
303 : 0 : aInfo.mfStep = static_cast<double>( aInfo.mbDateValues ? nNumDays : 0L );
304 : 0 : if( aInfo.mfEnd <= aInfo.mfStart )
305 : 0 : aInfo.mfEnd = aInfo.mfStart + nNumDays;
306 : :
307 : 0 : return aInfo;
308 : : }
309 : :
310 : 0 : sal_Int32 ScDPDateGroupDlg::GetDatePart() const
311 : : {
312 : : // return DAYS for special "number of days" mode
313 : 0 : if( maRbNumDays.IsChecked() )
314 : 0 : return com::sun::star::sheet::DataPilotFieldGroupBy::DAYS;
315 : :
316 : : // return listbox contents for "units" mode
317 : 0 : sal_Int32 nDatePart = 0;
318 : 0 : for( sal_uLong nIdx = 0, nCount = maLbUnits.GetEntryCount(); nIdx < nCount; ++nIdx )
319 : 0 : if( maLbUnits.IsChecked( static_cast< sal_uInt16 >( nIdx ) ) )
320 : 0 : nDatePart |= spnDateParts[ nIdx ];
321 : 0 : return nDatePart;
322 : : }
323 : :
324 : 0 : IMPL_LINK( ScDPDateGroupDlg, ClickHdl, RadioButton*, pButton )
325 : : {
326 : 0 : if( pButton == &maRbNumDays )
327 : : {
328 : 0 : maLbUnits.Disable();
329 : : // enable and set focus to edit field on clicking "num of days" radio button
330 : 0 : maEdNumDays.Enable();
331 : 0 : maEdNumDays.GrabFocus();
332 : 0 : maBtnOk.Enable();
333 : : }
334 : 0 : else if( pButton == &maRbUnits )
335 : : {
336 : 0 : maEdNumDays.Disable();
337 : : // enable and set focus to listbox on clicking "units" radio button
338 : 0 : maLbUnits.Enable();
339 : 0 : maLbUnits.GrabFocus();
340 : : // disable OK button if no date part selected
341 : 0 : CheckHdl( &maLbUnits );
342 : : }
343 : 0 : return 0;
344 : : }
345 : :
346 : 0 : IMPL_LINK( ScDPDateGroupDlg, CheckHdl, SvxCheckListBox*, pListBox )
347 : : {
348 : : // enable/disable OK button on modifying check list box
349 : 0 : if( pListBox == &maLbUnits )
350 : 0 : maBtnOk.Enable( maLbUnits.GetCheckedEntryCount() > 0 );
351 : 0 : return 0;
352 : 0 : }
353 : :
354 : : // ============================================================================
355 : :
356 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|