Version 4.0.2.1, tag libreoffice-4.0.2.1
[LibreOffice.git] / formula / source / ui / dlg / parawin.cxx
blob23d8ec8624207c98890fac3344a62d56f3cb89c5
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 <comphelper/string.hxx>
21 #include <svl/zforlist.hxx>
22 #include <svl/stritem.hxx>
24 #include "parawin.hxx"
25 #include "parawin.hrc"
26 #include "helpids.hrc"
27 #include "formula/formdata.hxx"
28 #include "formula/IFunctionDescription.hxx"
29 #include "ModuleHelper.hxx"
30 #include "ForResId.hrc"
32 #define VAR_ARGS 30
33 #define PAIRED_VAR_ARGS (VAR_ARGS + VAR_ARGS)
34 namespace formula
36 //============================================================================
38 ParaWin::ParaWin(Window* pParent,IControlReferenceHandler* _pDlg,Point aPos):
39 TabPage (pParent,ModuleRes(RID_FORMULATAB_PARAMETER)),
40 pFuncDesc ( NULL ),
41 pMyParent (_pDlg),
42 aFtEditDesc ( this, ModuleRes( FT_EDITDESC ) ),
43 aFtArgName ( this, ModuleRes( FT_PARNAME ) ),
44 aFtArgDesc ( this, ModuleRes( FT_PARDESC ) ),
46 aBtnFx1 ( this, ModuleRes( BTN_FX1 ) ),
47 aFtArg1 ( this, ModuleRes( FT_ARG1 ) ),
48 aEdArg1 ( this, ModuleRes( ED_ARG1 ) ),
49 aRefBtn1 ( this, ModuleRes( RB_ARG1 ) ),
51 aBtnFx2 ( this, ModuleRes( BTN_FX2 ) ),
52 aFtArg2 ( this, ModuleRes( FT_ARG2 ) ),
53 aEdArg2 ( this, ModuleRes( ED_ARG2 ) ),
54 aRefBtn2 ( this, ModuleRes( RB_ARG2 ) ),
56 aBtnFx3 ( this, ModuleRes( BTN_FX3 ) ),
57 aFtArg3 ( this, ModuleRes( FT_ARG3 ) ),
58 aEdArg3 ( this, ModuleRes( ED_ARG3 ) ),
59 aRefBtn3 ( this, ModuleRes( RB_ARG3 ) ),
61 aBtnFx4 ( this, ModuleRes( BTN_FX4 ) ),
62 aFtArg4 ( this, ModuleRes( FT_ARG4 ) ),
63 aEdArg4 ( this, ModuleRes( ED_ARG4 ) ),
64 aRefBtn4 ( this, ModuleRes( RB_ARG4 ) ),
66 aSlider ( this, ModuleRes( WND_SLIDER ) ),
67 m_sOptional ( ModuleRes( STR_OPTIONAL ) ),
68 m_sRequired ( ModuleRes( STR_REQUIRED ) ),
69 bRefMode (sal_False)
71 FreeResource();
72 aDefaultString=aFtEditDesc.GetText();
74 SetPosPixel(aPos);
75 nEdFocus=NOT_FOUND;
76 nActiveLine=0;
77 Size aSize = aSlider.GetSizePixel();
78 aSize.Width() = GetSettings().GetStyleSettings().GetScrollBarSize();
79 aSlider.SetSizePixel( aSize );
80 aSlider.SetEndScrollHdl( LINK( this, ParaWin, ScrollHdl ) );
81 aSlider.SetScrollHdl( LINK( this, ParaWin, ScrollHdl ) );
83 InitArgInput( 0, aFtArg1, aBtnFx1, aEdArg1, aRefBtn1);
84 InitArgInput( 1, aFtArg2, aBtnFx2, aEdArg2, aRefBtn2);
85 InitArgInput( 2, aFtArg3, aBtnFx3, aEdArg3, aRefBtn3);
86 InitArgInput( 3, aFtArg4, aBtnFx4, aEdArg4, aRefBtn4);
87 ClearAll();
90 void ParaWin::UpdateArgDesc( sal_uInt16 nArg )
92 if (nArg==NOT_FOUND) return;
94 if ( nArgs > 4 )
95 nArg = sal::static_int_cast<sal_uInt16>( nArg + GetSliderPos() );
97 if ( (nArgs > 0) && (nArg<nArgs) )
99 String aArgDesc;
100 String aArgName;
102 SetArgumentDesc( String() );
103 SetArgumentText( String() );
105 if ( nArgs < VAR_ARGS )
107 sal_uInt16 nRealArg = (aVisibleArgMapping.size() < nArg) ? aVisibleArgMapping[nArg] : nArg;
108 aArgDesc = pFuncDesc->getParameterDescription(nRealArg);
109 aArgName = pFuncDesc->getParameterName(nRealArg);
110 aArgName += ' ';
111 aArgName += (pFuncDesc->isParameterOptional(nRealArg)) ? m_sOptional : m_sRequired ;
113 else if ( nArgs < PAIRED_VAR_ARGS )
115 sal_uInt16 nFix = nArgs - VAR_ARGS;
116 sal_uInt16 nPos = ( nArg < nFix ? nArg : nFix );
117 sal_uInt16 nRealArg = (nPos < aVisibleArgMapping.size() ?
118 aVisibleArgMapping[nPos] : aVisibleArgMapping.back());
119 aArgDesc = pFuncDesc->getParameterDescription(nRealArg);
120 aArgName = pFuncDesc->getParameterName(nRealArg);
121 if ( nArg >= nFix )
122 aArgName += String::CreateFromInt32(nArg-nFix+1);
123 aArgName += ' ';
125 aArgName += (nArg > nFix || pFuncDesc->isParameterOptional(nRealArg)) ? m_sOptional : m_sRequired ;
127 else
129 sal_uInt16 nFix = nArgs - PAIRED_VAR_ARGS;
130 sal_uInt16 nPos;
131 if ( nArg < nFix )
132 nPos = nArg;
133 else
134 nPos = nFix + ( (nArg-nFix) % 2);
135 sal_uInt16 nRealArg = (nPos < aVisibleArgMapping.size() ?
136 aVisibleArgMapping[nPos] : aVisibleArgMapping.back());
137 aArgDesc = pFuncDesc->getParameterDescription(nRealArg);
138 aArgName = pFuncDesc->getParameterName(nRealArg);
139 if ( nArg >= nFix )
140 aArgName += String::CreateFromInt32((nArg-nFix)/2 + 1);
141 aArgName += ' ';
143 aArgName += (nArg > (nFix+1) || pFuncDesc->isParameterOptional(nRealArg)) ? m_sOptional : m_sRequired ;
146 SetArgumentDesc(aArgDesc);
147 SetArgumentText(aArgName);
151 void ParaWin::UpdateArgInput( sal_uInt16 nOffset, sal_uInt16 i )
153 sal_uInt16 nArg = nOffset + i;
154 if ( nArgs < VAR_ARGS)
156 if(nArg<nArgs)
158 sal_uInt16 nRealArg = aVisibleArgMapping[nArg];
159 SetArgNameFont (i,(pFuncDesc->isParameterOptional(nRealArg))
160 ? aFntLight : aFntBold );
161 SetArgName (i,pFuncDesc->getParameterName(nRealArg));
164 else if ( nArgs < PAIRED_VAR_ARGS)
166 sal_uInt16 nFix = nArgs - VAR_ARGS;
167 sal_uInt16 nPos = ( nArg < nFix ? nArg : nFix );
168 sal_uInt16 nRealArg = (nPos < aVisibleArgMapping.size() ?
169 aVisibleArgMapping[nPos] : aVisibleArgMapping.back());
170 SetArgNameFont( i,
171 (nArg > nFix || pFuncDesc->isParameterOptional(nRealArg)) ?
172 aFntLight : aFntBold );
173 if ( nArg >= nFix )
175 String aArgName( pFuncDesc->getParameterName(nRealArg) );
176 aArgName += String::CreateFromInt32(nArg-nFix+1);
177 SetArgName( i, aArgName );
179 else
180 SetArgName( i, pFuncDesc->getParameterName(nRealArg) );
182 else
184 sal_uInt16 nFix = nArgs - PAIRED_VAR_ARGS;
185 sal_uInt16 nPos;
186 if ( nArg < nFix )
187 nPos = nArg;
188 else
189 nPos = nFix + ( (nArg-nFix) % 2);
190 sal_uInt16 nRealArg = (nPos < aVisibleArgMapping.size() ?
191 aVisibleArgMapping[nPos] : aVisibleArgMapping.back());
192 SetArgNameFont( i,
193 (nArg > (nFix+1) || pFuncDesc->isParameterOptional(nRealArg)) ?
194 aFntLight : aFntBold );
195 if ( nArg >= nFix )
197 String aArgName( pFuncDesc->getParameterName(nRealArg) );
198 aArgName += String::CreateFromInt32((nArg-nFix)/2 + 1);
199 SetArgName( i, aArgName );
201 else
202 SetArgName( i, pFuncDesc->getParameterName(nRealArg) );
204 if(nArg<nArgs) SetArgVal(i,aParaArray[nArg]);
208 ParaWin::~ParaWin()
210 // #i66422# if the focus changes during destruction of the controls,
211 // don't call the focus handlers
212 Link aEmptyLink;
213 aBtnFx1.SetGetFocusHdl( aEmptyLink );
214 aBtnFx2.SetGetFocusHdl( aEmptyLink );
215 aBtnFx3.SetGetFocusHdl( aEmptyLink );
216 aBtnFx4.SetGetFocusHdl( aEmptyLink );
219 sal_uInt16 ParaWin::GetActiveLine()
221 return nActiveLine;
224 void ParaWin::SetActiveLine(sal_uInt16 no)
226 if(no<nArgs)
228 long nOffset = GetSliderPos();
229 nActiveLine=no;
230 long nNewEdPos=(long)nActiveLine-nOffset;
231 if(nNewEdPos<0 || nNewEdPos>3)
233 nOffset+=nNewEdPos;
234 SetSliderPos((sal_uInt16) nOffset);
235 nOffset=GetSliderPos();
237 nEdFocus=no-(sal_uInt16)nOffset;
238 UpdateArgDesc( nEdFocus );
242 RefEdit* ParaWin::GetActiveEdit()
244 if(nArgs>0 && nEdFocus!=NOT_FOUND)
246 return aArgInput[nEdFocus].GetArgEdPtr();
248 else
250 return NULL;
255 String ParaWin::GetArgument(sal_uInt16 no)
257 String aStr;
258 if(no<aParaArray.size())
260 aStr=aParaArray[no];
261 if(no==nActiveLine && aStr.Len()==0)
262 aStr+=' ';
264 return aStr;
267 String ParaWin::GetActiveArgName()
269 String aStr;
270 if(nArgs>0 && nEdFocus!=NOT_FOUND)
272 aStr=aArgInput[nEdFocus].GetArgName();
274 return aStr;
278 void ParaWin::SetArgument(sal_uInt16 no, const String& aString)
280 if (no < aParaArray.size())
281 aParaArray[no] = comphelper::string::stripStart(aString, ' ');
284 void ParaWin::DelParaArray()
286 ::std::vector<String>().swap(aParaArray);
289 void ParaWin::SetArgumentFonts(const Font&aBoldFont,const Font&aLightFont)
291 aFntBold=aBoldFont;
292 aFntLight=aLightFont;
295 void ParaWin::SetFunctionDesc(const IFunctionDescription* pFDesc)
297 pFuncDesc=pFDesc;
299 SetArgumentDesc( String() );
300 SetArgumentText( String() );
301 SetEditDesc( String() );
302 nArgs = 0;
303 if ( pFuncDesc!=NULL)
305 if ( !pFuncDesc->getDescription().isEmpty() )
307 SetEditDesc(pFuncDesc->getDescription());
309 else
311 SetEditDesc(aDefaultString);
313 nArgs = pFuncDesc->getSuppressedArgumentCount();
314 pFuncDesc->fillVisibleArgumentMapping(aVisibleArgMapping);
315 aSlider.Hide();
316 rtl::OString sHelpId = pFuncDesc->getHelpId();
317 SetHelpId( sHelpId );
318 aEdArg1.SetHelpId( sHelpId );
319 aEdArg2.SetHelpId( sHelpId );
320 aEdArg3.SetHelpId( sHelpId );
321 aEdArg4.SetHelpId( sHelpId );
323 // Unique-IDs muessen gleich bleiben fuer Automatisierung
324 SetUniqueId( HID_FORMULA_FAP_PAGE );
325 aEdArg1.SetUniqueId( HID_FORMULA_FAP_EDIT1 );
326 aEdArg2.SetUniqueId( HID_FORMULA_FAP_EDIT2 );
327 aEdArg3.SetUniqueId( HID_FORMULA_FAP_EDIT3 );
328 aEdArg4.SetUniqueId( HID_FORMULA_FAP_EDIT4 );
329 SetActiveLine(0);
331 else
333 nActiveLine=0;
338 void ParaWin::SetArgumentText(const String& aText)
340 aFtArgName.SetText(aText);
343 void ParaWin::SetArgumentDesc(const String& aText)
345 aFtArgDesc.SetText(aText);
348 void ParaWin::SetEditDesc(const String& aText)
350 aFtEditDesc.SetText(aText);
353 void ParaWin::SetArgName(sal_uInt16 no,const String& aText)
355 aArgInput[no].SetArgName(aText);
358 void ParaWin::SetArgNameFont(sal_uInt16 no,const Font& aFont)
360 aArgInput[no].SetArgNameFont(aFont);
363 void ParaWin::SetArgVal(sal_uInt16 no,const String& aText)
365 aArgInput[no].SetArgVal(aText);
368 void ParaWin::HideParaLine(sal_uInt16 no)
370 aArgInput[no].Hide();
373 void ParaWin::ShowParaLine(sal_uInt16 no)
375 aArgInput[no].Show();
378 void ParaWin::SetEdFocus(sal_uInt16 no)
380 UpdateArgDesc(no);
381 if(no<4 && no<aParaArray.size())
382 aArgInput[no].GetArgEdPtr()->GrabFocus();
386 void ParaWin::InitArgInput( sal_uInt16 nPos, FixedText& rFtArg, ImageButton& rBtnFx,
387 ArgEdit& rEdArg, RefButton& rRefBtn)
390 rRefBtn.SetReferences(pMyParent,&rEdArg);
391 rEdArg.SetRefDialog(pMyParent);
393 aArgInput[nPos].InitArgInput (&rFtArg,&rBtnFx,&rEdArg,&rRefBtn);
395 aArgInput[nPos].Hide();
397 aArgInput[nPos].SetFxClickHdl ( LINK( this, ParaWin, GetFxHdl ) );
398 aArgInput[nPos].SetFxFocusHdl ( LINK( this, ParaWin, GetFxFocusHdl ) );
399 aArgInput[nPos].SetEdFocusHdl ( LINK( this, ParaWin, GetEdFocusHdl ) );
400 aArgInput[nPos].SetEdModifyHdl ( LINK( this, ParaWin, ModifyHdl ) );
403 void ParaWin::ClearAll()
405 SetFunctionDesc(NULL);
406 SetArgumentOffset(0);
409 void ParaWin::SetArgumentOffset(sal_uInt16 nOffset)
411 DelParaArray();
412 aSlider.SetThumbPos(0);
414 aParaArray.resize(nArgs);
416 if ( nArgs > 0 )
418 for ( int i=0; i<4 && i<nArgs; i++ )
420 String aString;
421 aArgInput[i].SetArgVal(aString);
422 aArgInput[i].GetArgEdPtr()->Init(
423 (i==0) ? (ArgEdit *)NULL : aArgInput[i-1].GetArgEdPtr(),
424 (i==3 || i==nArgs-1) ? (ArgEdit *)NULL : aArgInput[i+1].GetArgEdPtr(),
425 aSlider, nArgs );
429 if ( nArgs < 5 )
431 aSlider.Hide();
433 else
435 aSlider.SetPageSize( 4 );
436 aSlider.SetVisibleSize( 4 );
437 aSlider.SetLineSize( 1 );
438 aSlider.SetRange( Range( 0, nArgs ) );
439 aSlider.SetThumbPos( nOffset );
440 aSlider.Show();
443 UpdateParas();
446 void ParaWin::UpdateParas()
448 sal_uInt16 i;
449 sal_uInt16 nOffset = GetSliderPos();
451 if ( nArgs > 0 )
453 for ( i=0; (i<nArgs) && (i<4); i++ )
455 UpdateArgInput( nOffset, i );
456 ShowParaLine(i);
460 for ( i=nArgs; i<4; i++ ) HideParaLine(i);
464 sal_uInt16 ParaWin::GetSliderPos()
466 return (sal_uInt16) aSlider.GetThumbPos();
469 void ParaWin::SetSliderPos(sal_uInt16 nSliderPos)
471 sal_uInt16 nOffset = GetSliderPos();
473 if(aSlider.IsVisible() && nOffset!=nSliderPos)
475 aSlider.SetThumbPos(nSliderPos);
476 for ( sal_uInt16 i=0; i<4; i++ )
478 UpdateArgInput( nSliderPos, i );
483 void ParaWin::SliderMoved()
485 sal_uInt16 nOffset = GetSliderPos();
487 for ( sal_uInt16 i=0; i<4; i++ )
489 UpdateArgInput( nOffset, i );
491 if(nEdFocus!=NOT_FOUND)
493 UpdateArgDesc( nEdFocus );
494 aArgInput[nEdFocus].SetArgSelection(Selection(0,SELECTION_MAX ));
495 nActiveLine=nEdFocus+nOffset;
496 ArgumentModified();
498 aScrollLink.Call(this);
501 void ParaWin::ArgumentModified()
503 aArgModifiedLink.Call(this);
506 void ParaWin::FxClick()
508 aFxLink.Call(this);
512 IMPL_LINK( ParaWin, GetFxHdl, ArgInput*, pPtr )
514 sal_uInt16 nOffset = GetSliderPos();
515 nEdFocus=NOT_FOUND;
516 for ( sal_uInt16 nPos=0; nPos<5;nPos++)
518 if(pPtr == &aArgInput[nPos])
520 nEdFocus=nPos;
521 break;
525 if(nEdFocus!=NOT_FOUND)
527 aArgInput[nEdFocus].SetArgSelection(Selection(0,SELECTION_MAX ));
528 nActiveLine=nEdFocus+nOffset;
529 FxClick();
531 return 0;
534 IMPL_LINK( ParaWin, GetFxFocusHdl, ArgInput*, pPtr )
536 sal_uInt16 nOffset = GetSliderPos();
537 nEdFocus=NOT_FOUND;
538 for ( sal_uInt16 nPos=0; nPos<5;nPos++)
540 if(pPtr == &aArgInput[nPos])
542 nEdFocus=nPos;
543 break;
547 if(nEdFocus!=NOT_FOUND)
549 aArgInput[nEdFocus].SetArgSelection(Selection(0,SELECTION_MAX ));
550 UpdateArgDesc( nEdFocus );
551 nActiveLine=nEdFocus+nOffset;
553 return 0;
558 IMPL_LINK( ParaWin, GetEdFocusHdl, ArgInput*, pPtr )
560 sal_uInt16 nOffset = GetSliderPos();
561 nEdFocus=NOT_FOUND;
562 for ( sal_uInt16 nPos=0; nPos<5;nPos++)
564 if(pPtr == &aArgInput[nPos])
566 nEdFocus=nPos;
567 break;
571 if(nEdFocus!=NOT_FOUND)
573 aArgInput[nEdFocus].SetArgSelection(Selection(0,SELECTION_MAX ));
574 UpdateArgDesc( nEdFocus );
575 nActiveLine=nEdFocus+nOffset;
576 ArgumentModified();
579 return 0;
583 IMPL_LINK_NOARG(ParaWin, ScrollHdl)
585 SliderMoved();
587 return 0;
590 IMPL_LINK( ParaWin, ModifyHdl, ArgInput*, pPtr )
592 sal_uInt16 nOffset = GetSliderPos();
593 nEdFocus=NOT_FOUND;
594 for ( sal_uInt16 nPos=0; nPos<5;nPos++)
596 if(pPtr == &aArgInput[nPos])
598 nEdFocus=nPos;
599 break;
602 if(nEdFocus!=NOT_FOUND)
604 aParaArray[nEdFocus+nOffset] = aArgInput[nEdFocus].GetArgVal();
605 UpdateArgDesc( nEdFocus);
606 nActiveLine=nEdFocus+nOffset;
609 ArgumentModified();
610 return 0;
615 } // formula
617 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */