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/.
13 #include <globstr.hrc>
14 #include <o3tl/safeint.hxx>
15 #include <scresid.hxx>
16 #include <globalnames.hxx>
17 #include <namemgrtable.hxx>
18 #include <rangenam.hxx>
20 #include <unotools/charclass.hxx>
21 #include <vcl/weld.hxx>
22 #include <tools/link.hxx>
24 void ScRangeManagerTable::GetCurrentLine(ScRangeNameLine
& rLine
)
26 std::unique_ptr
<weld::TreeIter
> xCurrentEntry(m_xTreeView
->make_iterator());
27 if (m_xTreeView
->get_cursor(xCurrentEntry
.get()))
28 GetLine(rLine
, *xCurrentEntry
);
31 void ScRangeManagerTable::DeleteSelectedEntries()
33 std::vector
<int> aRows
= m_xTreeView
->get_selected_rows();
34 std::sort(aRows
.begin(), aRows
.end());
35 for (auto it
= aRows
.rbegin(); it
!= aRows
.rend(); ++it
)
36 m_xTreeView
->remove(*it
);
39 bool ScRangeManagerTable::IsMultiSelection() const
41 return m_xTreeView
->count_selected_rows() > 1;
44 void ScRangeManagerTable::SetEntry(const ScRangeNameLine
& rLine
)
46 for (int i
= 0, nEntryCount
= m_xTreeView
->n_children(); i
< nEntryCount
; ++i
)
48 if (rLine
.aName
== m_xTreeView
->get_text(i
, 0)
49 && rLine
.aScope
== m_xTreeView
->get_text(i
, 2))
51 m_xTreeView
->set_cursor(i
);
56 ScRangeManagerTable::ScRangeManagerTable(std::unique_ptr
<weld::TreeView
> xTreeView
,
57 const std::map
<OUString
, ScRangeName
>& rRangeMap
,
58 const ScAddress
& rPos
)
59 : m_xTreeView(std::move(xTreeView
))
60 , maGlobalString(ScResId(STR_GLOBAL_SCOPE
))
61 , m_RangeMap(rRangeMap
)
66 auto nColWidth
= m_xTreeView
->get_size_request().Width() / 7;
67 std::vector
<int> aWidths
{ o3tl::narrowing
<int>(nColWidth
* 2),
68 o3tl::narrowing
<int>(nColWidth
* 3) };
69 m_xTreeView
->set_column_fixed_widths(aWidths
);
72 m_xTreeView
->set_selection_mode(SelectionMode::Multiple
);
73 m_xTreeView
->connect_size_allocate(LINK(this, ScRangeManagerTable
, SizeAllocHdl
));
74 m_xTreeView
->connect_visible_range_changed(LINK(this, ScRangeManagerTable
, VisRowsScrolledHdl
));
77 IMPL_LINK_NOARG(ScRangeManagerTable
, VisRowsScrolledHdl
, weld::TreeView
&, void)
79 CheckForFormulaString();
82 const ScRangeData
* ScRangeManagerTable::findRangeData(const ScRangeNameLine
& rLine
)
84 const ScRangeName
* pRangeName
;
85 if (rLine
.aScope
== maGlobalString
)
87 const auto iter
= m_RangeMap
.find(STR_GLOBAL_RANGE_NAME
);
88 assert(iter
!= m_RangeMap
.end());
89 pRangeName
= &iter
->second
;
93 const auto iter
= m_RangeMap
.find(rLine
.aScope
);
94 assert(iter
!= m_RangeMap
.end());
95 pRangeName
= &iter
->second
;
98 return pRangeName
->findByUpperName(ScGlobal::getCharClass().uppercase(rLine
.aName
));
101 void ScRangeManagerTable::CheckForFormulaString()
103 if (UpdatesBlocked())
106 auto lambda
= [this](weld::TreeIter
& rEntry
) {
107 OUString
sId(m_xTreeView
->get_id(rEntry
));
108 std::map
<OUString
, bool>::const_iterator itr
= maCalculatedFormulaEntries
.find(sId
);
109 if (itr
== maCalculatedFormulaEntries
.end() || !itr
->second
)
111 ScRangeNameLine aLine
;
112 GetLine(aLine
, rEntry
);
113 const ScRangeData
* pData
= findRangeData(aLine
);
114 OUString aFormulaString
= pData
->GetSymbol(maPos
);
115 m_xTreeView
->set_text(rEntry
, aFormulaString
, 1);
116 maCalculatedFormulaEntries
.insert(std::pair
<OUString
, bool>(sId
, true));
121 // ensure all visible entries are up to date
122 m_xTreeView
->visible_foreach(lambda
);
123 // and ensure all selected entries are up to date
124 m_xTreeView
->selected_foreach(lambda
);
127 IMPL_LINK_NOARG(ScRangeManagerTable
, SizeAllocHdl
, const Size
&, void) { CheckForFormulaString(); }
129 void ScRangeManagerTable::addEntry(const ScRangeNameLine
& rLine
, bool bSetCurEntry
)
131 int nRow
= m_xTreeView
->n_children();
132 m_xTreeView
->append();
133 m_xTreeView
->set_text(nRow
, rLine
.aName
, 0);
134 m_xTreeView
->set_text(nRow
, rLine
.aExpression
, 1);
135 m_xTreeView
->set_text(nRow
, rLine
.aScope
, 2);
136 // just unique to track which one has been cached by maCalculatedFormulaEntries
137 m_xTreeView
->set_id(nRow
, OUString::number(m_nId
++));
139 m_xTreeView
->set_cursor(nRow
);
142 void ScRangeManagerTable::GetLine(ScRangeNameLine
& rLine
, const weld::TreeIter
& rEntry
)
144 rLine
.aName
= m_xTreeView
->get_text(rEntry
, 0);
145 rLine
.aExpression
= m_xTreeView
->get_text(rEntry
, 1);
146 rLine
.aScope
= m_xTreeView
->get_text(rEntry
, 2);
149 void ScRangeManagerTable::Init()
151 m_xTreeView
->freeze();
152 m_xTreeView
->clear();
153 for (auto const& itr
: m_RangeMap
)
155 const ScRangeName
& rLocalRangeName
= itr
.second
;
156 ScRangeNameLine aLine
;
157 if (itr
.first
== STR_GLOBAL_RANGE_NAME
)
158 aLine
.aScope
= maGlobalString
;
160 aLine
.aScope
= itr
.first
;
161 for (const auto& rEntry
: rLocalRangeName
)
163 // Database and hidden named ranges are not shown in the Manage Names dialog
164 if (!rEntry
.second
->HasType(ScRangeData::Type::Database
)
165 && !rEntry
.second
->HasType(ScRangeData::Type::Hidden
))
167 aLine
.aName
= rEntry
.second
->GetName();
168 addEntry(aLine
, false);
175 std::vector
<ScRangeNameLine
> ScRangeManagerTable::GetSelectedEntries()
177 std::vector
<ScRangeNameLine
> aSelectedEntries
;
178 m_xTreeView
->selected_foreach([this, &aSelectedEntries
](weld::TreeIter
& rEntry
) {
179 ScRangeNameLine aLine
;
180 GetLine(aLine
, rEntry
);
181 aSelectedEntries
.push_back(aLine
);
184 return aSelectedEntries
;
187 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */