1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
11 #include <sfx2/dispatch.hxx>
12 #include <svl/zforlist.hxx>
13 #include <svl/undo.hxx>
15 #include "formulacell.hxx"
16 #include "rangelst.hxx"
17 #include "scitems.hxx"
19 #include "document.hxx"
20 #include "uiitems.hxx"
21 #include "reffact.hxx"
22 #include "scresid.hxx"
23 #include "docfunc.hxx"
24 #include "strload.hxx"
26 #include "StatisticsInputOutputDialog.hxx"
28 ScRangeList
ScStatisticsInputOutputDialog::MakeColumnRangeList(SCTAB aTab
, ScAddress aStart
, ScAddress aEnd
)
30 ScRangeList aRangeList
;
31 for (SCCOL inCol
= aStart
.Col(); inCol
<= aEnd
.Col(); inCol
++)
33 ScRange
aColumnRange (
34 ScAddress(inCol
, aStart
.Row(), aTab
),
35 ScAddress(inCol
, aEnd
.Row(), aTab
) );
37 aRangeList
.Append(aColumnRange
);
42 ScRangeList
ScStatisticsInputOutputDialog::MakeRowRangeList(SCTAB aTab
, ScAddress aStart
, ScAddress aEnd
)
44 ScRangeList aRangeList
;
45 for (SCROW inRow
= aStart
.Row(); inRow
<= aEnd
.Row(); inRow
++)
48 ScAddress(aStart
.Col(), inRow
, aTab
),
49 ScAddress(aEnd
.Col(), inRow
, aTab
) );
51 aRangeList
.Append(aRowRange
);
56 ScStatisticsInputOutputDialog::ScStatisticsInputOutputDialog(
57 SfxBindings
* pSfxBindings
, SfxChildWindow
* pChildWindow
,
58 vcl::Window
* pParent
, ScViewData
* pViewData
, const OUString
& rID
, const OUString
& rUIXMLDescription
) :
59 ScAnyRefDlg ( pSfxBindings
, pChildWindow
, pParent
, rID
, rUIXMLDescription
),
60 mViewData ( pViewData
),
61 mDocument ( pViewData
->GetDocument() ),
62 mInputRange ( ScAddress::INITIALIZE_INVALID
),
63 mAddressDetails ( mDocument
->GetAddressConvention(), 0, 0 ),
64 mOutputAddress ( ScAddress::INITIALIZE_INVALID
),
65 mGroupedBy ( BY_COLUMN
),
66 mpActiveEdit ( NULL
),
67 mCurrentAddress ( pViewData
->GetCurX(), pViewData
->GetCurY(), pViewData
->GetTabNo() ),
68 mDialogLostFocus( false )
70 get(mpInputRangeLabel
, "input-range-label");
71 get(mpInputRangeEdit
, "input-range-edit");
72 get(mpInputRangeButton
, "input-range-button");
73 mpInputRangeEdit
->SetReferences(this, mpInputRangeLabel
);
74 mpInputRangeButton
->SetReferences(this, mpInputRangeEdit
);
76 get(mpOutputRangeLabel
, "output-range-label");
77 get(mpOutputRangeEdit
, "output-range-edit");
78 get(mpOutputRangeButton
, "output-range-button");
79 mpOutputRangeEdit
->SetReferences(this, mpOutputRangeLabel
);
80 mpOutputRangeButton
->SetReferences(this, mpOutputRangeEdit
);
82 get(mpButtonOk
, "ok");
84 get(mpGroupByColumnsRadio
, "groupedby-columns-radio");
85 get(mpGroupByRowsRadio
, "groupedby-rows-radio");
88 GetRangeFromSelection();
91 ScStatisticsInputOutputDialog::~ScStatisticsInputOutputDialog()
96 void ScStatisticsInputOutputDialog::dispose()
98 mpInputRangeLabel
.clear();
99 mpInputRangeEdit
.clear();
100 mpInputRangeButton
.clear();
101 mpOutputRangeLabel
.clear();
102 mpOutputRangeEdit
.clear();
103 mpOutputRangeButton
.clear();
104 mpGroupByColumnsRadio
.clear();
105 mpGroupByRowsRadio
.clear();
107 mpActiveEdit
.clear();
108 ScAnyRefDlg::dispose();
111 void ScStatisticsInputOutputDialog::Init()
113 mpButtonOk
->SetClickHdl( LINK( this, ScStatisticsInputOutputDialog
, OkClicked
) );
114 mpButtonOk
->Enable(false);
116 Link
<> aLink
= LINK( this, ScStatisticsInputOutputDialog
, GetFocusHandler
);
117 mpInputRangeEdit
->SetGetFocusHdl( aLink
);
118 mpInputRangeButton
->SetGetFocusHdl( aLink
);
119 mpOutputRangeEdit
->SetGetFocusHdl( aLink
);
120 mpOutputRangeButton
->SetGetFocusHdl( aLink
);
122 aLink
= LINK( this, ScStatisticsInputOutputDialog
, LoseFocusHandler
);
123 mpInputRangeEdit
->SetLoseFocusHdl( aLink
);
124 mpInputRangeButton
->SetLoseFocusHdl( aLink
);
125 mpOutputRangeEdit
->SetLoseFocusHdl( aLink
);
126 mpOutputRangeButton
->SetLoseFocusHdl( aLink
);
128 aLink
= LINK( this, ScStatisticsInputOutputDialog
, RefInputModifyHandler
);
129 mpInputRangeEdit
->SetModifyHdl( aLink
);
130 mpOutputRangeEdit
->SetModifyHdl( aLink
);
132 mpOutputRangeEdit
->GrabFocus();
134 mpGroupByColumnsRadio
->SetToggleHdl( LINK( this, ScStatisticsInputOutputDialog
, GroupByChanged
) );
135 mpGroupByRowsRadio
->SetToggleHdl( LINK( this, ScStatisticsInputOutputDialog
, GroupByChanged
) );
137 mpGroupByColumnsRadio
->Check(true);
138 mpGroupByRowsRadio
->Check(false);
141 void ScStatisticsInputOutputDialog::GetRangeFromSelection()
143 mViewData
->GetSimpleArea(mInputRange
);
144 OUString
aCurrentString(mInputRange
.Format(SCR_ABS_3D
, mDocument
, mAddressDetails
));
145 mpInputRangeEdit
->SetText(aCurrentString
);
148 void ScStatisticsInputOutputDialog::SetActive()
150 if ( mDialogLostFocus
)
152 mDialogLostFocus
= false;
154 mpActiveEdit
->GrabFocus();
163 void ScStatisticsInputOutputDialog::SetReference( const ScRange
& rReferenceRange
, ScDocument
* pDocument
)
167 if ( rReferenceRange
.aStart
!= rReferenceRange
.aEnd
)
168 RefInputStart( mpActiveEdit
);
170 OUString aReferenceString
;
172 if ( mpActiveEdit
== mpInputRangeEdit
)
174 mInputRange
= rReferenceRange
;
175 aReferenceString
= mInputRange
.Format(SCR_ABS_3D
, pDocument
, mAddressDetails
);
176 mpInputRangeEdit
->SetRefString( aReferenceString
);
178 else if ( mpActiveEdit
== mpOutputRangeEdit
)
180 mOutputAddress
= rReferenceRange
.aStart
;
182 sal_uInt16 nFormat
= ( mOutputAddress
.Tab() == mCurrentAddress
.Tab() ) ? SCA_ABS
: SCA_ABS_3D
;
183 aReferenceString
= mOutputAddress
.Format(nFormat
, pDocument
, pDocument
->GetAddressConvention());
184 mpOutputRangeEdit
->SetRefString( aReferenceString
);
188 // Enable OK if both, input range and output address are set.
189 if (mInputRange
.IsValid() && mOutputAddress
.IsValid())
190 mpButtonOk
->Enable();
192 mpButtonOk
->Disable();
195 IMPL_LINK( ScStatisticsInputOutputDialog
, OkClicked
, PushButton
*, /*pButton*/ )
197 CalculateInputAndWriteToOutput();
202 IMPL_LINK( ScStatisticsInputOutputDialog
, GetFocusHandler
, Control
*, pCtrl
)
206 if( (pCtrl
== (Control
*) mpInputRangeEdit
) || (pCtrl
== (Control
*) mpInputRangeButton
) )
207 mpActiveEdit
= mpInputRangeEdit
;
208 else if( (pCtrl
== (Control
*) mpOutputRangeEdit
) || (pCtrl
== (Control
*) mpOutputRangeButton
) )
209 mpActiveEdit
= mpOutputRangeEdit
;
212 mpActiveEdit
->SetSelection( Selection( 0, SELECTION_MAX
) );
217 IMPL_LINK_NOARG( ScStatisticsInputOutputDialog
, LoseFocusHandler
)
219 mDialogLostFocus
= !IsActive();
223 IMPL_LINK_NOARG( ScStatisticsInputOutputDialog
, GroupByChanged
)
225 if (mpGroupByColumnsRadio
->IsChecked())
226 mGroupedBy
= BY_COLUMN
;
227 else if (mpGroupByRowsRadio
->IsChecked())
233 IMPL_LINK_NOARG( ScStatisticsInputOutputDialog
, RefInputModifyHandler
)
237 if ( mpActiveEdit
== mpInputRangeEdit
)
239 ScRangeList aRangeList
;
240 bool bValid
= ParseWithNames( aRangeList
, mpInputRangeEdit
->GetText(), mDocument
);
241 const ScRange
* pRange
= (bValid
&& aRangeList
.size() == 1) ? aRangeList
[0] : nullptr;
244 mInputRange
= *pRange
;
245 // Highlight the resulting range.
246 mpInputRangeEdit
->StartUpdateData();
250 mInputRange
= ScRange( ScAddress::INITIALIZE_INVALID
);
253 else if ( mpActiveEdit
== mpOutputRangeEdit
)
255 ScRangeList aRangeList
;
256 bool bValid
= ParseWithNames( aRangeList
, mpOutputRangeEdit
->GetText(), mDocument
);
257 const ScRange
* pRange
= (bValid
&& aRangeList
.size() == 1) ? aRangeList
[0] : nullptr;
260 mOutputAddress
= pRange
->aStart
;
262 // Crop output range to top left address for Edit field.
263 if (pRange
->aStart
!= pRange
->aEnd
)
265 sal_uInt16 nFormat
= ( mOutputAddress
.Tab() == mCurrentAddress
.Tab() ) ? SCA_ABS
: SCA_ABS_3D
;
266 OUString aReferenceString
= mOutputAddress
.Format(nFormat
, mDocument
, mDocument
->GetAddressConvention());
267 mpOutputRangeEdit
->SetRefString( aReferenceString
);
270 // Highlight the resulting range.
271 mpOutputRangeEdit
->StartUpdateData();
275 mOutputAddress
= ScAddress( ScAddress::INITIALIZE_INVALID
);
280 // Enable OK if both, input range and output address are set.
281 if (mInputRange
.IsValid() && mOutputAddress
.IsValid())
282 mpButtonOk
->Enable();
284 mpButtonOk
->Disable();
289 void ScStatisticsInputOutputDialog::CalculateInputAndWriteToOutput()
291 OUString
aUndo(SC_STRLOAD(RID_STATISTICS_DLGS
, GetUndoNameId()));
292 ScDocShell
* pDocShell
= mViewData
->GetDocShell();
293 svl::IUndoManager
* pUndoManager
= pDocShell
->GetUndoManager();
294 pUndoManager
->EnterListAction( aUndo
, aUndo
);
296 ScRange aOutputRange
= ApplyOutput(pDocShell
);
298 pUndoManager
->LeaveListAction();
299 pDocShell
->PostPaint( aOutputRange
, PAINT_GRID
);
302 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */