xref: /aoo4110/main/sw/source/ui/fldui/flddinf.cxx (revision b1cdbd2c)
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_sw.hxx"
26 
27 #ifdef SW_DLLIMPLEMENTATION
28 #undef SW_DLLIMPLEMENTATION
29 #endif
30 
31 #include <sfx2/request.hxx>
32 #include <sfx2/frame.hxx>
33 #include <vcl/svapp.hxx>
34 #include <svl/zforlist.hxx>
35 #include <svl/zformat.hxx>
36 
37 #include <helpid.h>
38 #include <swtypes.hxx>
39 #include <globals.hrc>
40 #include <fldbas.hxx>
41 #include <docufld.hxx>
42 #include <wrtsh.hxx>
43 
44 #include <fldui.hrc>
45 
46 #ifndef _FLDTDLG_HRC
47 #include <fldtdlg.hrc>
48 #endif
49 #ifndef _FLDDINF_HXX
50 #include <flddinf.hxx>
51 #endif
52 #include <swmodule.hxx>
53 #ifndef _VIEW_HXX
54 #include <view.hxx>
55 #endif
56 #include <com/sun/star/beans/XPropertySet.hpp>
57 #include <com/sun/star/util/Time.hpp>
58 #include <com/sun/star/util/DateTime.hpp>
59 #include <com/sun/star/util/Date.hpp>
60 
61 #define USER_DATA_VERSION_1 "1"
62 #define USER_DATA_VERSION USER_DATA_VERSION_1
63 
64 using namespace nsSwDocInfoSubType;
65 using namespace com::sun::star;
66 /*--------------------------------------------------------------------
67 	Beschreibung:
68  --------------------------------------------------------------------*/
69 
SwFldDokInfPage(Window * pWindow,const SfxItemSet & rCoreSet)70 SwFldDokInfPage::SwFldDokInfPage(Window* pWindow, const SfxItemSet& rCoreSet ) :
71 	SwFldPage( pWindow, SW_RES( TP_FLD_DOKINF ), rCoreSet ),
72 
73 	aTypeFT		(this, SW_RES(FT_DOKINFTYPE)),
74 	aTypeTLB	(this, SW_RES(TLB_DOKINFTYPE)),
75 	aSelectionFT(this, SW_RES(FT_DOKINFSELECTION)),
76 	aSelectionLB(this, SW_RES(LB_DOKINFSELECTION)),
77 	aFormatFT	(this, SW_RES(FT_DOKINFFORMAT)),
78 	aFormatLB	(this, SW_RES(LB_DOKINFFORMAT)),
79 	aFixedCB	(this, SW_RES(CB_DOKINFFIXEDCONTENT)),
80 
81     pSelEntry   (0),
82     aInfoStr    (SW_RES(STR_DOKINF_INFO))
83 {
84 	FreeResource();
85 
86 	aTypeTLB.SetHelpId(HID_FIELD_DINF_TYPE);
87 	aTypeTLB.SetSelectionMode(SINGLE_SELECTION);
88 	aTypeTLB.SetStyle(aTypeTLB.GetStyle()|WB_HASLINES|WB_CLIPCHILDREN|WB_SORT|WB_HASBUTTONS|WB_HASBUTTONSATROOT|WB_HSCROLL);
89 	// Font nicht setzen, damit der Font des Controls uebernommen wird!
90 	// Sonst bei falschem Font Bug an OV.
91 	aTypeTLB.SetSpaceBetweenEntries(0);
92 
93 	aTypeTLB.SetNodeDefaultImages();
94     //enable 'active' language selection
95     aFormatLB.SetShowLanguageControl(sal_True);
96 
97 	SFX_ITEMSET_ARG( &rCoreSet, pItem, SfxUnoAnyItem, SID_DOCINFO, sal_False );
98 	if ( pItem )
99         pItem->GetValue() >>= xCustomPropertySet;
100 }
101 
102 /*--------------------------------------------------------------------
103 	Beschreibung:
104  --------------------------------------------------------------------*/
105 
~SwFldDokInfPage()106 __EXPORT SwFldDokInfPage::~SwFldDokInfPage()
107 {
108 }
109 
110 /*--------------------------------------------------------------------
111 	Beschreibung:
112  --------------------------------------------------------------------*/
113 
Reset(const SfxItemSet &)114 void __EXPORT SwFldDokInfPage::Reset(const SfxItemSet& )
115 {
116 	Init();	// Allgemeine initialisierung
117 
118 	// TypeListBox initialisieren
119 	aTypeTLB.SetUpdateMode(sal_False);
120 	aTypeTLB.Clear();
121 	pSelEntry = 0;
122 
123 	// SubTypes in der TypeLB anzeigen
124 	sal_uInt16 nTypeId = TYP_DOCINFOFLD;
125 	SvLBoxEntry* pEntry = 0;
126 
127 	SvLBoxEntry* pInfo = 0;
128 
129 	sal_uInt16 nSubType = USHRT_MAX;
130 	if (IsFldEdit())
131     {
132         const SwField* pCurField = GetCurField();
133         nSubType = ((SwDocInfoField*)pCurField)->GetSubType() & 0xff;
134         if( nSubType == DI_CUSTOM )
135         {
136             m_sOldCustomFieldName = static_cast<const SwDocInfoField*>(pCurField)->GetName();
137         }
138         aFormatLB.SetAutomaticLanguage(pCurField->IsAutomaticLanguage());
139         SwWrtShell *pSh = GetWrtShell();
140         if(pSh)
141         {
142             const SvNumberformat* pFormat = pSh->GetNumberFormatter()->GetEntry(pCurField->GetFormat());
143             if(pFormat)
144                 aFormatLB.SetLanguage(pFormat->GetLanguage());
145         }
146     }
147 
148 	sal_uInt16 nSelEntryData = USHRT_MAX;
149 	String sUserData = GetUserData();
150 	if(sUserData.GetToken(0, ';').EqualsIgnoreCaseAscii(USER_DATA_VERSION_1))
151 	{
152 		String sVal = sUserData.GetToken(1, ';');
153         nSelEntryData = static_cast< sal_uInt16 >(sVal.ToInt32());
154 	}
155 
156     SvStringsDtor aLst;
157     GetFldMgr().GetSubTypes(nTypeId, aLst);
158     for (sal_uInt16 i = 0; i < aLst.Count(); ++i)
159 	{
160 		if (!IsFldEdit() || nSubType == i)
161 		{
162 			if (DI_CUSTOM == i)
163 			{
164                 if(xCustomPropertySet.is() )
165 				{
166                     uno::Reference< beans::XPropertySetInfo > xSetInfo = xCustomPropertySet->getPropertySetInfo();
167                     const uno::Sequence< beans::Property > rProperties = xSetInfo->getProperties();
168 //                    uno::Sequence< ::rtl::OUString > aPropertyNames(rProperties.getLength());
169 //                    for (sal_Int32 i = 0; i < rProperties.getLength(); ++i) {
170 //                        aPropertyNames[i] = rProperties[i].Name;
171 //                    }
172                     //if ( !IsFldEdit() )
173                     if( rProperties.getLength() )
174                     {
175                         pInfo = aTypeTLB.InsertEntry( String(SW_RES( STR_CUSTOM )) );
176 						pInfo->SetUserData(reinterpret_cast<void*>(USHRT_MAX));
177 
178                         for (sal_Int32 n=0; n < rProperties.getLength(); n++)
179                         {
180                             rtl::OUString sEntry = rProperties[n].Name;
181                             pEntry = aTypeTLB.InsertEntry(sEntry, pInfo);
182                             if(m_sOldCustomFieldName.equals( sEntry ))
183                             {
184                                 pSelEntry = pEntry;
185                                 aTypeTLB.Expand( pInfo );
186                             }
187                             pEntry->SetUserData(reinterpret_cast<void*>(i));
188                         }
189                     }
190 				}
191 			}
192 			else
193 			{
194 				if (!(IsFldDlgHtmlMode() && (i == DI_EDIT || i == DI_THEMA || i == DI_PRINT)))
195 				{
196                     pEntry = aTypeTLB.InsertEntry(*aLst[i]);
197                     pEntry->SetUserData(reinterpret_cast<void*>(i));
198 				}
199 			}
200 			if(nSelEntryData == i)
201 				pSelEntry = pEntry;
202 		}
203 	}
204 
205 	// alte Pos selektieren
206 	if (pSelEntry != 0)
207 	{
208 		aTypeTLB.Select(pSelEntry);
209 		nSubType = (sal_uInt16)(sal_uLong)pSelEntry->GetUserData();
210 	}
211     else if ( aTypeTLB.GetEntry(0) )
212 	{
213 		pSelEntry = aTypeTLB.GetEntry(0);
214 		nSubType = (sal_uInt16)(sal_uLong)pSelEntry->GetUserData();
215 	}
216 
217 	FillSelectionLB(nSubType);
218     if ( pSelEntry )
219         TypeHdl();
220 
221 	aTypeTLB.SetUpdateMode(sal_True);
222 	aTypeTLB.SetSelectHdl(LINK(this, SwFldDokInfPage, TypeHdl));
223 	aTypeTLB.SetDoubleClickHdl(LINK(this, SwFldDokInfPage, InsertHdl));
224 	aSelectionLB.SetSelectHdl(LINK(this, SwFldDokInfPage, SubTypeHdl));
225 	aSelectionLB.SetDoubleClickHdl(LINK(this, SwFldDokInfPage, InsertHdl));
226 	aFormatLB.SetDoubleClickHdl(LINK(this, SwFldDokInfPage, InsertHdl));
227 
228 	if (IsFldEdit())
229 	{
230 		nOldSel = aSelectionLB.GetSelectEntryPos();
231 		nOldFormat = GetCurField()->GetFormat();
232 		aFixedCB.SaveValue();
233 	}
234 }
235 
236 /*--------------------------------------------------------------------
237 	Beschreibung:
238  --------------------------------------------------------------------*/
239 
IMPL_LINK(SwFldDokInfPage,TypeHdl,ListBox *,EMPTYARG)240 IMPL_LINK( SwFldDokInfPage, TypeHdl, ListBox *, EMPTYARG )
241 {
242 	// Alte ListBoxPos sichern
243 	SvLBoxEntry* pOldEntry = pSelEntry;
244 
245 	// Aktuelle ListBoxPos
246 	pSelEntry = aTypeTLB.FirstSelected();
247 
248 	if(!pSelEntry)
249 	{
250 		pSelEntry = aTypeTLB.GetEntry(0);
251 		aTypeTLB.Select(pSelEntry);
252 	}
253 	else
254 
255 	if (pOldEntry != pSelEntry)
256 		FillSelectionLB((sal_uInt16)(sal_uLong)pSelEntry->GetUserData());
257 
258 	SubTypeHdl();
259 
260 	return 0;
261 }
262 
263 /*--------------------------------------------------------------------
264 	Beschreibung:
265  --------------------------------------------------------------------*/
IMPL_LINK(SwFldDokInfPage,SubTypeHdl,ListBox *,EMPTYARG)266 IMPL_LINK( SwFldDokInfPage, SubTypeHdl, ListBox *, EMPTYARG )
267 {
268 	sal_uInt16 nSubType = (sal_uInt16)(sal_uLong)pSelEntry->GetUserData();
269 	sal_uInt16 nPos = aSelectionLB.GetSelectEntryPos();
270 	sal_uInt16 nExtSubType;
271 	sal_uInt16 nNewType = 0;
272 
273 	if (nSubType != DI_EDIT)
274 	{
275 		if (nPos == LISTBOX_ENTRY_NOTFOUND)
276 		{
277 			if (!aSelectionLB.GetEntryCount())
278 			{
279 				aFormatLB.Clear();
280 				aFormatLB.Enable(sal_False);
281 				aFormatFT.Enable(sal_False);
282                 if( nSubType == DI_CUSTOM )
283                 {
284                     //find out which type the custom field has - for a start set to DATE format
285                     ::rtl::OUString sName = aTypeTLB.GetEntryText(pSelEntry);
286                     try
287                     {
288                         uno::Any aVal = xCustomPropertySet->getPropertyValue( sName );
289                         const uno::Type& rValueType = aVal.getValueType();
290                         if( rValueType == ::getCppuType( (util::DateTime*)0 ))
291                         {
292                             nNewType = NUMBERFORMAT_DATETIME;
293                         }
294                         else if( rValueType == ::getCppuType( (util::Date*)0 ))
295                         {
296                             nNewType = NUMBERFORMAT_DATE;
297                         }
298                         else if( rValueType == ::getCppuType( (util::Time*)0 ))
299                         {
300                             nNewType = NUMBERFORMAT_TIME;
301                         }
302                     }
303                     catch( const uno::Exception& )
304                     {
305                     }
306                 }
307                 else
308                     return 0;
309 			}
310 			nPos = 0;
311 		}
312 
313 		nExtSubType = (sal_uInt16)(sal_uLong)aSelectionLB.GetEntryData(nPos);
314 	}
315 	else
316 		nExtSubType = DI_SUB_TIME;
317 
318 	sal_uInt16 nOldType = 0;
319 	sal_Bool bEnable = sal_False;
320 	sal_Bool bOneArea = sal_False;
321 
322 	if (aFormatLB.IsEnabled())
323 		nOldType = aFormatLB.GetFormatType();
324 
325 	switch (nExtSubType)
326 	{
327 		case DI_SUB_AUTHOR:
328 			break;
329 
330 		case DI_SUB_DATE:
331 			nNewType = NUMBERFORMAT_DATE;
332 			bOneArea = sal_True;
333 			break;
334 
335 		case DI_SUB_TIME:
336 			nNewType = NUMBERFORMAT_TIME;
337 			bOneArea = sal_True;
338 			break;
339 	}
340 	if (!nNewType)
341 	{
342 		aFormatLB.Clear();
343 	}
344 	else
345 	{
346 		if (nOldType != nNewType)
347 		{
348 			aFormatLB.SetFormatType(nNewType);
349 			aFormatLB.SetOneArea(bOneArea);
350 		}
351 		bEnable = sal_True;
352 	}
353 
354 	sal_uLong nFormat = IsFldEdit() ? ((SwDocInfoField*)GetCurField())->GetFormat() : 0;
355 
356 	sal_uInt16 nOldSubType = IsFldEdit() ? (((SwDocInfoField*)GetCurField())->GetSubType() & 0xff00) : 0;
357 
358 	if (IsFldEdit())
359 	{
360         nPos = aSelectionLB.GetSelectEntryPos();
361 		if (nPos != LISTBOX_ENTRY_NOTFOUND )
362 		{
363 			nSubType = (sal_uInt16)(sal_uLong)aSelectionLB.GetEntryData(nPos);
364 
365 			nOldSubType &= ~DI_SUB_FIXED;
366 			if (nOldSubType == nSubType)
367 			{
368 				if (!nFormat && (nNewType == NUMBERFORMAT_DATE || nNewType == NUMBERFORMAT_TIME))
369 				{
370                     SwWrtShell *pSh = GetWrtShell();
371                     if(pSh)
372                     {
373                         SvNumberFormatter* pFormatter = pSh->GetNumberFormatter();
374                         LanguageType eLang = aFormatLB.GetCurLanguage();
375                         if (nNewType == NUMBERFORMAT_DATE)
376                             nFormat = pFormatter->GetFormatIndex( NF_DATE_SYSTEM_SHORT, eLang);
377                         else if (nNewType == NUMBERFORMAT_TIME)
378                             nFormat = pFormatter->GetFormatIndex( NF_TIME_HHMM, eLang);
379                     }
380 				}
381 				aFormatLB.SetDefFormat(nFormat);
382 			}
383 		}
384         else if( (nSubType == DI_CUSTOM)  && (nNewType != 0) )
385         {
386     		aFormatLB.SetDefFormat(nFormat);
387         }
388 	}
389 
390 	aFormatLB.Enable(bEnable);
391 	aFormatFT.Enable(bEnable);
392 
393 	if (bEnable && aFormatLB.GetSelectEntryPos() == LISTBOX_ENTRY_NOTFOUND)
394 	{
395 		aFormatLB.SelectEntryPos(0);
396 	}
397 
398 	return 0;
399 }
400 
401 /*--------------------------------------------------------------------
402 	Beschreibung:
403  --------------------------------------------------------------------*/
404 
FillSelectionLB(sal_uInt16 nSubType)405 sal_uInt16 SwFldDokInfPage::FillSelectionLB(sal_uInt16 nSubType)
406 {
407 	// Format-Listbox fuellen
408 	sal_uInt16 nTypeId = TYP_DOCINFOFLD;
409 
410 	EnableInsert(nSubType != USHRT_MAX);
411 
412 	if (nSubType == USHRT_MAX)	// Info-Text
413 		nSubType = DI_SUBTYPE_BEGIN;
414 
415 	aSelectionLB.Clear();
416 
417 	sal_uInt16 nSize = 0;
418 	sal_uInt16 nSelPos = USHRT_MAX;
419 	sal_uInt16 nExtSubType = IsFldEdit() ? (((SwDocInfoField*)GetCurField())->GetSubType() & 0xff00) : 0;
420 
421 	if (IsFldEdit())
422 	{
423 		aFixedCB.Check((nExtSubType & DI_SUB_FIXED) != 0);
424 		nExtSubType = ((nExtSubType & ~DI_SUB_FIXED) >> 8) - 1;
425 	}
426 
427 	if (nSubType < DI_CREATE || nSubType == DI_DOCNO || nSubType == DI_EDIT|| nSubType == DI_CUSTOM )
428 	{
429 		// Format Box ist fuer Title und Time leer
430 	}
431 	else
432 	{
433 		nSize = GetFldMgr().GetFormatCount(nTypeId, sal_False, IsFldDlgHtmlMode());
434 		for (sal_uInt16 i = 0; i < nSize; i++)
435 		{
436 			sal_uInt16 nPos = aSelectionLB.InsertEntry(GetFldMgr().GetFormatStr(nTypeId, i));
437             aSelectionLB.SetEntryData(nPos, reinterpret_cast<void*>(GetFldMgr().GetFormatId(nTypeId, i)));
438 			if (IsFldEdit() && i == nExtSubType)
439 				nSelPos = nPos;
440 		}
441 	}
442 
443 	sal_Bool bEnable = nSize != 0;
444 
445 	if (nSize)
446 	{
447 		if (!aSelectionLB.GetSelectEntryCount())
448 			aSelectionLB.SelectEntryPos(nSelPos == USHRT_MAX ? 0 : nSelPos);
449 
450 		bEnable = sal_True;
451 	}
452 
453 	aSelectionFT.Enable(bEnable);
454 	aSelectionLB.Enable(bEnable);
455 
456 	return nSize;
457 }
458 
459 /*--------------------------------------------------------------------
460 	Beschreibung:
461  --------------------------------------------------------------------*/
462 
FillItemSet(SfxItemSet &)463 sal_Bool __EXPORT SwFldDokInfPage::FillItemSet(SfxItemSet& )
464 {
465 	if (!pSelEntry || (sal_uInt16)(sal_uLong)pSelEntry->GetUserData() == USHRT_MAX)
466 		return sal_False;
467 
468 	sal_uInt16 nTypeId = TYP_DOCINFOFLD;
469 	sal_uInt16 nSubType = (sal_uInt16)(sal_uLong)pSelEntry->GetUserData();
470 
471 	sal_uLong nFormat = 0;
472 
473 	sal_uInt16 nPos = aSelectionLB.GetSelectEntryPos();
474 
475     ::rtl::OUString aName;
476     if (DI_CUSTOM == nSubType)
477         aName = aTypeTLB.GetEntryText(pSelEntry);
478 
479 	if (nPos != LISTBOX_ENTRY_NOTFOUND)
480 		nSubType |= (sal_uInt16)(sal_uLong)aSelectionLB.GetEntryData(nPos);
481 
482 	if (aFixedCB.IsChecked())
483 		nSubType |= DI_SUB_FIXED;
484 
485 	nPos = aFormatLB.GetSelectEntryPos();
486 	if(nPos != LISTBOX_ENTRY_NOTFOUND)
487 		nFormat = aFormatLB.GetFormat();
488 
489 	if (!IsFldEdit() || nOldSel != aSelectionLB.GetSelectEntryPos() ||
490 		nOldFormat != nFormat || aFixedCB.GetState() != aFixedCB.GetSavedValue()
491         || (DI_CUSTOM == nSubType && !aName.equals( m_sOldCustomFieldName )))
492 	{
493         InsertFld(nTypeId, nSubType, aName, aEmptyStr, nFormat,
494                 ' ', aFormatLB.IsAutomaticLanguage());
495 	}
496 
497 	return sal_False;
498 }
499 
500 /*--------------------------------------------------------------------
501 	Beschreibung:
502  --------------------------------------------------------------------*/
503 
Create(Window * pParent,const SfxItemSet & rAttrSet)504 SfxTabPage* __EXPORT SwFldDokInfPage::Create( 	Window* pParent,
505 						const SfxItemSet& rAttrSet )
506 {
507 	return ( new SwFldDokInfPage( pParent, rAttrSet ) );
508 }
509 
510 /*--------------------------------------------------------------------
511 	Beschreibung:
512  --------------------------------------------------------------------*/
513 
GetGroup()514 sal_uInt16 SwFldDokInfPage::GetGroup()
515 {
516 	return GRP_REG;
517 }
518 /* -----------------12.01.99 11:21-------------------
519  *
520  * --------------------------------------------------*/
FillUserData()521 void	SwFldDokInfPage::FillUserData()
522 {
523 	String sData( String::CreateFromAscii(
524 							RTL_CONSTASCII_STRINGPARAM( USER_DATA_VERSION )));
525 	sData += ';';
526 	SvLBoxEntry* pEntry = aTypeTLB.FirstSelected();
527     sal_uInt16 nTypeSel = pEntry ? sal::static_int_cast< sal_uInt16 >(reinterpret_cast< sal_uIntPtr >(pEntry->GetUserData())) : USHRT_MAX;
528 	sData += String::CreateFromInt32( nTypeSel );
529 	SetUserData(sData);
530 }
531 
532 
533 
534