nss: upgrade to release 3.73
[LibreOffice.git] / sc / source / ui / StatisticsDialogs / StatisticsInputOutputDialog.cxx
blobd2d53b23244fc0ead58e4b2cf081cfff52155620
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 */
11 #include <svl/undo.hxx>
13 #include <rangelst.hxx>
14 #include <docsh.hxx>
15 #include <document.hxx>
16 #include <scresid.hxx>
17 #include <tabvwsh.hxx>
19 #include <StatisticsInputOutputDialog.hxx>
21 ScRangeList ScStatisticsInputOutputDialog::MakeColumnRangeList(SCTAB aTab, ScAddress const & aStart, ScAddress const & aEnd)
23 ScRangeList aRangeList;
24 for (SCCOL inCol = aStart.Col(); inCol <= aEnd.Col(); inCol++)
26 ScRange aColumnRange (
27 ScAddress(inCol, aStart.Row(), aTab),
28 ScAddress(inCol, aEnd.Row(), aTab) );
30 aRangeList.push_back(aColumnRange);
32 return aRangeList;
35 ScRangeList ScStatisticsInputOutputDialog::MakeRowRangeList(SCTAB aTab, ScAddress const & aStart, ScAddress const & aEnd)
37 ScRangeList aRangeList;
38 for (SCROW inRow = aStart.Row(); inRow <= aEnd.Row(); inRow++)
40 ScRange aRowRange (
41 ScAddress(aStart.Col(), inRow, aTab),
42 ScAddress(aEnd.Col(), inRow, aTab) );
44 aRangeList.push_back(aRowRange);
46 return aRangeList;
49 ScStatisticsInputOutputDialog::ScStatisticsInputOutputDialog(
50 SfxBindings* pSfxBindings, SfxChildWindow* pChildWindow,
51 weld::Window* pParent, ScViewData& rViewData, const OUString& rUIXMLDescription, const OString& rID)
52 : ScAnyRefDlgController(pSfxBindings, pChildWindow, pParent, rUIXMLDescription, rID)
53 , mxInputRangeLabel(m_xBuilder->weld_label("input-range-label"))
54 , mxInputRangeEdit(new formula::RefEdit(m_xBuilder->weld_entry("input-range-edit")))
55 , mxInputRangeButton(new formula::RefButton(m_xBuilder->weld_button("input-range-button")))
56 , mxOutputRangeLabel(m_xBuilder->weld_label("output-range-label"))
57 , mxOutputRangeEdit(new formula::RefEdit(m_xBuilder->weld_entry("output-range-edit")))
58 , mxOutputRangeButton(new formula::RefButton(m_xBuilder->weld_button("output-range-button")))
59 , mxGroupByColumnsRadio(m_xBuilder->weld_radio_button("groupedby-columns-radio"))
60 , mxGroupByRowsRadio(m_xBuilder->weld_radio_button("groupedby-rows-radio"))
61 , mViewData(rViewData)
62 , mDocument(rViewData.GetDocument())
63 , mInputRange(ScAddress::INITIALIZE_INVALID)
64 , mAddressDetails(mDocument.GetAddressConvention(), 0, 0)
65 , mOutputAddress(ScAddress::INITIALIZE_INVALID)
66 , mGroupedBy(BY_COLUMN)
67 , mxButtonOk(m_xBuilder->weld_button("ok"))
68 , mpActiveEdit(nullptr)
69 , mCurrentAddress(rViewData.GetCurX(), rViewData.GetCurY(), rViewData.GetTabNo())
70 , mDialogLostFocus(false)
72 mxInputRangeEdit->SetReferences(this, mxInputRangeLabel.get());
73 mxInputRangeButton->SetReferences(this, mxInputRangeEdit.get());
75 mxOutputRangeEdit->SetReferences(this, mxOutputRangeLabel.get());
76 mxOutputRangeButton->SetReferences(this, mxOutputRangeEdit.get());
78 Init();
79 GetRangeFromSelection();
82 ScStatisticsInputOutputDialog::~ScStatisticsInputOutputDialog()
86 void ScStatisticsInputOutputDialog::Init()
88 mxButtonOk->connect_clicked( LINK( this, ScStatisticsInputOutputDialog, OkClicked ) );
89 mxButtonOk->set_sensitive(false);
91 Link<formula::RefEdit&,void> aEditLink = LINK( this, ScStatisticsInputOutputDialog, GetEditFocusHandler );
92 mxInputRangeEdit->SetGetFocusHdl( aEditLink );
93 mxOutputRangeEdit->SetGetFocusHdl( aEditLink );
94 Link<formula::RefButton&,void> aButtonLink = LINK( this, ScStatisticsInputOutputDialog, GetButtonFocusHandler );
95 mxInputRangeButton->SetGetFocusHdl( aButtonLink );
96 mxOutputRangeButton->SetGetFocusHdl( aButtonLink );
98 aEditLink = LINK( this, ScStatisticsInputOutputDialog, LoseEditFocusHandler );
99 mxInputRangeEdit->SetLoseFocusHdl( aEditLink );
100 mxOutputRangeEdit->SetLoseFocusHdl( aEditLink );
101 aButtonLink = LINK( this, ScStatisticsInputOutputDialog, LoseButtonFocusHandler );
102 mxInputRangeButton->SetLoseFocusHdl( aButtonLink );
103 mxOutputRangeButton->SetLoseFocusHdl( aButtonLink );
105 Link<formula::RefEdit&,void> aLink2 = LINK( this, ScStatisticsInputOutputDialog, RefInputModifyHandler);
106 mxInputRangeEdit->SetModifyHdl( aLink2);
107 mxOutputRangeEdit->SetModifyHdl( aLink2);
109 mxOutputRangeEdit->GrabFocus();
111 mxGroupByColumnsRadio->connect_toggled( LINK( this, ScStatisticsInputOutputDialog, GroupByChanged ) );
112 mxGroupByRowsRadio->connect_toggled( LINK( this, ScStatisticsInputOutputDialog, GroupByChanged ) );
114 mxGroupByColumnsRadio->set_active(true);
115 mxGroupByRowsRadio->set_active(false);
118 void ScStatisticsInputOutputDialog::GetRangeFromSelection()
120 mViewData.GetSimpleArea(mInputRange);
121 OUString aCurrentString(mInputRange.Format(mDocument, ScRefFlags::RANGE_ABS_3D, mAddressDetails));
122 mxInputRangeEdit->SetText(aCurrentString);
125 void ScStatisticsInputOutputDialog::SetActive()
127 if ( mDialogLostFocus )
129 mDialogLostFocus = false;
130 if( mpActiveEdit )
131 mpActiveEdit->GrabFocus();
133 else
135 m_xDialog->grab_focus();
137 RefInputDone();
140 void ScStatisticsInputOutputDialog::SetReference( const ScRange& rReferenceRange, ScDocument& rDocument )
142 if ( mpActiveEdit )
144 if ( rReferenceRange.aStart != rReferenceRange.aEnd )
145 RefInputStart( mpActiveEdit );
147 OUString aReferenceString;
149 if (mpActiveEdit == mxInputRangeEdit.get())
151 mInputRange = rReferenceRange;
152 aReferenceString = mInputRange.Format(rDocument, ScRefFlags::RANGE_ABS_3D, mAddressDetails);
153 mxInputRangeEdit->SetRefString( aReferenceString );
155 else if (mpActiveEdit == mxOutputRangeEdit.get())
157 mOutputAddress = rReferenceRange.aStart;
159 ScRefFlags nFormat = ( mOutputAddress.Tab() == mCurrentAddress.Tab() ) ?
160 ScRefFlags::ADDR_ABS :
161 ScRefFlags::ADDR_ABS_3D;
162 aReferenceString = mOutputAddress.Format(nFormat, &rDocument, rDocument.GetAddressConvention());
163 mxOutputRangeEdit->SetRefString( aReferenceString );
167 ValidateDialogInput();
170 IMPL_LINK_NOARG( ScStatisticsInputOutputDialog, OkClicked, weld::Button&, void )
172 CalculateInputAndWriteToOutput();
173 response(RET_OK);
176 IMPL_LINK(ScStatisticsInputOutputDialog, GetEditFocusHandler, formula::RefEdit&, rCtrl, void)
178 mpActiveEdit = nullptr;
180 if (&rCtrl == mxInputRangeEdit.get())
181 mpActiveEdit = mxInputRangeEdit.get();
182 if (&rCtrl == mxOutputRangeEdit.get())
183 mpActiveEdit = mxOutputRangeEdit.get();
185 if (mpActiveEdit)
186 mpActiveEdit->SelectAll();
189 IMPL_LINK(ScStatisticsInputOutputDialog, GetButtonFocusHandler, formula::RefButton&, rCtrl, void)
191 mpActiveEdit = nullptr;
193 if (&rCtrl == mxInputRangeButton.get())
194 mpActiveEdit = mxInputRangeEdit.get();
195 else if (&rCtrl == mxOutputRangeButton.get())
196 mpActiveEdit = mxOutputRangeEdit.get();
198 if (mpActiveEdit)
199 mpActiveEdit->SelectAll();
202 IMPL_LINK_NOARG(ScStatisticsInputOutputDialog, LoseEditFocusHandler, formula::RefEdit&, void)
204 mDialogLostFocus = !m_xDialog->has_toplevel_focus();
207 IMPL_LINK_NOARG(ScStatisticsInputOutputDialog, LoseButtonFocusHandler, formula::RefButton&, void)
209 mDialogLostFocus = !m_xDialog->has_toplevel_focus();
212 IMPL_LINK_NOARG( ScStatisticsInputOutputDialog, GroupByChanged, weld::ToggleButton&, void )
214 if (mxGroupByColumnsRadio->get_active())
215 mGroupedBy = BY_COLUMN;
216 else if (mxGroupByRowsRadio->get_active())
217 mGroupedBy = BY_ROW;
219 ValidateDialogInput();
222 IMPL_LINK_NOARG( ScStatisticsInputOutputDialog, RefInputModifyHandler, formula::RefEdit&, void )
224 if ( mpActiveEdit )
226 if (mpActiveEdit == mxInputRangeEdit.get())
228 ScRangeList aRangeList;
229 bool bValid = ParseWithNames( aRangeList, mxInputRangeEdit->GetText(), mDocument);
230 const ScRange* pRange = (bValid && aRangeList.size() == 1) ? &aRangeList[0] : nullptr;
231 if (pRange)
233 mInputRange = *pRange;
234 // Highlight the resulting range.
235 mxInputRangeEdit->StartUpdateData();
237 else
239 mInputRange = ScRange( ScAddress::INITIALIZE_INVALID);
242 else if (mpActiveEdit == mxOutputRangeEdit.get())
244 ScRangeList aRangeList;
245 bool bValid = ParseWithNames( aRangeList, mxOutputRangeEdit->GetText(), mDocument);
246 const ScRange* pRange = (bValid && aRangeList.size() == 1) ? &aRangeList[0] : nullptr;
247 if (pRange)
249 mOutputAddress = pRange->aStart;
251 // Crop output range to top left address for Edit field.
252 if (pRange->aStart != pRange->aEnd)
254 ScRefFlags nFormat = ( mOutputAddress.Tab() == mCurrentAddress.Tab() ) ?
255 ScRefFlags::ADDR_ABS :
256 ScRefFlags::ADDR_ABS_3D;
257 OUString aReferenceString = mOutputAddress.Format(nFormat, &mDocument, mDocument.GetAddressConvention());
258 mxOutputRangeEdit->SetRefString( aReferenceString );
261 // Highlight the resulting range.
262 mxOutputRangeEdit->StartUpdateData();
264 else
266 mOutputAddress = ScAddress( ScAddress::INITIALIZE_INVALID);
271 ValidateDialogInput();
274 void ScStatisticsInputOutputDialog::CalculateInputAndWriteToOutput()
276 OUString aUndo(ScResId(GetUndoNameId()));
277 ScDocShell* pDocShell = mViewData.GetDocShell();
278 SfxUndoManager* pUndoManager = pDocShell->GetUndoManager();
279 pUndoManager->EnterListAction( aUndo, aUndo, 0, mViewData.GetViewShell()->GetViewShellId() );
281 ScRange aOutputRange = ApplyOutput(pDocShell);
283 pUndoManager->LeaveListAction();
284 pDocShell->PostPaint( aOutputRange, PaintPartFlags::Grid );
287 bool ScStatisticsInputOutputDialog::InputRangesValid()
289 return mInputRange.IsValid() && mOutputAddress.IsValid();
292 void ScStatisticsInputOutputDialog::ValidateDialogInput()
294 // Enable OK button if all inputs are ok.
295 mxButtonOk->set_sensitive(InputRangesValid());
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */