xref: /AOO41X/main/formula/source/ui/dlg/parawin.cxx (revision ff0525f24f03981d56b7579b645949f111420994)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_formula.hxx"
26 
27 
28 
29 //----------------------------------------------------------------------------
30 
31 #include <svl/zforlist.hxx>
32 #include <svl/stritem.hxx>
33 
34 #include "parawin.hxx"
35 #include "parawin.hrc"
36 #include "helpids.hrc"
37 #include "formula/formdata.hxx"
38 #include "formula/IFunctionDescription.hxx"
39 #include "ModuleHelper.hxx"
40 #include "ForResId.hrc"
41 
42 #define VAR_ARGS 30
43 #define PAIRED_VAR_ARGS (VAR_ARGS + VAR_ARGS)
44 namespace formula
45 {
46 //============================================================================
47 
48 ParaWin::ParaWin(Window* pParent,IControlReferenceHandler* _pDlg,Point aPos):
49     TabPage         (pParent,ModuleRes(RID_FORMULATAB_PARAMETER)),
50     pFuncDesc       ( NULL ),
51     pMyParent       (_pDlg),
52     aFtEditDesc     ( this, ModuleRes( FT_EDITDESC ) ),
53     aFtArgName      ( this, ModuleRes( FT_PARNAME ) ),
54     aFtArgDesc      ( this, ModuleRes( FT_PARDESC ) ),
55 
56     aBtnFx1         ( this, ModuleRes( BTN_FX1 ) ),
57     aFtArg1         ( this, ModuleRes( FT_ARG1 ) ),
58     aEdArg1         ( this, ModuleRes( ED_ARG1 ) ),
59     aRefBtn1        ( this, ModuleRes( RB_ARG1 ) ),
60 
61     aBtnFx2         ( this, ModuleRes( BTN_FX2 ) ),
62     aFtArg2         ( this, ModuleRes( FT_ARG2 ) ),
63     aEdArg2         ( this, ModuleRes( ED_ARG2 ) ),
64     aRefBtn2        ( this, ModuleRes( RB_ARG2 ) ),
65 
66     aBtnFx3         ( this, ModuleRes( BTN_FX3 ) ),
67     aFtArg3         ( this, ModuleRes( FT_ARG3 ) ),
68     aEdArg3         ( this, ModuleRes( ED_ARG3 ) ),
69     aRefBtn3        ( this, ModuleRes( RB_ARG3 ) ),
70 
71     aBtnFx4         ( this, ModuleRes( BTN_FX4 ) ),
72     aFtArg4         ( this, ModuleRes( FT_ARG4 ) ),
73     aEdArg4         ( this, ModuleRes( ED_ARG4 ) ),
74     aRefBtn4        ( this, ModuleRes( RB_ARG4 ) ),
75 
76     aSlider         ( this, ModuleRes( WND_SLIDER ) ),
77     m_sOptional     ( ModuleRes( STR_OPTIONAL ) ),
78     m_sRequired     ( ModuleRes( STR_REQUIRED ) ),
79     bRefMode        (sal_False)
80 {
81     Image aFxHC( ModuleRes( IMG_FX_H ) );
82     FreeResource();
83     aDefaultString=aFtEditDesc.GetText();
84 
85     SetPosPixel(aPos);
86     nEdFocus=NOT_FOUND;
87     nActiveLine=0;
88     Size aSize = aSlider.GetSizePixel();
89     aSize.Width() = GetSettings().GetStyleSettings().GetScrollBarSize();
90     aSlider.SetSizePixel( aSize );
91     aSlider.SetEndScrollHdl( LINK( this, ParaWin, ScrollHdl ) );
92     aSlider.SetScrollHdl( LINK( this, ParaWin, ScrollHdl ) );
93 
94     aBtnFx1.SetModeImage( aFxHC, BMP_COLOR_HIGHCONTRAST );
95     aBtnFx2.SetModeImage( aFxHC, BMP_COLOR_HIGHCONTRAST );
96     aBtnFx3.SetModeImage( aFxHC, BMP_COLOR_HIGHCONTRAST );
97     aBtnFx4.SetModeImage( aFxHC, BMP_COLOR_HIGHCONTRAST );
98 
99     InitArgInput( 0, aFtArg1, aBtnFx1, aEdArg1, aRefBtn1);
100     InitArgInput( 1, aFtArg2, aBtnFx2, aEdArg2, aRefBtn2);
101     InitArgInput( 2, aFtArg3, aBtnFx3, aEdArg3, aRefBtn3);
102     InitArgInput( 3, aFtArg4, aBtnFx4, aEdArg4, aRefBtn4);
103     ClearAll();
104 }
105 
106 void ParaWin::UpdateArgDesc( sal_uInt16 nArg )
107 {
108     if (nArg==NOT_FOUND) return;
109 
110     if ( nArgs > 4 )
111         nArg = sal::static_int_cast<sal_uInt16>( nArg + GetSliderPos() );
112         //@ nArg += (sal_uInt16)aSlider.GetThumbPos();
113 
114     if ( (nArgs > 0) && (nArg<nArgs) )
115     {
116         String  aArgDesc;
117         String  aArgName;
118 
119         SetArgumentDesc( String() );
120         SetArgumentText( String() );
121 
122         if ( nArgs < VAR_ARGS )
123         {
124             sal_uInt16 nRealArg = (aVisibleArgMapping.size() < nArg) ? aVisibleArgMapping[nArg] : nArg;
125             aArgDesc  = pFuncDesc->getParameterDescription(nRealArg);
126             aArgName  = pFuncDesc->getParameterName(nRealArg);
127             aArgName += ' ';
128             aArgName += (pFuncDesc->isParameterOptional(nRealArg)) ? m_sOptional : m_sRequired ;
129         }
130         else if ( nArgs < PAIRED_VAR_ARGS )
131         {
132             sal_uInt16 nFix = nArgs - VAR_ARGS;
133             sal_uInt16 nPos = ( nArg < nFix ? nArg : nFix );
134             sal_uInt16 nRealArg = (nPos < aVisibleArgMapping.size() ?
135                     aVisibleArgMapping[nPos] : aVisibleArgMapping.back());
136             aArgDesc  = pFuncDesc->getParameterDescription(nRealArg);
137             aArgName  = pFuncDesc->getParameterName(nRealArg);
138             if ( nArg >= nFix )
139                 aArgName += String::CreateFromInt32(nArg-nFix+1);
140             aArgName += ' ';
141 
142             aArgName += (nArg > nFix || pFuncDesc->isParameterOptional(nRealArg)) ? m_sOptional : m_sRequired ;
143         }
144         else
145         {
146             sal_uInt16 nFix = nArgs - PAIRED_VAR_ARGS;
147             sal_uInt16 nPos;
148             if ( nArg < nFix )
149                 nPos = nArg;
150             else
151                 nPos = nFix + ( (nArg-nFix) % 2);
152             sal_uInt16 nRealArg = (nPos < aVisibleArgMapping.size() ?
153                     aVisibleArgMapping[nPos] : aVisibleArgMapping.back());
154             aArgDesc  = pFuncDesc->getParameterDescription(nRealArg);
155             aArgName  = pFuncDesc->getParameterName(nRealArg);
156             if ( nArg >= nFix )
157                 aArgName += String::CreateFromInt32((nArg-nFix)/2 + 1);
158             aArgName += ' ';
159 
160             aArgName += (nArg > (nFix+1) || pFuncDesc->isParameterOptional(nRealArg)) ? m_sOptional : m_sRequired ;
161         }
162 
163         SetArgumentDesc(aArgDesc);
164         SetArgumentText(aArgName);
165     }
166 }
167 
168 void ParaWin::UpdateArgInput( sal_uInt16 nOffset, sal_uInt16 i )
169 {
170     sal_uInt16 nArg = nOffset + i;
171     if ( nArgs < VAR_ARGS)
172     {
173         if(nArg<nArgs)
174         {
175             sal_uInt16 nRealArg = aVisibleArgMapping[nArg];
176             SetArgNameFont  (i,(pFuncDesc->isParameterOptional(nRealArg))
177                                             ? aFntLight : aFntBold );
178             SetArgName      (i,pFuncDesc->getParameterName(nRealArg));
179         }
180     }
181     else if ( nArgs < PAIRED_VAR_ARGS)
182     {
183         sal_uInt16 nFix = nArgs - VAR_ARGS;
184         sal_uInt16 nPos = ( nArg < nFix ? nArg : nFix );
185         sal_uInt16 nRealArg = (nPos < aVisibleArgMapping.size() ?
186                 aVisibleArgMapping[nPos] : aVisibleArgMapping.back());
187         SetArgNameFont( i,
188                 (nArg > nFix || pFuncDesc->isParameterOptional(nRealArg)) ?
189                 aFntLight : aFntBold );
190         if ( nArg >= nFix )
191         {
192             String aArgName( pFuncDesc->getParameterName(nRealArg) );
193             aArgName += String::CreateFromInt32(nArg-nFix+1);
194             SetArgName( i, aArgName );
195         }
196         else
197             SetArgName( i, pFuncDesc->getParameterName(nRealArg) );
198     }
199     else
200     {
201         sal_uInt16 nFix = nArgs - PAIRED_VAR_ARGS;
202         sal_uInt16 nPos;
203             if ( nArg < nFix )
204                 nPos = nArg;
205             else
206                 nPos = nFix + ( (nArg-nFix) % 2);
207         sal_uInt16 nRealArg = (nPos < aVisibleArgMapping.size() ?
208                 aVisibleArgMapping[nPos] : aVisibleArgMapping.back());
209         SetArgNameFont( i,
210                 (nArg > (nFix+1) || pFuncDesc->isParameterOptional(nRealArg)) ?
211                 aFntLight : aFntBold );
212         if ( nArg >= nFix )
213         {
214             String aArgName( pFuncDesc->getParameterName(nRealArg) );
215             aArgName += String::CreateFromInt32((nArg-nFix)/2 + 1);
216             SetArgName( i, aArgName );
217         }
218         else
219             SetArgName( i, pFuncDesc->getParameterName(nRealArg) );
220     }
221     if(nArg<nArgs) SetArgVal(i,aParaArray[nArg]);
222     //@ aArgInput[i].SetArgVal( *(pArgArr[nOffset+i]) );
223 
224 }
225 
226 ParaWin::~ParaWin()
227 {
228     // #i66422# if the focus changes during destruction of the controls,
229     // don't call the focus handlers
230     Link aEmptyLink;
231     aBtnFx1.SetGetFocusHdl( aEmptyLink );
232     aBtnFx2.SetGetFocusHdl( aEmptyLink );
233     aBtnFx3.SetGetFocusHdl( aEmptyLink );
234     aBtnFx4.SetGetFocusHdl( aEmptyLink );
235 }
236 
237 sal_uInt16 ParaWin::GetActiveLine()
238 {
239     return nActiveLine;
240 }
241 
242 void ParaWin::SetActiveLine(sal_uInt16 no)
243 {
244     if(no<nArgs)
245     {
246         long nOffset = GetSliderPos();
247         nActiveLine=no;
248         long nNewEdPos=(long)nActiveLine-nOffset;
249         if(nNewEdPos<0 || nNewEdPos>3)
250         {
251             nOffset+=nNewEdPos;
252             SetSliderPos((sal_uInt16) nOffset);
253             nOffset=GetSliderPos();
254         }
255         nEdFocus=no-(sal_uInt16)nOffset;
256         UpdateArgDesc( nEdFocus );
257     }
258 }
259 
260 RefEdit* ParaWin::GetActiveEdit()
261 {
262     if(nArgs>0 && nEdFocus!=NOT_FOUND)
263     {
264         return aArgInput[nEdFocus].GetArgEdPtr();
265     }
266     else
267     {
268         return NULL;
269     }
270 }
271 
272 
273 String ParaWin::GetArgument(sal_uInt16 no)
274 {
275     String aStr;
276     if(no<aParaArray.size())
277     {
278         aStr=aParaArray[no];
279         if(no==nActiveLine && aStr.Len()==0)
280             aStr+=' ';
281     }
282     return aStr;
283 }
284 
285 String  ParaWin::GetActiveArgName()
286 {
287     String aStr;
288     if(nArgs>0 && nEdFocus!=NOT_FOUND)
289     {
290         aStr=aArgInput[nEdFocus].GetArgName();
291     }
292     return aStr;
293 }
294 
295 
296 void ParaWin::SetArgument(sal_uInt16 no, const String& aString)
297 {
298     if(no<aParaArray.size())
299     {
300         aParaArray[no] = aString;
301         aParaArray[no].EraseLeadingChars();
302     }
303 }
304 
305 void ParaWin::DelParaArray()
306 {
307     ::std::vector<String>().swap(aParaArray);
308 }
309 
310 void ParaWin::SetArgumentFonts(const Font&aBoldFont,const Font&aLightFont)
311 {
312     aFntBold=aBoldFont;
313     aFntLight=aLightFont;
314 }
315 
316 void ParaWin::SetFunctionDesc(const IFunctionDescription* pFDesc)
317 {
318     pFuncDesc=pFDesc;
319 
320     SetArgumentDesc( String() );
321     SetArgumentText( String() );
322     SetEditDesc( String() );
323     nArgs = 0;
324     if ( pFuncDesc!=NULL)
325     {
326         if ( pFuncDesc->getDescription().getLength() )
327         {
328             SetEditDesc(pFuncDesc->getDescription());
329         }
330         else
331         {
332             SetEditDesc(aDefaultString);
333         }
334         nArgs = pFuncDesc->getSuppressedArgumentCount();
335         pFuncDesc->fillVisibleArgumentMapping(aVisibleArgMapping);
336         aSlider.Hide();
337         rtl::OString sHelpId = pFuncDesc->getHelpId();
338         SetHelpId( sHelpId );
339         aEdArg1.SetHelpId( sHelpId );
340         aEdArg2.SetHelpId( sHelpId );
341         aEdArg3.SetHelpId( sHelpId );
342         aEdArg4.SetHelpId( sHelpId );
343 
344         //  Unique-IDs muessen gleich bleiben fuer Automatisierung
345         SetUniqueId( HID_FORMULA_FAP_PAGE );
346         aEdArg1.SetUniqueId( HID_FORMULA_FAP_EDIT1 );
347         aEdArg2.SetUniqueId( HID_FORMULA_FAP_EDIT2 );
348         aEdArg3.SetUniqueId( HID_FORMULA_FAP_EDIT3 );
349         aEdArg4.SetUniqueId( HID_FORMULA_FAP_EDIT4 );
350         SetActiveLine(0);
351     }
352     else
353     {
354         nActiveLine=0;
355     }
356 
357 }
358 
359 void ParaWin::SetArgumentText(const String& aText)
360 {
361     aFtArgName.SetText(aText);
362 }
363 
364 void ParaWin::SetArgumentDesc(const String& aText)
365 {
366     aFtArgDesc.SetText(aText);
367 }
368 
369 void ParaWin::SetEditDesc(const String& aText)
370 {
371     aFtEditDesc.SetText(aText);
372 }
373 
374 void ParaWin::SetArgName(sal_uInt16 no,const String& aText)
375 {
376     aArgInput[no].SetArgName(aText);
377 }
378 
379 void ParaWin::SetArgNameFont(sal_uInt16 no,const Font& aFont)
380 {
381     aArgInput[no].SetArgNameFont(aFont);
382 }
383 
384 void ParaWin::SetArgVal(sal_uInt16 no,const String& aText)
385 {
386     aArgInput[no].SetArgVal(aText);
387 }
388 
389 void ParaWin::HideParaLine(sal_uInt16 no)
390 {
391     aArgInput[no].Hide();
392 }
393 
394 void ParaWin::ShowParaLine(sal_uInt16 no)
395 {
396     aArgInput[no].Show();
397 }
398 
399 void ParaWin::SetEdFocus(sal_uInt16 no)
400 {
401     UpdateArgDesc(no);
402     if(no<4 && no<aParaArray.size())
403         aArgInput[no].GetArgEdPtr()->GrabFocus();
404 }
405 
406 
407 void ParaWin::InitArgInput( sal_uInt16 nPos, FixedText& rFtArg, ImageButton& rBtnFx,
408                         ArgEdit& rEdArg, RefButton& rRefBtn)
409 {
410 
411     rRefBtn.SetReferences(pMyParent,&rEdArg);
412     rEdArg.SetRefDialog(pMyParent);
413 
414     aArgInput[nPos].InitArgInput (&rFtArg,&rBtnFx,&rEdArg,&rRefBtn);
415 
416     aArgInput[nPos].Hide();
417 
418     aArgInput[nPos].SetFxClickHdl   ( LINK( this, ParaWin, GetFxHdl ) );
419     aArgInput[nPos].SetFxFocusHdl   ( LINK( this, ParaWin, GetFxFocusHdl ) );
420     aArgInput[nPos].SetEdFocusHdl   ( LINK( this, ParaWin, GetEdFocusHdl ) );
421     aArgInput[nPos].SetEdModifyHdl  ( LINK( this, ParaWin, ModifyHdl ) );
422 }
423 
424 void ParaWin::ClearAll()
425 {
426     SetFunctionDesc(NULL);
427     SetArgumentOffset(0);
428 }
429 
430 void ParaWin::SetArgumentOffset(sal_uInt16 nOffset)
431 {
432     DelParaArray();
433     aSlider.SetThumbPos(0);
434 
435     aParaArray.resize(nArgs);
436 
437     if ( nArgs > 0 )
438     {
439         for ( int i=0; i<4 && i<nArgs; i++ )
440         {
441             String aString;
442             aArgInput[i].SetArgVal(aString);
443             aArgInput[i].GetArgEdPtr()->Init(
444                 (i==0)               ? (ArgEdit *)NULL : aArgInput[i-1].GetArgEdPtr(),
445                 (i==3 || i==nArgs-1) ? (ArgEdit *)NULL : aArgInput[i+1].GetArgEdPtr(),
446                                        aSlider, nArgs );
447         }
448     }
449 
450     if ( nArgs < 5 )
451     {
452         aSlider.Hide();
453     }
454     else
455     {
456         //aSlider.SetEndScrollHdl( LINK( this, ScFormulaDlg, ScrollHdl ) );
457         aSlider.SetPageSize( 4 );
458         aSlider.SetVisibleSize( 4 );
459         aSlider.SetLineSize( 1 );
460         aSlider.SetRange( Range( 0, nArgs ) );
461         aSlider.SetThumbPos( nOffset );
462         aSlider.Show();
463     }
464 
465     UpdateParas();
466 }
467 
468 void ParaWin::UpdateParas()
469 {
470     sal_uInt16 i;
471     sal_uInt16 nOffset = GetSliderPos();
472 
473     if ( nArgs > 0 )
474     {
475         for ( i=0; (i<nArgs) && (i<4); i++ )
476         {
477             UpdateArgInput( nOffset, i );
478             ShowParaLine(i);
479         }
480     }
481 
482     for ( i=nArgs; i<4; i++ ) HideParaLine(i);
483 }
484 
485 
486 sal_uInt16 ParaWin::GetSliderPos()
487 {
488     return (sal_uInt16) aSlider.GetThumbPos();
489 }
490 
491 void ParaWin::SetSliderPos(sal_uInt16 nSliderPos)
492 {
493     sal_uInt16 nOffset = GetSliderPos();
494 
495     if(aSlider.IsVisible() && nOffset!=nSliderPos)
496     {
497         aSlider.SetThumbPos(nSliderPos);
498         for ( sal_uInt16 i=0; i<4; i++ )
499         {
500             UpdateArgInput( nSliderPos, i );
501         }
502     }
503 }
504 
505 void ParaWin::SliderMoved()
506 {
507     sal_uInt16 nOffset = GetSliderPos();
508 
509     for ( sal_uInt16 i=0; i<4; i++ )
510     {
511         UpdateArgInput( nOffset, i );
512     }
513     if(nEdFocus!=NOT_FOUND)
514     {
515         UpdateArgDesc( nEdFocus );
516         aArgInput[nEdFocus].SetArgSelection(Selection(0,SELECTION_MAX ));
517         nActiveLine=nEdFocus+nOffset;
518         ArgumentModified();
519     }
520     aScrollLink.Call(this);
521 }
522 
523 void ParaWin::ArgumentModified()
524 {
525     aArgModifiedLink.Call(this);
526 }
527 
528 void ParaWin::FxClick()
529 {
530     aFxLink.Call(this);
531 }
532 
533 
534 IMPL_LINK( ParaWin, GetFxHdl, ArgInput*, pPtr )
535 {
536     sal_uInt16 nOffset = GetSliderPos();
537     nEdFocus=NOT_FOUND;
538     for ( sal_uInt16 nPos=0; nPos<5;nPos++)
539     {
540         if(pPtr == &aArgInput[nPos])
541         {
542             nEdFocus=nPos;
543             break;
544         }
545     }
546 
547     if(nEdFocus!=NOT_FOUND)
548     {
549         aArgInput[nEdFocus].SetArgSelection(Selection(0,SELECTION_MAX ));
550         nActiveLine=nEdFocus+nOffset;
551         FxClick();
552     }
553     return 0;
554 }
555 
556 IMPL_LINK( ParaWin, GetFxFocusHdl, ArgInput*, pPtr )
557 {
558     sal_uInt16 nOffset = GetSliderPos();
559     nEdFocus=NOT_FOUND;
560     for ( sal_uInt16 nPos=0; nPos<5;nPos++)
561     {
562         if(pPtr == &aArgInput[nPos])
563         {
564             nEdFocus=nPos;
565             break;
566         }
567     }
568 
569     if(nEdFocus!=NOT_FOUND)
570     {
571         aArgInput[nEdFocus].SetArgSelection(Selection(0,SELECTION_MAX ));
572         UpdateArgDesc( nEdFocus );
573         nActiveLine=nEdFocus+nOffset;
574     }
575     return 0;
576 }
577 
578 
579 
580 IMPL_LINK( ParaWin, GetEdFocusHdl, ArgInput*, pPtr )
581 {
582     sal_uInt16 nOffset = GetSliderPos();
583     nEdFocus=NOT_FOUND;
584     for ( sal_uInt16 nPos=0; nPos<5;nPos++)
585     {
586         if(pPtr == &aArgInput[nPos])
587         {
588             nEdFocus=nPos;
589             break;
590         }
591     }
592 
593     if(nEdFocus!=NOT_FOUND)
594     {
595         aArgInput[nEdFocus].SetArgSelection(Selection(0,SELECTION_MAX ));
596         UpdateArgDesc( nEdFocus );
597         nActiveLine=nEdFocus+nOffset;
598         ArgumentModified();
599     }
600 
601     return 0;
602 }
603 
604 
605 IMPL_LINK( ParaWin, ScrollHdl, ScrollBar*, EMPTYARG )
606 {
607     SliderMoved();
608 
609     return 0;
610 }
611 
612 IMPL_LINK( ParaWin, ModifyHdl, ArgInput*, pPtr )
613 {
614     sal_uInt16 nOffset = GetSliderPos();
615     nEdFocus=NOT_FOUND;
616     for ( sal_uInt16 nPos=0; nPos<5;nPos++)
617     {
618         if(pPtr == &aArgInput[nPos])
619         {
620             nEdFocus=nPos;
621             break;
622         }
623     }
624     if(nEdFocus!=NOT_FOUND)
625     {
626         aParaArray[nEdFocus+nOffset] = aArgInput[nEdFocus].GetArgVal();
627         UpdateArgDesc( nEdFocus);
628         nActiveLine=nEdFocus+nOffset;
629     }
630 
631     ArgumentModified();
632     return 0;
633 }
634 
635 
636 
637 } // formula
638