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 .
20 #include <o3tl/safeint.hxx>
21 #include <sal/config.h>
23 #include <svx/dialmgr.hxx>
25 #include <svx/sidebar/InspectorTextPanel.hxx>
27 #include <com/sun/star/awt/FontSlant.hpp>
28 #include <com/sun/star/lang/IllegalArgumentException.hpp>
29 #include <inspectorvalues.hrc>
33 const int MinimumPanelWidth
= 250;
35 namespace svx::sidebar
37 std::unique_ptr
<PanelLayout
> InspectorTextPanel::Create(weld::Widget
* pParent
)
39 if (pParent
== nullptr)
40 throw lang::IllegalArgumentException(
41 u
"no parent Window given to InspectorTextPanel::Create"_ustr
, nullptr, 0);
42 return std::make_unique
<InspectorTextPanel
>(pParent
);
45 InspectorTextPanel::InspectorTextPanel(weld::Widget
* pParent
)
46 : PanelLayout(pParent
, u
"InspectorTextPanel"_ustr
, u
"svx/ui/inspectortextpanel.ui"_ustr
)
47 , mpListBoxStyles(m_xBuilder
->weld_tree_view(u
"listbox_fonts"_ustr
))
49 mpListBoxStyles
->set_size_request(MinimumPanelWidth
, -1);
50 float fWidth
= mpListBoxStyles
->get_approximate_digit_width();
51 std::vector
<int> aWidths
{ o3tl::narrowing
<int>(fWidth
* 29) };
52 // 2nd column will fill remaining space
53 mpListBoxStyles
->set_column_fixed_widths(aWidths
);
56 static bool GetPropertyValues(std::u16string_view rPropName
, const uno::Any
& rAny
,
59 if (bool bValue
; rAny
>>= bValue
)
61 rString
= SvxResId(bValue
? RID_TRUE
: RID_FALSE
); // tdf#139136
63 else if (OUString aValue
; (rAny
>>= aValue
) && !(aValue
.isEmpty()))
67 else if (awt::FontSlant eValue
; rAny
>>= eValue
)
69 rString
= SvxResId(eValue
== awt::FontSlant_ITALIC
? RID_ITALIC
: RID_NORMAL
);
71 else if (tools::Long nValueLong
; rAny
>>= nValueLong
)
73 if (rPropName
.find(u
"Color") != std::u16string_view::npos
)
74 rString
= "0x" + OUString::number(nValueLong
, 16);
76 rString
= OUString::number(nValueLong
);
78 else if (double fValue
; rAny
>>= fValue
)
80 if (rPropName
.find(u
"Weight") != std::u16string_view::npos
)
81 rString
= SvxResId(fValue
> 100 ? RID_BOLD
: RID_NORMAL
);
83 rString
= OUString::number((round(fValue
* 100)) / 100.00);
85 else if (short nValueShort
; rAny
>>= nValueShort
)
87 rString
= OUString::number(nValueShort
);
95 static void FillBox_Impl(weld::TreeView
& rListBoxStyles
, const TreeNode
& rCurrent
,
96 const weld::TreeIter
* pParent
)
98 std::unique_ptr
<weld::TreeIter
> pResult
= rListBoxStyles
.make_iterator();
99 const OUString
& rName
= rCurrent
.sNodeName
;
102 if (!(rCurrent
.NodeType
!= TreeNode::SimpleProperty
103 || GetPropertyValues(rName
, rCurrent
.aValue
, sPairValue
)))
106 rListBoxStyles
.insert(pParent
, -1, &rName
, nullptr, nullptr, nullptr, false, pResult
.get());
107 rListBoxStyles
.set_sensitive(*pResult
, !rCurrent
.isGrey
, 0);
108 rListBoxStyles
.set_text_emphasis(*pResult
, rCurrent
.NodeType
== TreeNode::Category
, 0);
110 if (rCurrent
.NodeType
== TreeNode::SimpleProperty
)
112 rListBoxStyles
.set_text(*pResult
, sPairValue
, 1);
113 rListBoxStyles
.set_sensitive(*pResult
, !rCurrent
.isGrey
, 1);
114 rListBoxStyles
.set_text_emphasis(*pResult
, false, 1);
118 // Necessary, without this the selection line will be truncated.
119 rListBoxStyles
.set_text(*pResult
, u
""_ustr
, 1);
122 for (const TreeNode
& rChildNode
: rCurrent
.children
)
123 FillBox_Impl(rListBoxStyles
, rChildNode
, pResult
.get());
126 void InspectorTextPanel::updateEntries(const std::vector
<TreeNode
>& rStore
, const sal_Int32 nParIdx
)
128 mpListBoxStyles
->freeze();
129 mpListBoxStyles
->clear();
130 for (const TreeNode
& rChildNode
: rStore
)
132 FillBox_Impl(*mpListBoxStyles
, rChildNode
, nullptr);
135 mpListBoxStyles
->thaw();
137 weld::TreeView
* pTreeDiagram
= mpListBoxStyles
.get();
138 pTreeDiagram
->all_foreach([pTreeDiagram
](weld::TreeIter
& rEntry
) {
139 pTreeDiagram
->expand_row(rEntry
);
143 // Collapse "Default Paragraph Style"
145 std::unique_ptr
<weld::TreeIter
> pEntry
= mpListBoxStyles
->make_iterator();
146 if (!mpListBoxStyles
->get_iter_first(*pEntry
))
148 // skip the optional metadata items before "Default Paragraph Style"
149 for (sal_Int32 i
= 0; i
< nParIdx
; ++i
)
151 if (!mpListBoxStyles
->iter_next_sibling(*pEntry
))
154 if (!mpListBoxStyles
->iter_next(*pEntry
))
157 mpListBoxStyles
->collapse_row(*pEntry
);
160 InspectorTextPanel::~InspectorTextPanel() {}
162 } // end of namespace svx::sidebar
164 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */