1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/event.hxx>
21 #include <vcl/svapp.hxx>
22 #include <formula/IFunctionDescription.hxx>
24 #include "funcpage.hxx"
25 #include <unotools/syslocale.hxx>
26 #include <unotools/charclass.hxx>
30 IMPL_LINK(FuncPage
, KeyInputHdl
, const KeyEvent
&, rKEvt
, bool)
32 if (rKEvt
.GetCharCode() == ' ')
34 aDoubleClickLink
.Call(*this);
40 FuncPage::FuncPage(weld::Container
* pParent
, const IFunctionManager
* _pFunctionManager
)
41 : m_xBuilder(Application::CreateBuilder(pParent
, "formula/ui/functionpage.ui"))
42 , m_xContainer(m_xBuilder
->weld_container("FunctionPage"))
43 , m_xLbCategory(m_xBuilder
->weld_combo_box("category"))
44 , m_xLbFunction(m_xBuilder
->weld_tree_view("function"))
45 , m_xLbFunctionSearchString(m_xBuilder
->weld_entry("search"))
46 , m_pFunctionManager(_pFunctionManager
)
48 m_xLbFunction
->make_sorted();
49 m_aHelpId
= m_xLbFunction
->get_help_id();
51 m_pFunctionManager
->fillLastRecentlyUsedFunctions(aLRUList
);
53 const sal_uInt32 nCategoryCount
= m_pFunctionManager
->getCount();
54 for (sal_uInt32 j
= 0; j
< nCategoryCount
; ++j
)
56 const IFunctionCategory
* pCategory
= m_pFunctionManager
->getCategory(j
);
57 OUString
sId(OUString::number(reinterpret_cast<sal_Int64
>(pCategory
)));
58 m_xLbCategory
->append(sId
, pCategory
->getName());
61 m_xLbCategory
->set_active(1);
62 OUString searchStr
= m_xLbFunctionSearchString
->get_text();
63 UpdateFunctionList(searchStr
);
64 // lock to its initial size
65 m_xLbFunction
->set_size_request(m_xLbFunction
->get_preferred_size().Width(),
66 m_xLbFunction
->get_height_rows(15));
67 m_xLbCategory
->connect_changed(LINK(this, FuncPage
, SelComboBoxHdl
));
68 m_xLbFunction
->connect_changed(LINK(this, FuncPage
, SelTreeViewHdl
));
69 m_xLbFunction
->connect_row_activated(LINK(this, FuncPage
, DblClkHdl
));
70 m_xLbFunction
->connect_key_press(LINK(this, FuncPage
, KeyInputHdl
));
71 m_xLbFunctionSearchString
->connect_changed(LINK(this, FuncPage
, ModifyHdl
));
73 m_xLbFunctionSearchString
->grab_focus();
76 FuncPage::~FuncPage() {}
78 void FuncPage::impl_addFunctions(const IFunctionCategory
* _pCategory
)
80 const sal_uInt32 nCount
= _pCategory
->getCount();
81 for (sal_uInt32 i
= 0; i
< nCount
; ++i
)
83 TFunctionDesc
pDesc(_pCategory
->getFunction(i
));
84 if (!pDesc
->isHidden())
86 OUString
sId(OUString::number(reinterpret_cast<sal_Int64
>(pDesc
)));
87 m_xLbFunction
->append(sId
, pDesc
->getFunctionName());
92 //aStr is non-empty when user types in the search box to search some function
93 void FuncPage::UpdateFunctionList(const OUString
& aStr
)
95 m_xLbFunction
->clear();
96 m_xLbFunction
->freeze();
98 const sal_Int32 nSelPos
= m_xLbCategory
->get_active();
100 if (aStr
.isEmpty() || nSelPos
== 0)
102 const IFunctionCategory
* pCategory
103 = reinterpret_cast<const IFunctionCategory
*>(m_xLbCategory
->get_id(nSelPos
).toInt64());
107 if (pCategory
== nullptr)
109 const sal_uInt32 nCount
= m_pFunctionManager
->getCount();
110 for (sal_uInt32 i
= 0; i
< nCount
; ++i
)
112 impl_addFunctions(m_pFunctionManager
->getCategory(i
));
117 impl_addFunctions(pCategory
);
122 for (auto const& elem
: aLRUList
)
124 if (elem
) // may be null if a function is no longer available
126 OUString
sId(OUString::number(reinterpret_cast<sal_Int64
>(elem
)));
127 m_xLbFunction
->append(sId
, elem
->getFunctionName());
134 SvtSysLocale aSysLocale
;
135 const CharClass
* pCharClass
= aSysLocale
.GetCharClassPtr();
136 const OUString
aSearchStr(pCharClass
->uppercase(aStr
));
138 const sal_uInt32 nCategoryCount
= m_pFunctionManager
->getCount();
139 // Category listbox holds additional entries for Last Used and All, so
140 // the offset should be two but hard coded numbers are ugly...
141 const sal_Int32 nCategoryOffset
= m_xLbCategory
->get_count() - nCategoryCount
;
142 // If a real category (not Last Used or All) is selected, list only
143 // functions of that category. Else list all, LRU is handled above.
144 sal_Int32 nCatBeg
= (nSelPos
== -1 ? -1 : nSelPos
- nCategoryOffset
);
149 nCatEnd
= nCategoryCount
;
153 nCatEnd
= nCatBeg
+ 1;
155 for (sal_uInt32 i
= nCatBeg
; i
< nCatEnd
; ++i
)
157 const IFunctionCategory
* pCategory
= m_pFunctionManager
->getCategory(i
);
158 const sal_uInt32 nFunctionCount
= pCategory
->getCount();
159 for (sal_uInt32 j
= 0; j
< nFunctionCount
; ++j
)
161 TFunctionDesc
pDesc(pCategory
->getFunction(j
));
162 if (pCharClass
->uppercase(pDesc
->getFunctionName()).indexOf(aSearchStr
) >= 0)
164 if (!pDesc
->isHidden())
166 OUString
sId(OUString::number(reinterpret_cast<sal_Int64
>(pDesc
)));
167 m_xLbFunction
->append(sId
, pDesc
->getFunctionName());
174 m_xLbFunction
->thaw();
175 // Ensure no function is selected so the Next button doesn't overwrite a
176 // function that is not in the list with an arbitrary selected one.
177 m_xLbFunction
->unselect_all();
180 SelTreeViewHdl(*m_xLbFunction
);
183 IMPL_LINK_NOARG(FuncPage
, SelComboBoxHdl
, weld::ComboBox
&, void)
185 OUString searchStr
= m_xLbFunctionSearchString
->get_text();
186 m_xLbFunction
->set_help_id(m_aHelpId
);
187 UpdateFunctionList(searchStr
);
190 IMPL_LINK_NOARG(FuncPage
, SelTreeViewHdl
, weld::TreeView
&, void)
192 const IFunctionDescription
* pDesc
= GetFuncDesc(GetFunction());
195 const OString sHelpId
= pDesc
->getHelpId();
196 if (!sHelpId
.isEmpty())
197 m_xLbFunction
->set_help_id(sHelpId
);
199 aSelectionLink
.Call(*this);
202 IMPL_LINK_NOARG(FuncPage
, DblClkHdl
, weld::TreeView
&, bool)
204 aDoubleClickLink
.Call(*this);
208 IMPL_LINK_NOARG(FuncPage
, ModifyHdl
, weld::Entry
&, void)
210 // While typing select All category.
211 m_xLbCategory
->set_active(1);
212 OUString searchStr
= m_xLbFunctionSearchString
->get_text();
213 UpdateFunctionList(searchStr
);
216 void FuncPage::SetCategory(sal_Int32 nCat
)
218 m_xLbCategory
->set_active(nCat
);
219 UpdateFunctionList(OUString());
222 sal_Int32
FuncPage::GetFuncPos(const IFunctionDescription
* _pDesc
)
224 return m_xLbFunction
->find_id(OUString::number(reinterpret_cast<sal_Int64
>(_pDesc
)));
227 void FuncPage::SetFunction(sal_Int32 nFunc
)
230 m_xLbFunction
->unselect_all();
232 m_xLbFunction
->select(nFunc
);
235 void FuncPage::SetFocus() { m_xLbFunction
->grab_focus(); }
237 sal_Int32
FuncPage::GetCategory() const { return m_xLbCategory
->get_active(); }
239 sal_Int32
FuncPage::GetCategoryEntryCount() const { return m_xLbCategory
->get_count(); }
241 sal_Int32
FuncPage::GetFunction() const { return m_xLbFunction
->get_selected_index(); }
243 sal_Int32
FuncPage::GetFunctionEntryCount() const { return m_xLbFunction
->n_children(); }
245 OUString
FuncPage::GetSelFunctionName() const { return m_xLbFunction
->get_selected_text(); }
247 const IFunctionDescription
* FuncPage::GetFuncDesc(sal_Int32 nPos
) const
251 // not pretty, but hopefully rare
252 return reinterpret_cast<const IFunctionDescription
*>(m_xLbFunction
->get_id(nPos
).toInt64());
257 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */