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