LibreOffice Module svx (master) 1
InspectorTextPanel.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <o3tl/safeint.hxx>
21#include <sal/config.h>
22
23#include <svx/dialmgr.hxx>
24
26
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>
32
33using namespace css;
34
35const int MinimumPanelWidth = 250;
36
37namespace svx::sidebar
38{
39std::unique_ptr<PanelLayout> InspectorTextPanel::Create(weld::Widget* pParent)
40{
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);
45}
46
48 : PanelLayout(pParent, "InspectorTextPanel", "svx/ui/inspectortextpanel.ui")
49 , mpListBoxStyles(m_xBuilder->weld_tree_view("listbox_fonts"))
50{
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);
56}
57
58static bool GetPropertyValues(std::u16string_view rPropName, const uno::Any& rAny,
59 OUString& rString)
60{
61 if (bool bValue; rAny >>= bValue)
62 {
63 rString = SvxResId(bValue ? RID_TRUE : RID_FALSE); // tdf#139136
64 }
65 else if (OUString aValue; (rAny >>= aValue) && !(aValue.isEmpty()))
66 {
67 rString = aValue;
68 }
69 else if (awt::FontSlant eValue; rAny >>= eValue)
70 {
71 rString = SvxResId(eValue == awt::FontSlant_ITALIC ? RID_ITALIC : RID_NORMAL);
72 }
73 else if (tools::Long nValueLong; rAny >>= nValueLong)
74 {
75 if (rPropName.find(u"Color") != std::u16string_view::npos)
76 rString = "0x" + OUString::number(nValueLong, 16);
77 else
78 rString = OUString::number(nValueLong);
79 }
80 else if (double fValue; rAny >>= fValue)
81 {
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);
86 }
87 else if (short nValueShort; rAny >>= nValueShort)
88 {
89 rString = OUString::number(nValueShort);
90 }
91 else
92 return false;
93
94 return true;
95}
96
97static void FillBox_Impl(weld::TreeView& rListBoxStyles, const TreeNode& rCurrent,
98 const weld::TreeIter* pParent)
99{
100 std::unique_ptr<weld::TreeIter> pResult = rListBoxStyles.make_iterator();
101 const OUString& rName = rCurrent.sNodeName;
102 OUString sPairValue;
103
104 if (!(rCurrent.NodeType != TreeNode::SimpleProperty
105 || GetPropertyValues(rName, rCurrent.aValue, sPairValue)))
106 return;
107
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);
111
112 if (rCurrent.NodeType == TreeNode::SimpleProperty)
113 {
114 rListBoxStyles.set_text(*pResult, sPairValue, 1);
115 rListBoxStyles.set_sensitive(*pResult, !rCurrent.isGrey, 1);
116 rListBoxStyles.set_text_emphasis(*pResult, false, 1);
117 }
118 else
119 {
120 // Necessary, without this the selection line will be truncated.
121 rListBoxStyles.set_text(*pResult, "", 1);
122 }
123
124 for (const TreeNode& rChildNode : rCurrent.children)
125 FillBox_Impl(rListBoxStyles, rChildNode, pResult.get());
126}
127
128void InspectorTextPanel::updateEntries(const std::vector<TreeNode>& rStore, const sal_Int32 nParIdx)
129{
130 mpListBoxStyles->freeze();
131 mpListBoxStyles->clear();
132 for (const TreeNode& rChildNode : rStore)
133 {
134 FillBox_Impl(*mpListBoxStyles, rChildNode, nullptr);
135 }
136
137 mpListBoxStyles->thaw();
138
139 weld::TreeView* pTreeDiagram = mpListBoxStyles.get();
140 pTreeDiagram->all_foreach([pTreeDiagram](weld::TreeIter& rEntry) {
141 pTreeDiagram->expand_row(rEntry);
142 return false;
143 });
144
145 // Collapse "Default Paragraph Style"
146
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)
152 {
153 if (!mpListBoxStyles->iter_next_sibling(*pEntry))
154 return;
155 }
156 if (!mpListBoxStyles->iter_next(*pEntry))
157 return;
158
159 mpListBoxStyles->collapse_row(*pEntry);
160}
161
163
164} // end of namespace svx::sidebar
165
166/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const int MinimumPanelWidth
void updateEntries(const std::vector< TreeNode > &rStore, const sal_Int32 nParIdx)
InspectorTextPanel(weld::Widget *pParent)
static std::unique_ptr< PanelLayout > Create(weld::Widget *pParent)
std::unique_ptr< weld::TreeView > mpListBoxStyles
virtual std::unique_ptr< TreeIter > make_iterator(const TreeIter *pOrig=nullptr) const=0
virtual void expand_row(const TreeIter &rIter)=0
virtual void set_text(int row, const OUString &rText, int col=-1)=0
virtual void set_text_emphasis(int row, bool bOn, int col)=0
virtual void insert(const TreeIter *pParent, int pos, const OUString *pStr, const OUString *pId, const OUString *pIconName, VirtualDevice *pImageSurface, bool bChildrenOnDemand, TreeIter *pRet)=0
virtual void all_foreach(const std::function< bool(TreeIter &)> &func)=0
virtual void set_sensitive(int row, bool bSensitive, int col=-1)=0
OUString SvxResId(TranslateId aId)
Definition: dialmgr.cxx:24
float u
int i
static void FillBox_Impl(weld::TreeView &rListBoxStyles, const TreeNode &rCurrent, const weld::TreeIter *pParent)
static bool GetPropertyValues(std::u16string_view rPropName, const uno::Any &rAny, OUString &rString)
long Long
std::vector< TreeNode > children
enum svx::sidebar::TreeNode::@4 NodeType