xref: /AOO41X/main/sfx2/source/dialog/templdlg.cxx (revision 9dcd2d78ee6377e8ad1e2e01efd517f26f4562c3)
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 #ifndef _MENU_HXX //autogen
28 #include <vcl/menu.hxx>
29 #endif
30 #include <svl/intitem.hxx>
31 #include <svl/stritem.hxx>
32 #include <svl/style.hxx>
33 #ifndef GCC
34 #endif
35 
36 #define _SVSTDARR_STRINGSDTOR
37 #include <svl/svstdarr.hxx>
38 #include <comphelper/processfactory.hxx>
39 #include <comphelper/sequenceashashmap.hxx>
40 #include <unotools/intlwrapper.hxx>
41 #include <comphelper/processfactory.hxx>
42 #include <com/sun/star/container/XNameAccess.hpp>
43 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
44 #include <com/sun/star/beans/PropertyValue.hpp>
45 #include <com/sun/star/frame/XModuleManager.hpp>
46 
47 #include "sfx2/sfxhelp.hxx"
48 #include <sfx2/app.hxx>
49 #include <sfx2/dispatch.hxx>
50 #include <sfx2/bindings.hxx>
51 #include <sfx2/templdlg.hxx>
52 #include "templdgi.hxx"
53 #include "tplcitem.hxx"
54 #include "sfxtypes.hxx"
55 #include <sfx2/styfitem.hxx>
56 #include <sfx2/objsh.hxx>
57 #include <sfx2/viewsh.hxx>
58 #include <sfx2/newstyle.hxx>
59 #include "sfx2/tplpitem.hxx"
60 #include "sfx2/sfxresid.hxx"
61 
62 #include "templdlg.hrc"
63 #include <sfx2/sfx.hrc>
64 #include "dialog.hrc"
65 #include "arrdecl.hxx"
66 #include "fltfnc.hxx"
67 #include <sfx2/docfilt.hxx>
68 #include <sfx2/docfac.hxx>
69 #include "docvor.hxx"
70 #include <sfx2/doctempl.hxx>
71 #include <sfx2/module.hxx>
72 #include "sfx2/imgmgr.hxx"
73 #include "helpid.hrc"
74 #include "appdata.hxx"
75 #include <sfx2/viewfrm.hxx>
76 
77 #include <comphelper/configurationhelper.hxx>
78 
79 using namespace ::com::sun::star;
80 using namespace ::com::sun::star::beans;
81 using namespace ::com::sun::star::frame;
82 using namespace ::com::sun::star::uno;
83 
84 //=========================================================================
85 
86 // Fenster wird ab jetzt dynamisch erstellt. Daher hier R"ander usw.
87 
88 #define SFX_TEMPLDLG_HFRAME         3
89 #define SFX_TEMPLDLG_VTOPFRAME      3
90 
91 #define SFX_TEMPLDLG_VBOTFRAME      3
92 #define SFX_TEMPLDLG_MIDHSPACE      3
93 #define SFX_TEMPLDLG_MIDVSPACE      3
94 #define SFX_TEMPLDLG_FILTERHEIGHT   100
95 
96 static sal_uInt16 nLastItemId = USHRT_MAX;
97 
98 // filter box has maximum 12 entries visible
99 #define MAX_FILTER_ENTRIES          12
100 
101 #define SFX_TEMPLDLG_FILTER_MAX             0xFFFF
102 #define SFX_TEMPLDLG_FILTER_HIERARCHICAL    0xFFFE
103 
104 //=========================================================================
105 
106 TYPEINIT0(SfxCommonTemplateDialog_Impl);
107 TYPEINIT1(SfxTemplateDialog_Impl,SfxCommonTemplateDialog_Impl);
108 TYPEINIT1(SfxTemplateCatalog_Impl,SfxCommonTemplateDialog_Impl);
109 
110 SFX_IMPL_DOCKINGWINDOW(SfxTemplateDialogWrapper, SID_STYLE_DESIGNER)
111 
112 //-------------------------------------------------------------------------
113 
114 // Redirektionsfunktionen
115 
116 SfxTemplateDialog::SfxTemplateDialog
117 (
118     SfxBindings *pBind,
119     SfxChildWindow *pCW,
120     Window *pParent
121 )
122 
123 /*  [Beschreibung]
124     Gestalterklasse.
125 */
126     : SfxDockingWindow( pBind, pCW, pParent, SfxResId(DLG_STYLE_DESIGNER) ),
127 
128     pImpl( new SfxTemplateDialog_Impl( pBind, this ) )
129 
130 {
131     pImpl->updateNonFamilyImages();
132 }
133 
134 //-------------------------------------------------------------------------
135 
136 SfxTemplateDialog::~SfxTemplateDialog()
137 {
138     delete pImpl;
139 }
140 
141 ISfxTemplateCommon* SfxTemplateDialog::GetISfxTemplateCommon()
142 {
143     return pImpl->GetISfxTemplateCommon();
144 }
145 
146 void SfxTemplateDialog::SetParagraphFamily()
147 {
148     // first select the paragraph family
149     pImpl->FamilySelect( SFX_STYLE_FAMILY_PARA );
150     // then select the automatic filter
151     pImpl->SetAutomaticFilter();
152 }
153 
154 // ------------------------------------------------------------------------
155 
156 void SfxTemplateDialog::DataChanged( const DataChangedEvent& _rDCEvt )
157 {
158     if ( ( DATACHANGED_SETTINGS == _rDCEvt.GetType() ) &&
159          ( 0 != ( SETTINGS_STYLE & _rDCEvt.GetFlags() ) ) )
160     {
161         pImpl->updateFamilyImages();
162         pImpl->updateNonFamilyImages();
163     }
164 
165     SfxDockingWindow::DataChanged( _rDCEvt );
166 }
167 
168 //-------------------------------------------------------------------------
169 
170 void SfxTemplateDialog::Update()
171 {
172     pImpl->Update();
173 }
174 
175 //-------------------------------------------------------------------------
176 
177 void SfxTemplateDialog::Resize()
178 {
179     if(pImpl)
180         pImpl->Resize();
181     SfxDockingWindow::Resize();
182 }
183 
184 
185 //-------------------------------------------------------------------------
186 
187 SfxChildAlignment SfxTemplateDialog::CheckAlignment(SfxChildAlignment eActAlign,SfxChildAlignment eAlign)
188 {
189     switch (eAlign)
190     {
191         case SFX_ALIGN_TOP:
192         case SFX_ALIGN_HIGHESTTOP:
193         case SFX_ALIGN_LOWESTTOP:
194         case SFX_ALIGN_BOTTOM:
195         case SFX_ALIGN_LOWESTBOTTOM:
196         case SFX_ALIGN_HIGHESTBOTTOM:
197             return eActAlign;
198 
199         case SFX_ALIGN_LEFT:
200         case SFX_ALIGN_RIGHT:
201         case SFX_ALIGN_FIRSTLEFT:
202         case SFX_ALIGN_LASTLEFT:
203         case SFX_ALIGN_FIRSTRIGHT:
204         case SFX_ALIGN_LASTRIGHT:
205             return eAlign;
206 
207         default:
208             return eAlign;
209     }
210 }
211 
212 //-------------------------------------------------------------------------
213 
214 SfxTemplateCatalog::SfxTemplateCatalog(Window *pParent, SfxBindings *pBindings)
215  : SfxModalDialog(pParent,SfxResId(RID_STYLECATALOG))
216 {
217     pImpl = new SfxTemplateCatalog_Impl(pParent, pBindings, this);
218 }
219 
220 //-------------------------------------------------------------------------
221 
222 SfxTemplateCatalog::~SfxTemplateCatalog()
223 {
224     delete pImpl;
225 }
226 
227 //-------------------------------------------------------------------------
228 
229 void DropListBox_Impl::MouseButtonDown( const MouseEvent& rMEvt )
230 {
231     nModifier = rMEvt.GetModifier();
232 
233     sal_Bool bHitEmptySpace = ( NULL == GetEntry( rMEvt.GetPosPixel(), sal_True ) );
234     if( bHitEmptySpace && ( rMEvt.GetClicks() == 2 ) && rMEvt.IsMod1() )
235         Control::MouseButtonDown( rMEvt );
236     else
237         SvTreeListBox::MouseButtonDown( rMEvt );
238 }
239 
240 sal_Int8 DropListBox_Impl::AcceptDrop( const AcceptDropEvent& rEvt )
241 
242 /*  [Description: ]
243     Drop is enabled as long as it is allowed to create a new style by example, i.e. to
244     create a style out of the current selection.
245 */
246 
247 {
248     if ( IsDropFormatSupported( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR ) )
249     {
250         // special case: page styles are allowed to create new styles by example
251         // but not allowed to be created by drag and drop
252         if( pDialog->nActFamily == SfxCommonTemplateDialog_Impl::SfxFamilyIdToNId( SFX_STYLE_FAMILY_PAGE ) ||
253                 pDialog->bNewByExampleDisabled )
254             return DND_ACTION_NONE;
255         else
256             return DND_ACTION_COPY;
257     }
258     return SvTreeListBox::AcceptDrop( rEvt );
259 }
260 
261 //-------------------------------------------------------------------------
262 
263 sal_Int8 DropListBox_Impl::ExecuteDrop( const ExecuteDropEvent& rEvt )
264 {
265 //  rEvt.maDropEvent.Context->acceptDrop( DND_ACTION_NONE );
266 //  rEvt.maDropEvent.Context->dropComplete( sal_True );
267 
268     sal_Int8 nRet = DND_ACTION_NONE;
269     SfxObjectShell* pDocShell = pDialog->GetObjectShell();
270     TransferableDataHelper aHelper( rEvt.maDropEvent.Transferable );
271     sal_uInt32 nFormatCount = aHelper.GetFormatCount();
272     if ( pDocShell )
273     {
274         sal_Bool bFormatFound = sal_False;
275 
276         for ( sal_uInt32 i = 0; i < nFormatCount; ++i )
277         {
278             SotFormatStringId nId = aHelper.GetFormat(i);
279             TransferableObjectDescriptor aDesc;
280 
281             if ( aHelper.GetTransferableObjectDescriptor( nId, aDesc ) )
282             {
283                 if ( aDesc.maClassName == pDocShell->GetFactory().GetClassId() )
284                 {
285                     PostUserEvent( LINK( this, DropListBox_Impl, OnAsyncExecuteDrop ), 0 );
286 
287                     bFormatFound = sal_True;
288                     nRet =  rEvt.mnAction;
289                     break;
290                 }
291             }
292         }
293 
294         if ( !bFormatFound )
295             return SvTreeListBox::ExecuteDrop( rEvt );
296     }
297 
298     return nRet;
299 }
300 
301 
302 IMPL_LINK( DropListBox_Impl, OnAsyncExecuteDrop, SvLBoxEntry*, EMPTYARG )
303 {
304     pDialog->ActionSelect( SID_STYLE_NEW_BY_EXAMPLE );
305     return 0;
306 }
307 
308 
309 IMPL_LINK( DropListBox_Impl, OnAsyncExecuteError, void*, NOTINTERESTEDIN )
310 {
311     (void)NOTINTERESTEDIN; // unused
312     ErrorHandler::HandleError( ERRCODE_IO_WRONGFORMAT );
313 
314     return 0;
315 }
316 
317 
318 long DropListBox_Impl::Notify( NotifyEvent& rNEvt )
319 {
320     long nRet = 0;
321     if( rNEvt.GetType() == EVENT_KEYINPUT )
322     {
323         const KeyCode&  rKeyCode = rNEvt.GetKeyEvent()->GetKeyCode();
324         if(!rKeyCode.GetModifier())
325         {
326             if( pDialog->bCanDel && KEY_DELETE == rKeyCode.GetCode())
327             {
328                 pDialog->DeleteHdl( NULL );
329                 nRet =  1;
330             }
331             else if( KEY_RETURN == rKeyCode.GetCode())
332             {
333                 GetDoubleClickHdl().Call(this);
334                 nRet = 1;
335             }
336         }
337     }
338     if(!nRet)
339         nRet = SvTreeListBox::Notify( rNEvt );
340     return nRet;
341 }
342 
343 
344 //-------------------------------------------------------------------------
345 
346 
347 SfxActionListBox::SfxActionListBox
348 (
349     SfxCommonTemplateDialog_Impl* pParent,
350     WinBits nWinBits
351 )
352 
353 /*  [Beschreibung]
354 
355     ListBox- Klasse, die im Command-Handler ein PopupMenu (Gestalter
356     spezifisch) startet.
357 
358 */
359 
360 :       DropListBox_Impl(pParent->GetWindow(), nWinBits, pParent)
361 
362 {
363     EnableContextMenuHandling();
364 }
365 
366 //-------------------------------------------------------------------------
367 
368 SfxActionListBox::SfxActionListBox( SfxCommonTemplateDialog_Impl* pParent,
369                                     const ResId &rResId) :
370     DropListBox_Impl(pParent->GetWindow(), rResId, pParent)
371 {
372     EnableContextMenuHandling();
373 }
374 
375 //-------------------------------------------------------------------------
376 
377 PopupMenu* SfxActionListBox::CreateContextMenu( void )
378 {
379     //added by BerryJia for fixing Bug102739 2002-9-9 17:00(Beijing Time)
380     if( !( GetSelectionCount() > 0 ) )
381     {
382         pDialog->EnableEdit( sal_False );
383         pDialog->EnableDel( sal_False );
384     }
385     return pDialog->CreateContextMenu();
386 }
387 
388 //-------------------------------------------------------------------------
389 
390 SfxTemplateDialogWrapper::SfxTemplateDialogWrapper(Window *pParentWnd,
391         sal_uInt16 nId,  SfxBindings *p, SfxChildWinInfo *pInfo) :
392     SfxChildWindow(pParentWnd, nId)
393 {
394     SfxTemplateDialog *pWin = new SfxTemplateDialog(p, this, pParentWnd);
395     pWindow = pWin;
396     eChildAlignment = SFX_ALIGN_NOALIGNMENT;
397 
398     pWin->Initialize( pInfo );
399     pWin->SetMinOutputSizePixel(pWin->pImpl->GetMinOutputSizePixel());
400 }
401 
402 void SfxTemplateDialogWrapper::SetParagraphFamily()
403 {
404     // forward to SfxTemplateDialog, because SfxTemplateDialog isn't exported
405     static_cast< SfxTemplateDialog* >( GetWindow() )->SetParagraphFamily();
406 }
407 
408 
409 
410 //===== SfxTemplatePanelControl ===============================================
411 
412 SfxTemplatePanelControl::SfxTemplatePanelControl (
413     SfxBindings* pBindings,
414     Window* pParentWindow)
415     : DockingWindow(pParentWindow, SfxResId(DLG_STYLE_DESIGNER) ),
416       pImpl(new SfxTemplateDialog_Impl(pBindings, this)),
417       mpBindings(pBindings)
418 {
419     OSL_ASSERT(mpBindings!=NULL);
420 
421     pImpl->updateNonFamilyImages();
422 }
423 
424 
425 
426 
427 SfxTemplatePanelControl::~SfxTemplatePanelControl (void)
428 {
429     delete pImpl;
430 }
431 
432 
433 
434 
435 ISfxTemplateCommon* SfxTemplatePanelControl::GetISfxTemplateCommon()
436 {
437     return pImpl->GetISfxTemplateCommon();
438 }
439 
440 
441 
442 
443 void SfxTemplatePanelControl::SetParagraphFamily()
444 {
445     // first select the paragraph family
446     pImpl->FamilySelect( SFX_STYLE_FAMILY_PARA );
447     // then select the automatic filter
448     pImpl->SetAutomaticFilter();
449 }
450 
451 
452 
453 
454 void SfxTemplatePanelControl::DataChanged( const DataChangedEvent& _rDCEvt )
455 {
456     if ( ( DATACHANGED_SETTINGS == _rDCEvt.GetType() ) &&
457          ( 0 != ( SETTINGS_STYLE & _rDCEvt.GetFlags() ) ) )
458     {
459         pImpl->updateFamilyImages();
460         pImpl->updateNonFamilyImages();
461     }
462 
463     DockingWindow::DataChanged( _rDCEvt );
464 }
465 
466 
467 
468 
469 void SfxTemplatePanelControl::Update()
470 {
471     pImpl->Update();
472 }
473 
474 
475 
476 
477 void SfxTemplatePanelControl::Resize()
478 {
479     if(pImpl)
480         pImpl->Resize();
481     DockingWindow::Resize();
482 }
483 
484 
485 void SfxTemplatePanelControl::FreeResource (void)
486 {
487     DockingWindow::FreeResource();
488 }
489 
490 
491 SfxChildAlignment SfxTemplatePanelControl::CheckAlignment(SfxChildAlignment eActAlign,SfxChildAlignment eAlign)
492 {
493     switch (eAlign)
494     {
495         case SFX_ALIGN_TOP:
496         case SFX_ALIGN_HIGHESTTOP:
497         case SFX_ALIGN_LOWESTTOP:
498         case SFX_ALIGN_BOTTOM:
499         case SFX_ALIGN_LOWESTBOTTOM:
500         case SFX_ALIGN_HIGHESTBOTTOM:
501             return eActAlign;
502 
503         case SFX_ALIGN_LEFT:
504         case SFX_ALIGN_RIGHT:
505         case SFX_ALIGN_FIRSTLEFT:
506         case SFX_ALIGN_LASTLEFT:
507         case SFX_ALIGN_FIRSTRIGHT:
508         case SFX_ALIGN_LASTRIGHT:
509             return eAlign;
510 
511         default:
512             return eAlign;
513     }
514 }
515 
516 
517 void SfxTemplatePanelControl::StateChanged( StateChangedType nStateChange )
518 {
519     if ( nStateChange == STATE_CHANGE_INITSHOW )
520     {
521         SfxViewFrame *pFrame = mpBindings->GetDispatcher_Impl()->GetFrame();
522         Window* pEditWin = pFrame->GetViewShell()->GetWindow();
523 
524         Size aSize = pEditWin->GetSizePixel();
525         Point aPoint = pEditWin->OutputToScreenPixel( pEditWin->GetPosPixel() );
526         aPoint = GetParent()->ScreenToOutputPixel( aPoint );
527         Size aWinSize = GetSizePixel();
528         aPoint.X() += aSize.Width() - aWinSize.Width() - 20;
529         aPoint.Y() += aSize.Height() / 2 - aWinSize.Height() / 2;
530         //      SetFloatingPos( aPoint );
531     }
532 
533     DockingWindow::StateChanged( nStateChange );
534 }
535 
536 
537 //=========================================================================
538 SV_DECL_PTRARR_DEL(ExpandedEntries, StringPtr,16,8)
539 SV_IMPL_PTRARR(ExpandedEntries, StringPtr)
540 
541 /*  [Beschreibung]
542 
543     TreeListBox- Klasse f"ur die Anzeige der hierarchischen View
544     der Vorlagen
545 
546 */
547 
548 class StyleTreeListBox_Impl : public DropListBox_Impl
549 {
550 private:
551     SvLBoxEntry*                    pCurEntry;
552     SfxCommonTemplateDialog_Impl*   pCommon;
553     Link                            aDoubleClickLink;
554     Link                            aDropLink;
555     String                          aParent;
556     String                          aStyle;
557 
558 protected:
559     virtual void    Command( const CommandEvent& rMEvt );
560     virtual long    Notify( NotifyEvent& rNEvt );
561     virtual sal_Bool    DoubleClickHdl();
562     virtual long    ExpandingHdl();
563     virtual void    ExpandedHdl();
564     virtual sal_Bool    NotifyMoving(SvLBoxEntry*  pTarget,
565                                      SvLBoxEntry*  pEntry,
566                                      SvLBoxEntry*& rpNewParent,
567                                      sal_uIntPtr&        rNewChildPos);
568 public:
569     StyleTreeListBox_Impl( SfxCommonTemplateDialog_Impl* pParent, WinBits nWinStyle = 0);
570 
571     void            SetDoubleClickHdl(const Link &rLink) { aDoubleClickLink = rLink; }
572     void            SetDropHdl(const Link &rLink) { aDropLink = rLink; }
573         using SvLBox::GetParent;
574     const String&   GetParent() const { return aParent; }
575     const String&   GetStyle() const { return aStyle; }
576     void            MakeExpanded_Impl(ExpandedEntries& rEntries) const;
577 
578     virtual PopupMenu* CreateContextMenu( void );
579 };
580 
581 //-------------------------------------------------------------------------
582 
583 
584 void StyleTreeListBox_Impl::MakeExpanded_Impl(ExpandedEntries& rEntries) const
585 {
586     SvLBoxEntry *pEntry;
587     sal_uInt16 nCount=0;
588     for(pEntry=(SvLBoxEntry*)FirstVisible();pEntry;pEntry=(SvLBoxEntry*)NextVisible(pEntry))
589     {
590         if(IsExpanded(pEntry))
591         {
592             StringPtr pString=new String(GetEntryText(pEntry));
593             rEntries.Insert(pString,nCount++);
594         }
595     }
596 }
597 
598 PopupMenu* StyleTreeListBox_Impl::CreateContextMenu()
599 {
600     return pDialog->CreateContextMenu();
601 }
602 
603 sal_Bool StyleTreeListBox_Impl::DoubleClickHdl()
604 
605 /*  [Beschreibung]
606 
607 
608     DoubleClick-Handler; ruft entsprechenden Link.
609     Virtuelle Methode aus SV.
610 
611 */
612 {
613     aDoubleClickLink.Call(this);
614     return sal_False;
615 }
616 
617 //-------------------------------------------------------------------------
618 
619 void StyleTreeListBox_Impl::Command( const CommandEvent& rCEvt )
620 
621 /*  [Beschreibung]
622 
623     Command Handler; dieser executed ein PopupMenu (Gestalter
624     spezifisch).
625     Virtuelle Methode aus SV.
626 
627 */
628 {
629     SvTreeListBox::Command(rCEvt);
630 }
631 
632 //-------------------------------------------------------------------------
633 
634 long StyleTreeListBox_Impl::Notify( NotifyEvent& rNEvt )
635 {
636     // handle <RETURN> as double click
637 
638     long nRet = 0;
639     if ( rNEvt.GetType() == EVENT_KEYINPUT )
640     {
641         const KeyCode&  rKeyCode = rNEvt.GetKeyEvent()->GetKeyCode();
642         if ( !rKeyCode.GetModifier() && KEY_RETURN == rKeyCode.GetCode() )
643         {
644             aDoubleClickLink.Call( this );
645             nRet = 1;
646         }
647     }
648 
649     if ( !nRet )
650         nRet = DropListBox_Impl::Notify( rNEvt );
651 
652     return nRet;
653 }
654 
655 //-------------------------------------------------------------------------
656 
657 sal_Bool StyleTreeListBox_Impl::NotifyMoving(SvLBoxEntry*  pTarget,
658                                          SvLBoxEntry*  pEntry,
659                                          SvLBoxEntry*& rpNewParent,
660                                          sal_uIntPtr& lPos)
661 /*  [Beschreibung]
662 
663     NotifyMoving Handler; dieser leitet per Link das Event an den Dialog
664     weiter.
665     Virtuelle Methode aus SV.
666 
667 */
668 {
669     if(!pTarget || !pEntry)
670         return sal_False;
671     aParent = GetEntryText(pTarget);
672     aStyle  = GetEntryText(pEntry);
673     const sal_Bool bRet = (sal_Bool)aDropLink.Call(this);
674     rpNewParent = pTarget;
675     lPos=0;
676     IntlWrapper aIntlWrapper( ::comphelper::getProcessServiceFactory(), Application::GetSettings().GetLocale() );
677     const CollatorWrapper* pCollator = aIntlWrapper.getCaseCollator();
678     for(SvLBoxEntry *pTmpEntry=FirstChild(pTarget);
679         pTmpEntry && COMPARE_LESS==pCollator->compareString(
680             GetEntryText(pTmpEntry),GetEntryText(pEntry));
681         pTmpEntry=NextSibling(pTmpEntry),lPos++) ;
682 
683     return bRet? (sal_Bool)2: sal_False;
684 }
685 
686 //-------------------------------------------------------------------------
687 
688 long  StyleTreeListBox_Impl::ExpandingHdl()
689 
690 /*  [Beschreibung]
691 
692     ExpandingHdl Handler; der aktuelle Eintrag wird gemerkt.
693     Virtuelle Methode aus SV.
694 
695     [Querverweise]
696     <StyleTreeListBox_Impl::ExpandedHdl()>
697 
698 */
699 {
700     pCurEntry = GetCurEntry();
701     return sal_True;
702 }
703 
704 //-------------------------------------------------------------------------
705 
706 void  StyleTreeListBox_Impl::ExpandedHdl()
707 
708 /*  [Beschreibung]
709 
710     ExpandedHdl Handler;
711     Virtuelle Methode aus SV.
712 
713     [Querverweise]
714     <StyleTreeListBox_Impl::ExpandingHdl()>
715 
716 */
717 
718 {
719     SvLBoxEntry *pEntry = GetHdlEntry();
720     if(!IsExpanded(pEntry) && pCurEntry != GetCurEntry())
721         SelectAll( sal_False );
722     pCurEntry = 0;
723 }
724 
725 //-------------------------------------------------------------------------
726 
727 StyleTreeListBox_Impl::StyleTreeListBox_Impl(
728     SfxCommonTemplateDialog_Impl* pParent, WinBits nWinStyle) :
729     DropListBox_Impl(pParent->GetWindow(), nWinStyle, pParent),
730     pCurEntry(0),
731     pCommon(pParent)
732 
733 /*  [Beschreibung]
734 
735     Konstruktor StyleTreeListBox_Impl
736 
737 */
738 {
739     EnableContextMenuHandling();
740 }
741 
742 //-------------------------------------------------------------------------
743 //-------------------------------------------------------------------------
744 
745 class StyleTreeArr_Impl;
746 
747 
748 /*  [Beschreibung]
749 
750     Interne Struktur f"ur den Aufbau der hierarchischen View
751 
752 */
753 
754 struct StyleTree_Impl
755 {
756     String aName;
757     String aParent;
758     StyleTreeArr_Impl *pChilds;
759     sal_Bool bIsExpanded;
760     sal_Bool HasParent() const { return aParent.Len() != 0; }
761 
762     StyleTree_Impl(const String &rName, const String &rParent):
763         aName(rName), aParent(rParent), pChilds(0), bIsExpanded(0) {}
764     ~StyleTree_Impl();
765     void Put(StyleTree_Impl* pIns, sal_uIntPtr lPos=ULONG_MAX);
766     sal_uIntPtr Count();
767 };
768 
769 typedef StyleTree_Impl* StyleTree_ImplPtr;
770 SV_DECL_PTRARR_DEL(StyleTreeArr_Impl, StyleTree_ImplPtr, 16, 8)
771 SV_IMPL_PTRARR(StyleTreeArr_Impl, StyleTree_ImplPtr)
772 
773 
774 sal_uIntPtr StyleTree_Impl::Count()
775 {
776     return pChilds ? pChilds->Count() : 0L;
777 }
778 
779 //-------------------------------------------------------------------------
780 
781 StyleTree_Impl::~StyleTree_Impl()
782 {
783     delete pChilds;
784 }
785 
786 //-------------------------------------------------------------------------
787 
788 void StyleTree_Impl::Put(StyleTree_Impl* pIns, sal_uIntPtr lPos)
789 {
790     if ( !pChilds )
791         pChilds = new StyleTreeArr_Impl;
792 
793     if ( ULONG_MAX == lPos )
794         lPos = pChilds->Count();
795     pChilds->Insert( pIns, (sal_uInt16)lPos );
796 }
797 
798 //-------------------------------------------------------------------------
799 
800 StyleTreeArr_Impl &MakeTree_Impl(StyleTreeArr_Impl &rArr)
801 {
802     const sal_uInt16 nCount = rArr.Count();
803     // Alle unter ihren Parents einordnen
804     sal_uInt16 i;
805     for(i = 0; i < nCount; ++i)
806     {
807         StyleTree_ImplPtr pEntry = rArr[i];
808         if(pEntry->HasParent())
809         {
810             for(sal_uInt16 j = 0; j < nCount; ++j)
811             {
812                 StyleTree_ImplPtr pCmp = rArr[j];
813                 if(pCmp->aName == pEntry->aParent)
814                 {
815                     // initial sortiert einfuegen
816                     sal_uInt16 ii;
817                     IntlWrapper aIntlWrapper( ::comphelper::getProcessServiceFactory(), Application::GetSettings().GetLocale() );
818                     const CollatorWrapper* pCollator = aIntlWrapper.getCaseCollator();
819                     for ( ii = 0;
820                          ii < pCmp->Count() && COMPARE_LESS ==
821                          pCollator->compareString( (*pCmp->pChilds)[ii]->aName,
822                                         pEntry->aName);++ii) ;
823                     pCmp->Put(pEntry,ii);
824                     break;
825                 }
826             }
827         }
828     }
829     // alle, die schon unter ihrem Parent eingeordnet wurden
830     // entfernen
831     for(i = 0; i < rArr.Count(); )
832     {
833         if(rArr[i]->HasParent())
834             rArr.Remove(i);
835         else
836             ++i;
837     }
838     return rArr;
839 }
840 
841 //-------------------------------------------------------------------------
842 
843 
844 inline sal_Bool IsExpanded_Impl( const ExpandedEntries& rEntries,
845                              const String &rStr)
846 {
847     sal_uInt16 nCount=rEntries.Count();
848     for(sal_uInt16 n=0;n<nCount;n++)
849         if(*rEntries[n]==rStr)
850             return sal_True;
851     return sal_False;
852 }
853 
854 
855 
856 SvLBoxEntry* FillBox_Impl(SvTreeListBox *pBox,
857                                  StyleTree_ImplPtr pEntry,
858                                  const ExpandedEntries& rEntries,
859                                  SvLBoxEntry* pParent = 0)
860 {
861     SvLBoxEntry* pNewEntry = pBox->InsertEntry(pEntry->aName, pParent);
862     const sal_uInt16 nCount = pEntry->pChilds? pEntry->pChilds->Count(): 0;
863     for(sal_uInt16 i = 0; i < nCount; ++i)
864         FillBox_Impl(pBox, (*pEntry->pChilds)[i], rEntries, pNewEntry);
865     return pNewEntry;
866 }
867 
868 //-------------------------------------------------------------------------
869 
870 // Konstruktor
871 
872 SfxCommonTemplateDialog_Impl::SfxCommonTemplateDialog_Impl( SfxBindings* pB, Window* pW, bool ) :
873 
874     aISfxTemplateCommon     ( this ),
875     pBindings               ( pB ),
876     pWindow                 ( pW ),
877     pModule                 ( NULL ),
878     pTimer                  ( NULL ),
879     m_pStyleFamiliesId      ( NULL ),
880     pStyleSheetPool         ( NULL ),
881     pTreeBox                ( NULL ),
882     pCurObjShell            ( NULL ),
883     xModuleManager          ( ::comphelper::getProcessServiceFactory()->createInstance(
884                                 DEFINE_CONST_UNICODE("com.sun.star.frame.ModuleManager") ), UNO_QUERY ),
885     pbDeleted               ( NULL ),
886 
887     aFmtLb                  ( this, WB_BORDER | WB_TABSTOP | WB_SORT | WB_QUICK_SEARCH ),
888     aFilterLb               ( pW, WB_BORDER | WB_DROPDOWN | WB_TABSTOP ),
889 
890     nActFamily              ( SFX_TEMPLDLG_FILTER_MAX ),
891     nActFilter              ( 0 ),
892     nAppFilter              ( 0 ),
893 
894     bDontUpdate             ( sal_False ),
895     bIsWater                ( sal_False ),
896     bEnabled                ( sal_True ),
897     bUpdate                 ( sal_False ),
898     bUpdateFamily           ( sal_False ),
899     bCanEdit                ( sal_False ),
900     bCanDel                 ( sal_False ),
901     bCanNew                 ( sal_True ),
902     bWaterDisabled          ( sal_False ),
903     bNewByExampleDisabled   ( sal_False ),
904     bUpdateByExampleDisabled( sal_False ),
905     bTreeDrag               ( sal_True ),
906     bHierarchical           ( sal_False ),
907     bBindingUpdate          ( sal_True )
908 {
909     aFmtLb.SetAccessibleName(SfxResId(STR_STYLE_ELEMTLIST));
910     aFmtLb.SetHelpId( HID_TEMPLATE_FMT );
911     aFilterLb.SetHelpId( HID_TEMPLATE_FILTER );
912     aFmtLb.SetStyle( aFmtLb.GetStyle() | WB_SORT | WB_HIDESELECTION );
913     Font aFont = aFmtLb.GetFont();
914     aFont.SetWeight( WEIGHT_NORMAL );
915     aFmtLb.SetFont( aFont );
916 }
917 
918 //-------------------------------------------------------------------------
919 
920 SfxCommonTemplateDialog_Impl::SfxCommonTemplateDialog_Impl( SfxBindings* pB, Window* pW ) :
921 
922     aISfxTemplateCommon     ( this ),
923     pBindings               ( pB ),
924     pWindow                 ( pW ),
925     pModule                 ( NULL ),
926     pTimer                  ( NULL ),
927     pStyleSheetPool         ( NULL ),
928     pTreeBox                ( NULL ),
929     pCurObjShell            ( NULL ),
930     pbDeleted               ( NULL ),
931 
932     aFmtLb                  ( this, SfxResId( BT_VLIST ) ),
933     aFilterLb               ( pW, SfxResId( BT_FLIST ) ),
934 
935     nActFamily              ( SFX_TEMPLDLG_FILTER_MAX ),
936     nActFilter              ( 0 ),
937     nAppFilter              ( 0 ),
938 
939     bDontUpdate             ( sal_False ),
940     bIsWater                ( sal_False ),
941     bEnabled                ( sal_True ),
942     bUpdate                 ( sal_False ),
943     bUpdateFamily           ( sal_False ),
944     bCanEdit                ( sal_False ),
945     bCanDel                 ( sal_False ),
946     bCanNew                 ( sal_True ),
947     bWaterDisabled          ( sal_False ),
948     bNewByExampleDisabled   ( sal_False ),
949     bUpdateByExampleDisabled( sal_False ),
950     bTreeDrag               ( sal_True ),
951     bHierarchical           ( sal_False ),
952     bBindingUpdate          ( sal_True )
953 
954 {
955     aFmtLb.SetStyle( aFmtLb.GetStyle() | WB_SORT );
956 }
957 
958 //-------------------------------------------------------------------------
959 
960 sal_uInt16 SfxCommonTemplateDialog_Impl::StyleNrToInfoOffset(sal_uInt16 nId)
961 {
962     const SfxStyleFamilyItem *pItem=pStyleFamilies->GetObject(nId);
963     return SfxFamilyIdToNId(pItem->GetFamily())-1;
964 }
965 
966 //-------------------------------------------------------------------------
967 
968 void SfxTemplateDialog_Impl::EnableEdit(sal_Bool bEnable)
969 {
970     SfxCommonTemplateDialog_Impl::EnableEdit( bEnable );
971     if( !bEnable || !bUpdateByExampleDisabled )
972         EnableItem( SID_STYLE_UPDATE_BY_EXAMPLE, bEnable);
973 }
974 
975 //-------------------------------------------------------------------------
976 
977 
978 sal_uInt16 SfxCommonTemplateDialog_Impl::InfoOffsetToStyleNr(sal_uInt16 nId)
979 {
980     for ( sal_uInt16 i=0;i<pStyleFamilies->Count();i++ )
981         if ( SfxFamilyIdToNId(pStyleFamilies->GetObject(i)->GetFamily()) == nId+1 )
982             return i;
983     DBG_ERROR("Style Nummer nicht gefunden");
984     return 0;
985 }
986 
987 
988 //-------------------------------------------------------------------------
989 
990 void SfxCommonTemplateDialog_Impl::ReadResource()
991 {
992     // globale Benutzer-Resource auslesen
993     sal_uInt16 i;
994     for(i = 0; i < MAX_FAMILIES; ++i)
995         pFamilyState[i] = 0;
996 
997     SfxViewFrame* pViewFrame = pBindings->GetDispatcher_Impl()->GetFrame();
998     pCurObjShell = pViewFrame->GetObjectShell();
999     ResMgr* pMgr = pCurObjShell ? pCurObjShell->GetResMgr() : NULL;
1000     ResId aFamId( DLG_STYLE_DESIGNER, *pMgr );
1001     aFamId.SetRT(RSC_SFX_STYLE_FAMILIES);
1002     m_pStyleFamiliesId = new ResId( aFamId.GetId(), *pMgr );
1003     m_pStyleFamiliesId->SetRT(RSC_SFX_STYLE_FAMILIES);
1004     if( !pMgr || !pMgr->IsAvailable( aFamId ) )
1005         pStyleFamilies = new SfxStyleFamilies;
1006     else
1007         pStyleFamilies = new SfxStyleFamilies( aFamId );
1008 
1009     nActFilter = pCurObjShell ?
1010         static_cast< sal_uInt16 >( LoadFactoryStyleFilter( pCurObjShell ) ) :
1011         SFX_TEMPLDLG_FILTER_MAX;
1012     if ( pCurObjShell && SFX_TEMPLDLG_FILTER_MAX == nActFilter )
1013         nActFilter = pCurObjShell->GetAutoStyleFilterIndex();
1014 
1015         // Einfuegen in die Toolbox
1016         // umgekehrte Reihenfolge, da immer vorne eingefuegt wird.
1017     sal_uInt16 nCount = pStyleFamilies->Count();
1018 
1019     pBindings->ENTERREGISTRATIONS();
1020 
1021     for(i = 0; i < nCount; ++i)
1022     {
1023         sal_uInt16 nSlot = 0;
1024         switch((sal_uInt16)pStyleFamilies->GetObject(i)->GetFamily())
1025         {
1026             case SFX_STYLE_FAMILY_CHAR: nSlot = SID_STYLE_FAMILY1; break;
1027             case SFX_STYLE_FAMILY_PARA: nSlot = SID_STYLE_FAMILY2; break;
1028             case SFX_STYLE_FAMILY_FRAME:nSlot = SID_STYLE_FAMILY3; break;
1029             case SFX_STYLE_FAMILY_PAGE: nSlot = SID_STYLE_FAMILY4; break;
1030             case SFX_STYLE_FAMILY_PSEUDO: nSlot = SID_STYLE_FAMILY5; break;
1031             default: DBG_ERROR("unbekannte StyleFamily"); break;
1032         }
1033         pBoundItems[i] =
1034             new SfxTemplateControllerItem(nSlot, *this, *pBindings);
1035     }
1036     pBoundItems[i++] = new SfxTemplateControllerItem(
1037         SID_STYLE_WATERCAN, *this, *pBindings);
1038     pBoundItems[i++] = new SfxTemplateControllerItem(
1039         SID_STYLE_NEW_BY_EXAMPLE, *this, *pBindings);
1040     pBoundItems[i++] = new SfxTemplateControllerItem(
1041         SID_STYLE_UPDATE_BY_EXAMPLE, *this, *pBindings);
1042     pBoundItems[i++] = new SfxTemplateControllerItem(
1043         SID_STYLE_NEW, *this, *pBindings);
1044     pBoundItems[i++] = new SfxTemplateControllerItem(
1045         SID_STYLE_DRAGHIERARCHIE, *this, *pBindings);
1046     pBoundItems[i++] = new SfxTemplateControllerItem(
1047         SID_STYLE_EDIT, *this, *pBindings);
1048     pBoundItems[i++] = new SfxTemplateControllerItem(
1049         SID_STYLE_DELETE, *this, *pBindings);
1050     pBoundItems[i++] = new SfxTemplateControllerItem(
1051         SID_STYLE_FAMILY, *this, *pBindings);
1052     pBindings->LEAVEREGISTRATIONS();
1053 
1054     for(; i < COUNT_BOUND_FUNC; ++i)
1055         pBoundItems[i] = 0;
1056 
1057     StartListening(*pBindings);
1058 
1059 //In umgekehrter Reihenfolge des Auftretens in den Stylefamilies einfuegen.
1060 //Das ist fuer den Toolbar des Gestalters. Die Listbox des Kataloges achtet
1061 //selbst auf korrekte Reihenfolge.
1062 
1063 //Reihenfolgen: Reihenfolge in der Resource = Reihenfolge in Toolbar bzw.
1064 //Listbox.
1065 //Reihenfolge aufsteigender SIDs: Niedrige SIDs werden als erstes angezeigt,
1066 //wenn Vorlagen mehrerer Familien aktiv sind.
1067 
1068     // in the Writer the UpdateStyleByExample Toolbox button is removed and
1069     // the NewStyle button gets a PopupMenu
1070     if(nCount > 4)
1071         ReplaceUpdateButtonByMenu();
1072 
1073     for( ; nCount--; )
1074     {
1075         const SfxStyleFamilyItem *pItem = pStyleFamilies->GetObject( nCount );
1076         sal_uInt16 nId = SfxFamilyIdToNId( pItem->GetFamily() );
1077         InsertFamilyItem( nId, pItem );
1078     }
1079 
1080     LoadedFamilies();
1081 
1082     sal_uInt16 nStart = SID_STYLE_FAMILY1;
1083     sal_uInt16 nEnd = SID_STYLE_FAMILY4;
1084 
1085     for ( i = nStart; i <= nEnd; i++ )
1086         pBindings->Update(i);
1087 
1088     pModule = pCurObjShell ? pCurObjShell->GetModule() : NULL;
1089 }
1090 
1091 //-------------------------------------------------------------------------
1092 
1093 void SfxCommonTemplateDialog_Impl::ClearResource()
1094 {
1095     ClearFamilyList();
1096     DELETEX(pStyleFamilies);
1097     sal_uInt16 i;
1098     for ( i = 0; i < MAX_FAMILIES; ++i )
1099         DELETEX(pFamilyState[i]);
1100     for ( i = 0; i < COUNT_BOUND_FUNC; ++i )
1101         delete pBoundItems[i];
1102     pCurObjShell = NULL;
1103 
1104     DELETEZ( m_pStyleFamiliesId );
1105 }
1106 
1107 //-------------------------------------------------------------------------
1108 
1109 void SfxCommonTemplateDialog_Impl::Initialize()
1110 {
1111     // globale Benutzer-Resource auslesen
1112     ReadResource();
1113     pBindings->Invalidate( SID_STYLE_FAMILY );
1114     pBindings->Update( SID_STYLE_FAMILY );
1115     Update_Impl();
1116 
1117     aFilterLb.SetSelectHdl( LINK( this, SfxCommonTemplateDialog_Impl, FilterSelectHdl ) );
1118     aFmtLb.SetDoubleClickHdl( LINK( this, SfxCommonTemplateDialog_Impl, ApplyHdl ) );
1119     aFmtLb.SetSelectHdl( LINK( this, SfxCommonTemplateDialog_Impl, FmtSelectHdl ) );
1120 
1121     aFilterLb.Show();
1122     aFmtLb.Show();
1123 }
1124 
1125 //-------------------------------------------------------------------------
1126 
1127 SfxCommonTemplateDialog_Impl::~SfxCommonTemplateDialog_Impl()
1128 {
1129     String aEmpty;
1130     if ( bIsWater )
1131         Execute_Impl(SID_STYLE_WATERCAN, aEmpty, aEmpty, 0);
1132     GetWindow()->Hide();
1133     DELETEX(pStyleFamilies);
1134     DELETEZ( m_pStyleFamiliesId );
1135     sal_uInt16 i;
1136     for ( i = 0; i < MAX_FAMILIES; ++i )
1137         DELETEX(pFamilyState[i]);
1138     for ( i = 0; i < COUNT_BOUND_FUNC; ++i )
1139         delete pBoundItems[i];
1140     if ( pStyleSheetPool )
1141         EndListening(*pStyleSheetPool);
1142     pStyleSheetPool = NULL;
1143     delete pTreeBox;
1144     delete pTimer;
1145     if ( pbDeleted )
1146     {
1147         pbDeleted->bDead = true;
1148         pbDeleted = NULL;
1149     }
1150 }
1151 
1152 //-------------------------------------------------------------------------
1153 
1154 sal_uInt16 SfxCommonTemplateDialog_Impl::SfxFamilyIdToNId( SfxStyleFamily nFamily )
1155 {
1156     switch ( nFamily )
1157     {
1158         case SFX_STYLE_FAMILY_CHAR:   return 1;
1159         case SFX_STYLE_FAMILY_PARA:   return 2;
1160         case SFX_STYLE_FAMILY_FRAME:  return 3;
1161         case SFX_STYLE_FAMILY_PAGE:   return 4;
1162         case SFX_STYLE_FAMILY_PSEUDO: return 5;
1163         default:                      return 0;
1164     }
1165 }
1166 
1167 void SfxCommonTemplateDialog_Impl::SetAutomaticFilter()
1168 {
1169     sal_uInt16 nCount = aFilterLb.GetEntryCount();
1170     for ( sal_uInt16 i = 0; i < nCount; ++i )
1171     {
1172         sal_uIntPtr nFlags = (sal_uIntPtr)aFilterLb.GetEntryData(i);
1173         if ( SFXSTYLEBIT_AUTO == nFlags )
1174         {
1175             // automatic entry found -> select it
1176             aFilterLb.SelectEntryPos(i);
1177             // then call the handler to filter the styles
1178             FilterSelect( i - 1 );
1179             break;
1180         }
1181     }
1182 }
1183 
1184 //-------------------------------------------------------------------------
1185 
1186 // Hilfsfunktion: Zugriff auf aktuelles Family-Item
1187 const SfxStyleFamilyItem *SfxCommonTemplateDialog_Impl::GetFamilyItem_Impl() const
1188 {
1189     const sal_uInt16 nCount = pStyleFamilies->Count();
1190     for(sal_uInt16 i = 0; i < nCount; ++i)
1191     {
1192         const SfxStyleFamilyItem *pItem = pStyleFamilies->GetObject(i);
1193 //        if(!pItem)continue;
1194         sal_uInt16 nId = SfxFamilyIdToNId(pItem->GetFamily());
1195         if(nId == nActFamily)
1196             return pItem;
1197     }
1198     return 0;
1199 }
1200 
1201 //-------------------------------------------------------------------------
1202 
1203 void SfxCommonTemplateDialog_Impl::SelectStyle(const String &rStr)
1204 {
1205     const SfxStyleFamilyItem* pItem = GetFamilyItem_Impl();
1206     if ( !pItem )
1207         return;
1208     const SfxStyleFamily eFam = pItem->GetFamily();
1209     SfxStyleSheetBase* pStyle = pStyleSheetPool->Find( rStr, eFam, SFXSTYLEBIT_ALL );
1210     if( pStyle )
1211         EnableEdit( !(pStyle->GetMask() & SFXSTYLEBIT_READONLY) );
1212     else
1213         EnableEdit(sal_False);
1214 
1215     if ( pTreeBox )
1216     {
1217         if ( rStr.Len() )
1218         {
1219             SvLBoxEntry* pEntry = pTreeBox->First();
1220             while ( pEntry )
1221             {
1222                 if ( pTreeBox->GetEntryText( pEntry ) == rStr )
1223                 {
1224                     pTreeBox->MakeVisible( pEntry );
1225                     pTreeBox->Select( pEntry );
1226                     return;
1227                 }
1228                 pEntry = pTreeBox->Next( pEntry );
1229             }
1230         }
1231         else
1232             pTreeBox->SelectAll( sal_False );
1233     }
1234     else
1235     {
1236         sal_Bool bSelect = ( rStr.Len() > 0 );
1237         if ( bSelect )
1238         {
1239             SvLBoxEntry* pEntry = (SvLBoxEntry*)aFmtLb.FirstVisible();
1240             while ( pEntry && aFmtLb.GetEntryText( pEntry ) != rStr )
1241                 pEntry = (SvLBoxEntry*)aFmtLb.NextVisible( pEntry );
1242             if ( !pEntry )
1243                 bSelect = sal_False;
1244             else
1245             {
1246                 aFmtLb.MakeVisible( pEntry );
1247                 aFmtLb.Select( pEntry );
1248                 bWaterDisabled = !HasSelectedStyle(); //added by BerryJia for fixing Bug76391 2003-1-22
1249                 FmtSelectHdl( NULL );
1250             }
1251         }
1252 
1253         if ( !bSelect )
1254         {
1255             aFmtLb.SelectAll( sal_False );
1256             EnableEdit(sal_False);
1257         }
1258     }
1259 }
1260 
1261 //-------------------------------------------------------------------------
1262 
1263 String SfxCommonTemplateDialog_Impl::GetSelectedEntry() const
1264 {
1265     String aRet;
1266     if ( pTreeBox )
1267     {
1268         SvLBoxEntry* pEntry = pTreeBox->FirstSelected();
1269         if ( pEntry )
1270             aRet = pTreeBox->GetEntryText( pEntry );
1271     }
1272     else
1273     {
1274         SvLBoxEntry* pEntry = aFmtLb.FirstSelected();
1275         if ( pEntry )
1276             aRet = aFmtLb.GetEntryText( pEntry );
1277     }
1278     return aRet;
1279 }
1280 
1281 //-------------------------------------------------------------------------
1282 
1283 void SfxCommonTemplateDialog_Impl::EnableTreeDrag( sal_Bool bEnable )
1284 {
1285     if ( pStyleSheetPool )
1286     {
1287         SfxStyleSheetBase* pStyle = pStyleSheetPool->First();
1288         if ( pTreeBox )
1289         {
1290             if ( pStyle && pStyle->HasParentSupport() && bEnable )
1291                 pTreeBox->SetDragDropMode(SV_DRAGDROP_CTRL_MOVE);
1292             else
1293                 pTreeBox->SetDragDropMode(SV_DRAGDROP_NONE);
1294         }
1295     }
1296     bTreeDrag = bEnable;
1297 }
1298 
1299 //-------------------------------------------------------------------------
1300 
1301 void SfxCommonTemplateDialog_Impl::FillTreeBox()
1302 {
1303     DBG_ASSERT( pTreeBox, "FillTreeBox() without treebox");
1304     if(pStyleSheetPool && nActFamily != SFX_TEMPLDLG_FILTER_MAX)
1305     {
1306         const SfxStyleFamilyItem *pItem = GetFamilyItem_Impl();
1307         pStyleSheetPool->SetSearchMask(pItem->GetFamily(), SFXSTYLEBIT_ALL);
1308         StyleTreeArr_Impl aArr;
1309         SfxStyleSheetBase *pStyle = pStyleSheetPool->First();
1310         if(pStyle && pStyle->HasParentSupport() && bTreeDrag )
1311             pTreeBox->SetDragDropMode(SV_DRAGDROP_CTRL_MOVE);
1312         else
1313             pTreeBox->SetDragDropMode(SV_DRAGDROP_NONE);
1314         while(pStyle)
1315         {
1316             StyleTree_ImplPtr pNew =
1317                 new StyleTree_Impl(pStyle->GetName(), pStyle->GetParent());
1318             aArr.Insert(pNew, aArr.Count());
1319             pStyle = pStyleSheetPool->Next();
1320         }
1321         MakeTree_Impl(aArr);
1322         ExpandedEntries aEntries;
1323         if(pTreeBox)
1324             ((const StyleTreeListBox_Impl *)pTreeBox)->
1325                 MakeExpanded_Impl( aEntries);
1326         pTreeBox->SetUpdateMode( sal_False );
1327         pTreeBox->Clear();
1328         const sal_uInt16 nCount = aArr.Count();
1329         for(sal_uInt16 i = 0; i < nCount; ++i)
1330             FillBox_Impl(pTreeBox, aArr[i], aEntries);
1331 
1332 //      EnableEdit(sal_False);
1333         EnableItem(SID_STYLE_WATERCAN,sal_False);
1334 
1335         SfxTemplateItem* pState = pFamilyState[nActFamily-1];
1336 
1337         if ( nCount )
1338             pTreeBox->Expand( pTreeBox->First() );
1339 
1340         for ( SvLBoxEntry* pEntry = pTreeBox->First(); pEntry; pEntry = pTreeBox->Next( pEntry ) )
1341         {
1342             if ( IsExpanded_Impl( aEntries, pTreeBox->GetEntryText( pEntry ) ) )
1343                 pTreeBox->Expand( pEntry );
1344         }
1345 
1346         pTreeBox->SetUpdateMode( sal_True );
1347 
1348         String aStyle;
1349         if(pState)  //Aktuellen Eintrag selektieren
1350             aStyle = pState->GetStyleName();
1351         SelectStyle(aStyle);
1352         EnableDelete();
1353     }
1354 }
1355 
1356 //-------------------------------------------------------------------------
1357 sal_Bool SfxCommonTemplateDialog_Impl::HasSelectedStyle() const
1358 {
1359     return pTreeBox? pTreeBox->FirstSelected() != 0:
1360             aFmtLb.GetSelectionCount() != 0;
1361 }
1362 
1363 
1364 //-------------------------------------------------------------------------
1365 
1366 // intern: Aktualisierung der Anzeige
1367 void SfxCommonTemplateDialog_Impl::UpdateStyles_Impl(sal_uInt16 nFlags)     // Flags, was aktualisiert werden soll (s.o.)
1368 {
1369     DBG_ASSERT(nFlags, "nichts zu tun");
1370     const SfxStyleFamilyItem *pItem = GetFamilyItem_Impl();
1371     if (!pItem)
1372     {
1373         // Ist beim Vorlagenkatalog der Fall
1374         SfxTemplateItem **ppItem = pFamilyState;
1375         const sal_uInt16 nFamilyCount = pStyleFamilies->Count();
1376         sal_uInt16 n;
1377         for(n=0;n<nFamilyCount;n++)
1378             if(ppItem[StyleNrToInfoOffset(n)])break;
1379         if ( n == nFamilyCount )
1380             // passiert gelegentlich bei Beichten, Formularen etc.; weiss der Teufel warum
1381             return;
1382         ppItem+=StyleNrToInfoOffset(n);
1383         nAppFilter = (*ppItem)->GetValue();
1384         FamilySelect(  StyleNrToInfoOffset(n)+1 );
1385         pItem = GetFamilyItem_Impl();
1386     }
1387 
1388     const SfxStyleFamily eFam = pItem->GetFamily();
1389 
1390     SfxFilterTupel *pT = pItem->GetFilterList().GetObject(nActFilter);
1391     sal_uInt16 nFilter = pT ? pItem->GetFilterList().GetObject(nActFilter)->nFlags : 0;
1392     if(!nFilter)    // automatisch
1393         nFilter = nAppFilter;
1394 
1395     DBG_ASSERT(pStyleSheetPool, "kein StyleSheetPool");
1396     if(pStyleSheetPool)
1397     {
1398         pStyleSheetPool->SetSearchMask(eFam, nFilter);
1399         pItem = GetFamilyItem_Impl();
1400         if((nFlags & UPDATE_FAMILY) == UPDATE_FAMILY)
1401         {
1402             CheckItem(nActFamily, sal_True);    // Button in Toolbox checken
1403             aFilterLb.SetUpdateMode(sal_False);
1404             aFilterLb.Clear();
1405             //insert hierarchical at the beginning
1406             sal_uInt16 nPos = aFilterLb.InsertEntry(String(SfxResId(STR_STYLE_FILTER_HIERARCHICAL)), 0);
1407             aFilterLb.SetEntryData( nPos, (void*)(sal_uIntPtr)SFXSTYLEBIT_ALL );
1408             const SfxStyleFilter& rFilter = pItem->GetFilterList();
1409             for(sal_uInt16 i = 0; i < rFilter.Count(); ++i)
1410             {
1411                 sal_uIntPtr nFilterFlags = rFilter.GetObject(i)->nFlags;
1412                 nPos = aFilterLb.InsertEntry( rFilter.GetObject(i)->aName );
1413                 aFilterLb.SetEntryData( nPos, (void*)nFilterFlags );
1414             }
1415             if(nActFilter < aFilterLb.GetEntryCount() - 1)
1416                 aFilterLb.SelectEntryPos(nActFilter + 1);
1417             else
1418             {
1419                 nActFilter = 0;
1420                 aFilterLb.SelectEntryPos(1);
1421                 SfxFilterTupel *pActT = rFilter.GetObject(nActFilter);
1422                 sal_uInt16 nFilterFlags = pActT ? rFilter.GetObject(nActFilter)->nFlags : 0;
1423                 pStyleSheetPool->SetSearchMask(eFam, nFilterFlags);
1424             }
1425 
1426             //Falls in Treedarstellung wieder Family Hierarchie selektieren
1427             if(pTreeBox)
1428                 aFilterLb.SelectEntry(String(SfxResId(STR_STYLE_FILTER_HIERARCHICAL)));
1429 
1430             // show maximum 12 entries
1431             aFilterLb.SetDropDownLineCount( MAX_FILTER_ENTRIES );
1432             aFilterLb.SetUpdateMode(sal_True);
1433         }
1434         else
1435         {
1436             if( nActFilter < aFilterLb.GetEntryCount() - 1)
1437                 aFilterLb.SelectEntryPos(nActFilter + 1);
1438             else
1439             {
1440                 nActFilter = 0;
1441                 aFilterLb.SelectEntryPos(1);
1442             }
1443         }
1444 
1445         if(nFlags & UPDATE_FAMILY_LIST)
1446         {
1447 //          EnableEdit(sal_False);
1448             EnableItem(SID_STYLE_WATERCAN,sal_False);
1449 
1450             SfxStyleSheetBase *pStyle = pStyleSheetPool->First();
1451             SvLBoxEntry* pEntry = aFmtLb.First();
1452             SvStringsDtor aStrings;
1453 
1454             while( pStyle )
1455             {
1456                 //Bubblesort
1457                 for( sal_uInt16 nPos = aStrings.Count() + 1 ; nPos-- ;)
1458                 {
1459                     if( !nPos || *aStrings[nPos-1] < pStyle->GetName() )
1460                     {
1461                         // Die Namen stehen in den Styles, also nicht kopieren
1462                         // Reingefallen!: Writer hat insgesamt nur 1 Style
1463                         aStrings.Insert(
1464                             new String( pStyle->GetName() ), nPos );
1465                         break;
1466                     }
1467                 }
1468                 pStyle = pStyleSheetPool->Next();
1469             }
1470 
1471 
1472             sal_uInt16 nCount = aStrings.Count();
1473             sal_uInt16 nPos = 0;
1474             while( nPos < nCount && pEntry &&
1475                    *aStrings[ nPos ] == aFmtLb.GetEntryText( pEntry ) )
1476             {
1477                 nPos++;
1478                 pEntry = aFmtLb.Next( pEntry );
1479             }
1480 
1481             if( nPos < nCount || pEntry )
1482             {
1483                 // Box mit den Vorlagen fuellen
1484                 aFmtLb.SetUpdateMode(sal_False);
1485                 aFmtLb.Clear();
1486 
1487                 nPos = 0;
1488                 while( nPos < nCount )
1489                     aFmtLb.InsertEntry( *aStrings.GetObject( nPos++ ));
1490                 aFmtLb.SetUpdateMode(sal_True);
1491             }
1492                 // aktuelle Vorlage anzeigen
1493             SfxTemplateItem *pState = pFamilyState[nActFamily-1];
1494             String aStyle;
1495             if(pState)  //Aktuellen Eintrag selektieren
1496                 aStyle = pState->GetStyleName();
1497             SelectStyle(aStyle);
1498             EnableDelete();
1499         }
1500     }
1501 }
1502 
1503 //-------------------------------------------------------------------------
1504 
1505 // Aktualisierung Anzeige: Gie\skanne an/aus
1506 void SfxCommonTemplateDialog_Impl::SetWaterCanState(const SfxBoolItem *pItem)
1507 {
1508 //  EnableItem(SID_STYLE_WATERCAN, pItem != 0);
1509     bWaterDisabled =  pItem == 0;
1510 //added by BerryJia for fixing Bug76391 2003-1-7
1511     if(!bWaterDisabled)
1512         bWaterDisabled = !HasSelectedStyle();
1513 
1514     if(pItem && !bWaterDisabled)
1515     {
1516         CheckItem(SID_STYLE_WATERCAN, pItem->GetValue());
1517         EnableItem( SID_STYLE_WATERCAN, sal_True );
1518     }
1519     else
1520         if(!bWaterDisabled)
1521             EnableItem(SID_STYLE_WATERCAN, sal_True);
1522         else
1523             EnableItem(SID_STYLE_WATERCAN, sal_False);
1524 
1525 //Waehrend Giesskannenmodus Statusupdates ignorieren.
1526 
1527     sal_uInt16 nCount=pStyleFamilies->Count();
1528     pBindings->EnterRegistrations();
1529     for(sal_uInt16 n=0; n<nCount; n++)
1530     {
1531         SfxControllerItem *pCItem=pBoundItems[n];
1532         sal_Bool bChecked = pItem && pItem->GetValue();
1533         if( pCItem->IsBound() == bChecked )
1534         {
1535             if( !bChecked )
1536                 pCItem->ReBind();
1537             else
1538                 pCItem->UnBind();
1539         }
1540     }
1541     pBindings->LeaveRegistrations();
1542 }
1543 
1544 //-------------------------------------------------------------------------
1545 
1546 // Item mit dem Status einer Family wird kopiert und gemerkt
1547 // (Aktualisierung erfolgt, wenn alle Stati aktualisiert worden sind.
1548 // Siehe auch: <SfxBindings::AddDoneHdl(const Link &)>
1549 
1550 void SfxCommonTemplateDialog_Impl::SetFamilyState( sal_uInt16 nSlotId, const SfxTemplateItem* pItem )
1551 {
1552     sal_uInt16 nIdx = nSlotId - SID_STYLE_FAMILY_START;
1553     DELETEZ(pFamilyState[nIdx]);
1554     if ( pItem )
1555         pFamilyState[nIdx] = new SfxTemplateItem(*pItem);
1556     bUpdate = sal_True;
1557 
1558     // Wenn verwendete Vorlagen ( wie zum Teufel findet man das heraus ?? )
1559     bUpdateFamily = sal_True;
1560 }
1561 
1562 //-------------------------------------------------------------------------
1563 
1564 // Benachrichtigung durch SfxBindings, da"s die Aktualisierung
1565 // beendet ist. St"o\st die Aktualisierung der Anzeige an.
1566 
1567 void SfxCommonTemplateDialog_Impl::Update_Impl()
1568 {
1569     sal_Bool bDocChanged=sal_False;
1570     SfxStyleSheetBasePool* pNewPool = NULL;
1571     SfxViewFrame* pViewFrame = pBindings->GetDispatcher_Impl()->GetFrame();
1572     SfxObjectShell* pDocShell = pViewFrame->GetObjectShell();
1573     if( pDocShell )
1574         pNewPool = pDocShell->GetStyleSheetPool();
1575 
1576     if ( pNewPool != pStyleSheetPool && pDocShell )
1577     {
1578         SfxModule* pNewModule = pDocShell->GetModule();
1579         if( pNewModule && pNewModule != pModule )
1580         {
1581             ClearResource();
1582             ReadResource();
1583         }
1584         if ( pStyleSheetPool )
1585         {
1586             EndListening(*pStyleSheetPool);
1587             pStyleSheetPool = 0;
1588         }
1589 
1590         if ( pNewPool )
1591         {
1592             StartListening(*pNewPool);
1593             pStyleSheetPool = pNewPool;
1594             bDocChanged=sal_True;
1595         }
1596 //      InvalidateBindings();
1597     }
1598 
1599     if (bUpdateFamily)
1600         UpdateFamily_Impl();
1601 
1602     sal_uInt16 i;
1603     for(i = 0; i < MAX_FAMILIES; ++i)
1604         if(pFamilyState[i])
1605             break;
1606     if(i == MAX_FAMILIES || !pNewPool)
1607         // nichts erlaubt
1608         return;
1609 
1610      SfxTemplateItem *pItem = 0;
1611      // aktueller Bereich nicht innerhalb der erlaubten Bereiche
1612      // oder Default
1613      if(nActFamily == SFX_TEMPLDLG_FILTER_MAX || 0 == (pItem = pFamilyState[nActFamily-1] ) )
1614      {
1615          CheckItem(nActFamily, sal_False);
1616          SfxTemplateItem **ppItem = pFamilyState;
1617          const sal_uInt16 nFamilyCount = pStyleFamilies->Count();
1618          sal_uInt16 n;
1619          for(n=0;n<nFamilyCount;n++)
1620              if(ppItem[StyleNrToInfoOffset(n)])break;
1621          ppItem+=StyleNrToInfoOffset(n);
1622 
1623          nAppFilter = (*ppItem)->GetValue();
1624          FamilySelect(  StyleNrToInfoOffset(n)+1 );
1625 
1626          pItem = *ppItem;
1627      }
1628      else if( bDocChanged )
1629      {
1630          // andere DocShell -> alles neu
1631          CheckItem( nActFamily, sal_True );
1632          nActFilter = static_cast< sal_uInt16 >( LoadFactoryStyleFilter( pDocShell ) );
1633          if ( SFX_TEMPLDLG_FILTER_MAX == nActFilter )
1634             nActFilter = pDocShell->GetAutoStyleFilterIndex();
1635 
1636          nAppFilter = pItem->GetValue();
1637          if(!pTreeBox)
1638          {
1639              UpdateStyles_Impl(UPDATE_FAMILY_LIST);
1640          }
1641          else
1642              FillTreeBox();
1643      }
1644      else
1645      {
1646          // anderer Filter fuer automatisch
1647          CheckItem( nActFamily, sal_True );
1648          const SfxStyleFamilyItem *pStyleItem =  GetFamilyItem_Impl();
1649          if(0 == pStyleItem->GetFilterList().GetObject(nActFilter)->nFlags
1650             && nAppFilter != pItem->GetValue())
1651          {
1652              nAppFilter = pItem->GetValue();
1653              if(!pTreeBox)
1654                  UpdateStyles_Impl(UPDATE_FAMILY_LIST);
1655              else
1656                  FillTreeBox();
1657          }
1658          else
1659              nAppFilter = pItem->GetValue();
1660      }
1661      const String aStyle(pItem->GetStyleName());
1662      SelectStyle(aStyle);
1663      EnableDelete();
1664      EnableNew( bCanNew );
1665 }
1666 
1667 //-------------------------------------------------------------------------
1668 
1669 IMPL_LINK( SfxCommonTemplateDialog_Impl, TimeOut, Timer *, pTim )
1670 {
1671     (void)pTim; // unused
1672     if(!bDontUpdate)
1673     {
1674         bDontUpdate=sal_True;
1675         if(!pTreeBox)
1676             UpdateStyles_Impl(UPDATE_FAMILY_LIST);
1677         else
1678         {
1679             FillTreeBox();
1680             SfxTemplateItem *pState = pFamilyState[nActFamily-1];
1681             if(pState)
1682             {
1683                 const String aStyle(pState->GetStyleName());
1684                 SelectStyle(aStyle);
1685                 EnableDelete();
1686             }
1687         }
1688         bDontUpdate=sal_False;
1689         DELETEZ(pTimer);
1690     }
1691     else
1692         pTimer->Start();
1693     return 0;
1694 }
1695 
1696 
1697 //-------------------------------------------------------------------------
1698 void SfxCommonTemplateDialog_Impl::Notify(SfxBroadcaster& /*rBC*/, const SfxHint& rHint)
1699 {
1700     // Aktualisierung anstossen
1701     if(rHint.Type() == TYPE(SfxSimpleHint))
1702     {
1703         switch(((SfxSimpleHint&) rHint ).GetId())
1704         {
1705           case SFX_HINT_UPDATEDONE:
1706             {
1707                 SfxViewFrame *pViewFrame = pBindings->GetDispatcher_Impl()->GetFrame();
1708                 SfxObjectShell *pDocShell = pViewFrame->GetObjectShell();
1709                 if (
1710                     bUpdate &&
1711                     (
1712                      !IsCheckedItem(SID_STYLE_WATERCAN) ||
1713                      (pDocShell && pDocShell->GetStyleSheetPool() != pStyleSheetPool)
1714                     )
1715                    )
1716                 {
1717                     bUpdate = sal_False;
1718                     Update_Impl();
1719                 }
1720                 else if ( bUpdateFamily )
1721                 {
1722                     UpdateFamily_Impl();
1723                 }
1724 
1725                 if( pStyleSheetPool )
1726                 {
1727                     String aStr = GetSelectedEntry();
1728                     if( aStr.Len() && pStyleSheetPool )
1729                     {
1730                         const SfxStyleFamilyItem *pItem = GetFamilyItem_Impl();
1731                         if( !pItem ) break;
1732                         const SfxStyleFamily eFam = pItem->GetFamily();
1733                         SfxStyleSheetBase *pStyle =
1734                             pStyleSheetPool->Find(
1735                                 aStr, eFam, SFXSTYLEBIT_ALL );
1736                         if( pStyle )
1737                             EnableEdit(
1738                                 !(pStyle->GetMask() & SFXSTYLEBIT_READONLY) );
1739                         else
1740                             EnableEdit(sal_False);
1741                     }
1742                 }
1743                 break;
1744             }
1745         // noetig, wenn zwichen Dokumenten umgeschaltet wird,
1746         // aber in beiden Dokumenten die gleiche Vorlage gilt.
1747         // Nicht sofort Update_Impl rufen, fuer den Fall da\s eines
1748         // der Dokumente ein internes InPlaceObjekt ist!
1749           case SFX_HINT_DOCCHANGED:
1750             bUpdate = sal_True;
1751             break;
1752           case SFX_HINT_DYING:
1753           {
1754             EndListening(*pStyleSheetPool);
1755             pStyleSheetPool=0;
1756             break;
1757           }
1758         }
1759     }
1760 
1761     // Timer nicht aufsetzen, wenn der StyleSheetPool in die Kiste geht, denn
1762     // es kann sein, da\s sich ein neuer erst anmeldet, nachdem der Timer
1763     // abgelaufen ist - macht sich schlecht in UpdateStyles_Impl() !
1764 
1765     sal_uIntPtr nId = rHint.ISA(SfxSimpleHint) ? ( (SfxSimpleHint&)rHint ).GetId() : 0;
1766 
1767     if(!bDontUpdate && nId != SFX_HINT_DYING &&
1768        (rHint.Type() == TYPE(SfxStyleSheetPoolHint)||
1769        rHint.Type() == TYPE(SfxStyleSheetHint) ||
1770        rHint.Type() == TYPE( SfxStyleSheetHintExtended )))
1771     {
1772         if(!pTimer)
1773         {
1774             pTimer=new Timer;
1775             pTimer->SetTimeout(500);
1776             pTimer->SetTimeoutHdl(LINK(this,SfxCommonTemplateDialog_Impl,TimeOut));
1777         }
1778         pTimer->Start();
1779 
1780     }
1781 }
1782 
1783 
1784 //-------------------------------------------------------------------------
1785 
1786 // Anderer Filter; kann durch den Benutzer umgeschaltet werden
1787 // oder als Folge von Neu oder Bearbeiten, wenn die aktuelle
1788 // Vorlage einem anderen Filter zugewiesen wurde.
1789 void SfxCommonTemplateDialog_Impl::FilterSelect(
1790                 sal_uInt16 nEntry,                  // Idx des neuen Filters
1791                 sal_Bool bForce )                   // Aktualisierung erzwingen, auch wenn der neue Filter gleich dem aktuellen ist
1792 {
1793     if( nEntry != nActFilter || bForce )
1794     {
1795         nActFilter = nEntry;
1796         SfxViewFrame *pViewFrame = pBindings->GetDispatcher_Impl()->GetFrame();
1797         SfxObjectShell *pDocShell = pViewFrame->GetObjectShell();
1798         if (pDocShell)
1799         {
1800             pDocShell->SetAutoStyleFilterIndex(nActFilter);
1801             SaveFactoryStyleFilter( pDocShell, nActFilter );
1802         }
1803 
1804         SfxStyleSheetBasePool *pOldStyleSheetPool = pStyleSheetPool;
1805         pStyleSheetPool = pDocShell? pDocShell->GetStyleSheetPool(): 0;
1806         if ( pOldStyleSheetPool != pStyleSheetPool )
1807         {
1808             if ( pOldStyleSheetPool )
1809                 EndListening(*pOldStyleSheetPool);
1810             if ( pStyleSheetPool )
1811                 StartListening(*pOldStyleSheetPool);
1812         }
1813 
1814         UpdateStyles_Impl(UPDATE_FAMILY_LIST);
1815     }
1816 }
1817 
1818 //-------------------------------------------------------------------------
1819 
1820 // Intern: Ausf"uhren von Funktionen "uber den Dispatcher
1821 sal_Bool SfxCommonTemplateDialog_Impl::Execute_Impl(
1822     sal_uInt16 nId, const String &rStr, const String& rRefStr, sal_uInt16 nFamily,
1823     sal_uInt16 nMask, sal_uInt16 *pIdx, const sal_uInt16* pModifier)
1824 {
1825     SfxDispatcher &rDispatcher = *SFX_APP()->GetDispatcher_Impl();
1826     SfxStringItem aItem(nId, rStr);
1827     SfxUInt16Item aFamily(SID_STYLE_FAMILY, nFamily);
1828     SfxUInt16Item aMask( SID_STYLE_MASK, nMask );
1829     SfxStringItem aUpdName(SID_STYLE_UPD_BY_EX_NAME, rStr);
1830     SfxStringItem aRefName( SID_STYLE_REFERENCE, rRefStr );
1831     const SfxPoolItem* pItems[ 6 ];
1832     sal_uInt16 nCount = 0;
1833     if( rStr.Len() )
1834         pItems[ nCount++ ] = &aItem;
1835     pItems[ nCount++ ] = &aFamily;
1836     if( nMask )
1837         pItems[ nCount++ ] = &aMask;
1838     if(SID_STYLE_UPDATE_BY_EXAMPLE == nId)
1839     {
1840         //Sonderloesung fuer Numerierungsupdate im Writer
1841         const String aTemplName(GetSelectedEntry());
1842         aUpdName.SetValue(aTemplName);
1843         pItems[ nCount++ ] = &aUpdName;
1844     }
1845     if ( rRefStr.Len() )
1846         pItems[ nCount++ ] = &aRefName;
1847 
1848     pItems[ nCount++ ] = 0;
1849 
1850     Deleted aDeleted;
1851     pbDeleted = &aDeleted;
1852     sal_uInt16 nModi = pModifier ? *pModifier : 0;
1853     const SfxPoolItem* pItem = rDispatcher.Execute(
1854         nId, SFX_CALLMODE_SYNCHRON | SFX_CALLMODE_RECORD | SFX_CALLMODE_MODAL,
1855         pItems, nModi );
1856 
1857     // FIXME: Dialog can be destroyed while in Execute() check stack variable for dtor flag!
1858     if ( !pItem || aDeleted() )
1859         return sal_False;
1860 
1861     if ( nId == SID_STYLE_NEW || SID_STYLE_EDIT == nId )
1862     {
1863         SfxUInt16Item *pFilterItem = PTR_CAST(SfxUInt16Item, pItem);
1864         DBG_ASSERT(pFilterItem, "SfxUINT16Item erwartet");
1865         sal_uInt16 nFilterFlags = pFilterItem->GetValue() & ~SFXSTYLEBIT_USERDEF;
1866         if(!nFilterFlags)       // Benutzervorlage?
1867             nFilterFlags = pFilterItem->GetValue();
1868         const SfxStyleFamilyItem *pFamilyItem = GetFamilyItem_Impl();
1869         const sal_uInt16 nFilterCount = (sal_uInt16) pFamilyItem->GetFilterList().Count();
1870 
1871         for ( sal_uInt16 i = 0; i < nFilterCount; ++i )
1872         {
1873             const SfxFilterTupel *pTupel =
1874                 pFamilyItem->GetFilterList().GetObject(i);
1875 
1876             if ( ( pTupel->nFlags & nFilterFlags ) == nFilterFlags && pIdx )
1877                 *pIdx = i;
1878         }
1879     }
1880 
1881     // Reset destroyed flag otherwise we use the pointer in the dtor
1882     // where the local stack object is already destroyed. This would
1883     // overwrite objects on the stack!! See #i100110
1884     pbDeleted = NULL;
1885     return sal_True;
1886 }
1887 
1888 //-------------------------------------------------------------------------
1889 
1890 // Handler der Listbox der Filter
1891 IMPL_LINK( SfxCommonTemplateDialog_Impl, FilterSelectHdl, ListBox *, pBox )
1892 {
1893     if ( pBox->GetSelectEntry() ==  String(SfxResId(STR_STYLE_FILTER_HIERARCHICAL)) )
1894     {
1895         if ( !bHierarchical )
1896         {
1897             // TreeView einschalten
1898             bHierarchical=sal_True;
1899             const String aSelectEntry( GetSelectedEntry());
1900             aFmtLb.Hide();
1901             // aFilterLb.Disable();
1902 
1903             pTreeBox = new StyleTreeListBox_Impl(
1904                     this, WB_HASBUTTONS | WB_HASLINES |
1905                     WB_BORDER | WB_TABSTOP | WB_HASLINESATROOT |
1906                     WB_HASBUTTONSATROOT | WB_HIDESELECTION | WB_QUICK_SEARCH );
1907             pTreeBox->SetFont( aFmtLb.GetFont() );
1908 
1909             pTreeBox->SetPosSizePixel(aFmtLb.GetPosPixel(), aFmtLb.GetSizePixel());
1910             pTreeBox->SetNodeDefaultImages();
1911             pTreeBox->SetSelectHdl(
1912                 LINK(this, SfxCommonTemplateDialog_Impl, FmtSelectHdl));
1913             ((StyleTreeListBox_Impl*)pTreeBox)->
1914                 SetDoubleClickHdl(
1915                     LINK(this, SfxCommonTemplateDialog_Impl,  ApplyHdl));
1916             ((StyleTreeListBox_Impl*)pTreeBox)->
1917                 SetDropHdl(LINK(this, SfxCommonTemplateDialog_Impl,  DropHdl));
1918             pTreeBox->SetIndent(10);
1919 
1920             FillTreeBox();
1921             SelectStyle(aSelectEntry);
1922             pTreeBox->SetAccessibleName(SfxResId(STR_STYLE_ELEMTLIST));
1923             pTreeBox->Show();
1924 
1925             // Save the filter state
1926             SfxViewFrame *pViewFrame = pBindings->GetDispatcher_Impl()->GetFrame();
1927             SfxObjectShell *pDocShell = pViewFrame->GetObjectShell();
1928             if (pDocShell)
1929             {
1930                 // only in the configuration
1931                 // SetAutoStyleFilterIndex would update nActFilter
1932                 // which should only contain a valid listbox entry
1933                 SaveFactoryStyleFilter( pDocShell, SFX_TEMPLDLG_FILTER_HIERARCHICAL );
1934             }
1935         }
1936     }
1937 
1938     else
1939     {
1940         DELETEZ(pTreeBox);
1941         aFmtLb.Show();
1942         //                              aFilterLb.Enable();
1943         // Falls bHierarchical, kann sich die Familie geaendert haben
1944         // minus one since hierarchical is inserted at the start
1945         FilterSelect(pBox->GetSelectEntryPos() - 1, bHierarchical );
1946         bHierarchical=sal_False;
1947 //      UpdateStyles_Impl(UPDATE_FAMILY_LIST);  // Anzeige aktualisieren
1948     }
1949 
1950     return 0;
1951 }
1952 
1953 //-------------------------------------------------------------------------
1954 
1955 // Select-Handler der Toolbox
1956 void SfxCommonTemplateDialog_Impl::FamilySelect(sal_uInt16 nEntry)
1957 {
1958     if( nEntry != nActFamily )
1959     {
1960         CheckItem( nActFamily, sal_False );
1961         nActFamily = nEntry;
1962         SfxDispatcher* pDispat = pBindings->GetDispatcher_Impl();
1963         SfxUInt16Item aItem( SID_STYLE_FAMILY, nEntry );
1964         pDispat->Execute( SID_STYLE_FAMILY, SFX_CALLMODE_SYNCHRON, &aItem, 0L );
1965         pBindings->Invalidate( SID_STYLE_FAMILY );
1966         pBindings->Update( SID_STYLE_FAMILY );
1967         UpdateFamily_Impl();
1968     }
1969 }
1970 
1971 //-------------------------------------------------------------------------
1972 
1973 void SfxCommonTemplateDialog_Impl::ActionSelect(sal_uInt16 nEntry)
1974 {
1975     String aEmpty;
1976     switch(nEntry)
1977     {
1978         case SID_STYLE_WATERCAN:
1979         {
1980             const sal_Bool bState = IsCheckedItem(nEntry);
1981             sal_Bool bCheck;
1982             SfxBoolItem aBool;
1983             // wenn eine Vorlage ausgewaehlt ist.
1984             if(!bState && aFmtLb.GetSelectionCount())
1985             {
1986                 const String aTemplName(
1987                     GetSelectedEntry());
1988                 Execute_Impl(
1989                     SID_STYLE_WATERCAN, aTemplName, aEmpty,
1990                     (sal_uInt16)GetFamilyItem_Impl()->GetFamily() );
1991                 bCheck = sal_True;
1992             }
1993             else
1994             {
1995                 Execute_Impl(SID_STYLE_WATERCAN, aEmpty, aEmpty, 0);
1996                 bCheck = sal_False;
1997             }
1998             CheckItem(nEntry, bCheck);
1999             aBool.SetValue(bCheck);
2000             SetWaterCanState(&aBool);
2001             break;
2002         }
2003         case SID_STYLE_NEW_BY_EXAMPLE:
2004         {
2005             if(pStyleSheetPool && nActFamily != SFX_TEMPLDLG_FILTER_MAX)
2006             {
2007                 const SfxStyleFamily eFam=GetFamilyItem_Impl()->GetFamily();
2008 //pStyleSheetPool->GetSearchFamily();
2009                 const SfxStyleFamilyItem *pItem = GetFamilyItem_Impl();
2010                 sal_uInt16 nFilter;
2011                 if(pItem&&nActFilter!=SFX_TEMPLDLG_FILTER_MAX)
2012                 {
2013                     nFilter = pItem->GetFilterList().GetObject(
2014                         nActFilter)->nFlags;
2015                     if(!nFilter)    // automatisch
2016                         nFilter = nAppFilter;
2017                 }
2018                 else
2019                     nFilter=pStyleSheetPool->GetSearchMask();
2020                 pStyleSheetPool->SetSearchMask( eFam, SFXSTYLEBIT_USERDEF );
2021 
2022                 SfxNewStyleDlg *pDlg =
2023                     // why? : FloatingWindow must not be parent of a modal dialog
2024                     new SfxNewStyleDlg(pWindow, *pStyleSheetPool);
2025                 if(RET_OK == pDlg->Execute())
2026                 {
2027                     pStyleSheetPool->SetSearchMask(eFam, nFilter);
2028                     const String aTemplName(pDlg->GetName());
2029                     Execute_Impl(SID_STYLE_NEW_BY_EXAMPLE,
2030                                  aTemplName, aEmpty,
2031                                  (sal_uInt16)GetFamilyItem_Impl()->GetFamily(),
2032                                  nFilter);
2033                 }
2034                 pStyleSheetPool->SetSearchMask( eFam, nFilter );
2035                 delete pDlg;
2036             }
2037             break;
2038         }
2039         case SID_STYLE_UPDATE_BY_EXAMPLE:
2040         {
2041             Execute_Impl(SID_STYLE_UPDATE_BY_EXAMPLE,
2042                     aEmpty, aEmpty,
2043                     (sal_uInt16)GetFamilyItem_Impl()->GetFamily());
2044             break;
2045         }
2046         case SID_TEMPLATE_LOAD:
2047             SFX_APP()->GetDispatcher_Impl()->Execute(nEntry);
2048         break;
2049         default: DBG_ERROR("not implemented"); break;
2050     }
2051 }
2052 
2053 //-------------------------------------------------------------------------
2054 
2055 static rtl::OUString getModuleIdentifier( const Reference< XModuleManager >& i_xModMgr, SfxObjectShell* i_pObjSh )
2056 {
2057     DBG_ASSERT( i_xModMgr.is(), "getModuleIdentifier(): no XModuleManager" );
2058     DBG_ASSERT( i_pObjSh, "getModuleIdentifier(): no ObjectShell" );
2059 
2060     ::rtl::OUString sIdentifier;
2061 
2062     try
2063     {
2064         sIdentifier = i_xModMgr->identify( i_pObjSh->GetModel() );
2065     }
2066     catch ( ::com::sun::star::frame::UnknownModuleException& )
2067     {
2068         DBG_WARNING( "getModuleIdentifier(): unknown module" );
2069     }
2070     catch ( Exception& )
2071     {
2072         DBG_ERRORFILE( "getModuleIdentifier(): exception of XModuleManager::identify()" );
2073     }
2074 
2075     return sIdentifier;
2076 }
2077 
2078 //-------------------------------------------------------------------------
2079 
2080 sal_Int32 SfxCommonTemplateDialog_Impl::LoadFactoryStyleFilter( SfxObjectShell* i_pObjSh )
2081 {
2082     DBG_ASSERT( i_pObjSh, "SfxCommonTemplateDialog_Impl::LoadFactoryStyleFilter(): no ObjectShell" );
2083     sal_Int32 nFilter = -1;
2084 
2085     Sequence< PropertyValue > lProps;
2086     Reference< ::com::sun::star::container::XNameAccess > xContainer( xModuleManager, UNO_QUERY );
2087     if ( xContainer.is() )
2088     {
2089         ::comphelper::SequenceAsHashMap aFactoryProps(
2090             xContainer->getByName( getModuleIdentifier( xModuleManager, i_pObjSh ) ) );
2091         sal_Int32 nDefault = -1;
2092         nFilter = aFactoryProps.getUnpackedValueOrDefault( DEFINE_CONST_UNICODE("ooSetupFactoryStyleFilter"), nDefault );
2093     }
2094 
2095     return nFilter;
2096 }
2097 
2098 //-------------------------------------------------------------------------
2099 
2100 void SfxCommonTemplateDialog_Impl::SaveFactoryStyleFilter( SfxObjectShell* i_pObjSh, sal_Int32 i_nFilter )
2101 {
2102     DBG_ASSERT( i_pObjSh, "SfxCommonTemplateDialog_Impl::SaveFactoryStyleFilter(): no ObjectShell" );
2103     Reference< ::com::sun::star::container::XNameReplace > xContainer( xModuleManager, UNO_QUERY );
2104     if ( xContainer.is() )
2105     {
2106         Sequence< PropertyValue > lProps(1);
2107         lProps[0].Name = DEFINE_CONST_UNICODE("ooSetupFactoryStyleFilter");
2108         lProps[0].Value = makeAny( i_nFilter );;
2109         xContainer->replaceByName( getModuleIdentifier( xModuleManager, i_pObjSh ), makeAny( lProps ) );
2110     }
2111 }
2112 
2113 //-------------------------------------------------------------------------
2114 
2115 IMPL_LINK( SfxCommonTemplateDialog_Impl, DropHdl, StyleTreeListBox_Impl *, pBox )
2116 {
2117     bDontUpdate=sal_True;
2118     const SfxStyleFamilyItem *pItem = GetFamilyItem_Impl();
2119     const SfxStyleFamily eFam = pItem->GetFamily();
2120     long ret= pStyleSheetPool->SetParent(eFam,pBox->GetStyle(), pBox->GetParent())? 1L: 0L;
2121     bDontUpdate=sal_False;
2122     return ret;
2123 }
2124 
2125 //-------------------------------------------------------------------------
2126 
2127 // Handler des Neu-Buttons
2128 void SfxCommonTemplateDialog_Impl::NewHdl(void *)
2129 {
2130     String aEmpty;
2131     if ( nActFamily != SFX_TEMPLDLG_FILTER_MAX )
2132     {
2133         Window* pTmp;
2134         pTmp = Application::GetDefDialogParent();
2135         if ( ISA(SfxTemplateDialog_Impl) )
2136             Application::SetDefDialogParent( pWindow->GetParent() );
2137         else
2138             Application::SetDefDialogParent( pWindow );
2139 
2140         const SfxStyleFamilyItem *pItem = GetFamilyItem_Impl();
2141         const SfxStyleFamily eFam=pItem->GetFamily();
2142         sal_uInt16 nMask;
2143         if(pItem&&nActFilter!=SFX_TEMPLDLG_FILTER_MAX)
2144         {
2145             nMask = pItem->GetFilterList().GetObject(
2146                 nActFilter)->nFlags;
2147             if(!nMask)    // automatisch
2148                 nMask = nAppFilter;
2149         }
2150         else
2151             nMask=pStyleSheetPool->GetSearchMask();
2152 
2153         pStyleSheetPool->SetSearchMask(eFam,nMask);
2154 
2155         Execute_Impl(SID_STYLE_NEW,
2156                      aEmpty, GetSelectedEntry(),
2157                      ( sal_uInt16 )GetFamilyItem_Impl()->GetFamily(),
2158                      nMask);
2159 
2160         Application::SetDefDialogParent( pTmp );
2161 
2162 /*              {
2163             DBG_ASSERT(nFilter < aFilterLb.GetEntryCount(),
2164                        "Filter ueberindiziert");
2165 
2166             if(!pTreeBox)
2167             {
2168 //                              aFilterLb.SelectEntryPos(nFilter);
2169                 FilterSelect(nActFilter, sal_True);
2170             }
2171             else
2172             {
2173                 FillTreeBox();
2174                 SfxTemplateItem *pState = pFamilyState[nActFamily-1];
2175                 if(pState)
2176                 {
2177                     const String aStyle(pState->GetStyleName());
2178                     SelectStyle(aStyle);
2179                 }
2180                 EnableDelete();
2181             }*/
2182 //              }
2183     }
2184 }
2185 
2186 //-------------------------------------------------------------------------
2187 
2188 // Handler des Bearbeiten-Buttons
2189 void SfxCommonTemplateDialog_Impl::EditHdl(void *)
2190 {
2191     if(IsInitialized() && HasSelectedStyle())
2192     {
2193         sal_uInt16 nFilter = nActFilter;
2194         String aTemplName(GetSelectedEntry());
2195         const SfxStyleFamilyItem *pItem = GetFamilyItem_Impl();
2196         const SfxStyleFamily eFam = pItem->GetFamily();
2197         pStyleSheetPool->Find(aTemplName,eFam,SFXSTYLEBIT_ALL);  // -Wall required??
2198         Window* pTmp;
2199         //DefModalDialogParent setzen fuer
2200         //Modalitaet der nachfolgenden Dialoge
2201         pTmp = Application::GetDefDialogParent();
2202         if ( ISA(SfxTemplateDialog_Impl) )
2203             Application::SetDefDialogParent( pWindow->GetParent() );
2204         else
2205             Application::SetDefDialogParent( pWindow );
2206         if ( Execute_Impl( SID_STYLE_EDIT, aTemplName, String(),
2207                           (sal_uInt16)GetFamilyItem_Impl()->GetFamily(), 0, &nFilter ) )
2208         {
2209 //          DBG_ASSERT(nFilter < aFilterLb.GetEntryCount(), "Filter ueberindiziert");
2210 //          aTemplName = pStyle->GetName();
2211 // kann durch Bearbeiten umbenannt worden sein
2212 /*                      if(!pTreeBox)
2213             {
2214                 //                              aFilterLb.SelectEntryPos(nFilter);
2215                 //                              FilterSelect(nFilter, sal_True);
2216             }
2217             else
2218                 FillTreeBox();*/
2219         }
2220         Application::SetDefDialogParent( pTmp );
2221     }
2222 }
2223 
2224 //-------------------------------------------------------------------------
2225 
2226 // Handler des L"oschen-Buttons
2227 void SfxCommonTemplateDialog_Impl::DeleteHdl(void *)
2228 {
2229     if ( IsInitialized() && HasSelectedStyle() )
2230     {
2231         const String aTemplName( GetSelectedEntry() );
2232         const SfxStyleFamilyItem* pItem = GetFamilyItem_Impl();
2233         SfxStyleSheetBase* pStyle =
2234             pStyleSheetPool->Find( aTemplName, pItem->GetFamily(), SFXSTYLEBIT_ALL );
2235         if ( pStyle )
2236         {
2237             String aMsg;
2238             if ( pStyle->IsUsed() )
2239                 aMsg = String( SfxResId( STR_DELETE_STYLE_USED ) );
2240             aMsg += String ( SfxResId( STR_DELETE_STYLE ) );
2241             aMsg.SearchAndReplaceAscii( "$1", aTemplName );
2242 #if defined UNX
2243             QueryBox aBox( SFX_APP()->GetTopWindow(), WB_YES_NO | WB_DEF_NO, aMsg );
2244 #else
2245             QueryBox aBox( GetWindow(), WB_YES_NO | WB_DEF_NO , aMsg );
2246 #endif
2247             if ( RET_YES == aBox.Execute() )
2248             {
2249                 PrepareDeleteAction();
2250 
2251                 if ( pTreeBox ) // Damit die Treelistbox beim L"oschen nicht zuklappt
2252                 {
2253                     bDontUpdate = sal_True;
2254                 }
2255                 Execute_Impl( SID_STYLE_DELETE, aTemplName,
2256                             String(), (sal_uInt16)GetFamilyItem_Impl()->GetFamily() );
2257 
2258                 if ( pTreeBox )
2259                 {
2260                     pTreeBox->RemoveParentKeepChilds( pTreeBox->FirstSelected() );
2261                     bDontUpdate = sal_False;
2262                 }
2263             }
2264         }
2265     }
2266 }
2267 
2268 //-------------------------------------------------------------------------
2269 
2270 void    SfxCommonTemplateDialog_Impl::EnableDelete()
2271 {
2272     if(IsInitialized() && HasSelectedStyle())
2273     {
2274         DBG_ASSERT(pStyleSheetPool, "Kein StyleSheetPool");
2275         const String aTemplName(GetSelectedEntry());
2276         const SfxStyleFamilyItem *pItem = GetFamilyItem_Impl();
2277         const SfxStyleFamily eFam = pItem->GetFamily();
2278         sal_uInt16 nFilter = 0;
2279         if(pItem->GetFilterList().Count() > nActFilter)
2280             nFilter = pItem->GetFilterList().GetObject(nActFilter)->nFlags;
2281         if(!nFilter)    // automatisch
2282             nFilter = nAppFilter;
2283         const SfxStyleSheetBase *pStyle =
2284             pStyleSheetPool->Find(aTemplName,eFam,
2285                                   pTreeBox? SFXSTYLEBIT_ALL: nFilter);
2286         DBG_ASSERT(pStyle, "Style nicht gefunden");
2287         if(pStyle && pStyle->IsUserDefined())
2288         {
2289             EnableDel(sal_True);
2290         }
2291         else
2292         {
2293             EnableDel(sal_False);
2294         }
2295     }
2296     else
2297     {
2298         EnableDel(sal_False);
2299     }
2300 //  rBindings.Invalidate( SID_STYLE_DELETE );
2301 //  rBindings.Update( SID_STYLE_DELETE );
2302 }
2303 
2304 //-------------------------------------------------------------------------
2305 
2306 // nach Selektion eines Eintrags den Focus gfs. wieder auf das App-Fenster
2307 // setzen
2308 void    SfxCommonTemplateDialog_Impl::ResetFocus()
2309 {
2310     if(ISA(SfxTemplateDialog_Impl))
2311     {
2312         SfxViewFrame *pViewFrame = pBindings->GetDispatcher_Impl()->GetFrame();
2313         SfxViewShell *pVu = pViewFrame->GetViewShell();
2314         Window *pAppWin = pVu ? pVu->GetWindow(): 0;
2315         if(pAppWin)
2316             pAppWin->GrabFocus();
2317     }
2318 }
2319 
2320 //-------------------------------------------------------------------------
2321 
2322 // Doppelclick auf ein StyleSheet in der ListBox, wird angewendet.
2323 IMPL_LINK( SfxCommonTemplateDialog_Impl, ApplyHdl, Control *, pControl )
2324 {
2325     (void)pControl; //unused
2326     // nur, wenn dieser Bereich erlaubt ist
2327     if ( IsInitialized() && 0 != pFamilyState[nActFamily-1] &&
2328          GetSelectedEntry().Len() )
2329     {
2330         sal_uInt16 nModifier = aFmtLb.GetModifier();
2331         Execute_Impl(SID_STYLE_APPLY,
2332                      GetSelectedEntry(), String(),
2333                      ( sal_uInt16 )GetFamilyItem_Impl()->GetFamily(),
2334                      0, 0, &nModifier );
2335         if(ISA(SfxTemplateCatalog_Impl))
2336             ((SfxTemplateCatalog_Impl*) this)->pReal->EndDialog(RET_OK);
2337     }
2338     ResetFocus();
2339     return 0;
2340 }
2341 
2342 //-------------------------------------------------------------------------
2343 
2344 // Selektion einer Vorlage w"ahrend des Watercan-Status
2345 IMPL_LINK( SfxCommonTemplateDialog_Impl, FmtSelectHdl, SvTreeListBox *, pListBox )
2346 {
2347     // HilfePI antriggern, wenn von Call als Handler und Bereich erlaubt ist
2348     if( !pListBox || pListBox->IsSelected( pListBox->GetHdlEntry() ) )
2349     {
2350         // nur, wenn Giesskanne an ist
2351         if ( IsInitialized() &&
2352              IsCheckedItem(SID_STYLE_WATERCAN) &&
2353              // nur, wenn dieser Bereich erlaubt ist
2354              0 != pFamilyState[nActFamily-1] )
2355         {
2356             String aEmpty;
2357             Execute_Impl(SID_STYLE_WATERCAN,
2358                          aEmpty, aEmpty, 0);
2359             Execute_Impl(SID_STYLE_WATERCAN,
2360                          GetSelectedEntry(), aEmpty,
2361                          ( sal_uInt16 )GetFamilyItem_Impl()->GetFamily());
2362         }
2363 //      EnableEdit(sal_True);
2364         EnableItem(SID_STYLE_WATERCAN, !bWaterDisabled);
2365         EnableDelete();
2366     }
2367     if( pListBox )
2368         SelectStyle( pListBox->GetEntryText( pListBox->GetHdlEntry() ));
2369 
2370     return 0;
2371 }
2372 
2373 //-------------------------------------------------------------------------
2374 
2375 IMPL_LINK( SfxCommonTemplateDialog_Impl, MenuSelectHdl, Menu *, pMenu )
2376 {
2377     if( pMenu )
2378     {
2379         nLastItemId = pMenu->GetCurItemId();
2380         Application::PostUserEvent(
2381             LINK( this, SfxCommonTemplateDialog_Impl, MenuSelectHdl ), 0 );
2382         return sal_True;
2383     }
2384 
2385     switch(nLastItemId) {
2386     case ID_NEW: NewHdl(0); break;
2387     case ID_EDIT: EditHdl(0); break;
2388     case ID_DELETE: DeleteHdl(0); break;
2389     default: return sal_False;
2390     }
2391     return sal_True;
2392 }
2393 
2394 // -----------------------------------------------------------------------
2395 
2396 void SfxCommonTemplateDialog_Impl::ExecuteContextMenu_Impl( const Point& rPos, Window* pWin )
2397 {
2398     // Bug# 94152: This part should never be called, because before this happens, the TreeListBox should captured this!
2399     DBG_ASSERT( sal_False, "+SfxCommonTemplateDialog_Impl::ExecuteContextMenu_Impl(): How could this happen? Please infirm developer ASAP!" );
2400 
2401     PopupMenu* pMenu = CreateContextMenu();
2402     pMenu->Execute( pWin, rPos );
2403     delete pMenu;
2404 }
2405 
2406 // -----------------------------------------------------------------------
2407 
2408 SfxStyleFamily SfxCommonTemplateDialog_Impl::GetActualFamily() const
2409 {
2410     const SfxStyleFamilyItem *pFamilyItem = GetFamilyItem_Impl();
2411     if( !pFamilyItem || nActFamily == SFX_TEMPLDLG_FILTER_MAX )
2412         return SFX_STYLE_FAMILY_PARA;
2413     else
2414         return pFamilyItem->GetFamily();
2415 }
2416 
2417 // -----------------------------------------------------------------------
2418 
2419 void SfxCommonTemplateDialog_Impl::EnableExample_Impl(sal_uInt16 nId, sal_Bool bEnable)
2420 {
2421     if( nId == SID_STYLE_NEW_BY_EXAMPLE )
2422         bNewByExampleDisabled = !bEnable;
2423     else if( nId == SID_STYLE_UPDATE_BY_EXAMPLE )
2424         bUpdateByExampleDisabled = !bEnable;
2425     EnableItem(nId, bEnable);
2426 }
2427 
2428 void SfxCommonTemplateDialog_Impl::PrepareDeleteAction()
2429 {
2430 }
2431 
2432 // -----------------------------------------------------------------------
2433 
2434 PopupMenu* SfxCommonTemplateDialog_Impl::CreateContextMenu( void )
2435 {
2436     if ( bBindingUpdate )
2437     {
2438         pBindings->Invalidate( SID_STYLE_NEW, sal_True, sal_False );
2439         pBindings->Update( SID_STYLE_NEW );
2440         bBindingUpdate = sal_False;
2441     }
2442     PopupMenu* pMenu = new PopupMenu( SfxResId( MN_CONTEXT_TEMPLDLG ) );
2443     pMenu->SetSelectHdl( LINK( this, SfxCommonTemplateDialog_Impl, MenuSelectHdl ) );
2444     pMenu->EnableItem( ID_EDIT, bCanEdit );
2445     pMenu->EnableItem( ID_DELETE, bCanDel );
2446     pMenu->EnableItem( ID_NEW, bCanNew );
2447 
2448     return pMenu;
2449 }
2450 
2451 // ------------------------------------------------------------------------
2452 
2453 
2454 SfxTemplateDialog_Impl::SfxTemplateDialog_Impl(
2455     SfxBindings* pB, SfxTemplateDialog* pDlgWindow ) :
2456 
2457     SfxCommonTemplateDialog_Impl( pB, pDlgWindow, true ),
2458 
2459     m_pFloat            ( pDlgWindow ),
2460     m_bZoomIn           ( sal_False ),
2461     m_aActionTbL        ( pDlgWindow, this ),
2462     m_aActionTbR        ( pDlgWindow, SfxResId( TB_ACTION ) )
2463 
2464 {
2465     pDlgWindow->FreeResource();
2466     Initialize();
2467 }
2468 
2469 SfxTemplateDialog_Impl::SfxTemplateDialog_Impl(
2470     SfxBindings* pB, SfxTemplatePanelControl* pDlgWindow )
2471     : SfxCommonTemplateDialog_Impl( pB, pDlgWindow, true ),
2472       m_pFloat          ( pDlgWindow ),
2473       m_bZoomIn         ( sal_False ),
2474       m_aActionTbL        ( pDlgWindow, this ),
2475       m_aActionTbR      ( pDlgWindow, SfxResId( TB_ACTION ) )
2476 {
2477     pDlgWindow->FreeResource();
2478     Initialize();
2479 }
2480 
2481 void SfxTemplateDialog_Impl::Initialize (void)
2482 {
2483     // Read the filter stored in the configuration
2484     // This is already done in ReadResource(), invoked by Initialize()
2485     // and stored in nActFilter, but we can't rely on nActFilter's value
2486     // because it is changed in UpdateStyles_Impl
2487     SfxViewFrame* pViewFrame = pBindings->GetDispatcher_Impl()->GetFrame();
2488     pCurObjShell = pViewFrame->GetObjectShell();
2489     const sal_uInt16 nConfigFilter = pCurObjShell ?
2490         static_cast< sal_uInt16 >( LoadFactoryStyleFilter( pCurObjShell ) ) : 0;
2491 
2492     SfxCommonTemplateDialog_Impl::Initialize();
2493 
2494     m_aActionTbL.SetSelectHdl(LINK(this, SfxTemplateDialog_Impl, ToolBoxLSelect));
2495     m_aActionTbR.SetSelectHdl(LINK(this, SfxTemplateDialog_Impl, ToolBoxRSelect));
2496     m_aActionTbR.SetDropdownClickHdl(LINK(this, SfxTemplateDialog_Impl, ToolBoxRClick));
2497     m_aActionTbL.Show();
2498     m_aActionTbR.Show();
2499     Font aFont=aFilterLb.GetFont();
2500     aFont.SetWeight( WEIGHT_NORMAL );
2501     aFilterLb.SetFont( aFont );
2502     m_aActionTbL.SetHelpId( HID_TEMPLDLG_TOOLBOX_LEFT );
2503 
2504     if ( nConfigFilter == SFX_TEMPLDLG_FILTER_HIERARCHICAL )
2505     {
2506         bHierarchical = sal_False;
2507         aFilterLb.SelectEntry(String(SfxResId(STR_STYLE_FILTER_HIERARCHICAL)));
2508         FilterSelectHdl(&aFilterLb);
2509     }
2510 }
2511 
2512 // ------------------------------------------------------------------------
2513 
2514 void SfxTemplateDialog_Impl::EnableFamilyItem( sal_uInt16 nId, sal_Bool bEnable )
2515 {
2516     m_aActionTbL.EnableItem( nId, bEnable );
2517 }
2518 
2519 //-------------------------------------------------------------------------
2520 
2521 void SfxTemplateDialog_Impl::InsertFamilyItem(sal_uInt16 nId,const SfxStyleFamilyItem *pItem)
2522 {
2523     rtl::OString sHelpId;
2524     switch( (sal_uInt16) pItem->GetFamily() )
2525     {
2526         case SFX_STYLE_FAMILY_CHAR:     sHelpId = ".uno:CharStyle"; break;
2527         case SFX_STYLE_FAMILY_PARA:     sHelpId = ".uno:ParaStyle"; break;
2528         case SFX_STYLE_FAMILY_FRAME:    sHelpId = ".uno:FrameStyle"; break;
2529         case SFX_STYLE_FAMILY_PAGE:     sHelpId = ".uno:PageStyle"; break;
2530         case SFX_STYLE_FAMILY_PSEUDO:   sHelpId = ".uno:ListStyle"; break;
2531         default: DBG_ERROR("unknown StyleFamily"); break;
2532     }
2533     m_aActionTbL.InsertItem( nId, pItem->GetImage(), pItem->GetText(), 0, 0);
2534     m_aActionTbL.SetHelpId( nId, sHelpId );
2535 }
2536 
2537 // ------------------------------------------------------------------------
2538 
2539 void SfxTemplateDialog_Impl::ReplaceUpdateButtonByMenu()
2540 {
2541     m_aActionTbR.HideItem(SID_STYLE_UPDATE_BY_EXAMPLE);
2542     m_aActionTbR.SetItemBits( SID_STYLE_NEW_BY_EXAMPLE,
2543             TIB_DROPDOWNONLY|m_aActionTbR.GetItemBits( SID_STYLE_NEW_BY_EXAMPLE ));
2544 }
2545 
2546 // ------------------------------------------------------------------------
2547 void SfxTemplateDialog_Impl::updateFamilyImages()
2548 {
2549     if ( !m_pStyleFamiliesId )
2550         // we do not have a resource id to load the new images from
2551         return;
2552 
2553     // let the families collection update the images
2554     sal_Bool bIsHighContrast;
2555     if (m_pFloat != NULL)
2556         bIsHighContrast = m_pFloat->GetSettings().GetStyleSettings().GetHighContrastMode();
2557     else
2558         bIsHighContrast = false;
2559     pStyleFamilies->updateImages( *m_pStyleFamiliesId, bIsHighContrast ? BMP_COLOR_HIGHCONTRAST : BMP_COLOR_NORMAL );
2560 
2561     // and set the new images on our toolbox
2562     sal_uInt16 nLoop = pStyleFamilies->Count();
2563     for( ; nLoop--; )
2564     {
2565         const SfxStyleFamilyItem *pItem = pStyleFamilies->GetObject( nLoop );
2566         sal_uInt16 nId = SfxFamilyIdToNId( pItem->GetFamily() );
2567         m_aActionTbL.SetItemImage( nId, pItem->GetImage() );
2568     }
2569 }
2570 
2571 // ------------------------------------------------------------------------
2572 void SfxTemplateDialog_Impl::updateNonFamilyImages()
2573 {
2574     if (m_pFloat != NULL)
2575         m_aActionTbR.SetImageList( ImageList( SfxResId(
2576                     m_pFloat->GetSettings().GetStyleSettings().GetHighContrastMode() ? IMG_LST_STYLE_DESIGNER_HC
2577                                                              : DLG_STYLE_DESIGNER ) ) );
2578 }
2579 
2580 // ------------------------------------------------------------------------
2581 
2582 void SfxTemplateDialog_Impl::ClearFamilyList()
2583 {
2584     m_aActionTbL.Clear();
2585 }
2586 
2587 //-------------------------------------------------------------------------
2588 
2589 void SfxCommonTemplateDialog_Impl::InvalidateBindings()
2590 {
2591     pBindings->Invalidate(SID_STYLE_NEW_BY_EXAMPLE, sal_True, sal_False);
2592     pBindings->Update( SID_STYLE_NEW_BY_EXAMPLE );
2593     pBindings->Invalidate(SID_STYLE_UPDATE_BY_EXAMPLE, sal_True, sal_False);
2594     pBindings->Update( SID_STYLE_UPDATE_BY_EXAMPLE );
2595     pBindings->Invalidate( SID_STYLE_WATERCAN, sal_True, sal_False);
2596     pBindings->Update( SID_STYLE_WATERCAN );
2597     pBindings->Invalidate( SID_STYLE_NEW, sal_True, sal_False );
2598     pBindings->Update( SID_STYLE_NEW );
2599     pBindings->Invalidate( SID_STYLE_DRAGHIERARCHIE, sal_True, sal_False );
2600     pBindings->Update( SID_STYLE_DRAGHIERARCHIE );
2601 }
2602 
2603 //-------------------------------------------------------------------------
2604 
2605 SfxTemplateDialog_Impl::~SfxTemplateDialog_Impl()
2606 {
2607 /*
2608     SfxImageManager* pImgMgr = pBindings->GetImageManager();
2609     if ( pImgMgr )
2610     {
2611         pImgMgr->ReleaseToolBox( &m_aActionTbL );
2612         pImgMgr->ReleaseToolBox( &m_aActionTbR );
2613     }
2614 */
2615 }
2616 
2617 //-------------------------------------------------------------------------
2618 
2619 void SfxTemplateDialog_Impl::LoadedFamilies()
2620 {
2621     updateFamilyImages();
2622     Resize();
2623 }
2624 
2625 //-------------------------------------------------------------------------
2626 
2627 // "Uberladener Resize-Handler ( StarView )
2628 // Die Groesse der Listboxen wird angepasst
2629 void SfxTemplateDialog_Impl::Resize()
2630 {
2631     SfxDockingWindow* pDockingWindow = dynamic_cast<SfxDockingWindow*>(m_pFloat);
2632     FloatingWindow *pF = pDockingWindow!=NULL ? pDockingWindow->GetFloatingWindow() : NULL;
2633     if ( pF )
2634     {
2635 //      if(pF->IsZoomedIn() && m_bZoomIn==sal_False)
2636 //          pF->SetText(String(SfxResId( DLG_STYLE_DESIGNER )));
2637 //      if(!pF->IsZoomedIn() && m_bZoomIn==sal_True && GetFamilyItem_Impl())
2638 //          UpdateStyles_Impl(UPDATE_FAMILY); //Bereich wieder in Titel schreiben
2639         m_bZoomIn = pF->IsRollUp();
2640         if ( m_bZoomIn )
2641             return;
2642     }
2643 
2644     if (m_pFloat == NULL)
2645         return;
2646     Size aDlgSize=m_pFloat->PixelToLogic(m_pFloat->GetOutputSizePixel());
2647     Size aSizeATL=m_pFloat->PixelToLogic(m_aActionTbL.CalcWindowSizePixel());
2648     Size aSizeATR=m_pFloat->PixelToLogic(m_aActionTbR.CalcWindowSizePixel());
2649     Size aMinSize = GetMinOutputSizePixel();
2650 
2651     long nListHeight = m_pFloat->PixelToLogic( aFilterLb.GetSizePixel() ).Height();
2652     long nWidth = aDlgSize.Width()- 2 * SFX_TEMPLDLG_HFRAME;
2653 
2654     m_aActionTbL.SetPosSizePixel(m_pFloat->LogicToPixel(Point(SFX_TEMPLDLG_HFRAME,SFX_TEMPLDLG_VTOPFRAME)),
2655                                  m_pFloat->LogicToPixel(aSizeATL));
2656 
2657     // Die Position der rechten Toolbox nur ver"andern, wenn das Fenster
2658     // breit genug ist
2659     Point aPosATR(aDlgSize.Width()-SFX_TEMPLDLG_HFRAME-aSizeATR.Width(),SFX_TEMPLDLG_VTOPFRAME);
2660     if(aDlgSize.Width() >= aMinSize.Width())
2661         m_aActionTbR.SetPosPixel(m_pFloat->LogicToPixel(aPosATR));
2662     else
2663         m_aActionTbR.SetPosPixel( m_pFloat->LogicToPixel(
2664             Point( SFX_TEMPLDLG_HFRAME + aSizeATL.Width() + SFX_TEMPLDLG_MIDHSPACE,
2665                    SFX_TEMPLDLG_VTOPFRAME ) ) );
2666 
2667     m_aActionTbR.SetSizePixel(m_pFloat->LogicToPixel(aSizeATR));
2668 
2669     Point aFilterPos(
2670         m_pFloat->LogicToPixel(Point(SFX_TEMPLDLG_HFRAME,
2671             aDlgSize.Height()-SFX_TEMPLDLG_VBOTFRAME-nListHeight)) );
2672 
2673     Size aFilterSize(
2674         m_pFloat->LogicToPixel(Size(nWidth,SFX_TEMPLDLG_FILTERHEIGHT)) );
2675 
2676     Point aFmtPos(
2677         m_pFloat->LogicToPixel(Point(SFX_TEMPLDLG_HFRAME, SFX_TEMPLDLG_VTOPFRAME +
2678                             SFX_TEMPLDLG_MIDVSPACE+aSizeATL.Height())) );
2679     Size aFmtSize(
2680         m_pFloat->LogicToPixel(Size(nWidth,
2681                     aDlgSize.Height() - SFX_TEMPLDLG_VBOTFRAME -
2682                     SFX_TEMPLDLG_VTOPFRAME - 2*SFX_TEMPLDLG_MIDVSPACE-
2683                     nListHeight-aSizeATL.Height())) );
2684 
2685     // Die Position der Listboxen nur ver"andern, wenn das Fenster
2686     // hoch genug ist
2687     if(aDlgSize.Height() >= aMinSize.Height())
2688     {
2689         aFilterLb.SetPosPixel(aFilterPos);
2690         aFmtLb.SetPosPixel( aFmtPos );
2691         if(pTreeBox)
2692             pTreeBox->SetPosPixel(aFmtPos);
2693     }
2694     else
2695         aFmtSize.Height() += aFilterSize.Height();
2696 
2697     aFilterLb.SetSizePixel(aFilterSize);
2698     aFmtLb.SetSizePixel( aFmtSize );
2699     if(pTreeBox)
2700         pTreeBox->SetSizePixel(aFmtSize);
2701 }
2702 
2703 // -----------------------------------------------------------------------
2704 
2705 
2706 Size SfxTemplateDialog_Impl::GetMinOutputSizePixel()
2707 {
2708     if (m_pFloat != NULL)
2709     {
2710         Size aSizeATL=m_pFloat->PixelToLogic(m_aActionTbL.CalcWindowSizePixel());
2711         Size aSizeATR=m_pFloat->PixelToLogic(m_aActionTbR.CalcWindowSizePixel());
2712         Size aMinSize=Size(
2713             aSizeATL.Width()+aSizeATR.Width()+
2714                 2*SFX_TEMPLDLG_HFRAME + SFX_TEMPLDLG_MIDHSPACE,
2715             4*aSizeATL.Height()+2*SFX_TEMPLDLG_MIDVSPACE);
2716         return aMinSize;
2717     }
2718     else
2719         return Size(0,0);
2720 }
2721 
2722 //-------------------------------------------------------------------------
2723 
2724 void SfxTemplateDialog_Impl::Command( const CommandEvent& rCEvt )
2725 {
2726     if (m_pFloat != NULL)
2727         if(COMMAND_CONTEXTMENU  == rCEvt.GetCommand())
2728             ExecuteContextMenu_Impl( rCEvt.GetMousePosPixel(), m_pFloat );
2729         else
2730             m_pFloat->Command(rCEvt);
2731 }
2732 
2733 //-------------------------------------------------------------------------
2734 
2735 void SfxTemplateDialog_Impl::EnableItem(sal_uInt16 nMesId, sal_Bool bCheck)
2736 {
2737     String aEmpty;
2738     switch(nMesId)
2739     {
2740       case SID_STYLE_WATERCAN :
2741           if(!bCheck && IsCheckedItem(SID_STYLE_WATERCAN))
2742             Execute_Impl(SID_STYLE_WATERCAN, aEmpty, aEmpty, 0);
2743       case SID_STYLE_NEW_BY_EXAMPLE:
2744       case SID_STYLE_UPDATE_BY_EXAMPLE:
2745         m_aActionTbR.EnableItem(nMesId,bCheck);
2746         break;
2747     }
2748 }
2749 
2750 //-------------------------------------------------------------------------
2751 
2752 void SfxTemplateDialog_Impl::CheckItem(sal_uInt16 nMesId, sal_Bool bCheck)
2753 {
2754     switch(nMesId)
2755     {
2756         case SID_STYLE_WATERCAN :
2757             bIsWater=bCheck;
2758             m_aActionTbR.CheckItem(SID_STYLE_WATERCAN,bCheck);
2759             break;
2760         default:
2761             m_aActionTbL.CheckItem(nMesId,bCheck); break;
2762     }
2763 }
2764 
2765 //-------------------------------------------------------------------------
2766 
2767 sal_Bool SfxTemplateDialog_Impl::IsCheckedItem(sal_uInt16 nMesId)
2768 {
2769     switch(nMesId)
2770     {
2771         case SID_STYLE_WATERCAN :
2772             return m_aActionTbR.GetItemState(SID_STYLE_WATERCAN)==STATE_CHECK;
2773         default:
2774             return m_aActionTbL.GetItemState(nMesId)==STATE_CHECK;
2775     }
2776 }
2777 
2778 //-------------------------------------------------------------------------
2779 
2780 IMPL_LINK_INLINE_START( SfxTemplateDialog_Impl, ToolBoxLSelect, ToolBox *, pBox )
2781 {
2782     const sal_uInt16 nEntry = pBox->GetCurItemId();
2783     FamilySelect(nEntry);
2784     return 0;
2785 }
2786 IMPL_LINK_INLINE_END( SfxTemplateDialog_Impl, ToolBoxLSelect, ToolBox *, pBox )
2787 
2788 //-------------------------------------------------------------------------
2789 ::rtl::OUString lcl_GetLabel(uno::Any& rAny)
2790 {
2791     ::rtl::OUString sRet;
2792     uno::Sequence< beans::PropertyValue >aPropSeq;
2793     if ( rAny >>= aPropSeq )
2794     {
2795         for( sal_Int32 i = 0; i < aPropSeq.getLength(); i++ )
2796         {
2797             if ( aPropSeq[i].Name.equalsAscii( "Label" ))
2798             {
2799                 aPropSeq[i].Value >>= sRet;
2800                 break;
2801             }
2802         }
2803     }
2804     return sRet;
2805 }
2806 //-------------------------------------------------------------------------
2807 
2808 IMPL_LINK( SfxTemplateDialog_Impl, ToolBoxRSelect, ToolBox *, pBox )
2809 {
2810     const sal_uInt16 nEntry = pBox->GetCurItemId();
2811     if(nEntry != SID_STYLE_NEW_BY_EXAMPLE ||
2812             TIB_DROPDOWN != (pBox->GetItemBits(nEntry)&TIB_DROPDOWN))
2813         ActionSelect(nEntry);
2814     return 0;
2815 }
2816 //-------------------------------------------------------------------------
2817 IMPL_LINK( SfxTemplateDialog_Impl, ToolBoxRClick, ToolBox *, pBox )
2818 {
2819     const sal_uInt16 nEntry = pBox->GetCurItemId();
2820     if(nEntry == SID_STYLE_NEW_BY_EXAMPLE &&
2821             TIB_DROPDOWN == (pBox->GetItemBits(nEntry)&TIB_DROPDOWN))
2822     {
2823         //create a popup menu in Writer
2824         PopupMenu *pMenu = new PopupMenu;
2825         uno::Reference< container::XNameAccess > xNameAccess(
2826                     ::comphelper::getProcessServiceFactory()->
2827                     createInstance( ::rtl::OUString::createFromAscii(
2828                             "com.sun.star.frame.UICommandDescription") ), uno::UNO_QUERY );
2829         uno::Reference< container::XNameAccess > xUICommands;
2830         if ( xNameAccess.is() )
2831         {
2832             rtl::OUString sTextDoc = ::rtl::OUString::createFromAscii("com.sun.star.text.TextDocument");
2833             if(xNameAccess->hasByName(sTextDoc))
2834             {
2835                 uno::Any a = xNameAccess->getByName( sTextDoc );
2836                 a >>= xUICommands;
2837             }
2838         }
2839         if(!xUICommands.is())
2840             return 0;
2841         try
2842         {
2843             uno::Sequence< beans::PropertyValue > aPropSeq;
2844             uno::Any aCommand = xUICommands->getByName(::rtl::OUString::createFromAscii(".uno:StyleNewByExample"));
2845             ::rtl::OUString sLabel = lcl_GetLabel( aCommand );
2846             pMenu->InsertItem( SID_STYLE_NEW_BY_EXAMPLE, sLabel );
2847             pMenu->SetHelpId(SID_STYLE_NEW_BY_EXAMPLE, HID_TEMPLDLG_NEWBYEXAMPLE);
2848 
2849             aCommand = xUICommands->getByName(::rtl::OUString::createFromAscii(".uno:StyleUpdateByExample"));
2850             sLabel = lcl_GetLabel( aCommand );
2851 
2852             pMenu->InsertItem( SID_STYLE_UPDATE_BY_EXAMPLE, sLabel );
2853             pMenu->SetHelpId(SID_STYLE_UPDATE_BY_EXAMPLE, HID_TEMPLDLG_UPDATEBYEXAMPLE);
2854 
2855             aCommand = xUICommands->getByName(::rtl::OUString::createFromAscii(".uno:LoadStyles"));
2856             sLabel = lcl_GetLabel( aCommand );
2857             pMenu->InsertItem( SID_TEMPLATE_LOAD, sLabel );
2858             pMenu->SetHelpId(SID_TEMPLATE_LOAD, ".uno:LoadStyles");
2859 
2860             pMenu->SetSelectHdl(LINK(this, SfxTemplateDialog_Impl, MenuSelectHdl));
2861             pMenu->Execute( pBox,
2862                             pBox->GetItemRect(nEntry),
2863                             POPUPMENU_EXECUTE_DOWN );
2864             pBox->EndSelection();
2865         }
2866         catch(uno::Exception&)
2867         {
2868         }
2869         delete pMenu;
2870         pBox->Invalidate();
2871     }
2872     return 0;
2873 }
2874 //-------------------------------------------------------------------------
2875 IMPL_LINK( SfxTemplateDialog_Impl, MenuSelectHdl, Menu*, pMenu)
2876 {
2877     sal_uInt16 nMenuId = pMenu->GetCurItemId();
2878     ActionSelect(nMenuId);
2879     return 0;
2880 }
2881 //-------------------------------------------------------------------------
2882 
2883 SfxTemplateCatalog_Impl::SfxTemplateCatalog_Impl( Window* /*pParent*/, SfxBindings* pB,
2884                                                   SfxTemplateCatalog* pTmpWindow ) :
2885 
2886     SfxCommonTemplateDialog_Impl( pB, pTmpWindow ),
2887 
2888     aFamList    ( pTmpWindow, SfxResId( BT_TOOL ) ),
2889     aOkBtn      ( pTmpWindow, SfxResId( BT_OK ) ),
2890     aCancelBtn  ( pTmpWindow, SfxResId( BT_CANCEL ) ),
2891     aNewBtn     ( pTmpWindow, SfxResId( BT_NEW ) ),
2892     aChangeBtn  ( pTmpWindow, SfxResId( BT_EDIT ) ),
2893     aDelBtn     ( pTmpWindow, SfxResId( BT_DEL ) ),
2894     aOrgBtn     ( pTmpWindow, SfxResId( BT_ORG ) ),
2895     aHelpBtn    ( pTmpWindow, SfxResId( BT_HELP ) ),
2896     pReal       ( pTmpWindow ),
2897     aHelper     ( pTmpWindow )
2898 
2899 {
2900     aNewBtn.Disable();
2901     aDelBtn.Disable();
2902     aChangeBtn.Disable();
2903 
2904     SFX_APP()->Get_Impl()->pTemplateCommon = GetISfxTemplateCommon();
2905     pTmpWindow->FreeResource();
2906 
2907     Initialize();
2908 
2909     aFamList.SetSelectHdl(  LINK( this, SfxTemplateCatalog_Impl, FamListSelect ) );
2910     aOkBtn.SetClickHdl(     LINK( this, SfxTemplateCatalog_Impl, OkHdl ) );
2911     aCancelBtn.SetClickHdl( LINK( this, SfxTemplateCatalog_Impl, CancelHdl ) );
2912     aNewBtn.SetClickHdl(    LINK( this, SfxTemplateCatalog_Impl, NewHdl ) );
2913     aDelBtn.SetClickHdl(    LINK( this, SfxTemplateCatalog_Impl, DelHdl ) );
2914     aChangeBtn.SetClickHdl( LINK( this, SfxTemplateCatalog_Impl, ChangeHdl ) );
2915     aOrgBtn.SetClickHdl(    LINK( this, SfxTemplateCatalog_Impl, OrgHdl ) );
2916 }
2917 
2918 //-------------------------------------------------------------------------
2919 
2920 SfxTemplateCatalog_Impl::~SfxTemplateCatalog_Impl()
2921 {
2922     SFX_APP()->Get_Impl()->pTemplateCommon = 0;
2923 }
2924 
2925 //-------------------------------------------------------------------------
2926 
2927 IMPL_LINK_INLINE_START( SfxTemplateCatalog_Impl, OkHdl, Button *, pButton )
2928 {
2929     (void)pButton; //unused
2930     ApplyHdl( NULL );
2931     pReal->EndDialog( RET_OK );
2932     return 0;
2933 }
2934 IMPL_LINK_INLINE_END( SfxTemplateCatalog_Impl, OkHdl, Button *, pButton )
2935 
2936 //-------------------------------------------------------------------------
2937 
2938 IMPL_LINK_INLINE_START( SfxTemplateCatalog_Impl, CancelHdl, Button *, pButton )
2939 {
2940     (void)pButton; //unused
2941     pReal->EndDialog( RET_CANCEL );
2942     return 0;
2943 }
2944 IMPL_LINK_INLINE_END( SfxTemplateCatalog_Impl, CancelHdl, Button *, pButton )
2945 
2946 //-------------------------------------------------------------------------
2947 
2948 IMPL_LINK_INLINE_START( SfxTemplateCatalog_Impl, NewHdl, Button *, pButton )
2949 {
2950     (void)pButton; //unused
2951     aCancelBtn.SetText( String( SfxResId( STR_CLOSE ) ) );
2952     SfxCommonTemplateDialog_Impl::NewHdl( NULL );
2953     return 0;
2954 }
2955 IMPL_LINK_INLINE_END( SfxTemplateCatalog_Impl, NewHdl, Button *, pButton )
2956 
2957 //-------------------------------------------------------------------------
2958 
2959 IMPL_LINK_INLINE_START( SfxTemplateCatalog_Impl, ChangeHdl, Button *, pButton )
2960 {
2961     (void)pButton; //unused
2962     aCancelBtn.SetText( String( SfxResId( STR_CLOSE ) ) );
2963     SfxCommonTemplateDialog_Impl::EditHdl( NULL );
2964     return 0;
2965 }
2966 IMPL_LINK_INLINE_END( SfxTemplateCatalog_Impl, ChangeHdl, Button *, pButton )
2967 
2968 //-------------------------------------------------------------------------
2969 
2970 IMPL_LINK_INLINE_START( SfxTemplateCatalog_Impl, DelHdl, Button *, pButton )
2971 {
2972     (void)pButton; //unused
2973     SfxCommonTemplateDialog_Impl::DeleteHdl( NULL );
2974     return 0;
2975 }
2976 IMPL_LINK_INLINE_END( SfxTemplateCatalog_Impl, DelHdl, Button *, pButton )
2977 
2978 //-------------------------------------------------------------------------
2979 
2980 IMPL_LINK( SfxTemplateCatalog_Impl, OrgHdl, Button *, pButton )
2981 {
2982     (void)pButton; //unused
2983     aCancelBtn.SetText( String( SfxResId( STR_CLOSE ) ) );
2984     SfxDocumentTemplates aTemplates;
2985     aTemplates.Construct();
2986     SfxTemplateOrganizeDlg* pDlg = new SfxTemplateOrganizeDlg( pReal, &aTemplates );
2987     const short nRet = pDlg->Execute();
2988     delete pDlg;
2989     if ( RET_OK == nRet )
2990         Update_Impl();
2991     else if ( RET_EDIT_STYLE == nRet )
2992         pReal->EndDialog( RET_CANCEL );
2993     return 0;
2994 }
2995 
2996 //-------------------------------------------------------------------------
2997 
2998 void SfxTemplateCatalog_Impl::EnableEdit( sal_Bool bEnable )
2999 {
3000     SfxCommonTemplateDialog_Impl::EnableEdit( bEnable );
3001     aChangeBtn.Enable( bEnable );
3002 }
3003 
3004 //-------------------------------------------------------------------------
3005 
3006 void SfxTemplateCatalog_Impl::EnableDel( sal_Bool bEnable )
3007 {
3008     SfxCommonTemplateDialog_Impl::EnableDel( bEnable );
3009     aDelBtn.Enable( bEnable );
3010 }
3011 
3012 void SfxTemplateCatalog_Impl::EnableNew(sal_Bool bEnable)
3013 {
3014     SfxCommonTemplateDialog_Impl::EnableNew( bEnable );
3015     aNewBtn.Enable( bEnable );
3016 }
3017 
3018 //-------------------------------------------------------------------------
3019 
3020 IMPL_LINK_INLINE_START( SfxTemplateCatalog_Impl, FamListSelect, ListBox *, pList )
3021 {
3022     const sal_uInt16 nEntry = aFamIds[pList->GetSelectEntryPos()];
3023     FamilySelect(nEntry);
3024     return 0;
3025 }
3026 IMPL_LINK_INLINE_END( SfxTemplateCatalog_Impl, FamListSelect, ListBox *, pList )
3027 
3028 //-------------------------------------------------------------------------
3029 
3030 void SfxTemplateCatalog_Impl::EnableItem( sal_uInt16 nMesId, sal_Bool bCheck )
3031 {
3032     if ( nMesId == SID_STYLE_WATERCAN )
3033         aOkBtn.Enable( bCheck );
3034     if ( nMesId > SFX_STYLE_FAMILY_PSEUDO || nMesId < SFX_STYLE_FAMILY_CHAR )
3035         return;
3036 
3037 /*      for(sal_uInt16 i=0;i<aFamIds.Count&&aFamIds[i]!=nMesId;i++);
3038     if(i!=aFamIds.Count())
3039         aFamList.SelectEntry(aFamIds[i]);
3040     else
3041         DBG_ERROR("Entry nicht gefunden");*/
3042 
3043 }
3044 
3045 //-------------------------------------------------------------------------
3046 
3047 void SfxTemplateCatalog_Impl::CheckItem(sal_uInt16 nMesId, sal_Bool /*bCheck*/)
3048 {
3049     if ( nMesId > SFX_STYLE_FAMILY_PSEUDO || nMesId < SFX_STYLE_FAMILY_CHAR )
3050         return;
3051     sal_uInt16 i;
3052     for ( i = 0; i < aFamIds.Count() && aFamIds[i] != nMesId; i++ ) ;
3053     aFamList.SelectEntryPos(i);
3054 }
3055 
3056 //-------------------------------------------------------------------------
3057 
3058 sal_Bool SfxTemplateCatalog_Impl::IsCheckedItem(sal_uInt16 nMesId)
3059 {
3060     if ( nMesId > SFX_STYLE_FAMILY_PSEUDO || nMesId < SFX_STYLE_FAMILY_CHAR )
3061         return sal_False;
3062     sal_uInt16 i;
3063     for ( i = 0; i < aFamIds.Count() && aFamIds[i] != nMesId; i++ )
3064         ;
3065     return aFamList.IsEntrySelected( String::CreateFromInt32(i) );
3066 }
3067 
3068 //-------------------------------------------------------------------------
3069 
3070 // Der Katalog muss nur das Disablen beherrschen, da waehrend seiner
3071 // Lebenszeit keine Selektionsaenderungen vorgenommen werden koennen
3072 void SfxTemplateCatalog_Impl::EnableFamilyItem( sal_uInt16 nId, sal_Bool bEnable )
3073 {
3074     if ( !bEnable )
3075         for ( sal_uInt16 nPos = aFamIds.Count(); nPos--; )
3076             if ( aFamIds[ nPos ] == nId )
3077             {
3078                 aFamIds.Remove( nPos );
3079                 aFamList.RemoveEntry( nPos );
3080             }
3081 }
3082 
3083 void SfxTemplateCatalog_Impl::InsertFamilyItem( sal_uInt16 nId, const SfxStyleFamilyItem* pItem )
3084 {
3085     if ( nId > SFX_STYLE_FAMILY_PSEUDO || nId < SFX_STYLE_FAMILY_CHAR )
3086         return;
3087     aFamList.InsertEntry( pItem->GetText(), 0 );
3088     aFamIds.Insert( nId, 0 );
3089 }
3090 
3091 void SfxTemplateCatalog_Impl::ClearFamilyList()
3092 {
3093     aFamList.Clear();
3094     aFamIds.Remove( 0, aFamIds.Count() );
3095 }
3096 
3097 void SfxTemplateCatalog_Impl::PrepareDeleteAction()
3098 {
3099     aDelBtn.Disable();
3100     aCancelBtn.SetText( String( SfxResId( STR_CLOSE ) ) );
3101 }
3102 
3103 
3104 void SfxCommonTemplateDialog_Impl::SetFamily( sal_uInt16 nId )
3105 {
3106     if ( nId != nActFamily )
3107     {
3108         if ( nActFamily != SFX_TEMPLDLG_FILTER_MAX )
3109             CheckItem( nActFamily, sal_False );
3110         nActFamily = nId;
3111         if ( nId != SFX_TEMPLDLG_FILTER_MAX )
3112             bUpdateFamily = sal_True;
3113     }
3114 }
3115 
3116 void SfxCommonTemplateDialog_Impl::UpdateFamily_Impl()
3117 {
3118     bUpdateFamily = sal_False;
3119 
3120     SfxDispatcher* pDispat = pBindings->GetDispatcher_Impl();
3121     SfxViewFrame *pViewFrame = pDispat->GetFrame();
3122     SfxObjectShell *pDocShell = pViewFrame->GetObjectShell();
3123 
3124     SfxStyleSheetBasePool *pOldStyleSheetPool = pStyleSheetPool;
3125     pStyleSheetPool = pDocShell? pDocShell->GetStyleSheetPool(): 0;
3126     if ( pOldStyleSheetPool != pStyleSheetPool )
3127     {
3128         if ( pOldStyleSheetPool )
3129             EndListening(*pOldStyleSheetPool);
3130         if ( pStyleSheetPool )
3131             StartListening(*pOldStyleSheetPool);
3132     }
3133 
3134     bWaterDisabled = sal_False;
3135     bCanNew = sal_True;
3136     bTreeDrag = sal_True;
3137     bUpdateByExampleDisabled = sal_False;
3138 
3139     if ( pStyleSheetPool )
3140     {
3141         if(!pTreeBox)
3142             UpdateStyles_Impl(UPDATE_FAMILY | UPDATE_FAMILY_LIST);
3143         else
3144         {
3145             UpdateStyles_Impl(UPDATE_FAMILY);
3146             FillTreeBox();
3147         }
3148     }
3149 
3150     InvalidateBindings();
3151 
3152     if ( IsCheckedItem( SID_STYLE_WATERCAN ) &&
3153          // nur, wenn dieser Bereich erlaubt ist
3154          0 != pFamilyState[ nActFamily - 1 ] )
3155         Execute_Impl( SID_STYLE_APPLY, GetSelectedEntry(),
3156                       String(), (sal_uInt16)GetFamilyItem_Impl()->GetFamily() );
3157 }
3158 void SfxCommonTemplateDialog_Impl::ReplaceUpdateButtonByMenu()
3159 {
3160     //does nothing
3161 }
3162 
3163 void SfxTemplateDialog::StateChanged( StateChangedType nStateChange )
3164 {
3165     if ( nStateChange == STATE_CHANGE_INITSHOW )
3166     {
3167         SfxViewFrame *pFrame = GetBindings().GetDispatcher_Impl()->GetFrame();
3168         Window* pEditWin = pFrame->GetViewShell()->GetWindow();
3169 
3170         Size aSize = pEditWin->GetSizePixel();
3171         Point aPoint = pEditWin->OutputToScreenPixel( pEditWin->GetPosPixel() );
3172         aPoint = GetParent()->ScreenToOutputPixel( aPoint );
3173         Size aWinSize = GetSizePixel();
3174         aPoint.X() += aSize.Width() - aWinSize.Width() - 20;
3175         aPoint.Y() += aSize.Height() / 2 - aWinSize.Height() / 2;
3176         SetFloatingPos( aPoint );
3177     }
3178 
3179     SfxDockingWindow::StateChanged( nStateChange );
3180 }
3181 
3182 /*-- 10.12.2003 11:44:35---------------------------------------------------
3183 
3184   -----------------------------------------------------------------------*/
3185 DropToolBox_Impl::DropToolBox_Impl(Window* pParent, SfxTemplateDialog_Impl* pTemplateDialog) :
3186     ToolBox(pParent),
3187     DropTargetHelper(this),
3188     rParent(*pTemplateDialog)
3189 {
3190 }
3191 /*-- 10.12.2003 11:44:35---------------------------------------------------
3192 
3193   -----------------------------------------------------------------------*/
3194 DropToolBox_Impl::~DropToolBox_Impl()
3195 {
3196 }
3197 /*-- 10.12.2003 11:44:35---------------------------------------------------
3198 
3199   -----------------------------------------------------------------------*/
3200 sal_Int8    DropToolBox_Impl::AcceptDrop( const AcceptDropEvent& rEvt )
3201 {
3202     sal_Int8 nReturn = DND_ACTION_NONE;
3203     sal_uInt16 nItemId = GetItemId( rEvt.maPosPixel );
3204     if(USHRT_MAX != nItemId && !IsItemChecked( nItemId ))
3205     {
3206         SetCurItemId(nItemId);
3207         GetSelectHdl().Call(this);
3208     }
3209     // special case: page styles are allowed to create new styles by example
3210     // but not allowed to be created by drag and drop
3211     if ( nItemId != SfxCommonTemplateDialog_Impl::SfxFamilyIdToNId( SFX_STYLE_FAMILY_PAGE )&&
3212         IsDropFormatSupported( SOT_FORMATSTR_ID_OBJECTDESCRIPTOR ) &&
3213         !rParent.bNewByExampleDisabled )
3214     {
3215         nReturn = DND_ACTION_COPY;
3216     }
3217     return nReturn;
3218 }
3219 /*-- 10.12.2003 11:44:35---------------------------------------------------
3220 
3221   -----------------------------------------------------------------------*/
3222 sal_Int8    DropToolBox_Impl::ExecuteDrop( const ExecuteDropEvent& rEvt )
3223 {
3224      return rParent.aFmtLb.ExecuteDrop(rEvt);
3225 }
3226 
3227