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