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/.
10 #include <SparklineDataRangeDialog.hxx>
11 #include <Sparkline.hxx>
12 #include <reffact.hxx>
13 #include <docfunc.hxx>
17 SparklineDataRangeDialog::SparklineDataRangeDialog(SfxBindings
* pBindings
,
18 SfxChildWindow
* pChildWindow
,
19 weld::Window
* pWindow
, ScViewData
& rViewData
)
20 : ScAnyRefDlgController(pBindings
, pChildWindow
, pWindow
,
21 u
"modules/scalc/ui/sparklinedatarangedialog.ui"_ustr
,
22 u
"SparklineDataRangeDialog"_ustr
)
23 , mrViewData(rViewData
)
24 , mrDocument(rViewData
.GetDocument())
25 , mpActiveEdit(nullptr)
26 , mbDialogLostFocus(false)
27 , mxButtonOk(m_xBuilder
->weld_button(u
"ok"_ustr
))
28 , mxButtonCancel(m_xBuilder
->weld_button(u
"cancel"_ustr
))
29 , mxDataRangeLabel(m_xBuilder
->weld_label(u
"cell-range-label"_ustr
))
30 , mxDataRangeEdit(new formula::RefEdit(m_xBuilder
->weld_entry(u
"cell-range-edit"_ustr
)))
31 , mxDataRangeButton(new formula::RefButton(m_xBuilder
->weld_button(u
"cell-range-button"_ustr
)))
34 mxDataRangeEdit
->SetReferences(this, mxDataRangeLabel
.get());
35 mxDataRangeButton
->SetReferences(this, mxDataRangeEdit
.get());
37 mxButtonCancel
->connect_clicked(LINK(this, SparklineDataRangeDialog
, ButtonClicked
));
38 mxButtonOk
->connect_clicked(LINK(this, SparklineDataRangeDialog
, ButtonClicked
));
40 mxButtonOk
->set_sensitive(false);
42 Link
<formula::RefEdit
&, void> aEditLink
43 = LINK(this, SparklineDataRangeDialog
, EditFocusHandler
);
44 mxDataRangeEdit
->SetGetFocusHdl(aEditLink
);
45 aEditLink
= LINK(this, SparklineDataRangeDialog
, LoseEditFocusHandler
);
46 mxDataRangeEdit
->SetLoseFocusHdl(aEditLink
);
48 Link
<formula::RefButton
&, void> aButtonLink
49 = LINK(this, SparklineDataRangeDialog
, ButtonFocusHandler
);
50 mxDataRangeButton
->SetGetFocusHdl(aButtonLink
);
51 aButtonLink
= LINK(this, SparklineDataRangeDialog
, LoseButtonFocusHandler
);
52 mxDataRangeButton
->SetLoseFocusHdl(aButtonLink
);
54 Link
<formula::RefEdit
&, void> aModifyLink
55 = LINK(this, SparklineDataRangeDialog
, RefInputModifyHandler
);
56 mxDataRangeEdit
->SetModifyHdl(aModifyLink
);
60 mxDataRangeEdit
->GrabFocus();
63 SparklineDataRangeDialog::~SparklineDataRangeDialog() = default;
65 void SparklineDataRangeDialog::setupValues()
67 ScAddress aCurrentAddress
= mrViewData
.GetCurPos();
68 mpSparkline
= mrDocument
.GetSparkline(aCurrentAddress
);
72 ScRangeList
aRangeList(mpSparkline
->getInputRange());
73 if (!aRangeList
.empty())
75 maDataRange
= aRangeList
[0];
77 = maDataRange
.Format(mrDocument
, ScRefFlags::VALID
| ScRefFlags::TAB_3D
,
78 mrDocument
.GetAddressConvention());
79 mxDataRangeEdit
->SetRefString(aString
);
80 mxButtonOk
->set_sensitive(true);
85 void SparklineDataRangeDialog::Close()
87 DoClose(sc::SparklineDataRangeDialogWrapper::GetChildWindowId());
90 void SparklineDataRangeDialog::SetActive()
92 if (mbDialogLostFocus
)
94 mbDialogLostFocus
= false;
96 mpActiveEdit
->GrabFocus();
100 m_xDialog
->grab_focus();
105 void SparklineDataRangeDialog::SetReference(const ScRange
& rReferenceRange
, ScDocument
& rDocument
)
109 if (rReferenceRange
.aStart
!= rReferenceRange
.aEnd
)
110 RefInputStart(mpActiveEdit
);
113 const ScRefFlags eFlags
= ScRefFlags::VALID
| ScRefFlags::TAB_3D
;
114 auto eAddressConvention
= rDocument
.GetAddressConvention();
116 if (mpActiveEdit
== mxDataRangeEdit
.get())
118 maDataRange
= rReferenceRange
;
119 aString
= maDataRange
.Format(rDocument
, eFlags
, eAddressConvention
);
120 mxDataRangeEdit
->SetRefString(aString
);
125 IMPL_LINK(SparklineDataRangeDialog
, EditFocusHandler
, formula::RefEdit
&, rEdit
, void)
127 if (mxDataRangeEdit
.get() == &rEdit
)
128 mpActiveEdit
= mxDataRangeEdit
.get();
130 mpActiveEdit
= nullptr;
133 mpActiveEdit
->SelectAll();
136 IMPL_LINK(SparklineDataRangeDialog
, ButtonFocusHandler
, formula::RefButton
&, rButton
, void)
138 if (mxDataRangeButton
.get() == &rButton
)
139 mpActiveEdit
= mxDataRangeEdit
.get();
141 mpActiveEdit
= nullptr;
144 mpActiveEdit
->SelectAll();
147 IMPL_LINK_NOARG(SparklineDataRangeDialog
, LoseEditFocusHandler
, formula::RefEdit
&, void)
149 mbDialogLostFocus
= !m_xDialog
->has_toplevel_focus();
152 IMPL_LINK_NOARG(SparklineDataRangeDialog
, LoseButtonFocusHandler
, formula::RefButton
&, void)
154 mbDialogLostFocus
= !m_xDialog
->has_toplevel_focus();
157 IMPL_LINK_NOARG(SparklineDataRangeDialog
, RefInputModifyHandler
, formula::RefEdit
&, void)
161 if (mpActiveEdit
== mxDataRangeEdit
.get())
163 ScRangeList aRangeList
;
164 bool bValid
= ParseWithNames(aRangeList
, mxDataRangeEdit
->GetText(), mrDocument
);
165 const ScRange
* pRange
= (bValid
&& aRangeList
.size() == 1) ? &aRangeList
[0] : nullptr;
168 maDataRange
= *pRange
;
169 mxDataRangeEdit
->StartUpdateData();
173 maDataRange
= ScRange(ScAddress::INITIALIZE_INVALID
);
179 IMPL_LINK(SparklineDataRangeDialog
, ButtonClicked
, weld::Button
&, rButton
, void)
181 if (mxButtonOk
.get() == &rButton
)
188 response(RET_CANCEL
);
192 void SparklineDataRangeDialog::perform()
194 ScRangeList aList
{ maDataRange
};
196 auto& rDocFunc
= mrViewData
.GetDocShell()->GetDocFunc();
197 rDocFunc
.ChangeSparkline(mpSparkline
, mrViewData
.GetTabNo(), aList
);
202 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */