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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <o3tl/safeint.hxx>
23 #include <swmodule.hxx>
27 #include <sfx2/styfitem.hxx>
31 #include <hintids.hxx>
34 #include <swuiccoll.hxx>
36 const WhichRangesContainer
SwCondCollPage::s_aPageRg(svl::Items
<FN_COND_COLL
, FN_COND_COLL
>);
38 SwCondCollPage::SwCondCollPage(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
&rSet
)
39 : SfxTabPage(pPage
, pController
, "modules/swriter/ui/conditionpage.ui", "ConditionPage", &rSet
)
40 , m_pCmds(SwCondCollItem::GetCmds())
42 , m_xTbLinks(m_xBuilder
->weld_tree_view("links"))
43 , m_xStyleLB(m_xBuilder
->weld_tree_view("styles"))
44 , m_xFilterLB(m_xBuilder
->weld_combo_box("filter"))
45 , m_xRemovePB(m_xBuilder
->weld_button("remove"))
46 , m_xAssignPB(m_xBuilder
->weld_button("apply"))
48 m_xStyleLB
->make_sorted();
49 const auto nHeightRequest
= m_xStyleLB
->get_height_rows(12);
50 m_xStyleLB
->set_size_request(-1, nHeightRequest
);
51 m_xTbLinks
->set_size_request(-1, nHeightRequest
);
52 std::vector
<int> aWidths
54 o3tl::narrowing
<int>(m_xTbLinks
->get_approximate_digit_width() * 40)
56 m_xTbLinks
->set_column_fixed_widths(aWidths
);
58 const sal_Int32 nStrCount
= m_xFilterLB
->get_count();
59 for (sal_Int32 i
= 0; i
< nStrCount
; ++i
)
60 m_aStrArr
.push_back(m_xFilterLB
->get_text(i
));
66 m_xTbLinks
->connect_row_activated(LINK(this, SwCondCollPage
, AssignRemoveTreeListBoxHdl
));
67 m_xStyleLB
->connect_row_activated(LINK(this, SwCondCollPage
, AssignRemoveTreeListBoxHdl
));
68 m_xRemovePB
->connect_clicked(LINK(this, SwCondCollPage
, AssignRemoveClickHdl
));
69 m_xAssignPB
->connect_clicked(LINK(this, SwCondCollPage
, AssignRemoveClickHdl
));
70 m_xTbLinks
->connect_changed(LINK(this, SwCondCollPage
, SelectTreeListBoxHdl
));
71 m_xStyleLB
->connect_changed(LINK(this, SwCondCollPage
, SelectTreeListBoxHdl
));
72 m_xFilterLB
->connect_changed(LINK(this, SwCondCollPage
, SelectListBoxHdl
));
74 std::optional
<SfxStyleFamilies
> xFamilies(SW_MOD()->CreateStyleFamilies());
75 size_t nCount
= xFamilies
->size();
76 for (size_t j
= 0; j
< nCount
; ++j
)
78 const SfxStyleFamilyItem
&rFamilyItem
= xFamilies
->at(j
);
79 if (SfxStyleFamily::Para
== rFamilyItem
.GetFamily())
81 const SfxStyleFilter
& rFilterList
= rFamilyItem
.GetFilterList();
82 for (size_t i
= 0; i
< rFilterList
.size(); ++i
)
83 m_xFilterLB
->append(OUString::number(int(rFilterList
[i
].nFlags
)), rFilterList
[i
].aName
);
88 m_xFilterLB
->set_active(0);
92 SwCondCollPage::~SwCondCollPage()
96 DeactivateRC
SwCondCollPage::DeactivatePage(SfxItemSet
* _pSet
)
101 return DeactivateRC::LeavePage
;
104 std::unique_ptr
<SfxTabPage
> SwCondCollPage::Create(weld::Container
* pPage
, weld::DialogController
* pController
, const SfxItemSet
*rSet
)
106 return std::make_unique
<SwCondCollPage
>(pPage
, pController
, *rSet
);
109 bool SwCondCollPage::FillItemSet(SfxItemSet
*rSet
)
111 SwCondCollItem aCondItem
;
112 for (size_t i
= 0; i
< m_aStrArr
.size(); ++i
)
114 const OUString sEntry
= m_xTbLinks
->get_text(i
, 1);
115 aCondItem
.SetStyle( &sEntry
, i
);
117 rSet
->Put(aCondItem
);
121 void SwCondCollPage::Reset(const SfxItemSet
*)
126 SwView
* pView
= GetActiveView();
130 SfxStyleSheetBasePool
* pPool
= pView
->GetWrtShell().GetView().GetDocShell()->GetStyleSheetPool();
131 const SfxStyleSheetBase
* pBase
= pPool
->First(SfxStyleFamily::Para
);
134 if (!m_pFormat
|| pBase
->GetName() != m_pFormat
->GetName())
135 m_xStyleLB
->append_text(pBase
->GetName());
136 pBase
= pPool
->Next();
138 m_xStyleLB
->select(0);
140 for (size_t n
= 0; n
< m_aStrArr
.size(); ++n
)
142 m_xTbLinks
->append_text(m_aStrArr
[n
]);
144 const SwCollCondition
* pCond
= nullptr;
145 if( m_pFormat
&& RES_CONDTXTFMTCOLL
== m_pFormat
->Which() &&
146 nullptr != ( pCond
= static_cast<SwConditionTextFormatColl
*>(m_pFormat
)->
147 HasCondition( SwCollCondition( nullptr, m_pCmds
[n
].nCnd
, m_pCmds
[n
].nSubCond
) ) )
148 && pCond
->GetTextFormatColl() )
150 m_xTbLinks
->set_text(n
, pCond
->GetTextFormatColl()->GetName(), 1);
155 m_xTbLinks
->select(0);
156 SelectTreeListBoxHdl(*m_xTbLinks
);
161 IMPL_LINK(SwCondCollPage
, AssignRemoveClickHdl
, weld::Button
&, rBtn
, void)
166 IMPL_LINK(SwCondCollPage
, AssignRemoveTreeListBoxHdl
, weld::TreeView
&, rBtn
, bool)
172 void SwCondCollPage::AssignRemove(const weld::Widget
* pBtn
)
174 int nPos
= m_xTbLinks
->get_selected_index();
180 const bool bAssEnabled
= pBtn
!= m_xRemovePB
.get() && m_xAssignPB
->get_sensitive();
181 m_xAssignPB
->set_sensitive(!bAssEnabled
);
182 m_xRemovePB
->set_sensitive(bAssEnabled
);
184 m_xTbLinks
->set_text(nPos
, m_xStyleLB
->get_selected_text(), 1);
186 m_xTbLinks
->set_text(nPos
, "", 1);
189 IMPL_LINK(SwCondCollPage
, SelectTreeListBoxHdl
, weld::TreeView
&, rBox
, void)
194 IMPL_LINK(SwCondCollPage
, SelectListBoxHdl
, weld::ComboBox
&, rBox
, void)
199 void SwCondCollPage::SelectHdl(const weld::Widget
* pBox
)
201 if (pBox
== m_xFilterLB
.get())
205 SwView
* pView
= GetActiveView();
209 const sal_Int32 nSelPos
= m_xFilterLB
->get_active();
210 const SfxStyleSearchBits nSearchFlags
= static_cast<SfxStyleSearchBits
>(m_xFilterLB
->get_id(nSelPos
).toInt32());
211 SfxStyleSheetBasePool
* pPool
= pView
->GetWrtShell().GetView().GetDocShell()->GetStyleSheetPool();
212 const SfxStyleSheetBase
* pBase
= pPool
->First(SfxStyleFamily::Para
, nSearchFlags
);
217 if (!m_pFormat
|| pBase
->GetName() != m_pFormat
->GetName())
219 m_xStyleLB
->append_text(pBase
->GetName());
222 pBase
= pPool
->Next();
224 m_xStyleLB
->select(bEmpty
? -1 : 0);
225 SelectHdl(m_xStyleLB
.get());
229 int nSelected
= m_xTbLinks
->get_selected_index();
230 const OUString sTbEntry
= nSelected
!= -1
231 ? m_xTbLinks
->get_text(nSelected
, 1)
233 const OUString sStyle
= m_xStyleLB
->get_selected_text();
235 m_xAssignPB
->set_sensitive(sStyle
!= sTbEntry
);
237 if (pBox
!= m_xStyleLB
.get())
238 m_xRemovePB
->set_sensitive(!sTbEntry
.isEmpty());
242 void SwCondCollPage::SetCollection(SwFormat
* pFormat
)
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */