Bump version to 4.1-6
[LibreOffice.git] / sfx2 / source / dialog / mgetempl.cxx
blob932fddd09815bddc835d993b742875fbbca06c13
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 <comphelper/string.hxx>
21 #include <vcl/msgbox.hxx>
22 #include <vcl/field.hxx>
23 #include <svl/eitem.hxx>
24 #include <svl/intitem.hxx>
25 #include <svl/style.hxx>
27 #include <sfx2/styfitem.hxx>
28 #include <sfx2/styledlg.hxx>
29 #include <sfx2/app.hxx>
30 #include <sfx2/mgetempl.hxx>
31 #include <sfx2/objsh.hxx>
32 #include "sfxtypes.hxx"
33 #include "sfx2/sfxresid.hxx"
34 #include <sfx2/module.hxx>
36 #include <sfx2/sfx.hrc>
37 #include "dialog.hrc"
39 #include <svl/style.hrc>
41 /* SfxManageStyleSheetPage Constructor
43 * initializes the list box with the templates
45 SfxManageStyleSheetPage::SfxManageStyleSheetPage(Window* pParent, const SfxItemSet& rAttrSet)
46 : SfxTabPage(pParent, "ManageStylePage", "sfx/ui/managestylepage.ui", rAttrSet)
47 , pStyle(&((SfxStyleDialog*)GetParentDialog())->GetStyleSheet())
48 , pItem(0)
49 , bModified(false)
50 , aName(pStyle->GetName())
51 , aFollow(pStyle->GetFollow())
52 , aParent(pStyle->GetParent())
53 , nFlags(pStyle->GetMask())
55 get(m_pNameRo, "namero");
56 get(m_pNameRw, "namerw");
57 m_pNameRo->set_width_request(m_pNameRw->get_preferred_size().Width());
58 get(m_pAutoCB, "autoupdate");
59 get(m_pFollowFt, "nextstyleft");
60 get(m_pFollowLb, "nextstyle");
61 m_pFollowLb->SetStyle(m_pFollowLb->GetStyle() | WB_SORT);
62 const sal_Int32 nMaxWidth(62);
63 m_pFollowLb->setMaxWidthChars(nMaxWidth);
64 get(m_pBaseFt, "linkedwithft");
65 get(m_pBaseLb, "linkedwith");
66 m_pBaseLb->SetStyle(m_pBaseLb->GetStyle() | WB_SORT);
67 m_pBaseLb->setMaxWidthChars(nMaxWidth);
68 get(m_pFilterFt, "categoryft");
69 get(m_pFilterLb, "category");
70 m_pFilterLb->SetStyle(m_pFilterLb->GetStyle() | WB_SORT);
71 m_pFilterLb->setMaxWidthChars(nMaxWidth);
72 get(m_pDescFt, "desc");
74 // this Page needs ExchangeSupport
75 SetExchangeSupport();
77 ResMgr* pResMgr = SFX_APP()->GetModule_Impl()->GetResMgr();
78 OSL_ENSURE( pResMgr, "No ResMgr in Module" );
79 pFamilies = new SfxStyleFamilies( ResId( DLG_STYLE_DESIGNER, *pResMgr ) );
81 SfxStyleSheetBasePool* pPool = 0;
82 SfxObjectShell* pDocShell = SfxObjectShell::Current();
84 if ( pDocShell )
85 pPool = pDocShell->GetStyleSheetPool();
86 OSL_ENSURE( pPool, "no Pool or no DocShell" );
88 if ( pPool )
90 pPool->SetSearchMask( pStyle->GetFamily() );
91 pPool->First(); // for SW - update internal list
94 if ( !pStyle->GetName().Len() && pPool )
96 // NullString as Name -> generate Name
97 String aNoName( SfxResId(STR_NONAME).toString() );
98 sal_uInt16 nNo = 1;
99 String aNo( aNoName );
100 aNoName += OUString::number( nNo );
101 while ( pPool->Find( aNoName ) )
103 ++nNo;
104 aNoName = aNo;
105 aNoName += OUString::number( nNo );
107 pStyle->SetName( aNoName );
108 aName = aNoName;
109 aFollow = pStyle->GetFollow();
110 aParent = pStyle->GetParent();
112 m_pNameRw->SetText(pStyle->GetName());
114 // Set the field read-only if it is NOT an user-defined style
115 // but allow selecting and copying
116 if (!pStyle->IsUserDefined())
118 m_pNameRo->SetText(m_pNameRw->GetText());
119 m_pNameRw->Hide();
120 m_pNameRo->Show();
121 FixedText *pLabel = get<FixedText>("nameft");
122 pLabel->set_mnemonic_widget(m_pNameRo);
125 if ( pStyle->HasFollowSupport() && pPool )
127 SfxStyleSheetBase* pPoolStyle = pPool->First();
129 while ( pPoolStyle )
131 m_pFollowLb->InsertEntry( pPoolStyle->GetName() );
132 pPoolStyle = pPool->Next();
135 // A new Template is not yet in the Pool
136 if ( LISTBOX_ENTRY_NOTFOUND == m_pFollowLb->GetEntryPos( pStyle->GetName() ) )
137 m_pFollowLb->InsertEntry( pStyle->GetName() );
139 else
141 m_pFollowFt->Hide();
142 m_pFollowLb->Hide();
145 if ( pStyle->HasParentSupport() && pPool )
147 if ( pStyle->HasClearParentSupport() )
148 // the base template can be set to NULL
149 m_pBaseLb->InsertEntry( SfxResId(STR_NONE).toString() );
151 SfxStyleSheetBase* pPoolStyle = pPool->First();
153 while ( pPoolStyle )
155 const String aStr( pPoolStyle->GetName() );
156 // own name as base template
157 if ( aStr != aName )
158 m_pBaseLb->InsertEntry( aStr );
159 pPoolStyle = pPool->Next();
162 else
164 m_pBaseFt->Disable();
165 m_pBaseLb->Disable();
168 size_t nCount = pFamilies->size();
169 size_t i;
170 for ( i = 0; i < nCount; ++i )
172 pItem = pFamilies->at( i );
174 if ( pItem->GetFamily() == pStyle->GetFamily() )
175 break;
178 if ( i < nCount )
180 sal_uInt16 nStyleFilterIdx = 0xffff;
181 // Filter flags
182 const SfxStyleFilter& rList = pItem->GetFilterList();
183 nCount = rList.size();
184 sal_uInt16 nIdx = 0;
185 sal_uInt16 nMask = pStyle->GetMask() & ~SFXSTYLEBIT_USERDEF;
187 if ( !nMask ) // User Template?
188 nMask = pStyle->GetMask();
190 for ( i = 0; i < nCount; ++i )
192 SfxFilterTupel* pTupel = rList[ i ];
194 if ( pTupel->nFlags != SFXSTYLEBIT_AUTO &&
195 pTupel->nFlags != SFXSTYLEBIT_USED &&
196 pTupel->nFlags != SFXSTYLEBIT_ALL )
198 m_pFilterLb->InsertEntry( pTupel->aName, nIdx );
199 m_pFilterLb->SetEntryData(nIdx, (void*)(long)i);
201 if ( ( pTupel->nFlags & nMask ) == nMask )
202 nStyleFilterIdx = nIdx;
203 ++nIdx;
207 if ( nStyleFilterIdx != 0xFFFF )
208 m_pFilterLb->SelectEntryPos( nStyleFilterIdx );
211 if ( !m_pFilterLb->GetEntryCount() || !pStyle->IsUserDefined() )
213 pItem = 0;
214 m_pFilterFt->Disable();
215 m_pFilterLb->Disable();
217 else
218 m_pFilterLb->SaveValue();
219 SetDescriptionText_Impl();
221 if ( m_pFollowLb->IsEnabled() || m_pBaseLb->IsEnabled() )
223 m_pNameRw->SetGetFocusHdl(
224 LINK( this, SfxManageStyleSheetPage, GetFocusHdl ) );
225 m_pNameRw->SetLoseFocusHdl(
226 LINK( this, SfxManageStyleSheetPage, LoseFocusHdl ) );
228 // It is a style with auto update? (SW only)
229 if(SFX_ITEM_SET == rAttrSet.GetItemState(SID_ATTR_AUTO_STYLE_UPDATE))
230 m_pAutoCB->Show();
233 //-------------------------------------------------------------------------
235 SfxManageStyleSheetPage::~SfxManageStyleSheetPage()
237 /* [Description]
239 Destructor, release of the data
243 m_pNameRw->SetGetFocusHdl( Link() );
244 m_pNameRw->SetLoseFocusHdl( Link() );
245 delete pFamilies;
246 pItem = 0;
247 pStyle = 0;
251 //-------------------------------------------------------------------------
253 void SfxManageStyleSheetPage::UpdateName_Impl( ListBox* pBox,
254 const String& rNew )
256 /* [Description]
258 After the change of a template name update the ListBox pBox
260 [Parameter]
262 ListBox* pBox ListBox, whose entries are to be updated
263 const String& rNew the new Name
267 if ( pBox->IsEnabled() )
269 // it is the current entry, which name was modified
270 const sal_Bool bSelect = pBox->GetSelectEntry() == aBuf;
271 pBox->RemoveEntry( aBuf );
272 pBox->InsertEntry( rNew );
274 if ( bSelect )
275 pBox->SelectEntry( rNew );
279 //-------------------------------------------------------------------------
281 void SfxManageStyleSheetPage::SetDescriptionText_Impl()
283 /* [Description]
285 Set attribute description. Get the set metric for this.
289 SfxMapUnit eUnit = SFX_MAPUNIT_CM;
290 FieldUnit eFieldUnit( FUNIT_CM );
291 SfxModule* pModule = SfxModule::GetActiveModule();
292 if ( pModule )
294 const SfxPoolItem* pPoolItem = pModule->GetItem( SID_ATTR_METRIC );
295 if ( pPoolItem )
296 eFieldUnit = (FieldUnit)( (SfxUInt16Item*)pPoolItem )->GetValue();
299 switch ( eFieldUnit )
301 case FUNIT_MM: eUnit = SFX_MAPUNIT_MM; break;
302 case FUNIT_CM:
303 case FUNIT_M:
304 case FUNIT_KM: eUnit = SFX_MAPUNIT_CM; break;
305 case FUNIT_POINT:
306 case FUNIT_PICA: eUnit = SFX_MAPUNIT_POINT; break;
307 case FUNIT_INCH:
308 case FUNIT_FOOT:
309 case FUNIT_MILE: eUnit = SFX_MAPUNIT_INCH; break;
311 default:
312 OSL_FAIL( "non supported field unit" );
314 m_pDescFt->SetText( pStyle->GetDescription( eUnit ) );
317 //-------------------------------------------------------------------------
319 IMPL_LINK_INLINE_START( SfxManageStyleSheetPage, GetFocusHdl, Edit *, pEdit )
321 /* [Description]
323 StarView Handler; GetFocus-Handler of the Edits with the template name.
327 aBuf = comphelper::string::stripStart(pEdit->GetText(), ' ');
328 return 0;
330 IMPL_LINK_INLINE_END( SfxManageStyleSheetPage, GetFocusHdl, Edit *, pEdit )
332 //-------------------------------------------------------------------------
334 IMPL_LINK_INLINE_START( SfxManageStyleSheetPage, LoseFocusHdl, Edit *, pEdit )
336 /* [Description]
338 StarView Handler; loose-focus-handler of the edits of the template name.
339 This will update the listbox with the subsequent templates. The current
340 template itself is not returned in the listbox of the base templates.
344 const String aStr(comphelper::string::stripStart(pEdit->GetText(), ' '));
345 pEdit->SetText( aStr );
346 // Update the Listbox of the base template if possible
347 if ( aStr != aBuf )
348 UpdateName_Impl(m_pFollowLb, aStr);
349 return 0;
351 IMPL_LINK_INLINE_END( SfxManageStyleSheetPage, LoseFocusHdl, Edit *, pEdit )
353 //-------------------------------------------------------------------------
355 sal_Bool SfxManageStyleSheetPage::FillItemSet( SfxItemSet& rSet )
357 /* [Description]
359 Handler for setting the (modified) data. I called from the OK of the
360 SfxTabDialog.
362 [Parameter]
364 SfxItemSet &rAttrSet The set, which receives the data.
366 [Return value]
368 sal_Bool sal_True: The data had been changed
369 sal_False: The data had not been changed
371 [Cross-reference]
373 <class SfxTabDialog>
377 const sal_uInt16 nFilterIdx = m_pFilterLb->GetSelectEntryPos();
379 // Set Filter
381 if ( LISTBOX_ENTRY_NOTFOUND != nFilterIdx &&
382 nFilterIdx != m_pFilterLb->GetSavedValue() &&
383 m_pFilterLb->IsEnabled() )
385 bModified = sal_True;
386 OSL_ENSURE( pItem, "No Item" );
387 // is only possibly for user templates
388 sal_uInt16 nMask = pItem->GetFilterList()[ (size_t)m_pFilterLb->GetEntryData( nFilterIdx ) ]->nFlags | SFXSTYLEBIT_USERDEF;
389 pStyle->SetMask( nMask );
391 if(m_pAutoCB->IsVisible() &&
392 m_pAutoCB->IsChecked() != m_pAutoCB->GetSavedValue())
394 rSet.Put(SfxBoolItem(SID_ATTR_AUTO_STYLE_UPDATE, m_pAutoCB->IsChecked()));
397 return bModified;
400 //-------------------------------------------------------------------------
402 void SfxManageStyleSheetPage::Reset( const SfxItemSet& /*rAttrSet*/ )
404 /* [Description]
406 Handler to initialize the page with the initial data.
408 [Parameter]
410 const SfxItemSet &rAttrSet The data set
412 [Cross-reference]
414 <class SfxTabDialog>
418 bModified = sal_False;
419 String sCmp( pStyle->GetName() );
421 if ( sCmp != aName )
422 pStyle->SetName( aName );
423 m_pNameRw->SetText( aName );
425 if ( m_pFollowLb->IsEnabled() )
427 sCmp = pStyle->GetFollow();
429 if ( sCmp != aFollow )
430 pStyle->SetFollow( aFollow );
432 if ( !aFollow.Len() )
433 m_pFollowLb->SelectEntry( aName );
434 else
435 m_pFollowLb->SelectEntry( aFollow );
438 if ( m_pBaseLb->IsEnabled() )
440 sCmp = pStyle->GetParent();
442 if ( sCmp != aParent )
443 pStyle->SetParent( aParent );
445 if ( !aParent.Len() )
446 m_pBaseLb->SelectEntry( SfxResId(STR_NONE).toString() );
447 else
448 m_pBaseLb->SelectEntry( aParent );
450 if ( SfxResId(STR_STANDARD).toString().equals(aName) )
452 // the default template can not be linked
453 m_pBaseFt->Disable();
454 m_pBaseLb->Disable();
458 if ( m_pFilterLb->IsEnabled() )
460 sal_uInt16 nCmp = pStyle->GetMask();
462 if ( nCmp != nFlags )
463 pStyle->SetMask( nFlags );
464 m_pFilterLb->SelectEntryPos( m_pFilterLb->GetSavedValue() );
468 //-------------------------------------------------------------------------
470 SfxTabPage* SfxManageStyleSheetPage::Create( Window* pParent,
471 const SfxItemSet &rAttrSet )
473 /* [Description]
475 Factory for the creation of the page.
477 [Cross-reference]
479 <class SfxTabDialog>
483 return new SfxManageStyleSheetPage( pParent, rAttrSet );
486 //-------------------------------------------------------------------------
488 void SfxManageStyleSheetPage::ActivatePage( const SfxItemSet& rSet)
490 /* [Description]
492 ActivatePage handler of SfxTabDialog, is used for the update of the
493 descriptive text, since this might have changed through changes of data on
494 other pages.
496 [Parameter]
498 const SfxItemSet& the set for the exchange of data; is not used here.
500 [Cross-reference]
502 <SfxTabDialog::ActivatePage(const SfxItemSet &)>
506 SetDescriptionText_Impl();
508 // It is a style with auto update? (SW only)
509 const SfxPoolItem* pPoolItem;
511 if ( SFX_ITEM_SET ==
512 rSet.GetItemState( SID_ATTR_AUTO_STYLE_UPDATE, sal_False, &pPoolItem ) )
513 m_pAutoCB->Check( ( (const SfxBoolItem*)pPoolItem )->GetValue() );
514 m_pAutoCB->SaveValue();
517 //-------------------------------------------------------------------------
519 int SfxManageStyleSheetPage::DeactivatePage( SfxItemSet* pItemSet )
521 /* [Description]
523 DeactivatePage-handler of SfxTabDialog; data is set on the template, so
524 that the correct inheritance on the other pages of the dialog is made.
525 If an error occurs, leaving the page is prevented.
526 [Parameter]
528 SfxItemSet* the set for the exchange of data; is not used here.
530 [Cross-reference]
532 <SfxTabDialog::DeactivatePage(SfxItemSet*)>
536 int nRet = SfxTabPage::LEAVE_PAGE;
538 if ( m_pNameRw->IsModified() )
540 // By pressing <Enter> LoseFocus() is not trigged through StarView
541 if ( m_pNameRw->HasFocus() )
542 LoseFocusHdl( m_pNameRw );
544 if (!pStyle->SetName(comphelper::string::stripStart(m_pNameRw->GetText(), ' ')))
546 InfoBox aBox( this, SfxResId( MSG_TABPAGE_INVALIDNAME ) );
547 aBox.Execute();
548 m_pNameRw->GrabFocus();
549 m_pNameRw->SetSelection( Selection( SELECTION_MIN, SELECTION_MAX ) );
550 return SfxTabPage::KEEP_PAGE;
552 bModified = sal_True;
555 if ( pStyle->HasFollowSupport() && m_pFollowLb->IsEnabled() )
557 const OUString aFollowEntry( m_pFollowLb->GetSelectEntry() );
559 if ( pStyle->GetFollow() != aFollowEntry )
561 if ( !pStyle->SetFollow( aFollowEntry ) )
563 InfoBox aBox( this, SfxResId( MSG_TABPAGE_INVALIDSTYLE ) );
564 aBox.Execute();
565 m_pFollowLb->GrabFocus();
566 return SfxTabPage::KEEP_PAGE;
568 bModified = sal_True;
572 if ( m_pBaseLb->IsEnabled() )
574 OUString aParentEntry( m_pBaseLb->GetSelectEntry() );
576 if ( SfxResId(STR_NONE).toString().equals(aParentEntry) || aParentEntry == pStyle->GetName() )
577 aParentEntry = OUString();
579 if ( pStyle->GetParent() != aParentEntry )
581 if ( !pStyle->SetParent( aParentEntry ) )
583 InfoBox aBox( this, SfxResId( MSG_TABPAGE_INVALIDPARENT ) );
584 aBox.Execute();
585 m_pBaseLb->GrabFocus();
586 return SfxTabPage::KEEP_PAGE;
588 bModified = sal_True;
589 nRet |= (int)SfxTabPage::REFRESH_SET;
593 if ( pItemSet )
594 FillItemSet( *pItemSet );
596 return nRet;
599 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */