bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / fldui / fldvar.cxx
blob70f80d2175e6824d2cae7497e13215dd58db77e3
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <swtypes.hxx>
21 #include <sfx2/linkmgr.hxx>
22 #include <usrfld.hxx>
23 #include <docufld.hxx>
24 #include <expfld.hxx>
25 #include <ddefld.hxx>
26 #include <view.hxx>
27 #include <wrtsh.hxx>
28 #include <doc.hxx>
29 #include <docary.hxx>
30 #include <swmodule.hxx>
31 #include <fldvar.hxx>
32 #include <calc.hxx>
33 #include <svl/zformat.hxx>
34 #include <globals.hrc>
35 #include <fldui.hrc>
36 #include <fldtdlg.hrc>
38 #define USER_DATA_VERSION_1 "1"
39 #define USER_DATA_VERSION USER_DATA_VERSION_1
41 SwFldVarPage::SwFldVarPage(Window* pParent, const SfxItemSet& rCoreSet ) :
42 SwFldPage ( pParent, SW_RES( TP_FLD_VAR ), rCoreSet ),
43 aTypeFT (this, SW_RES(FT_VARTYPE)),
44 aTypeLB (this, SW_RES(LB_VARTYPE)),
45 aSelectionFT (this, SW_RES(FT_VARSELECTION)),
46 aSelectionLB (this, SW_RES(LB_VARSELECTION)),
47 aNameFT (this, SW_RES(FT_VARNAME)),
48 aNameED (this, SW_RES(ED_VARNAME)),
49 aValueFT (this, SW_RES(FT_VARVALUE)),
50 aValueED (this, SW_RES(ED_VARVALUE)),
51 aFormatFT (this, SW_RES(FT_VARFORMAT)),
52 aNumFormatLB (this, SW_RES(LB_VARNUMFORMAT)),
53 aFormatLB (this, SW_RES(LB_VARFORMAT)),
54 aChapterHeaderFT(this, SW_RES(FT_VARCHAPTERHEADER)),
55 aChapterLevelFT (this, SW_RES(FT_VARCHAPTERLEVEL)),
56 aChapterLevelLB (this, SW_RES(LB_VARCHAPTERLEVEL)),
57 aInvisibleCB (this, SW_RES(CB_VARINVISIBLE)),
58 aSeparatorFT (this, SW_RES(FT_VARSEPARATOR)),
59 aSeparatorED (this, SW_RES(ED_VARSEPARATOR)),
60 aNewDelTBX (this, SW_RES(TBX_VARNEWDEL)),
61 bInit (sal_True)
63 FreeResource();
65 aNewDelTBX.SetSizePixel( aNewDelTBX.CalcWindowSizePixel() );
67 long nDelta = ( aValueED.GetSizePixel().Height() -
68 aNewDelTBX.GetSizePixel().Height() ) / 2;
69 Point aNewPnt = aNewDelTBX.GetPosPixel();
70 aNewPnt.Y() += nDelta;
71 aNewDelTBX.SetPosPixel( aNewPnt );
73 sOldValueFT = aValueFT.GetText();
74 sOldNameFT = aNameFT.GetText();
76 for (sal_uInt16 i = 1; i <= MAXLEVEL; i++)
77 aChapterLevelLB.InsertEntry(OUString::number(i));
79 aChapterLevelLB.SelectEntryPos(0);
80 //enable 'active' language selection
81 aNumFormatLB.SetShowLanguageControl(sal_True);
84 SwFldVarPage::~SwFldVarPage()
88 void SwFldVarPage::Reset(const SfxItemSet& )
90 SavePos(&aTypeLB);
92 Init(); // general initialisation
94 aTypeLB.SetUpdateMode(sal_False);
95 aTypeLB.Clear();
97 sal_uInt16 nPos, nTypeId;
99 if (!IsFldEdit())
101 // initialise TypeListBox
102 const SwFldGroupRgn& rRg = GetFldMgr().GetGroupRange(IsFldDlgHtmlMode(), GetGroup());
104 for (short i = rRg.nStart; i < rRg.nEnd; ++i)
106 nTypeId = GetFldMgr().GetTypeId(i);
107 nPos = aTypeLB.InsertEntry(GetFldMgr().GetTypeStr(i));
108 aTypeLB.SetEntryData(nPos, reinterpret_cast<void*>(nTypeId));
111 else
113 SwField* pCurField = GetCurField();
114 nTypeId = pCurField->GetTypeId();
115 if (nTypeId == TYP_SETINPFLD)
116 nTypeId = TYP_INPUTFLD;
117 nPos = aTypeLB.InsertEntry(GetFldMgr().GetTypeStr(GetFldMgr().GetPos(nTypeId)));
118 aTypeLB.SetEntryData(nPos, reinterpret_cast<void*>(nTypeId));
119 aNumFormatLB.SetAutomaticLanguage(pCurField->IsAutomaticLanguage());
120 SwWrtShell *pSh = GetWrtShell();
121 if(!pSh)
122 pSh = ::GetActiveWrtShell();
123 if(pSh)
125 const SvNumberformat* pFormat = pSh->GetNumberFormatter()->GetEntry(pCurField->GetFormat());
126 if(pFormat)
127 aNumFormatLB.SetLanguage(pFormat->GetLanguage());
131 // select old Pos
132 RestorePos(&aTypeLB);
134 aTypeLB.SetDoubleClickHdl (LINK(this, SwFldVarPage, InsertHdl));
135 aTypeLB.SetSelectHdl (LINK(this, SwFldVarPage, TypeHdl));
136 aSelectionLB.SetSelectHdl (LINK(this, SwFldVarPage, SubTypeHdl));
137 aSelectionLB.SetDoubleClickHdl (LINK(this, SwFldVarPage, InsertHdl));
138 aFormatLB.SetDoubleClickHdl (LINK(this, SwFldVarPage, InsertHdl));
139 aNumFormatLB.SetDoubleClickHdl (LINK(this, SwFldVarPage, InsertHdl));
140 aNameED.SetModifyHdl (LINK(this, SwFldVarPage, ModifyHdl));
141 aValueED.SetModifyHdl (LINK(this, SwFldVarPage, ModifyHdl));
142 aNewDelTBX.SetClickHdl (LINK(this, SwFldVarPage, TBClickHdl));
143 aChapterLevelLB.SetSelectHdl (LINK(this, SwFldVarPage, ChapterHdl));
144 aSeparatorED.SetModifyHdl (LINK(this, SwFldVarPage, SeparatorHdl));
146 if( !IsRefresh() )
148 String sUserData = GetUserData();
149 if(!IsRefresh() && sUserData.GetToken(0, ';').EqualsIgnoreCaseAscii(USER_DATA_VERSION_1))
151 String sVal = sUserData.GetToken(1, ';');
152 sal_uInt16 nVal = (sal_uInt16)sVal.ToInt32();
153 if( USHRT_MAX != nVal )
155 for(sal_uInt16 i = 0; i < aTypeLB.GetEntryCount(); i++)
156 if(nVal == (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData(i))
158 aTypeLB.SelectEntryPos(i);
159 break;
164 TypeHdl(0);
166 aTypeLB.SetUpdateMode(sal_True);
168 if (IsFldEdit())
170 aSelectionLB.SaveValue();
171 aFormatLB.SaveValue();
172 nOldFormat = aNumFormatLB.GetFormat();
173 aNameED.SaveValue();
174 aValueED.SaveValue();
175 aInvisibleCB.SaveValue();
176 aChapterLevelLB.SaveValue();
177 aSeparatorED.SaveValue();
181 IMPL_LINK_NOARG(SwFldVarPage, TypeHdl)
183 // save old ListBoxPos
184 const sal_uInt16 nOld = GetTypeSel();
186 // current ListBoxPos
187 SetTypeSel(aTypeLB.GetSelectEntryPos());
189 if(GetTypeSel() == LISTBOX_ENTRY_NOTFOUND)
191 SetTypeSel(0);
192 aTypeLB.SelectEntryPos(0);
195 if (nOld != GetTypeSel() || nOld == LISTBOX_ENTRY_NOTFOUND)
197 bInit = sal_True;
198 if (nOld != LISTBOX_ENTRY_NOTFOUND)
200 aNameED.SetText(aEmptyStr);
201 aValueED.SetText(aEmptyStr);
204 aValueED.SetDropEnable(false);
205 UpdateSubType(); // initialise selection-listboxes
208 bInit = sal_False;
210 return 0;
213 IMPL_LINK( SwFldVarPage, SubTypeHdl, ListBox *, pBox )
215 sal_uInt16 nTypeId = (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData(GetTypeSel());
216 sal_uInt16 nSelPos = aSelectionLB.GetSelectEntryPos();
218 if (nSelPos != LISTBOX_ENTRY_NOTFOUND)
219 nSelPos = (sal_uInt16)(sal_uLong)aSelectionLB.GetEntryData(nSelPos);
221 if (IsFldEdit() && (!pBox || bInit))
223 if (nTypeId != TYP_FORMELFLD)
224 aNameED.SetText(GetFldMgr().GetCurFldPar1());
226 aValueED.SetText(GetFldMgr().GetCurFldPar2());
229 if (aNameFT.GetText() != OUString(sOldNameFT))
230 aNameFT.SetText(sOldNameFT);
231 if (aValueFT.GetText() != OUString(sOldValueFT))
232 aValueFT.SetText(sOldValueFT);
234 aNumFormatLB.SetUpdateMode(sal_False);
235 aFormatLB.SetUpdateMode(sal_False);
236 FillFormatLB(nTypeId);
238 sal_uInt16 nSize = aFormatLB.GetEntryCount();
240 sal_Bool bValue = sal_False, bName = sal_False, bNumFmt = sal_False,
241 bInvisible = sal_False, bSeparator = sal_False, bChapterLevel = sal_False;
242 sal_Bool bFormat = nSize != 0;
244 switch (nTypeId)
246 case TYP_USERFLD:
248 // change or create user type
249 SwUserFieldType* pType = (SwUserFieldType*)
250 GetFldMgr().GetFldType(RES_USERFLD, nSelPos);
252 if (pType)
254 if (!IsFldEdit())
256 if (pBox || (bInit && !IsRefresh())) // only when interacting via mouse
258 aNameED.SetText(pType->GetName());
260 if (pType->GetType() == UF_STRING)
262 aValueED.SetText(pType->GetContent());
263 aNumFormatLB.SelectEntryPos(0);
265 else
266 aValueED.SetText(pType->GetContent());
269 else
270 aValueED.SetText(pType->GetContent());
272 else
274 if (pBox) // only when interacting via mouse
276 aNameED.SetText(aEmptyStr);
277 aValueED.SetText(aEmptyStr);
280 bValue = bName = bNumFmt = bInvisible = sal_True;
282 aValueED.SetDropEnable(true);
283 break;
286 case TYP_SETFLD:
287 bValue = sal_True;
289 bNumFmt = bInvisible = sal_True;
291 if (!IsFldDlgHtmlMode())
292 bName = sal_True;
293 else
295 aNumFormatLB.Clear();
296 sal_uInt16 nPos = aNumFormatLB.InsertEntry(SW_RESSTR(FMT_SETVAR_TEXT), 0);
297 aNumFormatLB.SetEntryData(nPos, (void *)ULONG_MAX);
298 aNumFormatLB.SelectEntryPos(0);
300 // is there a corresponding SetField
301 if (IsFldEdit() || pBox) // only when interacting via mouse
303 if (nSelPos != LISTBOX_ENTRY_NOTFOUND)
305 String sName(aSelectionLB.GetSelectEntry());
306 aNameED.SetText(sName);
308 if (!IsFldDlgHtmlMode())
310 SwWrtShell *pSh = GetWrtShell();
311 if(!pSh)
312 pSh = ::GetActiveWrtShell();
313 if(pSh)
315 SwSetExpFieldType* pSetTyp = (SwSetExpFieldType*)
316 pSh->GetFldType(RES_SETEXPFLD, sName);
318 if (pSetTyp && pSetTyp->GetType() == nsSwGetSetExpType::GSE_STRING)
319 aNumFormatLB.SelectEntryPos(0); // textual
324 if (IsFldEdit())
326 // GetFormula leads to problems with date formats because
327 // only the numeric value without formating is returned.
328 // It must be used though because otherwise in GetPar2 only
329 // the value calculated by Kalkulator would be displayed
330 // (instead of test2 = test + 1)
331 aValueED.SetText(((SwSetExpField*)GetCurField())->GetFormula());
333 aValueED.SetDropEnable(true);
334 break;
336 case TYP_FORMELFLD:
338 bValue = sal_True;
339 bNumFmt = sal_True;
340 aValueFT.SetText(SW_RESSTR(STR_FORMULA));
341 aValueED.SetDropEnable(true);
343 break;
345 case TYP_GETFLD:
347 if (!IsFldEdit())
349 aNameED.SetText(aEmptyStr);
350 aValueED.SetText(aEmptyStr);
353 if (nSelPos != LISTBOX_ENTRY_NOTFOUND)
355 String sName(aSelectionLB.GetSelectEntry());
356 if (!IsFldEdit())
357 aNameED.SetText(sName);
359 // is there a corresponding SetField
360 SwWrtShell *pSh = GetWrtShell();
361 if(!pSh)
362 pSh = ::GetActiveWrtShell();
363 if(pSh)
365 SwSetExpFieldType* pSetTyp = (SwSetExpFieldType*)
366 pSh->GetFldType(RES_SETEXPFLD, sName);
368 if(pSetTyp)
370 if (pSetTyp->GetType() & nsSwGetSetExpType::GSE_STRING) // textual?
371 bFormat = sal_True;
372 else // numeric
373 bNumFmt = sal_True;
377 else
378 bFormat = sal_False;
380 EnableInsert(bFormat|bNumFmt);
382 break;
384 case TYP_INPUTFLD:
385 aValueFT.SetText(SW_RESSTR(STR_PROMPT));
387 if (nSelPos != LISTBOX_ENTRY_NOTFOUND)
389 bValue = bNumFmt = sal_True;
391 String sName;
393 sName = aSelectionLB.GetSelectEntry();
394 aNameED.SetText( sName );
396 // User- or SetField ?
397 sal_uInt16 nInpType = 0;
398 nInpType = static_cast< sal_uInt16 >(GetFldMgr().GetFldType(RES_USERFLD, sName) ? 0 : TYP_SETINPFLD);
400 if (nInpType) // SETEXPFLD
402 // is there a corresponding SetField
403 SwSetExpFieldType* pSetTyp = (SwSetExpFieldType*)
404 GetFldMgr().GetFldType(RES_SETEXPFLD, sName);
406 if(pSetTyp)
408 if (pSetTyp->GetType() == nsSwGetSetExpType::GSE_STRING) // textual?
410 aNumFormatLB.Clear();
412 sal_uInt16 nPos = aNumFormatLB.InsertEntry(SW_RESSTR(FMT_USERVAR_TEXT), 0);
413 aNumFormatLB.SetEntryData(nPos, (void *)ULONG_MAX);
414 aNumFormatLB.SelectEntryPos(0);
417 if (IsFldEdit() && (!pBox || bInit) )
418 aValueED.SetText(((SwSetExpField*)GetCurField())->GetPromptText());
420 else // USERFLD
421 bFormat = bNumFmt = sal_False;
423 break;
425 case TYP_DDEFLD:
426 aValueFT.SetText(SW_RESSTR(STR_DDE_CMD));
428 if (IsFldEdit() || pBox) // only when interacting via mouse
430 if (nSelPos != LISTBOX_ENTRY_NOTFOUND)
432 SwDDEFieldType* pType =
433 (SwDDEFieldType*) GetFldMgr().GetFldType(RES_DDEFLD, nSelPos);
435 if(pType)
437 aNameED.SetText(pType->GetName());
439 //JP 28.08.95: DDE-Topics/-Items can have blanks in their names!
440 // That's not considered here yet
441 String sCmd( pType->GetCmd() );
442 sal_uInt16 nTmpPos = sCmd.SearchAndReplace( sfx2::cTokenSeparator, ' ' );
443 sCmd.SearchAndReplace( sfx2::cTokenSeparator, ' ', nTmpPos );
445 aValueED.SetText( sCmd );
446 aFormatLB.SelectEntryPos(pType->GetType());
450 bName = bValue = sal_True;
451 break;
453 case TYP_SEQFLD:
455 bName = bValue = bSeparator = bChapterLevel = sal_True;
457 SwFieldType* pFldTyp;
458 if( IsFldEdit() )
459 pFldTyp = GetCurField()->GetTyp();
460 else
462 String sFldTypeName( aSelectionLB.GetEntry( nSelPos ));
463 if( sFldTypeName.Len() )
464 pFldTyp = GetFldMgr().GetFldType( RES_SETEXPFLD,
465 sFldTypeName );
466 else
467 pFldTyp = 0;
470 if( IsFldEdit() )
471 aValueED.SetText( ((SwSetExpField*)GetCurField())->
472 GetFormula() );
474 if( IsFldEdit() || pBox ) // only when interacting via mouse
475 aNameED.SetText( aSelectionLB.GetSelectEntry() );
477 if( pFldTyp )
479 sal_uInt8 nLevel = ((SwSetExpFieldType*)pFldTyp)->GetOutlineLvl();
480 if( 0x7f == nLevel )
481 aChapterLevelLB.SelectEntryPos( 0 );
482 else
483 aChapterLevelLB.SelectEntryPos( nLevel + 1 );
484 String sDelim = ((SwSetExpFieldType*)pFldTyp)->GetDelimiter();
485 aSeparatorED.SetText( sDelim );
486 ChapterHdl();
489 break;
491 case TYP_SETREFPAGEFLD:
493 bValue = sal_False;
494 aValueFT.SetText( SW_RESSTR( STR_OFFSET ));
496 if (IsFldEdit() || pBox) // only when interacting via mouse
497 aNameED.SetText(aEmptyStr);
499 if (nSelPos != 0 && nSelPos != LISTBOX_ENTRY_NOTFOUND)
501 bValue = sal_True; // SubType OFF - knows no Offset
502 if (IsFldEdit())
503 aValueED.SetText(OUString::number(((SwRefPageSetField*)GetCurField())->GetOffset()));
506 break;
508 case TYP_GETREFPAGEFLD:
509 aNameED.SetText(aEmptyStr);
510 aValueED.SetText(aEmptyStr);
511 break;
514 aNumFormatLB.Show(bNumFmt);
515 aFormatLB.Show(!bNumFmt);
517 if (IsFldEdit())
518 bName = sal_False;
520 aFormatLB.Enable(bFormat);
521 aFormatFT.Enable(bFormat|bNumFmt);
522 aNameFT.Enable(bName);
523 aNameED.Enable(bName);
524 aValueFT.Enable(bValue);
525 aValueED.Enable(bValue);
527 Size aSz(aFormatLB.GetSizePixel());
529 if (bChapterLevel)
530 aSz.Height() = aFormatLB.LogicToPixel(Size(1, 94), MAP_APPFONT).Height();
531 else
532 aSz.Height() = aFormatLB.LogicToPixel(Size(1, 123), MAP_APPFONT).Height();
534 aFormatLB.SetSizePixel(aSz);
536 aInvisibleCB.Show(!bSeparator);
537 aSeparatorFT.Show(bSeparator);
538 aSeparatorED.Show(bSeparator);
539 aChapterHeaderFT.Show(bChapterLevel);
540 aChapterLevelFT.Show(bChapterLevel);
541 aChapterLevelLB.Show(bChapterLevel);
542 aInvisibleCB.Enable(bInvisible);
544 ModifyHdl(); // apply/insert/delete status update
546 aNumFormatLB.SetUpdateMode(sal_True);
547 aFormatLB.SetUpdateMode(sal_True);
549 if(aSelectionLB.IsCallAddSelection())
551 nTypeId = (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData(GetTypeSel());
553 switch (nTypeId)
555 case TYP_FORMELFLD:
557 nSelPos = aSelectionLB.GetSelectEntryPos();
559 if (nSelPos != LISTBOX_ENTRY_NOTFOUND)
560 nSelPos = (sal_uInt16)(sal_uLong)aSelectionLB.GetEntryData(nSelPos);
562 if (nSelPos != LISTBOX_ENTRY_NOTFOUND && pBox && !bInit)
564 aValueED.ReplaceSelected(aSelectionLB.GetSelectEntry());
565 ModifyHdl();
568 break;
570 aSelectionLB.ResetCallAddSelection();
572 return 0;
575 /*--------------------------------------------------------------------
576 Description: renew types in SelectionBox
577 --------------------------------------------------------------------*/
578 void SwFldVarPage::UpdateSubType()
580 String sOldSel;
581 sal_uInt16 nTypeId = (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData(GetTypeSel());
583 SetSelectionSel(aSelectionLB.GetSelectEntryPos());
584 if(GetSelectionSel() != LISTBOX_ENTRY_NOTFOUND)
585 sOldSel = aSelectionLB.GetEntry(GetSelectionSel());
587 // fill Selection-Listbox
588 aSelectionLB.SetUpdateMode(sal_False);
589 aSelectionLB.Clear();
591 std::vector<OUString> aList;
592 GetFldMgr().GetSubTypes(nTypeId, aList);
593 size_t nCount = aList.size();
594 size_t nPos;
596 for(size_t i = 0; i < nCount; ++i)
598 if (nTypeId != TYP_INPUTFLD || i)
600 if (!IsFldEdit())
602 nPos = aSelectionLB.InsertEntry(aList[i]);
603 aSelectionLB.SetEntryData(nPos, reinterpret_cast<void*>(i));
605 else
607 sal_Bool bInsert = sal_False;
609 switch (nTypeId)
611 case TYP_INPUTFLD:
612 if (aList[i] == GetCurField()->GetPar1())
613 bInsert = sal_True;
614 break;
616 case TYP_FORMELFLD:
617 bInsert = sal_True;
618 break;
620 case TYP_GETFLD:
621 if (aList[i].equals(((const SwFormulaField*)GetCurField())->GetFormula()))
622 bInsert = sal_True;
623 break;
625 case TYP_SETFLD:
626 case TYP_USERFLD:
627 if (aList[i] == GetCurField()->GetTyp()->GetName())
629 bInsert = sal_True;
630 if (GetCurField()->GetSubType() & nsSwExtendedSubType::SUB_INVISIBLE)
631 aInvisibleCB.Check();
633 break;
635 case TYP_SETREFPAGEFLD:
636 if ((((SwRefPageSetField*)GetCurField())->IsOn() && i) ||
637 (!((SwRefPageSetField*)GetCurField())->IsOn() && !i))
638 sOldSel = aList[i];
640 // allow all entries for selection:
641 nPos = aSelectionLB.InsertEntry(aList[i]);
642 aSelectionLB.SetEntryData(nPos, reinterpret_cast<void*>(i));
643 break;
645 default:
646 if (aList[i] == GetCurField()->GetPar1())
647 bInsert = sal_True;
648 break;
650 if (bInsert)
652 nPos = aSelectionLB.InsertEntry(aList[i]);
653 aSelectionLB.SetEntryData(nPos, reinterpret_cast<void*>(i));
654 if (nTypeId != TYP_FORMELFLD)
655 break;
661 sal_Bool bEnable = aSelectionLB.GetEntryCount() != 0;
662 ListBox *pLB = 0;
664 if (bEnable)
666 aSelectionLB.SelectEntry(sOldSel);
667 if (!aSelectionLB.GetSelectEntryCount())
669 aSelectionLB.SelectEntryPos(0);
670 pLB = &aSelectionLB; // newly initialise all controls
674 aSelectionLB.Enable( bEnable );
675 aSelectionFT.Enable( bEnable );
677 SubTypeHdl(pLB);
678 aSelectionLB.SetUpdateMode(sal_True);
681 sal_uInt16 SwFldVarPage::FillFormatLB(sal_uInt16 nTypeId)
683 String sOldSel, sOldNumSel;
684 sal_uLong nOldNumFormat = 0;
686 sal_uInt16 nFormatSel = aFormatLB.GetSelectEntryPos();
687 if (nFormatSel != LISTBOX_ENTRY_NOTFOUND)
688 sOldSel = aFormatLB.GetEntry(nFormatSel);
690 sal_uInt16 nNumFormatSel = aNumFormatLB.GetSelectEntryPos();
691 if (nNumFormatSel != LISTBOX_ENTRY_NOTFOUND)
693 sOldNumSel = aNumFormatLB.GetEntry(nNumFormatSel);
694 nOldNumFormat = aNumFormatLB.GetFormat();
697 // fill Format-Listbox
698 aFormatLB.Clear();
699 aNumFormatLB.Clear();
700 sal_Bool bSpecialFmt = sal_False;
702 if( TYP_GETREFPAGEFLD != nTypeId )
704 if (IsFldEdit())
706 bSpecialFmt = GetCurField()->GetFormat() == SAL_MAX_UINT32;
708 if (!bSpecialFmt)
710 aNumFormatLB.SetDefFormat(GetCurField()->GetFormat());
711 sOldNumSel = aEmptyStr;
713 else
714 if (nTypeId == TYP_GETFLD || nTypeId == TYP_FORMELFLD)
715 aNumFormatLB.SetFormatType(NUMBERFORMAT_NUMBER);
717 else
719 if (nOldNumFormat && nOldNumFormat != ULONG_MAX)
720 aNumFormatLB.SetDefFormat(nOldNumFormat);
721 else
722 aNumFormatLB.SetFormatType(NUMBERFORMAT_NUMBER);
726 switch (nTypeId)
728 case TYP_USERFLD:
730 if (!IsFldEdit() || bSpecialFmt)
732 sal_uInt16 nPos = aNumFormatLB.InsertEntry(SW_RESSTR(FMT_MARK_TEXT), 0);
733 aNumFormatLB.SetEntryData(nPos, (void *)ULONG_MAX);
734 nPos = aNumFormatLB.InsertEntry(SW_RESSTR(FMT_USERVAR_CMD), 1);
735 aNumFormatLB.SetEntryData(nPos, (void *)ULONG_MAX);
738 break;
740 case TYP_SETFLD:
742 if (!IsFldEdit() || bSpecialFmt)
744 sal_uInt16 nPos = aNumFormatLB.InsertEntry(SW_RESSTR(FMT_SETVAR_TEXT), 0);
745 aNumFormatLB.SetEntryData(nPos, (void *)ULONG_MAX);
748 break;
750 case TYP_FORMELFLD:
752 sal_uInt16 nPos = aNumFormatLB.InsertEntry(SW_RESSTR(FMT_GETVAR_NAME), 0);
753 aNumFormatLB.SetEntryData(nPos, (void *)ULONG_MAX);
755 break;
757 case TYP_GETFLD:
759 sal_uInt16 nPos = aNumFormatLB.InsertEntry(SW_RESSTR(FMT_GETVAR_NAME), 0);
760 aNumFormatLB.SetEntryData(nPos, (void *)ULONG_MAX);
762 break;
765 if (IsFldEdit() && bSpecialFmt)
767 if (nTypeId == TYP_USERFLD && (GetCurField()->GetSubType() & nsSwExtendedSubType::SUB_CMD))
768 aNumFormatLB.SelectEntryPos(1);
769 else
770 aNumFormatLB.SelectEntryPos(0);
772 else
774 if (!nOldNumFormat && (nNumFormatSel = aNumFormatLB.GetEntryPos(sOldNumSel)) != LISTBOX_ENTRY_NOTFOUND)
775 aNumFormatLB.SelectEntryPos(nNumFormatSel);
776 else if (nOldNumFormat && nOldNumFormat == ULONG_MAX)
777 aNumFormatLB.SelectEntry(sOldSel);
780 sal_uInt16 nSize = GetFldMgr().GetFormatCount(nTypeId, false, IsFldDlgHtmlMode());
782 for (sal_uInt16 i = 0; i < nSize; i++)
784 sal_uInt16 nPos = aFormatLB.InsertEntry(GetFldMgr().GetFormatStr(nTypeId, i));
785 sal_uInt16 nFldId = GetFldMgr().GetFormatId( nTypeId, i );
786 aFormatLB.SetEntryData( nPos, reinterpret_cast<void*>(nFldId) );
787 if (IsFldEdit() && nFldId == GetCurField()->GetFormat())
788 aFormatLB.SelectEntryPos( nPos );
791 if (nSize && (!IsFldEdit() || !aFormatLB.GetSelectEntryCount()))
793 aFormatLB.SelectEntry(sOldSel);
795 if (!aFormatLB.GetSelectEntryCount())
797 aFormatLB.SelectEntry(SW_RESSTR(FMT_NUM_PAGEDESC));
798 if (!aFormatLB.GetSelectEntryCount())
800 aFormatLB.SelectEntry(SW_RESSTR(FMT_NUM_ARABIC));
801 if (!aFormatLB.GetSelectEntryCount())
802 aFormatLB.SelectEntryPos(0);
807 return nSize;
810 /*--------------------------------------------------------------------
811 Description: Modify
812 --------------------------------------------------------------------*/
813 IMPL_LINK_NOARG(SwFldVarPage, ModifyHdl)
815 String sValue(aValueED.GetText());
816 sal_Bool bHasValue = sValue.Len() != 0;
817 sal_uInt16 nTypeId = (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData(GetTypeSel());
818 bool bInsert = false, bApply = false, bDelete = false;
820 String sName( aNameED.GetText() );
821 xub_StrLen nLen = sName.Len();
823 switch( nTypeId )
825 case TYP_DDEFLD:
826 case TYP_USERFLD:
827 case TYP_SETFLD:
828 case TYP_SEQFLD:
829 SwCalc::IsValidVarName( sName, &sName );
830 if( sName.Len() != nLen )
832 nLen = sName.Len();
833 Selection aSel(aNameED.GetSelection());
834 aNameED.SetText( sName );
835 aNameED.SetSelection( aSel ); // restore Cursorpos
837 break;
841 // check buttons
842 switch (nTypeId)
844 case TYP_DDEFLD:
845 if( nLen )
847 // is there already a corrensponding type
848 bInsert = bApply = true;
850 SwFieldType* pType = GetFldMgr().GetFldType(RES_DDEFLD, sName);
852 SwWrtShell *pSh = GetWrtShell();
853 if(!pSh)
854 pSh = ::GetActiveWrtShell();
855 if(pSh && pType)
856 bDelete = !pSh->IsUsed( *pType );
858 break;
860 case TYP_USERFLD:
861 if( nLen )
863 // is there already a corresponding type
864 SwFieldType* pType = GetFldMgr().GetFldType(RES_USERFLD, sName);
866 SwWrtShell *pSh = GetWrtShell();
867 if(!pSh)
868 pSh = ::GetActiveWrtShell();
869 if(pSh && pType)
870 bDelete = !pSh->IsUsed( *pType );
872 pType = GetFldMgr().GetFldType(RES_SETEXPFLD, sName);
873 if (!pType) // no name conflict with variables
875 // user fields can also be inserted without content!
876 // Bug #56845
877 bInsert = bApply = true;
880 break;
882 default:
883 bInsert = true;
885 if (nTypeId == TYP_SETFLD || nTypeId == TYP_SEQFLD)
887 SwSetExpFieldType* pFldType = (SwSetExpFieldType*)
888 GetFldMgr().GetFldType(RES_SETEXPFLD, sName);
890 if (pFldType)
893 SwWrtShell *pSh = GetWrtShell();
894 if(!pSh)
895 pSh = ::GetActiveWrtShell();
896 if(pSh)
898 const SwFldTypes* p = pSh->GetDoc()->GetFldTypes();
899 sal_uInt16 i;
901 for (i = 0; i < INIT_FLDTYPES; i++)
903 SwFieldType* pType = (*p)[ i ];
904 if (pType == pFldType)
905 break;
908 if (i >= INIT_FLDTYPES && !pSh->IsUsed(*pFldType))
909 bDelete = true;
911 if (nTypeId == TYP_SEQFLD && !(pFldType->GetType() & nsSwGetSetExpType::GSE_SEQ))
912 bInsert = false;
914 if (nTypeId == TYP_SETFLD && (pFldType->GetType() & nsSwGetSetExpType::GSE_SEQ))
915 bInsert = false;
918 if (GetFldMgr().GetFldType(RES_USERFLD, sName))
919 bInsert = false;
922 if( !nLen && ( nTypeId == TYP_SETFLD ||
923 (!IsFldEdit() && nTypeId == TYP_GETFLD ) ) )
924 bInsert = false;
926 if( (nTypeId == TYP_SETFLD || nTypeId == TYP_FORMELFLD) &&
927 !bHasValue )
928 bInsert = false;
929 break;
932 aNewDelTBX.EnableItem(BT_VARAPPLY, bApply);
933 aNewDelTBX.EnableItem(BT_VARDELETE, bDelete);
934 EnableInsert(bInsert);
936 return 0;
939 IMPL_LINK( SwFldVarPage, TBClickHdl, ToolBox *, pBox )
941 sal_uInt16 nTypeId = (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData(GetTypeSel());
943 switch (pBox->GetCurItemId())
945 case BT_VARDELETE:
947 if( nTypeId == TYP_USERFLD )
948 GetFldMgr().RemoveFldType(RES_USERFLD, aSelectionLB.GetSelectEntry());
949 else
951 sal_uInt16 nWhich;
953 switch(nTypeId)
955 case TYP_SETFLD:
956 case TYP_SEQFLD:
957 nWhich = RES_SETEXPFLD;
958 break;
959 default:
960 nWhich = RES_DDEFLD;
961 break;
964 GetFldMgr().RemoveFldType(nWhich, aSelectionLB.GetSelectEntry());
967 UpdateSubType();
968 SwWrtShell *pSh = GetWrtShell();
969 if(!pSh)
970 pSh = ::GetActiveWrtShell();
971 if(pSh)
973 pSh->SetModified();
976 break;
978 case BT_VARAPPLY:
980 String sName(aNameED.GetText()), sValue(aValueED.GetText());
981 SwFieldType* pType = 0;
982 sal_uInt16 nId = 0;
983 sal_uInt16 nNumFormatPos = aNumFormatLB.GetSelectEntryPos();
985 switch (nTypeId)
987 case TYP_USERFLD: nId = RES_USERFLD; break;
988 case TYP_DDEFLD: nId = RES_DDEFLD; break;
989 case TYP_SETFLD: nId = RES_SETEXPFLD;break;
991 pType = GetFldMgr().GetFldType(nId, sName);
993 sal_uLong nFormat = aFormatLB.GetSelectEntryPos();
994 if (nFormat != LISTBOX_ENTRY_NOTFOUND)
995 nFormat = (sal_uLong)aFormatLB.GetEntryData((sal_uInt16)nFormat);
997 if (pType) // change
999 SwWrtShell *pSh = GetWrtShell();
1000 if(!pSh)
1001 pSh = ::GetActiveWrtShell();
1002 if(pSh)
1004 pSh->StartAllAction();
1006 if (nTypeId == TYP_USERFLD)
1008 if (nNumFormatPos != LISTBOX_ENTRY_NOTFOUND)
1010 sal_uLong nFmt = nNumFormatPos == 0 ? 0 : aNumFormatLB.GetFormat();
1011 if (nFmt)
1012 { // Switch language to office-language because Kalkulator expects
1013 // String in office format and it should be fed into dialog like
1014 // that
1015 nFmt = SwValueField::GetSystemFormat(pSh->GetNumberFormatter(), nFmt);
1017 ((SwUserFieldType*)pType)->SetContent(aValueED.GetText(), nFmt);
1018 ((SwUserFieldType*)pType)->SetType(
1019 nNumFormatPos == 0 ? nsSwGetSetExpType::GSE_STRING : nsSwGetSetExpType::GSE_EXPR );
1022 else
1024 if (nFormat != LISTBOX_ENTRY_NOTFOUND)
1026 // DDE-Topics/-Items can have blanks in their names!
1027 // That's not being considered here yet.
1028 sal_uInt16 nTmpPos = sValue.SearchAndReplace( ' ', sfx2::cTokenSeparator );
1029 sValue.SearchAndReplace( ' ', sfx2::cTokenSeparator, nTmpPos );
1030 ((SwDDEFieldType*)pType)->SetCmd(sValue);
1031 ((SwDDEFieldType*)pType)->SetType((sal_uInt16)nFormat);
1034 pType->UpdateFlds();
1036 pSh->EndAllAction();
1039 else // new
1041 if(nTypeId == TYP_USERFLD)
1043 SwWrtShell *pSh = GetWrtShell();
1044 if(!pSh)
1045 pSh = ::GetActiveWrtShell();
1046 if(pSh)
1048 SwUserFieldType aType( pSh->GetDoc(), sName );
1050 if (nNumFormatPos != LISTBOX_ENTRY_NOTFOUND)
1052 aType.SetType(nNumFormatPos == 0 ? nsSwGetSetExpType::GSE_STRING : nsSwGetSetExpType::GSE_EXPR);
1053 aType.SetContent( sValue, nNumFormatPos == 0 ? 0 : aNumFormatLB.GetFormat() );
1054 aSelectionLB.InsertEntry(sName);
1055 aSelectionLB.SelectEntry(sName);
1056 GetFldMgr().InsertFldType( aType ); // Userfld new
1060 else
1062 if (nFormat != LISTBOX_ENTRY_NOTFOUND)
1064 // DDE-Topics/-Items can have blanks in their names!
1065 // That's not being considered here yet.
1066 sal_uInt16 nTmpPos = sValue.SearchAndReplace( ' ', sfx2::cTokenSeparator );
1067 sValue.SearchAndReplace( ' ', sfx2::cTokenSeparator, nTmpPos );
1069 SwDDEFieldType aType(sName, sValue, (sal_uInt16)nFormat);
1070 aSelectionLB.InsertEntry(sName);
1071 aSelectionLB.SelectEntry(sName);
1072 GetFldMgr().InsertFldType(aType); // DDE-Field new
1076 if (IsFldEdit())
1077 GetFldMgr().GetCurFld(); // update FieldManager
1079 UpdateSubType();
1081 break;
1084 return sal_True;
1087 IMPL_LINK_NOARG(SwFldVarPage, ChapterHdl)
1089 sal_Bool bEnable = aChapterLevelLB.GetSelectEntryPos() != 0;
1091 aSeparatorED.Enable(bEnable);
1092 aSeparatorFT.Enable(bEnable);
1093 SeparatorHdl();
1095 return 0;
1098 IMPL_LINK_NOARG(SwFldVarPage, SeparatorHdl)
1100 sal_Bool bEnable = !aSeparatorED.GetText().isEmpty() ||
1101 aChapterLevelLB.GetSelectEntryPos() == 0;
1102 EnableInsert(bEnable);
1104 return 0;
1107 sal_Bool SwFldVarPage::FillItemSet(SfxItemSet& )
1109 sal_uInt16 nTypeId = (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData(GetTypeSel());
1111 String aVal(aValueED.GetText());
1112 String aName(aNameED.GetText());
1114 sal_uInt16 nSubType = aSelectionLB.GetSelectEntryPos();
1115 if(nSubType == LISTBOX_ENTRY_NOTFOUND)
1116 nSubType = 0;
1117 else
1118 nSubType = (sal_uInt16)(sal_uLong)aSelectionLB.GetEntryData(nSubType);
1120 sal_uLong nFormat;
1122 if (!aNumFormatLB.IsVisible())
1124 nFormat = aFormatLB.GetSelectEntryPos();
1126 if(nFormat == LISTBOX_ENTRY_NOTFOUND)
1127 nFormat = 0;
1128 else
1129 nFormat = (sal_uLong)aFormatLB.GetEntryData((sal_uInt16)nFormat);
1131 else
1133 nFormat = aNumFormatLB.GetFormat();
1135 if (nFormat && nFormat != ULONG_MAX && aNumFormatLB.IsAutomaticLanguage())
1137 // Switch language to office language because Kalkulator expects
1138 // String in office format and it should be fed into the dialog
1139 // like that
1140 SwWrtShell *pSh = GetWrtShell();
1141 if(!pSh)
1142 pSh = ::GetActiveWrtShell();
1143 if(pSh)
1145 nFormat = SwValueField::GetSystemFormat(pSh->GetNumberFormatter(), nFormat);
1149 sal_Unicode cSeparator = ' ';
1150 switch (nTypeId)
1152 case TYP_USERFLD:
1154 nSubType = (nFormat == ULONG_MAX) ? nsSwGetSetExpType::GSE_STRING : nsSwGetSetExpType::GSE_EXPR;
1156 if (nFormat == ULONG_MAX && aNumFormatLB.GetSelectEntry().Equals(SW_RESSTR(FMT_USERVAR_CMD)))
1157 nSubType |= nsSwExtendedSubType::SUB_CMD;
1159 if (aInvisibleCB.IsChecked())
1160 nSubType |= nsSwExtendedSubType::SUB_INVISIBLE;
1161 break;
1163 case TYP_FORMELFLD:
1165 nSubType = nsSwGetSetExpType::GSE_FORMULA;
1166 if (aNumFormatLB.IsVisible() && nFormat == ULONG_MAX)
1167 nSubType |= nsSwExtendedSubType::SUB_CMD;
1168 break;
1170 case TYP_GETFLD:
1172 nSubType &= 0xff00;
1173 if (aNumFormatLB.IsVisible() && nFormat == ULONG_MAX)
1174 nSubType |= nsSwExtendedSubType::SUB_CMD;
1175 break;
1177 case TYP_INPUTFLD:
1179 SwFieldType* pType = GetFldMgr().GetFldType(RES_USERFLD, aName);
1180 nSubType = static_cast< sal_uInt16 >((nSubType & 0xff00) | ((pType) ? INP_USR : INP_VAR));
1181 break;
1184 case TYP_SETFLD:
1186 if (IsFldDlgHtmlMode())
1188 nSubType = 0x0100;
1189 nSubType = (nSubType & 0xff00) | nsSwGetSetExpType::GSE_STRING;
1191 else
1192 nSubType = (nSubType & 0xff00) | ((nFormat == ULONG_MAX) ? nsSwGetSetExpType::GSE_STRING : nsSwGetSetExpType::GSE_EXPR);
1194 if (aInvisibleCB.IsChecked())
1195 nSubType |= nsSwExtendedSubType::SUB_INVISIBLE;
1196 break;
1198 case TYP_SEQFLD:
1200 nSubType = aChapterLevelLB.GetSelectEntryPos();
1201 if (nSubType == 0)
1202 nSubType = 0x7f;
1203 else
1205 nSubType--;
1206 OUString sSeparator = OUString(aSeparatorED.GetText()[0]);
1207 cSeparator = !sSeparator.isEmpty() ? sSeparator[0] : ' ';
1209 break;
1211 case TYP_GETREFPAGEFLD:
1212 if( SVX_NUM_CHAR_SPECIAL == nFormat )
1213 aVal = aValueED.GetText();
1214 break;
1217 if (!IsFldEdit() ||
1218 aNameED.GetSavedValue() != aNameED.GetText() ||
1219 aValueED.GetSavedValue() != aValueED.GetText() ||
1220 aSelectionLB.GetSavedValue() != aSelectionLB.GetSelectEntryPos() ||
1221 aFormatLB.GetSavedValue() != aFormatLB.GetSelectEntryPos() ||
1222 nOldFormat != aNumFormatLB.GetFormat() ||
1223 aInvisibleCB.GetState() != aInvisibleCB.GetSavedValue() ||
1224 aChapterLevelLB.GetSavedValue() != aChapterLevelLB.GetSelectEntryPos() ||
1225 aSeparatorED.GetSavedValue() != aSeparatorED.GetText())
1227 InsertFld( nTypeId, nSubType, aName, aVal, nFormat,
1228 cSeparator, aNumFormatLB.IsAutomaticLanguage() );
1231 UpdateSubType();
1233 return sal_False;
1236 SfxTabPage* SwFldVarPage::Create( Window* pParent,
1237 const SfxItemSet& rAttrSet )
1239 return ( new SwFldVarPage( pParent, rAttrSet ) );
1242 sal_uInt16 SwFldVarPage::GetGroup()
1244 return GRP_VAR;
1247 SelectionListBox::SelectionListBox( SwFldVarPage* pDialog, const ResId& rResId ) :
1248 ListBox (pDialog, rResId),
1249 bCallAddSelection(false)
1253 long SelectionListBox::PreNotify( NotifyEvent& rNEvt )
1255 long nHandled = ListBox::PreNotify( rNEvt );
1256 if ( rNEvt.GetType() == EVENT_KEYUP )
1258 const KeyEvent* pKEvt = rNEvt.GetKeyEvent();
1259 const KeyCode aKeyCode = pKEvt->GetKeyCode();
1260 const sal_uInt16 nModifier = aKeyCode.GetModifier();
1261 if( aKeyCode.GetCode() == KEY_SPACE && !nModifier)
1262 bCallAddSelection = true;
1264 if ( rNEvt.GetType() == EVENT_MOUSEBUTTONDOWN )
1266 const MouseEvent* pMEvt = rNEvt.GetMouseEvent();
1268 if (pMEvt && (pMEvt->IsMod1() || pMEvt->IsMod2())) // Alt or Ctrl
1269 bCallAddSelection = true;
1272 return nHandled;
1275 void SwFldVarPage::FillUserData()
1277 String sData(OUString(USER_DATA_VERSION));
1278 sData += ';';
1279 sal_uInt16 nTypeSel = aTypeLB.GetSelectEntryPos();
1280 if( LISTBOX_ENTRY_NOTFOUND == nTypeSel )
1281 nTypeSel = USHRT_MAX;
1282 else
1283 nTypeSel = (sal_uInt16)(sal_uLong)aTypeLB.GetEntryData( nTypeSel );
1284 sData += OUString::number( nTypeSel );
1285 SetUserData(sData);
1288 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */