Stop leaking all ScPostIt instances.
[LibreOffice.git] / sc / source / ui / StatisticsDialogs / StatisticsInputOutputDialog.cxx
blob787f17bb2dc5ddb7d060ceda117777ca8982d976
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 <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"
18 #include "docsh.hxx"
19 #include "document.hxx"
20 #include "uiitems.hxx"
21 #include "reffact.hxx"
22 #include "scresid.hxx"
23 #include "random.hxx"
24 #include "docfunc.hxx"
25 #include "strload.hxx"
27 #include "StatisticsInputOutputDialog.hxx"
29 ScRangeList ScStatisticsInputOutputDialog::MakeColumnRangeList(SCTAB aTab, ScAddress aStart, ScAddress aEnd)
31 ScRangeList aRangeList;
32 for (SCCOL inCol = aStart.Col(); inCol <= aEnd.Col(); inCol++)
34 ScRange aColumnRange (
35 ScAddress(inCol, aStart.Row(), aTab),
36 ScAddress(inCol, aEnd.Row(), aTab) );
38 aRangeList.Append(aColumnRange);
40 return aRangeList;
43 ScRangeList ScStatisticsInputOutputDialog::MakeRowRangeList(SCTAB aTab, ScAddress aStart, ScAddress aEnd)
45 ScRangeList aRangeList;
46 for (SCROW inRow = aStart.Row(); inRow <= aEnd.Row(); inRow++)
48 ScRange aRowRange (
49 ScAddress(aStart.Col(), inRow, aTab),
50 ScAddress(aEnd.Col(), inRow, aTab) );
52 aRangeList.Append(aRowRange);
54 return aRangeList;
57 ScStatisticsInputOutputDialog::ScStatisticsInputOutputDialog(
58 SfxBindings* pSfxBindings, SfxChildWindow* pChildWindow,
59 Window* pParent, ScViewData* pViewData, const OString& rID, const OUString& rUIXMLDescription ) :
60 ScAnyRefDlg ( pSfxBindings, pChildWindow, pParent, rID, rUIXMLDescription ),
61 mViewData ( pViewData ),
62 mDocument ( pViewData->GetDocument() ),
63 mAddressDetails ( mDocument->GetAddressConvention(), 0, 0 ),
64 mGroupedBy ( BY_COLUMN ),
65 mCurrentAddress ( pViewData->GetCurX(), pViewData->GetCurY(), pViewData->GetTabNo() ),
66 mDialogLostFocus( false )
68 get(mpInputRangeLabel, "input-range-label");
69 get(mpInputRangeEdit, "input-range-edit");
70 get(mpInputRangeButton, "input-range-button");
71 mpInputRangeEdit->SetReferences(this, mpInputRangeLabel);
72 mpInputRangeButton->SetReferences(this, mpInputRangeEdit);
74 get(mpOutputRangeLabel, "output-range-label");
75 get(mpOutputRangeEdit, "output-range-edit");
76 get(mpOutputRangeButton, "output-range-button");
77 mpOutputRangeEdit->SetReferences(this, mpOutputRangeLabel);
78 mpOutputRangeButton->SetReferences(this, mpOutputRangeEdit);
80 get(mpButtonOk, "ok");
81 get(mpButtonApply, "apply");
82 get(mpButtonClose, "close");
84 get(mpGroupByColumnsRadio, "groupedby-columns-radio");
85 get(mpGroupByRowsRadio, "groupedby-rows-radio");
87 Init();
88 GetRangeFromSelection();
91 ScStatisticsInputOutputDialog::~ScStatisticsInputOutputDialog()
94 void ScStatisticsInputOutputDialog::Init()
96 mpButtonOk->SetClickHdl( LINK( this, ScStatisticsInputOutputDialog, OkClicked ) );
97 mpButtonClose->SetClickHdl( LINK( this, ScStatisticsInputOutputDialog, CloseClicked ) );
98 mpButtonApply->SetClickHdl( LINK( this, ScStatisticsInputOutputDialog, ApplyClicked ) );
99 mpButtonOk->Enable(false);
100 mpButtonApply->Enable(false);
102 Link aLink = LINK( this, ScStatisticsInputOutputDialog, GetFocusHandler );
103 mpInputRangeEdit->SetGetFocusHdl( aLink );
104 mpInputRangeButton->SetGetFocusHdl( aLink );
105 mpOutputRangeEdit->SetGetFocusHdl( aLink );
106 mpOutputRangeButton->SetGetFocusHdl( aLink );
108 aLink = LINK( this, ScStatisticsInputOutputDialog, LoseFocusHandler );
109 mpInputRangeEdit->SetLoseFocusHdl( aLink );
110 mpInputRangeButton->SetLoseFocusHdl( aLink );
111 mpOutputRangeEdit->SetLoseFocusHdl( aLink );
112 mpOutputRangeButton->SetLoseFocusHdl( aLink );
114 mpOutputRangeEdit->GrabFocus();
116 mpGroupByColumnsRadio->SetToggleHdl( LINK( this, ScStatisticsInputOutputDialog, GroupByChanged ) );
117 mpGroupByRowsRadio->SetToggleHdl( LINK( this, ScStatisticsInputOutputDialog, GroupByChanged ) );
119 mpGroupByColumnsRadio->Check(true);
120 mpGroupByRowsRadio->Check(false);
123 void ScStatisticsInputOutputDialog::GetRangeFromSelection()
125 mViewData->GetSimpleArea(mInputRange);
126 OUString aCurrentString(mInputRange.Format(SCR_ABS_3D, mDocument, mAddressDetails));
127 mpInputRangeEdit->SetText(aCurrentString);
130 void ScStatisticsInputOutputDialog::SetActive()
132 if ( mDialogLostFocus )
134 mDialogLostFocus = false;
135 if( mpActiveEdit )
136 mpActiveEdit->GrabFocus();
138 else
140 GrabFocus();
142 RefInputDone();
145 void ScStatisticsInputOutputDialog::SetReference( const ScRange& rReferenceRange, ScDocument* pDocument )
147 if ( mpActiveEdit )
149 if ( rReferenceRange.aStart != rReferenceRange.aEnd )
150 RefInputStart( mpActiveEdit );
152 OUString aReferenceString;
154 if ( mpActiveEdit == mpInputRangeEdit )
156 mInputRange = rReferenceRange;
157 aReferenceString = mInputRange.Format(SCR_ABS_3D, pDocument, mAddressDetails);
158 mpInputRangeEdit->SetRefString( aReferenceString );
160 else if ( mpActiveEdit == mpOutputRangeEdit )
162 mOutputAddress = rReferenceRange.aStart;
164 sal_uInt16 nFormat = ( mOutputAddress.Tab() == mCurrentAddress.Tab() ) ? SCA_ABS : SCA_ABS_3D;
165 aReferenceString = mOutputAddress.Format(nFormat, pDocument, pDocument->GetAddressConvention());
166 mpOutputRangeEdit->SetRefString( aReferenceString );
168 // Enable OK, Cancel if output range is set
169 mpButtonOk->Enable(!mpOutputRangeEdit->GetText().isEmpty());
170 mpButtonApply->Enable(!mpOutputRangeEdit->GetText().isEmpty());
175 IMPL_LINK( ScStatisticsInputOutputDialog, OkClicked, PushButton*, /*pButton*/ )
177 ApplyClicked(NULL);
178 CloseClicked(NULL);
179 return 0;
183 IMPL_LINK( ScStatisticsInputOutputDialog, ApplyClicked, PushButton*, /*pButton*/ )
185 CalculateInputAndWriteToOutput();
186 return 0;
189 IMPL_LINK( ScStatisticsInputOutputDialog, CloseClicked, PushButton*, /*pButton*/ )
191 Close();
192 return 0;
195 IMPL_LINK( ScStatisticsInputOutputDialog, GetFocusHandler, Control*, pCtrl )
197 mpActiveEdit = NULL;
199 if( (pCtrl == (Control*) mpInputRangeEdit) || (pCtrl == (Control*) mpInputRangeButton) )
200 mpActiveEdit = mpInputRangeEdit;
201 else if( (pCtrl == (Control*) mpOutputRangeEdit) || (pCtrl == (Control*) mpOutputRangeButton) )
202 mpActiveEdit = mpOutputRangeEdit;
204 if( mpActiveEdit )
205 mpActiveEdit->SetSelection( Selection( 0, SELECTION_MAX ) );
207 return 0;
210 IMPL_LINK_NOARG( ScStatisticsInputOutputDialog, LoseFocusHandler )
212 mDialogLostFocus = !IsActive();
213 return 0;
216 IMPL_LINK_NOARG( ScStatisticsInputOutputDialog, GroupByChanged )
218 if (mpGroupByColumnsRadio->IsChecked())
219 mGroupedBy = BY_COLUMN;
220 else if (mpGroupByRowsRadio->IsChecked())
221 mGroupedBy = BY_ROW;
223 return 0;
226 void ScStatisticsInputOutputDialog::CalculateInputAndWriteToOutput()
228 OUString aUndo(SC_STRLOAD(RID_STATISTICS_DLGS, GetUndoNameId()));
229 ScDocShell* pDocShell = mViewData->GetDocShell();
230 svl::IUndoManager* pUndoManager = pDocShell->GetUndoManager();
231 pUndoManager->EnterListAction( aUndo, aUndo );
233 ScRange aOutputRange = ApplyOutput(pDocShell);
235 pUndoManager->LeaveListAction();
236 pDocShell->PostPaint( aOutputRange, PAINT_GRID );
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */