xref: /AOO41X/main/sfx2/source/dialog/mgetempl.cxx (revision b2937f997bda0a05141a2d862a64f7be893955b7)
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_sfx2.hxx"
26 
27 // include ---------------------------------------------------------------
28 
29 #ifndef _MSGBOX_HXX //autogen
30 #include <vcl/msgbox.hxx>
31 #endif
32 #include <vcl/field.hxx>
33 #include <svl/eitem.hxx>
34 #include <svl/intitem.hxx>
35 #include <svl/style.hxx>
36 #ifndef GCC
37 #endif
38 
39 #include <sfx2/styfitem.hxx>
40 #include <sfx2/styledlg.hxx>
41 #include <sfx2/app.hxx>
42 #include <sfx2/mgetempl.hxx>
43 #include <sfx2/objsh.hxx>
44 #include "sfxtypes.hxx"
45 #include "sfx2/sfxresid.hxx"
46 #include <sfx2/module.hxx>
47 
48 #include <sfx2/sfx.hrc>
49 #include "dialog.hrc"
50 #include "mgetempl.hrc"
51 
52 #ifndef _SFX_STYLE_HRC
53 #include <svl/style.hrc>
54 #endif
55 
56 // SfxManageStyleSheetPage -----------------------------------------------
57 
58 SfxManageStyleSheetPage::SfxManageStyleSheetPage( Window* pParent, const SfxItemSet& rAttrSet ) :
59 
60     SfxTabPage( pParent, SfxResId( TP_MANAGE_STYLES ), rAttrSet ),
61 
62     aNameFt     ( this, SfxResId( FT_NAME ) ),
63     aNameEd     ( this, SfxResId( ED_NAME ) ),
64     aNameMLE    ( this, SfxResId( MLE_NAME ) ),
65     aAutoCB     ( this, SfxResId( CB_AUTO ) ),
66 
67     aFollowFt   ( this, SfxResId( FT_NEXT ) ),
68     aFollowLb   ( this, SfxResId( LB_NEXT ) ),
69 
70     aBaseFt     ( this, SfxResId( FT_BASE ) ),
71     aBaseLb     ( this, SfxResId( LB_BASE ) ),
72 
73     aFilterFt   ( this, SfxResId( FT_REGION ) ),
74     aFilterLb   ( this, SfxResId( LB_REGION ) ),
75 
76     aDescGb     ( this, SfxResId( GB_DESC ) ),
77     aDescFt     ( this, SfxResId( FT_DESC ) ),
78     aDescED     ( this, SfxResId( ED_DESC ) ),
79 
80     pStyle( &( (SfxStyleDialog*)pParent->GetParent() )->GetStyleSheet() ),
81 
82     pItem       ( 0 ),
83     bModified   ( sal_False ),
84     aName       ( pStyle->GetName() ),
85     aFollow     ( pStyle->GetFollow() ),
86     aParent     ( pStyle->GetParent() ),
87     nFlags      ( pStyle->GetMask() )
88 
89 /*  [Beschreibung]
90 
91     Konstruktor; initialisiert die ListBoxen mit den Vorlagen
92 
93 */
94 
95 {
96     FreeResource();
97 
98     if ( IsBackground() )
99         aDescED.SetBackground( GetBackground() );
100     FixedInfo* pInfo = new FixedInfo( this );
101     aDescED.SetFont( pInfo->GetFont() );
102     delete pInfo;
103 
104     aDescED.Hide();
105     aDescFt.Show();
106 
107     // diese Page braucht ExchangeSupport
108     SetExchangeSupport();
109 
110     ResMgr* pResMgr = SFX_APP()->GetModule_Impl()->GetResMgr();
111     DBG_ASSERT( pResMgr, "kein ResMgr am Modul" );
112     pFamilies = new SfxStyleFamilies( ResId( DLG_STYLE_DESIGNER, *pResMgr ) );
113 
114     SfxStyleSheetBasePool* pPool = 0;
115     SfxObjectShell* pDocShell = SfxObjectShell::Current();
116 
117     if ( pDocShell )
118         pPool = pDocShell->GetStyleSheetPool();
119     DBG_ASSERT( pPool, "kein Pool oder keine DocShell" );
120 
121     if ( pPool )
122     {
123         pPool->SetSearchMask( pStyle->GetFamily() );
124         pPool->First();     // fuer SW - interne Liste updaten
125     }
126 
127     if ( !pStyle->GetName().Len() && pPool )
128     {
129         // NullString als Name -> Name generieren
130         String aNoName( SfxResId( STR_NONAME ) );
131         sal_uInt16 nNo = 1;
132         String aNo( aNoName );
133         aNoName += String::CreateFromInt32( nNo );
134         while ( pPool->Find( aNoName ) )
135         {
136             ++nNo;
137             aNoName = aNo;
138             aNoName += String::CreateFromInt32( nNo );
139         }
140         pStyle->SetName( aNoName );
141         aName = aNoName;
142         aFollow = pStyle->GetFollow();
143         aParent = pStyle->GetParent();
144     }
145     aNameEd.SetText(pStyle->GetName());
146 
147     // Set the field read-only if it is NOT an user-defined style
148     // but allow selecting and copying
149     if ( !pStyle->IsUserDefined() ) {
150         aNameEd.SetReadOnly();
151         aNameEd.Hide();
152 
153         aNameMLE.SetControlBackground( GetSettings().GetStyleSettings().GetDialogColor() );
154         aNameMLE.SetText( pStyle->GetName() );
155         aNameMLE.EnableCursor( sal_False );
156         aNameMLE.Show();
157     }
158 
159     if ( pStyle->HasFollowSupport() && pPool )
160     {
161         SfxStyleSheetBase* pPoolStyle = pPool->First();
162 
163         while ( pPoolStyle )
164         {
165             aFollowLb.InsertEntry( pPoolStyle->GetName() );
166             pPoolStyle = pPool->Next();
167         }
168 
169         // eine neue Vorlage ist noch nicht im Pool
170         if ( LISTBOX_ENTRY_NOTFOUND == aFollowLb.GetEntryPos( pStyle->GetName() ) )
171             aFollowLb.InsertEntry( pStyle->GetName() );
172     }
173     else
174     {
175         aFollowFt.Hide();
176         aFollowLb.Hide();
177 
178         aFilterFt.SetPosPixel( aBaseFt.GetPosPixel() );
179         aFilterLb.SetPosPixel( aBaseLb.GetPosPixel() );
180 
181         aBaseFt.SetPosPixel( aFollowFt.GetPosPixel() );
182         aBaseLb.SetPosPixel( aFollowLb.GetPosPixel() );
183     }
184 
185     if ( pStyle->HasParentSupport() && pPool )
186     {
187         if ( pStyle->HasClearParentSupport() )
188             // die Basisvorlage darf auf NULL gesetzt werden
189             aBaseLb.InsertEntry( String( SfxResId( STR_NONE ) ) );
190 
191         SfxStyleSheetBase* pPoolStyle = pPool->First();
192 
193         while ( pPoolStyle )
194         {
195             const String aStr( pPoolStyle->GetName() );
196             // eigener Namen nicht als Basisvorlage
197             if ( aStr != aName )
198                 aBaseLb.InsertEntry( aStr );
199             pPoolStyle = pPool->Next();
200         }
201     }
202     else
203     {
204         aBaseFt.Disable();
205         aBaseLb.Disable();
206     }
207     sal_uInt16 nCount = pFamilies->Count();
208 
209     sal_uInt16 i;
210     for ( i = 0; i < nCount; ++i )
211     {
212         pItem = pFamilies->GetObject(i);
213 
214         if ( pItem->GetFamily() == pStyle->GetFamily() )
215             break;
216     }
217 
218     sal_uInt16 nStyleFilterIdx = 0xffff;
219 
220     if ( i < nCount )
221     {
222         // Filterflags
223         const SfxStyleFilter& rList = pItem->GetFilterList();
224         nCount = (sal_uInt16)rList.Count();
225         sal_uInt16 nIdx = 0;
226         sal_uInt16 nMask = pStyle->GetMask() & ~SFXSTYLEBIT_USERDEF;
227 
228         if ( !nMask )   // Benutzervorlage?
229             nMask = pStyle->GetMask();
230 
231         for ( i = 0; i < nCount; ++i )
232         {
233             SfxFilterTupel* pTupel = rList.GetObject(i);
234 
235             if ( pTupel->nFlags != SFXSTYLEBIT_AUTO     &&
236                  pTupel->nFlags != SFXSTYLEBIT_USED     &&
237 //               pTupel->nFlags != SFXSTYLEBIT_USERDEF  &&
238                  pTupel->nFlags != SFXSTYLEBIT_ALL )
239             {
240                 aFilterLb.InsertEntry( pTupel->aName, nIdx );
241                 aFilterLb.SetEntryData(nIdx, (void*)(long)i);
242 
243                 if ( ( pTupel->nFlags & nMask ) == nMask )
244                     nStyleFilterIdx = nIdx;
245                 ++nIdx;
246             }
247         }
248 
249         if ( nStyleFilterIdx != 0xFFFF )
250             aFilterLb.SelectEntryPos( nStyleFilterIdx );
251     }
252 
253     if ( !aFilterLb.GetEntryCount() || !pStyle->IsUserDefined() )
254     {
255         pItem = 0;
256         aFilterFt.Disable();
257         aFilterLb.Disable();
258     }
259     else
260         aFilterLb.SaveValue();
261     SetDescriptionText_Impl();
262 
263     if ( aFollowLb.IsEnabled() || aBaseLb.IsEnabled() )
264     {
265         aNameEd.SetGetFocusHdl(
266             LINK( this, SfxManageStyleSheetPage, GetFocusHdl ) );
267         aNameEd.SetLoseFocusHdl(
268             LINK( this, SfxManageStyleSheetPage, LoseFocusHdl ) );
269     }
270     // ist es ein Style mit automatischem Update? (nur SW)
271     if(SFX_ITEM_SET == rAttrSet.GetItemState(SID_ATTR_AUTO_STYLE_UPDATE))
272     {
273         Size aSize = aNameEd.GetSizePixel();
274         aSize.Width() /= 2;
275         aNameEd.SetSizePixel(aSize);
276         aAutoCB.Show();
277     }
278 }
279 
280 //-------------------------------------------------------------------------
281 
282 SfxManageStyleSheetPage::~SfxManageStyleSheetPage()
283 
284 /*  [Beschreibung]
285 
286     Destruktor; Freigabe der Daten
287 
288 */
289 
290 {
291     aNameEd.SetGetFocusHdl( Link() );
292     aNameEd.SetLoseFocusHdl( Link() );
293     delete pFamilies;
294     pItem = 0;
295     pStyle = 0;
296 
297 }
298 
299 //-------------------------------------------------------------------------
300 
301 void SfxManageStyleSheetPage::UpdateName_Impl( ListBox* pBox,
302                                                const String& rNew )
303 
304 /*  [Beschreibung]
305 
306     Nach der "Anderung eines Vorlagennamens die ListBox pBox
307     aktualisieren
308 
309     [Parameter]
310 
311     ListBox* pBox           ListBox, deren Eintr"age aktualisiert
312                             werden sollen
313     const String& rNew      der neue Name
314 
315 */
316 
317 {
318     if ( pBox->IsEnabled() )
319     {
320         // ist der aktuelle Eintrag, dessen Namen modifizieren wurde
321         const sal_Bool bSelect = pBox->GetSelectEntry() == aBuf;
322         pBox->RemoveEntry( aBuf );
323         pBox->InsertEntry( rNew );
324 
325         if ( bSelect )
326             pBox->SelectEntry( rNew );
327     }
328 }
329 
330 //-------------------------------------------------------------------------
331 
332 void SfxManageStyleSheetPage::SetDescriptionText_Impl()
333 
334 /*  [Beschreibung]
335 
336     Attributbeschreibung setzen. daf"ur die eingestellte Metrik besorgen.
337 */
338 
339 {
340     SfxMapUnit eUnit = SFX_MAPUNIT_CM;
341 //    FieldUnit eFieldUnit = pSfxApp->GetOptions().GetMetric();
342     FieldUnit eFieldUnit( FUNIT_CM );
343     SfxModule* pModule = SfxModule::GetActiveModule();
344     if ( pModule )
345     {
346         const SfxPoolItem* pPoolItem = pModule->GetItem( SID_ATTR_METRIC );
347         if ( pPoolItem )
348             eFieldUnit = (FieldUnit)( (SfxUInt16Item*)pPoolItem )->GetValue();
349     }
350 
351     switch ( eFieldUnit )
352     {
353         case FUNIT_MM:      eUnit = SFX_MAPUNIT_MM; break;
354 
355         case FUNIT_CM:
356         case FUNIT_M:
357         case FUNIT_KM:      eUnit = SFX_MAPUNIT_CM; break;
358 
359         case FUNIT_POINT:
360         case FUNIT_PICA:    eUnit = SFX_MAPUNIT_POINT; break;
361 
362         case FUNIT_INCH:
363         case FUNIT_FOOT:
364         case FUNIT_MILE:    eUnit = SFX_MAPUNIT_INCH; break;
365 
366         default:
367             DBG_ERRORFILE( "not supported fieldunit" );
368     }
369     aDescFt.SetText( pStyle->GetDescription( eUnit ) );
370 }
371 
372 //-------------------------------------------------------------------------
373 
374 IMPL_LINK_INLINE_START( SfxManageStyleSheetPage, GetFocusHdl, Edit *, pEdit )
375 
376 /*  [Beschreibung]
377 
378     StarView Handler; GetFocus-Handler des Edits mit dem Vorlagennamen.
379 
380 */
381 
382 {
383     aBuf = pEdit->GetText().EraseLeadingChars();
384     return 0;
385 }
386 IMPL_LINK_INLINE_END( SfxManageStyleSheetPage, GetFocusHdl, Edit *, pEdit )
387 
388 //-------------------------------------------------------------------------
389 
390 IMPL_LINK_INLINE_START( SfxManageStyleSheetPage, LoseFocusHdl, Edit *, pEdit )
391 
392 /*  [Beschreibung]
393 
394     StarView Handler; LoseFocus-Handler des Edits mit dem Vorlagennamen.
395     Dieser aktualisiert ggf. die Listbox mit den Folgevorlagen.
396     In der Listbox mit den Basisvorlagen ist die aktuelle Vorlage
397     selbst nicht enthalten.
398 
399 */
400 
401 {
402     const String aStr( pEdit->GetText().EraseLeadingChars() );
403     pEdit->SetText( aStr );
404     // ggf. Listbox der Basisvorlagen aktualisieren
405     if ( aStr != aBuf )
406         UpdateName_Impl( &aFollowLb, aStr );
407     return 0;
408 }
409 IMPL_LINK_INLINE_END( SfxManageStyleSheetPage, LoseFocusHdl, Edit *, pEdit )
410 
411 //-------------------------------------------------------------------------
412 
413 sal_Bool SfxManageStyleSheetPage::FillItemSet( SfxItemSet& rSet )
414 
415 /*  [Beschreibung]
416 
417 
418     Handler f"ur das Setzen der (modifizierten) Daten. Wird aus
419     dem Ok des SfxTabDialog gerufen.
420 
421     [Parameter]
422 
423     SfxItemSet &rAttrSet        das Set, das die Daten entgegennimmt.
424 
425 
426     [R"uckgabewert]
427 
428     sal_Bool                        sal_True:  es hat eine "Anderung der Daten
429                                        stattgefunden
430                                     sal_False: es hat keine "Anderung der Daten
431                                        stattgefunden
432 
433     [Querverweise]
434 
435     <class SfxTabDialog>
436 
437 */
438 
439 {
440     const sal_uInt16 nFilterIdx = aFilterLb.GetSelectEntryPos();
441 
442     // Filter setzen
443 
444     if ( LISTBOX_ENTRY_NOTFOUND  != nFilterIdx      &&
445          nFilterIdx != aFilterLb.GetSavedValue()    &&
446          aFilterLb.IsEnabled() )
447     {
448         bModified = sal_True;
449         DBG_ASSERT( pItem, "kein Item" );
450         // geht nur bei Benutzervorlagen
451 #if OSL_DEBUG_LEVEL > 1
452         sal_uInt16 nIdx = (sal_uInt16)(long)aFilterLb.GetEntryData( nFilterIdx );
453         SfxFilterTupel* p;
454         p = pItem->GetFilterList().GetObject( nIdx );
455 #endif
456         sal_uInt16 nMask = pItem->GetFilterList().GetObject(
457             (sal_uInt16)(long)aFilterLb.GetEntryData( nFilterIdx ) )->nFlags |
458             SFXSTYLEBIT_USERDEF;
459         pStyle->SetMask( nMask );
460     }
461     if(aAutoCB.IsVisible() &&
462         aAutoCB.IsChecked() != aAutoCB.GetSavedValue())
463     {
464         rSet.Put(SfxBoolItem(SID_ATTR_AUTO_STYLE_UPDATE, aAutoCB.IsChecked()));
465     }
466 
467     return bModified;
468 }
469 
470 //-------------------------------------------------------------------------
471 
472 void SfxManageStyleSheetPage::Reset( const SfxItemSet& /*rAttrSet*/ )
473 
474 /*  [Beschreibung]
475 
476 
477     Handler f"ur das Initialisieren der Seite mit den initialen Daten.
478 
479     [Parameter]
480 
481     const SfxItemSet &rAttrSet      das Set mit den Daten
482 
483 
484     [Querverweise]
485 
486     <class SfxTabDialog>
487 
488 */
489 
490 {
491     bModified = sal_False;
492     String sCmp( pStyle->GetName() );
493 
494     if ( sCmp != aName )
495         pStyle->SetName( aName );
496     aNameEd.SetText( aName );
497 
498     if ( aFollowLb.IsEnabled() )
499     {
500         sCmp = pStyle->GetFollow();
501 
502         if ( sCmp != aFollow )
503             pStyle->SetFollow( aFollow );
504 
505         if ( !aFollow.Len() )
506             aFollowLb.SelectEntry( aName );
507         else
508             aFollowLb.SelectEntry( aFollow );
509     }
510 
511     if ( aBaseLb.IsEnabled() )
512     {
513         sCmp = pStyle->GetParent();
514 
515         if ( sCmp != aParent )
516             pStyle->SetParent( aParent );
517 
518         if ( !aParent.Len() )
519             aBaseLb.SelectEntry( String( SfxResId( STR_NONE ) ) );
520         else
521             aBaseLb.SelectEntry( aParent );
522 
523         if ( String( SfxResId( STR_STANDARD ) ) == aName )
524         {
525             // die Standardvorlage kann nicht verkn"upft werden
526             aBaseFt.Disable();
527             aBaseLb.Disable();
528         }
529     }
530 
531     if ( aFilterLb.IsEnabled() )
532     {
533         sal_uInt16 nCmp = pStyle->GetMask();
534 
535         if ( nCmp != nFlags )
536             pStyle->SetMask( nFlags );
537         aFilterLb.SelectEntryPos( aFilterLb.GetSavedValue() );
538     }
539 }
540 
541 //-------------------------------------------------------------------------
542 
543 SfxTabPage* SfxManageStyleSheetPage::Create( Window* pParent,
544                                              const SfxItemSet &rAttrSet )
545 
546 /*  [Beschreibung]
547 
548 
549     Factory f"ur das Erzeugen der Seite
550 
551     [Querverweise]
552 
553     <class SfxTabDialog>
554 
555 */
556 
557 {
558     return new SfxManageStyleSheetPage( pParent, rAttrSet );
559 }
560 
561 //-------------------------------------------------------------------------
562 
563 void SfxManageStyleSheetPage::ActivatePage( const SfxItemSet& rSet)
564 
565 /*  [Beschreibung]
566 
567     ActivatePage- Handler des SfxTabDialog; wird f"ur die Aktualisierung
568     des beschreibenden Textes verwendet, da sich dieser durch "Anderungen
569     der Daten anderer Seiten ge"andert haben kann.
570 
571     [Parameter]
572 
573     const SfxItemSet&       das Set f"ur den Datenaustausch; wird
574                             hier nicht verwendet.
575 
576     [Querverweise]
577 
578     <SfxTabDialog::ActivatePage(const SfxItemSet &)>
579 
580 */
581 
582 {
583     SetDescriptionText_Impl();
584 
585     // ist es ein Style mit automatischem Update? (nur SW)
586     const SfxPoolItem* pPoolItem;
587 
588     if ( SFX_ITEM_SET ==
589          rSet.GetItemState( SID_ATTR_AUTO_STYLE_UPDATE, sal_False, &pPoolItem ) )
590         aAutoCB.Check( ( (const SfxBoolItem*)pPoolItem )->GetValue() );
591     aAutoCB.SaveValue();
592 }
593 
594 //-------------------------------------------------------------------------
595 
596 int SfxManageStyleSheetPage::DeactivatePage( SfxItemSet* pItemSet )
597 
598 /*  [Beschreibung]
599 
600     DeactivatePage- Handler des SfxTabDialog; die Daten werden
601     an der Vorlage eingestellt, damit die richtige Vererbung
602     f"ur die anderen Seiten des Dialoges vorliegt.
603     Im Fehlerfall wird das Verlassen der Seite unterbunden.
604 
605     [Parameter]
606 
607     SfxItemSet*         das Set f"ur den Datenaustausch; wird hier nicht verwendet.
608 
609     [Querverweise]
610 
611     <SfxTabDialog::DeactivatePage(SfxItemSet*)>
612 
613 */
614 
615 {
616     int nRet = SfxTabPage::LEAVE_PAGE;
617 
618     if ( aNameEd.IsModified() )
619     {
620         // bei <Enter> wird kein LoseFocus() durch StarView ausgel"ost
621         if ( aNameEd.HasFocus() )
622             LoseFocusHdl( &aNameEd );
623 
624         if ( !pStyle->SetName( aNameEd.GetText().EraseLeadingChars() ) )
625         {
626             InfoBox aBox( this, SfxResId( MSG_TABPAGE_INVALIDNAME ) );
627             aBox.Execute();
628             aNameEd.GrabFocus();
629             aNameEd.SetSelection( Selection( SELECTION_MIN, SELECTION_MAX ) );
630             return SfxTabPage::KEEP_PAGE;
631         }
632         bModified = sal_True;
633     }
634 
635     if ( pStyle->HasFollowSupport() && aFollowLb.IsEnabled() )
636     {
637         const String aFollowEntry( aFollowLb.GetSelectEntry() );
638 
639         if ( pStyle->GetFollow() != aFollowEntry )
640         {
641             if ( !pStyle->SetFollow( aFollowEntry ) )
642             {
643                 InfoBox aBox( this, SfxResId( MSG_TABPAGE_INVALIDSTYLE ) );
644                 aBox.Execute();
645                 aFollowLb.GrabFocus();
646                 return SfxTabPage::KEEP_PAGE;
647             }
648             bModified = sal_True;
649         }
650     }
651 
652     if ( aBaseLb.IsEnabled() )
653     {
654         String aParentEntry( aBaseLb.GetSelectEntry() );
655 
656         if ( String( SfxResId( STR_NONE ) ) == aParentEntry || aParentEntry == pStyle->GetName() )
657             aParentEntry.Erase();
658 
659         if ( pStyle->GetParent() != aParentEntry )
660         {
661             if ( !pStyle->SetParent( aParentEntry ) )
662             {
663                 InfoBox aBox( this, SfxResId( MSG_TABPAGE_INVALIDPARENT ) );
664                 aBox.Execute();
665                 aBaseLb.GrabFocus();
666                 return SfxTabPage::KEEP_PAGE;
667             }
668             bModified = sal_True;
669             nRet |= (int)SfxTabPage::REFRESH_SET;
670         }
671     }
672 
673     if ( pItemSet )
674         FillItemSet( *pItemSet );
675 
676     return nRet;
677 }
678 
679