Avoid potential negative array index access to cached text.
[LibreOffice.git] / formula / source / ui / dlg / structpg.cxx
blob6cfc34a96ca109298f9e25b1ea8776ee4495bbaa
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 <vcl/svapp.hxx>
22 #include "structpg.hxx"
23 #include <formula/formula.hxx>
24 #include <formula/token.hxx>
25 #include <bitmaps.hlst>
27 namespace formula
30 void StructPage::SetActiveFlag(bool bFlag)
32 bActiveFlag = bFlag;
35 StructPage::StructPage(weld::Container* pParent)
36 : m_xBuilder(Application::CreateBuilder(pParent, "formula/ui/structpage.ui"))
37 , m_xContainer(m_xBuilder->weld_container("StructPage"))
38 , m_xTlbStruct(m_xBuilder->weld_tree_view("struct"))
39 , maImgEnd(BMP_STR_END)
40 , maImgError(BMP_STR_ERROR)
41 , pSelectedToken(nullptr)
42 , bActiveFlag(false)
44 m_xTlbStruct->set_size_request(m_xTlbStruct->get_approximate_digit_width() * 20,
45 m_xTlbStruct->get_height_rows(17));
47 m_xTlbStruct->connect_changed(LINK( this, StructPage, SelectHdl ) );
50 StructPage::~StructPage()
54 void StructPage::ClearStruct()
56 SetActiveFlag(false);
57 m_xTlbStruct->clear();
60 bool StructPage::InsertEntry(const OUString& rText, const weld::TreeIter* pParent,
61 sal_uInt16 nFlag, int nPos,
62 const FormulaToken* pIFormulaToken,
63 weld::TreeIter& rRet)
65 SetActiveFlag(false);
67 OUString sId(weld::toId(pIFormulaToken));
69 bool bEntry = false;
70 switch (nFlag)
72 case STRUCT_FOLDER:
73 m_xTlbStruct->insert(pParent, nPos, &rText, &sId, nullptr, nullptr,
74 false, &rRet);
75 m_xTlbStruct->set_image(rRet, BMP_STR_OPEN);
76 bEntry = true;
77 break;
78 case STRUCT_END:
79 m_xTlbStruct->insert(pParent, nPos, &rText, &sId, nullptr, nullptr,
80 false, &rRet);
81 m_xTlbStruct->set_image(rRet, maImgEnd);
82 bEntry = true;
83 break;
84 case STRUCT_ERROR:
85 m_xTlbStruct->insert(pParent, nPos, &rText, &sId, nullptr, nullptr,
86 false, &rRet);
87 m_xTlbStruct->set_image(rRet, maImgError);
88 bEntry = true;
89 break;
92 if (bEntry && pParent)
93 m_xTlbStruct->expand_row(*pParent);
94 return bEntry;
97 OUString StructPage::GetEntryText(const weld::TreeIter* pEntry) const
99 OUString aString;
100 if (pEntry)
101 aString = m_xTlbStruct->get_text(*pEntry);
102 return aString;
105 const FormulaToken* StructPage::GetFunctionEntry(const weld::TreeIter* pEntry)
107 if (!pEntry)
108 return nullptr;
110 const FormulaToken * pToken = weld::fromId<const FormulaToken*>(m_xTlbStruct->get_id(*pEntry));
111 if (pToken)
113 if ( !(pToken->IsFunction() || pToken->GetParamCount() > 1 ) )
115 std::unique_ptr<weld::TreeIter> xParent(m_xTlbStruct->make_iterator(pEntry));
116 if (!m_xTlbStruct->iter_parent(*xParent))
117 return nullptr;
118 return GetFunctionEntry(xParent.get());
120 else
122 return pToken;
125 return nullptr;
128 IMPL_LINK(StructPage, SelectHdl, weld::TreeView&, rTlb, void)
130 if (!GetActiveFlag())
131 return;
133 if (&rTlb == m_xTlbStruct.get())
135 std::unique_ptr<weld::TreeIter> xCurEntry(m_xTlbStruct->make_iterator());
136 if (m_xTlbStruct->get_cursor(xCurEntry.get()))
138 pSelectedToken = weld::fromId<const FormulaToken*>(m_xTlbStruct->get_id(*xCurEntry));
139 if (pSelectedToken)
141 if ( !(pSelectedToken->IsFunction() || pSelectedToken->GetParamCount() > 1) )
143 pSelectedToken = GetFunctionEntry(xCurEntry.get());
149 aSelLink.Call(*this);
152 } // formula
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */