Version 3.6.0.4, tag libreoffice-3.6.0.4
[LibreOffice.git] / sfx2 / source / dialog / mgetempl.cxx
blob2e377bb52af5be8d3d159ace8180818ab15cf1f2
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
30 // include ---------------------------------------------------------------
31 #include <vcl/msgbox.hxx>
32 #include <vcl/field.hxx>
33 #include <svl/eitem.hxx>
34 #include <svl/intitem.hxx>
35 #include <svl/style.hxx>
37 #include <sfx2/styfitem.hxx>
38 #include <sfx2/styledlg.hxx>
39 #include <sfx2/app.hxx>
40 #include <sfx2/mgetempl.hxx>
41 #include <sfx2/objsh.hxx>
42 #include "sfxtypes.hxx"
43 #include "sfx2/sfxresid.hxx"
44 #include <sfx2/module.hxx>
46 #include <sfx2/sfx.hrc>
47 #include "dialog.hrc"
48 #include "mgetempl.hrc"
50 #include <svl/style.hrc>
52 // SfxManageStyleSheetPage -----------------------------------------------
54 SfxManageStyleSheetPage::SfxManageStyleSheetPage( Window* pParent, const SfxItemSet& rAttrSet ) :
56 SfxTabPage( pParent, SfxResId( TP_MANAGE_STYLES ), rAttrSet ),
58 aNameFt ( this, SfxResId( FT_NAME ) ),
59 aNameEd ( this, SfxResId( ED_NAME ) ),
60 aNameMLE ( this, SfxResId( MLE_NAME ) ),
61 aAutoCB ( this, SfxResId( CB_AUTO ) ),
63 aFollowFt ( this, SfxResId( FT_NEXT ) ),
64 aFollowLb ( this, SfxResId( LB_NEXT ) ),
66 aBaseFt ( this, SfxResId( FT_BASE ) ),
67 aBaseLb ( this, SfxResId( LB_BASE ) ),
69 aFilterFt ( this, SfxResId( FT_REGION ) ),
70 aFilterLb ( this, SfxResId( LB_REGION ) ),
72 aDescGb ( this, SfxResId( GB_DESC ) ),
73 aDescFt ( this, SfxResId( FT_DESC ) ),
74 aDescED ( this, SfxResId( ED_DESC ) ),
76 pStyle( &( (SfxStyleDialog*)pParent->GetParent() )->GetStyleSheet() ),
78 pItem ( 0 ),
79 bModified ( sal_False ),
80 aName ( pStyle->GetName() ),
81 aFollow ( pStyle->GetFollow() ),
82 aParent ( pStyle->GetParent() ),
83 nFlags ( pStyle->GetMask() )
85 /* [Description]
87 Constructor, initializes the list box with the templates
91 FreeResource();
93 if ( IsBackground() )
94 aDescED.SetBackground( GetBackground() );
95 FixedInfo* pInfo = new FixedInfo( this );
96 aDescED.SetFont( pInfo->GetFont() );
97 delete pInfo;
99 aDescED.Hide();
100 aDescFt.Show();
102 // this Page needs ExchangeSupport
103 SetExchangeSupport();
105 ResMgr* pResMgr = SFX_APP()->GetModule_Impl()->GetResMgr();
106 OSL_ENSURE( pResMgr, "No ResMgr in Module" );
107 pFamilies = new SfxStyleFamilies( ResId( DLG_STYLE_DESIGNER, *pResMgr ) );
109 SfxStyleSheetBasePool* pPool = 0;
110 SfxObjectShell* pDocShell = SfxObjectShell::Current();
112 if ( pDocShell )
113 pPool = pDocShell->GetStyleSheetPool();
114 OSL_ENSURE( pPool, "no Pool or no DocShell" );
116 if ( pPool )
118 pPool->SetSearchMask( pStyle->GetFamily() );
119 pPool->First(); // for SW - update internal list
122 if ( !pStyle->GetName().Len() && pPool )
124 // NullString as Name -> generate Name
125 String aNoName( SfxResId( STR_NONAME ) );
126 sal_uInt16 nNo = 1;
127 String aNo( aNoName );
128 aNoName += String::CreateFromInt32( nNo );
129 while ( pPool->Find( aNoName ) )
131 ++nNo;
132 aNoName = aNo;
133 aNoName += String::CreateFromInt32( nNo );
135 pStyle->SetName( aNoName );
136 aName = aNoName;
137 aFollow = pStyle->GetFollow();
138 aParent = pStyle->GetParent();
140 aNameEd.SetText(pStyle->GetName());
142 // Set the field read-only if it is NOT an user-defined style
143 // but allow selecting and copying
144 if ( !pStyle->IsUserDefined() ) {
145 aNameEd.SetReadOnly();
146 aNameEd.Hide();
148 aNameMLE.SetControlBackground( GetSettings().GetStyleSettings().GetDialogColor() );
149 aNameMLE.SetText( pStyle->GetName() );
150 aNameMLE.EnableCursor( sal_False );
151 aNameMLE.Show();
154 if ( pStyle->HasFollowSupport() && pPool )
156 SfxStyleSheetBase* pPoolStyle = pPool->First();
158 while ( pPoolStyle )
160 aFollowLb.InsertEntry( pPoolStyle->GetName() );
161 pPoolStyle = pPool->Next();
164 // A new Template is not jet in the Pool
165 if ( LISTBOX_ENTRY_NOTFOUND == aFollowLb.GetEntryPos( pStyle->GetName() ) )
166 aFollowLb.InsertEntry( pStyle->GetName() );
168 else
170 aFollowFt.Hide();
171 aFollowLb.Hide();
173 aFilterFt.SetPosPixel( aBaseFt.GetPosPixel() );
174 aFilterLb.SetPosPixel( aBaseLb.GetPosPixel() );
176 aBaseFt.SetPosPixel( aFollowFt.GetPosPixel() );
177 aBaseLb.SetPosPixel( aFollowLb.GetPosPixel() );
180 if ( pStyle->HasParentSupport() && pPool )
182 if ( pStyle->HasClearParentSupport() )
183 // the base template can be set to NULL
184 aBaseLb.InsertEntry( String( SfxResId( STR_NONE ) ) );
186 SfxStyleSheetBase* pPoolStyle = pPool->First();
188 while ( pPoolStyle )
190 const String aStr( pPoolStyle->GetName() );
191 // own name as base template
192 if ( aStr != aName )
193 aBaseLb.InsertEntry( aStr );
194 pPoolStyle = pPool->Next();
197 else
199 aBaseFt.Disable();
200 aBaseLb.Disable();
203 size_t nCount = pFamilies->size();
204 size_t i;
205 for ( i = 0; i < nCount; ++i )
207 pItem = pFamilies->at( i );
209 if ( pItem->GetFamily() == pStyle->GetFamily() )
210 break;
213 if ( i < nCount )
215 sal_uInt16 nStyleFilterIdx = 0xffff;
216 // Filter flags
217 const SfxStyleFilter& rList = pItem->GetFilterList();
218 nCount = rList.size();
219 sal_uInt16 nIdx = 0;
220 sal_uInt16 nMask = pStyle->GetMask() & ~SFXSTYLEBIT_USERDEF;
222 if ( !nMask ) // User Template?
223 nMask = pStyle->GetMask();
225 for ( i = 0; i < nCount; ++i )
227 SfxFilterTupel* pTupel = rList[ i ];
229 if ( pTupel->nFlags != SFXSTYLEBIT_AUTO &&
230 pTupel->nFlags != SFXSTYLEBIT_USED &&
231 pTupel->nFlags != SFXSTYLEBIT_ALL )
233 aFilterLb.InsertEntry( pTupel->aName, nIdx );
234 aFilterLb.SetEntryData(nIdx, (void*)(long)i);
236 if ( ( pTupel->nFlags & nMask ) == nMask )
237 nStyleFilterIdx = nIdx;
238 ++nIdx;
242 if ( nStyleFilterIdx != 0xFFFF )
243 aFilterLb.SelectEntryPos( nStyleFilterIdx );
246 if ( !aFilterLb.GetEntryCount() || !pStyle->IsUserDefined() )
248 pItem = 0;
249 aFilterFt.Disable();
250 aFilterLb.Disable();
252 else
253 aFilterLb.SaveValue();
254 SetDescriptionText_Impl();
256 if ( aFollowLb.IsEnabled() || aBaseLb.IsEnabled() )
258 aNameEd.SetGetFocusHdl(
259 LINK( this, SfxManageStyleSheetPage, GetFocusHdl ) );
260 aNameEd.SetLoseFocusHdl(
261 LINK( this, SfxManageStyleSheetPage, LoseFocusHdl ) );
263 // It is a style with auto update? (SW only)
264 if(SFX_ITEM_SET == rAttrSet.GetItemState(SID_ATTR_AUTO_STYLE_UPDATE))
266 Size aSize = aNameEd.GetSizePixel();
267 aSize.Width() /= 2;
268 aNameEd.SetSizePixel(aSize);
269 aAutoCB.Show();
273 //-------------------------------------------------------------------------
275 SfxManageStyleSheetPage::~SfxManageStyleSheetPage()
277 /* [Description]
279 Destructor, release of the data
283 aNameEd.SetGetFocusHdl( Link() );
284 aNameEd.SetLoseFocusHdl( Link() );
285 delete pFamilies;
286 pItem = 0;
287 pStyle = 0;
291 //-------------------------------------------------------------------------
293 void SfxManageStyleSheetPage::UpdateName_Impl( ListBox* pBox,
294 const String& rNew )
296 /* [Description]
298 After the change of a template name update the ListBox pBox
300 [Parameter]
302 ListBox* pBox ListBox, whose entries are to be updated
303 const String& rNew the new Name
307 if ( pBox->IsEnabled() )
309 // it is the current entry, which name was modified
310 const sal_Bool bSelect = pBox->GetSelectEntry() == aBuf;
311 pBox->RemoveEntry( aBuf );
312 pBox->InsertEntry( rNew );
314 if ( bSelect )
315 pBox->SelectEntry( rNew );
319 //-------------------------------------------------------------------------
321 void SfxManageStyleSheetPage::SetDescriptionText_Impl()
323 /* [Description]
325 Set attribute description. Get the set metric for this.
329 SfxMapUnit eUnit = SFX_MAPUNIT_CM;
330 FieldUnit eFieldUnit( FUNIT_CM );
331 SfxModule* pModule = SfxModule::GetActiveModule();
332 if ( pModule )
334 const SfxPoolItem* pPoolItem = pModule->GetItem( SID_ATTR_METRIC );
335 if ( pPoolItem )
336 eFieldUnit = (FieldUnit)( (SfxUInt16Item*)pPoolItem )->GetValue();
339 switch ( eFieldUnit )
341 case FUNIT_MM: eUnit = SFX_MAPUNIT_MM; break;
342 case FUNIT_CM:
343 case FUNIT_M:
344 case FUNIT_KM: eUnit = SFX_MAPUNIT_CM; break;
345 case FUNIT_POINT:
346 case FUNIT_PICA: eUnit = SFX_MAPUNIT_POINT; break;
347 case FUNIT_INCH:
348 case FUNIT_FOOT:
349 case FUNIT_MILE: eUnit = SFX_MAPUNIT_INCH; break;
351 default:
352 OSL_FAIL( "non supported field unit" );
354 aDescFt.SetText( pStyle->GetDescription( eUnit ) );
357 //-------------------------------------------------------------------------
359 IMPL_LINK_INLINE_START( SfxManageStyleSheetPage, GetFocusHdl, Edit *, pEdit )
361 /* [Description]
363 StarView Handler; GetFocus-Handler of the Edits with the template name.
367 aBuf = pEdit->GetText().EraseLeadingChars();
368 return 0;
370 IMPL_LINK_INLINE_END( SfxManageStyleSheetPage, GetFocusHdl, Edit *, pEdit )
372 //-------------------------------------------------------------------------
374 IMPL_LINK_INLINE_START( SfxManageStyleSheetPage, LoseFocusHdl, Edit *, pEdit )
376 /* [Description]
378 StarView Handler; loose-focus-handler of the edits of the template name.
379 This will update the listbox with the subsequent templates. The current
380 template itself is not returned in the listbox of the base templates.
384 const String aStr( pEdit->GetText().EraseLeadingChars() );
385 pEdit->SetText( aStr );
386 // Update the Listbox of the base template if possible
387 if ( aStr != aBuf )
388 UpdateName_Impl( &aFollowLb, aStr );
389 return 0;
391 IMPL_LINK_INLINE_END( SfxManageStyleSheetPage, LoseFocusHdl, Edit *, pEdit )
393 //-------------------------------------------------------------------------
395 sal_Bool SfxManageStyleSheetPage::FillItemSet( SfxItemSet& rSet )
397 /* [Description]
399 Handler for setting the (modified) data. I called from the OK of the
400 SfxTabDialog.
402 [Parameter]
404 SfxItemSet &rAttrSet The set, which receives the data.
406 [Return value]
408 sal_Bool sal_True: The data had been changed
409 sal_False: The data had not been changed
411 [Cross-reference]
413 <class SfxTabDialog>
417 const sal_uInt16 nFilterIdx = aFilterLb.GetSelectEntryPos();
419 // Set Filter
421 if ( LISTBOX_ENTRY_NOTFOUND != nFilterIdx &&
422 nFilterIdx != aFilterLb.GetSavedValue() &&
423 aFilterLb.IsEnabled() )
425 bModified = sal_True;
426 OSL_ENSURE( pItem, "No Item" );
427 // is only possibly for user templates
428 sal_uInt16 nMask = pItem->GetFilterList()[ (size_t)aFilterLb.GetEntryData( nFilterIdx ) ]->nFlags | SFXSTYLEBIT_USERDEF;
429 pStyle->SetMask( nMask );
431 if(aAutoCB.IsVisible() &&
432 aAutoCB.IsChecked() != aAutoCB.GetSavedValue())
434 rSet.Put(SfxBoolItem(SID_ATTR_AUTO_STYLE_UPDATE, aAutoCB.IsChecked()));
437 return bModified;
440 //-------------------------------------------------------------------------
442 void SfxManageStyleSheetPage::Reset( const SfxItemSet& /*rAttrSet*/ )
444 /* [Description]
446 Handler to initialize the page with the initial data.
448 [Parameter]
450 const SfxItemSet &rAttrSet The data set
452 [Cross-reference]
454 <class SfxTabDialog>
458 bModified = sal_False;
459 String sCmp( pStyle->GetName() );
461 if ( sCmp != aName )
462 pStyle->SetName( aName );
463 aNameEd.SetText( aName );
465 if ( aFollowLb.IsEnabled() )
467 sCmp = pStyle->GetFollow();
469 if ( sCmp != aFollow )
470 pStyle->SetFollow( aFollow );
472 if ( !aFollow.Len() )
473 aFollowLb.SelectEntry( aName );
474 else
475 aFollowLb.SelectEntry( aFollow );
478 if ( aBaseLb.IsEnabled() )
480 sCmp = pStyle->GetParent();
482 if ( sCmp != aParent )
483 pStyle->SetParent( aParent );
485 if ( !aParent.Len() )
486 aBaseLb.SelectEntry( String( SfxResId( STR_NONE ) ) );
487 else
488 aBaseLb.SelectEntry( aParent );
490 if ( String( SfxResId( STR_STANDARD ) ) == aName )
492 // the default template can not be linked
493 aBaseFt.Disable();
494 aBaseLb.Disable();
498 if ( aFilterLb.IsEnabled() )
500 sal_uInt16 nCmp = pStyle->GetMask();
502 if ( nCmp != nFlags )
503 pStyle->SetMask( nFlags );
504 aFilterLb.SelectEntryPos( aFilterLb.GetSavedValue() );
508 //-------------------------------------------------------------------------
510 SfxTabPage* SfxManageStyleSheetPage::Create( Window* pParent,
511 const SfxItemSet &rAttrSet )
513 /* [Description]
515 Factory for the creation of the page.
517 [Cross-reference]
519 <class SfxTabDialog>
523 return new SfxManageStyleSheetPage( pParent, rAttrSet );
526 //-------------------------------------------------------------------------
528 void SfxManageStyleSheetPage::ActivatePage( const SfxItemSet& rSet)
530 /* [Description]
532 ActivatePage handler of SfxTabDialog, is used for the the update of the
533 descriptive text, since this might have changed through changes of data on
534 other pages.
536 [Parameter]
538 const SfxItemSet& the set for the exchange of data; is not used here.
540 [Cross-reference]
542 <SfxTabDialog::ActivatePage(const SfxItemSet &)>
546 SetDescriptionText_Impl();
548 // It is a style with auto update? (SW only)
549 const SfxPoolItem* pPoolItem;
551 if ( SFX_ITEM_SET ==
552 rSet.GetItemState( SID_ATTR_AUTO_STYLE_UPDATE, sal_False, &pPoolItem ) )
553 aAutoCB.Check( ( (const SfxBoolItem*)pPoolItem )->GetValue() );
554 aAutoCB.SaveValue();
557 //-------------------------------------------------------------------------
559 int SfxManageStyleSheetPage::DeactivatePage( SfxItemSet* pItemSet )
561 /* [Description]
563 DeactivatePage-handler of SfxTabDialog; data is set on the template, so
564 that the correct inheritance on the other pages of the dialog is made.
565 If an error occurs, leaving the page is prevented.
566 [Parameter]
568 SfxItemSet* the set for the exchange of data; is not used here.
570 [Cross-reference]
572 <SfxTabDialog::DeactivatePage(SfxItemSet*)>
576 int nRet = SfxTabPage::LEAVE_PAGE;
578 if ( aNameEd.IsModified() )
580 // By pressing <Enter> LoseFocus() is not trigged through StarView
581 if ( aNameEd.HasFocus() )
582 LoseFocusHdl( &aNameEd );
584 if ( !pStyle->SetName( aNameEd.GetText().EraseLeadingChars() ) )
586 InfoBox aBox( this, SfxResId( MSG_TABPAGE_INVALIDNAME ) );
587 aBox.Execute();
588 aNameEd.GrabFocus();
589 aNameEd.SetSelection( Selection( SELECTION_MIN, SELECTION_MAX ) );
590 return SfxTabPage::KEEP_PAGE;
592 bModified = sal_True;
595 if ( pStyle->HasFollowSupport() && aFollowLb.IsEnabled() )
597 const String aFollowEntry( aFollowLb.GetSelectEntry() );
599 if ( pStyle->GetFollow() != aFollowEntry )
601 if ( !pStyle->SetFollow( aFollowEntry ) )
603 InfoBox aBox( this, SfxResId( MSG_TABPAGE_INVALIDSTYLE ) );
604 aBox.Execute();
605 aFollowLb.GrabFocus();
606 return SfxTabPage::KEEP_PAGE;
608 bModified = sal_True;
612 if ( aBaseLb.IsEnabled() )
614 String aParentEntry( aBaseLb.GetSelectEntry() );
616 if ( String( SfxResId( STR_NONE ) ) == aParentEntry || aParentEntry == pStyle->GetName() )
617 aParentEntry.Erase();
619 if ( pStyle->GetParent() != aParentEntry )
621 if ( !pStyle->SetParent( aParentEntry ) )
623 InfoBox aBox( this, SfxResId( MSG_TABPAGE_INVALIDPARENT ) );
624 aBox.Execute();
625 aBaseLb.GrabFocus();
626 return SfxTabPage::KEEP_PAGE;
628 bModified = sal_True;
629 nRet |= (int)SfxTabPage::REFRESH_SET;
633 if ( pItemSet )
634 FillItemSet( *pItemSet );
636 return nRet;
639 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */