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  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  {
278  pScMod->SetInputMode(SC_INPUT_TABLE);
279  // the above call can result in us being disposed
281  return;
282  aString = "=" + xFuncList->get_selected_text();
283  if (pHdl)
284  pHdl->ClearText();
285  }
286  const ScFuncDesc* pDesc =
287  reinterpret_cast<const ScFuncDesc*>(xFuncList->get_selected_id().toInt64());
288  if (pDesc)
289  {
291  UpdateLRUList();
292  nArgs = pDesc->nArgCount;
293  if(nArgs>0)
294  {
295  // NOTE: Theoretically the first parameter could have the
296  // suppress flag as well, but practically it doesn't.
297  aFirstArgStr = pDesc->maDefArgNames[0];
298  aFirstArgStr = comphelper::string::strip(aFirstArgStr, ' ');
299  aFirstArgStr = aFirstArgStr.replaceAll(" ", "_");
300  aArgStr = aFirstArgStr;
301  if ( nArgs != VAR_ARGS && nArgs != PAIRED_VAR_ARGS )
302  { // no VarArgs or Fix plus VarArgs, but not VarArgs only
303  sal_uInt16 nFix;
304  if (nArgs >= PAIRED_VAR_ARGS)
305  nFix = nArgs - PAIRED_VAR_ARGS + 2;
306  else if (nArgs >= VAR_ARGS)
307  nFix = nArgs - VAR_ARGS + 1;
308  else
309  nFix = nArgs;
310  for ( sal_uInt16 nArg = 1;
311  nArg < nFix && !pDesc->pDefArgFlags[nArg].bOptional; nArg++ )
312  {
313  aArgStr.append("; ");
314  OUString sTmp = pDesc->maDefArgNames[nArg];
315  sTmp = comphelper::string::strip(sTmp, ' ');
316  sTmp = sTmp.replaceAll(" ", "_");
317  aArgStr.append(sTmp);
318  }
319  }
320  }
321  }
322  if (pHdl)
323  {
324  if (pHdl->GetEditString().isEmpty())
325  {
326  aString = "=" + xFuncList->get_selected_text();
327  }
328  EditView *pEdView=pHdl->GetActiveView();
329  if(pEdView!=nullptr) // @ needed because of crash during setting a name
330  {
331  if(nArgs>0)
332  {
333  pHdl->InsertFunction(aString);
334  pEdView->InsertText(aArgStr.makeStringAndClear(),true);
335  ESelection aESel=pEdView->GetSelection();
336  aESel.nEndPos = aESel.nStartPos + aFirstArgStr.getLength();
337  pEdView->SetSelection(aESel);
338  pHdl->DataChanged();
339  }
340  else
341  {
342  aString += "()";
343  pEdView->InsertText(aString);
344  pHdl->DataChanged();
345  }
346  }
347  }
348  InitLRUList();
349  }
350  if ( pCurSh )
351  {
352  vcl::Window* pShellWnd = pCurSh->GetWindow();
353 
354  if ( pShellWnd )
355  pShellWnd->GrabFocus();
356  }
357 
358 }
359 
360 /*************************************************************************
361 #* Handle: SelHdl
362 #*------------------------------------------------------------------------
363 #*
364 #* Class: ScFunctionWin
365 #*
366 #* Function: A change of the category will update the list of functions.
367 #*
368 #* Input: ---
369 #*
370 #* Output: ---
371 #*
372 #************************************************************************/
373 
375 {
376  UpdateFunctionList();
377  SetDescription();
378 }
379 
381 {
382  SetDescription();
383 }
384 
385 /*************************************************************************
386 #* Handle: SelHdl
387 #*------------------------------------------------------------------------
388 #*
389 #* Class: ScFunctionWin
390 #*
391 #* Function: A change of the category will update the list of functions.
392 #*
393 #* Input: ---
394 #*
395 #* Output: ---
396 #*
397 #************************************************************************/
398 
399 IMPL_LINK_NOARG( ScFunctionWin, SetSelectionClickHdl, weld::Button&, void )
400 {
401  DoEnter(); // saves the input
402 }
403 
404 IMPL_LINK_NOARG( ScFunctionWin, SetRowActivatedHdl, weld::TreeView&, bool )
405 {
406  DoEnter(); // saves the input
407  return true;
408 }
409 
410 void EnglishFunctionNameChange::setProperty(const css::uno::Any &rProperty)
411 {
412  ConfigurationListenerProperty::setProperty(rProperty);
415 }
416 
417 /* 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:215
bool bOptional
Parameter is optional.
Definition: funcdesc.hxx:212
const OUString & GetEditString()
Definition: inputhdl.cxx:4220
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:3532
aBuf
IMPL_LINK_NOARG(ScFunctionWin, SelComboHdl, weld::ComboBox &, void)
Definition: dwfunctr.cxx:374
Stores spreadsheet functions in categories, including a cumulative ('All') category and makes them ac...
Definition: funcdesc.hxx:306
EditView * GetActiveView()
Definition: inputhdl.cxx:2218
virtual void setProperty(const css::uno::Any &rProperty) override
Definition: dwfunctr.cxx:410
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:620
std::optional< OUString > mxFuncDesc
Description of function.
Definition: funcdesc.hxx:218
void InsertFunction(const OUString &rFuncName, bool bAddPar=true)
Definition: inputhdl.cxx:3485
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:2584
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