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 
47 ScFunctionWin::ScFunctionWin(vcl::Window* pParent, const css::uno::Reference<css::frame::XFrame> &rFrame)
48  : PanelLayout(pParent, "FunctionPanel", "modules/scalc/ui/functionpanel.ui", rFrame)
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_label("funcdesc"))
53  , xConfigListener(new comphelper::ConfigurationListener("/org.openoffice.Office.Calc/Formula/Syntax"))
54  , xConfigChange(std::make_unique<EnglishFunctionNameChange>(xConfigListener, this))
55  , pFuncDesc(nullptr)
56 {
57  xFuncList->set_size_request(-1, xFuncList->get_height_rows(10));
58 
59  InitLRUList();
60 
61  nArgs=0;
62  xFiFuncDesc->set_size_request(-1, 5 * xFiFuncDesc->get_text_height());
63 
64  xCatBox->connect_changed(LINK( this, ScFunctionWin, SelComboHdl));
65  xFuncList->connect_changed(LINK( this, ScFunctionWin, SelTreeHdl));
66 
67  xFuncList->connect_row_activated(LINK( this, ScFunctionWin, SetRowActivatedHdl));
68  xInsertButton->connect_clicked(LINK( this, ScFunctionWin, SetSelectionClickHdl));
69 
70  xCatBox->set_active(0);
71 
72  SelComboHdl(*xCatBox);
73 }
74 
75 /*************************************************************************
76 #* Member: ScFunctionWin
77 #*------------------------------------------------------------------------
78 #*
79 #* Class: ScFunctionWin
80 #*
81 #* Function: Destructor of ScFunctionWin Class
82 #*
83 #* Input: ---
84 #*
85 #* Output: ---
86 #*
87 #************************************************************************/
88 
90 {
91  disposeOnce();
92 }
93 
95 {
96  if (xConfigChange)
97  {
98  xConfigChange.reset();
99  xConfigListener->dispose();
100  xConfigListener.clear();
101  }
102  xCatBox.reset();
103  xFuncList.reset();
104  xInsertButton.reset();
105  xFiFuncDesc.reset();
107 }
108 
109 /*************************************************************************
110 #* Member: UpdateFunctionList
111 #*------------------------------------------------------------------------
112 #*
113 #* Class: ScFunctionWin
114 #*
115 #* Function: Updates the list of functions depending on the set category
116 #*
117 #* Input: ---
118 #*
119 #* Output: ---
120 #*
121 #************************************************************************/
122 
124 {
127 
128  sal_Int32 nSelPos = xCatBox->get_active();
129 
130  if (nSelPos == 0)
132 }
133 
134 /*************************************************************************
135 #* Member: UpdateFunctionList
136 #*------------------------------------------------------------------------
137 #*
138 #* Class: ScFunctionWin
139 #*
140 #* Function: Updates the list of last used functions.
141 #*
142 #* Input: ---
143 #*
144 #* Output: ---
145 #*
146 #************************************************************************/
147 
149 {
150  if (pFuncDesc && pFuncDesc->nFIndex!=0)
151  {
152  ScModule* pScMod = SC_MOD();
154  }
155 }
156 
157 /*************************************************************************
158 #* Member: SetDescription
159 #*------------------------------------------------------------------------
160 #*
161 #* Class: ScFunctionWin
162 #*
163 #* Function:
164 #*
165 #* Input: ---
166 #*
167 #* Output: ---
168 #*
169 #************************************************************************/
170 
172 {
173  xFiFuncDesc->set_label(EMPTY_OUSTRING);
174  const ScFuncDesc* pDesc =
175  reinterpret_cast<const ScFuncDesc*>(xFuncList->get_selected_id().toInt64());
176  if (pDesc)
177  {
178  pDesc->initArgumentInfo(); // full argument info is needed
179 
180  OUStringBuffer aBuf(xFuncList->get_selected_text());
181  aBuf.append(":\n\n");
182  aBuf.append(pDesc->GetParamList());
183  aBuf.append("\n\n");
184  aBuf.append(*pDesc->mxFuncDesc);
185 
186  xFiFuncDesc->set_label(aBuf.makeStringAndClear());
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(OUString::number(reinterpret_cast<sal_Int64>(pDesc)), *(pDesc->mxFuncName));
221  pDesc = pFuncMgr->Next();
222  }
223  }
224  else // LRU list
225  {
227  {
228  if (pDesc)
229  {
230  xFuncList->append(OUString::number(reinterpret_cast<sal_Int64>(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  OUString aFirstArgStr;
266  OUStringBuffer aArgStr;
267  OUString aString=xFuncList->get_selected_text();
269  nArgs=0;
270 
271  if(!aString.isEmpty())
272  {
273 
274  ScModule* pScMod = SC_MOD();
275  ScTabViewShell* pViewSh = dynamic_cast<ScTabViewShell*>( pCurSh );
276  ScInputHandler* pHdl = pScMod->GetInputHdl( pViewSh );
277  if(!pScMod->IsEditMode())
278  {
279  pScMod->SetInputMode(SC_INPUT_TABLE);
280  // the above call can result in us being disposed
282  return;
283  aString = "=" + xFuncList->get_selected_text();
284  if (pHdl)
285  pHdl->ClearText();
286  }
287  const ScFuncDesc* pDesc =
288  reinterpret_cast<const ScFuncDesc*>(xFuncList->get_selected_id().toInt64());
289  if (pDesc)
290  {
292  UpdateLRUList();
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 
400 IMPL_LINK_NOARG( ScFunctionWin, SetSelectionClickHdl, weld::Button&, void )
401 {
402  DoEnter(); // saves the input
403 }
404 
405 IMPL_LINK_NOARG( ScFunctionWin, SetRowActivatedHdl, weld::TreeView&, bool )
406 {
407  DoEnter(); // saves the input
408  return true;
409 }
410 
411 void EnglishFunctionNameChange::setProperty(const css::uno::Any &rProperty)
412 {
413  ConfigurationListenerProperty::setProperty(rProperty);
416 }
417 
418 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
void InitLRUList()
Definition: dwfunctr.cxx:123
VclPtr< ScFunctionWin > m_xFunctionWin
Definition: dwfunctr.hxx:32
#define PAIRED_VAR_ARGS
bool IsEditMode()
Definition: scmod.cxx:1341
#define EMPTY_OUSTRING
Definition: global.hxx:214
bool bOptional
Parameter is optional.
Definition: funcdesc.hxx:212
const OUString & GetEditString()
Definition: inputhdl.cxx:4215
const ScFuncDesc * Next() const
Returns the next function of the current category.
Definition: funcdesc.cxx:1126
sal_uInt16 char char * pDesc
Definition: callform.cxx:58
void SetDescription()
Definition: dwfunctr.cxx:171
void ClearText()
Definition: inputhdl.cxx:3526
aBuf
IMPL_LINK_NOARG(ScFunctionWin, SelComboHdl, weld::ComboBox &, void)
Definition: dwfunctr.cxx:375
Stores spreadsheet functions in categories, including a cumulative ('All') category and makes them ac...
Definition: funcdesc.hxx:306
EditView * GetActiveView()
Definition: inputhdl.cxx:2212
virtual void setProperty(const css::uno::Any &rProperty) override
Definition: dwfunctr.cxx:411
void InsertEntryToLRUList(sal_uInt16 nFIndex)
Definition: scmod.cxx:687
static SfxViewShell * Current()
ScFunctionWin(vcl::Window *pParent, const css::uno::Reference< css::frame::XFrame > &rFrame)
Definition: dwfunctr.cxx:47
ParameterFlags * pDefArgFlags
Flags for each parameter.
Definition: funcdesc.hxx:221
sal_Int32 nEndPos
OUString GetParamList() const
Returns list of all parameter names.
Definition: funcdesc.cxx:146
std::unique_ptr< weld::TreeView > xFuncList
Definition: dwfunctr.hxx:48
#define VAR_ARGS
#define SC_MOD()
Definition: scmod.hxx:253
std::unique_ptr< EnglishFunctionNameChange > xConfigChange
Definition: dwfunctr.hxx:53
static ScFunctionMgr * GetStarCalcFunctionMgr()
Definition: global.cxx:626
std::optional< OUString > mxFuncDesc
Description of function.
Definition: funcdesc.hxx:218
void InsertFunction(const OUString &rFuncName, bool bAddPar=true)
Definition: inputhdl.cxx:3479
std::unique_ptr< weld::Button > xInsertButton
Definition: dwfunctr.hxx:49
void InsertText(const OUString &rNew, bool bSelect=false)
void GrabFocus()
virtual ~ScFunctionWin() override
Definition: dwfunctr.cxx:89
void SetInputMode(ScInputMode eMode, const OUString *pInitText=nullptr)
Definition: scmod.cxx:1334
void UpdateLRUList()
Definition: dwfunctr.cxx:148
rtl::Reference< comphelper::ConfigurationListener > xConfigListener
Definition: dwfunctr.hxx:52
virtual void dispose() override
Definition: dwfunctr.cxx:94
virtual void dispose() override
virtual void fillLastRecentlyUsedFunctions(::std::vector< const formula::IFunctionDescription * > &_rLastRUFunctions) const override
Appends the last recently used functions.
Definition: funcdesc.cxx:1155
std::optional< OUString > mxFuncName
Function name.
Definition: funcdesc.hxx:217
OString strip(const OString &rIn, char c)
virtual void initArgumentInfo() const override
Requests function data from AddInCollection.
Definition: funcdesc.cxx:327
sal_uInt16 nFIndex
Unique function index.
Definition: funcdesc.hxx:222
void DataChanged(bool bFromTopNotify=false, bool bSetModified=true)
Definition: inputhdl.cxx:2578
const ScFuncDesc * pFuncDesc
Definition: dwfunctr.hxx:54
vcl::Window * GetWindow() const
void SetSelection(const ESelection &rNewSel)
std::unique_ptr< weld::ComboBox > xCatBox
Definition: dwfunctr.hxx:47
ESelection GetSelection() const
std::vector< OUString > maDefArgNames
Parameter name(s)
Definition: funcdesc.hxx:219
ScInputHandler * GetInputHdl(ScTabViewShell *pViewSh=nullptr, bool bUseRef=true)
Input-Handler.
Definition: scmod.cxx:1303
std::unique_ptr< weld::Label > xFiFuncDesc
Definition: dwfunctr.hxx:50
void DoEnter()
Definition: dwfunctr.cxx:263
sal_uInt16 nArgs
Definition: dwfunctr.hxx:55
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:224
bool isDisposed() const
void UpdateFunctionList()
Definition: dwfunctr.cxx:204
::std::vector< const formula::IFunctionDescription * > aLRUList
Definition: dwfunctr.hxx:57
const ScFuncDesc * First(sal_uInt16 nCategory=0) const
Returns the first function in category nCategory.
Definition: funcdesc.cxx:1108
sal_Int32 nStartPos