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 <sal/config.h>
21 
22 #include <string_view>
23 #include <svx/dialmgr.hxx>
24 
26 
27 #include <svl/languageoptions.hxx>
28 #include <com/sun/star/awt/FontSlant.hpp>
29 #include <com/sun/star/lang/IllegalArgumentException.hpp>
30 #include <inspectorvalues.hrc>
31 
32 using namespace css;
33 
34 const int MinimumPanelWidth = 250;
35 
36 namespace svx::sidebar
37 {
38 std::unique_ptr<PanelLayout> InspectorTextPanel::Create(weld::Widget* pParent)
39 {
40  if (pParent == nullptr)
41  throw lang::IllegalArgumentException("no parent Window given to InspectorTextPanel::Create",
42  nullptr, 0);
43  return std::make_unique<InspectorTextPanel>(pParent);
44 }
45 
46 InspectorTextPanel::InspectorTextPanel(weld::Widget* pParent)
47  : PanelLayout(pParent, "InspectorTextPanel", "svx/ui/inspectortextpanel.ui")
48  , mpListBoxStyles(m_xBuilder->weld_tree_view("listbox_fonts"))
49 {
50  mpListBoxStyles->set_size_request(MinimumPanelWidth, -1);
51  float fWidth = mpListBoxStyles->get_approximate_digit_width();
52  std::vector<int> aWidths;
53  aWidths.push_back(fWidth * 29);
54  // 2nd column will fill remaining space
55  mpListBoxStyles->set_column_fixed_widths(aWidths);
56 }
57 
58 static bool GetPropertyValues(const OUString& rPropName, const uno::Any& rAny, OUString& rString)
59 {
60  // Hide Asian and Complex properties
61  if (!SvtLanguageOptions().IsCJKFontEnabled() && rPropName.indexOf("Asian") != -1)
62  return false;
63  if (!SvtLanguageOptions().IsCTLFontEnabled() && rPropName.indexOf("Complex") != -1)
64  return false;
65 
66  if (bool bValue; rAny >>= bValue)
67  {
68  rString = SvxResId(bValue ? RID_TRUE : RID_FALSE); // tdf#139136
69  }
70  else if (OUString aValue; (rAny >>= aValue) && !(aValue.isEmpty()))
71  {
72  rString = aValue;
73  }
74  else if (awt::FontSlant eValue; rAny >>= eValue)
75  {
76  rString = SvxResId(eValue == awt::FontSlant_ITALIC ? RID_ITALIC : RID_NORMAL);
77  }
78  else if (tools::Long nValueLong; rAny >>= nValueLong)
79  {
80  if (rPropName.indexOf("Color") != -1)
81  rString = "0x" + OUString::number(nValueLong, 16);
82  else
83  rString = OUString::number(nValueLong);
84  }
85  else if (double fValue; rAny >>= fValue)
86  {
87  if (rPropName.indexOf("Weight") != -1)
88  rString = SvxResId(fValue > 100 ? RID_BOLD : RID_NORMAL);
89  else
90  rString = OUString::number((round(fValue * 100)) / 100.00);
91  }
92  else if (short nValueShort; rAny >>= nValueShort)
93  {
94  rString = OUString::number(nValueShort);
95  }
96  else
97  return false;
98 
99  return true;
100 }
101 
102 static void FillBox_Impl(weld::TreeView& rListBoxStyles, const TreeNode& rCurrent,
103  const weld::TreeIter* pParent)
104 {
105  std::unique_ptr<weld::TreeIter> pResult = rListBoxStyles.make_iterator();
106  const OUString& rName = rCurrent.sNodeName;
107  OUString sPairValue;
108 
109  if (!(rCurrent.NodeType != TreeNode::SimpleProperty
110  || GetPropertyValues(rName, rCurrent.aValue, sPairValue)))
111  return;
112 
113  rListBoxStyles.insert(pParent, -1, &rName, nullptr, nullptr, nullptr, false, pResult.get());
114  rListBoxStyles.set_sensitive(*pResult, !rCurrent.isGrey, 0);
115  rListBoxStyles.set_text_emphasis(*pResult, rCurrent.NodeType == TreeNode::Category, 0);
116 
117  if (rCurrent.NodeType == TreeNode::SimpleProperty)
118  {
119  rListBoxStyles.set_text(*pResult, sPairValue, 1);
120  rListBoxStyles.set_sensitive(*pResult, !rCurrent.isGrey, 1);
121  rListBoxStyles.set_text_emphasis(*pResult, false, 1);
122  }
123  else
124  {
125  // Necessary, without this the selection line will be truncated.
126  rListBoxStyles.set_text(*pResult, "", 1);
127  }
128 
129  for (const TreeNode& rChildNode : rCurrent.children)
130  FillBox_Impl(rListBoxStyles, rChildNode, pResult.get());
131 }
132 
133 void InspectorTextPanel::updateEntries(const std::vector<TreeNode>& rStore)
134 {
135  mpListBoxStyles->freeze();
136  mpListBoxStyles->clear();
137  for (const TreeNode& rChildNode : rStore)
138  {
139  FillBox_Impl(*mpListBoxStyles, rChildNode, nullptr);
140  }
141 
142  mpListBoxStyles->thaw();
143 
144  weld::TreeView* pTreeDiagram = mpListBoxStyles.get();
145  pTreeDiagram->all_foreach([pTreeDiagram](weld::TreeIter& rEntry) {
146  pTreeDiagram->expand_row(rEntry);
147  return false;
148  });
149 
150  std::unique_ptr<weld::TreeIter> pEntry = mpListBoxStyles->make_iterator();
151  if (!mpListBoxStyles->get_iter_first(*pEntry))
152  return;
153  if (!mpListBoxStyles->iter_next(*pEntry))
154  return;
155  mpListBoxStyles->collapse_row(*pEntry); // Collapse "Default Paragraph Style"
156 }
157 
159 
160 } // end of namespace svx::sidebar
161 
162 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void set_text(int row, const OUString &rText, int col=-1)=0
enum svx::sidebar::TreeNode::@4 NodeType
virtual void insert(const TreeIter *pParent, int pos, const OUString *pStr, const OUString *pId, const OUString *pIconName, VirtualDevice *pImageSurface, bool bChildrenOnDemand, TreeIter *pRet)=0
long Long
virtual std::unique_ptr< TreeIter > make_iterator(const TreeIter *pOrig=nullptr) const =0
virtual void set_text_emphasis(int row, bool bOn, int col)=0
OUString SvxResId(const char *pId)
Definition: dialmgr.cxx:25
static void FillBox_Impl(weld::TreeView &rListBoxStyles, const TreeNode &rCurrent, const weld::TreeIter *pParent)
void Create(SvxOrientationItem &rItem, SvStream &rStrm, sal_uInt16)
Definition: legacyitem.cxx:34
void updateEntries(const std::vector< TreeNode > &rStore)
const int MinimumPanelWidth
std::vector< TreeNode > children
virtual void expand_row(const TreeIter &rIter)=0
virtual void all_foreach(const std::function< bool(TreeIter &)> &func)=0
bool IsCTLFontEnabled() const
std::unique_ptr< weld::TreeView > mpListBoxStyles
virtual void set_sensitive(int row, bool bSensitive, int col=-1)=0
static bool GetPropertyValues(const OUString &rPropName, const uno::Any &rAny, OUString &rString)