xref: /trunk/main/sc/source/ui/dbgui/validate.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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_sc.hxx"
30 
31 #ifdef SC_DLLIMPLEMENTATION
32 #undef SC_DLLIMPLEMENTATION
33 #endif
34 
35 #include <vcl/svapp.hxx>
36 #include <svl/aeitem.hxx>
37 #include <svl/stritem.hxx>
38 #include <svl/eitem.hxx>
39 #include <svl/intitem.hxx>
40 #include <basic/sbmeth.hxx>
41 #include <basic/sbstar.hxx>
42 #include <basic/sbmod.hxx>
43 #include <sfx2/app.hxx>
44 
45 #include "scresid.hxx"
46 #include "sc.hrc"       // -> Slot IDs
47 
48 #include "validat.hxx"
49 #include "validate.hrc"
50 #include "validate.hxx"
51 #include "compiler.hxx"
52 #include "formula/opcode.hxx" //CHINA001
53 //<!--Added by PengYunQuan for Validity Cell Range Picker
54 #include "tabvwsh.hxx"
55 #include <sfx2/viewfrm.hxx>
56 #include <sfx2/childwin.hxx>
57 #include "reffact.hxx"
58 //-->Added by PengYunQuan for Validity Cell Range Picker
59 
60 // ============================================================================
61 
62 static sal_uInt16 pValueRanges[] =
63 {
64     FID_VALID_MODE, FID_VALID_ERRTEXT,
65     FID_VALID_LISTTYPE, FID_VALID_LISTTYPE,
66     0
67 };
68 
69 
70 // ============================================================================
71 
72 //<!--Modified by PengYunQuan for Validity Cell Range Picker
73 //ScValidationDlg::ScValidationDlg( Window* pParent, const SfxItemSet* pArgSet ) :
74 //    SfxTabDialog( pParent, ScResId( TAB_DLG_VALIDATION ), pArgSet )
75 ScValidationDlg::ScValidationDlg( Window*           pParent,
76                                  const SfxItemSet* pArgSet,
77                                  ScTabViewShell *pTabViewSh,
78                                  SfxBindings *pB /*= NULL*/
79                       ) :
80         ScValidationDlgBase( pParent ? pParent : SFX_APP()->GetTopWindow(), TAB_DLG_VALIDATION, pArgSet, pB ),
81             m_bOwnRefHdlr( false ),
82             m_pTabVwSh( pTabViewSh ),
83             m_bRefInputting( false )
84 //-->Modified by PengYunQuan for Validity Cell Range Picker
85 {
86     AddTabPage( TP_VALIDATION_VALUES,    ScTPValidationValue::Create, 0 );
87     AddTabPage( TP_VALIDATION_INPUTHELP, ScTPValidationHelp::Create,  0 );
88     AddTabPage( TP_VALIDATION_ERROR,     ScTPValidationError::Create, 0 );
89     FreeResource();
90 }
91 
92 //<!--Added by PengYunQuan for Validity Cell Range Picker
93 void ScTPValidationValue::SetReferenceHdl( const ScRange&rRange , ScDocument* pDoc )
94 {
95     if ( rRange.aStart != rRange.aEnd )
96         if ( ScValidationDlg *pValidationDlg = GetValidationDlg() )
97             if( m_pRefEdit )
98                 pValidationDlg/*->GetRefHandler()*/->RefInputStart( m_pRefEdit );
99 
100     if ( m_pRefEdit )
101     {
102         String  aStr;
103         rRange.Format( aStr, SCR_ABS_3D, pDoc );
104         m_pRefEdit->SetRefString( aStr );
105         //m_pRefEdit->SetRefString( rRange.aStart != rRange.aEnd ? aStr : String::CreateFromAscii("=").Append( aStr ) );
106     }
107 }
108 
109 void ScTPValidationValue:: SetActiveHdl()
110 {
111     if ( m_pRefEdit ) m_pRefEdit->GrabFocus();
112 
113     if ( ScValidationDlg *pValidationDlg = GetValidationDlg() )
114         if( m_pRefEdit )
115         {
116             pValidationDlg/*->GetRefHandler()*/->RefInputDone();
117         }
118 }
119 
120 void            ScTPValidationValue::RefInputStartPreHdl( ScRefEdit* pEdit, ScRefButton* pButton )
121 {
122     if ( ScValidationDlg *pValidationDlg = GetValidationDlg() )
123     {
124         if( pEdit == m_pRefEdit )
125         {
126             if( Window *pPreWnd = pEdit==&maEdMax?&maFtMax:(pEdit==&maEdMin?&maFtMin:NULL) )
127             {
128                 pPreWnd->SetParent( pValidationDlg );
129                 pPreWnd->Hide();
130             }
131 
132             m_pRefEdit->SetParent( pValidationDlg );
133         }
134 
135         if( pButton == &m_btnRef )m_btnRef.SetParent( pValidationDlg );
136     }
137 }
138 
139 void            ScTPValidationValue::RefInputDonePreHdl()
140 {
141 
142     if( m_pRefEdit && m_pRefEdit->GetParent()!= this )
143     {
144         if( Window *pPreWnd = m_pRefEdit==&maEdMax?&maFtMax:(m_pRefEdit==&maEdMin?&maFtMin:NULL) )
145         {
146             pPreWnd->SetParent( this );
147             pPreWnd->Show();
148         }
149 
150         m_pRefEdit->SetParent( this );
151 
152         m_btnRef.SetParent( m_pRefEdit ); //if Edit SetParent but button not, the tab order will be incorrect, need button to setparent to anthor window and restore parent later in order to restore the tab order
153 //      aExample1.SetParent( m_pRefEdit ); // the aExample1's child order will affect acc key
154     }
155 
156     if( m_btnRef.GetParent()!=this ) m_btnRef.SetParent( this );
157 //  if( aExample1.GetParent()!=this ) aExample1.SetParent( this );
158 }
159 
160 void            ScTPValidationValue::RefInputDonePostHdl()
161 {
162 
163 
164     if( m_pRefEdit && !m_pRefEdit->HasFocus() )
165         m_pRefEdit->GrabFocus();
166 
167 }
168 
169 
170 sal_Bool ScValidationDlg::Close()
171 {
172     if( m_bOwnRefHdlr )
173         if( SfxTabPage* pPage = GetTabPage( TP_VALIDATION_VALUES ) )
174             static_cast<ScTPValidationValue*>(pPage)->RemoveRefDlg();
175 
176     return ScValidationDlgBase::Close();
177 }
178 //-->Added by PengYunQuan for Validity Cell Range Picker
179 
180 ScValidationDlg::~ScValidationDlg()
181 {
182     //<!--Added by PengYunQuan for Validity Cell Range Picker
183     if( m_bOwnRefHdlr )
184         RemoveRefDlg( sal_False );
185     //-->Added by PengYunQuan for Validity Cell Range Picker
186 }
187 
188 
189 // ============================================================================
190 
191 namespace {
192 
193 /** Converts the passed ScValidationMode to the position in the list box. */
194 sal_uInt16 lclGetPosFromValMode( ScValidationMode eValMode )
195 {
196     sal_uInt16 nLbPos = SC_VALIDDLG_ALLOW_ANY;
197     switch( eValMode )
198     {
199         case SC_VALID_ANY:      nLbPos = SC_VALIDDLG_ALLOW_ANY;     break;
200         case SC_VALID_WHOLE:    nLbPos = SC_VALIDDLG_ALLOW_WHOLE;   break;
201         case SC_VALID_DECIMAL:  nLbPos = SC_VALIDDLG_ALLOW_DECIMAL; break;
202         case SC_VALID_DATE:     nLbPos = SC_VALIDDLG_ALLOW_DATE;    break;
203         case SC_VALID_TIME:     nLbPos = SC_VALIDDLG_ALLOW_TIME;    break;
204         case SC_VALID_TEXTLEN:  nLbPos = SC_VALIDDLG_ALLOW_TEXTLEN; break;
205         case SC_VALID_LIST:     nLbPos = SC_VALIDDLG_ALLOW_RANGE;   break;
206         case SC_VALID_CUSTOM:   nLbPos = SC_VALIDDLG_ALLOW_ANY;     break;  // not supported
207         default:    DBG_ERRORFILE( "lclGetPosFromValMode - unknown validity mode" );
208     }
209     return nLbPos;
210 }
211 
212 /** Converts the passed list box position to an ScValidationMode. */
213 ScValidationMode lclGetValModeFromPos( sal_uInt16 nLbPos )
214 {
215     ScValidationMode eValMode = SC_VALID_ANY;
216     switch( nLbPos )
217     {
218         case SC_VALIDDLG_ALLOW_ANY:     eValMode = SC_VALID_ANY;        break;
219         case SC_VALIDDLG_ALLOW_WHOLE:   eValMode = SC_VALID_WHOLE;      break;
220         case SC_VALIDDLG_ALLOW_DECIMAL: eValMode = SC_VALID_DECIMAL;    break;
221         case SC_VALIDDLG_ALLOW_DATE:    eValMode = SC_VALID_DATE;       break;
222         case SC_VALIDDLG_ALLOW_TIME:    eValMode = SC_VALID_TIME;       break;
223         case SC_VALIDDLG_ALLOW_RANGE:   eValMode = SC_VALID_LIST;       break;
224         case SC_VALIDDLG_ALLOW_LIST:    eValMode = SC_VALID_LIST;       break;
225         case SC_VALIDDLG_ALLOW_TEXTLEN: eValMode = SC_VALID_TEXTLEN;    break;
226         default:    DBG_ERRORFILE( "lclGetValModeFromPos - invalid list box position" );
227     }
228     return eValMode;
229 }
230 
231 /** Converts the passed ScConditionMode to the position in the list box. */
232 sal_uInt16 lclGetPosFromCondMode( ScConditionMode eCondMode )
233 {
234     sal_uInt16 nLbPos = SC_VALIDDLG_DATA_EQUAL;
235     switch( eCondMode )
236     {
237         case SC_COND_NONE:          // #111771# may occur in old XML files after Excel import
238         case SC_COND_EQUAL:         nLbPos = SC_VALIDDLG_DATA_EQUAL;        break;
239         case SC_COND_LESS:          nLbPos = SC_VALIDDLG_DATA_LESS;         break;
240         case SC_COND_GREATER:       nLbPos = SC_VALIDDLG_DATA_GREATER;      break;
241         case SC_COND_EQLESS:        nLbPos = SC_VALIDDLG_DATA_EQLESS;       break;
242         case SC_COND_EQGREATER:     nLbPos = SC_VALIDDLG_DATA_EQGREATER;    break;
243         case SC_COND_NOTEQUAL:      nLbPos = SC_VALIDDLG_DATA_NOTEQUAL;     break;
244         case SC_COND_BETWEEN:       nLbPos = SC_VALIDDLG_DATA_BETWEEN;      break;
245         case SC_COND_NOTBETWEEN:    nLbPos = SC_VALIDDLG_DATA_NOTBETWEEN;   break;
246         default:    DBG_ERRORFILE( "lclGetPosFromCondMode - unknown condition mode" );
247     }
248     return nLbPos;
249 }
250 
251 /** Converts the passed list box position to an ScConditionMode. */
252 ScConditionMode lclGetCondModeFromPos( sal_uInt16 nLbPos )
253 {
254     ScConditionMode eCondMode = SC_COND_EQUAL;
255     switch( nLbPos )
256     {
257         case SC_VALIDDLG_DATA_EQUAL:        eCondMode = SC_COND_EQUAL;      break;
258         case SC_VALIDDLG_DATA_LESS:         eCondMode = SC_COND_LESS;       break;
259         case SC_VALIDDLG_DATA_GREATER:      eCondMode = SC_COND_GREATER;    break;
260         case SC_VALIDDLG_DATA_EQLESS:       eCondMode = SC_COND_EQLESS;     break;
261         case SC_VALIDDLG_DATA_EQGREATER:    eCondMode = SC_COND_EQGREATER;  break;
262         case SC_VALIDDLG_DATA_NOTEQUAL:     eCondMode = SC_COND_NOTEQUAL;   break;
263         case SC_VALIDDLG_DATA_BETWEEN:      eCondMode = SC_COND_BETWEEN;    break;
264         case SC_VALIDDLG_DATA_NOTBETWEEN:   eCondMode = SC_COND_NOTBETWEEN; break;
265         default:    DBG_ERRORFILE( "lclGetCondModeFromPos - invalid list box position" );
266     }
267     return eCondMode;
268 }
269 
270 /** Converts line feed separated string to a formula with strings separated by semicolons.
271     @descr  Keeps all empty strings.
272     Example: abc\ndef\n\nghi -> "abc";"def";"";"ghi".
273     @param rFmlaStr  (out-param) The converted formula string. */
274 void lclGetFormulaFromStringList( String& rFmlaStr, const String& rStringList, sal_Unicode cFmlaSep )
275 {
276     rFmlaStr.Erase();
277     xub_StrLen nTokenCnt = rStringList.GetTokenCount( '\n' );
278     for( xub_StrLen nToken = 0, nStringIx = 0; nToken < nTokenCnt; ++nToken )
279     {
280         String aToken( rStringList.GetToken( 0, '\n', nStringIx ) );
281         ScGlobal::AddQuotes( aToken, '"' );
282         ScGlobal::AddToken( rFmlaStr, aToken, cFmlaSep );
283     }
284     if( !rFmlaStr.Len() )
285         rFmlaStr.AssignAscii( "\"\"" );
286 }
287 
288 
289 /** Converts formula with strings separated by semicolons to line feed separated string.
290     @descr  Keeps all empty strings. Ignores all empty tokens (multiple semicolons).
291     Example: "abc";;;"def";"";"ghi" -> abc\ndef\n\nghi.
292     @param rStringList  (out-param) The converted line feed separated string list.
293     @return  true = Conversion successful. */
294 bool lclGetStringListFromFormula( String& rStringList, const String& rFmlaStr, sal_Unicode cFmlaSep )
295 {
296     String aQuotes( RTL_CONSTASCII_USTRINGPARAM( "\"\"" ) );
297     xub_StrLen nTokenCnt = rFmlaStr.GetQuotedTokenCount( aQuotes, cFmlaSep );
298 
299     rStringList.Erase();
300     bool bIsStringList = (nTokenCnt > 0);
301     bool bTokenAdded = false;
302 
303     for( xub_StrLen nToken = 0, nStringIx = 0; bIsStringList && (nToken < nTokenCnt); ++nToken )
304     {
305         String aToken( rFmlaStr.GetQuotedToken( 0, aQuotes, cFmlaSep, nStringIx ) );
306         aToken.EraseLeadingAndTrailingChars();
307         if( aToken.Len() )      // ignore empty tokens, i.e. "a";;"b"
308         {
309             bIsStringList = ScGlobal::IsQuoted( aToken, '"' );
310             if( bIsStringList )
311             {
312                 ScGlobal::EraseQuotes( aToken, '"' );
313                 ScGlobal::AddToken( rStringList, aToken, '\n', 1, bTokenAdded );
314                 bTokenAdded = true;
315             }
316         }
317     }
318 
319     return bIsStringList;
320 }
321 
322 } // namespace
323 
324 // ----------------------------------------------------------------------------
325 
326 ScTPValidationValue::ScTPValidationValue( Window* pParent, const SfxItemSet& rArgSet ) :
327     SfxTabPage( pParent, ScResId( TP_VALIDATION_VALUES ), rArgSet ),
328     maFtAllow ( this, ScResId( FT_ALLOW ) ),
329     maLbAllow ( this, ScResId( LB_ALLOW ) ),
330     maCbAllow ( this, ScResId( TSB_ALLOW_BLANKS ) ),
331     maCbShow  ( this, ScResId( CB_SHOWLIST ) ),
332     maCbSort  ( this, ScResId( CB_SORTLIST ) ),
333     maFtValue ( this, ScResId( FT_VALUE ) ),
334     maLbValue ( this, ScResId( LB_VALUE ) ),
335     maFtMin   ( this, ScResId( FT_MIN ) ),
336     maEdMin   ( this, ScResId( EDT_MIN ) ),
337     maEdList  ( this, ScResId( EDT_LIST ) ),
338     maFtMax   ( this, ScResId( FT_MAX ) ),
339     maEdMax   ( this, ScResId( EDT_MAX ) ),
340     maFtHint  ( this, ScResId( FT_SOURCEHINT ) ),
341     maStrMin  ( ScResId( SCSTR_VALID_MINIMUM ) ),
342     maStrMax  ( ScResId( SCSTR_VALID_MAXIMUM ) ),
343     maStrValue( ScResId( SCSTR_VALID_VALUE ) ),
344     maStrRange( ScResId( SCSTR_VALID_RANGE ) ),
345     maStrList ( ScResId( SCSTR_VALID_LIST ) ),
346 //<!--Added by PengYunQuan for Validity Cell Range Picker
347         m_btnRef( this, ScResId( RB_VALIDITY_REF ) )
348 //-->Added by PengYunQuan for Validity Cell Range Picker
349 {
350     Init();
351     FreeResource();
352 
353     // list separator in formulas
354     //CHINA001 const String& rListSep = ScCompiler::pSymbolTableNative[ ocSep ];
355     String aListSep = ::GetScCompilerNativeSymbol( ocSep ); //CHINA001
356     DBG_ASSERT( aListSep.Len() == 1, "ScTPValidationValue::ScTPValidationValue - list separator error" );
357     mcFmlaSep = aListSep.Len() ? aListSep.GetChar( 0 ) : ';';
358     //<!--Added by PengYunQuan for Validity Cell Range Picker
359     m_btnRef.Hide();
360     //-->Added by PengYunQuan for Validity Cell Range Picker
361 }
362 
363 ScTPValidationValue::~ScTPValidationValue()
364 {
365 }
366 
367 void ScTPValidationValue::Init()
368 {
369     maLbAllow.SetSelectHdl( LINK( this, ScTPValidationValue, SelectHdl ) );
370     maLbValue.SetSelectHdl( LINK( this, ScTPValidationValue, SelectHdl ) );
371     maCbShow.SetClickHdl( LINK( this, ScTPValidationValue, CheckHdl ) );
372     //<!--Added by PengYunQuan for Validity Cell Range Picker
373     maEdMin.SetGetFocusHdl( LINK( this, ScTPValidationValue, EditSetFocusHdl ) );
374     maEdMin.SetLoseFocusHdl( LINK( this, ScTPValidationValue, KillFocusHdl ) );
375     maEdMax.SetGetFocusHdl( LINK( this, ScTPValidationValue, EditSetFocusHdl ) );
376     m_btnRef.SetLoseFocusHdl( LINK( this, ScTPValidationValue, KillFocusHdl ) );
377     maEdMax.SetLoseFocusHdl( LINK( this, ScTPValidationValue, KillFocusHdl ) );
378     //-->Added by PengYunQuan for Validity Cell Range Picker
379 
380     maLbAllow.SelectEntryPos( SC_VALIDDLG_ALLOW_ANY );
381     maLbValue.SelectEntryPos( SC_VALIDDLG_DATA_EQUAL );
382 
383     SelectHdl( NULL );
384     CheckHdl( NULL );
385 }
386 
387 SfxTabPage* ScTPValidationValue::Create( Window* pParent, const SfxItemSet& rArgSet )
388 {
389     return( new ScTPValidationValue( pParent, rArgSet ) );
390 }
391 
392 sal_uInt16* ScTPValidationValue::GetRanges()
393 {
394     return pValueRanges;
395 }
396 
397 void ScTPValidationValue::Reset( const SfxItemSet& rArgSet )
398 {
399     const SfxPoolItem* pItem;
400 
401     sal_uInt16 nLbPos = SC_VALIDDLG_ALLOW_ANY;
402     if( rArgSet.GetItemState( FID_VALID_MODE, sal_True, &pItem ) == SFX_ITEM_SET )
403         nLbPos = lclGetPosFromValMode( static_cast< ScValidationMode >(
404             static_cast< const SfxAllEnumItem* >( pItem )->GetValue() ) );
405     maLbAllow.SelectEntryPos( nLbPos );
406 
407     nLbPos = SC_VALIDDLG_DATA_EQUAL;
408     if( rArgSet.GetItemState( FID_VALID_CONDMODE, sal_True, &pItem ) == SFX_ITEM_SET )
409         nLbPos = lclGetPosFromCondMode( static_cast< ScConditionMode >(
410             static_cast< const SfxAllEnumItem* >( pItem )->GetValue() ) );
411     maLbValue.SelectEntryPos( nLbPos );
412 
413     // *** check boxes ***
414     sal_Bool bCheck = sal_True;
415     if( rArgSet.GetItemState( FID_VALID_BLANK, sal_True, &pItem ) == SFX_ITEM_SET )
416         bCheck = static_cast< const SfxBoolItem* >( pItem )->GetValue();
417     maCbAllow.Check( bCheck );
418 
419     sal_Int32 nListType = ValidListType::UNSORTED;
420     if( rArgSet.GetItemState( FID_VALID_LISTTYPE, sal_True, &pItem ) == SFX_ITEM_SET )
421         nListType = static_cast< const SfxInt16Item* >( pItem )->GetValue();
422     maCbShow.Check( nListType != ValidListType::INVISIBLE );
423     maCbSort.Check( nListType == ValidListType::SORTEDASCENDING );
424 
425     // *** formulas ***
426     String aFmlaStr;
427     if ( rArgSet.GetItemState( FID_VALID_VALUE1, sal_True, &pItem ) == SFX_ITEM_SET )
428         aFmlaStr = static_cast< const SfxStringItem* >( pItem )->GetValue();
429     SetFirstFormula( aFmlaStr );
430 
431     aFmlaStr.Erase();
432     if ( rArgSet.GetItemState( FID_VALID_VALUE2, sal_True, &pItem ) == SFX_ITEM_SET )
433         aFmlaStr = static_cast< const SfxStringItem* >( pItem )->GetValue();
434     SetSecondFormula( aFmlaStr );
435 
436     SelectHdl( NULL );
437     CheckHdl( NULL );
438 }
439 
440 sal_Bool ScTPValidationValue::FillItemSet( SfxItemSet& rArgSet )
441 {
442     sal_Int16 nListType = maCbShow.IsChecked() ?
443         (maCbSort.IsChecked() ? ValidListType::SORTEDASCENDING : ValidListType::UNSORTED) :
444         ValidListType::INVISIBLE;
445 
446     rArgSet.Put( SfxAllEnumItem( FID_VALID_MODE, sal::static_int_cast<sal_uInt16>(
447                     lclGetValModeFromPos( maLbAllow.GetSelectEntryPos() ) ) ) );
448     rArgSet.Put( SfxAllEnumItem( FID_VALID_CONDMODE, sal::static_int_cast<sal_uInt16>(
449                     lclGetCondModeFromPos( maLbValue.GetSelectEntryPos() ) ) ) );
450     rArgSet.Put( SfxStringItem( FID_VALID_VALUE1, GetFirstFormula() ) );
451     rArgSet.Put( SfxStringItem( FID_VALID_VALUE2, GetSecondFormula() ) );
452     rArgSet.Put( SfxBoolItem( FID_VALID_BLANK, maCbAllow.IsChecked() ) );
453     rArgSet.Put( SfxInt16Item( FID_VALID_LISTTYPE, nListType ) );
454     return sal_True;
455 }
456 
457 String ScTPValidationValue::GetFirstFormula() const
458 {
459     String aFmlaStr;
460     if( maLbAllow.GetSelectEntryPos() == SC_VALIDDLG_ALLOW_LIST )
461         lclGetFormulaFromStringList( aFmlaStr, maEdList.GetText(), mcFmlaSep );
462     else
463         aFmlaStr = maEdMin.GetText();
464     return aFmlaStr;
465 }
466 
467 String ScTPValidationValue::GetSecondFormula() const
468 {
469     return maEdMax.GetText();
470 }
471 
472 void ScTPValidationValue::SetFirstFormula( const String& rFmlaStr )
473 {
474     // try if formula is a string list, validation mode must already be set
475     String aStringList;
476     if( (maLbAllow.GetSelectEntryPos() == SC_VALIDDLG_ALLOW_RANGE) &&
477         lclGetStringListFromFormula( aStringList, rFmlaStr, mcFmlaSep ) )
478     {
479         maEdList.SetText( aStringList );
480         maEdMin.SetText( EMPTY_STRING );
481         // change validation mode to string list
482         maLbAllow.SelectEntryPos( SC_VALIDDLG_ALLOW_LIST );
483     }
484     else
485     {
486         maEdMin.SetText( rFmlaStr );
487         maEdList.SetText( EMPTY_STRING );
488     }
489 }
490 
491 void ScTPValidationValue::SetSecondFormula( const String& rFmlaStr )
492 {
493     maEdMax.SetText( rFmlaStr );
494 }
495 
496 //<!--Added by PengYunQuan for Validity Cell Range Picker
497 ScValidationDlg * ScTPValidationValue::GetValidationDlg()
498 {
499     if( Window *pParent = GetParent() )
500         do{
501             if ( dynamic_cast<ScValidationDlg*>( pParent ) )
502                 return static_cast< ScValidationDlg * >( pParent );
503         }while ( NULL != ( pParent = pParent->GetParent() ) );
504     return NULL;
505 }
506 void ScTPValidationValue::SetupRefDlg()
507 {
508     if( ScValidationDlg *pValidationDlg = GetValidationDlg() )
509         if( pValidationDlg->SetupRefDlg() )
510         {
511             pValidationDlg->SetHandler( this );
512             pValidationDlg->SetSetRefHdl( (ScRefHandlerHelper::PFUNCSETREFHDLTYPE)( &ScTPValidationValue::SetReferenceHdl ) );
513             pValidationDlg->SetSetActHdl( (ScRefHandlerHelper::PCOMMONHDLTYPE)( &ScTPValidationValue::SetActiveHdl ) );
514             pValidationDlg->SetRefInputStartPreHdl( (ScRefHandlerHelper::PINPUTSTARTDLTYPE)( &ScTPValidationValue::RefInputStartPreHdl ) );
515             pValidationDlg->SetRefInputDonePreHdl( (ScRefHandlerHelper::PCOMMONHDLTYPE)( &ScTPValidationValue::RefInputDonePreHdl ) );
516             pValidationDlg->SetRefInputDonePostHdl( (ScRefHandlerHelper::PCOMMONHDLTYPE)( &ScTPValidationValue::RefInputDonePostHdl ) );
517 
518             if ( maEdMax.IsVisible() ) { m_pRefEdit = &maEdMax; }
519             else if ( maEdMin.IsVisible() ) {  m_pRefEdit = &maEdMin; }
520 
521             if( m_pRefEdit && !m_pRefEdit->HasFocus() ) m_pRefEdit->GrabFocus();
522 
523             if( m_pRefEdit ) m_pRefEdit->SetRefDialog( pValidationDlg );
524             m_btnRef.SetReferences( pValidationDlg, m_pRefEdit );
525         }
526 }
527 
528 void ScTPValidationValue::RemoveRefDlg()
529 {
530     if( ScValidationDlg *pValidationDlg = GetValidationDlg() )
531         if( pValidationDlg->RemoveRefDlg() )
532         {
533             pValidationDlg->SetHandler( NULL );
534             pValidationDlg->SetSetRefHdl( NULL );
535             pValidationDlg->SetSetActHdl( NULL );
536             pValidationDlg->SetRefInputStartPreHdl( NULL );
537             pValidationDlg->SetRefInputDonePreHdl( NULL );
538             pValidationDlg->SetRefInputDonePostHdl( NULL );
539 
540             if( m_pRefEdit ) m_pRefEdit->SetRefDialog( NULL );
541             m_pRefEdit = NULL;
542 
543             m_btnRef.SetReferences( NULL, NULL );
544 
545 #if ! defined( WNT ) && !defined( _MSC_VER )
546             TidyListBoxes();
547 #endif
548         }
549 }
550 
551 void ScTPValidationValue::TidyListBoxes()
552 {
553     if ( Window *pWnd = GetChild( 0 ) )
554     {
555         bool bFindLst = false;
556         std::list<Window*> alstOrder;
557 
558         do{
559             if( pWnd->GetParent() == this )
560             {
561                 if ( !bFindLst )
562                 {
563                     try{
564                         if( dynamic_cast<ListBox*>(pWnd)||dynamic_cast<ListBox*>(pWnd->GetWindow(WINDOW_CLIENT) ) )
565                             bFindLst = true;
566                     }
567                     catch( ... )
568                     {
569                         if ( *(void**)pWnd == *(void**)&maLbValue )
570                             bFindLst = true;
571                         else if ( Window *pClient = pWnd->GetWindow( WINDOW_CLIENT ) )
572                             if ( *(void**)pClient == *(void**)&maLbValue )
573                                 bFindLst = true;
574                     }
575                 }
576 
577                 if ( bFindLst )
578                     alstOrder.push_back( pWnd->GetWindow( WINDOW_CLIENT ) );
579             }
580         }while( NULL != ( pWnd = pWnd->GetWindow( WINDOW_NEXT  ) ) );
581 
582         pWnd = GetChild(0);
583 
584         while( std::find( alstOrder.begin(), alstOrder.end(), pWnd ) != alstOrder.end() && NULL != ( pWnd = pWnd->GetWindow( WINDOW_NEXT) ) ) ;
585 
586         if ( pWnd )
587         {
588             for ( std::list<Window*>::iterator i = alstOrder.begin(); i!=alstOrder.end(); i++ )
589             {
590                 Window *pParent = (*i)->GetParent();
591                 (*i)->SetParent( pWnd );
592                 (*i)->SetParent( pParent );
593             }
594         }
595     }
596 }
597 
598 IMPL_LINK( ScTPValidationValue, EditSetFocusHdl, Edit *, /*pEdit*/ )
599 {
600     sal_uInt16  nPos=maLbAllow.GetSelectEntryPos();
601 
602     if ( nPos == SC_VALIDDLG_ALLOW_RANGE )
603     {
604         SetupRefDlg();
605     }
606 
607     return 0;
608 }
609 
610 IMPL_LINK( ScTPValidationValue, KillFocusHdl, Window *, pWnd )
611 {
612     if( pWnd == m_pRefEdit || pWnd == &m_btnRef )
613         if( ScValidationDlg *pValidationDlg = GetValidationDlg() )
614             if ( (pValidationDlg->IsActive() || pValidationDlg->IsChildFocus() ) && !pValidationDlg->IsRefInputting() )
615                 if( ( !m_pRefEdit || !m_pRefEdit->HasFocus()) && !m_btnRef.HasFocus() )
616                 {
617                     RemoveRefDlg();
618                 }
619 
620     return 0;
621 }
622 //-->Added by PengYunQuan for Validity Cell Range Picker
623 
624 // ----------------------------------------------------------------------------
625 
626 IMPL_LINK( ScTPValidationValue, SelectHdl, ListBox*, EMPTYARG )
627 {
628     sal_uInt16 nLbPos = maLbAllow.GetSelectEntryPos();
629     bool bEnable = (nLbPos != SC_VALIDDLG_ALLOW_ANY);
630     bool bRange = (nLbPos == SC_VALIDDLG_ALLOW_RANGE);
631     bool bList = (nLbPos == SC_VALIDDLG_ALLOW_LIST);
632 
633     maCbAllow.Enable( bEnable );   // Leerzellen
634     maFtValue.Enable( bEnable );
635     maLbValue.Enable( bEnable );
636     maFtMin.Enable( bEnable );
637     maEdMin.Enable( bEnable );
638     maEdList.Enable( bEnable );
639     maFtMax.Enable( bEnable );
640     maEdMax.Enable( bEnable );
641 
642     bool bShowMax = false;
643     if( bRange )
644         maFtMin.SetText( maStrRange );
645     else if( bList )
646         maFtMin.SetText( maStrList );
647     else
648     {
649         switch( maLbValue.GetSelectEntryPos() )
650         {
651             case SC_VALIDDLG_DATA_EQUAL:
652             case SC_VALIDDLG_DATA_NOTEQUAL:     maFtMin.SetText( maStrValue );  break;
653 
654             case SC_VALIDDLG_DATA_LESS:
655             case SC_VALIDDLG_DATA_EQLESS:       maFtMin.SetText( maStrMax );    break;
656 
657             case SC_VALIDDLG_DATA_BETWEEN:
658             case SC_VALIDDLG_DATA_NOTBETWEEN:   bShowMax = true;    // fall through
659             case SC_VALIDDLG_DATA_GREATER:
660             case SC_VALIDDLG_DATA_EQGREATER:    maFtMin.SetText( maStrMin );    break;
661 
662             default:
663                 DBG_ERRORFILE( "ScTPValidationValue::SelectHdl - unknown condition mode" );
664         }
665     }
666 
667     maCbShow.Show( bRange || bList );
668     maCbSort.Show( bRange || bList );
669     maFtValue.Show( !bRange && !bList );
670     maLbValue.Show( !bRange && !bList );
671     maEdMin.Show( !bList );
672     maEdList.Show( bList );
673     maFtMax.Show( bShowMax );
674     maEdMax.Show( bShowMax );
675     maFtHint.Show( bRange );
676     //<!--Added by PengYunQuan for Validity Cell Range Picker
677     m_btnRef.Show( bRange );
678     //-->Added by PengYunQuan for Validity Cell Range Picker
679     return 0;
680 }
681 
682 IMPL_LINK( ScTPValidationValue, CheckHdl, CheckBox*, EMPTYARG )
683 {
684     maCbSort.Enable( maCbShow.IsChecked() );
685     return 0;
686 }
687 
688 
689 //========================================================================
690 //========================================================================
691 // Input Help Page
692 
693 ScTPValidationHelp::ScTPValidationHelp( Window*         pParent,
694                                           const SfxItemSet& rArgSet )
695 
696     :   SfxTabPage      ( pParent,
697                           ScResId( TP_VALIDATION_INPUTHELP ),
698                           rArgSet ),
699         aTsbHelp        ( this, ScResId( TSB_HELP ) ),
700         aFlContent      ( this, ScResId( FL_CONTENT ) ),
701         aFtTitle        ( this, ScResId( FT_TITLE ) ),
702         aEdtTitle       ( this, ScResId( EDT_TITLE ) ),
703         aFtInputHelp    ( this, ScResId( FT_INPUTHELP ) ),
704         aEdInputHelp    ( this, ScResId( EDT_INPUTHELP ) ),
705 
706         mrArgSet            ( rArgSet )
707 {
708     Init();
709     FreeResource();
710 }
711 
712 // -----------------------------------------------------------------------
713 
714 __EXPORT ScTPValidationHelp::~ScTPValidationHelp()
715 {
716 }
717 
718 // -----------------------------------------------------------------------
719 
720 void ScTPValidationHelp::Init()
721 {
722     //aLb.SetSelectHdl( LINK( this, ScTPValidationHelp, SelectHdl ) );
723 
724     aTsbHelp.EnableTriState( sal_False );
725 }
726 
727 //------------------------------------------------------------------------
728 
729 sal_uInt16* __EXPORT ScTPValidationHelp::GetRanges()
730 {
731     return pValueRanges;
732 }
733 
734 // -----------------------------------------------------------------------
735 
736 SfxTabPage* __EXPORT ScTPValidationHelp::Create( Window*    pParent,
737                                          const SfxItemSet&  rArgSet )
738 {
739     return ( new ScTPValidationHelp( pParent, rArgSet ) );
740 }
741 
742 // -----------------------------------------------------------------------
743 
744 void __EXPORT ScTPValidationHelp::Reset( const SfxItemSet& rArgSet )
745 {
746     const SfxPoolItem* pItem;
747 
748     if ( rArgSet.GetItemState( FID_VALID_SHOWHELP, sal_True, &pItem ) == SFX_ITEM_SET )
749         aTsbHelp.SetState( ((const SfxBoolItem*)pItem)->GetValue() ? STATE_CHECK : STATE_NOCHECK );
750     else
751         aTsbHelp.SetState( STATE_NOCHECK );
752 
753     if ( rArgSet.GetItemState( FID_VALID_HELPTITLE, sal_True, &pItem ) == SFX_ITEM_SET )
754         aEdtTitle.SetText( ((const SfxStringItem*)pItem)->GetValue() );
755     else
756         aEdtTitle.SetText( EMPTY_STRING );
757 
758     if ( rArgSet.GetItemState( FID_VALID_HELPTEXT, sal_True, &pItem ) == SFX_ITEM_SET )
759         aEdInputHelp.SetText( ((const SfxStringItem*)pItem)->GetValue() );
760     else
761         aEdInputHelp.SetText( EMPTY_STRING );
762 }
763 
764 // -----------------------------------------------------------------------
765 
766 sal_Bool __EXPORT ScTPValidationHelp::FillItemSet( SfxItemSet& rArgSet )
767 {
768     rArgSet.Put( SfxBoolItem( FID_VALID_SHOWHELP, aTsbHelp.GetState() == STATE_CHECK ) );
769     rArgSet.Put( SfxStringItem( FID_VALID_HELPTITLE, aEdtTitle.GetText() ) );
770     rArgSet.Put( SfxStringItem( FID_VALID_HELPTEXT, aEdInputHelp.GetText() ) );
771 
772     return sal_True;
773 }
774 
775 //========================================================================
776 //========================================================================
777 // Error Alert Page
778 
779 ScTPValidationError::ScTPValidationError( Window*           pParent,
780                                           const SfxItemSet& rArgSet )
781 
782     :   SfxTabPage      ( pParent,
783                           ScResId( TP_VALIDATION_ERROR ),
784                           rArgSet ),
785         aTsbShow        ( this, ScResId( TSB_SHOW ) ),
786         aFlContent      ( this, ScResId( FL_CONTENT ) ),
787         aFtAction       ( this, ScResId( FT_ACTION ) ),
788         aLbAction       ( this, ScResId( LB_ACTION ) ),
789         aBtnSearch      ( this, ScResId( BTN_SEARCH ) ),
790         aFtTitle        ( this, ScResId( FT_TITLE ) ),
791         aEdtTitle       ( this, ScResId( EDT_TITLE ) ),
792         aFtError        ( this, ScResId( FT_ERROR ) ),
793         aEdError        ( this, ScResId( EDT_ERROR ) ),
794 
795         mrArgSet            ( rArgSet )
796 {
797     Init();
798     FreeResource();
799 }
800 
801 // -----------------------------------------------------------------------
802 
803 __EXPORT ScTPValidationError::~ScTPValidationError()
804 {
805 }
806 
807 // -----------------------------------------------------------------------
808 
809 void ScTPValidationError::Init()
810 {
811     aLbAction.SetSelectHdl( LINK( this, ScTPValidationError, SelectActionHdl ) );
812     aBtnSearch.SetClickHdl( LINK( this, ScTPValidationError, ClickSearchHdl ) );
813 
814     aLbAction.SelectEntryPos( 0 );
815     aTsbShow.EnableTriState( sal_False );
816 
817     SelectActionHdl( NULL );
818 }
819 
820 //------------------------------------------------------------------------
821 
822 sal_uInt16* __EXPORT ScTPValidationError::GetRanges()
823 {
824     return pValueRanges;
825 }
826 
827 // -----------------------------------------------------------------------
828 
829 SfxTabPage* __EXPORT ScTPValidationError::Create( Window*   pParent,
830                                          const SfxItemSet&  rArgSet )
831 {
832     return ( new ScTPValidationError( pParent, rArgSet ) );
833 }
834 
835 // -----------------------------------------------------------------------
836 
837 void __EXPORT ScTPValidationError::Reset( const SfxItemSet& rArgSet )
838 {
839     const SfxPoolItem* pItem;
840 
841     if ( rArgSet.GetItemState( FID_VALID_SHOWERR, sal_True, &pItem ) == SFX_ITEM_SET )
842         aTsbShow.SetState( ((const SfxBoolItem*)pItem)->GetValue() ? STATE_CHECK : STATE_NOCHECK );
843     else
844         aTsbShow.SetState( STATE_CHECK );   // #111720# check by default
845 
846     if ( rArgSet.GetItemState( FID_VALID_ERRSTYLE, sal_True, &pItem ) == SFX_ITEM_SET )
847         aLbAction.SelectEntryPos( ((const SfxAllEnumItem*)pItem)->GetValue() );
848     else
849         aLbAction.SelectEntryPos( 0 );
850 
851     if ( rArgSet.GetItemState( FID_VALID_ERRTITLE, sal_True, &pItem ) == SFX_ITEM_SET )
852         aEdtTitle.SetText( ((const SfxStringItem*)pItem)->GetValue() );
853     else
854         aEdtTitle.SetText( EMPTY_STRING );
855 
856     if ( rArgSet.GetItemState( FID_VALID_ERRTEXT, sal_True, &pItem ) == SFX_ITEM_SET )
857         aEdError.SetText( ((const SfxStringItem*)pItem)->GetValue() );
858     else
859         aEdError.SetText( EMPTY_STRING );
860 
861     SelectActionHdl( NULL );
862 }
863 
864 // -----------------------------------------------------------------------
865 
866 sal_Bool __EXPORT ScTPValidationError::FillItemSet( SfxItemSet& rArgSet )
867 {
868     rArgSet.Put( SfxBoolItem( FID_VALID_SHOWERR, aTsbShow.GetState() == STATE_CHECK ) );
869     rArgSet.Put( SfxAllEnumItem( FID_VALID_ERRSTYLE, aLbAction.GetSelectEntryPos() ) );
870     rArgSet.Put( SfxStringItem( FID_VALID_ERRTITLE, aEdtTitle.GetText() ) );
871     rArgSet.Put( SfxStringItem( FID_VALID_ERRTEXT, aEdError.GetText() ) );
872 
873     return sal_True;
874 }
875 
876 // -----------------------------------------------------------------------
877 
878 IMPL_LINK( ScTPValidationError, SelectActionHdl, ListBox*, EMPTYARG )
879 {
880     ScValidErrorStyle eStyle = (ScValidErrorStyle) aLbAction.GetSelectEntryPos();
881     sal_Bool bMacro = ( eStyle == SC_VALERR_MACRO );
882 
883     aBtnSearch.Enable( bMacro );
884     aFtError.Enable( !bMacro );
885     aEdError.Enable( !bMacro );
886 
887     return( 0L );
888 }
889 
890 // -----------------------------------------------------------------------
891 
892 IMPL_LINK( ScTPValidationError, ClickSearchHdl, PushButton*, EMPTYARG )
893 {
894     Window* pOld = Application::GetDefDialogParent();
895     Application::SetDefDialogParent( this );
896 
897     // Use static SfxApplication method to bring up selector dialog for
898     // choosing a script
899     ::rtl::OUString aScriptURL = SfxApplication::ChooseScript();
900 
901     Application::SetDefDialogParent( pOld );
902 
903     if ( aScriptURL != NULL && aScriptURL.getLength() != 0 )
904     {
905         aEdtTitle.SetText( aScriptURL );
906     }
907 
908     return( 0L );
909 }
910 
911 //<!--Added by PengYunQuan for Validity Cell Range Picker
912 bool ScValidationDlg::EnterRefStatus()
913 {
914     ScTabViewShell *pTabViewShell = GetTabViewShell();
915 
916     if( !pTabViewShell ) return false;
917 
918     sal_uInt16 nId  = SLOTID;
919     SfxViewFrame* pViewFrm = pTabViewShell->GetViewFrame();
920     SfxChildWindow* pWnd = pViewFrm->GetChildWindow( nId );
921 
922     if ( pWnd && pWnd->GetWindow()!= this ) pWnd = NULL;
923 
924     SC_MOD()->SetRefDialog( nId, pWnd ? sal_False : sal_True );
925 
926     return true;
927 }
928 
929 bool ScValidationDlg::LeaveRefStatus()
930 {
931     ScTabViewShell *pTabViewShell = GetTabViewShell();
932 
933     if( !pTabViewShell ) return false;
934 
935     sal_uInt16 nId  = SLOTID;
936     SfxViewFrame* pViewFrm = pTabViewShell->GetViewFrame();
937     //SfxChildWindow* pWnd = pViewFrm->GetChildWindow( nId );
938     if ( pViewFrm->GetChildWindow( nId ) )
939     {
940         DoClose( nId );
941     }
942     return true;
943 }
944 
945 bool ScValidationDlg::SetupRefDlg()
946 {
947     if ( m_bOwnRefHdlr ) return false;
948     if( EnterRefMode() )
949     {
950         SetModal( sal_False );
951         return  /*SetChkShell( GetDocShell() ),*/ m_bOwnRefHdlr = true && EnterRefStatus();
952     }
953 
954     return false;
955 }
956 
957 bool ScValidationDlg::RemoveRefDlg( sal_Bool bRestoreModal /* = sal_True */ )
958 {
959     bool bVisLock = false;
960     bool bFreeWindowLock = false;
961 
962     ScTabViewShell *pTabVwSh = GetTabViewShell();
963 
964     if( !pTabVwSh ) return false;
965 
966     if ( SfxChildWindow* pWnd = pTabVwSh->GetViewFrame()->GetChildWindow( SID_VALIDITY_REFERENCE ) )
967     {
968         bVisLock = static_cast<ScValidityRefChildWin*>(pWnd)->LockVisible( true );
969         bFreeWindowLock = static_cast<ScValidityRefChildWin*>(pWnd)->LockFreeWindow( true );
970     }
971 
972     if ( !m_bOwnRefHdlr ) return false;
973     if( LeaveRefStatus() && LeaveRefMode() )
974     {
975         m_bOwnRefHdlr = false;
976 
977         if( bRestoreModal )
978             SetModal( sal_True );
979     }
980 
981     if ( SfxChildWindow* pWnd = pTabVwSh->GetViewFrame()->GetChildWindow( SID_VALIDITY_REFERENCE ) )
982     {
983         static_cast<ScValidityRefChildWin*>(pWnd)->LockVisible( bVisLock );
984         static_cast<ScValidityRefChildWin*>(pWnd)->LockFreeWindow( bFreeWindowLock );
985     }
986 
987     return true;
988 }
989 
990 //TYPEINIT1( ScTPValidationValue, SfxTabPage )
991 
992 void ScTPValidationValue::ScRefButtonEx::Click()
993 {
994     if( ScTPValidationValue *pParent = dynamic_cast< ScTPValidationValue*>( GetParent() ) )
995         pParent->OnClick( this );
996 
997     ScRefButton::Click();
998 }
999 
1000 void ScTPValidationValue::OnClick( Button *pBtn )
1001 {
1002     if( pBtn == &m_btnRef )
1003         SetupRefDlg();
1004 }
1005 
1006 sal_Bool ScValidationDlg::IsChildFocus()
1007 {
1008     if ( const Window *pWin = Application::GetFocusWindow() )
1009         while( NULL != ( pWin = pWin->GetParent() ) )
1010             if( pWin == this )
1011                 return sal_True;
1012 
1013     return sal_False;
1014 }
1015 
1016 
1017 bool    ScValidationDlg::IsAlive()
1018 {
1019     return SC_MOD()->IsAliveRefDlg( SLOTID, this );
1020 }
1021 //-->Added by PengYunQuan for Validity Cell Range Picker
1022