LibreOffice Module sc (master) 1
dwfunctr.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 <comphelper/string.hxx>
21#include <editeng/editview.hxx>
22#include <sfx2/viewsh.hxx>
23#include <formula/funcvarargs.h>
24
25#include <global.hxx>
26#include <scmod.hxx>
27#include <inputhdl.hxx>
28#include <tabvwsh.hxx>
29#include <funcdesc.hxx>
30
31#include <dwfunctr.hxx>
32
33/*************************************************************************
34#* Member: ScFunctionWin
35#*------------------------------------------------------------------------
36#*
37#* Class: ScFunctionWin
38#*
39#* Function: Constructor of ScFunctionWin Class
40#*
41#* Input: Sfx - links, window, resource
42#*
43#* Output: ---
44#*
45#************************************************************************/
46
48 : PanelLayout(pParent, "FunctionPanel", "modules/scalc/ui/functionpanel.ui")
49 , xCatBox(m_xBuilder->weld_combo_box("category"))
50 , xFuncList(m_xBuilder->weld_tree_view("funclist"))
51 , xInsertButton(m_xBuilder->weld_button("insert"))
52 , xFiFuncDesc(m_xBuilder->weld_text_view("funcdesc"))
53 , xConfigListener(new comphelper::ConfigurationListener("/org.openoffice.Office.Calc/Formula/Syntax"))
54 , xConfigChange(std::make_unique<EnglishFunctionNameChange>(xConfigListener, this))
55 , pFuncDesc(nullptr)
56{
58
59 nArgs=0;
60 m_aHelpId = xFuncList->get_help_id();
61
62 // Description box has a height of 8 lines of text
63 xFiFuncDesc->set_size_request(-1, 8 * xFiFuncDesc->get_text_height());
64
65 xCatBox->connect_changed(LINK( this, ScFunctionWin, SelComboHdl));
66 xFuncList->connect_changed(LINK( this, ScFunctionWin, SelTreeHdl));
67
68 xFuncList->connect_row_activated(LINK( this, ScFunctionWin, SetRowActivatedHdl));
69 xInsertButton->connect_clicked(LINK( this, ScFunctionWin, SetSelectionClickHdl));
70
71 xCatBox->set_active(0);
72
73 SelComboHdl(*xCatBox);
74}
75
76/*************************************************************************
77#* Member: ScFunctionWin
78#*------------------------------------------------------------------------
79#*
80#* Class: ScFunctionWin
81#*
82#* Function: Destructor of ScFunctionWin Class
83#*
84#* Input: ---
85#*
86#* Output: ---
87#*
88#************************************************************************/
89
91{
92 xConfigChange.reset();
93 xConfigListener->dispose();
94 xConfigListener.clear();
95
96 xCatBox.reset();
97 xFuncList.reset();
98 xInsertButton.reset();
99 xFiFuncDesc.reset();
100}
101
102/*************************************************************************
103#* Member: UpdateFunctionList
104#*------------------------------------------------------------------------
105#*
106#* Class: ScFunctionWin
107#*
108#* Function: Updates the list of functions depending on the set category
109#*
110#* Input: ---
111#*
112#* Output: ---
113#*
114#************************************************************************/
115
117{
120
121 sal_Int32 nSelPos = xCatBox->get_active();
122
123 if (nSelPos == 0)
125}
126
127/*************************************************************************
128#* Member: UpdateFunctionList
129#*------------------------------------------------------------------------
130#*
131#* Class: ScFunctionWin
132#*
133#* Function: Updates the list of last used functions.
134#*
135#* Input: ---
136#*
137#* Output: ---
138#*
139#************************************************************************/
140
142{
143 if (pFuncDesc && pFuncDesc->nFIndex!=0)
144 {
145 ScModule* pScMod = SC_MOD();
147 }
148}
149
150/*************************************************************************
151#* Member: SetDescription
152#*------------------------------------------------------------------------
153#*
154#* Class: ScFunctionWin
155#*
156#* Function:
157#*
158#* Input: ---
159#*
160#* Output: ---
161#*
162#************************************************************************/
163
165{
166 xFiFuncDesc->set_text(OUString());
167 const ScFuncDesc* pDesc =
168 weld::fromId<const ScFuncDesc*>(xFuncList->get_selected_id());
169 if (pDesc)
170 {
171 pDesc->initArgumentInfo(); // full argument info is needed
172
173 OUString aBuf = xFuncList->get_selected_text() +
174 ":\n\n" +
175 pDesc->GetParamList() +
176 "\n\n" +
177 *pDesc->mxFuncDesc;
178
179 xFiFuncDesc->set_text(aBuf);
180
181 // Update help ID for the selected entry
182 const OUString sHelpId = pDesc->getHelpId();
183 if (!sHelpId.isEmpty())
184 xFuncList->set_help_id(pDesc->getHelpId());
185 else
186 xFuncList->set_help_id(m_aHelpId);
187 }
188}
189
190/*************************************************************************
191#* Member: UpdateFunctionList
192#*------------------------------------------------------------------------
193#*
194#* Class: ScFunctionWin
195#*
196#* Function: Updates the list of functions depending on the set category
197#*
198#* Input: ---
199#*
200#* Output: ---
201#*
202#************************************************************************/
203
205{
206 sal_Int32 nSelPos = xCatBox->get_active();
207 sal_Int32 nCategory = ( -1 != nSelPos )
208 ? (nSelPos-1) : 0;
209
210 xFuncList->clear();
211 xFuncList->freeze();
212
213 if ( nSelPos > 0 )
214 {
216
217 const ScFuncDesc* pDesc = pFuncMgr->First( nCategory );
218 while ( pDesc )
219 {
220 xFuncList->append(weld::toId(pDesc), *(pDesc->mxFuncName));
221 pDesc = pFuncMgr->Next();
222 }
223 }
224 else // LRU list
225 {
226 for (const formula::IFunctionDescription* pDesc : aLRUList)
227 {
228 if (pDesc)
229 {
230 xFuncList->append(weld::toId(pDesc), pDesc->getFunctionName());
231 }
232 }
233 }
234
235 xFuncList->thaw();
236
237 if (xFuncList->n_children() > 0)
238 {
239 xFuncList->set_sensitive(true);
240 xFuncList->select(0);
241 }
242 else
243 {
244 xFuncList->set_sensitive(false);
245 }
246}
247
248/*************************************************************************
249#* Member: DoEnter
250#*------------------------------------------------------------------------
251#*
252#* Class: ScFunctionWin
253#*
254#* Function: Save input into document. Is called after clicking the
255#* Apply button or a double-click on the function list.
256#*
257#* Input: ---
258#*
259#* Output: ---
260#*
261#************************************************************************/
262
264{
265 OUStringBuffer aArgStr;
266 OUString aString=xFuncList->get_selected_text();
268 nArgs=0;
269
270 if(!aString.isEmpty())
271 {
272 OUString aFirstArgStr;
273 ScModule* pScMod = SC_MOD();
274 ScTabViewShell* pViewSh = dynamic_cast<ScTabViewShell*>( pCurSh );
275 ScInputHandler* pHdl = pScMod->GetInputHdl( pViewSh );
276 if(!pScMod->IsEditMode())
277 {
280 // the above call can result in us being disposed
281 if (xDetectDisposed->isDisposed())
282 return;
283 aString = "=" + xFuncList->get_selected_text();
284 if (pHdl)
285 pHdl->ClearText();
286 }
287 const ScFuncDesc* pDesc =
288 weld::fromId<const ScFuncDesc*>(xFuncList->get_selected_id());
289 if (pDesc)
290 {
291 pFuncDesc=pDesc;
293 nArgs = pDesc->nArgCount;
294 if(nArgs>0)
295 {
296 // NOTE: Theoretically the first parameter could have the
297 // suppress flag as well, but practically it doesn't.
298 aFirstArgStr = pDesc->maDefArgNames[0];
299 aFirstArgStr = comphelper::string::strip(aFirstArgStr, ' ');
300 aFirstArgStr = aFirstArgStr.replaceAll(" ", "_");
301 aArgStr = aFirstArgStr;
302 if ( nArgs != VAR_ARGS && nArgs != PAIRED_VAR_ARGS )
303 { // no VarArgs or Fix plus VarArgs, but not VarArgs only
304 sal_uInt16 nFix;
305 if (nArgs >= PAIRED_VAR_ARGS)
306 nFix = nArgs - PAIRED_VAR_ARGS + 2;
307 else if (nArgs >= VAR_ARGS)
308 nFix = nArgs - VAR_ARGS + 1;
309 else
310 nFix = nArgs;
311 for ( sal_uInt16 nArg = 1;
312 nArg < nFix && !pDesc->pDefArgFlags[nArg].bOptional; nArg++ )
313 {
314 aArgStr.append("; ");
315 OUString sTmp = pDesc->maDefArgNames[nArg];
316 sTmp = comphelper::string::strip(sTmp, ' ');
317 sTmp = sTmp.replaceAll(" ", "_");
318 aArgStr.append(sTmp);
319 }
320 }
321 }
322 }
323 if (pHdl)
324 {
325 if (pHdl->GetEditString().isEmpty())
326 {
327 aString = "=" + xFuncList->get_selected_text();
328 }
329 EditView *pEdView=pHdl->GetActiveView();
330 if(pEdView!=nullptr) // @ needed because of crash during setting a name
331 {
332 if(nArgs>0)
333 {
334 pHdl->InsertFunction(aString);
335 pEdView->InsertText(aArgStr.makeStringAndClear(),true);
336 ESelection aESel=pEdView->GetSelection();
337 aESel.nEndPos = aESel.nStartPos + aFirstArgStr.getLength();
338 pEdView->SetSelection(aESel);
339 pHdl->DataChanged();
340 }
341 else
342 {
343 aString += "()";
344 pEdView->InsertText(aString);
345 pHdl->DataChanged();
346 }
347 }
348 }
349 InitLRUList();
350 }
351 if ( pCurSh )
352 {
353 vcl::Window* pShellWnd = pCurSh->GetWindow();
354
355 if ( pShellWnd )
356 pShellWnd->GrabFocus();
357 }
358
359}
360
361/*************************************************************************
362#* Handle: SelHdl
363#*------------------------------------------------------------------------
364#*
365#* Class: ScFunctionWin
366#*
367#* Function: A change of the category will update the list of functions.
368#*
369#* Input: ---
370#*
371#* Output: ---
372#*
373#************************************************************************/
374
376{
377 UpdateFunctionList();
378 SetDescription();
379}
380
382{
383 SetDescription();
384}
385
386/*************************************************************************
387#* Handle: SelHdl
388#*------------------------------------------------------------------------
389#*
390#* Class: ScFunctionWin
391#*
392#* Function: A change of the category will update the list of functions.
393#*
394#* Input: ---
395#*
396#* Output: ---
397#*
398#************************************************************************/
399
400IMPL_LINK_NOARG( ScFunctionWin, SetSelectionClickHdl, weld::Button&, void )
401{
402 DoEnter(); // saves the input
403}
404
405IMPL_LINK_NOARG( ScFunctionWin, SetRowActivatedHdl, weld::TreeView&, bool )
406{
407 DoEnter(); // saves the input
408 return true;
409}
410
411void EnglishFunctionNameChange::setProperty(const css::uno::Any &rProperty)
412{
413 ConfigurationListenerProperty::setProperty(rProperty);
416}
417
418/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ESelection GetSelection() const
void SetSelection(const ESelection &rNewSel)
void InsertText(const OUString &rNew, bool bSelect=false, bool bLOKShowSelect=true)
virtual void setProperty(const css::uno::Any &rProperty) override
Definition: dwfunctr.cxx:411
ScFunctionWin * m_pFunctionWin
Definition: dwfunctr.hxx:31
Stores and generates human readable descriptions for spreadsheet-functions, e.g. functions used in fo...
Definition: funcdesc.hxx:41
sal_uInt16 nArgCount
All parameter count, suppressed and unsuppressed.
Definition: funcdesc.hxx:223
virtual void initArgumentInfo() const override
Requests function data from AddInCollection.
Definition: funcdesc.cxx:310
std::vector< OUString > maDefArgNames
Parameter name(s)
Definition: funcdesc.hxx:218
virtual OUString getHelpId() const override
Returns the help id of the function.
Definition: funcdesc.cxx:336
std::optional< OUString > mxFuncDesc
Description of function.
Definition: funcdesc.hxx:217
ParameterFlags * pDefArgFlags
Flags for each parameter.
Definition: funcdesc.hxx:220
sal_uInt16 nFIndex
Unique function index.
Definition: funcdesc.hxx:221
std::optional< OUString > mxFuncName
Function name.
Definition: funcdesc.hxx:216
OUString GetParamList() const
Returns list of all parameter names.
Definition: funcdesc.cxx:146
Stores spreadsheet functions in categories, including a cumulative ('All') category and makes them ac...
Definition: funcdesc.hxx:309
const ScFuncDesc * Next() const
Returns the next function of the current category.
Definition: funcdesc.cxx:1102
const ScFuncDesc * First(sal_uInt16 nCategory=0) const
Returns the first function in category nCategory.
Definition: funcdesc.cxx:1084
virtual void fillLastRecentlyUsedFunctions(::std::vector< const formula::IFunctionDescription * > &_rLastRUFunctions) const override
Appends the last recently used functions.
Definition: funcdesc.cxx:1131
::std::vector< const formula::IFunctionDescription * > aLRUList
Definition: dwfunctr.hxx:57
void SetDescription()
Definition: dwfunctr.cxx:164
ScFunctionWin(weld::Widget *pParent)
Definition: dwfunctr.cxx:47
OUString m_aHelpId
Definition: dwfunctr.hxx:55
void UpdateLRUList()
Definition: dwfunctr.cxx:141
std::unique_ptr< weld::TextView > xFiFuncDesc
Definition: dwfunctr.hxx:49
void InitLRUList()
Definition: dwfunctr.cxx:116
std::unique_ptr< weld::TreeView > xFuncList
Definition: dwfunctr.hxx:47
void UpdateFunctionList()
Definition: dwfunctr.cxx:204
void DoEnter()
Definition: dwfunctr.cxx:263
std::unique_ptr< weld::Button > xInsertButton
Definition: dwfunctr.hxx:48
std::unique_ptr< EnglishFunctionNameChange > xConfigChange
Definition: dwfunctr.hxx:52
const ScFuncDesc * pFuncDesc
Definition: dwfunctr.hxx:53
rtl::Reference< comphelper::ConfigurationListener > xConfigListener
Definition: dwfunctr.hxx:51
std::unique_ptr< weld::ComboBox > xCatBox
Definition: dwfunctr.hxx:46
virtual ~ScFunctionWin() override
Definition: dwfunctr.cxx:90
sal_uInt16 nArgs
Definition: dwfunctr.hxx:54
static ScFunctionMgr * GetStarCalcFunctionMgr()
Definition: global.cxx:635
void DataChanged(bool bFromTopNotify=false, bool bSetModified=true)
Definition: inputhdl.cxx:2722
EditView * GetActiveView()
Definition: inputhdl.cxx:2355
void InsertFunction(const OUString &rFuncName, bool bAddPar=true)
Definition: inputhdl.cxx:3663
void ClearText()
Definition: inputhdl.cxx:3710
const OUString & GetEditString()
Definition: inputhdl.cxx:4483
ScInputHandler * GetInputHdl(ScTabViewShell *pViewSh=nullptr, bool bUseRef=true)
Input-Handler.
Definition: scmod.cxx:1355
void SetInputMode(ScInputMode eMode, const OUString *pInitText=nullptr)
Definition: scmod.cxx:1386
bool IsEditMode()
Definition: scmod.cxx:1393
void InsertEntryToLRUList(sal_uInt16 nFIndex)
Definition: scmod.cxx:754
static SAL_WARN_UNUSED_RESULT SfxViewShell * Current()
vcl::Window * GetWindow() const
void GrabFocus()
IMPL_LINK_NOARG(ScFunctionWin, SelComboHdl, weld::ComboBox &, void)
Definition: dwfunctr.cxx:375
#define VAR_ARGS
#define PAIRED_VAR_ARGS
@ SC_INPUT_TABLE
Definition: global.hxx:363
aBuf
OString strip(const OString &rIn, char c)
OUString toId(const void *pValue)
#define SC_MOD()
Definition: scmod.hxx:247
sal_Int32 nEndPos
sal_Int32 nStartPos
bool bOptional
Parameter is optional.
Definition: funcdesc.hxx:211