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 "StatisticsTwoVariableDialog.hxx"
28 ScStatisticsTwoVariableDialog::ScStatisticsTwoVariableDialog(
29 SfxBindings
* pSfxBindings
, SfxChildWindow
* pChildWindow
,
30 Window
* pParent
, ScViewData
* pViewData
, const OString
& rID
, const OUString
& rUIXMLDescription
) :
31 ScAnyRefDlg ( pSfxBindings
, pChildWindow
, pParent
, rID
, rUIXMLDescription
),
32 mViewData ( pViewData
),
33 mDocument ( pViewData
->GetDocument() ),
34 mAddressDetails ( mDocument
->GetAddressConvention(), 0, 0 ),
35 mGroupedBy ( BY_COLUMN
),
36 mpActiveEdit ( NULL
),
37 mCurrentAddress ( pViewData
->GetCurX(), pViewData
->GetCurY(), pViewData
->GetTabNo() ),
38 mDialogLostFocus( false )
40 get(mpVariable1RangeLabel
, "variable1-range-label");
41 get(mpVariable1RangeEdit
, "variable1-range-edit");
42 get(mpVariable1RangeButton
, "variable1-range-button");
43 mpVariable1RangeEdit
->SetReferences(this, mpVariable1RangeLabel
);
44 mpVariable1RangeButton
->SetReferences(this, mpVariable1RangeEdit
);
46 get(mpVariable2RangeLabel
, "variable2-range-label");
47 get(mpVariable2RangeEdit
, "variable2-range-edit");
48 get(mpVariable2RangeButton
, "variable2-range-button");
49 mpVariable2RangeEdit
->SetReferences(this, mpVariable2RangeLabel
);
50 mpVariable2RangeButton
->SetReferences(this, mpVariable2RangeEdit
);
52 get(mpOutputRangeLabel
, "output-range-label");
53 get(mpOutputRangeEdit
, "output-range-edit");
54 get(mpOutputRangeButton
, "output-range-button");
55 mpOutputRangeEdit
->SetReferences(this, mpOutputRangeLabel
);
56 mpOutputRangeButton
->SetReferences(this, mpOutputRangeEdit
);
58 get(mpButtonOk
, "ok");
60 get(mpGroupByColumnsRadio
, "groupedby-columns-radio");
61 get(mpGroupByRowsRadio
, "groupedby-rows-radio");
64 GetRangeFromSelection();
67 ScStatisticsTwoVariableDialog::~ScStatisticsTwoVariableDialog()
70 void ScStatisticsTwoVariableDialog::Init()
72 mpButtonOk
->SetClickHdl( LINK( this, ScStatisticsTwoVariableDialog
, OkClicked
) );
73 mpButtonOk
->Enable(false);
75 Link aLink
= LINK( this, ScStatisticsTwoVariableDialog
, GetFocusHandler
);
76 mpVariable1RangeEdit
->SetGetFocusHdl( aLink
);
77 mpVariable1RangeButton
->SetGetFocusHdl( aLink
);
78 mpVariable2RangeEdit
->SetGetFocusHdl( aLink
);
79 mpVariable2RangeButton
->SetGetFocusHdl( aLink
);
80 mpOutputRangeEdit
->SetGetFocusHdl( aLink
);
81 mpOutputRangeButton
->SetGetFocusHdl( aLink
);
83 aLink
= LINK( this, ScStatisticsTwoVariableDialog
, LoseFocusHandler
);
84 mpVariable1RangeEdit
->SetLoseFocusHdl( aLink
);
85 mpVariable1RangeButton
->SetLoseFocusHdl( aLink
);
86 mpVariable2RangeEdit
->SetLoseFocusHdl( aLink
);
87 mpVariable2RangeButton
->SetLoseFocusHdl( aLink
);
88 mpOutputRangeEdit
->SetLoseFocusHdl( aLink
);
89 mpOutputRangeButton
->SetLoseFocusHdl( aLink
);
91 mpOutputRangeEdit
->GrabFocus();
93 mpGroupByColumnsRadio
->SetToggleHdl( LINK( this, ScStatisticsTwoVariableDialog
, GroupByChanged
) );
94 mpGroupByRowsRadio
->SetToggleHdl( LINK( this, ScStatisticsTwoVariableDialog
, GroupByChanged
) );
96 mpGroupByColumnsRadio
->Check(true);
97 mpGroupByRowsRadio
->Check(false);
100 void ScStatisticsTwoVariableDialog::GetRangeFromSelection()
102 OUString aCurrentString
;
104 ScRange aCurrentRange
;
105 mViewData
->GetSimpleArea(aCurrentRange
);
107 if (aCurrentRange
.aEnd
.Col() - aCurrentRange
.aStart
.Col() == 1)
109 mVariable1Range
= aCurrentRange
;
110 mVariable1Range
.aEnd
.SetCol(mVariable1Range
.aStart
.Col());
111 aCurrentString
= mVariable1Range
.Format(SCR_ABS_3D
, mDocument
, mAddressDetails
);
112 mpVariable1RangeEdit
->SetText(aCurrentString
);
114 mVariable2Range
= aCurrentRange
;
115 mVariable2Range
.aStart
.SetCol(mVariable2Range
.aEnd
.Col());
116 aCurrentString
= mVariable2Range
.Format(SCR_ABS_3D
, mDocument
, mAddressDetails
);
117 mpVariable2RangeEdit
->SetText(aCurrentString
);
121 mVariable1Range
= aCurrentRange
;
122 aCurrentString
= mVariable1Range
.Format(SCR_ABS_3D
, mDocument
, mAddressDetails
);
123 mpVariable1RangeEdit
->SetText(aCurrentString
);
127 void ScStatisticsTwoVariableDialog::SetActive()
129 if ( mDialogLostFocus
)
131 mDialogLostFocus
= false;
133 mpActiveEdit
->GrabFocus();
142 void ScStatisticsTwoVariableDialog::SetReference( const ScRange
& rReferenceRange
, ScDocument
* pDocument
)
144 if ( mpActiveEdit
!= NULL
)
146 if ( rReferenceRange
.aStart
!= rReferenceRange
.aEnd
)
147 RefInputStart( mpActiveEdit
);
149 OUString aReferenceString
;
151 if ( mpActiveEdit
== mpVariable1RangeEdit
)
153 mVariable1Range
= rReferenceRange
;
154 aReferenceString
= mVariable1Range
.Format(SCR_ABS_3D
, pDocument
, mAddressDetails
);
155 mpVariable1RangeEdit
->SetRefString(aReferenceString
);
157 else if ( mpActiveEdit
== mpVariable2RangeEdit
)
159 mVariable2Range
= rReferenceRange
;
160 aReferenceString
= mVariable2Range
.Format(SCR_ABS_3D
, pDocument
, mAddressDetails
);
161 mpVariable2RangeEdit
->SetRefString(aReferenceString
);
163 else if ( mpActiveEdit
== mpOutputRangeEdit
)
165 mOutputAddress
= rReferenceRange
.aStart
;
167 sal_uInt16 nFormat
= ( mOutputAddress
.Tab() == mCurrentAddress
.Tab() ) ? SCA_ABS
: SCA_ABS_3D
;
168 aReferenceString
= mOutputAddress
.Format(nFormat
, pDocument
, pDocument
->GetAddressConvention());
169 mpOutputRangeEdit
->SetRefString( aReferenceString
);
171 // Enable OK, Cancel if output range is set
172 mpButtonOk
->Enable(!mpOutputRangeEdit
->GetText().isEmpty());
177 IMPL_LINK( ScStatisticsTwoVariableDialog
, OkClicked
, PushButton
*, /*pButton*/ )
179 CalculateInputAndWriteToOutput();
184 IMPL_LINK( ScStatisticsTwoVariableDialog
, GetFocusHandler
, Control
*, pCtrl
)
187 if( pCtrl
== mpVariable1RangeEdit
188 || pCtrl
== mpVariable1RangeButton
)
190 mpActiveEdit
= mpVariable1RangeEdit
;
192 else if( pCtrl
== mpVariable2RangeEdit
193 || pCtrl
== mpVariable2RangeButton
)
195 mpActiveEdit
= mpVariable2RangeEdit
;
197 else if( pCtrl
== mpOutputRangeEdit
198 || pCtrl
== mpOutputRangeButton
)
200 mpActiveEdit
= mpOutputRangeEdit
;
204 mpActiveEdit
->SetSelection( Selection( 0, SELECTION_MAX
) );
209 IMPL_LINK_NOARG( ScStatisticsTwoVariableDialog
, LoseFocusHandler
)
211 mDialogLostFocus
= !IsActive();
215 IMPL_LINK_NOARG( ScStatisticsTwoVariableDialog
, GroupByChanged
)
217 if (mpGroupByColumnsRadio
->IsChecked())
218 mGroupedBy
= BY_COLUMN
;
219 else if (mpGroupByRowsRadio
->IsChecked())
225 void ScStatisticsTwoVariableDialog::CalculateInputAndWriteToOutput()
227 OUString
aUndo(SC_STRLOAD(RID_STATISTICS_DLGS
, GetUndoNameId()));
228 ScDocShell
* pDocShell
= mViewData
->GetDocShell();
229 svl::IUndoManager
* pUndoManager
= pDocShell
->GetUndoManager();
230 pUndoManager
->EnterListAction( aUndo
, aUndo
);
232 ScRange aOutputRange
= ApplyOutput(pDocShell
);
234 pUndoManager
->LeaveListAction();
235 pDocShell
->PostPaint( aOutputRange
, PAINT_GRID
);
238 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */