bump product version to 4.1.6.2
[LibreOffice.git] / sw / source / ui / fldui / flddinf.cxx
blob68cb2a7b3e2c60d3bbe8171837eeca86b2f5047b
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 <sfx2/request.hxx>
21 #include <sfx2/frame.hxx>
22 #include <vcl/svapp.hxx>
23 #include <svl/zforlist.hxx>
24 #include <svl/zformat.hxx>
25 #include "svtools/treelistentry.hxx"
27 #include <helpid.h>
28 #include <swtypes.hxx>
29 #include <globals.hrc>
30 #include <fldbas.hxx>
31 #include <docufld.hxx>
32 #include <wrtsh.hxx>
34 #include <fldui.hrc>
36 #include <fldtdlg.hrc>
37 #include <flddinf.hxx>
38 #include <swmodule.hxx>
39 #include <view.hxx>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 #include <com/sun/star/util/Time.hpp>
42 #include <com/sun/star/util/DateTime.hpp>
43 #include <com/sun/star/util/Date.hpp>
45 #define USER_DATA_VERSION_1 "1"
46 #define USER_DATA_VERSION USER_DATA_VERSION_1
48 using namespace nsSwDocInfoSubType;
49 using namespace com::sun::star;
51 SwFldDokInfPage::SwFldDokInfPage(Window* pWindow, const SfxItemSet& rCoreSet ) :
52 SwFldPage( pWindow, SW_RES( TP_FLD_DOKINF ), rCoreSet ),
54 aTypeFT (this, SW_RES(FT_DOKINFTYPE)),
55 aTypeTLB (this, SW_RES(TLB_DOKINFTYPE)),
56 aSelectionFT(this, SW_RES(FT_DOKINFSELECTION)),
57 aSelectionLB(this, SW_RES(LB_DOKINFSELECTION)),
58 aFormatFT (this, SW_RES(FT_DOKINFFORMAT)),
59 aFormatLB (this, SW_RES(LB_DOKINFFORMAT)),
60 aFixedCB (this, SW_RES(CB_DOKINFFIXEDCONTENT)),
62 pSelEntry (0),
63 aInfoStr (SW_RES(STR_DOKINF_INFO))
65 FreeResource();
67 aTypeTLB.SetHelpId(HID_FIELD_DINF_TYPE);
68 aTypeTLB.SetSelectionMode(SINGLE_SELECTION);
69 aTypeTLB.SetStyle(aTypeTLB.GetStyle()|WB_HASLINES|WB_CLIPCHILDREN|WB_SORT|WB_HASBUTTONS|WB_HASBUTTONSATROOT|WB_HSCROLL);
70 // Don't set font, so that the control's font is adobted!
71 // Otherwise at wrong font bug to OV.
72 aTypeTLB.SetSpaceBetweenEntries(0);
74 aTypeTLB.SetNodeDefaultImages();
75 //enable 'active' language selection
76 aFormatLB.SetShowLanguageControl(sal_True);
78 SFX_ITEMSET_ARG( &rCoreSet, pItem, SfxUnoAnyItem, SID_DOCINFO, sal_False );
79 if ( pItem )
80 pItem->GetValue() >>= xCustomPropertySet;
83 SwFldDokInfPage::~SwFldDokInfPage()
87 void SwFldDokInfPage::Reset(const SfxItemSet& )
89 Init(); // general initialisation
91 // initialise TypeListBox
92 aTypeTLB.SetUpdateMode(sal_False);
93 aTypeTLB.Clear();
94 pSelEntry = 0;
96 // display SubTypes in TypeLB
97 sal_uInt16 nTypeId = TYP_DOCINFOFLD;
98 SvTreeListEntry* pEntry = 0;
100 SvTreeListEntry* pInfo = 0;
102 sal_uInt16 nSubType = USHRT_MAX;
103 if (IsFldEdit())
105 const SwField* pCurField = GetCurField();
106 nSubType = ((SwDocInfoField*)pCurField)->GetSubType() & 0xff;
107 if( nSubType == DI_CUSTOM )
109 m_sOldCustomFieldName = static_cast<const SwDocInfoField*>(pCurField)->GetName();
111 aFormatLB.SetAutomaticLanguage(pCurField->IsAutomaticLanguage());
112 SwWrtShell *pSh = GetWrtShell();
113 if(pSh)
115 const SvNumberformat* pFormat = pSh->GetNumberFormatter()->GetEntry(pCurField->GetFormat());
116 if(pFormat)
117 aFormatLB.SetLanguage(pFormat->GetLanguage());
121 sal_uInt16 nSelEntryData = USHRT_MAX;
122 String sUserData = GetUserData();
123 if(sUserData.GetToken(0, ';').EqualsIgnoreCaseAscii(USER_DATA_VERSION_1))
125 String sVal = sUserData.GetToken(1, ';');
126 nSelEntryData = static_cast< sal_uInt16 >(sVal.ToInt32());
129 std::vector<OUString> aLst;
130 GetFldMgr().GetSubTypes(nTypeId, aLst);
131 for(size_t i = 0; i < aLst.size(); ++i)
133 if (!IsFldEdit() || nSubType == i)
135 if (DI_CUSTOM == i)
137 if(xCustomPropertySet.is() )
139 uno::Reference< beans::XPropertySetInfo > xSetInfo = xCustomPropertySet->getPropertySetInfo();
140 const uno::Sequence< beans::Property > rProperties = xSetInfo->getProperties();
142 if( rProperties.getLength() )
144 pInfo = aTypeTLB.InsertEntry( String(SW_RES( STR_CUSTOM )) );
145 pInfo->SetUserData(reinterpret_cast<void*>(USHRT_MAX));
147 for (sal_Int32 n=0; n < rProperties.getLength(); n++)
149 OUString sEntry = rProperties[n].Name;
150 pEntry = aTypeTLB.InsertEntry(sEntry, pInfo);
151 if(m_sOldCustomFieldName.equals( sEntry ))
153 pSelEntry = pEntry;
154 aTypeTLB.Expand( pInfo );
156 pEntry->SetUserData(reinterpret_cast<void*>(i));
161 else
163 if (!(IsFldDlgHtmlMode() && (i == DI_EDIT || i == DI_THEMA || i == DI_PRINT)))
165 pEntry = aTypeTLB.InsertEntry(aLst[i]);
166 pEntry->SetUserData(reinterpret_cast<void*>(i));
169 if(nSelEntryData == i)
170 pSelEntry = pEntry;
174 // select old Pos
175 if (pSelEntry != 0)
177 aTypeTLB.Select(pSelEntry);
178 nSubType = (sal_uInt16)(sal_uLong)pSelEntry->GetUserData();
180 else if ( aTypeTLB.GetEntry(0) )
182 pSelEntry = aTypeTLB.GetEntry(0);
183 nSubType = (sal_uInt16)(sal_uLong)pSelEntry->GetUserData();
186 FillSelectionLB(nSubType);
187 if ( pSelEntry )
188 TypeHdl();
190 aTypeTLB.SetUpdateMode(sal_True);
191 aTypeTLB.SetSelectHdl(LINK(this, SwFldDokInfPage, TypeHdl));
192 aTypeTLB.SetDoubleClickHdl(LINK(this, SwFldDokInfPage, InsertHdl));
193 aSelectionLB.SetSelectHdl(LINK(this, SwFldDokInfPage, SubTypeHdl));
194 aSelectionLB.SetDoubleClickHdl(LINK(this, SwFldDokInfPage, InsertHdl));
195 aFormatLB.SetDoubleClickHdl(LINK(this, SwFldDokInfPage, InsertHdl));
197 if (IsFldEdit())
199 nOldSel = aSelectionLB.GetSelectEntryPos();
200 nOldFormat = GetCurField()->GetFormat();
201 aFixedCB.SaveValue();
205 IMPL_LINK_NOARG(SwFldDokInfPage, TypeHdl)
207 // save old ListBoxPos
208 SvTreeListEntry* pOldEntry = pSelEntry;
210 // current ListBoxPos
211 pSelEntry = aTypeTLB.FirstSelected();
213 if(!pSelEntry)
215 pSelEntry = aTypeTLB.GetEntry(0);
216 aTypeTLB.Select(pSelEntry);
218 else if (pOldEntry != pSelEntry)
219 FillSelectionLB((sal_uInt16)(sal_uLong)pSelEntry->GetUserData());
221 SubTypeHdl();
223 return 0;
226 IMPL_LINK_NOARG(SwFldDokInfPage, SubTypeHdl)
228 sal_uInt16 nSubType = (sal_uInt16)(sal_uLong)pSelEntry->GetUserData();
229 sal_uInt16 nPos = aSelectionLB.GetSelectEntryPos();
230 sal_uInt16 nExtSubType;
231 sal_uInt16 nNewType = 0;
233 if (nSubType != DI_EDIT)
235 if (nPos == LISTBOX_ENTRY_NOTFOUND)
237 if (!aSelectionLB.GetEntryCount())
239 aFormatLB.Clear();
240 aFormatLB.Enable(sal_False);
241 aFormatFT.Enable(sal_False);
242 if( nSubType == DI_CUSTOM )
244 //find out which type the custom field has - for a start set to DATE format
245 OUString sName = aTypeTLB.GetEntryText(pSelEntry);
248 uno::Any aVal = xCustomPropertySet->getPropertyValue( sName );
249 const uno::Type& rValueType = aVal.getValueType();
250 if( rValueType == ::getCppuType( (util::DateTime*)0 ))
252 nNewType = NUMBERFORMAT_DATETIME;
254 else if( rValueType == ::getCppuType( (util::Date*)0 ))
256 nNewType = NUMBERFORMAT_DATE;
258 else if( rValueType == ::getCppuType( (util::Time*)0 ))
260 nNewType = NUMBERFORMAT_TIME;
263 catch( const uno::Exception& )
267 else
268 return 0;
270 nPos = 0;
273 nExtSubType = (sal_uInt16)(sal_uLong)aSelectionLB.GetEntryData(nPos);
275 else
276 nExtSubType = DI_SUB_TIME;
278 sal_uInt16 nOldType = 0;
279 sal_Bool bEnable = sal_False;
280 sal_Bool bOneArea = sal_False;
282 if (aFormatLB.IsEnabled())
283 nOldType = aFormatLB.GetFormatType();
285 switch (nExtSubType)
287 case DI_SUB_AUTHOR:
288 break;
290 case DI_SUB_DATE:
291 nNewType = NUMBERFORMAT_DATE;
292 bOneArea = sal_True;
293 break;
295 case DI_SUB_TIME:
296 nNewType = NUMBERFORMAT_TIME;
297 bOneArea = sal_True;
298 break;
300 if (!nNewType)
302 aFormatLB.Clear();
304 else
306 if (nOldType != nNewType)
308 aFormatLB.SetFormatType(nNewType);
309 aFormatLB.SetOneArea(bOneArea);
311 bEnable = sal_True;
314 sal_uLong nFormat = IsFldEdit() ? ((SwDocInfoField*)GetCurField())->GetFormat() : 0;
316 sal_uInt16 nOldSubType = IsFldEdit() ? (((SwDocInfoField*)GetCurField())->GetSubType() & 0xff00) : 0;
318 if (IsFldEdit())
320 nPos = aSelectionLB.GetSelectEntryPos();
321 if (nPos != LISTBOX_ENTRY_NOTFOUND )
323 nSubType = (sal_uInt16)(sal_uLong)aSelectionLB.GetEntryData(nPos);
325 nOldSubType &= ~DI_SUB_FIXED;
326 if (nOldSubType == nSubType)
328 if (!nFormat && (nNewType == NUMBERFORMAT_DATE || nNewType == NUMBERFORMAT_TIME))
330 SwWrtShell *pSh = GetWrtShell();
331 if(pSh)
333 SvNumberFormatter* pFormatter = pSh->GetNumberFormatter();
334 LanguageType eLang = aFormatLB.GetCurLanguage();
335 if (nNewType == NUMBERFORMAT_DATE)
336 nFormat = pFormatter->GetFormatIndex( NF_DATE_SYSTEM_SHORT, eLang);
337 else if (nNewType == NUMBERFORMAT_TIME)
338 nFormat = pFormatter->GetFormatIndex( NF_TIME_HHMM, eLang);
341 aFormatLB.SetDefFormat(nFormat);
344 else if( (nSubType == DI_CUSTOM) && (nNewType != 0) )
346 aFormatLB.SetDefFormat(nFormat);
350 aFormatLB.Enable(bEnable);
351 aFormatFT.Enable(bEnable);
353 if (bEnable && aFormatLB.GetSelectEntryPos() == LISTBOX_ENTRY_NOTFOUND)
355 aFormatLB.SelectEntryPos(0);
358 return 0;
361 sal_uInt16 SwFldDokInfPage::FillSelectionLB(sal_uInt16 nSubType)
363 // fill Format-Listbox
364 sal_uInt16 nTypeId = TYP_DOCINFOFLD;
366 EnableInsert(nSubType != USHRT_MAX);
368 if (nSubType == USHRT_MAX) // Info-Text
369 nSubType = DI_SUBTYPE_BEGIN;
371 aSelectionLB.Clear();
373 sal_uInt16 nSize = 0;
374 sal_uInt16 nSelPos = USHRT_MAX;
375 sal_uInt16 nExtSubType = IsFldEdit() ? (((SwDocInfoField*)GetCurField())->GetSubType() & 0xff00) : 0;
377 if (IsFldEdit())
379 aFixedCB.Check((nExtSubType & DI_SUB_FIXED) != 0);
380 nExtSubType = ((nExtSubType & ~DI_SUB_FIXED) >> 8) - 1;
383 if (nSubType < DI_CREATE || nSubType == DI_DOCNO || nSubType == DI_EDIT|| nSubType == DI_CUSTOM )
385 // Format Box is empty for Title and Time
387 else
389 nSize = GetFldMgr().GetFormatCount(nTypeId, false, IsFldDlgHtmlMode());
390 for (sal_uInt16 i = 0; i < nSize; i++)
392 sal_uInt16 nPos = aSelectionLB.InsertEntry(GetFldMgr().GetFormatStr(nTypeId, i));
393 aSelectionLB.SetEntryData(nPos, reinterpret_cast<void*>(GetFldMgr().GetFormatId(nTypeId, i)));
394 if (IsFldEdit() && i == nExtSubType)
395 nSelPos = nPos;
399 sal_Bool bEnable = nSize != 0;
401 if (nSize)
403 if (!aSelectionLB.GetSelectEntryCount())
404 aSelectionLB.SelectEntryPos(nSelPos == USHRT_MAX ? 0 : nSelPos);
406 bEnable = sal_True;
409 aSelectionFT.Enable(bEnable);
410 aSelectionLB.Enable(bEnable);
412 return nSize;
415 sal_Bool SwFldDokInfPage::FillItemSet(SfxItemSet& )
417 if (!pSelEntry || (sal_uInt16)(sal_uLong)pSelEntry->GetUserData() == USHRT_MAX)
418 return sal_False;
420 sal_uInt16 nTypeId = TYP_DOCINFOFLD;
421 sal_uInt16 nSubType = (sal_uInt16)(sal_uLong)pSelEntry->GetUserData();
423 sal_uLong nFormat = 0;
425 sal_uInt16 nPos = aSelectionLB.GetSelectEntryPos();
427 OUString aName;
428 if (DI_CUSTOM == nSubType)
429 aName = aTypeTLB.GetEntryText(pSelEntry);
431 if (nPos != LISTBOX_ENTRY_NOTFOUND)
432 nSubType |= (sal_uInt16)(sal_uLong)aSelectionLB.GetEntryData(nPos);
434 if (aFixedCB.IsChecked())
435 nSubType |= DI_SUB_FIXED;
437 nPos = aFormatLB.GetSelectEntryPos();
438 if(nPos != LISTBOX_ENTRY_NOTFOUND)
439 nFormat = aFormatLB.GetFormat();
441 if (!IsFldEdit() || nOldSel != aSelectionLB.GetSelectEntryPos() ||
442 nOldFormat != nFormat || aFixedCB.GetState() != aFixedCB.GetSavedValue()
443 || (DI_CUSTOM == nSubType && !aName.equals( m_sOldCustomFieldName )))
445 InsertFld(nTypeId, nSubType, aName, aEmptyStr, nFormat,
446 ' ', aFormatLB.IsAutomaticLanguage());
449 return sal_False;
452 SfxTabPage* SwFldDokInfPage::Create( Window* pParent,
453 const SfxItemSet& rAttrSet )
455 return ( new SwFldDokInfPage( pParent, rAttrSet ) );
458 sal_uInt16 SwFldDokInfPage::GetGroup()
460 return GRP_REG;
463 void SwFldDokInfPage::FillUserData()
465 String sData(OUString(USER_DATA_VERSION));
466 sData += ';';
467 SvTreeListEntry* pEntry = aTypeTLB.FirstSelected();
468 sal_uInt16 nTypeSel = pEntry ? sal::static_int_cast< sal_uInt16 >(reinterpret_cast< sal_uIntPtr >(pEntry->GetUserData())) : USHRT_MAX;
469 sData += OUString::number( nTypeSel );
470 SetUserData(sData);
475 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */