1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sfx2/app.hxx>
21 #include <basic/basmgr.hxx>
22 #include <basic/sbmod.hxx>
24 #include <swtypes.hxx>
25 #include <globals.hrc>
27 #include <docufld.hxx>
30 #include <swmodule.hxx>
31 #include <fldfunc.hxx>
32 #include <flddropdown.hxx>
34 #include <fldtdlg.hrc>
36 #define USER_DATA_VERSION_1 "1"
37 #define USER_DATA_VERSION USER_DATA_VERSION_1
39 using namespace ::com::sun::star
;
42 SwFldFuncPage::SwFldFuncPage(Window
* pParent
, const SfxItemSet
& rCoreSet
) :
43 SwFldPage( pParent
, SW_RES( TP_FLD_FUNC
), rCoreSet
),
45 aTypeFT (this, SW_RES(FT_FUNCTYPE
)),
46 aTypeLB (this, SW_RES(LB_FUNCTYPE
)),
47 aSelectionFT(this, SW_RES(FT_FUNCSELECTION
)),
48 aSelectionLB(this, SW_RES(LB_FUNCSELECTION
)),
49 aFormatFT (this, SW_RES(FT_FUNCFORMAT
)),
50 aFormatLB (this, SW_RES(LB_FUNCFORMAT
)),
51 aNameFT (this, SW_RES(FT_FUNCNAME
)),
52 aNameED (this, SW_RES(ED_FUNCNAME
)),
53 aValueFT (this, SW_RES(FT_FUNCVALUE
)),
54 aValueED (this, SW_RES(ED_FUNCVALUE
)),
55 aCond1FT (this, SW_RES(FT_FUNCCOND1
)),
56 aCond1ED (this, SW_RES(ED_FUNCCOND1
)),
57 aCond2FT (this, SW_RES(FT_FUNCCOND2
)),
58 aCond2ED (this, SW_RES(ED_FUNCCOND2
)),
59 aMacroBT (this, SW_RES(BT_FUNCMACRO
)),
60 aListItemFT( this, SW_RES( FT_LISTITEM
)),
61 aListItemED( this, SW_RES( ED_LISTITEM
)),
62 aListAddPB( this, SW_RES( PB_LISTADD
)),
63 aListItemsFT( this, SW_RES( FT_LISTITEMS
)),
64 aListItemsLB( this, SW_RES( LB_LISTITEMS
)),
65 aListRemovePB( this, SW_RES( PB_LISTREMOVE
)),
66 aListUpPB( this, SW_RES( PB_LISTUP
)),
67 aListDownPB( this, SW_RES( PB_LISTDOWN
)),
68 aListNameFT( this, SW_RES( FT_LISTNAME
)),
69 aListNameED( this, SW_RES( ED_LISTNAME
)),
70 bDropDownLBChanged(false)
74 aNameED
.SetPosPixel(Point(aNameED
.GetPosPixel().X(), aFormatLB
.GetPosPixel().Y()));
76 aNameED
.SetModifyHdl(LINK(this, SwFldFuncPage
, ModifyHdl
));
78 sOldValueFT
= aValueFT
.GetText();
79 sOldNameFT
= aNameFT
.GetText();
81 aCond1ED
.ShowBrackets(false);
82 aCond2ED
.ShowBrackets(false);
86 SwFldFuncPage::~SwFldFuncPage()
90 void SwFldFuncPage::Reset(const SfxItemSet
& )
93 Init(); // general initialisation
95 aTypeLB
.SetUpdateMode(sal_False
);
98 sal_uInt16 nPos
, nTypeId
;
102 // initialise TypeListBox
103 const SwFldGroupRgn
& rRg
= GetFldMgr().GetGroupRange(IsFldDlgHtmlMode(), GetGroup());
106 for(short i
= rRg
.nStart
; i
< rRg
.nEnd
; ++i
)
108 nTypeId
= GetFldMgr().GetTypeId(i
);
109 nPos
= aTypeLB
.InsertEntry(GetFldMgr().GetTypeStr(i
));
110 aTypeLB
.SetEntryData(nPos
, reinterpret_cast<void*>(nTypeId
));
115 nTypeId
= GetCurField()->GetTypeId();
116 nPos
= aTypeLB
.InsertEntry(GetFldMgr().GetTypeStr(GetFldMgr().GetPos(nTypeId
)));
117 aTypeLB
.SetEntryData(nPos
, reinterpret_cast<void*>(nTypeId
));
119 if (nTypeId
== TYP_MACROFLD
)
121 String
sName(GetCurField()->GetPar1());
122 GetFldMgr().SetMacroPath(sName
);
127 RestorePos(&aTypeLB
);
129 aTypeLB
.SetDoubleClickHdl (LINK(this, SwFldFuncPage
, InsertHdl
));
130 aTypeLB
.SetSelectHdl (LINK(this, SwFldFuncPage
, TypeHdl
));
131 aSelectionLB
.SetSelectHdl (LINK(this, SwFldFuncPage
, SelectHdl
));
132 aSelectionLB
.SetDoubleClickHdl (LINK(this, SwFldFuncPage
, InsertMacroHdl
));
133 aFormatLB
.SetDoubleClickHdl (LINK(this, SwFldFuncPage
, InsertHdl
));
134 aMacroBT
.SetClickHdl (LINK(this, SwFldFuncPage
, MacroHdl
));
135 Link
aListModifyLk( LINK(this, SwFldFuncPage
, ListModifyHdl
));
136 aListAddPB
.SetClickHdl(aListModifyLk
);
137 aListRemovePB
.SetClickHdl(aListModifyLk
);
138 aListUpPB
.SetClickHdl(aListModifyLk
);
139 aListDownPB
.SetClickHdl(aListModifyLk
);
140 aListItemED
.SetReturnActionLink(aListModifyLk
);
141 Link aListEnableLk
= LINK(this, SwFldFuncPage
, ListEnableHdl
);
142 aListItemED
.SetModifyHdl(aListEnableLk
);
143 aListItemsLB
.SetSelectHdl(aListEnableLk
);
147 String sUserData
= GetUserData();
148 if(sUserData
.GetToken(0, ';').EqualsIgnoreCaseAscii(USER_DATA_VERSION_1
))
150 String sVal
= sUserData
.GetToken(1, ';');
151 sal_uInt16 nVal
= static_cast< sal_uInt16
>(sVal
.ToInt32());
152 if(nVal
!= USHRT_MAX
)
154 for(sal_uInt16 i
= 0; i
< aTypeLB
.GetEntryCount(); i
++)
155 if(nVal
== (sal_uInt16
)(sal_uLong
)aTypeLB
.GetEntryData(i
))
157 aTypeLB
.SelectEntryPos(i
);
165 aTypeLB
.SetUpdateMode(sal_True
);
170 aValueED
.SaveValue();
171 aCond1ED
.SaveValue();
172 aCond2ED
.SaveValue();
173 nOldFormat
= GetCurField()->GetFormat();
177 IMPL_LINK_NOARG(SwFldFuncPage
, TypeHdl
)
179 // save old ListBoxPos
180 const sal_uInt16 nOld
= GetTypeSel();
182 // current ListBoxPos
183 SetTypeSel(aTypeLB
.GetSelectEntryPos());
185 if(GetTypeSel() == LISTBOX_ENTRY_NOTFOUND
)
188 aTypeLB
.SelectEntryPos(0);
191 if (nOld
!= GetTypeSel())
193 sal_uInt16 nTypeId
= (sal_uInt16
)(sal_uLong
)aTypeLB
.GetEntryData(GetTypeSel());
195 // fill Selection-Listbox
198 // fill Format-Listbox
201 sal_uInt16 nSize
= GetFldMgr().GetFormatCount(nTypeId
, false, IsFldDlgHtmlMode());
203 for (sal_uInt16 i
= 0; i
< nSize
; i
++)
205 sal_uInt16 nPos
= aFormatLB
.InsertEntry(GetFldMgr().GetFormatStr(nTypeId
, i
));
206 aFormatLB
.SetEntryData( nPos
, reinterpret_cast<void*>(GetFldMgr().GetFormatId( nTypeId
, i
)) );
211 if (IsFldEdit() && nTypeId
== TYP_JUMPEDITFLD
)
212 aFormatLB
.SelectEntry(SW_RESSTR(FMT_MARK_BEGIN
+ (sal_uInt16
)GetCurField()->GetFormat()));
214 if (!aFormatLB
.GetSelectEntryCount())
215 aFormatLB
.SelectEntryPos(0);
218 sal_Bool bValue
= sal_False
, bName
= sal_False
, bMacro
= sal_False
, bInsert
= sal_True
;
219 sal_Bool bShowSelection
= sal_False
;
220 sal_Bool bFormat
= nSize
!= 0;
222 // two controls for conditional text
223 sal_Bool bDropDown
= TYP_DROPDOWN
== nTypeId
;
224 sal_Bool bCondTxtFld
= TYP_CONDTXTFLD
== nTypeId
;
226 aCond1FT
.Show(!bDropDown
&& bCondTxtFld
);
227 aCond1ED
.Show(!bDropDown
&& bCondTxtFld
);
228 aCond2FT
.Show(!bDropDown
&& bCondTxtFld
);
229 aCond2ED
.Show(!bDropDown
&& bCondTxtFld
);
230 aValueFT
.Show(!bDropDown
&& !bCondTxtFld
);
231 aValueED
.Show(!bDropDown
&& !bCondTxtFld
);
232 aMacroBT
.Show(!bDropDown
);
233 aNameED
.Show(!bDropDown
);
234 aNameFT
.Show(!bDropDown
);
236 aListItemFT
.Show(bDropDown
);
237 aListItemED
.Show(bDropDown
);
238 aListAddPB
.Show(bDropDown
);
239 aListItemsFT
.Show(bDropDown
);
240 aListItemsLB
.Show(bDropDown
);
241 aListRemovePB
.Show(bDropDown
);
242 aListUpPB
.Show(bDropDown
);
243 aListDownPB
.Show(bDropDown
);
244 aListNameFT
.Show(bDropDown
);
245 aListNameED
.Show(bDropDown
);
247 aNameED
.SetDropEnable(false);
253 const SwDropDownField
* pDrop
= (const SwDropDownField
*)GetCurField();
254 uno::Sequence
<OUString
> aItems
= pDrop
->GetItemSequence();
255 const OUString
* pArray
= aItems
.getConstArray();
256 aListItemsLB
.Clear();
257 for(sal_Int32 i
= 0; i
< aItems
.getLength(); i
++)
258 aListItemsLB
.InsertEntry(pArray
[i
]);
259 aListItemsLB
.SelectEntry(pDrop
->GetSelectedItem());
260 aListNameED
.SetText(pDrop
->GetPar2());
261 aListNameED
.SaveValue();
262 bDropDownLBChanged
= false;
266 aNameED
.SetText(GetCurField()->GetPar1());
267 aValueED
.SetText(GetCurField()->GetPar2());
272 aNameED
.SetText(aEmptyStr
);
273 aValueED
.SetText(aEmptyStr
);
278 if( aNameFT
.GetText() != OUString(sOldNameFT
) )
279 aNameFT
.SetText(sOldNameFT
);
280 if (aValueFT
.GetText() != OUString(sOldValueFT
))
281 aValueFT
.SetText(sOldValueFT
);
287 if (GetFldMgr().GetMacroPath().Len())
292 aNameFT
.SetText(SW_RESSTR(STR_MACNAME
));
293 aValueFT
.SetText(SW_RESSTR(STR_PROMPT
));
294 aNameED
.SetText(GetFldMgr().GetMacroName());
295 aNameED
.SetAccessibleName(aNameFT
.GetText());
296 aValueED
.SetAccessibleName(aValueFT
.GetText());
299 case TYP_HIDDENPARAFLD
:
300 aNameFT
.SetText(SW_RESSTR(STR_COND
));
301 aNameED
.SetDropEnable(true);
303 aNameED
.SetAccessibleName(aNameFT
.GetText());
304 aValueED
.SetAccessibleName(aValueFT
.GetText());
307 case TYP_HIDDENTXTFLD
:
309 aNameFT
.SetText(SW_RESSTR(STR_COND
));
310 aNameED
.SetDropEnable(true);
311 aValueFT
.SetText(SW_RESSTR(STR_INSTEXT
));
312 SwWrtShell
* pSh
= GetActiveWrtShell();
313 if (!IsFldEdit() && pSh
)
314 aValueED
.SetText(pSh
->GetSelTxt());
315 bName
= bValue
= sal_True
;
316 aNameED
.SetAccessibleName(aNameFT
.GetText());
317 aValueED
.SetAccessibleName(aValueFT
.GetText());
322 aNameFT
.SetText(SW_RESSTR(STR_COND
));
323 aNameED
.SetDropEnable(true);
326 aCond1ED
.SetText(GetCurField()->GetPar2().getToken(0, '|'));
327 aCond2ED
.SetText(GetCurField()->GetPar2().getToken(1, '|'));
330 bName
= bValue
= sal_True
;
331 aNameED
.SetAccessibleName(aNameFT
.GetText());
332 aValueED
.SetAccessibleName(aValueFT
.GetText());
335 case TYP_JUMPEDITFLD
:
336 aNameFT
.SetText(SW_RESSTR(STR_JUMPEDITFLD
));
337 aValueFT
.SetText(SW_RESSTR(STR_PROMPT
));
338 bName
= bValue
= sal_True
;
339 aNameED
.SetAccessibleName(aNameFT
.GetText());
340 aValueED
.SetAccessibleName(aValueFT
.GetText());
344 aValueFT
.SetText(SW_RESSTR(STR_PROMPT
));
346 aNameED
.SetAccessibleName(aNameFT
.GetText());
347 aValueED
.SetAccessibleName(aValueFT
.GetText());
350 case TYP_COMBINED_CHARS
:
352 aNameFT
.SetText(SW_RESSTR(STR_COMBCHRS_FT
));
353 aNameED
.SetDropEnable(true);
356 const sal_Int32 nLen
= aNameED
.GetText().getLength();
357 if( !nLen
|| nLen
> MAX_COMBINED_CHARACTERS
)
359 aNameED
.SetAccessibleName(aNameFT
.GetText());
360 aValueED
.SetAccessibleName(aValueFT
.GetText());
384 aFormatLB
.Enable(bFormat
);
385 aFormatFT
.Enable(bFormat
);
386 aNameFT
.Enable(bName
);
387 aNameED
.Enable(bName
);
388 aValueFT
.Enable(bValue
);
389 aValueED
.Enable(bValue
);
390 aMacroBT
.Enable(bMacro
);
392 EnableInsert( bInsert
);
398 IMPL_LINK_NOARG(SwFldFuncPage
, SelectHdl
)
400 sal_uInt16 nTypeId
= (sal_uInt16
)(sal_uLong
)aTypeLB
.GetEntryData(GetTypeSel());
402 if( TYP_MACROFLD
== nTypeId
)
403 aNameED
.SetText( aSelectionLB
.GetSelectEntry() );
408 IMPL_LINK_NOARG(SwFldFuncPage
, InsertMacroHdl
)
416 IMPL_LINK( SwFldFuncPage
, ListModifyHdl
, Control
*, pControl
)
418 aListItemsLB
.SetUpdateMode(sal_False
);
419 if(pControl
== &aListAddPB
||
420 (pControl
== &aListItemED
&& aListAddPB
.IsEnabled()))
422 String
sEntry(aListItemED
.GetText());
423 aListItemsLB
.InsertEntry(sEntry
);
424 aListItemsLB
.SelectEntry(sEntry
);
426 else if(aListItemsLB
.GetSelectEntryCount())
428 sal_uInt16 nSelPos
= aListItemsLB
.GetSelectEntryPos();
429 if(pControl
== &aListRemovePB
)
431 aListItemsLB
.RemoveEntry(nSelPos
);
432 aListItemsLB
.SelectEntryPos(nSelPos
? nSelPos
- 1 : 0);
434 else if(pControl
== &aListUpPB
)
438 String sEntry
= aListItemsLB
.GetSelectEntry();
439 aListItemsLB
.RemoveEntry(nSelPos
);
441 aListItemsLB
.InsertEntry(sEntry
, nSelPos
);
442 aListItemsLB
.SelectEntryPos(nSelPos
);
445 else if(pControl
== &aListDownPB
)
447 if(nSelPos
< aListItemsLB
.GetEntryCount() - 1)
449 String sEntry
= aListItemsLB
.GetSelectEntry();
450 aListItemsLB
.RemoveEntry(nSelPos
);
452 aListItemsLB
.InsertEntry(sEntry
, nSelPos
);
453 aListItemsLB
.SelectEntryPos(nSelPos
);
457 bDropDownLBChanged
= true;
458 aListItemsLB
.SetUpdateMode(sal_True
);
463 IMPL_LINK_NOARG(SwFldFuncPage
, ListEnableHdl
)
465 //enable "Add" button when text is in the Edit that's not already member of the box
466 aListAddPB
.Enable(!aListItemED
.GetText().isEmpty() &&
467 LISTBOX_ENTRY_NOTFOUND
== aListItemsLB
.GetEntryPos(aListItemED
.GetText()));
468 sal_Bool bEnableButtons
= aListItemsLB
.GetSelectEntryCount() > 0;
469 aListRemovePB
.Enable(bEnableButtons
);
470 aListUpPB
.Enable(bEnableButtons
&& (aListItemsLB
.GetSelectEntryPos() > 0));
471 aListDownPB
.Enable(bEnableButtons
&&
472 (aListItemsLB
.GetSelectEntryPos() < (aListItemsLB
.GetEntryCount() - 1)));
477 /*--------------------------------------------------------------------
478 Description: renew types in SelectionBox
479 --------------------------------------------------------------------*/
480 void SwFldFuncPage::UpdateSubType()
482 sal_uInt16 nTypeId
= (sal_uInt16
)(sal_uLong
)aTypeLB
.GetEntryData(GetTypeSel());
484 // fill Selction-Listbox
485 aSelectionLB
.SetUpdateMode(sal_False
);
486 aSelectionLB
.Clear();
488 std::vector
<OUString
> aLst
;
489 GetFldMgr().GetSubTypes(nTypeId
, aLst
);
490 size_t nCount
= aLst
.size();
492 for(size_t i
= 0; i
< nCount
; ++i
)
494 size_t nPos
= aSelectionLB
.InsertEntry(aLst
[i
]);
495 aSelectionLB
.SetEntryData(nPos
, reinterpret_cast<void*>(i
));
498 sal_Bool bEnable
= nCount
!= 0;
500 aSelectionLB
.Enable( bEnable
);
501 aSelectionFT
.Enable( bEnable
);
505 aSelectionLB
.SelectEntryPos(0);
508 if (nTypeId
== TYP_MACROFLD
)
510 sal_Bool bHasMacro
= GetFldMgr().GetMacroPath().Len() != 0;
514 aNameED
.SetText(GetFldMgr().GetMacroName());
518 EnableInsert(bHasMacro
);
521 aSelectionLB
.SetUpdateMode(sal_True
);
524 /*--------------------------------------------------------------------
525 Description: call MacroBrowser, fill Listbox with Macros
526 --------------------------------------------------------------------*/
527 IMPL_LINK( SwFldFuncPage
, MacroHdl
, Button
*, pBtn
)
529 Window
* pDefModalDlgParent
= Application::GetDefDialogParent();
530 Application::SetDefDialogParent( pBtn
);
532 String
sMacro(TurnMacroString(aNameED
.GetText()));
533 while (sMacro
.SearchAndReplace('.', ';') != STRING_NOTFOUND
) ;
535 if (GetFldMgr().ChooseMacro(sMacro
))
538 Application::SetDefDialogParent( pDefModalDlgParent
);
543 sal_Bool
SwFldFuncPage::FillItemSet(SfxItemSet
& )
545 sal_uInt16 nTypeId
= (sal_uInt16
)(sal_uLong
)aTypeLB
.GetEntryData(GetTypeSel());
547 sal_uInt16 nSubType
= 0;
549 sal_uLong nFormat
= aFormatLB
.GetSelectEntryPos();
551 if(nFormat
== LISTBOX_ENTRY_NOTFOUND
)
554 nFormat
= (sal_uLong
)aFormatLB
.GetEntryData((sal_uInt16
)nFormat
);
556 String
aVal(aValueED
.GetText());
557 String
aName(aNameED
.GetText());
563 // to prevent removal of CR/LF restore old content
564 if(!aNameED
.IsModified() && IsFldEdit())
565 aName
= GetCurField()->GetPar1();
570 // use the full script URL, not the name in the Edit control
571 aName
= GetFldMgr().GetMacroPath();
575 aVal
= aCond1ED
.GetText();
577 aVal
+= aCond2ED
.GetText();
581 aName
= aListNameED
.GetText();
582 for(sal_uInt16 i
= 0; i
< aListItemsLB
.GetEntryCount(); i
++)
586 aVal
+= aListItemsLB
.GetEntry(i
);
595 aNameED
.GetSavedValue() != aNameED
.GetText() ||
596 aValueED
.GetSavedValue() != aValueED
.GetText() ||
597 aCond1ED
.GetSavedValue() != aCond1ED
.GetText() ||
598 aCond2ED
.GetSavedValue() != aCond2ED
.GetText() ||
599 aListNameED
.GetSavedValue() != aListNameED
.GetText() ||
600 bDropDownLBChanged
||
601 nOldFormat
!= nFormat
)
603 InsertFld( nTypeId
, nSubType
, aName
, aVal
, nFormat
);
606 ModifyHdl(); // enable/disable Insert if applicable
611 String
SwFldFuncPage::TurnMacroString(const String
&rMacro
)
615 // reverse content of aName
619 for (sal_uInt16 i
= 0; i
< 4 && nPos
!= -1; i
++)
622 sTmp
= rMacro
.Copy(nPos
);
624 sTmp
= rMacro
.GetToken(0, '.', nPos
);
628 sBuf
.Insert( sTmp
, 0 );
636 SfxTabPage
* SwFldFuncPage::Create( Window
* pParent
,
637 const SfxItemSet
& rAttrSet
)
639 return ( new SwFldFuncPage( pParent
, rAttrSet
) );
642 sal_uInt16
SwFldFuncPage::GetGroup()
647 void SwFldFuncPage::FillUserData()
649 String
sData(OUString(USER_DATA_VERSION
));
651 sal_uInt16 nTypeSel
= aTypeLB
.GetSelectEntryPos();
652 if( LISTBOX_ENTRY_NOTFOUND
== nTypeSel
)
653 nTypeSel
= USHRT_MAX
;
655 nTypeSel
= sal::static_int_cast
< sal_uInt16
>(reinterpret_cast< sal_uIntPtr
>(aTypeLB
.GetEntryData( nTypeSel
)));
656 sData
+= OUString::number( nTypeSel
);
660 IMPL_LINK_NOARG(SwFldFuncPage
, ModifyHdl
)
662 String
aName(aNameED
.GetText());
663 const sal_uInt16 nLen
= aName
.Len();
665 sal_Bool bEnable
= sal_True
;
666 sal_uInt16 nTypeId
= (sal_uInt16
)(sal_uLong
)aTypeLB
.GetEntryData(GetTypeSel());
668 if( TYP_COMBINED_CHARS
== nTypeId
&&
669 (!nLen
|| nLen
> MAX_COMBINED_CHARACTERS
))
672 EnableInsert( bEnable
);
677 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */