Bump version to 24.04.3.4
[LibreOffice.git] / svx / source / sidebar / inspector / InspectorTextPanel.cxx
blob3ddba2db9e58a02eb167826e704ce865e051fea7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <svl/cjkoptions.hxx>
28 #include <svl/ctloptions.hxx>
29 #include <com/sun/star/awt/FontSlant.hpp>
30 #include <com/sun/star/lang/IllegalArgumentException.hpp>
31 #include <inspectorvalues.hrc>
33 using namespace css;
35 const int MinimumPanelWidth = 250;
37 namespace svx::sidebar
39 std::unique_ptr<PanelLayout> InspectorTextPanel::Create(weld::Widget* pParent)
41 if (pParent == nullptr)
42 throw lang::IllegalArgumentException("no parent Window given to InspectorTextPanel::Create",
43 nullptr, 0);
44 return std::make_unique<InspectorTextPanel>(pParent);
47 InspectorTextPanel::InspectorTextPanel(weld::Widget* pParent)
48 : PanelLayout(pParent, "InspectorTextPanel", "svx/ui/inspectortextpanel.ui")
49 , mpListBoxStyles(m_xBuilder->weld_tree_view("listbox_fonts"))
51 mpListBoxStyles->set_size_request(MinimumPanelWidth, -1);
52 float fWidth = mpListBoxStyles->get_approximate_digit_width();
53 std::vector<int> aWidths{ o3tl::narrowing<int>(fWidth * 29) };
54 // 2nd column will fill remaining space
55 mpListBoxStyles->set_column_fixed_widths(aWidths);
58 static bool GetPropertyValues(std::u16string_view rPropName, const uno::Any& rAny,
59 OUString& rString)
61 if (bool bValue; rAny >>= bValue)
63 rString = SvxResId(bValue ? RID_TRUE : RID_FALSE); // tdf#139136
65 else if (OUString aValue; (rAny >>= aValue) && !(aValue.isEmpty()))
67 rString = aValue;
69 else if (awt::FontSlant eValue; rAny >>= eValue)
71 rString = SvxResId(eValue == awt::FontSlant_ITALIC ? RID_ITALIC : RID_NORMAL);
73 else if (tools::Long nValueLong; rAny >>= nValueLong)
75 if (rPropName.find(u"Color") != std::u16string_view::npos)
76 rString = "0x" + OUString::number(nValueLong, 16);
77 else
78 rString = OUString::number(nValueLong);
80 else if (double fValue; rAny >>= fValue)
82 if (rPropName.find(u"Weight") != std::u16string_view::npos)
83 rString = SvxResId(fValue > 100 ? RID_BOLD : RID_NORMAL);
84 else
85 rString = OUString::number((round(fValue * 100)) / 100.00);
87 else if (short nValueShort; rAny >>= nValueShort)
89 rString = OUString::number(nValueShort);
91 else
92 return false;
94 return true;
97 static void FillBox_Impl(weld::TreeView& rListBoxStyles, const TreeNode& rCurrent,
98 const weld::TreeIter* pParent)
100 std::unique_ptr<weld::TreeIter> pResult = rListBoxStyles.make_iterator();
101 const OUString& rName = rCurrent.sNodeName;
102 OUString sPairValue;
104 if (!(rCurrent.NodeType != TreeNode::SimpleProperty
105 || GetPropertyValues(rName, rCurrent.aValue, sPairValue)))
106 return;
108 rListBoxStyles.insert(pParent, -1, &rName, nullptr, nullptr, nullptr, false, pResult.get());
109 rListBoxStyles.set_sensitive(*pResult, !rCurrent.isGrey, 0);
110 rListBoxStyles.set_text_emphasis(*pResult, rCurrent.NodeType == TreeNode::Category, 0);
112 if (rCurrent.NodeType == TreeNode::SimpleProperty)
114 rListBoxStyles.set_text(*pResult, sPairValue, 1);
115 rListBoxStyles.set_sensitive(*pResult, !rCurrent.isGrey, 1);
116 rListBoxStyles.set_text_emphasis(*pResult, false, 1);
118 else
120 // Necessary, without this the selection line will be truncated.
121 rListBoxStyles.set_text(*pResult, "", 1);
124 for (const TreeNode& rChildNode : rCurrent.children)
125 FillBox_Impl(rListBoxStyles, rChildNode, pResult.get());
128 void InspectorTextPanel::updateEntries(const std::vector<TreeNode>& rStore, const sal_Int32 nParIdx)
130 mpListBoxStyles->freeze();
131 mpListBoxStyles->clear();
132 for (const TreeNode& rChildNode : rStore)
134 FillBox_Impl(*mpListBoxStyles, rChildNode, nullptr);
137 mpListBoxStyles->thaw();
139 weld::TreeView* pTreeDiagram = mpListBoxStyles.get();
140 pTreeDiagram->all_foreach([pTreeDiagram](weld::TreeIter& rEntry) {
141 pTreeDiagram->expand_row(rEntry);
142 return false;
145 // Collapse "Default Paragraph Style"
147 std::unique_ptr<weld::TreeIter> pEntry = mpListBoxStyles->make_iterator();
148 if (!mpListBoxStyles->get_iter_first(*pEntry))
149 return;
150 // skip the optional metadata items before "Default Paragraph Style"
151 for (sal_Int32 i = 0; i < nParIdx; ++i)
153 if (!mpListBoxStyles->iter_next_sibling(*pEntry))
154 return;
156 if (!mpListBoxStyles->iter_next(*pEntry))
157 return;
159 mpListBoxStyles->collapse_row(*pEntry);
162 InspectorTextPanel::~InspectorTextPanel() {}
164 } // end of namespace svx::sidebar
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */