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