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