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_sc.hxx"
26 
27 #undef SC_DLLIMPLEMENTATION
28 
29 
30 
31 //------------------------------------------------------------------
32 
33 #define _TPHFEDIT_CXX
34 #include "scitems.hxx"
35 #include <editeng/eeitem.hxx>
36 
37 //CHINA001 #include <svx/chardlg.hxx>
38 #include <editeng/editobj.hxx>
39 #include <editeng/editstat.hxx>
40 #include <editeng/editview.hxx>
41 #include <editeng/flditem.hxx>
42 #include <sfx2/basedlgs.hxx>
43 #include <sfx2/objsh.hxx>
44 #include <vcl/msgbox.hxx>
45 #include <vcl/svapp.hxx>
46 #include <unotools/useroptions.hxx>
47 #define _SVSTDARR_USHORTS
48 #include <svl/svstdarr.hxx>
49 
50 //CHINA001 #include "tphfedit.hxx"
51 #include "editutil.hxx"
52 #include "global.hxx"
53 #include "attrib.hxx"
54 #include "patattr.hxx"
55 #include "scresid.hxx"
56 #include "sc.hrc"
57 #include "globstr.hrc"
58 #include "tabvwsh.hxx"
59 #include "prevwsh.hxx"
60 #include "hfedtdlg.hrc"
61 #include "textdlgs.hxx"
62 #include "AccessibleEditObject.hxx"
63 
64 #include "scuitphfedit.hxx" //CHINA001
65 #include <memory> // header file for auto_ptr
66 
67 // STATIC DATA -----------------------------------------------------------
68 
69 static sal_uInt16 pPageRightHeaderRanges[] = { SID_SCATTR_PAGE_HEADERRIGHT,
70 										   SID_SCATTR_PAGE_HEADERRIGHT,
71 										   0 };
72 
73 static sal_uInt16 pPageRightFooterRanges[] = { SID_SCATTR_PAGE_FOOTERRIGHT,
74 										   SID_SCATTR_PAGE_FOOTERRIGHT,
75 										   0 };
76 
77 static sal_uInt16 pPageLeftHeaderRanges[] =  { SID_SCATTR_PAGE_HEADERLEFT,
78 										   SID_SCATTR_PAGE_HEADERLEFT,
79 										   0 };
80 
81 static sal_uInt16 pPageLeftFooterRanges[] =  { SID_SCATTR_PAGE_FOOTERLEFT,
82 										   SID_SCATTR_PAGE_FOOTERLEFT,
83 										   0 };
84 
85 
86 static ScEditWindow* pActiveEdWnd = NULL;
87 
88 
89 //========================================================================
90 // class ScHFEditPage
91 //
92 
93 ScHFEditPage::ScHFEditPage( Window* 			pParent,
94 							sal_uInt16				nResId,
95 							const SfxItemSet&	rCoreAttrs,
96 							sal_uInt16				nWhichId,
97 							bool bHeader  )
98 
99 	:	SfxTabPage		( pParent, ScResId( nResId ), rCoreAttrs ),
100 
101 		aFtLeft 		( this, ScResId( FT_LEFT ) ),
102 		aWndLeft		( this, ScResId( WND_LEFT ), Left ),
103 		aFtCenter		( this, ScResId( FT_CENTER ) ),
104 		aWndCenter		( this, ScResId( WND_CENTER ), Center ),
105 		aFtRight		( this, ScResId( FT_RIGHT ) ),
106 		aWndRight		( this, ScResId( WND_RIGHT ), Right ),
107 		maFtDefinedHF		( this, ScResId( FT_HF_DEFINED ) ),
108 		maLbDefined		( this, ScResId( LB_DEFINED ) ),
109 		maFtCustomHF		( this, ScResId( FT_HF_CUSTOM ) ),
110 		aBtnText		( this, ScResId( BTN_TEXT ) ),
111 		aBtnFile		( this, ScResId( BTN_FILE ) ),
112 		aBtnTable		( this, ScResId( BTN_TABLE ) ),
113 		aBtnPage		( this, ScResId( BTN_PAGE ) ),
114 		aBtnLastPage	( this, ScResId( BTN_PAGES ) ),
115 		aBtnDate		( this, ScResId( BTN_DATE ) ),
116 		aBtnTime		( this, ScResId( BTN_TIME ) ),
117 		aFlInfo 		( this, ScResId( FL_INFO ) ),
118 		aFtInfo 		( this, ScResId( FT_INFO ) ),
119 		aPopUpFile		( ScResId( RID_POPUP_FCOMMAND) ),
120 		nWhich			( nWhichId )
121 {
122 	//!	use default style from current document?
123 	//!	if font color is used, header/footer background color must be set
124 
125 	ScPatternAttr	aPatAttr( rCoreAttrs.GetPool() );
126 
127 
128 	aBtnFile.SetPopupMenu(&aPopUpFile);
129 
130 	maLbDefined.SetSelectHdl( LINK( this, ScHFEditPage, ListHdl_Impl ) );
131 	aBtnFile.SetMenuHdl( LINK( this, ScHFEditPage, MenuHdl ) );
132 	aBtnText	.SetClickHdl( LINK( this, ScHFEditPage, ClickHdl ) );
133 	aBtnPage	.SetClickHdl( LINK( this, ScHFEditPage, ClickHdl ) );
134 	aBtnLastPage.SetClickHdl( LINK( this, ScHFEditPage, ClickHdl ) );
135 	aBtnDate	.SetClickHdl( LINK( this, ScHFEditPage, ClickHdl ) );
136 	aBtnTime	.SetClickHdl( LINK( this, ScHFEditPage, ClickHdl ) );
137 	aBtnFile	.SetClickHdl( LINK( this, ScHFEditPage, ClickHdl ) );
138 	aBtnTable	.SetClickHdl( LINK( this, ScHFEditPage, ClickHdl ) );
139 
140     aBtnText    .SetModeImage( Image( ScResId( IMG_TEXT_H  ) ), BMP_COLOR_HIGHCONTRAST );
141     aBtnFile    .SetModeImage( Image( ScResId( IMG_FILE_H  ) ), BMP_COLOR_HIGHCONTRAST );
142     aBtnTable   .SetModeImage( Image( ScResId( IMG_TABLE_H ) ), BMP_COLOR_HIGHCONTRAST );
143     aBtnPage    .SetModeImage( Image( ScResId( IMG_PAGE_H  ) ), BMP_COLOR_HIGHCONTRAST );
144     aBtnLastPage.SetModeImage( Image( ScResId( IMG_PAGES_H ) ), BMP_COLOR_HIGHCONTRAST );
145     aBtnDate    .SetModeImage( Image( ScResId( IMG_DATE_H  ) ), BMP_COLOR_HIGHCONTRAST );
146     aBtnTime    .SetModeImage( Image( ScResId( IMG_TIME_H  ) ), BMP_COLOR_HIGHCONTRAST );
147 
148     if(!bHeader)
149     {
150         maFtDefinedHF.SetText(ScGlobal::GetRscString( STR_FOOTER ));
151         maFtCustomHF.SetText(ScGlobal::GetRscString( STR_HF_CUSTOM_FOOTER ));
152     }
153 	if( Application::GetSettings().GetLayoutRTL() )
154 	{
155 		Point pt1 = aWndLeft.GetPosPixel();
156 		Point pt2 = aWndRight.GetPosPixel();
157 		aWndLeft.SetPosPixel(pt2);
158 		aWndRight.SetPosPixel(pt1);
159 
160 		pt1 = aFtLeft.GetPosPixel();
161 		pt2 = aFtRight.GetPosPixel();
162 		aFtLeft.SetPosPixel(pt2);
163 		aFtRight.SetPosPixel(pt1);
164 	}
165 	aWndLeft.	SetFont( aPatAttr );
166 	aWndCenter. SetFont( aPatAttr );
167 	aWndRight.	SetFont( aPatAttr );
168 
169 	FillCmdArr();
170 
171 	aWndLeft.GrabFocus();
172 
173 	InitPreDefinedList();
174 
175 	FreeResource();
176 }
177 
178 // -----------------------------------------------------------------------
179 
180 __EXPORT ScHFEditPage::~ScHFEditPage()
181 {
182 }
183 
184 void ScHFEditPage::SetNumType(SvxNumType eNumType)
185 {
186 	aWndLeft.SetNumType(eNumType);
187 	aWndCenter.SetNumType(eNumType);
188 	aWndRight.SetNumType(eNumType);
189 }
190 
191 // -----------------------------------------------------------------------
192 
193 #define IS_AVAILABLE(w)(rCoreSet.GetItemState( (w) ) >= SFX_ITEM_AVAILABLE)
194 
195 void __EXPORT ScHFEditPage::Reset( const SfxItemSet& rCoreSet )
196 {
197 	if ( IS_AVAILABLE( nWhich ) )
198 	{
199 		const ScPageHFItem& rItem = (const ScPageHFItem&)(rCoreSet.Get( nWhich ));
200 
201         if( const EditTextObject* pLeft = rItem.GetLeftArea() )
202             aWndLeft.SetText( *pLeft );
203         if( const EditTextObject* pCenter = rItem.GetCenterArea() )
204             aWndCenter.SetText( *pCenter );
205         if( const EditTextObject* pRight = rItem.GetRightArea() )
206             aWndRight.SetText( *pRight );
207 
208 		SetSelectDefinedList();
209 	}
210 }
211 
212 #undef IS_AVAILABLE
213 
214 // -----------------------------------------------------------------------
215 
216 sal_Bool __EXPORT ScHFEditPage::FillItemSet( SfxItemSet& rCoreSet )
217 {
218 	ScPageHFItem	aItem( nWhich );
219 	EditTextObject* pLeft	= aWndLeft	.CreateTextObject();
220 	EditTextObject* pCenter = aWndCenter.CreateTextObject();
221 	EditTextObject* pRight	= aWndRight .CreateTextObject();
222 
223 	aItem.SetLeftArea  ( *pLeft );
224 	aItem.SetCenterArea( *pCenter );
225 	aItem.SetRightArea ( *pRight );
226 	delete pLeft;
227 	delete pCenter;
228 	delete pRight;
229 
230 	rCoreSet.Put( aItem );
231 
232 	return sal_True;
233 }
234 
235 // -----------------------------------------------------------------------
236 
237 #define SET_CMD(i,id) \
238 	aCmd  = aDel;							\
239 	aCmd += ScGlobal::GetRscString( id );	\
240 	aCmd += aDel;							\
241 	aCmdArr[i] = aCmd;
242 
243 // -----------------------------------------------------------------------
244 
245 void ScHFEditPage::FillCmdArr()
246 {
247 	String aDel( ScGlobal::GetRscString( STR_HFCMD_DELIMITER ) );
248 	String aCmd;
249 
250 	SET_CMD( 0, STR_HFCMD_PAGE )
251 	SET_CMD( 1, STR_HFCMD_PAGES )
252 	SET_CMD( 2, STR_HFCMD_DATE )
253 	SET_CMD( 3, STR_HFCMD_TIME )
254 	SET_CMD( 4, STR_HFCMD_FILE )
255 	SET_CMD( 5, STR_HFCMD_TABLE )
256 }
257 
258 #undef SET_CMD
259 
260 void ScHFEditPage::InitPreDefinedList()
261 {
262     SvtUserOptions aUserOpt;
263 
264     Color* pTxtColour = NULL;
265     Color* pFldColour = NULL;
266 
267     // Get the all field values at the outset.
268     String aPageFieldValue(aWndLeft.GetEditEngine()->CalcFieldValue(SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD), 0,0, pTxtColour, pFldColour));
269     String aSheetFieldValue(aWndLeft.GetEditEngine()->CalcFieldValue(SvxFieldItem(SvxTableField(), EE_FEATURE_FIELD), 0,0, pTxtColour, pFldColour));
270     String aFileFieldValue(aWndLeft.GetEditEngine()->CalcFieldValue(SvxFieldItem(SvxFileField(), EE_FEATURE_FIELD), 0,0, pTxtColour, pFldColour));
271     String aExtFileFieldValue(aWndLeft.GetEditEngine()->CalcFieldValue(SvxFieldItem(SvxExtFileField(), EE_FEATURE_FIELD), 0,0, pTxtColour, pFldColour));
272     String aDateFieldValue(aWndLeft.GetEditEngine()->CalcFieldValue(SvxFieldItem(SvxDateField(), EE_FEATURE_FIELD), 0,0, pTxtColour, pFldColour));
273 
274     maLbDefined.Clear();
275 
276     maLbDefined.InsertEntry( ScGlobal::GetRscString( STR_HF_NONE_IN_BRACKETS ));
277 
278     String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
279     aPageEntry += ' ';
280     aPageEntry += aPageFieldValue;
281     maLbDefined.InsertEntry(aPageEntry);
282 
283     String aPageOfEntry(aPageEntry);
284     aPageOfEntry += ' ';
285     aPageOfEntry += ScGlobal::GetRscString( STR_HF_OF_QUESTION );
286     maLbDefined.InsertEntry( aPageOfEntry);
287 
288     maLbDefined.InsertEntry(aSheetFieldValue);
289 
290     String aConfidentialEntry(aUserOpt.GetCompany());
291     aConfidentialEntry += ' ';
292     aConfidentialEntry += ScGlobal::GetRscString( STR_HF_CONFIDENTIAL );
293     aConfidentialEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
294     aConfidentialEntry += aDateFieldValue;
295     aConfidentialEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
296     aConfidentialEntry += aPageEntry;
297     maLbDefined.InsertEntry( aConfidentialEntry);
298 
299     String aFileNamePageEntry(aFileFieldValue);
300     aFileNamePageEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
301     aFileNamePageEntry += aPageEntry;
302     maLbDefined.InsertEntry( aFileNamePageEntry);
303 
304     maLbDefined.InsertEntry( aExtFileFieldValue);
305 
306     String aPageSheetNameEntry(aPageEntry);
307     aPageSheetNameEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
308     aPageSheetNameEntry += aSheetFieldValue;
309     maLbDefined.InsertEntry( aPageSheetNameEntry);
310 
311     String aPageFileNameEntry(aPageEntry);
312     aPageFileNameEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
313     aPageFileNameEntry += aFileFieldValue;
314     maLbDefined.InsertEntry( aPageFileNameEntry);
315 
316     String aPagePathNameEntry(aPageEntry);
317     aPagePathNameEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
318     aPagePathNameEntry += aExtFileFieldValue;
319     maLbDefined.InsertEntry( aPagePathNameEntry);
320 
321     String aUserNameEntry(aUserOpt.GetFirstName());
322     aUserNameEntry += ' ';
323     aUserNameEntry += (String)aUserOpt.GetLastName();
324     aUserNameEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
325     aUserNameEntry += aPageEntry;
326     aUserNameEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
327     aUserNameEntry += aDateFieldValue;
328     maLbDefined.InsertEntry( aUserNameEntry);
329 
330     String aCreatedByEntry(ScGlobal::GetRscString( STR_HF_CREATED_BY ) );
331     aCreatedByEntry += ' ';
332     aCreatedByEntry += (String)aUserOpt.GetFirstName();
333     aCreatedByEntry += ' ';
334     aCreatedByEntry += (String)aUserOpt.GetLastName();
335     aCreatedByEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
336     aCreatedByEntry += aDateFieldValue;
337     aCreatedByEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
338     aCreatedByEntry += aPageEntry;
339     maLbDefined.InsertEntry( aCreatedByEntry);
340 }
341 
342 void ScHFEditPage::InsertToDefinedList()
343 {
344     sal_uInt16 nCount =  maLbDefined.GetEntryCount();
345     if(nCount == eEntryCount)
346     {
347         String aCustomizedEntry(ScGlobal::GetRscString( STR_HF_CUSTOMIZED ) );
348         maLbDefined.InsertEntry( aCustomizedEntry);
349         maLbDefined.SelectEntryPos(eEntryCount);
350     }
351 }
352 
353 void ScHFEditPage::RemoveFromDefinedList()
354 {
355     sal_uInt16 nCount =  maLbDefined.GetEntryCount();
356     if(nCount > eEntryCount )
357         maLbDefined.RemoveEntry( nCount-1);
358 }
359 
360 // determine if the header/footer exists in our predefined list and set select to it.
361 void ScHFEditPage::SetSelectDefinedList()
362 {
363     SvtUserOptions aUserOpt;
364 
365     // default to customized
366     ScHFEntryId eSelectEntry = eEntryCount;
367 
368     ::std::auto_ptr< EditTextObject > pLeftObj;
369     ::std::auto_ptr< EditTextObject > pCenterObj;
370     ::std::auto_ptr< EditTextObject > pRightObj;
371 
372     XubString aLeftEntry;
373     XubString aCenterEntry;
374     XubString aRightEntry;
375 
376     pLeftObj.reset(aWndLeft.GetEditEngine()->CreateTextObject());
377     pCenterObj.reset(aWndCenter.GetEditEngine()->CreateTextObject());
378     pRightObj.reset(aWndRight.GetEditEngine()->CreateTextObject());
379 
380     bool bFound = false;
381 
382     sal_uInt16 i;
383     sal_uInt16 nCount =  maLbDefined.GetEntryCount();
384     for(i = 0; i < nCount && !bFound; i++)
385     {
386         switch(static_cast<ScHFEntryId>(i))
387         {
388             case eNoneEntry:
389             {
390                 aLeftEntry = pLeftObj->GetText(0);
391                 aCenterEntry = pCenterObj->GetText(0);
392                 aRightEntry = pRightObj->GetText(0);
393                 if(aLeftEntry == EMPTY_STRING && aCenterEntry == EMPTY_STRING
394                     && aRightEntry == EMPTY_STRING)
395                 {
396                     eSelectEntry = eNoneEntry;
397                     bFound = true;
398                 }
399             }
400             break;
401 
402             case ePageEntry:
403             {
404                 aLeftEntry = pLeftObj->GetText(0);
405                 aRightEntry = pRightObj->GetText(0);
406                 if(aLeftEntry == EMPTY_STRING && aRightEntry == EMPTY_STRING)
407                 {
408                     if(IsPageEntry(aWndCenter.GetEditEngine(), pCenterObj.get()))
409                     {
410                         eSelectEntry = ePageEntry;
411                         bFound = true;
412                     }
413                 }
414             }
415             break;
416 
417 
418             //TODO
419             case ePagesEntry:
420             {
421             }
422             break;
423 
424             case eSheetEntry:
425             {
426                 aLeftEntry = pLeftObj->GetText(0);
427                 aRightEntry = pRightObj->GetText(0);
428                 if(aLeftEntry == EMPTY_STRING && aRightEntry == EMPTY_STRING)
429                 {
430                     if(pCenterObj->IsFieldObject())
431                     {
432                         const SvxFieldItem* pFieldItem = pCenterObj->GetField();
433                         if(pFieldItem)
434                         {
435                             const SvxFieldData* pField = pFieldItem->GetField();
436                             if(pField && pField->ISA(SvxTableField))
437                             {
438                                 eSelectEntry = eSheetEntry;
439                                 bFound = true;
440                             }
441                         }
442                     }
443                 }
444             }
445             break;
446 
447             case eConfidentialEntry:
448             {
449                 if(IsDateEntry(pCenterObj.get()) && IsPageEntry(aWndRight.GetEditEngine(), pRightObj.get()))
450                 {
451                     String aConfidentialEntry(aUserOpt.GetCompany());
452                     aConfidentialEntry += ' ';
453                     aConfidentialEntry += ScGlobal::GetRscString( STR_HF_CONFIDENTIAL );
454                     if(aConfidentialEntry == aWndLeft.GetEditEngine()->GetText(0))
455                     {
456                         eSelectEntry = eConfidentialEntry;
457                         bFound = true;
458                     }
459                 }
460             }
461             break;
462 
463             //TODO
464             case eFileNamePageEntry:
465             {
466             }
467             break;
468 
469             case eExtFileNameEntry:
470             {
471                 aLeftEntry = pLeftObj->GetText(0);
472                 aRightEntry = pRightObj->GetText(0);
473                 if(IsExtFileNameEntry(pCenterObj.get()) && aLeftEntry == EMPTY_STRING
474                     && aRightEntry == EMPTY_STRING)
475                 {
476                     eSelectEntry = eExtFileNameEntry;
477                     bFound = true;
478                 }
479             }
480             break;
481 
482             //TODO
483             case ePageSheetEntry:
484             {
485             }
486             break;
487 
488             //TODO
489             case ePageFileNameEntry:
490             {
491             }
492             break;
493 
494             case ePageExtFileNameEntry:
495             {
496                 aLeftEntry = pLeftObj->GetText(0);
497                 if(IsPageEntry(aWndCenter.GetEditEngine(), pCenterObj.get()) &&
498                     IsExtFileNameEntry(pRightObj.get()) && aLeftEntry == EMPTY_STRING)
499                 {
500                     eSelectEntry = ePageExtFileNameEntry;
501                     bFound = true;
502                 }
503             }
504             break;
505 
506             case eUserNameEntry:
507             {
508                 if(IsDateEntry(pRightObj.get()) && IsPageEntry(aWndCenter.GetEditEngine(), pCenterObj.get()))
509                 {
510                     String aUserNameEntry(aUserOpt.GetFirstName());
511                     aUserNameEntry += ' ';
512                     aUserNameEntry += (String)aUserOpt.GetLastName();
513                     if(aUserNameEntry == aWndLeft.GetEditEngine()->GetText(0))
514                     {
515                         eSelectEntry = eUserNameEntry;
516                         bFound = true;
517                     }
518                 }
519             }
520             break;
521 
522             case eCreatedByEntry:
523             {
524                 if(IsDateEntry(pCenterObj.get()) && IsPageEntry(aWndRight.GetEditEngine(), pRightObj.get()))
525                 {
526                     String aCreatedByEntry(ScGlobal::GetRscString( STR_HF_CREATED_BY ) );
527                     aCreatedByEntry += ' ';
528                     aCreatedByEntry += (String)aUserOpt.GetFirstName();
529                     aCreatedByEntry += ' ';
530                     aCreatedByEntry += (String)aUserOpt.GetLastName();
531                     if(aCreatedByEntry == aWndLeft.GetEditEngine()->GetText(0))
532                     {
533                         eSelectEntry = eCreatedByEntry;
534                         bFound = true;
535                     }
536                 }
537             }
538             break;
539 
540             default:
541             {
542                 // added to avoid warnings
543             }
544         }
545     }
546 
547     if(eSelectEntry == eEntryCount)
548         InsertToDefinedList();
549 
550     maLbDefined.SelectEntryPos( sal::static_int_cast<sal_uInt16>( eSelectEntry ) );
551 }
552 
553 bool ScHFEditPage::IsPageEntry(EditEngine*pEngine, EditTextObject* pTextObj)
554 {
555     if(!pEngine && !pTextObj)
556         return false;
557 
558     bool bReturn = false;
559 
560     if(!pTextObj->IsFieldObject())
561     {
562         SvUShorts aPosList;
563         pEngine->GetPortions(0,aPosList);
564         if(aPosList.Count() == 2)
565         {
566             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
567             aPageEntry += ' ';
568             ESelection aSel(0,0,0,0);
569             aSel.nEndPos = aPageEntry.Len();
570             if(aPageEntry == pEngine->GetText(aSel))
571             {
572                 aSel.nStartPos = aSel.nEndPos;
573                 aSel.nEndPos++;
574                 ::std::auto_ptr< EditTextObject > pPageObj;
575                 pPageObj.reset(pEngine->CreateTextObject(aSel));
576                 if(pPageObj.get() && pPageObj->IsFieldObject() )
577                 {
578                     const SvxFieldItem* pFieldItem = pPageObj->GetField();
579                     if(pFieldItem)
580                     {
581                         const SvxFieldData* pField = pFieldItem->GetField();
582                         if(pField && pField->ISA(SvxPageField))
583                             bReturn = true;
584                     }
585                 }
586             }
587         }
588     }
589     return bReturn;
590 }
591 
592 bool ScHFEditPage::IsDateEntry(EditTextObject* pTextObj)
593 {
594     if(!pTextObj)
595         return false;
596 
597     bool bReturn = false;
598     if(pTextObj->IsFieldObject())
599     {
600         const SvxFieldItem* pFieldItem = pTextObj->GetField();
601         if(pFieldItem)
602         {
603             const SvxFieldData* pField = pFieldItem->GetField();
604             if(pField && pField->ISA(SvxDateField))
605                 bReturn = true;
606         }
607     }
608     return bReturn;
609 }
610 
611 bool ScHFEditPage::IsExtFileNameEntry(EditTextObject* pTextObj)
612 {
613     if(!pTextObj)
614         return false;
615     bool bReturn = false;
616     if(pTextObj->IsFieldObject())
617     {
618         const SvxFieldItem* pFieldItem = pTextObj->GetField();
619         if(pFieldItem)
620 	{
621             const SvxFieldData* pField = pFieldItem->GetField();
622             if(pField && pField->ISA(SvxExtFileField))
623                 bReturn = true;
624         }
625     }
626     return bReturn;
627 }
628 
629 void ScHFEditPage::ProcessDefinedListSel(ScHFEntryId eSel, bool bTravelling)
630 {
631     SvtUserOptions aUserOpt;
632     ::std::auto_ptr< EditTextObject > pTextObj;
633 
634     switch(eSel)
635     {
636         case eNoneEntry:
637             ClearTextAreas();
638             if(!bTravelling)
639                 aWndLeft.GrabFocus();
640         break;
641 
642         case ePageEntry:
643         {
644             ClearTextAreas();
645             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
646             aPageEntry += ' ';
647             aWndCenter.GetEditEngine()->SetText(aPageEntry);
648             aWndCenter.InsertField( SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD) );
649             if(!bTravelling)
650                 aWndCenter.GrabFocus();
651         }
652         break;
653 
654         case ePagesEntry:
655         {
656             ClearTextAreas();
657             ESelection aSel(0,0,0,0);
658             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
659             aPageEntry += ' ';
660             aWndCenter.GetEditEngine()->SetText(aPageEntry);
661             aSel.nEndPos = aPageEntry.Len();
662             aWndCenter.GetEditEngine()->QuickInsertField(SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
663             ++aSel.nEndPos;
664             String aPageOfEntry = ' ';
665             aPageOfEntry += ScGlobal::GetRscString( STR_HF_OF );
666             aPageOfEntry += ' ';
667             aWndCenter.GetEditEngine()->QuickInsertText(aPageOfEntry,ESelection(aSel.nEndPara,aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
668             aSel.nEndPos = sal::static_int_cast<xub_StrLen>( aSel.nEndPos + aPageOfEntry.Len() );
669             aWndCenter.GetEditEngine()->QuickInsertField(SvxFieldItem(SvxPagesField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara,aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
670             pTextObj.reset(aWndCenter.GetEditEngine()->CreateTextObject());
671             aWndCenter.SetText(*pTextObj);
672             XubString aEntry(pTextObj.get()->GetText(0));
673             if(!bTravelling)
674                 aWndCenter.GrabFocus();
675         }
676         break;
677 
678         case eSheetEntry:
679             ClearTextAreas();
680             aWndCenter.InsertField( SvxFieldItem(SvxTableField(), EE_FEATURE_FIELD) );
681             if(!bTravelling)
682                 aWndCenter.GrabFocus();
683         break;
684 
685         case eConfidentialEntry:
686         {
687             ClearTextAreas();
688             String aConfidentialEntry(aUserOpt.GetCompany());
689             aConfidentialEntry += ' ';
690             aConfidentialEntry += ScGlobal::GetRscString( STR_HF_CONFIDENTIAL );
691             aWndLeft.GetEditEngine()->SetText(aConfidentialEntry);
692             aWndCenter.InsertField( SvxFieldItem(SvxDateField(Date(),SVXDATETYPE_VAR), EE_FEATURE_FIELD) );
693             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
694             aPageEntry += ' ';
695             aWndRight.GetEditEngine()->SetText(aPageEntry);
696             aWndRight.InsertField( SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD) );
697             if(!bTravelling)
698                 aWndRight.GrabFocus();
699         }
700         break;
701 
702         case eFileNamePageEntry:
703         {
704             ClearTextAreas();
705             ESelection aSel(0,0,0,0);
706             aWndCenter.GetEditEngine()->QuickInsertField(SvxFieldItem( SvxFileField(), EE_FEATURE_FIELD ), aSel );
707             ++aSel.nEndPos;
708             String aPageEntry(RTL_CONSTASCII_STRINGPARAM(", "));
709             aPageEntry += ScGlobal::GetRscString( STR_PAGE ) ;
710             aPageEntry += ' ';
711             aWndCenter.GetEditEngine()->QuickInsertText(aPageEntry, ESelection(aSel.nEndPara,aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
712             aSel.nStartPos = aSel.nEndPos;
713             aSel.nEndPos = sal::static_int_cast<xub_StrLen>( aSel.nEndPos + aPageEntry.Len() );
714             aWndCenter.GetEditEngine()->QuickInsertField(SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara,aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
715             pTextObj.reset(aWndCenter.GetEditEngine()->CreateTextObject());
716             aWndCenter.SetText(*pTextObj);
717             XubString aEntry2(pTextObj.get()->GetText(0));
718             if(!bTravelling)
719                 aWndCenter.GrabFocus();
720         }
721         break;
722 
723         case eExtFileNameEntry:
724             ClearTextAreas();
725             aWndCenter.InsertField( SvxFieldItem( SvxExtFileField(
726                 EMPTY_STRING, SVXFILETYPE_VAR, SVXFILEFORMAT_FULLPATH ), EE_FEATURE_FIELD ) );
727             if(!bTravelling)
728                 aWndCenter.GrabFocus();
729         break;
730 
731         case ePageSheetEntry:
732         {
733             ClearTextAreas();
734             ESelection aSel(0,0,0,0);
735             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
736             aPageEntry += ' ';
737             aWndCenter.GetEditEngine()->SetText(aPageEntry);
738             aSel.nEndPos = aPageEntry.Len();
739             aWndCenter.GetEditEngine()->QuickInsertField(SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
740             ++aSel.nEndPos;
741             String aCommaSpace(RTL_CONSTASCII_STRINGPARAM(", "));
742             aWndCenter.GetEditEngine()->QuickInsertText(aCommaSpace,ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
743             aSel.nEndPos = sal::static_int_cast<xub_StrLen>( aSel.nEndPos + aCommaSpace.Len() );
744             aWndCenter.GetEditEngine()->QuickInsertField( SvxFieldItem(SvxTableField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
745             pTextObj.reset(aWndCenter.GetEditEngine()->CreateTextObject());
746             aWndCenter.SetText(*pTextObj);
747             if(!bTravelling)
748                 aWndCenter.GrabFocus();
749         }
750         break;
751 
752         case ePageFileNameEntry:
753         {
754             ClearTextAreas();
755             ESelection aSel(0,0,0,0);
756             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
757             aPageEntry += ' ';
758             aWndCenter.GetEditEngine()->SetText(aPageEntry);
759             aSel.nEndPos = aPageEntry.Len();
760             aWndCenter.GetEditEngine()->QuickInsertField(SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
761             ++aSel.nEndPos;
762             String aCommaSpace(RTL_CONSTASCII_STRINGPARAM(", "));
763             aWndCenter.GetEditEngine()->QuickInsertText(aCommaSpace,ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
764             aSel.nEndPos = sal::static_int_cast<xub_StrLen>( aSel.nEndPos + aCommaSpace.Len() );
765             aWndCenter.GetEditEngine()->QuickInsertField( SvxFieldItem(SvxFileField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
766             pTextObj.reset(aWndCenter.GetEditEngine()->CreateTextObject());
767             aWndCenter.SetText(*pTextObj);
768             if(!bTravelling)
769                 aWndCenter.GrabFocus();
770         }
771         break;
772 
773         case ePageExtFileNameEntry:
774         {
775             ClearTextAreas();
776             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
777             aPageEntry += ' ';
778             aWndCenter.GetEditEngine()->SetText(aPageEntry);
779             aWndCenter.InsertField( SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD) );
780             aWndRight.InsertField( SvxFieldItem( SvxExtFileField(
781                 EMPTY_STRING, SVXFILETYPE_VAR, SVXFILEFORMAT_FULLPATH ), EE_FEATURE_FIELD ) );
782             if(!bTravelling)
783                 aWndRight.GrabFocus();
784         }
785         break;
786 
787         case eUserNameEntry:
788         {
789             ClearTextAreas();
790             String aUserNameEntry(aUserOpt.GetFirstName());
791             aUserNameEntry += ' ';
792             aUserNameEntry += (String)aUserOpt.GetLastName();
793             aWndLeft.GetEditEngine()->SetText(aUserNameEntry);
794             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
795             aPageEntry += ' ';
796             aWndCenter.GetEditEngine()->SetText(aPageEntry);
797             aWndCenter.InsertField( SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD) );
798             aWndRight.InsertField( SvxFieldItem(SvxDateField(Date(),SVXDATETYPE_VAR), EE_FEATURE_FIELD) );
799             if(!bTravelling)
800                 aWndRight.GrabFocus();
801         }
802         break;
803 
804         case eCreatedByEntry:
805         {
806             ClearTextAreas();
807             String aCreatedByEntry(ScGlobal::GetRscString( STR_HF_CREATED_BY ) );
808             aCreatedByEntry += ' ';
809             aCreatedByEntry += (String)aUserOpt.GetFirstName();
810             aCreatedByEntry += ' ';
811             aCreatedByEntry += (String)aUserOpt.GetLastName();
812             aWndLeft.GetEditEngine()->SetText(aCreatedByEntry);
813             aWndCenter.InsertField( SvxFieldItem(SvxDateField(Date(),SVXDATETYPE_VAR), EE_FEATURE_FIELD) );
814             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
815             aPageEntry += ' ';
816             aWndRight.GetEditEngine()->SetText(aPageEntry);
817             aWndRight.InsertField( SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD) );
818             if(!bTravelling)
819                 aWndRight.GrabFocus();
820         }
821         break;
822 
823         default :
824             break;
825     }
826 }
827 
828 void ScHFEditPage::ClearTextAreas()
829 {
830     aWndLeft.GetEditEngine()->SetText(EMPTY_STRING);
831     aWndLeft.Invalidate();
832     aWndCenter.GetEditEngine()->SetText(EMPTY_STRING);
833     aWndCenter.Invalidate();
834     aWndRight.GetEditEngine()->SetText(EMPTY_STRING);
835     aWndRight.Invalidate();
836 }
837 
838 //-----------------------------------------------------------------------
839 // Handler:
840 //-----------------------------------------------------------------------
841 
842 IMPL_LINK( ScHFEditPage, ListHdl_Impl, ListBox*, pList )
843 {
844     if ( pList && pList == &maLbDefined )
845     {
846         ScHFEntryId eSel = static_cast<ScHFEntryId>(maLbDefined.GetSelectEntryPos());
847         if(!maLbDefined.IsTravelSelect())
848         {
849             ProcessDefinedListSel(eSel);
850 
851             // check if we need to remove the customized entry.
852             if(eSel < eEntryCount)
853                 RemoveFromDefinedList();
854         }
855         else
856         {
857             ProcessDefinedListSel(eSel, true);
858         }
859     }
860     return 0;
861 }
862 
863 IMPL_LINK( ScHFEditPage, ClickHdl, ImageButton*, pBtn )
864 {
865 	pActiveEdWnd = ::GetScEditWindow(); //CHINA001
866 	if ( !pActiveEdWnd )
867 		return 0;
868 
869 	if ( pBtn == &aBtnText )
870 	{
871 		pActiveEdWnd->SetCharAttriutes();
872 	}
873 	else
874 	{
875 		if ( pBtn == &aBtnPage )
876             pActiveEdWnd->InsertField( SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD) );
877 		else if ( pBtn == &aBtnLastPage )
878             pActiveEdWnd->InsertField( SvxFieldItem(SvxPagesField(), EE_FEATURE_FIELD) );
879 		else if ( pBtn == &aBtnDate )
880             pActiveEdWnd->InsertField( SvxFieldItem(SvxDateField(Date(),SVXDATETYPE_VAR), EE_FEATURE_FIELD) );
881 		else if ( pBtn == &aBtnTime )
882             pActiveEdWnd->InsertField( SvxFieldItem(SvxTimeField(), EE_FEATURE_FIELD) );
883 		else if ( pBtn == &aBtnFile )
884 		{
885             pActiveEdWnd->InsertField( SvxFieldItem( SvxFileField(), EE_FEATURE_FIELD ) );
886 		}
887 		else if ( pBtn == &aBtnTable )
888             pActiveEdWnd->InsertField( SvxFieldItem(SvxTableField(), EE_FEATURE_FIELD) );
889 	}
890 	InsertToDefinedList();
891 	pActiveEdWnd->GrabFocus();
892 
893 	return 0;
894 }
895 
896 IMPL_LINK( ScHFEditPage, MenuHdl, ScExtIButton*, pBtn )
897 {
898 	pActiveEdWnd = ::GetScEditWindow(); //CHINA001
899 	if ( !pActiveEdWnd )
900 		return 0;
901 
902 	if(pBtn!=NULL)
903 	{
904 		switch(pBtn->GetSelected())
905 		{
906 			case FILE_COMMAND_TITEL:
907                 pActiveEdWnd->InsertField( SvxFieldItem( SvxFileField(), EE_FEATURE_FIELD ) );
908 				break;
909 			case FILE_COMMAND_FILENAME:
910 				pActiveEdWnd->InsertField( SvxFieldItem( SvxExtFileField(
911                         EMPTY_STRING, SVXFILETYPE_VAR, SVXFILEFORMAT_NAME_EXT ), EE_FEATURE_FIELD ) );
912 				break;
913 			case FILE_COMMAND_PATH:
914 				pActiveEdWnd->InsertField( SvxFieldItem( SvxExtFileField(
915                         EMPTY_STRING, SVXFILETYPE_VAR, SVXFILEFORMAT_FULLPATH ), EE_FEATURE_FIELD ) );
916 				break;
917 		}
918 	}
919 	return 0;
920 }
921 
922 //========================================================================
923 // class ScRightHeaderEditPage
924 //========================================================================
925 
926 ScRightHeaderEditPage::ScRightHeaderEditPage( Window* pParent, const SfxItemSet& rCoreSet )
927 	: ScHFEditPage( pParent, RID_SCPAGE_HFED_HR, rCoreSet,
928 					rCoreSet.GetPool()->GetWhich(SID_SCATTR_PAGE_HEADERRIGHT ), true )
929 	{}
930 
931 // -----------------------------------------------------------------------
932 
933 sal_uInt16* __EXPORT ScRightHeaderEditPage::GetRanges()
934 	{ return pPageRightHeaderRanges; }
935 
936 // -----------------------------------------------------------------------
937 
938 SfxTabPage* __EXPORT ScRightHeaderEditPage::Create( Window* pParent, const SfxItemSet& rCoreSet )
939 	{ return ( new ScRightHeaderEditPage( pParent, rCoreSet ) ); };
940 
941 
942 //========================================================================
943 // class ScLeftHeaderEditPage
944 //========================================================================
945 
946 ScLeftHeaderEditPage::ScLeftHeaderEditPage( Window* pParent, const SfxItemSet& rCoreSet )
947 	: ScHFEditPage( pParent, RID_SCPAGE_HFED_HL, rCoreSet,
948 					rCoreSet.GetPool()->GetWhich(SID_SCATTR_PAGE_HEADERLEFT ), true )
949 	{}
950 
951 // -----------------------------------------------------------------------
952 
953 sal_uInt16* __EXPORT ScLeftHeaderEditPage::GetRanges()
954 	{ return pPageLeftHeaderRanges; }
955 
956 // -----------------------------------------------------------------------
957 
958 SfxTabPage* __EXPORT ScLeftHeaderEditPage::Create( Window* pParent, const SfxItemSet& rCoreSet )
959 	{ return ( new ScLeftHeaderEditPage( pParent, rCoreSet ) ); };
960 
961 //========================================================================
962 // class ScRightFooterEditPage
963 //========================================================================
964 
965 ScRightFooterEditPage::ScRightFooterEditPage( Window* pParent, const SfxItemSet& rCoreSet )
966 	: ScHFEditPage( pParent, RID_SCPAGE_HFED_FR, rCoreSet,
967 					rCoreSet.GetPool()->GetWhich(SID_SCATTR_PAGE_FOOTERRIGHT ), false )
968 	{}
969 
970 // -----------------------------------------------------------------------
971 
972 sal_uInt16* __EXPORT ScRightFooterEditPage::GetRanges()
973 	{ return pPageRightFooterRanges; }
974 
975 // -----------------------------------------------------------------------
976 
977 SfxTabPage* __EXPORT ScRightFooterEditPage::Create( Window* pParent, const SfxItemSet& rCoreSet )
978 	{ return ( new ScRightFooterEditPage( pParent, rCoreSet ) ); };
979 
980 //========================================================================
981 // class ScLeftFooterEditPage
982 //========================================================================
983 
984 ScLeftFooterEditPage::ScLeftFooterEditPage( Window* pParent, const SfxItemSet& rCoreSet )
985 	: ScHFEditPage( pParent, RID_SCPAGE_HFED_FL, rCoreSet,
986 					rCoreSet.GetPool()->GetWhich(SID_SCATTR_PAGE_FOOTERLEFT ), false )
987 	{}
988 
989 // -----------------------------------------------------------------------
990 
991 sal_uInt16* __EXPORT ScLeftFooterEditPage::GetRanges()
992 	{ return pPageLeftFooterRanges; }
993 
994 // -----------------------------------------------------------------------
995 
996 SfxTabPage* __EXPORT ScLeftFooterEditPage::Create( Window* pParent, const SfxItemSet& rCoreSet )
997 	{ return ( new ScLeftFooterEditPage( pParent, rCoreSet ) ); };
998