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