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