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 	//IAccessibility2 Implementation 2009-----
170 	aWndLeft.SetObjectSelectHdl( LINK(this,ScHFEditPage,ObjectSelectHdl) );
171 	aWndCenter.SetObjectSelectHdl( LINK(this,ScHFEditPage,ObjectSelectHdl) );
172 	aWndRight.SetObjectSelectHdl( LINK(this,ScHFEditPage,ObjectSelectHdl) );
173 	aBtnText.SetAccessibleRelationMemberOf( &maFtCustomHF );
174 	aBtnFile.SetAccessibleRelationMemberOf( &maFtCustomHF );
175 	aBtnTable.SetAccessibleRelationMemberOf( &maFtCustomHF );
176 	aBtnPage.SetAccessibleRelationMemberOf( &maFtCustomHF );
177 	aBtnLastPage.SetAccessibleRelationMemberOf( &maFtCustomHF );
178 	aBtnDate.SetAccessibleRelationMemberOf( &maFtCustomHF );
179 	aBtnTime.SetAccessibleRelationMemberOf( &maFtCustomHF );
180 	//-----IAccessibility2 Implementation 2009
181 	FillCmdArr();
182 
183 	aWndLeft.GrabFocus();
184 
185 	InitPreDefinedList();
186 
187 	FreeResource();
188 }
189 	//IAccessibility2 Implementation 2009-----
190     IMPL_LINK( ScHFEditPage, ObjectSelectHdl, ScEditWindow*, pEdit )
191 	{
192 		(void)pEdit;
193 	    aBtnText.GrabFocus();
194 	    return NULL;
195 	}
196 	//-----IAccessibility2 Implementation 2009
197 
198 // -----------------------------------------------------------------------
199 
200 __EXPORT ScHFEditPage::~ScHFEditPage()
201 {
202 }
203 
204 void ScHFEditPage::SetNumType(SvxNumType eNumType)
205 {
206 	aWndLeft.SetNumType(eNumType);
207 	aWndCenter.SetNumType(eNumType);
208 	aWndRight.SetNumType(eNumType);
209 }
210 
211 // -----------------------------------------------------------------------
212 
213 #define IS_AVAILABLE(w)(rCoreSet.GetItemState( (w) ) >= SFX_ITEM_AVAILABLE)
214 
215 void __EXPORT ScHFEditPage::Reset( const SfxItemSet& rCoreSet )
216 {
217 	if ( IS_AVAILABLE( nWhich ) )
218 	{
219 		const ScPageHFItem& rItem = (const ScPageHFItem&)(rCoreSet.Get( nWhich ));
220 
221         if( const EditTextObject* pLeft = rItem.GetLeftArea() )
222             aWndLeft.SetText( *pLeft );
223         if( const EditTextObject* pCenter = rItem.GetCenterArea() )
224             aWndCenter.SetText( *pCenter );
225         if( const EditTextObject* pRight = rItem.GetRightArea() )
226             aWndRight.SetText( *pRight );
227 
228 		SetSelectDefinedList();
229 	}
230 }
231 
232 #undef IS_AVAILABLE
233 
234 // -----------------------------------------------------------------------
235 
236 sal_Bool __EXPORT ScHFEditPage::FillItemSet( SfxItemSet& rCoreSet )
237 {
238 	ScPageHFItem	aItem( nWhich );
239 	EditTextObject* pLeft	= aWndLeft	.CreateTextObject();
240 	EditTextObject* pCenter = aWndCenter.CreateTextObject();
241 	EditTextObject* pRight	= aWndRight .CreateTextObject();
242 
243 	aItem.SetLeftArea  ( *pLeft );
244 	aItem.SetCenterArea( *pCenter );
245 	aItem.SetRightArea ( *pRight );
246 	delete pLeft;
247 	delete pCenter;
248 	delete pRight;
249 
250 	rCoreSet.Put( aItem );
251 
252 	return sal_True;
253 }
254 
255 // -----------------------------------------------------------------------
256 
257 #define SET_CMD(i,id) \
258 	aCmd  = aDel;							\
259 	aCmd += ScGlobal::GetRscString( id );	\
260 	aCmd += aDel;							\
261 	aCmdArr[i] = aCmd;
262 
263 // -----------------------------------------------------------------------
264 
265 void ScHFEditPage::FillCmdArr()
266 {
267 	String aDel( ScGlobal::GetRscString( STR_HFCMD_DELIMITER ) );
268 	String aCmd;
269 
270 	SET_CMD( 0, STR_HFCMD_PAGE )
271 	SET_CMD( 1, STR_HFCMD_PAGES )
272 	SET_CMD( 2, STR_HFCMD_DATE )
273 	SET_CMD( 3, STR_HFCMD_TIME )
274 	SET_CMD( 4, STR_HFCMD_FILE )
275 	SET_CMD( 5, STR_HFCMD_TABLE )
276 }
277 
278 #undef SET_CMD
279 
280 void ScHFEditPage::InitPreDefinedList()
281 {
282     SvtUserOptions aUserOpt;
283 
284     Color* pTxtColour = NULL;
285     Color* pFldColour = NULL;
286 
287     // Get the all field values at the outset.
288     String aPageFieldValue(aWndLeft.GetEditEngine()->CalcFieldValue(SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD), 0,0, pTxtColour, pFldColour));
289     String aSheetFieldValue(aWndLeft.GetEditEngine()->CalcFieldValue(SvxFieldItem(SvxTableField(), EE_FEATURE_FIELD), 0,0, pTxtColour, pFldColour));
290     String aFileFieldValue(aWndLeft.GetEditEngine()->CalcFieldValue(SvxFieldItem(SvxFileField(), EE_FEATURE_FIELD), 0,0, pTxtColour, pFldColour));
291     String aExtFileFieldValue(aWndLeft.GetEditEngine()->CalcFieldValue(SvxFieldItem(SvxExtFileField(), EE_FEATURE_FIELD), 0,0, pTxtColour, pFldColour));
292     String aDateFieldValue(aWndLeft.GetEditEngine()->CalcFieldValue(SvxFieldItem(SvxDateField(), EE_FEATURE_FIELD), 0,0, pTxtColour, pFldColour));
293 
294     maLbDefined.Clear();
295 
296     maLbDefined.InsertEntry( ScGlobal::GetRscString( STR_HF_NONE_IN_BRACKETS ));
297 
298     String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
299     aPageEntry += ' ';
300     aPageEntry += aPageFieldValue;
301     maLbDefined.InsertEntry(aPageEntry);
302 
303     String aPageOfEntry(aPageEntry);
304     aPageOfEntry += ' ';
305     aPageOfEntry += ScGlobal::GetRscString( STR_HF_OF_QUESTION );
306     maLbDefined.InsertEntry( aPageOfEntry);
307 
308     maLbDefined.InsertEntry(aSheetFieldValue);
309 
310     String aConfidentialEntry(aUserOpt.GetCompany());
311     aConfidentialEntry += ' ';
312     aConfidentialEntry += ScGlobal::GetRscString( STR_HF_CONFIDENTIAL );
313     aConfidentialEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
314     aConfidentialEntry += aDateFieldValue;
315     aConfidentialEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
316     aConfidentialEntry += aPageEntry;
317     maLbDefined.InsertEntry( aConfidentialEntry);
318 
319     String aFileNamePageEntry(aFileFieldValue);
320     aFileNamePageEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
321     aFileNamePageEntry += aPageEntry;
322     maLbDefined.InsertEntry( aFileNamePageEntry);
323 
324     maLbDefined.InsertEntry( aExtFileFieldValue);
325 
326     String aPageSheetNameEntry(aPageEntry);
327     aPageSheetNameEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
328     aPageSheetNameEntry += aSheetFieldValue;
329     maLbDefined.InsertEntry( aPageSheetNameEntry);
330 
331     String aPageFileNameEntry(aPageEntry);
332     aPageFileNameEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
333     aPageFileNameEntry += aFileFieldValue;
334     maLbDefined.InsertEntry( aPageFileNameEntry);
335 
336     String aPagePathNameEntry(aPageEntry);
337     aPagePathNameEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
338     aPagePathNameEntry += aExtFileFieldValue;
339     maLbDefined.InsertEntry( aPagePathNameEntry);
340 
341     String aUserNameEntry(aUserOpt.GetFirstName());
342     aUserNameEntry += ' ';
343     aUserNameEntry += (String)aUserOpt.GetLastName();
344     aUserNameEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
345     aUserNameEntry += aPageEntry;
346     aUserNameEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
347     aUserNameEntry += aDateFieldValue;
348     maLbDefined.InsertEntry( aUserNameEntry);
349 
350     String aCreatedByEntry(ScGlobal::GetRscString( STR_HF_CREATED_BY ) );
351     aCreatedByEntry += ' ';
352     aCreatedByEntry += (String)aUserOpt.GetFirstName();
353     aCreatedByEntry += ' ';
354     aCreatedByEntry += (String)aUserOpt.GetLastName();
355     aCreatedByEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
356     aCreatedByEntry += aDateFieldValue;
357     aCreatedByEntry.AppendAscii(RTL_CONSTASCII_STRINGPARAM(", "));
358     aCreatedByEntry += aPageEntry;
359     maLbDefined.InsertEntry( aCreatedByEntry);
360 }
361 
362 void ScHFEditPage::InsertToDefinedList()
363 {
364     sal_uInt16 nCount =  maLbDefined.GetEntryCount();
365     if(nCount == eEntryCount)
366     {
367         String aCustomizedEntry(ScGlobal::GetRscString( STR_HF_CUSTOMIZED ) );
368         maLbDefined.InsertEntry( aCustomizedEntry);
369         maLbDefined.SelectEntryPos(eEntryCount);
370     }
371 }
372 
373 void ScHFEditPage::RemoveFromDefinedList()
374 {
375     sal_uInt16 nCount =  maLbDefined.GetEntryCount();
376     if(nCount > eEntryCount )
377         maLbDefined.RemoveEntry( nCount-1);
378 }
379 
380 // determine if the header/footer exists in our predefined list and set select to it.
381 void ScHFEditPage::SetSelectDefinedList()
382 {
383     SvtUserOptions aUserOpt;
384 
385     // default to customized
386     ScHFEntryId eSelectEntry = eEntryCount;
387 
388     ::std::auto_ptr< EditTextObject > pLeftObj;
389     ::std::auto_ptr< EditTextObject > pCenterObj;
390     ::std::auto_ptr< EditTextObject > pRightObj;
391 
392     XubString aLeftEntry;
393     XubString aCenterEntry;
394     XubString aRightEntry;
395 
396     pLeftObj.reset(aWndLeft.GetEditEngine()->CreateTextObject());
397     pCenterObj.reset(aWndCenter.GetEditEngine()->CreateTextObject());
398     pRightObj.reset(aWndRight.GetEditEngine()->CreateTextObject());
399 
400     bool bFound = false;
401 
402     sal_uInt16 i;
403     sal_uInt16 nCount =  maLbDefined.GetEntryCount();
404     for(i = 0; i < nCount && !bFound; i++)
405     {
406         switch(static_cast<ScHFEntryId>(i))
407         {
408             case eNoneEntry:
409             {
410                 aLeftEntry = pLeftObj->GetText(0);
411                 aCenterEntry = pCenterObj->GetText(0);
412                 aRightEntry = pRightObj->GetText(0);
413                 if(aLeftEntry == EMPTY_STRING && aCenterEntry == EMPTY_STRING
414                     && aRightEntry == EMPTY_STRING)
415                 {
416                     eSelectEntry = eNoneEntry;
417                     bFound = true;
418                 }
419             }
420             break;
421 
422             case ePageEntry:
423             {
424                 aLeftEntry = pLeftObj->GetText(0);
425                 aRightEntry = pRightObj->GetText(0);
426                 if(aLeftEntry == EMPTY_STRING && aRightEntry == EMPTY_STRING)
427                 {
428                     if(IsPageEntry(aWndCenter.GetEditEngine(), pCenterObj.get()))
429                     {
430                         eSelectEntry = ePageEntry;
431                         bFound = true;
432                     }
433                 }
434             }
435             break;
436 
437 
438             //TODO
439             case ePagesEntry:
440             {
441             }
442             break;
443 
444             case eSheetEntry:
445             {
446                 aLeftEntry = pLeftObj->GetText(0);
447                 aRightEntry = pRightObj->GetText(0);
448                 if(aLeftEntry == EMPTY_STRING && aRightEntry == EMPTY_STRING)
449                 {
450                     if(pCenterObj->IsFieldObject())
451                     {
452                         const SvxFieldItem* pFieldItem = pCenterObj->GetField();
453                         if(pFieldItem)
454                         {
455                             const SvxFieldData* pField = pFieldItem->GetField();
456                             if(pField && pField->ISA(SvxTableField))
457                             {
458                                 eSelectEntry = eSheetEntry;
459                                 bFound = true;
460                             }
461                         }
462                     }
463                 }
464             }
465             break;
466 
467             case eConfidentialEntry:
468             {
469                 if(IsDateEntry(pCenterObj.get()) && IsPageEntry(aWndRight.GetEditEngine(), pRightObj.get()))
470                 {
471                     String aConfidentialEntry(aUserOpt.GetCompany());
472                     aConfidentialEntry += ' ';
473                     aConfidentialEntry += ScGlobal::GetRscString( STR_HF_CONFIDENTIAL );
474                     if(aConfidentialEntry == aWndLeft.GetEditEngine()->GetText(0))
475                     {
476                         eSelectEntry = eConfidentialEntry;
477                         bFound = true;
478                     }
479                 }
480             }
481             break;
482 
483             //TODO
484             case eFileNamePageEntry:
485             {
486             }
487             break;
488 
489             case eExtFileNameEntry:
490             {
491                 aLeftEntry = pLeftObj->GetText(0);
492                 aRightEntry = pRightObj->GetText(0);
493                 if(IsExtFileNameEntry(pCenterObj.get()) && aLeftEntry == EMPTY_STRING
494                     && aRightEntry == EMPTY_STRING)
495                 {
496                     eSelectEntry = eExtFileNameEntry;
497                     bFound = true;
498                 }
499             }
500             break;
501 
502             //TODO
503             case ePageSheetEntry:
504             {
505             }
506             break;
507 
508             //TODO
509             case ePageFileNameEntry:
510             {
511             }
512             break;
513 
514             case ePageExtFileNameEntry:
515             {
516                 aLeftEntry = pLeftObj->GetText(0);
517                 if(IsPageEntry(aWndCenter.GetEditEngine(), pCenterObj.get()) &&
518                     IsExtFileNameEntry(pRightObj.get()) && aLeftEntry == EMPTY_STRING)
519                 {
520                     eSelectEntry = ePageExtFileNameEntry;
521                     bFound = true;
522                 }
523             }
524             break;
525 
526             case eUserNameEntry:
527             {
528                 if(IsDateEntry(pRightObj.get()) && IsPageEntry(aWndCenter.GetEditEngine(), pCenterObj.get()))
529                 {
530                     String aUserNameEntry(aUserOpt.GetFirstName());
531                     aUserNameEntry += ' ';
532                     aUserNameEntry += (String)aUserOpt.GetLastName();
533                     if(aUserNameEntry == aWndLeft.GetEditEngine()->GetText(0))
534                     {
535                         eSelectEntry = eUserNameEntry;
536                         bFound = true;
537                     }
538                 }
539             }
540             break;
541 
542             case eCreatedByEntry:
543             {
544                 if(IsDateEntry(pCenterObj.get()) && IsPageEntry(aWndRight.GetEditEngine(), pRightObj.get()))
545                 {
546                     String aCreatedByEntry(ScGlobal::GetRscString( STR_HF_CREATED_BY ) );
547                     aCreatedByEntry += ' ';
548                     aCreatedByEntry += (String)aUserOpt.GetFirstName();
549                     aCreatedByEntry += ' ';
550                     aCreatedByEntry += (String)aUserOpt.GetLastName();
551                     if(aCreatedByEntry == aWndLeft.GetEditEngine()->GetText(0))
552                     {
553                         eSelectEntry = eCreatedByEntry;
554                         bFound = true;
555                     }
556                 }
557             }
558             break;
559 
560             default:
561             {
562                 // added to avoid warnings
563             }
564         }
565     }
566 
567     if(eSelectEntry == eEntryCount)
568         InsertToDefinedList();
569 
570     maLbDefined.SelectEntryPos( sal::static_int_cast<sal_uInt16>( eSelectEntry ) );
571 }
572 
573 bool ScHFEditPage::IsPageEntry(EditEngine*pEngine, EditTextObject* pTextObj)
574 {
575     if(!pEngine && !pTextObj)
576         return false;
577 
578     bool bReturn = false;
579 
580     if(!pTextObj->IsFieldObject())
581     {
582         SvUShorts aPosList;
583         pEngine->GetPortions(0,aPosList);
584         if(aPosList.Count() == 2)
585         {
586             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
587             aPageEntry += ' ';
588             ESelection aSel(0,0,0,0);
589             aSel.nEndPos = aPageEntry.Len();
590             if(aPageEntry == pEngine->GetText(aSel))
591             {
592                 aSel.nStartPos = aSel.nEndPos;
593                 aSel.nEndPos++;
594                 ::std::auto_ptr< EditTextObject > pPageObj;
595                 pPageObj.reset(pEngine->CreateTextObject(aSel));
596                 if(pPageObj.get() && pPageObj->IsFieldObject() )
597                 {
598                     const SvxFieldItem* pFieldItem = pPageObj->GetField();
599                     if(pFieldItem)
600                     {
601                         const SvxFieldData* pField = pFieldItem->GetField();
602                         if(pField && pField->ISA(SvxPageField))
603                             bReturn = true;
604                     }
605                 }
606             }
607         }
608     }
609     return bReturn;
610 }
611 
612 bool ScHFEditPage::IsDateEntry(EditTextObject* pTextObj)
613 {
614     if(!pTextObj)
615         return false;
616 
617     bool bReturn = false;
618     if(pTextObj->IsFieldObject())
619     {
620         const SvxFieldItem* pFieldItem = pTextObj->GetField();
621         if(pFieldItem)
622         {
623             const SvxFieldData* pField = pFieldItem->GetField();
624             if(pField && pField->ISA(SvxDateField))
625                 bReturn = true;
626         }
627     }
628     return bReturn;
629 }
630 
631 bool ScHFEditPage::IsExtFileNameEntry(EditTextObject* pTextObj)
632 {
633     if(!pTextObj)
634         return false;
635     bool bReturn = false;
636     if(pTextObj->IsFieldObject())
637     {
638         const SvxFieldItem* pFieldItem = pTextObj->GetField();
639         if(pFieldItem)
640 	{
641             const SvxFieldData* pField = pFieldItem->GetField();
642             if(pField && pField->ISA(SvxExtFileField))
643                 bReturn = true;
644         }
645     }
646     return bReturn;
647 }
648 
649 void ScHFEditPage::ProcessDefinedListSel(ScHFEntryId eSel, bool bTravelling)
650 {
651     SvtUserOptions aUserOpt;
652     ::std::auto_ptr< EditTextObject > pTextObj;
653 
654     switch(eSel)
655     {
656         case eNoneEntry:
657             ClearTextAreas();
658             if(!bTravelling)
659                 aWndLeft.GrabFocus();
660         break;
661 
662         case ePageEntry:
663         {
664             ClearTextAreas();
665             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
666             aPageEntry += ' ';
667             aWndCenter.GetEditEngine()->SetText(aPageEntry);
668             aWndCenter.InsertField( SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD) );
669             if(!bTravelling)
670                 aWndCenter.GrabFocus();
671         }
672         break;
673 
674         case ePagesEntry:
675         {
676             ClearTextAreas();
677             ESelection aSel(0,0,0,0);
678             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
679             aPageEntry += ' ';
680             aWndCenter.GetEditEngine()->SetText(aPageEntry);
681             aSel.nEndPos = aPageEntry.Len();
682             aWndCenter.GetEditEngine()->QuickInsertField(SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
683             ++aSel.nEndPos;
684             String aPageOfEntry = ' ';
685             aPageOfEntry += ScGlobal::GetRscString( STR_HF_OF );
686             aPageOfEntry += ' ';
687             aWndCenter.GetEditEngine()->QuickInsertText(aPageOfEntry,ESelection(aSel.nEndPara,aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
688             aSel.nEndPos = sal::static_int_cast<xub_StrLen>( aSel.nEndPos + aPageOfEntry.Len() );
689             aWndCenter.GetEditEngine()->QuickInsertField(SvxFieldItem(SvxPagesField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara,aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
690             pTextObj.reset(aWndCenter.GetEditEngine()->CreateTextObject());
691             aWndCenter.SetText(*pTextObj);
692             XubString aEntry(pTextObj.get()->GetText(0));
693             if(!bTravelling)
694                 aWndCenter.GrabFocus();
695         }
696         break;
697 
698         case eSheetEntry:
699             ClearTextAreas();
700             aWndCenter.InsertField( SvxFieldItem(SvxTableField(), EE_FEATURE_FIELD) );
701             if(!bTravelling)
702                 aWndCenter.GrabFocus();
703         break;
704 
705         case eConfidentialEntry:
706         {
707             ClearTextAreas();
708             String aConfidentialEntry(aUserOpt.GetCompany());
709             aConfidentialEntry += ' ';
710             aConfidentialEntry += ScGlobal::GetRscString( STR_HF_CONFIDENTIAL );
711             aWndLeft.GetEditEngine()->SetText(aConfidentialEntry);
712             aWndCenter.InsertField( SvxFieldItem(SvxDateField(Date(),SVXDATETYPE_VAR), EE_FEATURE_FIELD) );
713             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
714             aPageEntry += ' ';
715             aWndRight.GetEditEngine()->SetText(aPageEntry);
716             aWndRight.InsertField( SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD) );
717             if(!bTravelling)
718                 aWndRight.GrabFocus();
719         }
720         break;
721 
722         case eFileNamePageEntry:
723         {
724             ClearTextAreas();
725             ESelection aSel(0,0,0,0);
726             aWndCenter.GetEditEngine()->QuickInsertField(SvxFieldItem( SvxFileField(), EE_FEATURE_FIELD ), aSel );
727             ++aSel.nEndPos;
728             String aPageEntry(RTL_CONSTASCII_STRINGPARAM(", "));
729             aPageEntry += ScGlobal::GetRscString( STR_PAGE ) ;
730             aPageEntry += ' ';
731             aWndCenter.GetEditEngine()->QuickInsertText(aPageEntry, ESelection(aSel.nEndPara,aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
732             aSel.nStartPos = aSel.nEndPos;
733             aSel.nEndPos = sal::static_int_cast<xub_StrLen>( aSel.nEndPos + aPageEntry.Len() );
734             aWndCenter.GetEditEngine()->QuickInsertField(SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara,aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
735             pTextObj.reset(aWndCenter.GetEditEngine()->CreateTextObject());
736             aWndCenter.SetText(*pTextObj);
737             XubString aEntry2(pTextObj.get()->GetText(0));
738             if(!bTravelling)
739                 aWndCenter.GrabFocus();
740         }
741         break;
742 
743         case eExtFileNameEntry:
744             ClearTextAreas();
745             aWndCenter.InsertField( SvxFieldItem( SvxExtFileField(
746                 EMPTY_STRING, SVXFILETYPE_VAR, SVXFILEFORMAT_FULLPATH ), EE_FEATURE_FIELD ) );
747             if(!bTravelling)
748                 aWndCenter.GrabFocus();
749         break;
750 
751         case ePageSheetEntry:
752         {
753             ClearTextAreas();
754             ESelection aSel(0,0,0,0);
755             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
756             aPageEntry += ' ';
757             aWndCenter.GetEditEngine()->SetText(aPageEntry);
758             aSel.nEndPos = aPageEntry.Len();
759             aWndCenter.GetEditEngine()->QuickInsertField(SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
760             ++aSel.nEndPos;
761             String aCommaSpace(RTL_CONSTASCII_STRINGPARAM(", "));
762             aWndCenter.GetEditEngine()->QuickInsertText(aCommaSpace,ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
763             aSel.nEndPos = sal::static_int_cast<xub_StrLen>( aSel.nEndPos + aCommaSpace.Len() );
764             aWndCenter.GetEditEngine()->QuickInsertField( SvxFieldItem(SvxTableField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
765             pTextObj.reset(aWndCenter.GetEditEngine()->CreateTextObject());
766             aWndCenter.SetText(*pTextObj);
767             if(!bTravelling)
768                 aWndCenter.GrabFocus();
769         }
770         break;
771 
772         case ePageFileNameEntry:
773         {
774             ClearTextAreas();
775             ESelection aSel(0,0,0,0);
776             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
777             aPageEntry += ' ';
778             aWndCenter.GetEditEngine()->SetText(aPageEntry);
779             aSel.nEndPos = aPageEntry.Len();
780             aWndCenter.GetEditEngine()->QuickInsertField(SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
781             ++aSel.nEndPos;
782             String aCommaSpace(RTL_CONSTASCII_STRINGPARAM(", "));
783             aWndCenter.GetEditEngine()->QuickInsertText(aCommaSpace,ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
784             aSel.nEndPos = sal::static_int_cast<xub_StrLen>( aSel.nEndPos + aCommaSpace.Len() );
785             aWndCenter.GetEditEngine()->QuickInsertField( SvxFieldItem(SvxFileField(), EE_FEATURE_FIELD), ESelection(aSel.nEndPara, aSel.nEndPos, aSel.nEndPara, aSel.nEndPos));
786             pTextObj.reset(aWndCenter.GetEditEngine()->CreateTextObject());
787             aWndCenter.SetText(*pTextObj);
788             if(!bTravelling)
789                 aWndCenter.GrabFocus();
790         }
791         break;
792 
793         case ePageExtFileNameEntry:
794         {
795             ClearTextAreas();
796             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
797             aPageEntry += ' ';
798             aWndCenter.GetEditEngine()->SetText(aPageEntry);
799             aWndCenter.InsertField( SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD) );
800             aWndRight.InsertField( SvxFieldItem( SvxExtFileField(
801                 EMPTY_STRING, SVXFILETYPE_VAR, SVXFILEFORMAT_FULLPATH ), EE_FEATURE_FIELD ) );
802             if(!bTravelling)
803                 aWndRight.GrabFocus();
804         }
805         break;
806 
807         case eUserNameEntry:
808         {
809             ClearTextAreas();
810             String aUserNameEntry(aUserOpt.GetFirstName());
811             aUserNameEntry += ' ';
812             aUserNameEntry += (String)aUserOpt.GetLastName();
813             aWndLeft.GetEditEngine()->SetText(aUserNameEntry);
814             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
815             aPageEntry += ' ';
816             aWndCenter.GetEditEngine()->SetText(aPageEntry);
817             aWndCenter.InsertField( SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD) );
818             aWndRight.InsertField( SvxFieldItem(SvxDateField(Date(),SVXDATETYPE_VAR), EE_FEATURE_FIELD) );
819             if(!bTravelling)
820                 aWndRight.GrabFocus();
821         }
822         break;
823 
824         case eCreatedByEntry:
825         {
826             ClearTextAreas();
827             String aCreatedByEntry(ScGlobal::GetRscString( STR_HF_CREATED_BY ) );
828             aCreatedByEntry += ' ';
829             aCreatedByEntry += (String)aUserOpt.GetFirstName();
830             aCreatedByEntry += ' ';
831             aCreatedByEntry += (String)aUserOpt.GetLastName();
832             aWndLeft.GetEditEngine()->SetText(aCreatedByEntry);
833             aWndCenter.InsertField( SvxFieldItem(SvxDateField(Date(),SVXDATETYPE_VAR), EE_FEATURE_FIELD) );
834             String aPageEntry(ScGlobal::GetRscString( STR_PAGE ) );
835             aPageEntry += ' ';
836             aWndRight.GetEditEngine()->SetText(aPageEntry);
837             aWndRight.InsertField( SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD) );
838             if(!bTravelling)
839                 aWndRight.GrabFocus();
840         }
841         break;
842 
843         default :
844             break;
845     }
846 }
847 
848 void ScHFEditPage::ClearTextAreas()
849 {
850     aWndLeft.GetEditEngine()->SetText(EMPTY_STRING);
851     aWndLeft.Invalidate();
852     aWndCenter.GetEditEngine()->SetText(EMPTY_STRING);
853     aWndCenter.Invalidate();
854     aWndRight.GetEditEngine()->SetText(EMPTY_STRING);
855     aWndRight.Invalidate();
856 }
857 
858 //-----------------------------------------------------------------------
859 // Handler:
860 //-----------------------------------------------------------------------
861 
862 IMPL_LINK( ScHFEditPage, ListHdl_Impl, ListBox*, pList )
863 {
864     if ( pList && pList == &maLbDefined )
865     {
866         ScHFEntryId eSel = static_cast<ScHFEntryId>(maLbDefined.GetSelectEntryPos());
867         if(!maLbDefined.IsTravelSelect())
868         {
869             ProcessDefinedListSel(eSel);
870 
871             // check if we need to remove the customized entry.
872             if(eSel < eEntryCount)
873                 RemoveFromDefinedList();
874         }
875         else
876         {
877             ProcessDefinedListSel(eSel, true);
878         }
879     }
880     return 0;
881 }
882 
883 IMPL_LINK( ScHFEditPage, ClickHdl, ImageButton*, pBtn )
884 {
885 	pActiveEdWnd = ::GetScEditWindow(); //CHINA001
886 	if ( !pActiveEdWnd )
887 		return 0;
888 
889 	if ( pBtn == &aBtnText )
890 	{
891 		pActiveEdWnd->SetCharAttriutes();
892 	}
893 	else
894 	{
895 		if ( pBtn == &aBtnPage )
896             pActiveEdWnd->InsertField( SvxFieldItem(SvxPageField(), EE_FEATURE_FIELD) );
897 		else if ( pBtn == &aBtnLastPage )
898             pActiveEdWnd->InsertField( SvxFieldItem(SvxPagesField(), EE_FEATURE_FIELD) );
899 		else if ( pBtn == &aBtnDate )
900             pActiveEdWnd->InsertField( SvxFieldItem(SvxDateField(Date(),SVXDATETYPE_VAR), EE_FEATURE_FIELD) );
901 		else if ( pBtn == &aBtnTime )
902             pActiveEdWnd->InsertField( SvxFieldItem(SvxTimeField(), EE_FEATURE_FIELD) );
903 		else if ( pBtn == &aBtnFile )
904 		{
905             pActiveEdWnd->InsertField( SvxFieldItem( SvxFileField(), EE_FEATURE_FIELD ) );
906 		}
907 		else if ( pBtn == &aBtnTable )
908             pActiveEdWnd->InsertField( SvxFieldItem(SvxTableField(), EE_FEATURE_FIELD) );
909 	}
910 	InsertToDefinedList();
911 	pActiveEdWnd->GrabFocus();
912 
913 	return 0;
914 }
915 
916 IMPL_LINK( ScHFEditPage, MenuHdl, ScExtIButton*, pBtn )
917 {
918 	pActiveEdWnd = ::GetScEditWindow(); //CHINA001
919 	if ( !pActiveEdWnd )
920 		return 0;
921 
922 	if(pBtn!=NULL)
923 	{
924 		switch(pBtn->GetSelected())
925 		{
926 			case FILE_COMMAND_TITEL:
927                 pActiveEdWnd->InsertField( SvxFieldItem( SvxFileField(), EE_FEATURE_FIELD ) );
928 				break;
929 			case FILE_COMMAND_FILENAME:
930 				pActiveEdWnd->InsertField( SvxFieldItem( SvxExtFileField(
931                         EMPTY_STRING, SVXFILETYPE_VAR, SVXFILEFORMAT_NAME_EXT ), EE_FEATURE_FIELD ) );
932 				break;
933 			case FILE_COMMAND_PATH:
934 				pActiveEdWnd->InsertField( SvxFieldItem( SvxExtFileField(
935                         EMPTY_STRING, SVXFILETYPE_VAR, SVXFILEFORMAT_FULLPATH ), EE_FEATURE_FIELD ) );
936 				break;
937 		}
938 	}
939 	return 0;
940 }
941 
942 //========================================================================
943 // class ScRightHeaderEditPage
944 //========================================================================
945 
946 ScRightHeaderEditPage::ScRightHeaderEditPage( Window* pParent, const SfxItemSet& rCoreSet )
947 	: ScHFEditPage( pParent, RID_SCPAGE_HFED_HR, rCoreSet,
948 					rCoreSet.GetPool()->GetWhich(SID_SCATTR_PAGE_HEADERRIGHT ), true )
949 	{}
950 
951 // -----------------------------------------------------------------------
952 
953 sal_uInt16* __EXPORT ScRightHeaderEditPage::GetRanges()
954 	{ return pPageRightHeaderRanges; }
955 
956 // -----------------------------------------------------------------------
957 
958 SfxTabPage* __EXPORT ScRightHeaderEditPage::Create( Window* pParent, const SfxItemSet& rCoreSet )
959 	{ return ( new ScRightHeaderEditPage( pParent, rCoreSet ) ); };
960 
961 
962 //========================================================================
963 // class ScLeftHeaderEditPage
964 //========================================================================
965 
966 ScLeftHeaderEditPage::ScLeftHeaderEditPage( Window* pParent, const SfxItemSet& rCoreSet )
967 	: ScHFEditPage( pParent, RID_SCPAGE_HFED_HL, rCoreSet,
968 					rCoreSet.GetPool()->GetWhich(SID_SCATTR_PAGE_HEADERLEFT ), true )
969 	{}
970 
971 // -----------------------------------------------------------------------
972 
973 sal_uInt16* __EXPORT ScLeftHeaderEditPage::GetRanges()
974 	{ return pPageLeftHeaderRanges; }
975 
976 // -----------------------------------------------------------------------
977 
978 SfxTabPage* __EXPORT ScLeftHeaderEditPage::Create( Window* pParent, const SfxItemSet& rCoreSet )
979 	{ return ( new ScLeftHeaderEditPage( pParent, rCoreSet ) ); };
980 
981 //========================================================================
982 // class ScRightFooterEditPage
983 //========================================================================
984 
985 ScRightFooterEditPage::ScRightFooterEditPage( Window* pParent, const SfxItemSet& rCoreSet )
986 	: ScHFEditPage( pParent, RID_SCPAGE_HFED_FR, rCoreSet,
987 					rCoreSet.GetPool()->GetWhich(SID_SCATTR_PAGE_FOOTERRIGHT ), false )
988 	{}
989 
990 // -----------------------------------------------------------------------
991 
992 sal_uInt16* __EXPORT ScRightFooterEditPage::GetRanges()
993 	{ return pPageRightFooterRanges; }
994 
995 // -----------------------------------------------------------------------
996 
997 SfxTabPage* __EXPORT ScRightFooterEditPage::Create( Window* pParent, const SfxItemSet& rCoreSet )
998 	{ return ( new ScRightFooterEditPage( pParent, rCoreSet ) ); };
999 
1000 //========================================================================
1001 // class ScLeftFooterEditPage
1002 //========================================================================
1003 
1004 ScLeftFooterEditPage::ScLeftFooterEditPage( Window* pParent, const SfxItemSet& rCoreSet )
1005 	: ScHFEditPage( pParent, RID_SCPAGE_HFED_FL, rCoreSet,
1006 					rCoreSet.GetPool()->GetWhich(SID_SCATTR_PAGE_FOOTERLEFT ), false )
1007 	{}
1008 
1009 // -----------------------------------------------------------------------
1010 
1011 sal_uInt16* __EXPORT ScLeftFooterEditPage::GetRanges()
1012 	{ return pPageLeftFooterRanges; }
1013 
1014 // -----------------------------------------------------------------------
1015 
1016 SfxTabPage* __EXPORT ScLeftFooterEditPage::Create( Window* pParent, const SfxItemSet& rCoreSet )
1017 	{ return ( new ScLeftFooterEditPage( pParent, rCoreSet ) ); };
1018